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