New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCE: Chapters timings messed up #1762

Open
passat1 opened this Issue Dec 28, 2018 · 24 comments

Comments

5 participants
@passat1
Copy link

passat1 commented Dec 28, 2018

Description of the problem

After recoding MKV file with chapters in output MP4 file chapter timings are randomly modified for few seconds.
Chapter times of output file are totally different from times of input file.
This problem is with every MKV to MP4 conversion and this is only one example.
Did not try MP4 to MP4 or MKV to MKV conversion.

HandBrake version (e.g., 1.0.0)

HandBrake 1.2.0 x64

Operating system and version (e.g., Ubuntu 18.04 LTS, macOS 10.14 Mojave, Windows 10 1809)

Windows 8.1 x64

Error message text or screenshot

MediaInfo of INPUT video:

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 4 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 2 h 5 min
Bit rate                                 : 3 929 kb/s
Width                                    : 1 280 pixels
Height                                   : 534 pixels
Display aspect ratio                     : 2.35:1
Frame rate mode                          : Constant
Frame rate                               : 23.976 (24000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.240
Stream size                              : 3.45 GiB (93%)
Writing library                          : x264 core 155 r2893 b00bcaf
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=240 / keyint_min=24 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=18.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=62500 / vbv_bufsize=78125 / crf_max=0.0 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00
Default                                  : Yes
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709

Audio #1
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : A_AAC-2
Duration                                 : 2 h 5 min
Bit rate                                 : 192 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 170 MiB (4%)
Language                                 : English
Default                                  : Yes
Forced                                   : No

Audio #2
ID                                       : 3
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : A_AAC-2
Duration                                 : 2 h 5 min
Bit rate                                 : 93.4 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 83.9 MiB (2%)
Title                                    : Director's comments
Language                                 : English
Default                                  : No
Forced                                   : No

Text #1
ID                                       : 4
Format                                   : UTF-8
Codec ID                                 : S_TEXT/UTF8
Codec ID/Info                            : UTF-8 Plain Text
Duration                                 : 1 h 56 min
Bit rate                                 : 56 b/s
Count of elements                        : 1445
Stream size                              : 48.7 KiB (0%)
Language                                 : English
Default                                  : No
Forced                                   : No

Text #2
ID                                       : 5
Format                                   : UTF-8
Codec ID                                 : S_TEXT/UTF8
Codec ID/Info                            : UTF-8 Plain Text
Duration                                 : 2 h 1 min
Bit rate                                 : 83 b/s
Count of elements                        : 1573
Stream size                              : 74.2 KiB (0%)
Title                                    : Director's comments
Language                                 : English
Default                                  : No
Forced                                   : No

Menu
00:00:00.000                             : en:01. Opening Credits
00:00:34.001                             : en:02. Chapter 1
00:08:13.077                             : en:03. Chapter 2
00:16:22.649                             : en:04. Chapter 3
00:23:13.268                             : en:05. Chapter 4
00:31:37.104                             : en:06. Chapter 5
00:39:02.591                             : en:07. Chapter 6
00:44:25.205                             : en:08. Chapter 7
00:51:27.252                             : en:09. Chapter 8
00:57:52.386                             : en:10. Chapter 9
01:05:09.656                             : en:11. Chapter 10
01:11:44.551                             : en:12. Chapter 11
01:18:00.760                             : en:13. Chapter 12
01:23:32.258                             : en:14. Chapter 13
01:29:56.850                             : en:15. Chapter 14
01:36:00.544                             : en:16. Chapter 15
01:39:56.616                             : en:17. Chapter 16
01:50:08.603                             : en:18. Chapter 17
01:55:31.300                             : en:19. Chapter 18
02:01:03.001                             : en:20. Closing Credits

MediaInfo of OUTPUT file:

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 2 h 5 min
Bit rate                                 : 3 736 kb/s
Width                                    : 1 280 pixels
Height                                   : 534 pixels
Display aspect ratio                     : 2.35:1
Frame rate mode                          : Variable
Frame rate                               : 23.976 (24000/1001) FPS
Minimum frame rate                       : 23.974 FPS
Maximum frame rate                       : 23.981 FPS
Standard                                 : Component
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.228
Stream size                              : 3.28 GiB (93%)
Encoded date                             : UTC 2018-12-28 10:25:03
Tagged date                              : UTC 2018-12-28 10:25:03
Color range                              : Limited
Menus                                    : 4
Codec configuration box                  : avcC

Audio #1
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 2 h 5 min
Bit rate mode                            : Constant
Bit rate                                 : 192 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 170 MiB (5%)
Title                                    : Stereo
Language                                 : English
Default                                  : Yes
Alternate group                          : 1
Encoded date                             : UTC 2018-12-28 10:25:03
Tagged date                              : UTC 2018-12-28 10:25:03
Menus                                    : 4

Audio #2
ID                                       : 3
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 2 h 5 min
Bit rate mode                            : Constant
Bit rate                                 : 93.4 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 83.9 MiB (2%)
Title                                    : Comments
Language                                 : English
Default                                  : No
Alternate group                          : 1
Encoded date                             : UTC 2018-12-28 10:25:03
Tagged date                              : UTC 2018-12-28 10:25:03
Menus                                    : 4

Menu
ID                                       : 4
Codec ID                                 : text
Duration                                 : 2 h 5 min
Language                                 : English
Encoded date                             : UTC 2018-12-28 10:25:03
Tagged date                              : UTC 2018-12-28 10:25:03
Menu For                                 : 1,2,3
00:00:00.000                             : 01. Opening Credits
00:00:40.707                             : 02. Chapter 1
00:08:18.665                             : 03. Chapter 2
00:16:27.153                             : 04. Chapter 3
00:23:14.226                             : 05. Chapter 4
00:31:43.068                             : 06. Chapter 5
00:39:10.849                             : 07. Chapter 6
00:44:26.330                             : 08. Chapter 7
00:51:33.757                             : 09. Chapter 8
00:58:00.477                             : 10. Chapter 9
01:05:18.081                             : 11. Chapter 10
01:11:44.801                             : 12. Chapter 11
01:18:01.343                             : 13. Chapter 12
01:23:37.179                             : 14. Chapter 13
01:30:03.899                             : 15. Chapter 14
01:36:10.265                             : 16. Chapter 15
01:40:04.332                             : 17. Chapter 16
01:50:14.942                             : 18. Chapter 17
01:55:40.600                             : 19. Chapter 18
02:01:06.259                             : 20. Closing Credits
Bit rate mode                            : VBR

HandBrake Activity Log (see https://handbrake.fr/docs/en/latest/help/activity-log.html)

In next post ...
@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 28, 2018

HandBrake Activity Log (see https://handbrake.fr/docs/en/latest/help/activity-log.html)

HandBrake 1.2.0 (2018121700)
OS: Microsoft Windows NT 6.3.9600.0
Ram: 8191 MB, 
GPU Information:
  AMD Radeon HD 7700 Series - 22.19.171.1024
Screen: 1920x1080
Temp Dir: D:\Temp\HandBrakePortableTemp\
Install Dir: D:\PortableApps\HandBrakePortable\App\HandBrake64
Data Dir: C:\Users\Master\AppData\Roaming\HandBrake

-------------------------------------------


# Starting Encode ...

[11:25:02] hb_init: starting libhb thread
[11:25:02] 1 job(s) to process
[11:25:02] CPU: Intel(R) Core(TM)2 Quad  CPU   Q9550  @ 2.83GHz
[11:25:02]  - logical processor count: 4
[11:25:02] Intel Quick Sync Video support: no
[11:25:02] hb_scan: path=D:\Videos\_RADNI\Shooter (2007).mkv, title_index=1
udfread ERROR: ECMA 167 Volume Recognition failed
src/libbluray/disc/disc.c:323: failed opening UDF image D:\Videos\_RADNI\Shooter (2007).mkv
src/libbluray/disc/disc.c:424: error opening file BDMV\index.bdmv
src/libbluray/disc/disc.c:424: error opening file BDMV\BACKUP\index.bdmv
[11:25:02] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 6.0.0
libdvdread: Encrypted DVD support unavailable.
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[11:25:02] dvd: not a dvd - trying as a stream/file instead
Input #0, matroska,webm, from 'D:\Videos\_RADNI\Shooter (2007).mkv':
  Metadata:
    title           : -Jackal-
    encoder         : libebml v1.3.6 + libmatroska v1.4.9
    creation_time   : 2018-05-14T12:18:40.000000Z
  Duration: 02:05:41.61, start: 0.000000, bitrate: 4214 kb/s
    Chapter #0:0: start 0.000000, end 34.001000
    Metadata:
      title           : 01. Opening Credits
    Chapter #0:1: start 34.001000, end 493.077000
    Metadata:
      title           : 02. Chapter 1
    Chapter #0:2: start 493.077000, end 982.649000
    Metadata:
      title           : 03. Chapter 2
    Chapter #0:3: start 982.649000, end 1393.268000
    Metadata:
      title           : 04. Chapter 3
    Chapter #0:4: start 1393.268000, end 1897.104000
    Metadata:
      title           : 05. Chapter 4
    Chapter #0:5: start 1897.104000, end 2342.591000
    Metadata:
      title           : 06. Chapter 5
    Chapter #0:6: start 2342.591000, end 2665.205000
    Metadata:
      title           : 07. Chapter 6
    Chapter #0:7: start 2665.205000, end 3087.252000
    Metadata:
      title           : 08. Chapter 7
    Chapter #0:8: start 3087.252000, end 3472.386000
    Metadata:
      title           : 09. Chapter 8
    Chapter #0:9: start 3472.386000, end 3909.656000
    Metadata:
      title           : 10. Chapter 9
    Chapter #0:10: start 3909.656000, end 4304.551000
    Metadata:
      title           : 11. Chapter 10
    Chapter #0:11: start 4304.551000, end 4680.760000
    Metadata:
      title           : 12. Chapter 11
    Chapter #0:12: start 4680.760000, end 5012.258000
    Metadata:
      title           : 13. Chapter 12
    Chapter #0:13: start 5012.258000, end 5396.850000
    Metadata:
      title           : 14. Chapter 13
    Chapter #0:14: start 5396.850000, end 5760.544000
    Metadata:
      title           : 15. Chapter 14
    Chapter #0:15: start 5760.544000, end 5996.616000
    Metadata:
      title           : 16. Chapter 15
    Chapter #0:16: start 5996.616000, end 6608.603000
    Metadata:
      title           : 17. Chapter 16
    Chapter #0:17: start 6608.603000, end 6931.300000
    Metadata:
      title           : 18. Chapter 17
    Chapter #0:18: start 6931.300000, end 7263.001000
    Metadata:
      title           : 19. Chapter 18
    Chapter #0:19: start 7263.001000, end 7541.610000
    Metadata:
      title           : 20. Closing Credits
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1280x534 [SAR 97:99 DAR 62080:26433], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Metadata:
      BPS-eng         : 3928536
      DURATION-eng    : 02:05:41.534000000
      NUMBER_OF_FRAMES-eng: 180816
      NUMBER_OF_BYTES-eng: 3703398671
      _STATISTICS_WRITING_APP-eng: mkvmerge v23.0.0 ('The Bride Said No') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-05-14 12:18:40
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:1(eng): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
    Metadata:
      BPS-eng         : 189375
      DURATION-eng    : 02:05:41.589000000
      NUMBER_OF_FRAMES-eng: 353512
      NUMBER_OF_BYTES-eng: 178523561
      _STATISTICS_WRITING_APP-eng: mkvmerge v23.0.0 ('The Bride Said No') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-05-14 12:18:40
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:2(eng): Audio: aac (LC), 48000 Hz, stereo, fltp
    Metadata:
      title           : Director's comments
      BPS-eng         : 93375
      DURATION-eng    : 02:05:41.610000000
      NUMBER_OF_FRAMES-eng: 353513
      NUMBER_OF_BYTES-eng: 88025160
      _STATISTICS_WRITING_APP-eng: mkvmerge v23.0.0 ('The Bride Said No') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-05-14 12:18:40
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:3(eng): Subtitle: subrip
    Metadata:
      BPS-eng         : 56
      DURATION-eng    : 01:56:44.747000000
      NUMBER_OF_FRAMES-eng: 1445
      NUMBER_OF_BYTES-eng: 49901
      _STATISTICS_WRITING_APP-eng: mkvmerge v23.0.0 ('The Bride Said No') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-05-14 12:18:40
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:4(eng): Subtitle: subrip
    Metadata:
      title           : Director's comments
      BPS-eng         : 83
      DURATION-eng    : 02:01:52.513000000
      NUMBER_OF_FRAMES-eng: 1573
      NUMBER_OF_BYTES-eng: 75977
      _STATISTICS_WRITING_APP-eng: mkvmerge v23.0.0 ('The Bride Said No') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-05-14 12:18:40
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
[11:25:02] scan: decoding previews for title 1
[11:25:02] scan: audio 0x1: aac, rate=48000Hz, bitrate=1 English (AAC LC) (2.0 ch)
[11:25:02] scan: audio 0x2: aac, rate=48000Hz, bitrate=1 English (AAC LC) (2.0 ch)
[11:25:02] scan: 10 previews, 1280x534, 23.976 fps, autocrop = 0/0/0/0, aspect 2.35:1, PAR 97:99
[11:25:02] scan: supported video decoders: avcodec qsv
[11:25:02] libhb: scan thread found 1 valid title(s)
[11:25:02] starting job
[11:25:02] Auto Passthru: allowed codecs are AAC, AC3, E-AC3, TrueHD, DTS, DTS-HD, MP3, FLAC
[11:25:02] Auto Passthru: fallback is AAC (avcodec)
[11:25:02] Auto Passthru: using AAC Passthru for track 1
[11:25:02] Auto Passthru: using AAC Passthru for track 2
[11:25:02] job configuration:
[11:25:02]  * source
[11:25:02]    + D:\Videos\_RADNI\Shooter (2007).mkv
[11:25:02]    + title 1, chapter(s) 1 to 20
[11:25:02]    + container: matroska,webm
[11:25:02]    + data rate: 4214 kbps
[11:25:02]  * destination
[11:25:02]    + E:\TEMP\Shooter (2007).mp4
[11:25:02]    + container: MPEG-4 (libavformat)
[11:25:02]      + chapter markers
[11:25:02]  * video track
[11:25:02]    + decoder: h264
[11:25:02]    + filters
[11:25:02]      + Framerate Shaper (mode=1)
[11:25:02]        + frame rate: 23.976 fps -> constant 23.976 fps
[11:25:02]      + Crop and Scale (width=1280:height=534:crop-top=0:crop-bottom=0:crop-left=0:crop-right=0)
[11:25:02]        + source: 1280 * 534, crop (0/0/0/0): 1280 * 534, scale: 1280 * 534
[11:25:02]    + Output geometry
[11:25:02]      + storage dimensions: 1280 x 534
[11:25:02]      + pixel aspect ratio: 97 : 99
[11:25:02]      + display dimensions: 1254 x 534
[11:25:02]    + encoder: H.264 (AMD VCE)
[11:25:02]      + preset:  quality
[11:25:02]      + profile: high
[11:25:02]      + level:   4.1
[11:25:02]      + quality: 20.00 (QP)
[11:25:02]      + color profile: 1-1-1
[11:25:02]  * audio track 1
[11:25:02]    + decoder: English (AAC LC) (2.0 ch) (track 1, id 0x1)
[11:25:02]      + samplerate: 48000 Hz
[11:25:02]    + AAC Passthru
[11:25:02]  * audio track 2
[11:25:02]    + name: Comments
[11:25:02]    + decoder: English (AAC LC) (2.0 ch) (track 2, id 0x2)
[11:25:02]      + samplerate: 48000 Hz
[11:25:02]    + AAC Passthru
[11:25:02] sync: expecting 180817 video frames
[11:25:02] encavcodecInit: H.264 (AMD VCE)
[11:25:02] encavcodec: encoding at QP 20.00
[11:25:02] encavcodec: encoding with stored aspect 97/99
[11:25:03] sync: first pts video is 0
[11:25:03] sync: "01. Opening Credits" (1) at frame 1 time 0
[11:25:03] sync: first pts audio 0x1 is 0
[11:25:03] sync: first pts audio 0x2 is 0
[11:25:09] sync: "02. Chapter 1" (2) at frame 820 time 3074321
[11:26:35] sync: "03. Chapter 2" (3) at frame 11824 time 44380586
[11:28:05] sync: "04. Chapter 3" (4) at frame 23562 time 88442103
[11:29:22] sync: "05. Chapter 4" (5) at frame 33407 time 125397772
[11:31:01] sync: "06. Chapter 5" (6) at frame 45486 time 170739318
[11:32:29] sync: "07. Chapter 6" (7) at frame 56168 time 210836876
[11:33:31] sync: "08. Chapter 7" (8) at frame 63903 time 239872132
[11:34:50] sync: "09. Chapter 8" (9) at frame 74022 time 277856328
[11:36:00] sync: "10. Chapter 9" (10) at frame 83255 time 312514702
[11:37:19] sync: "11. Chapter 10" (11) at frame 93739 time 351869017
[11:38:32] sync: "12. Chapter 11" (12) at frame 103208 time 387413276
[11:39:40] sync: "13. Chapter 12" (13) at frame 112228 time 421272101
[11:40:40] sync: "14. Chapter 13" (14) at frame 120176 time 451106906
[11:41:51] sync: "15. Chapter 14" (15) at frame 129396 time 485716481
[11:42:58] sync: "16. Chapter 15" (16) at frame 138116 time 518449181
[11:43:43] sync: "17. Chapter 16" (17) at frame 143776 time 539695406
[11:45:34] sync: "18. Chapter 17" (18) at frame 158450 time 594777933
[11:46:33] sync: "19. Chapter 18" (19) at frame 166187 time 623820697
[11:47:33] sync: "20. Closing Credits" (20) at frame 174139 time 653670517
[11:48:23] reader: done. 1 scr changes
[11:48:23] work: average encoding speed for job is 129.115845 fps
[11:48:24] vfr: 180816 frames output, 0 dropped and 0 duped for CFR/PFR
[11:48:24] vfr: lost time: 0 (0 frames)
[11:48:24] vfr: gained time: 0 (0 frames) (0 not accounted for)
[11:48:24] aac-decoder done: 353512 frames, 0 decoder errors
[11:48:24] aac-decoder done: 353513 frames, 0 decoder errors
[11:48:24] h264-decoder done: 180816 frames, 0 decoder errors
[11:48:24] sync: got 180816 frames, 180817 expected
[11:48:24] sync: framerate min 23.981 fps, max 23.981 fps, avg 23.976 fps
[11:48:24] mux: track 0, 180816 frames, 3522307081 bytes, 3736.37 kbps, fifo 2048
[11:48:24] mux: track 1, 353512 frames, 178523561 bytes, 189.37 kbps, fifo 4096
[11:48:24] mux: track 2, 353513 frames, 88025160 bytes, 93.37 kbps, fifo 4096
[11:48:24] libhb: work result = 0

# Encode Completed ...
@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Dec 28, 2018

We had a similar issue with NVENC where an encoder option needed to be set to force IDR frames whenever HandBrake needed a keyframe at a chapter boundary. Looking at the AMD amfenc code, there isn't even a way to force a keyframe. It ignores the frame flag we set that forces one of these. I don't know the amf encoder api well enough to say whether this is fixable or not.

@sr55 sr55 added the Bug label Dec 28, 2018

@sr55

This comment has been minimized.

Copy link
Member

sr55 commented Dec 28, 2018

I could be wrong, but it doesn't look like there is any way to set this though ffmpeg currently.

GPUOpen-LibrariesAndSDKs/AMF#85 also doesn't bode well ...

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 28, 2018

Maybe I'm wrong, but I can't see any special reason why key frame MUST be exactly at chapter boundary.
It could be few (not too much) frames before.
On almost all original discs key frame is few (1-5) frames BEFORE chapter mark.
Another words, chapter marks are few frames after scene change (key frame), where key frame is in almost all (if not all) cases generated by default.
So chapter mark should be aligned (if it should be changed at all) with last previous key frame (which could be known at that moment) as result of scene change, not with next one.
For that reason there is no need to force new key frame few frames after previous, and maybe that is the reason why encoder refuses to generate it, because there is minimum distance between two key (Intra) frames.
Or, why should not just copy input chapter list to output as it is?
Now I have to remux every file with chapters from source file, which works well, much better than with modified chapter list.

@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Dec 28, 2018

If there is no keyframe at the chapter then you can't seek to the chapter. Seeking to anything but a keyframe results in one of two possible outcomes. The player starts playing without everything it needs to construct a complete frame and you get garbled output until the next keyframe, or the player just skips frames till it reaches the next keyframe.

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 29, 2018

... Or player finds LAST PREVIOUS key frame and reconstructs picture at that exact chapter position.
That how (at least) MPC-HC is working.
If previous key frame is only few frames before chapter mark than it is fast, but if previous key frame is too far away, than it needs more time to go through all frames to exact chapter position, and than picture is shown.
Best way to test that is to be in "PAUSE" mode and to jump from chapter to chapter.

@bradleysepos

This comment has been minimized.

Copy link
Member

bradleysepos commented Dec 29, 2018

First test (try), then comment.

@passat1 Accusatory language isn't welcome here. Please read the project's Code of Conduct.

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 29, 2018

Sorry, edited previous post.
But anyway, I would like that my statement is tested on same and other players (verified) and then posted comment about it.

@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Dec 29, 2018

I don't think it is a good idea to modify the current behaviour so that it works with one known well behaved player. I'm sure there are other players out there that are also well behaved, but there are lots of players that are not. HandBrake's practice of placing an IDR frame at the chapter position is the right thing to do because it works well with many players.

The correct fix for this problem is to fix the encoder so that an IDR frame can be added at the chapter position. If it is determined that this is absolutely not possible, then we can add a special case for VCE so that it behaves as well as possible.

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 29, 2018

I agree, that would be best solution, but 'till then, I think that leaving chapters as they are is only "better" solution.

@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Dec 29, 2018

till then...

Don't expect any fix for this overnight. The person who submitted the VCE code and helps maintain it for us is on holiday. When he's available to evaluate possible solutions, we will come up with a fix.

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 29, 2018

I'm dealing with that for quite some time by re-muxing original chapters in output file, so that is not great problem for me.
My wish was only to start discussion about problem and to try to help with finding some way to some improvement or possible solution, and to learn something by the way.
I do not expect anything, not soon.

@passat1

This comment has been minimized.

Copy link

passat1 commented Dec 30, 2018

I did some further analysis and found one problem that might lead to all other problems with chapter timings.
When analyzing source file in "HandBrake Activity Log" original chapters are like this:

    Chapter #0:0: start 0.000000, end 70.584000
    Metadata:
      title           : Opening Credits
    Chapter #0:1: start 70.584000, end 319.027000
    Metadata:
      title           : Chapter 01
    Chapter #0:2: start 319.027000, end 794.335000
    Metadata:
      title           : Chapter 02
    Chapter #0:3: start 794.335000, end 1074.865000
    Metadata:
      title           : Chapter 03
    Chapter #0:4: start 1074.865000, end 1599.389000
    Metadata:
      title           : Chapter 04
    Chapter #0:5: start 1599.389000, end 1864.196000
    Metadata:
      title           : Chapter 05
    Chapter #0:6: start 1864.196000, end 2393.975000
    Metadata:
      title           : Chapter 06
    Chapter #0:7: start 2393.975000, end 2701.782000
    Metadata:
      title           : Chapter 07
    Chapter #0:8: start 2701.782000, end 3197.945000
    Metadata:
      title           : Chapter 08
    Chapter #0:9: start 3197.945000, end 3589.336000
    Metadata:
      title           : Chapter 09
    Chapter #0:10: start 3589.336000, end 3942.522000
    Metadata:
      title           : Chapter 10
    Chapter #0:11: start 3942.522000, end 4332.954000
    Metadata:
      title           : Chapter 11
    Chapter #0:12: start 4332.954000, end 4570.775000
    Metadata:
      title           : Chapter 12
    Chapter #0:13: start 4570.775000, end 5030.817000
    Metadata:
      title           : Chapter 13
    Chapter #0:14: start 5030.817000, end 5409.029000
    Metadata:
      title           : Chapter 14
    Chapter #0:15: start 5409.029000, end 5946.941000
    Metadata:
      title           : Chapter 15
    Chapter #0:16: start 5946.941000, end 6170.875000
    Metadata:
      title           : Chapter 16
    Chapter #0:17: start 6170.875000, end 6559.658000

And they are converted in frames:

[18:30:13] sync: "Opening Credits" (1) at frame 1 time 0
[18:30:31] sync: "Chapter 2" (2) at frame 1694 time 6355098 (correct frame number is 1692)
[18:31:38] sync: "Chapter 3" (3) at frame 7650 time 28712433
[18:33:52] sync: "Chapter 4" (4) at frame 19046 time 71490168
[18:35:08] sync: "Chapter 5" (5) at frame 25772 time 96737891
[18:37:33] sync: "Chapter 6" (6) at frame 38348 time 143945051
[18:38:46] sync: "Chapter 7" (7) at frame 44697 time 167777610
[18:41:12] sync: "Chapter 8" (8) at frame 57399 time 215457742
[18:42:40] sync: "Chapter 9" (9) at frame 64779 time 243160417
[18:44:56] sync: "Chapter 10" (10) at frame 76675 time 287815027
[18:46:45] sync: "Chapter 11" (11) at frame 86059 time 323040217
[18:48:22] sync: "Chapter 12" (12) at frame 94527 time 354826972
[18:50:11] sync: "Chapter 13" (13) at frame 103888 time 389965826
[18:51:18] sync: "Chapter 14" (14) at frame 109590 time 411369708
[18:53:23] sync: "Chapter 15" (15) at frame 120620 time 452773571
[18:55:10] sync: "Chapter 16" (16) at frame 129688 time 486812576
[18:57:41] sync: "Chapter 17" (17) at frame 142587 time 535232197
[18:58:43] sync: "Closing Credits" (18) at frame 147954 time 555378573

All frame numbers are larger than should be.
And to be clear, chapter times are not times of last frame of previous chapters, but times of firs frame of that chapter.

Lets explain on example for beginning of second chapter:

Chapter #0:1: start 70.584000

Calculation for frame number at given time position is:
Frame number = INT ( Time * FPS ) + 1
For first frame in file it would be INT ( 0 * anything) + 1 = 1

As frame rate (FPS) is 23.976 (24000/1001) then frame number for second chapter from this example is:
INT (70.548 * 23.976 ) + 1 = INT (1691.458848) + 1 = 1961 +1 = 1692
In log file it is:

[18:30:31] sync: "Chapter 2" (2) at frame 1694 time 6355098

Same is for all other chapter marks.
They are all calculated few frames AFTER exact chapter mark position.
And same could be seen in my second post.

Now, as chapter mark is usually placed at scene change which is detected by encoder, which then, by default creates key frame at that point, and then from program gets request to generate NEXT frame as key frame, request is refused because there could not be two consecutive (or too close) key frames.
It must have some limit in minimal number of frames between two key frames.

After that program waits for next key frame and moves chapter mark to that position, which is usually few seconds further.

To complete story...
As all original chapters in this example are placed EXACTLY at first frame AFTER scene change, which is always key (I) frame, when I put original chapters to new (encoded) file, then again every chapter mark is exactly on key frame and seeking is PERFECT.
So, IMHO, best solution is to copy chapters as they are from source file, until calculation is solved and tested.

Also, in same "HandBrake Activity Log" is line:

Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x808 [SAR 1:1 DAR 240:101], SAR 202:205 DAR 96:41, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)

So correct frame rate (fps) 23.976 is "rounded" to two decimals places as 23.98 fps, and if calculations are made with this value, than it brings further error in calculation of exact chapter frame position.

Another problem that bothers me is RENAMING all chapters that are named as "Chapter" followed by number, which could be again seen from this example.
I think that it's not good practice and should be removed, or at least given as option in preferences.
That is one more reason why I MUST replace complete chapters with those from source file every time.
Program should not change anything against user will.

@sr55 sr55 changed the title Chapters timings messed up VCE: Chapters timings messed up Jan 5, 2019

@sr55 sr55 added this to the 1.2.1 milestone Jan 5, 2019

@Xaymar

This comment has been minimized.

Copy link

Xaymar commented Jan 6, 2019

GPUOpen-LibrariesAndSDKs/AMF#85 also doesn't bode well ...

That issue is purely for H265. H264 will work fine, unless you want to insert forcefully skipped frames. Though I'm not aware of a way to force the frame type in ffmpeg (haven't spent a lot of time on it after AMD submitted it).

@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Jan 6, 2019

Though I'm not aware of a way to force the frame type in ffmpeg

When preparing an AVFrame to encode,
frame.pict_type = AV_PICTURE_TYPE_I;

Some encoders (e.g. nvenc) also require an AVOption during encoder initialization to tell them to force IDR frames when AV_PICTURE_TYPE_I is specified. ffmpeg does nothing with the pict_type field for the VCE encoder. So either there is no capability to do this in the underlying VCE API or it was just overlooked when the ffmpeg shim was written.

@Xaymar

This comment has been minimized.

Copy link

Xaymar commented Jan 14, 2019

I'll see if I can add support for it to the AMD part of ffmpeg. No promises though.

@passat1

This comment has been minimized.

Copy link

passat1 commented Jan 14, 2019

Thanks for response.
It would be great if support for VCE in ffmpeg could be improved.
It would also be nice if somebody could check that calculation of conversion from chapter times to frame numbers, and to confirm/deny my statement in #1762 (comment)
Not being able to force I frame at given position is problem, but as I see it, it's not only problem in this chain of events.
Also extra options from HB "preanalysis" and "vbaq" are not processed by encoder at all, but that's another story. I don't know if it is problem in HB or ffmpeg.

@Xaymar

This comment has been minimized.

Copy link

Xaymar commented Jan 14, 2019

The options Preanalysis and VBAQ are definitely being applied, but their effects are basically indistinguishable for 99.9% of the footage, only doing very little to help. In fact I've measured both actually resulting in a negative effect on quality, which caused the GCN4 H264 encoder to drop further below x264 superfast in quality.

H264:

    if (ctx->rate_control_mode == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP) {
        AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_RATE_CONTROL_PREANALYSIS_ENABLE, AMF_VIDEO_ENCODER_PREENCODE_DISABLED);
        if (ctx->preanalysis)
            av_log(ctx, AV_LOG_WARNING, "Pre-Analysis is not supported by cqp Rate Control Method, automatically disabled\n");
    } else {
        AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_RATE_CONTROL_PREANALYSIS_ENABLE, ctx->preanalysis);
    }
...
    if (ctx->rate_control_mode == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP) {
        AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_ENABLE_VBAQ, 0);
        if (ctx->enable_vbaq)
            av_log(ctx, AV_LOG_WARNING, "VBAQ is not supported by cqp Rate Control Method, automatically disabled\n");
    } else {
        AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_ENABLE_VBAQ, !!ctx->enable_vbaq);
    }

H265:

AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_PREANALYSIS_ENABLE, ctx->preanalysis);

    if (ctx->rate_control_mode == AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CONSTANT_QP) {
        AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_ENABLE_VBAQ, false);
        if (ctx->enable_vbaq)
            av_log(ctx, AV_LOG_WARNING, "VBAQ is not supported by cqp Rate Control Method, automatically disabled\n");
    } else {
        AMF_ASSIGN_PROPERTY_BOOL(res, ctx->encoder, AMF_VIDEO_ENCODER_HEVC_ENABLE_VBAQ, !!ctx->enable_vbaq);
    }

VBAQ and PreAnalysis do not have any effect when used on Constant QP encoding, since they affect the compression quantizer, which in those cases are fixed to a specific value. I believe this was done to avoid a failure to initialize the encoder and actually does print a log line if it is replaced. The behavior however should be identical for both H264 and H265, which it currently isn't - probably an oversight or not actually necessary anymore with recent drivers.

@passat1

This comment has been minimized.

Copy link

passat1 commented Jan 14, 2019

Thanks for reply.

So, as I understand from this code, if QP is used and VBAQ and/or Preanalysis are set in Extra Options:

preanalysis=1:vbaq=1

and in log file it looks like this

[15:24:29]    + encoder: H.264 (AMD VCE)
[15:24:29]      + preset:  quality
[15:24:29]      + options: preanalysis=1:vbaq=1
[15:24:29]      + profile: high
[15:24:29]      + level:   4.1
[15:24:29]      + quality: 22.00 (QP)
[15:24:29]      + color profile: 1-1-1

then in log file I should have messages

Pre-Analysis is not supported by cqp Rate Control Method, automatically disabled\n

and/or

VBAQ is not supported by cqp Rate Control Method, automatically disabled\n

But I have no such message in log:

[15:24:29] job configuration:
[15:24:29]  * source
[15:24:29]    + D:\Videos\DOCU\_NOVO\Curiosity (2018) - Breakthrough - Drilling for Marsquakes; The InSight Lander.mkv
[15:24:29]    + title 1, chapter(s) 1 to 1
[15:24:29]    + container: matroska,webm
[15:24:29]    + data rate: 3650 kbps
[15:24:29]  * destination
[15:24:29]    + E:\TEMP\Curiosity (2018) - Breakthrough - Drilling for Marsquakes; The InSight Lander.mp4
[15:24:29]    + container: MPEG-4 (libavformat)
[15:24:29]  * video track
[15:24:29]    + decoder: h264
[15:24:29]    + filters
[15:24:29]      + Framerate Shaper (mode=1)
[15:24:29]        + frame rate: 23.976 fps -> constant 23.976 fps
[15:24:29]      + Crop and Scale (width=1920:height=1080:crop-top=0:crop-bottom=0:crop-left=0:crop-right=0)
[15:24:29]        + source: 1920 * 1080, crop (0/0/0/0): 1920 * 1080, scale: 1920 * 1080
[15:24:29]    + Output geometry
[15:24:29]      + storage dimensions: 1920 x 1080
[15:24:29]      + pixel aspect ratio: 1 : 1
[15:24:29]      + display dimensions: 1920 x 1080
[15:24:29]    + encoder: H.264 (AMD VCE)
[15:24:29]      + preset:  quality
[15:24:29]      + options: preanalysis=1:vbaq=1
[15:24:29]      + profile: high
[15:24:29]      + level:   4.1
[15:24:29]      + quality: 22.00 (QP)
[15:24:29]      + color profile: 1-1-1
[15:24:29]  * audio track 1
[15:24:29]    + decoder: English (AAC LC) (2.0 ch) (track 1, id 0x1)
[15:24:29]      + samplerate: 44100 Hz
[15:24:29]    + mixdown: Stereo
[15:24:29]    + dither: none
[15:24:29]    + encoder: AAC (libavcodec)
[15:24:29]      + bitrate: 192 kbps, samplerate: 44100 Hz
[15:24:29]  * audio track 2
[15:24:29]    + decoder: English (AAC LC) (2.0 ch) (track 1, id 0x1)
[15:24:29]      + samplerate: 44100 Hz
[15:24:29]    + AAC Passthru
[15:24:29] sync: expecting 13940 video frames
[15:24:29] encavcodecInit: H.264 (AMD VCE)
[15:24:29] encavcodec: encoding at QP 22.00
[15:24:29] encavcodec: encoding with stored aspect 1/1
[15:24:30] sync: first pts video is 0
[15:24:30] sync: "Chapter 1" (1) at frame 1 time 0
[15:24:30] sync: first pts audio 0x1 is 0
[15:24:30] sync: first pts audio 0x1 is 0
[15:29:43] reader: done. 1 scr changes
[15:29:45] work: average encoding speed for job is 44.354992 fps
[15:29:45] vfr: 13938 frames output, 0 dropped and 0 duped for CFR/PFR
[15:29:45] vfr: lost time: 0 (0 frames)
[15:29:45] vfr: gained time: 0 (0 frames) (0 not accounted for)
[15:29:45] aac-decoder done: 25040 frames, 0 decoder errors
[15:29:45] aac-decoder done: 25040 frames, 0 decoder errors
[15:29:45] h264-decoder done: 13938 frames, 0 decoder errors
[15:29:45] sync: got 13938 frames, 13940 expected
[15:29:45] sync: framerate min 23.981 fps, max 23.981 fps, avg 23.976 fps
[15:29:45] mux: track 0, 13938 frames, 341062215 bytes, 4692.53 kbps, fifo 2048
[15:29:45] mux: track 1, 25041 frames, 14035240 bytes, 193.10 kbps, fifo 4096
[15:29:45] mux: track 2, 25040 frames, 12273285 bytes, 168.86 kbps, fifo 4096
[15:29:45] libhb: work result = 0

# Encode Completed ...

Like those options are not passed at all.
Did I misunderstood something or am I doing something wrong?

Also if I use Avg Bitrate with same options, then output files with and without those options have exactly same size.

@Xaymar

This comment has been minimized.

Copy link

Xaymar commented Jan 14, 2019

Unfortunately I can't comment further on that issue, as I only know the ffmpeg code. I do not know how Handbrake handles these cases, or if Handbrake doesn't set the proper fields in AmfContext for this.

The effect of VBAQ and Preanalysis isn't perfectly visible, and often situational. And if you leave filler data active, you will not see a single bitrate variation anyway, as it will always fill up to match the target bitrate.

@passat1

This comment has been minimized.

Copy link

passat1 commented Jan 14, 2019

Thanks Xaymar.
As you know, OBS had similar problems with VBAQ and Preanalysis not working, but they solved it after AMD Crimson 17.7.2 release, so I updated to that version with no change in HB.
But that is out of this topic.

@Xaymar

This comment has been minimized.

Copy link

Xaymar commented Jan 14, 2019

I've submitted the patch to ffmpeg. Edit: Updated to reflect newest changes.

@passat1

This comment has been minimized.

Copy link

passat1 commented Jan 14, 2019

Thanks.
Hopefully this will help solving problem in HB.

@jstebbins

This comment has been minimized.

Copy link
Contributor

jstebbins commented Jan 15, 2019

Cool. I'll create a PR for HandBrake that includes this patch so that it can be tested

@jstebbins jstebbins referenced this issue Jan 15, 2019

Open

Fix chapter timing with AMD VCE #1801

0 of 1 task complete

jstebbins added a commit to jstebbins/HandBrake that referenced this issue Jan 15, 2019

encavcodec: set frame.key_frame for AMD VCE encoder
AMD VCE encoder needs this in order to force an IDR rather than an I
frame.

Fixes HandBrake#1762
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment