OSDN Git Service

hb_log is for libhb; use fprintf like the rest of the file.
[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                 fprintf( stderr, "Applying the following x264 options: %s\n", 
447                         x264opts);
448                 job->x264opts = x264opts;
449             }
450             else /*avoids a bus error crash when options aren't specified*/
451                         {
452                                 job->x264opts =  NULL;
453                         }
454                         if (maxWidth)
455                                 job->maxWidth = maxWidth;
456                         if (maxHeight)
457                                 job->maxHeight = maxHeight;
458                                 
459             if( twoPass )
460             {
461                 job->pass = 1;
462                 hb_add( h, job );
463                 job->pass = 2;
464                                 job->x264opts = x264opts2;
465                 hb_add( h, job );
466             }
467             else
468             {
469                 job->pass = 0;
470                 hb_add( h, job );
471             }
472             hb_start( h );
473             break;
474         }
475
476 #define p s.param.working
477         case HB_STATE_WORKING:
478             fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
479                      p.job_cur, p.job_count, 100.0 * p.progress );
480             if( p.seconds > -1 )
481             {
482                 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
483                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
484                          p.hours, p.minutes, p.seconds );
485             }
486             break;
487 #undef p
488
489 #define p s.param.muxing
490         case HB_STATE_MUXING:
491         {
492             fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
493             break;
494         }
495 #undef p
496
497 #define p s.param.workdone
498         case HB_STATE_WORKDONE:
499             /* Print error if any, then exit */
500             switch( p.error )
501             {
502                 case HB_ERROR_NONE:
503                     fprintf( stderr, "\nRip done!\n" );
504                     break;
505                 case HB_ERROR_CANCELED:
506                     fprintf( stderr, "\nRip canceled.\n" );
507                     break;
508                 default:
509                     fprintf( stderr, "\nRip failed (error %x).\n",
510                              p.error );
511             }
512             die = 1;
513             break;
514 #undef p
515     }
516     return 0;
517 }
518
519 /****************************************************************************
520  * SigHandler:
521  ****************************************************************************/
522 static volatile int64_t i_die_date = 0;
523 void SigHandler( int i_signal )
524 {
525     if( die == 0 )
526     {
527         die = 1;
528         i_die_date = hb_get_date();
529         fprintf( stderr, "Signal %d received, terminating - do it "
530                  "again in case it gets stuck\n", i_signal );
531     }
532     else if( i_die_date + 500 < hb_get_date() )
533     {
534         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
535         exit( 1 );
536     }
537 }
538
539 /****************************************************************************
540  * ShowHelp:
541  ****************************************************************************/
542 static void ShowHelp()
543 {
544     int i;
545     
546     fprintf( stderr,
547     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
548     "\n"
549         "### General Handbrake Options------------------------------------------------\n\n"
550     "    -h, --help              Print help\n"
551     "    -u, --update            Check for updates and exit\n"
552     "    -v, --verbose           Be verbose\n"
553     "    -C, --cpu               Set CPU count (default: autodetected)\n"
554     "\n"
555         
556         "### Source Options-----------------------------------------------------------\n\n"
557         "    -i, --input <string>    Set input device\n"
558         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
559     "                            default: 1)\n"
560     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
561     "                            1 to 3, or \"3\" for chapter 3 only,\n"
562     "                            default: all chapters)\n"
563         "\n"
564         
565         "### Destination Options------------------------------------------------------\n\n"
566     "    -o, --output <string>   Set output file name\n"
567         "    -f, --format <string>   Set output format (avi/mp4/ogm, default:\n"
568     "                            autodetected from file name)\n"
569     "\n"
570         
571         "### Picture Settings---------------------------------------------------------\n\n"
572     "    -w, --width <number>    Set picture width\n"
573     "    -l, --height <number>   Set picture height\n"
574     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
575         "    -Y, --maxHeight <#>     Set maximum height\n"
576         "    -X, --maxWidth <#>      Set maximum width\n"
577         "    -s, --subtitle <number> Select subtitle (default: none)\n"
578         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
579         "\n"
580         
581         "### Video Options------------------------------------------------------------\n\n"
582         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
583     "                            x264,x264b13,x264b30 default: ffmpeg)\n"
584         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
585         "    -Q, --crf               Use with -q for CRF instead of CQP\n"
586     "    -S, --size <MB>         Set target size\n"
587         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
588         "    -r, --rate              Set video framerate (" );
589     for( i = 0; i < hb_video_rates_count; i++ )
590     {
591         fprintf( stderr, hb_video_rates[i].string );
592         if( i != hb_video_rates_count - 1 )
593             fprintf( stderr, "/" );
594     }
595     fprintf( stderr, ")\n"
596         "\n"
597         "    -2, --two-pass          Use two-pass mode\n"
598     "    -d, --deinterlace       Deinterlace video\n"
599     "    -g, --grayscale         Grayscale encoding\n"
600     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
601         
602         "\n"
603         
604         
605         "### Audio Options-----------------------------------------------------------\n\n"
606         "    -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
607     "                            meaning passthrough, default: guessed)\n"
608         "    -B, --ab <kb/s>         Set audio bitrate (default: 128)\n"
609         "    -a, --audio <string>    Select audio channel(s) (\"none\" for no \n"
610     "                            audio, default: first one)\n"
611     "    -6, --mixdown <string>  Format for surround sound downmixing\n"
612     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
613     "    -R, --arate             Set audio samplerate (" );
614     for( i = 0; i < hb_audio_rates_count; i++ )
615     {
616         fprintf( stderr, hb_audio_rates[i].string );
617         if( i != hb_audio_rates_count - 1 )
618             fprintf( stderr, "/" );
619     }
620     fprintf( stderr, " kHz)\n"
621    
622     
623         
624         "\n"
625         
626         
627         "### Advanced H264 Options----------------------------------------------------\n\n"
628         "    -x, --x264opts <string> Specify advanced x264 options in the\n"
629         "                            same style as mencoder:\n"
630         "                            option1=value1:option2=value2\n" );
631 }
632
633 /****************************************************************************
634  * ParseOptions:
635  ****************************************************************************/
636 static int ParseOptions( int argc, char ** argv )
637 {
638     for( ;; )
639     {
640         static struct option long_options[] =
641           {
642             { "help",        no_argument,       NULL,    'h' },
643             { "update",      no_argument,       NULL,    'u' },
644             { "verbose",     no_argument,       NULL,    'v' },
645             { "cpu",         required_argument, NULL,    'C' },
646
647             { "format",      required_argument, NULL,    'f' },
648             { "input",       required_argument, NULL,    'i' },
649             { "output",      required_argument, NULL,    'o' },
650
651             { "title",       required_argument, NULL,    't' },
652             { "chapters",    required_argument, NULL,    'c' },
653             { "markers",     no_argument,       NULL,    'm' },
654             { "audio",       required_argument, NULL,    'a' },
655             { "mixdown",     required_argument, NULL,    '6' },
656             { "subtitle",    required_argument, NULL,    's' },
657
658             { "encoder",     required_argument, NULL,    'e' },
659             { "aencoder",    required_argument, NULL,    'E' },
660             { "two-pass",    no_argument,       NULL,    '2' },
661             { "deinterlace", no_argument,       NULL,    'd' },
662             { "grayscale",   no_argument,       NULL,    'g' },
663             { "pixelratio",  no_argument,       NULL,    'p' },
664             { "width",       required_argument, NULL,    'w' },
665             { "height",      required_argument, NULL,    'l' },
666             { "crop",        required_argument, NULL,    'n' },
667
668             { "vb",          required_argument, NULL,    'b' },
669             { "quality",     required_argument, NULL,    'q' },
670             { "size",        required_argument, NULL,    'S' },
671             { "ab",          required_argument, NULL,    'B' },
672             { "rate",        required_argument, NULL,    'r' },
673             { "arate",       required_argument, NULL,    'R' },
674                         { "crf",                 no_argument,           NULL,    'Q' },
675                         { "x264opts",    required_argument, NULL,    'x' },
676                         { "maxHeight",   required_argument, NULL,        'Y' },
677                         { "maxWidth",    required_argument, NULL,        'X' },
678                         
679             { 0, 0, 0, 0 }
680           };
681
682         int option_index = 0;
683         int c;
684
685         c = getopt_long( argc, argv,
686                          "hvuC:f:i:o:t:c:ma:6:s:e:E:2dgpw:l:n:b:q:S:B:r:R:Qx:Y:X:",
687                          long_options, &option_index );
688         if( c < 0 )
689         {
690             break;
691         }
692
693         switch( c )
694         {
695             case 'h':
696                 ShowHelp();
697                 exit( 0 );
698             case 'u':
699                 update = 1;
700                 break;
701             case 'v':
702                 debug = HB_DEBUG_ALL;
703                 break;
704             case 'C':
705                 cpu = atoi( optarg );
706                 break;
707
708             case 'f':
709                 format = strdup( optarg );
710                 break;
711             case 'i':
712                 input = strdup( optarg );
713                 break;
714             case 'o':
715                 output = strdup( optarg );
716                 break;
717
718             case 't':
719                 titleindex = atoi( optarg );
720                 break;
721             case 'c':
722             {
723                 int start, end;
724                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
725                 {
726                     chapter_start = start;
727                     chapter_end   = end;
728                 }
729                 else if( sscanf( optarg, "%d", &start ) == 1 )
730                 {
731                     chapter_start = start;
732                     chapter_end   = chapter_start;
733                 }
734                 else
735                 {
736                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
737                              optarg );
738                     return -1;
739                 }
740                 break;
741             }
742             case 'm':
743                 chapter_markers = 1;
744                 break;
745             case 'a':
746                 audios = strdup( optarg );
747                 break;
748             case '6':
749                 if( !strcasecmp( optarg, "mono" ) )
750                 {
751                     audio_mixdown = HB_AMIXDOWN_MONO;
752                 }
753                 else if( !strcasecmp( optarg, "stereo" ) )
754                 {
755                     audio_mixdown = HB_AMIXDOWN_STEREO;
756                 }
757                 else if( !strcasecmp( optarg, "dpl1" ) )
758                 {
759                     audio_mixdown = HB_AMIXDOWN_DOLBY;
760                 }
761                 else if( !strcasecmp( optarg, "dpl2" ) )
762                 {
763                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
764                                 }
765                 else if( !strcasecmp( optarg, "6ch" ) )
766                 {
767                     audio_mixdown = HB_AMIXDOWN_6CH;
768                 }
769                 break;
770             case 's':
771                 sub = atoi( optarg );
772                 break;
773
774             case '2':
775                 twoPass = 1;
776                 break;
777             case 'd':
778                 deinterlace = 1;
779                 break;
780             case 'g':
781                 grayscale = 1;
782                 break;
783             case 'p':
784                 pixelratio = 1;
785                 break;
786             case 'e':
787                 if( !strcasecmp( optarg, "ffmpeg" ) )
788                 {
789                     vcodec = HB_VCODEC_FFMPEG;
790                 }
791                 else if( !strcasecmp( optarg, "xvid" ) )
792                 {
793                     vcodec = HB_VCODEC_XVID;
794                 }
795                 else if( !strcasecmp( optarg, "x264" ) )
796                 {
797                     vcodec = HB_VCODEC_X264;
798                 }
799                 else if( !strcasecmp( optarg, "x264b13" ) )
800                 {
801                     vcodec = HB_VCODEC_X264;
802                     h264_13 = 1;
803                 }
804                 else if( !strcasecmp( optarg, "x264b30" ) )
805                 {
806                     vcodec = HB_VCODEC_X264;
807                     h264_30 = 1;
808                 }
809                 else
810                 {
811                     fprintf( stderr, "invalid codec (%s)\n", optarg );
812                     return -1;
813                 }
814                 break;
815             case 'E':
816                 if( !strcasecmp( optarg, "ac3" ) )
817                 {
818                     acodec = HB_ACODEC_AC3;
819                 }
820                 else if( !strcasecmp( optarg, "lame" ) )
821                 {
822                     acodec = HB_ACODEC_LAME;
823                 }
824                 break;
825             case 'w':
826                 width = atoi( optarg );
827                 break;
828             case 'l':
829                 height = atoi( optarg );
830                 break;
831             case 'n':
832             {
833                 int    i;
834                 char * tmp = optarg;
835                 for( i = 0; i < 4; i++ )
836                 {
837                     if( !*tmp )
838                         break;
839                     crop[i] = strtol( tmp, &tmp, 0 );
840                     tmp++;
841                 }
842                 break;
843             }
844             case 'r':
845             {
846                 int i;
847                 vrate = 0;
848                 for( i = 0; i < hb_video_rates_count; i++ )
849                 {
850                     if( !strcmp( optarg, hb_video_rates[i].string ) )
851                     {
852                         vrate = hb_video_rates[i].rate;
853                         break;
854                     }
855                 }
856                 if( !vrate )
857                 {
858                     fprintf( stderr, "invalid framerate %s\n", optarg );
859                 }
860                 break;
861             }
862             case 'R':
863             {
864                 int i;
865                 arate = 0;
866                 for( i = 0; i < hb_audio_rates_count; i++ )
867                 {
868                     if( !strcmp( optarg, hb_audio_rates[i].string ) )
869                     {
870                         arate = hb_audio_rates[i].rate;
871                         break;
872                     }
873                 }
874                 if( !arate )
875                 {
876                     fprintf( stderr, "invalid framerate %s\n", optarg );
877                 }
878                 break;
879             }
880             case 'b':
881                 vbitrate = atoi( optarg );
882                 break;
883             case 'q':
884                 vquality = atof( optarg );
885                 break;
886             case 'S':
887                 size = atoi( optarg );
888                 break;
889             case 'B':
890                 abitrate = atoi( optarg );
891                 break;
892                         case 'Q':
893                                 crf = 1;
894                                 break;
895                         case 'x':
896                                 x264opts = strdup( optarg );
897                                 x264opts2 = strdup( optarg );
898                             break;
899                         case 'Y':
900                                 maxHeight = atoi( optarg );
901                                 break;
902                         case 'X':
903                                 maxWidth = atoi (optarg );
904                                 break;
905                                 
906             default:
907                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
908                 return -1;
909         }
910     }
911
912     return 0;
913 }
914
915 static int CheckOptions( int argc, char ** argv )
916 {
917     if( update )
918     {
919         return 0;
920     }
921
922     if( input == NULL || *input == '\0' )
923     {
924         fprintf( stderr, "Missing input device. Run %s --help for "
925                  "syntax.\n", argv[0] );
926         return 1;
927     }
928
929     /* Parse format */
930     if( titleindex > 0 )
931     {
932         if( output == NULL || *output == '\0' )
933         {
934             fprintf( stderr, "Missing output file name. Run %s --help "
935                      "for syntax.\n", argv[0] );
936             return 1;
937         }
938
939         if( !format )
940         {
941             char * p = strrchr( output, '.' );
942
943             /* autodetect */
944             if( p && !strcasecmp( p, ".avi" ) )
945             {
946                 mux = HB_MUX_AVI;
947             }
948             else if( p && ( !strcasecmp( p, ".mp4" )  ||
949                                                         !strcasecmp( p, ".m4v" ) ) )
950             {
951                 if ( h264_30 == 1 )
952                     mux = HB_MUX_IPOD;
953                 else
954                     mux = HB_MUX_MP4;
955             }
956             else if( p && ( !strcasecmp( p, ".ogm" ) ||
957                             !strcasecmp( p, ".ogg" ) ) )
958             {
959                 mux = HB_MUX_OGM;
960             }
961             else
962             {
963                 fprintf( stderr, "Output format couldn't be guessed "
964                          "from file name, using default.\n" );
965                 return 0;
966             }
967         }
968         else if( !strcasecmp( format, "avi" ) )
969         {
970             mux = HB_MUX_AVI;
971         }
972         else if( !strcasecmp( format, "mp4" ) )
973         {
974             if ( h264_30 == 1)
975                 mux = HB_MUX_IPOD;
976             else
977                 mux = HB_MUX_MP4;
978         }
979         else if( !strcasecmp( format, "ogm" ) ||
980                  !strcasecmp( format, "ogg" ) )
981         {
982             mux = HB_MUX_OGM;
983         }
984         else
985         {
986             fprintf( stderr, "Invalid output format (%s). Possible "
987                      "choices are avi, mp4 and ogm\n.", format );
988             return 1;
989         }
990
991         if( !acodec )
992         {
993             if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
994             {
995                 acodec = HB_ACODEC_FAAC;
996             }
997             else if( mux == HB_MUX_AVI )
998             {
999                 acodec = HB_ACODEC_LAME;
1000             }
1001             else if( mux == HB_MUX_OGM )
1002             {
1003                 acodec = HB_ACODEC_VORBIS;
1004             }
1005         }
1006
1007     }
1008
1009     return 0;
1010 }
1011