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 "../common/bs.h"
36 #include "../common/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 ||
88 !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
90 sps->b_direct8x8_inference = 1;
93 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
97 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
99 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
104 sps->crop.i_left = 0;
105 sps->crop.i_right = 0;
107 sps->crop.i_bottom = 0;
111 sps->vui.b_aspect_ratio_info_present = 0;
113 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
115 sps->vui.b_aspect_ratio_info_present = 1;
116 sps->vui.i_sar_width = param->vui.i_sar_width;
117 sps->vui.i_sar_height= param->vui.i_sar_height;
119 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
121 if( param->i_fps_num > 0 && param->i_fps_den > 0)
123 sps->vui.b_timing_info_present = 1;
124 /* The standard is confusing me, but this seems to work best
125 with other encoders */
126 sps->vui.i_num_units_in_tick = param->i_fps_den;
127 sps->vui.i_time_scale = param->i_fps_num;
128 sps->vui.b_fixed_frame_rate = 1;
130 sps->b_vui |= sps->vui.b_timing_info_present;
134 void x264_sps_write( bs_t *s, x264_sps_t *sps )
136 bs_write( s, 8, sps->i_profile_idc );
137 bs_write( s, 1, sps->b_constraint_set0 );
138 bs_write( s, 1, sps->b_constraint_set1 );
139 bs_write( s, 1, sps->b_constraint_set2 );
141 bs_write( s, 5, 0 ); /* reserved */
143 bs_write( s, 8, sps->i_level_idc );
145 bs_write_ue( s, sps->i_id );
146 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
147 bs_write_ue( s, sps->i_poc_type );
148 if( sps->i_poc_type == 0 )
150 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
152 else if( sps->i_poc_type == 1 )
156 bs_write( s, 1, sps->b_delta_pic_order_always_zero );
157 bs_write_se( s, sps->i_offset_for_non_ref_pic );
158 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
159 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
161 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
163 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
166 bs_write_ue( s, sps->i_num_ref_frames );
167 bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
168 bs_write_ue( s, sps->i_mb_width - 1 );
169 bs_write_ue( s, sps->i_mb_height - 1);
170 bs_write( s, 1, sps->b_frame_mbs_only );
171 if( !sps->b_frame_mbs_only )
173 bs_write( s, 1, sps->b_mb_adaptive_frame_field );
175 bs_write( s, 1, sps->b_direct8x8_inference );
177 bs_write( s, 1, sps->b_crop );
180 bs_write_ue( s, sps->crop.i_left );
181 bs_write_ue( s, sps->crop.i_right );
182 bs_write_ue( s, sps->crop.i_top );
183 bs_write_ue( s, sps->crop.i_bottom );
186 bs_write( s, 1, sps->b_vui );
189 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
190 if( sps->vui.b_aspect_ratio_info_present )
193 static const struct { int w, h; int sar; } sar[] =
195 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
196 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
197 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
198 { 160,99, 13}, { 0, 0, -1 }
200 for( i = 0; sar[i].sar != -1; i++ )
202 if( sar[i].w == sps->vui.i_sar_width &&
203 sar[i].h == sps->vui.i_sar_height )
206 if( sar[i].sar != -1 )
208 bs_write( s, 8, sar[i].sar );
212 bs_write( s, 8, 255); /* aspect_ration_idc (extented) */
213 bs_write( s, 16, sps->vui.i_sar_width );
214 bs_write( s, 16, sps->vui.i_sar_height );
218 bs_write1( s, 0 ); /* overscan_info_present_flag */
220 bs_write1( s, 0 ); /* video_signal_type_present_flag */
222 bs_write( s, 3, 5 ); /* unspecified video format */
223 bs_write1( s, 1 ); /* video full range flag */
224 bs_write1( s, 0 ); /* colour description present flag */
226 bs_write1( s, 0 ); /* chroma_loc_info_present_flag */
228 bs_write1( s, sps->vui.b_timing_info_present );
229 if( sps->vui.b_timing_info_present )
231 bs_write( s, 32, sps->vui.i_num_units_in_tick );
232 bs_write( s, 32, sps->vui.i_time_scale );
233 bs_write1( s, sps->vui.b_fixed_frame_rate );
236 bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
237 bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
238 bs_write1( s, 0 ); /* pic_struct_present_flag */
239 bs_write1( s, 0 ); /* bitstream_restriction_flag */
242 bs_rbsp_trailing( s );
245 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
248 pps->i_sps_id = sps->i_id;
249 pps->b_cabac = param->b_cabac;
251 pps->b_pic_order = 0;
252 pps->i_num_slice_groups = 1;
254 if( pps->i_num_slice_groups > 1 )
258 pps->i_slice_group_map_type = 0;
259 if( pps->i_slice_group_map_type == 0 )
261 for( i = 0; i < pps->i_num_slice_groups; i++ )
263 pps->i_run_length[i] = 1;
266 else if( pps->i_slice_group_map_type == 2 )
268 for( i = 0; i < pps->i_num_slice_groups; i++ )
270 pps->i_top_left[i] = 0;
271 pps->i_bottom_right[i] = 0;
274 else if( pps->i_slice_group_map_type >= 3 &&
275 pps->i_slice_group_map_type <= 5 )
277 pps->b_slice_group_change_direction = 0;
278 pps->i_slice_group_change_rate = 0;
280 else if( pps->i_slice_group_map_type == 6 )
282 pps->i_pic_size_in_map_units = 1;
283 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
285 pps->i_slice_group_id[i] = 0;
289 pps->i_num_ref_idx_l0_active = 1;
290 pps->i_num_ref_idx_l1_active = 1;
292 pps->b_weighted_pred = 0;
293 pps->b_weighted_bipred = 0;
295 pps->i_pic_init_qp = 26;
296 pps->i_pic_init_qs = 26;
298 pps->i_chroma_qp_index_offset = 0;
300 if( !param->b_deblocking_filter )
302 pps->b_deblocking_filter_control = 1;
306 pps->b_deblocking_filter_control = 1;
309 pps->b_deblocking_filter_control = 1;
310 pps->b_constrained_intra_pred = 0;
311 pps->b_redundant_pic_cnt = 0;
314 void x264_pps_write( bs_t *s, x264_pps_t *pps )
316 bs_write_ue( s, pps->i_id );
317 bs_write_ue( s, pps->i_sps_id );
319 bs_write( s, 1, pps->b_cabac );
320 bs_write( s, 1, pps->b_pic_order );
321 bs_write_ue( s, pps->i_num_slice_groups - 1 );
323 if( pps->i_num_slice_groups > 1 )
327 bs_write_ue( s, pps->i_slice_group_map_type );
328 if( pps->i_slice_group_map_type == 0 )
330 for( i = 0; i < pps->i_num_slice_groups; i++ )
332 bs_write_ue( s, pps->i_run_length[i] - 1 );
335 else if( pps->i_slice_group_map_type == 2 )
337 for( i = 0; i < pps->i_num_slice_groups; i++ )
339 bs_write_ue( s, pps->i_top_left[i] );
340 bs_write_ue( s, pps->i_bottom_right[i] );
343 else if( pps->i_slice_group_map_type >= 3 &&
344 pps->i_slice_group_map_type <= 5 )
346 bs_write( s, 1, pps->b_slice_group_change_direction );
347 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
349 else if( pps->i_slice_group_map_type == 6 )
351 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
352 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
355 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
356 * pps->i_slice_group_id[i] );
362 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
363 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
364 bs_write( s, 1, pps->b_weighted_pred );
365 bs_write( s, 2, pps->b_weighted_bipred );
367 bs_write_se( s, pps->i_pic_init_qp - 26 );
368 bs_write_se( s, pps->i_pic_init_qs - 26 );
369 bs_write_se( s, pps->i_chroma_qp_index_offset );
371 bs_write( s, 1, pps->b_deblocking_filter_control );
372 bs_write( s, 1, pps->b_constrained_intra_pred );
373 bs_write( s, 1, pps->b_redundant_pic_cnt );
375 bs_rbsp_trailing( s );