OSDN Git Service

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