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