OSDN Git Service

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