Some fixes. For a now we fully support only .ts, due to huge problem with h264/hevc audio bursts
This commit is contained in:
+33
-32
@@ -47,19 +47,15 @@ public:
|
||||
explicit VideoReader(const std::string& filename) {
|
||||
if (!open_file(filename))
|
||||
return;
|
||||
if (have_video) {
|
||||
get_source_info();
|
||||
if (have_video) {
|
||||
allocate_video_conversion_buffers();
|
||||
}
|
||||
if (has_audio) {
|
||||
allocate_audio_conversion_buffers();
|
||||
}
|
||||
}
|
||||
if (!have_video && !has_audio) return;
|
||||
get_source_info();
|
||||
if (have_video) allocate_video_conversion_buffers();
|
||||
if (has_audio) allocate_audio_conversion_buffers();
|
||||
}
|
||||
|
||||
~VideoReader() {
|
||||
avcodec_free_context(&video_codec_context);
|
||||
avcodec_free_context(&audio_codec_context);
|
||||
avformat_close_input(&format_context);
|
||||
sws_freeContext(sws_video_ctx);
|
||||
swr_free(&swr_audio_ctx);
|
||||
@@ -74,7 +70,7 @@ public:
|
||||
FrameKind get_next_frame(uint8_t* video_buf, uint32_t mxl_stride, uint8_t* audio_buf, int max_audio_samples, int& out_samples_written) {
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
// Drain any frames buffered in the decoder first
|
||||
if (avcodec_receive_frame(video_codec_context, video_frame) == 0) {
|
||||
if (have_video && avcodec_receive_frame(video_codec_context, video_frame) == 0) {
|
||||
if (!video_buf) {
|
||||
av_frame_unref(video_frame);
|
||||
continue; // nowhere to write — discard frame
|
||||
@@ -104,7 +100,7 @@ public:
|
||||
return FrameKind::Video;
|
||||
}
|
||||
|
||||
if (avcodec_receive_frame(audio_codec_context, audio_frame) == 0) {
|
||||
if (has_audio && avcodec_receive_frame(audio_codec_context, audio_frame) == 0) {
|
||||
if (!audio_buf) {
|
||||
av_frame_unref(audio_frame);
|
||||
continue; // nowhere to write — discard frame
|
||||
@@ -142,7 +138,8 @@ public:
|
||||
if (av_read_frame(format_context, packet) < 0) {
|
||||
// EOF — loop back to start
|
||||
avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD);
|
||||
avcodec_flush_buffers(video_codec_context);
|
||||
if (have_video) avcodec_flush_buffers(video_codec_context);
|
||||
if (has_audio) avcodec_flush_buffers(audio_codec_context);
|
||||
swr_close(swr_audio_ctx);
|
||||
swr_init(swr_audio_ctx);
|
||||
continue;
|
||||
@@ -203,30 +200,34 @@ private:
|
||||
}
|
||||
|
||||
void get_source_info() {
|
||||
AVCodecParameters* video_codec_params = format_context->streams[video_stream_index]->codecpar;
|
||||
const AVCodec* video_codec = avcodec_find_decoder(video_codec_params->codec_id);
|
||||
if (!video_codec)
|
||||
throw std::runtime_error("Unsupported video codec");
|
||||
if (video_stream_index != -1) {
|
||||
AVCodecParameters* video_codec_params = format_context->streams[video_stream_index]->codecpar;
|
||||
const AVCodec* video_codec = avcodec_find_decoder(video_codec_params->codec_id);
|
||||
if (!video_codec)
|
||||
throw std::runtime_error("Unsupported video codec");
|
||||
|
||||
video_codec_context = avcodec_alloc_context3(video_codec);
|
||||
if (avcodec_parameters_to_context(video_codec_context, video_codec_params) < 0)
|
||||
throw std::runtime_error("Could not copy video codec parameters");
|
||||
video_codec_context = avcodec_alloc_context3(video_codec);
|
||||
if (avcodec_parameters_to_context(video_codec_context, video_codec_params) < 0)
|
||||
throw std::runtime_error("Could not copy video codec parameters");
|
||||
|
||||
if (avcodec_open2(video_codec_context, video_codec, nullptr) < 0) {
|
||||
avcodec_free_context(&video_codec_context);
|
||||
throw std::runtime_error("Could not open video codec");
|
||||
if (avcodec_open2(video_codec_context, video_codec, nullptr) < 0) {
|
||||
avcodec_free_context(&video_codec_context);
|
||||
throw std::runtime_error("Could not open video codec");
|
||||
}
|
||||
|
||||
AVRational fps = video_codec_context->framerate;
|
||||
if (fps.num == 0 || fps.den == 0)
|
||||
fps = format_context->streams[video_stream_index]->avg_frame_rate;
|
||||
|
||||
video_info.width = video_codec_context->width;
|
||||
video_info.height = video_codec_context->height;
|
||||
video_info.fps_num = fps.num;
|
||||
video_info.fps_den = fps.den;
|
||||
video_info.pix_fmt = video_codec_context->pix_fmt;
|
||||
} else {
|
||||
have_video = false;
|
||||
}
|
||||
|
||||
AVRational fps = video_codec_context->framerate;
|
||||
if (fps.num == 0 || fps.den == 0)
|
||||
fps = format_context->streams[video_stream_index]->avg_frame_rate;
|
||||
|
||||
video_info.width = video_codec_context->width;
|
||||
video_info.height = video_codec_context->height;
|
||||
video_info.fps_num = fps.num;
|
||||
video_info.fps_den = fps.den;
|
||||
video_info.pix_fmt = video_codec_context->pix_fmt;
|
||||
|
||||
// audio part
|
||||
if (audio_stream_index == -1) return;
|
||||
AVCodecParameters* audio_codec_params = format_context->streams[audio_stream_index]->codecpar;
|
||||
|
||||
Reference in New Issue
Block a user