1 /*****************************************************************************
2 * set: h264 encoder (SPS and PPS init and write)
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: set.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
36 #include "common/common.h"
37 #include "common/macroblock.h"
42 static const uint8_t *const x264_cqm_jvt[6] =
44 x264_cqm_jvt4i, x264_cqm_jvt4p,
45 x264_cqm_jvt4i, x264_cqm_jvt4p,
46 x264_cqm_jvt8i, x264_cqm_jvt8p
49 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
51 const int len = idx<4 ? 16 : 64;
52 const int *zigzag = idx<4 ? x264_zigzag_scan4 : x264_zigzag_scan8;
53 const uint8_t *list = pps->scaling_list[idx];
54 const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
55 : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
58 if( memcmp( list, def_list, len ) )
60 bs_write( s, 1, 1 ); // scaling_list_present_flag
61 for( j = 0; j < len; j++ )
62 bs_write_se( s, list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8) ); // delta
65 bs_write( s, 1, 0 ); // scaling_list_present_flag
68 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
72 sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
73 if( sps->b_qpprime_y_zero_transform_bypass )
74 sps->i_profile_idc = PROFILE_HIGH444;
75 else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
76 sps->i_profile_idc = PROFILE_HIGH;
77 else if( param->b_cabac || param->i_bframe > 0 )
78 sps->i_profile_idc = PROFILE_MAIN;
80 sps->i_profile_idc = PROFILE_BASELINE;
81 sps->i_level_idc = param->i_level_idc;
83 sps->b_constraint_set0 = 0;
84 sps->b_constraint_set1 = 0;
85 sps->b_constraint_set2 = 0;
87 sps->i_log2_max_frame_num = 4; /* at least 4 */
88 while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
90 sps->i_log2_max_frame_num++;
92 sps->i_log2_max_frame_num++; /* just in case */
95 if( sps->i_poc_type == 0 )
97 sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
99 else if( sps->i_poc_type == 1 )
104 sps->b_delta_pic_order_always_zero = 1;
105 sps->i_offset_for_non_ref_pic = 0;
106 sps->i_offset_for_top_to_bottom_field = 0;
107 sps->i_num_ref_frames_in_poc_cycle = 0;
109 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
111 sps->i_offset_for_ref_frame[i] = 0;
115 sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
116 /* extra slot with pyramid so that we don't have to override the
117 * order of forgetting old pictures */
118 sps->vui.i_max_dec_frame_buffering =
119 sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames + param->b_bframe_pyramid);
121 sps->b_gaps_in_frame_num_value_allowed = 0;
122 sps->i_mb_width = ( param->i_width + 15 ) / 16;
123 sps->i_mb_height= ( param->i_height + 15 )/ 16;
124 sps->b_frame_mbs_only = 1;
125 sps->b_mb_adaptive_frame_field = 0;
126 sps->b_direct8x8_inference = 0;
127 if( sps->b_frame_mbs_only == 0 ||
128 !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
130 sps->b_direct8x8_inference = 1;
133 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
136 sps->crop.i_left = 0;
137 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
139 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
144 sps->crop.i_left = 0;
145 sps->crop.i_right = 0;
147 sps->crop.i_bottom = 0;
151 sps->vui.b_aspect_ratio_info_present = 0;
153 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
155 sps->vui.b_aspect_ratio_info_present = 1;
156 sps->vui.i_sar_width = param->vui.i_sar_width;
157 sps->vui.i_sar_height= param->vui.i_sar_height;
159 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
161 if( param->i_fps_num > 0 && param->i_fps_den > 0)
163 sps->vui.b_timing_info_present = 1;
164 /* The standard is confusing me, but this seems to work best
165 with other encoders */
166 sps->vui.i_num_units_in_tick = param->i_fps_den;
167 sps->vui.i_time_scale = param->i_fps_num;
168 sps->vui.b_fixed_frame_rate = 1;
170 sps->b_vui |= sps->vui.b_timing_info_present;
172 sps->vui.b_bitstream_restriction = param->i_bframe > 0;
173 if( sps->vui.b_bitstream_restriction )
175 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
176 sps->vui.i_max_bytes_per_pic_denom = 0;
177 sps->vui.i_max_bits_per_mb_denom = 0;
178 sps->vui.i_log2_max_mv_length_horizontal =
179 sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
181 sps->b_vui |= sps->vui.b_bitstream_restriction;
185 void x264_sps_write( bs_t *s, x264_sps_t *sps )
187 bs_write( s, 8, sps->i_profile_idc );
188 bs_write( s, 1, sps->b_constraint_set0 );
189 bs_write( s, 1, sps->b_constraint_set1 );
190 bs_write( s, 1, sps->b_constraint_set2 );
192 bs_write( s, 5, 0 ); /* reserved */
194 bs_write( s, 8, sps->i_level_idc );
196 bs_write_ue( s, sps->i_id );
198 if( sps->i_profile_idc >= PROFILE_HIGH )
200 bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
201 bs_write_ue( s, 0 ); // bit_depth_luma_minus8
202 bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
203 bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
204 bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
207 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
208 bs_write_ue( s, sps->i_poc_type );
209 if( sps->i_poc_type == 0 )
211 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
213 else if( sps->i_poc_type == 1 )
217 bs_write( s, 1, sps->b_delta_pic_order_always_zero );
218 bs_write_se( s, sps->i_offset_for_non_ref_pic );
219 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
220 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
222 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
224 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
227 bs_write_ue( s, sps->i_num_ref_frames );
228 bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
229 bs_write_ue( s, sps->i_mb_width - 1 );
230 bs_write_ue( s, sps->i_mb_height - 1);
231 bs_write( s, 1, sps->b_frame_mbs_only );
232 if( !sps->b_frame_mbs_only )
234 bs_write( s, 1, sps->b_mb_adaptive_frame_field );
236 bs_write( s, 1, sps->b_direct8x8_inference );
238 bs_write( s, 1, sps->b_crop );
241 bs_write_ue( s, sps->crop.i_left );
242 bs_write_ue( s, sps->crop.i_right );
243 bs_write_ue( s, sps->crop.i_top );
244 bs_write_ue( s, sps->crop.i_bottom );
247 bs_write( s, 1, sps->b_vui );
250 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
251 if( sps->vui.b_aspect_ratio_info_present )
254 static const struct { int w, h; int sar; } sar[] =
256 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
257 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
258 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
259 { 160,99, 13}, { 0, 0, -1 }
261 for( i = 0; sar[i].sar != -1; i++ )
263 if( sar[i].w == sps->vui.i_sar_width &&
264 sar[i].h == sps->vui.i_sar_height )
267 if( sar[i].sar != -1 )
269 bs_write( s, 8, sar[i].sar );
273 bs_write( s, 8, 255); /* aspect_ratio_idc (extented) */
274 bs_write( s, 16, sps->vui.i_sar_width );
275 bs_write( s, 16, sps->vui.i_sar_height );
279 bs_write1( s, 0 ); /* overscan_info_present_flag */
281 bs_write1( s, 0 ); /* video_signal_type_present_flag */
283 bs_write( s, 3, 5 ); /* unspecified video format */
284 bs_write1( s, 1 ); /* video full range flag */
285 bs_write1( s, 0 ); /* colour description present flag */
287 bs_write1( s, 0 ); /* chroma_loc_info_present_flag */
289 bs_write1( s, sps->vui.b_timing_info_present );
290 if( sps->vui.b_timing_info_present )
292 bs_write( s, 32, sps->vui.i_num_units_in_tick );
293 bs_write( s, 32, sps->vui.i_time_scale );
294 bs_write1( s, sps->vui.b_fixed_frame_rate );
297 bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
298 bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
299 bs_write1( s, 0 ); /* pic_struct_present_flag */
300 bs_write1( s, sps->vui.b_bitstream_restriction );
301 if( sps->vui.b_bitstream_restriction )
303 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
304 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
305 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
306 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
307 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
308 bs_write_ue( s, sps->vui.i_num_reorder_frames );
309 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
313 bs_rbsp_trailing( s );
316 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
321 pps->i_sps_id = sps->i_id;
322 pps->b_cabac = param->b_cabac;
324 pps->b_pic_order = 0;
325 pps->i_num_slice_groups = 1;
328 if( pps->i_num_slice_groups > 1 )
330 pps->i_slice_group_map_type = 0;
331 if( pps->i_slice_group_map_type == 0 )
333 for( i = 0; i < pps->i_num_slice_groups; i++ )
335 pps->i_run_length[i] = 1;
338 else if( pps->i_slice_group_map_type == 2 )
340 for( i = 0; i < pps->i_num_slice_groups; i++ )
342 pps->i_top_left[i] = 0;
343 pps->i_bottom_right[i] = 0;
346 else if( pps->i_slice_group_map_type >= 3 &&
347 pps->i_slice_group_map_type <= 5 )
349 pps->b_slice_group_change_direction = 0;
350 pps->i_slice_group_change_rate = 0;
352 else if( pps->i_slice_group_map_type == 6 )
354 pps->i_pic_size_in_map_units = 1;
355 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
357 pps->i_slice_group_id[i] = 0;
362 pps->i_num_ref_idx_l0_active = 1;
363 pps->i_num_ref_idx_l1_active = 1;
365 pps->b_weighted_pred = 0;
366 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
368 pps->i_pic_init_qp = 26;
369 pps->i_pic_init_qs = 26;
371 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
372 pps->b_deblocking_filter_control = 1;
373 pps->b_constrained_intra_pred = 0;
374 pps->b_redundant_pic_cnt = 0;
376 pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
378 pps->i_cqm_preset = param->i_cqm_preset;
379 switch( pps->i_cqm_preset )
382 for( i = 0; i < 6; i++ )
383 pps->scaling_list[i] = x264_cqm_flat16;
386 for( i = 0; i < 6; i++ )
387 pps->scaling_list[i] = x264_cqm_jvt[i];
389 case X264_CQM_CUSTOM:
390 pps->scaling_list[CQM_4IY] = param->cqm_4iy;
391 pps->scaling_list[CQM_4IC] = param->cqm_4ic;
392 pps->scaling_list[CQM_4PY] = param->cqm_4py;
393 pps->scaling_list[CQM_4PC] = param->cqm_4pc;
394 pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
395 pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
396 for( i = 0; i < 6; i++ )
397 for( j = 0; j < (i<4?16:64); j++ )
398 if( pps->scaling_list[i][j] == 0 )
399 pps->scaling_list[i] = x264_cqm_jvt[i];
404 void x264_pps_write( bs_t *s, x264_pps_t *pps )
406 bs_write_ue( s, pps->i_id );
407 bs_write_ue( s, pps->i_sps_id );
409 bs_write( s, 1, pps->b_cabac );
410 bs_write( s, 1, pps->b_pic_order );
411 bs_write_ue( s, pps->i_num_slice_groups - 1 );
414 if( pps->i_num_slice_groups > 1 )
418 bs_write_ue( s, pps->i_slice_group_map_type );
419 if( pps->i_slice_group_map_type == 0 )
421 for( i = 0; i < pps->i_num_slice_groups; i++ )
423 bs_write_ue( s, pps->i_run_length[i] - 1 );
426 else if( pps->i_slice_group_map_type == 2 )
428 for( i = 0; i < pps->i_num_slice_groups; i++ )
430 bs_write_ue( s, pps->i_top_left[i] );
431 bs_write_ue( s, pps->i_bottom_right[i] );
434 else if( pps->i_slice_group_map_type >= 3 &&
435 pps->i_slice_group_map_type <= 5 )
437 bs_write( s, 1, pps->b_slice_group_change_direction );
438 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
440 else if( pps->i_slice_group_map_type == 6 )
442 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
443 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
446 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
447 * pps->i_slice_group_id[i] );
454 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
455 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
456 bs_write( s, 1, pps->b_weighted_pred );
457 bs_write( s, 2, pps->b_weighted_bipred );
459 bs_write_se( s, pps->i_pic_init_qp - 26 );
460 bs_write_se( s, pps->i_pic_init_qs - 26 );
461 bs_write_se( s, pps->i_chroma_qp_index_offset );
463 bs_write( s, 1, pps->b_deblocking_filter_control );
464 bs_write( s, 1, pps->b_constrained_intra_pred );
465 bs_write( s, 1, pps->b_redundant_pic_cnt );
467 if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
469 bs_write( s, 1, pps->b_transform_8x8_mode );
470 bs_write( s, 1, (pps->i_cqm_preset != X264_CQM_FLAT) );
471 if( pps->i_cqm_preset != X264_CQM_FLAT )
473 scaling_list_write( s, pps, CQM_4IY );
474 scaling_list_write( s, pps, CQM_4IC );
475 bs_write( s, 1, 0 ); // Cr = Cb
476 scaling_list_write( s, pps, CQM_4PY );
477 scaling_list_write( s, pps, CQM_4PC );
478 bs_write( s, 1, 0 ); // Cr = Cb
479 if( pps->b_transform_8x8_mode )
481 scaling_list_write( s, pps, CQM_8IY+4 );
482 scaling_list_write( s, pps, CQM_8PY+4 );
485 bs_write_se( s, pps->i_chroma_qp_index_offset );
488 bs_rbsp_trailing( s );
491 void x264_sei_version_write( bs_t *s )
494 // random ID number generated according to ISO-11578
495 const uint8_t uuid[16] = {
496 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
497 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
501 sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
502 X264_BUILD, X264_VERSION );
503 length = strlen(version)+1+16;
505 bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
506 while( length > 255 )
507 bs_write( s, 8, 255 ), length -= 255;
508 bs_write( s, 8, length ); // payload_size
510 for( i = 0; i < 16; i++ )
511 bs_write( s, 8, uuid[i] );
512 for( i = 0; i < length-16; i++ )
513 bs_write( s, 8, version[i] );
515 bs_rbsp_trailing( s );