Bring the open2() call back into avcodec
I only need to pass the format to the custom get_format function, not this* which is even wrong if user moves the Decoder object.
This commit is contained in:
parent
2b035ddf49
commit
eae5f4cfdf
2 changed files with 30 additions and 21 deletions
|
@ -6,6 +6,25 @@ extern "C" {
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace mgs::ffmpeg {
|
namespace mgs::ffmpeg {
|
||||||
|
namespace {
|
||||||
|
::AVPixelFormat get_favourite_format_ifp (::AVCodecContext* s, const ::AVPixelFormat* fmt) {
|
||||||
|
::AVPixelFormat fav_format;
|
||||||
|
std::copy(
|
||||||
|
reinterpret_cast<const char*>(&s->opaque),
|
||||||
|
reinterpret_cast<const char*>(&s->opaque) + sizeof(::AVPixelFormat),
|
||||||
|
reinterpret_cast<char*>(&fav_format)
|
||||||
|
);
|
||||||
|
|
||||||
|
const ::AVPixelFormat* curr_fmt = fmt;
|
||||||
|
while (*curr_fmt != -1) {
|
||||||
|
if (*curr_fmt == fav_format)
|
||||||
|
return fav_format;
|
||||||
|
++curr_fmt;
|
||||||
|
}
|
||||||
|
return ::avcodec_default_get_format(s, fmt);
|
||||||
|
}
|
||||||
|
} //unnamed namespace
|
||||||
|
|
||||||
AVCodec::AVCodec (::AVCodecID id) :
|
AVCodec::AVCodec (::AVCodecID id) :
|
||||||
m_avcodec(::avcodec_find_decoder(id))
|
m_avcodec(::avcodec_find_decoder(id))
|
||||||
{
|
{
|
||||||
|
@ -27,7 +46,17 @@ Decoder AVCodec::make_decoder (unsigned int width, unsigned int height, ::AVPixe
|
||||||
|
|
||||||
context->width = width;
|
context->width = width;
|
||||||
context->height = height;
|
context->height = height;
|
||||||
context->codec = codec;
|
context->get_format = &get_favourite_format_ifp;
|
||||||
|
|
||||||
|
static_assert(sizeof(void*) >= sizeof(::AVPixelFormat));
|
||||||
|
std::copy(
|
||||||
|
reinterpret_cast<const char*>(&dst_format),
|
||||||
|
reinterpret_cast<const char*>(&dst_format) + sizeof(::AVPixelFormat),
|
||||||
|
reinterpret_cast<char*>(&context->opaque)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (::avcodec_open2(context.get(), codec, nullptr) < 0)
|
||||||
|
throw std::runtime_error("Could not open codec");
|
||||||
|
|
||||||
return {std::move(context), dst_format};
|
return {std::move(context), dst_format};
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,19 +13,6 @@ namespace mgs::ffmpeg {
|
||||||
namespace {
|
namespace {
|
||||||
constexpr int Align = 32;
|
constexpr int Align = 32;
|
||||||
|
|
||||||
::AVPixelFormat get_favourite_format_ifp (::AVCodecContext* s, const ::AVPixelFormat* fmt) {
|
|
||||||
assert(s->opaque);
|
|
||||||
|
|
||||||
const ::AVPixelFormat* curr_fmt = fmt;
|
|
||||||
const auto fav_format = static_cast<const Decoder*>(s->opaque)->destination_format();
|
|
||||||
while (*curr_fmt != -1) {
|
|
||||||
if (*curr_fmt == fav_format)
|
|
||||||
return fav_format;
|
|
||||||
++curr_fmt;
|
|
||||||
}
|
|
||||||
return ::avcodec_default_get_format(s, fmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
Decoder::Decoder (UniqueAVCodecContext&& context, ::AVPixelFormat dst_format) :
|
Decoder::Decoder (UniqueAVCodecContext&& context, ::AVPixelFormat dst_format) :
|
||||||
|
@ -35,13 +22,6 @@ Decoder::Decoder (UniqueAVCodecContext&& context, ::AVPixelFormat dst_format) :
|
||||||
m_frame2(::av_frame_alloc()),
|
m_frame2(::av_frame_alloc()),
|
||||||
m_dst_format(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;
|
Decoder::~Decoder() noexcept = default;
|
||||||
|
|
Loading…
Add table
Reference in a new issue