OSDN Git Service

CLI: m4v is a valid output format.
[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 (!acodecs)
469                     {
470                         acodecs = strdup("faac,ac3");
471                     }
472
473                     mux = HB_MUX_MP4;
474                     job->largeFileSize = 1;
475                     vcodec = HB_VCODEC_X264;
476                     job->vbitrate = 2500;
477                     default_abitrate = 160;
478                     default_arate = 48000;
479                     default_acodec = HB_ACODEC_FAAC;
480                     x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
481                     job->chapter_markers = 1;
482                     pixelratio = 1;
483                 }
484
485                 if (!strcmp(preset_name, "Bedlam"))
486                 {
487                     mux = HB_MUX_MKV;
488                     vcodec = HB_VCODEC_X264;
489                     job->vbitrate = 1800;
490                     default_acodec = HB_ACODEC_AC3;
491                     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");
492                     job->chapter_markers = 1;
493                     pixelratio = 1;
494                     twoPass = 1;
495                     turbo_opts_enabled = 1;
496                 }
497
498                 if (!strcmp(preset_name, "Blind"))
499                 {
500                     mux = HB_MUX_MP4;
501                     job->vbitrate = 512;
502                     default_abitrate = 128;
503                     default_arate = 48000;
504                     default_acodec = HB_ACODEC_FAAC;
505                     job->width = 512;
506                     job->chapter_markers = 1;
507                 }
508
509                 if (!strcmp(preset_name, "Broke"))
510                 {
511                     mux = HB_MUX_MP4;
512                     vcodec = HB_VCODEC_X264;
513                     size = 695;
514                     default_abitrate = 128;
515                     default_arate = 48000;
516                     default_acodec = HB_ACODEC_FAAC;
517                     job->width = 640;
518                     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");
519                     job->chapter_markers = 1;
520                     twoPass = 1;
521                     turbo_opts_enabled = 1;
522                 }
523
524                 if (!strcmp(preset_name, "Classic"))
525                 {
526                     mux = HB_MUX_MP4;
527                     job->vbitrate = 1000;
528                     default_abitrate = 160;
529                     default_arate = 48000;
530                     default_acodec = HB_ACODEC_FAAC;
531                 }
532
533                 if (!strcmp(preset_name, "Constant Quality Rate"))
534                 {
535                     mux = HB_MUX_MKV;
536                     vcodec = HB_VCODEC_X264;
537                     job->vquality = 0.64709997177124023;
538                     job->crf = 1;
539                     default_acodec = HB_ACODEC_AC3;
540                     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");
541                     job->chapter_markers = 1;
542                     pixelratio = 1;
543                 }
544
545                 if (!strcmp(preset_name, "Deux Six Quatre"))
546                 {
547                     mux = HB_MUX_MKV;
548                     vcodec = HB_VCODEC_X264;
549                     job->vbitrate = 1600;
550                     default_acodec = HB_ACODEC_AC3;
551                     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");
552                     job->chapter_markers = 1;
553                     pixelratio = 1;
554                     twoPass = 1;
555                     turbo_opts_enabled = 1;
556                 }
557
558                 if (!strcmp(preset_name, "Film"))
559                 {
560                     mux = HB_MUX_MKV;
561                     vcodec = HB_VCODEC_X264;
562                     job->vbitrate = 1800;
563                     default_acodec = HB_ACODEC_AC3;
564                     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");
565                     job->chapter_markers = 1;
566                     pixelratio = 1;
567                     twoPass = 1;
568                     turbo_opts_enabled = 1;
569                 }
570
571                 if (!strcmp(preset_name, "iPhone / iPod Touch"))
572                 {
573                     mux = HB_MUX_MP4;
574                     job->ipod_atom = 1;
575                     vcodec = HB_VCODEC_X264;
576                     job->vbitrate = 960;
577                     default_abitrate = 128;
578                     default_arate = 48000;
579                     default_acodec = HB_ACODEC_FAAC;
580                     job->width = 480;
581                     x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
582                     job->chapter_markers = 1;
583                 }
584
585                 if (!strcmp(preset_name, "iPod High-Rez"))
586                 {
587                     mux = HB_MUX_MP4;
588                     job->ipod_atom = 1;
589                     vcodec = HB_VCODEC_X264;
590                     job->vbitrate = 1500;
591                     default_abitrate = 160;
592                     default_arate = 48000;
593                     default_acodec = HB_ACODEC_FAAC;
594                     job->width = 640;
595                     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");
596                     job->chapter_markers = 1;
597                 }
598
599                 if (!strcmp(preset_name, "iPod Low-Rez"))
600                 {
601                     mux = HB_MUX_MP4;
602                     job->ipod_atom = 1;
603                     vcodec = HB_VCODEC_X264;
604                     job->vbitrate = 700;
605                     default_abitrate = 160;
606                     default_arate = 48000;
607                     default_acodec = HB_ACODEC_FAAC;
608                     job->width = 320;
609                     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");
610                     job->chapter_markers = 1;
611                 }
612
613                 if (!strcmp(preset_name, "Normal"))
614                 {
615                     mux = HB_MUX_MP4;
616                     vcodec = HB_VCODEC_X264;
617                     job->vbitrate = 1500;
618                     default_abitrate = 160;
619                     default_arate = 48000;
620                     default_acodec = HB_ACODEC_FAAC;
621                     x264opts = strdup("ref=2:bframes=2:subme=5:me=umh");
622                     job->chapter_markers = 1;
623                     pixelratio = 1;
624                     twoPass = 1;
625                     turbo_opts_enabled = 1;
626                 }
627
628                 if (!strcmp(preset_name, "PS3"))
629                 {
630                     mux = HB_MUX_MP4;
631                     vcodec = HB_VCODEC_X264;
632                     job->vbitrate = 2500;
633                     default_abitrate = 160;
634                     default_arate = 48000;
635                     default_acodec = HB_ACODEC_FAAC;
636                     x264opts = strdup("level=41:subme=5:me=umh");
637                     pixelratio = 1;
638                 }
639
640                 if (!strcmp(preset_name, "PSP"))
641                 {
642                     mux = HB_MUX_MP4;
643                     job->vbitrate = 1024;
644                     default_abitrate = 128;
645                     default_arate = 48000;
646                     default_acodec = HB_ACODEC_FAAC;
647                     job->width = 368;
648                     job->height = 208;
649                     job->chapter_markers = 1;
650                 }
651
652                 if (!strcmp(preset_name, "QuickTime"))
653                 {
654                     mux = HB_MUX_MP4;
655                     vcodec = HB_VCODEC_X264;
656                     job->vbitrate = 2000;
657                     default_abitrate = 160;
658                     default_arate = 48000;
659                     default_acodec = HB_ACODEC_FAAC;
660                     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");
661                     job->chapter_markers = 1;
662                     pixelratio = 1;
663                     twoPass = 1;
664                     turbo_opts_enabled = 1;
665                 }
666
667                 if (!strcmp(preset_name, "Television"))
668                 {
669                     mux = HB_MUX_MKV;
670                     vcodec = HB_VCODEC_X264;
671                     job->vbitrate = 1300;
672                     default_abitrate = 160;
673                     default_arate = 48000;
674                     default_acodec = HB_ACODEC_FAAC;
675                     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");
676                     deinterlace = 1;
677                     deinterlace_opt = "0";
678                     denoise = 1;
679                     denoise_opt = "2:1:2:3";
680                     job->chapter_markers = 1;
681                     twoPass = 1;
682                     turbo_opts_enabled = 1;
683                 }
684
685                 if (!strcmp(preset_name, "Xbox 360"))
686                 {
687                     mux = HB_MUX_MP4;
688                     vcodec = HB_VCODEC_X264;
689                     job->vbitrate = 2000;
690                     default_abitrate = 160;
691                     default_arate = 48000;
692                     default_acodec = HB_ACODEC_FAAC;
693                     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");
694                     pixelratio = 1;
695                 }
696             }
697
698                         if ( chapter_markers )
699                         {
700                                 job->chapter_markers = chapter_markers;
701
702                 if( marker_file != NULL )
703                 {
704                     hb_csv_file_t * file = hb_open_csv_file( marker_file );
705                     hb_csv_cell_t * cell;
706                     int row = 0;
707                     int chapter = 0;
708
709                     fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
710
711                     if( file == NULL )
712                     {
713                          fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
714                     }
715                     else
716                     {
717                         /* Parse the cells */
718                         while( NULL != ( cell = hb_read_next_cell( file ) ) )
719                         {
720                             /* We have a chapter number */
721                             if( cell->cell_col == 0 )
722                             {
723                                 row = cell->cell_row;
724                                 chapter = atoi( cell->cell_text );
725                             }
726
727                             /* We have a chapter name */
728                             if( cell->cell_col == 1 && row == cell->cell_row )
729                             {
730                                 /* If we have a valid chapter, copy the string an terminate it */
731                                 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
732                                 {
733                                     hb_chapter_t * chapter_s;
734
735                                     chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
736                                     strncpy(chapter_s->title, cell->cell_text, 1023);
737                                     chapter_s->title[1023] = '\0';
738                                 }
739                             }
740
741
742                             hb_dispose_cell( cell );
743                         }
744
745                         hb_close_csv_file( file );
746                     }
747                 }
748                         }
749
750             if( crop[0] >= 0 && crop[1] >= 0 &&
751                 crop[2] >= 0 && crop[3] >= 0 )
752             {
753                 memcpy( job->crop, crop, 4 * sizeof( int ) );
754             }
755
756             job->deinterlace = deinterlace;
757             job->grayscale   = grayscale;
758             if (loosePixelratio)
759             {
760                 job->pixel_ratio = 2;
761                 if (modulus)
762                 {
763                     job->modulus = modulus;
764                 }
765             }
766             else
767             {
768                 job->pixel_ratio = pixelratio;
769             }
770
771             if (vfr)
772             {
773                 detelecine = 1;
774                 job->vfr = 1;
775             }
776
777             /* Add selected filters */
778             job->filters = hb_list_init();
779             if( detelecine )
780             {
781                 hb_filter_detelecine.settings = detelecine_opt;
782                 hb_list_add( job->filters, &hb_filter_detelecine );
783             }
784             if( deinterlace )
785             {
786                 hb_filter_deinterlace.settings = deinterlace_opt;
787                 hb_list_add( job->filters, &hb_filter_deinterlace );
788             }
789             if( deblock )
790             {
791                 hb_filter_deblock.settings = deblock_opt;
792                 hb_list_add( job->filters, &hb_filter_deblock );
793             }
794             if( denoise )
795             {
796                 hb_filter_denoise.settings = denoise_opt;
797                 hb_list_add( job->filters, &hb_filter_denoise );
798             }
799
800             if( width && height )
801             {
802                 job->width  = width;
803                 job->height = height;
804             }
805             else if( width )
806             {
807                 job->width = width;
808                 hb_fix_aspect( job, HB_KEEP_WIDTH );
809             }
810             else if( height && !loosePixelratio)
811             {
812                 job->height = height;
813                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
814             }
815             else if( !width && !height && !pixelratio && !loosePixelratio )
816             {
817                 hb_fix_aspect( job, HB_KEEP_WIDTH );
818             }
819             else if (!width && loosePixelratio)
820             {
821                 /* Default to full width when one isn't specified for loose anamorphic */
822                 job->width = title->width - job->crop[2] - job->crop[3];
823                 /* The height will be thrown away in hb.c but calculate it anyway */
824                 hb_fix_aspect( job, HB_KEEP_WIDTH );
825             }
826
827             if( vquality >= 0.0 && vquality <= 1.0 )
828             {
829                 job->vquality = vquality;
830                 job->vbitrate = 0;
831             }
832             else if( vbitrate )
833             {
834                 job->vquality = -1.0;
835                 job->vbitrate = vbitrate;
836             }
837             if( vcodec )
838             {
839                 job->vcodec = vcodec;
840             }
841             if( h264_13 )
842             {
843                 job->h264_level = 13;
844             }
845                 if( h264_30 )
846                 {
847                     job->h264_level = 30;
848             }
849             if( vrate )
850             {
851                 job->vrate = 27000000;
852                 job->vrate_base = vrate;
853             }
854
855             /* Parse audio tracks */
856             if( hb_list_count(audios) == 0 )
857             {
858                 /* Create a new audio track with default settings */
859                 audio = calloc(1, sizeof(*audio));
860                 hb_audio_config_init(audio);
861                 /* Add it to our audios */
862                 hb_list_add(audios, audio);
863             }
864
865             num_audio_tracks = hb_list_count(audios);
866             for (i = 0; i < num_audio_tracks; i++)
867             {
868                 audio = hb_list_item(audios, 0);
869                 if( (audio == NULL) || (audio->in.track == -1) ||
870                     (audio->out.track == -1) || (audio->out.codec == 0) )
871                 {
872                     num_audio_tracks--;
873                 }
874                 else
875                 {
876                     hb_audio_add( job, audio );
877                 }
878                 hb_list_rem(audios, audio);
879                 if( audio != NULL)
880                     free( audio );
881             }
882             /* Audio Tracks */
883
884             /* Audio Codecs */
885             i = 0;
886             if( acodecs )
887             {
888                 char * token = strtok(acodecs, ",");
889                 if( token == NULL )
890                     token = acodecs;
891                 while ( token != NULL )
892                 {
893                     if ((acodec = get_acodec_for_string(token)) == -1)
894                     {
895                         fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
896                         acodec = default_acodec;
897                     }
898                     if( i < num_audio_tracks )
899                     {
900                         audio = hb_list_audio_config_item(job->list_audio, i);                        
901                         audio->out.codec = acodec;
902                     }
903                     if( i >= num_audio_tracks )
904                     {
905                         int last_track = hb_list_count(job->list_audio);
906                         fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
907                             last_track, token);
908                         hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, last_track - 1 );
909                         audio = calloc(1, sizeof(*audio));
910                         hb_audio_config_init(audio);
911                         audio->in.track = last_audio->in.track;
912                         audio->out.track = num_audio_tracks++;
913                         audio->out.codec = acodec;
914                         hb_audio_add(job, audio);
915                         free( audio );
916                     }
917                     token = strtok(NULL, ",");
918                     i++;
919                 }
920             }
921             if( i < num_audio_tracks )
922             {
923                 /* We have fewer inputs than audio tracks, use the default codec for
924                  * this container for the remaining tracks. Unless we only have one input
925                  * then use that codec instead.
926                  */
927                 if (i != 1)
928                     acodec = default_acodec;
929                 for ( ; i < num_audio_tracks; i++)
930                 {
931                     audio = hb_list_audio_config_item(job->list_audio, i);
932                     audio->out.codec = acodec;
933                 }
934             }
935             /* Audio Codecs */
936
937             /* Sample Rate */
938             i = 0;
939             if( arates )
940             {
941                 char * token = strtok(arates, ",");
942                 if (token == NULL)
943                     token = arates;
944                 while ( token != NULL )
945                 {
946                     arate = atoi(token);
947                     audio = hb_list_audio_config_item(job->list_audio, i);
948                     if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
949                     {
950                         /* Would probably be better to have this handled in libhb. */
951                         audio->out.samplerate = audio->in.samplerate;
952                     }
953                     else
954                     {
955                         if (!is_sample_rate_valid(arate))
956                         {
957                             fprintf(stderr, "Invalid sample rate %d, using default\n", arate);
958                             arate = default_arate;
959                         }
960                         audio->out.samplerate = arate;
961                     }
962                     if( (++i) >= num_audio_tracks )
963                         break;  /* We have more inputs than audio tracks, oops */
964                     token = strtok(NULL, ",");
965                 }
966             }
967             if (i < num_audio_tracks)
968             {
969                 /* We have fewer inputs than audio tracks, use default sample rate.
970                  * Unless we only have one input, then use that for all tracks.
971                  */
972                 if (i != 1)
973                     arate = default_arate;
974                 for ( ; i < num_audio_tracks; i++)
975                 {
976                     audio = hb_list_audio_config_item(job->list_audio, i);
977                     if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
978                     {
979                         audio->out.samplerate = audio->in.samplerate;
980                         continue;
981                     }
982                     audio->out.samplerate = arate;
983                 }
984             }
985             /* Sample Rate */
986
987             /* Audio Bitrate */
988             i = 0;
989             if( abitrates )
990             {
991                 char * token = strtok(abitrates, ",");
992                 if (token == NULL)
993                     token = abitrates;
994                 while ( token != NULL )
995                 {
996                     abitrate = atoi(token);
997                     audio = hb_list_audio_config_item(job->list_audio, i);
998                     if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
999                     {
1000                         audio->out.bitrate = audio->in.bitrate;   /* See note above about arate. */
1001                     }
1002                     else
1003                     {
1004                         audio->out.bitrate = abitrate;
1005                     }
1006                     if( (++i) >= num_audio_tracks )
1007                         break;  /* We have more inputs than audio tracks, oops */
1008                     token = strtok(NULL, ",");
1009                 }
1010             }
1011             if (i < num_audio_tracks)
1012             {
1013                 /* We have fewer inputs than audio tracks, use the default bitrate
1014                  * for the remaining tracks. Unless we only have one input, then use
1015                  * that for all tracks.
1016                  */
1017                 if (i != 1)
1018                     abitrate = default_abitrate;
1019                 for (; i < num_audio_tracks; i++)
1020                 {
1021                     audio = hb_list_audio_config_item(job->list_audio, i);
1022                     if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1023                     {
1024                         audio->out.bitrate = audio->in.bitrate;
1025                         continue;
1026                     }
1027                     audio->out.bitrate = abitrate;
1028                 }
1029             }
1030             /* Audio Bitrate */
1031
1032             /* Audio DRC */
1033             i = 0;
1034             if ( dynamic_range_compression )
1035             {
1036                 char * token = strtok(dynamic_range_compression, ",");
1037                 if (token == NULL)
1038                     token = dynamic_range_compression;
1039                 while ( token != NULL )
1040                 {
1041                     d_r_c = atof(token);
1042                     audio = hb_list_audio_config_item(job->list_audio, i);
1043                     audio->out.dynamic_range_compression = d_r_c;
1044                     if( (++i) >= num_audio_tracks )
1045                         break;  /* We have more inputs than audio tracks, oops */
1046                     token = strtok(NULL, ",");
1047                 }
1048             }
1049             if (i < num_audio_tracks)
1050             {
1051                 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1052                  * tracks. Unless we only have one input, then use the same DRC for all
1053                  * tracks.
1054                  */
1055                 if (i != 1)
1056                     d_r_c = 0;
1057                 for (; i < num_audio_tracks; i++)
1058                 {
1059                     audio = hb_list_audio_config_item(job->list_audio, i);
1060                     audio->out.dynamic_range_compression = d_r_c;
1061                 }
1062             }
1063             /* Audio DRC */
1064
1065             /* Audio Mixdown */
1066             i = 0;
1067             if ( mixdowns )
1068             {
1069                 char * token = strtok(mixdowns, ",");
1070                 if (token == NULL)
1071                     token = mixdowns;
1072                 while ( token != NULL )
1073                 {
1074                     mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1075                     audio = hb_list_audio_config_item(job->list_audio, i);
1076                     audio->out.mixdown = mixdown;
1077                     if( (++i) >= num_audio_tracks )
1078                         break;  /* We have more inputs than audio tracks, oops */
1079                     token = strtok(NULL, ",");
1080                 }
1081             }
1082             if (i < num_audio_tracks)
1083             {
1084                 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1085                  * we only have one input, then use that.
1086                  */
1087                 if (i != 1)
1088                     mixdown = HB_AMIXDOWN_DOLBYPLII;
1089                 for (; i < num_audio_tracks; i++)
1090                 {
1091                    audio = hb_list_audio_config_item(job->list_audio, i);
1092                    audio->out.mixdown = mixdown;
1093                 }
1094             }
1095             /* Audio Mixdown */
1096
1097             if( size )
1098             {
1099                 job->vbitrate = hb_calc_bitrate( job, size );
1100                 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1101                          job->vbitrate );
1102             }
1103
1104             if( sub )
1105             {
1106                 job->subtitle = sub - 1;
1107             }
1108
1109             if( native_language )
1110             {
1111                 job->native_language = strdup( native_language );
1112             }
1113
1114             if( job->mux )
1115             {
1116                 job->mux = mux;
1117             }
1118
1119             if ( largeFileSize )
1120             {
1121                 job->largeFileSize = 1;
1122             }
1123             if ( mp4_optimize )
1124             {
1125                 job->mp4_optimize = 1;
1126             }
1127             if ( ipod_atom )
1128             {
1129                 job->ipod_atom = 1;
1130             }
1131
1132             job->file = strdup( output );
1133
1134             if( crf )
1135             {
1136                 job->crf = 1;
1137             }
1138
1139             if( x264opts != NULL && *x264opts != '\0' )
1140             {
1141                 job->x264opts = x264opts;
1142             }
1143             else /*avoids a bus error crash when options aren't specified*/
1144             {
1145                 job->x264opts =  NULL;
1146             }
1147             if (maxWidth)
1148                 job->maxWidth = maxWidth;
1149             if (maxHeight)
1150                 job->maxHeight = maxHeight;
1151
1152             if( subtitle_force )
1153             {
1154                 job->subtitle_force = subtitle_force;
1155             }
1156
1157             if( subtitle_scan )
1158             {
1159                 char *x264opts_tmp;
1160
1161                 /*
1162                  * When subtitle scan is enabled do a fast pre-scan job
1163                  * which will determine which subtitles to enable, if any.
1164                  */
1165                 job->pass = -1;
1166
1167                 x264opts_tmp = job->x264opts;
1168
1169                 job->x264opts = NULL;
1170
1171                 job->indepth_scan = subtitle_scan;
1172                 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1173                          "subtitles if found for foreign language segments\n");
1174                 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1175                 *(job->select_subtitle) = NULL;
1176
1177                 /*
1178                  * Add the pre-scan job
1179                  */
1180                 hb_add( h, job );
1181
1182                 job->x264opts = x264opts_tmp;
1183             }
1184
1185             if( twoPass )
1186             {
1187                 /*
1188                  * If subtitle_scan is enabled then only turn it on
1189                  * for the first pass and then off again for the
1190                  * second.
1191                  */
1192                 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1193
1194                 job->select_subtitle = NULL;
1195
1196                 job->pass = 1;
1197
1198                 job->indepth_scan = 0;
1199
1200                 if (x264opts)
1201                 {
1202                     x264opts2 = strdup(x264opts);
1203                 }
1204
1205                 /*
1206                  * If turbo options have been selected then append them
1207                  * to the x264opts now (size includes one ':' and the '\0')
1208                  */
1209                 if( turbo_opts_enabled )
1210                 {
1211                     int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1212                     char *tmp_x264opts;
1213
1214                     tmp_x264opts = malloc(size * sizeof(char));
1215                     if( x264opts )
1216                     {
1217                         snprintf( tmp_x264opts, size, "%s:%s",
1218                                   x264opts, turbo_opts );
1219                         free( x264opts );
1220                     } else {
1221                         /*
1222                          * No x264opts to modify, but apply the turbo options
1223                          * anyway as they may be modifying defaults
1224                          */
1225                         snprintf( tmp_x264opts, size, "%s",
1226                                   turbo_opts );
1227                     }
1228                     x264opts = tmp_x264opts;
1229
1230                     fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1231                              x264opts );
1232
1233                     job->x264opts = x264opts;
1234                 }
1235                 hb_add( h, job );
1236
1237                 job->select_subtitle = subtitle_tmp;
1238
1239                 job->pass = 2;
1240                 /*
1241                  * On the second pass we turn off subtitle scan so that we
1242                  * can actually encode using any subtitles that were auto
1243                  * selected in the first pass (using the whacky select-subtitle
1244                  * attribute of the job).
1245                  */
1246                 job->indepth_scan = 0;
1247
1248                 job->x264opts = x264opts2;
1249
1250                 hb_add( h, job );
1251             }
1252             else
1253             {
1254                 /*
1255                  * Turn on subtitle scan if requested, note that this option
1256                  * precludes encoding of any actual subtitles.
1257                  */
1258
1259                 job->indepth_scan = 0;
1260                 job->pass = 0;
1261                 hb_add( h, job );
1262             }
1263             hb_start( h );
1264             break;
1265         }
1266
1267 #define p s.param.working
1268         case HB_STATE_WORKING:
1269             fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1270                      p.job_cur, p.job_count, 100.0 * p.progress );
1271             if( p.seconds > -1 )
1272             {
1273                 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1274                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1275                          p.hours, p.minutes, p.seconds );
1276             }
1277             fflush(stdout);
1278             break;
1279 #undef p
1280
1281 #define p s.param.muxing
1282         case HB_STATE_MUXING:
1283         {
1284             if (show_mux_warning)
1285             {
1286                 fprintf( stdout, "\rMuxing: this may take awhile..." );
1287                 fflush(stdout);
1288                 show_mux_warning = 0;
1289             }
1290             break;
1291         }
1292 #undef p
1293
1294 #define p s.param.workdone
1295         case HB_STATE_WORKDONE:
1296             /* Print error if any, then exit */
1297             switch( p.error )
1298             {
1299                 case HB_ERROR_NONE:
1300                     fprintf( stderr, "\nRip done!\n" );
1301                     break;
1302                 case HB_ERROR_CANCELED:
1303                     fprintf( stderr, "\nRip canceled.\n" );
1304                     break;
1305                 default:
1306                     fprintf( stderr, "\nRip failed (error %x).\n",
1307                              p.error );
1308             }
1309             die = 1;
1310             break;
1311 #undef p
1312     }
1313     return 0;
1314 }
1315
1316 /****************************************************************************
1317  * SigHandler:
1318  ****************************************************************************/
1319 static volatile int64_t i_die_date = 0;
1320 void SigHandler( int i_signal )
1321 {
1322     if( die == 0 )
1323     {
1324         die = 1;
1325         i_die_date = hb_get_date();
1326         fprintf( stderr, "Signal %d received, terminating - do it "
1327                  "again in case it gets stuck\n", i_signal );
1328     }
1329     else if( i_die_date + 500 < hb_get_date() )
1330     {
1331         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1332         exit( 1 );
1333     }
1334 }
1335
1336 /****************************************************************************
1337  * ShowHelp:
1338  ****************************************************************************/
1339 static void ShowHelp()
1340 {
1341     int i;
1342
1343     fprintf( stderr,
1344     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1345     "\n"
1346         "### General Handbrake Options------------------------------------------------\n\n"
1347     "    -h, --help              Print help\n"
1348     "    -u, --update            Check for updates and exit\n"
1349     "    -v, --verbose           Be verbose\n"
1350     "    -C, --cpu               Set CPU count (default: autodetected)\n"
1351     "    -Z. --preset <string>   Use a built-in preset. Capitalization matters, and\n"
1352     "                            if the preset name has spaces, surround it with\n"
1353     "                            double quotation marks\n"
1354     "    -z, --preset-list       See a list of available built-in presets\n"
1355     "\n"
1356
1357         "### Source Options-----------------------------------------------------------\n\n"
1358         "    -i, --input <string>    Set input device\n"
1359         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
1360     "                            default: 1)\n"
1361     "    -L, --longest           Select the longest title\n"
1362     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1363     "                            1 to 3, or \"3\" for chapter 3 only,\n"
1364     "                            default: all chapters)\n"
1365         "\n"
1366
1367         "### Destination Options------------------------------------------------------\n\n"
1368     "    -o, --output <string>   Set output file name\n"
1369         "    -f, --format <string>   Set output format (avi/mp4/ogm/mkv, default:\n"
1370     "                            autodetected from file name)\n"
1371     "    -4, --large-file        Use 64-bit mp4 files that can hold more than\n"
1372     "                            4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1373     "    -O, --optimize          Optimize mp4 files for HTTP streaming\n"
1374     "    -I, --ipod-atom         Mark mp4 files so iPods will accept them\n"
1375     "\n"
1376
1377         "### Picture Settings---------------------------------------------------------\n\n"
1378     "    -w, --width <number>    Set picture width\n"
1379     "    -l, --height <number>   Set picture height\n"
1380     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
1381         "    -Y, --maxHeight <#>     Set maximum height\n"
1382         "    -X, --maxWidth <#>      Set maximum width\n"
1383         "    -s, --subtitle <number> Select subtitle (default: none)\n"
1384     "    -U, --subtitle-scan     Scan for subtitles in an extra 1st pass, and choose\n"
1385     "                            the one that's only used 10 percent of the time\n"
1386     "                            or less. This should locate subtitles for short\n"
1387     "                            foreign language segments. Best used in conjunction\n"
1388     "                            with --subtitle-forced.\n"
1389     "    -F, --subtitle-forced   Only display subtitles from the selected stream if\n"
1390     "                            the subtitle has the forced flag set. May be used in\n"
1391     "                            conjunction with --subtitle-scan to auto-select\n"
1392     "                            a stream if it contains forced subtitles.\n"
1393     "    -N, --native-language   Select subtitles with this language if it does not\n"
1394     "          <string>          match the Audio language. Provide the language's\n"
1395     "                            iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1396         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
1397         "\n"
1398
1399         "### Video Options------------------------------------------------------------\n\n"
1400         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
1401     "                            x264,theora default: ffmpeg)\n"
1402         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
1403         "    -Q, --cqp               Use with -q for CQP instead of CRF\n"
1404     "    -S, --size <MB>         Set target size\n"
1405         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
1406         "    -r, --rate              Set video framerate (" );
1407     for( i = 0; i < hb_video_rates_count; i++ )
1408     {
1409         fprintf( stderr, hb_video_rates[i].string );
1410         if( i != hb_video_rates_count - 1 )
1411             fprintf( stderr, "/" );
1412     }
1413     fprintf( stderr, ")\n"
1414         "\n"
1415         "    -2, --two-pass          Use two-pass mode\n"
1416      "    -d, --deinterlace       Deinterlace video with yadif/mcdeint filter\n"
1417      "          <YM:FD:MM:QP>     (default 0:-1:-1:1)\n"
1418      "           or\n"
1419      "          <fast/slow/slower>\n"
1420      "    -7, --deblock           Deblock video with pp7 filter\n"
1421      "          <QP:M>            (default 0:2)\n"
1422      "    -8, --denoise           Denoise video with hqdn3d filter\n"
1423      "          <SL:SC:TL:TC>     (default 4:3:6:4.5)\n"
1424      "           or\n"
1425      "          <weak/medium/strong>\n"
1426      "    -9, --detelecine        Detelecine video with pullup filter\n"
1427      "          <L:R:T:B:SB:MP>   (default 1:1:4:4:0:0)\n"
1428     "    -g, --grayscale         Grayscale encoding\n"
1429     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
1430     "    -P, --loosePixelratio   Store pixel aspect ratio with specified width\n"
1431     "           <modulus>        Takes as optional argument what number you want\n"
1432     "                            the dimensions to divide cleanly by (default 16)\n"
1433
1434
1435         "\n"
1436
1437
1438         "### Audio Options-----------------------------------------------------------\n\n"
1439         "    -a, --audio <string>    Select audio channel(s), separated by commas\n"
1440     "                            More than one output track can be used for one\n"
1441     "                            input.\n"
1442     "                            (\"none\" for no audio, \"1,2,3\" for multiple\n"
1443     "                             tracks, default: first one)\n"
1444     "    -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1445         "                            ac3 meaning passthrough\n"
1446         "                            Seperated by commas for more than one audio track.\n"
1447         "                            (default: guessed)\n"
1448         "    -B, --ab <kb/s>         Set audio bitrate(s)  (default: 128)\n"
1449     "                            Seperated by commas for more than one audio track.\n"
1450         "    -6, --mixdown <string>  Format(s) for surround sound downmixing\n"
1451     "                            Seperated by commas for more than one audio track.\n"
1452     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1453     "    -R, --arate             Set audio samplerate(s) (" );
1454     for( i = 0; i < hb_audio_rates_count; i++ )
1455     {
1456         fprintf( stderr, hb_audio_rates[i].string );
1457         if( i != hb_audio_rates_count - 1 )
1458             fprintf( stderr, "/" );
1459     }
1460     fprintf( stderr, " kHz)\n"
1461     "                            Seperated by commas for more than one audio track.\n"
1462     "    -D, --drc <float>       Apply extra dynamic range compression to the audio,\n"
1463     "                            making soft sounds louder. Range is 1.0 to 4.0\n"
1464     "                            (too loud), with 1.5 - 2.5 being a useful range.\n"
1465     "                            Seperated by commas for more than one audio track.\n"
1466
1467
1468         "\n"
1469
1470
1471     "### Advanced Options---------------------------------------------------------\n\n"
1472     "    -x, --x264opts <string> Specify advanced x264 options in the\n"
1473     "                            same style as mencoder:\n"
1474     "                            option1=value1:option2=value2\n"
1475     "    -T, --turbo             When using 2-pass use the turbo options\n"
1476     "                            on the first pass to improve speed\n"
1477     "                            (only works with x264, affects PSNR by about 0.05dB,\n"
1478     "                            and increases first pass speed two to four times)\n"
1479     "    -V, --vfr               Perform variable framerate detelecine on NTSC video\n"
1480     );
1481 }
1482
1483 /****************************************************************************
1484  * ShowPresets:
1485  ****************************************************************************/
1486 static void ShowPresets()
1487 {
1488     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");
1489
1490     printf("\n+ AppleTV:  -e x264 -b 2500 -B 160 -R 48 -E faac,ac3 -f mp4 -4 -m -p -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0\n");
1491
1492     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");
1493
1494     printf("\n+ Blind:  -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1495
1496     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");
1497
1498     printf("\n+ Classic:  -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1499
1500     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");
1501
1502     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");
1503
1504     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");
1505
1506     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");
1507
1508     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");
1509
1510     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");
1511
1512     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");
1513
1514     printf("\n+ PS3:  -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1515
1516     printf("\n+ PSP:  -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1517
1518     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");
1519
1520     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");
1521
1522     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");
1523 }
1524
1525 /****************************************************************************
1526  * ParseOptions:
1527  ****************************************************************************/
1528 static int ParseOptions( int argc, char ** argv )
1529 {
1530     for( ;; )
1531     {
1532         static struct option long_options[] =
1533           {
1534             { "help",        no_argument,       NULL,    'h' },
1535             { "update",      no_argument,       NULL,    'u' },
1536             { "verbose",     no_argument,       NULL,    'v' },
1537             { "cpu",         required_argument, NULL,    'C' },
1538
1539             { "format",      required_argument, NULL,    'f' },
1540             { "input",       required_argument, NULL,    'i' },
1541             { "output",      required_argument, NULL,    'o' },
1542             { "large-file",  no_argument,       NULL,    '4' },
1543             { "optimize",    no_argument,       NULL,    'O' },
1544             { "ipod-atom",   no_argument,       NULL,    'I' },
1545
1546             { "title",       required_argument, NULL,    't' },
1547             { "longest",     no_argument,       NULL,    'L' },
1548             { "chapters",    required_argument, NULL,    'c' },
1549             { "markers",     optional_argument, NULL,    'm' },
1550             { "audio",       required_argument, NULL,    'a' },
1551             { "mixdown",     required_argument, NULL,    '6' },
1552             { "drc",         required_argument, NULL,    'D' },
1553             { "subtitle",    required_argument, NULL,    's' },
1554             { "subtitle-scan", no_argument,     NULL,    'U' },
1555             { "subtitle-forced", no_argument,   NULL,    'F' },
1556             { "native-language", required_argument, NULL,'N' },
1557
1558             { "encoder",     required_argument, NULL,    'e' },
1559             { "aencoder",    required_argument, NULL,    'E' },
1560             { "two-pass",    no_argument,       NULL,    '2' },
1561             { "deinterlace", optional_argument, NULL,    'd' },
1562             { "deblock",     optional_argument, NULL,    '7' },
1563             { "denoise",     optional_argument, NULL,    '8' },
1564             { "detelecine",  optional_argument, NULL,    '9' },
1565             { "grayscale",   no_argument,       NULL,    'g' },
1566             { "pixelratio",  no_argument,       NULL,    'p' },
1567             { "loosePixelratio", optional_argument,   NULL,    'P' },
1568             { "width",       required_argument, NULL,    'w' },
1569             { "height",      required_argument, NULL,    'l' },
1570             { "crop",        required_argument, NULL,    'n' },
1571
1572             { "vb",          required_argument, NULL,    'b' },
1573             { "quality",     required_argument, NULL,    'q' },
1574             { "size",        required_argument, NULL,    'S' },
1575             { "ab",          required_argument, NULL,    'B' },
1576             { "rate",        required_argument, NULL,    'r' },
1577             { "arate",       required_argument, NULL,    'R' },
1578             { "cqp",         no_argument,       NULL,    'Q' },
1579             { "x264opts",    required_argument, NULL,    'x' },
1580             { "turbo",       no_argument,       NULL,    'T' },
1581             { "maxHeight",   required_argument, NULL,    'Y' },
1582             { "maxWidth",    required_argument, NULL,    'X' },
1583             { "preset",      required_argument, NULL,    'Z' },
1584             { "preset-list", no_argument,       NULL,    'z' },
1585             { "vfr",         no_argument,       NULL,    'V' },
1586
1587             { 0, 0, 0, 0 }
1588           };
1589
1590         int option_index = 0;
1591         int c;
1592
1593                 c = getopt_long( argc, argv,
1594                                                  "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",
1595                          long_options, &option_index );
1596         if( c < 0 )
1597         {
1598             break;
1599         }
1600
1601         switch( c )
1602         {
1603             case 'h':
1604                 ShowHelp();
1605                 exit( 0 );
1606             case 'u':
1607                 update = 1;
1608                 break;
1609             case 'v':
1610                 debug = HB_DEBUG_ALL;
1611                 break;
1612             case 'C':
1613                 cpu = atoi( optarg );
1614                 break;
1615
1616             case 'Z':
1617                 preset = 1;
1618                 preset_name = strdup(optarg);
1619                 break;
1620             case 'z':
1621                 ShowPresets();
1622                 exit ( 0 );
1623
1624             case 'f':
1625                 format = strdup( optarg );
1626                 break;
1627             case 'i':
1628                 input = strdup( optarg );
1629                 break;
1630             case 'o':
1631                 output = strdup( optarg );
1632                 break;
1633             case '4':
1634                 largeFileSize = 1;
1635                 break;
1636             case 'O':
1637                 mp4_optimize = 1;
1638                 break;
1639             case 'I':
1640                 ipod_atom = 1;
1641                 break;
1642
1643             case 't':
1644                 titleindex = atoi( optarg );
1645                 break;
1646             case 'L':
1647                 longest_title = 1;
1648                 break;
1649             case 'c':
1650             {
1651                 int start, end;
1652                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1653                 {
1654                     chapter_start = start;
1655                     chapter_end   = end;
1656                 }
1657                 else if( sscanf( optarg, "%d", &start ) == 1 )
1658                 {
1659                     chapter_start = start;
1660                     chapter_end   = chapter_start;
1661                 }
1662                 else
1663                 {
1664                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
1665                              optarg );
1666                     return -1;
1667                 }
1668                 break;
1669             }
1670             case 'm':
1671                 if( optarg != NULL )
1672                 {
1673                     marker_file = strdup( optarg );
1674                 }
1675                 chapter_markers = 1;
1676                 break;
1677             case 'a':
1678             {
1679                 char * token = strtok(optarg, ",");
1680                 if (token == NULL)
1681                     token = optarg;
1682                 int track_start, track_end;
1683                 while( token != NULL )
1684                 {
1685                     audio = calloc(1, sizeof(*audio));
1686                     hb_audio_config_init(audio);
1687                     if (strlen(token) >= 3)
1688                     {
1689                         if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1690                         {
1691                             int i;
1692                             for (i = track_start - 1; i < track_end; i++)
1693                             {
1694                                 if (i != track_start - 1)
1695                                 {
1696                                     audio = calloc(1, sizeof(*audio));
1697                                     hb_audio_config_init(audio);
1698                                 }
1699                                 audio->in.track = i;
1700                                 audio->out.track = num_audio_tracks++;
1701                                 hb_list_add(audios, audio);
1702                             }
1703                         }
1704                         else if( !strcasecmp(token, "none" ) )
1705                         {
1706                             audio->in.track = audio->out.track = -1;
1707                             audio->out.codec = 0;
1708                             hb_list_add(audios, audio);
1709                             break;
1710                         }
1711                         else
1712                         {
1713                             fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1714                                     token);
1715                             free(audio);
1716                         }
1717                     }
1718                     else
1719                     {
1720                         audio->in.track = atoi(token) - 1;
1721                         audio->out.track = num_audio_tracks++;
1722                         hb_list_add(audios, audio);
1723                     }
1724                     token = strtok(NULL, ",");
1725                 }
1726                 break;
1727             }
1728             case '6':
1729                 if( optarg != NULL )
1730                 {
1731                     mixdowns = strdup( optarg );
1732                 }
1733                 break;
1734             case 'D':
1735                 if( optarg != NULL )
1736                 {
1737                     dynamic_range_compression = strdup( optarg );
1738                 }
1739                 break;
1740             case 's':
1741                 sub = atoi( optarg );
1742                 break;
1743             case 'U':
1744                 subtitle_scan = 1;
1745                 break;
1746             case 'F':
1747                 subtitle_force = 1;
1748                 break;
1749             case 'N':
1750                 native_language = strdup( optarg );
1751                 break;
1752             case '2':
1753                 twoPass = 1;
1754                 break;
1755             case 'd':
1756                 if( optarg != NULL )
1757                 {
1758                     if (!( strcmp( optarg, "fast" ) ))
1759                     {
1760                         deinterlace_opt = "-1";
1761                     }
1762                     else if (!( strcmp( optarg, "slow" ) ))
1763                     {
1764                         deinterlace_opt = "2";
1765                     }
1766                     else if (!( strcmp( optarg, "slower" ) ))
1767                     {
1768                         deinterlace_opt = "0";
1769                     }
1770                     else
1771                     {
1772                         deinterlace_opt = strdup( optarg );
1773                     }
1774                 }
1775                 deinterlace = 1;
1776                 break;
1777             case '7':
1778                 if( optarg != NULL )
1779                 {
1780                     deblock_opt = strdup( optarg );
1781                 }
1782                 deblock = 1;
1783                 break;
1784             case '8':
1785                 if( optarg != NULL )
1786                 {
1787                     if (!( strcmp( optarg, "weak" ) ))
1788                     {
1789                         denoise_opt = "2:1:2:3";
1790                     }
1791                     else if (!( strcmp( optarg, "medium" ) ))
1792                     {
1793                         denoise_opt = "3:2:2:3";
1794                     }
1795                     else if (!( strcmp( optarg, "strong" ) ))
1796                     {
1797                         denoise_opt = "7:7:5:5";
1798                     }
1799                     else
1800                     {
1801                         denoise_opt = strdup( optarg );
1802                     }
1803                 }
1804                 denoise = 1;
1805                 break;
1806             case '9':
1807                 if( optarg != NULL )
1808                 {
1809                     detelecine_opt = strdup( optarg );
1810                 }
1811                 detelecine = 1;
1812                 break;
1813             case 'g':
1814                 grayscale = 1;
1815                 break;
1816             case 'p':
1817                 pixelratio = 1;
1818                 break;
1819             case 'P':
1820                 loosePixelratio = 1;
1821                 if( optarg != NULL )
1822                 {
1823                     modulus = atoi( optarg );
1824                 }
1825                 break;
1826             case 'e':
1827                 if( !strcasecmp( optarg, "ffmpeg" ) )
1828                 {
1829                     vcodec = HB_VCODEC_FFMPEG;
1830                 }
1831                 else if( !strcasecmp( optarg, "xvid" ) )
1832                 {
1833                     vcodec = HB_VCODEC_XVID;
1834                 }
1835                 else if( !strcasecmp( optarg, "x264" ) )
1836                 {
1837                     vcodec = HB_VCODEC_X264;
1838                 }
1839                 else if( !strcasecmp( optarg, "x264b13" ) )
1840                 {
1841                     vcodec = HB_VCODEC_X264;
1842                     h264_13 = 1;
1843                 }
1844                 else if( !strcasecmp( optarg, "x264b30" ) )
1845                 {
1846                     vcodec = HB_VCODEC_X264;
1847                     h264_30 = 1;
1848                 }
1849                 else if( !strcasecmp( optarg, "theora" ) )
1850                 {
1851                     vcodec = HB_VCODEC_THEORA;
1852                 }
1853                 else
1854                 {
1855                     fprintf( stderr, "invalid codec (%s)\n", optarg );
1856                     return -1;
1857                 }
1858                 break;
1859             case 'E':
1860                 if( optarg != NULL )
1861                 {
1862                     acodecs = strdup( optarg );
1863                 }
1864                 break;
1865             case 'w':
1866                 width = atoi( optarg );
1867                 break;
1868             case 'l':
1869                 height = atoi( optarg );
1870                 break;
1871             case 'n':
1872             {
1873                 int    i;
1874                 char * tmp = optarg;
1875                 for( i = 0; i < 4; i++ )
1876                 {
1877                     if( !*tmp )
1878                         break;
1879                     crop[i] = strtol( tmp, &tmp, 0 );
1880                     tmp++;
1881                 }
1882                 break;
1883             }
1884             case 'r':
1885             {
1886                 int i;
1887                 vrate = 0;
1888                 for( i = 0; i < hb_video_rates_count; i++ )
1889                 {
1890                     if( !strcmp( optarg, hb_video_rates[i].string ) )
1891                     {
1892                         vrate = hb_video_rates[i].rate;
1893                         break;
1894                     }
1895                 }
1896                 if( !vrate )
1897                 {
1898                     fprintf( stderr, "invalid framerate %s\n", optarg );
1899                 }
1900                 break;
1901             }
1902             case 'R':
1903                 if( optarg != NULL )
1904                 {
1905                     arates = strdup( optarg );
1906                 }
1907                 break;
1908             case 'b':
1909                 vbitrate = atoi( optarg );
1910                 break;
1911             case 'q':
1912                 vquality = atof( optarg );
1913                 break;
1914             case 'S':
1915                 size = atoi( optarg );
1916                 break;
1917             case 'B':
1918                 if( optarg != NULL )
1919                 {
1920                     abitrates = strdup( optarg );
1921                 }
1922                 break;
1923             case 'Q':
1924                 crf = 0;
1925                 break;
1926             case 'x':
1927                 x264opts = strdup( optarg );
1928                 break;
1929             case 'T':
1930                 turbo_opts_enabled = 1;
1931                 break;
1932             case 'Y':
1933                 maxHeight = atoi( optarg );
1934                 break;
1935             case 'X':
1936                 maxWidth = atoi (optarg );
1937                 break;
1938             case 'V':
1939                 vfr = 1;
1940                 break;
1941
1942             default:
1943                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1944                 return -1;
1945         }
1946     }
1947
1948     return 0;
1949 }
1950
1951 static int CheckOptions( int argc, char ** argv )
1952 {
1953     if( update )
1954     {
1955         return 0;
1956     }
1957
1958     if( input == NULL || *input == '\0' )
1959     {
1960         fprintf( stderr, "Missing input device. Run %s --help for "
1961                  "syntax.\n", argv[0] );
1962         return 1;
1963     }
1964
1965     /* Parse format */
1966     if( titleindex > 0 )
1967     {
1968         if( output == NULL || *output == '\0' )
1969         {
1970             fprintf( stderr, "Missing output file name. Run %s --help "
1971                      "for syntax.\n", argv[0] );
1972             return 1;
1973         }
1974
1975         if( !format )
1976         {
1977             char * p = strrchr( output, '.' );
1978
1979             /* autodetect */
1980             if( p && !strcasecmp( p, ".avi" ) )
1981             {
1982                 mux = HB_MUX_AVI;
1983                 default_acodec = HB_ACODEC_LAME;
1984             }
1985             else if( p && ( !strcasecmp( p, ".mp4" )  ||
1986                             !strcasecmp( p, ".m4v" ) ) )
1987             {
1988                 if ( h264_30 == 1 )
1989                     mux = HB_MUX_IPOD;
1990                 else
1991                     mux = HB_MUX_MP4;
1992                 default_acodec = HB_ACODEC_FAAC;
1993             }
1994             else if( p && ( !strcasecmp( p, ".ogm" ) ||
1995                             !strcasecmp( p, ".ogg" ) ) )
1996             {
1997                 mux = HB_MUX_OGM;
1998                 default_acodec = HB_ACODEC_VORBIS;
1999             }
2000             else if( p && !strcasecmp(p, ".mkv" ) )
2001             {
2002                 mux = HB_MUX_MKV;
2003                 default_acodec = HB_ACODEC_AC3;
2004             }
2005             else
2006             {
2007                 fprintf( stderr, "Output format couldn't be guessed "
2008                          "from file name, using default.\n" );
2009                 return 0;
2010             }
2011         }
2012         else if( !strcasecmp( format, "avi" ) )
2013         {
2014             mux = HB_MUX_AVI;
2015             default_acodec = HB_ACODEC_LAME;
2016         }
2017         else if( !strcasecmp( format, "mp4" ) ||
2018                  !strcasecmp( format, "m4v" ) )
2019         {
2020             if ( h264_30 == 1)
2021                 mux = HB_MUX_IPOD;
2022             else
2023                 mux = HB_MUX_MP4;
2024             default_acodec = HB_ACODEC_FAAC;
2025         }
2026         else if( !strcasecmp( format, "ogm" ) ||
2027                  !strcasecmp( format, "ogg" ) )
2028         {
2029             mux = HB_MUX_OGM;
2030             default_acodec = HB_ACODEC_VORBIS;
2031         }
2032         else if( !strcasecmp( format, "mkv" ) )
2033         {
2034             mux = HB_MUX_MKV;
2035             default_acodec = HB_ACODEC_AC3;
2036         }
2037         else
2038         {
2039             fprintf( stderr, "Invalid output format (%s). Possible "
2040                      "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2041             return 1;
2042         }
2043     }
2044
2045     return 0;
2046 }
2047
2048 static int get_acodec_for_string( char *codec )
2049 {
2050     if( !strcasecmp( codec, "ac3" ) )
2051     {
2052         return HB_ACODEC_AC3;
2053     }
2054     else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2055     {
2056         return HB_ACODEC_DCA;
2057     }
2058     else if( !strcasecmp( codec, "lame" ) )
2059     {
2060         return HB_ACODEC_LAME;
2061     }
2062     else if( !strcasecmp( codec, "faac" ) )
2063     {
2064         return HB_ACODEC_FAAC;
2065     }
2066     else if( !strcasecmp( codec, "vorbis") )
2067     {
2068         return HB_ACODEC_VORBIS;
2069     }
2070     else
2071     {
2072         return -1;
2073     }
2074 }
2075
2076 static int is_sample_rate_valid(int rate)
2077 {
2078     int i;
2079     for( i = 0; i < hb_audio_rates_count; i++ )
2080     {
2081             if (rate == hb_audio_rates[i].rate)
2082                 return 1;
2083     }
2084     return 0;
2085 }