set.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:19k
源码类别:

Audio

开发平台:

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