OSDN Git Service

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