OSDN Git Service

Made a small change to test.c to explicitly select the first audio track, and use...
[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
15 /* Options */
16 static int    debug       = HB_DEBUG_NONE;
17 static int    update      = 0;
18 static char * input       = NULL;
19 static char * output      = NULL;
20 static char * format      = NULL;
21 static int    titleindex  = 1;
22 static int    twoPass     = 0;
23 static int    deinterlace = 0;
24 static int    grayscale   = 0;
25 static int    vcodec      = HB_VCODEC_FFMPEG;
26 static int    h264_13     = 0;
27 static int    h264_30     = 0;
28 static char * audios      = NULL;
29 static int    audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
30 static int    sub         = 0;
31 static int    width       = 0;
32 static int    height      = 0;
33 static int    crop[4]     = { -1,-1,-1,-1 };
34 static int    cpu         = 0;
35 static int    vrate       = 0;
36 static int    arate       = 0;
37 static float  vquality    = -1.0;
38 static int    vbitrate    = 0;
39 static int    size        = 0;
40 static int    abitrate    = 0;
41 static int    mux         = 0;
42 static int    acodec      = 0;
43 static int    pixelratio  = 0;
44 static int    chapter_start = 0;
45 static int    chapter_end   = 0;
46 static int    chapter_markers = 0;
47 static int        crf                   = 0;
48 static char       *x264opts             = NULL;
49 static char       *x264opts2    = NULL;
50 static int        maxHeight             = 0;
51 static int        maxWidth              = 0;
52
53 /* Exit cleanly on Ctrl-C */
54 static volatile int die = 0;
55 static void SigHandler( int );
56
57 /* Utils */
58 static void ShowCommands();
59 static void ShowHelp();
60 static int  ParseOptions( int argc, char ** argv );
61 static int  CheckOptions( int argc, char ** argv );
62 static int  HandleEvents( hb_handle_t * h );
63
64 int main( int argc, char ** argv )
65 {
66     hb_handle_t * h;
67     int           build;
68     char        * version;
69
70     /* Parse command line */
71     if( ParseOptions( argc, argv ) ||
72         CheckOptions( argc, argv ) )
73     {
74         return 1;
75     }
76
77     /* Init libhb */
78     h = hb_init( debug, update );
79
80     /* Show version */
81     fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
82              hb_get_version( h ), hb_get_build( h ) );
83
84     /* Check for update */
85     if( update )
86     {
87         if( ( build = hb_check_update( h, &version ) ) > -1 )
88         {
89             fprintf( stderr, "You are using an old version of "
90                      "HandBrake.\nLatest is %s (build %d).\n", version,
91                      build );
92         }
93         else
94         {
95             fprintf( stderr, "Your version of HandBrake is up to "
96                      "date.\n" );
97         }
98         hb_close( &h );
99         return 0;
100     }
101
102     /* Geeky */
103     fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
104              hb_get_cpu_count( h ) > 1 ? "s" : "" );
105     if( cpu )
106     {
107         fprintf( stderr, "Forcing %d CPU%s\n", cpu,
108                  cpu > 1 ? "s" : "" );
109         hb_set_cpu_count( h, cpu );
110     }
111
112     /* Exit ASAP on Ctrl-C */
113     signal( SIGINT, SigHandler );
114
115     /* Feed libhb with a DVD to scan */
116     fprintf( stderr, "Opening %s...\n", input );
117     hb_scan( h, input, titleindex );
118
119     /* Wait... */
120     while( !die )
121     {
122 #if !defined(SYS_BEOS)
123         fd_set         fds;
124         struct timeval tv;
125         int            ret;
126         char           buf[257];
127
128         tv.tv_sec  = 0;
129         tv.tv_usec = 100000;
130
131         FD_ZERO( &fds );
132         FD_SET( STDIN_FILENO, &fds );
133         ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
134
135         if( ret > 0 )
136         {
137             int size = 0;
138
139             while( size < 256 &&
140                    read( STDIN_FILENO, &buf[size], 1 ) > 0 )
141             {
142                 if( buf[size] == '\n' )
143                 {
144                     break;
145                 }
146                 size++;
147             }
148
149             if( size >= 256 || buf[size] == '\n' )
150             {
151                 switch( buf[0] )
152                 {
153                     case 'q':
154                         die = 1;
155                         break;
156                     case 'p':
157                         hb_pause( h );
158                         break;
159                     case 'r':
160                         hb_resume( h );
161                         break;
162                     case 'h':
163                         ShowCommands();
164                         break;
165                 }
166             }
167         }
168         hb_snooze( 200 );
169 #else
170         hb_snooze( 200 );
171 #endif
172
173         HandleEvents( h );
174     }
175
176     /* Clean up */
177     hb_close( &h );
178     if( input )  free( input );
179     if( output ) free( output );
180     if( format ) free( format );
181     if( audios ) free( audios );
182         if( x264opts ) free (x264opts );
183         if( x264opts2 ) free (x264opts2 );
184         
185     fprintf( stderr, "HandBrake has exited.\n" );
186
187     return 0;
188 }
189
190 static void ShowCommands()
191 {
192     fprintf( stderr, "Commands:\n" );
193     fprintf( stderr, " [h]elp    Show this message\n" );
194     fprintf( stderr, " [q]uit    Exit HandBrakeCLI\n" );
195     fprintf( stderr, " [p]ause   Pause encoding\n" );
196     fprintf( stderr, " [r]esume  Resume encoding\n" );
197 }
198
199 static void PrintTitleInfo( hb_title_t * title )
200 {
201     hb_chapter_t  * chapter;
202     hb_audio_t    * audio;
203     hb_subtitle_t * subtitle;
204     int i;
205
206     fprintf( stderr, "+ title %d:\n", title->index );
207     fprintf( stderr, "  + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
208              title->vts, title->ttn, title->cell_start, title->cell_end,
209              title->block_count );
210     fprintf( stderr, "  + duration: %02d:%02d:%02d\n",
211              title->hours, title->minutes, title->seconds );
212     fprintf( stderr, "  + size: %dx%d, aspect: %.2f, %.3f fps\n",
213              title->width, title->height,
214              (float) title->aspect / HB_ASPECT_BASE,
215              (float) title->rate / title->rate_base );
216     fprintf( stderr, "  + autocrop: %d/%d/%d/%d\n", title->crop[0],
217              title->crop[1], title->crop[2], title->crop[3] );
218     fprintf( stderr, "  + chapters:\n" );
219     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
220     {
221         chapter = hb_list_item( title->list_chapter, i );
222         fprintf( stderr, "    + %d: cells %d->%d, %d blocks, duration "
223                  "%02d:%02d:%02d\n", chapter->index,
224                  chapter->cell_start, chapter->cell_end,
225                  chapter->block_count, chapter->hours, chapter->minutes,
226                  chapter->seconds );
227     }
228     fprintf( stderr, "  + audio tracks:\n" );
229     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
230     {
231         audio = hb_list_item( title->list_audio, i );
232         if( audio->codec & HB_ACODEC_AC3 )
233         {
234             fprintf( stderr, "    + %d, %s, %dHz, %dbps\n", i + 1,
235                      audio->lang, audio->rate, audio->bitrate );
236         }
237         else
238         {
239             fprintf( stderr, "    + %d, %s\n", i + 1, audio->lang );
240         }
241     }
242     fprintf( stderr, "  + subtitle tracks:\n" );
243     for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
244     {
245         subtitle = hb_list_item( title->list_subtitle, i );
246         fprintf( stderr, "    + %d, %s\n", i + 1, subtitle->lang );
247     }
248 }
249
250 static int HandleEvents( hb_handle_t * h )
251 {
252     hb_state_t s;
253     hb_get_state( h, &s );
254     switch( s.state )
255     {
256         case HB_STATE_IDLE:
257             /* Nothing to do */
258             break;
259
260 #define p s.param.scanning
261         case HB_STATE_SCANNING:
262             /* Show what title is currently being scanned */
263             fprintf( stderr, "Scanning title %d", p.title_cur );
264             if( !titleindex )
265                 fprintf( stderr, " of %d", p.title_count );
266             fprintf( stderr, "...\n" );
267             break;
268 #undef p
269
270         case HB_STATE_SCANDONE:
271         {
272             hb_list_t  * list;
273             hb_title_t * title;
274             hb_job_t   * job;
275
276             list = hb_get_titles( h );
277
278             if( !hb_list_count( list ) )
279             {
280                 /* No valid title, stop right there */
281                 fprintf( stderr, "No title found.\n" );
282                 die = 1;
283                 break;
284             }
285             if( !titleindex )
286             {
287                 /* Scan-only mode, print infos and exit */
288                 int i;
289                 for( i = 0; i < hb_list_count( list ); i++ )
290                 {
291                     title = hb_list_item( list, i );
292                     PrintTitleInfo( title );
293                 }
294                 die = 1;
295                 break;
296             }
297
298             /* Set job settings */
299             title = hb_list_item( list, 0 );
300             job   = title->job;
301
302             PrintTitleInfo( title );
303
304             if( chapter_start && chapter_end )
305             {
306                 job->chapter_start = MAX( job->chapter_start,
307                                           chapter_start );
308                 job->chapter_end   = MIN( job->chapter_end,
309                                           chapter_end );
310                 job->chapter_end   = MAX( job->chapter_start,
311                                           job->chapter_end );
312             }
313
314                         if ( chapter_markers )
315                         {
316                                 job->chapter_markers = chapter_markers;
317                         }
318
319             if( crop[0] >= 0 && crop[1] >= 0 &&
320                 crop[2] >= 0 && crop[3] >= 0 )
321             {
322                 memcpy( job->crop, crop, 4 * sizeof( int ) );
323             }
324
325             job->deinterlace = deinterlace;
326             job->grayscale   = grayscale;
327             job->pixel_ratio = pixelratio;
328
329             if( width && height )
330             {
331                 job->width  = width;
332                 job->height = height;
333             }
334             else if( width )
335             {
336                 job->width = width;
337                 hb_fix_aspect( job, HB_KEEP_WIDTH );
338             }
339             else if( height )
340             {
341                 job->height = height;
342                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
343             }
344             else if( !width && !height && !pixelratio )
345             {
346                 hb_fix_aspect( job, HB_KEEP_WIDTH );
347             }
348
349             if( vquality >= 0.0 && vquality <= 1.0 )
350             {
351                 job->vquality = vquality;
352                 job->vbitrate = 0;
353             }
354             else if( vbitrate )
355             {
356                 job->vquality = -1.0;
357                 job->vbitrate = vbitrate;
358             }
359             if( vcodec )
360             {
361                 job->vcodec = vcodec;
362             }
363             if( h264_13 ) 
364             { 
365                 job->h264_level = 13; 
366             }
367                 if( h264_30 )
368                 {
369                     job->h264_level = 30;
370             }
371             if( vrate )
372             {
373                 job->vrate = 27000000;
374                 job->vrate_base = vrate;
375             }
376             if( arate )
377             {
378                 job->arate = arate;
379             }
380
381             if( audios )
382             {
383                 if( strcasecmp( audios, "none" ) )
384                 {
385                     int    audio_count = 0;
386                     char * tmp         = audios;
387                     while( *tmp )
388                     {
389                         if( *tmp < '0' || *tmp > '9' )
390                         {
391                             /* Skip non numeric char */
392                             tmp++;
393                             continue;
394                         }
395                                                 job->audio_mixdowns[audio_count] = audio_mixdown;
396                         job->audios[audio_count++] =
397                             strtol( tmp, &tmp, 0 ) - 1;
398                     }
399                     job->audios[audio_count] = -1;
400                 }
401                 else
402                 {
403                     job->audios[0] = -1;
404                 }
405             }
406                         else
407                         {
408                             /* default to the first audio track if none has been specified */
409                             job->audios[0] = 0;
410                             job->audio_mixdowns[0] = audio_mixdown;
411                         }
412             if( abitrate )
413             {
414                 job->abitrate = abitrate;
415             }
416             if( acodec )
417             {
418                 job->acodec = acodec;
419             }
420
421             if( size )
422             {
423                 job->vbitrate = hb_calc_bitrate( job, size );
424                 fprintf( stderr, "Calculated bitrate: %d kbps\n",
425                          job->vbitrate );
426             }
427
428             if( sub )
429             {
430                 job->subtitle = sub - 1;
431             }
432
433             if( job->mux )
434             {
435                 job->mux = mux;
436             }
437             job->file = strdup( output );
438
439                         if( crf )
440                         {
441                                 job->crf = 1;
442                         }
443
444                         if (x264opts != NULL && *x264opts != '\0' )
445                         {
446                                 hb_log("Applying the following x264 options: %s", x264opts);
447                                 job->x264opts = x264opts;
448                         }
449                         else /*avoids a bus error crash when options aren't specified*/
450                         {
451                                 job->x264opts =  NULL;
452                         }
453                         if (maxWidth)
454                                 job->maxWidth = maxWidth;
455                         if (maxHeight)
456                                 job->maxHeight = maxHeight;
457                                 
458             if( twoPass )
459             {
460                 job->pass = 1;
461                 hb_add( h, job );
462                 job->pass = 2;
463                                 job->x264opts = x264opts2;
464                 hb_add( h, job );
465             }
466             else
467             {
468                 job->pass = 0;
469                 hb_add( h, job );
470             }
471             hb_start( h );
472             break;
473         }
474
475 #define p s.param.working
476         case HB_STATE_WORKING:
477             fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
478                      p.job_cur, p.job_count, 100.0 * p.progress );
479             if( p.seconds > -1 )
480             {
481                 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
482                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
483                          p.hours, p.minutes, p.seconds );
484             }
485             break;
486 #undef p
487
488 #define p s.param.muxing
489         case HB_STATE_MUXING:
490         {
491             fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
492             break;
493         }
494 #undef p
495
496 #define p s.param.workdone
497         case HB_STATE_WORKDONE:
498             /* Print error if any, then exit */
499             switch( p.error )
500             {
501                 case HB_ERROR_NONE:
502                     fprintf( stderr, "\nRip done!\n" );
503                     break;
504                 case HB_ERROR_CANCELED:
505                     fprintf( stderr, "\nRip canceled.\n" );
506                     break;
507                 default:
508                     fprintf( stderr, "\nRip failed (error %x).\n",
509                              p.error );
510             }
511             die = 1;
512             break;
513 #undef p
514     }
515     return 0;
516 }
517
518 /****************************************************************************
519  * SigHandler:
520  ****************************************************************************/
521 static volatile int64_t i_die_date = 0;
522 void SigHandler( int i_signal )
523 {
524     if( die == 0 )
525     {
526         die = 1;
527         i_die_date = hb_get_date();
528         fprintf( stderr, "Signal %d received, terminating - do it "
529                  "again in case it gets stuck\n", i_signal );
530     }
531     else if( i_die_date + 500 < hb_get_date() )
532     {
533         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
534         exit( 1 );
535     }
536 }
537
538 /****************************************************************************
539  * ShowHelp:
540  ****************************************************************************/
541 static void ShowHelp()
542 {
543     int i;
544     
545     fprintf( stderr,
546     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
547     "\n"
548         "### General Handbrake Options------------------------------------------------\n\n"
549     "    -h, --help              Print help\n"
550     "    -u, --update            Check for updates and exit\n"
551     "    -v, --verbose           Be verbose\n"
552     "    -C, --cpu               Set CPU count (default: autodetected)\n"
553     "\n"
554         
555         "### Source Options-----------------------------------------------------------\n\n"
556         "    -i, --input <string>    Set input device\n"
557         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
558     "                            default: 1)\n"
559     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
560     "                            1 to 3, or \"3\" for chapter 3 only,\n"
561     "                            default: all chapters)\n"
562         "\n"
563         
564         "### Destination Options------------------------------------------------------\n\n"
565     "    -o, --output <string>   Set output file name\n"
566         "    -f, --format <string>   Set output format (avi/mp4/ogm, default:\n"
567     "                            autodetected from file name)\n"
568     "\n"
569         
570         "### Picture Settings---------------------------------------------------------\n\n"
571     "    -w, --width <number>    Set picture width\n"
572     "    -l, --height <number>   Set picture height\n"
573     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
574         "    -Y, --maxHeight <#>     Set maximum height\n"
575         "    -X, --maxWidth <#>      Set maximum width\n"
576         "    -s, --subtitle <number> Select subtitle (default: none)\n"
577         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
578         "\n"
579         
580         "### Video Options------------------------------------------------------------\n\n"
581         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
582     "                            x264,x264b13,x264b30 default: ffmpeg)\n"
583         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
584         "    -Q, --crf               Use with -q for CRF instead of CQP\n"
585     "    -S, --size <MB>         Set target size\n"
586         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
587         "    -r, --rate              Set video framerate (" );
588     for( i = 0; i < hb_video_rates_count; i++ )
589     {
590         fprintf( stderr, hb_video_rates[i].string );
591         if( i != hb_video_rates_count - 1 )
592             fprintf( stderr, "/" );
593     }
594     fprintf( stderr, ")\n"
595         "\n"
596         "    -2, --two-pass          Use two-pass mode\n"
597     "    -d, --deinterlace       Deinterlace video\n"
598     "    -g, --grayscale         Grayscale encoding\n"
599     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
600         
601         "\n"
602         
603         
604         "### Audio Options-----------------------------------------------------------\n\n"
605         "    -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
606     "                            meaning passthrough, default: guessed)\n"
607         "    -B, --ab <kb/s>         Set audio bitrate (default: 128)\n"
608         "    -a, --audio <string>    Select audio channel(s) (\"none\" for no \n"
609     "                            audio, default: first one)\n"
610     "    -6, --mixdown <string>  Format for surround sound downmixing\n"
611     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
612     "    -R, --arate             Set audio samplerate (" );
613     for( i = 0; i < hb_audio_rates_count; i++ )
614     {
615         fprintf( stderr, hb_audio_rates[i].string );
616         if( i != hb_audio_rates_count - 1 )
617             fprintf( stderr, "/" );
618     }
619     fprintf( stderr, " kHz)\n"
620    
621     
622         
623         "\n"
624         
625         
626         "### Advanced H264 Options----------------------------------------------------\n\n"
627         "    -x, --x264opts <string> Specify advanced x264 options in the\n"
628         "                            same style as mencoder:\n"
629         "                            option1=value1:option2=value2\n" );
630 }
631
632 /****************************************************************************
633  * ParseOptions:
634  ****************************************************************************/
635 static int ParseOptions( int argc, char ** argv )
636 {
637     for( ;; )
638     {
639         static struct option long_options[] =
640           {
641             { "help",        no_argument,       NULL,    'h' },
642             { "update",      no_argument,       NULL,    'u' },
643             { "verbose",     no_argument,       NULL,    'v' },
644             { "cpu",         required_argument, NULL,    'C' },
645
646             { "format",      required_argument, NULL,    'f' },
647             { "input",       required_argument, NULL,    'i' },
648             { "output",      required_argument, NULL,    'o' },
649
650             { "title",       required_argument, NULL,    't' },
651             { "chapters",    required_argument, NULL,    'c' },
652             { "markers",     no_argument,       NULL,    'm' },
653             { "audio",       required_argument, NULL,    'a' },
654             { "mixdown",     required_argument, NULL,    '6' },
655             { "subtitle",    required_argument, NULL,    's' },
656
657             { "encoder",     required_argument, NULL,    'e' },
658             { "aencoder",    required_argument, NULL,    'E' },
659             { "two-pass",    no_argument,       NULL,    '2' },
660             { "deinterlace", no_argument,       NULL,    'd' },
661             { "grayscale",   no_argument,       NULL,    'g' },
662             { "pixelratio",  no_argument,       NULL,    'p' },
663             { "width",       required_argument, NULL,    'w' },
664             { "height",      required_argument, NULL,    'l' },
665             { "crop",        required_argument, NULL,    'n' },
666
667             { "vb",          required_argument, NULL,    'b' },
668             { "quality",     required_argument, NULL,    'q' },
669             { "size",        required_argument, NULL,    'S' },
670             { "ab",          required_argument, NULL,    'B' },
671             { "rate",        required_argument, NULL,    'r' },
672             { "arate",       required_argument, NULL,    'R' },
673                         { "crf",                 no_argument,           NULL,    'Q' },
674                         { "x264opts",    required_argument, NULL,    'x' },
675                         { "maxHeight",   required_argument, NULL,        'Y' },
676                         { "maxWidth",    required_argument, NULL,        'X' },
677                         
678             { 0, 0, 0, 0 }
679           };
680
681         int option_index = 0;
682         int c;
683
684         c = getopt_long( argc, argv,
685                          "hvuC:f:i:o:t:c:ma:6:s:e:E:2dgpw:l:n:b:q:S:B:r:R:Qx:Y:X:",
686                          long_options, &option_index );
687         if( c < 0 )
688         {
689             break;
690         }
691
692         switch( c )
693         {
694             case 'h':
695                 ShowHelp();
696                 exit( 0 );
697             case 'u':
698                 update = 1;
699                 break;
700             case 'v':
701                 debug = HB_DEBUG_ALL;
702                 break;
703             case 'C':
704                 cpu = atoi( optarg );
705                 break;
706
707             case 'f':
708                 format = strdup( optarg );
709                 break;
710             case 'i':
711                 input = strdup( optarg );
712                 break;
713             case 'o':
714                 output = strdup( optarg );
715                 break;
716
717             case 't':
718                 titleindex = atoi( optarg );
719                 break;
720             case 'c':
721             {
722                 int start, end;
723                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
724                 {
725                     chapter_start = start;
726                     chapter_end   = end;
727                 }
728                 else if( sscanf( optarg, "%d", &start ) == 1 )
729                 {
730                     chapter_start = start;
731                     chapter_end   = chapter_start;
732                 }
733                 else
734                 {
735                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
736                              optarg );
737                     return -1;
738                 }
739                 break;
740             }
741             case 'm':
742                 chapter_markers = 1;
743                 break;
744             case 'a':
745                 audios = strdup( optarg );
746                 break;
747             case '6':
748                 if( !strcasecmp( optarg, "mono" ) )
749                 {
750                     audio_mixdown = HB_AMIXDOWN_MONO;
751                 }
752                 else if( !strcasecmp( optarg, "stereo" ) )
753                 {
754                     audio_mixdown = HB_AMIXDOWN_STEREO;
755                 }
756                 else if( !strcasecmp( optarg, "dpl1" ) )
757                 {
758                     audio_mixdown = HB_AMIXDOWN_DOLBY;
759                 }
760                 else if( !strcasecmp( optarg, "dpl2" ) )
761                 {
762                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
763                                 }
764                 else if( !strcasecmp( optarg, "6ch" ) )
765                 {
766                     audio_mixdown = HB_AMIXDOWN_6CH;
767                 }
768                 break;
769             case 's':
770                 sub = atoi( optarg );
771                 break;
772
773             case '2':
774                 twoPass = 1;
775                 break;
776             case 'd':
777                 deinterlace = 1;
778                 break;
779             case 'g':
780                 grayscale = 1;
781                 break;
782             case 'p':
783                 pixelratio = 1;
784                 break;
785             case 'e':
786                 if( !strcasecmp( optarg, "ffmpeg" ) )
787                 {
788                     vcodec = HB_VCODEC_FFMPEG;
789                 }
790                 else if( !strcasecmp( optarg, "xvid" ) )
791                 {
792                     vcodec = HB_VCODEC_XVID;
793                 }
794                 else if( !strcasecmp( optarg, "x264" ) )
795                 {
796                     vcodec = HB_VCODEC_X264;
797                 }
798                 else if( !strcasecmp( optarg, "x264b13" ) )
799                 {
800                     vcodec = HB_VCODEC_X264;
801                     h264_13 = 1;
802                 }
803                 else if( !strcasecmp( optarg, "x264b30" ) )
804                 {
805                     vcodec = HB_VCODEC_X264;
806                     h264_30 = 1;
807                 }
808                 else
809                 {
810                     fprintf( stderr, "invalid codec (%s)\n", optarg );
811                     return -1;
812                 }
813                 break;
814             case 'E':
815                 if( !strcasecmp( optarg, "ac3" ) )
816                 {
817                     acodec = HB_ACODEC_AC3;
818                 }
819                 else if( !strcasecmp( optarg, "lame" ) )
820                 {
821                     acodec = HB_ACODEC_LAME;
822                 }
823                 break;
824             case 'w':
825                 width = atoi( optarg );
826                 break;
827             case 'l':
828                 height = atoi( optarg );
829                 break;
830             case 'n':
831             {
832                 int    i;
833                 char * tmp = optarg;
834                 for( i = 0; i < 4; i++ )
835                 {
836                     if( !*tmp )
837                         break;
838                     crop[i] = strtol( tmp, &tmp, 0 );
839                     tmp++;
840                 }
841                 break;
842             }
843             case 'r':
844             {
845                 int i;
846                 vrate = 0;
847                 for( i = 0; i < hb_video_rates_count; i++ )
848                 {
849                     if( !strcmp( optarg, hb_video_rates[i].string ) )
850                     {
851                         vrate = hb_video_rates[i].rate;
852                         break;
853                     }
854                 }
855                 if( !vrate )
856                 {
857                     fprintf( stderr, "invalid framerate %s\n", optarg );
858                 }
859                 break;
860             }
861             case 'R':
862             {
863                 int i;
864                 arate = 0;
865                 for( i = 0; i < hb_audio_rates_count; i++ )
866                 {
867                     if( !strcmp( optarg, hb_audio_rates[i].string ) )
868                     {
869                         arate = hb_audio_rates[i].rate;
870                         break;
871                     }
872                 }
873                 if( !arate )
874                 {
875                     fprintf( stderr, "invalid framerate %s\n", optarg );
876                 }
877                 break;
878             }
879             case 'b':
880                 vbitrate = atoi( optarg );
881                 break;
882             case 'q':
883                 vquality = atof( optarg );
884                 break;
885             case 'S':
886                 size = atoi( optarg );
887                 break;
888             case 'B':
889                 abitrate = atoi( optarg );
890                 break;
891                         case 'Q':
892                                 crf = 1;
893                                 break;
894                         case 'x':
895                                 x264opts = strdup( optarg );
896                                 x264opts2 = strdup( optarg );
897                             break;
898                         case 'Y':
899                                 maxHeight = atoi( optarg );
900                                 break;
901                         case 'X':
902                                 maxWidth = atoi (optarg );
903                                 break;
904                                 
905             default:
906                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
907                 return -1;
908         }
909     }
910
911     return 0;
912 }
913
914 static int CheckOptions( int argc, char ** argv )
915 {
916     if( update )
917     {
918         return 0;
919     }
920
921     if( input == NULL || *input == '\0' )
922     {
923         fprintf( stderr, "Missing input device. Run %s --help for "
924                  "syntax.\n", argv[0] );
925         return 1;
926     }
927
928     /* Parse format */
929     if( titleindex > 0 )
930     {
931         if( output == NULL || *output == '\0' )
932         {
933             fprintf( stderr, "Missing output file name. Run %s --help "
934                      "for syntax.\n", argv[0] );
935             return 1;
936         }
937
938         if( !format )
939         {
940             char * p = strrchr( output, '.' );
941
942             /* autodetect */
943             if( p && !strcasecmp( p, ".avi" ) )
944             {
945                 mux = HB_MUX_AVI;
946             }
947             else if( p && ( !strcasecmp( p, ".mp4" )  ||
948                                                         !strcasecmp( p, ".m4v" ) ) )
949             {
950                 if ( h264_30 == 1 )
951                     mux = HB_MUX_IPOD;
952                 else
953                     mux = HB_MUX_MP4;
954             }
955             else if( p && ( !strcasecmp( p, ".ogm" ) ||
956                             !strcasecmp( p, ".ogg" ) ) )
957             {
958                 mux = HB_MUX_OGM;
959             }
960             else
961             {
962                 fprintf( stderr, "Output format couldn't be guessed "
963                          "from file name, using default.\n" );
964                 return 0;
965             }
966         }
967         else if( !strcasecmp( format, "avi" ) )
968         {
969             mux = HB_MUX_AVI;
970         }
971         else if( !strcasecmp( format, "mp4" ) )
972         {
973             if ( h264_30 == 1)
974                 mux = HB_MUX_IPOD;
975             else
976                 mux = HB_MUX_MP4;
977         }
978         else if( !strcasecmp( format, "ogm" ) ||
979                  !strcasecmp( format, "ogg" ) )
980         {
981             mux = HB_MUX_OGM;
982         }
983         else
984         {
985             fprintf( stderr, "Invalid output format (%s). Possible "
986                      "choices are avi, mp4 and ogm\n.", format );
987             return 1;
988         }
989
990         if( !acodec )
991         {
992             if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
993             {
994                 acodec = HB_ACODEC_FAAC;
995             }
996             else if( mux == HB_MUX_AVI )
997             {
998                 acodec = HB_ACODEC_LAME;
999             }
1000             else if( mux == HB_MUX_OGM )
1001             {
1002                 acodec = HB_ACODEC_VORBIS;
1003             }
1004         }
1005
1006     }
1007
1008     return 0;
1009 }
1010