2024-10-10 07:23:13 +00:00
|
|
|
#include "decoder.h"
|
|
|
|
|
2024-10-14 02:46:59 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2024-12-02 07:24:30 +00:00
|
|
|
AVPixelFormat Decoder::hw_pix_fmt_ = AV_PIX_FMT_NONE;
|
2024-11-17 00:00:00 +00:00
|
|
|
|
|
|
|
Decoder::Decoder() : fmt_ctx_(nullptr), dec_ctx_(nullptr), in_vstream_idx_(-1) {}
|
|
|
|
|
|
|
|
Decoder::~Decoder() {
|
|
|
|
if (dec_ctx_) {
|
|
|
|
avcodec_free_context(&dec_ctx_);
|
|
|
|
dec_ctx_ = nullptr;
|
|
|
|
}
|
|
|
|
if (fmt_ctx_) {
|
|
|
|
avformat_close_input(&fmt_ctx_);
|
|
|
|
fmt_ctx_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2024-10-10 07:23:13 +00:00
|
|
|
|
2024-12-02 07:24:30 +00:00
|
|
|
AVPixelFormat Decoder::get_hw_format(AVCodecContext *_, const AVPixelFormat *pix_fmts) {
|
|
|
|
for (const AVPixelFormat *p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
|
2024-11-17 00:00:00 +00:00
|
|
|
if (*p == hw_pix_fmt_) {
|
2024-10-10 07:23:13 +00:00
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
}
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to get HW surface format.");
|
2024-10-10 07:23:13 +00:00
|
|
|
return AV_PIX_FMT_NONE;
|
2024-10-08 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
int Decoder::init(
|
2024-10-10 07:23:13 +00:00
|
|
|
AVHWDeviceType hw_type,
|
|
|
|
AVBufferRef *hw_ctx,
|
2024-11-17 00:00:00 +00:00
|
|
|
const std::filesystem::path &in_fpath
|
2024-10-08 02:29:00 +00:00
|
|
|
) {
|
|
|
|
int ret;
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Open the input file
|
|
|
|
if ((ret = avformat_open_input(&fmt_ctx_, in_fpath.u8string().c_str(), nullptr, nullptr)) < 0) {
|
|
|
|
spdlog::error("Could not open input file '{}'", in_fpath.u8string());
|
2024-10-08 02:29:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Retrieve stream information
|
|
|
|
if ((ret = avformat_find_stream_info(fmt_ctx_, nullptr)) < 0) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to retrieve input stream information");
|
2024-10-08 02:29:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first video stream
|
2024-11-17 00:00:00 +00:00
|
|
|
ret = av_find_best_stream(fmt_ctx_, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
|
2024-10-08 02:29:00 +00:00
|
|
|
if (ret < 0) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Could not find video stream in the input file");
|
2024-10-08 02:29:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stream_index = ret;
|
2024-11-17 00:00:00 +00:00
|
|
|
AVStream *video_stream = fmt_ctx_->streams[stream_index];
|
2024-10-08 02:29:00 +00:00
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Find the decoder for the video stream
|
2024-10-10 07:23:13 +00:00
|
|
|
const AVCodec *decoder = avcodec_find_decoder(video_stream->codecpar->codec_id);
|
|
|
|
if (!decoder) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error(
|
2024-10-25 00:00:00 +00:00
|
|
|
"Failed to find decoder for codec ID {}",
|
|
|
|
static_cast<int>(video_stream->codecpar->codec_id)
|
2024-10-14 02:46:59 +00:00
|
|
|
);
|
2024-10-08 02:29:00 +00:00
|
|
|
return AVERROR_DECODER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Allocate the decoder context
|
|
|
|
dec_ctx_ = avcodec_alloc_context3(decoder);
|
|
|
|
if (!dec_ctx_) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to allocate the decoder context");
|
2024-10-08 02:29:00 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Copy codec parameters from input stream to decoder context
|
|
|
|
if ((ret = avcodec_parameters_to_context(dec_ctx_, video_stream->codecpar)) < 0) {
|
|
|
|
spdlog::error("Failed to copy decoder parameters to input decoder context");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the time base and frame rate
|
|
|
|
dec_ctx_->time_base = video_stream->time_base;
|
|
|
|
dec_ctx_->pkt_timebase = video_stream->time_base;
|
|
|
|
dec_ctx_->framerate = av_guess_frame_rate(fmt_ctx_, video_stream, nullptr);
|
|
|
|
|
2024-10-10 07:23:13 +00:00
|
|
|
// Set hardware device context
|
|
|
|
if (hw_ctx != nullptr) {
|
2024-11-17 00:00:00 +00:00
|
|
|
dec_ctx_->hw_device_ctx = av_buffer_ref(hw_ctx);
|
|
|
|
dec_ctx_->get_format = get_hw_format;
|
2024-10-10 07:23:13 +00:00
|
|
|
|
|
|
|
// Automatically determine the hardware pixel format
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
|
|
|
|
if (config == nullptr) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error(
|
|
|
|
"Decoder {} does not support device type {}.",
|
2024-10-10 07:23:13 +00:00
|
|
|
decoder->name,
|
|
|
|
av_hwdevice_get_type_name(hw_type)
|
|
|
|
);
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
|
|
|
|
config->device_type == hw_type) {
|
2024-11-17 00:00:00 +00:00
|
|
|
hw_pix_fmt_ = config->pix_fmt;
|
2024-10-10 07:23:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
// Open the decoder
|
|
|
|
if ((ret = avcodec_open2(dec_ctx_, decoder, nullptr)) < 0) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to open decoder for stream #{}", stream_index);
|
2024-10-08 02:29:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-11-17 00:00:00 +00:00
|
|
|
in_vstream_idx_ = stream_index;
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-11-17 00:00:00 +00:00
|
|
|
|
|
|
|
AVFormatContext *Decoder::get_format_context() const {
|
|
|
|
return fmt_ctx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodecContext *Decoder::get_codec_context() const {
|
|
|
|
return dec_ctx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Decoder::get_video_stream_index() const {
|
|
|
|
return in_vstream_idx_;
|
|
|
|
}
|