OSDN Git Service

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