[FFmpeg-devel] [PATCH] lavf/utils: Fix DTS for short H264 streams.

Sasi Inguva isasi at google.com
Fri Mar 11 21:39:19 CET 2016


With this patch it doesn't produce invalid DTS for the file you mentioned
tickets/1242/sample.mkv
./ffmpeg -i sample.mkv -c:v copy -an -t 1 -f framecrc -

On Fri, Mar 11, 2016 at 12:36 PM, Sasi Inguva <isasi at google.com> wrote:

> Having another go at this. My goal is to fix DTS of H264 videos which are
> shorter than 7 frames and don't have B-frames. Currently ffmpeg will output
> DTS=N/A for such frames.
>
> On Fri, Mar 11, 2016 at 12:34 PM, Sasi Inguva <isasi at google.com> wrote:
>
>> Fill DTS if all packets have been read in avformat_find_stream_info, and
>> still
>> has_decode_delay_been_guessed returns false.
>>
>> Signed-off-by: Sasi Inguva <isasi at google.com>
>> ---
>>  libavformat/utils.c | 37 +++++++++++++++++++++++++++++++++++++
>>  1 file changed, 37 insertions(+)
>>
>> diff --git a/libavformat/utils.c b/libavformat/utils.c
>> index 5f48de1..f22309d 100644
>> --- a/libavformat/utils.c
>> +++ b/libavformat/utils.c
>> @@ -877,6 +877,7 @@ static int has_decode_delay_been_guessed(AVStream *st)
>>         avpriv_h264_has_num_reorder_frames(st->codec) ==
>> st->codec->has_b_frames)
>>          return 1;
>>  #endif
>> +
>>      if (st->codec->has_b_frames<3)
>>          return st->nb_decoded_frames >= 7;
>>      else if (st->codec->has_b_frames<4)
>> @@ -3165,6 +3166,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
>> AVDictionary **options)
>>      int64_t max_stream_analyze_duration;
>>      int64_t max_subtitle_analyze_duration;
>>      int64_t probesize = ic->probesize;
>> +    int eof_reached = 0;
>>
>>      flush_codecs = probesize > 0;
>>
>> @@ -3331,6 +3333,7 @@ int avformat_find_stream_info(AVFormatContext *ic,
>> AVDictionary **options)
>>
>>          if (ret < 0) {
>>              /* EOF or error*/
>> +            eof_reached = 1;
>>              break;
>>          }
>>
>> @@ -3454,6 +3457,40 @@ int avformat_find_stream_info(AVFormatContext *ic,
>> AVDictionary **options)
>>          count++;
>>      }
>>
>> +    if (eof_reached && ic->internal->packet_buffer) {
>> +        int stream_index;
>> +        for (stream_index = 0; stream_index < ic->nb_streams;
>> stream_index++) {
>> +            AVPacketList *pktl = ic->internal->packet_buffer;
>> +            int64_t pts_buffer[MAX_REORDER_DELAY+1];
>> +
>> +            // EOF already reached while reading the stream above.
>> +            // So continue with reoordering DTS with whatever delay we
>> have.
>> +            int codec_delay = st->codec->has_b_frames;
>> +
>> +            st = ic->streams[stream_index];
>> +
>> +            if (st->codec->codec_id != AV_CODEC_ID_H264 ||
>> +                has_decode_delay_been_guessed(st)) {
>> +                continue;
>> +            }
>> +
>> +
>> +            for (; pktl; pktl = get_next_pkt(ic, st, pktl)) {
>> +                if (pktl->pkt.stream_index != stream_index)
>> +                    continue;
>> +
>> +                if (pktl->pkt.pts != AV_NOPTS_VALUE && codec_delay <=
>> MAX_REORDER_DELAY) {
>> +                    int j;
>> +                    pts_buffer[0] = pktl->pkt.pts;
>> +                    for (j = 0; j < codec_delay && pts_buffer[j] >
>> pts_buffer[j + 1]; j++)
>> +                      FFSWAP(int64_t, pts_buffer[j], pts_buffer[j + 1]);
>> +
>> +                    pktl->pkt.dts = select_from_pts_buffer(st,
>> pts_buffer, pktl->pkt.dts);
>> +                }
>> +            }
>> +        }
>> +    }
>> +
>>      if (flush_codecs) {
>>          AVPacket empty_pkt = { 0 };
>>          int err = 0;
>> --
>> 2.7.0.rc3.207.g0ac5344
>>
>>
>


More information about the ffmpeg-devel mailing list