OSDN Git Service

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