1 /*****************************************************************************
2 * set: header writing
3 *****************************************************************************
4 * Copyright (C) 2003-2014 x264 project
5 *
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 *
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.
13 *
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.
18 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
26
27 #include "common/common.h"
28 #include "set.h"
29
30 #define bs_write_ue bs_write_ue_big
31
32 // Indexed by pic_struct values
33 static const uint8_t num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
34 const static uint8_t avcintra_uuid[] = {0xF7, 0x49, 0x3E, 0xB3, 0xD4, 0x00, 0x47, 0x96, 0x86, 0x86, 0xC9, 0x70, 0x7B, 0x64, 0x37, 0x2A};
35
transpose(uint8_t * buf,int w)36 static void transpose( uint8_t *buf, int w )
37 {
38 int i;
39 int j;
40
41 for( i = 0; i < w; i++ )
42 for( j = 0; j < i; j++ )
43 XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
44 }
45
scaling_list_write(bs_t * s,x264_pps_t * pps,int idx)46 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
47 {
48 const int len = idx<4 ? 16 : 64;
49 const uint8_t *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
50 const uint8_t *list = pps->scaling_list[idx];
51 const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
52 : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
53 : (idx==CQM_8IC+4) ? pps->scaling_list[CQM_8IY+4]
54 : (idx==CQM_8PC+4) ? pps->scaling_list[CQM_8PY+4]
55 : x264_cqm_jvt[idx];
56 if( !memcmp( list, def_list, len ) )
57 bs_write1( s, 0 ); // scaling_list_present_flag
58 else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
59 {
60 bs_write1( s, 1 ); // scaling_list_present_flag
61 bs_write_se( s, -8 ); // use jvt list
62 }
63 else
64 {
65 int run;
66 int j;
67
68 bs_write1( s, 1 ); // scaling_list_present_flag
69
70 // try run-length compression of trailing values
71 for( run = len; run > 1; run-- )
72 if( list[zigzag[run-1]] != list[zigzag[run-2]] )
73 break;
74 if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) )
75 run = len;
76
77 for( j = 0; j < run; j++ )
78 bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta
79
80 if( run < len )
81 bs_write_se( s, (int8_t)-list[zigzag[run]] );
82 }
83 }
84
x264_sei_write(bs_t * s,uint8_t * payload,int payload_size,int payload_type)85 void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type )
86 {
87 int i;
88
89 bs_realign( s );
90
91 for( i = 0; i <= payload_type-255; i += 255 )
92 bs_write( s, 8, 255 );
93 bs_write( s, 8, payload_type-i );
94
95 for( i = 0; i <= payload_size-255; i += 255 )
96 bs_write( s, 8, 255 );
97 bs_write( s, 8, payload_size-i );
98
99 for( i = 0; i < payload_size; i++ )
100 bs_write( s, 8, payload[i] );
101
102 bs_rbsp_trailing( s );
103 bs_flush( s );
104 }
105
x264_sps_init(x264_sps_t * sps,int i_id,x264_param_t * param)106 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
107 {
108 int csp = param->i_csp & X264_CSP_MASK;
109 int max_frame_num;
110
111 sps->i_id = i_id;
112 sps->i_mb_width = ( param->i_width + 15 ) / 16;
113 sps->i_mb_height= ( param->i_height + 15 ) / 16;
114 sps->i_chroma_format_idc = csp >= X264_CSP_I444 ? CHROMA_444 :
115 csp >= X264_CSP_I422 ? CHROMA_422 : CHROMA_420;
116
117 sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
118 if( sps->b_qpprime_y_zero_transform_bypass || sps->i_chroma_format_idc == CHROMA_444 )
119 sps->i_profile_idc = PROFILE_HIGH444_PREDICTIVE;
120 else if( sps->i_chroma_format_idc == CHROMA_422 )
121 sps->i_profile_idc = PROFILE_HIGH422;
122 else if( BIT_DEPTH > 8 )
123 sps->i_profile_idc = PROFILE_HIGH10;
124 else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
125 sps->i_profile_idc = PROFILE_HIGH;
126 else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
127 sps->i_profile_idc = PROFILE_MAIN;
128 else
129 sps->i_profile_idc = PROFILE_BASELINE;
130
131 sps->b_constraint_set0 = sps->i_profile_idc == PROFILE_BASELINE;
132 /* x264 doesn't support the features that are in Baseline and not in Main,
133 * namely arbitrary_slice_order and slice_groups. */
134 sps->b_constraint_set1 = sps->i_profile_idc <= PROFILE_MAIN;
135 /* Never set constraint_set2, it is not necessary and not used in real world. */
136 sps->b_constraint_set2 = 0;
137 sps->b_constraint_set3 = 0;
138
139 sps->i_level_idc = param->i_level_idc;
140 if( param->i_level_idc == 9 && ( sps->i_profile_idc == PROFILE_BASELINE || sps->i_profile_idc == PROFILE_MAIN ) )
141 {
142 sps->b_constraint_set3 = 1; /* level 1b with Baseline or Main profile is signalled via constraint_set3 */
143 sps->i_level_idc = 11;
144 }
145 /* Intra profiles */
146 if( param->i_keyint_max == 1 && sps->i_profile_idc > PROFILE_HIGH )
147 sps->b_constraint_set3 = 1;
148
149 sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
150 /* extra slot with pyramid so that we don't have to override the
151 * order of forgetting old pictures */
152 sps->vui.i_max_dec_frame_buffering =
153 sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
154 param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
155 sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
156 if( param->i_keyint_max == 1 )
157 {
158 sps->i_num_ref_frames = 0;
159 sps->vui.i_max_dec_frame_buffering = 0;
160 }
161
162 /* number of refs + current frame */
163 max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
164 /* Intra refresh cannot write a recovery time greater than max frame num-1 */
165 if( param->b_intra_refresh )
166 {
167 int time_to_recovery = X264_MIN( sps->i_mb_width - 1, param->i_keyint_max ) + param->i_bframe - 1;
168 max_frame_num = X264_MAX( max_frame_num, time_to_recovery+1 );
169 }
170
171 sps->i_log2_max_frame_num = 4;
172 while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
173 sps->i_log2_max_frame_num++;
174
175 sps->i_poc_type = param->i_bframe || param->b_interlaced ? 0 : 2;
176 if( sps->i_poc_type == 0 )
177 {
178 int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
179 sps->i_log2_max_poc_lsb = 4;
180 while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
181 sps->i_log2_max_poc_lsb++;
182 }
183
184 sps->b_vui = 1;
185
186 sps->b_gaps_in_frame_num_value_allowed = 0;
187 sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
188 if( !sps->b_frame_mbs_only )
189 sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
190 sps->b_mb_adaptive_frame_field = param->b_interlaced;
191 sps->b_direct8x8_inference = 1;
192
193 sps->crop.i_left = param->crop_rect.i_left;
194 sps->crop.i_top = param->crop_rect.i_top;
195 sps->crop.i_right = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
196 sps->crop.i_bottom = (param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
197 sps->b_crop = sps->crop.i_left || sps->crop.i_top ||
198 sps->crop.i_right || sps->crop.i_bottom;
199
200 sps->vui.b_aspect_ratio_info_present = 0;
201 if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
202 {
203 sps->vui.b_aspect_ratio_info_present = 1;
204 sps->vui.i_sar_width = param->vui.i_sar_width;
205 sps->vui.i_sar_height= param->vui.i_sar_height;
206 }
207
208 sps->vui.b_overscan_info_present = param->vui.i_overscan > 0 && param->vui.i_overscan <= 2;
209 if( sps->vui.b_overscan_info_present )
210 sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
211
212 sps->vui.b_signal_type_present = 0;
213 sps->vui.i_vidformat = ( param->vui.i_vidformat >= 0 && param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
214 sps->vui.b_fullrange = ( param->vui.b_fullrange >= 0 && param->vui.b_fullrange <= 1 ? param->vui.b_fullrange :
215 ( csp >= X264_CSP_BGR ? 1 : 0 ) );
216 sps->vui.b_color_description_present = 0;
217
218 sps->vui.i_colorprim = ( param->vui.i_colorprim >= 0 && param->vui.i_colorprim <= 9 ? param->vui.i_colorprim : 2 );
219 sps->vui.i_transfer = ( param->vui.i_transfer >= 0 && param->vui.i_transfer <= 15 ? param->vui.i_transfer : 2 );
220 sps->vui.i_colmatrix = ( param->vui.i_colmatrix >= 0 && param->vui.i_colmatrix <= 10 ? param->vui.i_colmatrix :
221 ( csp >= X264_CSP_BGR ? 0 : 2 ) );
222 if( sps->vui.i_colorprim != 2 ||
223 sps->vui.i_transfer != 2 ||
224 sps->vui.i_colmatrix != 2 )
225 {
226 sps->vui.b_color_description_present = 1;
227 }
228
229 if( sps->vui.i_vidformat != 5 ||
230 sps->vui.b_fullrange ||
231 sps->vui.b_color_description_present )
232 {
233 sps->vui.b_signal_type_present = 1;
234 }
235
236 /* FIXME: not sufficient for interlaced video */
237 sps->vui.b_chroma_loc_info_present = param->vui.i_chroma_loc > 0 && param->vui.i_chroma_loc <= 5 &&
238 sps->i_chroma_format_idc == CHROMA_420;
239 if( sps->vui.b_chroma_loc_info_present )
240 {
241 sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
242 sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
243 }
244
245 sps->vui.b_timing_info_present = param->i_timebase_num > 0 && param->i_timebase_den > 0;
246
247 if( sps->vui.b_timing_info_present )
248 {
249 sps->vui.i_num_units_in_tick = param->i_timebase_num;
250 sps->vui.i_time_scale = param->i_timebase_den * 2;
251 sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
252 }
253
254 sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
255 sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
256 sps->vui.b_pic_struct_present = param->b_pic_struct;
257
258 // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
259
260 sps->vui.b_bitstream_restriction = param->i_keyint_max > 1;
261 if( sps->vui.b_bitstream_restriction )
262 {
263 sps->vui.b_motion_vectors_over_pic_boundaries = 1;
264 sps->vui.i_max_bytes_per_pic_denom = 0;
265 sps->vui.i_max_bits_per_mb_denom = 0;
266 sps->vui.i_log2_max_mv_length_horizontal =
267 sps->vui.i_log2_max_mv_length_vertical = (int)log2f( X264_MAX( 1, param->analyse.i_mv_range*4-1 ) ) + 1;
268 }
269 }
270
x264_sps_write(bs_t * s,x264_sps_t * sps)271 void x264_sps_write( bs_t *s, x264_sps_t *sps )
272 {
273 bs_realign( s );
274 bs_write( s, 8, sps->i_profile_idc );
275 bs_write1( s, sps->b_constraint_set0 );
276 bs_write1( s, sps->b_constraint_set1 );
277 bs_write1( s, sps->b_constraint_set2 );
278 bs_write1( s, sps->b_constraint_set3 );
279
280 bs_write( s, 4, 0 ); /* reserved */
281
282 bs_write( s, 8, sps->i_level_idc );
283
284 bs_write_ue( s, sps->i_id );
285
286 if( sps->i_profile_idc >= PROFILE_HIGH )
287 {
288 bs_write_ue( s, sps->i_chroma_format_idc );
289 if( sps->i_chroma_format_idc == CHROMA_444 )
290 bs_write1( s, 0 ); // separate_colour_plane_flag
291 bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
292 bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
293 bs_write1( s, sps->b_qpprime_y_zero_transform_bypass );
294 bs_write1( s, 0 ); // seq_scaling_matrix_present_flag
295 }
296
297 bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
298 bs_write_ue( s, sps->i_poc_type );
299 if( sps->i_poc_type == 0 )
300 bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
301 bs_write_ue( s, sps->i_num_ref_frames );
302 bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
303 bs_write_ue( s, sps->i_mb_width - 1 );
304 bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
305 bs_write1( s, sps->b_frame_mbs_only );
306 if( !sps->b_frame_mbs_only )
307 bs_write1( s, sps->b_mb_adaptive_frame_field );
308 bs_write1( s, sps->b_direct8x8_inference );
309
310 bs_write1( s, sps->b_crop );
311 if( sps->b_crop )
312 {
313 int h_shift = sps->i_chroma_format_idc == CHROMA_420 || sps->i_chroma_format_idc == CHROMA_422;
314 int v_shift = sps->i_chroma_format_idc == CHROMA_420;
315 bs_write_ue( s, sps->crop.i_left >> h_shift );
316 bs_write_ue( s, sps->crop.i_right >> h_shift );
317 bs_write_ue( s, sps->crop.i_top >> v_shift );
318 bs_write_ue( s, sps->crop.i_bottom >> v_shift );
319 }
320
321 bs_write1( s, sps->b_vui );
322 if( sps->b_vui )
323 {
324 bs_write1( s, sps->vui.b_aspect_ratio_info_present );
325 if( sps->vui.b_aspect_ratio_info_present )
326 {
327 int i;
328 static const struct { uint8_t w, h, sar; } sar[] =
329 {
330 // aspect_ratio_idc = 0 -> unspecified
331 { 1, 1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
332 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
333 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
334 {160, 99, 13}, { 4, 3, 14}, { 3, 2, 15}, { 2, 1, 16},
335 // aspect_ratio_idc = [17..254] -> reserved
336 { 0, 0, 255 }
337 };
338 for( i = 0; sar[i].sar != 255; i++ )
339 {
340 if( sar[i].w == sps->vui.i_sar_width &&
341 sar[i].h == sps->vui.i_sar_height )
342 break;
343 }
344 bs_write( s, 8, sar[i].sar );
345 if( sar[i].sar == 255 ) /* aspect_ratio_idc (extended) */
346 {
347 bs_write( s, 16, sps->vui.i_sar_width );
348 bs_write( s, 16, sps->vui.i_sar_height );
349 }
350 }
351
352 bs_write1( s, sps->vui.b_overscan_info_present );
353 if( sps->vui.b_overscan_info_present )
354 bs_write1( s, sps->vui.b_overscan_info );
355
356 bs_write1( s, sps->vui.b_signal_type_present );
357 if( sps->vui.b_signal_type_present )
358 {
359 bs_write( s, 3, sps->vui.i_vidformat );
360 bs_write1( s, sps->vui.b_fullrange );
361 bs_write1( s, sps->vui.b_color_description_present );
362 if( sps->vui.b_color_description_present )
363 {
364 bs_write( s, 8, sps->vui.i_colorprim );
365 bs_write( s, 8, sps->vui.i_transfer );
366 bs_write( s, 8, sps->vui.i_colmatrix );
367 }
368 }
369
370 bs_write1( s, sps->vui.b_chroma_loc_info_present );
371 if( sps->vui.b_chroma_loc_info_present )
372 {
373 bs_write_ue( s, sps->vui.i_chroma_loc_top );
374 bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
375 }
376
377 bs_write1( s, sps->vui.b_timing_info_present );
378 if( sps->vui.b_timing_info_present )
379 {
380 bs_write32( s, sps->vui.i_num_units_in_tick );
381 bs_write32( s, sps->vui.i_time_scale );
382 bs_write1( s, sps->vui.b_fixed_frame_rate );
383 }
384
385 bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
386 if( sps->vui.b_nal_hrd_parameters_present )
387 {
388 bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
389 bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
390 bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
391
392 bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
393 bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
394
395 bs_write1( s, sps->vui.hrd.b_cbr_hrd );
396
397 bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
398 bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
399 bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
400 bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
401 }
402
403 bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
404
405 if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
406 bs_write1( s, 0 ); /* low_delay_hrd_flag */
407
408 bs_write1( s, sps->vui.b_pic_struct_present );
409 bs_write1( s, sps->vui.b_bitstream_restriction );
410 if( sps->vui.b_bitstream_restriction )
411 {
412 bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
413 bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
414 bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
415 bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
416 bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
417 bs_write_ue( s, sps->vui.i_num_reorder_frames );
418 bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
419 }
420 }
421
422 bs_rbsp_trailing( s );
423 bs_flush( s );
424 }
425
x264_pps_init(x264_pps_t * pps,int i_id,x264_param_t * param,x264_sps_t * sps)426 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
427 {
428 int i;
429 int j;
430
431 pps->i_id = i_id;
432 pps->i_sps_id = sps->i_id;
433 pps->b_cabac = param->b_cabac;
434
435 pps->b_pic_order = !param->i_avcintra_class && param->b_interlaced;
436 pps->i_num_slice_groups = 1;
437
438 pps->i_num_ref_idx_l0_default_active = param->i_frame_reference;
439 pps->i_num_ref_idx_l1_default_active = 1;
440
441 pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
442 pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
443
444 pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR || param->b_stitchable ? 26 + QP_BD_OFFSET : SPEC_QP( param->rc.i_qp_constant );
445 pps->i_pic_init_qs = 26 + QP_BD_OFFSET;
446
447 pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
448 pps->b_deblocking_filter_control = 1;
449 pps->b_constrained_intra_pred = param->b_constrained_intra;
450 pps->b_redundant_pic_cnt = 0;
451
452 pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
453
454 pps->i_cqm_preset = param->i_cqm_preset;
455
456 switch( pps->i_cqm_preset )
457 {
458 case X264_CQM_FLAT:
459 for( i = 0; i < 8; i++ )
460 pps->scaling_list[i] = x264_cqm_flat16;
461 break;
462 case X264_CQM_JVT:
463 for( i = 0; i < 8; i++ )
464 pps->scaling_list[i] = x264_cqm_jvt[i];
465 break;
466 case X264_CQM_CUSTOM:
467 /* match the transposed DCT & zigzag */
468 transpose( param->cqm_4iy, 4 );
469 transpose( param->cqm_4py, 4 );
470 transpose( param->cqm_4ic, 4 );
471 transpose( param->cqm_4pc, 4 );
472 transpose( param->cqm_8iy, 8 );
473 transpose( param->cqm_8py, 8 );
474 transpose( param->cqm_8ic, 8 );
475 transpose( param->cqm_8pc, 8 );
476 pps->scaling_list[CQM_4IY] = param->cqm_4iy;
477 pps->scaling_list[CQM_4PY] = param->cqm_4py;
478 pps->scaling_list[CQM_4IC] = param->cqm_4ic;
479 pps->scaling_list[CQM_4PC] = param->cqm_4pc;
480 pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
481 pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
482 pps->scaling_list[CQM_8IC+4] = param->cqm_8ic;
483 pps->scaling_list[CQM_8PC+4] = param->cqm_8pc;
484 for( i = 0; i < 8; i++ )
485 for( j = 0; j < (i < 4 ? 16 : 64); j++ )
486 if( pps->scaling_list[i][j] == 0 )
487 pps->scaling_list[i] = x264_cqm_jvt[i];
488 break;
489 }
490 }
491
x264_pps_write(bs_t * s,x264_sps_t * sps,x264_pps_t * pps)492 void x264_pps_write( bs_t *s, x264_sps_t *sps, x264_pps_t *pps )
493 {
494 bs_realign( s );
495 bs_write_ue( s, pps->i_id );
496 bs_write_ue( s, pps->i_sps_id );
497
498 bs_write1( s, pps->b_cabac );
499 bs_write1( s, pps->b_pic_order );
500 bs_write_ue( s, pps->i_num_slice_groups - 1 );
501
502 bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
503 bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
504 bs_write1( s, pps->b_weighted_pred );
505 bs_write( s, 2, pps->b_weighted_bipred );
506
507 bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
508 bs_write_se( s, pps->i_pic_init_qs - 26 - QP_BD_OFFSET );
509 bs_write_se( s, pps->i_chroma_qp_index_offset );
510
511 bs_write1( s, pps->b_deblocking_filter_control );
512 bs_write1( s, pps->b_constrained_intra_pred );
513 bs_write1( s, pps->b_redundant_pic_cnt );
514
515 if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
516 {
517 bs_write1( s, pps->b_transform_8x8_mode );
518 bs_write1( s, (pps->i_cqm_preset != X264_CQM_FLAT) );
519 if( pps->i_cqm_preset != X264_CQM_FLAT )
520 {
521 scaling_list_write( s, pps, CQM_4IY );
522 scaling_list_write( s, pps, CQM_4IC );
523 bs_write1( s, 0 ); // Cr = Cb
524 scaling_list_write( s, pps, CQM_4PY );
525 scaling_list_write( s, pps, CQM_4PC );
526 bs_write1( s, 0 ); // Cr = Cb
527 if( pps->b_transform_8x8_mode )
528 {
529 if( sps->i_chroma_format_idc == CHROMA_444 )
530 {
531 scaling_list_write( s, pps, CQM_8IY+4 );
532 scaling_list_write( s, pps, CQM_8IC+4 );
533 bs_write1( s, 0 ); // Cr = Cb
534 scaling_list_write( s, pps, CQM_8PY+4 );
535 scaling_list_write( s, pps, CQM_8PC+4 );
536 bs_write1( s, 0 ); // Cr = Cb
537 }
538 else
539 {
540 scaling_list_write( s, pps, CQM_8IY+4 );
541 scaling_list_write( s, pps, CQM_8PY+4 );
542 }
543 }
544 }
545 bs_write_se( s, pps->i_chroma_qp_index_offset );
546 }
547
548 bs_rbsp_trailing( s );
549 bs_flush( s );
550 }
551
x264_sei_recovery_point_write(x264_t * h,bs_t * s,int recovery_frame_cnt)552 void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
553 {
554 bs_t q;
555 uint8_t tmp_buf[100];
556 bs_init( &q, tmp_buf, 100 );
557
558 bs_realign( &q );
559
560 bs_write_ue( &q, recovery_frame_cnt ); // recovery_frame_cnt
561 bs_write1( &q, 1 ); //exact_match_flag 1
562 bs_write1( &q, 0 ); //broken_link_flag 0
563 bs_write( &q, 2, 0 ); //changing_slice_group 0
564
565 bs_align_10( &q );
566 bs_flush( &q );
567
568 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_RECOVERY_POINT );
569 }
570
x264_sei_version_write(x264_t * h,bs_t * s)571 int x264_sei_version_write( x264_t *h, bs_t *s )
572 {
573 // random ID number generated according to ISO-11578
574 static const uint8_t uuid[16] =
575 {
576 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
577 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
578 };
579 char *opts = x264_param2string( &h->param, 0 );
580 char *payload;
581 int length;
582
583 if( !opts )
584 return -1;
585 CHECKED_MALLOC( payload, 200 + strlen( opts ) );
586
587 memcpy( payload, uuid, 16 );
588 sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
589 "Copy%s 2003-2014 - http://www.videolan.org/x264.html - options: %s",
590 X264_BUILD, X264_VERSION, HAVE_GPL?"left":"right", opts );
591 length = strlen(payload)+1;
592
593 x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
594
595 x264_free( opts );
596 x264_free( payload );
597 return 0;
598 fail:
599 x264_free( opts );
600 return -1;
601 }
602
x264_sei_buffering_period_write(x264_t * h,bs_t * s)603 void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
604 {
605 x264_sps_t *sps = h->sps;
606 bs_t q;
607 uint8_t tmp_buf[100];
608 bs_init( &q, tmp_buf, 100 );
609
610 bs_realign( &q );
611 bs_write_ue( &q, sps->i_id );
612
613 if( sps->vui.b_nal_hrd_parameters_present )
614 {
615 bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
616 bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
617 }
618
619 bs_align_10( &q );
620 bs_flush( &q );
621
622 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_BUFFERING_PERIOD );
623 }
624
x264_sei_pic_timing_write(x264_t * h,bs_t * s)625 void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
626 {
627 x264_sps_t *sps = h->sps;
628 bs_t q;
629 uint8_t tmp_buf[100];
630 bs_init( &q, tmp_buf, 100 );
631
632 bs_realign( &q );
633
634 if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
635 {
636 bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay - h->i_cpb_delay_pir_offset );
637 bs_write( &q, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
638 }
639
640 if( sps->vui.b_pic_struct_present )
641 {
642 int i;
643
644 bs_write( &q, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
645
646 // These clock timestamps are not standardised so we don't set them
647 // They could be time of origin, capture or alternative ideal display
648 for( i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
649 bs_write1( &q, 0 ); // clock_timestamp_flag
650 }
651
652 bs_align_10( &q );
653 bs_flush( &q );
654
655 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_PIC_TIMING );
656 }
657
x264_sei_frame_packing_write(x264_t * h,bs_t * s)658 void x264_sei_frame_packing_write( x264_t *h, bs_t *s )
659 {
660 int quincunx_sampling_flag = h->param.i_frame_packing == 0;
661 bs_t q;
662 uint8_t tmp_buf[100];
663 bs_init( &q, tmp_buf, 100 );
664
665 bs_realign( &q );
666
667 bs_write_ue( &q, 0 ); // frame_packing_arrangement_id
668 bs_write1( &q, 0 ); // frame_packing_arrangement_cancel_flag
669 bs_write ( &q, 7, h->param.i_frame_packing ); // frame_packing_arrangement_type
670 bs_write1( &q, quincunx_sampling_flag ); // quincunx_sampling_flag
671
672 // 0: views are unrelated, 1: left view is on the left, 2: left view is on the right
673 bs_write ( &q, 6, 1 ); // content_interpretation_type
674
675 bs_write1( &q, 0 ); // spatial_flipping_flag
676 bs_write1( &q, 0 ); // frame0_flipped_flag
677 bs_write1( &q, 0 ); // field_views_flag
678 bs_write1( &q, h->param.i_frame_packing == 5 && !(h->fenc->i_frame&1) ); // current_frame_is_frame0_flag
679 bs_write1( &q, 0 ); // frame0_self_contained_flag
680 bs_write1( &q, 0 ); // frame1_self_contained_flag
681 if ( quincunx_sampling_flag == 0 && h->param.i_frame_packing != 5 )
682 {
683 bs_write( &q, 4, 0 ); // frame0_grid_position_x
684 bs_write( &q, 4, 0 ); // frame0_grid_position_y
685 bs_write( &q, 4, 0 ); // frame1_grid_position_x
686 bs_write( &q, 4, 0 ); // frame1_grid_position_y
687 }
688 bs_write( &q, 8, 0 ); // frame_packing_arrangement_reserved_byte
689 // "frame_packing_arrangement_repetition_period equal to 1 specifies that the frame packing arrangement SEI message persists in output"
690 // for (i_frame_packing == 5) this will undermine current_frame_is_frame0_flag which must alternate every view sequence
691 bs_write_ue( &q, h->param.i_frame_packing != 5 ); // frame_packing_arrangement_repetition_period
692 bs_write1( &q, 0 ); // frame_packing_arrangement_extension_flag
693
694 bs_align_10( &q );
695 bs_flush( &q );
696
697 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_FRAME_PACKING );
698 }
699
x264_filler_write(x264_t * h,bs_t * s,int filler)700 void x264_filler_write( x264_t *h, bs_t *s, int filler )
701 {
702 int i;
703
704 bs_realign( s );
705
706 for( i = 0; i < filler; i++ )
707 bs_write( s, 8, 0xff );
708
709 bs_rbsp_trailing( s );
710 bs_flush( s );
711 }
712
x264_sei_dec_ref_pic_marking_write(x264_t * h,bs_t * s)713 void x264_sei_dec_ref_pic_marking_write( x264_t *h, bs_t *s )
714 {
715 x264_slice_header_t *sh = &h->sh_backup;
716 bs_t q;
717 uint8_t tmp_buf[100];
718 bs_init( &q, tmp_buf, 100 );
719
720 bs_realign( &q );
721
722 /* We currently only use this for repeating B-refs, as required by Blu-ray. */
723 bs_write1( &q, 0 ); //original_idr_flag
724 bs_write_ue( &q, sh->i_frame_num ); //original_frame_num
725 if( !h->sps->b_frame_mbs_only )
726 bs_write1( &q, 0 ); //original_field_pic_flag
727
728 bs_write1( &q, sh->i_mmco_command_count > 0 );
729 if( sh->i_mmco_command_count > 0 )
730 {
731 int i;
732
733 for( i = 0; i < sh->i_mmco_command_count; i++ )
734 {
735 bs_write_ue( &q, 1 );
736 bs_write_ue( &q, sh->mmco[i].i_difference_of_pic_nums - 1 );
737 }
738 bs_write_ue( &q, 0 );
739 }
740
741 bs_align_10( &q );
742 bs_flush( &q );
743
744 x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_DEC_REF_PIC_MARKING );
745 }
746
x264_sei_avcintra_umid_write(x264_t * h,bs_t * s)747 int x264_sei_avcintra_umid_write( x264_t *h, bs_t *s )
748 {
749 uint8_t data[512];
750 const char *msg = "UMID";
751 const int len = 497;
752
753 memset( data, 0xff, len );
754 memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
755 memcpy( data+16, msg, strlen(msg) );
756
757 data[20] = 0x13;
758 /* These bytes appear to be some sort of frame/seconds counter in certain applications,
759 * but others jump around, so leave them as zero for now */
760 data[22] = data[23] = data[25] = data[26] = 0;
761 data[28] = 0x14;
762 data[30] = data[31] = data[33] = data[34] = 0;
763 data[36] = 0x60;
764 data[41] = 0x22; /* Believed to be some sort of end of basic UMID identifier */
765 data[60] = 0x62;
766 data[62] = data[63] = data[65] = data[66] = 0;
767 data[68] = 0x63;
768 data[70] = data[71] = data[73] = data[74] = 0;
769
770 x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
771
772 return 0;
773 }
774
x264_sei_avcintra_vanc_write(x264_t * h,bs_t * s,int len)775 int x264_sei_avcintra_vanc_write( x264_t *h, bs_t *s, int len )
776 {
777 uint8_t data[6000];
778 const char *msg = "VANC";
779 if( len > sizeof(data) )
780 {
781 x264_log( h, X264_LOG_ERROR, "AVC-Intra SEI is too large (%d)\n", len );
782 return -1;
783 }
784
785 memset( data, 0xff, len );
786 memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
787 memcpy( data+16, msg, strlen(msg) );
788
789 x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
790
791 return 0;
792 }
793
794 const x264_level_t x264_levels[] =
795 {
796 { 10, 1485, 99, 396, 64, 175, 64, 64, 0, 2, 0, 0, 1 },
797 { 9, 1485, 99, 396, 128, 350, 64, 64, 0, 2, 0, 0, 1 }, /* "1b" */
798 { 11, 3000, 396, 900, 192, 500, 128, 64, 0, 2, 0, 0, 1 },
799 { 12, 6000, 396, 2376, 384, 1000, 128, 64, 0, 2, 0, 0, 1 },
800 { 13, 11880, 396, 2376, 768, 2000, 128, 64, 0, 2, 0, 0, 1 },
801 { 20, 11880, 396, 2376, 2000, 2000, 128, 64, 0, 2, 0, 0, 1 },
802 { 21, 19800, 792, 4752, 4000, 4000, 256, 64, 0, 2, 0, 0, 0 },
803 { 22, 20250, 1620, 8100, 4000, 4000, 256, 64, 0, 2, 0, 0, 0 },
804 { 30, 40500, 1620, 8100, 10000, 10000, 256, 32, 22, 2, 0, 1, 0 },
805 { 31, 108000, 3600, 18000, 14000, 14000, 512, 16, 60, 4, 1, 1, 0 },
806 { 32, 216000, 5120, 20480, 20000, 20000, 512, 16, 60, 4, 1, 1, 0 },
807 { 40, 245760, 8192, 32768, 20000, 25000, 512, 16, 60, 4, 1, 1, 0 },
808 { 41, 245760, 8192, 32768, 50000, 62500, 512, 16, 24, 2, 1, 1, 0 },
809 { 42, 522240, 8704, 34816, 50000, 62500, 512, 16, 24, 2, 1, 1, 1 },
810 { 50, 589824, 22080, 110400, 135000, 135000, 512, 16, 24, 2, 1, 1, 1 },
811 { 51, 983040, 36864, 184320, 240000, 240000, 512, 16, 24, 2, 1, 1, 1 },
812 { 52, 2073600, 36864, 184320, 240000, 240000, 512, 16, 24, 2, 1, 1, 1 },
813 { 0 }
814 };
815
816 #define ERROR(...)\
817 {\
818 if( verbose )\
819 x264_log( h, X264_LOG_WARNING, __VA_ARGS__ );\
820 ret = 1;\
821 }
822
x264_validate_levels(x264_t * h,int verbose)823 int x264_validate_levels( x264_t *h, int verbose )
824 {
825 int ret = 0;
826 int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
827 int dpb = mbs * h->sps->vui.i_max_dec_frame_buffering;
828 int cbp_factor = h->sps->i_profile_idc>=PROFILE_HIGH422 ? 16 :
829 h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
830 h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
831
832 const x264_level_t *l = x264_levels;
833 while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
834 l++;
835
836 if( l->frame_size < mbs
837 || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
838 || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
839 ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
840 h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
841 if( dpb > l->dpb )
842 ERROR( "DPB size (%d frames, %d mbs) > level limit (%d frames, %d mbs)\n",
843 h->sps->vui.i_max_dec_frame_buffering, dpb, l->dpb / mbs, l->dpb );
844
845 #define CHECK( name, limit, val ) \
846 if( (val) > (limit) ) \
847 ERROR( name " (%"PRId64") > level limit (%d)\n", (int64_t)(val), (limit) );
848
849 CHECK( "VBV bitrate", (l->bitrate * cbp_factor) / 4, h->param.rc.i_vbv_max_bitrate );
850 CHECK( "VBV buffer", (l->cpb * cbp_factor) / 4, h->param.rc.i_vbv_buffer_size );
851 CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
852 CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
853 CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
854
855 if( h->param.i_fps_den > 0 )
856 CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
857
858 /* TODO check the rest of the limits */
859 return ret;
860 }
861