1 /* $Id: test.c,v 1.82 2005/11/19 08:25:54 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
14 #if defined( __MINGW32__ )
18 #if defined( PTW32_STATIC_LIB )
25 #if defined( __APPLE_CC__ )
26 #import <CoreServices/CoreServices.h>
27 #include <IOKit/IOKitLib.h>
28 #include <IOKit/storage/IOMedia.h>
29 #include <IOKit/storage/IODVDMedia.h>
33 static int debug = HB_DEBUG_ALL;
34 static int update = 0;
35 static int dvdnav = 1;
36 static char * input = NULL;
37 static char * output = NULL;
38 static char * format = NULL;
39 static int titleindex = 1;
40 static int longest_title = 0;
41 static char * native_language = NULL;
42 static int native_dub = 0;
43 static int twoPass = 0;
44 static int deinterlace = 0;
45 static char * deinterlace_opt = 0;
46 static int deblock = 0;
47 static char * deblock_opt = 0;
48 static int denoise = 0;
49 static char * denoise_opt = 0;
50 static int detelecine = 0;
51 static char * detelecine_opt = 0;
52 static int decomb = 0;
53 static char * decomb_opt = 0;
54 static int grayscale = 0;
55 static int vcodec = HB_VCODEC_FFMPEG;
56 static int h264_13 = 0;
57 static int h264_30 = 0;
58 static hb_list_t * audios = NULL;
59 static hb_audio_config_t * audio = NULL;
60 static int num_audio_tracks = 0;
61 static char * mixdowns = NULL;
62 static char * dynamic_range_compression = NULL;
63 static char * atracks = NULL;
64 static char * arates = NULL;
65 static char * abitrates = NULL;
66 static char * acodecs = NULL;
67 static char * anames = NULL;
68 static int default_acodec = HB_ACODEC_FAAC;
69 static int default_abitrate = 160;
70 static int audio_explicit = 0;
71 static char ** subtracks = NULL;
72 static char ** subforce = NULL;
73 static char * subburn = NULL;
74 static char * subdefault = NULL;
75 static char ** srtfile = NULL;
76 static char ** srtcodeset = NULL;
77 static char ** srtoffset = NULL;
78 static char ** srtlang = NULL;
79 static int srtdefault = -1;
80 static int subtitle_scan = 0;
82 static int height = 0;
83 static int crop[4] = { -1,-1,-1,-1 };
86 static float vquality = -1.0;
87 static int vbitrate = 0;
90 static int anamorphic_mode = 0;
91 static int modulus = 0;
92 static int par_height = 0;
93 static int par_width = 0;
94 static int display_width = 0;
95 static int keep_display_aspect = 0;
96 static int itu_par = 0;
98 static int chapter_start = 0;
99 static int chapter_end = 0;
100 static int chapter_markers = 0;
101 static char * marker_file = NULL;
102 static char *x264opts = NULL;
103 static char *x264opts2 = NULL;
104 static int maxHeight = 0;
105 static int maxWidth = 0;
106 static int turbo_opts_enabled = 0;
107 static char * turbo_opts = "ref=1:subme=2:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
108 static int largeFileSize = 0;
109 static int preset = 0;
110 static char * preset_name = 0;
112 static int mp4_optimize = 0;
113 static int ipod_atom = 0;
114 static int color_matrix = 0;
115 static int preview_count = 10;
116 static int store_previews = 0;
117 static int start_at_preview = 0;
118 static int64_t stop_at_pts = 0;
119 static int stop_at_frame = 0;
120 static char * stop_at_string = NULL;
121 static char * stop_at_token = NULL;
123 /* Exit cleanly on Ctrl-C */
124 static volatile int die = 0;
125 static void SigHandler( int );
128 static void ShowCommands();
129 static void ShowHelp();
130 static void ShowPresets();
132 static int ParseOptions( int argc, char ** argv );
133 static int CheckOptions( int argc, char ** argv );
134 static int HandleEvents( hb_handle_t * h );
136 static int get_acodec_for_string( char *codec );
137 static int is_sample_rate_valid(int rate);
140 static char* bsd_name_for_path(char *path);
141 static int device_is_dvd(char *device);
142 static io_service_t get_iokit_service( char *device );
143 static int is_dvd_service( io_service_t service );
144 static int is_whole_media_service( io_service_t service );
147 /* Only print the "Muxing..." message once */
148 static int show_mux_warning = 1;
150 /****************************************************************************
153 * When using the CLI just display using hb_log as we always did in the past
154 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
155 ****************************************************************************/
156 static void hb_cli_error_handler ( const char *errmsg )
158 fprintf( stderr, "ERROR: %s\n", errmsg );
161 int main( int argc, char ** argv )
167 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
168 * force it to unbuffered otherwise informative output is not easily parsed.
170 #if defined( _WIN32 ) || defined( __MINGW32__ )
171 setvbuf( stdout, NULL, _IONBF, 0 );
172 setvbuf( stderr, NULL, _IONBF, 0 );
175 audios = hb_list_init();
177 /* Parse command line */
178 if( ParseOptions( argc, argv ) ||
179 CheckOptions( argc, argv ) )
184 #if defined( PTW32_STATIC_LIB )
185 pthread_win32_process_attach_np();
186 pthread_win32_thread_attach_np();
189 /* Register our error handler */
190 hb_register_error_handler(&hb_cli_error_handler);
193 h = hb_init( debug, update );
194 hb_dvd_set_dvdnav( dvdnav );
197 fprintf( stderr, "%s - %s - %s\n",
198 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
200 /* Check for update */
203 if( ( build = hb_check_update( h, &version ) ) > -1 )
205 fprintf( stderr, "You are using an old version of "
206 "HandBrake.\nLatest is %s (build %d).\n", version,
211 fprintf( stderr, "Your version of HandBrake is up to "
219 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
220 hb_get_cpu_count( h ) > 1 ? "s" : "" );
223 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
224 cpu > 1 ? "s" : "" );
225 hb_set_cpu_count( h, cpu );
228 /* Exit ASAP on Ctrl-C */
229 signal( SIGINT, SigHandler );
231 /* Feed libhb with a DVD to scan */
232 fprintf( stderr, "Opening %s...\n", input );
236 * We need to scan for all the titles in order to find the longest
241 hb_scan( h, input, titleindex, preview_count, store_previews );
246 #if defined( __MINGW32__ )
250 case 0x03: /* ctrl-c */
252 fprintf( stdout, "\nEncoding Quit by user command\n" );
256 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
268 #elif !defined(SYS_BEOS)
278 FD_SET( STDIN_FILENO, &fds );
279 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
286 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
288 if( buf[size] == '\n' )
295 if( size >= 256 || buf[size] == '\n' )
300 fprintf( stdout, "\nEncoding Quit by user command\n" );
304 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
326 if( input ) free( input );
327 if( output ) free( output );
328 if( format ) free( format );
331 while( ( audio = hb_list_item( audios, 0 ) ) )
333 hb_list_rem( audios, audio );
334 if( audio->out.name )
336 free( audio->out.name );
340 hb_list_close( &audios );
342 if( mixdowns ) free( mixdowns );
343 if( dynamic_range_compression ) free( dynamic_range_compression );
344 if( atracks ) free( atracks );
345 if( arates ) free( arates );
346 if( abitrates ) free( abitrates );
347 if( acodecs ) free( acodecs );
348 if( anames ) free( anames );
349 if (native_language ) free (native_language );
350 if( x264opts ) free (x264opts );
351 if( x264opts2 ) free (x264opts2 );
352 if (preset_name) free (preset_name);
353 if( stop_at_string ) free( stop_at_string );
355 fprintf( stderr, "HandBrake has exited.\n" );
357 #if defined( PTW32_STATIC_LIB )
358 pthread_win32_thread_detach_np();
359 pthread_win32_process_detach_np();
365 static void ShowCommands()
367 fprintf( stdout, "\nCommands:\n" );
368 fprintf( stdout, " [h]elp Show this message\n" );
369 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
370 fprintf( stdout, " [p]ause Pause encoding\n" );
371 fprintf( stdout, " [r]esume Resume encoding\n" );
374 static void PrintTitleInfo( hb_title_t * title )
376 hb_chapter_t * chapter;
377 hb_subtitle_t * subtitle;
380 fprintf( stderr, "+ title %d:\n", title->index );
381 if ( title->type == HB_STREAM_TYPE )
383 fprintf( stderr, " + stream: %s\n", title->path );
385 else if ( title->type == HB_DVD_TYPE )
387 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
388 title->vts, title->ttn, title->cell_start, title->cell_end,
389 title->block_count );
392 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
393 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
394 title->hours, title->minutes, title->seconds );
395 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
396 title->width, title->height,
397 title->pixel_aspect_width,
398 title->pixel_aspect_height,
399 (float) title->aspect,
400 (float) title->rate / title->rate_base );
401 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
402 title->crop[1], title->crop[2], title->crop[3] );
403 fprintf( stderr, " + chapters:\n" );
404 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
406 chapter = hb_list_item( title->list_chapter, i );
407 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
408 "%02d:%02d:%02d\n", chapter->index,
409 chapter->cell_start, chapter->cell_end,
410 chapter->block_count, chapter->hours, chapter->minutes,
413 fprintf( stderr, " + audio tracks:\n" );
414 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
416 audio = hb_list_audio_config_item( title->list_audio, i );
417 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
419 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
421 audio->lang.description,
422 audio->lang.iso639_2,
423 audio->in.samplerate,
428 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
430 audio->lang.description,
431 audio->lang.iso639_2 );
434 fprintf( stderr, " + subtitle tracks:\n" );
435 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
437 subtitle = hb_list_item( title->list_subtitle, i );
438 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
439 i + 1, subtitle->lang,
441 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
444 if(title->detected_interlacing)
446 /* Interlacing was found in half or more of the preview frames */
447 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
452 static int test_sub_list( char ** list, char * needle, int pos )
456 if ( list == NULL || needle == NULL )
459 if ( list[0] == NULL && pos == 1 )
462 for ( i = 0; list[i] != NULL; i++ )
464 if ( strcasecmp( list[i], needle ) == 0 )
470 static int HandleEvents( hb_handle_t * h )
473 int tmp_num_audio_tracks;
475 hb_get_state( h, &s );
482 #define p s.param.scanning
483 case HB_STATE_SCANNING:
484 /* Show what title is currently being scanned */
485 fprintf( stderr, "Scanning title %d", p.title_cur );
487 fprintf( stderr, " of %d", p.title_count );
488 fprintf( stderr, "...\n" );
492 case HB_STATE_SCANDONE:
500 /* Audio argument string parsing variables */
504 int mixdown = HB_AMIXDOWN_DOLBYPLII;
506 /* Audio argument string parsing variables */
508 list = hb_get_titles( h );
510 if( !hb_list_count( list ) )
512 /* No valid title, stop right there */
513 fprintf( stderr, "No title found.\n" );
520 int longest_title_idx=0;
521 int longest_title_pos=-1;
522 int longest_title_time=0;
525 fprintf( stderr, "Searching for longest title...\n" );
527 for( i = 0; i < hb_list_count( list ); i++ )
529 title = hb_list_item( list, i );
530 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
531 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
532 i, title->index, title_time );
533 if( longest_title_time < title_time )
535 longest_title_time = title_time;
536 longest_title_pos = i;
537 longest_title_idx = title->index;
540 if( longest_title_pos == -1 )
542 fprintf( stderr, "No longest title found.\n" );
546 titleindex = longest_title_idx;
547 fprintf( stderr, "Found longest title, setting title to %d\n",
550 title = hb_list_item( list, longest_title_pos);
552 title = hb_list_item( list, 0 );
557 /* Scan-only mode, print infos and exit */
559 for( i = 0; i < hb_list_count( list ); i++ )
561 title = hb_list_item( list, i );
562 PrintTitleInfo( title );
568 /* Set job settings */
571 PrintTitleInfo( title );
573 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
575 job->chapter_start = MAX( job->chapter_start,
577 job->chapter_end = MIN( job->chapter_end,
579 job->chapter_end = MAX( job->chapter_start,
590 fprintf( stderr, "+ Using preset: %s", preset_name);
592 if (!strcmp(preset_name, "Universal"))
598 vcodec = HB_VCODEC_X264;
599 job->vquality = 20.0;
602 atracks = strdup("1,1");
606 acodecs = strdup("faac,ac3");
610 abitrates = strdup("160,160");
614 mixdowns = strdup("dpl2,auto");
618 arates = strdup("48,Auto");
620 if( !dynamic_range_compression )
622 dynamic_range_compression = strdup("0.0,0.0");
627 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6");
630 job->chapter_markers = 1;
633 if (!strcmp(preset_name, "iPod"))
640 vcodec = HB_VCODEC_X264;
644 atracks = strdup("1");
648 acodecs = strdup("faac");
652 abitrates = strdup("160");
656 mixdowns = strdup("dpl2");
660 arates = strdup("48");
662 if( !dynamic_range_compression )
664 dynamic_range_compression = strdup("0.0");
669 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0");
671 job->chapter_markers = 1;
674 if (!strcmp(preset_name, "iPhone & iPod Touch"))
680 vcodec = HB_VCODEC_X264;
681 job->vquality = 20.0;
684 atracks = strdup("1");
688 acodecs = strdup("faac");
692 abitrates = strdup("128");
696 mixdowns = strdup("dpl2");
700 arates = strdup("48");
702 if( !dynamic_range_compression )
704 dynamic_range_compression = strdup("0.0");
709 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0");
711 job->chapter_markers = 1;
714 if (!strcmp(preset_name, "AppleTV"))
720 job->largeFileSize = 1;
721 vcodec = HB_VCODEC_X264;
722 job->vquality = 20.0;
725 atracks = strdup("1,1");
729 acodecs = strdup("faac,ac3");
733 abitrates = strdup("160,160");
737 mixdowns = strdup("dpl2,auto");
741 arates = strdup("48,Auto");
743 if( !dynamic_range_compression )
745 dynamic_range_compression = strdup("0.0,0.0");
750 x264opts = strdup("cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0");
753 job->chapter_markers = 1;
756 if (!strcmp(preset_name, "Normal"))
762 vcodec = HB_VCODEC_X264;
763 job->vquality = 20.0;
766 atracks = strdup("1");
770 acodecs = strdup("faac");
774 abitrates = strdup("160");
778 mixdowns = strdup("dpl2");
782 arates = strdup("48");
784 if( !dynamic_range_compression )
786 dynamic_range_compression = strdup("0.0");
790 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
793 job->chapter_markers = 1;
796 if (!strcmp(preset_name, "High Profile"))
802 vcodec = HB_VCODEC_X264;
803 job->vquality = 20.0;
806 atracks = strdup("1,1");
810 acodecs = strdup("faac,ac3");
814 abitrates = strdup("160,160");
818 mixdowns = strdup("dpl2,auto");
822 arates = strdup("48,Auto");
824 if( !dynamic_range_compression )
826 dynamic_range_compression = strdup("0.0,0.0");
830 x264opts = strdup("b-adapt=2:rc-lookahead=50");
835 job->chapter_markers = 1;
838 if (!strcmp(preset_name, "Classic"))
844 job->vbitrate = 1000;
847 atracks = strdup("1");
851 acodecs = strdup("faac");
855 abitrates = strdup("160");
859 mixdowns = strdup("dpl2");
863 arates = strdup("48");
865 if( !dynamic_range_compression )
867 dynamic_range_compression = strdup("0.0");
871 if (!strcmp(preset_name, "AppleTV Legacy"))
877 job->largeFileSize = 1;
878 vcodec = HB_VCODEC_X264;
879 job->vbitrate = 2500;
882 atracks = strdup("1,1");
886 acodecs = strdup("faac,ac3");
890 abitrates = strdup("160,160");
894 mixdowns = strdup("dpl2,auto");
898 arates = strdup("48,Auto");
900 if( !dynamic_range_compression )
902 dynamic_range_compression = strdup("0.0,0.0");
906 x264opts = strdup("ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
909 job->chapter_markers = 1;
912 if (!strcmp(preset_name, "iPhone Legacy"))
919 vcodec = HB_VCODEC_X264;
923 atracks = strdup("1");
927 acodecs = strdup("faac");
931 abitrates = strdup("128");
935 mixdowns = strdup("dpl2");
939 arates = strdup("48");
941 if( !dynamic_range_compression )
943 dynamic_range_compression = strdup("0.0");
948 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:bframes=0:subme=6:8x8dct=0:trellis=0");
950 job->chapter_markers = 1;
953 if (!strcmp(preset_name, "iPod Legacy"))
960 vcodec = HB_VCODEC_X264;
961 job->vbitrate = 1500;
964 atracks = strdup("1");
968 acodecs = strdup("faac");
972 abitrates = strdup("160");
976 mixdowns = strdup("dpl2");
980 arates = strdup("48");
982 if( !dynamic_range_compression )
984 dynamic_range_compression = strdup("0.0");
989 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0");
991 job->chapter_markers = 1;
995 if ( chapter_markers )
997 job->chapter_markers = chapter_markers;
999 if( marker_file != NULL )
1001 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1002 hb_csv_cell_t * cell;
1006 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1010 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1014 /* Parse the cells */
1015 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1017 /* We have a chapter number */
1018 if( cell->cell_col == 0 )
1020 row = cell->cell_row;
1021 chapter = atoi( cell->cell_text );
1024 /* We have a chapter name */
1025 if( cell->cell_col == 1 && row == cell->cell_row )
1027 /* If we have a valid chapter, copy the string an terminate it */
1028 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1030 hb_chapter_t * chapter_s;
1032 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1033 strncpy(chapter_s->title, cell->cell_text, 1023);
1034 chapter_s->title[1023] = '\0';
1039 hb_dispose_cell( cell );
1042 hb_close_csv_file( file );
1047 /* No marker file */
1049 int number_of_chapters = hb_list_count(job->title->list_chapter);
1052 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1054 hb_chapter_t * chapter_s;
1055 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1056 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1057 chapter_s->title[1023] = '\0';
1062 if( crop[0] >= 0 && crop[1] >= 0 &&
1063 crop[2] >= 0 && crop[3] >= 0 )
1065 memcpy( job->crop, crop, 4 * sizeof( int ) );
1068 job->deinterlace = deinterlace;
1069 job->grayscale = grayscale;
1071 /* Add selected filters */
1072 job->filters = hb_list_init();
1075 hb_filter_detelecine.settings = detelecine_opt;
1076 hb_list_add( job->filters, &hb_filter_detelecine );
1080 hb_filter_decomb.settings = decomb_opt;
1081 hb_list_add( job->filters, &hb_filter_decomb );
1085 hb_filter_deinterlace.settings = deinterlace_opt;
1086 hb_list_add( job->filters, &hb_filter_deinterlace );
1090 hb_filter_deblock.settings = deblock_opt;
1091 hb_list_add( job->filters, &hb_filter_deblock );
1095 hb_filter_denoise.settings = denoise_opt;
1096 hb_list_add( job->filters, &hb_filter_denoise );
1099 switch( anamorphic_mode )
1101 case 0: // Non-anamorphic
1103 if( width && height )
1106 job->height = height;
1111 hb_fix_aspect( job, HB_KEEP_WIDTH );
1115 job->height = height;
1116 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1118 else if( !width && !height )
1120 hb_fix_aspect( job, HB_KEEP_WIDTH );
1125 case 1: // Strict anammorphic
1126 job->anamorphic.mode = anamorphic_mode;
1129 case 2: // Loose anamorphic
1130 job->anamorphic.mode = 2;
1134 job->anamorphic.modulus = modulus;
1139 job->anamorphic.itu_par = itu_par;
1146 else if( !width && !height )
1148 /* Default to full width when one isn't specified for loose anamorphic */
1149 job->width = title->width - job->crop[2] - job->crop[3];
1154 case 3: // Custom Anamorphic 3: Power User Jamboree
1155 job->anamorphic.mode = 3;
1159 job->anamorphic.modulus = modulus;
1164 job->anamorphic.itu_par = itu_par;
1167 if( par_width && par_height )
1169 job->anamorphic.par_width = par_width;
1170 job->anamorphic.par_height = par_height;
1173 if( keep_display_aspect )
1175 job->anamorphic.keep_display_aspect = 1;
1177 /* First, what *is* the display aspect? */
1178 int cropped_width = title->width - job->crop[2] - job->crop[3];
1179 int cropped_height = title->height - job->crop[0] - job->crop[1];
1181 /* XXX -- I'm assuming people want to keep the source
1182 display AR even though they might have already
1183 asked for ITU values instead. */
1184 float source_display_width = (float)cropped_width *
1185 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1186 float display_aspect = source_display_width / (float)cropped_height;
1187 /* When keeping display aspect, we have to rank some values
1188 by priority in order to consistently handle situations
1189 when more than one might be specified by default.
1191 * First off, PAR gets ignored. (err make this reality)
1192 * Height will be respected over all other settings,
1193 * If it isn't set, display_width will be followed.
1194 * If it isn't set, width will be followed. */
1197 /* We scale the display width to the new height */
1198 display_width = (int)( (double)height * display_aspect );
1200 else if( display_width )
1202 /* We scale the height to the new display width */
1203 height = (int)( (double)display_width / display_aspect );
1209 /* Adjust the PAR to create the new display width
1210 from the default job width. */
1211 job->anamorphic.dar_width = display_width;
1213 job->anamorphic.dar_height = height ?
1215 title->height - job->crop[0] - job->crop[1];
1218 if( width && height )
1220 /* Use these storage dimensions */
1222 job->height = height;
1226 /* Use just this storage width */
1228 job->height = title->height - job->crop[0] - job->crop[1];
1232 /* Use just this storage height. */
1233 job->height = height;
1234 job->width = title->width - job->crop[2] - job->crop[3];
1236 else if( !width && !height )
1238 /* Assume source dimensions after cropping. */
1239 job->width = title->width - job->crop[2] - job->crop[3];
1240 job->height = title->height - job->crop[0] - job->crop[1];
1246 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1248 job->vquality = vquality;
1253 job->vquality = -1.0;
1254 job->vbitrate = vbitrate;
1258 job->vcodec = vcodec;
1262 job->h264_level = 13;
1266 job->h264_level = 30;
1271 job->vrate = 27000000;
1272 job->vrate_base = vrate;
1276 // cfr or pfr flag with no rate specified implies
1277 // use the title rate.
1279 job->vrate = title->rate;
1280 job->vrate_base = title->rate_base;
1283 /* Grab audio tracks */
1286 char * token = strtok(atracks, ",");
1289 int track_start, track_end;
1290 while( token != NULL )
1292 audio = calloc(1, sizeof(*audio));
1293 hb_audio_config_init(audio);
1294 if (strlen(token) >= 3)
1296 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1299 for (i = track_start - 1; i < track_end; i++)
1301 if (i != track_start - 1)
1303 audio = calloc(1, sizeof(*audio));
1304 hb_audio_config_init(audio);
1306 audio->in.track = i;
1307 audio->out.track = num_audio_tracks++;
1308 hb_list_add(audios, audio);
1311 else if( !strcasecmp(token, "none" ) )
1313 audio->in.track = audio->out.track = -1;
1314 audio->out.codec = 0;
1315 hb_list_add(audios, audio);
1320 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1327 audio->in.track = atoi(token) - 1;
1328 audio->out.track = num_audio_tracks++;
1329 hb_list_add(audios, audio);
1331 token = strtok(NULL, ",");
1335 /* Parse audio tracks */
1336 if( native_language && native_dub )
1338 if( hb_list_count( audios ) == 0 || !audio_explicit )
1340 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1345 audio = hb_list_audio_config_item( title->list_audio, i );
1347 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1349 if( strncasecmp( native_language, audio_lang,
1350 sizeof( audio_lang ) ) == 0 &&
1351 audio->lang.type != 3 && // Directors 1
1352 audio->lang.type != 4) // Directors 2
1355 * Matched an audio to our native language - use it.
1356 * Replace any existing audio tracks that a preset may
1359 if( hb_list_count(audios) == 0) {
1360 audio = calloc(1, sizeof(*audio));
1361 hb_audio_config_init(audio);
1362 audio->in.track = track;
1363 audio->out.track = num_audio_tracks++;
1364 /* Add it to our audios */
1365 hb_list_add(audios, audio);
1368 * Update the track numbers on what is already in
1371 for( i=0; i < hb_list_count( audios ); i++ )
1373 audio = hb_list_item( audios, i );
1375 audio->in.track = track;
1382 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1386 if( hb_list_count(audios) == 0 &&
1387 hb_list_count(job->title->list_audio) > 0 )
1389 /* Create a new audio track with default settings */
1390 audio = calloc(1, sizeof(*audio));
1391 hb_audio_config_init(audio);
1392 /* Add it to our audios */
1393 hb_list_add(audios, audio);
1396 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1397 for (i = 0; i < tmp_num_audio_tracks; i++)
1399 audio = hb_list_item(audios, 0);
1400 if( (audio == NULL) || (audio->in.track == -1) ||
1401 (audio->out.track == -1) || (audio->out.codec == 0) )
1407 if( hb_audio_add( job, audio ) == 0 )
1409 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1410 audio->in.track + 1 );
1415 hb_list_rem(audios, audio);
1417 if( audio->out.name )
1419 free( audio->out.name);
1428 char * token = strtok(acodecs, ",");
1431 while ( token != NULL )
1433 if ((acodec = get_acodec_for_string(token)) == -1)
1435 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1436 acodec = default_acodec;
1438 if( i < num_audio_tracks )
1440 audio = hb_list_audio_config_item(job->list_audio, i);
1441 audio->out.codec = acodec;
1445 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1446 hb_audio_config_t audio;
1450 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1452 hb_audio_config_init(&audio);
1453 audio.in.track = last_audio->in.track;
1454 audio.out.track = num_audio_tracks++;
1455 audio.out.codec = acodec;
1456 hb_audio_add(job, &audio);
1460 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1463 token = strtok(NULL, ",");
1467 if( i < num_audio_tracks )
1469 /* We have fewer inputs than audio tracks, use the default codec for
1470 * this container for the remaining tracks. Unless we only have one input
1471 * then use that codec instead.
1474 acodec = default_acodec;
1475 for ( ; i < num_audio_tracks; i++)
1477 audio = hb_list_audio_config_item(job->list_audio, i);
1478 audio->out.codec = acodec;
1487 char * token = strtok(arates, ",");
1490 while ( token != NULL )
1492 arate = atoi(token);
1493 audio = hb_list_audio_config_item(job->list_audio, i);
1496 for( j = 0; j < hb_audio_rates_count; j++ )
1498 if( !strcmp( token, hb_audio_rates[j].string ) )
1500 arate = hb_audio_rates[j].rate;
1507 if (!is_sample_rate_valid(arate))
1509 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1510 arate = audio->in.samplerate;
1513 audio->out.samplerate = arate;
1514 if( (++i) >= num_audio_tracks )
1515 break; /* We have more inputs than audio tracks, oops */
1519 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1521 token = strtok(NULL, ",");
1524 if (i < num_audio_tracks)
1526 /* We have fewer inputs than audio tracks, use default sample rate.
1527 * Unless we only have one input, then use that for all tracks.
1530 arate = audio->in.samplerate;
1531 for ( ; i < num_audio_tracks; i++)
1533 audio = hb_list_audio_config_item(job->list_audio, i);
1534 audio->out.samplerate = arate;
1543 char * token = strtok(abitrates, ",");
1546 while ( token != NULL )
1548 abitrate = atoi(token);
1549 audio = hb_list_audio_config_item(job->list_audio, i);
1553 audio->out.bitrate = abitrate;
1554 if( (++i) >= num_audio_tracks )
1555 break; /* We have more inputs than audio tracks, oops */
1559 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1561 token = strtok(NULL, ",");
1564 if (i < num_audio_tracks)
1566 /* We have fewer inputs than audio tracks, use the default bitrate
1567 * for the remaining tracks. Unless we only have one input, then use
1568 * that for all tracks.
1571 abitrate = default_abitrate;
1572 for (; i < num_audio_tracks; i++)
1574 audio = hb_list_audio_config_item(job->list_audio, i);
1575 audio->out.bitrate = abitrate;
1582 if ( dynamic_range_compression )
1584 char * token = strtok(dynamic_range_compression, ",");
1586 token = dynamic_range_compression;
1587 while ( token != NULL )
1589 d_r_c = atof(token);
1590 audio = hb_list_audio_config_item(job->list_audio, i);
1593 audio->out.dynamic_range_compression = d_r_c;
1594 if( (++i) >= num_audio_tracks )
1595 break; /* We have more inputs than audio tracks, oops */
1599 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1601 token = strtok(NULL, ",");
1604 if (i < num_audio_tracks)
1606 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1607 * tracks. Unless we only have one input, then use the same DRC for all
1612 for (; i < num_audio_tracks; i++)
1614 audio = hb_list_audio_config_item(job->list_audio, i);
1615 audio->out.dynamic_range_compression = d_r_c;
1624 char * token = strtok(mixdowns, ",");
1627 while ( token != NULL )
1629 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1630 audio = hb_list_audio_config_item(job->list_audio, i);
1633 audio->out.mixdown = mixdown;
1634 if( (++i) >= num_audio_tracks )
1635 break; /* We have more inputs than audio tracks, oops */
1639 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1641 token = strtok(NULL, ",");
1644 if (i < num_audio_tracks)
1646 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1647 * we only have one input, then use that.
1650 mixdown = HB_AMIXDOWN_DOLBYPLII;
1651 for (; i < num_audio_tracks; i++)
1653 audio = hb_list_audio_config_item(job->list_audio, i);
1654 audio->out.mixdown = mixdown;
1659 /* Audio Track Names */
1663 char * token = strtok(anames, ",");
1666 while ( token != NULL )
1668 audio = hb_list_audio_config_item(job->list_audio, i);
1671 audio->out.name = strdup(token);
1672 if( (++i) >= num_audio_tracks )
1673 break; /* We have more names than audio tracks, oops */
1677 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1680 token = strtok(NULL, ",");
1683 if( i < num_audio_tracks && i == 1 )
1685 /* We have exactly one name and more than one audio track. Use the same
1686 * name for all tracks. */
1687 for ( ; i < num_audio_tracks; i++)
1689 audio = hb_list_audio_config_item(job->list_audio, i);
1690 audio->out.name = strdup(anames);
1693 /* Audio Track Names */
1697 job->vbitrate = hb_calc_bitrate( job, size );
1698 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1708 for ( i = 0; subtracks[i] != NULL; i++ )
1711 token = subtracks[i];
1712 if( strcasecmp(token, "scan" ) == 0 )
1714 int burn = 0, force = 0, def = 0;
1716 if ( subburn != NULL )
1718 burn = ( pos == 1 && subburn[0] == 0 ) ||
1719 ( strcmp( "scan", subburn ) == 0 );
1721 if ( subdefault != NULL )
1723 def = ( pos == 1 && subdefault[0] == 0 ) ||
1724 ( strcmp( "scan", subdefault ) == 0 );
1726 force = test_sub_list( subforce, "scan", pos );
1728 if ( !burn && mux == HB_MUX_MKV )
1730 job->select_subtitle_config.dest = PASSTHRUSUB;
1740 if ( !( !burn && mux == HB_MUX_MP4 ) )
1742 job->select_subtitle_config.force = force;
1743 job->select_subtitle_config.default_track = def;
1746 fprintf( stderr, "Warning: Subtitle Scan for MP4 requires the '--subtitle-burn' option to be selected\n");
1751 hb_subtitle_t * subtitle;
1752 hb_subtitle_config_t sub_config;
1754 int burn = 0, force = 0, def = 0;
1756 track = atoi(token) - 1;
1757 subtitle = hb_list_item(title->list_subtitle, track);
1758 if( subtitle == NULL )
1760 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1763 sub_config = subtitle->config;
1765 if ( subburn != NULL )
1767 burn = ( pos == 1 && subburn[0] == 0 ) ||
1768 ( strcmp( token, subburn ) == 0 );
1770 if ( subdefault != NULL )
1772 def = ( pos == 1 && subdefault[0] == 0 ) ||
1773 ( strcmp( token, subdefault ) == 0 );
1776 force = test_sub_list(subforce, token, pos);
1778 if ( !burn && mux == HB_MUX_MKV &&
1779 subtitle->format == PICTURESUB)
1781 sub_config.dest = PASSTHRUSUB;
1783 else if (!burn && mux == HB_MUX_MP4 &&
1784 subtitle->format == PICTURESUB)
1786 // Skip any non-burned vobsubs when output is mp4
1787 fprintf( stderr, "Warning: Skipping subtitle track %d, can't pass-through VOBSUBs in an MP4 container,\nadd '--subtitle-burn %d' to the command line\n", track+1, track+1 );
1790 else if ( burn && subtitle->format == PICTURESUB )
1792 // Only allow one subtitle to be burned into video
1795 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1800 sub_config.force = force;
1801 sub_config.default_track = def;
1802 hb_subtitle_add( job, &sub_config, track );
1811 hb_subtitle_config_t sub_config;
1814 for( i=0; srtfile[i] != NULL; i++ )
1816 char *codeset = "L1";
1822 if( srtcodeset && srtcodeset[i] )
1824 codeset = srtcodeset[i];
1826 if( srtoffset && srtoffset[i] )
1828 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1830 if ( srtlang && srtlang[i] )
1834 sub_config.default_track =
1835 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1836 sub_config.force = 0;
1837 strncpy( sub_config.src_filename, srtfile[i], 128);
1838 strncpy( sub_config.src_codeset, codeset, 40);
1839 sub_config.offset = offset;
1841 hb_srt_add( job, &sub_config, lang);
1845 if( native_language )
1849 audio = hb_list_audio_config_item(job->list_audio, 0);
1853 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1855 if( strncasecmp( native_language, audio_lang,
1856 sizeof( audio_lang ) ) != 0 )
1859 * Audio language is not the same as our native language.
1860 * If we have any subtitles in our native language they
1861 * should be selected here if they haven't already been.
1863 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1864 int matched_track = 0;
1866 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1868 subtitle = hb_list_item( title->list_subtitle, i );
1870 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1873 * Found the first matching subtitle in our
1874 * native language. Is it already selected?
1876 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1878 subtitle2 = hb_list_item( job->list_subtitle, i );
1880 if( subtitle2->track == subtitle->track) {
1889 if( subtitle2 == NULL )
1892 * Not already selected, so select it.
1894 hb_subtitle_config_t sub_config;
1898 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1900 sub_config = subtitle->config;
1902 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1904 sub_config.dest = PASSTHRUSUB;
1907 sub_config.force = 0;
1908 sub_config.default_track = 1;
1909 hb_subtitle_add( job, &sub_config, matched_track);
1926 if ( largeFileSize )
1928 job->largeFileSize = 1;
1932 job->mp4_optimize = 1;
1939 job->file = strdup( output );
1943 job->color_matrix = color_matrix;
1946 if( x264opts != NULL && *x264opts != '\0' )
1948 job->x264opts = x264opts;
1950 else /*avoids a bus error crash when options aren't specified*/
1952 job->x264opts = NULL;
1955 job->maxWidth = maxWidth;
1957 job->maxHeight = maxHeight;
1959 if( start_at_preview )
1961 job->start_at_preview = start_at_preview - 1;
1962 job->seek_points = preview_count;
1967 job->pts_to_stop = stop_at_pts;
1973 job->frame_to_stop = stop_at_frame;
1982 * When subtitle scan is enabled do a fast pre-scan job
1983 * which will determine which subtitles to enable, if any.
1987 x264opts_tmp = job->x264opts;
1989 job->x264opts = NULL;
1991 job->indepth_scan = subtitle_scan;
1992 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1993 "subtitles if found for foreign language segments\n");
1996 * Add the pre-scan job
2000 job->x264opts = x264opts_tmp;
2006 * If subtitle_scan is enabled then only turn it on
2007 * for the first pass and then off again for the
2012 job->indepth_scan = 0;
2016 x264opts2 = strdup(x264opts);
2020 * If turbo options have been selected then append them
2021 * to the x264opts now (size includes one ':' and the '\0')
2023 if( turbo_opts_enabled )
2025 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2028 tmp_x264opts = malloc(size * sizeof(char));
2031 snprintf( tmp_x264opts, size, "%s:%s",
2032 x264opts, turbo_opts );
2036 * No x264opts to modify, but apply the turbo options
2037 * anyway as they may be modifying defaults
2039 snprintf( tmp_x264opts, size, "%s",
2042 x264opts = tmp_x264opts;
2044 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2047 job->x264opts = x264opts;
2053 * On the second pass we turn off subtitle scan so that we
2054 * can actually encode using any subtitles that were auto
2055 * selected in the first pass (using the whacky select-subtitle
2056 * attribute of the job).
2058 job->indepth_scan = 0;
2060 job->x264opts = x264opts2;
2067 * Turn on subtitle scan if requested, note that this option
2068 * precludes encoding of any actual subtitles.
2071 job->indepth_scan = 0;
2079 #define p s.param.working
2080 case HB_STATE_WORKING:
2081 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2082 p.job_cur, p.job_count, 100.0 * p.progress );
2083 if( p.seconds > -1 )
2085 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2086 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2087 p.hours, p.minutes, p.seconds );
2093 #define p s.param.muxing
2094 case HB_STATE_MUXING:
2096 if (show_mux_warning)
2098 fprintf( stdout, "\rMuxing: this may take awhile..." );
2100 show_mux_warning = 0;
2106 #define p s.param.workdone
2107 case HB_STATE_WORKDONE:
2108 /* Print error if any, then exit */
2112 fprintf( stderr, "\nRip done!\n" );
2114 case HB_ERROR_CANCELED:
2115 fprintf( stderr, "\nRip canceled.\n" );
2118 fprintf( stderr, "\nRip failed (error %x).\n",
2128 /****************************************************************************
2130 ****************************************************************************/
2131 static volatile int64_t i_die_date = 0;
2132 void SigHandler( int i_signal )
2137 i_die_date = hb_get_date();
2138 fprintf( stderr, "Signal %d received, terminating - do it "
2139 "again in case it gets stuck\n", i_signal );
2141 else if( i_die_date + 500 < hb_get_date() )
2143 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2148 /****************************************************************************
2150 ****************************************************************************/
2151 static void ShowHelp()
2154 FILE* const out = stdout;
2157 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2159 "### General Handbrake Options------------------------------------------------\n\n"
2160 " -h, --help Print help\n"
2161 " -u, --update Check for updates and exit\n"
2162 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2163 " -C, --cpu Set CPU count (default: autodetected)\n"
2164 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2165 " if the preset name has spaces, surround it with\n"
2166 " double quotation marks\n"
2167 " -z, --preset-list See a list of available built-in presets\n"
2168 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2169 " (experimental, enabled by default for testing)\n"
2172 "### Source Options-----------------------------------------------------------\n\n"
2173 " -i, --input <string> Set input device\n"
2174 " -t, --title <number> Select a title to encode (0 to scan only,\n"
2176 " -L, --longest Select the longest title\n"
2177 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2178 " 1 to 3, or \"3\" for chapter 3 only,\n"
2179 " default: all chapters)\n"
2180 " --angle <number> Select the DVD angle\n"
2181 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2182 " and whether or not they're stored to disk (0 or 1).\n"
2183 " (default: 10:0)\n"
2184 " --start-at-preview <#> Start encoding at a given preview.\n"
2185 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2186 " or pts (on a 90kHz clock)"
2189 "### Destination Options------------------------------------------------------\n\n"
2190 " -o, --output <string> Set output file name\n"
2191 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2192 " autodetected from file name)\n"
2193 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2194 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2195 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2196 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2197 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2201 "### Video Options------------------------------------------------------------\n\n"
2202 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2203 " (default: ffmpeg)\n"
2204 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2205 " same style as mencoder:\n"
2206 " option1=value1:option2=value2\n"
2207 " -q, --quality <float> Set video quality (0.0..1.0)\n"
2208 " -S, --size <MB> Set target size\n"
2209 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2210 " -2, --two-pass Use two-pass mode\n"
2211 " -T, --turbo When using 2-pass use the turbo options\n"
2212 " on the first pass to improve speed\n"
2213 " (only works with x264, affects PSNR by about 0.05dB,\n"
2214 " and increases first pass speed two to four times)\n"
2215 " -r, --rate Set video framerate (" );
2216 for( i = 0; i < hb_video_rates_count; i++ )
2218 fprintf( out, hb_video_rates[i].string );
2219 if( i != hb_video_rates_count - 1 )
2220 fprintf( out, "/" );
2223 " Be aware that not specifying a framerate lets\n"
2224 " HandBrake preserve a source's time stamps,\n"
2225 " potentially creating variable framerate video\n"
2226 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2227 " frame rate control. VFR preserves the source\n"
2228 " timing. CFR makes the output constant rate at\n"
2229 " the rate given by the -r flag (or the source's\n"
2230 " average rate if no -r is given). PFR doesn't\n"
2231 " allow the rate to go over the rate specified\n"
2232 " with the -r flag but won't change the source\n"
2233 " timing if it's below that rate.\n"
2234 " If none of these flags are given, the default\n"
2235 " is --cfr when -r is given and --vfr otherwise\n"
2238 "### Audio Options-----------------------------------------------------------\n\n"
2239 " -a, --audio <string> Select audio track(s), separated by commas\n"
2240 " More than one output track can be used for one\n"
2242 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2243 " tracks, default: first one)\n" );
2247 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2248 " ac3 and dts meaning passthrough\n"
2249 " Separated by commas for more than one audio track.\n"
2250 " (default: guessed)\n" );
2253 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2254 " ac3 and dts meaning passthrough\n"
2255 " Separated by commas for more than one audio track.\n"
2256 " (default: guessed)\n" );
2259 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2260 " Separated by commas for more than one audio track.\n"
2261 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2262 " Separated by commas for more than one audio track.\n"
2263 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2264 " -R, --arate Set audio samplerate(s) (" );
2265 for( i = 0; i < hb_audio_rates_count; i++ )
2267 fprintf( out, hb_audio_rates[i].string );
2268 if( i != hb_audio_rates_count - 1 )
2269 fprintf( out, "/" );
2271 fprintf( out, " kHz)\n"
2272 " Separated by commas for more than one audio track.\n"
2273 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2274 " making soft sounds louder. Range is 1.0 to 4.0\n"
2275 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2276 " Separated by commas for more than one audio track.\n"
2277 " -A, --aname <string> Audio track name(s),\n"
2278 " Separated by commas for more than one audio track.\n"
2281 "### Picture Settings---------------------------------------------------------\n\n"
2282 " -w, --width <number> Set picture width\n"
2283 " -l, --height <number> Set picture height\n"
2284 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2285 " -Y, --maxHeight <#> Set maximum height\n"
2286 " -X, --maxWidth <#> Set maximum width\n"
2287 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2288 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2289 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2290 " directly control all parameters.\n"
2291 " --display-width Set the width to scale the actual pixels to\n"
2292 " <number> at playback, for custom anamorphic.\n"
2293 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2294 " when using custom anamorphic\n"
2295 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2297 " (--display-width and --pixel-aspect are mutually\n"
2298 " exclusive and the former will override the latter)\n"
2299 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2300 " custom anamorphic, useful with underscanned sources\n"
2301 " --modulus Set the number you want the scaled pixel dimensions\n"
2302 " <number> to divide cleanly by, for loose and custom\n"
2303 " anamorphic modes (default: 16)\n"
2304 " -M --color-matrix Set the color space signaled by the output\n"
2305 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2306 " default: set by resolution)\n"
2309 "### Filters---------------------------------------------------------\n\n"
2311 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2312 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2314 " <fast/slow/slower>\n"
2315 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2316 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2317 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2318 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2319 " Note: this filter drops duplicate frames to\n"
2320 " restore the pre-telecine framerate, unless you\n"
2321 " specify a constant framerate (--rate 29.97)\n"
2322 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2323 " -8, --denoise Denoise video with hqdn3d filter\n"
2324 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2326 " <weak/medium/strong>\n"
2327 " -7, --deblock Deblock video with pp7 filter\n"
2328 " <QP:M> (default 5:2)\n"
2329 " -g, --grayscale Grayscale encoding\n"
2332 "### Subtitle Options------------------------------------------------------------\n\n"
2333 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2334 " More than one output track can be used for one\n"
2336 " (\"1,2,3\" for multiple tracks.\n"
2337 " A special track name \"scan\" adds an extra 1st pass.\n"
2338 " This extra pass scans subtitles matching the\n"
2339 " language of the first audio or the language \n"
2340 " selected by --native-language.\n"
2341 " The one that's only used 10 percent of the time\n"
2342 " or less is selected. This should locate subtitles\n"
2343 " for short foreign language segments. Best used in\n"
2344 " conjunction with --subtitle-forced.\n"
2345 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2346 " <string> the subtitle has the forced flag set. May be used in\n"
2347 " conjunction with \"scan\" track to auto-select\n"
2348 " a stream if it contains forced subtitles.\n"
2349 " Separated by commas for more than one audio track.\n"
2350 " (\"1,2,3\" for multiple tracks.\n"
2351 " If \"string\" is omitted, the first trac is forced.\n"
2352 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2353 " <number> If \"number\" is omitted, the first trac is burned.\n"
2354 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2355 " <number> to be displayed upon playback. Setting no default\n"
2356 " means no subtitle will be automatically displayed\n"
2357 " If \"number\" is omitted, the first trac is default.\n"
2358 " -N, --native-language Specifiy the your language preference. When the first\n"
2359 " <string> audio track does not match your native language then\n"
2360 " select the first subtitle that does. When used in\n"
2361 " conjunction with --native-dub the audio track is\n"
2362 " changed in preference to subtitles. Provide the\n"
2363 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2364 " --native-dub Used in conjunction with --native-language\n"
2365 " requests that if no audio tracks are selected the\n"
2366 " default selected audio track will be the first one\n"
2367 " that matches the --native-language. If there are no\n"
2368 " matching audio tracks then the first matching\n"
2369 " subtitle track is used instead.\n"
2370 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2371 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2372 " <string> encoded in, separted by commas.\n"
2373 " Use 'iconv -l' for a list of valid\n"
2374 " codesets. If not specified latin1 is assumed\n"
2375 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2376 " <string> separted by commas. If not specified zero is assumed.\n"
2377 " Offsets may be negative.\n"
2378 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2379 " for the SRT file(s) separated by commas. If not specified\n"
2380 " then 'und' is used.\n"
2381 " --srt-default Flag the selected srt as the default subtitle\n"
2382 " <number> to be displayed upon playback. Setting no default\n"
2383 " means no subtitle will be automatically displayed\n"
2384 " If \"number\" is omitted, the first srt is default.\n"
2385 " \"number\" is an 1 based index into the srt-file list\n"
2392 /****************************************************************************
2394 ****************************************************************************/
2395 static void ShowPresets()
2397 printf("\n< Apple\n");
2399 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6\n");
2401 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0\n");
2403 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0\n");
2405 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0\n");
2409 printf("\n< Regular\n");
2411 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 --strict-anamorphic -m -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0\n");
2413 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50\n");
2417 printf("\n< Legacy\n");
2419 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2421 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0\n");
2423 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:bframes=0:subme=6:8x8dct=0:trellis=0\n");
2425 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0\n");
2430 static char * hb_strndup( char * str, int len )
2433 int str_len = strlen( str );
2435 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2436 strncpy( res, str, len );
2441 static char** str_split( char *str, char delem )
2448 if ( str == NULL || str[0] == 0 )
2450 ret = malloc( sizeof(char*) );
2455 // Find number of elements in the string
2458 while ( ( pos = strchr( pos, delem ) ) != NULL )
2464 ret = calloc( ( count + 1 ), sizeof(char*) );
2467 for ( i = 0; i < count - 1; i++ )
2469 end = strchr( pos, delem );
2470 ret[i] = hb_strndup(pos, end - pos);
2473 ret[i] = strdup(pos);
2478 /****************************************************************************
2480 ****************************************************************************/
2481 static int ParseOptions( int argc, char ** argv )
2484 #define PREVIEWS 257
2485 #define START_AT_PREVIEW 258
2489 #define DISPLAY_WIDTH 262
2490 #define PIXEL_ASPECT 263
2492 #define KEEP_DISPLAY_ASPECT 265
2493 #define SUB_BURNED 266
2494 #define SUB_DEFAULT 267
2495 #define NATIVE_DUB 268
2496 #define SRT_FILE 269
2497 #define SRT_CODESET 270
2498 #define SRT_OFFSET 271
2499 #define SRT_LANG 272
2500 #define SRT_DEFAULT 273
2504 static struct option long_options[] =
2506 { "help", no_argument, NULL, 'h' },
2507 { "update", no_argument, NULL, 'u' },
2508 { "verbose", optional_argument, NULL, 'v' },
2509 { "cpu", required_argument, NULL, 'C' },
2510 { "no-dvdnav", no_argument, NULL, DVDNAV },
2512 { "format", required_argument, NULL, 'f' },
2513 { "input", required_argument, NULL, 'i' },
2514 { "output", required_argument, NULL, 'o' },
2515 { "large-file", no_argument, NULL, '4' },
2516 { "optimize", no_argument, NULL, 'O' },
2517 { "ipod-atom", no_argument, NULL, 'I' },
2519 { "title", required_argument, NULL, 't' },
2520 { "longest", no_argument, NULL, 'L' },
2521 { "chapters", required_argument, NULL, 'c' },
2522 { "angle", required_argument, NULL, ANGLE },
2523 { "markers", optional_argument, NULL, 'm' },
2524 { "audio", required_argument, NULL, 'a' },
2525 { "mixdown", required_argument, NULL, '6' },
2526 { "drc", required_argument, NULL, 'D' },
2527 { "subtitle", required_argument, NULL, 's' },
2528 { "subtitle-forced", optional_argument, NULL, 'F' },
2529 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2530 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2531 { "srt-file", required_argument, NULL, SRT_FILE },
2532 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2533 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2534 { "srt-lang", required_argument, NULL, SRT_LANG },
2535 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2536 { "native-language", required_argument, NULL,'N' },
2537 { "native-dub", no_argument, NULL, NATIVE_DUB },
2538 { "encoder", required_argument, NULL, 'e' },
2539 { "aencoder", required_argument, NULL, 'E' },
2540 { "two-pass", no_argument, NULL, '2' },
2541 { "deinterlace", optional_argument, NULL, 'd' },
2542 { "deblock", optional_argument, NULL, '7' },
2543 { "denoise", optional_argument, NULL, '8' },
2544 { "detelecine", optional_argument, NULL, '9' },
2545 { "decomb", optional_argument, NULL, '5' },
2546 { "grayscale", no_argument, NULL, 'g' },
2547 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2548 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2549 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2550 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2551 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2552 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2553 { "modulus", required_argument, NULL, MODULUS },
2554 { "itu-par", no_argument, &itu_par, 1 },
2555 { "width", required_argument, NULL, 'w' },
2556 { "height", required_argument, NULL, 'l' },
2557 { "crop", required_argument, NULL, 'n' },
2559 { "vb", required_argument, NULL, 'b' },
2560 { "quality", required_argument, NULL, 'q' },
2561 { "size", required_argument, NULL, 'S' },
2562 { "ab", required_argument, NULL, 'B' },
2563 { "rate", required_argument, NULL, 'r' },
2564 { "arate", required_argument, NULL, 'R' },
2565 { "x264opts", required_argument, NULL, 'x' },
2566 { "turbo", no_argument, NULL, 'T' },
2567 { "maxHeight", required_argument, NULL, 'Y' },
2568 { "maxWidth", required_argument, NULL, 'X' },
2569 { "preset", required_argument, NULL, 'Z' },
2570 { "preset-list", no_argument, NULL, 'z' },
2572 { "aname", required_argument, NULL, 'A' },
2573 { "color-matrix",required_argument, NULL, 'M' },
2574 { "previews", required_argument, NULL, PREVIEWS },
2575 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2576 { "stop-at", required_argument, NULL, STOP_AT },
2577 { "vfr", no_argument, &cfr, 0 },
2578 { "cfr", no_argument, &cfr, 1 },
2579 { "pfr", no_argument, &cfr, 2 },
2583 int option_index = 0;
2586 c = getopt_long( argc, argv,
2587 "hv::uC:f:4i:Io:t:Lc:m::M:a:A:6:s:UF::N:e:E:2dD:7895gOw:l:n:b:q:S:B:r:R:x:TY:X:Z:z",
2588 long_options, &option_index );
2597 /* option was handled entirely in getopt_long */
2606 if( optarg != NULL )
2608 debug = atoi( optarg );
2616 cpu = atoi( optarg );
2621 preset_name = strdup(optarg);
2631 format = strdup( optarg );
2634 input = strdup( optarg );
2636 char *devName = bsd_name_for_path( input ); // alloc
2639 if( device_is_dvd( devName ))
2642 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2643 sprintf( input, "/dev/%s", devName );
2650 output = strdup( optarg );
2663 titleindex = atoi( optarg );
2671 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2673 chapter_start = start;
2676 else if( sscanf( optarg, "%d", &start ) == 1 )
2678 chapter_start = start;
2679 chapter_end = chapter_start;
2683 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2690 angle = atoi( optarg );
2693 if( optarg != NULL )
2695 marker_file = strdup( optarg );
2697 chapter_markers = 1;
2700 if( optarg != NULL )
2702 atracks = strdup( optarg );
2711 if( optarg != NULL )
2713 mixdowns = strdup( optarg );
2717 if( optarg != NULL )
2719 dynamic_range_compression = strdup( optarg );
2723 subtracks = str_split( optarg, ',' );
2726 subforce = str_split( optarg, ',' );
2729 if( optarg != NULL )
2731 subburn = strdup( optarg );
2739 if( optarg != NULL )
2741 subdefault = strdup( optarg );
2749 native_language = strdup( optarg );
2755 srtfile = str_split( optarg, ',' );
2758 srtcodeset = str_split( optarg, ',' );
2761 srtoffset = str_split( optarg, ',' );
2764 srtlang = str_split( optarg, ',' );
2767 if( optarg != NULL )
2769 srtdefault = atoi( optarg );
2780 if( optarg != NULL )
2782 if (!( strcmp( optarg, "fast" ) ))
2784 deinterlace_opt = "-1";
2786 else if (!( strcmp( optarg, "slow" ) ))
2788 deinterlace_opt = "2";
2790 else if (!( strcmp( optarg, "slower" ) ))
2792 deinterlace_opt = "0";
2796 deinterlace_opt = strdup( optarg );
2802 if( optarg != NULL )
2804 deblock_opt = strdup( optarg );
2809 if( optarg != NULL )
2811 if (!( strcmp( optarg, "weak" ) ))
2813 denoise_opt = "2:1:2:3";
2815 else if (!( strcmp( optarg, "medium" ) ))
2817 denoise_opt = "3:2:2:3";
2819 else if (!( strcmp( optarg, "strong" ) ))
2821 denoise_opt = "7:7:5:5";
2825 denoise_opt = strdup( optarg );
2831 if( optarg != NULL )
2833 detelecine_opt = strdup( optarg );
2838 if( optarg != NULL )
2840 decomb_opt = strdup( optarg );
2848 if( optarg != NULL )
2850 sscanf( optarg, "%i", &display_width );
2854 if( optarg != NULL )
2856 sscanf( optarg, "%i:%i", &par_width, &par_height );
2860 if( optarg != NULL )
2862 sscanf( optarg, "%i", &modulus );
2866 if( !strcasecmp( optarg, "ffmpeg" ) )
2868 vcodec = HB_VCODEC_FFMPEG;
2870 else if( !strcasecmp( optarg, "x264" ) )
2872 vcodec = HB_VCODEC_X264;
2874 else if( !strcasecmp( optarg, "x264b13" ) )
2876 vcodec = HB_VCODEC_X264;
2879 else if( !strcasecmp( optarg, "x264b30" ) )
2881 vcodec = HB_VCODEC_X264;
2884 else if( !strcasecmp( optarg, "theora" ) )
2886 vcodec = HB_VCODEC_THEORA;
2890 fprintf( stderr, "invalid codec (%s)\n", optarg );
2895 if( optarg != NULL )
2897 acodecs = strdup( optarg );
2901 width = atoi( optarg );
2904 height = atoi( optarg );
2909 char * tmp = optarg;
2910 for( i = 0; i < 4; i++ )
2914 crop[i] = strtol( tmp, &tmp, 0 );
2923 for( i = 0; i < hb_video_rates_count; i++ )
2925 if( !strcmp( optarg, hb_video_rates[i].string ) )
2927 vrate = hb_video_rates[i].rate;
2933 fprintf( stderr, "invalid framerate %s\n", optarg );
2935 else if ( cfr == 0 )
2942 if( optarg != NULL )
2944 arates = strdup( optarg );
2948 vbitrate = atoi( optarg );
2951 vquality = atof( optarg );
2954 size = atoi( optarg );
2957 if( optarg != NULL )
2959 abitrates = strdup( optarg );
2963 x264opts = strdup( optarg );
2966 turbo_opts_enabled = 1;
2969 maxHeight = atoi( optarg );
2972 maxWidth = atoi (optarg );
2975 if( optarg != NULL )
2977 anames = strdup( optarg );
2981 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2983 case START_AT_PREVIEW:
2984 start_at_preview = atoi( optarg );
2987 stop_at_string = strdup( optarg );
2988 stop_at_token = strtok( stop_at_string, ":");
2989 if( !strcmp( stop_at_token, "frame" ) )
2991 stop_at_token = strtok( NULL, ":");
2992 stop_at_frame = atoi(stop_at_token);
2994 else if( !strcmp( stop_at_token, "pts" ) )
2996 stop_at_token = strtok( NULL, ":");
2997 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2999 else if( !strcmp( stop_at_token, "duration" ) )
3001 stop_at_token = strtok( NULL, ":");
3002 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3003 stop_at_pts *= 90000LL;
3007 if( atoi( optarg ) == 601 )
3009 else if( atoi( optarg ) == 709 )
3013 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
3021 static int CheckOptions( int argc, char ** argv )
3028 if( input == NULL || *input == '\0' )
3030 fprintf( stderr, "Missing input device. Run %s --help for "
3031 "syntax.\n", argv[0] );
3036 if( titleindex > 0 )
3038 if( output == NULL || *output == '\0' )
3040 fprintf( stderr, "Missing output file name. Run %s --help "
3041 "for syntax.\n", argv[0] );
3047 char * p = strrchr( output, '.' );
3050 if( p && ( !strcasecmp( p, ".mp4" ) ||
3051 !strcasecmp( p, ".m4v" ) ) )
3057 default_acodec = HB_ACODEC_FAAC;
3059 else if( p && !strcasecmp(p, ".mkv" ) )
3062 default_acodec = HB_ACODEC_AC3;
3066 fprintf( stderr, "Output format couldn't be guessed "
3067 "from file name, using default.\n" );
3071 else if( !strcasecmp( format, "mp4" ) ||
3072 !strcasecmp( format, "m4v" ) )
3078 default_acodec = HB_ACODEC_FAAC;
3080 else if( !strcasecmp( format, "mkv" ) )
3083 default_acodec = HB_ACODEC_AC3;
3087 fprintf( stderr, "Invalid output format (%s). Possible "
3088 "choices are mp4, m4v and mkv\n.", format );
3096 static int get_acodec_for_string( char *codec )
3098 if( !strcasecmp( codec, "ac3" ) )
3100 return HB_ACODEC_AC3;
3102 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3104 return HB_ACODEC_DCA;
3106 else if( !strcasecmp( codec, "lame" ) )
3108 return HB_ACODEC_LAME;
3110 else if( !strcasecmp( codec, "faac" ) )
3112 return HB_ACODEC_FAAC;
3114 else if( !strcasecmp( codec, "vorbis") )
3116 return HB_ACODEC_VORBIS;
3119 else if( !strcasecmp( codec, "ca_aac") )
3121 return HB_ACODEC_CA_AAC;
3130 static int is_sample_rate_valid(int rate)
3133 for( i = 0; i < hb_audio_rates_count; i++ )
3135 if (rate == hb_audio_rates[i].rate)
3142 /****************************************************************************
3145 * Returns the BSD device name for the block device that contains the
3146 * passed-in path. Returns NULL on failure.
3147 ****************************************************************************/
3148 static char* bsd_name_for_path(char *path)
3152 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3158 // Get the volume reference number.
3159 FSCatalogInfo catalogInfo;
3160 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3166 FSVolumeRefNum volRefNum = catalogInfo.volume;
3168 // Now let's get the device name
3169 GetVolParmsInfoBuffer volumeParms;
3170 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3176 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3177 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3178 if( volumeParms.vMVersion < 4 )
3183 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3184 if( !volumeParms.vMDeviceID )
3189 return strdup( volumeParms.vMDeviceID );
3192 /****************************************************************************
3195 * Returns whether or not the passed in BSD device represents a DVD, or other
3197 ****************************************************************************/
3198 static int device_is_dvd(char *device)
3200 io_service_t service = get_iokit_service(device);
3201 if( service == IO_OBJECT_NULL )
3205 int result = is_dvd_service(service);
3206 IOObjectRelease(service);
3210 /****************************************************************************
3213 * Returns the IOKit service object for the passed in BSD device name.
3214 ****************************************************************************/
3215 static io_service_t get_iokit_service( char *device )
3217 CFMutableDictionaryRef matchingDict;
3218 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3219 if( matchingDict == NULL )
3221 return IO_OBJECT_NULL;
3223 // Fetch the object with the matching BSD node name. There should only be
3224 // one match, so IOServiceGetMatchingService is used instead of
3225 // IOServiceGetMatchingServices to simplify the code.
3226 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3229 /****************************************************************************
3232 * Returns whether or not the service passed in is a DVD.
3234 * Searches for an IOMedia object that represents the entire (whole) media that
3235 * the volume is on. If the volume is on partitioned media, the whole media
3236 * object will be a parent of the volume's media object. If the media is not
3237 * partitioned, the volume's media object will be the whole media object.
3238 ****************************************************************************/
3239 static int is_dvd_service( io_service_t service )
3241 kern_return_t kernResult;
3244 // Create an iterator across all parents of the service object passed in.
3245 kernResult = IORegistryEntryCreateIterator( service,
3247 kIORegistryIterateRecursively | kIORegistryIterateParents,
3249 if( kernResult != KERN_SUCCESS )
3253 if( iter == IO_OBJECT_NULL )
3258 // A reference on the initial service object is released in the do-while
3259 // loop below, so add a reference to balance.
3260 IOObjectRetain( service );
3265 if( is_whole_media_service( service ) &&
3266 IOObjectConformsTo( service, kIODVDMediaClass) )
3270 IOObjectRelease( service );
3271 } while( !result && (service = IOIteratorNext( iter )) );
3272 IOObjectRelease( iter );
3277 /****************************************************************************
3278 * is_whole_media_service
3280 * Returns whether or not the service passed in is an IOMedia service and
3281 * represents the "whole" media instead of just a partition.
3283 * The whole media object is indicated in the IORegistry by the presence of a
3284 * property with the key "Whole" and value "Yes".
3285 ****************************************************************************/
3286 static int is_whole_media_service( io_service_t service )
3290 if( IOObjectConformsTo( service, kIOMediaClass ) )
3292 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3293 CFSTR( kIOMediaWholeKey ),
3294 kCFAllocatorDefault,
3300 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3301 CFRelease( wholeMedia );
3306 #endif // __APPLE_CC__