]> git.sesse.net Git - x264/blobdiff - x264.c
fix SOFLAGS used when building gtk frontend
[x264] / x264.c
diff --git a/x264.c b/x264.c
index badb5237d3c7d48c9062b604e33fd1e1572d7dba..1f435f4a06021c58954ec9b324cf627577971e0a 100644 (file)
--- a/x264.c
+++ b/x264.c
@@ -1,10 +1,10 @@
 /*****************************************************************************
- * x264: h264 encoder/decoder testing program.
+ * x264: h264 encoder testing program.
  *****************************************************************************
- * Copyright (C) 2003 Laurent Aimar
- * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $
+ * Copyright (C) 2003-2008 x264 project
  *
- * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ * Authors: Loren Merritt <lorenm@u.washington.edu>
+ *          Laurent Aimar <fenrir@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  *****************************************************************************/
 
-#define _LARGEFILE_SOURCE
-#define _FILE_OFFSET_BITS 64
-
 #include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
 #include <math.h>
 
 #include <signal.h>
 #define _GNU_SOURCE
 #include <getopt.h>
 
-#ifdef _MSC_VER
-#include <io.h>     /* _setmode() */
-#include <fcntl.h>  /* _O_BINARY */
-#endif
+#include "common/common.h"
+#include "common/cpu.h"
+#include "x264.h"
+#include "muxers.h"
 
 #ifndef _MSC_VER
 #include "config.h"
 #endif
 
-#include "common/common.h"
-#include "x264.h"
-#include "muxers.h"
-
-#define DATA_MAX 3000000
-uint8_t data[DATA_MAX];
+uint8_t *mux_buffer = NULL;
+int mux_buffer_size = 0;
 
 /* Ctrl-C handler */
 static int     b_ctrl_c = 0;
@@ -92,8 +83,14 @@ int main( int argc, char **argv )
 {
     x264_param_t param;
     cli_opt_t opt;
+    int ret;
 
-#ifdef _MSC_VER
+#ifdef PTW32_STATIC_LIB
+    pthread_win32_process_attach_np();
+    pthread_win32_thread_attach_np();
+#endif
+
+#ifdef _WIN32
     _setmode(_fileno(stdin), _O_BINARY);
     _setmode(_fileno(stdout), _O_BINARY);
 #endif
@@ -107,7 +104,14 @@ int main( int argc, char **argv )
     /* Control-C handler */
     signal( SIGINT, SigIntHandler );
 
-    return Encode( &param, &opt );
+    ret = Encode( &param, &opt );
+
+#ifdef PTW32_STATIC_LIB
+    pthread_win32_thread_detach_np();
+    pthread_win32_process_detach_np();
+#endif
+
+    return ret;
 }
 
 static char const *strtable_lookup( const char * const table[], int index )
@@ -156,21 +160,24 @@ static void Help( x264_param_t *defaults, int b_longhelp )
     H0( "  -I, --keyint <integer>      Maximum GOP size [%d]\n", defaults->i_keyint_max );
     H1( "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n", defaults->i_keyint_min );
     H1( "      --scenecut <integer>    How aggressively to insert extra I-frames [%d]\n", defaults->i_scenecut_threshold );
+    H1( "      --pre-scenecut          Faster, less precise scenecut detection.\n"
+        "                                  Required and implied by multi-threading.\n" );
     H0( "  -b, --bframes <integer>     Number of B-frames between I and P [%d]\n", defaults->i_bframe );
     H1( "      --no-b-adapt            Disable adaptive B-frame decision\n" );
     H1( "      --b-bias <integer>      Influences how often B-frames are used [%d]\n", defaults->i_bframe_bias );
     H0( "      --b-pyramid             Keep some B-frames as references\n" );
     H0( "      --no-cabac              Disable CABAC\n" );
     H0( "  -r, --ref <integer>         Number of reference frames [%d]\n", defaults->i_frame_reference );
-    H1( "      --nf                    Disable loop filter\n" );
-    H0( "  -f, --filter <alpha:beta>   Loop filter AlphaC0 and Beta parameters [%d:%d]\n",
+    H1( "      --no-deblock            Disable loop filter\n" );
+    H0( "  -f, --deblock <alpha:beta>  Loop filter AlphaC0 and Beta parameters [%d:%d]\n",
                                        defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta );
+    H0( "      --interlaced            Enable pure-interlaced mode\n" );
     H0( "\n" );
     H0( "Ratecontrol:\n" );
     H0( "\n" );
     H0( "  -q, --qp <integer>          Set QP (0=lossless) [%d]\n", defaults->rc.i_qp_constant );
     H0( "  -B, --bitrate <integer>     Set bitrate (kbit/s)\n" );
-    H0( "      --crf <integer>         Quality-based VBR (nominal QP)\n" );
+    H0( "      --crf <float>           Quality-based VBR (nominal QP)\n" );
     H1( "      --vbv-maxrate <integer> Max local bitrate (kbit/s) [%d]\n", defaults->rc.i_vbv_max_bitrate );
     H0( "      --vbv-bufsize <integer> Enable CBR and set size of the VBV buffer (kbit) [%d]\n", defaults->rc.i_vbv_buffer_size );
     H1( "      --vbv-init <float>      Initial VBV buffer occupancy [%.1f]\n", defaults->rc.f_vbv_buffer_init );
@@ -181,13 +188,20 @@ static void Help( x264_param_t *defaults, int b_longhelp )
     H0( "      --ipratio <float>       QP factor between I and P [%.2f]\n", defaults->rc.f_ip_factor );
     H0( "      --pbratio <float>       QP factor between P and B [%.2f]\n", defaults->rc.f_pb_factor );
     H1( "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n", defaults->analyse.i_chroma_qp_offset );
+    H0( "      --aq-mode <integer>     How AQ distributes bits [%d]\n"
+        "                                  - 0: Disabled\n"
+        "                                  - 1: Avoid moving bits between frames\n"
+        "                                  - 2: Move bits between frames\n", defaults->rc.i_aq_mode );
+    H0( "      --aq-strength <float>   Reduces blocking and blurring in flat and\n"
+        "                              textured areas. [%.1f]\n"
+        "                                  - 0.5: weak AQ\n"
+        "                                  - 1.5: strong AQ\n", defaults->rc.f_aq_strength );
     H0( "\n" );
     H0( "  -p, --pass <1|2|3>          Enable multipass ratecontrol\n"
         "                                  - 1: First pass, creates stats file\n"
         "                                  - 2: Last pass, does not overwrite stats file\n"
         "                                  - 3: Nth pass, overwrites stats file\n" );
     H0( "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n", defaults->rc.psz_stat_out );
-    H1( "      --rceq <string>         Ratecontrol equation [\"%s\"]\n", defaults->rc.psz_rc_eq );
     H0( "      --qcomp <float>         QP curve compression: 0.0 => CBR, 1.0 => CQP [%.2f]\n", defaults->rc.f_qcompress );
     H1( "      --cplxblur <float>      Reduce fluctuations in QP (before curve compression) [%.1f]\n", defaults->rc.f_complexity_blur );
     H1( "      --qblur <float>         Reduce fluctuations in QP (after curve compression) [%.1f]\n", defaults->rc.f_qblur );
@@ -201,22 +215,30 @@ static void Help( x264_param_t *defaults, int b_longhelp )
     H0( "\n" );
     H0( "Analysis:\n" );
     H0( "\n" );
-    H0( "  -A, --analyse <string>      Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
+    H0( "  -A, --partitions <string>   Partitions to consider [\"p8x8,b8x8,i8x8,i4x4\"]\n"
         "                                  - p8x8, p4x4, b8x8, i8x8, i4x4\n"
         "                                  - none, all\n"
         "                                  (p4x4 requires p8x8. i8x8 requires --8x8dct.)\n" );
     H0( "      --direct <string>       Direct MV prediction mode [\"%s\"]\n"
         "                                  - none, spatial, temporal, auto\n",
                                        strtable_lookup( x264_direct_pred_names, defaults->analyse.i_direct_mv_pred ) );
+    H1( "      --direct-8x8 <-1|0|1>   Direct prediction size [%d]\n"
+        "                                  -  0: 4x4\n"
+        "                                  -  1: 8x8\n"
+        "                                  - -1: smallest possible according to level\n",
+                                       defaults->analyse.i_direct_8x8_inference );
     H0( "  -w, --weightb               Weighted prediction for B-frames\n" );
     H0( "      --me <string>           Integer pixel motion estimation method [\"%s\"]\n",
                                        strtable_lookup( x264_motion_est_names, defaults->analyse.i_me_method ) );
     H1( "                                  - dia: diamond search, radius 1 (fast)\n"
         "                                  - hex: hexagonal search, radius 2\n"
         "                                  - umh: uneven multi-hexagon search\n"
-        "                                  - esa: exhaustive search (slow)\n" );
+        "                                  - esa: exhaustive search\n"
+        "                                  - tesa: hadamard exhaustive search (slow)\n" );
     else H0( "                                  - dia, hex, umh\n" );
     H0( "      --merange <integer>     Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range );
+    H1( "      --mvrange <integer>     Maximum motion vector length [-1 (auto)]\n" );
+    H1( "      --mvrange-thread <int>  Minimum buffer between threads [-1 (auto)]\n" );
     H0( "  -m, --subme <integer>       Subpixel motion estimation and partition\n"
         "                                  decision quality: 1=fast, 7=best. [%d]\n", defaults->analyse.i_subpel_refine );
     H0( "      --b-rdo                 RD based mode decision for B-frames. Requires subme 6.\n" );
@@ -232,6 +254,9 @@ static void Help( x264_param_t *defaults, int b_longhelp )
     H0( "      --no-dct-decimate       Disables coefficient thresholding on P-frames\n" );
     H0( "      --nr <integer>          Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
     H1( "\n" );
+    H1( "      --deadzone-inter <int>  Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
+    H1( "      --deadzone-intra <int>  Set the size of the intra luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[1] );
+    H1( "                                  Deadzones should be in the range 0 - 32.\n" );
     H1( "      --cqm <string>          Preset quant matrices [\"flat\"]\n"
         "                                  - jvt, flat\n" );
     H0( "      --cqmfile <string>      Read custom quant matrices from a JM-compatible file\n" );
@@ -286,10 +311,14 @@ static void Help( x264_param_t *defaults, int b_longhelp )
     H0( "      --progress              Show a progress indicator while encoding\n" );
     H0( "      --quiet                 Quiet Mode\n" );
     H0( "      --no-psnr               Disable PSNR computation\n" );
-    H0( "      --threads <integer>     Parallel encoding (uses slices)\n" );
+    H0( "      --no-ssim               Disable SSIM computation\n" );
+    H0( "      --threads <integer>     Parallel encoding\n" );
     H0( "      --thread-input          Run Avisynth in its own thread\n" );
+    H1( "      --non-deterministic     Slightly improve quality of SMP, at the cost of repeatability\n" );
+    H1( "      --asm <integer>         Override CPU detection\n" );
     H1( "      --no-asm                Disable all CPU optimizations\n" );
     H1( "      --visualize             Show MB types overlayed on the encoded video\n" );
+    H1( "      --dump-yuv <string>     Save reconstructed frames\n" );
     H1( "      --sps-id <integer>      Set SPS and PPS id numbers [%d]\n", defaults->i_sps_id );
     H1( "      --aud                   Use access unit delimiters\n" );
     H0( "\n" );
@@ -351,8 +380,12 @@ static int  Parse( int argc, char **argv,
             { "min-keyint",required_argument,NULL,'i' },
             { "keyint",  required_argument, NULL, 'I' },
             { "scenecut",required_argument, NULL, 0 },
+            { "pre-scenecut", no_argument,  NULL, 0 },
             { "nf",      no_argument,       NULL, 0 },
-            { "filter",  required_argument, NULL, 'f' },
+            { "no-deblock", no_argument,    NULL, 0 },
+            { "filter",  required_argument, NULL, 0 },
+            { "deblock", required_argument, NULL, 'f' },
+            { "interlaced", no_argument,    NULL, 0 },
             { "no-cabac",no_argument,       NULL, 0 },
             { "qp",      required_argument, NULL, 'q' },
             { "qpmin",   required_argument, NULL, 0 },
@@ -360,17 +393,22 @@ static int  Parse( int argc, char **argv,
             { "qpstep",  required_argument, NULL, 0 },
             { "crf",     required_argument, NULL, 0 },
             { "ref",     required_argument, NULL, 'r' },
+            { "asm",     required_argument, NULL, 0 },
             { "no-asm",  no_argument,       NULL, 0 },
             { "sar",     required_argument, NULL, 0 },
             { "fps",     required_argument, NULL, 0 },
             { "frames",  required_argument, NULL, OPT_FRAMES },
             { "seek",    required_argument, NULL, OPT_SEEK },
             { "output",  required_argument, NULL, 'o' },
-            { "analyse", required_argument, NULL, 'A' },
+            { "analyse", required_argument, NULL, 0 },
+            { "partitions", required_argument, NULL, 'A' },
             { "direct",  required_argument, NULL, 0 },
+            { "direct-8x8", required_argument, NULL, 0 },
             { "weightb", no_argument,       NULL, 'w' },
             { "me",      required_argument, NULL, 0 },
             { "merange", required_argument, NULL, 0 },
+            { "mvrange", required_argument, NULL, 0 },
+            { "mvrange-thread", required_argument, NULL, 0 },
             { "subme",   required_argument, NULL, 'm' },
             { "b-rdo",   no_argument,       NULL, 0 },
             { "mixed-refs", no_argument,    NULL, 0 },
@@ -380,6 +418,10 @@ static int  Parse( int argc, char **argv,
             { "trellis", required_argument, NULL, 't' },
             { "no-fast-pskip", no_argument, NULL, 0 },
             { "no-dct-decimate", no_argument, NULL, 0 },
+            { "aq-strength", required_argument, NULL, 0 },
+            { "aq-mode", required_argument, NULL, 0 },
+            { "deadzone-inter", required_argument, NULL, '0' },
+            { "deadzone-intra", required_argument, NULL, '0' },
             { "level",   required_argument, NULL, 0 },
             { "ratetol", required_argument, NULL, 0 },
             { "vbv-maxrate", required_argument, NULL, 0 },
@@ -390,7 +432,6 @@ static int  Parse( int argc, char **argv,
             { "chroma-qp-offset", required_argument, NULL, 0 },
             { "pass",    required_argument, NULL, 'p' },
             { "stats",   required_argument, NULL, 0 },
-            { "rceq",    required_argument, NULL, 0 },
             { "qcomp",   required_argument, NULL, 0 },
             { "qblur",   required_argument, NULL, 0 },
             { "cplxblur",required_argument, NULL, 0 },
@@ -398,11 +439,14 @@ static int  Parse( int argc, char **argv,
             { "qpfile",  required_argument, NULL, OPT_QPFILE },
             { "threads", required_argument, NULL, 0 },
             { "thread-input", no_argument,  NULL, OPT_THREAD_INPUT },
+            { "non-deterministic", no_argument, NULL, 0 },
             { "no-psnr", no_argument,       NULL, 0 },
+            { "no-ssim", no_argument,       NULL, 0 },
             { "quiet",   no_argument,       NULL, OPT_QUIET },
             { "verbose", no_argument,       NULL, 'v' },
             { "progress",no_argument,       NULL, OPT_PROGRESS },
             { "visualize",no_argument,      NULL, OPT_VISUALIZE },
+            { "dump-yuv",required_argument, NULL, 0 },
             { "sps-id",  required_argument, NULL, 0 },
             { "aud",     no_argument,       NULL, 0 },
             { "nr",      required_argument, NULL, 0 },
@@ -502,7 +546,6 @@ static int  Parse( int argc, char **argv,
                 break;
             case OPT_QUIET:
                 param->i_log_level = X264_LOG_NONE;
-                param->analyse.b_psnr = 0;
                 break;
             case 'v':
                 param->i_log_level = X264_LOG_DEBUG;
@@ -536,7 +579,7 @@ static int  Parse( int argc, char **argv,
                     }
                 }
 
-                b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg ? optarg : "true" );
+                b_error |= x264_param_parse( param, long_options[long_options_index].name, optarg );
             }
         }
 
@@ -587,7 +630,7 @@ static int  Parse( int argc, char **argv,
             sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
         }
     }
-        
+
     if( !(b_avis || b_y4m) && ( !param->i_width || !param->i_height ) )
     {
         fprintf( stderr, "x264 [error]: Rawyuv input requires a resolution.\n" );
@@ -624,7 +667,8 @@ static int  Parse( int argc, char **argv,
     }
 
 #ifdef HAVE_PTHREAD
-    if( b_thread_input || param->i_threads > 1 )
+    if( b_thread_input || param->i_threads > 1
+        || (param->i_threads == 0 && x264_cpu_num_processors() > 1) )
     {
         if( open_file_thread( NULL, &opt->hin, param ) )
         {
@@ -645,11 +689,20 @@ static int  Parse( int argc, char **argv,
 
 static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
 {
-    int num = -1, qp;
+    int num = -1, qp, ret;
     char type;
+    uint64_t file_pos;
     while( num < i_frame )
     {
-        int ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
+        file_pos = ftell( opt->qpfile );
+        ret = fscanf( opt->qpfile, "%d %c %d\n", &num, &type, &qp );
+               if( num > i_frame || ret == EOF )
+               {
+                       pic->i_type = X264_TYPE_AUTO;
+                       pic->i_qpplus1 = 0;
+                       fseek( opt->qpfile , file_pos , SEEK_SET );
+                       break;
+               }
         if( num < i_frame )
             continue;
         pic->i_qpplus1 = qp+1;
@@ -659,7 +712,7 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
         else if( type == 'B' ) pic->i_type = X264_TYPE_BREF;
         else if( type == 'b' ) pic->i_type = X264_TYPE_B;
         else ret = 0;
-        if( ret != 3 || qp < 0 || qp > 51 || num > i_frame )
+        if( ret != 3 || qp < -1 || qp > 51 )
         {
             fprintf( stderr, "x264 [error]: can't parse qpfile for frame %d\n", i_frame );
             fclose( opt->qpfile );
@@ -672,7 +725,7 @@ static void parse_qpfile( cli_opt_t *opt, x264_picture_t *pic, int i_frame )
 }
 
 /*****************************************************************************
- * Decode:
+ * Encode:
  *****************************************************************************/
 
 static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
@@ -690,17 +743,17 @@ static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
     for( i = 0; i < i_nal; i++ )
     {
         int i_size;
-        int i_data;
 
-        i_data = DATA_MAX;
-        if( ( i_size = x264_nal_encode( data, &i_data, 1, &nal[i] ) ) > 0 )
-        {
-            i_file += p_write_nalu( hout, data, i_size );
-        }
-        else if( i_size < 0 )
+        if( mux_buffer_size < nal[i].i_payload * 3/2 + 4 )
         {
-            fprintf( stderr, "x264 [error]: need to increase buffer size (size=%d)\n", -i_size );
+            mux_buffer_size = nal[i].i_payload * 2 + 4;
+            x264_free( mux_buffer );
+            mux_buffer = x264_malloc( mux_buffer_size );
         }
+
+        i_size = mux_buffer_size;
+        x264_nal_encode( mux_buffer, &i_size, 1, &nal[i] );
+        i_file += p_write_nalu( hout, mux_buffer, i_size );
     }
     if (i_nal)
         p_set_eop( hout, &pic_out );
@@ -708,9 +761,6 @@ static int  Encode_frame( x264_t *h, hnd_t hout, x264_picture_t *pic )
     return i_file;
 }
 
-/*****************************************************************************
- * Encode:
- *****************************************************************************/
 static int  Encode( x264_param_t *param, cli_opt_t *opt )
 {
     x264_t *h;
@@ -733,7 +783,6 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     {
         fprintf( stderr, "x264 [error]: x264_encoder_open failed\n" );
         p_close_infile( opt->hin );
-        p_close_outfile( opt->hout );
         return -1;
     }
 
@@ -773,7 +822,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
         i_frame++;
 
         /* update status line (up to 1000 times per input file) */
-        if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG && 
+        if( opt->b_progress && param->i_log_level < X264_LOG_DEBUG &&
             ( i_frame_total ? i_frame * 1000 / i_frame_total > i_progress
                             : i_frame % 10 == 0 ) )
         {
@@ -801,6 +850,7 @@ static int  Encode( x264_param_t *param, cli_opt_t *opt )
     i_end = x264_mdate();
     x264_picture_clean( &pic );
     x264_encoder_close( h );
+    x264_free( mux_buffer );
     fprintf( stderr, "\n" );
 
     if( b_ctrl_c )