From 5aabee89967cf367e4128eedd60c8916459042c7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:14:53 +0200 Subject: decoder/ffmpeg: add local variable "av_stream" Code simplification. --- src/decoder/ffmpeg_decoder_plugin.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index 70628df9d..9e8f2c00d 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -375,8 +375,9 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) return; } - AVCodecContext *codec_context = - format_context->streams[audio_stream]->codec; + AVStream *av_stream = format_context->streams[audio_stream]; + + AVCodecContext *codec_context = av_stream->codec; if (codec_context->codec_name[0] != 0) g_debug("codec '%s'", codec_context->codec_name); @@ -427,7 +428,7 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) if (packet.stream_index == audio_stream) cmd = ffmpeg_send_packet(decoder, input, &packet, codec_context, - &format_context->streams[audio_stream]->time_base); + &av_stream->time_base); else cmd = decoder_get_command(decoder); -- cgit v1.2.3 From 9aa91e0f171d63015a70b0f74335333bb4735763 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:03:03 +0200 Subject: decoder/ffmpeg: move formula to time_from_ffmpeg() --- src/decoder/ffmpeg_decoder_plugin.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index 9e8f2c00d..484796fe3 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -214,6 +214,15 @@ align16(void *p, size_t *length_p) return (char *)p + add; } +G_GNUC_CONST +static double +time_from_ffmpeg(int64_t t, const AVRational time_base) +{ + assert(t != (int64_t)AV_NOPTS_VALUE); + + return av_rescale_q(t, time_base, (AVRational){1, 1}); +} + static enum decoder_command ffmpeg_send_packet(struct decoder *decoder, struct input_stream *is, const AVPacket *packet, @@ -222,8 +231,7 @@ ffmpeg_send_packet(struct decoder *decoder, struct input_stream *is, { if (packet->pts != (int64_t)AV_NOPTS_VALUE) decoder_timestamp(decoder, - av_rescale_q(packet->pts, *time_base, - (AVRational){1, 1})); + time_from_ffmpeg(packet->pts, *time_base)); #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52,25,0) AVPacket packet2 = *packet; -- cgit v1.2.3 From 42d8c2981f4e76fab5c4c3ed6ff840df7a41a1a4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:07:32 +0200 Subject: decoder/ffmpeg: higher precision timestamps --- src/decoder/ffmpeg_decoder_plugin.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index 484796fe3..1a1598f36 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -220,7 +220,8 @@ time_from_ffmpeg(int64_t t, const AVRational time_base) { assert(t != (int64_t)AV_NOPTS_VALUE); - return av_rescale_q(t, time_base, (AVRational){1, 1}); + return (double)av_rescale_q(t, time_base, (AVRational){1, 1024}) + / (double)1024; } static enum decoder_command -- cgit v1.2.3 From 724a59aaf7c396845bb5de7ad815c7ca842c2778 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:32:29 +0200 Subject: decoder/ffmpeg: don't require key frame for seeking Use flag AV_TIME_BASE. --- src/decoder/ffmpeg_decoder_plugin.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index 1a1598f36..f8c9db700 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -447,7 +447,8 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) int64_t where = decoder_seek_where(decoder) * AV_TIME_BASE; - if (av_seek_frame(format_context, -1, where, 0) < 0) + if (av_seek_frame(format_context, -1, where, + AV_TIME_BASE) < 0) decoder_seek_error(decoder); else decoder_command_finished(decoder); -- cgit v1.2.3 From ce35ba9ac99ed96c7f55c6d80e85fb9bfe1ed09e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:12:53 +0200 Subject: decoder/ffmpeg: explicitly specify the current stream for seeking Use AVStream.time_base to convert the decoder_seek_where() value, and pass the current stream number to av_seek_frame(). --- src/decoder/ffmpeg_decoder_plugin.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index f8c9db700..77c1a122f 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -224,6 +224,14 @@ time_from_ffmpeg(int64_t t, const AVRational time_base) / (double)1024; } +G_GNUC_CONST +static int64_t +time_to_ffmpeg(double t, const AVRational time_base) +{ + return av_rescale_q((int64_t)(t * 1024), (AVRational){1, 1024}, + time_base); +} + static enum decoder_command ffmpeg_send_packet(struct decoder *decoder, struct input_stream *is, const AVPacket *packet, @@ -445,9 +453,10 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) if (cmd == DECODE_COMMAND_SEEK) { int64_t where = - decoder_seek_where(decoder) * AV_TIME_BASE; + time_to_ffmpeg(decoder_seek_where(decoder), + av_stream->time_base); - if (av_seek_frame(format_context, -1, where, + if (av_seek_frame(format_context, audio_stream, where, AV_TIME_BASE) < 0) decoder_seek_error(decoder); else -- cgit v1.2.3 From 2ed870c8546ce4654f956beea60546260d98a85c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 15 Sep 2011 21:41:25 +0200 Subject: decoder/ffmpeg: flush the codec after seeking Let the codec start with fresh buffers. This should fix the remaining seeking issues. --- src/decoder/ffmpeg_decoder_plugin.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/decoder') diff --git a/src/decoder/ffmpeg_decoder_plugin.c b/src/decoder/ffmpeg_decoder_plugin.c index 77c1a122f..ba47b2c2c 100644 --- a/src/decoder/ffmpeg_decoder_plugin.c +++ b/src/decoder/ffmpeg_decoder_plugin.c @@ -459,8 +459,10 @@ ffmpeg_decode(struct decoder *decoder, struct input_stream *input) if (av_seek_frame(format_context, audio_stream, where, AV_TIME_BASE) < 0) decoder_seek_error(decoder); - else + else { + avcodec_flush_buffers(codec_context); decoder_command_finished(decoder); + } } } while (cmd != DECODE_COMMAND_STOP); -- cgit v1.2.3