Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Video Encoding > MPEG-4 AVC / H.264
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 16th August 2009, 12:31   #1  |  Link
cretindesalpes
͡҉҉ ̵̡̢̛̗̘̙̜̝̞̟̠͇̊̋̌̍̎̏̿̿
 
cretindesalpes's Avatar
 
Join Date: Feb 2009
Location: No support in PM
Posts: 712
Matching encoding options to concatenate streams

I'm trying to edit a H264 video stored in a MP4 container to replace a short sequence by one of my own. I use ffmpeg with libx264 to encode my new sequence. I use mp4box to split the original video into segments and concatenate them with the new sequence.

I encounter some difficulties during the concatenation. I read on this thread that the streams need to use exactly the same encoding options. However I don't know the options set to generate the source video. I ran h264_parse on both versions of the sequence I want to replace and obtained the following differences (full dump of the sps and pps sections at the end of the post):
Code:
Sequence parameter set:

[orig]   level_idc: 41
[mine]   level_idc: 40

[orig]   num_ref_frames: 4
[mine]   num_ref_frames: 6

[orig]     time_scale: 48000
[mine]     time_scale: 48002

[orig]    num_reorder_frames: 1
           max_dec_frame_buffering: 4
[mine]    num_reorder_frames: 2
           max_dec_frame_buffering: 6

Picture parameter set:

[orig]   pic_init_qp_minus26: -5
[mine]   pic_init_qp_minus26: 0
I don't know which options I should tweak in ffmpeg (I can use other tools if required, I'm operating on Windows) to make the parameter sets match and I would really like to avoid reencoding the whole video, if possible.

Any help appreciated.

______

Code:
*** Original ***

Nal length 29 start code 4 bytes 
 ref 3 type 7 Sequence parameter set
   profile: 100
   constaint_set0_flag: 0
   constaint_set1_flag: 0
   constaint_set2_flag: 0
   constaint_set3_flag: 0
   level_idc: 41
   seq parameter set id: 0
   chroma format idx: 1
   bit depth luma minus8: 0
   bit depth chroma minus8: 0
   Qpprime Y Zero Transform Bypass flag: 0
   Seq Scaling Matrix Present Flag: 0
   log2_max_frame_num_minus4: 5
   pic_order_cnt_type: 0
    log2_max_pic_order_cnt_lsb_minus4: 6
   num_ref_frames: 4
   gaps_in_frame_num_value_allowed_flag: 0
   pic_width_in_mbs_minus1: 79 (1280)
   pic_height_in_map_minus1: 44
   frame_mbs_only_flag: 1
     derived height: 720
   direct_8x8_inference_flag: 1
   frame_cropping_flag: 0
   vui_parameters_present_flag: 1
    aspect_ratio_info_present_flag: 1
     aspect_ratio_idc:1
    overscan_info_present_flag: 0
    video_signal_info_present_flag: 0
    chroma_loc_info_present_flag: 0
    timing_info_present_flag: 1
     num_units_in_tick: 1001
     time_scale: 48000
     fixed_frame_scale: 1
    nal_hrd_parameters_present_flag: 0
    vcl_hrd_parameters_present_flag: 0
    pic_struct_present_flag: 0
    motion_vectors_over_pic_boundaries_flag: 1
    max_bytes_per_pic_denom: 0
    max_bits_per_mb_denom: 0
    log2_max_mv_length_horizontal: 11
    log2_max_mv_length_vertical: 11
    num_reorder_frames: 1
     max_dec_frame_buffering: 4
Nal length 10 start code 4 bytes 
 ref 3 type 8 Picture parameter set
   pic_parameter_set_id: 0
   seq_parameter_set_id: 0
   entropy_coding_mode_flag: 1
   pic_order_present_flag: 0
   num_slice_groups_minus1: 0
   num_ref_idx_l0_active_minus1: 0
   num_ref_idx_l1_active_minus1: 0
   weighted_pred_flag: 0
   weighted_bipred_idc: 2
   pic_init_qp_minus26: -5
   pic_init_qs_minus26: 0
   chroma_qp_index_offset: -2
   deblocking_filter_control_present_flag: 1
   constrained_intra_pred_flag: 0
   redundant_pic_cnt_present_flag: 0
   transform_8x8_mode_flag: 1
   pic_scaling_matrix_present_flag: 0
   second_chroma_qp_index_offset: 4294967294
[...]

*** Mine ***

Nal length 29 start code 4 bytes 
 ref 3 type 7 Sequence parameter set
   profile: 100
   constaint_set0_flag: 0
   constaint_set1_flag: 0
   constaint_set2_flag: 0
   constaint_set3_flag: 0
   level_idc: 40
   seq parameter set id: 0
   chroma format idx: 1
   bit depth luma minus8: 0
   bit depth chroma minus8: 0
   Qpprime Y Zero Transform Bypass flag: 0
   Seq Scaling Matrix Present Flag: 0
   log2_max_frame_num_minus4: 5
   pic_order_cnt_type: 0
    log2_max_pic_order_cnt_lsb_minus4: 6
   num_ref_frames: 6
   gaps_in_frame_num_value_allowed_flag: 0
   pic_width_in_mbs_minus1: 79 (1280)
   pic_height_in_map_minus1: 44
   frame_mbs_only_flag: 1
     derived height: 720
   direct_8x8_inference_flag: 1
   frame_cropping_flag: 0
   vui_parameters_present_flag: 1
    aspect_ratio_info_present_flag: 1
     aspect_ratio_idc:1
    overscan_info_present_flag: 0
    video_signal_info_present_flag: 0
    chroma_loc_info_present_flag: 0
    timing_info_present_flag: 1
     num_units_in_tick: 1001
     time_scale: 48002
     fixed_frame_scale: 1
    nal_hrd_parameters_present_flag: 0
    vcl_hrd_parameters_present_flag: 0
    pic_struct_present_flag: 0
    motion_vectors_over_pic_boundaries_flag: 1
    max_bytes_per_pic_denom: 0
    max_bits_per_mb_denom: 0
    log2_max_mv_length_horizontal: 11
    log2_max_mv_length_vertical: 11
    num_reorder_frames: 2
     max_dec_frame_buffering: 6
Nal length 9 start code 4 bytes 
 ref 3 type 8 Picture parameter set
   pic_parameter_set_id: 0
   seq_parameter_set_id: 0
   entropy_coding_mode_flag: 1
   pic_order_present_flag: 0
   num_slice_groups_minus1: 0
   num_ref_idx_l0_active_minus1: 0
   num_ref_idx_l1_active_minus1: 0
   weighted_pred_flag: 0
   weighted_bipred_idc: 2
   pic_init_qp_minus26: 0
   pic_init_qs_minus26: 0
   chroma_qp_index_offset: -2
   deblocking_filter_control_present_flag: 1
   constrained_intra_pred_flag: 0
   redundant_pic_cnt_present_flag: 0
   transform_8x8_mode_flag: 1
   pic_scaling_matrix_present_flag: 0
   second_chroma_qp_index_offset: 4294967294
[...]

Last edited by cretindesalpes; 16th August 2009 at 12:38.
cretindesalpes is offline   Reply With Quote
Old 16th August 2009, 15:39   #2  |  Link
MatLz
I often say "maybe"...
 
MatLz's Avatar
 
Join Date: Jul 2009
Location: France
Posts: 583
I think equal number of bframes is required, same parameters for cabac and trellis too maybe...
MatLz is offline   Reply With Quote
Old 16th August 2009, 17:47   #3  |  Link
cretindesalpes
͡҉҉ ̵̡̢̛̗̘̙̜̝̞̟̠͇̊̋̌̍̎̏̿̿
 
cretindesalpes's Avatar
 
Join Date: Feb 2009
Location: No support in PM
Posts: 712
Quote:
Originally Posted by MatLz View Post
I think equal number of bframes is required, same parameters for cabac and trellis too maybe...
But...
Quote:
Originally Posted by cretindesalpes
I don't know the options set to generate the source video
So, how can I deduce the cabac, trellis, etc. values from the sps and pps information ? That was my question.
cretindesalpes is offline   Reply With Quote
Old 16th August 2009, 18:04   #4  |  Link
MatLz
I often say "maybe"...
 
MatLz's Avatar
 
Join Date: Jul 2009
Location: France
Posts: 583
Use mediainfo. It gives the encoding settings.
MatLz is offline   Reply With Quote
Old 16th August 2009, 18:42   #5  |  Link
DeeGee
Registered User
 
DeeGee's Avatar
 
Join Date: May 2004
Location: Finland
Posts: 102
That is if the video was encoded with x264. For other encoders it only tells what profile video is, if cabac was used, reframes bitrate and aspect ratio. I don't know, are there other free video tools that collect the info from the video stream itself?
DeeGee is offline   Reply With Quote
Old 16th August 2009, 19:41   #6  |  Link
Dark Shikari
x264 developer
 
Dark Shikari's Avatar
 
Join Date: Sep 2005
Posts: 8,666
Or just change the SPS-ID in the new encoded sequence so that you can concatenate them without matching the settings...
Dark Shikari is offline   Reply With Quote
Old 16th August 2009, 19:45   #7  |  Link
cretindesalpes
͡҉҉ ̵̡̢̛̗̘̙̜̝̞̟̠͇̊̋̌̍̎̏̿̿
 
cretindesalpes's Avatar
 
Join Date: Feb 2009
Location: No support in PM
Posts: 712
Thank you for your replies.

I almost got the job done:
  • level_idc: it seems that ffmpeg doesn't support explicit profile settings. I had to specify a huge bitrate to obtain High Profile 4.1 which is coded as 41. Maybe is there other, cleaner ways to achieve this ?
  • num_ref_frames and max_dec_frame_buffering: -refs 4
  • time_scale: -r 24000/1001 instead of 24001/1001...
  • num_reorder_frames: -bf 1
But I still miss the pic_init_qp_minus26 field, and I have no clue about it. Any thought ?
cretindesalpes is offline   Reply With Quote
Old 16th August 2009, 19:48   #8  |  Link
Dark Shikari
x264 developer
 
Dark Shikari's Avatar
 
Join Date: Sep 2005
Posts: 8,666
Quote:
Originally Posted by cretindesalpes View Post
level_idc: it seems that ffmpeg doesn't support explicit profile settings. I had to specify a huge bitrate to obtain High Profile 4.1 which is coded as 41. Maybe is there other, cleaner ways to achieve this ?
-level 41, but see my post above.
Dark Shikari is offline   Reply With Quote
Old 16th August 2009, 19:57   #9  |  Link
cretindesalpes
͡҉҉ ̵̡̢̛̗̘̙̜̝̞̟̠͇̊̋̌̍̎̏̿̿
 
cretindesalpes's Avatar
 
Join Date: Feb 2009
Location: No support in PM
Posts: 712
Great, -level 41 works, and I'm going to try your suggestion for the SPS-ID (or PPS-ID ?).
cretindesalpes is offline   Reply With Quote
Old 16th August 2009, 20:06   #10  |  Link
creamyhorror
Registered User
 
Join Date: Mar 2008
Posts: 118
Quote:
Originally Posted by Dark Shikari View Post
Or just change the SPS-ID in the new encoded sequence so that you can concatenate them without matching the settings...
Sorry if I'm misunderstanding, but from old posts it seems like x264 always uses the same SPS-ID, so if the original encode was done in x264, I take it changing the SPS-Id wouldn't be necessary?

Also, how do global headers factor into this? Should you (and how do you) rewrite the MKV or MP4 headers after concatenating like this...remux it using MKVtoolnix? (I heard some time ago that MKVmerge only read the first part of a concatenated stream, but that may have changed.)
creamyhorror is offline   Reply With Quote
Old 16th August 2009, 20:31   #11  |  Link
Dark Shikari
x264 developer
 
Dark Shikari's Avatar
 
Join Date: Sep 2005
Posts: 8,666
Quote:
Originally Posted by creamyhorror View Post
Sorry if I'm misunderstanding, but from old posts it seems like x264 always uses the same SPS-ID, so if the original encode was done in x264, I take it changing the SPS-Id wouldn't be necessary?
No, the point is that if you set a new SPS-ID, the new sequence can use whatever settings it wants, because it uses a different SPS than the original stream.
Dark Shikari is offline   Reply With Quote
Old 16th August 2009, 20:41   #12  |  Link
creamyhorror
Registered User
 
Join Date: Mar 2008
Posts: 118
Quote:
Originally Posted by Dark Shikari View Post
No, the point is that if you set a new SPS-ID, the new sequence can use whatever settings it wants, because it uses a different SPS than the original stream.
Oh, that's great. Thanks for the clarification.
creamyhorror is offline   Reply With Quote
Old 16th August 2009, 22:14   #13  |  Link
cretindesalpes
͡҉҉ ̵̡̢̛̗̘̙̜̝̞̟̠͇̊̋̌̍̎̏̿̿
 
cretindesalpes's Avatar
 
Join Date: Feb 2009
Location: No support in PM
Posts: 712
I had to switch to x264.exe to use --sps-id, because I couldn't find the equivalent function in ffmpeg. But the SPS-ID trick doen't impress mp4box, which still identifies the tracks as different and doesn't want to concatenate them. Finally I could set the right pic_init_qp_minus26 value by using --qp 21 and made mp4box happy.
cretindesalpes is offline   Reply With Quote
Old 16th August 2009, 22:18   #14  |  Link
Dark Shikari
x264 developer
 
Dark Shikari's Avatar
 
Join Date: Sep 2005
Posts: 8,666
Quote:
Originally Posted by cretindesalpes View Post
I had to switch to x264.exe to use --sps-id, because I couldn't find the equivalent function in ffmpeg. But the SPS-ID trick doen't impress mp4box, which still identifies the tracks as different and doesn't want to concatenate them. Finally I could set the right pic_init_qp_minus26 value by using --qp 21 and made mp4box happy.
You could just cat the .h264 streams together with, er, "cat"
Dark Shikari is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:35.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.