OSDN Git Service

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