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

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * common.c: h264 library
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2008 x264 project
  5.  *
  6.  * Authors: Loren Merritt <lorenm@u.washington.edu>
  7.  *          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., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <stdarg.h>
  24. #include <ctype.h>
  25. #ifdef HAVE_MALLOC_H
  26. #include <malloc.h>
  27. #endif
  28. #include "common.h"
  29. #include "cpu.h"
  30. static void x264_log_default( void *, int, const char *, va_list );
  31. /****************************************************************************
  32.  * x264_param_default:
  33.  ****************************************************************************/
  34. void    x264_param_default( x264_param_t *param )
  35. {
  36.     /* */
  37.     memset( param, 0, sizeof( x264_param_t ) );
  38.     /* CPU autodetect */
  39.     param->cpu = x264_cpu_detect();
  40.     param->i_threads = 1;
  41.     param->b_deterministic = 1;
  42.     /* Video properties */
  43.     param->i_csp           = X264_CSP_I420;
  44.     param->i_width         = 0;
  45.     param->i_height        = 0;
  46.     param->vui.i_sar_width = 0;
  47.     param->vui.i_sar_height= 0;
  48.     param->vui.i_overscan  = 0;  /* undef */
  49.     param->vui.i_vidformat = 5;  /* undef */
  50.     param->vui.b_fullrange = 0;  /* off */
  51.     param->vui.i_colorprim = 2;  /* undef */
  52.     param->vui.i_transfer  = 2;  /* undef */
  53.     param->vui.i_colmatrix = 2;  /* undef */
  54.     param->vui.i_chroma_loc= 0;  /* left center */
  55.     param->i_fps_num       = 25;
  56.     param->i_fps_den       = 1;
  57.     param->i_level_idc     = -1;
  58.     /* Encoder parameters */
  59.     param->i_frame_reference = 1;
  60.     param->i_keyint_max = 250;
  61.     param->i_keyint_min = 25;
  62.     param->i_bframe = 0;
  63.     param->i_scenecut_threshold = 40;
  64.     param->b_bframe_adaptive = 1;
  65.     param->i_bframe_bias = 0;
  66.     param->b_bframe_pyramid = 0;
  67.     param->b_deblocking_filter = 1;
  68.     param->i_deblocking_filter_alphac0 = 0;
  69.     param->i_deblocking_filter_beta = 0;
  70.     param->b_cabac = 1;
  71.     param->i_cabac_init_idc = 0;
  72.     param->rc.i_rc_method = X264_RC_NONE;
  73.     param->rc.i_bitrate = 0;
  74.     param->rc.f_rate_tolerance = 1.0;
  75.     param->rc.i_vbv_max_bitrate = 0;
  76.     param->rc.i_vbv_buffer_size = 0;
  77.     param->rc.f_vbv_buffer_init = 0.9;
  78.     param->rc.i_qp_constant = 26;
  79.     param->rc.f_rf_constant = 0;
  80.     param->rc.i_qp_min = 10;
  81.     param->rc.i_qp_max = 51;
  82.     param->rc.i_qp_step = 4;
  83.     param->rc.f_ip_factor = 1.4;
  84.     param->rc.f_pb_factor = 1.3;
  85.     param->rc.i_aq_mode = X264_AQ_GLOBAL;
  86.     param->rc.f_aq_strength = 1.0;
  87.     param->rc.b_stat_write = 0;
  88.     param->rc.psz_stat_out = "x264_2pass.log";
  89.     param->rc.b_stat_read = 0;
  90.     param->rc.psz_stat_in = "x264_2pass.log";
  91.     param->rc.f_qcompress = 0.6;
  92.     param->rc.f_qblur = 0.5;
  93.     param->rc.f_complexity_blur = 20;
  94.     param->rc.i_zones = 0;
  95.     /* Log */
  96.     param->pf_log = x264_log_default;
  97.     param->p_log_private = NULL;
  98.     param->i_log_level = X264_LOG_INFO;
  99.     /* */
  100.     param->analyse.intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
  101.     param->analyse.inter = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8
  102.                          | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
  103.     param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
  104.     param->analyse.i_me_method = X264_ME_HEX;
  105.     param->analyse.i_me_range = 16;
  106.     param->analyse.i_subpel_refine = 5;
  107.     param->analyse.b_chroma_me = 1;
  108.     param->analyse.i_mv_range_thread = -1;
  109.     param->analyse.i_mv_range = -1; // set from level_idc
  110.     param->analyse.i_direct_8x8_inference = 1;
  111.     param->analyse.i_chroma_qp_offset = 0;
  112.     param->analyse.b_fast_pskip = 1;
  113.     param->analyse.b_dct_decimate = 1;
  114.     param->analyse.i_luma_deadzone[0] = 21;
  115.     param->analyse.i_luma_deadzone[1] = 11;
  116.     param->analyse.b_psnr = 1;
  117.     param->analyse.b_ssim = 1;
  118.     param->i_cqm_preset = X264_CQM_FLAT;
  119.     memset( param->cqm_4iy, 16, 16 );
  120.     memset( param->cqm_4ic, 16, 16 );
  121.     memset( param->cqm_4py, 16, 16 );
  122.     memset( param->cqm_4pc, 16, 16 );
  123.     memset( param->cqm_8iy, 16, 64 );
  124.     memset( param->cqm_8py, 16, 64 );
  125.     param->b_repeat_headers = 1;
  126.     param->b_aud = 0;
  127. }
  128. static int parse_enum( const char *arg, const char * const *names, int *dst )
  129. {
  130.     int i;
  131.     for( i = 0; names[i]; i++ )
  132.         if( !strcmp( arg, names[i] ) )
  133.         {
  134.             *dst = i;
  135.             return 0;
  136.         }
  137.     return -1;
  138. }
  139. static int parse_cqm( const char *str, uint8_t *cqm, int length )
  140. {
  141.     int i = 0;
  142.     do {
  143.         int coef;
  144.         if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
  145.             return -1;
  146.         cqm[i++] = coef;
  147.     } while( i < length && (str = strchr( str, ',' )) && str++ );
  148.     return (i == length) ? 0 : -1;
  149. }
  150. static int x264_atobool( const char *str, int *b_error )
  151. {
  152.     if( !strcmp(str, "1") ||
  153.         !strcmp(str, "true") ||
  154.         !strcmp(str, "yes") )
  155.         return 1;
  156.     if( !strcmp(str, "0") ||
  157.         !strcmp(str, "false") ||
  158.         !strcmp(str, "no") )
  159.         return 0;
  160.     *b_error = 1;
  161.     return 0;
  162. }
  163. static int x264_atoi( const char *str, int *b_error )
  164. {
  165.     char *end;
  166.     int v = strtol( str, &end, 0 );
  167.     if( end == str || *end != '' )
  168.         *b_error = 1;
  169.     return v;
  170. }
  171. static double x264_atof( const char *str, int *b_error )
  172. {
  173.     char *end;
  174.     double v = strtod( str, &end );
  175.     if( end == str || *end != '' )
  176.         *b_error = 1;
  177.     return v;
  178. }
  179. #define atobool(str) ( name_was_bool = 1, x264_atobool( str, &b_error ) )
  180. #define atoi(str) x264_atoi( str, &b_error )
  181. #define atof(str) x264_atof( str, &b_error )
  182. int x264_param_parse( x264_param_t *p, const char *name, const char *value )
  183. {
  184.     char *name_buf = NULL;
  185.     int b_error = 0;
  186.     int name_was_bool;
  187.     int value_was_null = !value;
  188.     int i;
  189.     if( !name )
  190.         return X264_PARAM_BAD_NAME;
  191.     if( !value )
  192.         value = "true";
  193.     if( value[0] == '=' )
  194.         value++;
  195.     if( strchr( name, '_' ) ) // s/_/-/g
  196.     {
  197.         char *p;
  198.         name_buf = strdup(name);
  199.         while( (p = strchr( name_buf, '_' )) )
  200.             *p = '-';
  201.         name = name_buf;
  202.     }
  203.     if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
  204.         (!strncmp( name, "no", 2 ) && (i = 2)) )
  205.     {
  206.         name += i;
  207.         value = atobool(value) ? "false" : "true";
  208.     }
  209.     name_was_bool = 0;
  210. #define OPT(STR) else if( !strcmp( name, STR ) )
  211. #define OPT2(STR0, STR1) else if( !strcmp( name, STR0 ) || !strcmp( name, STR1 ) )
  212.     if(0);
  213.     OPT("asm")
  214.     {
  215.         p->cpu = isdigit(value[0]) ? atoi(value) :
  216.                  !strcmp(value, "auto") || atobool(value) ? x264_cpu_detect() : 0;
  217.         if( b_error )
  218.         {
  219.             char *buf = strdup(value);
  220.             char *tok, *saveptr, *init;
  221.             b_error = 0;
  222.             p->cpu = 0;
  223.             for( init=buf; (tok=strtok_r(init, ",", &saveptr)); init=NULL )
  224.             {
  225.                 for( i=0; x264_cpu_names[i].flags && strcasecmp(tok, x264_cpu_names[i].name); i++ );
  226.                 p->cpu |= x264_cpu_names[i].flags;
  227.                 if( !x264_cpu_names[i].flags )
  228.                     b_error = 1;
  229.             }
  230.             free( buf );
  231.         }
  232.     }
  233.     OPT("threads")
  234.     {
  235.         if( !strcmp(value, "auto") )
  236.             p->i_threads = 0;
  237.         else
  238.             p->i_threads = atoi(value);
  239.     }
  240.     OPT2("deterministic", "n-deterministic")
  241.         p->b_deterministic = atobool(value);
  242.     OPT2("level", "level-idc")
  243.     {
  244.         if( atof(value) < 6 )
  245.             p->i_level_idc = (int)(10*atof(value)+.5);
  246.         else
  247.             p->i_level_idc = atoi(value);
  248.     }
  249.     OPT("sar")
  250.     {
  251.         b_error = ( 2 != sscanf( value, "%d:%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) &&
  252.                     2 != sscanf( value, "%d/%d", &p->vui.i_sar_width, &p->vui.i_sar_height ) );
  253.     }
  254.     OPT("overscan")
  255.         b_error |= parse_enum( value, x264_overscan_names, &p->vui.i_overscan );
  256.     OPT("videoformat")
  257.         b_error |= parse_enum( value, x264_vidformat_names, &p->vui.i_vidformat );
  258.     OPT("fullrange")
  259.         b_error |= parse_enum( value, x264_fullrange_names, &p->vui.b_fullrange );
  260.     OPT("colorprim")
  261.         b_error |= parse_enum( value, x264_colorprim_names, &p->vui.i_colorprim );
  262.     OPT("transfer")
  263.         b_error |= parse_enum( value, x264_transfer_names, &p->vui.i_transfer );
  264.     OPT("colormatrix")
  265.         b_error |= parse_enum( value, x264_colmatrix_names, &p->vui.i_colmatrix );
  266.     OPT("chromaloc")
  267.     {
  268.         p->vui.i_chroma_loc = atoi(value);
  269.         b_error = ( p->vui.i_chroma_loc < 0 || p->vui.i_chroma_loc > 5 );
  270.     }
  271.     OPT("fps")
  272.     {
  273.         if( sscanf( value, "%d/%d", &p->i_fps_num, &p->i_fps_den ) == 2 )
  274.             ;
  275.         else
  276.         {
  277.             float fps = atof(value);
  278.             p->i_fps_num = (int)(fps * 1000 + .5);
  279.             p->i_fps_den = 1000;
  280.         }
  281.     }
  282.     OPT2("ref", "frameref")
  283.         p->i_frame_reference = atoi(value);
  284.     OPT("keyint")
  285.     {
  286.         p->i_keyint_max = atoi(value);
  287.         if( p->i_keyint_min > p->i_keyint_max )
  288.             p->i_keyint_min = p->i_keyint_max;
  289.     }
  290.     OPT2("min-keyint", "keyint-min")
  291.     {
  292.         p->i_keyint_min = atoi(value);
  293.         if( p->i_keyint_max < p->i_keyint_min )
  294.             p->i_keyint_max = p->i_keyint_min;
  295.     }
  296.     OPT("scenecut")
  297.         p->i_scenecut_threshold = atoi(value);
  298.     OPT("pre-scenecut")
  299.         p->b_pre_scenecut = atobool(value);
  300.     OPT("bframes")
  301.         p->i_bframe = atoi(value);
  302.     OPT("b-adapt")
  303.         p->b_bframe_adaptive = atobool(value);
  304.     OPT("b-bias")
  305.         p->i_bframe_bias = atoi(value);
  306.     OPT("b-pyramid")
  307.         p->b_bframe_pyramid = atobool(value);
  308.     OPT("nf")
  309.         p->b_deblocking_filter = !atobool(value);
  310.     OPT2("filter", "deblock")
  311.     {
  312.         if( 2 == sscanf( value, "%d:%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) ||
  313.             2 == sscanf( value, "%d,%d", &p->i_deblocking_filter_alphac0, &p->i_deblocking_filter_beta ) )
  314.         {
  315.             p->b_deblocking_filter = 1;
  316.         }
  317.         else if( sscanf( value, "%d", &p->i_deblocking_filter_alphac0 ) )
  318.         {
  319.             p->b_deblocking_filter = 1;
  320.             p->i_deblocking_filter_beta = p->i_deblocking_filter_alphac0;
  321.         }
  322.         else
  323.             p->b_deblocking_filter = atobool(value);
  324.     }
  325.     OPT("cabac")
  326.         p->b_cabac = atobool(value);
  327.     OPT("cabac-idc")
  328.         p->i_cabac_init_idc = atoi(value);
  329.     OPT("interlaced")
  330.         p->b_interlaced = atobool(value);
  331.     OPT("cqm")
  332.     {
  333.         if( strstr( value, "flat" ) )
  334.             p->i_cqm_preset = X264_CQM_FLAT;
  335.         else if( strstr( value, "jvt" ) )
  336.             p->i_cqm_preset = X264_CQM_JVT;
  337.         else
  338.             p->psz_cqm_file = strdup(value);
  339.     }
  340.     OPT("cqmfile")
  341.         p->psz_cqm_file = strdup(value);
  342.     OPT("cqm4")
  343.     {
  344.         p->i_cqm_preset = X264_CQM_CUSTOM;
  345.         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  346.         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  347.         b_error |= parse_cqm( value, p->cqm_4py, 16 );
  348.         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  349.     }
  350.     OPT("cqm8")
  351.     {
  352.         p->i_cqm_preset = X264_CQM_CUSTOM;
  353.         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
  354.         b_error |= parse_cqm( value, p->cqm_8py, 64 );
  355.     }
  356.     OPT("cqm4i")
  357.     {
  358.         p->i_cqm_preset = X264_CQM_CUSTOM;
  359.         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  360.         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  361.     }
  362.     OPT("cqm4p")
  363.     {
  364.         p->i_cqm_preset = X264_CQM_CUSTOM;
  365.         b_error |= parse_cqm( value, p->cqm_4py, 16 );
  366.         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  367.     }
  368.     OPT("cqm4iy")
  369.     {
  370.         p->i_cqm_preset = X264_CQM_CUSTOM;
  371.         b_error |= parse_cqm( value, p->cqm_4iy, 16 );
  372.     }
  373.     OPT("cqm4ic")
  374.     {
  375.         p->i_cqm_preset = X264_CQM_CUSTOM;
  376.         b_error |= parse_cqm( value, p->cqm_4ic, 16 );
  377.     }
  378.     OPT("cqm4py")
  379.     {
  380.         p->i_cqm_preset = X264_CQM_CUSTOM;
  381.         b_error |= parse_cqm( value, p->cqm_4py, 16 );
  382.     }
  383.     OPT("cqm4pc")
  384.     {
  385.         p->i_cqm_preset = X264_CQM_CUSTOM;
  386.         b_error |= parse_cqm( value, p->cqm_4pc, 16 );
  387.     }
  388.     OPT("cqm8i")
  389.     {
  390.         p->i_cqm_preset = X264_CQM_CUSTOM;
  391.         b_error |= parse_cqm( value, p->cqm_8iy, 64 );
  392.     }
  393.     OPT("cqm8p")
  394.     {
  395.         p->i_cqm_preset = X264_CQM_CUSTOM;
  396.         b_error |= parse_cqm( value, p->cqm_8py, 64 );
  397.     }
  398.     OPT("log")
  399.         p->i_log_level = atoi(value);
  400. #ifdef VISUALIZE
  401.     OPT("visualize")
  402.         p->b_visualize = atobool(value);
  403. #endif
  404.     OPT("dump-yuv")
  405.         p->psz_dump_yuv = strdup(value);
  406.     OPT2("analyse", "partitions")
  407.     {
  408.         p->analyse.inter = 0;
  409.         if( strstr( value, "none" ) )  p->analyse.inter =  0;
  410.         if( strstr( value, "all" ) )   p->analyse.inter = ~0;
  411.         if( strstr( value, "i4x4" ) )  p->analyse.inter |= X264_ANALYSE_I4x4;
  412.         if( strstr( value, "i8x8" ) )  p->analyse.inter |= X264_ANALYSE_I8x8;
  413.         if( strstr( value, "p8x8" ) )  p->analyse.inter |= X264_ANALYSE_PSUB16x16;
  414.         if( strstr( value, "p4x4" ) )  p->analyse.inter |= X264_ANALYSE_PSUB8x8;
  415.         if( strstr( value, "b8x8" ) )  p->analyse.inter |= X264_ANALYSE_BSUB16x16;
  416.     }
  417.     OPT("8x8dct")
  418.         p->analyse.b_transform_8x8 = atobool(value);
  419.     OPT2("weightb", "weight-b")
  420.         p->analyse.b_weighted_bipred = atobool(value);
  421.     OPT2("direct", "direct-pred")
  422.         b_error |= parse_enum( value, x264_direct_pred_names, &p->analyse.i_direct_mv_pred );
  423.     OPT("direct-8x8")
  424.         p->analyse.i_direct_8x8_inference = atoi(value);
  425.     OPT("chroma-qp-offset")
  426.         p->analyse.i_chroma_qp_offset = atoi(value);
  427.     OPT("me")
  428.         b_error |= parse_enum( value, x264_motion_est_names, &p->analyse.i_me_method );
  429.     OPT2("merange", "me-range")
  430.         p->analyse.i_me_range = atoi(value);
  431.     OPT2("mvrange", "mv-range")
  432.         p->analyse.i_mv_range = atoi(value);
  433.     OPT2("mvrange-thread", "mv-range-thread")
  434.         p->analyse.i_mv_range_thread = atoi(value);
  435.     OPT2("subme", "subq")
  436.         p->analyse.i_subpel_refine = atoi(value);
  437.     OPT("bime")
  438.         p->analyse.b_bidir_me = atobool(value);
  439.     OPT("chroma-me")
  440.         p->analyse.b_chroma_me = atobool(value);
  441.     OPT2("b-rdo", "brdo")
  442.         p->analyse.b_bframe_rdo = atobool(value);
  443.     OPT("mixed-refs")
  444.         p->analyse.b_mixed_references = atobool(value);
  445.     OPT("trellis")
  446.         p->analyse.i_trellis = atoi(value);
  447.     OPT("fast-pskip")
  448.         p->analyse.b_fast_pskip = atobool(value);
  449.     OPT("dct-decimate")
  450.         p->analyse.b_dct_decimate = atobool(value);
  451.     OPT("deadzone-inter")
  452.         p->analyse.i_luma_deadzone[0] = atoi(value);
  453.     OPT("deadzone-intra")
  454.         p->analyse.i_luma_deadzone[1] = atoi(value);
  455.     OPT("nr")
  456.         p->analyse.i_noise_reduction = atoi(value);
  457.     OPT("bitrate")
  458.     {
  459.         p->rc.i_bitrate = atoi(value);
  460.         p->rc.i_rc_method = X264_RC_ABR;
  461.     }
  462.     OPT2("qp", "qp_constant")
  463.     {
  464.         p->rc.i_qp_constant = atoi(value);
  465.         p->rc.i_rc_method = X264_RC_CQP;
  466.     }
  467.     OPT("crf")
  468.     {
  469.         p->rc.f_rf_constant = atof(value);
  470.         p->rc.i_rc_method = X264_RC_CRF;
  471.     }
  472.     OPT2("qpmin", "qp-min")
  473.         p->rc.i_qp_min = atoi(value);
  474.     OPT2("qpmax", "qp-max")
  475.         p->rc.i_qp_max = atoi(value);
  476.     OPT2("qpstep", "qp-step")
  477.         p->rc.i_qp_step = atoi(value);
  478.     OPT("ratetol")
  479.         p->rc.f_rate_tolerance = !strncmp("inf", value, 3) ? 1e9 : atof(value);
  480.     OPT("vbv-maxrate")
  481.         p->rc.i_vbv_max_bitrate = atoi(value);
  482.     OPT("vbv-bufsize")
  483.         p->rc.i_vbv_buffer_size = atoi(value);
  484.     OPT("vbv-init")
  485.         p->rc.f_vbv_buffer_init = atof(value);
  486.     OPT2("ipratio", "ip-factor")
  487.         p->rc.f_ip_factor = atof(value);
  488.     OPT2("pbratio", "pb-factor")
  489.         p->rc.f_pb_factor = atof(value);
  490.     OPT("aq-mode")
  491.         p->rc.i_aq_mode = atoi(value);
  492.     OPT("aq-strength")
  493.         p->rc.f_aq_strength = atof(value);
  494.     OPT("pass")
  495.     {
  496.         int i = x264_clip3( atoi(value), 0, 3 );
  497.         p->rc.b_stat_write = i & 1;
  498.         p->rc.b_stat_read = i & 2;
  499.     }
  500.     OPT("stats")
  501.     {
  502.         p->rc.psz_stat_in = strdup(value);
  503.         p->rc.psz_stat_out = strdup(value);
  504.     }
  505.     OPT("qcomp")
  506.         p->rc.f_qcompress = atof(value);
  507.     OPT("qblur")
  508.         p->rc.f_qblur = atof(value);
  509.     OPT2("cplxblur", "cplx-blur")
  510.         p->rc.f_complexity_blur = atof(value);
  511.     OPT("zones")
  512.         p->rc.psz_zones = strdup(value);
  513.     OPT("psnr")
  514.         p->analyse.b_psnr = atobool(value);
  515.     OPT("ssim")
  516.         p->analyse.b_ssim = atobool(value);
  517.     OPT("aud")
  518.         p->b_aud = atobool(value);
  519.     OPT("sps-id")
  520.         p->i_sps_id = atoi(value);
  521.     OPT("global-header")
  522.         p->b_repeat_headers = !atobool(value);
  523.     OPT("repeat-headers")
  524.         p->b_repeat_headers = atobool(value);
  525.     else
  526.         return X264_PARAM_BAD_NAME;
  527. #undef OPT
  528. #undef OPT2
  529. #undef atobool
  530. #undef atoi
  531. #undef atof
  532.     if( name_buf )
  533.         free( name_buf );
  534.     b_error |= value_was_null && !name_was_bool;
  535.     return b_error ? X264_PARAM_BAD_VALUE : 0;
  536. }
  537. /****************************************************************************
  538.  * x264_log:
  539.  ****************************************************************************/
  540. void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
  541. {
  542.     if( i_level <= h->param.i_log_level )
  543.     {
  544.         va_list arg;
  545.         va_start( arg, psz_fmt );
  546.         h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
  547.         va_end( arg );
  548.     }
  549. }
  550. static void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
  551. {
  552.     char *psz_prefix;
  553.     switch( i_level )
  554.     {
  555.         case X264_LOG_ERROR:
  556.             psz_prefix = "error";
  557.             break;
  558.         case X264_LOG_WARNING:
  559.             psz_prefix = "warning";
  560.             break;
  561.         case X264_LOG_INFO:
  562.             psz_prefix = "info";
  563.             break;
  564.         case X264_LOG_DEBUG:
  565.             psz_prefix = "debug";
  566.             break;
  567.         default:
  568.             psz_prefix = "unknown";
  569.             break;
  570.     }
  571.     fprintf( stderr, "x264 [%s]: ", psz_prefix );
  572.     vfprintf( stderr, psz_fmt, arg );
  573. }
  574. /****************************************************************************
  575.  * x264_picture_alloc:
  576.  ****************************************************************************/
  577. void x264_picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
  578. {
  579.     pic->i_type = X264_TYPE_AUTO;
  580.     pic->i_qpplus1 = 0;
  581.     pic->img.i_csp = i_csp;
  582.     pic->img.i_plane = 3;
  583.     pic->img.plane[0] = x264_malloc( 3 * i_width * i_height / 2 );
  584.     pic->img.plane[1] = pic->img.plane[0] + i_width * i_height;
  585.     pic->img.plane[2] = pic->img.plane[1] + i_width * i_height / 4;
  586.     pic->img.i_stride[0] = i_width;
  587.     pic->img.i_stride[1] = i_width / 2;
  588.     pic->img.i_stride[2] = i_width / 2;
  589. }
  590. /****************************************************************************
  591.  * x264_picture_clean:
  592.  ****************************************************************************/
  593. void x264_picture_clean( x264_picture_t *pic )
  594. {
  595.     x264_free( pic->img.plane[0] );
  596.     /* just to be safe */
  597.     memset( pic, 0, sizeof( x264_picture_t ) );
  598. }
  599. /****************************************************************************
  600.  * x264_nal_encode:
  601.  ****************************************************************************/
  602. int x264_nal_encode( void *p_data, int *pi_data, int b_annexeb, x264_nal_t *nal )
  603. {
  604.     uint8_t *dst = p_data;
  605.     uint8_t *src = nal->p_payload;
  606.     uint8_t *end = &nal->p_payload[nal->i_payload];
  607.     int i_count = 0;
  608.     /* FIXME this code doesn't check overflow */
  609.     if( b_annexeb )
  610.     {
  611.         /* long nal start code (we always use long ones)*/
  612.         *dst++ = 0x00;
  613.         *dst++ = 0x00;
  614.         *dst++ = 0x00;
  615.         *dst++ = 0x01;
  616.     }
  617.     /* nal header */
  618.     *dst++ = ( 0x00 << 7 ) | ( nal->i_ref_idc << 5 ) | nal->i_type;
  619.     while( src < end )
  620.     {
  621.         if( i_count == 2 && *src <= 0x03 )
  622.         {
  623.             *dst++ = 0x03;
  624.             i_count = 0;
  625.         }
  626.         if( *src == 0 )
  627.             i_count++;
  628.         else
  629.             i_count = 0;
  630.         *dst++ = *src++;
  631.     }
  632.     *pi_data = dst - (uint8_t*)p_data;
  633.     return *pi_data;
  634. }
  635. /****************************************************************************
  636.  * x264_malloc:
  637.  ****************************************************************************/
  638. void *x264_malloc( int i_size )
  639. {
  640. #ifdef SYS_MACOSX
  641.     /* Mac OS X always returns 16 bytes aligned memory */
  642.     return malloc( i_size );
  643. #elif defined( HAVE_MALLOC_H )
  644.     return memalign( 16, i_size );
  645. #else
  646.     uint8_t * buf;
  647.     uint8_t * align_buf;
  648.     buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
  649.               sizeof( int ) );
  650.     align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
  651.     align_buf -= (long) align_buf & 15;
  652.     *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
  653.     *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
  654.     return align_buf;
  655. #endif
  656. }
  657. /****************************************************************************
  658.  * x264_free:
  659.  ****************************************************************************/
  660. void x264_free( void *p )
  661. {
  662.     if( p )
  663.     {
  664. #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
  665.         free( p );
  666. #else
  667.         free( *( ( ( void **) p ) - 1 ) );
  668. #endif
  669.     }
  670. }
  671. /****************************************************************************
  672.  * x264_realloc:
  673.  ****************************************************************************/
  674. void *x264_realloc( void *p, int i_size )
  675. {
  676. #ifdef HAVE_MALLOC_H
  677.     return realloc( p, i_size );
  678. #else
  679.     int       i_old_size = 0;
  680.     uint8_t * p_new;
  681.     if( p )
  682.     {
  683.         i_old_size = *( (int*) ( (uint8_t*) p - sizeof( void ** ) -
  684.                          sizeof( int ) ) );
  685.     }
  686.     p_new = x264_malloc( i_size );
  687.     if( i_old_size > 0 && i_size > 0 )
  688.     {
  689.         memcpy( p_new, p, ( i_old_size < i_size ) ? i_old_size : i_size );
  690.     }
  691.     x264_free( p );
  692.     return p_new;
  693. #endif
  694. }
  695. /****************************************************************************
  696.  * x264_reduce_fraction:
  697.  ****************************************************************************/
  698. void x264_reduce_fraction( int *n, int *d )
  699. {
  700.     int a = *n;
  701.     int b = *d;
  702.     int c;
  703.     if( !a || !b )
  704.         return;
  705.     c = a % b;
  706.     while(c)
  707.     {
  708. a = b;
  709. b = c;
  710. c = a % b;
  711.     }
  712.     *n /= b;
  713.     *d /= b;
  714. }
  715. /****************************************************************************
  716.  * x264_slurp_file:
  717.  ****************************************************************************/
  718. char *x264_slurp_file( const char *filename )
  719. {
  720.     int b_error = 0;
  721.     int i_size;
  722.     char *buf;
  723.     FILE *fh = fopen( filename, "rb" );
  724.     if( !fh )
  725.         return NULL;
  726.     b_error |= fseek( fh, 0, SEEK_END ) < 0;
  727.     b_error |= ( i_size = ftell( fh ) ) <= 0;
  728.     b_error |= fseek( fh, 0, SEEK_SET ) < 0;
  729.     if( b_error )
  730.         return NULL;
  731.     buf = x264_malloc( i_size+2 );
  732.     if( buf == NULL )
  733.         return NULL;
  734.     b_error |= fread( buf, 1, i_size, fh ) != i_size;
  735.     if( buf[i_size-1] != 'n' )
  736.         buf[i_size++] = 'n';
  737.     buf[i_size] = 0;
  738.     fclose( fh );
  739.     if( b_error )
  740.     {
  741.         x264_free( buf );
  742.         return NULL;
  743.     }
  744.     return buf;
  745. }
  746. /****************************************************************************
  747.  * x264_param2string:
  748.  ****************************************************************************/
  749. char *x264_param2string( x264_param_t *p, int b_res )
  750. {
  751.     int len = 1000;
  752.     char *buf, *s;
  753.     if( p->rc.psz_zones )
  754.         len += strlen(p->rc.psz_zones);
  755.     buf = s = x264_malloc( len );
  756.     if( b_res )
  757.     {
  758.         s += sprintf( s, "%dx%d ", p->i_width, p->i_height );
  759.         s += sprintf( s, "fps=%d/%d ", p->i_fps_num, p->i_fps_den );
  760.     }
  761.     s += sprintf( s, "cabac=%d", p->b_cabac );
  762.     s += sprintf( s, " ref=%d", p->i_frame_reference );
  763.     s += sprintf( s, " deblock=%d:%d:%d", p->b_deblocking_filter,
  764.                   p->i_deblocking_filter_alphac0, p->i_deblocking_filter_beta );
  765.     s += sprintf( s, " analyse=%#x:%#x", p->analyse.intra, p->analyse.inter );
  766.     s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
  767.     s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine );
  768.     s += sprintf( s, " brdo=%d", p->analyse.b_bframe_rdo );
  769.     s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references );
  770.     s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
  771.     s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me );
  772.     s += sprintf( s, " trellis=%d", p->analyse.i_trellis );
  773.     s += sprintf( s, " 8x8dct=%d", p->analyse.b_transform_8x8 );
  774.     s += sprintf( s, " cqm=%d", p->i_cqm_preset );
  775.     s += sprintf( s, " deadzone=%d,%d", p->analyse.i_luma_deadzone[0], p->analyse.i_luma_deadzone[1] );
  776.     s += sprintf( s, " chroma_qp_offset=%d", p->analyse.i_chroma_qp_offset );
  777.     s += sprintf( s, " threads=%d", p->i_threads );
  778.     s += sprintf( s, " nr=%d", p->analyse.i_noise_reduction );
  779.     s += sprintf( s, " decimate=%d", p->analyse.b_dct_decimate );
  780.     s += sprintf( s, " mbaff=%d", p->b_interlaced );
  781.     s += sprintf( s, " bframes=%d", p->i_bframe );
  782.     if( p->i_bframe )
  783.     {
  784.         s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d wpredb=%d bime=%d",
  785.                       p->b_bframe_pyramid, p->b_bframe_adaptive, p->i_bframe_bias,
  786.                       p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred,
  787.                       p->analyse.b_bidir_me );
  788.     }
  789.     s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d%s",
  790.                   p->i_keyint_max, p->i_keyint_min, p->i_scenecut_threshold,
  791.                   p->b_pre_scenecut ? "(pre)" : "" );
  792.     s += sprintf( s, " rc=%s", p->rc.i_rc_method == X264_RC_ABR ?
  793.                                ( p->rc.b_stat_read ? "2pass" : p->rc.i_vbv_buffer_size ? "cbr" : "abr" )
  794.                                : p->rc.i_rc_method == X264_RC_CRF ? "crf" : "cqp" );
  795.     if( p->rc.i_rc_method == X264_RC_ABR || p->rc.i_rc_method == X264_RC_CRF )
  796.     {
  797.         if( p->rc.i_rc_method == X264_RC_CRF )
  798.             s += sprintf( s, " crf=%.1f", p->rc.f_rf_constant );
  799.         else
  800.             s += sprintf( s, " bitrate=%d ratetol=%.1f",
  801.                           p->rc.i_bitrate, p->rc.f_rate_tolerance );
  802.         s += sprintf( s, " qcomp=%.2f qpmin=%d qpmax=%d qpstep=%d",
  803.                       p->rc.f_qcompress, p->rc.i_qp_min, p->rc.i_qp_max, p->rc.i_qp_step );
  804.         if( p->rc.b_stat_read )
  805.             s += sprintf( s, " cplxblur=%.1f qblur=%.1f",
  806.                           p->rc.f_complexity_blur, p->rc.f_qblur );
  807.         if( p->rc.i_vbv_buffer_size )
  808.             s += sprintf( s, " vbv_maxrate=%d vbv_bufsize=%d",
  809.                           p->rc.i_vbv_max_bitrate, p->rc.i_vbv_buffer_size );
  810.     }
  811.     else if( p->rc.i_rc_method == X264_RC_CQP )
  812.         s += sprintf( s, " qp=%d", p->rc.i_qp_constant );
  813.     if( !(p->rc.i_rc_method == X264_RC_CQP && p->rc.i_qp_constant == 0) )
  814.     {
  815.         s += sprintf( s, " ip_ratio=%.2f", p->rc.f_ip_factor );
  816.         if( p->i_bframe )
  817.             s += sprintf( s, " pb_ratio=%.2f", p->rc.f_pb_factor );
  818.         s += sprintf( s, " aq=%d", p->rc.i_aq_mode );
  819.         if( p->rc.i_aq_mode )
  820.             s += sprintf( s, ":%.2f", p->rc.f_aq_strength );
  821.         if( p->rc.psz_zones )
  822.             s += sprintf( s, " zones=%s", p->rc.psz_zones );
  823.         else if( p->rc.i_zones )
  824.             s += sprintf( s, " zones" );
  825.     }
  826.     return buf;
  827. }