]> git.sesse.net Git - x264/blobdiff - x264.c
faster mmx satd. *x16: 20%, *x8: 10%, total: 2-4%.
[x264] / x264.c
diff --git a/x264.c b/x264.c
index 2d261868e26b1e401faf020f2340f1f8baeaa309..bc30e30be4837219dc0839357de1b6a296e69b23 100644 (file)
--- a/x264.c
+++ b/x264.c
 #include <gpac/m4_author.h>
 #endif
 
+
 #include "common/common.h"
 #include "x264.h"
+
 #ifndef _MSC_VER
 #include "config.h"
 #endif
 
+#include "matroska.h"
+
 #define DATA_MAX 3000000
 uint8_t data[DATA_MAX];
 
@@ -114,6 +118,12 @@ static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture );
 static int close_file_mp4( hnd_t handle );
 #endif
 
+static int open_file_mkv( char *psz_filename, hnd_t *p_handle );
+static int set_param_mkv( hnd_t handle, x264_param_t *p_param );
+static int write_nalu_mkv( hnd_t handle, uint8_t *p_nal, int i_size );
+static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture );
+static int close_file_mkv( hnd_t handle );
+
 static void Help( x264_param_t *defaults );
 static int  Parse( int argc, char **argv, x264_param_t *param, cli_opt_t *opt );
 static int  Encode( x264_param_t *param, cli_opt_t *opt );
@@ -157,6 +167,7 @@ static void Help( x264_param_t *defaults )
              "  or AVI or Avisynth if compiled with AVIS support (%s).\n"
              "Outfile type is selected by filename:\n"
              " .264 -> Raw bytestream\n"
+             " .mkv -> Matroska\n"
              " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
              "\n"
              "Options:\n"
@@ -232,6 +243,19 @@ static void Help( x264_param_t *defaults )
              "      --no-chroma-me          Ignore chroma in motion estimation\n"
              "  -8, --8x8dct                Adaptive spatial transform size\n"
              "\n"
+             "      --cqm <string>          Preset quant matrices [\"flat\"]\n"
+             "                                  - jvt, flat\n"
+             "      --cqmfile <string>      Read quant matrices from a JM-compatible file\n"
+             "                                  Overrides any other --cqm* options.\n"
+             "      --cqm4 <list>           Set all 4x4 quant matrices\n"
+             "                                  Takes a comma-separated list of 16 integers.\n"
+             "      --cqm8 <list>           Set all 8x8 quant matrices\n"
+             "                                  Takes a comma-separated list of 64 integers.\n"
+             "      --cqm4i, --cqm4p, --cqm8i, --cqm8p\n"
+             "                              Set both luma and chroma quant matrices\n"
+             "      --cqm4iy, --cqm4ic, --cqm4py, --cqm4pc\n"
+             "                              Set individual quant matrices\n"
+             "\n"
              "Input/Output:\n"
              "\n"
              "      --level <integer>       Specify level (as defined by Annex A)\n"
@@ -294,6 +318,18 @@ static void Help( x264_param_t *defaults )
            );
 }
 
+static int parse_cqm( const char *str, uint8_t *cqm, int length )
+{
+    int i = 0;
+    do {
+        int coef;
+        if( !sscanf( str, "%d", &coef ) || coef < 1 || coef > 255 )
+            return -1;
+        cqm[i++] = coef;
+    } while( i < length && (str = strchr( str, ',' )) && str++ );
+    return (i == length) ? 0 : -1;
+}
+
 /*****************************************************************************
  * Parse:
  *****************************************************************************/
@@ -324,6 +360,7 @@ static int  Parse( int argc, char **argv,
     opterr = 0; // no error message
     for( ;; )
     {
+        int b_error = 0;
         int long_options_index;
 #define OPT_QPMIN 256
 #define OPT_QPMAX 257
@@ -360,6 +397,18 @@ static int  Parse( int argc, char **argv,
 #define OPT_SEEK 291
 #define OPT_ZONES 292
 #define OPT_THREADS 293
+#define OPT_CQM 294
+#define OPT_CQM4 295
+#define OPT_CQM4I 296
+#define OPT_CQM4IY 297
+#define OPT_CQM4IC 298
+#define OPT_CQM4P 299
+#define OPT_CQM4PY 300
+#define OPT_CQM4PC 301
+#define OPT_CQM8 302
+#define OPT_CQM8I 303
+#define OPT_CQM8P 304
+#define OPT_CQMFILE 305
 
         static struct option long_options[] =
         {
@@ -416,6 +465,18 @@ static int  Parse( int argc, char **argv,
             { "progress",no_argument,       NULL, OPT_PROGRESS },
             { "visualize",no_argument,      NULL, OPT_VISUALIZE },
             { "aud",     no_argument,       NULL, OPT_AUD },
+            { "cqm",     required_argument, NULL, OPT_CQM },
+            { "cqmfile", required_argument, NULL, OPT_CQMFILE },
+            { "cqm4",    required_argument, NULL, OPT_CQM4 },
+            { "cqm4i",   required_argument, NULL, OPT_CQM4I },
+            { "cqm4iy",  required_argument, NULL, OPT_CQM4IY },
+            { "cqm4ic",  required_argument, NULL, OPT_CQM4IC },
+            { "cqm4p",   required_argument, NULL, OPT_CQM4P },
+            { "cqm4py",  required_argument, NULL, OPT_CQM4PY },
+            { "cqm4pc",  required_argument, NULL, OPT_CQM4PC },
+            { "cqm8",    required_argument, NULL, OPT_CQM8 },
+            { "cqm8i",   required_argument, NULL, OPT_CQM8I },
+            { "cqm8p",   required_argument, NULL, OPT_CQM8P },
             {0, 0, 0, 0}
         };
 
@@ -455,9 +516,13 @@ static int  Parse( int argc, char **argv,
                 break;
             case 'i':
                 param->i_keyint_min = atol( optarg );
+                if( param->i_keyint_max < param->i_keyint_min )
+                    param->i_keyint_max = param->i_keyint_min;
                 break;
             case 'I':
                 param->i_keyint_max = atol( optarg );
+                if( param->i_keyint_min > param->i_keyint_max )
+                    param->i_keyint_min = param->i_keyint_max;
                 break;
             case OPT_SCENECUT:
                 param->i_scenecut_threshold = atol( optarg );
@@ -516,6 +581,14 @@ static int  Parse( int argc, char **argv,
                     return -1;
 #endif
                 }
+        else if( !strncasecmp(optarg + strlen(optarg) - 4, ".mkv", 4) )
+        {
+            p_open_outfile = open_file_mkv;
+                    p_write_nalu = write_nalu_mkv;
+                    p_set_outfile_param = set_param_mkv;
+                    p_set_eop = set_eop_mkv;
+                    p_close_outfile = close_file_mkv;
+        }
                 if( !strcmp(optarg, "-") )
                     opt->hout = stdout;
                 else if( p_open_outfile( optarg, &opt->hout ) )
@@ -603,7 +676,7 @@ static int  Parse( int argc, char **argv,
                 param->i_level_idc = atoi(optarg);
                 break;
             case OPT_RATETOL:
-                param->rc.f_rate_tolerance = atof(optarg);
+                param->rc.f_rate_tolerance = !strncmp("inf", optarg, 3) ? 1e9 : atof(optarg);
                 break;
             case OPT_VBVMAXRATE:
                 param->rc.i_vbv_max_bitrate = atoi( optarg );
@@ -680,10 +753,76 @@ static int  Parse( int argc, char **argv,
                 fprintf( stderr, "not compiled with visualization support\n" );
 #endif
                 break;
+            case OPT_CQM:
+                if( strstr( optarg, "flat" ) )
+                    param->i_cqm_preset = X264_CQM_FLAT;
+                else if( strstr( optarg, "jvt" ) )
+                    param->i_cqm_preset = X264_CQM_JVT;
+                else
+                {
+                    fprintf( stderr, "bad CQM preset `%s'\n", optarg );
+                    return -1;
+                }
+                break;
+            case OPT_CQMFILE:
+                param->psz_cqm_file = optarg;
+                break;
+            case OPT_CQM4:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+                b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
+                b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
+                break;
+            case OPT_CQM8:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
+                b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
+                break;
+            case OPT_CQM4I:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+                break;
+            case OPT_CQM4P:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4py, 16 );
+                b_error |= parse_cqm( optarg, param->cqm_4pc, 16 );
+                break;
+            case OPT_CQM4IY:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+                break;
+            case OPT_CQM4IC:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+                break;
+            case OPT_CQM4PY:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4iy, 16 );
+                break;
+            case OPT_CQM4PC:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_4ic, 16 );
+                break;
+            case OPT_CQM8I:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_8iy, 64 );
+                break;
+            case OPT_CQM8P:
+                param->i_cqm_preset = X264_CQM_CUSTOM;
+                b_error |= parse_cqm( optarg, param->cqm_8py, 64 );
+                break;
             default:
                 fprintf( stderr, "unknown option (%c)\n", optopt );
                 return -1;
         }
+
+        if( b_error )
+        {
+            fprintf( stderr, "bad argument (%s)\n", optarg );
+            return -1;
+        }
     }
 
     /* Get the file name */
@@ -989,7 +1128,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         if( p_read_frame( &pic, opt->hin, i_frame + opt->i_seek, param->i_width, param->i_height ) )
             break;
 
-        pic.i_pts = i_frame * param->i_fps_den;
+        pic.i_pts = (int64_t)i_frame * param->i_fps_den;
 
         i_file += Encode_frame( h, opt->hout, &pic );
 
@@ -1453,3 +1592,237 @@ static int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture )
 }
 
 #endif
+
+
+/* -- mkv muxing support ------------------------------------------------- */
+typedef struct
+{
+    mk_Writer *w;
+
+    uint8_t   *sps, *pps;
+    int       sps_len, pps_len;
+
+    int       width, height, d_width, d_height;
+
+    int64_t   frame_duration;
+    int       fps_num;
+
+    int       b_header_written;
+    char      b_writing_frame;
+} mkv_t;
+
+static int write_header_mkv( mkv_t *p_mkv )
+{
+    int       ret;
+    uint8_t   *avcC;
+    int  avcC_len;
+
+    if( p_mkv->sps == NULL || p_mkv->pps == NULL ||
+        p_mkv->width == 0 || p_mkv->height == 0 ||
+        p_mkv->d_width == 0 || p_mkv->d_height == 0)
+        return -1;
+
+    avcC_len = 5 + 1 + 2 + p_mkv->sps_len + 1 + 2 + p_mkv->pps_len;
+    avcC = malloc(avcC_len);
+    if (avcC == NULL)
+        return -1;
+
+    avcC[0] = 1;
+    avcC[1] = p_mkv->sps[1];
+    avcC[2] = p_mkv->sps[2];
+    avcC[3] = p_mkv->sps[3];
+    avcC[4] = 0xfe; // nalu size length is three bytes
+    avcC[5] = 0xe1; // one sps
+
+    avcC[6] = p_mkv->sps_len >> 8;
+    avcC[7] = p_mkv->sps_len;
+
+    memcpy(avcC+8, p_mkv->sps, p_mkv->sps_len);
+
+    avcC[8+p_mkv->sps_len] = 1; // one pps
+    avcC[9+p_mkv->sps_len] = p_mkv->pps_len >> 8;
+    avcC[10+p_mkv->sps_len] = p_mkv->pps_len;
+
+    memcpy( avcC+11+p_mkv->sps_len, p_mkv->pps, p_mkv->pps_len );
+
+    ret = mk_writeHeader( p_mkv->w, "x264", "V_MPEG4/ISO/AVC",
+                          avcC, avcC_len, p_mkv->frame_duration, 50000,
+                          p_mkv->width, p_mkv->height,
+                          p_mkv->d_width, p_mkv->d_height );
+
+    free( avcC );
+
+    p_mkv->b_header_written = 1;
+
+    return ret;
+}
+
+static int open_file_mkv( char *psz_filename, hnd_t *p_handle )
+{
+    mkv_t *p_mkv;
+
+    *p_handle = NULL;
+
+    p_mkv  = malloc(sizeof(*p_mkv));
+    if (p_mkv == NULL)
+        return -1;
+
+    memset(p_mkv, 0, sizeof(*p_mkv));
+
+    p_mkv->w = mk_createWriter(psz_filename);
+    if (p_mkv->w == NULL)
+    {
+        free(p_mkv);
+        return -1;
+    }
+
+    *p_handle = p_mkv;
+
+    return 0;
+}
+
+static int set_param_mkv( hnd_t handle, x264_param_t *p_param )
+{
+    mkv_t   *p_mkv = handle;
+    int64_t dw, dh;
+
+    if( p_param->i_fps_num > 0 )
+    {
+        p_mkv->frame_duration = (int64_t)p_param->i_fps_den *
+                                (int64_t)1000000000 / p_param->i_fps_num;
+        p_mkv->fps_num = p_param->i_fps_num;
+    }
+    else
+    {
+        p_mkv->frame_duration = 0;
+        p_mkv->fps_num = 1;
+    }
+
+    p_mkv->width = p_param->i_width;
+    p_mkv->height = p_param->i_height;
+
+    if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
+    {
+        dw = (int64_t)p_param->i_width  * p_param->vui.i_sar_width;
+        dh = (int64_t)p_param->i_height * p_param->vui.i_sar_height;
+    }
+    else
+    {
+        dw = p_param->i_width;
+        dh = p_param->i_height;
+    }
+
+    if( dw > 0 && dh > 0 )
+    {
+       int64_t a = dw, b = dh;
+
+        for (;;)
+        {
+            int64_t c = a % b;
+            if( c == 0 )
+              break;
+            a = b;
+            b = c;
+        }
+
+       dw /= b;
+       dh /= b;
+    }
+
+    p_mkv->d_width = (int)dw;
+    p_mkv->d_height = (int)dh;
+
+    return 0;
+}
+
+static int write_nalu_mkv( hnd_t handle, uint8_t *p_nalu, int i_size )
+{
+    mkv_t *p_mkv = handle;
+    uint8_t type = p_nalu[4] & 0x1f;
+    uint8_t dsize[3];
+    int psize;
+
+    switch( type )
+    {
+    // sps
+    case 0x07:
+        if( !p_mkv->sps )
+        {
+            p_mkv->sps = malloc(i_size - 4);
+            if (p_mkv->sps == NULL)
+                return -1;
+            p_mkv->sps_len = i_size - 4;
+            memcpy(p_mkv->sps, p_nalu + 4, i_size - 4);
+        }
+        break;
+
+    // pps
+    case 0x08:
+        if( !p_mkv->pps )
+        {
+            p_mkv->pps = malloc(i_size - 4);
+            if (p_mkv->pps == NULL)
+                return -1;
+            p_mkv->pps_len = i_size - 4;
+            memcpy(p_mkv->pps, p_nalu + 4, i_size - 4);
+        }
+        break;
+
+    // slice, sei
+    case 0x1:
+    case 0x5:
+    case 0x6:
+        if( !p_mkv->b_writing_frame )
+        {
+            if( mk_startFrame(p_mkv->w) < 0 )
+               return -1;
+            p_mkv->b_writing_frame = 1;
+        }
+        psize = i_size - 4 ;
+        dsize[0] = psize >> 16;
+        dsize[1] = psize >> 8;
+        dsize[2] = psize;
+        if( mk_addFrameData(p_mkv->w, dsize, 3) < 0 ||
+           mk_addFrameData(p_mkv->w, p_nalu + 4, i_size - 4) < 0 )
+           return -1;
+        break;
+
+    default:
+        break;
+    }
+
+    if( !p_mkv->b_header_written && p_mkv->pps && p_mkv->sps &&
+        write_header_mkv(p_mkv) < 0 )
+        return -1;
+
+    return i_size;
+}
+
+static int set_eop_mkv( hnd_t handle, x264_picture_t *p_picture )
+{
+    mkv_t *p_mkv = handle;
+    int64_t i_stamp = (int64_t)(p_picture->i_pts * 1e9 / p_mkv->fps_num);
+
+    p_mkv->b_writing_frame = 0;
+
+    return mk_setFrameFlags( p_mkv->w, i_stamp,
+                             p_picture->i_type == X264_TYPE_IDR );
+}
+
+static int close_file_mkv( hnd_t handle )
+{
+    mkv_t *p_mkv = handle;
+    int   ret;
+
+    if( p_mkv->sps )
+        free( p_mkv->sps );
+    if( p_mkv->pps )
+        free( p_mkv->pps );
+
+    ret = mk_close(p_mkv->w);
+
+    free( p_mkv );
+
+    return ret;
+}
+