x264.c 25.4 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*****************************************************************************
 * x264: h264 encoder/decoder testing program.
 *****************************************************************************
 * Copyright (C) 2003 Laurent Aimar
 * $Id: x264.c,v 1.1 2004/06/03 19:24:12 fenrir Exp $
 *
 * Authors: 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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.
 *****************************************************************************/

#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

39
#include "common/common.h"
40
#include "x264.h"
Laurent Aimar's avatar
Laurent Aimar committed
41 42 43 44 45 46 47 48 49 50 51

#define DATA_MAX 3000000
uint8_t data[DATA_MAX];

/* Ctrl-C handler */
static int     i_ctrl_c = 0;
static void    SigIntHandler( int a )
{
    i_ctrl_c = 1;
}

52
static void Help( x264_param_t *defaults );
Laurent Aimar's avatar
Laurent Aimar committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static int  Parse( int argc, char **argv, x264_param_t  *param, FILE **p_fin, FILE **p_fout, int *pb_decompress );
static int  Encode( x264_param_t  *param, FILE *fyuv,  FILE *fout );
static int  Decode( x264_param_t  *param, FILE *fh26l, FILE *fout );

/****************************************************************************
 * main:
 ****************************************************************************/
int main( int argc, char **argv )
{
    x264_param_t param;

    FILE    *fout;
    FILE    *fin;

    int     b_decompress;
    int     i_ret;

#ifdef _MSC_VER
    _setmode(_fileno(stdin), _O_BINARY);    /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
    _setmode(_fileno(stdout), _O_BINARY);
#endif

    x264_param_default( &param );
76
    param.b_cabac = 0;
Laurent Aimar's avatar
Laurent Aimar committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    /* Parse command line */
    if( Parse( argc, argv, &param, &fin, &fout, &b_decompress ) < 0 )
    {
        return -1;
    }

    /* Control-C handler */
    signal( SIGINT, SigIntHandler );

    if( b_decompress )
        i_ret = Decode( &param, fin, fout );
    else
        i_ret = Encode( &param, fin, fout );

    return i_ret;
}

/*****************************************************************************
 * Help:
 *****************************************************************************/
98
static void Help( x264_param_t *defaults )
Laurent Aimar's avatar
Laurent Aimar committed
99 100
{
    fprintf( stderr,
101
             "x264 build:%d\n"
Laurent Aimar's avatar
Laurent Aimar committed
102 103 104 105
             "Syntax: x264 [options] [-o out.h26l] in.yuv widthxheigh\n"
             "\n"
             "  -h, --help                  Print this help\n"
             "\n"
106 107
             "  -I, --keyint <integer  >    Maximum GOP size [%d]\n"
             "  -i, --min-keyint <integer>  Minimum GOP size [%d]\n"
108 109
             "      --scenecut <integer>    How aggresively to insert extra I frames [%d]\n"
             "  -b, --bframe <integer>      Number of B-frames between I and P [%d]\n"
110 111
             "      --no-b-adapt            Disable adaptive B-frame decision\n"
             "      --b-bias <integer>      Influences how often B-frames are used [%d]\n"
112
             "      --b-pyramid             Keep some B-frames as references\n"
Laurent Aimar's avatar
Laurent Aimar committed
113 114
             "\n"
             "  -c, --cabac                 Enable CABAC\n"
115
             "  -r, --ref <integer>         Number of reference frames [%d]\n"
Laurent Aimar's avatar
Laurent Aimar committed
116
             "  -n, --nf                    Disable loop filter\n"
117
             "  -f, --filter <alpha:beta>   Loop filter AplhaCO and Beta parameters [%d]\n"
118
             "\n"
119
             "  -q, --qp <integer>          Set QP [%d]\n"
120
             "  -B, --bitrate <integer>     Set bitrate\n"
121 122 123 124 125 126 127 128
             "      --qpmin <integer>       Set min QP [%d]\n"
             "      --qpmax <integer>       Set max QP [%d]\n"
             "      --qpstep <integer>      Set max QP step [%d]\n"
             "      --rcsens <integer>      CBR ratecontrol sensitivity [%d]\n"
             "      --rcbuf <integer>       Size of VBV buffer [%d]\n"
             "      --rcinitbuf <integer>   Initial VBV buffer occupancy [%d]\n"
             "      --ipratio <float>       QP factor between I and P [%.2f]\n"
             "      --pbratio <float>       QP factor between P and B [%.2f]\n"
129
             "      --chroma-qp-offset <integer>  QP difference between chroma and luma [%d]\n"
Laurent Aimar's avatar
Laurent Aimar committed
130
             "\n"
131
             "  -p, --pass <1|2>            Enable 2 pass ratecontrol\n"
132 133 134 135 136
             "      --stats <string>        Filename for 2 pass stats [\"%s\"]\n"
             "      --rceq <string>         Ratecontrol equation [\"%s\"]\n"
             "      --qcomp <float>         0.0 => CBR, 1.0 => CQP [%.2f]\n"
             "      --cplxblur <float>      reduce fluctuations in QP (before curve compression) [%.1f]\n"
             "      --qblur <float>         reduce fluctuations in QP (after curve compression) [%.1f]\n"
137 138
             "\n"

139
             "  -A, --analyse <string>      Analyse options: [\"i4x4|psub16x16|bsub16x16\"]\n"
Laurent Aimar's avatar
Laurent Aimar committed
140
             "                                  - i4x4\n"
141
             "                                  - psub16x16, psub8x8, bsub16x16\n"
Laurent Aimar's avatar
Laurent Aimar committed
142
             "                                  - none, all\n"
143 144
             "      --direct <string>       Direct MV prediction mode [\"temporal\"]\n"
             "                                  - none, spatial, temporal\n"
145
             "  -w, --weightb               Weighted prediction for B-frames\n"
146
             "  -m, --subme <integer>       Subpixel motion estimation quality: 1=fast, 5=best. [%d]\n"
Laurent Aimar's avatar
Laurent Aimar committed
147
             "\n"
148
             "      --level <integer>       Specify IDC level\n"
Laurent Aimar's avatar
Laurent Aimar committed
149
             "  -s, --sar width:height      Specify Sample Aspect Ratio\n"
Loren Merritt's avatar
Loren Merritt committed
150
             "      --fps <float|rational>  Specify framerate\n"
151
             "      --frames <integer>      Maximum number of frames to encode\n"
Laurent Aimar's avatar
Laurent Aimar committed
152 153 154
             "  -o, --output                Specify output file\n"
             "\n"
             "      --no-asm                Disable any CPU optims\n"
155 156
             "      --no-psnr               Disable PSNR computaion\n"
             "      --quiet                 Quiet Mode\n"
157
             "  -v, --verbose               Print stats for each frame\n"
Laurent Aimar's avatar
Laurent Aimar committed
158
             "\n",
159
            X264_BUILD,
160 161
            defaults->i_keyint_max,
            defaults->i_keyint_min,
162 163
            defaults->i_scenecut_threshold,
            defaults->i_bframe,
164
            defaults->i_bframe_bias,
165 166 167 168 169 170 171 172 173 174 175
            defaults->i_frame_reference,
            defaults->i_deblocking_filter_alphac0,
            defaults->rc.i_qp_constant,
            defaults->rc.i_qp_min,
            defaults->rc.i_qp_max,
            defaults->rc.i_qp_step,
            defaults->rc.i_rc_sens,
            defaults->rc.i_rc_buffer_size,
            defaults->rc.i_rc_init_buffer,
            defaults->rc.f_ip_factor,
            defaults->rc.f_pb_factor,
176
            defaults->analyse.i_chroma_qp_offset,
177 178 179 180 181 182
            defaults->rc.psz_stat_out,
            defaults->rc.psz_rc_eq,
            defaults->rc.f_qcompress,
            defaults->rc.f_complexity_blur,
            defaults->rc.f_qblur,
            defaults->analyse.i_subpel_refine
Laurent Aimar's avatar
Laurent Aimar committed
183 184 185 186 187 188 189 190 191 192 193
           );
}

/*****************************************************************************
 * Parse:
 *****************************************************************************/
static int  Parse( int argc, char **argv,
                   x264_param_t  *param,
                   FILE **p_fin, FILE **p_fout, int *pb_decompress )
{
    char *psz_filename = NULL;
194
    x264_param_t defaults = *param;
Laurent Aimar's avatar
Laurent Aimar committed
195 196 197 198 199 200 201 202 203 204 205

    /* Default output */
    *p_fout = stdout;
    *p_fin  = stdin;
    *pb_decompress = 0;

    /* Parse command line options */
    opterr = 0; // no error message
    for( ;; )
    {
        int long_options_index;
206 207 208 209 210 211 212 213
#define OPT_QPMIN 256
#define OPT_QPMAX 257
#define OPT_QPSTEP 258
#define OPT_RCSENS 259
#define OPT_IPRATIO 260
#define OPT_PBRATIO 261
#define OPT_RCBUF 262
#define OPT_RCIBUF 263
214 215 216
#define OPT_RCSTATS 264
#define OPT_RCEQ 265
#define OPT_QCOMP 266
217 218
#define OPT_NOPSNR 267
#define OPT_QUIET 268
219
#define OPT_SCENECUT 270
Loren Merritt's avatar
Loren Merritt committed
220 221
#define OPT_QBLUR 271
#define OPT_CPLXBLUR 272
222
#define OPT_FRAMES 273
Loren Merritt's avatar
Loren Merritt committed
223
#define OPT_FPS 274
224
#define OPT_DIRECT 275
225
#define OPT_LEVEL 276
226 227
#define OPT_NOBADAPT 277
#define OPT_BBIAS 278
228
#define OPT_BPYRAMID 279
229
#define OPT_CHROMA_QP 280
230

Laurent Aimar's avatar
Laurent Aimar committed
231 232 233 234 235
        static struct option long_options[] =
        {
            { "help",    no_argument,       NULL, 'h' },
            { "bitrate", required_argument, NULL, 'B' },
            { "bframe",  required_argument, NULL, 'b' },
236 237
            { "no-b-adapt", no_argument,    NULL, OPT_NOBADAPT },
            { "b-bias",  required_argument, NULL, OPT_BBIAS },
238
            { "b-pyramid", no_argument,     NULL, OPT_BPYRAMID },
239 240
            { "min-keyint",required_argument,NULL,'i' },
            { "keyint",  required_argument, NULL, 'I' },
241
            { "scenecut",required_argument, NULL, OPT_SCENECUT },
Laurent Aimar's avatar
Laurent Aimar committed
242 243 244 245
            { "nf",      no_argument,       NULL, 'n' },
            { "filter",  required_argument, NULL, 'f' },
            { "cabac",   no_argument,       NULL, 'c' },
            { "qp",      required_argument, NULL, 'q' },
246 247 248
            { "qpmin",   required_argument, NULL, OPT_QPMIN },
            { "qpmax",   required_argument, NULL, OPT_QPMAX },
            { "qpstep",  required_argument, NULL, OPT_QPSTEP },
Laurent Aimar's avatar
Laurent Aimar committed
249 250 251
            { "ref",     required_argument, NULL, 'r' },
            { "no-asm",  no_argument,       NULL, 'C' },
            { "sar",     required_argument, NULL, 's' },
Loren Merritt's avatar
Loren Merritt committed
252
            { "fps",     required_argument, NULL, OPT_FPS },
253
            { "frames",  required_argument, NULL, OPT_FRAMES },
Laurent Aimar's avatar
Laurent Aimar committed
254 255
            { "output",  required_argument, NULL, 'o' },
            { "analyse", required_argument, NULL, 'A' },
256
            { "direct",  required_argument, NULL, OPT_DIRECT },
257
            { "weightb", no_argument,       NULL, 'w' },
258
            { "subme",   required_argument, NULL, 'm' },
259
            { "level",   required_argument, NULL, OPT_LEVEL },
260 261
            { "rcsens",  required_argument, NULL, OPT_RCSENS },
            { "rcbuf",   required_argument, NULL, OPT_RCBUF },
262
            { "rcinitbuf",required_argument,NULL, OPT_RCIBUF },
263 264
            { "ipratio", required_argument, NULL, OPT_IPRATIO },
            { "pbratio", required_argument, NULL, OPT_PBRATIO },
265
            { "chroma-qp-offset", required_argument, NULL, OPT_CHROMA_QP },
266 267 268 269
            { "pass",    required_argument, NULL, 'p' },
            { "stats",   required_argument, NULL, OPT_RCSTATS },
            { "rceq",    required_argument, NULL, OPT_RCEQ },
            { "qcomp",   required_argument, NULL, OPT_QCOMP },
Loren Merritt's avatar
Loren Merritt committed
270 271
            { "qblur",   required_argument, NULL, OPT_QBLUR },
            { "cplxblur",required_argument, NULL, OPT_CPLXBLUR },
272 273
            { "no-psnr", no_argument,       NULL, OPT_NOPSNR },
            { "quiet",   no_argument,       NULL, OPT_QUIET },
274
            { "verbose", no_argument,       NULL, 'v' },
Laurent Aimar's avatar
Laurent Aimar committed
275 276 277 278 279
            {0, 0, 0, 0}
        };

        int c;

280
        c = getopt_long( argc, argv, "hi:I:b:r:cxB:q:nf:o:s:A:m:p:vw",
Laurent Aimar's avatar
Laurent Aimar committed
281 282 283 284 285 286 287 288 289 290
                         long_options, &long_options_index);

        if( c == -1 )
        {
            break;
        }

        switch( c )
        {
            case 'h':
291
                Help( &defaults );
Laurent Aimar's avatar
Laurent Aimar committed
292 293 294 295 296
                return -1;

            case 0:
                break;
            case 'B':
297 298
                param->rc.i_bitrate = atol( optarg );
                param->rc.b_cbr = 1;
Laurent Aimar's avatar
Laurent Aimar committed
299 300 301 302
                break;
            case 'b':
                param->i_bframe = atol( optarg );
                break;
303 304 305 306 307 308
            case OPT_NOBADAPT:
                param->b_bframe_adaptive = 0;
                break;
            case OPT_BBIAS:
                param->i_bframe_bias = atol( optarg );
                break;
309 310 311
            case OPT_BPYRAMID:
                param->b_bframe_pyramid = 1;
                break;
Laurent Aimar's avatar
Laurent Aimar committed
312
            case 'i':
313
                param->i_keyint_min = atol( optarg );
Laurent Aimar's avatar
Laurent Aimar committed
314 315
                break;
            case 'I':
316
                param->i_keyint_max = atol( optarg );
Laurent Aimar's avatar
Laurent Aimar committed
317
                break;
318 319 320
            case OPT_SCENECUT:
                param->i_scenecut_threshold = atol( optarg );
                break;
Laurent Aimar's avatar
Laurent Aimar committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334
            case 'n':
                param->b_deblocking_filter = 0;
                break;
            case 'f':
            {
                char *p = strchr( optarg, ':' );
                if( p )
                {
                    param->i_deblocking_filter_alphac0 = atoi( optarg );
                    param->i_deblocking_filter_beta = atoi( p );
                }
                break;
            }
            case 'q':
335
                param->rc.i_qp_constant = atoi( optarg );
Laurent Aimar's avatar
Laurent Aimar committed
336
                break;
337
            case OPT_QPMIN:
338
                param->rc.i_qp_min = atoi( optarg );
339 340
                break;
            case OPT_QPMAX:
341
                param->rc.i_qp_max = atoi( optarg );
342 343
                break;
            case OPT_QPSTEP:
344
                param->rc.i_qp_step = atoi( optarg );
345
                break;
Laurent Aimar's avatar
Laurent Aimar committed
346 347 348 349 350 351 352 353 354 355 356 357
            case 'r':
                param->i_frame_reference = atoi( optarg );
                break;
            case 'c':
                param->b_cabac = 1;
                break;
            case 'x':
                *pb_decompress = 1;
                break;
            case 'C':
                param->cpu = 0;
                break;
358 359 360
            case OPT_FRAMES:
                param->i_maxframes = atoi( optarg );
                break;
Laurent Aimar's avatar
Laurent Aimar committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
            case'o':
                if( ( *p_fout = fopen( optarg, "wb" ) ) == NULL )
                {
                    fprintf( stderr, "cannot open output file `%s'\n", optarg );
                    return -1;
                }
                break;
            case 's':
            {
                char *p = strchr( optarg, ':' );
                if( p )
                {
                    param->vui.i_sar_width = atoi( optarg );
                    param->vui.i_sar_height = atoi( p + 1 );
                }
                break;
            }
Loren Merritt's avatar
Loren Merritt committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            case OPT_FPS:
            {
                float fps;
                if( sscanf( optarg, "%d/%d", &param->i_fps_num, &param->i_fps_den ) == 2 )
                    ;
                else if( sscanf( optarg, "%f", &fps ) )
                {
                    param->i_fps_num = (int)(fps * 1000 + .5);
                    param->i_fps_den = 1000;
                }
                else
                {
                    fprintf( stderr, "bad fps `%s'\n", optarg );
                    return -1;
                }
            }
Laurent Aimar's avatar
Laurent Aimar committed
394 395 396
            case 'A':
                param->analyse.inter = 0;
                if( strstr( optarg, "none" ) )  param->analyse.inter = 0x000000;
397
                if( strstr( optarg, "all" ) )   param->analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16;
Laurent Aimar's avatar
Laurent Aimar committed
398 399 400 401

                if( strstr( optarg, "i4x4" ) )      param->analyse.inter |= X264_ANALYSE_I4x4;
                if( strstr( optarg, "psub16x16" ) ) param->analyse.inter |= X264_ANALYSE_PSUB16x16;
                if( strstr( optarg, "psub8x8" ) )   param->analyse.inter |= X264_ANALYSE_PSUB8x8;
402 403 404 405 406 407 408 409 410 411 412
                if( strstr( optarg, "bsub16x16" ) ) param->analyse.inter |= X264_ANALYSE_BSUB16x16;
                break;
            case OPT_DIRECT:
                if( strstr( optarg, "temporal" ) )
                    param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
                else if( strstr( optarg, "spatial" ) )
                    param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
                else if( strstr( optarg, "none" ) )
                    param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
                else
                    param->analyse.i_direct_mv_pred = atoi( optarg );
Laurent Aimar's avatar
Laurent Aimar committed
413
                break;
414 415 416
            case 'w':
                param->analyse.b_weighted_bipred = 1;
                break;
417
            case 'm':
418 419
                param->analyse.i_subpel_refine = atoi(optarg);
                break;
420 421 422
            case OPT_LEVEL:
                param->i_level_idc = atoi(optarg);
                break;
423
            case OPT_RCBUF:
424
                param->rc.i_rc_buffer_size = atoi(optarg);
425 426
                break;
            case OPT_RCIBUF:
427
                param->rc.i_rc_init_buffer = atoi(optarg);
428 429
                break;
            case OPT_RCSENS:
430
                param->rc.i_rc_sens = atoi(optarg);
431 432
                break;
            case OPT_IPRATIO:
433
                param->rc.f_ip_factor = atof(optarg);
434 435
                break;
            case OPT_PBRATIO:
436
                param->rc.f_pb_factor = atof(optarg);
437
                break;
438 439 440
            case OPT_CHROMA_QP:
                param->analyse.i_chroma_qp_offset = atoi(optarg);
                break;
441 442 443 444 445 446 447
            case 'p':
            {
                int i_pass = atoi(optarg);
                if( i_pass == 1 )
                    param->rc.b_stat_write = 1;
                else if( i_pass == 2 )
                    param->rc.b_stat_read = 1;
448
                else if( i_pass > 2 )
449 450 451 452 453 454 455 456 457 458 459 460 461
                    param->rc.b_stat_read =
                    param->rc.b_stat_write = 1;
                break;
            }
            case OPT_RCSTATS:
                param->rc.psz_stat_in = optarg;
                param->rc.psz_stat_out = optarg;
                break;
            case OPT_RCEQ:
                param->rc.psz_rc_eq = optarg;
               break;
            case OPT_QCOMP:
                param->rc.f_qcompress = atof(optarg);
462
                break;
Loren Merritt's avatar
Loren Merritt committed
463 464 465 466 467 468
            case OPT_QBLUR:
                param->rc.f_qblur = atof(optarg);
                break;
            case OPT_CPLXBLUR:
                param->rc.f_complexity_blur = atof(optarg);
                break;
469 470 471 472 473 474
            case OPT_NOPSNR:
                param->analyse.b_psnr = 0;
                break;
            case OPT_QUIET:
                param->i_log_level = X264_LOG_NONE;
                break;
475 476 477
            case 'v':
                param->i_log_level = X264_LOG_DEBUG;
                break;
Laurent Aimar's avatar
Laurent Aimar committed
478 479 480 481 482 483 484 485 486
            default:
                fprintf( stderr, "unknown option (%c)\n", optopt );
                return -1;
        }
    }

    /* Get the file name */
    if( optind > argc - 1 )
    {
487
        Help( &defaults );
Laurent Aimar's avatar
Laurent Aimar committed
488 489 490 491 492 493 494 495
        return -1;
    }
    psz_filename = argv[optind++];

    if( !(*pb_decompress) )
    {
        if( optind > argc - 1 )
        {
496
            char *psz;
Laurent Aimar's avatar
Laurent Aimar committed
497
            /* try to parse the file name */
498
            for( psz = psz_filename; *psz; psz++ )
Laurent Aimar's avatar
Laurent Aimar committed
499
            {
500 501
                if( *psz >= '0' && *psz <= '9'
                    && sscanf( psz, "%ux%u", &param->i_width, &param->i_height ) == 2 )
Laurent Aimar's avatar
Laurent Aimar committed
502
                {
503
                    fprintf( stderr, "x264: file name gives %dx%d\n", param->i_width, param->i_height );
Laurent Aimar's avatar
Laurent Aimar committed
504 505 506 507 508 509
                    break;
                }
            }
        }
        else
        {
510
            sscanf( argv[optind++], "%ux%u", &param->i_width, &param->i_height );
Laurent Aimar's avatar
Laurent Aimar committed
511 512
        }

513 514 515 516 517
        if( !param->i_width || !param->i_height )
        {
            Help( &defaults );
            return -1;
        }
Laurent Aimar's avatar
Laurent Aimar committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    }

    /* open the input */
    if( !strcmp( psz_filename, "-" ) )
    {
        *p_fin = stdin;
        optind++;
    }
    else if( ( *p_fin = fopen( psz_filename, "rb" ) ) == NULL )
    {
        fprintf( stderr, "could not open input file '%s'\n", psz_filename );
        return -1;
    }

    return 0;
}

/*****************************************************************************
 * Decode:
 *****************************************************************************/
static int  Decode( x264_param_t  *param, FILE *fh26l, FILE *fout )
{
    fprintf( stderr, "decompressor not working (help is welcome)\n" );
    return -1;
#if 0
    x264_nal_t nal;
    int i_data;
    int b_eof;

    //param.cpu = 0;
    if( ( h = x264_decoder_open( &param ) ) == NULL )
    {
        fprintf( stderr, "x264_decoder_open failed\n" );
        return -1;
    }

    i_start = x264_mdate();
    b_eof = 0;
    i_frame = 0;
    i_data  = 0;
    nal.p_payload = malloc( DATA_MAX );

    while( !i_ctrl_c )
    {
        uint8_t *p, *p_next, *end;
        int i_size;
        /* fill buffer */
        if( i_data < DATA_MAX && !b_eof )
        {
            int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fh26l );
            if( i_read <= 0 )
            {
                b_eof = 1;
            }
            else
            {
                i_data += i_read;
            }
        }

        if( i_data < 3 )
        {
            break;
        }

        end = &data[i_data];

        /* extract one nal */
        p = &data[0];
        while( p < end - 3 )
        {
            if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
            {
                break;
            }
            p++;
        }

        if( p >= end - 3 )
        {
            fprintf( stderr, "garbage (i_data = %d)\n", i_data );
            i_data = 0;
            continue;
        }

        p_next = p + 3;
        while( p_next < end - 3 )
        {
            if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
            {
                break;
            }
            p_next++;
        }

        if( p_next == end - 3 && i_data < DATA_MAX )
        {
            p_next = end;
        }

        /* decode this nal */
        i_size = p_next - p - 3;
        if( i_size <= 0 )
        {
            if( b_eof )
            {
                break;
            }
            fprintf( stderr, "nal too large (FIXME) ?\n" );
            i_data = 0;
            continue;
        }

        x264_nal_decode( &nal, p +3, i_size );

        /* decode the content of the nal */
        x264_decoder_decode( h, &pic, &nal );

        if( pic != NULL )
        {
            int i;

            i_frame++;

            for( i = 0; i < pic->i_plane;i++ )
            {
                int i_line;
                int i_div;

                i_div = i==0 ? 1 : 2;
                for( i_line = 0; i_line < pic->i_height/i_div; i_line++ )
                {
                    fwrite( pic->plane[i]+i_line*pic->i_stride[i], 1, pic->i_width/i_div, fout );
                }
            }
        }

        memmove( &data[0], p_next, end - p_next );
        i_data -= p_next - &data[0];
    }

    i_end = x264_mdate();
    free( nal.p_payload );
    fprintf( stderr, "\n" );

    x264_decoder_close( h );

    fclose( fh26l );
    if( fout != stdout )
    {
        fclose( fout );
    }
    if( i_frame > 0 )
    {
        double fps = (double)i_frame * (double)1000000 /
                     (double)( i_end - i_start );
        fprintf( stderr, "decoded %d frames %ffps\n", i_frame, fps );
    }
#endif
}

/*****************************************************************************
 * Encode:
 *****************************************************************************/
static int  Encode( x264_param_t  *param, FILE *fyuv, FILE *fout )
{
    x264_t *h;
    x264_picture_t pic;

    int     i_frame, i_frame_total;
    int64_t i_start, i_end;
    int64_t i_file;

    i_frame_total = 0;
    if( !fseek( fyuv, 0, SEEK_END ) )
    {
        int64_t i_size = ftell( fyuv );
        fseek( fyuv, 0, SEEK_SET );
        i_frame_total = (int)(i_size / ( param->i_width * param->i_height * 3 / 2 ));
    }

    if( ( h = x264_encoder_open( param ) ) == NULL )
    {
        fprintf( stderr, "x264_encoder_open failed\n" );
        return -1;
    }

    /* Create a new pic */
    x264_picture_alloc( &pic, X264_CSP_I420, param->i_width, param->i_height );

    i_start = x264_mdate();
    for( i_frame = 0, i_file = 0; i_ctrl_c == 0 ; i_frame++ )
    {
        int         i_nal;
        x264_nal_t  *nal;

        int         i;

716 717 718
        if (param->i_maxframes!=0 && i_frame>=param->i_maxframes)
            break;

Laurent Aimar's avatar
Laurent Aimar committed
719 720 721 722 723 724 725 726 727 728
        /* read a frame */
        if( fread( pic.img.plane[0], 1, param->i_width * param->i_height, fyuv ) <= 0 ||
            fread( pic.img.plane[1], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 ||
            fread( pic.img.plane[2], 1, param->i_width * param->i_height / 4, fyuv ) <= 0 )
        {
            break;
        }

        /* Do not force any parameters */
        pic.i_type = X264_TYPE_AUTO;
729
        if( x264_encoder_encode( h, &nal, &i_nal, &pic, &pic ) < 0 )
Laurent Aimar's avatar
Laurent Aimar committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        {
            fprintf( stderr, "x264_encoder_encode failed\n" );
        }

        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 += fwrite( data, 1, i_size, fout );
            }
            else if( i_size < 0 )
            {
                fprintf( stderr,
                         "need to increase buffer size (size=%d)\n", -i_size );
            }
        }
    }
    i_end = x264_mdate();
    x264_picture_clean( &pic );
    x264_encoder_close( h );
    fprintf( stderr, "\n" );

    fclose( fyuv );
    if( fout != stdout )
    {
        fclose( fout );
    }

    if( i_frame > 0 )
    {
        double fps = (double)i_frame * (double)1000000 /
                     (double)( i_end - i_start );

Loren Merritt's avatar
Loren Merritt committed
767
        fprintf( stderr, "encoded %d frames, %.2f fps, %.2f kb/s\n", i_frame, fps, (double) i_file * 8 * param->i_fps_num / ( param->i_fps_den * i_frame * 1000 ) );
Laurent Aimar's avatar
Laurent Aimar committed
768 769 770 771 772 773
    }

    return 0;
}