OSDN Git Service

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