From e09e645a88f37fa2a81d754cb9c86c4a584941a1 Mon Sep 17 00:00:00 2001 From: Felix Palmen Date: Mon, 20 Mar 2023 09:42:43 +0100 Subject: [PATCH] Fix build with ffmpeg 6 This doesn't attempt to replace deprecated functions, it only replaces functions that are gone (avcodec_encode_[audio|video]2) with straight-forward replacement code. It fixes the build with the latest 6.0 release and still builds fine with ffmpeg 4.4 --- src/video_capture_class.cpp.orig 2021-07-08 16:55:15 UTC +++ src/video_capture_class.cpp @@ -87,7 +87,9 @@ bool VideoCaptureClass::StartCapture(const char *filen int ret; +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) av_register_all(); +#endif // avformat_alloc_output_context2(&format_ctx, nullptr, nullptr, filename); @@ -267,7 +269,7 @@ int VideoCaptureClass::GetRecordedFrameCount() ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -void VideoCaptureClass::AddStream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id) +void VideoCaptureClass::AddStream(OutputStream *ost, AVFormatContext *oc, const AVCodec **codec, enum AVCodecID codec_id) { AVCodecContext *c; int i; @@ -363,7 +365,7 @@ void VideoCaptureClass::CloseStream(OutputStream *ost) swr_free(&ost->swr_ctx); } -bool VideoCaptureClass::OpenVideo(AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) +bool VideoCaptureClass::OpenVideo(const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) { int ret; AVCodecContext *c = ost->enc; @@ -431,7 +433,7 @@ AVFrame* VideoCaptureClass::AllocPicture(enum AVPixelF return picture; } -bool VideoCaptureClass::OpenAudio(AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) +bool VideoCaptureClass::OpenAudio(const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg) { AVCodecContext *c; int nb_samples; @@ -532,7 +534,10 @@ int VideoCaptureClass::WriteVideoFrame(AVFormatContext av_init_packet(&pkt); /* encode the image */ - ret = avcodec_encode_video2(c, &pkt, frame, &got_packet); + ret = avcodec_receive_packet(c, &pkt); + if (ret == 0) got_packet = 1; + if (ret == AVERROR(EAGAIN)) ret = 0; + if (ret == 0) ret = avcodec_send_frame(c, frame); if (ret < 0) { char err_msg[AV_ERROR_MAX_STRING_SIZE]; @@ -562,7 +567,7 @@ int VideoCaptureClass::WriteAudioFrame(AVFormatContext AVPacket pkt = {}; // data and size must be 0; AVFrame *frame; int64_t ret; - int got_packet; + int got_packet = 0; int64_t dst_nb_samples; av_init_packet(&pkt); c = ost->enc; @@ -595,7 +600,10 @@ int VideoCaptureClass::WriteAudioFrame(AVFormatContext frame->pts = av_rescale_q(ost->samples_count, AVRational{1, c->sample_rate}, c->time_base); ost->samples_count += dst_nb_samples; } - ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet); + ret = avcodec_receive_packet(c, &pkt); + if (ret == 0) got_packet = 1; + if (ret == AVERROR(EAGAIN)) ret = 0; + if (ret == 0) ret = avcodec_send_frame(c, frame); if (ret < 0) { --- src/video_capture_class.h.orig 2021-07-08 16:55:15 UTC +++ src/video_capture_class.h @@ -31,6 +31,7 @@ using namespace std; extern "C" { + #include #include #include #include @@ -84,11 +85,11 @@ class VideoCaptureClass (public) bool mutex_01; private: - void AddStream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id); + void AddStream(OutputStream *ost, AVFormatContext *oc, const AVCodec **codec, enum AVCodecID codec_id); void CloseStream(OutputStream *ost); - bool OpenVideo(AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg); + bool OpenVideo(const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg); AVFrame* AllocPicture(enum AVPixelFormat pix_fmt, int width, int height); - bool OpenAudio(AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg); + bool OpenAudio(const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg); AVFrame* AllocAudioFrame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples); int WriteFrame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt); @@ -110,9 +111,9 @@ class VideoCaptureClass (public) int video_bitrate, audio_bitrate; - AVOutputFormat *output_format; + const AVOutputFormat *output_format; OutputStream audio_stream; - AVCodec *video_codec, *audio_codec; + const AVCodec *video_codec, *audio_codec; AVDictionary *options; uint8_t* source_video_data;