OSDN Git Service

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