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/bs.h"
37 #include "common/set.h"
40 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
44 if( param->b_cabac || param->i_bframe > 0 )
45 sps->i_profile_idc = PROFILE_MAIN;
47 sps->i_profile_idc = PROFILE_BASELINE;
48 sps->i_level_idc = param->i_level_idc;
50 sps->b_constraint_set0 = 0;
51 sps->b_constraint_set1 = 0;
52 sps->b_constraint_set2 = 0;
54 sps->i_log2_max_frame_num = 4; /* at least 4 */
55 while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
57 sps->i_log2_max_frame_num++;
59 sps->i_log2_max_frame_num++; /* just in case */
62 if( sps->i_poc_type == 0 )
64 sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
66 else if( sps->i_poc_type == 1 )
71 sps->b_delta_pic_order_always_zero = 1;
72 sps->i_offset_for_non_ref_pic = 0;
73 sps->i_offset_for_top_to_bottom_field = 0;
74 sps->i_num_ref_frames_in_poc_cycle = 0;
76 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
78 sps->i_offset_for_ref_frame[i] = 0;
82 sps->i_num_ref_frames = param->i_frame_reference;
84 sps->i_num_ref_frames++; /* for backwards ref in B */
85 if( param->b_bframe_pyramid )
86 sps->i_num_ref_frames++; /* for 2nd backwards ref */
87 sps->b_gaps_in_frame_num_value_allowed = 0;
88 sps->i_mb_width = ( param->i_width + 15 ) / 16;
89 sps->i_mb_height= ( param->i_height + 15 )/ 16;
90 sps->b_frame_mbs_only = 1;
91 sps->b_mb_adaptive_frame_field = 0;
92 sps->b_direct8x8_inference = 0;
93 if( sps->b_frame_mbs_only == 0 ||
94 !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
96 sps->b_direct8x8_inference = 1;
99 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
102 sps->crop.i_left = 0;
103 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
105 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
110 sps->crop.i_left = 0;
111 sps->crop.i_right = 0;
113 sps->crop.i_bottom = 0;
117 sps->vui.b_aspect_ratio_info_present = 0;
119 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
121 sps->vui.b_aspect_ratio_info_present = 1;
122 sps->vui.i_sar_width = param->vui.i_sar_width;
123 sps->vui.i_sar_height= param->vui.i_sar_height;
125 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
127 if( param->i_fps_num > 0 && param->i_fps_den > 0)
129 sps->vui.b_timing_info_present = 1;
130 /* The standard is confusing me, but this seems to work best
131 with other encoders */
132 sps->vui.i_num_units_in_tick = param->i_fps_den;
133 sps->vui.i_time_scale = param->i_fps_num;
134 sps->vui.b_fixed_frame_rate = 1;
136 sps->b_vui |= sps->vui.b_timing_info_present;
138 sps->vui.b_bitstream_restriction = param->i_bframe > 0;
139 if( sps->vui.b_bitstream_restriction )
141 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
142 sps->vui.i_max_bytes_per_pic_denom = 0;
143 sps->vui.i_max_bits_per_mb_denom = 0;
144 sps->vui.i_log2_max_mv_length_horizontal =
145 sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
146 sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
147 sps->vui.i_max_dec_frame_buffering = param->i_frame_reference + sps->vui.i_num_reorder_frames + 1;
149 sps->b_vui |= sps->vui.b_bitstream_restriction;
153 void x264_sps_write( bs_t *s, x264_sps_t *sps )
155 bs_write( s, 8, sps->i_profile_idc );
156 bs_write( s, 1, sps->b_constraint_set0 );
157 bs_write( s, 1, sps->b_constraint_set1 );
158 bs_write( s, 1, sps->b_constraint_set2 );
160 bs_write( s, 5, 0 ); /* reserved */
162 bs_write( s, 8, sps->i_level_idc );
164 bs_write_ue( s, sps->i_id );
165 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
166 bs_write_ue( s, sps->i_poc_type );
167 if( sps->i_poc_type == 0 )
169 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
171 else if( sps->i_poc_type == 1 )
175 bs_write( s, 1, sps->b_delta_pic_order_always_zero );
176 bs_write_se( s, sps->i_offset_for_non_ref_pic );
177 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
178 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
180 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
182 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
185 bs_write_ue( s, sps->i_num_ref_frames );
186 bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
187 bs_write_ue( s, sps->i_mb_width - 1 );
188 bs_write_ue( s, sps->i_mb_height - 1);
189 bs_write( s, 1, sps->b_frame_mbs_only );
190 if( !sps->b_frame_mbs_only )
192 bs_write( s, 1, sps->b_mb_adaptive_frame_field );
194 bs_write( s, 1, sps->b_direct8x8_inference );
196 bs_write( s, 1, sps->b_crop );
199 bs_write_ue( s, sps->crop.i_left );
200 bs_write_ue( s, sps->crop.i_right );
201 bs_write_ue( s, sps->crop.i_top );
202 bs_write_ue( s, sps->crop.i_bottom );
205 bs_write( s, 1, sps->b_vui );
208 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
209 if( sps->vui.b_aspect_ratio_info_present )
212 static const struct { int w, h; int sar; } sar[] =
214 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
215 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
216 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
217 { 160,99, 13}, { 0, 0, -1 }
219 for( i = 0; sar[i].sar != -1; i++ )
221 if( sar[i].w == sps->vui.i_sar_width &&
222 sar[i].h == sps->vui.i_sar_height )
225 if( sar[i].sar != -1 )
227 bs_write( s, 8, sar[i].sar );
231 bs_write( s, 8, 255); /* aspect_ratio_idc (extented) */
232 bs_write( s, 16, sps->vui.i_sar_width );
233 bs_write( s, 16, sps->vui.i_sar_height );
237 bs_write1( s, 0 ); /* overscan_info_present_flag */
239 bs_write1( s, 0 ); /* video_signal_type_present_flag */
241 bs_write( s, 3, 5 ); /* unspecified video format */
242 bs_write1( s, 1 ); /* video full range flag */
243 bs_write1( s, 0 ); /* colour description present flag */
245 bs_write1( s, 0 ); /* chroma_loc_info_present_flag */
247 bs_write1( s, sps->vui.b_timing_info_present );
248 if( sps->vui.b_timing_info_present )
250 bs_write( s, 32, sps->vui.i_num_units_in_tick );
251 bs_write( s, 32, sps->vui.i_time_scale );
252 bs_write1( s, sps->vui.b_fixed_frame_rate );
255 bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
256 bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
257 bs_write1( s, 0 ); /* pic_struct_present_flag */
258 bs_write1( s, sps->vui.b_bitstream_restriction );
259 if( sps->vui.b_bitstream_restriction )
261 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
262 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
263 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
264 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
265 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
266 bs_write_ue( s, sps->vui.i_num_reorder_frames );
267 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
271 bs_rbsp_trailing( s );
274 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
277 pps->i_sps_id = sps->i_id;
278 pps->b_cabac = param->b_cabac;
280 pps->b_pic_order = 0;
281 pps->i_num_slice_groups = 1;
283 if( pps->i_num_slice_groups > 1 )
287 pps->i_slice_group_map_type = 0;
288 if( pps->i_slice_group_map_type == 0 )
290 for( i = 0; i < pps->i_num_slice_groups; i++ )
292 pps->i_run_length[i] = 1;
295 else if( pps->i_slice_group_map_type == 2 )
297 for( i = 0; i < pps->i_num_slice_groups; i++ )
299 pps->i_top_left[i] = 0;
300 pps->i_bottom_right[i] = 0;
303 else if( pps->i_slice_group_map_type >= 3 &&
304 pps->i_slice_group_map_type <= 5 )
306 pps->b_slice_group_change_direction = 0;
307 pps->i_slice_group_change_rate = 0;
309 else if( pps->i_slice_group_map_type == 6 )
311 pps->i_pic_size_in_map_units = 1;
312 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
314 pps->i_slice_group_id[i] = 0;
318 pps->i_num_ref_idx_l0_active = 1;
319 pps->i_num_ref_idx_l1_active = 1;
321 pps->b_weighted_pred = 0;
322 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
324 pps->i_pic_init_qp = 26;
325 pps->i_pic_init_qs = 26;
327 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
328 pps->b_deblocking_filter_control = 1;
329 pps->b_constrained_intra_pred = 0;
330 pps->b_redundant_pic_cnt = 0;
333 void x264_pps_write( bs_t *s, x264_pps_t *pps )
335 bs_write_ue( s, pps->i_id );
336 bs_write_ue( s, pps->i_sps_id );
338 bs_write( s, 1, pps->b_cabac );
339 bs_write( s, 1, pps->b_pic_order );
340 bs_write_ue( s, pps->i_num_slice_groups - 1 );
342 if( pps->i_num_slice_groups > 1 )
346 bs_write_ue( s, pps->i_slice_group_map_type );
347 if( pps->i_slice_group_map_type == 0 )
349 for( i = 0; i < pps->i_num_slice_groups; i++ )
351 bs_write_ue( s, pps->i_run_length[i] - 1 );
354 else if( pps->i_slice_group_map_type == 2 )
356 for( i = 0; i < pps->i_num_slice_groups; i++ )
358 bs_write_ue( s, pps->i_top_left[i] );
359 bs_write_ue( s, pps->i_bottom_right[i] );
362 else if( pps->i_slice_group_map_type >= 3 &&
363 pps->i_slice_group_map_type <= 5 )
365 bs_write( s, 1, pps->b_slice_group_change_direction );
366 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
368 else if( pps->i_slice_group_map_type == 6 )
370 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
371 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
374 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
375 * pps->i_slice_group_id[i] );
381 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
382 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
383 bs_write( s, 1, pps->b_weighted_pred );
384 bs_write( s, 2, pps->b_weighted_bipred );
386 bs_write_se( s, pps->i_pic_init_qp - 26 );
387 bs_write_se( s, pps->i_pic_init_qs - 26 );
388 bs_write_se( s, pps->i_chroma_qp_index_offset );
390 bs_write( s, 1, pps->b_deblocking_filter_control );
391 bs_write( s, 1, pps->b_constrained_intra_pred );
392 bs_write( s, 1, pps->b_redundant_pic_cnt );
394 bs_rbsp_trailing( s );
397 void x264_sei_version_write( bs_t *s )
400 // random ID number generated according to ISO-11578
401 const uint8_t uuid[16] = {
402 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
403 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
407 sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
408 X264_BUILD, X264_VERSION );
409 length = strlen(version)+1+16;
411 bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
412 while( length > 255 )
413 bs_write( s, 8, 255 ), length -= 255;
414 bs_write( s, 8, length ); // payload_size
416 for( i = 0; i < 16; i++ )
417 bs_write( s, 8, uuid[i] );
418 for( i = 0; i < length-16; i++ )
419 bs_write( s, 8, version[i] );
421 bs_rbsp_trailing( s );