Skip to content

Commit 0d85222

Browse files
Fix deprecated libavcodec (#1)
* fix deprecated features for libav 7 * fix formatting errors
1 parent abb0c49 commit 0d85222

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

include/ffmpeg_encoder_decoder/utils.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ std::vector<enum AVPixelFormat> get_hwframe_transfer_formats(AVBufferRef * hwfra
5656
* finds formats that the encoder supports. Note that for VAAPI, this will just
5757
* return AV_PIX_FMT_VAAPI since it uses hardware frames.
5858
*/
59-
std::vector<enum AVPixelFormat> get_encoder_formats(const AVCodec * avctx);
59+
std::vector<enum AVPixelFormat> get_encoder_formats(
60+
AVCodecContext * context, const AVCodec * avctx);
6061
/**!
6162
* picks from a vector of formats the "best" pixel format for a given encoder
6263
*/

src/decoder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ Decoder::~Decoder() { reset(); }
3838
void Decoder::reset()
3939
{
4040
if (codecContext_) {
41-
avcodec_close(codecContext_);
42-
av_free(codecContext_);
41+
avcodec_free_context(&codecContext_);
42+
// avcodec_close(codecContext_);
43+
// av_free(codecContext_);
4344
codecContext_ = NULL;
4445
}
4546
if (swsContext_) {

src/encoder.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ static void free_frame(AVFrame ** frame)
5454
void Encoder::closeCodec()
5555
{
5656
if (codecContext_) {
57-
avcodec_close(codecContext_);
57+
avcodec_free_context(&codecContext_);
58+
// avcodec_close(codecContext_);
5859
codecContext_ = nullptr;
5960
}
6061
free_frame(&frame_);
@@ -174,12 +175,14 @@ void Encoder::doOpenCodec(int width, int height)
174175
throw(std::runtime_error("cannot find encoder: " + encoder_));
175176
}
176177

177-
auto pixFmts = utils::get_encoder_formats(codec);
178178
// allocate codec context
179179
codecContext_ = avcodec_alloc_context3(codec);
180180
if (!codecContext_) {
181181
throw(std::runtime_error("cannot allocate codec context!"));
182182
}
183+
184+
auto pixFmts = utils::get_encoder_formats(codecContext_, codec);
185+
183186
codecContext_->bit_rate = bitRate_;
184187
codecContext_->qmax = qmax_; // 0: highest, 63: worst quality bound
185188
codecContext_->width = width;

src/utils.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@ enum AVPixelFormat get_preferred_pixel_format(
101101
return (AV_PIX_FMT_NONE);
102102
}
103103

104-
std::vector<enum AVPixelFormat> get_encoder_formats(const AVCodec * c)
104+
std::vector<enum AVPixelFormat> get_encoder_formats(AVCodecContext * context, const AVCodec * c)
105105
{
106106
std::vector<enum AVPixelFormat> formats;
107-
if (c && c->pix_fmts) {
108-
for (const auto * p = c->pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
109-
formats.push_back(*p);
107+
if (c) {
108+
const enum AVPixelFormat * pix_fmts = nullptr;
109+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 13, 100)
110+
pix_fmts = c->pix_fmts;
111+
(void)context;
112+
#else
113+
avcodec_get_supported_config(
114+
context, c, AV_CODEC_CONFIG_PIX_FORMAT, 0, (const void **)&pix_fmts, NULL);
115+
#endif
116+
if (pix_fmts) {
117+
for (const auto * p = pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
118+
formats.push_back(*p);
119+
}
110120
}
111121
}
112122
return (formats);

0 commit comments

Comments
 (0)