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