OSDN Git Service

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