OSDN Git Service

Formatting: Remove a lot of trailing whitespace.
[handbrake-jp/handbrake-jp-git.git] / test / test.c
1 /* $Id: test.c,v 1.82 2005/11/19 08:25:54 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <signal.h>
8 #include <getopt.h>
9 #include <sys/time.h>
10 #include <time.h>
11 #include <unistd.h>
12
13 #include "hb.h"
14 #include "parsecsv.h"
15
16 /* Options */
17 static int    debug       = HB_DEBUG_NONE;
18 static int    update      = 0;
19 static char * input       = NULL;
20 static char * output      = NULL;
21 static char * format      = NULL;
22 static int    titleindex  = 1;
23 static int    longest_title = 0;
24 static int    subtitle_scan = 0;
25 static int    subtitle_force = 0;
26 static char * native_language = NULL;
27 static int    twoPass     = 0;
28 static int    deinterlace           = 0;
29 static char * deinterlace_opt       = 0;
30 static int    deblock               = 0;
31 static char * deblock_opt           = 0;
32 static int    denoise               = 0;
33 static char * denoise_opt           = 0;
34 static int    detelecine            = 0;
35 static char * detelecine_opt        = 0;
36 static int    grayscale   = 0;
37 static int    vcodec      = HB_VCODEC_FFMPEG;
38 static int    h264_13     = 0;
39 static int    h264_30     = 0;
40 static char * audios      = NULL;
41 static int    audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
42 static float  dynamic_range_compression = 0;
43 static int    sub         = 0;
44 static int    width       = 0;
45 static int    height      = 0;
46 static int    crop[4]     = { -1,-1,-1,-1 };
47 static int    cpu         = 0;
48 static int    vrate       = 0;
49 static int    arate       = 0;
50 static float  vquality    = -1.0;
51 static int    vbitrate    = 0;
52 static int    size        = 0;
53 static int    abitrate    = 0;
54 static int    mux         = 0;
55 static int    acodec      = 0;
56 static int    pixelratio  = 0;
57 static int    loosePixelratio = 0;
58 static int    modulus       = 0;
59 static int    chapter_start = 0;
60 static int    chapter_end   = 0;
61 static int    chapter_markers = 0;
62 static char * marker_file   = NULL;
63 static int        crf                   = 1;
64 static char       *x264opts             = NULL;
65 static char       *x264opts2    = NULL;
66 static int        maxHeight             = 0;
67 static int        maxWidth              = 0;
68 static int    turbo_opts_enabled = 0;
69 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
70 static int    largeFileSize = 0;
71 static int    preset        = 0;
72 static char * preset_name   = 0;
73 static int    vfr           = 0;
74 static int    mp4_optimize  = 0;
75 static int    ipod_atom     = 0;
76
77 /* Exit cleanly on Ctrl-C */
78 static volatile int die = 0;
79 static void SigHandler( int );
80
81 /* Utils */
82 static void ShowCommands();
83 static void ShowHelp();
84 static void ShowPresets();
85
86 static int  ParseOptions( int argc, char ** argv );
87 static int  CheckOptions( int argc, char ** argv );
88 static int  HandleEvents( hb_handle_t * h );
89
90 /* Only print the "Muxing..." message once */
91 static int show_mux_warning = 1;
92
93 /****************************************************************************
94  * hb_error_handler
95  *
96  * When using the CLI just display using hb_log as we always did in the past
97  * make sure that we prefix with a nice ERROR message to catch peoples eyes.
98  ****************************************************************************/
99 static void hb_cli_error_handler ( const char *errmsg )
100 {
101     fprintf( stderr, "ERROR: %s", errmsg );
102 }
103
104 int main( int argc, char ** argv )
105 {
106     hb_handle_t * h;
107     int           build;
108     char        * version;
109
110     /* Parse command line */
111     if( ParseOptions( argc, argv ) ||
112         CheckOptions( argc, argv ) )
113     {
114         return 1;
115     }
116
117     /* Register our error handler */
118     hb_register_error_handler(&hb_cli_error_handler);
119
120     /* Init libhb */
121     h = hb_init( debug, update );
122
123     /* Show version */
124     fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
125              hb_get_version( h ), hb_get_build( h ) );
126
127     /* Check for update */
128     if( update )
129     {
130         if( ( build = hb_check_update( h, &version ) ) > -1 )
131         {
132             fprintf( stderr, "You are using an old version of "
133                      "HandBrake.\nLatest is %s (build %d).\n", version,
134                      build );
135         }
136         else
137         {
138             fprintf( stderr, "Your version of HandBrake is up to "
139                      "date.\n" );
140         }
141         hb_close( &h );
142         return 0;
143     }
144
145     /* Geeky */
146     fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
147              hb_get_cpu_count( h ) > 1 ? "s" : "" );
148     if( cpu )
149     {
150         fprintf( stderr, "Forcing %d CPU%s\n", cpu,
151                  cpu > 1 ? "s" : "" );
152         hb_set_cpu_count( h, cpu );
153     }
154
155     /* Exit ASAP on Ctrl-C */
156     signal( SIGINT, SigHandler );
157
158     /* Feed libhb with a DVD to scan */
159     fprintf( stderr, "Opening %s...\n", input );
160
161     if (longest_title) {
162         /*
163          * We need to scan for all the titles in order to find the longest
164          */
165         titleindex = 0;
166     }
167     hb_scan( h, input, titleindex );
168
169     /* Wait... */
170     while( !die )
171     {
172 #if !defined(SYS_BEOS)
173         fd_set         fds;
174         struct timeval tv;
175         int            ret;
176         char           buf[257];
177
178         tv.tv_sec  = 0;
179         tv.tv_usec = 100000;
180
181         FD_ZERO( &fds );
182         FD_SET( STDIN_FILENO, &fds );
183         ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
184
185         if( ret > 0 )
186         {
187             int size = 0;
188
189             while( size < 256 &&
190                    read( STDIN_FILENO, &buf[size], 1 ) > 0 )
191             {
192                 if( buf[size] == '\n' )
193                 {
194                     break;
195                 }
196                 size++;
197             }
198
199             if( size >= 256 || buf[size] == '\n' )
200             {
201                 switch( buf[0] )
202                 {
203                     case 'q':
204                         fprintf( stdout, "\nEncoding Quit by user command\n" );
205                         die = 1;
206                         break;
207                     case 'p':
208                         fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
209                         hb_pause( h );
210                         break;
211                     case 'r':
212                         hb_resume( h );
213                         break;
214                     case 'h':
215                         ShowCommands();
216                         break;
217                 }
218             }
219         }
220         hb_snooze( 200 );
221 #else
222         hb_snooze( 200 );
223 #endif
224
225         HandleEvents( h );
226     }
227
228     /* Clean up */
229     hb_close( &h );
230     if( input )  free( input );
231     if( output ) free( output );
232     if( format ) free( format );
233     if( audios ) free( audios );
234     if (native_language ) free (native_language );
235         if( x264opts ) free (x264opts );
236         if( x264opts2 ) free (x264opts2 );
237     if (preset_name) free (preset_name);
238
239     fprintf( stderr, "HandBrake has exited.\n" );
240
241     return 0;
242 }
243
244 static void ShowCommands()
245 {
246     fprintf( stdout, "\nCommands:\n" );
247     fprintf( stdout, " [h]elp    Show this message\n" );
248     fprintf( stdout, " [q]uit    Exit HandBrakeCLI\n" );
249     fprintf( stdout, " [p]ause   Pause encoding\n" );
250     fprintf( stdout, " [r]esume  Resume encoding\n" );
251 }
252
253 static void PrintTitleInfo( hb_title_t * title )
254 {
255     hb_chapter_t  * chapter;
256     hb_audio_t    * audio;
257     hb_subtitle_t * subtitle;
258     int i;
259
260     fprintf( stderr, "+ title %d:\n", title->index );
261     fprintf( stderr, "  + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
262              title->vts, title->ttn, title->cell_start, title->cell_end,
263              title->block_count );
264     fprintf( stderr, "  + duration: %02d:%02d:%02d\n",
265              title->hours, title->minutes, title->seconds );
266     fprintf( stderr, "  + size: %dx%d, aspect: %.2f, %.3f fps\n",
267              title->width, title->height,
268              (float) title->aspect / HB_ASPECT_BASE,
269              (float) title->rate / title->rate_base );
270     fprintf( stderr, "  + autocrop: %d/%d/%d/%d\n", title->crop[0],
271              title->crop[1], title->crop[2], title->crop[3] );
272     fprintf( stderr, "  + chapters:\n" );
273     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
274     {
275         chapter = hb_list_item( title->list_chapter, i );
276         fprintf( stderr, "    + %d: cells %d->%d, %d blocks, duration "
277                  "%02d:%02d:%02d\n", chapter->index,
278                  chapter->cell_start, chapter->cell_end,
279                  chapter->block_count, chapter->hours, chapter->minutes,
280                  chapter->seconds );
281     }
282     fprintf( stderr, "  + audio tracks:\n" );
283     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
284     {
285         audio = hb_list_item( title->list_audio, i );
286         if( ( audio->codec & HB_ACODEC_AC3 ) || ( audio->codec & HB_ACODEC_DCA) )
287         {
288             fprintf( stderr, "    + %d, %s, %dHz, %dbps\n", i + 1,
289                      audio->lang, audio->rate, audio->bitrate );
290         }
291         else
292         {
293             fprintf( stderr, "    + %d, %s\n", i + 1, audio->lang );
294         }
295     }
296     fprintf( stderr, "  + subtitle tracks:\n" );
297     for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
298     {
299         subtitle = hb_list_item( title->list_subtitle, i );
300         fprintf( stderr, "    + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
301             subtitle->iso639_2);
302     }
303 }
304
305 static int HandleEvents( hb_handle_t * h )
306 {
307     hb_state_t s;
308     hb_get_state( h, &s );
309     switch( s.state )
310     {
311         case HB_STATE_IDLE:
312             /* Nothing to do */
313             break;
314
315 #define p s.param.scanning
316         case HB_STATE_SCANNING:
317             /* Show what title is currently being scanned */
318             fprintf( stderr, "Scanning title %d", p.title_cur );
319             if( !titleindex )
320                 fprintf( stderr, " of %d", p.title_count );
321             fprintf( stderr, "...\n" );
322             break;
323 #undef p
324
325         case HB_STATE_SCANDONE:
326         {
327             hb_list_t  * list;
328             hb_title_t * title;
329             hb_job_t   * job;
330
331             list = hb_get_titles( h );
332
333             if( !hb_list_count( list ) )
334             {
335                 /* No valid title, stop right there */
336                 fprintf( stderr, "No title found.\n" );
337                 die = 1;
338                 break;
339             }
340             if( longest_title )
341             {
342                 int i;
343                 int longest_title_idx=0;
344                 int longest_title_pos=-1;
345                 int longest_title_time=0;
346                 int title_time;
347
348                 fprintf( stderr, "Searching for longest title...\n" );
349
350                 for( i = 0; i < hb_list_count( list ); i++ )
351                 {
352                     title = hb_list_item( list, i );
353                     title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
354                     fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
355                              i, title->index, title_time );
356                     if( longest_title_time < title_time )
357                     {
358                         longest_title_time = title_time;
359                         longest_title_pos = i;
360                         longest_title_idx = title->index;
361                     }
362                 }
363                 if( longest_title_pos == -1 )
364                 {
365                     fprintf( stderr, "No longest title found.\n" );
366                     die = 1;
367                     break;
368                 }
369                 titleindex = longest_title_idx;
370                 fprintf( stderr, "Found longest title, setting title to %d\n",
371                          longest_title_idx);
372
373                 title = hb_list_item( list, longest_title_pos);
374             } else {
375                 title = hb_list_item( list, 0 );
376             }
377
378             if( !titleindex )
379             {
380                 /* Scan-only mode, print infos and exit */
381                 int i;
382                 for( i = 0; i < hb_list_count( list ); i++ )
383                 {
384                     title = hb_list_item( list, i );
385                     PrintTitleInfo( title );
386                 }
387                 die = 1;
388                 break;
389             }
390
391             /* Set job settings */
392             job   = title->job;
393
394             PrintTitleInfo( title );
395
396             if( chapter_start && chapter_end )
397             {
398                 job->chapter_start = MAX( job->chapter_start,
399                                           chapter_start );
400                 job->chapter_end   = MIN( job->chapter_end,
401                                           chapter_end );
402                 job->chapter_end   = MAX( job->chapter_start,
403                                           job->chapter_end );
404             }
405
406             if (preset)
407             {
408                 fprintf( stderr, "+ Using preset: %s", preset_name);
409
410                 if (!strcmp(preset_name, "Animation"))
411                 {
412                     mux = HB_MUX_MKV;
413                     vcodec = HB_VCODEC_X264;
414                     job->vbitrate = 1000;
415                     job->abitrate = 160;
416                     job->arate = 48000;
417                     acodec = HB_ACODEC_FAAC;
418                     x264opts = strdup("ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2");
419                     deinterlace = 1;
420                     deinterlace_opt = "0";
421                     job->chapter_markers = 1;
422                     pixelratio = 1;
423                     twoPass = 1;
424                     turbo_opts_enabled = 1;
425                 }
426
427                 if (!strcmp(preset_name, "AppleTV"))
428                 {
429                     mux = HB_MUX_MP4;
430                     job->largeFileSize = 1;
431                     vcodec = HB_VCODEC_X264;
432                     job->vbitrate = 2500;
433                     job->abitrate = 160;
434                     job->arate = 48000;
435                     acodec = HB_ACODEC_FAAC;
436                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII_AC3;
437                     arate = 48000;
438                     x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
439                     job->chapter_markers = 1;
440                     pixelratio = 1;
441                 }
442
443                 if (!strcmp(preset_name, "Bedlam"))
444                 {
445                     mux = HB_MUX_MKV;
446                     vcodec = HB_VCODEC_X264;
447                     job->vbitrate = 1800;
448                     acodec = HB_ACODEC_AC3;
449                     x264opts = strdup("ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1");
450                     job->chapter_markers = 1;
451                     pixelratio = 1;
452                     twoPass = 1;
453                     turbo_opts_enabled = 1;
454                 }
455
456                 if (!strcmp(preset_name, "Blind"))
457                 {
458                     mux = HB_MUX_MP4;
459                     job->vbitrate = 512;
460                     job->abitrate = 128;
461                     job->arate = 48000;
462                     acodec = HB_ACODEC_FAAC;
463                     job->width = 512;
464                     job->chapter_markers = 1;
465                 }
466
467                 if (!strcmp(preset_name, "Broke"))
468                 {
469                     mux = HB_MUX_MP4;
470                     vcodec = HB_VCODEC_X264;
471                     size = 695;
472                     job->abitrate = 128;
473                     job->arate = 48000;
474                     acodec = HB_ACODEC_FAAC;
475                     job->width = 640;
476                     x264opts = strdup("ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip");
477                     job->chapter_markers = 1;
478                     twoPass = 1;
479                     turbo_opts_enabled = 1;
480                 }
481
482                 if (!strcmp(preset_name, "Classic"))
483                 {
484                     mux = HB_MUX_MP4;
485                     job->vbitrate = 1000;
486                     job->abitrate = 160;
487                     job->arate = 48000;
488                     acodec = HB_ACODEC_FAAC;
489                 }
490
491                 if (!strcmp(preset_name, "Constant Quality Rate"))
492                 {
493                     mux = HB_MUX_MKV;
494                     vcodec = HB_VCODEC_X264;
495                     job->vquality = 0.64709997177124023;
496                     job->crf = 1;
497                     acodec = HB_ACODEC_AC3;
498                     x264opts = strdup("ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh");
499                     job->chapter_markers = 1;
500                     pixelratio = 1;
501                 }
502
503                 if (!strcmp(preset_name, "Deux Six Quatre"))
504                 {
505                     mux = HB_MUX_MKV;
506                     vcodec = HB_VCODEC_X264;
507                     job->vbitrate = 1600;
508                     acodec = HB_ACODEC_AC3;
509                     x264opts = strdup("ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip");
510                     job->chapter_markers = 1;
511                     pixelratio = 1;
512                     twoPass = 1;
513                     turbo_opts_enabled = 1;
514                 }
515
516                 if (!strcmp(preset_name, "Film"))
517                 {
518                     mux = HB_MUX_MKV;
519                     vcodec = HB_VCODEC_X264;
520                     job->vbitrate = 1800;
521                     acodec = HB_ACODEC_AC3;
522                     x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip");
523                     job->chapter_markers = 1;
524                     pixelratio = 1;
525                     twoPass = 1;
526                     turbo_opts_enabled = 1;
527                 }
528
529                 if (!strcmp(preset_name, "iPhone / iPod Touch"))
530                 {
531                     mux = HB_MUX_MP4;
532                     job->ipod_atom = 1;
533                     vcodec = HB_VCODEC_X264;
534                     job->vbitrate = 960;
535                     job->abitrate = 128;
536                     job->arate = 48000;
537                     acodec = HB_ACODEC_FAAC;
538                     job->width = 480;
539                     x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
540                     job->chapter_markers = 1;
541                 }
542
543                 if (!strcmp(preset_name, "iPod High-Rez"))
544                 {
545                     mux = HB_MUX_MP4;
546                     job->ipod_atom = 1;
547                     vcodec = HB_VCODEC_X264;
548                     job->vbitrate = 1500;
549                     job->abitrate = 160;
550                     job->arate = 48000;
551                     acodec = HB_ACODEC_FAAC;
552                     job->width = 640;
553                     x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
554                     job->chapter_markers = 1;
555                 }
556
557                 if (!strcmp(preset_name, "iPod Low-Rez"))
558                 {
559                     mux = HB_MUX_MP4;
560                     job->ipod_atom = 1;
561                     vcodec = HB_VCODEC_X264;
562                     job->vbitrate = 700;
563                     job->abitrate = 160;
564                     job->arate = 48000;
565                     acodec = HB_ACODEC_FAAC;
566                     job->width = 320;
567                     x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
568                     job->chapter_markers = 1;
569                 }
570
571                 if (!strcmp(preset_name, "Normal"))
572                 {
573                     mux = HB_MUX_MP4;
574                     vcodec = HB_VCODEC_X264;
575                     job->vbitrate = 1500;
576                     job->abitrate = 160;
577                     job->arate = 48000;
578                     acodec = HB_ACODEC_FAAC;
579                     x264opts = strdup("ref=2:bframes=2:subme=5:me=umh");
580                     job->chapter_markers = 1;
581                     pixelratio = 1;
582                     twoPass = 1;
583                     turbo_opts_enabled = 1;
584                 }
585
586                 if (!strcmp(preset_name, "PS3"))
587                 {
588                     mux = HB_MUX_MP4;
589                     vcodec = HB_VCODEC_X264;
590                     job->vbitrate = 2500;
591                     job->abitrate = 160;
592                     job->arate = 48000;
593                     acodec = HB_ACODEC_FAAC;
594                     x264opts = strdup("level=41:subme=5:me=umh");
595                     pixelratio = 1;
596                 }
597
598                 if (!strcmp(preset_name, "PSP"))
599                 {
600                     mux = HB_MUX_MP4;
601                     job->vbitrate = 1024;
602                     job->abitrate = 128;
603                     job->arate = 48000;
604                     acodec = HB_ACODEC_FAAC;
605                     job->width = 368;
606                     job->height = 208;
607                     job->chapter_markers = 1;
608                 }
609
610                 if (!strcmp(preset_name, "QuickTime"))
611                 {
612                     mux = HB_MUX_MP4;
613                     vcodec = HB_VCODEC_X264;
614                     job->vbitrate = 2000;
615                     job->abitrate = 160;
616                     job->arate = 48000;
617                     acodec = HB_ACODEC_FAAC;
618                     x264opts = strdup("ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip");
619                     job->chapter_markers = 1;
620                     pixelratio = 1;
621                     twoPass = 1;
622                     turbo_opts_enabled = 1;
623                 }
624
625                 if (!strcmp(preset_name, "Television"))
626                 {
627                     mux = HB_MUX_MKV;
628                     vcodec = HB_VCODEC_X264;
629                     job->vbitrate = 1300;
630                     job->abitrate = 160;
631                     job->arate = 48000;
632                     acodec = HB_ACODEC_FAAC;
633                     x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip");
634                     deinterlace = 1;
635                     deinterlace_opt = "0";
636                     denoise = 1;
637                     denoise_opt = "2:1:2:3";
638                     job->chapter_markers = 1;
639                     twoPass = 1;
640                     turbo_opts_enabled = 1;
641                 }
642
643                 if (!strcmp(preset_name, "Xbox 360"))
644                 {
645                     mux = HB_MUX_MP4;
646                     vcodec = HB_VCODEC_X264;
647                     job->vbitrate = 2000;
648                     job->abitrate = 160;
649                     job->arate = 48000;
650                     acodec = HB_ACODEC_FAAC;
651                     x264opts = strdup("level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1");
652                     pixelratio = 1;
653                 }
654             }
655
656                         if ( chapter_markers )
657                         {
658                                 job->chapter_markers = chapter_markers;
659
660                 if( marker_file != NULL )
661                 {
662                     hb_csv_file_t * file = hb_open_csv_file( marker_file );
663                     hb_csv_cell_t * cell;
664                     int row = 0;
665                     int chapter = 0;
666
667                     fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
668
669                     if( file == NULL )
670                     {
671                          fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
672                     }
673                     else
674                     {
675                         /* Parse the cells */
676                         while( NULL != ( cell = hb_read_next_cell( file ) ) )
677                         {
678                             /* We have a chapter number */
679                             if( cell->cell_col == 0 )
680                             {
681                                 row = cell->cell_row;
682                                 chapter = atoi( cell->cell_text );
683                             }
684
685                             /* We have a chapter name */
686                             if( cell->cell_col == 1 && row == cell->cell_row )
687                             {
688                                 /* If we have a valid chapter, copy the string an terminate it */
689                                 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
690                                 {
691                                     hb_chapter_t * chapter_s;
692
693                                     chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
694                                     strncpy(chapter_s->title, cell->cell_text, 1023);
695                                     chapter_s->title[1023] = '\0';
696                                 }
697                             }
698
699
700                             hb_dispose_cell( cell );
701                         }
702
703                         hb_close_csv_file( file );
704                     }
705                 }
706                         }
707
708             if( crop[0] >= 0 && crop[1] >= 0 &&
709                 crop[2] >= 0 && crop[3] >= 0 )
710             {
711                 memcpy( job->crop, crop, 4 * sizeof( int ) );
712             }
713
714             job->deinterlace = deinterlace;
715             job->grayscale   = grayscale;
716             if (loosePixelratio)
717             {
718                 job->pixel_ratio = 2;
719                 if (modulus)
720                 {
721                     job->modulus = modulus;
722                 }
723             }
724             else
725             {
726                 job->pixel_ratio = pixelratio;
727             }
728             /* Add selected filters */
729             job->filters = hb_list_init();
730             if( detelecine )
731             {
732                 hb_filter_detelecine.settings = detelecine_opt;
733                 hb_list_add( job->filters, &hb_filter_detelecine );
734             }
735             if( deinterlace )
736             {
737                 hb_filter_deinterlace.settings = deinterlace_opt;
738                 hb_list_add( job->filters, &hb_filter_deinterlace );
739             }
740             if( deblock )
741             {
742                 hb_filter_deblock.settings = deblock_opt;
743                 hb_list_add( job->filters, &hb_filter_deblock );
744             }
745             if( denoise )
746             {
747                 hb_filter_denoise.settings = denoise_opt;
748                 hb_list_add( job->filters, &hb_filter_denoise );
749             }
750
751             if( width && height )
752             {
753                 job->width  = width;
754                 job->height = height;
755             }
756             else if( width )
757             {
758                 job->width = width;
759                 hb_fix_aspect( job, HB_KEEP_WIDTH );
760             }
761             else if( height && !loosePixelratio)
762             {
763                 job->height = height;
764                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
765             }
766             else if( !width && !height && !pixelratio && !loosePixelratio )
767             {
768                 hb_fix_aspect( job, HB_KEEP_WIDTH );
769             }
770             else if (!width && loosePixelratio)
771             {
772                 /* Default to full width when one isn't specified for loose anamorphic */
773                 job->width = title->width - job->crop[2] - job->crop[3];
774                 /* The height will be thrown away in hb.c but calculate it anyway */
775                 hb_fix_aspect( job, HB_KEEP_WIDTH );
776             }
777
778             if( vquality >= 0.0 && vquality <= 1.0 )
779             {
780                 job->vquality = vquality;
781                 job->vbitrate = 0;
782             }
783             else if( vbitrate )
784             {
785                 job->vquality = -1.0;
786                 job->vbitrate = vbitrate;
787             }
788             if( vcodec )
789             {
790                 job->vcodec = vcodec;
791             }
792             if( h264_13 )
793             {
794                 job->h264_level = 13;
795             }
796                 if( h264_30 )
797                 {
798                     job->h264_level = 30;
799             }
800             if( vrate )
801             {
802                 job->vrate = 27000000;
803                 job->vrate_base = vrate;
804             }
805             if( arate )
806             {
807                 job->arate = arate;
808             }
809
810             if( audios )
811             {
812                 if( strcasecmp( audios, "none" ) )
813                 {
814                     int    audio_count = 0;
815                     char * tmp         = audios;
816                     while( *tmp )
817                     {
818                         if( *tmp < '0' || *tmp > '9' )
819                         {
820                             /* Skip non numeric char */
821                             tmp++;
822                             continue;
823                         }
824                                                 job->audio_mixdowns[audio_count] = audio_mixdown;
825                         job->audios[audio_count++] =
826                             strtol( tmp, &tmp, 0 ) - 1;
827                     }
828                     job->audios[audio_count] = -1;
829                 }
830                 else
831                 {
832                     job->audios[0] = -1;
833                 }
834             }
835                         else
836                         {
837                             /* default to the first audio track if none has been specified */
838                             job->audios[0] = 0;
839                             job->audio_mixdowns[0] = audio_mixdown;
840                         }
841
842                         if( audio_mixdown == HB_AMIXDOWN_DOLBYPLII_AC3)
843                         {
844                int i;
845                for( i = 3 ; i > 0; i--)
846                {
847                    job->audios[i*2+1] = job->audios[i];
848                    job->audios[i*2] = job->audios[i];
849                    if(job->audios[i] != -1  )
850                    {
851                        job->audio_mixdowns[i*2+1] = HB_AMIXDOWN_AC3;
852                        job->audio_mixdowns[i*2] = HB_AMIXDOWN_DOLBYPLII;
853                    }
854                }
855
856                job->audios[1] = job->audios[0];
857                job->audio_mixdowns[1] = HB_AMIXDOWN_AC3;
858                job->audio_mixdowns[0] = HB_AMIXDOWN_DOLBYPLII;
859             }
860
861             if( abitrate )
862             {
863                 job->abitrate = abitrate;
864             }
865             if( acodec )
866             {
867                 job->acodec = acodec;
868             }
869             if ( dynamic_range_compression )
870             {
871                 job->dynamic_range_compression = dynamic_range_compression;
872             }
873
874             if( size )
875             {
876                 job->vbitrate = hb_calc_bitrate( job, size );
877                 fprintf( stderr, "Calculated bitrate: %d kbps\n",
878                          job->vbitrate );
879             }
880
881             if( sub )
882             {
883                 job->subtitle = sub - 1;
884             }
885
886             if( native_language )
887             {
888                 job->native_language = strdup( native_language );
889             }
890
891             if( job->mux )
892             {
893                 job->mux = mux;
894             }
895
896             if ( largeFileSize )
897             {
898                 job->largeFileSize = 1;
899             }
900             if ( mp4_optimize )
901             {
902                 job->mp4_optimize = 1;
903             }
904             if ( ipod_atom )
905             {
906                 job->ipod_atom = 1;
907             }
908
909             job->file = strdup( output );
910
911             if( crf )
912             {
913                 job->crf = 1;
914             }
915
916             if( x264opts != NULL && *x264opts != '\0' )
917             {
918                 job->x264opts = x264opts;
919             }
920             else /*avoids a bus error crash when options aren't specified*/
921             {
922                 job->x264opts =  NULL;
923             }
924             if (maxWidth)
925                 job->maxWidth = maxWidth;
926             if (maxHeight)
927                 job->maxHeight = maxHeight;
928
929             if (vfr)
930                 job->vfr = 1;
931
932             if( subtitle_force )
933             {
934                 job->subtitle_force = subtitle_force;
935             }
936
937             if( subtitle_scan )
938             {
939                 char *x264opts_tmp;
940
941                 /*
942                  * When subtitle scan is enabled do a fast pre-scan job
943                  * which will determine which subtitles to enable, if any.
944                  */
945                 job->pass = -1;
946
947                 x264opts_tmp = job->x264opts;
948
949                 job->x264opts = NULL;
950
951                 job->indepth_scan = subtitle_scan;
952                 fprintf( stderr, "Subtitle Scan Enabled - enabling "
953                          "subtitles if found for foreign language segments\n");
954                 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
955                 *(job->select_subtitle) = NULL;
956
957                 /*
958                  * Add the pre-scan job
959                  */
960                 hb_add( h, job );
961
962                 job->x264opts = x264opts_tmp;
963             }
964
965             if( twoPass )
966             {
967                 /*
968                  * If subtitle_scan is enabled then only turn it on
969                  * for the first pass and then off again for the
970                  * second.
971                  */
972                 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
973
974                 job->select_subtitle = NULL;
975
976                 job->pass = 1;
977
978                 job->indepth_scan = 0;
979
980                 if (x264opts)
981                 {
982                     x264opts2 = strdup(x264opts);
983                 }
984
985                 /*
986                  * If turbo options have been selected then append them
987                  * to the x264opts now (size includes one ':' and the '\0')
988                  */
989                 if( turbo_opts_enabled )
990                 {
991                     int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
992                     char *tmp_x264opts;
993
994                     tmp_x264opts = malloc(size * sizeof(char));
995                     if( x264opts )
996                     {
997                         snprintf( tmp_x264opts, size, "%s:%s",
998                                   x264opts, turbo_opts );
999                         free( x264opts );
1000                     } else {
1001                         /*
1002                          * No x264opts to modify, but apply the turbo options
1003                          * anyway as they may be modifying defaults
1004                          */
1005                         snprintf( tmp_x264opts, size, "%s",
1006                                   turbo_opts );
1007                     }
1008                     x264opts = tmp_x264opts;
1009
1010                     fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1011                              x264opts );
1012
1013                     job->x264opts = x264opts;
1014                 }
1015                 hb_add( h, job );
1016
1017                 job->select_subtitle = subtitle_tmp;
1018
1019                 job->pass = 2;
1020                 /*
1021                  * On the second pass we turn off subtitle scan so that we
1022                  * can actually encode using any subtitles that were auto
1023                  * selected in the first pass (using the whacky select-subtitle
1024                  * attribute of the job).
1025                  */
1026                 job->indepth_scan = 0;
1027
1028                 job->x264opts = x264opts2;
1029
1030                 hb_add( h, job );
1031             }
1032             else
1033             {
1034                 /*
1035                  * Turn on subtitle scan if requested, note that this option
1036                  * precludes encoding of any actual subtitles.
1037                  */
1038
1039                 job->indepth_scan = 0;
1040                 job->pass = 0;
1041                 hb_add( h, job );
1042             }
1043             hb_start( h );
1044             break;
1045         }
1046
1047 #define p s.param.working
1048         case HB_STATE_WORKING:
1049             fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1050                      p.job_cur, p.job_count, 100.0 * p.progress );
1051             if( p.seconds > -1 )
1052             {
1053                 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1054                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1055                          p.hours, p.minutes, p.seconds );
1056             }
1057             fflush(stdout);
1058             break;
1059 #undef p
1060
1061 #define p s.param.muxing
1062         case HB_STATE_MUXING:
1063         {
1064             if (show_mux_warning)
1065             {
1066                 fprintf( stdout, "\rMuxing: this may take awhile..." );
1067                 fflush(stdout);
1068                 show_mux_warning = 0;
1069             }
1070             break;
1071         }
1072 #undef p
1073
1074 #define p s.param.workdone
1075         case HB_STATE_WORKDONE:
1076             /* Print error if any, then exit */
1077             switch( p.error )
1078             {
1079                 case HB_ERROR_NONE:
1080                     fprintf( stderr, "\nRip done!\n" );
1081                     break;
1082                 case HB_ERROR_CANCELED:
1083                     fprintf( stderr, "\nRip canceled.\n" );
1084                     break;
1085                 default:
1086                     fprintf( stderr, "\nRip failed (error %x).\n",
1087                              p.error );
1088             }
1089             die = 1;
1090             break;
1091 #undef p
1092     }
1093     return 0;
1094 }
1095
1096 /****************************************************************************
1097  * SigHandler:
1098  ****************************************************************************/
1099 static volatile int64_t i_die_date = 0;
1100 void SigHandler( int i_signal )
1101 {
1102     if( die == 0 )
1103     {
1104         die = 1;
1105         i_die_date = hb_get_date();
1106         fprintf( stderr, "Signal %d received, terminating - do it "
1107                  "again in case it gets stuck\n", i_signal );
1108     }
1109     else if( i_die_date + 500 < hb_get_date() )
1110     {
1111         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1112         exit( 1 );
1113     }
1114 }
1115
1116 /****************************************************************************
1117  * ShowHelp:
1118  ****************************************************************************/
1119 static void ShowHelp()
1120 {
1121     int i;
1122
1123     fprintf( stderr,
1124     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1125     "\n"
1126         "### General Handbrake Options------------------------------------------------\n\n"
1127     "    -h, --help              Print help\n"
1128     "    -u, --update            Check for updates and exit\n"
1129     "    -v, --verbose           Be verbose\n"
1130     "    -C, --cpu               Set CPU count (default: autodetected)\n"
1131     "    -Z. --preset <string>   Use a built-in preset. Capitalization matters, and\n"
1132     "                            if the preset name has spaces, surround it with\n"
1133     "                            double quotation marks\n"
1134     "    -z, --preset-list       See a list of available built-in presets\n"
1135     "\n"
1136
1137         "### Source Options-----------------------------------------------------------\n\n"
1138         "    -i, --input <string>    Set input device\n"
1139         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
1140     "                            default: 1)\n"
1141     "    -L, --longest           Select the longest title\n"
1142     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1143     "                            1 to 3, or \"3\" for chapter 3 only,\n"
1144     "                            default: all chapters)\n"
1145         "\n"
1146
1147         "### Destination Options------------------------------------------------------\n\n"
1148     "    -o, --output <string>   Set output file name\n"
1149         "    -f, --format <string>   Set output format (avi/mp4/ogm/mkv, default:\n"
1150     "                            autodetected from file name)\n"
1151     "    -4, --large-file        Use 64-bit mp4 files that can hold more than\n"
1152     "                            4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1153     "    -O, --optimize          Optimize mp4 files for HTTP streaming\n"
1154     "    -I, --ipod-atom         Mark mp4 files so iPods will accept them\n"
1155     "\n"
1156
1157         "### Picture Settings---------------------------------------------------------\n\n"
1158     "    -w, --width <number>    Set picture width\n"
1159     "    -l, --height <number>   Set picture height\n"
1160     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
1161         "    -Y, --maxHeight <#>     Set maximum height\n"
1162         "    -X, --maxWidth <#>      Set maximum width\n"
1163         "    -s, --subtitle <number> Select subtitle (default: none)\n"
1164     "    -U, --subtitle-scan     Scan for subtitles in an extra 1st pass, and choose\n"
1165     "                            the one that's only used 10 percent of the time\n"
1166     "                            or less. This should locate subtitles for short\n"
1167     "                            foreign language segments. Best used in conjunction\n"
1168     "                            with --subtitle-forced.\n"
1169     "    -F, --subtitle-forced   Only display subtitles from the selected stream if\n"
1170     "                            the subtitle has the forced flag set. May be used in\n"
1171     "                            conjunction with --subtitle-scan to auto-select\n"
1172     "                            a stream if it contains forced subtitles.\n"
1173     "    -N, --native-language   Select subtitles with this language if it does not\n"
1174     "          <string>          match the Audio language. Provide the language's\n"
1175     "                            iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1176         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
1177         "\n"
1178
1179         "### Video Options------------------------------------------------------------\n\n"
1180         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
1181     "                            x264,x264b13,x264b30 default: ffmpeg)\n"
1182         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
1183         "    -Q, --cqp               Use with -q for CQP instead of CRF\n"
1184     "    -S, --size <MB>         Set target size\n"
1185         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
1186         "    -r, --rate              Set video framerate (" );
1187     for( i = 0; i < hb_video_rates_count; i++ )
1188     {
1189         fprintf( stderr, hb_video_rates[i].string );
1190         if( i != hb_video_rates_count - 1 )
1191             fprintf( stderr, "/" );
1192     }
1193     fprintf( stderr, ")\n"
1194         "\n"
1195         "    -2, --two-pass          Use two-pass mode\n"
1196      "    -d, --deinterlace       Deinterlace video with yadif/mcdeint filter\n"
1197      "          <YM:FD:MM:QP>     (default 0:-1:-1:1)\n"
1198      "           or\n"
1199      "          <fast/slow/slower>\n"
1200      "    -7, --deblock           Deblock video with pp7 filter\n"
1201      "          <QP:M>            (default 0:2)\n"
1202      "    -8, --denoise           Denoise video with hqdn3d filter\n"
1203      "          <SL:SC:TL:TC>     (default 4:3:6:4.5)\n"
1204      "           or\n"
1205      "          <weak/medium/strong>\n"
1206      "    -9, --detelecine        Detelecine video with pullup filter\n"
1207      "          <L:R:T:B:SB:MP>   (default 1:1:4:4:0:0)\n"
1208     "    -g, --grayscale         Grayscale encoding\n"
1209     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
1210     "    -P, --loosePixelratio   Store pixel aspect ratio with specified width\n"
1211     "           <modulus>        Takes as optional argument what number you want\n"
1212     "                            the dimensions to divide cleanly by (default 16)\n"
1213
1214
1215         "\n"
1216
1217
1218         "### Audio Options-----------------------------------------------------------\n\n"
1219         "    -E, --aencoder <string> Audio encoder (faac/lame/vorbis/ac3/aac+ac3) \n"
1220         "                            ac3 meaning passthrough, aac+ac3 meaning an\n"
1221         "                            aac dpl2 mixdown paired with ac3 pass-thru\n"
1222         "                            (default: guessed)\n"
1223         "    -B, --ab <kb/s>         Set audio bitrate (default: 128)\n"
1224         "    -a, --audio <string>    Select audio channel(s), separated by commas\n"
1225         "                            (\"none\" for no audio, \"1,2,3\" for multiple\n"
1226         "                             tracks, default: first one,\n"
1227         "                             max 8 normally, max 4 with aac+ac3)\n"
1228     "    -6, --mixdown <string>  Format for surround sound downmixing\n"
1229     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1230     "    -R, --arate             Set audio samplerate (" );
1231     for( i = 0; i < hb_audio_rates_count; i++ )
1232     {
1233         fprintf( stderr, hb_audio_rates[i].string );
1234         if( i != hb_audio_rates_count - 1 )
1235             fprintf( stderr, "/" );
1236     }
1237     fprintf( stderr, " kHz)\n"
1238     "    -D, --drc <float>       Apply extra dynamic range compression to the audio,\n"
1239     "                            making soft sounds louder. Range is 1.0 to 4.0\n"
1240     "                            (too loud), with 1.5 - 2.5 being a useful range.\n"
1241
1242
1243         "\n"
1244
1245
1246     "### Advanced Options---------------------------------------------------------\n\n"
1247     "    -x, --x264opts <string> Specify advanced x264 options in the\n"
1248     "                            same style as mencoder:\n"
1249     "                            option1=value1:option2=value2\n"
1250     "    -T, --turbo             When using 2-pass use the turbo options\n"
1251     "                            on the first pass to improve speed\n"
1252     "                            (only works with x264, affects PSNR by about 0.05dB,\n"
1253     "                            and increases first pass speed two to four times)\n"
1254     "    -V, --vfr               Perform variable framerate detelecine on NTSC video\n"
1255     );
1256 }
1257
1258 /****************************************************************************
1259  * ShowPresets:
1260  ****************************************************************************/
1261 static void ShowPresets()
1262 {
1263     printf("\n+ Animation:  -e x264 -b 1000 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" -m -p -2 -T -x ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2\n");
1264
1265     printf("\n+ AppleTV:  -e x264 -b 2500 -B 160 -R 48 -E aac+ac3 -f mp4 -4 -m -p -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0\n");
1266
1267     printf("\n+ Bedlam:  -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1\n");
1268
1269     printf("\n+ Blind:  -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1270
1271     printf("\n+ Broke:  -e x264 -S 695 -B 128 -R 48 -E faac -f mp4 -w 640 -m -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1272
1273     printf("\n+ Classic:  -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1274
1275     printf("\n+ Constant Quality Rate:  -e x264 -q 0.64709997177124023 -E ac3 -f mkv -m -p -x ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh\n");
1276
1277     printf("\n+ Deux Six Quatre:  -e x264 -b 1600 -E ac3 -f mkv -m -p -2 -T -x ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1278
1279     printf("\n+ Film:  -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip\n");
1280
1281     printf("\n+ iPhone / iPod Touch:  -e x264 -b 960 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1\n");
1282
1283     printf("\n+ iPod High-Rez:  -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -I -w 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1284
1285     printf("\n+ iPod Low-Rez:  -e x264 -b 700 -B 160 -R 48 -E faac -f mp4 -I -w 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1286
1287     printf("\n+ Normal:  -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=2:bframes=2:subme=5:me=umh\n");
1288
1289     printf("\n+ PS3:  -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1290
1291     printf("\n+ PSP:  -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1292
1293     printf("\n+ QuickTime:  -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip\n");
1294
1295     printf("\n+ Television:  -e x264 -b 1300 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" --denoise=\"weak\" -m -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip\n");
1296
1297     printf("\n+ Xbox 360:  -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -p -x level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1\n");
1298 }
1299
1300 /****************************************************************************
1301  * ParseOptions:
1302  ****************************************************************************/
1303 static int ParseOptions( int argc, char ** argv )
1304 {
1305     for( ;; )
1306     {
1307         static struct option long_options[] =
1308           {
1309             { "help",        no_argument,       NULL,    'h' },
1310             { "update",      no_argument,       NULL,    'u' },
1311             { "verbose",     no_argument,       NULL,    'v' },
1312             { "cpu",         required_argument, NULL,    'C' },
1313
1314             { "format",      required_argument, NULL,    'f' },
1315             { "input",       required_argument, NULL,    'i' },
1316             { "output",      required_argument, NULL,    'o' },
1317             { "large-file",  no_argument,       NULL,    '4' },
1318             { "optimize",    no_argument,       NULL,    'O' },
1319             { "ipod-atom",   no_argument,       NULL,    'I' },
1320
1321             { "title",       required_argument, NULL,    't' },
1322             { "longest",     no_argument,       NULL,    'L' },
1323             { "chapters",    required_argument, NULL,    'c' },
1324             { "markers",     optional_argument, NULL,    'm' },
1325             { "audio",       required_argument, NULL,    'a' },
1326             { "mixdown",     required_argument, NULL,    '6' },
1327             { "drc",         required_argument, NULL,    'D' },
1328             { "subtitle",    required_argument, NULL,    's' },
1329             { "subtitle-scan", no_argument,     NULL,    'U' },
1330             { "subtitle-forced", no_argument,   NULL,    'F' },
1331             { "native-language", required_argument, NULL,'N' },
1332
1333             { "encoder",     required_argument, NULL,    'e' },
1334             { "aencoder",    required_argument, NULL,    'E' },
1335             { "two-pass",    no_argument,       NULL,    '2' },
1336             { "deinterlace", optional_argument, NULL,    'd' },
1337             { "deblock",     optional_argument, NULL,    '7' },
1338             { "denoise",     optional_argument, NULL,    '8' },
1339             { "detelecine",  optional_argument, NULL,    '9' },
1340             { "grayscale",   no_argument,       NULL,    'g' },
1341             { "pixelratio",  no_argument,       NULL,    'p' },
1342             { "loosePixelratio", optional_argument,   NULL,    'P' },
1343             { "width",       required_argument, NULL,    'w' },
1344             { "height",      required_argument, NULL,    'l' },
1345             { "crop",        required_argument, NULL,    'n' },
1346
1347             { "vb",          required_argument, NULL,    'b' },
1348             { "quality",     required_argument, NULL,    'q' },
1349             { "size",        required_argument, NULL,    'S' },
1350             { "ab",          required_argument, NULL,    'B' },
1351             { "rate",        required_argument, NULL,    'r' },
1352             { "arate",       required_argument, NULL,    'R' },
1353             { "cqp",         no_argument,       NULL,    'Q' },
1354             { "x264opts",    required_argument, NULL,    'x' },
1355             { "turbo",       no_argument,       NULL,    'T' },
1356             { "maxHeight",   required_argument, NULL,    'Y' },
1357             { "maxWidth",    required_argument, NULL,    'X' },
1358             { "preset",      required_argument, NULL,    'Z' },
1359             { "preset-list", no_argument,       NULL,    'z' },
1360             { "vfr",         no_argument,       NULL,    'V' },
1361
1362             { 0, 0, 0, 0 }
1363           };
1364
1365         int option_index = 0;
1366         int c;
1367
1368                 c = getopt_long( argc, argv,
1369                                                  "hvuC:f:4i:Io:t:Lc:m::a:6:s:UFN:e:E:2dD:789gpOP::w:l:n:b:q:S:B:r:R:Qx:TY:X:VZ:z",
1370                          long_options, &option_index );
1371         if( c < 0 )
1372         {
1373             break;
1374         }
1375
1376         switch( c )
1377         {
1378             case 'h':
1379                 ShowHelp();
1380                 exit( 0 );
1381             case 'u':
1382                 update = 1;
1383                 break;
1384             case 'v':
1385                 debug = HB_DEBUG_ALL;
1386                 break;
1387             case 'C':
1388                 cpu = atoi( optarg );
1389                 break;
1390
1391             case 'Z':
1392                 preset = 1;
1393                 preset_name = strdup(optarg);
1394                 break;
1395             case 'z':
1396                 ShowPresets();
1397                 exit ( 0 );
1398
1399             case 'f':
1400                 format = strdup( optarg );
1401                 break;
1402             case 'i':
1403                 input = strdup( optarg );
1404                 break;
1405             case 'o':
1406                 output = strdup( optarg );
1407                 break;
1408             case '4':
1409                 largeFileSize = 1;
1410                 break;
1411             case 'O':
1412                 mp4_optimize = 1;
1413                 break;
1414             case 'I':
1415                 ipod_atom = 1;
1416                 break;
1417
1418             case 't':
1419                 titleindex = atoi( optarg );
1420                 break;
1421             case 'L':
1422                 longest_title = 1;
1423                 break;
1424             case 'c':
1425             {
1426                 int start, end;
1427                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1428                 {
1429                     chapter_start = start;
1430                     chapter_end   = end;
1431                 }
1432                 else if( sscanf( optarg, "%d", &start ) == 1 )
1433                 {
1434                     chapter_start = start;
1435                     chapter_end   = chapter_start;
1436                 }
1437                 else
1438                 {
1439                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
1440                              optarg );
1441                     return -1;
1442                 }
1443                 break;
1444             }
1445             case 'm':
1446                 if( optarg != NULL )
1447                 {
1448                     marker_file = strdup( optarg );
1449                 }
1450                 chapter_markers = 1;
1451                 break;
1452             case 'a':
1453                 audios = strdup( optarg );
1454                 break;
1455             case '6':
1456                 if( !strcasecmp( optarg, "mono" ) )
1457                 {
1458                     audio_mixdown = HB_AMIXDOWN_MONO;
1459                 }
1460                 else if( !strcasecmp( optarg, "stereo" ) )
1461                 {
1462                     audio_mixdown = HB_AMIXDOWN_STEREO;
1463                 }
1464                 else if( !strcasecmp( optarg, "dpl1" ) )
1465                 {
1466                     audio_mixdown = HB_AMIXDOWN_DOLBY;
1467                 }
1468                 else if( !strcasecmp( optarg, "dpl2" ) )
1469                 {
1470                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
1471                 }
1472                 else if( !strcasecmp( optarg, "6ch" ) )
1473                 {
1474                     audio_mixdown = HB_AMIXDOWN_6CH;
1475                 }
1476                 break;
1477             case 'D':
1478                 dynamic_range_compression = atof( optarg );
1479                 break;
1480             case 's':
1481                 sub = atoi( optarg );
1482                 break;
1483             case 'U':
1484                 subtitle_scan = 1;
1485                 break;
1486             case 'F':
1487                 subtitle_force = 1;
1488                 break;
1489             case 'N':
1490                 native_language = strdup( optarg );
1491                 break;
1492             case '2':
1493                 twoPass = 1;
1494                 break;
1495             case 'd':
1496                 if( optarg != NULL )
1497                 {
1498                     if (!( strcmp( optarg, "fast" ) ))
1499                     {
1500                         deinterlace_opt = "-1";
1501                     }
1502                     else if (!( strcmp( optarg, "slow" ) ))
1503                     {
1504                         deinterlace_opt = "2";
1505                     }
1506                     else if (!( strcmp( optarg, "slower" ) ))
1507                     {
1508                         deinterlace_opt = "0";
1509                     }
1510                     else
1511                     {
1512                         deinterlace_opt = strdup( optarg );
1513                     }
1514                 }
1515                 deinterlace = 1;
1516                 break;
1517             case '7':
1518                 if( optarg != NULL )
1519                 {
1520                     deblock_opt = strdup( optarg );
1521                 }
1522                 deblock = 1;
1523                 break;
1524             case '8':
1525                 if( optarg != NULL )
1526                 {
1527                     if (!( strcmp( optarg, "weak" ) ))
1528                     {
1529                         denoise_opt = "2:1:2:3";
1530                     }
1531                     else if (!( strcmp( optarg, "medium" ) ))
1532                     {
1533                         denoise_opt = "3:2:2:3";
1534                     }
1535                     else if (!( strcmp( optarg, "strong" ) ))
1536                     {
1537                         denoise_opt = "7:7:5:5";
1538                     }
1539                     else
1540                     {
1541                         denoise_opt = strdup( optarg );
1542                     }
1543                 }
1544                 denoise = 1;
1545                 break;
1546             case '9':
1547                 if( optarg != NULL )
1548                 {
1549                     detelecine_opt = strdup( optarg );
1550                 }
1551                 detelecine = 1;
1552                 break;
1553             case 'g':
1554                 grayscale = 1;
1555                 break;
1556             case 'p':
1557                 pixelratio = 1;
1558                 break;
1559             case 'P':
1560                 loosePixelratio = 1;
1561                 if( optarg != NULL )
1562                 {
1563                     modulus = atoi( optarg );
1564                 }
1565                 break;
1566             case 'e':
1567                 if( !strcasecmp( optarg, "ffmpeg" ) )
1568                 {
1569                     vcodec = HB_VCODEC_FFMPEG;
1570                 }
1571                 else if( !strcasecmp( optarg, "xvid" ) )
1572                 {
1573                     vcodec = HB_VCODEC_XVID;
1574                 }
1575                 else if( !strcasecmp( optarg, "x264" ) )
1576                 {
1577                     vcodec = HB_VCODEC_X264;
1578                 }
1579                 else if( !strcasecmp( optarg, "x264b13" ) )
1580                 {
1581                     vcodec = HB_VCODEC_X264;
1582                     h264_13 = 1;
1583                 }
1584                 else if( !strcasecmp( optarg, "x264b30" ) )
1585                 {
1586                     vcodec = HB_VCODEC_X264;
1587                     h264_30 = 1;
1588                 }
1589                 else
1590                 {
1591                     fprintf( stderr, "invalid codec (%s)\n", optarg );
1592                     return -1;
1593                 }
1594                 break;
1595             case 'E':
1596                 if( !strcasecmp( optarg, "ac3" ) )
1597                 {
1598                     acodec = HB_ACODEC_AC3;
1599                 }
1600                 else if( !strcasecmp( optarg, "lame" ) )
1601                 {
1602                     acodec = HB_ACODEC_LAME;
1603                 }
1604                 else if( !strcasecmp( optarg, "faac" ) )
1605                 {
1606                     acodec = HB_ACODEC_FAAC;
1607                 }
1608                 else if( !strcasecmp( optarg, "vorbis") )
1609                 {
1610                     acodec = HB_ACODEC_VORBIS;
1611                 }
1612                 else if( !strcasecmp( optarg, "aac+ac3") )
1613                 {
1614                     acodec = HB_ACODEC_FAAC;
1615                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII_AC3;
1616                     arate = 48000;
1617                 }
1618                 break;
1619             case 'w':
1620                 width = atoi( optarg );
1621                 break;
1622             case 'l':
1623                 height = atoi( optarg );
1624                 break;
1625             case 'n':
1626             {
1627                 int    i;
1628                 char * tmp = optarg;
1629                 for( i = 0; i < 4; i++ )
1630                 {
1631                     if( !*tmp )
1632                         break;
1633                     crop[i] = strtol( tmp, &tmp, 0 );
1634                     tmp++;
1635                 }
1636                 break;
1637             }
1638             case 'r':
1639             {
1640                 int i;
1641                 vrate = 0;
1642                 for( i = 0; i < hb_video_rates_count; i++ )
1643                 {
1644                     if( !strcmp( optarg, hb_video_rates[i].string ) )
1645                     {
1646                         vrate = hb_video_rates[i].rate;
1647                         break;
1648                     }
1649                 }
1650                 if( !vrate )
1651                 {
1652                     fprintf( stderr, "invalid framerate %s\n", optarg );
1653                 }
1654                 break;
1655             }
1656             case 'R':
1657             {
1658                 int i;
1659                 arate = 0;
1660                 for( i = 0; i < hb_audio_rates_count; i++ )
1661                 {
1662                     if( !strcmp( optarg, hb_audio_rates[i].string ) )
1663                     {
1664                         arate = hb_audio_rates[i].rate;
1665                         break;
1666                     }
1667                 }
1668                 if( !arate )
1669                 {
1670                     fprintf( stderr, "invalid framerate %s\n", optarg );
1671                 }
1672                 break;
1673             }
1674             case 'b':
1675                 vbitrate = atoi( optarg );
1676                 break;
1677             case 'q':
1678                 vquality = atof( optarg );
1679                 break;
1680             case 'S':
1681                 size = atoi( optarg );
1682                 break;
1683             case 'B':
1684                 abitrate = atoi( optarg );
1685                 break;
1686             case 'Q':
1687                 crf = 0;
1688                 break;
1689             case 'x':
1690                 x264opts = strdup( optarg );
1691                 break;
1692             case 'T':
1693                 turbo_opts_enabled = 1;
1694                 break;
1695             case 'Y':
1696                 maxHeight = atoi( optarg );
1697                 break;
1698             case 'X':
1699                 maxWidth = atoi (optarg );
1700                 break;
1701             case 'V':
1702                 vfr = 1;
1703                 break;
1704
1705             default:
1706                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1707                 return -1;
1708         }
1709     }
1710
1711     return 0;
1712 }
1713
1714 static int CheckOptions( int argc, char ** argv )
1715 {
1716     if( update )
1717     {
1718         return 0;
1719     }
1720
1721     if( input == NULL || *input == '\0' )
1722     {
1723         fprintf( stderr, "Missing input device. Run %s --help for "
1724                  "syntax.\n", argv[0] );
1725         return 1;
1726     }
1727
1728     /* Parse format */
1729     if( titleindex > 0 )
1730     {
1731         if( output == NULL || *output == '\0' )
1732         {
1733             fprintf( stderr, "Missing output file name. Run %s --help "
1734                      "for syntax.\n", argv[0] );
1735             return 1;
1736         }
1737
1738         if( !format )
1739         {
1740             char * p = strrchr( output, '.' );
1741
1742             /* autodetect */
1743             if( p && !strcasecmp( p, ".avi" ) )
1744             {
1745                 mux = HB_MUX_AVI;
1746             }
1747             else if( p && ( !strcasecmp( p, ".mp4" )  ||
1748                             !strcasecmp( p, ".m4v" ) ) )
1749             {
1750                 if ( h264_30 == 1 )
1751                     mux = HB_MUX_IPOD;
1752                 else
1753                     mux = HB_MUX_MP4;
1754             }
1755             else if( p && ( !strcasecmp( p, ".ogm" ) ||
1756                             !strcasecmp( p, ".ogg" ) ) )
1757             {
1758                 mux = HB_MUX_OGM;
1759             }
1760             else if( p && !strcasecmp(p, ".mkv" ) )
1761             {
1762                 mux = HB_MUX_MKV;
1763             }
1764             else
1765             {
1766                 fprintf( stderr, "Output format couldn't be guessed "
1767                          "from file name, using default.\n" );
1768                 return 0;
1769             }
1770         }
1771         else if( !strcasecmp( format, "avi" ) )
1772         {
1773             mux = HB_MUX_AVI;
1774         }
1775         else if( !strcasecmp( format, "mp4" ) )
1776         {
1777             if ( h264_30 == 1)
1778                 mux = HB_MUX_IPOD;
1779             else
1780                 mux = HB_MUX_MP4;
1781         }
1782         else if( !strcasecmp( format, "ogm" ) ||
1783                  !strcasecmp( format, "ogg" ) )
1784         {
1785             mux = HB_MUX_OGM;
1786         }
1787         else if( !strcasecmp( format, "mkv" ) )
1788         {
1789             mux = HB_MUX_MKV;
1790         }
1791         else
1792         {
1793             fprintf( stderr, "Invalid output format (%s). Possible "
1794                      "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
1795             return 1;
1796         }
1797
1798         if( !acodec )
1799         {
1800             if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
1801             {
1802                 acodec = HB_ACODEC_FAAC;
1803             }
1804             else if( mux == HB_MUX_AVI )
1805             {
1806                 acodec = HB_ACODEC_LAME;
1807             }
1808             else if( mux == HB_MUX_OGM )
1809             {
1810                 acodec = HB_ACODEC_VORBIS;
1811             }
1812             else if( mux == HB_MUX_MKV )
1813             {
1814                 acodec = HB_ACODEC_AC3;
1815             }
1816         }
1817
1818     }
1819
1820     return 0;
1821 }