@@ -21,6 +21,13 @@ extern "C" {
2121#include < libavdevice/avdevice.h>
2222}
2323
24+ // This should be dropped as soon as the major LTS linux distros switch to ffmpeg >= 59
25+ #if (LIBAVFORMAT_VERSION_MAJOR < 59)
26+ # define ff_const
27+ #else
28+ # define ff_const const
29+ #endif
30+
2431struct VideoStream {
2532 struct Demuxer {
2633 enum State : int {
@@ -113,8 +120,7 @@ struct VideoStream {
113120 // / Release (mark as available) a frame
114121 bool ReleaseFrame (int id);
115122
116- AVCodec* decoder;
117- AVCodecContext* decoder_ctx;
123+ AVCodecContext* codec_ctx;
118124 AVDictionary* dict;
119125 AVFormatContext* fmt_ctx;
120126
@@ -294,8 +300,7 @@ void VideoStream::Demuxer::Update() {
294300}
295301
296302VideoStream::VideoStream ()
297- : decoder(NULL )
298- , decoder_ctx(NULL )
303+ : codec_ctx(NULL )
299304 , dict(NULL )
300305 , fmt_ctx(NULL )
301306 , hw_pix_fmt(AV_PIX_FMT_NONE)
@@ -336,7 +341,7 @@ bool VideoStream::Open(const char* uri) {
336341 av_dict_set (&dict, " tune" , " zerolatency" , 0 );
337342 // av_dict_set(&dict, "pixel_format", "nv12", 0);
338343
339- AVInputFormat* input_format = NULL ;
344+ ff_const AVInputFormat* input_format = NULL ;
340345
341346#ifdef _WINDOWS
342347 // eat spaces
@@ -362,6 +367,7 @@ bool VideoStream::Open(const char* uri) {
362367 }
363368
364369 // grab "best" video stream.
370+ ff_const AVCodec* decoder = NULL ;
365371 video_index = av_find_best_stream (fmt_ctx, AVMEDIA_TYPE_VIDEO, -1 , -1 , &decoder, 0 );
366372 if (video_index < 0 ) {
367373 return false ;
@@ -379,20 +385,20 @@ bool VideoStream::Open(const char* uri) {
379385 }
380386 }
381387
382- decoder_ctx = avcodec_alloc_context3 (decoder);
383- if (decoder_ctx == NULL ) {
388+ codec_ctx = avcodec_alloc_context3 (decoder);
389+ if (codec_ctx == NULL ) {
384390 return false ;
385391 }
386392
387- if (decoder_ctx ->thread_count == 1 ) {
388- decoder_ctx ->thread_count = std::thread::hardware_concurrency ();
389- if (decoder_ctx ->thread_count > 8 ) {
390- decoder_ctx ->thread_count = 8 ;
393+ if (codec_ctx ->thread_count == 1 ) {
394+ codec_ctx ->thread_count = std::thread::hardware_concurrency ();
395+ if (codec_ctx ->thread_count > 8 ) {
396+ codec_ctx ->thread_count = 8 ;
391397 }
392- decoder_ctx ->thread_type = FF_THREAD_SLICE;
398+ codec_ctx ->thread_type = FF_THREAD_SLICE;
393399 }
394400
395- if (avcodec_parameters_to_context (decoder_ctx , fmt_ctx->streams [video_index]->codecpar ) < 0 ) {
401+ if (avcodec_parameters_to_context (codec_ctx , fmt_ctx->streams [video_index]->codecpar ) < 0 ) {
396402 return false ;
397403 }
398404
@@ -401,12 +407,12 @@ bool VideoStream::Open(const char* uri) {
401407 ret = av_hwdevice_ctx_create (&hw_device_ctx, config->device_type , NULL , NULL , 0 );
402408 if (ret >= 0 ) {
403409 hw_pix_fmt = config->pix_fmt ;
404- decoder_ctx ->hw_device_ctx = av_buffer_ref (hw_device_ctx);
410+ codec_ctx ->hw_device_ctx = av_buffer_ref (hw_device_ctx);
405411 }
406412 }
407413
408414 // initialize video decoder.
409- ret = avcodec_open2 (decoder_ctx , decoder, NULL );
415+ ret = avcodec_open2 (codec_ctx , decoder, NULL );
410416 if (ret < 0 ) {
411417 return false ;
412418 }
@@ -416,10 +422,10 @@ bool VideoStream::Open(const char* uri) {
416422 new_frame = true ;
417423 buffer_id = 0 ;
418424 output = av_frame_alloc ();
419- available.push_back (new uint8_t [decoder_ctx ->width * decoder_ctx ->height * 3 ]);
420- av_image_fill_arrays (output->data , output->linesize , available.front (), AV_PIX_FMT_RGB24, decoder_ctx ->width , decoder_ctx ->height , 1 );
425+ available.push_back (new uint8_t [codec_ctx ->width * codec_ctx ->height * 3 ]);
426+ av_image_fill_arrays (output->data , output->linesize , available.front (), AV_PIX_FMT_RGB24, codec_ctx ->width , codec_ctx ->height , 1 );
421427
422- avcodec_flush_buffers (decoder_ctx );
428+ avcodec_flush_buffers (codec_ctx );
423429
424430 return true ;
425431}
@@ -454,9 +460,9 @@ void VideoStream::Stop() {
454460 av_buffer_unref (&hw_device_ctx);
455461 hw_device_ctx = NULL ;
456462 }
457- if (decoder_ctx != NULL ) {
458- avcodec_free_context (&decoder_ctx );
459- decoder_ctx = NULL ;
463+ if (codec_ctx != NULL ) {
464+ avcodec_free_context (&codec_ctx );
465+ codec_ctx = NULL ;
460466 }
461467 if (fmt_ctx != NULL ) {
462468 avformat_close_input (&fmt_ctx);
@@ -517,7 +523,7 @@ void VideoStream::Loop() {
517523 demuxer.Clear ();
518524 int64_t ts = seek * AV_TIME_BASE / 1000000000L ;
519525 avformat_seek_file (fmt_ctx, -1 , INT64_MIN, ts, INT64_MAX, 0 );
520- avcodec_flush_buffers (decoder_ctx );
526+ avcodec_flush_buffers (codec_ctx );
521527 current = std::chrono::system_clock::now ();
522528 elapsed = seek.exchange (-1 ) / 1000 ;
523529 demuxer.Resume ();
@@ -528,9 +534,9 @@ void VideoStream::Loop() {
528534 continue ;
529535 }
530536
531- ret = avcodec_send_packet (decoder_ctx , packet);
537+ ret = avcodec_send_packet (codec_ctx , packet);
532538 while ((ret >= 0 ) && (state == VideoStream::Run) && (seek < 0 )) {
533- ret = avcodec_receive_frame (decoder_ctx , frame);
539+ ret = avcodec_receive_frame (codec_ctx , frame);
534540 if ((ret == AVERROR (EAGAIN)) || (ret == AVERROR_EOF)) {
535541 ret = 0 ;
536542 break ;
@@ -555,12 +561,12 @@ void VideoStream::Loop() {
555561 }
556562
557563 if (converter == NULL ) {
558- converter = sws_getContext (src->width , src->height , (AVPixelFormat)src->format , decoder_ctx ->width , decoder_ctx ->height , AV_PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL , NULL , NULL );
564+ converter = sws_getContext (src->width , src->height , (AVPixelFormat)src->format , codec_ctx ->width , codec_ctx ->height , AV_PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL , NULL , NULL );
559565 }
560566
561567 {
562568 std::lock_guard<std::mutex> lock (buffer_mutex);
563- sws_scale (converter, (const unsigned char * const *)src->data , src->linesize , 0 , decoder_ctx ->height , output->data , output->linesize );
569+ sws_scale (converter, (const unsigned char * const *)src->data , src->linesize , 0 , codec_ctx ->height , output->data , output->linesize );
564570 new_frame = true ;
565571 }
566572 }
@@ -625,9 +631,9 @@ int VideoStream::CommitFrame(uint8_t **out) {
625631
626632 // prepare new frame
627633 if (available.empty ()) {
628- available.push_back (new uint8_t [decoder_ctx ->width * decoder_ctx ->height * 3 ]);
634+ available.push_back (new uint8_t [codec_ctx ->width * codec_ctx ->height * 3 ]);
629635 }
630- av_image_fill_arrays (output->data , output->linesize , available.front (), AV_PIX_FMT_RGB24, decoder_ctx ->width , decoder_ctx ->height , 1 );
636+ av_image_fill_arrays (output->data , output->linesize , available.front (), AV_PIX_FMT_RGB24, codec_ctx ->width , codec_ctx ->height , 1 );
631637
632638 new_frame = false ;
633639
@@ -775,9 +781,9 @@ VIDEO_STREAM_API int GetFrame(VideoStreamHandle h, const void** data, int* width
775781 return 0 ;
776782 }
777783
778- *width = it->second .decoder_ctx ->width ;
779- *height = it->second .decoder_ctx ->height ;
780- *pitch = it->second .decoder_ctx ->width * 3 ;
784+ *width = it->second .codec_ctx ->width ;
785+ *height = it->second .codec_ctx ->height ;
786+ *pitch = it->second .codec_ctx ->width * 3 ;
781787 *format = VFF_RGB24;
782788 return id;
783789}
0 commit comments