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 *****************************************************************************/
35 #include "../core/bs.h"
36 #include "../core/set.h"
38 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
42 if( param->b_cabac || param->i_bframe > 0 )
43 sps->i_profile_idc = PROFILE_MAIN;
45 sps->i_profile_idc = PROFILE_BASELINE;
47 sps->i_level_idc = 21; /* FIXME ? */
48 sps->b_constraint_set0 = 0;
49 sps->b_constraint_set1 = 0;
50 sps->b_constraint_set2 = 0;
52 sps->i_log2_max_frame_num = 4; /* at least 4 */
53 while( (1 << sps->i_log2_max_frame_num) <= param->i_idrframe * param->i_iframe )
55 sps->i_log2_max_frame_num++;
57 sps->i_log2_max_frame_num++; /* just in case */
60 if( sps->i_poc_type == 0 )
62 sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
64 else if( sps->i_poc_type == 1 )
69 sps->b_delta_pic_order_always_zero = 1;
70 sps->i_offset_for_non_ref_pic = 0;
71 sps->i_offset_for_top_to_bottom_field = 0;
72 sps->i_num_ref_frames_in_poc_cycle = 0;
74 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
76 sps->i_offset_for_ref_frame[i] = 0;
80 sps->i_num_ref_frames = param->i_frame_reference + 1; /* +1 for 2 ref in B */
81 sps->b_gaps_in_frame_num_value_allowed = 0;
82 sps->i_mb_width = ( param->i_width + 15 ) / 16;
83 sps->i_mb_height= ( param->i_height + 15 )/ 16;
84 sps->b_frame_mbs_only = 1;
85 sps->b_mb_adaptive_frame_field = 0;
86 sps->b_direct8x8_inference = 0;
87 if( sps->b_frame_mbs_only == 0 )
89 sps->b_direct8x8_inference = 1;
92 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
96 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
98 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
103 sps->crop.i_left = 0;
104 sps->crop.i_right = 0;
106 sps->crop.i_bottom = 0;
111 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
113 int w = param->vui.i_sar_width;
114 int h = param->vui.i_sar_height;
127 while( w > 65535 || h > 65535 )
133 if( w == 0 || h == 0 )
135 fprintf( stderr, "x264: cannot create valid sample aspect ratio\n" );
136 sps->vui.b_aspect_ratio_info_present = 0;
140 fprintf( stderr, "x264: no need for a SAR\n" );
141 sps->vui.b_aspect_ratio_info_present = 0;
145 fprintf( stderr, "x264: using SAR=%d/%d\n", w, h );
146 sps->vui.b_aspect_ratio_info_present = 1;
147 sps->vui.i_sar_width = w;
148 sps->vui.i_sar_height= h;
151 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
153 if( param->i_fps_num > 0 && param->i_fps_den > 0)
155 sps->vui.b_timing_info_present = 1;
156 /* The standard is confusing me, but this seems to work best
157 with other encoders */
158 sps->vui.i_num_units_in_tick = param->i_fps_den;
159 sps->vui.i_time_scale = param->i_fps_num;
160 sps->vui.b_fixed_frame_rate = 1;
162 sps->b_vui |= sps->vui.b_timing_info_present;
166 void x264_sps_write( bs_t *s, x264_sps_t *sps )
168 bs_write( s, 8, sps->i_profile_idc );
169 bs_write( s, 1, sps->b_constraint_set0 );
170 bs_write( s, 1, sps->b_constraint_set1 );
171 bs_write( s, 1, sps->b_constraint_set2 );
173 bs_write( s, 5, 0 ); /* reserved */
175 bs_write( s, 8, sps->i_level_idc );
177 bs_write_ue( s, sps->i_id );
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_ration_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, 0 ); /* bitstream_restriction_flag */
274 bs_rbsp_trailing( s );
277 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
280 pps->i_sps_id = sps->i_id;
281 pps->b_cabac = param->b_cabac;
283 pps->b_pic_order = 0;
284 pps->i_num_slice_groups = 1;
286 if( pps->i_num_slice_groups > 1 )
290 pps->i_slice_group_map_type = 0;
291 if( pps->i_slice_group_map_type == 0 )
293 for( i = 0; i < pps->i_num_slice_groups; i++ )
295 pps->i_run_length[i] = 1;
298 else if( pps->i_slice_group_map_type == 2 )
300 for( i = 0; i < pps->i_num_slice_groups; i++ )
302 pps->i_top_left[i] = 0;
303 pps->i_bottom_right[i] = 0;
306 else if( pps->i_slice_group_map_type >= 3 &&
307 pps->i_slice_group_map_type <= 5 )
309 pps->b_slice_group_change_direction = 0;
310 pps->i_slice_group_change_rate = 0;
312 else if( pps->i_slice_group_map_type == 6 )
314 pps->i_pic_size_in_map_units = 1;
315 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
317 pps->i_slice_group_id[i] = 0;
321 pps->i_num_ref_idx_l0_active = 1;
322 pps->i_num_ref_idx_l1_active = 1;
324 pps->b_weighted_pred = 0;
325 pps->b_weighted_bipred = 0;
327 pps->i_pic_init_qp = 26;
328 pps->i_pic_init_qs = 26;
330 pps->i_chroma_qp_index_offset = 0;
332 if( !param->b_deblocking_filter )
334 pps->b_deblocking_filter_control = 1;
338 pps->b_deblocking_filter_control = 1;
341 pps->b_deblocking_filter_control = 1;
342 pps->b_constrained_intra_pred = 0;
343 pps->b_redundant_pic_cnt = 0;
346 void x264_pps_write( bs_t *s, x264_pps_t *pps )
348 bs_write_ue( s, pps->i_id );
349 bs_write_ue( s, pps->i_sps_id );
351 bs_write( s, 1, pps->b_cabac );
352 bs_write( s, 1, pps->b_pic_order );
353 bs_write_ue( s, pps->i_num_slice_groups - 1 );
355 if( pps->i_num_slice_groups > 1 )
359 bs_write_ue( s, pps->i_slice_group_map_type );
360 if( pps->i_slice_group_map_type == 0 )
362 for( i = 0; i < pps->i_num_slice_groups; i++ )
364 bs_write_ue( s, pps->i_run_length[i] - 1 );
367 else if( pps->i_slice_group_map_type == 2 )
369 for( i = 0; i < pps->i_num_slice_groups; i++ )
371 bs_write_ue( s, pps->i_top_left[i] );
372 bs_write_ue( s, pps->i_bottom_right[i] );
375 else if( pps->i_slice_group_map_type >= 3 &&
376 pps->i_slice_group_map_type <= 5 )
378 bs_write( s, 1, pps->b_slice_group_change_direction );
379 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
381 else if( pps->i_slice_group_map_type == 6 )
383 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
384 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
387 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
388 * pps->i_slice_group_id[i] );
394 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
395 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
396 bs_write( s, 1, pps->b_weighted_pred );
397 bs_write( s, 2, pps->b_weighted_bipred );
399 bs_write_se( s, pps->i_pic_init_qp - 26 );
400 bs_write_se( s, pps->i_pic_init_qs - 26 );
401 bs_write_se( s, pps->i_chroma_qp_index_offset );
403 bs_write( s, 1, pps->b_deblocking_filter_control );
404 bs_write( s, 1, pps->b_constrained_intra_pred );
405 bs_write( s, 1, pps->b_redundant_pic_cnt );
407 bs_rbsp_trailing( s );