OSDN Git Service

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