]> git.sesse.net Git - x264/blob - encoder/set.c
Support infinite keyint (--keyint infinite).
[x264] / encoder / set.c
1 /*****************************************************************************
2  * set: h264 encoder (SPS and PPS init and write)
3  *****************************************************************************
4  * Copyright (C) 2003-2008 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
24 #include <math.h>
25
26 #include "common/common.h"
27 #include "set.h"
28
29 #define bs_write_ue bs_write_ue_big
30
31 // Indexed by pic_struct values
32 static const uint8_t num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
33
34 static void transpose( uint8_t *buf, int w )
35 {
36     for( int i = 0; i < w; i++ )
37         for( int j = 0; j < i; j++ )
38             XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
39 }
40
41 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
42 {
43     const int len = idx<4 ? 16 : 64;
44     const uint8_t *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
45     const uint8_t *list = pps->scaling_list[idx];
46     const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
47                             : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
48                             : x264_cqm_jvt[idx];
49     if( !memcmp( list, def_list, len ) )
50         bs_write( s, 1, 0 ); // scaling_list_present_flag
51     else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
52     {
53         bs_write( s, 1, 1 ); // scaling_list_present_flag
54         bs_write_se( s, -8 ); // use jvt list
55     }
56     else
57     {
58         int run;
59         bs_write( s, 1, 1 ); // scaling_list_present_flag
60
61         // try run-length compression of trailing values
62         for( run = len; run > 1; run-- )
63             if( list[zigzag[run-1]] != list[zigzag[run-2]] )
64                 break;
65         if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) )
66             run = len;
67
68         for( int j = 0; j < run; j++ )
69             bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta
70
71         if( run < len )
72             bs_write_se( s, (int8_t)-list[zigzag[run]] );
73     }
74 }
75
76 static uint8_t *x264_sei_write_header( bs_t *s, int payload_type )
77 {
78     bs_write( s, 8, payload_type );
79
80     bs_flush( s );
81     uint8_t *p_start = s->p;
82     bs_realign( s );
83
84     bs_write( s, 8, 0 );
85     return p_start;
86 }
87
88 static void x264_sei_write( bs_t *s, uint8_t *p_start )
89 {
90     bs_align_10( s );
91     bs_flush( s );
92
93     p_start[0] = s->p - p_start - 1; // -1 for the length byte
94     bs_realign( s );
95
96     bs_rbsp_trailing( s );
97 }
98
99 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
100 {
101     sps->i_id = i_id;
102     int max_frame_num;
103
104     sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
105     if( sps->b_qpprime_y_zero_transform_bypass )
106         sps->i_profile_idc  = PROFILE_HIGH444_PREDICTIVE;
107     else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
108         sps->i_profile_idc  = PROFILE_HIGH;
109     else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
110         sps->i_profile_idc  = PROFILE_MAIN;
111     else
112         sps->i_profile_idc  = PROFILE_BASELINE;
113     sps->i_level_idc = param->i_level_idc;
114
115     sps->b_constraint_set0  = sps->i_profile_idc == PROFILE_BASELINE;
116     /* x264 doesn't support the features that are in Baseline and not in Main,
117      * namely arbitrary_slice_order and slice_groups. */
118     sps->b_constraint_set1  = sps->i_profile_idc <= PROFILE_MAIN;
119     /* Never set constraint_set2, it is not necessary and not used in real world. */
120     sps->b_constraint_set2  = 0;
121
122     sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
123     /* extra slot with pyramid so that we don't have to override the
124      * order of forgetting old pictures */
125     sps->vui.i_max_dec_frame_buffering =
126     sps->i_num_ref_frames = X264_MIN(16, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
127                             param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
128     sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
129
130     /* number of refs + current frame */
131     max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
132     sps->i_log2_max_frame_num = 4;
133     while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
134         sps->i_log2_max_frame_num++;
135
136     sps->i_poc_type = 0;
137     if( sps->i_poc_type == 0 )
138     {
139         int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
140         sps->i_log2_max_poc_lsb = 4;
141         while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
142             sps->i_log2_max_poc_lsb++;
143     }
144     else if( sps->i_poc_type == 1 )
145     {
146         int i;
147
148         /* FIXME */
149         sps->b_delta_pic_order_always_zero = 1;
150         sps->i_offset_for_non_ref_pic = 0;
151         sps->i_offset_for_top_to_bottom_field = 0;
152         sps->i_num_ref_frames_in_poc_cycle = 0;
153
154         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
155         {
156             sps->i_offset_for_ref_frame[i] = 0;
157         }
158     }
159
160     sps->b_vui = 1;
161
162     sps->b_gaps_in_frame_num_value_allowed = 0;
163     sps->i_mb_width = ( param->i_width + 15 ) / 16;
164     sps->i_mb_height= ( param->i_height + 15 ) / 16;
165     sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
166     if( !sps->b_frame_mbs_only )
167         sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
168     sps->b_mb_adaptive_frame_field = param->b_interlaced;
169     sps->b_direct8x8_inference = 1;
170
171     sps->crop.i_left   = 0;
172     sps->crop.i_top    = 0;
173     sps->crop.i_right  = sps->i_mb_width*16 - param->i_width;
174     sps->crop.i_bottom = (sps->i_mb_height*16 - param->i_height) >> !sps->b_frame_mbs_only;
175     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
176                   sps->crop.i_right || sps->crop.i_bottom;
177
178     sps->vui.b_aspect_ratio_info_present = 0;
179     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
180     {
181         sps->vui.b_aspect_ratio_info_present = 1;
182         sps->vui.i_sar_width = param->vui.i_sar_width;
183         sps->vui.i_sar_height= param->vui.i_sar_height;
184     }
185
186     sps->vui.b_overscan_info_present = ( param->vui.i_overscan ? 1 : 0 );
187     if( sps->vui.b_overscan_info_present )
188         sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
189
190     sps->vui.b_signal_type_present = 0;
191     sps->vui.i_vidformat = ( param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
192     sps->vui.b_fullrange = ( param->vui.b_fullrange ? 1 : 0 );
193     sps->vui.b_color_description_present = 0;
194
195     sps->vui.i_colorprim = ( param->vui.i_colorprim <=  9 ? param->vui.i_colorprim : 2 );
196     sps->vui.i_transfer  = ( param->vui.i_transfer  <= 11 ? param->vui.i_transfer  : 2 );
197     sps->vui.i_colmatrix = ( param->vui.i_colmatrix <=  9 ? param->vui.i_colmatrix : 2 );
198     if( sps->vui.i_colorprim != 2 ||
199         sps->vui.i_transfer  != 2 ||
200         sps->vui.i_colmatrix != 2 )
201     {
202         sps->vui.b_color_description_present = 1;
203     }
204
205     if( sps->vui.i_vidformat != 5 ||
206         sps->vui.b_fullrange ||
207         sps->vui.b_color_description_present )
208     {
209         sps->vui.b_signal_type_present = 1;
210     }
211
212     /* FIXME: not sufficient for interlaced video */
213     sps->vui.b_chroma_loc_info_present = ( param->vui.i_chroma_loc ? 1 : 0 );
214     if( sps->vui.b_chroma_loc_info_present )
215     {
216         sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
217         sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
218     }
219
220     sps->vui.b_timing_info_present = param->i_timebase_num > 0 && param->i_timebase_den > 0;
221
222     if( sps->vui.b_timing_info_present )
223     {
224         sps->vui.i_num_units_in_tick = param->i_timebase_num;
225         sps->vui.i_time_scale = param->i_timebase_den * 2;
226         sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
227     }
228
229     sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
230     sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
231     sps->vui.b_pic_struct_present = param->b_pic_struct;
232
233     // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
234
235     sps->vui.b_bitstream_restriction = 1;
236     if( sps->vui.b_bitstream_restriction )
237     {
238         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
239         sps->vui.i_max_bytes_per_pic_denom = 0;
240         sps->vui.i_max_bits_per_mb_denom = 0;
241         sps->vui.i_log2_max_mv_length_horizontal =
242         sps->vui.i_log2_max_mv_length_vertical = (int)log2f( X264_MAX( 1, param->analyse.i_mv_range*4-1 ) ) + 1;
243     }
244 }
245
246 void x264_sps_write( bs_t *s, x264_sps_t *sps )
247 {
248     bs_realign( s );
249     bs_write( s, 8, sps->i_profile_idc );
250     bs_write( s, 1, sps->b_constraint_set0 );
251     bs_write( s, 1, sps->b_constraint_set1 );
252     bs_write( s, 1, sps->b_constraint_set2 );
253
254     bs_write( s, 5, 0 );    /* reserved */
255
256     bs_write( s, 8, sps->i_level_idc );
257
258     bs_write_ue( s, sps->i_id );
259
260     if( sps->i_profile_idc >= PROFILE_HIGH )
261     {
262         bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
263         bs_write_ue( s, 0 ); // bit_depth_luma_minus8
264         bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
265         bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
266         bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
267     }
268
269     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
270     bs_write_ue( s, sps->i_poc_type );
271     if( sps->i_poc_type == 0 )
272     {
273         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
274     }
275     else if( sps->i_poc_type == 1 )
276     {
277         int i;
278
279         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
280         bs_write_se( s, sps->i_offset_for_non_ref_pic );
281         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
282         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
283
284         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
285         {
286             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
287         }
288     }
289     bs_write_ue( s, sps->i_num_ref_frames );
290     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
291     bs_write_ue( s, sps->i_mb_width - 1 );
292     if (sps->b_frame_mbs_only)
293     {
294         bs_write_ue( s, sps->i_mb_height - 1);
295     }
296     else // interlaced
297     {
298         bs_write_ue( s, sps->i_mb_height/2 - 1);
299     }
300     bs_write( s, 1, sps->b_frame_mbs_only );
301     if( !sps->b_frame_mbs_only )
302     {
303         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
304     }
305     bs_write( s, 1, sps->b_direct8x8_inference );
306
307     bs_write( s, 1, sps->b_crop );
308     if( sps->b_crop )
309     {
310         bs_write_ue( s, sps->crop.i_left   / 2 );
311         bs_write_ue( s, sps->crop.i_right  / 2 );
312         bs_write_ue( s, sps->crop.i_top    / 2 );
313         bs_write_ue( s, sps->crop.i_bottom / 2 );
314     }
315
316     bs_write( s, 1, sps->b_vui );
317     if( sps->b_vui )
318     {
319         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
320         if( sps->vui.b_aspect_ratio_info_present )
321         {
322             int i;
323             static const struct { uint8_t w, h, sar; } sar[] =
324             {
325                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
326                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
327                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
328                 { 160,99, 13}, { 0, 0, 255 }
329             };
330             for( i = 0; sar[i].sar != 255; i++ )
331             {
332                 if( sar[i].w == sps->vui.i_sar_width &&
333                     sar[i].h == sps->vui.i_sar_height )
334                     break;
335             }
336             bs_write( s, 8, sar[i].sar );
337             if( sar[i].sar == 255 ) /* aspect_ratio_idc (extended) */
338             {
339                 bs_write( s, 16, sps->vui.i_sar_width );
340                 bs_write( s, 16, sps->vui.i_sar_height );
341             }
342         }
343
344         bs_write1( s, sps->vui.b_overscan_info_present );
345         if( sps->vui.b_overscan_info_present )
346             bs_write1( s, sps->vui.b_overscan_info );
347
348         bs_write1( s, sps->vui.b_signal_type_present );
349         if( sps->vui.b_signal_type_present )
350         {
351             bs_write( s, 3, sps->vui.i_vidformat );
352             bs_write1( s, sps->vui.b_fullrange );
353             bs_write1( s, sps->vui.b_color_description_present );
354             if( sps->vui.b_color_description_present )
355             {
356                 bs_write( s, 8, sps->vui.i_colorprim );
357                 bs_write( s, 8, sps->vui.i_transfer );
358                 bs_write( s, 8, sps->vui.i_colmatrix );
359             }
360         }
361
362         bs_write1( s, sps->vui.b_chroma_loc_info_present );
363         if( sps->vui.b_chroma_loc_info_present )
364         {
365             bs_write_ue( s, sps->vui.i_chroma_loc_top );
366             bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
367         }
368
369         bs_write1( s, sps->vui.b_timing_info_present );
370         if( sps->vui.b_timing_info_present )
371         {
372             bs_write32( s, sps->vui.i_num_units_in_tick );
373             bs_write32( s, sps->vui.i_time_scale );
374             bs_write1( s, sps->vui.b_fixed_frame_rate );
375         }
376
377         bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
378         if( sps->vui.b_nal_hrd_parameters_present )
379         {
380             bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
381             bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
382             bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
383
384             bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
385             bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
386
387             bs_write1( s, sps->vui.hrd.b_cbr_hrd );
388
389             bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
390             bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
391             bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
392             bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
393         }
394
395         bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
396
397         if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
398             bs_write1( s, 0 );   /* low_delay_hrd_flag */
399
400         bs_write1( s, sps->vui.b_pic_struct_present );
401         bs_write1( s, sps->vui.b_bitstream_restriction );
402         if( sps->vui.b_bitstream_restriction )
403         {
404             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
405             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
406             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
407             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
408             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
409             bs_write_ue( s, sps->vui.i_num_reorder_frames );
410             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
411         }
412     }
413
414     bs_rbsp_trailing( s );
415     bs_flush( s );
416 }
417
418 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
419 {
420     pps->i_id = i_id;
421     pps->i_sps_id = sps->i_id;
422     pps->b_cabac = param->b_cabac;
423
424     pps->b_pic_order = param->b_interlaced;
425     pps->i_num_slice_groups = 1;
426
427     pps->i_num_ref_idx_l0_default_active = param->i_frame_reference;
428     pps->i_num_ref_idx_l1_default_active = 1;
429
430     pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
431     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
432
433     pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR ? 26 : param->rc.i_qp_constant;
434     pps->i_pic_init_qs = 26;
435
436     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
437     pps->b_deblocking_filter_control = 1;
438     pps->b_constrained_intra_pred = param->b_constrained_intra;
439     pps->b_redundant_pic_cnt = 0;
440
441     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
442
443     pps->i_cqm_preset = param->i_cqm_preset;
444     switch( pps->i_cqm_preset )
445     {
446     case X264_CQM_FLAT:
447         for( int i = 0; i < 6; i++ )
448             pps->scaling_list[i] = x264_cqm_flat16;
449         break;
450     case X264_CQM_JVT:
451         for( int i = 0; i < 6; i++ )
452             pps->scaling_list[i] = x264_cqm_jvt[i];
453         break;
454     case X264_CQM_CUSTOM:
455         /* match the transposed DCT & zigzag */
456         transpose( param->cqm_4iy, 4 );
457         transpose( param->cqm_4ic, 4 );
458         transpose( param->cqm_4py, 4 );
459         transpose( param->cqm_4pc, 4 );
460         transpose( param->cqm_8iy, 8 );
461         transpose( param->cqm_8py, 8 );
462         pps->scaling_list[CQM_4IY] = param->cqm_4iy;
463         pps->scaling_list[CQM_4IC] = param->cqm_4ic;
464         pps->scaling_list[CQM_4PY] = param->cqm_4py;
465         pps->scaling_list[CQM_4PC] = param->cqm_4pc;
466         pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
467         pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
468         for( int i = 0; i < 6; i++ )
469             for( int j = 0; j < (i < 4 ? 16 : 64); j++ )
470                 if( pps->scaling_list[i][j] == 0 )
471                     pps->scaling_list[i] = x264_cqm_jvt[i];
472         break;
473     }
474 }
475
476 void x264_pps_write( bs_t *s, x264_pps_t *pps )
477 {
478     bs_realign( s );
479     bs_write_ue( s, pps->i_id );
480     bs_write_ue( s, pps->i_sps_id );
481
482     bs_write( s, 1, pps->b_cabac );
483     bs_write( s, 1, pps->b_pic_order );
484     bs_write_ue( s, pps->i_num_slice_groups - 1 );
485
486     bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
487     bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
488     bs_write( s, 1, pps->b_weighted_pred );
489     bs_write( s, 2, pps->b_weighted_bipred );
490
491     bs_write_se( s, pps->i_pic_init_qp - 26 );
492     bs_write_se( s, pps->i_pic_init_qs - 26 );
493     bs_write_se( s, pps->i_chroma_qp_index_offset );
494
495     bs_write( s, 1, pps->b_deblocking_filter_control );
496     bs_write( s, 1, pps->b_constrained_intra_pred );
497     bs_write( s, 1, pps->b_redundant_pic_cnt );
498
499     if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
500     {
501         bs_write( s, 1, pps->b_transform_8x8_mode );
502         bs_write( s, 1, (pps->i_cqm_preset != X264_CQM_FLAT) );
503         if( pps->i_cqm_preset != X264_CQM_FLAT )
504         {
505             scaling_list_write( s, pps, CQM_4IY );
506             scaling_list_write( s, pps, CQM_4IC );
507             bs_write( s, 1, 0 ); // Cr = Cb
508             scaling_list_write( s, pps, CQM_4PY );
509             scaling_list_write( s, pps, CQM_4PC );
510             bs_write( s, 1, 0 ); // Cr = Cb
511             if( pps->b_transform_8x8_mode )
512             {
513                 scaling_list_write( s, pps, CQM_8IY+4 );
514                 scaling_list_write( s, pps, CQM_8PY+4 );
515             }
516         }
517         bs_write_se( s, pps->i_chroma_qp_index_offset );
518     }
519
520     bs_rbsp_trailing( s );
521     bs_flush( s );
522 }
523
524 void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
525 {
526     bs_realign( s );
527     uint8_t *p_start = x264_sei_write_header( s, SEI_RECOVERY_POINT );
528
529     bs_write_ue( s, recovery_frame_cnt ); // recovery_frame_cnt
530     bs_write( s, 1, 1 ); //exact_match_flag 1
531     bs_write( s, 1, 0 ); //broken_link_flag 0
532     bs_write( s, 2, 0 ); //changing_slice_group 0
533
534     x264_sei_write( s, p_start );
535     bs_flush( s );
536 }
537
538 int x264_sei_version_write( x264_t *h, bs_t *s )
539 {
540     int i;
541     // random ID number generated according to ISO-11578
542     static const uint8_t uuid[16] =
543     {
544         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
545         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
546     };
547     char *opts = x264_param2string( &h->param, 0 );
548     char *version;
549     int length;
550
551     if( !opts )
552         return -1;
553     CHECKED_MALLOC( version, 200 + strlen( opts ) );
554
555     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
556              "Copyleft 2003-2010 - http://www.videolan.org/x264.html - options: %s",
557              X264_BUILD, X264_VERSION, opts );
558     length = strlen(version)+1+16;
559
560     bs_realign( s );
561     bs_write( s, 8, SEI_USER_DATA_UNREGISTERED );
562     // payload_size
563     for( i = 0; i <= length-255; i += 255 )
564         bs_write( s, 8, 255 );
565     bs_write( s, 8, length-i );
566
567     for( int j = 0; j < 16; j++ )
568         bs_write( s, 8, uuid[j] );
569     for( int j = 0; j < length-16; j++ )
570         bs_write( s, 8, version[j] );
571
572     bs_rbsp_trailing( s );
573     bs_flush( s );
574
575     x264_free( opts );
576     x264_free( version );
577     return 0;
578 fail:
579     x264_free( opts );
580     return -1;
581 }
582
583 void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
584 {
585     x264_sps_t *sps = h->sps;
586     bs_realign( s );
587     uint8_t *p_start = x264_sei_write_header( s, SEI_BUFFERING_PERIOD );
588
589     bs_write_ue( s, sps->i_id );
590
591     if( sps->vui.b_nal_hrd_parameters_present )
592     {
593         bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
594         bs_write( s, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
595     }
596
597     x264_sei_write( s, p_start );
598     bs_flush( s );
599 }
600
601 void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
602 {
603     x264_sps_t *sps = h->sps;
604     bs_realign( s );
605     uint8_t *p_start = x264_sei_write_header( s, SEI_PIC_TIMING );
606
607     if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
608     {
609         bs_write( s, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay );
610         bs_write( s, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
611     }
612
613     if( sps->vui.b_pic_struct_present )
614     {
615         bs_write( s, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
616
617         // These clock timestamps are not standardised so we don't set them
618         // They could be time of origin, capture or alternative ideal display
619         for( int i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
620             bs_write1( s, 0 ); // clock_timestamp_flag
621     }
622
623     x264_sei_write( s, p_start );
624     bs_flush( s );
625 }
626
627 void x264_filler_write( x264_t *h, bs_t *s, int filler )
628 {
629     bs_realign( s );
630
631     for( int i = 0; i < filler; i++ )
632         bs_write( s, 8, 0xff );
633
634     bs_rbsp_trailing( s );
635     bs_flush( s );
636 }
637
638 const x264_level_t x264_levels[] =
639 {
640     { 10,   1485,    99,   152064,     64,    175,  64, 64,  0, 2, 0, 0, 1 },
641 //  {"1b",  1485,    99,   152064,    128,    350,  64, 64,  0, 2, 0, 0, 1 },
642     { 11,   3000,   396,   345600,    192,    500, 128, 64,  0, 2, 0, 0, 1 },
643     { 12,   6000,   396,   912384,    384,   1000, 128, 64,  0, 2, 0, 0, 1 },
644     { 13,  11880,   396,   912384,    768,   2000, 128, 64,  0, 2, 0, 0, 1 },
645     { 20,  11880,   396,   912384,   2000,   2000, 128, 64,  0, 2, 0, 0, 1 },
646     { 21,  19800,   792,  1824768,   4000,   4000, 256, 64,  0, 2, 0, 0, 0 },
647     { 22,  20250,  1620,  3110400,   4000,   4000, 256, 64,  0, 2, 0, 0, 0 },
648     { 30,  40500,  1620,  3110400,  10000,  10000, 256, 32, 22, 2, 0, 1, 0 },
649     { 31, 108000,  3600,  6912000,  14000,  14000, 512, 16, 60, 4, 1, 1, 0 },
650     { 32, 216000,  5120,  7864320,  20000,  20000, 512, 16, 60, 4, 1, 1, 0 },
651     { 40, 245760,  8192, 12582912,  20000,  25000, 512, 16, 60, 4, 1, 1, 0 },
652     { 41, 245760,  8192, 12582912,  50000,  62500, 512, 16, 24, 2, 1, 1, 0 },
653     { 42, 522240,  8704, 13369344,  50000,  62500, 512, 16, 24, 2, 1, 1, 1 },
654     { 50, 589824, 22080, 42393600, 135000, 135000, 512, 16, 24, 2, 1, 1, 1 },
655     { 51, 983040, 36864, 70778880, 240000, 240000, 512, 16, 24, 2, 1, 1, 1 },
656     { 0 }
657 };
658
659 #define ERROR(...)\
660 {\
661     if( verbose )\
662         x264_log( h, X264_LOG_WARNING, __VA_ARGS__ );\
663     ret = 1;\
664 }
665
666 int x264_validate_levels( x264_t *h, int verbose )
667 {
668     int ret = 0;
669     int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
670     int dpb = mbs * 384 * h->sps->vui.i_max_dec_frame_buffering;
671     int cbp_factor = h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
672
673     const x264_level_t *l = x264_levels;
674     while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
675         l++;
676
677     if( l->frame_size < mbs
678         || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
679         || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
680         ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
681                h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
682     if( dpb > l->dpb )
683         ERROR( "DPB size (%d frames, %d bytes) > level limit (%d frames, %d bytes)\n",
684                 h->sps->vui.i_max_dec_frame_buffering, dpb, (int)(l->dpb / (384*mbs)), l->dpb );
685
686 #define CHECK( name, limit, val ) \
687     if( (val) > (limit) ) \
688         ERROR( name " (%d) > level limit (%d)\n", (int)(val), (limit) );
689
690     CHECK( "VBV bitrate", (l->bitrate * cbp_factor) / 4, h->param.rc.i_vbv_max_bitrate );
691     CHECK( "VBV buffer", (l->cpb * cbp_factor) / 4, h->param.rc.i_vbv_buffer_size );
692     CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
693     CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
694     CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
695
696     if( h->param.i_fps_den > 0 )
697         CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
698
699     /* TODO check the rest of the limits */
700     return ret;
701 }