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 anamorphic_mode = 0;
75 static int modulus = 0;
76 static int par_height = 0;
77 static int par_width = 0;
78 static int display_width = 0;
79 static int keep_display_aspect = 0;
80 static int itu_par = 0;
82 static int chapter_start = 0;
83 static int chapter_end = 0;
84 static int chapter_markers = 0;
85 static char * marker_file = NULL;
87 static char *x264opts = NULL;
88 static char *x264opts2 = NULL;
89 static int maxHeight = 0;
90 static int maxWidth = 0;
91 static int turbo_opts_enabled = 0;
92 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
93 static int largeFileSize = 0;
94 static int preset = 0;
95 static char * preset_name = 0;
97 static int mp4_optimize = 0;
98 static int ipod_atom = 0;
99 static int color_matrix = 0;
100 static int preview_count = 10;
101 static int store_previews = 0;
102 static int start_at_preview = 0;
103 static int64_t stop_at_pts = 0;
104 static int stop_at_frame = 0;
105 static char * stop_at_string = NULL;
106 static char * stop_at_token = NULL;
108 /* Exit cleanly on Ctrl-C */
109 static volatile int die = 0;
110 static void SigHandler( int );
113 static void ShowCommands();
114 static void ShowHelp();
115 static void ShowPresets();
117 static int ParseOptions( int argc, char ** argv );
118 static int CheckOptions( int argc, char ** argv );
119 static int HandleEvents( hb_handle_t * h );
121 static int get_acodec_for_string( char *codec );
122 static int is_sample_rate_valid(int rate);
125 static char* bsd_name_for_path(char *path);
126 static int device_is_dvd(char *device);
127 static io_service_t get_iokit_service( char *device );
128 static int is_dvd_service( io_service_t service );
129 static is_whole_media_service( io_service_t service );
132 /* Only print the "Muxing..." message once */
133 static int show_mux_warning = 1;
135 /****************************************************************************
138 * When using the CLI just display using hb_log as we always did in the past
139 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
140 ****************************************************************************/
141 static void hb_cli_error_handler ( const char *errmsg )
143 fprintf( stderr, "ERROR: %s\n", errmsg );
146 int main( int argc, char ** argv )
152 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
153 * force it to unbuffered otherwise informative output is not easily parsed.
155 #if defined( _WIN32 ) || defined( __MINGW32__ )
156 setvbuf( stdout, NULL, _IONBF, 0 );
157 setvbuf( stderr, NULL, _IONBF, 0 );
160 audios = hb_list_init();
162 /* Parse command line */
163 if( ParseOptions( argc, argv ) ||
164 CheckOptions( argc, argv ) )
169 #ifdef PTW32_STATIC_LIB
170 pthread_win32_process_attach_np();
171 pthread_win32_thread_attach_np();
174 /* Register our error handler */
175 hb_register_error_handler(&hb_cli_error_handler);
178 h = hb_init( debug, update );
179 hb_dvd_set_dvdnav( dvdnav );
182 fprintf( stderr, "%s - %s - %s\n",
183 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
185 /* Check for update */
188 if( ( build = hb_check_update( h, &version ) ) > -1 )
190 fprintf( stderr, "You are using an old version of "
191 "HandBrake.\nLatest is %s (build %d).\n", version,
196 fprintf( stderr, "Your version of HandBrake is up to "
204 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
205 hb_get_cpu_count( h ) > 1 ? "s" : "" );
208 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
209 cpu > 1 ? "s" : "" );
210 hb_set_cpu_count( h, cpu );
213 /* Exit ASAP on Ctrl-C */
214 signal( SIGINT, SigHandler );
216 /* Feed libhb with a DVD to scan */
217 fprintf( stderr, "Opening %s...\n", input );
221 * We need to scan for all the titles in order to find the longest
226 hb_scan( h, input, titleindex, preview_count, store_previews );
231 #if !defined(SYS_BEOS) && !defined(__MINGW32__)
241 FD_SET( STDIN_FILENO, &fds );
242 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
249 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
251 if( buf[size] == '\n' )
258 if( size >= 256 || buf[size] == '\n' )
263 fprintf( stdout, "\nEncoding Quit by user command\n" );
267 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
289 if( input ) free( input );
290 if( output ) free( output );
291 if( format ) free( format );
294 while( ( audio = hb_list_item( audios, 0 ) ) )
296 hb_list_rem( audios, audio );
297 if( audio->out.name )
299 free( audio->out.name );
303 hb_list_close( &audios );
305 if( mixdowns ) free( mixdowns );
306 if( dynamic_range_compression ) free( dynamic_range_compression );
307 if( atracks ) free( atracks );
308 if( arates ) free( arates );
309 if( abitrates ) free( abitrates );
310 if( acodecs ) free( acodecs );
311 if( anames ) free( anames );
312 if (native_language ) free (native_language );
313 if( x264opts ) free (x264opts );
314 if( x264opts2 ) free (x264opts2 );
315 if (preset_name) free (preset_name);
316 if( stop_at_string ) free( stop_at_string );
318 fprintf( stderr, "HandBrake has exited.\n" );
320 #ifdef PTW32_STATIC_LIB
321 pthread_win32_thread_detach_np();
322 pthread_win32_process_detach_np();
328 static void ShowCommands()
330 fprintf( stdout, "\nCommands:\n" );
331 fprintf( stdout, " [h]elp Show this message\n" );
332 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
333 fprintf( stdout, " [p]ause Pause encoding\n" );
334 fprintf( stdout, " [r]esume Resume encoding\n" );
337 static void PrintTitleInfo( hb_title_t * title )
339 hb_chapter_t * chapter;
340 hb_audio_config_t * audio;
341 hb_subtitle_t * subtitle;
344 fprintf( stderr, "+ title %d:\n", title->index );
345 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
346 title->vts, title->ttn, title->cell_start, title->cell_end,
347 title->block_count );
349 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
350 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
351 title->hours, title->minutes, title->seconds );
352 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
353 title->width, title->height,
354 title->pixel_aspect_width,
355 title->pixel_aspect_height,
356 (float) title->aspect,
357 (float) title->rate / title->rate_base );
358 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
359 title->crop[1], title->crop[2], title->crop[3] );
360 fprintf( stderr, " + chapters:\n" );
361 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
363 chapter = hb_list_item( title->list_chapter, i );
364 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
365 "%02d:%02d:%02d\n", chapter->index,
366 chapter->cell_start, chapter->cell_end,
367 chapter->block_count, chapter->hours, chapter->minutes,
370 fprintf( stderr, " + audio tracks:\n" );
371 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
373 audio = hb_list_audio_config_item( title->list_audio, i );
374 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
376 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
377 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
381 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
384 fprintf( stderr, " + subtitle tracks:\n" );
385 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
387 subtitle = hb_list_item( title->list_subtitle, i );
388 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
392 if(title->detected_interlacing)
394 /* Interlacing was found in half or more of the preview frames */
395 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
400 static int HandleEvents( hb_handle_t * h )
403 int tmp_num_audio_tracks;
405 hb_get_state( h, &s );
412 #define p s.param.scanning
413 case HB_STATE_SCANNING:
414 /* Show what title is currently being scanned */
415 fprintf( stderr, "Scanning title %d", p.title_cur );
417 fprintf( stderr, " of %d", p.title_count );
418 fprintf( stderr, "...\n" );
422 case HB_STATE_SCANDONE:
429 /* Audio argument string parsing variables */
433 int mixdown = HB_AMIXDOWN_DOLBYPLII;
435 /* Audio argument string parsing variables */
437 list = hb_get_titles( h );
439 if( !hb_list_count( list ) )
441 /* No valid title, stop right there */
442 fprintf( stderr, "No title found.\n" );
449 int longest_title_idx=0;
450 int longest_title_pos=-1;
451 int longest_title_time=0;
454 fprintf( stderr, "Searching for longest title...\n" );
456 for( i = 0; i < hb_list_count( list ); i++ )
458 title = hb_list_item( list, i );
459 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
460 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
461 i, title->index, title_time );
462 if( longest_title_time < title_time )
464 longest_title_time = title_time;
465 longest_title_pos = i;
466 longest_title_idx = title->index;
469 if( longest_title_pos == -1 )
471 fprintf( stderr, "No longest title found.\n" );
475 titleindex = longest_title_idx;
476 fprintf( stderr, "Found longest title, setting title to %d\n",
479 title = hb_list_item( list, longest_title_pos);
481 title = hb_list_item( list, 0 );
486 /* Scan-only mode, print infos and exit */
488 for( i = 0; i < hb_list_count( list ); i++ )
490 title = hb_list_item( list, i );
491 PrintTitleInfo( title );
497 /* Set job settings */
500 PrintTitleInfo( title );
502 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
504 job->chapter_start = MAX( job->chapter_start,
506 job->chapter_end = MIN( job->chapter_end,
508 job->chapter_end = MAX( job->chapter_start,
519 fprintf( stderr, "+ Using preset: %s", preset_name);
521 if (!strcmp(preset_name, "Universal"))
527 vcodec = HB_VCODEC_X264;
528 job->vquality = 20.0;
532 atracks = strdup("1,1");
536 acodecs = strdup("faac,ac3");
540 abitrates = strdup("160,160");
544 mixdowns = strdup("dpl2,auto");
548 arates = strdup("48,Auto");
550 if( !dynamic_range_compression )
552 dynamic_range_compression = strdup("0.0,0.0");
557 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:me=umh");
560 job->chapter_markers = 1;
563 if (!strcmp(preset_name, "iPod"))
570 vcodec = HB_VCODEC_X264;
574 atracks = strdup("1");
578 acodecs = strdup("faac");
582 abitrates = strdup("160");
586 mixdowns = strdup("dpl2");
590 arates = strdup("48");
592 if( !dynamic_range_compression )
594 dynamic_range_compression = strdup("0.0");
599 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
601 job->chapter_markers = 1;
604 if (!strcmp(preset_name, "iPhone & iPod Touch"))
610 vcodec = HB_VCODEC_X264;
611 job->vquality = 20.0;
615 atracks = strdup("1");
619 acodecs = strdup("faac");
623 abitrates = strdup("128");
627 mixdowns = strdup("dpl2");
631 arates = strdup("48");
633 if( !dynamic_range_compression )
635 dynamic_range_compression = strdup("0.0");
640 x264opts = strdup("cabac=0:ref=2:mixed-refs:me=umh");
642 job->chapter_markers = 1;
645 if (!strcmp(preset_name, "AppleTV"))
651 job->largeFileSize = 1;
652 vcodec = HB_VCODEC_X264;
653 job->vquality = 20.0;
657 atracks = strdup("1,1");
661 acodecs = strdup("faac,ac3");
665 abitrates = strdup("160,160");
669 mixdowns = strdup("dpl2,auto");
673 arates = strdup("48,Auto");
675 if( !dynamic_range_compression )
677 dynamic_range_compression = strdup("0.0,0.0");
682 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1");
685 job->chapter_markers = 1;
688 if (!strcmp(preset_name, "Normal"))
694 vcodec = HB_VCODEC_X264;
695 job->vquality = 20.0;
699 atracks = strdup("1");
703 acodecs = strdup("faac");
707 abitrates = strdup("160");
711 mixdowns = strdup("dpl2");
715 arates = strdup("48");
717 if( !dynamic_range_compression )
719 dynamic_range_compression = strdup("0.0");
723 x264opts = strdup("ref=2:bframes=2:me=umh");
726 job->chapter_markers = 1;
729 if (!strcmp(preset_name, "High Profile"))
735 vcodec = HB_VCODEC_X264;
736 job->vquality = 20.0;
740 atracks = strdup("1,1");
744 acodecs = strdup("faac,ac3");
748 abitrates = strdup("160,160");
752 mixdowns = strdup("dpl2,auto");
756 arates = strdup("48,Auto");
758 if( !dynamic_range_compression )
760 dynamic_range_compression = strdup("0.0,0.0");
764 x264opts = strdup("ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct");
769 job->chapter_markers = 1;
772 if (!strcmp(preset_name, "Classic"))
778 job->vbitrate = 1000;
781 atracks = strdup("1");
785 acodecs = strdup("faac");
789 abitrates = strdup("160");
793 mixdowns = strdup("dpl2");
797 arates = strdup("48");
799 if( !dynamic_range_compression )
801 dynamic_range_compression = strdup("0.0");
805 if (!strcmp(preset_name, "AppleTV Legacy"))
811 job->largeFileSize = 1;
812 vcodec = HB_VCODEC_X264;
813 job->vbitrate = 2500;
816 atracks = strdup("1,1");
820 acodecs = strdup("faac,ac3");
824 abitrates = strdup("160,160");
828 mixdowns = strdup("dpl2,auto");
832 arates = strdup("48,Auto");
834 if( !dynamic_range_compression )
836 dynamic_range_compression = strdup("0.0,0.0");
840 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0");
843 job->chapter_markers = 1;
846 if (!strcmp(preset_name, "iPhone Legacy"))
853 vcodec = HB_VCODEC_X264;
857 atracks = strdup("1");
861 acodecs = strdup("faac");
865 abitrates = strdup("128");
869 mixdowns = strdup("dpl2");
873 arates = strdup("48");
875 if( !dynamic_range_compression )
877 dynamic_range_compression = strdup("0.0");
882 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0");
884 job->chapter_markers = 1;
887 if (!strcmp(preset_name, "iPod Legacy"))
894 vcodec = HB_VCODEC_X264;
895 job->vbitrate = 1500;
898 atracks = strdup("1");
902 acodecs = strdup("faac");
906 abitrates = strdup("160");
910 mixdowns = strdup("dpl2");
914 arates = strdup("48");
916 if( !dynamic_range_compression )
918 dynamic_range_compression = strdup("0.0");
923 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");
925 job->chapter_markers = 1;
929 if ( chapter_markers )
931 job->chapter_markers = chapter_markers;
933 if( marker_file != NULL )
935 hb_csv_file_t * file = hb_open_csv_file( marker_file );
936 hb_csv_cell_t * cell;
940 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
944 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
948 /* Parse the cells */
949 while( NULL != ( cell = hb_read_next_cell( file ) ) )
951 /* We have a chapter number */
952 if( cell->cell_col == 0 )
954 row = cell->cell_row;
955 chapter = atoi( cell->cell_text );
958 /* We have a chapter name */
959 if( cell->cell_col == 1 && row == cell->cell_row )
961 /* If we have a valid chapter, copy the string an terminate it */
962 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
964 hb_chapter_t * chapter_s;
966 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
967 strncpy(chapter_s->title, cell->cell_text, 1023);
968 chapter_s->title[1023] = '\0';
973 hb_dispose_cell( cell );
976 hb_close_csv_file( file );
983 int number_of_chapters = hb_list_count(job->title->list_chapter);
986 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
988 hb_chapter_t * chapter_s;
989 chapter_s = hb_list_item( job->title->list_chapter, chapter);
990 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
991 chapter_s->title[1023] = '\0';
996 if( crop[0] >= 0 && crop[1] >= 0 &&
997 crop[2] >= 0 && crop[3] >= 0 )
999 memcpy( job->crop, crop, 4 * sizeof( int ) );
1002 job->deinterlace = deinterlace;
1003 job->grayscale = grayscale;
1005 /* Add selected filters */
1006 job->filters = hb_list_init();
1009 hb_filter_detelecine.settings = detelecine_opt;
1010 hb_list_add( job->filters, &hb_filter_detelecine );
1014 hb_filter_decomb.settings = decomb_opt;
1015 hb_list_add( job->filters, &hb_filter_decomb );
1019 hb_filter_deinterlace.settings = deinterlace_opt;
1020 hb_list_add( job->filters, &hb_filter_deinterlace );
1024 hb_filter_deblock.settings = deblock_opt;
1025 hb_list_add( job->filters, &hb_filter_deblock );
1029 hb_filter_denoise.settings = denoise_opt;
1030 hb_list_add( job->filters, &hb_filter_denoise );
1033 switch( anamorphic_mode )
1035 case 0: // Non-anamorphic
1037 if( width && height )
1040 job->height = height;
1045 hb_fix_aspect( job, HB_KEEP_WIDTH );
1049 job->height = height;
1050 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1052 else if( !width && !height )
1054 hb_fix_aspect( job, HB_KEEP_WIDTH );
1059 case 1: // Strict anammorphic
1060 job->anamorphic.mode = anamorphic_mode;
1063 case 2: // Loose anamorphic
1064 job->anamorphic.mode = 2;
1068 job->anamorphic.modulus = modulus;
1073 job->anamorphic.itu_par = itu_par;
1080 else if( !width && !height )
1082 /* Default to full width when one isn't specified for loose anamorphic */
1083 job->width = title->width - job->crop[2] - job->crop[3];
1088 case 3: // Custom Anamorphic 3: Power User Jamboree
1089 job->anamorphic.mode = 3;
1093 job->anamorphic.modulus = modulus;
1098 job->anamorphic.itu_par = itu_par;
1101 if( par_width && par_height )
1103 job->anamorphic.par_width = par_width;
1104 job->anamorphic.par_height = par_height;
1107 if( keep_display_aspect )
1109 /* First, what *is* the display aspect? */
1110 int cropped_width = title->width - job->crop[2] - job->crop[3];
1111 int cropped_height = title->height - job->crop[0] - job->crop[1];
1113 /* XXX -- I'm assuming people want to keep the source
1114 display AR even though they might have already
1115 asked for ITU values instead. */
1116 float source_display_width = (float)cropped_width *
1117 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1118 float display_aspect = source_display_width / cropped_height;
1120 /* When keeping display aspect, we have to rank some values
1121 by priority in order to consistently handle situations
1122 when more than one might be specified by default.
1124 * First off, PAR gets ignored. (err make this reality)
1125 * Height will be respected over all other settings,
1126 * If it isn't set, display_width will be followed.
1127 * If it isn't set, width will be followed. */
1130 /* We scale the display width to the new height */
1131 display_width = (int)( (double)height * display_aspect );
1133 else if( display_width )
1135 /* We scale the height to the new display width */
1136 height = (int)( (double)display_width / display_aspect );
1140 /* We assume the source height minus cropping, round
1141 to a mod-friendly number, figure out the proper
1142 display width at that height, and adjust the PAR
1143 to create that display width from the new source width. */
1145 temp_height = title->height - job->crop[0] - job->crop[1];
1148 temp_modulus = modulus;
1152 temp_height = MULTIPLE_MOD( temp_height, temp_modulus );
1153 job->anamorphic.par_width = (int)( (double)temp_height * display_aspect );
1154 job->anamorphic.par_height = width;
1160 /* Adjust the PAR to create the new display width
1161 from the default job width. */
1162 job->anamorphic.dar_width = display_width;
1164 job->anamorphic.dar_height = height ?
1166 title->height - job->crop[0] - job->crop[1];
1169 if( width && height )
1171 /* Use these storage dimensions */
1173 job->height = height;
1177 /* Use just this storage width */
1179 job->height = title->height - job->crop[0] - job->crop[1];
1183 /* Use just this storage height. */
1184 job->height = height;
1185 job->width = title->width - job->crop[2] - job->crop[3];
1187 else if( !width && !height )
1189 /* Assume source dimensions after cropping. */
1190 job->width = title->width - job->crop[2] - job->crop[3];
1191 job->height = title->height - job->crop[0] - job->crop[1];
1197 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1199 job->vquality = vquality;
1204 job->vquality = -1.0;
1205 job->vbitrate = vbitrate;
1209 job->vcodec = vcodec;
1213 job->h264_level = 13;
1217 job->h264_level = 30;
1222 job->vrate = 27000000;
1223 job->vrate_base = vrate;
1227 // cfr or pfr flag with no rate specified implies
1228 // use the title rate.
1230 job->vrate = title->rate;
1231 job->vrate_base = title->rate_base;
1234 /* Grab audio tracks */
1237 char * token = strtok(atracks, ",");
1240 int track_start, track_end;
1241 while( token != NULL )
1243 audio = calloc(1, sizeof(*audio));
1244 hb_audio_config_init(audio);
1245 if (strlen(token) >= 3)
1247 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1250 for (i = track_start - 1; i < track_end; i++)
1252 if (i != track_start - 1)
1254 audio = calloc(1, sizeof(*audio));
1255 hb_audio_config_init(audio);
1257 audio->in.track = i;
1258 audio->out.track = num_audio_tracks++;
1259 hb_list_add(audios, audio);
1262 else if( !strcasecmp(token, "none" ) )
1264 audio->in.track = audio->out.track = -1;
1265 audio->out.codec = 0;
1266 hb_list_add(audios, audio);
1271 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1278 audio->in.track = atoi(token) - 1;
1279 audio->out.track = num_audio_tracks++;
1280 hb_list_add(audios, audio);
1282 token = strtok(NULL, ",");
1286 /* Parse audio tracks */
1287 if( hb_list_count(audios) == 0 )
1289 /* Create a new audio track with default settings */
1290 audio = calloc(1, sizeof(*audio));
1291 hb_audio_config_init(audio);
1292 /* Add it to our audios */
1293 hb_list_add(audios, audio);
1296 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1297 for (i = 0; i < tmp_num_audio_tracks; i++)
1299 audio = hb_list_item(audios, 0);
1300 if( (audio == NULL) || (audio->in.track == -1) ||
1301 (audio->out.track == -1) || (audio->out.codec == 0) )
1307 if( hb_audio_add( job, audio ) == 0 )
1309 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1310 audio->in.track + 1 );
1315 hb_list_rem(audios, audio);
1317 if( audio->out.name )
1319 free( audio->out.name);
1328 char * token = strtok(acodecs, ",");
1331 while ( token != NULL )
1333 if ((acodec = get_acodec_for_string(token)) == -1)
1335 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1336 acodec = default_acodec;
1338 if( i < num_audio_tracks )
1340 audio = hb_list_audio_config_item(job->list_audio, i);
1341 audio->out.codec = acodec;
1345 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1346 hb_audio_config_t audio;
1350 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1352 hb_audio_config_init(&audio);
1353 audio.in.track = last_audio->in.track;
1354 audio.out.track = num_audio_tracks++;
1355 audio.out.codec = acodec;
1356 hb_audio_add(job, &audio);
1360 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1363 token = strtok(NULL, ",");
1367 if( i < num_audio_tracks )
1369 /* We have fewer inputs than audio tracks, use the default codec for
1370 * this container for the remaining tracks. Unless we only have one input
1371 * then use that codec instead.
1374 acodec = default_acodec;
1375 for ( ; i < num_audio_tracks; i++)
1377 audio = hb_list_audio_config_item(job->list_audio, i);
1378 audio->out.codec = acodec;
1387 char * token = strtok(arates, ",");
1390 while ( token != NULL )
1392 arate = atoi(token);
1393 audio = hb_list_audio_config_item(job->list_audio, i);
1396 for( j = 0; j < hb_audio_rates_count; j++ )
1398 if( !strcmp( token, hb_audio_rates[j].string ) )
1400 arate = hb_audio_rates[j].rate;
1407 if (!is_sample_rate_valid(arate))
1409 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1410 arate = audio->in.samplerate;
1413 audio->out.samplerate = arate;
1414 if( (++i) >= num_audio_tracks )
1415 break; /* We have more inputs than audio tracks, oops */
1419 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1421 token = strtok(NULL, ",");
1424 if (i < num_audio_tracks)
1426 /* We have fewer inputs than audio tracks, use default sample rate.
1427 * Unless we only have one input, then use that for all tracks.
1430 arate = audio->in.samplerate;
1431 for ( ; i < num_audio_tracks; i++)
1433 audio = hb_list_audio_config_item(job->list_audio, i);
1434 audio->out.samplerate = arate;
1443 char * token = strtok(abitrates, ",");
1446 while ( token != NULL )
1448 abitrate = atoi(token);
1449 audio = hb_list_audio_config_item(job->list_audio, i);
1453 audio->out.bitrate = abitrate;
1454 if( (++i) >= num_audio_tracks )
1455 break; /* We have more inputs than audio tracks, oops */
1459 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1461 token = strtok(NULL, ",");
1464 if (i < num_audio_tracks)
1466 /* We have fewer inputs than audio tracks, use the default bitrate
1467 * for the remaining tracks. Unless we only have one input, then use
1468 * that for all tracks.
1471 abitrate = default_abitrate;
1472 for (; i < num_audio_tracks; i++)
1474 audio = hb_list_audio_config_item(job->list_audio, i);
1475 audio->out.bitrate = abitrate;
1482 if ( dynamic_range_compression )
1484 char * token = strtok(dynamic_range_compression, ",");
1486 token = dynamic_range_compression;
1487 while ( token != NULL )
1489 d_r_c = atof(token);
1490 audio = hb_list_audio_config_item(job->list_audio, i);
1493 audio->out.dynamic_range_compression = d_r_c;
1494 if( (++i) >= num_audio_tracks )
1495 break; /* We have more inputs than audio tracks, oops */
1499 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1501 token = strtok(NULL, ",");
1504 if (i < num_audio_tracks)
1506 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1507 * tracks. Unless we only have one input, then use the same DRC for all
1512 for (; i < num_audio_tracks; i++)
1514 audio = hb_list_audio_config_item(job->list_audio, i);
1515 audio->out.dynamic_range_compression = d_r_c;
1524 char * token = strtok(mixdowns, ",");
1527 while ( token != NULL )
1529 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1530 audio = hb_list_audio_config_item(job->list_audio, i);
1533 audio->out.mixdown = mixdown;
1534 if( (++i) >= num_audio_tracks )
1535 break; /* We have more inputs than audio tracks, oops */
1539 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1541 token = strtok(NULL, ",");
1544 if (i < num_audio_tracks)
1546 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1547 * we only have one input, then use that.
1550 mixdown = HB_AMIXDOWN_DOLBYPLII;
1551 for (; i < num_audio_tracks; i++)
1553 audio = hb_list_audio_config_item(job->list_audio, i);
1554 audio->out.mixdown = mixdown;
1559 /* Audio Track Names */
1563 char * token = strtok(anames, ",");
1566 while ( token != NULL )
1568 audio = hb_list_audio_config_item(job->list_audio, i);
1571 audio->out.name = strdup(token);
1572 if( (++i) >= num_audio_tracks )
1573 break; /* We have more names than audio tracks, oops */
1577 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1580 token = strtok(NULL, ",");
1583 if( i < num_audio_tracks && i == 1 )
1585 /* We have exactly one name and more than one audio track. Use the same
1586 * name for all tracks. */
1587 for ( ; i < num_audio_tracks; i++)
1589 audio = hb_list_audio_config_item(job->list_audio, i);
1590 audio->out.name = strdup(anames);
1593 /* Audio Track Names */
1597 job->vbitrate = hb_calc_bitrate( job, size );
1598 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1604 hb_subtitle_t *subtitle;
1606 * Find the subtitle with the same track as "sub" and
1607 * add that to the job subtitle list
1609 subtitle = hb_list_item( title->list_subtitle, sub-1 );
1611 if( subtitle_force ) {
1612 subtitle->config.force = subtitle_force;
1614 hb_list_add( job->list_subtitle, subtitle );
1616 fprintf( stderr, "Could not find subtitle track %d, skipped\n", sub );
1620 if( native_language )
1622 job->native_language = strdup( native_language );
1630 if ( largeFileSize )
1632 job->largeFileSize = 1;
1636 job->mp4_optimize = 1;
1643 job->file = strdup( output );
1652 job->color_matrix = color_matrix;
1655 if( x264opts != NULL && *x264opts != '\0' )
1657 job->x264opts = x264opts;
1659 else /*avoids a bus error crash when options aren't specified*/
1661 job->x264opts = NULL;
1664 job->maxWidth = maxWidth;
1666 job->maxHeight = maxHeight;
1668 if( start_at_preview )
1670 job->start_at_preview = start_at_preview - 1;
1671 job->seek_points = preview_count;
1676 job->pts_to_stop = stop_at_pts;
1682 job->frame_to_stop = stop_at_frame;
1691 * When subtitle scan is enabled do a fast pre-scan job
1692 * which will determine which subtitles to enable, if any.
1696 x264opts_tmp = job->x264opts;
1698 job->x264opts = NULL;
1700 job->indepth_scan = subtitle_scan;
1701 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1702 "subtitles if found for foreign language segments\n");
1703 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1704 *(job->select_subtitle) = NULL;
1706 job->select_subtitle_config.dest = RENDERSUB;
1707 job->select_subtitle_config.default_track = 0;
1708 job->select_subtitle_config.force = subtitle_force;
1711 * Add the pre-scan job
1715 job->x264opts = x264opts_tmp;
1721 * If subtitle_scan is enabled then only turn it on
1722 * for the first pass and then off again for the
1725 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1727 job->select_subtitle = NULL;
1731 job->indepth_scan = 0;
1735 x264opts2 = strdup(x264opts);
1739 * If turbo options have been selected then append them
1740 * to the x264opts now (size includes one ':' and the '\0')
1742 if( turbo_opts_enabled )
1744 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1747 tmp_x264opts = malloc(size * sizeof(char));
1750 snprintf( tmp_x264opts, size, "%s:%s",
1751 x264opts, turbo_opts );
1755 * No x264opts to modify, but apply the turbo options
1756 * anyway as they may be modifying defaults
1758 snprintf( tmp_x264opts, size, "%s",
1761 x264opts = tmp_x264opts;
1763 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1766 job->x264opts = x264opts;
1770 job->select_subtitle = subtitle_tmp;
1774 * On the second pass we turn off subtitle scan so that we
1775 * can actually encode using any subtitles that were auto
1776 * selected in the first pass (using the whacky select-subtitle
1777 * attribute of the job).
1779 job->indepth_scan = 0;
1781 job->x264opts = x264opts2;
1788 * Turn on subtitle scan if requested, note that this option
1789 * precludes encoding of any actual subtitles.
1792 job->indepth_scan = 0;
1800 #define p s.param.working
1801 case HB_STATE_WORKING:
1802 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1803 p.job_cur, p.job_count, 100.0 * p.progress );
1804 if( p.seconds > -1 )
1806 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1807 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1808 p.hours, p.minutes, p.seconds );
1814 #define p s.param.muxing
1815 case HB_STATE_MUXING:
1817 if (show_mux_warning)
1819 fprintf( stdout, "\rMuxing: this may take awhile..." );
1821 show_mux_warning = 0;
1827 #define p s.param.workdone
1828 case HB_STATE_WORKDONE:
1829 /* Print error if any, then exit */
1833 fprintf( stderr, "\nRip done!\n" );
1835 case HB_ERROR_CANCELED:
1836 fprintf( stderr, "\nRip canceled.\n" );
1839 fprintf( stderr, "\nRip failed (error %x).\n",
1849 /****************************************************************************
1851 ****************************************************************************/
1852 static volatile int64_t i_die_date = 0;
1853 void SigHandler( int i_signal )
1858 i_die_date = hb_get_date();
1859 fprintf( stderr, "Signal %d received, terminating - do it "
1860 "again in case it gets stuck\n", i_signal );
1862 else if( i_die_date + 500 < hb_get_date() )
1864 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1869 /****************************************************************************
1871 ****************************************************************************/
1872 static void ShowHelp()
1875 FILE* const out = stdout;
1878 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1880 "### General Handbrake Options------------------------------------------------\n\n"
1881 " -h, --help Print help\n"
1882 " -u, --update Check for updates and exit\n"
1883 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1884 " -C, --cpu Set CPU count (default: autodetected)\n"
1885 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1886 " if the preset name has spaces, surround it with\n"
1887 " double quotation marks\n"
1888 " -z, --preset-list See a list of available built-in presets\n"
1889 " --dvdnav Use dvdnav (Experimental)\n"
1892 "### Source Options-----------------------------------------------------------\n\n"
1893 " -i, --input <string> Set input device\n"
1894 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1896 " -L, --longest Select the longest title\n"
1897 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1898 " 1 to 3, or \"3\" for chapter 3 only,\n"
1899 " default: all chapters)\n"
1900 " --angle <number> Select the DVD angle\n"
1901 " --previews <#:B> Select how many preview images are generated (max 30),\n"
1902 " and whether or not they're stored to disk (0 or 1).\n"
1903 " (default: 10:0)\n"
1904 " --start-at-preview <#> Start encoding at a given preview.\n"
1905 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
1906 " or pts (on a 90kHz clock)"
1909 "### Destination Options------------------------------------------------------\n\n"
1910 " -o, --output <string> Set output file name\n"
1911 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1912 " autodetected from file name)\n"
1913 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1914 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1915 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1916 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1917 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1921 "### Video Options------------------------------------------------------------\n\n"
1922 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
1923 " (default: ffmpeg)\n"
1924 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1925 " same style as mencoder:\n"
1926 " option1=value1:option2=value2\n"
1927 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1928 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1929 " -S, --size <MB> Set target size\n"
1930 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1931 " -2, --two-pass Use two-pass mode\n"
1932 " -T, --turbo When using 2-pass use the turbo options\n"
1933 " on the first pass to improve speed\n"
1934 " (only works with x264, affects PSNR by about 0.05dB,\n"
1935 " and increases first pass speed two to four times)\n"
1936 " -r, --rate Set video framerate (" );
1937 for( i = 0; i < hb_video_rates_count; i++ )
1939 fprintf( out, hb_video_rates[i].string );
1940 if( i != hb_video_rates_count - 1 )
1941 fprintf( out, "/" );
1944 " Be aware that not specifying a framerate lets\n"
1945 " HandBrake preserve a source's time stamps,\n"
1946 " potentially creating variable framerate video\n"
1947 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
1948 " frame rate control. VFR preserves the source\n"
1949 " timing. CFR makes the output constant rate at\n"
1950 " the rate given by the -r flag (or the source's\n"
1951 " average rate if no -r is given). PFR doesn't\n"
1952 " allow the rate to go over the rate specified\n"
1953 " with the -r flag but won't change the source\n"
1954 " timing if it's below that rate.\n"
1955 " If none of these flags are given, the default\n"
1956 " is --cfr when -r is given and --vfr otherwise\n"
1959 "### Audio Options-----------------------------------------------------------\n\n"
1960 " -a, --audio <string> Select audio track(s), separated by commas\n"
1961 " More than one output track can be used for one\n"
1963 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1964 " tracks, default: first one)\n" );
1968 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
1969 " ac3 and dts meaning passthrough\n"
1970 " Separated by commas for more than one audio track.\n"
1971 " (default: guessed)\n" );
1974 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
1975 " ac3 and dts meaning passthrough\n"
1976 " Separated by commas for more than one audio track.\n"
1977 " (default: guessed)\n" );
1980 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
1981 " Separated by commas for more than one audio track.\n"
1982 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1983 " Separated by commas for more than one audio track.\n"
1984 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1985 " -R, --arate Set audio samplerate(s) (" );
1986 for( i = 0; i < hb_audio_rates_count; i++ )
1988 fprintf( out, hb_audio_rates[i].string );
1989 if( i != hb_audio_rates_count - 1 )
1990 fprintf( out, "/" );
1992 fprintf( out, " kHz)\n"
1993 " Separated by commas for more than one audio track.\n"
1994 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1995 " making soft sounds louder. Range is 1.0 to 4.0\n"
1996 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1997 " Separated by commas for more than one audio track.\n"
1998 " -A, --aname <string> Audio track name(s),\n"
1999 " Separated by commas for more than one audio track.\n"
2002 "### Picture Settings---------------------------------------------------------\n\n"
2003 " -w, --width <number> Set picture width\n"
2004 " -l, --height <number> Set picture height\n"
2005 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2006 " -Y, --maxHeight <#> Set maximum height\n"
2007 " -X, --maxWidth <#> Set maximum width\n"
2008 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2009 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2010 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2011 " directly control all parameters.\n"
2012 " --display-width Set the width to scale the actual pixels to\n"
2013 " <number> at playback, for custom anamorphic.\n"
2014 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2015 " when using custom anamorphic\n"
2016 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2018 " (--display-width and --pixel-aspect are mutually\n"
2019 " exclusive and the former will override the latter)\n"
2020 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2021 " custom anamorphic, useful with underscanned sources\n"
2022 " --modulus Set the number you want the scaled pixel dimensions\n"
2023 " <number> to divide cleanly by, for loose and custom\n"
2024 " anamorphic modes (default: 16)\n"
2025 " -M --color-matrix Set the color space signaled by the output\n"
2026 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2027 " default: set by resolution)\n"
2030 "### Filters---------------------------------------------------------\n\n"
2032 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2033 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2035 " <fast/slow/slower>\n"
2036 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2037 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2038 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2039 " Note: this filter drops duplicate frames to\n"
2040 " restore the pre-telecine framerate, unless you\n"
2041 " specify a constant framerate (--rate 29.97)\n"
2042 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2043 " -8, --denoise Denoise video with hqdn3d filter\n"
2044 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2046 " <weak/medium/strong>\n"
2047 " -7, --deblock Deblock video with pp7 filter\n"
2048 " <QP:M> (default 5:2)\n"
2049 " -g, --grayscale Grayscale encoding\n"
2052 "### Subtitle Options------------------------------------------------------------\n\n"
2053 " -s, --subtitle <number> Select subtitle (default: none)\n"
2054 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
2055 " the one that's only used 10 percent of the time\n"
2056 " or less. This should locate subtitles for short\n"
2057 " foreign language segments. Best used in conjunction\n"
2058 " with --subtitle-forced.\n"
2059 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2060 " the subtitle has the forced flag set. May be used in\n"
2061 " conjunction with --subtitle-scan to auto-select\n"
2062 " a stream if it contains forced subtitles.\n"
2063 " -N, --native-language Select subtitles with this language if it does not\n"
2064 " <string> match the Audio language. Provide the language's\n"
2065 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2074 /****************************************************************************
2076 ****************************************************************************/
2077 static void ShowPresets()
2079 printf("\n< Apple\n");
2081 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:mixed-refs=1:me=umh\n");
2083 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1\n");
2085 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:mixed-refs:me=umh\n");
2087 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1\n");
2091 printf("\n< Regular\n");
2093 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 --strict-anamorphic -m -x ref=2:bframes=2:me=umh\n");
2095 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct\n");
2099 printf("\n< Legacy\n");
2101 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2103 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0\n");
2105 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0\n");
2107 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0\n");
2112 /****************************************************************************
2114 ****************************************************************************/
2115 static int ParseOptions( int argc, char ** argv )
2118 #define PREVIEWS 257
2119 #define START_AT_PREVIEW 258
2123 #define DISPLAY_WIDTH 262
2124 #define PIXEL_ASPECT 263
2126 #define KEEP_DISPLAY_ASPECT 265
2130 static struct option long_options[] =
2132 { "help", no_argument, NULL, 'h' },
2133 { "update", no_argument, NULL, 'u' },
2134 { "verbose", optional_argument, NULL, 'v' },
2135 { "cpu", required_argument, NULL, 'C' },
2136 { "dvdnav", no_argument, NULL, DVDNAV },
2138 { "format", required_argument, NULL, 'f' },
2139 { "input", required_argument, NULL, 'i' },
2140 { "output", required_argument, NULL, 'o' },
2141 { "large-file", no_argument, NULL, '4' },
2142 { "optimize", no_argument, NULL, 'O' },
2143 { "ipod-atom", no_argument, NULL, 'I' },
2145 { "title", required_argument, NULL, 't' },
2146 { "longest", no_argument, NULL, 'L' },
2147 { "chapters", required_argument, NULL, 'c' },
2148 { "angle", required_argument, NULL, ANGLE },
2149 { "markers", optional_argument, NULL, 'm' },
2150 { "audio", required_argument, NULL, 'a' },
2151 { "mixdown", required_argument, NULL, '6' },
2152 { "drc", required_argument, NULL, 'D' },
2153 { "subtitle", required_argument, NULL, 's' },
2154 { "subtitle-scan", no_argument, NULL, 'U' },
2155 { "subtitle-forced", no_argument, NULL, 'F' },
2156 { "native-language", required_argument, NULL,'N' },
2158 { "encoder", required_argument, NULL, 'e' },
2159 { "aencoder", required_argument, NULL, 'E' },
2160 { "two-pass", no_argument, NULL, '2' },
2161 { "deinterlace", optional_argument, NULL, 'd' },
2162 { "deblock", optional_argument, NULL, '7' },
2163 { "denoise", optional_argument, NULL, '8' },
2164 { "detelecine", optional_argument, NULL, '9' },
2165 { "decomb", optional_argument, NULL, '5' },
2166 { "grayscale", no_argument, NULL, 'g' },
2167 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2168 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2169 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2170 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2171 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2172 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2173 { "modulus", required_argument, NULL, MODULUS },
2174 { "itu-par", no_argument, &itu_par, 1 },
2175 { "width", required_argument, NULL, 'w' },
2176 { "height", required_argument, NULL, 'l' },
2177 { "crop", required_argument, NULL, 'n' },
2179 { "vb", required_argument, NULL, 'b' },
2180 { "quality", required_argument, NULL, 'q' },
2181 { "size", required_argument, NULL, 'S' },
2182 { "ab", required_argument, NULL, 'B' },
2183 { "rate", required_argument, NULL, 'r' },
2184 { "arate", required_argument, NULL, 'R' },
2185 { "cqp", no_argument, NULL, 'Q' },
2186 { "x264opts", required_argument, NULL, 'x' },
2187 { "turbo", no_argument, NULL, 'T' },
2188 { "maxHeight", required_argument, NULL, 'Y' },
2189 { "maxWidth", required_argument, NULL, 'X' },
2190 { "preset", required_argument, NULL, 'Z' },
2191 { "preset-list", no_argument, NULL, 'z' },
2193 { "aname", required_argument, NULL, 'A' },
2194 { "color-matrix",required_argument, NULL, 'M' },
2195 { "previews", required_argument, NULL, PREVIEWS },
2196 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2197 { "stop-at", required_argument, NULL, STOP_AT },
2198 { "vfr", no_argument, &cfr, 0 },
2199 { "cfr", no_argument, &cfr, 1 },
2200 { "pfr", no_argument, &cfr, 2 },
2204 int option_index = 0;
2207 c = getopt_long( argc, argv,
2208 "hv::uC:f:4i:Io:t:Lc:m::M:a:A:6:s:UFN:e:E:2dD:7895gOw:l:n:b:q:S:B:r:R:Qx:TY:X:Z:z",
2209 long_options, &option_index );
2218 /* option was handled entirely in getopt_long */
2227 if( optarg != NULL )
2229 debug = atoi( optarg );
2237 cpu = atoi( optarg );
2242 preset_name = strdup(optarg);
2252 format = strdup( optarg );
2255 input = strdup( optarg );
2257 char *devName = bsd_name_for_path( input ); // alloc
2260 if( device_is_dvd( devName ))
2263 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2264 sprintf( input, "/dev/%s", devName );
2271 output = strdup( optarg );
2284 titleindex = atoi( optarg );
2292 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2294 chapter_start = start;
2297 else if( sscanf( optarg, "%d", &start ) == 1 )
2299 chapter_start = start;
2300 chapter_end = chapter_start;
2304 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2311 angle = atoi( optarg );
2314 if( optarg != NULL )
2316 marker_file = strdup( optarg );
2318 chapter_markers = 1;
2321 if( optarg != NULL )
2323 atracks = strdup( optarg );
2331 if( optarg != NULL )
2333 mixdowns = strdup( optarg );
2337 if( optarg != NULL )
2339 dynamic_range_compression = strdup( optarg );
2343 sub = atoi( optarg );
2352 native_language = strdup( optarg );
2358 if( optarg != NULL )
2360 if (!( strcmp( optarg, "fast" ) ))
2362 deinterlace_opt = "-1";
2364 else if (!( strcmp( optarg, "slow" ) ))
2366 deinterlace_opt = "2";
2368 else if (!( strcmp( optarg, "slower" ) ))
2370 deinterlace_opt = "0";
2374 deinterlace_opt = strdup( optarg );
2380 if( optarg != NULL )
2382 deblock_opt = strdup( optarg );
2387 if( optarg != NULL )
2389 if (!( strcmp( optarg, "weak" ) ))
2391 denoise_opt = "2:1:2:3";
2393 else if (!( strcmp( optarg, "medium" ) ))
2395 denoise_opt = "3:2:2:3";
2397 else if (!( strcmp( optarg, "strong" ) ))
2399 denoise_opt = "7:7:5:5";
2403 denoise_opt = strdup( optarg );
2409 if( optarg != NULL )
2411 detelecine_opt = strdup( optarg );
2416 if( optarg != NULL )
2418 decomb_opt = strdup( optarg );
2426 if( optarg != NULL )
2428 sscanf( optarg, "%i", &display_width );
2432 if( optarg != NULL )
2434 sscanf( optarg, "%i:%i", &par_width, &par_height );
2438 if( optarg != NULL )
2440 sscanf( optarg, "%i", &modulus );
2444 if( !strcasecmp( optarg, "ffmpeg" ) )
2446 vcodec = HB_VCODEC_FFMPEG;
2448 else if( !strcasecmp( optarg, "x264" ) )
2450 vcodec = HB_VCODEC_X264;
2452 else if( !strcasecmp( optarg, "x264b13" ) )
2454 vcodec = HB_VCODEC_X264;
2457 else if( !strcasecmp( optarg, "x264b30" ) )
2459 vcodec = HB_VCODEC_X264;
2462 else if( !strcasecmp( optarg, "theora" ) )
2464 vcodec = HB_VCODEC_THEORA;
2468 fprintf( stderr, "invalid codec (%s)\n", optarg );
2473 if( optarg != NULL )
2475 acodecs = strdup( optarg );
2479 width = atoi( optarg );
2482 height = atoi( optarg );
2487 char * tmp = optarg;
2488 for( i = 0; i < 4; i++ )
2492 crop[i] = strtol( tmp, &tmp, 0 );
2501 for( i = 0; i < hb_video_rates_count; i++ )
2503 if( !strcmp( optarg, hb_video_rates[i].string ) )
2505 vrate = hb_video_rates[i].rate;
2511 fprintf( stderr, "invalid framerate %s\n", optarg );
2513 else if ( cfr == 0 )
2520 if( optarg != NULL )
2522 arates = strdup( optarg );
2526 vbitrate = atoi( optarg );
2529 vquality = atof( optarg );
2532 size = atoi( optarg );
2535 if( optarg != NULL )
2537 abitrates = strdup( optarg );
2544 x264opts = strdup( optarg );
2547 turbo_opts_enabled = 1;
2550 maxHeight = atoi( optarg );
2553 maxWidth = atoi (optarg );
2556 if( optarg != NULL )
2558 anames = strdup( optarg );
2562 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2564 case START_AT_PREVIEW:
2565 start_at_preview = atoi( optarg );
2568 stop_at_string = strdup( optarg );
2569 stop_at_token = strtok( stop_at_string, ":");
2570 if( !strcmp( stop_at_token, "frame" ) )
2572 stop_at_token = strtok( NULL, ":");
2573 stop_at_frame = atoi(stop_at_token);
2575 else if( !strcmp( stop_at_token, "pts" ) )
2577 stop_at_token = strtok( NULL, ":");
2578 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2580 else if( !strcmp( stop_at_token, "duration" ) )
2582 stop_at_token = strtok( NULL, ":");
2583 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2584 stop_at_pts *= 90000LL;
2588 if( atoi( optarg ) == 601 )
2590 else if( atoi( optarg ) == 709 )
2594 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2602 static int CheckOptions( int argc, char ** argv )
2609 if( input == NULL || *input == '\0' )
2611 fprintf( stderr, "Missing input device. Run %s --help for "
2612 "syntax.\n", argv[0] );
2617 if( titleindex > 0 )
2619 if( output == NULL || *output == '\0' )
2621 fprintf( stderr, "Missing output file name. Run %s --help "
2622 "for syntax.\n", argv[0] );
2628 char * p = strrchr( output, '.' );
2631 if( p && !strcasecmp( p, ".avi" ) )
2634 default_acodec = HB_ACODEC_LAME;
2636 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2637 !strcasecmp( p, ".m4v" ) ) )
2643 default_acodec = HB_ACODEC_FAAC;
2645 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2646 !strcasecmp( p, ".ogg" ) ) )
2649 default_acodec = HB_ACODEC_VORBIS;
2651 else if( p && !strcasecmp(p, ".mkv" ) )
2654 default_acodec = HB_ACODEC_AC3;
2658 fprintf( stderr, "Output format couldn't be guessed "
2659 "from file name, using default.\n" );
2663 else if( !strcasecmp( format, "avi" ) )
2666 default_acodec = HB_ACODEC_LAME;
2668 else if( !strcasecmp( format, "mp4" ) ||
2669 !strcasecmp( format, "m4v" ) )
2675 default_acodec = HB_ACODEC_FAAC;
2677 else if( !strcasecmp( format, "ogm" ) ||
2678 !strcasecmp( format, "ogg" ) )
2681 default_acodec = HB_ACODEC_VORBIS;
2683 else if( !strcasecmp( format, "mkv" ) )
2686 default_acodec = HB_ACODEC_AC3;
2690 fprintf( stderr, "Invalid output format (%s). Possible "
2691 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2699 static int get_acodec_for_string( char *codec )
2701 if( !strcasecmp( codec, "ac3" ) )
2703 return HB_ACODEC_AC3;
2705 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2707 return HB_ACODEC_DCA;
2709 else if( !strcasecmp( codec, "lame" ) )
2711 return HB_ACODEC_LAME;
2713 else if( !strcasecmp( codec, "faac" ) )
2715 return HB_ACODEC_FAAC;
2717 else if( !strcasecmp( codec, "vorbis") )
2719 return HB_ACODEC_VORBIS;
2722 else if( !strcasecmp( codec, "ca_aac") )
2724 return HB_ACODEC_CA_AAC;
2733 static int is_sample_rate_valid(int rate)
2736 for( i = 0; i < hb_audio_rates_count; i++ )
2738 if (rate == hb_audio_rates[i].rate)
2745 /****************************************************************************
2748 * Returns the BSD device name for the block device that contains the
2749 * passed-in path. Returns NULL on failure.
2750 ****************************************************************************/
2751 static char* bsd_name_for_path(char *path)
2755 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2761 // Get the volume reference number.
2762 FSCatalogInfo catalogInfo;
2763 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2769 FSVolumeRefNum volRefNum = catalogInfo.volume;
2771 // Now let's get the device name
2772 GetVolParmsInfoBuffer volumeParms;
2773 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2779 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
2780 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
2781 if( volumeParms.vMVersion < 4 )
2786 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
2787 if( !volumeParms.vMDeviceID )
2792 return strdup( volumeParms.vMDeviceID );
2795 /****************************************************************************
2798 * Returns whether or not the passed in BSD device represents a DVD, or other
2800 ****************************************************************************/
2801 static int device_is_dvd(char *device)
2803 io_service_t service = get_iokit_service(device);
2804 if( service == IO_OBJECT_NULL )
2808 int result = is_dvd_service(service);
2809 IOObjectRelease(service);
2813 /****************************************************************************
2816 * Returns the IOKit service object for the passed in BSD device name.
2817 ****************************************************************************/
2818 static io_service_t get_iokit_service( char *device )
2820 CFMutableDictionaryRef matchingDict;
2821 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2822 if( matchingDict == NULL )
2824 return IO_OBJECT_NULL;
2826 // Fetch the object with the matching BSD node name. There should only be
2827 // one match, so IOServiceGetMatchingService is used instead of
2828 // IOServiceGetMatchingServices to simplify the code.
2829 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2832 /****************************************************************************
2835 * Returns whether or not the service passed in is a DVD.
2837 * Searches for an IOMedia object that represents the entire (whole) media that
2838 * the volume is on. If the volume is on partitioned media, the whole media
2839 * object will be a parent of the volume's media object. If the media is not
2840 * partitioned, the volume's media object will be the whole media object.
2841 ****************************************************************************/
2842 static int is_dvd_service( io_service_t service )
2844 kern_return_t kernResult;
2847 // Create an iterator across all parents of the service object passed in.
2848 kernResult = IORegistryEntryCreateIterator( service,
2850 kIORegistryIterateRecursively | kIORegistryIterateParents,
2852 if( kernResult != KERN_SUCCESS )
2856 if( iter == IO_OBJECT_NULL )
2861 // A reference on the initial service object is released in the do-while
2862 // loop below, so add a reference to balance.
2863 IOObjectRetain( service );
2868 if( is_whole_media_service( service ) &&
2869 IOObjectConformsTo( service, kIODVDMediaClass) )
2873 IOObjectRelease( service );
2874 } while( !result && (service = IOIteratorNext( iter )) );
2875 IOObjectRelease( iter );
2880 /****************************************************************************
2881 * is_whole_media_service
2883 * Returns whether or not the service passed in is an IOMedia service and
2884 * represents the "whole" media instead of just a partition.
2886 * The whole media object is indicated in the IORegistry by the presence of a
2887 * property with the key "Whole" and value "Yes".
2888 ****************************************************************************/
2889 static is_whole_media_service( io_service_t service )
2893 if( IOObjectConformsTo( service, kIOMediaClass ) )
2895 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2896 CFSTR( kIOMediaWholeKey ),
2897 kCFAllocatorDefault,
2903 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2904 CFRelease( wholeMedia );
2909 #endif // __APPLE_CC__