Opened 11 years ago

Closed 9 years ago

#2975 closed defect (fixed)

h264 streams have wrong timestamps when muxed with Matroska

Reported by: jamal Owned by:
Priority: important Component: avformat
Version: git-master Keywords: h264 mkv regression
Cc: iam@valdikss.org.ru, barderne@gmail.com Blocked By:
Blocking: Reproduced by developer: yes
Analyzed by developer: no

Description

Encoding with ffmpeg/libx264, then checking the output with mkvinfo:

$ ./ffmpeg -f lavfi -i testsrc -t 10 -c:v libx264 -preset veryfast test.mkv
ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfreetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --arch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, lavfi, from 'testsrc':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 320x240 [SAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0000000001C53860] using SAR=1/1
[libx264 @ 0000000001C53860] using cpu capabilities: MMX2 SSE2Fast SSEMisalign LZCNT
[libx264 @ 0000000001C53860] profile High 4:4:4 Predictive, level 1.3, 4:4:4 8-bit
[libx264 @ 0000000001C53860] 264 - core 133 r2339 585324f - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=1 r
ef=1 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pski
p=1 chroma_qp_offset=6 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_a
dapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=10 rc=crf mbtree=1 crf=23.0 qcomp=0.60
 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, matroska, to 'test.mkv':
  Metadata:
    encoder         : Lavf55.18.100
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv444p, 320x240 [SAR 1:1 DAR 4:3], q=-1--1, 1k tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
frame=  250 fps=0.0 q=-1.0 Lsize=      51kB time=00:00:09.92 bitrate=  42.3kbits/s
video:49kB audio:0kB subtitle:0 global headers:0kB muxing overhead 4.321356%
[libx264 @ 0000000001C53860] frame I:1     Avg QP:24.54  size:  2863
[libx264 @ 0000000001C53860] frame P:77    Avg QP:15.07  size:   488
[libx264 @ 0000000001C53860] frame B:172   Avg QP:15.11  size:    53
[libx264 @ 0000000001C53860] consecutive B-frames:  8.0%  0.8%  0.0% 91.2%
[libx264 @ 0000000001C53860] mb I  I16..4: 65.0%  9.7% 25.3%
[libx264 @ 0000000001C53860] mb P  I16..4:  3.7%  1.6%  0.4%  P16..4:  7.0%  3.9%  1.2%  0.0%  0.0%    skip:82.2%
[libx264 @ 0000000001C53860] mb B  I16..4:  0.5%  0.1%  0.0%  B16..8:  1.0%  0.1%  0.0%  direct: 0.4%  skip:97.9%  L0:46.8% L1:52.7% BI: 0.5%
[libx264 @ 0000000001C53860] 8x8 transform intra:22.2% inter:24.4%
[libx264 @ 0000000001C53860] coded y,u,v intra: 10.4% 16.1% 12.1% inter: 0.8% 0.9% 0.7%
[libx264 @ 0000000001C53860] i16 v,h,dc,p: 48% 42%  1%  9%
[libx264 @ 0000000001C53860] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 90%  2%  7%  0%  0% 0%  0%  0%  0%
[libx264 @ 0000000001C53860] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 47% 22%  1%  1% 1%  0%  1%  1%
[libx264 @ 0000000001C53860] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0000000001C53860] kb/s:39.63

$ ./ffmpeg -i test.mkv
ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfreetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --arch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, matroska,webm, from 'test.mkv':
  Metadata:
    ENCODER         : Lavf55.18.100
  Duration: 00:00:10.08, start: 0.000000, bitrate: 41 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 320x240 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
At least one output file must be specified

$ mkvinfo -v test.mkv
+ EBML head
|+ EBML version: 1
|+ EBML read version: 1
|+ EBML maximum ID length: 4
|+ EBML maximum size length: 8
|+ Doc type: matroska
|+ Doc type version: 4
|+ Doc type read version: 2
+ Segment, size 52375
|+ Seek head (subentries will be skipped)
|+ EbmlVoid (size: 149)
|+ Segment information
| + Timecode scale: 1000000
| + Muxing application: Lavf55.18.100
| + Writing application: Lavf55.18.100
| + Segment UID: 0xe8 0x3c 0x24 0x06 0x4a 0xdf 0x9d 0xd3 0xd9 0x3c 0x3c 0xaa 0x5c 0xd4 0x46 0x42
| + Duration: 10.080s (00:00:10.080)
|+ Segment tracks
| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 1
|  + Lacing flag: 0
|  + Language: und
|  + Codec ID: V_MPEG4/ISO/AVC
|  + Track type: video
|  + Default duration: 40.000ms (25.000 frames/fields per second for a video track)
|  + Video track
|   + Pixel width: 320
|   + Pixel height: 240
|   + Display width: 320
|   + Display height: 240
|  + CodecPrivate, length 42 (h.264 profile: High 4:4:4 Predictive @L1.3)
|+ Tags
| + Tag
|  + Targets
|  + Simple
|   + Name: ENCODER
|   + String: Lavf55.18.100
|+ Cluster
| + Cluster timecode: 0.080s
| + SimpleBlock (key, track number 1, 1 frame(s), timecode 0.080s = 00:00:00.080)
|  + Frame with size 3552
| + SimpleBlock (track number 1, 1 frame(s), timecode 0.240s = 00:00:00.240)
|  + Frame with size 556

[...]

Notice the duration of the file and timecode of the first block.

Rerunning the above command but muxing instead to mp4 (So ffmpeg's matroska muxer is not part of the equation), then remuxing with mkvtoolnix, then looking at the result with mkvinfo:

$ ./ffmpeg -f lavfi -i testsrc -t 10 -c:v libx264 -preset veryfast test_toolnix.mp4

ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfreetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --arch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, lavfi, from 'testsrc':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 320x240 [SAR 1:1 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0000000000693640] using SAR=1/1
[libx264 @ 0000000000693640] using cpu capabilities: MMX2 SSE2Fast SSEMisalign LZCNT
[libx264 @ 0000000000693640] profile High 4:4:4 Predictive, level 1.3, 4:4:4 8-bit
[libx264 @ 0000000000693640] 264 - core 133 r2339 585324f - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=1 r
ef=1 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pski
p=1 chroma_qp_offset=6 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_a
dapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=10 rc=crf mbtree=1 crf=23.0 qcomp=0.60
 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'test_toolnix.mp4':
  Metadata:
    encoder         : Lavf55.18.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv444p, 320x240 [SAR 1:1 DAR 4:3], q=-1--1, 12800 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
frame=  223 fps=0.0 q=28.0 size=      42kB time=00:00:08.16 bitrate=  42.0kbits/s
frame=  250 fps=0.0 q=-1.0 Lsize=      53kB time=00:00:09.92 bitrate=  43.5kbits/s
video:49kB audio:0kB subtitle:0 global headers:0kB muxing overhead 7.315520%
[libx264 @ 0000000000693640] frame I:1     Avg QP:24.54  size:  2863
[libx264 @ 0000000000693640] frame P:77    Avg QP:15.07  size:   488
[libx264 @ 0000000000693640] frame B:172   Avg QP:15.11  size:    53
[libx264 @ 0000000000693640] consecutive B-frames:  8.0%  0.8%  0.0% 91.2%
[libx264 @ 0000000000693640] mb I  I16..4: 65.0%  9.7% 25.3%
[libx264 @ 0000000000693640] mb P  I16..4:  3.7%  1.6%  0.4%  P16..4:  7.0%  3.9%  1.2%  0.0%  0.0%    skip:82.2%
[libx264 @ 0000000000693640] mb B  I16..4:  0.5%  0.1%  0.0%  B16..8:  1.0%  0.1%  0.0%  direct: 0.4%  skip:97.9%  L0:46.8% L1:52.7% BI: 0.5%
[libx264 @ 0000000000693640] 8x8 transform intra:22.2% inter:24.4%
[libx264 @ 0000000000693640] coded y,u,v intra: 10.4% 16.1% 12.1% inter: 0.8% 0.9% 0.7%
[libx264 @ 0000000000693640] i16 v,h,dc,p: 48% 42%  1%  9%
[libx264 @ 0000000000693640] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 90%  2%  7%  0%  0% 0%  0%  0%  0%
[libx264 @ 0000000000693640] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 47% 22%  1%  1% 1%  0%  1%  1%
[libx264 @ 0000000000693640] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0000000000693640] kb/s:39.63

$ ./ffmpeg -i test_toolnix.mp4
ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl
 --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enab
le-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfr
eetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --a
rch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test_toolnix.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.18.100
  Duration: 00:00:10.00, start: 0.000000, bitrate: 43 kb/s
    Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), y
uv444p, 320x240 [SAR 1:1 DAR 4:3], 40 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (d
efault)
    Metadata:
      handler_name    : VideoHandler
At least one output file must be specified

/* MKVToolnix GUI muxing at this point, can't paste anything */

$ mkvinfo -v test_toolnix.mkv
+ EBML head
|+ EBML version: 1
|+ EBML read version: 1
|+ EBML maximum ID length: 4
|+ EBML maximum size length: 8
|+ Doc type: matroska
|+ Doc type version: 4
|+ Doc type read version: 2
+ Segment, size 57293
|+ Seek head (subentries will be skipped)
|+ EbmlVoid (size: 4046)
|+ Segment information
| + Timecode scale: 1000000
| + Muxing application: libebml v1.3.0 + libmatroska v1.4.1
| + Writing application: mkvmerge v6.4.1 ('Omega Point') built on Sep 16 2013 22:05:10
| + Duration: 10.000s (00:00:10.000)
| + Date: Fri Sep 20 20:12:48 2013 UTC
| + Segment UID: 0xae 0x90 0x6b 0x02 0xaa 0xc7 0x3a 0xda 0x98 0x03 0x12 0x17 0xb5 0x2b 0x8b 0xee
|+ Segment tracks
| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 1711331502
|  + Track type: video
|  + Lacing flag: 0
|  + MinCache: 1
|  + Codec ID: V_MPEG4/ISO/AVC
|  + CodecPrivate, length 42 (h.264 profile: High 4:4:4 Predictive @L1.3)
|  + Default duration: 40.000ms (25.000 frames/fields per second for a video track)
|  + Language: und
|  + Video track
|   + Pixel width: 320
|   + Pixel height: 240
|   + Display width: 320
|   + Display height: 240
|+ EbmlVoid (size: 1058)
|+ Cluster
| + Cluster timecode: 0.000s
| + SimpleBlock (key, track number 1, 1 frame(s), timecode 0.000s = 00:00:00.000)
|  + Frame with size 3552
| + SimpleBlock (track number 1, 1 frame(s), timecode 0.160s = 00:00:00.160)
|  + Frame with size 556
[...]

Notice again the duration and timecode of first block.

Remuxing test_toolnix.mkv with ffmpeg (Can also use the mp4 for this, the result is the same), then looking at it with mkvinfo:

$ ./ffmpeg -i test_toolnix.mkv -c:v copy remux.mkv
ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfreetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --arch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, matroska,webm, from 'test_toolnix.mkv':
  Metadata:
    creation_time   : 2013-09-20 20:12:48
  Duration: 00:00:10.00, start: 0.000000, bitrate: 45 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 320x240 [SAR 1:1DAR 4:3], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Output #0, matroska, to 'remux.mkv':
  Metadata:
    encoder         : Lavf55.18.100
    Stream #0:0: Video: h264 (H264 / 0x34363248), yuv444p, 320x240 [SAR 1:1 DAR4:3], q=2-31, 25 fps, 1k tbn, 1k tbc (default)
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame=  250 fps=0.0 q=-1.0 Lsize=      51kB time=00:00:09.88 bitrate=  42.5kbits/s
video:49kB audio:0kB subtitle:0 global headers:0kB muxing overhead 4.400287%

$ ./ffmpeg -i remux.mkv
ffmpeg version N-56469-gf6622f9 Copyright (c) 2000-2013 the FFmpeg developers
  built on Sep 20 2013 05:04:04 with gcc 4.7.3 (GCC)
  configuration: --enable-gpl --enable-nonfree --enable-libquvi --enable-openssl --enable-nonfree --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libtheora --enable-libvpx --enable-libx264 --enable-libmp3lame --enable-libfreetype --cross-prefix=x86_64-w64-mingw32- --target-os=mingw32 --cpu=amdfam10 --arch=x86_64 --prefix=/mingw64
  libavutil      52. 45.100 / 52. 45.100
  libavcodec     55. 33.100 / 55. 33.100
  libavformat    55. 18.100 / 55. 18.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 86.102 /  3. 86.102
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
Input #0, matroska,webm, from 'remux.mkv':
  Metadata:
    MAJOR_BRAND     : isom
    MINOR_VERSION   : 512
    COMPATIBLE_BRANDS: isomiso2avc1mp41
    ENCODER         : Lavf55.18.100
  Duration: 00:00:10.08, start: 0.000000, bitrate: 41 kb/s
    Stream #0:0(und): Video: h264 (High 4:4:4 Predictive), yuv444p, 320x240 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      LANGUAGE        : und
      HANDLER_NAME    : VideoHandler
At least one output file must be specified

$ mkvinfo -v remux.mkv
+ EBML head
|+ EBML version: 1
|+ EBML read version: 1
|+ EBML maximum ID length: 4
|+ EBML maximum size length: 8
|+ Doc type: matroska
|+ Doc type version: 4
|+ Doc type read version: 2
+ Segment, size 52375
|+ Seek head (subentries will be skipped)
|+ EbmlVoid (size: 149)
|+ Segment information
| + Timecode scale: 1000000
| + Muxing application: Lavf55.18.100
| + Writing application: Lavf55.18.100
| + Segment UID: 0xb5 0x1b 0x58 0x0b 0xe3 0x7b 0x7c 0x52 0x7f 0x49 0x72 0x1c 0x88 0xaa 0x3e 0x2a
| + Duration: 10.080s (00:00:10.080)
|+ Segment tracks
| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 1
|  + Lacing flag: 0
|  + Language: und
|  + Codec ID: V_MPEG4/ISO/AVC
|  + Track type: video
|  + Default duration: 40.000ms (25.000 frames/fields per second for a video track)
|  + Video track
|   + Pixel width: 320
|   + Pixel height: 240
|   + Display width: 320
|   + Display height: 240
|  + CodecPrivate, length 42 (h.264 profile: High 4:4:4 Predictive @L1.3)
|+ Tags
| + Tag
|  + Targets
|  + Simple
|   + Name: ENCODER
|   + String: Lavf55.18.100
|+ Cluster
| + Cluster timecode: 0.080s
| + SimpleBlock (key, track number 1, 1 frame(s), timecode 0.080s = 00:00:00.080)
|  + Frame with size 3552
| + SimpleBlock (track number 1, 1 frame(s), timecode 0.240s = 00:00:00.240)
|  + Frame with size 556

[...]

Duration and timecode of first block are wrong again.

This can be reproduced with any h264 stream, created by ffmpeg/libx264 or not.
The pts of first packet/block will always be a few ms ahead after re/muxing, which adds to the total duration (10.080s vs 10.000s in this case)

Change History (17)

comment:1 by Carl Eugen Hoyos, 11 years ago

Why is it a problem that the timestamp of the first block != 0?

comment:2 by jamal, 11 years ago

Well, for starters it's making the final duration value wrong (Should be 10 seconds, ends up being 10.08 seconds in the examples above).

Aside from that, now that we're writing cues for subtitle packets, those extra milliseconds added during muxing are in many cases making a video keyframe cue have a higher timestamp than the timestamp from a subtitle cue that comes right after it, which should not happen and promptly triggers warnings in mkvalidator (Something that doesn't happen with other codecs, like vp8 or theora, that start at pts 0).

comment:3 by Carl Eugen Hoyos, 11 years ago

Keywords: h264 mkv added

comment:4 by jamal, 11 years ago

Maybe the subject of the ticket should be changed? What's happening here is that during muxing, a few ms are added to each packet's ts value.
I don't know if starting at 0 or not is correct for mkv-h264, but afaics that's not the actual issue here.

comment:5 by ValdikSS, 10 years ago

I can confirm this issue.

ffmpeg -i input.mkv -map 0:v -map_metadata -1 -map_chapters -1 -c copy -f segment -reset_timestamps 1 split%02d.mkv
ffmpeg -f concat -i <(for f in $PWD/split*.mkv; do echo "file '$f'"; done) -map 0:v -c copy joined.mkv
ffmpeg -i joined.mkv -i input.mkv -map 0 -map 1 -map -1:v -c copy final.mkv

This will split input.mkv video to 30 single .mkv files and join them again.
final.mkv audio is desynced.

Issue is only reproducible with matroska container. All other tested containers produced perfectly synced final.mkv
Splitting with mkvmerge and joining again also produce synced file.

comment:7 by ValdikSS, 10 years ago

Cc: iam@valdikss.org.ru added

in reply to:  5 ; comment:8 by cdlscpmv, 10 years ago

Replying to ValdikSS:

I can confirm this issue.

ffmpeg -i input.mkv -map 0:v -map_metadata -1 -map_chapters -1 -c copy -f segment -reset_timestamps 1 split%02d.mkv
ffmpeg -f concat -i <(for f in $PWD/split*.mkv; do echo "file '$f'"; done) -map 0:v -c copy joined.mkv
ffmpeg -i joined.mkv -i input.mkv -map 0 -map 1 -map -1:v -c copy final.mkv

This will split input.mkv video to 30 single .mkv files and join them again.
final.mkv audio is desynced.

Issue is only reproducible with matroska container. All other tested containers produced perfectly synced final.mkv
Splitting with mkvmerge and joining again also produce synced file.

I cannot notice any desynchronization in final.mkv

ffmpeg version 1.2.6
configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --optflags='-march=native -O2 -pipe -mfpmath=sse' --extra-cflags='-march=native -O2 -pipe -mfpmath=sse' --extra-cxxflags='-march=native -O2 -pipe -mfpmath=sse' --disable-static --enable-gpl --enable-postproc --enable-avfilter --enable-avresample --disable-stripping --disable-indev=v4l2 --disable-indev=alsa --disable-indev=oss --disable-indev=jack --disable-outdev=alsa --disable-outdev=oss --disable-outdev=sdl --enable-bzlib --disable-runtime-cpudetect --disable-debug --disable-doc --disable-gnutls --enable-hardcoded-tables --enable-iconv --enable-network --disable-openssl --disable-ffplay --disable-vaapi --disable-vdpau --enable-zlib --disable-libvo-aacenc --disable-libvo-amrwbenc --enable-libmp3lame --disable-libfdk-aac --disable-libaacplus --disable-libfaac --enable-libtheora --disable-libtwolame --disable-libx264 --disable-libxvid --disable-libcdio --disable-libiec61883 --disable-libdc1394 --disable-libcaca --disable-openal --disable-libv4l2 --enable-libpulse --enable-x11grab --disable-libflite --disable-frei0r --disable-fontconfig --disable-libass --disable-libfreetype --disable-libsoxr --enable-pthreads --disable-libopencore-amrwb --disable-libopencore-amrnb --disable-libopenjpeg --disable-libbluray --disable-libcelt --disable-libgsm --disable-libmodplug --enable-libopus --disable-librtmp --disable-libschroedinger --disable-libspeex --enable-libvorbis --disable-libvpx --disable-amd3dnow --disable-amd3dnowext --disable-altivec --disable-avx --disable-mmxext --disable-ssse3 --disable-vis --disable-neon --cpu=host
libavutil      52. 18.100 / 52. 18.100
libavcodec     54. 92.100 / 54. 92.100
libavformat    54. 63.104 / 54. 63.104
libavdevice    54.  3.103 / 54.  3.103
libavfilter     3. 42.103 /  3. 42.103
libswscale      2.  2.100 /  2.  2.100
libswresample   0. 17.102 /  0. 17.102
libpostproc    52.  2.100 / 52.  2.100

in reply to:  8 comment:9 by ValdikSS, 10 years ago

Replying to cdlscpmv:

I cannot notice any desynchronization in final.mkv

That's interesting. It could be a regression then.
Could you please upload your final.mkv somewhere?

I'll download old builds and see how it works there.

comment:10 by ValdikSS, 10 years ago

Yes, totally a regression. It works with ffmpeg 1.2.

comment:11 by ValdikSS, 10 years ago

c2cb01d418dd18e1cf997c038d37378d773121be is the first bad commit
commit c2cb01d418dd18e1cf997c038d37378d773121be
Author: Luca Barbato <lu_zero@gentoo.org>
Date: Wed Apr 3 14:11:10 2013 +0200

lavf: introduce AVFMT_TS_NEGATIVE


Most formats do not support negative timestamps, shift them to avoid
unexpected behaviour and a number of bad crashes.


CC:libav-stable@libav.org


Signed-off-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>

:040000 040000 11d40f442e48f8c05f7d64e6ffd71ad84dd81345 eea7ff60273fec11db8fbe4b40cd683f985229d8 M libavformat
:040000 040000 dab89a0f1daf4e5c37454605af93258415a9adae 00543833f8c87aef46d344be3d3e3799f3cefa0f M tests

comment:12 by ValdikSS, 10 years ago

I'm very sorry, but my last message is wrong.
The latest working commit is 05f4c0506198548c2e93e2029763008383ab48ca
c4bf74022562036bac3f53f4d5b8b1787d904c24 breaks things.

comment:13 by ValdikSS, 10 years ago

matroskaenc.c sets avoid_negative_ts=1 by default. You can execute:

ffmpeg -f lavfi -i testsrc -t 10 -c:v libx264 -preset veryfast -avoid_negative_ts 0 test.mkv

and you'll get a videofile with 00:00:10.000 duration instead of 00:00:10.080.
It seems like mkv muxer bug, because all other containers muxes exactly 00:00:10.000 with -avoid_negative_ts 1.

And in my case, -f segment forces -avoid_negative_ts 1 so there is +80ms delay in every splitted file.

Last edited 10 years ago by ValdikSS (previous) (diff)

comment:14 by jamal, 10 years ago

Component: undeterminedavformat
Keywords: regression added
Status: newopen
Summary: h264 streams don't start with timestamp/timecode 0 when muxed with Matroskah264 streams have wrong timestamps when muxed with Matroska

comment:15 by Carl Eugen Hoyos, 10 years ago

Priority: normalimportant

comment:16 by wave, 9 years ago

Cc: barderne@gmail.com added

comment:17 by Hendrik, 9 years ago

Reproduced by developer: set
Resolution: fixed
Status: openclosed
Note: See TracTickets for help on using tickets.