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"
41 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
45 sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
46 if( sps->b_qpprime_y_zero_transform_bypass )
47 sps->i_profile_idc = PROFILE_HIGH444;
48 else if( param->analyse.b_transform_8x8 )
49 sps->i_profile_idc = PROFILE_HIGH;
50 else if( param->b_cabac || param->i_bframe > 0 )
51 sps->i_profile_idc = PROFILE_MAIN;
53 sps->i_profile_idc = PROFILE_BASELINE;
54 sps->i_level_idc = param->i_level_idc;
56 sps->b_constraint_set0 = 0;
57 sps->b_constraint_set1 = 0;
58 sps->b_constraint_set2 = 0;
60 sps->i_log2_max_frame_num = 4; /* at least 4 */
61 while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
63 sps->i_log2_max_frame_num++;
65 sps->i_log2_max_frame_num++; /* just in case */
68 if( sps->i_poc_type == 0 )
70 sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
72 else if( sps->i_poc_type == 1 )
77 sps->b_delta_pic_order_always_zero = 1;
78 sps->i_offset_for_non_ref_pic = 0;
79 sps->i_offset_for_top_to_bottom_field = 0;
80 sps->i_num_ref_frames_in_poc_cycle = 0;
82 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
84 sps->i_offset_for_ref_frame[i] = 0;
88 sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
89 sps->vui.i_max_dec_frame_buffering =
90 sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames);
92 sps->b_gaps_in_frame_num_value_allowed = 0;
93 sps->i_mb_width = ( param->i_width + 15 ) / 16;
94 sps->i_mb_height= ( param->i_height + 15 )/ 16;
95 sps->b_frame_mbs_only = 1;
96 sps->b_mb_adaptive_frame_field = 0;
97 sps->b_direct8x8_inference = 0;
98 if( sps->b_frame_mbs_only == 0 ||
99 !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
101 sps->b_direct8x8_inference = 1;
104 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
107 sps->crop.i_left = 0;
108 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
110 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
115 sps->crop.i_left = 0;
116 sps->crop.i_right = 0;
118 sps->crop.i_bottom = 0;
122 sps->vui.b_aspect_ratio_info_present = 0;
124 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
126 sps->vui.b_aspect_ratio_info_present = 1;
127 sps->vui.i_sar_width = param->vui.i_sar_width;
128 sps->vui.i_sar_height= param->vui.i_sar_height;
130 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
132 if( param->i_fps_num > 0 && param->i_fps_den > 0)
134 sps->vui.b_timing_info_present = 1;
135 /* The standard is confusing me, but this seems to work best
136 with other encoders */
137 sps->vui.i_num_units_in_tick = param->i_fps_den;
138 sps->vui.i_time_scale = param->i_fps_num;
139 sps->vui.b_fixed_frame_rate = 1;
141 sps->b_vui |= sps->vui.b_timing_info_present;
143 sps->vui.b_bitstream_restriction = param->i_bframe > 0;
144 if( sps->vui.b_bitstream_restriction )
146 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
147 sps->vui.i_max_bytes_per_pic_denom = 0;
148 sps->vui.i_max_bits_per_mb_denom = 0;
149 sps->vui.i_log2_max_mv_length_horizontal =
150 sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
152 sps->b_vui |= sps->vui.b_bitstream_restriction;
156 void x264_sps_write( bs_t *s, x264_sps_t *sps )
158 bs_write( s, 8, sps->i_profile_idc );
159 bs_write( s, 1, sps->b_constraint_set0 );
160 bs_write( s, 1, sps->b_constraint_set1 );
161 bs_write( s, 1, sps->b_constraint_set2 );
163 bs_write( s, 5, 0 ); /* reserved */
165 bs_write( s, 8, sps->i_level_idc );
167 bs_write_ue( s, sps->i_id );
169 if( sps->i_profile_idc >= PROFILE_HIGH )
171 bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
172 bs_write_ue( s, 0 ); // bit_depth_luma_minus8
173 bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
174 bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
175 bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
178 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
179 bs_write_ue( s, sps->i_poc_type );
180 if( sps->i_poc_type == 0 )
182 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
184 else if( sps->i_poc_type == 1 )
188 bs_write( s, 1, sps->b_delta_pic_order_always_zero );
189 bs_write_se( s, sps->i_offset_for_non_ref_pic );
190 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
191 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
193 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
195 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
198 bs_write_ue( s, sps->i_num_ref_frames );
199 bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
200 bs_write_ue( s, sps->i_mb_width - 1 );
201 bs_write_ue( s, sps->i_mb_height - 1);
202 bs_write( s, 1, sps->b_frame_mbs_only );
203 if( !sps->b_frame_mbs_only )
205 bs_write( s, 1, sps->b_mb_adaptive_frame_field );
207 bs_write( s, 1, sps->b_direct8x8_inference );
209 bs_write( s, 1, sps->b_crop );
212 bs_write_ue( s, sps->crop.i_left );
213 bs_write_ue( s, sps->crop.i_right );
214 bs_write_ue( s, sps->crop.i_top );
215 bs_write_ue( s, sps->crop.i_bottom );
218 bs_write( s, 1, sps->b_vui );
221 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
222 if( sps->vui.b_aspect_ratio_info_present )
225 static const struct { int w, h; int sar; } sar[] =
227 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
228 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
229 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
230 { 160,99, 13}, { 0, 0, -1 }
232 for( i = 0; sar[i].sar != -1; i++ )
234 if( sar[i].w == sps->vui.i_sar_width &&
235 sar[i].h == sps->vui.i_sar_height )
238 if( sar[i].sar != -1 )
240 bs_write( s, 8, sar[i].sar );
244 bs_write( s, 8, 255); /* aspect_ratio_idc (extented) */
245 bs_write( s, 16, sps->vui.i_sar_width );
246 bs_write( s, 16, sps->vui.i_sar_height );
250 bs_write1( s, 0 ); /* overscan_info_present_flag */
252 bs_write1( s, 0 ); /* video_signal_type_present_flag */
254 bs_write( s, 3, 5 ); /* unspecified video format */
255 bs_write1( s, 1 ); /* video full range flag */
256 bs_write1( s, 0 ); /* colour description present flag */
258 bs_write1( s, 0 ); /* chroma_loc_info_present_flag */
260 bs_write1( s, sps->vui.b_timing_info_present );
261 if( sps->vui.b_timing_info_present )
263 bs_write( s, 32, sps->vui.i_num_units_in_tick );
264 bs_write( s, 32, sps->vui.i_time_scale );
265 bs_write1( s, sps->vui.b_fixed_frame_rate );
268 bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
269 bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
270 bs_write1( s, 0 ); /* pic_struct_present_flag */
271 bs_write1( s, sps->vui.b_bitstream_restriction );
272 if( sps->vui.b_bitstream_restriction )
274 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
275 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
276 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
277 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
278 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
279 bs_write_ue( s, sps->vui.i_num_reorder_frames );
280 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
284 bs_rbsp_trailing( s );
287 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
290 pps->i_sps_id = sps->i_id;
291 pps->b_cabac = param->b_cabac;
293 pps->b_pic_order = 0;
294 pps->i_num_slice_groups = 1;
297 if( pps->i_num_slice_groups > 1 )
301 pps->i_slice_group_map_type = 0;
302 if( pps->i_slice_group_map_type == 0 )
304 for( i = 0; i < pps->i_num_slice_groups; i++ )
306 pps->i_run_length[i] = 1;
309 else if( pps->i_slice_group_map_type == 2 )
311 for( i = 0; i < pps->i_num_slice_groups; i++ )
313 pps->i_top_left[i] = 0;
314 pps->i_bottom_right[i] = 0;
317 else if( pps->i_slice_group_map_type >= 3 &&
318 pps->i_slice_group_map_type <= 5 )
320 pps->b_slice_group_change_direction = 0;
321 pps->i_slice_group_change_rate = 0;
323 else if( pps->i_slice_group_map_type == 6 )
325 pps->i_pic_size_in_map_units = 1;
326 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
328 pps->i_slice_group_id[i] = 0;
333 pps->i_num_ref_idx_l0_active = 1;
334 pps->i_num_ref_idx_l1_active = 1;
336 pps->b_weighted_pred = 0;
337 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
339 pps->i_pic_init_qp = 26;
340 pps->i_pic_init_qs = 26;
342 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
343 pps->b_deblocking_filter_control = 1;
344 pps->b_constrained_intra_pred = 0;
345 pps->b_redundant_pic_cnt = 0;
347 pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
350 void x264_pps_write( bs_t *s, x264_pps_t *pps )
352 bs_write_ue( s, pps->i_id );
353 bs_write_ue( s, pps->i_sps_id );
355 bs_write( s, 1, pps->b_cabac );
356 bs_write( s, 1, pps->b_pic_order );
357 bs_write_ue( s, pps->i_num_slice_groups - 1 );
360 if( pps->i_num_slice_groups > 1 )
364 bs_write_ue( s, pps->i_slice_group_map_type );
365 if( pps->i_slice_group_map_type == 0 )
367 for( i = 0; i < pps->i_num_slice_groups; i++ )
369 bs_write_ue( s, pps->i_run_length[i] - 1 );
372 else if( pps->i_slice_group_map_type == 2 )
374 for( i = 0; i < pps->i_num_slice_groups; i++ )
376 bs_write_ue( s, pps->i_top_left[i] );
377 bs_write_ue( s, pps->i_bottom_right[i] );
380 else if( pps->i_slice_group_map_type >= 3 &&
381 pps->i_slice_group_map_type <= 5 )
383 bs_write( s, 1, pps->b_slice_group_change_direction );
384 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
386 else if( pps->i_slice_group_map_type == 6 )
388 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
389 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
392 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
393 * pps->i_slice_group_id[i] );
400 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
401 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
402 bs_write( s, 1, pps->b_weighted_pred );
403 bs_write( s, 2, pps->b_weighted_bipred );
405 bs_write_se( s, pps->i_pic_init_qp - 26 );
406 bs_write_se( s, pps->i_pic_init_qs - 26 );
407 bs_write_se( s, pps->i_chroma_qp_index_offset );
409 bs_write( s, 1, pps->b_deblocking_filter_control );
410 bs_write( s, 1, pps->b_constrained_intra_pred );
411 bs_write( s, 1, pps->b_redundant_pic_cnt );
413 if( pps->b_transform_8x8_mode )
415 bs_write( s, 1, pps->b_transform_8x8_mode );
416 bs_write( s, 1, 0 ); // pic_scaling_matrix_present_flag
417 bs_write_se( s, 0 ); // second_chroma_qp_index_offset
420 bs_rbsp_trailing( s );
423 void x264_sei_version_write( bs_t *s )
426 // random ID number generated according to ISO-11578
427 const uint8_t uuid[16] = {
428 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
429 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
433 sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
434 X264_BUILD, X264_VERSION );
435 length = strlen(version)+1+16;
437 bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
438 while( length > 255 )
439 bs_write( s, 8, 255 ), length -= 255;
440 bs_write( s, 8, length ); // payload_size
442 for( i = 0; i < 16; i++ )
443 bs_write( s, 8, uuid[i] );
444 for( i = 0; i < length-16; i++ )
445 bs_write( s, 8, version[i] );
447 bs_rbsp_trailing( s );