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_NONE;
34 static int update = 0;
35 static int dvdnav = 0;
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;
103 static char *x264opts = NULL;
104 static char *x264opts2 = NULL;
105 static int maxHeight = 0;
106 static int maxWidth = 0;
107 static int turbo_opts_enabled = 0;
108 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
109 static int largeFileSize = 0;
110 static int preset = 0;
111 static char * preset_name = 0;
113 static int mp4_optimize = 0;
114 static int ipod_atom = 0;
115 static int color_matrix = 0;
116 static int preview_count = 10;
117 static int store_previews = 0;
118 static int start_at_preview = 0;
119 static int64_t stop_at_pts = 0;
120 static int stop_at_frame = 0;
121 static char * stop_at_string = NULL;
122 static char * stop_at_token = NULL;
124 /* Exit cleanly on Ctrl-C */
125 static volatile int die = 0;
126 static void SigHandler( int );
129 static void ShowCommands();
130 static void ShowHelp();
131 static void ShowPresets();
133 static int ParseOptions( int argc, char ** argv );
134 static int CheckOptions( int argc, char ** argv );
135 static int HandleEvents( hb_handle_t * h );
137 static int get_acodec_for_string( char *codec );
138 static int is_sample_rate_valid(int rate);
141 static char* bsd_name_for_path(char *path);
142 static int device_is_dvd(char *device);
143 static io_service_t get_iokit_service( char *device );
144 static int is_dvd_service( io_service_t service );
145 static int is_whole_media_service( io_service_t service );
148 /* Only print the "Muxing..." message once */
149 static int show_mux_warning = 1;
151 /****************************************************************************
154 * When using the CLI just display using hb_log as we always did in the past
155 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
156 ****************************************************************************/
157 static void hb_cli_error_handler ( const char *errmsg )
159 fprintf( stderr, "ERROR: %s\n", errmsg );
162 int main( int argc, char ** argv )
168 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
169 * force it to unbuffered otherwise informative output is not easily parsed.
171 #if defined( _WIN32 ) || defined( __MINGW32__ )
172 setvbuf( stdout, NULL, _IONBF, 0 );
173 setvbuf( stderr, NULL, _IONBF, 0 );
176 audios = hb_list_init();
178 /* Parse command line */
179 if( ParseOptions( argc, argv ) ||
180 CheckOptions( argc, argv ) )
185 #if defined( PTW32_STATIC_LIB )
186 pthread_win32_process_attach_np();
187 pthread_win32_thread_attach_np();
190 /* Register our error handler */
191 hb_register_error_handler(&hb_cli_error_handler);
194 h = hb_init( debug, update );
195 hb_dvd_set_dvdnav( dvdnav );
198 fprintf( stderr, "%s - %s - %s\n",
199 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
201 /* Check for update */
204 if( ( build = hb_check_update( h, &version ) ) > -1 )
206 fprintf( stderr, "You are using an old version of "
207 "HandBrake.\nLatest is %s (build %d).\n", version,
212 fprintf( stderr, "Your version of HandBrake is up to "
220 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
221 hb_get_cpu_count( h ) > 1 ? "s" : "" );
224 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
225 cpu > 1 ? "s" : "" );
226 hb_set_cpu_count( h, cpu );
229 /* Exit ASAP on Ctrl-C */
230 signal( SIGINT, SigHandler );
232 /* Feed libhb with a DVD to scan */
233 fprintf( stderr, "Opening %s...\n", input );
237 * We need to scan for all the titles in order to find the longest
242 hb_scan( h, input, titleindex, preview_count, store_previews );
247 #if defined( __MINGW32__ )
251 case 0x03: /* ctrl-c */
253 fprintf( stdout, "\nEncoding Quit by user command\n" );
257 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
269 #elif !defined(SYS_BEOS)
279 FD_SET( STDIN_FILENO, &fds );
280 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
287 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
289 if( buf[size] == '\n' )
296 if( size >= 256 || buf[size] == '\n' )
301 fprintf( stdout, "\nEncoding Quit by user command\n" );
305 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
327 if( input ) free( input );
328 if( output ) free( output );
329 if( format ) free( format );
332 while( ( audio = hb_list_item( audios, 0 ) ) )
334 hb_list_rem( audios, audio );
335 if( audio->out.name )
337 free( audio->out.name );
341 hb_list_close( &audios );
343 if( mixdowns ) free( mixdowns );
344 if( dynamic_range_compression ) free( dynamic_range_compression );
345 if( atracks ) free( atracks );
346 if( arates ) free( arates );
347 if( abitrates ) free( abitrates );
348 if( acodecs ) free( acodecs );
349 if( anames ) free( anames );
350 if (native_language ) free (native_language );
351 if( x264opts ) free (x264opts );
352 if( x264opts2 ) free (x264opts2 );
353 if (preset_name) free (preset_name);
354 if( stop_at_string ) free( stop_at_string );
356 fprintf( stderr, "HandBrake has exited.\n" );
358 #if defined( PTW32_STATIC_LIB )
359 pthread_win32_thread_detach_np();
360 pthread_win32_process_detach_np();
366 static void ShowCommands()
368 fprintf( stdout, "\nCommands:\n" );
369 fprintf( stdout, " [h]elp Show this message\n" );
370 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
371 fprintf( stdout, " [p]ause Pause encoding\n" );
372 fprintf( stdout, " [r]esume Resume encoding\n" );
375 static void PrintTitleInfo( hb_title_t * title )
377 hb_chapter_t * chapter;
378 hb_subtitle_t * subtitle;
381 fprintf( stderr, "+ title %d:\n", title->index );
382 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
383 title->vts, title->ttn, title->cell_start, title->cell_end,
384 title->block_count );
386 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
387 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
388 title->hours, title->minutes, title->seconds );
389 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
390 title->width, title->height,
391 title->pixel_aspect_width,
392 title->pixel_aspect_height,
393 (float) title->aspect,
394 (float) title->rate / title->rate_base );
395 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
396 title->crop[1], title->crop[2], title->crop[3] );
397 fprintf( stderr, " + chapters:\n" );
398 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
400 chapter = hb_list_item( title->list_chapter, i );
401 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
402 "%02d:%02d:%02d\n", chapter->index,
403 chapter->cell_start, chapter->cell_end,
404 chapter->block_count, chapter->hours, chapter->minutes,
407 fprintf( stderr, " + audio tracks:\n" );
408 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
410 audio = hb_list_audio_config_item( title->list_audio, i );
411 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
413 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
415 audio->lang.description,
416 audio->lang.iso639_2,
417 audio->in.samplerate,
422 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
424 audio->lang.description,
425 audio->lang.iso639_2 );
428 fprintf( stderr, " + subtitle tracks:\n" );
429 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
431 subtitle = hb_list_item( title->list_subtitle, i );
432 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
433 i + 1, subtitle->lang,
435 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
438 if(title->detected_interlacing)
440 /* Interlacing was found in half or more of the preview frames */
441 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
446 static int test_sub_list( char ** list, char * needle, int pos )
450 if ( list == NULL || needle == NULL )
453 if ( list[0] == NULL && pos == 1 )
456 for ( i = 0; list[i] != NULL; i++ )
458 if ( strcasecmp( list[i], needle ) == 0 )
464 static int HandleEvents( hb_handle_t * h )
467 int tmp_num_audio_tracks;
469 hb_get_state( h, &s );
476 #define p s.param.scanning
477 case HB_STATE_SCANNING:
478 /* Show what title is currently being scanned */
479 fprintf( stderr, "Scanning title %d", p.title_cur );
481 fprintf( stderr, " of %d", p.title_count );
482 fprintf( stderr, "...\n" );
486 case HB_STATE_SCANDONE:
494 /* Audio argument string parsing variables */
498 int mixdown = HB_AMIXDOWN_DOLBYPLII;
500 /* Audio argument string parsing variables */
502 list = hb_get_titles( h );
504 if( !hb_list_count( list ) )
506 /* No valid title, stop right there */
507 fprintf( stderr, "No title found.\n" );
514 int longest_title_idx=0;
515 int longest_title_pos=-1;
516 int longest_title_time=0;
519 fprintf( stderr, "Searching for longest title...\n" );
521 for( i = 0; i < hb_list_count( list ); i++ )
523 title = hb_list_item( list, i );
524 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
525 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
526 i, title->index, title_time );
527 if( longest_title_time < title_time )
529 longest_title_time = title_time;
530 longest_title_pos = i;
531 longest_title_idx = title->index;
534 if( longest_title_pos == -1 )
536 fprintf( stderr, "No longest title found.\n" );
540 titleindex = longest_title_idx;
541 fprintf( stderr, "Found longest title, setting title to %d\n",
544 title = hb_list_item( list, longest_title_pos);
546 title = hb_list_item( list, 0 );
551 /* Scan-only mode, print infos and exit */
553 for( i = 0; i < hb_list_count( list ); i++ )
555 title = hb_list_item( list, i );
556 PrintTitleInfo( title );
562 /* Set job settings */
565 PrintTitleInfo( title );
567 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
569 job->chapter_start = MAX( job->chapter_start,
571 job->chapter_end = MIN( job->chapter_end,
573 job->chapter_end = MAX( job->chapter_start,
584 fprintf( stderr, "+ Using preset: %s", preset_name);
586 if (!strcmp(preset_name, "Universal"))
592 vcodec = HB_VCODEC_X264;
593 job->vquality = 20.0;
597 atracks = strdup("1,1");
601 acodecs = strdup("faac,ac3");
605 abitrates = strdup("160,160");
609 mixdowns = strdup("dpl2,auto");
613 arates = strdup("48,Auto");
615 if( !dynamic_range_compression )
617 dynamic_range_compression = strdup("0.0,0.0");
622 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:me=umh");
625 job->chapter_markers = 1;
628 if (!strcmp(preset_name, "iPod"))
635 vcodec = HB_VCODEC_X264;
639 atracks = strdup("1");
643 acodecs = strdup("faac");
647 abitrates = strdup("160");
651 mixdowns = strdup("dpl2");
655 arates = strdup("48");
657 if( !dynamic_range_compression )
659 dynamic_range_compression = strdup("0.0");
664 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
666 job->chapter_markers = 1;
669 if (!strcmp(preset_name, "iPhone & iPod Touch"))
675 vcodec = HB_VCODEC_X264;
676 job->vquality = 20.0;
680 atracks = strdup("1");
684 acodecs = strdup("faac");
688 abitrates = strdup("128");
692 mixdowns = strdup("dpl2");
696 arates = strdup("48");
698 if( !dynamic_range_compression )
700 dynamic_range_compression = strdup("0.0");
705 x264opts = strdup("cabac=0:ref=2:mixed-refs:me=umh");
707 job->chapter_markers = 1;
710 if (!strcmp(preset_name, "AppleTV"))
716 job->largeFileSize = 1;
717 vcodec = HB_VCODEC_X264;
718 job->vquality = 20.0;
722 atracks = strdup("1,1");
726 acodecs = strdup("faac,ac3");
730 abitrates = strdup("160,160");
734 mixdowns = strdup("dpl2,auto");
738 arates = strdup("48,Auto");
740 if( !dynamic_range_compression )
742 dynamic_range_compression = strdup("0.0,0.0");
747 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1");
750 job->chapter_markers = 1;
753 if (!strcmp(preset_name, "Normal"))
759 vcodec = HB_VCODEC_X264;
760 job->vquality = 20.0;
764 atracks = strdup("1");
768 acodecs = strdup("faac");
772 abitrates = strdup("160");
776 mixdowns = strdup("dpl2");
780 arates = strdup("48");
782 if( !dynamic_range_compression )
784 dynamic_range_compression = strdup("0.0");
788 x264opts = strdup("ref=2:bframes=2:me=umh");
791 job->chapter_markers = 1;
794 if (!strcmp(preset_name, "High Profile"))
800 vcodec = HB_VCODEC_X264;
801 job->vquality = 20.0;
805 atracks = strdup("1,1");
809 acodecs = strdup("faac,ac3");
813 abitrates = strdup("160,160");
817 mixdowns = strdup("dpl2,auto");
821 arates = strdup("48,Auto");
823 if( !dynamic_range_compression )
825 dynamic_range_compression = strdup("0.0,0.0");
829 x264opts = strdup("ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct");
834 job->chapter_markers = 1;
837 if (!strcmp(preset_name, "Classic"))
843 job->vbitrate = 1000;
846 atracks = strdup("1");
850 acodecs = strdup("faac");
854 abitrates = strdup("160");
858 mixdowns = strdup("dpl2");
862 arates = strdup("48");
864 if( !dynamic_range_compression )
866 dynamic_range_compression = strdup("0.0");
870 if (!strcmp(preset_name, "AppleTV Legacy"))
876 job->largeFileSize = 1;
877 vcodec = HB_VCODEC_X264;
878 job->vbitrate = 2500;
881 atracks = strdup("1,1");
885 acodecs = strdup("faac,ac3");
889 abitrates = strdup("160,160");
893 mixdowns = strdup("dpl2,auto");
897 arates = strdup("48,Auto");
899 if( !dynamic_range_compression )
901 dynamic_range_compression = strdup("0.0,0.0");
905 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0");
908 job->chapter_markers = 1;
911 if (!strcmp(preset_name, "iPhone Legacy"))
918 vcodec = HB_VCODEC_X264;
922 atracks = strdup("1");
926 acodecs = strdup("faac");
930 abitrates = strdup("128");
934 mixdowns = strdup("dpl2");
938 arates = strdup("48");
940 if( !dynamic_range_compression )
942 dynamic_range_compression = strdup("0.0");
947 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0");
949 job->chapter_markers = 1;
952 if (!strcmp(preset_name, "iPod Legacy"))
959 vcodec = HB_VCODEC_X264;
960 job->vbitrate = 1500;
963 atracks = strdup("1");
967 acodecs = strdup("faac");
971 abitrates = strdup("160");
975 mixdowns = strdup("dpl2");
979 arates = strdup("48");
981 if( !dynamic_range_compression )
983 dynamic_range_compression = strdup("0.0");
988 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");
990 job->chapter_markers = 1;
994 if ( chapter_markers )
996 job->chapter_markers = chapter_markers;
998 if( marker_file != NULL )
1000 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1001 hb_csv_cell_t * cell;
1005 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1009 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1013 /* Parse the cells */
1014 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1016 /* We have a chapter number */
1017 if( cell->cell_col == 0 )
1019 row = cell->cell_row;
1020 chapter = atoi( cell->cell_text );
1023 /* We have a chapter name */
1024 if( cell->cell_col == 1 && row == cell->cell_row )
1026 /* If we have a valid chapter, copy the string an terminate it */
1027 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1029 hb_chapter_t * chapter_s;
1031 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1032 strncpy(chapter_s->title, cell->cell_text, 1023);
1033 chapter_s->title[1023] = '\0';
1038 hb_dispose_cell( cell );
1041 hb_close_csv_file( file );
1046 /* No marker file */
1048 int number_of_chapters = hb_list_count(job->title->list_chapter);
1051 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1053 hb_chapter_t * chapter_s;
1054 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1055 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1056 chapter_s->title[1023] = '\0';
1061 if( crop[0] >= 0 && crop[1] >= 0 &&
1062 crop[2] >= 0 && crop[3] >= 0 )
1064 memcpy( job->crop, crop, 4 * sizeof( int ) );
1067 job->deinterlace = deinterlace;
1068 job->grayscale = grayscale;
1070 /* Add selected filters */
1071 job->filters = hb_list_init();
1074 hb_filter_detelecine.settings = detelecine_opt;
1075 hb_list_add( job->filters, &hb_filter_detelecine );
1079 hb_filter_decomb.settings = decomb_opt;
1080 hb_list_add( job->filters, &hb_filter_decomb );
1084 hb_filter_deinterlace.settings = deinterlace_opt;
1085 hb_list_add( job->filters, &hb_filter_deinterlace );
1089 hb_filter_deblock.settings = deblock_opt;
1090 hb_list_add( job->filters, &hb_filter_deblock );
1094 hb_filter_denoise.settings = denoise_opt;
1095 hb_list_add( job->filters, &hb_filter_denoise );
1098 switch( anamorphic_mode )
1100 case 0: // Non-anamorphic
1102 if( width && height )
1105 job->height = height;
1110 hb_fix_aspect( job, HB_KEEP_WIDTH );
1114 job->height = height;
1115 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1117 else if( !width && !height )
1119 hb_fix_aspect( job, HB_KEEP_WIDTH );
1124 case 1: // Strict anammorphic
1125 job->anamorphic.mode = anamorphic_mode;
1128 case 2: // Loose anamorphic
1129 job->anamorphic.mode = 2;
1133 job->anamorphic.modulus = modulus;
1138 job->anamorphic.itu_par = itu_par;
1145 else if( !width && !height )
1147 /* Default to full width when one isn't specified for loose anamorphic */
1148 job->width = title->width - job->crop[2] - job->crop[3];
1153 case 3: // Custom Anamorphic 3: Power User Jamboree
1154 job->anamorphic.mode = 3;
1158 job->anamorphic.modulus = modulus;
1163 job->anamorphic.itu_par = itu_par;
1166 if( par_width && par_height )
1168 job->anamorphic.par_width = par_width;
1169 job->anamorphic.par_height = par_height;
1172 if( keep_display_aspect )
1174 job->anamorphic.keep_display_aspect = 1;
1176 /* First, what *is* the display aspect? */
1177 int cropped_width = title->width - job->crop[2] - job->crop[3];
1178 int cropped_height = title->height - job->crop[0] - job->crop[1];
1180 /* XXX -- I'm assuming people want to keep the source
1181 display AR even though they might have already
1182 asked for ITU values instead. */
1183 float source_display_width = (float)cropped_width *
1184 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1185 float display_aspect = source_display_width / (float)cropped_height;
1186 /* When keeping display aspect, we have to rank some values
1187 by priority in order to consistently handle situations
1188 when more than one might be specified by default.
1190 * First off, PAR gets ignored. (err make this reality)
1191 * Height will be respected over all other settings,
1192 * If it isn't set, display_width will be followed.
1193 * If it isn't set, width will be followed. */
1196 /* We scale the display width to the new height */
1197 display_width = (int)( (double)height * display_aspect );
1199 else if( display_width )
1201 /* We scale the height to the new display width */
1202 height = (int)( (double)display_width / display_aspect );
1208 /* Adjust the PAR to create the new display width
1209 from the default job width. */
1210 job->anamorphic.dar_width = display_width;
1212 job->anamorphic.dar_height = height ?
1214 title->height - job->crop[0] - job->crop[1];
1217 if( width && height )
1219 /* Use these storage dimensions */
1221 job->height = height;
1225 /* Use just this storage width */
1227 job->height = title->height - job->crop[0] - job->crop[1];
1231 /* Use just this storage height. */
1232 job->height = height;
1233 job->width = title->width - job->crop[2] - job->crop[3];
1235 else if( !width && !height )
1237 /* Assume source dimensions after cropping. */
1238 job->width = title->width - job->crop[2] - job->crop[3];
1239 job->height = title->height - job->crop[0] - job->crop[1];
1245 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1247 job->vquality = vquality;
1252 job->vquality = -1.0;
1253 job->vbitrate = vbitrate;
1257 job->vcodec = vcodec;
1261 job->h264_level = 13;
1265 job->h264_level = 30;
1270 job->vrate = 27000000;
1271 job->vrate_base = vrate;
1275 // cfr or pfr flag with no rate specified implies
1276 // use the title rate.
1278 job->vrate = title->rate;
1279 job->vrate_base = title->rate_base;
1282 /* Grab audio tracks */
1285 char * token = strtok(atracks, ",");
1288 int track_start, track_end;
1289 while( token != NULL )
1291 audio = calloc(1, sizeof(*audio));
1292 hb_audio_config_init(audio);
1293 if (strlen(token) >= 3)
1295 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1298 for (i = track_start - 1; i < track_end; i++)
1300 if (i != track_start - 1)
1302 audio = calloc(1, sizeof(*audio));
1303 hb_audio_config_init(audio);
1305 audio->in.track = i;
1306 audio->out.track = num_audio_tracks++;
1307 hb_list_add(audios, audio);
1310 else if( !strcasecmp(token, "none" ) )
1312 audio->in.track = audio->out.track = -1;
1313 audio->out.codec = 0;
1314 hb_list_add(audios, audio);
1319 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1326 audio->in.track = atoi(token) - 1;
1327 audio->out.track = num_audio_tracks++;
1328 hb_list_add(audios, audio);
1330 token = strtok(NULL, ",");
1334 /* Parse audio tracks */
1335 if( native_language && native_dub )
1337 if( hb_list_count( audios ) == 0 || !audio_explicit )
1339 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1344 audio = hb_list_audio_config_item( title->list_audio, i );
1346 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1348 if( strncasecmp( native_language, audio_lang,
1349 sizeof( audio_lang ) ) == 0 &&
1350 audio->lang.type != 3 && // Directors 1
1351 audio->lang.type != 4) // Directors 2
1354 * Matched an audio to our native language - use it.
1355 * Replace any existing audio tracks that a preset may
1358 if( hb_list_count(audios) == 0) {
1359 audio = calloc(1, sizeof(*audio));
1360 hb_audio_config_init(audio);
1361 audio->in.track = track;
1362 audio->out.track = num_audio_tracks++;
1363 /* Add it to our audios */
1364 hb_list_add(audios, audio);
1367 * Update the track numbers on what is already in
1370 for( i=0; i < hb_list_count( audios ); i++ )
1372 audio = hb_list_item( audios, i );
1374 audio->in.track = track;
1381 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1385 if( hb_list_count(audios) == 0 )
1387 /* Create a new audio track with default settings */
1388 audio = calloc(1, sizeof(*audio));
1389 hb_audio_config_init(audio);
1390 /* Add it to our audios */
1391 hb_list_add(audios, audio);
1394 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1395 for (i = 0; i < tmp_num_audio_tracks; i++)
1397 audio = hb_list_item(audios, 0);
1398 if( (audio == NULL) || (audio->in.track == -1) ||
1399 (audio->out.track == -1) || (audio->out.codec == 0) )
1405 if( hb_audio_add( job, audio ) == 0 )
1407 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1408 audio->in.track + 1 );
1413 hb_list_rem(audios, audio);
1415 if( audio->out.name )
1417 free( audio->out.name);
1426 char * token = strtok(acodecs, ",");
1429 while ( token != NULL )
1431 if ((acodec = get_acodec_for_string(token)) == -1)
1433 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1434 acodec = default_acodec;
1436 if( i < num_audio_tracks )
1438 audio = hb_list_audio_config_item(job->list_audio, i);
1439 audio->out.codec = acodec;
1443 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1444 hb_audio_config_t audio;
1448 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1450 hb_audio_config_init(&audio);
1451 audio.in.track = last_audio->in.track;
1452 audio.out.track = num_audio_tracks++;
1453 audio.out.codec = acodec;
1454 hb_audio_add(job, &audio);
1458 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1461 token = strtok(NULL, ",");
1465 if( i < num_audio_tracks )
1467 /* We have fewer inputs than audio tracks, use the default codec for
1468 * this container for the remaining tracks. Unless we only have one input
1469 * then use that codec instead.
1472 acodec = default_acodec;
1473 for ( ; i < num_audio_tracks; i++)
1475 audio = hb_list_audio_config_item(job->list_audio, i);
1476 audio->out.codec = acodec;
1485 char * token = strtok(arates, ",");
1488 while ( token != NULL )
1490 arate = atoi(token);
1491 audio = hb_list_audio_config_item(job->list_audio, i);
1494 for( j = 0; j < hb_audio_rates_count; j++ )
1496 if( !strcmp( token, hb_audio_rates[j].string ) )
1498 arate = hb_audio_rates[j].rate;
1505 if (!is_sample_rate_valid(arate))
1507 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1508 arate = audio->in.samplerate;
1511 audio->out.samplerate = arate;
1512 if( (++i) >= num_audio_tracks )
1513 break; /* We have more inputs than audio tracks, oops */
1517 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1519 token = strtok(NULL, ",");
1522 if (i < num_audio_tracks)
1524 /* We have fewer inputs than audio tracks, use default sample rate.
1525 * Unless we only have one input, then use that for all tracks.
1528 arate = audio->in.samplerate;
1529 for ( ; i < num_audio_tracks; i++)
1531 audio = hb_list_audio_config_item(job->list_audio, i);
1532 audio->out.samplerate = arate;
1541 char * token = strtok(abitrates, ",");
1544 while ( token != NULL )
1546 abitrate = atoi(token);
1547 audio = hb_list_audio_config_item(job->list_audio, i);
1551 audio->out.bitrate = abitrate;
1552 if( (++i) >= num_audio_tracks )
1553 break; /* We have more inputs than audio tracks, oops */
1557 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1559 token = strtok(NULL, ",");
1562 if (i < num_audio_tracks)
1564 /* We have fewer inputs than audio tracks, use the default bitrate
1565 * for the remaining tracks. Unless we only have one input, then use
1566 * that for all tracks.
1569 abitrate = default_abitrate;
1570 for (; i < num_audio_tracks; i++)
1572 audio = hb_list_audio_config_item(job->list_audio, i);
1573 audio->out.bitrate = abitrate;
1580 if ( dynamic_range_compression )
1582 char * token = strtok(dynamic_range_compression, ",");
1584 token = dynamic_range_compression;
1585 while ( token != NULL )
1587 d_r_c = atof(token);
1588 audio = hb_list_audio_config_item(job->list_audio, i);
1591 audio->out.dynamic_range_compression = d_r_c;
1592 if( (++i) >= num_audio_tracks )
1593 break; /* We have more inputs than audio tracks, oops */
1597 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1599 token = strtok(NULL, ",");
1602 if (i < num_audio_tracks)
1604 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1605 * tracks. Unless we only have one input, then use the same DRC for all
1610 for (; i < num_audio_tracks; i++)
1612 audio = hb_list_audio_config_item(job->list_audio, i);
1613 audio->out.dynamic_range_compression = d_r_c;
1622 char * token = strtok(mixdowns, ",");
1625 while ( token != NULL )
1627 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1628 audio = hb_list_audio_config_item(job->list_audio, i);
1631 audio->out.mixdown = mixdown;
1632 if( (++i) >= num_audio_tracks )
1633 break; /* We have more inputs than audio tracks, oops */
1637 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1639 token = strtok(NULL, ",");
1642 if (i < num_audio_tracks)
1644 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1645 * we only have one input, then use that.
1648 mixdown = HB_AMIXDOWN_DOLBYPLII;
1649 for (; i < num_audio_tracks; i++)
1651 audio = hb_list_audio_config_item(job->list_audio, i);
1652 audio->out.mixdown = mixdown;
1657 /* Audio Track Names */
1661 char * token = strtok(anames, ",");
1664 while ( token != NULL )
1666 audio = hb_list_audio_config_item(job->list_audio, i);
1669 audio->out.name = strdup(token);
1670 if( (++i) >= num_audio_tracks )
1671 break; /* We have more names than audio tracks, oops */
1675 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1678 token = strtok(NULL, ",");
1681 if( i < num_audio_tracks && i == 1 )
1683 /* We have exactly one name and more than one audio track. Use the same
1684 * name for all tracks. */
1685 for ( ; i < num_audio_tracks; i++)
1687 audio = hb_list_audio_config_item(job->list_audio, i);
1688 audio->out.name = strdup(anames);
1691 /* Audio Track Names */
1695 job->vbitrate = hb_calc_bitrate( job, size );
1696 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1706 for ( i = 0; subtracks[i] != NULL; i++ )
1709 token = subtracks[i];
1710 if( strcasecmp(token, "scan" ) == 0 )
1712 int burn = 0, force = 0, def = 0;
1714 if ( subburn != NULL )
1716 burn = ( pos == 1 && subburn[0] == 0 ) ||
1717 ( strcmp( "scan", subburn ) == 0 );
1719 if ( subdefault != NULL )
1721 def = ( pos == 1 && subdefault[0] == 0 ) ||
1722 ( strcmp( "scan", subdefault ) == 0 );
1724 force = test_sub_list( subforce, "scan", pos );
1726 if ( !burn && mux == HB_MUX_MKV )
1728 job->select_subtitle_config.dest = PASSTHRUSUB;
1738 if ( !( !burn && mux == HB_MUX_MP4 ) )
1740 job->select_subtitle_config.force = force;
1741 job->select_subtitle_config.default_track = def;
1744 fprintf( stderr, "Warning: Subtitle Scan for MP4 requires the '--subtitle-burn' option to be selected\n");
1749 hb_subtitle_t * subtitle;
1750 hb_subtitle_config_t sub_config;
1752 int burn = 0, force = 0, def = 0;
1754 track = atoi(token) - 1;
1755 subtitle = hb_list_item(title->list_subtitle, track);
1756 if( subtitle == NULL )
1758 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1761 sub_config = subtitle->config;
1763 if ( subburn != NULL )
1765 burn = ( pos == 1 && subburn[0] == 0 ) ||
1766 ( strcmp( token, subburn ) == 0 );
1768 if ( subdefault != NULL )
1770 def = ( pos == 1 && subdefault[0] == 0 ) ||
1771 ( strcmp( token, subdefault ) == 0 );
1774 force = test_sub_list(subforce, token, pos);
1776 if ( !burn && mux == HB_MUX_MKV &&
1777 subtitle->format == PICTURESUB)
1779 sub_config.dest = PASSTHRUSUB;
1781 else if (!burn && mux == HB_MUX_MP4 &&
1782 subtitle->format == PICTURESUB)
1784 // Skip any non-burned vobsubs when output is mp4
1785 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 );
1788 else if ( burn && subtitle->format == PICTURESUB )
1790 // Only allow one subtitle to be burned into video
1793 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1798 sub_config.force = force;
1799 sub_config.default_track = def;
1800 hb_subtitle_add( job, &sub_config, track );
1809 hb_subtitle_config_t sub_config;
1812 for( i=0; srtfile[i] != NULL; i++ )
1814 char *codeset = "L1";
1820 if( srtcodeset && srtcodeset[i] )
1822 codeset = srtcodeset[i];
1824 if( srtoffset && srtoffset[i] )
1826 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1828 if ( srtlang && srtlang[i] )
1832 sub_config.default_track =
1833 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1834 sub_config.force = 0;
1835 strncpy( sub_config.src_filename, srtfile[i], 128);
1836 strncpy( sub_config.src_codeset, codeset, 40);
1837 sub_config.offset = offset;
1839 hb_srt_add( job, &sub_config, lang);
1843 if( native_language )
1847 audio = hb_list_audio_config_item(job->list_audio, 0);
1851 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1853 if( strncasecmp( native_language, audio_lang,
1854 sizeof( audio_lang ) ) != 0 )
1857 * Audio language is not the same as our native language.
1858 * If we have any subtitles in our native language they
1859 * should be selected here if they haven't already been.
1861 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1862 int matched_track = 0;
1864 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1866 subtitle = hb_list_item( title->list_subtitle, i );
1868 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1871 * Found the first matching subtitle in our
1872 * native language. Is it already selected?
1874 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1876 subtitle2 = hb_list_item( job->list_subtitle, i );
1878 if( subtitle2->track == subtitle->track) {
1887 if( subtitle2 == NULL )
1890 * Not already selected, so select it.
1892 hb_subtitle_config_t sub_config;
1896 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1898 sub_config = subtitle->config;
1900 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1902 sub_config.dest = PASSTHRUSUB;
1905 sub_config.force = 0;
1906 sub_config.default_track = 1;
1907 hb_subtitle_add( job, &sub_config, matched_track);
1924 if ( largeFileSize )
1926 job->largeFileSize = 1;
1930 job->mp4_optimize = 1;
1937 job->file = strdup( output );
1946 job->color_matrix = color_matrix;
1949 if( x264opts != NULL && *x264opts != '\0' )
1951 job->x264opts = x264opts;
1953 else /*avoids a bus error crash when options aren't specified*/
1955 job->x264opts = NULL;
1958 job->maxWidth = maxWidth;
1960 job->maxHeight = maxHeight;
1962 if( start_at_preview )
1964 job->start_at_preview = start_at_preview - 1;
1965 job->seek_points = preview_count;
1970 job->pts_to_stop = stop_at_pts;
1976 job->frame_to_stop = stop_at_frame;
1985 * When subtitle scan is enabled do a fast pre-scan job
1986 * which will determine which subtitles to enable, if any.
1990 x264opts_tmp = job->x264opts;
1992 job->x264opts = NULL;
1994 job->indepth_scan = subtitle_scan;
1995 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1996 "subtitles if found for foreign language segments\n");
1999 * Add the pre-scan job
2003 job->x264opts = x264opts_tmp;
2009 * If subtitle_scan is enabled then only turn it on
2010 * for the first pass and then off again for the
2015 job->indepth_scan = 0;
2019 x264opts2 = strdup(x264opts);
2023 * If turbo options have been selected then append them
2024 * to the x264opts now (size includes one ':' and the '\0')
2026 if( turbo_opts_enabled )
2028 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2031 tmp_x264opts = malloc(size * sizeof(char));
2034 snprintf( tmp_x264opts, size, "%s:%s",
2035 x264opts, turbo_opts );
2039 * No x264opts to modify, but apply the turbo options
2040 * anyway as they may be modifying defaults
2042 snprintf( tmp_x264opts, size, "%s",
2045 x264opts = tmp_x264opts;
2047 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2050 job->x264opts = x264opts;
2056 * On the second pass we turn off subtitle scan so that we
2057 * can actually encode using any subtitles that were auto
2058 * selected in the first pass (using the whacky select-subtitle
2059 * attribute of the job).
2061 job->indepth_scan = 0;
2063 job->x264opts = x264opts2;
2070 * Turn on subtitle scan if requested, note that this option
2071 * precludes encoding of any actual subtitles.
2074 job->indepth_scan = 0;
2082 #define p s.param.working
2083 case HB_STATE_WORKING:
2084 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2085 p.job_cur, p.job_count, 100.0 * p.progress );
2086 if( p.seconds > -1 )
2088 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2089 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2090 p.hours, p.minutes, p.seconds );
2096 #define p s.param.muxing
2097 case HB_STATE_MUXING:
2099 if (show_mux_warning)
2101 fprintf( stdout, "\rMuxing: this may take awhile..." );
2103 show_mux_warning = 0;
2109 #define p s.param.workdone
2110 case HB_STATE_WORKDONE:
2111 /* Print error if any, then exit */
2115 fprintf( stderr, "\nRip done!\n" );
2117 case HB_ERROR_CANCELED:
2118 fprintf( stderr, "\nRip canceled.\n" );
2121 fprintf( stderr, "\nRip failed (error %x).\n",
2131 /****************************************************************************
2133 ****************************************************************************/
2134 static volatile int64_t i_die_date = 0;
2135 void SigHandler( int i_signal )
2140 i_die_date = hb_get_date();
2141 fprintf( stderr, "Signal %d received, terminating - do it "
2142 "again in case it gets stuck\n", i_signal );
2144 else if( i_die_date + 500 < hb_get_date() )
2146 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2151 /****************************************************************************
2153 ****************************************************************************/
2154 static void ShowHelp()
2157 FILE* const out = stdout;
2160 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2162 "### General Handbrake Options------------------------------------------------\n\n"
2163 " -h, --help Print help\n"
2164 " -u, --update Check for updates and exit\n"
2165 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2166 " -C, --cpu Set CPU count (default: autodetected)\n"
2167 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2168 " if the preset name has spaces, surround it with\n"
2169 " double quotation marks\n"
2170 " -z, --preset-list See a list of available built-in presets\n"
2171 " --dvdnav Use dvdnav (Experimental)\n"
2174 "### Source Options-----------------------------------------------------------\n\n"
2175 " -i, --input <string> Set input device\n"
2176 " -t, --title <number> Select a title to encode (0 to scan only,\n"
2178 " -L, --longest Select the longest title\n"
2179 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2180 " 1 to 3, or \"3\" for chapter 3 only,\n"
2181 " default: all chapters)\n"
2182 " --angle <number> Select the DVD angle\n"
2183 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2184 " and whether or not they're stored to disk (0 or 1).\n"
2185 " (default: 10:0)\n"
2186 " --start-at-preview <#> Start encoding at a given preview.\n"
2187 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2188 " or pts (on a 90kHz clock)"
2191 "### Destination Options------------------------------------------------------\n\n"
2192 " -o, --output <string> Set output file name\n"
2193 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
2194 " autodetected from file name)\n"
2195 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2196 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2197 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2198 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2199 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2203 "### Video Options------------------------------------------------------------\n\n"
2204 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2205 " (default: ffmpeg)\n"
2206 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2207 " same style as mencoder:\n"
2208 " option1=value1:option2=value2\n"
2209 " -q, --quality <float> Set video quality (0.0..1.0)\n"
2210 " -Q, --cqp Use with -q for CQP instead of CRF\n"
2211 " -S, --size <MB> Set target size\n"
2212 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2213 " -2, --two-pass Use two-pass mode\n"
2214 " -T, --turbo When using 2-pass use the turbo options\n"
2215 " on the first pass to improve speed\n"
2216 " (only works with x264, affects PSNR by about 0.05dB,\n"
2217 " and increases first pass speed two to four times)\n"
2218 " -r, --rate Set video framerate (" );
2219 for( i = 0; i < hb_video_rates_count; i++ )
2221 fprintf( out, hb_video_rates[i].string );
2222 if( i != hb_video_rates_count - 1 )
2223 fprintf( out, "/" );
2226 " Be aware that not specifying a framerate lets\n"
2227 " HandBrake preserve a source's time stamps,\n"
2228 " potentially creating variable framerate video\n"
2229 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2230 " frame rate control. VFR preserves the source\n"
2231 " timing. CFR makes the output constant rate at\n"
2232 " the rate given by the -r flag (or the source's\n"
2233 " average rate if no -r is given). PFR doesn't\n"
2234 " allow the rate to go over the rate specified\n"
2235 " with the -r flag but won't change the source\n"
2236 " timing if it's below that rate.\n"
2237 " If none of these flags are given, the default\n"
2238 " is --cfr when -r is given and --vfr otherwise\n"
2241 "### Audio Options-----------------------------------------------------------\n\n"
2242 " -a, --audio <string> Select audio track(s), separated by commas\n"
2243 " More than one output track can be used for one\n"
2245 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2246 " tracks, default: first one)\n" );
2250 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2251 " ac3 and dts meaning passthrough\n"
2252 " Separated by commas for more than one audio track.\n"
2253 " (default: guessed)\n" );
2256 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2257 " ac3 and dts meaning passthrough\n"
2258 " Separated by commas for more than one audio track.\n"
2259 " (default: guessed)\n" );
2262 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2263 " Separated by commas for more than one audio track.\n"
2264 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2265 " Separated by commas for more than one audio track.\n"
2266 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2267 " -R, --arate Set audio samplerate(s) (" );
2268 for( i = 0; i < hb_audio_rates_count; i++ )
2270 fprintf( out, hb_audio_rates[i].string );
2271 if( i != hb_audio_rates_count - 1 )
2272 fprintf( out, "/" );
2274 fprintf( out, " kHz)\n"
2275 " Separated by commas for more than one audio track.\n"
2276 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2277 " making soft sounds louder. Range is 1.0 to 4.0\n"
2278 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2279 " Separated by commas for more than one audio track.\n"
2280 " -A, --aname <string> Audio track name(s),\n"
2281 " Separated by commas for more than one audio track.\n"
2284 "### Picture Settings---------------------------------------------------------\n\n"
2285 " -w, --width <number> Set picture width\n"
2286 " -l, --height <number> Set picture height\n"
2287 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2288 " -Y, --maxHeight <#> Set maximum height\n"
2289 " -X, --maxWidth <#> Set maximum width\n"
2290 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2291 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2292 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2293 " directly control all parameters.\n"
2294 " --display-width Set the width to scale the actual pixels to\n"
2295 " <number> at playback, for custom anamorphic.\n"
2296 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2297 " when using custom anamorphic\n"
2298 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2300 " (--display-width and --pixel-aspect are mutually\n"
2301 " exclusive and the former will override the latter)\n"
2302 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2303 " custom anamorphic, useful with underscanned sources\n"
2304 " --modulus Set the number you want the scaled pixel dimensions\n"
2305 " <number> to divide cleanly by, for loose and custom\n"
2306 " anamorphic modes (default: 16)\n"
2307 " -M --color-matrix Set the color space signaled by the output\n"
2308 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2309 " default: set by resolution)\n"
2312 "### Filters---------------------------------------------------------\n\n"
2314 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2315 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2317 " <fast/slow/slower>\n"
2318 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2319 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2320 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2321 " Note: this filter drops duplicate frames to\n"
2322 " restore the pre-telecine framerate, unless you\n"
2323 " specify a constant framerate (--rate 29.97)\n"
2324 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2325 " -8, --denoise Denoise video with hqdn3d filter\n"
2326 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2328 " <weak/medium/strong>\n"
2329 " -7, --deblock Deblock video with pp7 filter\n"
2330 " <QP:M> (default 5:2)\n"
2331 " -g, --grayscale Grayscale encoding\n"
2334 "### Subtitle Options------------------------------------------------------------\n\n"
2335 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2336 " More than one output track can be used for one\n"
2338 " (\"1,2,3\" for multiple tracks.\n"
2339 " A special track name \"scan\" adds an extra 1st pass.\n"
2340 " This extra pass scans subtitles matching the\n"
2341 " language of the first audio or the language \n"
2342 " selected by --native-language.\n"
2343 " The one that's only used 10 percent of the time\n"
2344 " or less is selected. This should locate subtitles\n"
2345 " for short foreign language segments. Best used in\n"
2346 " conjunction with --subtitle-forced.\n"
2347 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2348 " <string> the subtitle has the forced flag set. May be used in\n"
2349 " conjunction with \"scan\" track to auto-select\n"
2350 " a stream if it contains forced subtitles.\n"
2351 " Separated by commas for more than one audio track.\n"
2352 " (\"1,2,3\" for multiple tracks.\n"
2353 " If \"string\" is omitted, the first trac is forced.\n"
2354 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2355 " <number> If \"number\" is omitted, the first trac is burned.\n"
2356 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2357 " <number> to be displayed upon playback. Setting no default\n"
2358 " means no subtitle will be automatically displayed\n"
2359 " If \"number\" is omitted, the first trac is default.\n"
2360 " -N, --native-language Specifiy the your language preference. When the first\n"
2361 " <string> audio track does not match your native language then\n"
2362 " select the first subtitle that does. When used in\n"
2363 " conjunction with --native-dub the audio track is\n"
2364 " changed in preference to subtitles. Provide the\n"
2365 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2366 " --native-dub Used in conjunction with --native-language\n"
2367 " requests that if no audio tracks are selected the\n"
2368 " default selected audio track will be the first one\n"
2369 " that matches the --native-language. If there are no\n"
2370 " matching audio tracks then the first matching\n"
2371 " subtitle track is used instead.\n"
2372 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2373 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2374 " <string> encoded in, separted by commas.\n"
2375 " Use 'iconv -l' for a list of valid\n"
2376 " codesets. If not specified latin1 is assumed\n"
2377 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2378 " <string> separted by commas. If not specified zero is assumed.\n"
2379 " Offsets may be negative.\n"
2380 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2381 " for the SRT file(s) separated by commas. If not specified\n"
2382 " then 'und' is used.\n"
2383 " --srt-default Flag the selected srt as the default subtitle\n"
2384 " <number> to be displayed upon playback. Setting no default\n"
2385 " means no subtitle will be automatically displayed\n"
2386 " If \"number\" is omitted, the first srt is default.\n"
2387 " \"number\" is an 1 based index into the srt-file list\n"
2394 /****************************************************************************
2396 ****************************************************************************/
2397 static void ShowPresets()
2399 printf("\n< Apple\n");
2401 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:mixed-refs=1:me=umh\n");
2403 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\n");
2405 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:mixed-refs:me=umh\n");
2407 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:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1\n");
2411 printf("\n< Regular\n");
2413 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:me=umh\n");
2415 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 ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct\n");
2419 printf("\n< Legacy\n");
2421 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2423 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 bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0\n");
2425 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\n");
2427 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\n");
2432 static char * hb_strndup( char * str, int len )
2435 int str_len = strlen( str );
2437 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2438 strncpy( res, str, len );
2443 static char** str_split( char *str, char delem )
2450 if ( str == NULL || str[0] == 0 )
2452 ret = malloc( sizeof(char*) );
2457 // Find number of elements in the string
2460 while ( ( pos = strchr( pos, delem ) ) != NULL )
2466 ret = calloc( ( count + 1 ), sizeof(char*) );
2469 for ( i = 0; i < count - 1; i++ )
2471 end = strchr( pos, delem );
2472 ret[i] = hb_strndup(pos, end - pos);
2475 ret[i] = strdup(pos);
2480 /****************************************************************************
2482 ****************************************************************************/
2483 static int ParseOptions( int argc, char ** argv )
2486 #define PREVIEWS 257
2487 #define START_AT_PREVIEW 258
2491 #define DISPLAY_WIDTH 262
2492 #define PIXEL_ASPECT 263
2494 #define KEEP_DISPLAY_ASPECT 265
2495 #define SUB_BURNED 266
2496 #define SUB_DEFAULT 267
2497 #define NATIVE_DUB 268
2498 #define SRT_FILE 269
2499 #define SRT_CODESET 270
2500 #define SRT_OFFSET 271
2501 #define SRT_LANG 272
2502 #define SRT_DEFAULT 273
2506 static struct option long_options[] =
2508 { "help", no_argument, NULL, 'h' },
2509 { "update", no_argument, NULL, 'u' },
2510 { "verbose", optional_argument, NULL, 'v' },
2511 { "cpu", required_argument, NULL, 'C' },
2512 { "dvdnav", no_argument, NULL, DVDNAV },
2514 { "format", required_argument, NULL, 'f' },
2515 { "input", required_argument, NULL, 'i' },
2516 { "output", required_argument, NULL, 'o' },
2517 { "large-file", no_argument, NULL, '4' },
2518 { "optimize", no_argument, NULL, 'O' },
2519 { "ipod-atom", no_argument, NULL, 'I' },
2521 { "title", required_argument, NULL, 't' },
2522 { "longest", no_argument, NULL, 'L' },
2523 { "chapters", required_argument, NULL, 'c' },
2524 { "angle", required_argument, NULL, ANGLE },
2525 { "markers", optional_argument, NULL, 'm' },
2526 { "audio", required_argument, NULL, 'a' },
2527 { "mixdown", required_argument, NULL, '6' },
2528 { "drc", required_argument, NULL, 'D' },
2529 { "subtitle", required_argument, NULL, 's' },
2530 { "subtitle-forced", optional_argument, NULL, 'F' },
2531 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2532 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2533 { "srt-file", required_argument, NULL, SRT_FILE },
2534 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2535 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2536 { "srt-lang", required_argument, NULL, SRT_LANG },
2537 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2538 { "native-language", required_argument, NULL,'N' },
2539 { "native-dub", no_argument, NULL, NATIVE_DUB },
2540 { "encoder", required_argument, NULL, 'e' },
2541 { "aencoder", required_argument, NULL, 'E' },
2542 { "two-pass", no_argument, NULL, '2' },
2543 { "deinterlace", optional_argument, NULL, 'd' },
2544 { "deblock", optional_argument, NULL, '7' },
2545 { "denoise", optional_argument, NULL, '8' },
2546 { "detelecine", optional_argument, NULL, '9' },
2547 { "decomb", optional_argument, NULL, '5' },
2548 { "grayscale", no_argument, NULL, 'g' },
2549 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2550 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2551 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2552 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2553 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2554 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2555 { "modulus", required_argument, NULL, MODULUS },
2556 { "itu-par", no_argument, &itu_par, 1 },
2557 { "width", required_argument, NULL, 'w' },
2558 { "height", required_argument, NULL, 'l' },
2559 { "crop", required_argument, NULL, 'n' },
2561 { "vb", required_argument, NULL, 'b' },
2562 { "quality", required_argument, NULL, 'q' },
2563 { "size", required_argument, NULL, 'S' },
2564 { "ab", required_argument, NULL, 'B' },
2565 { "rate", required_argument, NULL, 'r' },
2566 { "arate", required_argument, NULL, 'R' },
2567 { "cqp", no_argument, NULL, 'Q' },
2568 { "x264opts", required_argument, NULL, 'x' },
2569 { "turbo", no_argument, NULL, 'T' },
2570 { "maxHeight", required_argument, NULL, 'Y' },
2571 { "maxWidth", required_argument, NULL, 'X' },
2572 { "preset", required_argument, NULL, 'Z' },
2573 { "preset-list", no_argument, NULL, 'z' },
2575 { "aname", required_argument, NULL, 'A' },
2576 { "color-matrix",required_argument, NULL, 'M' },
2577 { "previews", required_argument, NULL, PREVIEWS },
2578 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2579 { "stop-at", required_argument, NULL, STOP_AT },
2580 { "vfr", no_argument, &cfr, 0 },
2581 { "cfr", no_argument, &cfr, 1 },
2582 { "pfr", no_argument, &cfr, 2 },
2586 int option_index = 0;
2589 c = getopt_long( argc, argv,
2590 "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:Qx:TY:X:Z:z",
2591 long_options, &option_index );
2600 /* option was handled entirely in getopt_long */
2609 if( optarg != NULL )
2611 debug = atoi( optarg );
2619 cpu = atoi( optarg );
2624 preset_name = strdup(optarg);
2634 format = strdup( optarg );
2637 input = strdup( optarg );
2639 char *devName = bsd_name_for_path( input ); // alloc
2642 if( device_is_dvd( devName ))
2645 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2646 sprintf( input, "/dev/%s", devName );
2653 output = strdup( optarg );
2666 titleindex = atoi( optarg );
2674 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2676 chapter_start = start;
2679 else if( sscanf( optarg, "%d", &start ) == 1 )
2681 chapter_start = start;
2682 chapter_end = chapter_start;
2686 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2693 angle = atoi( optarg );
2696 if( optarg != NULL )
2698 marker_file = strdup( optarg );
2700 chapter_markers = 1;
2703 if( optarg != NULL )
2705 atracks = strdup( optarg );
2714 if( optarg != NULL )
2716 mixdowns = strdup( optarg );
2720 if( optarg != NULL )
2722 dynamic_range_compression = strdup( optarg );
2726 subtracks = str_split( optarg, ',' );
2729 subforce = str_split( optarg, ',' );
2732 if( optarg != NULL )
2734 subburn = strdup( optarg );
2742 if( optarg != NULL )
2744 subdefault = strdup( optarg );
2752 native_language = strdup( optarg );
2758 srtfile = str_split( optarg, ',' );
2761 srtcodeset = str_split( optarg, ',' );
2764 srtoffset = str_split( optarg, ',' );
2767 srtlang = str_split( optarg, ',' );
2770 if( optarg != NULL )
2772 srtdefault = atoi( optarg );
2783 if( optarg != NULL )
2785 if (!( strcmp( optarg, "fast" ) ))
2787 deinterlace_opt = "-1";
2789 else if (!( strcmp( optarg, "slow" ) ))
2791 deinterlace_opt = "2";
2793 else if (!( strcmp( optarg, "slower" ) ))
2795 deinterlace_opt = "0";
2799 deinterlace_opt = strdup( optarg );
2805 if( optarg != NULL )
2807 deblock_opt = strdup( optarg );
2812 if( optarg != NULL )
2814 if (!( strcmp( optarg, "weak" ) ))
2816 denoise_opt = "2:1:2:3";
2818 else if (!( strcmp( optarg, "medium" ) ))
2820 denoise_opt = "3:2:2:3";
2822 else if (!( strcmp( optarg, "strong" ) ))
2824 denoise_opt = "7:7:5:5";
2828 denoise_opt = strdup( optarg );
2834 if( optarg != NULL )
2836 detelecine_opt = strdup( optarg );
2841 if( optarg != NULL )
2843 decomb_opt = strdup( optarg );
2851 if( optarg != NULL )
2853 sscanf( optarg, "%i", &display_width );
2857 if( optarg != NULL )
2859 sscanf( optarg, "%i:%i", &par_width, &par_height );
2863 if( optarg != NULL )
2865 sscanf( optarg, "%i", &modulus );
2869 if( !strcasecmp( optarg, "ffmpeg" ) )
2871 vcodec = HB_VCODEC_FFMPEG;
2873 else if( !strcasecmp( optarg, "x264" ) )
2875 vcodec = HB_VCODEC_X264;
2877 else if( !strcasecmp( optarg, "x264b13" ) )
2879 vcodec = HB_VCODEC_X264;
2882 else if( !strcasecmp( optarg, "x264b30" ) )
2884 vcodec = HB_VCODEC_X264;
2887 else if( !strcasecmp( optarg, "theora" ) )
2889 vcodec = HB_VCODEC_THEORA;
2893 fprintf( stderr, "invalid codec (%s)\n", optarg );
2898 if( optarg != NULL )
2900 acodecs = strdup( optarg );
2904 width = atoi( optarg );
2907 height = atoi( optarg );
2912 char * tmp = optarg;
2913 for( i = 0; i < 4; i++ )
2917 crop[i] = strtol( tmp, &tmp, 0 );
2926 for( i = 0; i < hb_video_rates_count; i++ )
2928 if( !strcmp( optarg, hb_video_rates[i].string ) )
2930 vrate = hb_video_rates[i].rate;
2936 fprintf( stderr, "invalid framerate %s\n", optarg );
2938 else if ( cfr == 0 )
2945 if( optarg != NULL )
2947 arates = strdup( optarg );
2951 vbitrate = atoi( optarg );
2954 vquality = atof( optarg );
2957 size = atoi( optarg );
2960 if( optarg != NULL )
2962 abitrates = strdup( optarg );
2969 x264opts = strdup( optarg );
2972 turbo_opts_enabled = 1;
2975 maxHeight = atoi( optarg );
2978 maxWidth = atoi (optarg );
2981 if( optarg != NULL )
2983 anames = strdup( optarg );
2987 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2989 case START_AT_PREVIEW:
2990 start_at_preview = atoi( optarg );
2993 stop_at_string = strdup( optarg );
2994 stop_at_token = strtok( stop_at_string, ":");
2995 if( !strcmp( stop_at_token, "frame" ) )
2997 stop_at_token = strtok( NULL, ":");
2998 stop_at_frame = atoi(stop_at_token);
3000 else if( !strcmp( stop_at_token, "pts" ) )
3002 stop_at_token = strtok( NULL, ":");
3003 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3005 else if( !strcmp( stop_at_token, "duration" ) )
3007 stop_at_token = strtok( NULL, ":");
3008 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3009 stop_at_pts *= 90000LL;
3013 if( atoi( optarg ) == 601 )
3015 else if( atoi( optarg ) == 709 )
3019 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
3027 static int CheckOptions( int argc, char ** argv )
3034 if( input == NULL || *input == '\0' )
3036 fprintf( stderr, "Missing input device. Run %s --help for "
3037 "syntax.\n", argv[0] );
3042 if( titleindex > 0 )
3044 if( output == NULL || *output == '\0' )
3046 fprintf( stderr, "Missing output file name. Run %s --help "
3047 "for syntax.\n", argv[0] );
3053 char * p = strrchr( output, '.' );
3056 if( p && !strcasecmp( p, ".avi" ) )
3059 default_acodec = HB_ACODEC_LAME;
3061 else if( p && ( !strcasecmp( p, ".mp4" ) ||
3062 !strcasecmp( p, ".m4v" ) ) )
3068 default_acodec = HB_ACODEC_FAAC;
3070 else if( p && ( !strcasecmp( p, ".ogm" ) ||
3071 !strcasecmp( p, ".ogg" ) ) )
3074 default_acodec = HB_ACODEC_VORBIS;
3076 else if( p && !strcasecmp(p, ".mkv" ) )
3079 default_acodec = HB_ACODEC_AC3;
3083 fprintf( stderr, "Output format couldn't be guessed "
3084 "from file name, using default.\n" );
3088 else if( !strcasecmp( format, "avi" ) )
3091 default_acodec = HB_ACODEC_LAME;
3093 else if( !strcasecmp( format, "mp4" ) ||
3094 !strcasecmp( format, "m4v" ) )
3100 default_acodec = HB_ACODEC_FAAC;
3102 else if( !strcasecmp( format, "ogm" ) ||
3103 !strcasecmp( format, "ogg" ) )
3106 default_acodec = HB_ACODEC_VORBIS;
3108 else if( !strcasecmp( format, "mkv" ) )
3111 default_acodec = HB_ACODEC_AC3;
3115 fprintf( stderr, "Invalid output format (%s). Possible "
3116 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
3124 static int get_acodec_for_string( char *codec )
3126 if( !strcasecmp( codec, "ac3" ) )
3128 return HB_ACODEC_AC3;
3130 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3132 return HB_ACODEC_DCA;
3134 else if( !strcasecmp( codec, "lame" ) )
3136 return HB_ACODEC_LAME;
3138 else if( !strcasecmp( codec, "faac" ) )
3140 return HB_ACODEC_FAAC;
3142 else if( !strcasecmp( codec, "vorbis") )
3144 return HB_ACODEC_VORBIS;
3147 else if( !strcasecmp( codec, "ca_aac") )
3149 return HB_ACODEC_CA_AAC;
3158 static int is_sample_rate_valid(int rate)
3161 for( i = 0; i < hb_audio_rates_count; i++ )
3163 if (rate == hb_audio_rates[i].rate)
3170 /****************************************************************************
3173 * Returns the BSD device name for the block device that contains the
3174 * passed-in path. Returns NULL on failure.
3175 ****************************************************************************/
3176 static char* bsd_name_for_path(char *path)
3180 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3186 // Get the volume reference number.
3187 FSCatalogInfo catalogInfo;
3188 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3194 FSVolumeRefNum volRefNum = catalogInfo.volume;
3196 // Now let's get the device name
3197 GetVolParmsInfoBuffer volumeParms;
3198 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3204 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3205 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3206 if( volumeParms.vMVersion < 4 )
3211 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3212 if( !volumeParms.vMDeviceID )
3217 return strdup( volumeParms.vMDeviceID );
3220 /****************************************************************************
3223 * Returns whether or not the passed in BSD device represents a DVD, or other
3225 ****************************************************************************/
3226 static int device_is_dvd(char *device)
3228 io_service_t service = get_iokit_service(device);
3229 if( service == IO_OBJECT_NULL )
3233 int result = is_dvd_service(service);
3234 IOObjectRelease(service);
3238 /****************************************************************************
3241 * Returns the IOKit service object for the passed in BSD device name.
3242 ****************************************************************************/
3243 static io_service_t get_iokit_service( char *device )
3245 CFMutableDictionaryRef matchingDict;
3246 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3247 if( matchingDict == NULL )
3249 return IO_OBJECT_NULL;
3251 // Fetch the object with the matching BSD node name. There should only be
3252 // one match, so IOServiceGetMatchingService is used instead of
3253 // IOServiceGetMatchingServices to simplify the code.
3254 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3257 /****************************************************************************
3260 * Returns whether or not the service passed in is a DVD.
3262 * Searches for an IOMedia object that represents the entire (whole) media that
3263 * the volume is on. If the volume is on partitioned media, the whole media
3264 * object will be a parent of the volume's media object. If the media is not
3265 * partitioned, the volume's media object will be the whole media object.
3266 ****************************************************************************/
3267 static int is_dvd_service( io_service_t service )
3269 kern_return_t kernResult;
3272 // Create an iterator across all parents of the service object passed in.
3273 kernResult = IORegistryEntryCreateIterator( service,
3275 kIORegistryIterateRecursively | kIORegistryIterateParents,
3277 if( kernResult != KERN_SUCCESS )
3281 if( iter == IO_OBJECT_NULL )
3286 // A reference on the initial service object is released in the do-while
3287 // loop below, so add a reference to balance.
3288 IOObjectRetain( service );
3293 if( is_whole_media_service( service ) &&
3294 IOObjectConformsTo( service, kIODVDMediaClass) )
3298 IOObjectRelease( service );
3299 } while( !result && (service = IOIteratorNext( iter )) );
3300 IOObjectRelease( iter );
3305 /****************************************************************************
3306 * is_whole_media_service
3308 * Returns whether or not the service passed in is an IOMedia service and
3309 * represents the "whole" media instead of just a partition.
3311 * The whole media object is indicated in the IORegistry by the presence of a
3312 * property with the key "Whole" and value "Yes".
3313 ****************************************************************************/
3314 static int is_whole_media_service( io_service_t service )
3318 if( IOObjectConformsTo( service, kIOMediaClass ) )
3320 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3321 CFSTR( kIOMediaWholeKey ),
3322 kCFAllocatorDefault,
3328 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3329 CFRelease( wholeMedia );
3334 #endif // __APPLE_CC__