1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2011 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
27 #include "common/common.h"
30 #define bs_write_ue bs_write_ue_big
32 // Indexed by pic_struct values
33 static const uint8_t num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
35 static void transpose( uint8_t *buf, int w )
37 for( int i = 0; i < w; i++ )
38 for( int j = 0; j < i; j++ )
39 XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
42 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
44 const int len = idx<4 ? 16 : 64;
45 const uint8_t *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
46 const uint8_t *list = pps->scaling_list[idx];
47 const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
48 : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
50 if( !memcmp( list, def_list, len ) )
51 bs_write1( s, 0 ); // scaling_list_present_flag
52 else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
54 bs_write1( s, 1 ); // scaling_list_present_flag
55 bs_write_se( s, -8 ); // use jvt list
60 bs_write1( s, 1 ); // scaling_list_present_flag
62 // try run-length compression of trailing values
63 for( run = len; run > 1; run-- )
64 if( list[zigzag[run-1]] != list[zigzag[run-2]] )
66 if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) )
69 for( int j = 0; j < run; j++ )
70 bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta
73 bs_write_se( s, (int8_t)-list[zigzag[run]] );
77 void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type )
83 for( i = 0; i <= payload_type-255; i += 255 )
84 bs_write( s, 8, 255 );
85 bs_write( s, 8, payload_type-i );
87 for( i = 0; i <= payload_size-255; i += 255 )
88 bs_write( s, 8, 255 );
89 bs_write( s, 8, payload_size-i );
91 for( i = 0; i < payload_size; i++ )
92 bs_write(s, 8, payload[i] );
94 bs_rbsp_trailing( s );
98 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
101 sps->i_mb_width = ( param->i_width + 15 ) / 16;
102 sps->i_mb_height= ( param->i_height + 15 ) / 16;
104 sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
105 if( sps->b_qpprime_y_zero_transform_bypass )
106 sps->i_profile_idc = PROFILE_HIGH444_PREDICTIVE;
107 else if( BIT_DEPTH > 8 )
108 sps->i_profile_idc = PROFILE_HIGH10;
109 else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
110 sps->i_profile_idc = PROFILE_HIGH;
111 else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
112 sps->i_profile_idc = PROFILE_MAIN;
114 sps->i_profile_idc = PROFILE_BASELINE;
116 sps->b_constraint_set0 = sps->i_profile_idc == PROFILE_BASELINE;
117 /* x264 doesn't support the features that are in Baseline and not in Main,
118 * namely arbitrary_slice_order and slice_groups. */
119 sps->b_constraint_set1 = sps->i_profile_idc <= PROFILE_MAIN;
120 /* Never set constraint_set2, it is not necessary and not used in real world. */
121 sps->b_constraint_set2 = 0;
122 sps->b_constraint_set3 = 0;
124 sps->i_level_idc = param->i_level_idc;
125 if( param->i_level_idc == 9 && ( sps->i_profile_idc >= PROFILE_BASELINE && sps->i_profile_idc <= PROFILE_EXTENDED ) )
127 sps->b_constraint_set3 = 1; /* level 1b with Baseline, Main or Extended profile is signalled via constraint_set3 */
128 sps->i_level_idc = 11;
130 /* High 10 Intra profile */
131 if( param->i_keyint_max == 1 && sps->i_profile_idc == PROFILE_HIGH10 )
132 sps->b_constraint_set3 = 1;
134 sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
135 /* extra slot with pyramid so that we don't have to override the
136 * order of forgetting old pictures */
137 sps->vui.i_max_dec_frame_buffering =
138 sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
139 param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
140 sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
141 if( param->i_keyint_max == 1 )
143 sps->i_num_ref_frames = 0;
144 sps->vui.i_max_dec_frame_buffering = 0;
147 /* number of refs + current frame */
148 int max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
149 /* Intra refresh cannot write a recovery time greater than max frame num-1 */
150 if( param->b_intra_refresh )
152 int time_to_recovery = X264_MIN( sps->i_mb_width - 1, param->i_keyint_max ) + param->i_bframe - 1;
153 max_frame_num = X264_MAX( max_frame_num, time_to_recovery+1 );
156 sps->i_log2_max_frame_num = 4;
157 while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
158 sps->i_log2_max_frame_num++;
160 sps->i_poc_type = param->i_bframe || param->b_interlaced ? 0 : 2;
161 if( sps->i_poc_type == 0 )
163 int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
164 sps->i_log2_max_poc_lsb = 4;
165 while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
166 sps->i_log2_max_poc_lsb++;
168 else if( sps->i_poc_type == 1 )
173 sps->b_delta_pic_order_always_zero = 1;
174 sps->i_offset_for_non_ref_pic = 0;
175 sps->i_offset_for_top_to_bottom_field = 0;
176 sps->i_num_ref_frames_in_poc_cycle = 0;
178 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
180 sps->i_offset_for_ref_frame[i] = 0;
186 sps->b_gaps_in_frame_num_value_allowed = 0;
187 sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
188 if( !sps->b_frame_mbs_only )
189 sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
190 sps->b_mb_adaptive_frame_field = param->b_interlaced;
191 sps->b_direct8x8_inference = 1;
193 sps->crop.i_left = param->crop_rect.i_left;
194 sps->crop.i_top = param->crop_rect.i_top;
195 sps->crop.i_right = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
196 sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
197 sps->b_crop = sps->crop.i_left || sps->crop.i_top ||
198 sps->crop.i_right || sps->crop.i_bottom;
200 sps->vui.b_aspect_ratio_info_present = 0;
201 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
203 sps->vui.b_aspect_ratio_info_present = 1;
204 sps->vui.i_sar_width = param->vui.i_sar_width;
205 sps->vui.i_sar_height= param->vui.i_sar_height;
208 sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 );
209 if( sps->vui.b_overscan_info_present )
210 sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
212 sps->vui.b_signal_type_present = 0;
213 sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
214 sps->vui.b_fullrange = ( param->vui.b_fullrange ? 1 : 0 );
215 sps->vui.b_color_description_present = 0;
217 sps->vui.i_colorprim = ( param->vui.i_colorprim <= 9 ? param->vui.i_colorprim : 2 );
218 sps->vui.i_transfer = ( param->vui.i_transfer <= 11 ? param->vui.i_transfer : 2 );
219 sps->vui.i_colmatrix = ( param->vui.i_colmatrix <= 9 ? param->vui.i_colmatrix : 2 );
220 if( sps->vui.i_colorprim != 2 ||
221 sps->vui.i_transfer != 2 ||
222 sps->vui.i_colmatrix != 2 )
224 sps->vui.b_color_description_present = 1;
227 if( sps->vui.i_vidformat != 5 ||
228 sps->vui.b_fullrange ||
229 sps->vui.b_color_description_present )
231 sps->vui.b_signal_type_present = 1;
234 /* FIXME: not sufficient for interlaced video */
235 sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 );
236 if( sps->vui.b_chroma_loc_info_present )
238 sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
239 sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
242 sps->vui.b_timing_info_present = param->i_timebase_num > 0 && param->i_timebase_den > 0;
244 if( sps->vui.b_timing_info_present )
246 sps->vui.i_num_units_in_tick = param->i_timebase_num;
247 sps->vui.i_time_scale = param->i_timebase_den * 2;
248 sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
251 sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
252 sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
253 sps->vui.b_pic_struct_present = param->b_pic_struct;
255 // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
257 sps->vui.b_bitstream_restriction = 1;
258 if( sps->vui.b_bitstream_restriction )
260 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
261 sps->vui.i_max_bytes_per_pic_denom = 0;
262 sps->vui.i_max_bits_per_mb_denom = 0;
263 sps->vui.i_log2_max_mv_length_horizontal =
264 sps->vui.i_log2_max_mv_length_vertical = (int)log2f( X264_MAX( 1, param->analyse.i_mv_range*4-1 ) ) + 1;
268 void x264_sps_write( bs_t *s, x264_sps_t *sps )
271 bs_write( s, 8, sps->i_profile_idc );
272 bs_write1( s, sps->b_constraint_set0 );
273 bs_write1( s, sps->b_constraint_set1 );
274 bs_write1( s, sps->b_constraint_set2 );
275 bs_write1( s, sps->b_constraint_set3 );
277 bs_write( s, 4, 0 ); /* reserved */
279 bs_write( s, 8, sps->i_level_idc );
281 bs_write_ue( s, sps->i_id );
283 if( sps->i_profile_idc >= PROFILE_HIGH )
285 bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
286 bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
287 bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
288 bs_write1( s, sps->b_qpprime_y_zero_transform_bypass );
289 bs_write1( s, 0 ); // seq_scaling_matrix_present_flag
292 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
293 bs_write_ue( s, sps->i_poc_type );
294 if( sps->i_poc_type == 0 )
296 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
298 else if( sps->i_poc_type == 1 )
300 bs_write1( s, sps->b_delta_pic_order_always_zero );
301 bs_write_se( s, sps->i_offset_for_non_ref_pic );
302 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
303 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
305 for( int i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
306 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
308 bs_write_ue( s, sps->i_num_ref_frames );
309 bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
310 bs_write_ue( s, sps->i_mb_width - 1 );
311 bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
312 bs_write1( s, sps->b_frame_mbs_only );
313 if( !sps->b_frame_mbs_only )
314 bs_write1( s, sps->b_mb_adaptive_frame_field );
315 bs_write1( s, sps->b_direct8x8_inference );
317 bs_write1( s, sps->b_crop );
320 bs_write_ue( s, sps->crop.i_left / 2 );
321 bs_write_ue( s, sps->crop.i_right / 2 );
322 bs_write_ue( s, sps->crop.i_top / 2 );
323 bs_write_ue( s, sps->crop.i_bottom / 2 );
326 bs_write1( s, sps->b_vui );
329 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
330 if( sps->vui.b_aspect_ratio_info_present )
333 static const struct { uint8_t w, h, sar; } sar[] =
335 // aspect_ratio_idc = 0 -> unspecified
336 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
337 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
338 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
339 {160, 99, 13}, { 4, 3, 14}, { 3, 2, 15}, { 2, 1, 16},
340 // aspect_ratio_idc = [17..254] -> reserved
343 for( i = 0; sar[i].sar != 255; i++ )
345 if( sar[i].w == sps->vui.i_sar_width &&
346 sar[i].h == sps->vui.i_sar_height )
349 bs_write( s, 8, sar[i].sar );
350 if( sar[i].sar == 255 ) /* aspect_ratio_idc (extended) */
352 bs_write( s, 16, sps->vui.i_sar_width );
353 bs_write( s, 16, sps->vui.i_sar_height );
357 bs_write1( s, sps->vui.b_overscan_info_present );
358 if( sps->vui.b_overscan_info_present )
359 bs_write1( s, sps->vui.b_overscan_info );
361 bs_write1( s, sps->vui.b_signal_type_present );
362 if( sps->vui.b_signal_type_present )
364 bs_write( s, 3, sps->vui.i_vidformat );
365 bs_write1( s, sps->vui.b_fullrange );
366 bs_write1( s, sps->vui.b_color_description_present );
367 if( sps->vui.b_color_description_present )
369 bs_write( s, 8, sps->vui.i_colorprim );
370 bs_write( s, 8, sps->vui.i_transfer );
371 bs_write( s, 8, sps->vui.i_colmatrix );
375 bs_write1( s, sps->vui.b_chroma_loc_info_present );
376 if( sps->vui.b_chroma_loc_info_present )
378 bs_write_ue( s, sps->vui.i_chroma_loc_top );
379 bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
382 bs_write1( s, sps->vui.b_timing_info_present );
383 if( sps->vui.b_timing_info_present )
385 bs_write32( s, sps->vui.i_num_units_in_tick );
386 bs_write32( s, sps->vui.i_time_scale );
387 bs_write1( s, sps->vui.b_fixed_frame_rate );
390 bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
391 if( sps->vui.b_nal_hrd_parameters_present )
393 bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
394 bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
395 bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
397 bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
398 bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
400 bs_write1( s, sps->vui.hrd.b_cbr_hrd );
402 bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
403 bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
404 bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
405 bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
408 bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
410 if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
411 bs_write1( s, 0 ); /* low_delay_hrd_flag */
413 bs_write1( s, sps->vui.b_pic_struct_present );
414 bs_write1( s, sps->vui.b_bitstream_restriction );
415 if( sps->vui.b_bitstream_restriction )
417 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
418 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
419 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
420 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
421 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
422 bs_write_ue( s, sps->vui.i_num_reorder_frames );
423 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
427 bs_rbsp_trailing( s );
431 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
434 pps->i_sps_id = sps->i_id;
435 pps->b_cabac = param->b_cabac;
437 pps->b_pic_order = param->b_interlaced;
438 pps->i_num_slice_groups = 1;
440 pps->i_num_ref_idx_l0_default_active = param->i_frame_reference;
441 pps->i_num_ref_idx_l1_default_active = 1;
443 pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
444 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
446 pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR ? 26 + QP_BD_OFFSET : SPEC_QP( param->rc.i_qp_constant );
447 pps->i_pic_init_qs = 26 + QP_BD_OFFSET;
449 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
450 pps->b_deblocking_filter_control = 1;
451 pps->b_constrained_intra_pred = param->b_constrained_intra;
452 pps->b_redundant_pic_cnt = 0;
454 pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
456 pps->i_cqm_preset = param->i_cqm_preset;
457 switch( pps->i_cqm_preset )
460 for( int i = 0; i < 6; i++ )
461 pps->scaling_list[i] = x264_cqm_flat16;
464 for( int i = 0; i < 6; i++ )
465 pps->scaling_list[i] = x264_cqm_jvt[i];
467 case X264_CQM_CUSTOM:
468 /* match the transposed DCT & zigzag */
469 transpose( param->cqm_4iy, 4 );
470 transpose( param->cqm_4ic, 4 );
471 transpose( param->cqm_4py, 4 );
472 transpose( param->cqm_4pc, 4 );
473 transpose( param->cqm_8iy, 8 );
474 transpose( param->cqm_8py, 8 );
475 pps->scaling_list[CQM_4IY] = param->cqm_4iy;
476 pps->scaling_list[CQM_4IC] = param->cqm_4ic;
477 pps->scaling_list[CQM_4PY] = param->cqm_4py;
478 pps->scaling_list[CQM_4PC] = param->cqm_4pc;
479 pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
480 pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
481 for( int i = 0; i < 6; i++ )
482 for( int j = 0; j < (i < 4 ? 16 : 64); j++ )
483 if( pps->scaling_list[i][j] == 0 )
484 pps->scaling_list[i] = x264_cqm_jvt[i];
489 void x264_pps_write( bs_t *s, x264_pps_t *pps )
492 bs_write_ue( s, pps->i_id );
493 bs_write_ue( s, pps->i_sps_id );
495 bs_write1( s, pps->b_cabac );
496 bs_write1( s, pps->b_pic_order );
497 bs_write_ue( s, pps->i_num_slice_groups - 1 );
499 bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
500 bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
501 bs_write1( s, pps->b_weighted_pred );
502 bs_write( s, 2, pps->b_weighted_bipred );
504 bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
505 bs_write_se( s, pps->i_pic_init_qs - 26 - QP_BD_OFFSET );
506 bs_write_se( s, pps->i_chroma_qp_index_offset );
508 bs_write1( s, pps->b_deblocking_filter_control );
509 bs_write1( s, pps->b_constrained_intra_pred );
510 bs_write1( s, pps->b_redundant_pic_cnt );
512 if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
514 bs_write1( s, pps->b_transform_8x8_mode );
515 bs_write1( s, (pps->i_cqm_preset != X264_CQM_FLAT) );
516 if( pps->i_cqm_preset != X264_CQM_FLAT )
518 scaling_list_write( s, pps, CQM_4IY );
519 scaling_list_write( s, pps, CQM_4IC );
520 bs_write1( s, 0 ); // Cr = Cb
521 scaling_list_write( s, pps, CQM_4PY );
522 scaling_list_write( s, pps, CQM_4PC );
523 bs_write1( s, 0 ); // Cr = Cb
524 if( pps->b_transform_8x8_mode )
526 scaling_list_write( s, pps, CQM_8IY+4 );
527 scaling_list_write( s, pps, CQM_8PY+4 );
530 bs_write_se( s, pps->i_chroma_qp_index_offset );
533 bs_rbsp_trailing( s );
537 void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
540 uint8_t tmp_buf[100];
541 bs_init( &q, tmp_buf, 100 );
545 bs_write_ue( &q, recovery_frame_cnt ); // recovery_frame_cnt
546 bs_write1( &q, 1 ); //exact_match_flag 1
547 bs_write1( &q, 0 ); //broken_link_flag 0
548 bs_write( &q, 2, 0 ); //changing_slice_group 0
553 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_RECOVERY_POINT );
557 int x264_sei_version_write( x264_t *h, bs_t *s )
559 // random ID number generated according to ISO-11578
560 static const uint8_t uuid[16] =
562 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
563 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
565 char *opts = x264_param2string( &h->param, 0 );
571 CHECKED_MALLOC( payload, 200 + strlen( opts ) );
573 memcpy( payload, uuid, 16 );
574 sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
575 "Copy%s 2003-2011 - http://www.videolan.org/x264.html - options: %s",
576 X264_BUILD, X264_VERSION, HAVE_GPL?"left":"right", opts );
577 length = strlen(payload)+1;
579 x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
582 x264_free( payload );
589 void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
591 x264_sps_t *sps = h->sps;
593 uint8_t tmp_buf[100];
594 bs_init( &q, tmp_buf, 100 );
597 bs_write_ue( &q, sps->i_id );
599 if( sps->vui.b_nal_hrd_parameters_present )
601 bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
602 bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
608 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_BUFFERING_PERIOD );
611 void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
613 x264_sps_t *sps = h->sps;
615 uint8_t tmp_buf[100];
616 bs_init( &q, tmp_buf, 100 );
620 if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
622 bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay - h->i_cpb_delay_pir_offset );
623 bs_write( &q, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
626 if( sps->vui.b_pic_struct_present )
628 bs_write( &q, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
630 // These clock timestamps are not standardised so we don't set them
631 // They could be time of origin, capture or alternative ideal display
632 for( int i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
633 bs_write1( &q, 0 ); // clock_timestamp_flag
639 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_PIC_TIMING );
642 void x264_sei_frame_packing_write( x264_t *h, bs_t *s )
645 uint8_t tmp_buf[100];
646 bs_init( &q, tmp_buf, 100 );
650 bs_write_ue( &q, 0 ); // frame_packing_arrangement_id
651 bs_write1( &q, 0 ); // frame_packing_arrangement_cancel_flag
652 bs_write ( &q, 7, h->param.i_frame_packing ); // frame_packing_arrangement_type
653 bs_write1( &q, 0 ); // quincunx_sampling_flag
655 // 0: views are unrelated, 1: left view is on the left, 2: left view is on the right
656 bs_write ( &q, 6, 1 ); // content_interpretation_type
658 bs_write1( &q, 0 ); // spatial_flipping_flag
659 bs_write1( &q, 0 ); // frame0_flipped_flag
660 bs_write1( &q, 0 ); // field_views_flag
661 bs_write1( &q, h->param.i_frame_packing == 5 && !(h->fenc->i_frame&1) ); // current_frame_is_frame0_flag
662 bs_write1( &q, 0 ); // frame0_self_contained_flag
663 bs_write1( &q, 0 ); // frame1_self_contained_flag
664 if ( /* quincunx_sampling_flag == 0 && */ h->param.i_frame_packing != 5 )
666 bs_write( &q, 4, 0 ); // frame0_grid_position_x
667 bs_write( &q, 4, 0 ); // frame0_grid_position_y
668 bs_write( &q, 4, 0 ); // frame1_grid_position_x
669 bs_write( &q, 4, 0 ); // frame1_grid_position_y
671 bs_write( &q, 8, 0 ); // frame_packing_arrangement_reserved_byte
672 bs_write_ue( &q, 1 ); // frame_packing_arrangement_repetition_period
673 bs_write1( &q, 0 ); // frame_packing_arrangement_extension_flag
678 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_FRAME_PACKING );
681 void x264_filler_write( x264_t *h, bs_t *s, int filler )
685 for( int i = 0; i < filler; i++ )
686 bs_write( s, 8, 0xff );
688 bs_rbsp_trailing( s );
692 const x264_level_t x264_levels[] =
694 { 10, 1485, 99, 152064, 64, 175, 64, 64, 0, 2, 0, 0, 1 },
695 { 9, 1485, 99, 152064, 128, 350, 64, 64, 0, 2, 0, 0, 1 }, /* "1b" */
696 { 11, 3000, 396, 345600, 192, 500, 128, 64, 0, 2, 0, 0, 1 },
697 { 12, 6000, 396, 912384, 384, 1000, 128, 64, 0, 2, 0, 0, 1 },
698 { 13, 11880, 396, 912384, 768, 2000, 128, 64, 0, 2, 0, 0, 1 },
699 { 20, 11880, 396, 912384, 2000, 2000, 128, 64, 0, 2, 0, 0, 1 },
700 { 21, 19800, 792, 1824768, 4000, 4000, 256, 64, 0, 2, 0, 0, 0 },
701 { 22, 20250, 1620, 3110400, 4000, 4000, 256, 64, 0, 2, 0, 0, 0 },
702 { 30, 40500, 1620, 3110400, 10000, 10000, 256, 32, 22, 2, 0, 1, 0 },
703 { 31, 108000, 3600, 6912000, 14000, 14000, 512, 16, 60, 4, 1, 1, 0 },
704 { 32, 216000, 5120, 7864320, 20000, 20000, 512, 16, 60, 4, 1, 1, 0 },
705 { 40, 245760, 8192, 12582912, 20000, 25000, 512, 16, 60, 4, 1, 1, 0 },
706 { 41, 245760, 8192, 12582912, 50000, 62500, 512, 16, 24, 2, 1, 1, 0 },
707 { 42, 522240, 8704, 13369344, 50000, 62500, 512, 16, 24, 2, 1, 1, 1 },
708 { 50, 589824, 22080, 42393600, 135000, 135000, 512, 16, 24, 2, 1, 1, 1 },
709 { 51, 983040, 36864, 70778880, 240000, 240000, 512, 16, 24, 2, 1, 1, 1 },
716 x264_log( h, X264_LOG_WARNING, __VA_ARGS__ );\
720 int x264_validate_levels( x264_t *h, int verbose )
723 int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
724 int dpb = mbs * 384 * h->sps->vui.i_max_dec_frame_buffering;
725 int cbp_factor = h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
726 h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
728 const x264_level_t *l = x264_levels;
729 while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
732 if( l->frame_size < mbs
733 || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
734 || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
735 ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
736 h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
738 ERROR( "DPB size (%d frames, %d bytes) > level limit (%d frames, %d bytes)\n",
739 h->sps->vui.i_max_dec_frame_buffering, dpb, (int)(l->dpb / (384*mbs)), l->dpb );
741 #define CHECK( name, limit, val ) \
742 if( (val) > (limit) ) \
743 ERROR( name " (%d) > level limit (%d)\n", (int)(val), (limit) );
745 CHECK( "VBV bitrate", (l->bitrate * cbp_factor) / 4, h->param.rc.i_vbv_max_bitrate );
746 CHECK( "VBV buffer", (l->cpb * cbp_factor) / 4, h->param.rc.i_vbv_buffer_size );
747 CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
748 CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
749 CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
751 if( h->param.i_fps_den > 0 )
752 CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
754 /* TODO check the rest of the limits */