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 if( param->analyse.b_transform_8x8 )
46 sps->i_profile_idc = PROFILE_HIGH;
47 else if( param->b_cabac || param->i_bframe > 0 )
48 sps->i_profile_idc = PROFILE_MAIN;
50 sps->i_profile_idc = PROFILE_BASELINE;
51 sps->i_level_idc = param->i_level_idc;
53 sps->b_constraint_set0 = 0;
54 sps->b_constraint_set1 = 0;
55 sps->b_constraint_set2 = 0;
57 sps->i_log2_max_frame_num = 4; /* at least 4 */
58 while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
60 sps->i_log2_max_frame_num++;
62 sps->i_log2_max_frame_num++; /* just in case */
65 if( sps->i_poc_type == 0 )
67 sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1; /* max poc = 2*frame_num */
69 else if( sps->i_poc_type == 1 )
74 sps->b_delta_pic_order_always_zero = 1;
75 sps->i_offset_for_non_ref_pic = 0;
76 sps->i_offset_for_top_to_bottom_field = 0;
77 sps->i_num_ref_frames_in_poc_cycle = 0;
79 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
81 sps->i_offset_for_ref_frame[i] = 0;
85 sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
86 sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames);
87 sps->vui.i_max_dec_frame_buffering = sps->i_num_ref_frames + 1;
89 sps->b_gaps_in_frame_num_value_allowed = 0;
90 sps->i_mb_width = ( param->i_width + 15 ) / 16;
91 sps->i_mb_height= ( param->i_height + 15 )/ 16;
92 sps->b_frame_mbs_only = 1;
93 sps->b_mb_adaptive_frame_field = 0;
94 sps->b_direct8x8_inference = 0;
95 if( sps->b_frame_mbs_only == 0 ||
96 !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
98 sps->b_direct8x8_inference = 1;
101 if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
104 sps->crop.i_left = 0;
105 sps->crop.i_right = ( 16 - param->i_width % 16)/2;
107 sps->crop.i_bottom = ( 16 - param->i_height % 16)/2;
112 sps->crop.i_left = 0;
113 sps->crop.i_right = 0;
115 sps->crop.i_bottom = 0;
119 sps->vui.b_aspect_ratio_info_present = 0;
121 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
123 sps->vui.b_aspect_ratio_info_present = 1;
124 sps->vui.i_sar_width = param->vui.i_sar_width;
125 sps->vui.i_sar_height= param->vui.i_sar_height;
127 sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
129 if( param->i_fps_num > 0 && param->i_fps_den > 0)
131 sps->vui.b_timing_info_present = 1;
132 /* The standard is confusing me, but this seems to work best
133 with other encoders */
134 sps->vui.i_num_units_in_tick = param->i_fps_den;
135 sps->vui.i_time_scale = param->i_fps_num;
136 sps->vui.b_fixed_frame_rate = 1;
138 sps->b_vui |= sps->vui.b_timing_info_present;
140 sps->vui.b_bitstream_restriction = param->i_bframe > 0;
141 if( sps->vui.b_bitstream_restriction )
143 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
144 sps->vui.i_max_bytes_per_pic_denom = 0;
145 sps->vui.i_max_bits_per_mb_denom = 0;
146 sps->vui.i_log2_max_mv_length_horizontal =
147 sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 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 );
166 if( sps->i_profile_idc >= PROFILE_HIGH )
168 bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
169 bs_write_ue( s, 0 ); // bit_depth_luma_minus8
170 bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
171 bs_write( s, 1, 0 ); // qpprime_y_zero_transform_bypass_flag
172 bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
175 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
176 bs_write_ue( s, sps->i_poc_type );
177 if( sps->i_poc_type == 0 )
179 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
181 else if( sps->i_poc_type == 1 )
185 bs_write( s, 1, sps->b_delta_pic_order_always_zero );
186 bs_write_se( s, sps->i_offset_for_non_ref_pic );
187 bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
188 bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
190 for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
192 bs_write_se( s, sps->i_offset_for_ref_frame[i] );
195 bs_write_ue( s, sps->i_num_ref_frames );
196 bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
197 bs_write_ue( s, sps->i_mb_width - 1 );
198 bs_write_ue( s, sps->i_mb_height - 1);
199 bs_write( s, 1, sps->b_frame_mbs_only );
200 if( !sps->b_frame_mbs_only )
202 bs_write( s, 1, sps->b_mb_adaptive_frame_field );
204 bs_write( s, 1, sps->b_direct8x8_inference );
206 bs_write( s, 1, sps->b_crop );
209 bs_write_ue( s, sps->crop.i_left );
210 bs_write_ue( s, sps->crop.i_right );
211 bs_write_ue( s, sps->crop.i_top );
212 bs_write_ue( s, sps->crop.i_bottom );
215 bs_write( s, 1, sps->b_vui );
218 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
219 if( sps->vui.b_aspect_ratio_info_present )
222 static const struct { int w, h; int sar; } sar[] =
224 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
225 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
226 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
227 { 160,99, 13}, { 0, 0, -1 }
229 for( i = 0; sar[i].sar != -1; i++ )
231 if( sar[i].w == sps->vui.i_sar_width &&
232 sar[i].h == sps->vui.i_sar_height )
235 if( sar[i].sar != -1 )
237 bs_write( s, 8, sar[i].sar );
241 bs_write( s, 8, 255); /* aspect_ratio_idc (extented) */
242 bs_write( s, 16, sps->vui.i_sar_width );
243 bs_write( s, 16, sps->vui.i_sar_height );
247 bs_write1( s, 0 ); /* overscan_info_present_flag */
249 bs_write1( s, 0 ); /* video_signal_type_present_flag */
251 bs_write( s, 3, 5 ); /* unspecified video format */
252 bs_write1( s, 1 ); /* video full range flag */
253 bs_write1( s, 0 ); /* colour description present flag */
255 bs_write1( s, 0 ); /* chroma_loc_info_present_flag */
257 bs_write1( s, sps->vui.b_timing_info_present );
258 if( sps->vui.b_timing_info_present )
260 bs_write( s, 32, sps->vui.i_num_units_in_tick );
261 bs_write( s, 32, sps->vui.i_time_scale );
262 bs_write1( s, sps->vui.b_fixed_frame_rate );
265 bs_write1( s, 0 ); /* nal_hrd_parameters_present_flag */
266 bs_write1( s, 0 ); /* vcl_hrd_parameters_present_flag */
267 bs_write1( s, 0 ); /* pic_struct_present_flag */
268 bs_write1( s, sps->vui.b_bitstream_restriction );
269 if( sps->vui.b_bitstream_restriction )
271 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
272 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
273 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
274 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
275 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
276 bs_write_ue( s, sps->vui.i_num_reorder_frames );
277 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
281 bs_rbsp_trailing( s );
284 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
287 pps->i_sps_id = sps->i_id;
288 pps->b_cabac = param->b_cabac;
290 pps->b_pic_order = 0;
291 pps->i_num_slice_groups = 1;
293 if( pps->i_num_slice_groups > 1 )
297 pps->i_slice_group_map_type = 0;
298 if( pps->i_slice_group_map_type == 0 )
300 for( i = 0; i < pps->i_num_slice_groups; i++ )
302 pps->i_run_length[i] = 1;
305 else if( pps->i_slice_group_map_type == 2 )
307 for( i = 0; i < pps->i_num_slice_groups; i++ )
309 pps->i_top_left[i] = 0;
310 pps->i_bottom_right[i] = 0;
313 else if( pps->i_slice_group_map_type >= 3 &&
314 pps->i_slice_group_map_type <= 5 )
316 pps->b_slice_group_change_direction = 0;
317 pps->i_slice_group_change_rate = 0;
319 else if( pps->i_slice_group_map_type == 6 )
321 pps->i_pic_size_in_map_units = 1;
322 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
324 pps->i_slice_group_id[i] = 0;
328 pps->i_num_ref_idx_l0_active = 1;
329 pps->i_num_ref_idx_l1_active = 1;
331 pps->b_weighted_pred = 0;
332 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
334 pps->i_pic_init_qp = 26;
335 pps->i_pic_init_qs = 26;
337 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
338 pps->b_deblocking_filter_control = 1;
339 pps->b_constrained_intra_pred = 0;
340 pps->b_redundant_pic_cnt = 0;
342 pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
345 void x264_pps_write( bs_t *s, x264_pps_t *pps )
347 bs_write_ue( s, pps->i_id );
348 bs_write_ue( s, pps->i_sps_id );
350 bs_write( s, 1, pps->b_cabac );
351 bs_write( s, 1, pps->b_pic_order );
352 bs_write_ue( s, pps->i_num_slice_groups - 1 );
354 if( pps->i_num_slice_groups > 1 )
358 bs_write_ue( s, pps->i_slice_group_map_type );
359 if( pps->i_slice_group_map_type == 0 )
361 for( i = 0; i < pps->i_num_slice_groups; i++ )
363 bs_write_ue( s, pps->i_run_length[i] - 1 );
366 else if( pps->i_slice_group_map_type == 2 )
368 for( i = 0; i < pps->i_num_slice_groups; i++ )
370 bs_write_ue( s, pps->i_top_left[i] );
371 bs_write_ue( s, pps->i_bottom_right[i] );
374 else if( pps->i_slice_group_map_type >= 3 &&
375 pps->i_slice_group_map_type <= 5 )
377 bs_write( s, 1, pps->b_slice_group_change_direction );
378 bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
380 else if( pps->i_slice_group_map_type == 6 )
382 bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
383 for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
386 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
387 * pps->i_slice_group_id[i] );
393 bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
394 bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
395 bs_write( s, 1, pps->b_weighted_pred );
396 bs_write( s, 2, pps->b_weighted_bipred );
398 bs_write_se( s, pps->i_pic_init_qp - 26 );
399 bs_write_se( s, pps->i_pic_init_qs - 26 );
400 bs_write_se( s, pps->i_chroma_qp_index_offset );
402 bs_write( s, 1, pps->b_deblocking_filter_control );
403 bs_write( s, 1, pps->b_constrained_intra_pred );
404 bs_write( s, 1, pps->b_redundant_pic_cnt );
406 if( pps->b_transform_8x8_mode )
408 bs_write( s, 1, pps->b_transform_8x8_mode );
409 bs_write( s, 1, 0 ); // pic_scaling_matrix_present_flag
410 bs_write_se( s, 0 ); // second_chroma_qp_index_offset
413 bs_rbsp_trailing( s );
416 void x264_sei_version_write( bs_t *s )
419 // random ID number generated according to ISO-11578
420 const uint8_t uuid[16] = {
421 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
422 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
426 sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
427 X264_BUILD, X264_VERSION );
428 length = strlen(version)+1+16;
430 bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
431 while( length > 255 )
432 bs_write( s, 8, 255 ), length -= 255;
433 bs_write( s, 8, length ); // payload_size
435 for( i = 0; i < 16; i++ )
436 bs_write( s, 8, uuid[i] );
437 for( i = 0; i < length-16; i++ )
438 bs_write( s, 8, version[i] );
440 bs_rbsp_trailing( s );