OSDN Git Service

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