OSDN Git Service

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