Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/ffmpeg_encoder_decoder/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ std::vector<enum AVPixelFormat> get_hwframe_transfer_formats(AVBufferRef * hwfra
* finds formats that the encoder supports. Note that for VAAPI, this will just
* return AV_PIX_FMT_VAAPI since it uses hardware frames.
*/
std::vector<enum AVPixelFormat> get_encoder_formats(const AVCodec * avctx);
std::vector<enum AVPixelFormat> get_encoder_formats(
AVCodecContext * context, const AVCodec * avctx);
/**!
* picks from a vector of formats the "best" pixel format for a given encoder
*/
Expand Down
5 changes: 3 additions & 2 deletions src/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Decoder::~Decoder() { reset(); }
void Decoder::reset()
{
if (codecContext_) {
avcodec_close(codecContext_);
av_free(codecContext_);
avcodec_free_context(&codecContext_);
// avcodec_close(codecContext_);
// av_free(codecContext_);
codecContext_ = NULL;
}
if (swsContext_) {
Expand Down
7 changes: 5 additions & 2 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ static void free_frame(AVFrame ** frame)
void Encoder::closeCodec()
{
if (codecContext_) {
avcodec_close(codecContext_);
avcodec_free_context(&codecContext_);
// avcodec_close(codecContext_);
codecContext_ = nullptr;
}
free_frame(&frame_);
Expand Down Expand Up @@ -174,12 +175,14 @@ void Encoder::doOpenCodec(int width, int height)
throw(std::runtime_error("cannot find encoder: " + encoder_));
}

auto pixFmts = utils::get_encoder_formats(codec);
// allocate codec context
codecContext_ = avcodec_alloc_context3(codec);
if (!codecContext_) {
throw(std::runtime_error("cannot allocate codec context!"));
}

auto pixFmts = utils::get_encoder_formats(codecContext_, codec);

codecContext_->bit_rate = bitRate_;
codecContext_->qmax = qmax_; // 0: highest, 63: worst quality bound
codecContext_->width = width;
Expand Down
18 changes: 14 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ enum AVPixelFormat get_preferred_pixel_format(
return (AV_PIX_FMT_NONE);
}

std::vector<enum AVPixelFormat> get_encoder_formats(const AVCodec * c)
std::vector<enum AVPixelFormat> get_encoder_formats(AVCodecContext * context, const AVCodec * c)
{
std::vector<enum AVPixelFormat> formats;
if (c && c->pix_fmts) {
for (const auto * p = c->pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
formats.push_back(*p);
if (c) {
const enum AVPixelFormat * pix_fmts = nullptr;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 13, 100)
pix_fmts = c->pix_fmts;
(void)context;
#else
avcodec_get_supported_config(
context, c, AV_CODEC_CONFIG_PIX_FORMAT, 0, (const void **)&pix_fmts, NULL);
#endif
if (pix_fmts) {
for (const auto * p = pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
formats.push_back(*p);
}
}
}
return (formats);
Expand Down