Opened 7 years ago

Closed 7 years ago

#6024 closed defect (fixed)

avcodec_send_packet fails to decode mpeg2 correctly

Reported by: Pavel Koshevoy Owned by:
Priority: normal Component: avcodec
Version: git-master Keywords: truncated
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no


Summary of the bug:
How to reproduce:

$ ./ffmpeg_g -flags truncated -i "" -c:v ffv1 -c:a copy -y /tmp/truncated.nut
ffmpeg version N-82861-ged412d2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (SUSE Linux)
  configuration: --prefix=/Developer/x86_64 --prefix=/Developer/x86_64 --enable-runtime-cpudetect --enable-libass --enable-libmodplug --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-gnutls --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-debug=3 --disable-optimizations --disable-stripping --enable-pic --disable-static --enable-shared --enable-rpath
  libavutil      55. 43.100 / 55. 43.100
  libavcodec     57. 68.100 / 57. 68.100
  libavformat    57. 60.100 / 57. 60.100
  libavdevice    57.  2.100 / 57.  2.100
  libavfilter     6. 68.100 /  6. 68.100
  libswscale      4.  3.101 /  4.  3.101
  libswresample   2.  4.100 /  2.  4.100
  libpostproc    54.  2.100 / 54.  2.100
[mp3 @ 0x1b10680] Header missing
[mpeg2video @ 0x1b0f7a0] Invalid frame dimensions 0x0.
    Last message repeated 3 times
[mpegts @ 0x1b09a40] PES packet size mismatch
Input #0, mpegts, from '':
  Duration: 00:00:06.53, start: 17163.308933, bitrate: 9217 kb/s
    Stream #0:0[0x1062]: Video: mpeg2video (4:2:2), yuv422p(tv, top first), 720x576 [SAR 16:15 DAR 4:3], 9000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0:1[0x1063]: Audio: mp2, 48000 Hz, stereo, s16p, 384 kb/s
Output #0, nut, to '/tmp/truncated.nut':
    encoder         : Lavf57.60.100
    Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv422p, 720x576 [SAR 16:15 DAR 4:3], q=2-31, 200 kb/s, 25 fps, 51200 tbn, 25 tbc
      encoder         : Lavc57.68.100 ffv1
    Stream #0:1: Audio: mp2 (P[0][0][0] / 0x0050), 48000 Hz, stereo, s16p, 384 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg2video (native) -> ffv1 (native))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mpegts @ 0x1b09a40] PES packet size mismatchme=00:00:05.60 bitrate=28272.5kbits/s dup=0 drop=1 speed=1.31x    
frame=   71 fps= 15 q=-0.0 Lsize=   22548kB time=00:00:06.32 bitrate=29226.9kbits/s dup=0 drop=2 speed=1.33x    
video:22254kB audio:292kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.008866%

However, if you remove the -flags truncated parameter it decodes correctly.

This was discovered while porting from deprecated avcodec_decode_video2 API to the new avcodec_send_packet/avcodec_receive_frame API. The new avcodec_send_packet API specifies that the packet will be consumed completely when it returns 0. However, that does not appear to be the case when the decoder has the AV_CODEC_FLAG_TRUNCATED flag turned on.

Using the deprecated avcodec_decode_video2 API workflow the packet would be resubmitted until all data is consumed, therefore the deprecated API works correctly even when AV_CODEC_FLAG_TRUNCATED is turned on.

Change History (4)

comment:2 by Hendrik, 7 years ago

Is there any particular reason you are even using the truncated flag? Its mostly a deprecated concept that works on an extremely small subset of decoders and might disappear in the future, as the decode API doesn't specify it.

comment:3 by Pavel Koshevoy, 7 years ago

The code that broke predates me by many years .. I don't know what the original motivation for using the truncated flag was. Probably it was set in the hope of greater resiliency against dropped or damaged packets. This is how it was set in the affected codebase:

    ctx = avcodec_alloc_context3(codec);
    if (codec->capabilities & CODEC_CAP_TRUNCATED)
        ctx->flags |= CODEC_FLAG_TRUNCATED; // we do not send complete frames


There may be other decoders that do not implement the new API internally and may be similarly afflicted, I don't think this is limited to mpeg2 decoder alone

comment:4 by Carl Eugen Hoyos, 7 years ago

Component: undeterminedavcodec
Keywords: avcodec_send_packet avcodec_send_packet flags removed
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.