OSDN Git Service

Added "--subtitle-force" option to the CLI, and added changes to decsub to
[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    chapter_start = 0;
57 static int    chapter_end   = 0;
58 static int    chapter_markers = 0;
59 static char * marker_file   = NULL;
60 static int        crf                   = 0;
61 static char       *x264opts             = NULL;
62 static char       *x264opts2    = NULL;
63 static int        maxHeight             = 0;
64 static int        maxWidth              = 0;
65 static int    turbo_opts_enabled = 0;
66 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
67 static int    largeFileSize = 0;
68
69 /* Exit cleanly on Ctrl-C */
70 static volatile int die = 0;
71 static void SigHandler( int );
72
73 /* Utils */
74 static void ShowCommands();
75 static void ShowHelp();
76 static int  ParseOptions( int argc, char ** argv );
77 static int  CheckOptions( int argc, char ** argv );
78 static int  HandleEvents( hb_handle_t * h );
79
80 int main( int argc, char ** argv )
81 {
82     hb_handle_t * h;
83     int           build;
84     char        * version;
85
86     /* Parse command line */
87     if( ParseOptions( argc, argv ) ||
88         CheckOptions( argc, argv ) )
89     {
90         return 1;
91     }
92
93     /* Init libhb */
94     h = hb_init( debug, update );
95
96     /* Show version */
97     fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
98              hb_get_version( h ), hb_get_build( h ) );
99
100     /* Check for update */
101     if( update )
102     {
103         if( ( build = hb_check_update( h, &version ) ) > -1 )
104         {
105             fprintf( stderr, "You are using an old version of "
106                      "HandBrake.\nLatest is %s (build %d).\n", version,
107                      build );
108         }
109         else
110         {
111             fprintf( stderr, "Your version of HandBrake is up to "
112                      "date.\n" );
113         }
114         hb_close( &h );
115         return 0;
116     }
117
118     /* Geeky */
119     fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
120              hb_get_cpu_count( h ) > 1 ? "s" : "" );
121     if( cpu )
122     {
123         fprintf( stderr, "Forcing %d CPU%s\n", cpu,
124                  cpu > 1 ? "s" : "" );
125         hb_set_cpu_count( h, cpu );
126     }
127
128     /* Exit ASAP on Ctrl-C */
129     signal( SIGINT, SigHandler );
130
131     /* Feed libhb with a DVD to scan */
132     fprintf( stderr, "Opening %s...\n", input );
133
134     if (longest_title) {
135         /*
136          * We need to scan for all the titles in order to find the longest
137          */
138         titleindex = 0;
139     }
140     hb_scan( h, input, titleindex );
141
142     /* Wait... */
143     while( !die )
144     {
145 #if !defined(SYS_BEOS)
146         fd_set         fds;
147         struct timeval tv;
148         int            ret;
149         char           buf[257];
150
151         tv.tv_sec  = 0;
152         tv.tv_usec = 100000;
153
154         FD_ZERO( &fds );
155         FD_SET( STDIN_FILENO, &fds );
156         ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
157
158         if( ret > 0 )
159         {
160             int size = 0;
161
162             while( size < 256 &&
163                    read( STDIN_FILENO, &buf[size], 1 ) > 0 )
164             {
165                 if( buf[size] == '\n' )
166                 {
167                     break;
168                 }
169                 size++;
170             }
171
172             if( size >= 256 || buf[size] == '\n' )
173             {
174                 switch( buf[0] )
175                 {
176                     case 'q':
177                         die = 1;
178                         break;
179                     case 'p':
180                         hb_pause( h );
181                         break;
182                     case 'r':
183                         hb_resume( h );
184                         break;
185                     case 'h':
186                         ShowCommands();
187                         break;
188                 }
189             }
190         }
191         hb_snooze( 200 );
192 #else
193         hb_snooze( 200 );
194 #endif
195
196         HandleEvents( h );
197     }
198
199     /* Clean up */
200     hb_close( &h );
201     if( input )  free( input );
202     if( output ) free( output );
203     if( format ) free( format );
204     if( audios ) free( audios );
205     if (native_language ) free (native_language );
206         if( x264opts ) free (x264opts );
207         if( x264opts2 ) free (x264opts2 );
208         
209     fprintf( stderr, "HandBrake has exited.\n" );
210
211     return 0;
212 }
213
214 static void ShowCommands()
215 {
216     fprintf( stderr, "Commands:\n" );
217     fprintf( stderr, " [h]elp    Show this message\n" );
218     fprintf( stderr, " [q]uit    Exit HandBrakeCLI\n" );
219     fprintf( stderr, " [p]ause   Pause encoding\n" );
220     fprintf( stderr, " [r]esume  Resume encoding\n" );
221 }
222
223 static void PrintTitleInfo( hb_title_t * title )
224 {
225     hb_chapter_t  * chapter;
226     hb_audio_t    * audio;
227     hb_subtitle_t * subtitle;
228     int i;
229
230     fprintf( stderr, "+ title %d:\n", title->index );
231     fprintf( stderr, "  + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
232              title->vts, title->ttn, title->cell_start, title->cell_end,
233              title->block_count );
234     fprintf( stderr, "  + duration: %02d:%02d:%02d\n",
235              title->hours, title->minutes, title->seconds );
236     fprintf( stderr, "  + size: %dx%d, aspect: %.2f, %.3f fps\n",
237              title->width, title->height,
238              (float) title->aspect / HB_ASPECT_BASE,
239              (float) title->rate / title->rate_base );
240     fprintf( stderr, "  + autocrop: %d/%d/%d/%d\n", title->crop[0],
241              title->crop[1], title->crop[2], title->crop[3] );
242     fprintf( stderr, "  + chapters:\n" );
243     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
244     {
245         chapter = hb_list_item( title->list_chapter, i );
246         fprintf( stderr, "    + %d: cells %d->%d, %d blocks, duration "
247                  "%02d:%02d:%02d\n", chapter->index,
248                  chapter->cell_start, chapter->cell_end,
249                  chapter->block_count, chapter->hours, chapter->minutes,
250                  chapter->seconds );
251     }
252     fprintf( stderr, "  + audio tracks:\n" );
253     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
254     {
255         audio = hb_list_item( title->list_audio, i );
256         if( ( audio->codec & HB_ACODEC_AC3 ) || ( audio->codec & HB_ACODEC_DCA) )
257         {
258             fprintf( stderr, "    + %d, %s, %dHz, %dbps\n", i + 1,
259                      audio->lang, audio->rate, audio->bitrate );
260         }
261         else
262         {
263             fprintf( stderr, "    + %d, %s\n", i + 1, audio->lang );
264         }
265     }
266     fprintf( stderr, "  + subtitle tracks:\n" );
267     for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
268     {
269         subtitle = hb_list_item( title->list_subtitle, i );
270         fprintf( stderr, "    + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
271             subtitle->iso639_2);
272     }
273 }
274
275 static int HandleEvents( hb_handle_t * h )
276 {
277     hb_state_t s;
278     hb_get_state( h, &s );
279     switch( s.state )
280     {
281         case HB_STATE_IDLE:
282             /* Nothing to do */
283             break;
284
285 #define p s.param.scanning
286         case HB_STATE_SCANNING:
287             /* Show what title is currently being scanned */
288             fprintf( stderr, "Scanning title %d", p.title_cur );
289             if( !titleindex )
290                 fprintf( stderr, " of %d", p.title_count );
291             fprintf( stderr, "...\n" );
292             break;
293 #undef p
294
295         case HB_STATE_SCANDONE:
296         {
297             hb_list_t  * list;
298             hb_title_t * title;
299             hb_job_t   * job;
300
301             list = hb_get_titles( h );
302
303             if( !hb_list_count( list ) )
304             {
305                 /* No valid title, stop right there */
306                 fprintf( stderr, "No title found.\n" );
307                 die = 1;
308                 break;
309             }
310             if( longest_title )
311             {
312                 int i;
313                 int longest_title_idx=0;
314                 int longest_title_pos=-1;
315                 int longest_title_time=0;
316                 int title_time;
317                 
318                 fprintf( stderr, "Searching for longest title...\n" );
319
320                 for( i = 0; i < hb_list_count( list ); i++ )
321                 {
322                     title = hb_list_item( list, i );
323                     title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
324                     fprintf( stderr, " + Title (%d) index %d has length %dsec\n", 
325                              i, title->index, title_time );
326                     if( longest_title_time < title_time )
327                     {
328                         longest_title_time = title_time;
329                         longest_title_pos = i;
330                         longest_title_idx = title->index;
331                     }               
332                 }
333                 if( longest_title_pos == -1 ) 
334                 {
335                     fprintf( stderr, "No longest title found.\n" );
336                     die = 1;
337                     break;
338                 }
339                 titleindex = longest_title_idx;
340                 fprintf( stderr, "Found longest title, setting title to %d\n", 
341                          longest_title_idx);
342
343                 title = hb_list_item( list, longest_title_pos);
344             } else {
345                 title = hb_list_item( list, 0 );
346             }
347
348             if( !titleindex )
349             {
350                 /* Scan-only mode, print infos and exit */
351                 int i;
352                 for( i = 0; i < hb_list_count( list ); i++ )
353                 {
354                     title = hb_list_item( list, i );
355                     PrintTitleInfo( title );
356                 }
357                 die = 1;
358                 break;
359             }
360
361             /* Set job settings */
362             job   = title->job;
363
364             PrintTitleInfo( title );
365
366             if( chapter_start && chapter_end )
367             {
368                 job->chapter_start = MAX( job->chapter_start,
369                                           chapter_start );
370                 job->chapter_end   = MIN( job->chapter_end,
371                                           chapter_end );
372                 job->chapter_end   = MAX( job->chapter_start,
373                                           job->chapter_end );
374             }
375
376                         if ( chapter_markers )
377                         {
378                                 job->chapter_markers = chapter_markers;
379
380                 if( marker_file != NULL )
381                 {
382                     hb_csv_file_t * file = hb_open_csv_file( marker_file );
383                     hb_csv_cell_t * cell;
384                     int row = 0;
385                     int chapter = 0;
386                     
387                     fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
388                     
389                     if( file == NULL )
390                     {
391                          fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
392                     }
393                     else
394                     {
395                         /* Parse the cells */
396                         while( NULL != ( cell = hb_read_next_cell( file ) ) )
397                         {                            
398                             /* We have a chapter number */
399                             if( cell->cell_col == 0 )
400                             {
401                                 row = cell->cell_row;
402                                 chapter = atoi( cell->cell_text );
403                             }
404                              
405                             /* We have a chapter name */
406                             if( cell->cell_col == 1 && row == cell->cell_row )
407                             {
408                                 /* If we have a valid chapter, copy the string an terminate it */
409                                 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
410                                 {
411                                     hb_chapter_t * chapter_s;
412                                     
413                                     chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
414                                     strncpy(chapter_s->title, cell->cell_text, 1023);
415                                     chapter_s->title[1023] = '\0';
416                                 }
417                             }                               
418                         
419                                                            
420                             hb_dispose_cell( cell );
421                         }
422                         
423                         hb_close_csv_file( file );
424                     }
425                 }
426                         }
427
428             if( crop[0] >= 0 && crop[1] >= 0 &&
429                 crop[2] >= 0 && crop[3] >= 0 )
430             {
431                 memcpy( job->crop, crop, 4 * sizeof( int ) );
432             }
433
434             job->deinterlace = deinterlace;
435             job->grayscale   = grayscale;
436             job->pixel_ratio = pixelratio;
437
438             /* Add selected filters */
439             job->filters = hb_list_init();
440             if( detelecine )
441             {
442                 hb_filter_detelecine.settings = detelecine_opt;
443                 hb_list_add( job->filters, &hb_filter_detelecine );
444             }
445             if( deinterlace )
446             {
447                 hb_filter_deinterlace.settings = deinterlace_opt;
448                 hb_list_add( job->filters, &hb_filter_deinterlace );
449             }
450             if( deblock )
451             {
452                 hb_filter_deblock.settings = deblock_opt;
453                 hb_list_add( job->filters, &hb_filter_deblock );
454             }
455             if( denoise )
456             {
457                 hb_filter_denoise.settings = denoise_opt;
458                 hb_list_add( job->filters, &hb_filter_denoise );
459             }
460             
461             if( width && height )
462             {
463                 job->width  = width;
464                 job->height = height;
465             }
466             else if( width )
467             {
468                 job->width = width;
469                 hb_fix_aspect( job, HB_KEEP_WIDTH );
470             }
471             else if( height )
472             {
473                 job->height = height;
474                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
475             }
476             else if( !width && !height && !pixelratio )
477             {
478                 hb_fix_aspect( job, HB_KEEP_WIDTH );
479             }
480
481             if( vquality >= 0.0 && vquality <= 1.0 )
482             {
483                 job->vquality = vquality;
484                 job->vbitrate = 0;
485             }
486             else if( vbitrate )
487             {
488                 job->vquality = -1.0;
489                 job->vbitrate = vbitrate;
490             }
491             if( vcodec )
492             {
493                 job->vcodec = vcodec;
494             }
495             if( h264_13 ) 
496             { 
497                 job->h264_level = 13; 
498             }
499                 if( h264_30 )
500                 {
501                     job->h264_level = 30;
502             }
503             if( vrate )
504             {
505                 job->vrate = 27000000;
506                 job->vrate_base = vrate;
507             }
508             if( arate )
509             {
510                 job->arate = arate;
511             }
512
513             if( audios )
514             {
515                 if( strcasecmp( audios, "none" ) )
516                 {
517                     int    audio_count = 0;
518                     char * tmp         = audios;
519                     while( *tmp )
520                     {
521                         if( *tmp < '0' || *tmp > '9' )
522                         {
523                             /* Skip non numeric char */
524                             tmp++;
525                             continue;
526                         }
527                                                 job->audio_mixdowns[audio_count] = audio_mixdown;
528                         job->audios[audio_count++] =
529                             strtol( tmp, &tmp, 0 ) - 1;
530                     }
531                     job->audios[audio_count] = -1;
532                 }
533                 else
534                 {
535                     job->audios[0] = -1;
536                 }
537             }
538                         else
539                         {
540                             /* default to the first audio track if none has been specified */
541                             job->audios[0] = 0;
542                             job->audio_mixdowns[0] = audio_mixdown;
543                         }
544             if( abitrate )
545             {
546                 job->abitrate = abitrate;
547             }
548             if( acodec )
549             {
550                 job->acodec = acodec;
551             }
552
553             if( size )
554             {
555                 job->vbitrate = hb_calc_bitrate( job, size );
556                 fprintf( stderr, "Calculated bitrate: %d kbps\n",
557                          job->vbitrate );
558             }
559
560             if( sub )
561             {
562                 job->subtitle = sub - 1;
563             }
564
565             if( native_language )
566             {
567                 job->native_language = strdup( native_language );
568             }
569
570             if( job->mux )
571             {
572                 job->mux = mux;
573             }
574             
575             if ( largeFileSize )
576             {
577                 job->largeFileSize = 1;
578             }
579             
580             job->file = strdup( output );
581
582             if( crf )
583             {
584                 job->crf = 1;
585             }
586
587             if( x264opts != NULL && *x264opts != '\0' )
588             {
589                 fprintf( stderr, "Applying the following x264 options: %s\n", 
590                          x264opts);
591                 job->x264opts = x264opts;
592             }
593             else /*avoids a bus error crash when options aren't specified*/
594             {
595                 job->x264opts =  NULL;
596             }
597             if (maxWidth)
598                 job->maxWidth = maxWidth;
599             if (maxHeight)
600                 job->maxHeight = maxHeight;
601
602             if( subtitle_force )
603             {
604                 job->subtitle_force = subtitle_force;
605             } 
606                 
607             if( twoPass )
608             {
609                 /*
610                  * If subtitle_scan is enabled then only turn it on
611                  * for the first pass and then off again for the
612                  * second. 
613                  */
614                 job->pass = 1;
615                 job->subtitle_scan = subtitle_scan;
616                 if( subtitle_scan ) 
617                 {
618                     fprintf( stderr, "Subtitle Scan Enabled - enabling "
619                              "subtitles if found for foreign language segments\n");
620                     job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
621                     *(job->select_subtitle) = NULL;
622                 } 
623
624                 /*
625                  * If turbo options have been selected then append them
626                  * to the x264opts now (size includes one ':' and the '\0')
627                  */
628                 if( turbo_opts_enabled ) 
629                 {
630                     int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
631                     char *tmp_x264opts;
632                         
633                     tmp_x264opts = malloc(size * sizeof(char));
634                     if( x264opts ) 
635                     {
636                         snprintf( tmp_x264opts, size, "%s:%s", 
637                                   x264opts, turbo_opts );  
638                         free( x264opts );
639                     } else {
640                         /*
641                          * No x264opts to modify, but apply the turbo options
642                          * anyway as they may be modifying defaults
643                          */
644                         snprintf( tmp_x264opts, size, "%s", 
645                                   turbo_opts );
646                     }
647                     x264opts = tmp_x264opts;
648
649                     fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
650                              x264opts );
651
652                     job->x264opts = x264opts;
653                 }     
654                 hb_add( h, job );
655                 job->pass = 2;
656                 /*
657                  * On the second pass we turn off subtitle scan so that we
658                  * can actually encode using any subtitles that were auto
659                  * selected in the first pass (using the whacky select-subtitle
660                  * attribute of the job).
661                  */
662                 job->subtitle_scan = 0;
663
664                 job->x264opts = x264opts2;
665                 
666                 hb_add( h, job );
667             }
668             else
669             {
670                 /*
671                  * Turn on subtitle scan if requested, note that this option
672                  * precludes encoding of any actual subtitles.
673                  */
674                 if ( subtitle_scan ) 
675                 {
676                     fprintf( stderr, "Warning: Subtitle Scan only works in two-pass, disabling\n");
677                 }
678                 job->pass = 0;
679                 hb_add( h, job );
680             }
681             hb_start( h );
682             break;
683         }
684
685 #define p s.param.working
686         case HB_STATE_WORKING:
687             fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
688                      p.job_cur, p.job_count, 100.0 * p.progress );
689             if( p.seconds > -1 )
690             {
691                 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
692                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
693                          p.hours, p.minutes, p.seconds );
694             }
695             break;
696 #undef p
697
698 #define p s.param.muxing
699         case HB_STATE_MUXING:
700         {
701             fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
702             break;
703         }
704 #undef p
705
706 #define p s.param.workdone
707         case HB_STATE_WORKDONE:
708             /* Print error if any, then exit */
709             switch( p.error )
710             {
711                 case HB_ERROR_NONE:
712                     fprintf( stderr, "\nRip done!\n" );
713                     break;
714                 case HB_ERROR_CANCELED:
715                     fprintf( stderr, "\nRip canceled.\n" );
716                     break;
717                 default:
718                     fprintf( stderr, "\nRip failed (error %x).\n",
719                              p.error );
720             }
721             die = 1;
722             break;
723 #undef p
724     }
725     return 0;
726 }
727
728 /****************************************************************************
729  * SigHandler:
730  ****************************************************************************/
731 static volatile int64_t i_die_date = 0;
732 void SigHandler( int i_signal )
733 {
734     if( die == 0 )
735     {
736         die = 1;
737         i_die_date = hb_get_date();
738         fprintf( stderr, "Signal %d received, terminating - do it "
739                  "again in case it gets stuck\n", i_signal );
740     }
741     else if( i_die_date + 500 < hb_get_date() )
742     {
743         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
744         exit( 1 );
745     }
746 }
747
748 /****************************************************************************
749  * ShowHelp:
750  ****************************************************************************/
751 static void ShowHelp()
752 {
753     int i;
754     
755     fprintf( stderr,
756     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
757     "\n"
758         "### General Handbrake Options------------------------------------------------\n\n"
759     "    -h, --help              Print help\n"
760     "    -u, --update            Check for updates and exit\n"
761     "    -v, --verbose           Be verbose\n"
762     "    -C, --cpu               Set CPU count (default: autodetected)\n"
763     "\n"
764         
765         "### Source Options-----------------------------------------------------------\n\n"
766         "    -i, --input <string>    Set input device\n"
767         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
768     "                            default: 1)\n"
769     "    -L, --longest           Select the longest title\n"
770     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
771     "                            1 to 3, or \"3\" for chapter 3 only,\n"
772     "                            default: all chapters)\n"
773         "\n"
774         
775         "### Destination Options------------------------------------------------------\n\n"
776     "    -o, --output <string>   Set output file name\n"
777         "    -f, --format <string>   Set output format (avi/mp4/ogm/mkv, default:\n"
778     "                            autodetected from file name)\n"
779     "    -4, --large-file        Use 64-bit mp4 files that can hold more than\n"
780     "                            4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
781     "\n"
782         
783         "### Picture Settings---------------------------------------------------------\n\n"
784     "    -w, --width <number>    Set picture width\n"
785     "    -l, --height <number>   Set picture height\n"
786     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
787         "    -Y, --maxHeight <#>     Set maximum height\n"
788         "    -X, --maxWidth <#>      Set maximum width\n"
789         "    -s, --subtitle <number> Select subtitle (default: none)\n"
790     "    -U, --subtitle-scan     Scan for subtitles on the first pass, and choose\n"
791     "                            the one that's only used 10 percent of the time\n"
792     "                            or less. This should locate subtitles for short\n"
793     "                            foreign language segments. Only works with 2-pass.\n"
794     "    -F, --subtitle-force    Only display subtitles from the selected stream if\n"
795     "                            the subtitles have the forced flag set.\n"
796     "    -N, --native-language   Select subtitles with this language if it does not\n"
797     "          <string>          match the Audio language. Provide the language's\n"
798     "                            iso639-2 code (fre, eng, spa, dut, et cetera)\n"
799         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
800         "\n"
801         
802         "### Video Options------------------------------------------------------------\n\n"
803         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
804     "                            x264,x264b13,x264b30 default: ffmpeg)\n"
805         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
806         "    -Q, --crf               Use with -q for CRF instead of CQP\n"
807     "    -S, --size <MB>         Set target size\n"
808         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
809         "    -r, --rate              Set video framerate (" );
810     for( i = 0; i < hb_video_rates_count; i++ )
811     {
812         fprintf( stderr, hb_video_rates[i].string );
813         if( i != hb_video_rates_count - 1 )
814             fprintf( stderr, "/" );
815     }
816     fprintf( stderr, ")\n"
817         "\n"
818         "    -2, --two-pass          Use two-pass mode\n"
819      "    -d, --deinterlace       Deinterlace video with yadif/mcdeint filter\n"
820      "          <YM:FD:MM:QP>     (default 0:-1:-1:1)\n"            
821      "    -7, --deblock           Deblock video with pp7 filter\n"
822      "          <QP:M>            (default 0:2)\n"
823      "    -8, --denoise           Denoise video with hqdn3d filter\n"
824      "          <SL:SC:TL:TC>     (default 4:3:6:4.5)\n"
825      "    -9, --detelecine        Detelecine video with pullup filter\n"
826      "          <L:R:T:B:SB:MP>   (default 1:1:4:4:0:0)\n"
827     "    -g, --grayscale         Grayscale encoding\n"
828     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
829         
830         "\n"
831         
832         
833         "### Audio Options-----------------------------------------------------------\n\n"
834         "    -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
835     "                            meaning passthrough, default: guessed)\n"
836         "    -B, --ab <kb/s>         Set audio bitrate (default: 128)\n"
837         "    -a, --audio <string>    Select audio channel(s) (\"none\" for no \n"
838     "                            audio, default: first one)\n"
839     "    -6, --mixdown <string>  Format for surround sound downmixing\n"
840     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
841     "    -R, --arate             Set audio samplerate (" );
842     for( i = 0; i < hb_audio_rates_count; i++ )
843     {
844         fprintf( stderr, hb_audio_rates[i].string );
845         if( i != hb_audio_rates_count - 1 )
846             fprintf( stderr, "/" );
847     }
848     fprintf( stderr, " kHz)\n"
849    
850     
851         
852         "\n"
853         
854         
855     "### Advanced H264 Options----------------------------------------------------\n\n"
856     "    -x, --x264opts <string> Specify advanced x264 options in the\n"
857     "                            same style as mencoder:\n"
858     "                            option1=value1:option2=value2\n"
859     "    -T, --turbo             When using 2-pass use the turbo options\n"
860     "                            on the first pass to improve speed\n"
861     "                            (only works with x264, affects PSNR by about 0.05dB,\n"
862     "                            and increases first pass speed two to four times)\n");
863 }
864
865 /****************************************************************************
866  * ParseOptions:
867  ****************************************************************************/
868 static int ParseOptions( int argc, char ** argv )
869 {
870     for( ;; )
871     {
872         static struct option long_options[] =
873           {
874             { "help",        no_argument,       NULL,    'h' },
875             { "update",      no_argument,       NULL,    'u' },
876             { "verbose",     no_argument,       NULL,    'v' },
877             { "cpu",         required_argument, NULL,    'C' },
878
879             { "format",      required_argument, NULL,    'f' },
880             { "input",       required_argument, NULL,    'i' },
881             { "output",      required_argument, NULL,    'o' },
882             { "large-file",  no_argument,       NULL,    '4' },
883             
884             { "title",       required_argument, NULL,    't' },
885             { "longest",     no_argument,       NULL,    'L' },
886             { "chapters",    required_argument, NULL,    'c' },
887             { "markers",     optional_argument, NULL,    'm' },
888             { "audio",       required_argument, NULL,    'a' },
889             { "mixdown",     required_argument, NULL,    '6' },
890             { "subtitle",    required_argument, NULL,    's' },
891             { "subtitle-scan", no_argument,     NULL,    'U' },
892             { "subtitle-forced", no_argument,   NULL,    'F' },
893             { "native-language", required_argument, NULL,'N' },
894
895             { "encoder",     required_argument, NULL,    'e' },
896             { "aencoder",    required_argument, NULL,    'E' },
897             { "two-pass",    no_argument,       NULL,    '2' },
898             { "deinterlace", optional_argument, NULL,    'd' },
899             { "deblock",     optional_argument, NULL,    '7' },
900             { "denoise",     optional_argument, NULL,    '8' },
901             { "detelecine",  optional_argument, NULL,    '9' },
902             { "grayscale",   no_argument,       NULL,    'g' },
903             { "pixelratio",  no_argument,       NULL,    'p' },
904             { "width",       required_argument, NULL,    'w' },
905             { "height",      required_argument, NULL,    'l' },
906             { "crop",        required_argument, NULL,    'n' },
907
908             { "vb",          required_argument, NULL,    'b' },
909             { "quality",     required_argument, NULL,    'q' },
910             { "size",        required_argument, NULL,    'S' },
911             { "ab",          required_argument, NULL,    'B' },
912             { "rate",        required_argument, NULL,    'r' },
913             { "arate",       required_argument, NULL,    'R' },
914             { "crf",         no_argument,       NULL,    'Q' },
915             { "x264opts",    required_argument, NULL,    'x' },
916             { "turbo",       no_argument,       NULL,    'T' },
917             
918             { "maxHeight",   required_argument, NULL,    'Y' },
919             { "maxWidth",    required_argument, NULL,    'X' },
920                         
921             { 0, 0, 0, 0 }
922           };
923
924         int option_index = 0;
925         int c;
926
927         c = getopt_long( argc, argv,
928                          "hvuC:f:4i:o:t:Lc:ma:6:s:UFN:e:E:2d789gpw:l:n:b:q:S:B:r:R:Qx:TY:X:",
929                          long_options, &option_index );
930         if( c < 0 )
931         {
932             break;
933         }
934
935         switch( c )
936         {
937             case 'h':
938                 ShowHelp();
939                 exit( 0 );
940             case 'u':
941                 update = 1;
942                 break;
943             case 'v':
944                 debug = HB_DEBUG_ALL;
945                 break;
946             case 'C':
947                 cpu = atoi( optarg );
948                 break;
949
950             case 'f':
951                 format = strdup( optarg );
952                 break;
953             case 'i':
954                 input = strdup( optarg );
955                 break;
956             case 'o':
957                 output = strdup( optarg );
958                 break;
959             case '4':
960                 largeFileSize = 1;
961                 break;
962             case 't':
963                 titleindex = atoi( optarg );
964                 break;
965             case 'L':
966                 longest_title = 1;
967                 break;
968             case 'c':
969             {
970                 int start, end;
971                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
972                 {
973                     chapter_start = start;
974                     chapter_end   = end;
975                 }
976                 else if( sscanf( optarg, "%d", &start ) == 1 )
977                 {
978                     chapter_start = start;
979                     chapter_end   = chapter_start;
980                 }
981                 else
982                 {
983                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
984                              optarg );
985                     return -1;
986                 }
987                 break;
988             }
989             case 'm':
990                 if( optarg != NULL )
991                 {
992                     marker_file = strdup( optarg );
993                 }
994                 chapter_markers = 1;
995                 break;
996             case 'a':
997                 audios = strdup( optarg );
998                 break;
999             case '6':
1000                 if( !strcasecmp( optarg, "mono" ) )
1001                 {
1002                     audio_mixdown = HB_AMIXDOWN_MONO;
1003                 }
1004                 else if( !strcasecmp( optarg, "stereo" ) )
1005                 {
1006                     audio_mixdown = HB_AMIXDOWN_STEREO;
1007                 }
1008                 else if( !strcasecmp( optarg, "dpl1" ) )
1009                 {
1010                     audio_mixdown = HB_AMIXDOWN_DOLBY;
1011                 }
1012                 else if( !strcasecmp( optarg, "dpl2" ) )
1013                 {
1014                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
1015                 }
1016                 else if( !strcasecmp( optarg, "6ch" ) )
1017                 {
1018                     audio_mixdown = HB_AMIXDOWN_6CH;
1019                 }
1020                 break;
1021             case 's':
1022                 sub = atoi( optarg );
1023                 break;
1024             case 'U':
1025                 subtitle_scan = 1;
1026                 break;
1027             case 'F':
1028                 subtitle_force = 1;
1029                 break;
1030             case 'N':
1031                 native_language = strdup( optarg );
1032                 break;
1033             case '2':
1034                 twoPass = 1;
1035                 break;
1036             case 'd':
1037                 if( optarg != NULL )
1038                 {
1039                     deinterlace_opt = strdup( optarg );
1040                 }
1041                 deinterlace = 1;
1042                 break;
1043             case '7':
1044                 if( optarg != NULL )
1045                 {
1046                     deblock_opt = strdup( optarg );
1047                 }
1048                 deblock = 1;
1049                 break;
1050             case '8':
1051                 if( optarg != NULL )
1052                 {
1053                     denoise_opt = strdup( optarg );
1054                 }
1055                 denoise = 1;
1056                 break;                
1057             case '9':
1058                 if( optarg != NULL )
1059                 {
1060                     detelecine_opt = strdup( optarg );
1061                 }
1062                 detelecine = 1;
1063                 break;                
1064             case 'g':
1065                 grayscale = 1;
1066                 break;
1067             case 'p':
1068                 pixelratio = 1;
1069                 break;
1070             case 'e':
1071                 if( !strcasecmp( optarg, "ffmpeg" ) )
1072                 {
1073                     vcodec = HB_VCODEC_FFMPEG;
1074                 }
1075                 else if( !strcasecmp( optarg, "xvid" ) )
1076                 {
1077                     vcodec = HB_VCODEC_XVID;
1078                 }
1079                 else if( !strcasecmp( optarg, "x264" ) )
1080                 {
1081                     vcodec = HB_VCODEC_X264;
1082                 }
1083                 else if( !strcasecmp( optarg, "x264b13" ) )
1084                 {
1085                     vcodec = HB_VCODEC_X264;
1086                     h264_13 = 1;
1087                 }
1088                 else if( !strcasecmp( optarg, "x264b30" ) )
1089                 {
1090                     vcodec = HB_VCODEC_X264;
1091                     h264_30 = 1;
1092                 }
1093                 else
1094                 {
1095                     fprintf( stderr, "invalid codec (%s)\n", optarg );
1096                     return -1;
1097                 }
1098                 break;
1099             case 'E':
1100                 if( !strcasecmp( optarg, "ac3" ) )
1101                 {
1102                     acodec = HB_ACODEC_AC3;
1103                 }
1104                 else if( !strcasecmp( optarg, "lame" ) )
1105                 {
1106                     acodec = HB_ACODEC_LAME;
1107                 }
1108                 else if( !strcasecmp( optarg, "faac" ) )
1109                 {
1110                     acodec = HB_ACODEC_FAAC;
1111                 }
1112                 else if( !strcasecmp( optarg, "vorbis") )
1113                 {
1114                     acodec = HB_ACODEC_VORBIS;
1115                 }
1116                 break;
1117             case 'w':
1118                 width = atoi( optarg );
1119                 break;
1120             case 'l':
1121                 height = atoi( optarg );
1122                 break;
1123             case 'n':
1124             {
1125                 int    i;
1126                 char * tmp = optarg;
1127                 for( i = 0; i < 4; i++ )
1128                 {
1129                     if( !*tmp )
1130                         break;
1131                     crop[i] = strtol( tmp, &tmp, 0 );
1132                     tmp++;
1133                 }
1134                 break;
1135             }
1136             case 'r':
1137             {
1138                 int i;
1139                 vrate = 0;
1140                 for( i = 0; i < hb_video_rates_count; i++ )
1141                 {
1142                     if( !strcmp( optarg, hb_video_rates[i].string ) )
1143                     {
1144                         vrate = hb_video_rates[i].rate;
1145                         break;
1146                     }
1147                 }
1148                 if( !vrate )
1149                 {
1150                     fprintf( stderr, "invalid framerate %s\n", optarg );
1151                 }
1152                 break;
1153             }
1154             case 'R':
1155             {
1156                 int i;
1157                 arate = 0;
1158                 for( i = 0; i < hb_audio_rates_count; i++ )
1159                 {
1160                     if( !strcmp( optarg, hb_audio_rates[i].string ) )
1161                     {
1162                         arate = hb_audio_rates[i].rate;
1163                         break;
1164                     }
1165                 }
1166                 if( !arate )
1167                 {
1168                     fprintf( stderr, "invalid framerate %s\n", optarg );
1169                 }
1170                 break;
1171             }
1172             case 'b':
1173                 vbitrate = atoi( optarg );
1174                 break;
1175             case 'q':
1176                 vquality = atof( optarg );
1177                 break;
1178             case 'S':
1179                 size = atoi( optarg );
1180                 break;
1181             case 'B':
1182                 abitrate = atoi( optarg );
1183                 break;
1184             case 'Q':
1185                 crf = 1;
1186                 break;
1187             case 'x':
1188                 x264opts = strdup( optarg );
1189                 x264opts2 = strdup( optarg );
1190                 break;
1191             case 'T':
1192                 turbo_opts_enabled = 1;
1193                 break;
1194             case 'Y':
1195                 maxHeight = atoi( optarg );
1196                 break;
1197             case 'X':
1198                 maxWidth = atoi (optarg );
1199                 break;
1200                                 
1201             default:
1202                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1203                 return -1;
1204         }
1205     }
1206
1207     return 0;
1208 }
1209
1210 static int CheckOptions( int argc, char ** argv )
1211 {
1212     if( update )
1213     {
1214         return 0;
1215     }
1216
1217     if( input == NULL || *input == '\0' )
1218     {
1219         fprintf( stderr, "Missing input device. Run %s --help for "
1220                  "syntax.\n", argv[0] );
1221         return 1;
1222     }
1223
1224     /* Parse format */
1225     if( titleindex > 0 )
1226     {
1227         if( output == NULL || *output == '\0' )
1228         {
1229             fprintf( stderr, "Missing output file name. Run %s --help "
1230                      "for syntax.\n", argv[0] );
1231             return 1;
1232         }
1233
1234         if( !format )
1235         {
1236             char * p = strrchr( output, '.' );
1237
1238             /* autodetect */
1239             if( p && !strcasecmp( p, ".avi" ) )
1240             {
1241                 mux = HB_MUX_AVI;
1242             }
1243             else if( p && ( !strcasecmp( p, ".mp4" )  ||
1244                             !strcasecmp( p, ".m4v" ) ) )
1245             {
1246                 if ( h264_30 == 1 )
1247                     mux = HB_MUX_IPOD;
1248                 else
1249                     mux = HB_MUX_MP4;
1250             }
1251             else if( p && ( !strcasecmp( p, ".ogm" ) ||
1252                             !strcasecmp( p, ".ogg" ) ) )
1253             {
1254                 mux = HB_MUX_OGM;
1255             }
1256             else if( p && !strcasecmp(p, ".mkv" ) )
1257             {
1258                 mux = HB_MUX_MKV;
1259             }
1260             else
1261             {
1262                 fprintf( stderr, "Output format couldn't be guessed "
1263                          "from file name, using default.\n" );
1264                 return 0;
1265             }
1266         }
1267         else if( !strcasecmp( format, "avi" ) )
1268         {
1269             mux = HB_MUX_AVI;
1270         }
1271         else if( !strcasecmp( format, "mp4" ) )
1272         {
1273             if ( h264_30 == 1)
1274                 mux = HB_MUX_IPOD;
1275             else
1276                 mux = HB_MUX_MP4;
1277         }
1278         else if( !strcasecmp( format, "ogm" ) ||
1279                  !strcasecmp( format, "ogg" ) )
1280         {
1281             mux = HB_MUX_OGM;
1282         }
1283         else if( !strcasecmp( format, "mkv" ) )
1284         {
1285             mux = HB_MUX_MKV;
1286         }
1287         else
1288         {
1289             fprintf( stderr, "Invalid output format (%s). Possible "
1290                      "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
1291             return 1;
1292         }
1293
1294         if( !acodec )
1295         {
1296             if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
1297             {
1298                 acodec = HB_ACODEC_FAAC;
1299             }
1300             else if( mux == HB_MUX_AVI )
1301             {
1302                 acodec = HB_ACODEC_LAME;
1303             }
1304             else if( mux == HB_MUX_OGM )
1305             {
1306                 acodec = HB_ACODEC_VORBIS;
1307             }
1308             else if( mux == HB_MUX_MKV )
1309             {
1310                 acodec = HB_ACODEC_AC3;
1311             }
1312         }
1313
1314     }
1315
1316     return 0;
1317 }
1318