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