@@ -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.
189189static 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}
364369static 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)
531536static 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