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 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
382 title->vts, title->ttn, title->cell_start, title->cell_end,
383 title->block_count );
385 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
386 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
387 title->hours, title->minutes, title->seconds );
388 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
389 title->width, title->height,
390 title->pixel_aspect_width,
391 title->pixel_aspect_height,
392 (float) title->aspect,
393 (float) title->rate / title->rate_base );
394 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
395 title->crop[1], title->crop[2], title->crop[3] );
396 fprintf( stderr, " + chapters:\n" );
397 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
399 chapter = hb_list_item( title->list_chapter, i );
400 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
401 "%02d:%02d:%02d\n", chapter->index,
402 chapter->cell_start, chapter->cell_end,
403 chapter->block_count, chapter->hours, chapter->minutes,
406 fprintf( stderr, " + audio tracks:\n" );
407 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
409 audio = hb_list_audio_config_item( title->list_audio, i );
410 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
412 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
414 audio->lang.description,
415 audio->lang.iso639_2,
416 audio->in.samplerate,
421 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
423 audio->lang.description,
424 audio->lang.iso639_2 );
427 fprintf( stderr, " + subtitle tracks:\n" );
428 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
430 subtitle = hb_list_item( title->list_subtitle, i );
431 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
432 i + 1, subtitle->lang,
434 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
437 if(title->detected_interlacing)
439 /* Interlacing was found in half or more of the preview frames */
440 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
445 static int test_sub_list( char ** list, char * needle, int pos )
449 if ( list == NULL || needle == NULL )
452 if ( list[0] == NULL && pos == 1 )
455 for ( i = 0; list[i] != NULL; i++ )
457 if ( strcasecmp( list[i], needle ) == 0 )
463 static int HandleEvents( hb_handle_t * h )
466 int tmp_num_audio_tracks;
468 hb_get_state( h, &s );
475 #define p s.param.scanning
476 case HB_STATE_SCANNING:
477 /* Show what title is currently being scanned */
478 fprintf( stderr, "Scanning title %d", p.title_cur );
480 fprintf( stderr, " of %d", p.title_count );
481 fprintf( stderr, "...\n" );
485 case HB_STATE_SCANDONE:
493 /* Audio argument string parsing variables */
497 int mixdown = HB_AMIXDOWN_DOLBYPLII;
499 /* Audio argument string parsing variables */
501 list = hb_get_titles( h );
503 if( !hb_list_count( list ) )
505 /* No valid title, stop right there */
506 fprintf( stderr, "No title found.\n" );
513 int longest_title_idx=0;
514 int longest_title_pos=-1;
515 int longest_title_time=0;
518 fprintf( stderr, "Searching for longest title...\n" );
520 for( i = 0; i < hb_list_count( list ); i++ )
522 title = hb_list_item( list, i );
523 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
524 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
525 i, title->index, title_time );
526 if( longest_title_time < title_time )
528 longest_title_time = title_time;
529 longest_title_pos = i;
530 longest_title_idx = title->index;
533 if( longest_title_pos == -1 )
535 fprintf( stderr, "No longest title found.\n" );
539 titleindex = longest_title_idx;
540 fprintf( stderr, "Found longest title, setting title to %d\n",
543 title = hb_list_item( list, longest_title_pos);
545 title = hb_list_item( list, 0 );
550 /* Scan-only mode, print infos and exit */
552 for( i = 0; i < hb_list_count( list ); i++ )
554 title = hb_list_item( list, i );
555 PrintTitleInfo( title );
561 /* Set job settings */
564 PrintTitleInfo( title );
566 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
568 job->chapter_start = MAX( job->chapter_start,
570 job->chapter_end = MIN( job->chapter_end,
572 job->chapter_end = MAX( job->chapter_start,
583 fprintf( stderr, "+ Using preset: %s", preset_name);
585 if (!strcmp(preset_name, "Universal"))
591 vcodec = HB_VCODEC_X264;
592 job->vquality = 20.0;
595 atracks = strdup("1,1");
599 acodecs = strdup("faac,ac3");
603 abitrates = strdup("160,160");
607 mixdowns = strdup("dpl2,auto");
611 arates = strdup("48,Auto");
613 if( !dynamic_range_compression )
615 dynamic_range_compression = strdup("0.0,0.0");
620 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6");
623 job->chapter_markers = 1;
626 if (!strcmp(preset_name, "iPod"))
633 vcodec = HB_VCODEC_X264;
637 atracks = strdup("1");
641 acodecs = strdup("faac");
645 abitrates = strdup("160");
649 mixdowns = strdup("dpl2");
653 arates = strdup("48");
655 if( !dynamic_range_compression )
657 dynamic_range_compression = strdup("0.0");
662 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");
664 job->chapter_markers = 1;
667 if (!strcmp(preset_name, "iPhone & iPod Touch"))
673 vcodec = HB_VCODEC_X264;
674 job->vquality = 20.0;
677 atracks = strdup("1");
681 acodecs = strdup("faac");
685 abitrates = strdup("128");
689 mixdowns = strdup("dpl2");
693 arates = strdup("48");
695 if( !dynamic_range_compression )
697 dynamic_range_compression = strdup("0.0");
702 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0");
704 job->chapter_markers = 1;
707 if (!strcmp(preset_name, "AppleTV"))
713 job->largeFileSize = 1;
714 vcodec = HB_VCODEC_X264;
715 job->vquality = 20.0;
718 atracks = strdup("1,1");
722 acodecs = strdup("faac,ac3");
726 abitrates = strdup("160,160");
730 mixdowns = strdup("dpl2,auto");
734 arates = strdup("48,Auto");
736 if( !dynamic_range_compression )
738 dynamic_range_compression = strdup("0.0,0.0");
743 x264opts = strdup("cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0");
746 job->chapter_markers = 1;
749 if (!strcmp(preset_name, "Normal"))
755 vcodec = HB_VCODEC_X264;
756 job->vquality = 20.0;
759 atracks = strdup("1");
763 acodecs = strdup("faac");
767 abitrates = strdup("160");
771 mixdowns = strdup("dpl2");
775 arates = strdup("48");
777 if( !dynamic_range_compression )
779 dynamic_range_compression = strdup("0.0");
783 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
786 job->chapter_markers = 1;
789 if (!strcmp(preset_name, "High Profile"))
795 vcodec = HB_VCODEC_X264;
796 job->vquality = 20.0;
799 atracks = strdup("1,1");
803 acodecs = strdup("faac,ac3");
807 abitrates = strdup("160,160");
811 mixdowns = strdup("dpl2,auto");
815 arates = strdup("48,Auto");
817 if( !dynamic_range_compression )
819 dynamic_range_compression = strdup("0.0,0.0");
823 x264opts = strdup("b-adapt=2:rc-lookahead=50");
828 job->chapter_markers = 1;
831 if (!strcmp(preset_name, "Classic"))
837 job->vbitrate = 1000;
840 atracks = strdup("1");
844 acodecs = strdup("faac");
848 abitrates = strdup("160");
852 mixdowns = strdup("dpl2");
856 arates = strdup("48");
858 if( !dynamic_range_compression )
860 dynamic_range_compression = strdup("0.0");
864 if (!strcmp(preset_name, "AppleTV Legacy"))
870 job->largeFileSize = 1;
871 vcodec = HB_VCODEC_X264;
872 job->vbitrate = 2500;
875 atracks = strdup("1,1");
879 acodecs = strdup("faac,ac3");
883 abitrates = strdup("160,160");
887 mixdowns = strdup("dpl2,auto");
891 arates = strdup("48,Auto");
893 if( !dynamic_range_compression )
895 dynamic_range_compression = strdup("0.0,0.0");
899 x264opts = strdup("ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
902 job->chapter_markers = 1;
905 if (!strcmp(preset_name, "iPhone Legacy"))
912 vcodec = HB_VCODEC_X264;
916 atracks = strdup("1");
920 acodecs = strdup("faac");
924 abitrates = strdup("128");
928 mixdowns = strdup("dpl2");
932 arates = strdup("48");
934 if( !dynamic_range_compression )
936 dynamic_range_compression = strdup("0.0");
941 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");
943 job->chapter_markers = 1;
946 if (!strcmp(preset_name, "iPod Legacy"))
953 vcodec = HB_VCODEC_X264;
954 job->vbitrate = 1500;
957 atracks = strdup("1");
961 acodecs = strdup("faac");
965 abitrates = strdup("160");
969 mixdowns = strdup("dpl2");
973 arates = strdup("48");
975 if( !dynamic_range_compression )
977 dynamic_range_compression = strdup("0.0");
982 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");
984 job->chapter_markers = 1;
988 if ( chapter_markers )
990 job->chapter_markers = chapter_markers;
992 if( marker_file != NULL )
994 hb_csv_file_t * file = hb_open_csv_file( marker_file );
995 hb_csv_cell_t * cell;
999 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1003 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1007 /* Parse the cells */
1008 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1010 /* We have a chapter number */
1011 if( cell->cell_col == 0 )
1013 row = cell->cell_row;
1014 chapter = atoi( cell->cell_text );
1017 /* We have a chapter name */
1018 if( cell->cell_col == 1 && row == cell->cell_row )
1020 /* If we have a valid chapter, copy the string an terminate it */
1021 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1023 hb_chapter_t * chapter_s;
1025 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1026 strncpy(chapter_s->title, cell->cell_text, 1023);
1027 chapter_s->title[1023] = '\0';
1032 hb_dispose_cell( cell );
1035 hb_close_csv_file( file );
1040 /* No marker file */
1042 int number_of_chapters = hb_list_count(job->title->list_chapter);
1045 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1047 hb_chapter_t * chapter_s;
1048 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1049 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1050 chapter_s->title[1023] = '\0';
1055 if( crop[0] >= 0 && crop[1] >= 0 &&
1056 crop[2] >= 0 && crop[3] >= 0 )
1058 memcpy( job->crop, crop, 4 * sizeof( int ) );
1061 job->deinterlace = deinterlace;
1062 job->grayscale = grayscale;
1064 /* Add selected filters */
1065 job->filters = hb_list_init();
1068 hb_filter_detelecine.settings = detelecine_opt;
1069 hb_list_add( job->filters, &hb_filter_detelecine );
1073 hb_filter_decomb.settings = decomb_opt;
1074 hb_list_add( job->filters, &hb_filter_decomb );
1078 hb_filter_deinterlace.settings = deinterlace_opt;
1079 hb_list_add( job->filters, &hb_filter_deinterlace );
1083 hb_filter_deblock.settings = deblock_opt;
1084 hb_list_add( job->filters, &hb_filter_deblock );
1088 hb_filter_denoise.settings = denoise_opt;
1089 hb_list_add( job->filters, &hb_filter_denoise );
1092 switch( anamorphic_mode )
1094 case 0: // Non-anamorphic
1096 if( width && height )
1099 job->height = height;
1104 hb_fix_aspect( job, HB_KEEP_WIDTH );
1108 job->height = height;
1109 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1111 else if( !width && !height )
1113 hb_fix_aspect( job, HB_KEEP_WIDTH );
1118 case 1: // Strict anammorphic
1119 job->anamorphic.mode = anamorphic_mode;
1122 case 2: // Loose anamorphic
1123 job->anamorphic.mode = 2;
1127 job->anamorphic.modulus = modulus;
1132 job->anamorphic.itu_par = itu_par;
1139 else if( !width && !height )
1141 /* Default to full width when one isn't specified for loose anamorphic */
1142 job->width = title->width - job->crop[2] - job->crop[3];
1147 case 3: // Custom Anamorphic 3: Power User Jamboree
1148 job->anamorphic.mode = 3;
1152 job->anamorphic.modulus = modulus;
1157 job->anamorphic.itu_par = itu_par;
1160 if( par_width && par_height )
1162 job->anamorphic.par_width = par_width;
1163 job->anamorphic.par_height = par_height;
1166 if( keep_display_aspect )
1168 job->anamorphic.keep_display_aspect = 1;
1170 /* First, what *is* the display aspect? */
1171 int cropped_width = title->width - job->crop[2] - job->crop[3];
1172 int cropped_height = title->height - job->crop[0] - job->crop[1];
1174 /* XXX -- I'm assuming people want to keep the source
1175 display AR even though they might have already
1176 asked for ITU values instead. */
1177 float source_display_width = (float)cropped_width *
1178 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1179 float display_aspect = source_display_width / (float)cropped_height;
1180 /* When keeping display aspect, we have to rank some values
1181 by priority in order to consistently handle situations
1182 when more than one might be specified by default.
1184 * First off, PAR gets ignored. (err make this reality)
1185 * Height will be respected over all other settings,
1186 * If it isn't set, display_width will be followed.
1187 * If it isn't set, width will be followed. */
1190 /* We scale the display width to the new height */
1191 display_width = (int)( (double)height * display_aspect );
1193 else if( display_width )
1195 /* We scale the height to the new display width */
1196 height = (int)( (double)display_width / display_aspect );
1202 /* Adjust the PAR to create the new display width
1203 from the default job width. */
1204 job->anamorphic.dar_width = display_width;
1206 job->anamorphic.dar_height = height ?
1208 title->height - job->crop[0] - job->crop[1];
1211 if( width && height )
1213 /* Use these storage dimensions */
1215 job->height = height;
1219 /* Use just this storage width */
1221 job->height = title->height - job->crop[0] - job->crop[1];
1225 /* Use just this storage height. */
1226 job->height = height;
1227 job->width = title->width - job->crop[2] - job->crop[3];
1229 else if( !width && !height )
1231 /* Assume source dimensions after cropping. */
1232 job->width = title->width - job->crop[2] - job->crop[3];
1233 job->height = title->height - job->crop[0] - job->crop[1];
1239 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1241 job->vquality = vquality;
1246 job->vquality = -1.0;
1247 job->vbitrate = vbitrate;
1251 job->vcodec = vcodec;
1255 job->h264_level = 13;
1259 job->h264_level = 30;
1264 job->vrate = 27000000;
1265 job->vrate_base = vrate;
1269 // cfr or pfr flag with no rate specified implies
1270 // use the title rate.
1272 job->vrate = title->rate;
1273 job->vrate_base = title->rate_base;
1276 /* Grab audio tracks */
1279 char * token = strtok(atracks, ",");
1282 int track_start, track_end;
1283 while( token != NULL )
1285 audio = calloc(1, sizeof(*audio));
1286 hb_audio_config_init(audio);
1287 if (strlen(token) >= 3)
1289 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1292 for (i = track_start - 1; i < track_end; i++)
1294 if (i != track_start - 1)
1296 audio = calloc(1, sizeof(*audio));
1297 hb_audio_config_init(audio);
1299 audio->in.track = i;
1300 audio->out.track = num_audio_tracks++;
1301 hb_list_add(audios, audio);
1304 else if( !strcasecmp(token, "none" ) )
1306 audio->in.track = audio->out.track = -1;
1307 audio->out.codec = 0;
1308 hb_list_add(audios, audio);
1313 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1320 audio->in.track = atoi(token) - 1;
1321 audio->out.track = num_audio_tracks++;
1322 hb_list_add(audios, audio);
1324 token = strtok(NULL, ",");
1328 /* Parse audio tracks */
1329 if( native_language && native_dub )
1331 if( hb_list_count( audios ) == 0 || !audio_explicit )
1333 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1338 audio = hb_list_audio_config_item( title->list_audio, i );
1340 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1342 if( strncasecmp( native_language, audio_lang,
1343 sizeof( audio_lang ) ) == 0 &&
1344 audio->lang.type != 3 && // Directors 1
1345 audio->lang.type != 4) // Directors 2
1348 * Matched an audio to our native language - use it.
1349 * Replace any existing audio tracks that a preset may
1352 if( hb_list_count(audios) == 0) {
1353 audio = calloc(1, sizeof(*audio));
1354 hb_audio_config_init(audio);
1355 audio->in.track = track;
1356 audio->out.track = num_audio_tracks++;
1357 /* Add it to our audios */
1358 hb_list_add(audios, audio);
1361 * Update the track numbers on what is already in
1364 for( i=0; i < hb_list_count( audios ); i++ )
1366 audio = hb_list_item( audios, i );
1368 audio->in.track = track;
1375 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1379 if( hb_list_count(audios) == 0 &&
1380 hb_list_count(job->title->list_audio) > 0 )
1382 /* Create a new audio track with default settings */
1383 audio = calloc(1, sizeof(*audio));
1384 hb_audio_config_init(audio);
1385 /* Add it to our audios */
1386 hb_list_add(audios, audio);
1389 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1390 for (i = 0; i < tmp_num_audio_tracks; i++)
1392 audio = hb_list_item(audios, 0);
1393 if( (audio == NULL) || (audio->in.track == -1) ||
1394 (audio->out.track == -1) || (audio->out.codec == 0) )
1400 if( hb_audio_add( job, audio ) == 0 )
1402 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1403 audio->in.track + 1 );
1408 hb_list_rem(audios, audio);
1410 if( audio->out.name )
1412 free( audio->out.name);
1421 char * token = strtok(acodecs, ",");
1424 while ( token != NULL )
1426 if ((acodec = get_acodec_for_string(token)) == -1)
1428 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1429 acodec = default_acodec;
1431 if( i < num_audio_tracks )
1433 audio = hb_list_audio_config_item(job->list_audio, i);
1434 audio->out.codec = acodec;
1438 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1439 hb_audio_config_t audio;
1443 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1445 hb_audio_config_init(&audio);
1446 audio.in.track = last_audio->in.track;
1447 audio.out.track = num_audio_tracks++;
1448 audio.out.codec = acodec;
1449 hb_audio_add(job, &audio);
1453 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1456 token = strtok(NULL, ",");
1460 if( i < num_audio_tracks )
1462 /* We have fewer inputs than audio tracks, use the default codec for
1463 * this container for the remaining tracks. Unless we only have one input
1464 * then use that codec instead.
1467 acodec = default_acodec;
1468 for ( ; i < num_audio_tracks; i++)
1470 audio = hb_list_audio_config_item(job->list_audio, i);
1471 audio->out.codec = acodec;
1480 char * token = strtok(arates, ",");
1483 while ( token != NULL )
1485 arate = atoi(token);
1486 audio = hb_list_audio_config_item(job->list_audio, i);
1489 for( j = 0; j < hb_audio_rates_count; j++ )
1491 if( !strcmp( token, hb_audio_rates[j].string ) )
1493 arate = hb_audio_rates[j].rate;
1500 if (!is_sample_rate_valid(arate))
1502 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1503 arate = audio->in.samplerate;
1506 audio->out.samplerate = arate;
1507 if( (++i) >= num_audio_tracks )
1508 break; /* We have more inputs than audio tracks, oops */
1512 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1514 token = strtok(NULL, ",");
1517 if (i < num_audio_tracks)
1519 /* We have fewer inputs than audio tracks, use default sample rate.
1520 * Unless we only have one input, then use that for all tracks.
1523 arate = audio->in.samplerate;
1524 for ( ; i < num_audio_tracks; i++)
1526 audio = hb_list_audio_config_item(job->list_audio, i);
1527 audio->out.samplerate = arate;
1536 char * token = strtok(abitrates, ",");
1539 while ( token != NULL )
1541 abitrate = atoi(token);
1542 audio = hb_list_audio_config_item(job->list_audio, i);
1546 audio->out.bitrate = abitrate;
1547 if( (++i) >= num_audio_tracks )
1548 break; /* We have more inputs than audio tracks, oops */
1552 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1554 token = strtok(NULL, ",");
1557 if (i < num_audio_tracks)
1559 /* We have fewer inputs than audio tracks, use the default bitrate
1560 * for the remaining tracks. Unless we only have one input, then use
1561 * that for all tracks.
1564 abitrate = default_abitrate;
1565 for (; i < num_audio_tracks; i++)
1567 audio = hb_list_audio_config_item(job->list_audio, i);
1568 audio->out.bitrate = abitrate;
1575 if ( dynamic_range_compression )
1577 char * token = strtok(dynamic_range_compression, ",");
1579 token = dynamic_range_compression;
1580 while ( token != NULL )
1582 d_r_c = atof(token);
1583 audio = hb_list_audio_config_item(job->list_audio, i);
1586 audio->out.dynamic_range_compression = d_r_c;
1587 if( (++i) >= num_audio_tracks )
1588 break; /* We have more inputs than audio tracks, oops */
1592 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1594 token = strtok(NULL, ",");
1597 if (i < num_audio_tracks)
1599 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1600 * tracks. Unless we only have one input, then use the same DRC for all
1605 for (; i < num_audio_tracks; i++)
1607 audio = hb_list_audio_config_item(job->list_audio, i);
1608 audio->out.dynamic_range_compression = d_r_c;
1617 char * token = strtok(mixdowns, ",");
1620 while ( token != NULL )
1622 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1623 audio = hb_list_audio_config_item(job->list_audio, i);
1626 audio->out.mixdown = mixdown;
1627 if( (++i) >= num_audio_tracks )
1628 break; /* We have more inputs than audio tracks, oops */
1632 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1634 token = strtok(NULL, ",");
1637 if (i < num_audio_tracks)
1639 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1640 * we only have one input, then use that.
1643 mixdown = HB_AMIXDOWN_DOLBYPLII;
1644 for (; i < num_audio_tracks; i++)
1646 audio = hb_list_audio_config_item(job->list_audio, i);
1647 audio->out.mixdown = mixdown;
1652 /* Audio Track Names */
1656 char * token = strtok(anames, ",");
1659 while ( token != NULL )
1661 audio = hb_list_audio_config_item(job->list_audio, i);
1664 audio->out.name = strdup(token);
1665 if( (++i) >= num_audio_tracks )
1666 break; /* We have more names than audio tracks, oops */
1670 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1673 token = strtok(NULL, ",");
1676 if( i < num_audio_tracks && i == 1 )
1678 /* We have exactly one name and more than one audio track. Use the same
1679 * name for all tracks. */
1680 for ( ; i < num_audio_tracks; i++)
1682 audio = hb_list_audio_config_item(job->list_audio, i);
1683 audio->out.name = strdup(anames);
1686 /* Audio Track Names */
1690 job->vbitrate = hb_calc_bitrate( job, size );
1691 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1701 for ( i = 0; subtracks[i] != NULL; i++ )
1704 token = subtracks[i];
1705 if( strcasecmp(token, "scan" ) == 0 )
1707 int burn = 0, force = 0, def = 0;
1709 if ( subburn != NULL )
1711 burn = ( pos == 1 && subburn[0] == 0 ) ||
1712 ( strcmp( "scan", subburn ) == 0 );
1714 if ( subdefault != NULL )
1716 def = ( pos == 1 && subdefault[0] == 0 ) ||
1717 ( strcmp( "scan", subdefault ) == 0 );
1719 force = test_sub_list( subforce, "scan", pos );
1721 if ( !burn && mux == HB_MUX_MKV )
1723 job->select_subtitle_config.dest = PASSTHRUSUB;
1733 if ( !( !burn && mux == HB_MUX_MP4 ) )
1735 job->select_subtitle_config.force = force;
1736 job->select_subtitle_config.default_track = def;
1739 fprintf( stderr, "Warning: Subtitle Scan for MP4 requires the '--subtitle-burn' option to be selected\n");
1744 hb_subtitle_t * subtitle;
1745 hb_subtitle_config_t sub_config;
1747 int burn = 0, force = 0, def = 0;
1749 track = atoi(token) - 1;
1750 subtitle = hb_list_item(title->list_subtitle, track);
1751 if( subtitle == NULL )
1753 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1756 sub_config = subtitle->config;
1758 if ( subburn != NULL )
1760 burn = ( pos == 1 && subburn[0] == 0 ) ||
1761 ( strcmp( token, subburn ) == 0 );
1763 if ( subdefault != NULL )
1765 def = ( pos == 1 && subdefault[0] == 0 ) ||
1766 ( strcmp( token, subdefault ) == 0 );
1769 force = test_sub_list(subforce, token, pos);
1771 if ( !burn && mux == HB_MUX_MKV &&
1772 subtitle->format == PICTURESUB)
1774 sub_config.dest = PASSTHRUSUB;
1776 else if (!burn && mux == HB_MUX_MP4 &&
1777 subtitle->format == PICTURESUB)
1779 // Skip any non-burned vobsubs when output is mp4
1780 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 );
1783 else if ( burn && subtitle->format == PICTURESUB )
1785 // Only allow one subtitle to be burned into video
1788 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1793 sub_config.force = force;
1794 sub_config.default_track = def;
1795 hb_subtitle_add( job, &sub_config, track );
1804 hb_subtitle_config_t sub_config;
1807 for( i=0; srtfile[i] != NULL; i++ )
1809 char *codeset = "L1";
1815 if( srtcodeset && srtcodeset[i] )
1817 codeset = srtcodeset[i];
1819 if( srtoffset && srtoffset[i] )
1821 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1823 if ( srtlang && srtlang[i] )
1827 sub_config.default_track =
1828 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1829 sub_config.force = 0;
1830 strncpy( sub_config.src_filename, srtfile[i], 128);
1831 strncpy( sub_config.src_codeset, codeset, 40);
1832 sub_config.offset = offset;
1834 hb_srt_add( job, &sub_config, lang);
1838 if( native_language )
1842 audio = hb_list_audio_config_item(job->list_audio, 0);
1846 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1848 if( strncasecmp( native_language, audio_lang,
1849 sizeof( audio_lang ) ) != 0 )
1852 * Audio language is not the same as our native language.
1853 * If we have any subtitles in our native language they
1854 * should be selected here if they haven't already been.
1856 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1857 int matched_track = 0;
1859 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1861 subtitle = hb_list_item( title->list_subtitle, i );
1863 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1866 * Found the first matching subtitle in our
1867 * native language. Is it already selected?
1869 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1871 subtitle2 = hb_list_item( job->list_subtitle, i );
1873 if( subtitle2->track == subtitle->track) {
1882 if( subtitle2 == NULL )
1885 * Not already selected, so select it.
1887 hb_subtitle_config_t sub_config;
1891 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1893 sub_config = subtitle->config;
1895 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1897 sub_config.dest = PASSTHRUSUB;
1900 sub_config.force = 0;
1901 sub_config.default_track = 1;
1902 hb_subtitle_add( job, &sub_config, matched_track);
1919 if ( largeFileSize )
1921 job->largeFileSize = 1;
1925 job->mp4_optimize = 1;
1932 job->file = strdup( output );
1936 job->color_matrix = color_matrix;
1939 if( x264opts != NULL && *x264opts != '\0' )
1941 job->x264opts = x264opts;
1943 else /*avoids a bus error crash when options aren't specified*/
1945 job->x264opts = NULL;
1948 job->maxWidth = maxWidth;
1950 job->maxHeight = maxHeight;
1952 if( start_at_preview )
1954 job->start_at_preview = start_at_preview - 1;
1955 job->seek_points = preview_count;
1960 job->pts_to_stop = stop_at_pts;
1966 job->frame_to_stop = stop_at_frame;
1975 * When subtitle scan is enabled do a fast pre-scan job
1976 * which will determine which subtitles to enable, if any.
1980 x264opts_tmp = job->x264opts;
1982 job->x264opts = NULL;
1984 job->indepth_scan = subtitle_scan;
1985 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1986 "subtitles if found for foreign language segments\n");
1989 * Add the pre-scan job
1993 job->x264opts = x264opts_tmp;
1999 * If subtitle_scan is enabled then only turn it on
2000 * for the first pass and then off again for the
2005 job->indepth_scan = 0;
2009 x264opts2 = strdup(x264opts);
2013 * If turbo options have been selected then append them
2014 * to the x264opts now (size includes one ':' and the '\0')
2016 if( turbo_opts_enabled )
2018 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2021 tmp_x264opts = malloc(size * sizeof(char));
2024 snprintf( tmp_x264opts, size, "%s:%s",
2025 x264opts, turbo_opts );
2029 * No x264opts to modify, but apply the turbo options
2030 * anyway as they may be modifying defaults
2032 snprintf( tmp_x264opts, size, "%s",
2035 x264opts = tmp_x264opts;
2037 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2040 job->x264opts = x264opts;
2046 * On the second pass we turn off subtitle scan so that we
2047 * can actually encode using any subtitles that were auto
2048 * selected in the first pass (using the whacky select-subtitle
2049 * attribute of the job).
2051 job->indepth_scan = 0;
2053 job->x264opts = x264opts2;
2060 * Turn on subtitle scan if requested, note that this option
2061 * precludes encoding of any actual subtitles.
2064 job->indepth_scan = 0;
2072 #define p s.param.working
2073 case HB_STATE_WORKING:
2074 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2075 p.job_cur, p.job_count, 100.0 * p.progress );
2076 if( p.seconds > -1 )
2078 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2079 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2080 p.hours, p.minutes, p.seconds );
2086 #define p s.param.muxing
2087 case HB_STATE_MUXING:
2089 if (show_mux_warning)
2091 fprintf( stdout, "\rMuxing: this may take awhile..." );
2093 show_mux_warning = 0;
2099 #define p s.param.workdone
2100 case HB_STATE_WORKDONE:
2101 /* Print error if any, then exit */
2105 fprintf( stderr, "\nRip done!\n" );
2107 case HB_ERROR_CANCELED:
2108 fprintf( stderr, "\nRip canceled.\n" );
2111 fprintf( stderr, "\nRip failed (error %x).\n",
2121 /****************************************************************************
2123 ****************************************************************************/
2124 static volatile int64_t i_die_date = 0;
2125 void SigHandler( int i_signal )
2130 i_die_date = hb_get_date();
2131 fprintf( stderr, "Signal %d received, terminating - do it "
2132 "again in case it gets stuck\n", i_signal );
2134 else if( i_die_date + 500 < hb_get_date() )
2136 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2141 /****************************************************************************
2143 ****************************************************************************/
2144 static void ShowHelp()
2147 FILE* const out = stdout;
2150 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2152 "### General Handbrake Options------------------------------------------------\n\n"
2153 " -h, --help Print help\n"
2154 " -u, --update Check for updates and exit\n"
2155 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2156 " -C, --cpu Set CPU count (default: autodetected)\n"
2157 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2158 " if the preset name has spaces, surround it with\n"
2159 " double quotation marks\n"
2160 " -z, --preset-list See a list of available built-in presets\n"
2161 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2162 " (experimental, enabled by default for testing)\n"
2165 "### Source Options-----------------------------------------------------------\n\n"
2166 " -i, --input <string> Set input device\n"
2167 " -t, --title <number> Select a title to encode (0 to scan only,\n"
2169 " -L, --longest Select the longest title\n"
2170 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2171 " 1 to 3, or \"3\" for chapter 3 only,\n"
2172 " default: all chapters)\n"
2173 " --angle <number> Select the DVD angle\n"
2174 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2175 " and whether or not they're stored to disk (0 or 1).\n"
2176 " (default: 10:0)\n"
2177 " --start-at-preview <#> Start encoding at a given preview.\n"
2178 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2179 " or pts (on a 90kHz clock)"
2182 "### Destination Options------------------------------------------------------\n\n"
2183 " -o, --output <string> Set output file name\n"
2184 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2185 " autodetected from file name)\n"
2186 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2187 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2188 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2189 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2190 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2194 "### Video Options------------------------------------------------------------\n\n"
2195 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2196 " (default: ffmpeg)\n"
2197 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2198 " same style as mencoder:\n"
2199 " option1=value1:option2=value2\n"
2200 " -q, --quality <float> Set video quality (0.0..1.0)\n"
2201 " -S, --size <MB> Set target size\n"
2202 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2203 " -2, --two-pass Use two-pass mode\n"
2204 " -T, --turbo When using 2-pass use the turbo options\n"
2205 " on the first pass to improve speed\n"
2206 " (only works with x264, affects PSNR by about 0.05dB,\n"
2207 " and increases first pass speed two to four times)\n"
2208 " -r, --rate Set video framerate (" );
2209 for( i = 0; i < hb_video_rates_count; i++ )
2211 fprintf( out, hb_video_rates[i].string );
2212 if( i != hb_video_rates_count - 1 )
2213 fprintf( out, "/" );
2216 " Be aware that not specifying a framerate lets\n"
2217 " HandBrake preserve a source's time stamps,\n"
2218 " potentially creating variable framerate video\n"
2219 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2220 " frame rate control. VFR preserves the source\n"
2221 " timing. CFR makes the output constant rate at\n"
2222 " the rate given by the -r flag (or the source's\n"
2223 " average rate if no -r is given). PFR doesn't\n"
2224 " allow the rate to go over the rate specified\n"
2225 " with the -r flag but won't change the source\n"
2226 " timing if it's below that rate.\n"
2227 " If none of these flags are given, the default\n"
2228 " is --cfr when -r is given and --vfr otherwise\n"
2231 "### Audio Options-----------------------------------------------------------\n\n"
2232 " -a, --audio <string> Select audio track(s), separated by commas\n"
2233 " More than one output track can be used for one\n"
2235 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2236 " tracks, default: first one)\n" );
2240 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2241 " ac3 and dts meaning passthrough\n"
2242 " Separated by commas for more than one audio track.\n"
2243 " (default: guessed)\n" );
2246 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2247 " ac3 and dts meaning passthrough\n"
2248 " Separated by commas for more than one audio track.\n"
2249 " (default: guessed)\n" );
2252 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2253 " Separated by commas for more than one audio track.\n"
2254 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2255 " Separated by commas for more than one audio track.\n"
2256 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2257 " -R, --arate Set audio samplerate(s) (" );
2258 for( i = 0; i < hb_audio_rates_count; i++ )
2260 fprintf( out, hb_audio_rates[i].string );
2261 if( i != hb_audio_rates_count - 1 )
2262 fprintf( out, "/" );
2264 fprintf( out, " kHz)\n"
2265 " Separated by commas for more than one audio track.\n"
2266 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2267 " making soft sounds louder. Range is 1.0 to 4.0\n"
2268 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2269 " Separated by commas for more than one audio track.\n"
2270 " -A, --aname <string> Audio track name(s),\n"
2271 " Separated by commas for more than one audio track.\n"
2274 "### Picture Settings---------------------------------------------------------\n\n"
2275 " -w, --width <number> Set picture width\n"
2276 " -l, --height <number> Set picture height\n"
2277 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2278 " -Y, --maxHeight <#> Set maximum height\n"
2279 " -X, --maxWidth <#> Set maximum width\n"
2280 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2281 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2282 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2283 " directly control all parameters.\n"
2284 " --display-width Set the width to scale the actual pixels to\n"
2285 " <number> at playback, for custom anamorphic.\n"
2286 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2287 " when using custom anamorphic\n"
2288 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2290 " (--display-width and --pixel-aspect are mutually\n"
2291 " exclusive and the former will override the latter)\n"
2292 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2293 " custom anamorphic, useful with underscanned sources\n"
2294 " --modulus Set the number you want the scaled pixel dimensions\n"
2295 " <number> to divide cleanly by, for loose and custom\n"
2296 " anamorphic modes (default: 16)\n"
2297 " -M --color-matrix Set the color space signaled by the output\n"
2298 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2299 " default: set by resolution)\n"
2302 "### Filters---------------------------------------------------------\n\n"
2304 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2305 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2307 " <fast/slow/slower>\n"
2308 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2309 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2310 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2311 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2312 " Note: this filter drops duplicate frames to\n"
2313 " restore the pre-telecine framerate, unless you\n"
2314 " specify a constant framerate (--rate 29.97)\n"
2315 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2316 " -8, --denoise Denoise video with hqdn3d filter\n"
2317 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2319 " <weak/medium/strong>\n"
2320 " -7, --deblock Deblock video with pp7 filter\n"
2321 " <QP:M> (default 5:2)\n"
2322 " -g, --grayscale Grayscale encoding\n"
2325 "### Subtitle Options------------------------------------------------------------\n\n"
2326 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2327 " More than one output track can be used for one\n"
2329 " (\"1,2,3\" for multiple tracks.\n"
2330 " A special track name \"scan\" adds an extra 1st pass.\n"
2331 " This extra pass scans subtitles matching the\n"
2332 " language of the first audio or the language \n"
2333 " selected by --native-language.\n"
2334 " The one that's only used 10 percent of the time\n"
2335 " or less is selected. This should locate subtitles\n"
2336 " for short foreign language segments. Best used in\n"
2337 " conjunction with --subtitle-forced.\n"
2338 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2339 " <string> the subtitle has the forced flag set. May be used in\n"
2340 " conjunction with \"scan\" track to auto-select\n"
2341 " a stream if it contains forced subtitles.\n"
2342 " Separated by commas for more than one audio track.\n"
2343 " (\"1,2,3\" for multiple tracks.\n"
2344 " If \"string\" is omitted, the first trac is forced.\n"
2345 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2346 " <number> If \"number\" is omitted, the first trac is burned.\n"
2347 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2348 " <number> to be displayed upon playback. Setting no default\n"
2349 " means no subtitle will be automatically displayed\n"
2350 " If \"number\" is omitted, the first trac is default.\n"
2351 " -N, --native-language Specifiy the your language preference. When the first\n"
2352 " <string> audio track does not match your native language then\n"
2353 " select the first subtitle that does. When used in\n"
2354 " conjunction with --native-dub the audio track is\n"
2355 " changed in preference to subtitles. Provide the\n"
2356 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2357 " --native-dub Used in conjunction with --native-language\n"
2358 " requests that if no audio tracks are selected the\n"
2359 " default selected audio track will be the first one\n"
2360 " that matches the --native-language. If there are no\n"
2361 " matching audio tracks then the first matching\n"
2362 " subtitle track is used instead.\n"
2363 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2364 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2365 " <string> encoded in, separted by commas.\n"
2366 " Use 'iconv -l' for a list of valid\n"
2367 " codesets. If not specified latin1 is assumed\n"
2368 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2369 " <string> separted by commas. If not specified zero is assumed.\n"
2370 " Offsets may be negative.\n"
2371 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2372 " for the SRT file(s) separated by commas. If not specified\n"
2373 " then 'und' is used.\n"
2374 " --srt-default Flag the selected srt as the default subtitle\n"
2375 " <number> to be displayed upon playback. Setting no default\n"
2376 " means no subtitle will be automatically displayed\n"
2377 " If \"number\" is omitted, the first srt is default.\n"
2378 " \"number\" is an 1 based index into the srt-file list\n"
2385 /****************************************************************************
2387 ****************************************************************************/
2388 static void ShowPresets()
2390 printf("\n< Apple\n");
2392 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");
2394 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");
2396 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");
2398 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");
2402 printf("\n< Regular\n");
2404 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");
2406 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");
2410 printf("\n< Legacy\n");
2412 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2414 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");
2416 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");
2418 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");
2423 static char * hb_strndup( char * str, int len )
2426 int str_len = strlen( str );
2428 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2429 strncpy( res, str, len );
2434 static char** str_split( char *str, char delem )
2441 if ( str == NULL || str[0] == 0 )
2443 ret = malloc( sizeof(char*) );
2448 // Find number of elements in the string
2451 while ( ( pos = strchr( pos, delem ) ) != NULL )
2457 ret = calloc( ( count + 1 ), sizeof(char*) );
2460 for ( i = 0; i < count - 1; i++ )
2462 end = strchr( pos, delem );
2463 ret[i] = hb_strndup(pos, end - pos);
2466 ret[i] = strdup(pos);
2471 /****************************************************************************
2473 ****************************************************************************/
2474 static int ParseOptions( int argc, char ** argv )
2477 #define PREVIEWS 257
2478 #define START_AT_PREVIEW 258
2482 #define DISPLAY_WIDTH 262
2483 #define PIXEL_ASPECT 263
2485 #define KEEP_DISPLAY_ASPECT 265
2486 #define SUB_BURNED 266
2487 #define SUB_DEFAULT 267
2488 #define NATIVE_DUB 268
2489 #define SRT_FILE 269
2490 #define SRT_CODESET 270
2491 #define SRT_OFFSET 271
2492 #define SRT_LANG 272
2493 #define SRT_DEFAULT 273
2497 static struct option long_options[] =
2499 { "help", no_argument, NULL, 'h' },
2500 { "update", no_argument, NULL, 'u' },
2501 { "verbose", optional_argument, NULL, 'v' },
2502 { "cpu", required_argument, NULL, 'C' },
2503 { "no-dvdnav", no_argument, NULL, DVDNAV },
2505 { "format", required_argument, NULL, 'f' },
2506 { "input", required_argument, NULL, 'i' },
2507 { "output", required_argument, NULL, 'o' },
2508 { "large-file", no_argument, NULL, '4' },
2509 { "optimize", no_argument, NULL, 'O' },
2510 { "ipod-atom", no_argument, NULL, 'I' },
2512 { "title", required_argument, NULL, 't' },
2513 { "longest", no_argument, NULL, 'L' },
2514 { "chapters", required_argument, NULL, 'c' },
2515 { "angle", required_argument, NULL, ANGLE },
2516 { "markers", optional_argument, NULL, 'm' },
2517 { "audio", required_argument, NULL, 'a' },
2518 { "mixdown", required_argument, NULL, '6' },
2519 { "drc", required_argument, NULL, 'D' },
2520 { "subtitle", required_argument, NULL, 's' },
2521 { "subtitle-forced", optional_argument, NULL, 'F' },
2522 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2523 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2524 { "srt-file", required_argument, NULL, SRT_FILE },
2525 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2526 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2527 { "srt-lang", required_argument, NULL, SRT_LANG },
2528 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2529 { "native-language", required_argument, NULL,'N' },
2530 { "native-dub", no_argument, NULL, NATIVE_DUB },
2531 { "encoder", required_argument, NULL, 'e' },
2532 { "aencoder", required_argument, NULL, 'E' },
2533 { "two-pass", no_argument, NULL, '2' },
2534 { "deinterlace", optional_argument, NULL, 'd' },
2535 { "deblock", optional_argument, NULL, '7' },
2536 { "denoise", optional_argument, NULL, '8' },
2537 { "detelecine", optional_argument, NULL, '9' },
2538 { "decomb", optional_argument, NULL, '5' },
2539 { "grayscale", no_argument, NULL, 'g' },
2540 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2541 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2542 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2543 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2544 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2545 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2546 { "modulus", required_argument, NULL, MODULUS },
2547 { "itu-par", no_argument, &itu_par, 1 },
2548 { "width", required_argument, NULL, 'w' },
2549 { "height", required_argument, NULL, 'l' },
2550 { "crop", required_argument, NULL, 'n' },
2552 { "vb", required_argument, NULL, 'b' },
2553 { "quality", required_argument, NULL, 'q' },
2554 { "size", required_argument, NULL, 'S' },
2555 { "ab", required_argument, NULL, 'B' },
2556 { "rate", required_argument, NULL, 'r' },
2557 { "arate", required_argument, NULL, 'R' },
2558 { "x264opts", required_argument, NULL, 'x' },
2559 { "turbo", no_argument, NULL, 'T' },
2560 { "maxHeight", required_argument, NULL, 'Y' },
2561 { "maxWidth", required_argument, NULL, 'X' },
2562 { "preset", required_argument, NULL, 'Z' },
2563 { "preset-list", no_argument, NULL, 'z' },
2565 { "aname", required_argument, NULL, 'A' },
2566 { "color-matrix",required_argument, NULL, 'M' },
2567 { "previews", required_argument, NULL, PREVIEWS },
2568 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2569 { "stop-at", required_argument, NULL, STOP_AT },
2570 { "vfr", no_argument, &cfr, 0 },
2571 { "cfr", no_argument, &cfr, 1 },
2572 { "pfr", no_argument, &cfr, 2 },
2576 int option_index = 0;
2579 c = getopt_long( argc, argv,
2580 "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",
2581 long_options, &option_index );
2590 /* option was handled entirely in getopt_long */
2599 if( optarg != NULL )
2601 debug = atoi( optarg );
2609 cpu = atoi( optarg );
2614 preset_name = strdup(optarg);
2624 format = strdup( optarg );
2627 input = strdup( optarg );
2629 char *devName = bsd_name_for_path( input ); // alloc
2632 if( device_is_dvd( devName ))
2635 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2636 sprintf( input, "/dev/%s", devName );
2643 output = strdup( optarg );
2656 titleindex = atoi( optarg );
2664 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2666 chapter_start = start;
2669 else if( sscanf( optarg, "%d", &start ) == 1 )
2671 chapter_start = start;
2672 chapter_end = chapter_start;
2676 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2683 angle = atoi( optarg );
2686 if( optarg != NULL )
2688 marker_file = strdup( optarg );
2690 chapter_markers = 1;
2693 if( optarg != NULL )
2695 atracks = strdup( optarg );
2704 if( optarg != NULL )
2706 mixdowns = strdup( optarg );
2710 if( optarg != NULL )
2712 dynamic_range_compression = strdup( optarg );
2716 subtracks = str_split( optarg, ',' );
2719 subforce = str_split( optarg, ',' );
2722 if( optarg != NULL )
2724 subburn = strdup( optarg );
2732 if( optarg != NULL )
2734 subdefault = strdup( optarg );
2742 native_language = strdup( optarg );
2748 srtfile = str_split( optarg, ',' );
2751 srtcodeset = str_split( optarg, ',' );
2754 srtoffset = str_split( optarg, ',' );
2757 srtlang = str_split( optarg, ',' );
2760 if( optarg != NULL )
2762 srtdefault = atoi( optarg );
2773 if( optarg != NULL )
2775 if (!( strcmp( optarg, "fast" ) ))
2777 deinterlace_opt = "-1";
2779 else if (!( strcmp( optarg, "slow" ) ))
2781 deinterlace_opt = "2";
2783 else if (!( strcmp( optarg, "slower" ) ))
2785 deinterlace_opt = "0";
2789 deinterlace_opt = strdup( optarg );
2795 if( optarg != NULL )
2797 deblock_opt = strdup( optarg );
2802 if( optarg != NULL )
2804 if (!( strcmp( optarg, "weak" ) ))
2806 denoise_opt = "2:1:2:3";
2808 else if (!( strcmp( optarg, "medium" ) ))
2810 denoise_opt = "3:2:2:3";
2812 else if (!( strcmp( optarg, "strong" ) ))
2814 denoise_opt = "7:7:5:5";
2818 denoise_opt = strdup( optarg );
2824 if( optarg != NULL )
2826 detelecine_opt = strdup( optarg );
2831 if( optarg != NULL )
2833 decomb_opt = strdup( optarg );
2841 if( optarg != NULL )
2843 sscanf( optarg, "%i", &display_width );
2847 if( optarg != NULL )
2849 sscanf( optarg, "%i:%i", &par_width, &par_height );
2853 if( optarg != NULL )
2855 sscanf( optarg, "%i", &modulus );
2859 if( !strcasecmp( optarg, "ffmpeg" ) )
2861 vcodec = HB_VCODEC_FFMPEG;
2863 else if( !strcasecmp( optarg, "x264" ) )
2865 vcodec = HB_VCODEC_X264;
2867 else if( !strcasecmp( optarg, "x264b13" ) )
2869 vcodec = HB_VCODEC_X264;
2872 else if( !strcasecmp( optarg, "x264b30" ) )
2874 vcodec = HB_VCODEC_X264;
2877 else if( !strcasecmp( optarg, "theora" ) )
2879 vcodec = HB_VCODEC_THEORA;
2883 fprintf( stderr, "invalid codec (%s)\n", optarg );
2888 if( optarg != NULL )
2890 acodecs = strdup( optarg );
2894 width = atoi( optarg );
2897 height = atoi( optarg );
2902 char * tmp = optarg;
2903 for( i = 0; i < 4; i++ )
2907 crop[i] = strtol( tmp, &tmp, 0 );
2916 for( i = 0; i < hb_video_rates_count; i++ )
2918 if( !strcmp( optarg, hb_video_rates[i].string ) )
2920 vrate = hb_video_rates[i].rate;
2926 fprintf( stderr, "invalid framerate %s\n", optarg );
2928 else if ( cfr == 0 )
2935 if( optarg != NULL )
2937 arates = strdup( optarg );
2941 vbitrate = atoi( optarg );
2944 vquality = atof( optarg );
2947 size = atoi( optarg );
2950 if( optarg != NULL )
2952 abitrates = strdup( optarg );
2956 x264opts = strdup( optarg );
2959 turbo_opts_enabled = 1;
2962 maxHeight = atoi( optarg );
2965 maxWidth = atoi (optarg );
2968 if( optarg != NULL )
2970 anames = strdup( optarg );
2974 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2976 case START_AT_PREVIEW:
2977 start_at_preview = atoi( optarg );
2980 stop_at_string = strdup( optarg );
2981 stop_at_token = strtok( stop_at_string, ":");
2982 if( !strcmp( stop_at_token, "frame" ) )
2984 stop_at_token = strtok( NULL, ":");
2985 stop_at_frame = atoi(stop_at_token);
2987 else if( !strcmp( stop_at_token, "pts" ) )
2989 stop_at_token = strtok( NULL, ":");
2990 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2992 else if( !strcmp( stop_at_token, "duration" ) )
2994 stop_at_token = strtok( NULL, ":");
2995 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2996 stop_at_pts *= 90000LL;
3000 if( atoi( optarg ) == 601 )
3002 else if( atoi( optarg ) == 709 )
3006 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
3014 static int CheckOptions( int argc, char ** argv )
3021 if( input == NULL || *input == '\0' )
3023 fprintf( stderr, "Missing input device. Run %s --help for "
3024 "syntax.\n", argv[0] );
3029 if( titleindex > 0 )
3031 if( output == NULL || *output == '\0' )
3033 fprintf( stderr, "Missing output file name. Run %s --help "
3034 "for syntax.\n", argv[0] );
3040 char * p = strrchr( output, '.' );
3043 if( p && ( !strcasecmp( p, ".mp4" ) ||
3044 !strcasecmp( p, ".m4v" ) ) )
3050 default_acodec = HB_ACODEC_FAAC;
3052 else if( p && !strcasecmp(p, ".mkv" ) )
3055 default_acodec = HB_ACODEC_AC3;
3059 fprintf( stderr, "Output format couldn't be guessed "
3060 "from file name, using default.\n" );
3064 else if( !strcasecmp( format, "mp4" ) ||
3065 !strcasecmp( format, "m4v" ) )
3071 default_acodec = HB_ACODEC_FAAC;
3073 else if( !strcasecmp( format, "mkv" ) )
3076 default_acodec = HB_ACODEC_AC3;
3080 fprintf( stderr, "Invalid output format (%s). Possible "
3081 "choices are mp4, m4v and mkv\n.", format );
3089 static int get_acodec_for_string( char *codec )
3091 if( !strcasecmp( codec, "ac3" ) )
3093 return HB_ACODEC_AC3;
3095 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3097 return HB_ACODEC_DCA;
3099 else if( !strcasecmp( codec, "lame" ) )
3101 return HB_ACODEC_LAME;
3103 else if( !strcasecmp( codec, "faac" ) )
3105 return HB_ACODEC_FAAC;
3107 else if( !strcasecmp( codec, "vorbis") )
3109 return HB_ACODEC_VORBIS;
3112 else if( !strcasecmp( codec, "ca_aac") )
3114 return HB_ACODEC_CA_AAC;
3123 static int is_sample_rate_valid(int rate)
3126 for( i = 0; i < hb_audio_rates_count; i++ )
3128 if (rate == hb_audio_rates[i].rate)
3135 /****************************************************************************
3138 * Returns the BSD device name for the block device that contains the
3139 * passed-in path. Returns NULL on failure.
3140 ****************************************************************************/
3141 static char* bsd_name_for_path(char *path)
3145 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3151 // Get the volume reference number.
3152 FSCatalogInfo catalogInfo;
3153 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3159 FSVolumeRefNum volRefNum = catalogInfo.volume;
3161 // Now let's get the device name
3162 GetVolParmsInfoBuffer volumeParms;
3163 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3169 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3170 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3171 if( volumeParms.vMVersion < 4 )
3176 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3177 if( !volumeParms.vMDeviceID )
3182 return strdup( volumeParms.vMDeviceID );
3185 /****************************************************************************
3188 * Returns whether or not the passed in BSD device represents a DVD, or other
3190 ****************************************************************************/
3191 static int device_is_dvd(char *device)
3193 io_service_t service = get_iokit_service(device);
3194 if( service == IO_OBJECT_NULL )
3198 int result = is_dvd_service(service);
3199 IOObjectRelease(service);
3203 /****************************************************************************
3206 * Returns the IOKit service object for the passed in BSD device name.
3207 ****************************************************************************/
3208 static io_service_t get_iokit_service( char *device )
3210 CFMutableDictionaryRef matchingDict;
3211 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3212 if( matchingDict == NULL )
3214 return IO_OBJECT_NULL;
3216 // Fetch the object with the matching BSD node name. There should only be
3217 // one match, so IOServiceGetMatchingService is used instead of
3218 // IOServiceGetMatchingServices to simplify the code.
3219 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3222 /****************************************************************************
3225 * Returns whether or not the service passed in is a DVD.
3227 * Searches for an IOMedia object that represents the entire (whole) media that
3228 * the volume is on. If the volume is on partitioned media, the whole media
3229 * object will be a parent of the volume's media object. If the media is not
3230 * partitioned, the volume's media object will be the whole media object.
3231 ****************************************************************************/
3232 static int is_dvd_service( io_service_t service )
3234 kern_return_t kernResult;
3237 // Create an iterator across all parents of the service object passed in.
3238 kernResult = IORegistryEntryCreateIterator( service,
3240 kIORegistryIterateRecursively | kIORegistryIterateParents,
3242 if( kernResult != KERN_SUCCESS )
3246 if( iter == IO_OBJECT_NULL )
3251 // A reference on the initial service object is released in the do-while
3252 // loop below, so add a reference to balance.
3253 IOObjectRetain( service );
3258 if( is_whole_media_service( service ) &&
3259 IOObjectConformsTo( service, kIODVDMediaClass) )
3263 IOObjectRelease( service );
3264 } while( !result && (service = IOIteratorNext( iter )) );
3265 IOObjectRelease( iter );
3270 /****************************************************************************
3271 * is_whole_media_service
3273 * Returns whether or not the service passed in is an IOMedia service and
3274 * represents the "whole" media instead of just a partition.
3276 * The whole media object is indicated in the IORegistry by the presence of a
3277 * property with the key "Whole" and value "Yes".
3278 ****************************************************************************/
3279 static int is_whole_media_service( io_service_t service )
3283 if( IOObjectConformsTo( service, kIOMediaClass ) )
3285 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3286 CFSTR( kIOMediaWholeKey ),
3287 kCFAllocatorDefault,
3293 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3294 CFRelease( wholeMedia );
3299 #endif // __APPLE_CC__