Log in

View Full Version : Matching encoding options to concatenate streams


cretindesalpes
16th August 2009, 12:31
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 (http://forum.doom9.org/showthread.php?t=124645) 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):
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.

______

*** 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
[...]

MatLz
16th August 2009, 15:39
I think equal number of bframes is required, same parameters for cabac and trellis too maybe...

cretindesalpes
16th August 2009, 17:47
I think equal number of bframes is required, same parameters for cabac and trellis too maybe...
But...
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.

MatLz
16th August 2009, 18:04
Use mediainfo. It gives the encoding settings.

DeeGee
16th August 2009, 18:42
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?

Dark Shikari
16th August 2009, 19:41
Or just change the SPS-ID in the new encoded sequence so that you can concatenate them without matching the settings...

cretindesalpes
16th August 2009, 19:45
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 ?

Dark Shikari
16th August 2009, 19:48
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.

cretindesalpes
16th August 2009, 19:57
Great, -level 41 works, and I'm going to try your suggestion for the SPS-ID (or PPS-ID ?).

creamyhorror
16th August 2009, 20:06
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.)

Dark Shikari
16th August 2009, 20:31
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.

creamyhorror
16th August 2009, 20:41
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.

cretindesalpes
16th August 2009, 22:14
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.

Dark Shikari
16th August 2009, 22:18
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" ;)