diff --git a/src/ffmpeg/avcodec.cpp b/src/ffmpeg/avcodec.cpp index b2e32ac..f1d4fdf 100644 --- a/src/ffmpeg/avcodec.cpp +++ b/src/ffmpeg/avcodec.cpp @@ -27,9 +27,7 @@ Decoder AVCodec::make_decoder (unsigned int width, unsigned int height, ::AVPixe context->width = width; context->height = height; - - if (::avcodec_open2(context.get(), codec, nullptr) < 0) - throw std::runtime_error("Could not open codec"); + context->codec = codec; return {std::move(context), dst_format}; } diff --git a/src/ffmpeg/decoder.cpp b/src/ffmpeg/decoder.cpp index 65378b9..288bd72 100644 --- a/src/ffmpeg/decoder.cpp +++ b/src/ffmpeg/decoder.cpp @@ -36,8 +36,12 @@ Decoder::Decoder (UniqueAVCodecContext&& context, ::AVPixelFormat dst_format) : m_dst_format(dst_format) { assert(not m_context->opaque); + assert(m_context->codec); m_context->opaque = this; m_context->get_format = &get_favourite_format_ifp; + + if (::avcodec_open2(m_context.get(), m_context->codec, nullptr) < 0) + throw std::runtime_error("Could not open codec"); } Decoder::~Decoder() noexcept = default;