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. */
18 #import <CoreServices/CoreServices.h>
19 #include <IOKit/IOKitLib.h>
20 #include <IOKit/storage/IOMedia.h>
21 #include <IOKit/storage/IODVDMedia.h>
25 static int debug = HB_DEBUG_NONE;
26 static int update = 0;
27 static int dvdnav = 0;
28 static char * input = NULL;
29 static char * output = NULL;
30 static char * format = NULL;
31 static int titleindex = 1;
32 static int longest_title = 0;
33 static int subtitle_scan = 0;
34 static int subtitle_force = 0;
35 static char * native_language = NULL;
36 static int twoPass = 0;
37 static int deinterlace = 0;
38 static char * deinterlace_opt = 0;
39 static int deblock = 0;
40 static char * deblock_opt = 0;
41 static int denoise = 0;
42 static char * denoise_opt = 0;
43 static int detelecine = 0;
44 static char * detelecine_opt = 0;
45 static int decomb = 0;
46 static char * decomb_opt = 0;
47 static int grayscale = 0;
48 static int vcodec = HB_VCODEC_FFMPEG;
49 static int h264_13 = 0;
50 static int h264_30 = 0;
51 static hb_list_t * audios = NULL;
52 static hb_audio_config_t * audio = NULL;
53 static int num_audio_tracks = 0;
54 static char * mixdowns = NULL;
55 static char * dynamic_range_compression = NULL;
56 static char * atracks = NULL;
57 static char * arates = NULL;
58 static char * abitrates = NULL;
59 static char * acodecs = NULL;
60 static char * anames = NULL;
61 static int default_acodec = HB_ACODEC_FAAC;
62 static int default_arate = 48000;
63 static int default_abitrate = 160;
66 static int height = 0;
67 static int crop[4] = { -1,-1,-1,-1 };
70 static float vquality = -1.0;
71 static int vbitrate = 0;
74 static int pixelratio = 0;
75 static int loosePixelratio = 0;
76 static int modulus = 0;
77 static int par_height = 0;
78 static int par_width = 0;
80 static int chapter_start = 0;
81 static int chapter_end = 0;
82 static int chapter_markers = 0;
83 static char * marker_file = NULL;
85 static char *x264opts = NULL;
86 static char *x264opts2 = NULL;
87 static int maxHeight = 0;
88 static int maxWidth = 0;
89 static int turbo_opts_enabled = 0;
90 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
91 static int largeFileSize = 0;
92 static int preset = 0;
93 static char * preset_name = 0;
95 static int mp4_optimize = 0;
96 static int ipod_atom = 0;
97 static int color_matrix = 0;
98 static int preview_count = 10;
99 static int store_previews = 0;
100 static int start_at_preview = 0;
101 static int64_t stop_at_pts = 0;
102 static int stop_at_frame = 0;
103 static char * stop_at_string = NULL;
104 static char * stop_at_token = NULL;
106 /* Exit cleanly on Ctrl-C */
107 static volatile int die = 0;
108 static void SigHandler( int );
111 static void ShowCommands();
112 static void ShowHelp();
113 static void ShowPresets();
115 static int ParseOptions( int argc, char ** argv );
116 static int CheckOptions( int argc, char ** argv );
117 static int HandleEvents( hb_handle_t * h );
119 static int get_acodec_for_string( char *codec );
120 static int is_sample_rate_valid(int rate);
123 static char* bsd_name_for_path(char *path);
124 static int device_is_dvd(char *device);
125 static io_service_t get_iokit_service( char *device );
126 static int is_dvd_service( io_service_t service );
127 static is_whole_media_service( io_service_t service );
130 /* Only print the "Muxing..." message once */
131 static int show_mux_warning = 1;
133 /****************************************************************************
136 * When using the CLI just display using hb_log as we always did in the past
137 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
138 ****************************************************************************/
139 static void hb_cli_error_handler ( const char *errmsg )
141 fprintf( stderr, "ERROR: %s\n", errmsg );
144 int main( int argc, char ** argv )
150 audios = hb_list_init();
152 /* Parse command line */
153 if( ParseOptions( argc, argv ) ||
154 CheckOptions( argc, argv ) )
159 #ifdef PTW32_STATIC_LIB
160 pthread_win32_process_attach_np();
161 pthread_win32_thread_attach_np();
164 /* Register our error handler */
165 hb_register_error_handler(&hb_cli_error_handler);
168 h = hb_init( debug, update );
169 hb_dvd_set_dvdnav( dvdnav );
172 fprintf( stderr, "%s - %s - %s\n",
173 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
175 /* Check for update */
178 if( ( build = hb_check_update( h, &version ) ) > -1 )
180 fprintf( stderr, "You are using an old version of "
181 "HandBrake.\nLatest is %s (build %d).\n", version,
186 fprintf( stderr, "Your version of HandBrake is up to "
194 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
195 hb_get_cpu_count( h ) > 1 ? "s" : "" );
198 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
199 cpu > 1 ? "s" : "" );
200 hb_set_cpu_count( h, cpu );
203 /* Exit ASAP on Ctrl-C */
204 signal( SIGINT, SigHandler );
206 /* Feed libhb with a DVD to scan */
207 fprintf( stderr, "Opening %s...\n", input );
211 * We need to scan for all the titles in order to find the longest
216 hb_scan( h, input, titleindex, preview_count, store_previews );
221 #if !defined(SYS_BEOS) && !defined(__MINGW32__)
231 FD_SET( STDIN_FILENO, &fds );
232 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
239 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
241 if( buf[size] == '\n' )
248 if( size >= 256 || buf[size] == '\n' )
253 fprintf( stdout, "\nEncoding Quit by user command\n" );
257 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
279 if( input ) free( input );
280 if( output ) free( output );
281 if( format ) free( format );
284 while( ( audio = hb_list_item( audios, 0 ) ) )
286 hb_list_rem( audios, audio );
287 if( audio->out.name )
289 free( audio->out.name );
293 hb_list_close( &audios );
295 if( mixdowns ) free( mixdowns );
296 if( dynamic_range_compression ) free( dynamic_range_compression );
297 if( atracks ) free( atracks );
298 if( arates ) free( arates );
299 if( abitrates ) free( abitrates );
300 if( acodecs ) free( acodecs );
301 if( anames ) free( anames );
302 if (native_language ) free (native_language );
303 if( x264opts ) free (x264opts );
304 if( x264opts2 ) free (x264opts2 );
305 if (preset_name) free (preset_name);
306 if( stop_at_string ) free( stop_at_string );
308 fprintf( stderr, "HandBrake has exited.\n" );
310 #ifdef PTW32_STATIC_LIB
311 pthread_win32_thread_detach_np();
312 pthread_win32_process_detach_np();
318 static void ShowCommands()
320 fprintf( stdout, "\nCommands:\n" );
321 fprintf( stdout, " [h]elp Show this message\n" );
322 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
323 fprintf( stdout, " [p]ause Pause encoding\n" );
324 fprintf( stdout, " [r]esume Resume encoding\n" );
327 static void PrintTitleInfo( hb_title_t * title )
329 hb_chapter_t * chapter;
330 hb_audio_config_t * audio;
331 hb_subtitle_t * subtitle;
334 fprintf( stderr, "+ title %d:\n", title->index );
335 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
336 title->vts, title->ttn, title->cell_start, title->cell_end,
337 title->block_count );
339 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
340 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
341 title->hours, title->minutes, title->seconds );
342 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
343 title->width, title->height,
344 (float) title->aspect,
345 (float) title->rate / title->rate_base );
346 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
347 title->crop[1], title->crop[2], title->crop[3] );
348 fprintf( stderr, " + chapters:\n" );
349 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
351 chapter = hb_list_item( title->list_chapter, i );
352 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
353 "%02d:%02d:%02d\n", chapter->index,
354 chapter->cell_start, chapter->cell_end,
355 chapter->block_count, chapter->hours, chapter->minutes,
358 fprintf( stderr, " + audio tracks:\n" );
359 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
361 audio = hb_list_audio_config_item( title->list_audio, i );
362 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
364 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
365 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
369 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
372 fprintf( stderr, " + subtitle tracks:\n" );
373 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
375 subtitle = hb_list_item( title->list_subtitle, i );
376 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
380 if(title->detected_interlacing)
382 /* Interlacing was found in half or more of the preview frames */
383 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
388 static int HandleEvents( hb_handle_t * h )
391 int tmp_num_audio_tracks;
393 hb_get_state( h, &s );
400 #define p s.param.scanning
401 case HB_STATE_SCANNING:
402 /* Show what title is currently being scanned */
403 fprintf( stderr, "Scanning title %d", p.title_cur );
405 fprintf( stderr, " of %d", p.title_count );
406 fprintf( stderr, "...\n" );
410 case HB_STATE_SCANDONE:
417 /* Audio argument string parsing variables */
421 int mixdown = HB_AMIXDOWN_DOLBYPLII;
423 /* Audio argument string parsing variables */
425 list = hb_get_titles( h );
427 if( !hb_list_count( list ) )
429 /* No valid title, stop right there */
430 fprintf( stderr, "No title found.\n" );
437 int longest_title_idx=0;
438 int longest_title_pos=-1;
439 int longest_title_time=0;
442 fprintf( stderr, "Searching for longest title...\n" );
444 for( i = 0; i < hb_list_count( list ); i++ )
446 title = hb_list_item( list, i );
447 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
448 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
449 i, title->index, title_time );
450 if( longest_title_time < title_time )
452 longest_title_time = title_time;
453 longest_title_pos = i;
454 longest_title_idx = title->index;
457 if( longest_title_pos == -1 )
459 fprintf( stderr, "No longest title found.\n" );
463 titleindex = longest_title_idx;
464 fprintf( stderr, "Found longest title, setting title to %d\n",
467 title = hb_list_item( list, longest_title_pos);
469 title = hb_list_item( list, 0 );
474 /* Scan-only mode, print infos and exit */
476 for( i = 0; i < hb_list_count( list ); i++ )
478 title = hb_list_item( list, i );
479 PrintTitleInfo( title );
485 /* Set job settings */
488 PrintTitleInfo( title );
490 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
492 job->chapter_start = MAX( job->chapter_start,
494 job->chapter_end = MIN( job->chapter_end,
496 job->chapter_end = MAX( job->chapter_start,
507 fprintf( stderr, "+ Using preset: %s", preset_name);
509 if (!strcmp(preset_name, "Universal"))
512 vcodec = HB_VCODEC_X264;
513 job->vquality = 0.589999973773956;
517 atracks = strdup("1,1");
521 abitrates = strdup("160,auto");
525 arates = strdup("48,Auto");
529 acodecs = strdup("faac,ac3");
533 mixdowns = strdup("dpl2,auto");
538 x264opts = strdup("level=30:cabac=0:ref=3:mixed-refs=1:analyse=all:me=umh:no-fast-pskip=1");
541 job->chapter_markers = 1;
544 if (!strcmp(preset_name, "iPod"))
548 vcodec = HB_VCODEC_X264;
552 atracks = strdup("1");
556 abitrates = strdup("160");
560 arates = strdup("48");
564 acodecs = strdup("faac");
568 mixdowns = strdup("dpl2");
573 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
575 job->chapter_markers = 1;
578 if (!strcmp(preset_name, "iPhone & iPod Touch"))
581 vcodec = HB_VCODEC_X264;
582 job->vquality = 0.589999973773956;
586 atracks = strdup("1");
590 abitrates = strdup("128");
594 arates = strdup("48");
598 acodecs = strdup("faac");
602 mixdowns = strdup("dpl2");
607 x264opts = strdup("level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1");
609 job->chapter_markers = 1;
612 if (!strcmp(preset_name, "AppleTV"))
615 job->largeFileSize = 1;
616 vcodec = HB_VCODEC_X264;
617 job->vquality = 0.589999973773956;
621 atracks = strdup("1,1");
625 abitrates = strdup("160,auto");
629 arates = strdup("48,Auto");
633 acodecs = strdup("faac,ac3");
637 mixdowns = strdup("dpl2,auto");
642 x264opts = strdup("level=30:cabac=0:ref=3:mixed-refs=1:bframes=6:weightb=1:direct=auto:no-fast-pskip=1:me=umh:subq=7:analyse=all");
645 job->chapter_markers = 1;
648 if (!strcmp(preset_name, "QuickTime"))
651 vcodec = HB_VCODEC_X264;
652 job->vbitrate = 1800;
655 atracks = strdup("1");
659 abitrates = strdup("160");
663 arates = strdup("Auto");
667 acodecs = strdup("faac");
671 mixdowns = strdup("dpl2");
675 x264opts = strdup("ref=3:mixed-refs:bframes=3:weightb:direct=auto:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip=1:psy-rd=1,1");
678 job->chapter_markers = 1;
680 turbo_opts_enabled = 1;
683 if (!strcmp(preset_name, "AppleTV Legacy"))
686 job->largeFileSize = 1;
687 vcodec = HB_VCODEC_X264;
688 job->vbitrate = 2500;
691 atracks = strdup("1,1");
695 abitrates = strdup("160,auto");
699 arates = strdup("48,Auto");
703 acodecs = strdup("faac,ac3");
707 mixdowns = strdup("dpl2,auto");
711 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
714 job->chapter_markers = 1;
717 if (!strcmp(preset_name, "iPhone Legacy"))
721 vcodec = HB_VCODEC_X264;
725 atracks = strdup("1");
729 abitrates = strdup("128");
733 arates = strdup("48");
737 acodecs = strdup("faac");
741 mixdowns = strdup("dpl2");
746 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:trellis=1");
748 job->chapter_markers = 1;
751 if (!strcmp(preset_name, "iPod Legacy"))
755 vcodec = HB_VCODEC_X264;
756 job->vbitrate = 1500;
759 atracks = strdup("1");
763 abitrates = strdup("160");
767 arates = strdup("48");
771 acodecs = strdup("faac");
775 mixdowns = strdup("dpl2");
780 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
782 job->chapter_markers = 1;
785 if (!strcmp(preset_name, "Normal"))
788 vcodec = HB_VCODEC_X264;
789 job->vbitrate = 1500;
792 atracks = strdup("1");
796 abitrates = strdup("160");
800 arates = strdup("Auto");
804 acodecs = strdup("faac");
808 mixdowns = strdup("dpl2");
812 x264opts = strdup("ref=2:bframes=2:me=umh");
815 job->chapter_markers = 1;
817 turbo_opts_enabled = 1;
820 if (!strcmp(preset_name, "Classic"))
823 job->vbitrate = 1000;
826 atracks = strdup("1");
830 abitrates = strdup("160");
834 arates = strdup("Auto");
838 acodecs = strdup("faac");
842 mixdowns = strdup("dpl2");
846 if (!strcmp(preset_name, "Animation"))
849 vcodec = HB_VCODEC_X264;
850 job->vbitrate = 1000;
853 atracks = strdup("1");
857 abitrates = strdup("160");
861 arates = strdup("Auto");
865 acodecs = strdup("faac");
869 mixdowns = strdup("dpl2");
873 x264opts = strdup("ref=5:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2:psy-rd=1,1:subme=9");
878 job->chapter_markers = 1;
880 turbo_opts_enabled = 1;
883 if (!strcmp(preset_name, "Constant Quality Rate"))
886 vcodec = HB_VCODEC_X264;
887 job->vquality = 0.600000023841858;
891 atracks = strdup("1");
895 abitrates = strdup("auto");
899 arates = strdup("Auto");
903 acodecs = strdup("ac3");
907 mixdowns = strdup("auto");
911 x264opts = strdup("ref=3:mixed-refs:bframes=3:b-pyramid:weightb:filter=-2,-1:trellis=1:analyse=all:8x8dct:me=umh:subme=9:psy-rd=1,1");
914 job->chapter_markers = 1;
917 if (!strcmp(preset_name, "Film"))
920 vcodec = HB_VCODEC_X264;
921 job->vbitrate = 1800;
924 atracks = strdup("1");
928 abitrates = strdup("auto");
932 arates = strdup("Auto");
936 acodecs = strdup("ac3");
940 mixdowns = strdup("auto");
944 x264opts = strdup("ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:no-fast-pskip:psy-rd=1,1");
947 job->chapter_markers = 1;
949 turbo_opts_enabled = 1;
952 if (!strcmp(preset_name, "Television"))
955 vcodec = HB_VCODEC_X264;
956 job->vbitrate = 1300;
959 atracks = strdup("1");
963 abitrates = strdup("160");
967 arates = strdup("Auto");
971 acodecs = strdup("faac");
975 mixdowns = strdup("dpl2");
979 x264opts = strdup("ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip=1:psy-rd=1,1");
984 job->chapter_markers = 1;
986 turbo_opts_enabled = 1;
989 if (!strcmp(preset_name, "PSP"))
992 job->vbitrate = 1024;
995 atracks = strdup("1");
999 abitrates = strdup("128");
1003 arates = strdup("48");
1007 acodecs = strdup("faac");
1011 mixdowns = strdup("dpl2");
1015 job->chapter_markers = 1;
1018 if (!strcmp(preset_name, "PS3"))
1021 vcodec = HB_VCODEC_X264;
1022 job->vbitrate = 2500;
1025 atracks = strdup("1");
1029 abitrates = strdup("160");
1033 arates = strdup("48");
1037 acodecs = strdup("faac");
1041 mixdowns = strdup("dpl2");
1049 x264opts = strdup("level=41:me=umh");
1054 if (!strcmp(preset_name, "Xbox 360"))
1057 vcodec = HB_VCODEC_X264;
1058 job->vbitrate = 2000;
1061 atracks = strdup("1");
1065 abitrates = strdup("160");
1069 arates = strdup("48");
1073 acodecs = strdup("faac");
1077 mixdowns = strdup("dpl2");
1081 x264opts = strdup("level=40:ref=2:mixed-refs:bframes=3:weightb:subme=9:direct=auto:b-pyramid:me=umh:analyse=all:no-fast-pskip:filter=-2,-1");
1087 if ( chapter_markers )
1089 job->chapter_markers = chapter_markers;
1091 if( marker_file != NULL )
1093 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1094 hb_csv_cell_t * cell;
1098 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1102 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1106 /* Parse the cells */
1107 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1109 /* We have a chapter number */
1110 if( cell->cell_col == 0 )
1112 row = cell->cell_row;
1113 chapter = atoi( cell->cell_text );
1116 /* We have a chapter name */
1117 if( cell->cell_col == 1 && row == cell->cell_row )
1119 /* If we have a valid chapter, copy the string an terminate it */
1120 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1122 hb_chapter_t * chapter_s;
1124 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1125 strncpy(chapter_s->title, cell->cell_text, 1023);
1126 chapter_s->title[1023] = '\0';
1131 hb_dispose_cell( cell );
1134 hb_close_csv_file( file );
1139 /* No marker file */
1141 int number_of_chapters = hb_list_count(job->title->list_chapter);
1144 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1146 hb_chapter_t * chapter_s;
1147 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1148 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1149 chapter_s->title[1023] = '\0';
1154 if( crop[0] >= 0 && crop[1] >= 0 &&
1155 crop[2] >= 0 && crop[3] >= 0 )
1157 memcpy( job->crop, crop, 4 * sizeof( int ) );
1160 job->deinterlace = deinterlace;
1161 job->grayscale = grayscale;
1162 if (loosePixelratio)
1164 job->anamorphic.mode = 2;
1167 job->anamorphic.modulus = modulus;
1169 if( par_width && par_height )
1171 job->anamorphic.mode = 3;
1172 job->anamorphic.par_width = par_width;
1173 job->anamorphic.par_height = par_height;
1178 job->anamorphic.mode = pixelratio;
1181 /* Add selected filters */
1182 job->filters = hb_list_init();
1185 hb_filter_detelecine.settings = detelecine_opt;
1186 hb_list_add( job->filters, &hb_filter_detelecine );
1190 hb_filter_decomb.settings = decomb_opt;
1191 hb_list_add( job->filters, &hb_filter_decomb );
1195 hb_filter_deinterlace.settings = deinterlace_opt;
1196 hb_list_add( job->filters, &hb_filter_deinterlace );
1200 hb_filter_deblock.settings = deblock_opt;
1201 hb_list_add( job->filters, &hb_filter_deblock );
1205 hb_filter_denoise.settings = denoise_opt;
1206 hb_list_add( job->filters, &hb_filter_denoise );
1209 if( width && height )
1212 job->height = height;
1217 hb_fix_aspect( job, HB_KEEP_WIDTH );
1219 else if( height && !loosePixelratio)
1221 job->height = height;
1222 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1224 else if( !width && !height && !pixelratio && !loosePixelratio )
1226 hb_fix_aspect( job, HB_KEEP_WIDTH );
1228 else if (!width && loosePixelratio)
1230 /* Default to full width when one isn't specified for loose anamorphic */
1231 job->width = title->width - job->crop[2] - job->crop[3];
1232 /* The height will be thrown away in hb.c but calculate it anyway */
1233 hb_fix_aspect( job, HB_KEEP_WIDTH );
1236 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1238 job->vquality = vquality;
1243 job->vquality = -1.0;
1244 job->vbitrate = vbitrate;
1248 job->vcodec = vcodec;
1252 job->h264_level = 13;
1256 job->h264_level = 30;
1261 job->vrate = 27000000;
1262 job->vrate_base = vrate;
1266 // cfr or pfr flag with no rate specified implies
1267 // use the title rate.
1269 job->vrate = title->rate;
1270 job->vrate_base = title->rate_base;
1273 /* Grab audio tracks */
1276 char * token = strtok(atracks, ",");
1279 int track_start, track_end;
1280 while( token != NULL )
1282 audio = calloc(1, sizeof(*audio));
1283 hb_audio_config_init(audio);
1284 if (strlen(token) >= 3)
1286 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1289 for (i = track_start - 1; i < track_end; i++)
1291 if (i != track_start - 1)
1293 audio = calloc(1, sizeof(*audio));
1294 hb_audio_config_init(audio);
1296 audio->in.track = i;
1297 audio->out.track = num_audio_tracks++;
1298 hb_list_add(audios, audio);
1301 else if( !strcasecmp(token, "none" ) )
1303 audio->in.track = audio->out.track = -1;
1304 audio->out.codec = 0;
1305 hb_list_add(audios, audio);
1310 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1317 audio->in.track = atoi(token) - 1;
1318 audio->out.track = num_audio_tracks++;
1319 hb_list_add(audios, audio);
1321 token = strtok(NULL, ",");
1325 /* Parse audio tracks */
1326 if( hb_list_count(audios) == 0 )
1328 /* Create a new audio track with default settings */
1329 audio = calloc(1, sizeof(*audio));
1330 hb_audio_config_init(audio);
1331 /* Add it to our audios */
1332 hb_list_add(audios, audio);
1335 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1336 for (i = 0; i < tmp_num_audio_tracks; i++)
1338 audio = hb_list_item(audios, 0);
1339 if( (audio == NULL) || (audio->in.track == -1) ||
1340 (audio->out.track == -1) || (audio->out.codec == 0) )
1346 if( hb_audio_add( job, audio ) == 0 )
1348 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1349 audio->in.track + 1 );
1354 hb_list_rem(audios, audio);
1356 if( audio->out.name )
1358 free( audio->out.name);
1367 char * token = strtok(acodecs, ",");
1370 while ( token != NULL )
1372 if ((acodec = get_acodec_for_string(token)) == -1)
1374 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1375 acodec = default_acodec;
1377 if( i < num_audio_tracks )
1379 audio = hb_list_audio_config_item(job->list_audio, i);
1380 audio->out.codec = acodec;
1384 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1385 hb_audio_config_t audio;
1389 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1391 hb_audio_config_init(&audio);
1392 audio.in.track = last_audio->in.track;
1393 audio.out.track = num_audio_tracks++;
1394 audio.out.codec = acodec;
1395 hb_audio_add(job, &audio);
1399 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1402 token = strtok(NULL, ",");
1406 if( i < num_audio_tracks )
1408 /* We have fewer inputs than audio tracks, use the default codec for
1409 * this container for the remaining tracks. Unless we only have one input
1410 * then use that codec instead.
1413 acodec = default_acodec;
1414 for ( ; i < num_audio_tracks; i++)
1416 audio = hb_list_audio_config_item(job->list_audio, i);
1417 audio->out.codec = acodec;
1426 char * token = strtok(arates, ",");
1429 while ( token != NULL )
1431 arate = atoi(token);
1432 audio = hb_list_audio_config_item(job->list_audio, i);
1435 for( j = 0; j < hb_audio_rates_count; j++ )
1437 if( !strcmp( token, hb_audio_rates[j].string ) )
1439 arate = hb_audio_rates[j].rate;
1446 if (!is_sample_rate_valid(arate))
1448 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1449 arate = audio->in.samplerate;
1452 audio->out.samplerate = arate;
1453 if( (++i) >= num_audio_tracks )
1454 break; /* We have more inputs than audio tracks, oops */
1458 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1460 token = strtok(NULL, ",");
1463 if (i < num_audio_tracks)
1465 /* We have fewer inputs than audio tracks, use default sample rate.
1466 * Unless we only have one input, then use that for all tracks.
1469 arate = audio->in.samplerate;
1470 for ( ; i < num_audio_tracks; i++)
1472 audio = hb_list_audio_config_item(job->list_audio, i);
1473 audio->out.samplerate = arate;
1482 char * token = strtok(abitrates, ",");
1485 while ( token != NULL )
1487 abitrate = atoi(token);
1488 audio = hb_list_audio_config_item(job->list_audio, i);
1492 audio->out.bitrate = abitrate;
1493 if( (++i) >= num_audio_tracks )
1494 break; /* We have more inputs than audio tracks, oops */
1498 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1500 token = strtok(NULL, ",");
1503 if (i < num_audio_tracks)
1505 /* We have fewer inputs than audio tracks, use the default bitrate
1506 * for the remaining tracks. Unless we only have one input, then use
1507 * that for all tracks.
1510 abitrate = default_abitrate;
1511 for (; i < num_audio_tracks; i++)
1513 audio = hb_list_audio_config_item(job->list_audio, i);
1514 audio->out.bitrate = abitrate;
1521 if ( dynamic_range_compression )
1523 char * token = strtok(dynamic_range_compression, ",");
1525 token = dynamic_range_compression;
1526 while ( token != NULL )
1528 d_r_c = atof(token);
1529 audio = hb_list_audio_config_item(job->list_audio, i);
1532 audio->out.dynamic_range_compression = d_r_c;
1533 if( (++i) >= num_audio_tracks )
1534 break; /* We have more inputs than audio tracks, oops */
1538 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1540 token = strtok(NULL, ",");
1543 if (i < num_audio_tracks)
1545 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1546 * tracks. Unless we only have one input, then use the same DRC for all
1551 for (; i < num_audio_tracks; i++)
1553 audio = hb_list_audio_config_item(job->list_audio, i);
1554 audio->out.dynamic_range_compression = d_r_c;
1563 char * token = strtok(mixdowns, ",");
1566 while ( token != NULL )
1568 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1569 audio = hb_list_audio_config_item(job->list_audio, i);
1572 audio->out.mixdown = mixdown;
1573 if( (++i) >= num_audio_tracks )
1574 break; /* We have more inputs than audio tracks, oops */
1578 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1580 token = strtok(NULL, ",");
1583 if (i < num_audio_tracks)
1585 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1586 * we only have one input, then use that.
1589 mixdown = HB_AMIXDOWN_DOLBYPLII;
1590 for (; i < num_audio_tracks; i++)
1592 audio = hb_list_audio_config_item(job->list_audio, i);
1593 audio->out.mixdown = mixdown;
1598 /* Audio Track Names */
1602 char * token = strtok(anames, ",");
1605 while ( token != NULL )
1607 audio = hb_list_audio_config_item(job->list_audio, i);
1610 audio->out.name = strdup(token);
1611 if( (++i) >= num_audio_tracks )
1612 break; /* We have more names than audio tracks, oops */
1616 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1619 token = strtok(NULL, ",");
1622 if( i < num_audio_tracks && i == 1 )
1624 /* We have exactly one name and more than one audio track. Use the same
1625 * name for all tracks. */
1626 for ( ; i < num_audio_tracks; i++)
1628 audio = hb_list_audio_config_item(job->list_audio, i);
1629 audio->out.name = strdup(anames);
1632 /* Audio Track Names */
1636 job->vbitrate = hb_calc_bitrate( job, size );
1637 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1643 hb_subtitle_t *subtitle;
1645 * Find the subtitle with the same track as "sub" and
1646 * add that to the job subtitle list
1648 subtitle = hb_list_item( title->list_subtitle, sub-1 );
1650 hb_list_add( job->list_subtitle, subtitle );
1652 fprintf( stderr, "Could not find subtitle track %d, skipped\n", sub );
1656 if( native_language )
1658 job->native_language = strdup( native_language );
1666 if ( largeFileSize )
1668 job->largeFileSize = 1;
1672 job->mp4_optimize = 1;
1679 job->file = strdup( output );
1688 job->color_matrix = color_matrix;
1691 if( x264opts != NULL && *x264opts != '\0' )
1693 job->x264opts = x264opts;
1695 else /*avoids a bus error crash when options aren't specified*/
1697 job->x264opts = NULL;
1700 job->maxWidth = maxWidth;
1702 job->maxHeight = maxHeight;
1704 if( subtitle_force )
1706 job->subtitle_force = subtitle_force;
1709 if( start_at_preview )
1711 job->start_at_preview = start_at_preview - 1;
1712 job->seek_points = preview_count;
1717 job->pts_to_stop = stop_at_pts;
1723 job->frame_to_stop = stop_at_frame;
1732 * When subtitle scan is enabled do a fast pre-scan job
1733 * which will determine which subtitles to enable, if any.
1737 x264opts_tmp = job->x264opts;
1739 job->x264opts = NULL;
1741 job->indepth_scan = subtitle_scan;
1742 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1743 "subtitles if found for foreign language segments\n");
1744 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1745 *(job->select_subtitle) = NULL;
1748 * Add the pre-scan job
1752 job->x264opts = x264opts_tmp;
1758 * If subtitle_scan is enabled then only turn it on
1759 * for the first pass and then off again for the
1762 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1764 job->select_subtitle = NULL;
1768 job->indepth_scan = 0;
1772 x264opts2 = strdup(x264opts);
1776 * If turbo options have been selected then append them
1777 * to the x264opts now (size includes one ':' and the '\0')
1779 if( turbo_opts_enabled )
1781 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1784 tmp_x264opts = malloc(size * sizeof(char));
1787 snprintf( tmp_x264opts, size, "%s:%s",
1788 x264opts, turbo_opts );
1792 * No x264opts to modify, but apply the turbo options
1793 * anyway as they may be modifying defaults
1795 snprintf( tmp_x264opts, size, "%s",
1798 x264opts = tmp_x264opts;
1800 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1803 job->x264opts = x264opts;
1807 job->select_subtitle = subtitle_tmp;
1811 * On the second pass we turn off subtitle scan so that we
1812 * can actually encode using any subtitles that were auto
1813 * selected in the first pass (using the whacky select-subtitle
1814 * attribute of the job).
1816 job->indepth_scan = 0;
1818 job->x264opts = x264opts2;
1825 * Turn on subtitle scan if requested, note that this option
1826 * precludes encoding of any actual subtitles.
1829 job->indepth_scan = 0;
1837 #define p s.param.working
1838 case HB_STATE_WORKING:
1839 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1840 p.job_cur, p.job_count, 100.0 * p.progress );
1841 if( p.seconds > -1 )
1843 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1844 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1845 p.hours, p.minutes, p.seconds );
1851 #define p s.param.muxing
1852 case HB_STATE_MUXING:
1854 if (show_mux_warning)
1856 fprintf( stdout, "\rMuxing: this may take awhile..." );
1858 show_mux_warning = 0;
1864 #define p s.param.workdone
1865 case HB_STATE_WORKDONE:
1866 /* Print error if any, then exit */
1870 fprintf( stderr, "\nRip done!\n" );
1872 case HB_ERROR_CANCELED:
1873 fprintf( stderr, "\nRip canceled.\n" );
1876 fprintf( stderr, "\nRip failed (error %x).\n",
1886 /****************************************************************************
1888 ****************************************************************************/
1889 static volatile int64_t i_die_date = 0;
1890 void SigHandler( int i_signal )
1895 i_die_date = hb_get_date();
1896 fprintf( stderr, "Signal %d received, terminating - do it "
1897 "again in case it gets stuck\n", i_signal );
1899 else if( i_die_date + 500 < hb_get_date() )
1901 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1906 /****************************************************************************
1908 ****************************************************************************/
1909 static void ShowHelp()
1912 FILE* const out = stdout;
1915 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1917 "### General Handbrake Options------------------------------------------------\n\n"
1918 " -h, --help Print help\n"
1919 " -u, --update Check for updates and exit\n"
1920 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1921 " -C, --cpu Set CPU count (default: autodetected)\n"
1922 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1923 " if the preset name has spaces, surround it with\n"
1924 " double quotation marks\n"
1925 " -z, --preset-list See a list of available built-in presets\n"
1926 " --dvdnav Use dvdnav (Experimental)\n"
1929 "### Source Options-----------------------------------------------------------\n\n"
1930 " -i, --input <string> Set input device\n"
1931 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1933 " -L, --longest Select the longest title\n"
1934 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1935 " 1 to 3, or \"3\" for chapter 3 only,\n"
1936 " default: all chapters)\n"
1937 " --angle <number> Select the DVD angle\n"
1938 " --previews <#:B> Select how many preview images are generated (max 30),\n"
1939 " and whether or not they're stored to disk (0 or 1).\n"
1940 " (default: 10:0)\n"
1941 " --start-at-preview <#> Start encoding at a given preview.\n"
1942 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
1943 " or pts (on a 90kHz clock)"
1946 "### Destination Options------------------------------------------------------\n\n"
1947 " -o, --output <string> Set output file name\n"
1948 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1949 " autodetected from file name)\n"
1950 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1951 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1952 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1953 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1954 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1958 "### Video Options------------------------------------------------------------\n\n"
1959 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1960 " x264,theora default: ffmpeg)\n"
1961 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1962 " same style as mencoder:\n"
1963 " option1=value1:option2=value2\n"
1964 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1965 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1966 " -S, --size <MB> Set target size\n"
1967 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1968 " -2, --two-pass Use two-pass mode\n"
1969 " -T, --turbo When using 2-pass use the turbo options\n"
1970 " on the first pass to improve speed\n"
1971 " (only works with x264, affects PSNR by about 0.05dB,\n"
1972 " and increases first pass speed two to four times)\n"
1973 " -r, --rate Set video framerate (" );
1974 for( i = 0; i < hb_video_rates_count; i++ )
1976 fprintf( out, hb_video_rates[i].string );
1977 if( i != hb_video_rates_count - 1 )
1978 fprintf( out, "/" );
1981 " Be aware that not specifying a framerate lets\n"
1982 " HandBrake preserve a source's time stamps,\n"
1983 " potentially creating variable framerate video\n"
1984 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
1985 " frame rate control. VFR preserves the source\n"
1986 " timing. CFR makes the output constant rate at\n"
1987 " the rate given by the -r flag (or the source's\n"
1988 " average rate if no -r is given). PFR doesn't\n"
1989 " allow the rate to go over the rate specified\n"
1990 " with the -r flag but won't change the source\n"
1991 " timing if it's below that rate.\n"
1992 " If none of these flags are given, the default\n"
1993 " is --cfr when -r is given and --vfr otherwise\n"
1996 "### Audio Options-----------------------------------------------------------\n\n"
1997 " -a, --audio <string> Select audio track(s), separated by commas\n"
1998 " More than one output track can be used for one\n"
2000 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2001 " tracks, default: first one)\n"
2002 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2003 " ac3 and dts meaning passthrough\n"
2004 " Separated by commas for more than one audio track.\n"
2005 " (default: guessed)\n"
2006 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2007 " Separated by commas for more than one audio track.\n"
2008 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2009 " Separated by commas for more than one audio track.\n"
2010 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2011 " -R, --arate Set audio samplerate(s) (" );
2012 for( i = 0; i < hb_audio_rates_count; i++ )
2014 fprintf( out, hb_audio_rates[i].string );
2015 if( i != hb_audio_rates_count - 1 )
2016 fprintf( out, "/" );
2018 fprintf( out, " kHz)\n"
2019 " Separated by commas for more than one audio track.\n"
2020 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2021 " making soft sounds louder. Range is 1.0 to 4.0\n"
2022 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2023 " Separated by commas for more than one audio track.\n"
2024 " -A, --aname <string> Audio track name(s),\n"
2025 " Separated by commas for more than one audio track.\n"
2028 "### Picture Settings---------------------------------------------------------\n\n"
2029 " -w, --width <number> Set picture width\n"
2030 " -l, --height <number> Set picture height\n"
2031 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2032 " -Y, --maxHeight <#> Set maximum height\n"
2033 " -X, --maxWidth <#> Set maximum width\n"
2034 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
2035 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
2036 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
2037 " the dimensions to divide cleanly by (default 16)\n"
2038 " and the pixel ratio to use (default autodetected)\n"
2039 " -M --color-matrix Set the color space signaled by the output\n"
2040 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2041 " default: set by resolution)\n"
2044 "### Filters---------------------------------------------------------\n\n"
2046 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2047 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2049 " <fast/slow/slower>\n"
2050 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2051 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2052 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2053 " Note: this filter drops duplicate frames to\n"
2054 " restore the pre-telecine framerate, unless you\n"
2055 " specify a constant framerate (--rate 29.97)\n"
2056 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2057 " -8, --denoise Denoise video with hqdn3d filter\n"
2058 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2060 " <weak/medium/strong>\n"
2061 " -7, --deblock Deblock video with pp7 filter\n"
2062 " <QP:M> (default 5:2)\n"
2063 " -g, --grayscale Grayscale encoding\n"
2066 "### Subtitle Options------------------------------------------------------------\n\n"
2067 " -s, --subtitle <number> Select subtitle (default: none)\n"
2068 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
2069 " the one that's only used 10 percent of the time\n"
2070 " or less. This should locate subtitles for short\n"
2071 " foreign language segments. Best used in conjunction\n"
2072 " with --subtitle-forced.\n"
2073 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2074 " the subtitle has the forced flag set. May be used in\n"
2075 " conjunction with --subtitle-scan to auto-select\n"
2076 " a stream if it contains forced subtitles.\n"
2077 " -N, --native-language Select subtitles with this language if it does not\n"
2078 " <string> match the Audio language. Provide the language's\n"
2079 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2088 /****************************************************************************
2090 ****************************************************************************/
2091 static void ShowPresets()
2093 printf("\n< Apple\n");
2095 printf("\n + Universal: -e x264 -q 0.589999973773956 -a 1,1 -E faac,ac3 -B 160,auto -R 48,Auto -6 dpl2,auto -f mp4 -X 720 -P -m -x level=30:cabac=0:ref=3:mixed-refs=1:analyse=all:me=umh:no-fast-pskip=1\n");
2097 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -R 48 -6 dpl2 -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");
2099 printf("\n + iPhone & iPod Touch: -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1\n");
2101 printf("\n + AppleTV: -e x264 -q 0.589999973773956 -a 1,1 -E faac,ac3 -B 160,auto -R 48,Auto -6 dpl2,auto -f mp4 -4 -X 960 -P -m -x level=30:cabac=0:ref=3:mixed-refs=1:bframes=6:weightb=1:direct=auto:no-fast-pskip=1:me=umh:subq=7:analyse=all\n");
2103 printf("\n + QuickTime: -e x264 -b 1800 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4 -p -m -2 -T -x ref=3:mixed-refs:bframes=3:weightb:direct=auto:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip=1:psy-rd=1,1\n");
2105 printf("\n << Legacy\n");
2107 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,auto -R 48,Auto -6 dpl2,auto -f mp4 -4 -p -m -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0\n");
2109 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -I -X 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:trellis=1\n");
2111 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -R 48 -6 dpl2 -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\n");
2117 printf("\n< Basic\n");
2119 printf("\n + Normal: -e x264 -b 1500 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4 -p -m -2 -T -x ref=2:bframes=2:me=umh\n");
2121 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4\n");
2125 printf("\n< High Profile\n");
2127 printf("\n + Animation: -e x264 -b 1000 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mkv --detelecine --decomb -p -m -2 -T -x ref=5:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2:psy-rd=1,1:subme=9\n");
2129 printf("\n + Constant Quality Rate: -e x264 -q 0.600000023841858 -a 1 -E ac3 -B 160 -R Auto -6 auto -f mkv -p -m -x ref=3:mixed-refs:bframes=3:b-pyramid:weightb:filter=-2,-1:trellis=1:analyse=all:8x8dct:me=umh:subme=9:psy-rd=1,1\n");
2131 printf("\n + Film: -e x264 -b 1800 -a 1 -E ac3 -B 160 -R Auto -6 auto -f mkv -p -m -2 -T -x ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:no-fast-pskip:psy-rd=1,1\n");
2133 printf("\n + Television: -e x264 -b 1300 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mkv --detelecine --decomb -p -m -2 -T -x ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip=1:psy-rd=1,1\n");
2137 printf("\n< Gaming Consoles\n");
2139 printf("\n + PSP: -b 1024 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 368 -Y 208 -m\n");
2141 printf("\n + PS3: -e x264 -b 2500 -a 1 -E faac -B 160 -R 48 -6 dpl2 -f mp4 --crop 0:0:0:0 -p -x level=41:me=umh\n");
2143 printf("\n + Xbox 360: -e x264 -b 2000 -a 1 -E faac -B 160 -R 48 -6 dpl2 -f mp4 -p -x level=40:ref=2:mixed-refs:bframes=3:weightb:subme=9:direct=auto:b-pyramid:me=umh:analyse=all:no-fast-pskip:filter=-2,-1\n");
2149 /****************************************************************************
2151 ****************************************************************************/
2152 static int ParseOptions( int argc, char ** argv )
2155 #define PREVIEWS 257
2156 #define START_AT_PREVIEW 258
2163 static struct option long_options[] =
2165 { "help", no_argument, NULL, 'h' },
2166 { "update", no_argument, NULL, 'u' },
2167 { "verbose", optional_argument, NULL, 'v' },
2168 { "cpu", required_argument, NULL, 'C' },
2169 { "dvdnav", no_argument, NULL, DVDNAV },
2171 { "format", required_argument, NULL, 'f' },
2172 { "input", required_argument, NULL, 'i' },
2173 { "output", required_argument, NULL, 'o' },
2174 { "large-file", no_argument, NULL, '4' },
2175 { "optimize", no_argument, NULL, 'O' },
2176 { "ipod-atom", no_argument, NULL, 'I' },
2178 { "title", required_argument, NULL, 't' },
2179 { "longest", no_argument, NULL, 'L' },
2180 { "chapters", required_argument, NULL, 'c' },
2181 { "angle", required_argument, NULL, ANGLE },
2182 { "markers", optional_argument, NULL, 'm' },
2183 { "audio", required_argument, NULL, 'a' },
2184 { "mixdown", required_argument, NULL, '6' },
2185 { "drc", required_argument, NULL, 'D' },
2186 { "subtitle", required_argument, NULL, 's' },
2187 { "subtitle-scan", no_argument, NULL, 'U' },
2188 { "subtitle-forced", no_argument, NULL, 'F' },
2189 { "native-language", required_argument, NULL,'N' },
2191 { "encoder", required_argument, NULL, 'e' },
2192 { "aencoder", required_argument, NULL, 'E' },
2193 { "two-pass", no_argument, NULL, '2' },
2194 { "deinterlace", optional_argument, NULL, 'd' },
2195 { "deblock", optional_argument, NULL, '7' },
2196 { "denoise", optional_argument, NULL, '8' },
2197 { "detelecine", optional_argument, NULL, '9' },
2198 { "decomb", optional_argument, NULL, '5' },
2199 { "grayscale", no_argument, NULL, 'g' },
2200 { "pixelratio", no_argument, NULL, 'p' },
2201 { "loosePixelratio", optional_argument, NULL, 'P' },
2202 { "width", required_argument, NULL, 'w' },
2203 { "height", required_argument, NULL, 'l' },
2204 { "crop", required_argument, NULL, 'n' },
2206 { "vb", required_argument, NULL, 'b' },
2207 { "quality", required_argument, NULL, 'q' },
2208 { "size", required_argument, NULL, 'S' },
2209 { "ab", required_argument, NULL, 'B' },
2210 { "rate", required_argument, NULL, 'r' },
2211 { "arate", required_argument, NULL, 'R' },
2212 { "cqp", no_argument, NULL, 'Q' },
2213 { "x264opts", required_argument, NULL, 'x' },
2214 { "turbo", no_argument, NULL, 'T' },
2215 { "maxHeight", required_argument, NULL, 'Y' },
2216 { "maxWidth", required_argument, NULL, 'X' },
2217 { "preset", required_argument, NULL, 'Z' },
2218 { "preset-list", no_argument, NULL, 'z' },
2220 { "aname", required_argument, NULL, 'A' },
2221 { "color-matrix",required_argument, NULL, 'M' },
2222 { "previews", required_argument, NULL, PREVIEWS },
2223 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2224 { "stop-at", required_argument, NULL, STOP_AT },
2225 { "vfr", no_argument, &cfr, 0 },
2226 { "cfr", no_argument, &cfr, 1 },
2227 { "pfr", no_argument, &cfr, 2 },
2231 int option_index = 0;
2234 c = getopt_long( argc, argv,
2235 "hv::uC:f:4i:Io:t:Lc:m::M:a:A:6:s:UFN:e:E:2dD:7895gpOP::w:l:n:b:q:S:B:r:R:Qx:TY:X:Z:z",
2236 long_options, &option_index );
2245 /* option was handled entirely in getopt_long */
2254 if( optarg != NULL )
2256 debug = atoi( optarg );
2264 cpu = atoi( optarg );
2269 preset_name = strdup(optarg);
2279 format = strdup( optarg );
2282 input = strdup( optarg );
2284 char *devName = bsd_name_for_path( input ); // alloc
2287 if( device_is_dvd( devName ))
2290 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2291 sprintf( input, "/dev/%s", devName );
2298 output = strdup( optarg );
2311 titleindex = atoi( optarg );
2319 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2321 chapter_start = start;
2324 else if( sscanf( optarg, "%d", &start ) == 1 )
2326 chapter_start = start;
2327 chapter_end = chapter_start;
2331 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2338 angle = atoi( optarg );
2341 if( optarg != NULL )
2343 marker_file = strdup( optarg );
2345 chapter_markers = 1;
2348 if( optarg != NULL )
2350 atracks = strdup( optarg );
2358 if( optarg != NULL )
2360 mixdowns = strdup( optarg );
2364 if( optarg != NULL )
2366 dynamic_range_compression = strdup( optarg );
2370 sub = atoi( optarg );
2379 native_language = strdup( optarg );
2385 if( optarg != NULL )
2387 if (!( strcmp( optarg, "fast" ) ))
2389 deinterlace_opt = "-1";
2391 else if (!( strcmp( optarg, "slow" ) ))
2393 deinterlace_opt = "2";
2395 else if (!( strcmp( optarg, "slower" ) ))
2397 deinterlace_opt = "0";
2401 deinterlace_opt = strdup( optarg );
2407 if( optarg != NULL )
2409 deblock_opt = strdup( optarg );
2414 if( optarg != NULL )
2416 if (!( strcmp( optarg, "weak" ) ))
2418 denoise_opt = "2:1:2:3";
2420 else if (!( strcmp( optarg, "medium" ) ))
2422 denoise_opt = "3:2:2:3";
2424 else if (!( strcmp( optarg, "strong" ) ))
2426 denoise_opt = "7:7:5:5";
2430 denoise_opt = strdup( optarg );
2436 if( optarg != NULL )
2438 detelecine_opt = strdup( optarg );
2443 if( optarg != NULL )
2445 decomb_opt = strdup( optarg );
2456 loosePixelratio = 1;
2457 if( optarg != NULL )
2459 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
2463 if( !strcasecmp( optarg, "ffmpeg" ) )
2465 vcodec = HB_VCODEC_FFMPEG;
2467 else if( !strcasecmp( optarg, "xvid" ) )
2469 vcodec = HB_VCODEC_XVID;
2471 else if( !strcasecmp( optarg, "x264" ) )
2473 vcodec = HB_VCODEC_X264;
2475 else if( !strcasecmp( optarg, "x264b13" ) )
2477 vcodec = HB_VCODEC_X264;
2480 else if( !strcasecmp( optarg, "x264b30" ) )
2482 vcodec = HB_VCODEC_X264;
2485 else if( !strcasecmp( optarg, "theora" ) )
2487 vcodec = HB_VCODEC_THEORA;
2491 fprintf( stderr, "invalid codec (%s)\n", optarg );
2496 if( optarg != NULL )
2498 acodecs = strdup( optarg );
2502 width = atoi( optarg );
2505 height = atoi( optarg );
2510 char * tmp = optarg;
2511 for( i = 0; i < 4; i++ )
2515 crop[i] = strtol( tmp, &tmp, 0 );
2524 for( i = 0; i < hb_video_rates_count; i++ )
2526 if( !strcmp( optarg, hb_video_rates[i].string ) )
2528 vrate = hb_video_rates[i].rate;
2534 fprintf( stderr, "invalid framerate %s\n", optarg );
2536 else if ( cfr == 0 )
2543 if( optarg != NULL )
2545 arates = strdup( optarg );
2549 vbitrate = atoi( optarg );
2552 vquality = atof( optarg );
2555 size = atoi( optarg );
2558 if( optarg != NULL )
2560 abitrates = strdup( optarg );
2567 x264opts = strdup( optarg );
2570 turbo_opts_enabled = 1;
2573 maxHeight = atoi( optarg );
2576 maxWidth = atoi (optarg );
2579 if( optarg != NULL )
2581 anames = strdup( optarg );
2585 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2587 case START_AT_PREVIEW:
2588 start_at_preview = atoi( optarg );
2591 stop_at_string = strdup( optarg );
2592 stop_at_token = strtok( stop_at_string, ":");
2593 if( !strcmp( stop_at_token, "frame" ) )
2595 stop_at_token = strtok( NULL, ":");
2596 stop_at_frame = atoi(stop_at_token);
2598 else if( !strcmp( stop_at_token, "pts" ) )
2600 stop_at_token = strtok( NULL, ":");
2601 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2603 else if( !strcmp( stop_at_token, "duration" ) )
2605 stop_at_token = strtok( NULL, ":");
2606 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2607 stop_at_pts *= 90000LL;
2611 if( atoi( optarg ) == 601 )
2613 else if( atoi( optarg ) == 709 )
2617 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2625 static int CheckOptions( int argc, char ** argv )
2632 if( input == NULL || *input == '\0' )
2634 fprintf( stderr, "Missing input device. Run %s --help for "
2635 "syntax.\n", argv[0] );
2640 if( titleindex > 0 )
2642 if( output == NULL || *output == '\0' )
2644 fprintf( stderr, "Missing output file name. Run %s --help "
2645 "for syntax.\n", argv[0] );
2651 char * p = strrchr( output, '.' );
2654 if( p && !strcasecmp( p, ".avi" ) )
2657 default_acodec = HB_ACODEC_LAME;
2659 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2660 !strcasecmp( p, ".m4v" ) ) )
2666 default_acodec = HB_ACODEC_FAAC;
2668 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2669 !strcasecmp( p, ".ogg" ) ) )
2672 default_acodec = HB_ACODEC_VORBIS;
2674 else if( p && !strcasecmp(p, ".mkv" ) )
2677 default_acodec = HB_ACODEC_AC3;
2681 fprintf( stderr, "Output format couldn't be guessed "
2682 "from file name, using default.\n" );
2686 else if( !strcasecmp( format, "avi" ) )
2689 default_acodec = HB_ACODEC_LAME;
2691 else if( !strcasecmp( format, "mp4" ) ||
2692 !strcasecmp( format, "m4v" ) )
2698 default_acodec = HB_ACODEC_FAAC;
2700 else if( !strcasecmp( format, "ogm" ) ||
2701 !strcasecmp( format, "ogg" ) )
2704 default_acodec = HB_ACODEC_VORBIS;
2706 else if( !strcasecmp( format, "mkv" ) )
2709 default_acodec = HB_ACODEC_AC3;
2713 fprintf( stderr, "Invalid output format (%s). Possible "
2714 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2722 static int get_acodec_for_string( char *codec )
2724 if( !strcasecmp( codec, "ac3" ) )
2726 return HB_ACODEC_AC3;
2728 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2730 return HB_ACODEC_DCA;
2732 else if( !strcasecmp( codec, "lame" ) )
2734 return HB_ACODEC_LAME;
2736 else if( !strcasecmp( codec, "faac" ) )
2738 return HB_ACODEC_FAAC;
2740 else if( !strcasecmp( codec, "vorbis") )
2742 return HB_ACODEC_VORBIS;
2750 static int is_sample_rate_valid(int rate)
2753 for( i = 0; i < hb_audio_rates_count; i++ )
2755 if (rate == hb_audio_rates[i].rate)
2762 /****************************************************************************
2765 * Returns the BSD device name for the block device that contains the
2766 * passed-in path. Returns NULL on failure.
2767 ****************************************************************************/
2768 static char* bsd_name_for_path(char *path)
2772 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2778 // Get the volume reference number.
2779 FSCatalogInfo catalogInfo;
2780 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2786 FSVolumeRefNum volRefNum = catalogInfo.volume;
2788 // Now let's get the device name
2789 GetVolParmsInfoBuffer volumeParms;
2790 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2796 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
2797 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
2798 if( volumeParms.vMVersion < 4 )
2803 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
2804 if( !volumeParms.vMDeviceID )
2809 return strdup( volumeParms.vMDeviceID );
2812 /****************************************************************************
2815 * Returns whether or not the passed in BSD device represents a DVD, or other
2817 ****************************************************************************/
2818 static int device_is_dvd(char *device)
2820 io_service_t service = get_iokit_service(device);
2821 if( service == IO_OBJECT_NULL )
2825 int result = is_dvd_service(service);
2826 IOObjectRelease(service);
2830 /****************************************************************************
2833 * Returns the IOKit service object for the passed in BSD device name.
2834 ****************************************************************************/
2835 static io_service_t get_iokit_service( char *device )
2837 CFMutableDictionaryRef matchingDict;
2838 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2839 if( matchingDict == NULL )
2841 return IO_OBJECT_NULL;
2843 // Fetch the object with the matching BSD node name. There should only be
2844 // one match, so IOServiceGetMatchingService is used instead of
2845 // IOServiceGetMatchingServices to simplify the code.
2846 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2849 /****************************************************************************
2852 * Returns whether or not the service passed in is a DVD.
2854 * Searches for an IOMedia object that represents the entire (whole) media that
2855 * the volume is on. If the volume is on partitioned media, the whole media
2856 * object will be a parent of the volume's media object. If the media is not
2857 * partitioned, the volume's media object will be the whole media object.
2858 ****************************************************************************/
2859 static int is_dvd_service( io_service_t service )
2861 kern_return_t kernResult;
2864 // Create an iterator across all parents of the service object passed in.
2865 kernResult = IORegistryEntryCreateIterator( service,
2867 kIORegistryIterateRecursively | kIORegistryIterateParents,
2869 if( kernResult != KERN_SUCCESS )
2873 if( iter == IO_OBJECT_NULL )
2878 // A reference on the initial service object is released in the do-while
2879 // loop below, so add a reference to balance.
2880 IOObjectRetain( service );
2885 if( is_whole_media_service( service ) &&
2886 IOObjectConformsTo( service, kIODVDMediaClass) )
2890 IOObjectRelease( service );
2891 } while( !result && (service = IOIteratorNext( iter )) );
2892 IOObjectRelease( iter );
2897 /****************************************************************************
2898 * is_whole_media_service
2900 * Returns whether or not the service passed in is an IOMedia service and
2901 * represents the "whole" media instead of just a partition.
2903 * The whole media object is indicated in the IORegistry by the presence of a
2904 * property with the key "Whole" and value "Yes".
2905 ****************************************************************************/
2906 static is_whole_media_service( io_service_t service )
2910 if( IOObjectConformsTo( service, kIOMediaClass ) )
2912 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2913 CFSTR( kIOMediaWholeKey ),
2914 kCFAllocatorDefault,
2920 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2921 CFRelease( wholeMedia );
2926 #endif // __APPLE_CC__