OSDN Git Service

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