OSDN Git Service

Add Subtitle scanning for forced subtitles and normal subtitles from the CLI
[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( subtitle_scan )
608             {
609                 char *x264opts_tmp;
610
611                 /*
612                  * When subtitle scan is enabled do a fast pre-scan job
613                  * which will determine which subtitles to enable, if any.
614                  */
615                 job->pass = -1;
616                 
617                 x264opts_tmp = job->x264opts;
618
619                 job->x264opts = NULL;
620
621                 job->subtitle_scan = subtitle_scan;  
622                 fprintf( stderr, "Subtitle Scan Enabled - enabling "
623                          "subtitles if found for foreign language segments\n");
624                 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
625                 *(job->select_subtitle) = NULL;
626                 
627                 /*
628                  * Add the pre-scan job
629                  */
630                 hb_add( h, job );
631
632                 job->x264opts = x264opts_tmp;
633             }
634
635             if( twoPass )
636             {
637                 /*
638                  * If subtitle_scan is enabled then only turn it on
639                  * for the first pass and then off again for the
640                  * second. 
641                  */
642                 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
643
644                 job->select_subtitle = NULL;
645
646                 job->pass = 1;
647
648                 job->subtitle_scan = 0;
649
650                 /*
651                  * If turbo options have been selected then append them
652                  * to the x264opts now (size includes one ':' and the '\0')
653                  */
654                 if( turbo_opts_enabled ) 
655                 {
656                     int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
657                     char *tmp_x264opts;
658                         
659                     tmp_x264opts = malloc(size * sizeof(char));
660                     if( x264opts ) 
661                     {
662                         snprintf( tmp_x264opts, size, "%s:%s", 
663                                   x264opts, turbo_opts );  
664                         free( x264opts );
665                     } else {
666                         /*
667                          * No x264opts to modify, but apply the turbo options
668                          * anyway as they may be modifying defaults
669                          */
670                         snprintf( tmp_x264opts, size, "%s", 
671                                   turbo_opts );
672                     }
673                     x264opts = tmp_x264opts;
674
675                     fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
676                              x264opts );
677
678                     job->x264opts = x264opts;
679                 }     
680                 hb_add( h, job );
681
682                 job->select_subtitle = subtitle_tmp;
683
684                 job->pass = 2;
685                 /*
686                  * On the second pass we turn off subtitle scan so that we
687                  * can actually encode using any subtitles that were auto
688                  * selected in the first pass (using the whacky select-subtitle
689                  * attribute of the job).
690                  */
691                 job->subtitle_scan = 0;
692
693                 job->x264opts = x264opts2;
694                 
695                 hb_add( h, job );
696             }
697             else
698             {
699                 /*
700                  * Turn on subtitle scan if requested, note that this option
701                  * precludes encoding of any actual subtitles.
702                  */ 
703
704                 job->subtitle_scan = 0;
705                 job->pass = 0;
706                 hb_add( h, job );
707             }
708             hb_start( h );
709             break;
710         }
711
712 #define p s.param.working
713         case HB_STATE_WORKING:
714             fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
715                      p.job_cur, p.job_count, 100.0 * p.progress );
716             if( p.seconds > -1 )
717             {
718                 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
719                          "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
720                          p.hours, p.minutes, p.seconds );
721             }
722             break;
723 #undef p
724
725 #define p s.param.muxing
726         case HB_STATE_MUXING:
727         {
728             fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
729             break;
730         }
731 #undef p
732
733 #define p s.param.workdone
734         case HB_STATE_WORKDONE:
735             /* Print error if any, then exit */
736             switch( p.error )
737             {
738                 case HB_ERROR_NONE:
739                     fprintf( stderr, "\nRip done!\n" );
740                     break;
741                 case HB_ERROR_CANCELED:
742                     fprintf( stderr, "\nRip canceled.\n" );
743                     break;
744                 default:
745                     fprintf( stderr, "\nRip failed (error %x).\n",
746                              p.error );
747             }
748             die = 1;
749             break;
750 #undef p
751     }
752     return 0;
753 }
754
755 /****************************************************************************
756  * SigHandler:
757  ****************************************************************************/
758 static volatile int64_t i_die_date = 0;
759 void SigHandler( int i_signal )
760 {
761     if( die == 0 )
762     {
763         die = 1;
764         i_die_date = hb_get_date();
765         fprintf( stderr, "Signal %d received, terminating - do it "
766                  "again in case it gets stuck\n", i_signal );
767     }
768     else if( i_die_date + 500 < hb_get_date() )
769     {
770         fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
771         exit( 1 );
772     }
773 }
774
775 /****************************************************************************
776  * ShowHelp:
777  ****************************************************************************/
778 static void ShowHelp()
779 {
780     int i;
781     
782     fprintf( stderr,
783     "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
784     "\n"
785         "### General Handbrake Options------------------------------------------------\n\n"
786     "    -h, --help              Print help\n"
787     "    -u, --update            Check for updates and exit\n"
788     "    -v, --verbose           Be verbose\n"
789     "    -C, --cpu               Set CPU count (default: autodetected)\n"
790     "\n"
791         
792         "### Source Options-----------------------------------------------------------\n\n"
793         "    -i, --input <string>    Set input device\n"
794         "    -t, --title <number>    Select a title to encode (0 to scan only,\n"
795     "                            default: 1)\n"
796     "    -L, --longest           Select the longest title\n"
797     "    -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
798     "                            1 to 3, or \"3\" for chapter 3 only,\n"
799     "                            default: all chapters)\n"
800         "\n"
801         
802         "### Destination Options------------------------------------------------------\n\n"
803     "    -o, --output <string>   Set output file name\n"
804         "    -f, --format <string>   Set output format (avi/mp4/ogm/mkv, default:\n"
805     "                            autodetected from file name)\n"
806     "    -4, --large-file        Use 64-bit mp4 files that can hold more than\n"
807     "                            4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
808     "\n"
809         
810         "### Picture Settings---------------------------------------------------------\n\n"
811     "    -w, --width <number>    Set picture width\n"
812     "    -l, --height <number>   Set picture height\n"
813     "        --crop <T:B:L:R>    Set cropping values (default: autocrop)\n"
814         "    -Y, --maxHeight <#>     Set maximum height\n"
815         "    -X, --maxWidth <#>      Set maximum width\n"
816         "    -s, --subtitle <number> Select subtitle (default: none)\n"
817     "    -U, --subtitle-scan     Scan for subtitles on the first pass, and choose\n"
818     "                            the one that's only used 10 percent of the time\n"
819     "                            or less. This should locate subtitles for short\n"
820     "                            foreign language segments. Only works with 2-pass.\n"
821     "    -F, --subtitle-forced   Only display subtitles from the selected stream if\n"
822     "                            the subtitle has the forced flag set. May be used in\n"
823     "                            conjunction with --subtitle-scan to auto-select\n"
824     "                            a stream if it contains forced subtitles.\n"
825     "    -N, --native-language   Select subtitles with this language if it does not\n"
826     "          <string>          match the Audio language. Provide the language's\n"
827     "                            iso639-2 code (fre, eng, spa, dut, et cetera)\n"
828         "    -m, --markers           Add chapter markers (mp4 output format only)\n"
829         "\n"
830         
831         "### Video Options------------------------------------------------------------\n\n"
832         "    -e, --encoder <string>  Set video library encoder (ffmpeg,xvid,\n"
833     "                            x264,x264b13,x264b30 default: ffmpeg)\n"
834         "    -q, --quality <float>   Set video quality (0.0..1.0)\n"
835         "    -Q, --crf               Use with -q for CRF instead of CQP\n"
836     "    -S, --size <MB>         Set target size\n"
837         "    -b, --vb <kb/s>         Set video bitrate (default: 1000)\n"
838         "    -r, --rate              Set video framerate (" );
839     for( i = 0; i < hb_video_rates_count; i++ )
840     {
841         fprintf( stderr, hb_video_rates[i].string );
842         if( i != hb_video_rates_count - 1 )
843             fprintf( stderr, "/" );
844     }
845     fprintf( stderr, ")\n"
846         "\n"
847         "    -2, --two-pass          Use two-pass mode\n"
848      "    -d, --deinterlace       Deinterlace video with yadif/mcdeint filter\n"
849      "          <YM:FD:MM:QP>     (default 0:-1:-1:1)\n"            
850      "    -7, --deblock           Deblock video with pp7 filter\n"
851      "          <QP:M>            (default 0:2)\n"
852      "    -8, --denoise           Denoise video with hqdn3d filter\n"
853      "          <SL:SC:TL:TC>     (default 4:3:6:4.5)\n"
854      "    -9, --detelecine        Detelecine video with pullup filter\n"
855      "          <L:R:T:B:SB:MP>   (default 1:1:4:4:0:0)\n"
856     "    -g, --grayscale         Grayscale encoding\n"
857     "    -p, --pixelratio        Store pixel aspect ratio in video stream\n"
858         
859         "\n"
860         
861         
862         "### Audio Options-----------------------------------------------------------\n\n"
863         "    -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
864     "                            meaning passthrough, default: guessed)\n"
865         "    -B, --ab <kb/s>         Set audio bitrate (default: 128)\n"
866         "    -a, --audio <string>    Select audio channel(s) (\"none\" for no \n"
867     "                            audio, default: first one)\n"
868     "    -6, --mixdown <string>  Format for surround sound downmixing\n"
869     "                            (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
870     "    -R, --arate             Set audio samplerate (" );
871     for( i = 0; i < hb_audio_rates_count; i++ )
872     {
873         fprintf( stderr, hb_audio_rates[i].string );
874         if( i != hb_audio_rates_count - 1 )
875             fprintf( stderr, "/" );
876     }
877     fprintf( stderr, " kHz)\n"
878    
879     
880         
881         "\n"
882         
883         
884     "### Advanced H264 Options----------------------------------------------------\n\n"
885     "    -x, --x264opts <string> Specify advanced x264 options in the\n"
886     "                            same style as mencoder:\n"
887     "                            option1=value1:option2=value2\n"
888     "    -T, --turbo             When using 2-pass use the turbo options\n"
889     "                            on the first pass to improve speed\n"
890     "                            (only works with x264, affects PSNR by about 0.05dB,\n"
891     "                            and increases first pass speed two to four times)\n");
892 }
893
894 /****************************************************************************
895  * ParseOptions:
896  ****************************************************************************/
897 static int ParseOptions( int argc, char ** argv )
898 {
899     for( ;; )
900     {
901         static struct option long_options[] =
902           {
903             { "help",        no_argument,       NULL,    'h' },
904             { "update",      no_argument,       NULL,    'u' },
905             { "verbose",     no_argument,       NULL,    'v' },
906             { "cpu",         required_argument, NULL,    'C' },
907
908             { "format",      required_argument, NULL,    'f' },
909             { "input",       required_argument, NULL,    'i' },
910             { "output",      required_argument, NULL,    'o' },
911             { "large-file",  no_argument,       NULL,    '4' },
912             
913             { "title",       required_argument, NULL,    't' },
914             { "longest",     no_argument,       NULL,    'L' },
915             { "chapters",    required_argument, NULL,    'c' },
916             { "markers",     optional_argument, NULL,    'm' },
917             { "audio",       required_argument, NULL,    'a' },
918             { "mixdown",     required_argument, NULL,    '6' },
919             { "subtitle",    required_argument, NULL,    's' },
920             { "subtitle-scan", no_argument,     NULL,    'U' },
921             { "subtitle-forced", no_argument,   NULL,    'F' },
922             { "native-language", required_argument, NULL,'N' },
923
924             { "encoder",     required_argument, NULL,    'e' },
925             { "aencoder",    required_argument, NULL,    'E' },
926             { "two-pass",    no_argument,       NULL,    '2' },
927             { "deinterlace", optional_argument, NULL,    'd' },
928             { "deblock",     optional_argument, NULL,    '7' },
929             { "denoise",     optional_argument, NULL,    '8' },
930             { "detelecine",  optional_argument, NULL,    '9' },
931             { "grayscale",   no_argument,       NULL,    'g' },
932             { "pixelratio",  no_argument,       NULL,    'p' },
933             { "width",       required_argument, NULL,    'w' },
934             { "height",      required_argument, NULL,    'l' },
935             { "crop",        required_argument, NULL,    'n' },
936
937             { "vb",          required_argument, NULL,    'b' },
938             { "quality",     required_argument, NULL,    'q' },
939             { "size",        required_argument, NULL,    'S' },
940             { "ab",          required_argument, NULL,    'B' },
941             { "rate",        required_argument, NULL,    'r' },
942             { "arate",       required_argument, NULL,    'R' },
943             { "crf",         no_argument,       NULL,    'Q' },
944             { "x264opts",    required_argument, NULL,    'x' },
945             { "turbo",       no_argument,       NULL,    'T' },
946             
947             { "maxHeight",   required_argument, NULL,    'Y' },
948             { "maxWidth",    required_argument, NULL,    'X' },
949                         
950             { 0, 0, 0, 0 }
951           };
952
953         int option_index = 0;
954         int c;
955
956         c = getopt_long( argc, argv,
957                          "hvuC:f:4i:o:t:Lc:ma:6:s:UFN:e:E:2d789gpw:l:n:b:q:S:B:r:R:Qx:TY:X:",
958                          long_options, &option_index );
959         if( c < 0 )
960         {
961             break;
962         }
963
964         switch( c )
965         {
966             case 'h':
967                 ShowHelp();
968                 exit( 0 );
969             case 'u':
970                 update = 1;
971                 break;
972             case 'v':
973                 debug = HB_DEBUG_ALL;
974                 break;
975             case 'C':
976                 cpu = atoi( optarg );
977                 break;
978
979             case 'f':
980                 format = strdup( optarg );
981                 break;
982             case 'i':
983                 input = strdup( optarg );
984                 break;
985             case 'o':
986                 output = strdup( optarg );
987                 break;
988             case '4':
989                 largeFileSize = 1;
990                 break;
991             case 't':
992                 titleindex = atoi( optarg );
993                 break;
994             case 'L':
995                 longest_title = 1;
996                 break;
997             case 'c':
998             {
999                 int start, end;
1000                 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1001                 {
1002                     chapter_start = start;
1003                     chapter_end   = end;
1004                 }
1005                 else if( sscanf( optarg, "%d", &start ) == 1 )
1006                 {
1007                     chapter_start = start;
1008                     chapter_end   = chapter_start;
1009                 }
1010                 else
1011                 {
1012                     fprintf( stderr, "chapters: invalid syntax (%s)\n",
1013                              optarg );
1014                     return -1;
1015                 }
1016                 break;
1017             }
1018             case 'm':
1019                 if( optarg != NULL )
1020                 {
1021                     marker_file = strdup( optarg );
1022                 }
1023                 chapter_markers = 1;
1024                 break;
1025             case 'a':
1026                 audios = strdup( optarg );
1027                 break;
1028             case '6':
1029                 if( !strcasecmp( optarg, "mono" ) )
1030                 {
1031                     audio_mixdown = HB_AMIXDOWN_MONO;
1032                 }
1033                 else if( !strcasecmp( optarg, "stereo" ) )
1034                 {
1035                     audio_mixdown = HB_AMIXDOWN_STEREO;
1036                 }
1037                 else if( !strcasecmp( optarg, "dpl1" ) )
1038                 {
1039                     audio_mixdown = HB_AMIXDOWN_DOLBY;
1040                 }
1041                 else if( !strcasecmp( optarg, "dpl2" ) )
1042                 {
1043                     audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
1044                 }
1045                 else if( !strcasecmp( optarg, "6ch" ) )
1046                 {
1047                     audio_mixdown = HB_AMIXDOWN_6CH;
1048                 }
1049                 break;
1050             case 's':
1051                 sub = atoi( optarg );
1052                 break;
1053             case 'U':
1054                 subtitle_scan = 1;
1055                 break;
1056             case 'F':
1057                 subtitle_force = 1;
1058                 break;
1059             case 'N':
1060                 native_language = strdup( optarg );
1061                 break;
1062             case '2':
1063                 twoPass = 1;
1064                 break;
1065             case 'd':
1066                 if( optarg != NULL )
1067                 {
1068                     deinterlace_opt = strdup( optarg );
1069                 }
1070                 deinterlace = 1;
1071                 break;
1072             case '7':
1073                 if( optarg != NULL )
1074                 {
1075                     deblock_opt = strdup( optarg );
1076                 }
1077                 deblock = 1;
1078                 break;
1079             case '8':
1080                 if( optarg != NULL )
1081                 {
1082                     denoise_opt = strdup( optarg );
1083                 }
1084                 denoise = 1;
1085                 break;                
1086             case '9':
1087                 if( optarg != NULL )
1088                 {
1089                     detelecine_opt = strdup( optarg );
1090                 }
1091                 detelecine = 1;
1092                 break;                
1093             case 'g':
1094                 grayscale = 1;
1095                 break;
1096             case 'p':
1097                 pixelratio = 1;
1098                 break;
1099             case 'e':
1100                 if( !strcasecmp( optarg, "ffmpeg" ) )
1101                 {
1102                     vcodec = HB_VCODEC_FFMPEG;
1103                 }
1104                 else if( !strcasecmp( optarg, "xvid" ) )
1105                 {
1106                     vcodec = HB_VCODEC_XVID;
1107                 }
1108                 else if( !strcasecmp( optarg, "x264" ) )
1109                 {
1110                     vcodec = HB_VCODEC_X264;
1111                 }
1112                 else if( !strcasecmp( optarg, "x264b13" ) )
1113                 {
1114                     vcodec = HB_VCODEC_X264;
1115                     h264_13 = 1;
1116                 }
1117                 else if( !strcasecmp( optarg, "x264b30" ) )
1118                 {
1119                     vcodec = HB_VCODEC_X264;
1120                     h264_30 = 1;
1121                 }
1122                 else
1123                 {
1124                     fprintf( stderr, "invalid codec (%s)\n", optarg );
1125                     return -1;
1126                 }
1127                 break;
1128             case 'E':
1129                 if( !strcasecmp( optarg, "ac3" ) )
1130                 {
1131                     acodec = HB_ACODEC_AC3;
1132                 }
1133                 else if( !strcasecmp( optarg, "lame" ) )
1134                 {
1135                     acodec = HB_ACODEC_LAME;
1136                 }
1137                 else if( !strcasecmp( optarg, "faac" ) )
1138                 {
1139                     acodec = HB_ACODEC_FAAC;
1140                 }
1141                 else if( !strcasecmp( optarg, "vorbis") )
1142                 {
1143                     acodec = HB_ACODEC_VORBIS;
1144                 }
1145                 break;
1146             case 'w':
1147                 width = atoi( optarg );
1148                 break;
1149             case 'l':
1150                 height = atoi( optarg );
1151                 break;
1152             case 'n':
1153             {
1154                 int    i;
1155                 char * tmp = optarg;
1156                 for( i = 0; i < 4; i++ )
1157                 {
1158                     if( !*tmp )
1159                         break;
1160                     crop[i] = strtol( tmp, &tmp, 0 );
1161                     tmp++;
1162                 }
1163                 break;
1164             }
1165             case 'r':
1166             {
1167                 int i;
1168                 vrate = 0;
1169                 for( i = 0; i < hb_video_rates_count; i++ )
1170                 {
1171                     if( !strcmp( optarg, hb_video_rates[i].string ) )
1172                     {
1173                         vrate = hb_video_rates[i].rate;
1174                         break;
1175                     }
1176                 }
1177                 if( !vrate )
1178                 {
1179                     fprintf( stderr, "invalid framerate %s\n", optarg );
1180                 }
1181                 break;
1182             }
1183             case 'R':
1184             {
1185                 int i;
1186                 arate = 0;
1187                 for( i = 0; i < hb_audio_rates_count; i++ )
1188                 {
1189                     if( !strcmp( optarg, hb_audio_rates[i].string ) )
1190                     {
1191                         arate = hb_audio_rates[i].rate;
1192                         break;
1193                     }
1194                 }
1195                 if( !arate )
1196                 {
1197                     fprintf( stderr, "invalid framerate %s\n", optarg );
1198                 }
1199                 break;
1200             }
1201             case 'b':
1202                 vbitrate = atoi( optarg );
1203                 break;
1204             case 'q':
1205                 vquality = atof( optarg );
1206                 break;
1207             case 'S':
1208                 size = atoi( optarg );
1209                 break;
1210             case 'B':
1211                 abitrate = atoi( optarg );
1212                 break;
1213             case 'Q':
1214                 crf = 1;
1215                 break;
1216             case 'x':
1217                 x264opts = strdup( optarg );
1218                 x264opts2 = strdup( optarg );
1219                 break;
1220             case 'T':
1221                 turbo_opts_enabled = 1;
1222                 break;
1223             case 'Y':
1224                 maxHeight = atoi( optarg );
1225                 break;
1226             case 'X':
1227                 maxWidth = atoi (optarg );
1228                 break;
1229                                 
1230             default:
1231                 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1232                 return -1;
1233         }
1234     }
1235
1236     return 0;
1237 }
1238
1239 static int CheckOptions( int argc, char ** argv )
1240 {
1241     if( update )
1242     {
1243         return 0;
1244     }
1245
1246     if( input == NULL || *input == '\0' )
1247     {
1248         fprintf( stderr, "Missing input device. Run %s --help for "
1249                  "syntax.\n", argv[0] );
1250         return 1;
1251     }
1252
1253     /* Parse format */
1254     if( titleindex > 0 )
1255     {
1256         if( output == NULL || *output == '\0' )
1257         {
1258             fprintf( stderr, "Missing output file name. Run %s --help "
1259                      "for syntax.\n", argv[0] );
1260             return 1;
1261         }
1262
1263         if( !format )
1264         {
1265             char * p = strrchr( output, '.' );
1266
1267             /* autodetect */
1268             if( p && !strcasecmp( p, ".avi" ) )
1269             {
1270                 mux = HB_MUX_AVI;
1271             }
1272             else if( p && ( !strcasecmp( p, ".mp4" )  ||
1273                             !strcasecmp( p, ".m4v" ) ) )
1274             {
1275                 if ( h264_30 == 1 )
1276                     mux = HB_MUX_IPOD;
1277                 else
1278                     mux = HB_MUX_MP4;
1279             }
1280             else if( p && ( !strcasecmp( p, ".ogm" ) ||
1281                             !strcasecmp( p, ".ogg" ) ) )
1282             {
1283                 mux = HB_MUX_OGM;
1284             }
1285             else if( p && !strcasecmp(p, ".mkv" ) )
1286             {
1287                 mux = HB_MUX_MKV;
1288             }
1289             else
1290             {
1291                 fprintf( stderr, "Output format couldn't be guessed "
1292                          "from file name, using default.\n" );
1293                 return 0;
1294             }
1295         }
1296         else if( !strcasecmp( format, "avi" ) )
1297         {
1298             mux = HB_MUX_AVI;
1299         }
1300         else if( !strcasecmp( format, "mp4" ) )
1301         {
1302             if ( h264_30 == 1)
1303                 mux = HB_MUX_IPOD;
1304             else
1305                 mux = HB_MUX_MP4;
1306         }
1307         else if( !strcasecmp( format, "ogm" ) ||
1308                  !strcasecmp( format, "ogg" ) )
1309         {
1310             mux = HB_MUX_OGM;
1311         }
1312         else if( !strcasecmp( format, "mkv" ) )
1313         {
1314             mux = HB_MUX_MKV;
1315         }
1316         else
1317         {
1318             fprintf( stderr, "Invalid output format (%s). Possible "
1319                      "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
1320             return 1;
1321         }
1322
1323         if( !acodec )
1324         {
1325             if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
1326             {
1327                 acodec = HB_ACODEC_FAAC;
1328             }
1329             else if( mux == HB_MUX_AVI )
1330             {
1331                 acodec = HB_ACODEC_LAME;
1332             }
1333             else if( mux == HB_MUX_OGM )
1334             {
1335                 acodec = HB_ACODEC_VORBIS;
1336             }
1337             else if( mux == HB_MUX_MKV )
1338             {
1339                 acodec = HB_ACODEC_AC3;
1340             }
1341         }
1342
1343     }
1344
1345     return 0;
1346 }
1347