Skip to content

Commit b111be9

Browse files
committed
Fix ffmpeg plugin
1 parent e65759b commit b111be9

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

plugins/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ $(PLUG_PREFIX)ffmpeg$(PLUG_NATIVE_EXT):
238238
$(CC) $(BASE_CFLAGS) $(CFLAGS) -DFTEPLUGIN -s -o $@ -shared $(PLUG_CFLAGS) $(AV_CFLAGS) $(AVPLUG_OBJS) $(PLUG_DEFFILE) $(PLUG_LDFLAGS) $(AV_LDFLAGS)
239239
$(call EMBEDMETA,ffmpeg,$@,FFMPEG Video Decoding Plugin,Provides support for more audio formats as well as video playback and better capture support.)
240240
endif
241-
241+
NATIVE_PLUGINS+=ffmpeg
242242
######################################
243243

244244
######################################

plugins/avplug/avaudio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void S_AV_Purge(sfx_t *s)
4545

4646
// Free the audio decoder
4747
if (ctx->pACodecCtx)
48-
avcodec_close(ctx->pACodecCtx);
48+
avcodec_free_context(&ctx->pACodecCtx);
4949
av_free(ctx->pAFrame);
5050

5151
// Close the video file
@@ -70,9 +70,9 @@ static void S_AV_Purge(sfx_t *s)
7070
static void S_AV_ReadFrame(struct avaudioctx *ctx)
7171
{ //reads an audioframe and spits its data into the output sound file for the game engine to use.
7272
qaudiofmt_t outformat = QAF_S16, informat=QAF_S16;
73-
int channels = ctx->pACodecCtx->channels;
73+
int channels = ctx->pACodecCtx->ch_layout.nb_channels;
7474
int planes = 1, p;
75-
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
75+
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->ch_layout.nb_channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
7676
switch(ctx->pACodecCtx->sample_fmt)
7777
{ //we don't support planar audio. we just treat it as mono instead.
7878
default:
@@ -413,7 +413,7 @@ static qboolean QDECL S_LoadAVSound (sfx_t *s, qbyte *data, size_t datalen, int
413413
{
414414
struct avaudioctx *ctx;
415415
int i;
416-
AVCodec *pCodec;
416+
const AVCodec *pCodec;
417417
const int iBufSize = 4 * 1024;
418418

419419
if (!ffmpeg_audiodecoder)

plugins/avplug/avdecode.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ static void AVDec_Destroy(void *vctx)
126126
// Free the video stuff
127127
av_free(ctx->rgb_data);
128128
if (ctx->pVCodecCtx)
129-
avcodec_close(ctx->pVCodecCtx);
129+
avcodec_free_context(&ctx->pVCodecCtx);
130130
av_free(ctx->pVFrame);
131131

132132
// Free the audio decoder
133133
if (ctx->pACodecCtx)
134-
avcodec_close(ctx->pACodecCtx);
134+
avcodec_free_context(&ctx->pACodecCtx);
135135
av_free(ctx->pAFrame);
136136

137137
// Close the video file
@@ -148,7 +148,7 @@ static void *AVDec_Create(const char *medianame)
148148
struct decctx *ctx;
149149

150150
unsigned int i;
151-
AVCodec *pCodec;
151+
const AVCodec *pCodec;
152152
qboolean useioctx = false;
153153
// const char *extension = strrchr(medianame, '.');
154154

@@ -370,8 +370,8 @@ static qboolean VARGS AVDec_DisplayFrame(void *vctx, qboolean nosound, qboolean
370370
while(0==avcodec_receive_frame(ctx->pACodecCtx, ctx->pAFrame))
371371
{
372372
int width = 2;
373-
int channels = ctx->pACodecCtx->channels;
374-
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
373+
int channels = ctx->pACodecCtx->ch_layout.nb_channels;
374+
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->ch_layout.nb_channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
375375
void *auddata = ctx->pAFrame->data[0];
376376
switch(ctx->pACodecCtx->sample_fmt)
377377
{
@@ -534,8 +534,8 @@ static qboolean VARGS AVDec_DisplayFrame(void *vctx, qboolean nosound, qboolean
534534
if (okay)
535535
{
536536
int width = 2;
537-
int channels = ctx->pACodecCtx->channels;
538-
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
537+
int channels = ctx->pACodecCtx->ch_layout.nb_channels;
538+
unsigned int auddatasize = av_samples_get_buffer_size(NULL, ctx->pACodecCtx->ch_layout.nb_channels, ctx->pAFrame->nb_samples, ctx->pACodecCtx->sample_fmt, 1);
539539
void *auddata = ctx->pAFrame->data[0];
540540
switch(ctx->pACodecCtx->sample_fmt)
541541
{

plugins/avplug/avencode.c

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static AVFrame *alloc_frame(enum AVPixelFormat pix_fmt, int width, int height)
108108
picture->height = height;
109109
return picture;
110110
}
111-
static AVStream *add_video_stream(struct encctx *ctx, AVCodec *codec, int fps, int width, int height)
111+
static AVStream *add_video_stream(struct encctx *ctx, const AVCodec *codec, int fps, int width, int height)
112112
{
113113
AVCodecContext *c;
114114
AVStream *st;
@@ -175,7 +175,7 @@ static void close_video(struct encctx *ctx)
175175
if (!ctx->video_st)
176176
return;
177177

178-
avcodec_close(ctx->video_codec);
178+
avcodec_free_context(&ctx->video_codec);
179179
if (ctx->picture)
180180
{
181181
av_free(ctx->picture->data[0]);
@@ -188,32 +188,32 @@ static void close_video(struct encctx *ctx)
188188
//frame can be null on eof.
189189
static void AVEnc_DoEncode(AVFormatContext *fc, AVStream *stream, AVCodecContext *codec, AVFrame *frame)
190190
{
191-
AVPacket pkt;
191+
AVPacket *pkt = av_packet_alloc();
192192
int err = avcodec_send_frame(codec, frame);
193193
if (err)
194194
{
195195
char buf[512];
196196
Con_Printf("avcodec_send_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
197197
}
198198

199-
av_init_packet(&pkt);
200-
while (!(err=avcodec_receive_packet(codec, &pkt)))
199+
while (!(err=avcodec_receive_packet(codec, pkt)))
201200
{
202-
av_packet_rescale_ts(&pkt, codec->time_base, stream->time_base);
203-
pkt.stream_index = stream->index;
204-
err = av_interleaved_write_frame(fc, &pkt);
201+
av_packet_rescale_ts(pkt, codec->time_base, stream->time_base);
202+
pkt->stream_index = stream->index;
203+
err = av_interleaved_write_frame(fc, pkt);
205204
if (err)
206205
{
207206
char buf[512];
208207
Con_Printf("av_interleaved_write_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
209208
}
210-
av_packet_unref(&pkt);
209+
av_packet_unref(pkt);
211210
}
212211
if (err && err != AVERROR(EAGAIN) && err != AVERROR_EOF)
213212
{
214213
char buf[512];
215214
Con_Printf("avcodec_receive_packet: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
216215
}
216+
av_packet_free(&pkt);
217217
}
218218
#endif
219219

@@ -266,41 +266,43 @@ static void AVEnc_Video (void *vctx, int frameno, void *data, int bytestride, in
266266
AVEnc_DoEncode(ctx->fc, ctx->video_st, ctx->video_codec, ctx->picture);
267267
#else
268268
{
269-
AVPacket pkt;
270269
int success;
271270
int err;
271+
AVPacket *pkt = av_packet_alloc();
272272

273-
av_init_packet(&pkt);
274-
pkt.data = ctx->video_outbuf;
275-
pkt.size = ctx->video_outbuf_size;
273+
pkt->data = ctx->video_outbuf;
274+
pkt->size = ctx->video_outbuf_size;
276275
success = 0;
277-
err = avcodec_encode_video2(ctx->video_codec, &pkt, ctx->picture, &success);
276+
err = avcodec_encode_video2(ctx->video_codec, pkt, ctx->picture, &success);
278277
if (err)
279278
{
280279
char buf[512];
281280
Con_Printf("avcodec_encode_video2: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
282281
}
283282
else if (err == 0 && success)
284283
{
285-
av_packet_rescale_ts(&pkt, ctx->video_codec->time_base, ctx->video_st->time_base);
286-
pkt.stream_index = ctx->video_st->index;
287-
err = av_interleaved_write_frame(ctx->fc, &pkt);
284+
av_packet_rescale_ts(pkt, ctx->video_codec->time_base, ctx->video_st->time_base);
285+
pkt->stream_index = ctx->video_st->index;
286+
err = av_interleaved_write_frame(ctx->fc, pkt);
288287

289288
if (err)
290289
{
291290
char buf[512];
292291
Con_Printf("av_interleaved_write_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
293292
}
294293
}
294+
av_packet_free(&pkt);
295295
}
296296
#endif
297297
}
298298

299-
static AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int *samplerate, int *bits, int channels)
299+
static AVStream *add_audio_stream(struct encctx *ctx, const AVCodec *codec, int *samplerate, int *bits, int channels)
300300
{
301301
AVCodecContext *c;
302302
AVStream *st;
303303
int bitrate = ffmpeg_audiobitrate->value;
304+
int num_sample_fmts = 0;
305+
enum AVSampleFormat *sample_fmts;
304306

305307
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
306308
st = avformat_new_stream(ctx->fc, codec);
@@ -331,9 +333,12 @@ static AVStream *add_audio_stream(struct encctx *ctx, AVCodec *codec, int *sampl
331333
c->time_base.num = 1;
332334
c->time_base.den = *samplerate;
333335
c->sample_rate = *samplerate;
334-
c->channels = channels;
335-
c->channel_layout = av_get_default_channel_layout(c->channels);
336-
c->sample_fmt = codec->sample_fmts[0];
336+
c->ch_layout.nb_channels = channels;
337+
av_channel_layout_default(&c->ch_layout, channels);
338+
339+
avcodec_get_supported_config(c, codec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, (const void **)&sample_fmts, &num_sample_fmts);
340+
341+
c->sample_fmt = sample_fmts[0];
337342

338343
// if (c->sample_fmt == AV_SAMPLE_FMT_FLTP || c->sample_fmt == AV_SAMPLE_FMT_FLT)
339344
// *bits = 32; //get the engine to mix 32bit audio instead of whatever its currently set to.
@@ -359,7 +364,7 @@ static void close_audio(struct encctx *ctx)
359364
if (!ctx->audio_st)
360365
return;
361366

362-
avcodec_close(ctx->audio_codec);
367+
avcodec_free_context(&ctx->audio_codec);
363368
}
364369
static void AVEnc_Audio (void *vctx, void *data, int bytes)
365370
{
@@ -370,7 +375,7 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
370375

371376
while (bytes)
372377
{
373-
int i, p, chans = ctx->audio_codec->channels;
378+
int i, p, chans = ctx->audio_codec->ch_layout.nb_channels;
374379
int blocksize = sizeof(float)*chans;
375380
int count = bytes / blocksize;
376381
int planesize = ctx->audio_codec->frame_size;
@@ -485,7 +490,7 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
485490
}
486491

487492
ctx->audio->nb_samples = ctx->audio_outcount;
488-
avcodec_fill_audio_frame(ctx->audio, ctx->audio_codec->channels, ctx->audio_codec->sample_fmt, ctx->audio_outbuf, av_get_bytes_per_sample(ctx->audio_codec->sample_fmt)*ctx->audio_outcount*ctx->audio_codec->channels, 1);
493+
avcodec_fill_audio_frame(ctx->audio, ctx->audio_codec->ch_layout.nb_channels, ctx->audio_codec->sample_fmt, ctx->audio_outbuf, av_get_bytes_per_sample(ctx->audio_codec->sample_fmt)*ctx->audio_outcount*ctx->audio_codec->ch_layout.nb_channels, 1);
489494
ctx->audio->pts = ctx->audio_pts;
490495
ctx->audio_pts += ctx->audio_outcount;
491496
ctx->audio_outcount = 0;
@@ -494,14 +499,13 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
494499
AVEnc_DoEncode(ctx->fc, ctx->audio_st, ctx->audio_codec, ctx->audio);
495500
#else
496501
{
497-
AVPacket pkt;
498502
int success;
499503
int err;
500-
av_init_packet(&pkt);
501-
pkt.data = NULL;
502-
pkt.size = 0;
504+
AVPacket *pkt = av_packet_alloc();
505+
pkt->data = NULL;
506+
pkt->size = 0;
503507
success = 0;
504-
err = avcodec_encode_audio2(ctx->audio_codec, &pkt, ctx->audio, &success);
508+
err = avcodec_encode_audio2(ctx->audio_codec, pkt, ctx->audio, &success);
505509

506510
if (err)
507511
{
@@ -514,15 +518,16 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
514518
// if(ctx->audio_codec->coded_frame->key_frame)
515519
// pkt.flags |= AV_PKT_FLAG_KEY;
516520

517-
av_packet_rescale_ts(&pkt, ctx->audio_codec->time_base, ctx->audio_st->time_base);
518-
pkt.stream_index = ctx->audio_st->index;
519-
err = av_interleaved_write_frame(ctx->fc, &pkt);
521+
av_packet_rescale_ts(pkt, ctx->audio_codec->time_base, ctx->audio_st->time_base);
522+
pkt->stream_index = ctx->audio_st->index;
523+
err = av_interleaved_write_frame(ctx->fc, pkt);
520524
if (err)
521525
{
522526
char buf[512];
523527
Con_Printf("av_interleaved_write_frame: error: %s\n", av_make_error_string(buf, sizeof(buf), err));
524528
}
525529
}
530+
av_packet_free(&pkt);
526531
}
527532
#endif
528533
}
@@ -531,9 +536,9 @@ static void AVEnc_Audio (void *vctx, void *data, int bytes)
531536
static void *AVEnc_Begin (char *streamname, int videorate, int width, int height, int *sndkhz, int *sndchannels, int *sndbits)
532537
{
533538
struct encctx *ctx;
534-
AVOutputFormat *fmt = NULL;
535-
AVCodec *videocodec = NULL;
536-
AVCodec *audiocodec = NULL;
539+
const AVOutputFormat *fmt = NULL;
540+
const AVCodec *videocodec = NULL;
541+
const AVCodec *audiocodec = NULL;
537542
int err;
538543
char errtxt[AV_ERROR_MAX_STRING_SIZE] = {0};
539544

@@ -628,6 +633,7 @@ static void *AVEnc_Begin (char *streamname, int videorate, int width, int height
628633

629634
//pick default codecs
630635
ctx->video_st = NULL;
636+
ctx->audio_st = NULL;
631637
if (videocodec)
632638
ctx->video_st = add_video_stream(ctx, videocodec, videorate, width, height);
633639
if (audiocodec)
@@ -677,7 +683,7 @@ static void *AVEnc_Begin (char *streamname, int videorate, int width, int height
677683
sz = ctx->audio_codec->frame_size;
678684
if (!sz)
679685
sz = VARIABLE_AUDIO_FRAME_MAX_SIZE;
680-
sz *= av_get_bytes_per_sample(ctx->audio_codec->sample_fmt) * ctx->audio_codec->channels;
686+
sz *= av_get_bytes_per_sample(ctx->audio_codec->sample_fmt) * ctx->audio_codec->ch_layout.nb_channels;
681687
ctx->audio_outbuf = av_malloc(sz);
682688

683689
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)

0 commit comments

Comments
 (0)