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. */
17 #import <CoreServices/CoreServices.h>
18 #include <IOKit/IOKitLib.h>
19 #include <IOKit/storage/IOMedia.h>
20 #include <IOKit/storage/IODVDMedia.h>
24 static int debug = HB_DEBUG_NONE;
25 static int update = 0;
26 static char * input = NULL;
27 static char * output = NULL;
28 static char * format = NULL;
29 static int titleindex = 1;
30 static int longest_title = 0;
31 static int subtitle_scan = 0;
32 static int subtitle_force = 0;
33 static char * native_language = NULL;
34 static int twoPass = 0;
35 static int deinterlace = 0;
36 static char * deinterlace_opt = 0;
37 static int deblock = 0;
38 static char * deblock_opt = 0;
39 static int denoise = 0;
40 static char * denoise_opt = 0;
41 static int detelecine = 0;
42 static char * detelecine_opt = 0;
43 static int decomb = 0;
44 static char * decomb_opt = 0;
45 static int grayscale = 0;
46 static int vcodec = HB_VCODEC_FFMPEG;
47 static int h264_13 = 0;
48 static int h264_30 = 0;
49 static hb_list_t * audios = NULL;
50 static hb_audio_config_t * audio = NULL;
51 static int num_audio_tracks = 0;
52 static char * mixdowns = NULL;
53 static char * dynamic_range_compression = NULL;
54 static char * atracks = NULL;
55 static char * arates = NULL;
56 static char * abitrates = NULL;
57 static char * acodecs = NULL;
58 static char * anames = NULL;
59 static int default_acodec = HB_ACODEC_FAAC;
60 static int default_arate = 48000;
61 static int default_abitrate = 160;
64 static int height = 0;
65 static int crop[4] = { -1,-1,-1,-1 };
68 static float vquality = -1.0;
69 static int vbitrate = 0;
72 static int pixelratio = 0;
73 static int loosePixelratio = 0;
74 static int modulus = 0;
75 static int par_height = 0;
76 static int par_width = 0;
77 static int chapter_start = 0;
78 static int chapter_end = 0;
79 static int chapter_markers = 0;
80 static char * marker_file = NULL;
82 static char *x264opts = NULL;
83 static char *x264opts2 = NULL;
84 static int maxHeight = 0;
85 static int maxWidth = 0;
86 static int turbo_opts_enabled = 0;
87 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
88 static int largeFileSize = 0;
89 static int preset = 0;
90 static char * preset_name = 0;
92 static int mp4_optimize = 0;
93 static int ipod_atom = 0;
94 static int color_matrix = 0;
96 /* Exit cleanly on Ctrl-C */
97 static volatile int die = 0;
98 static void SigHandler( int );
101 static void ShowCommands();
102 static void ShowHelp();
103 static void ShowPresets();
105 static int ParseOptions( int argc, char ** argv );
106 static int CheckOptions( int argc, char ** argv );
107 static int HandleEvents( hb_handle_t * h );
109 static int get_acodec_for_string( char *codec );
110 static int is_sample_rate_valid(int rate);
113 static char* bsd_name_for_path(char *path);
114 static int device_is_dvd(char *device);
115 static io_service_t get_iokit_service( char *device );
116 static int is_dvd_service( io_service_t service );
117 static is_whole_media_service( io_service_t service );
120 /* Only print the "Muxing..." message once */
121 static int show_mux_warning = 1;
123 /****************************************************************************
126 * When using the CLI just display using hb_log as we always did in the past
127 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
128 ****************************************************************************/
129 static void hb_cli_error_handler ( const char *errmsg )
131 fprintf( stderr, "ERROR: %s\n", errmsg );
134 int main( int argc, char ** argv )
140 audios = hb_list_init();
142 /* Parse command line */
143 if( ParseOptions( argc, argv ) ||
144 CheckOptions( argc, argv ) )
149 /* Register our error handler */
150 hb_register_error_handler(&hb_cli_error_handler);
153 h = hb_init( debug, update );
156 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
157 hb_get_version( h ), hb_get_build( h ) );
159 /* Check for update */
162 if( ( build = hb_check_update( h, &version ) ) > -1 )
164 fprintf( stderr, "You are using an old version of "
165 "HandBrake.\nLatest is %s (build %d).\n", version,
170 fprintf( stderr, "Your version of HandBrake is up to "
178 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
179 hb_get_cpu_count( h ) > 1 ? "s" : "" );
182 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
183 cpu > 1 ? "s" : "" );
184 hb_set_cpu_count( h, cpu );
187 /* Exit ASAP on Ctrl-C */
188 signal( SIGINT, SigHandler );
190 /* Feed libhb with a DVD to scan */
191 fprintf( stderr, "Opening %s...\n", input );
195 * We need to scan for all the titles in order to find the longest
199 hb_scan( h, input, titleindex );
204 #if !defined(SYS_BEOS)
214 FD_SET( STDIN_FILENO, &fds );
215 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
222 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
224 if( buf[size] == '\n' )
231 if( size >= 256 || buf[size] == '\n' )
236 fprintf( stdout, "\nEncoding Quit by user command\n" );
240 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
262 if( input ) free( input );
263 if( output ) free( output );
264 if( format ) free( format );
267 while( ( audio = hb_list_item( audios, 0 ) ) )
269 hb_list_rem( audios, audio );
270 if( audio->out.name )
272 free( audio->out.name );
276 hb_list_close( &audios );
278 if( mixdowns ) free( mixdowns );
279 if( dynamic_range_compression ) free( dynamic_range_compression );
280 if( atracks ) free( atracks );
281 if( arates ) free( arates );
282 if( abitrates ) free( abitrates );
283 if( acodecs ) free( acodecs );
284 if( anames ) free( anames );
285 if (native_language ) free (native_language );
286 if( x264opts ) free (x264opts );
287 if( x264opts2 ) free (x264opts2 );
288 if (preset_name) free (preset_name);
290 fprintf( stderr, "HandBrake has exited.\n" );
295 static void ShowCommands()
297 fprintf( stdout, "\nCommands:\n" );
298 fprintf( stdout, " [h]elp Show this message\n" );
299 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
300 fprintf( stdout, " [p]ause Pause encoding\n" );
301 fprintf( stdout, " [r]esume Resume encoding\n" );
304 static void PrintTitleInfo( hb_title_t * title )
306 hb_chapter_t * chapter;
307 hb_audio_config_t * audio;
308 hb_subtitle_t * subtitle;
311 fprintf( stderr, "+ title %d:\n", title->index );
312 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
313 title->vts, title->ttn, title->cell_start, title->cell_end,
314 title->block_count );
315 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
316 title->hours, title->minutes, title->seconds );
317 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
318 title->width, title->height,
319 (float) title->aspect,
320 (float) title->rate / title->rate_base );
321 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
322 title->crop[1], title->crop[2], title->crop[3] );
323 fprintf( stderr, " + chapters:\n" );
324 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
326 chapter = hb_list_item( title->list_chapter, i );
327 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
328 "%02d:%02d:%02d\n", chapter->index,
329 chapter->cell_start, chapter->cell_end,
330 chapter->block_count, chapter->hours, chapter->minutes,
333 fprintf( stderr, " + audio tracks:\n" );
334 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
336 audio = hb_list_audio_config_item( title->list_audio, i );
337 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
339 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
340 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
344 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
347 fprintf( stderr, " + subtitle tracks:\n" );
348 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
350 subtitle = hb_list_item( title->list_subtitle, i );
351 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
355 if(title->detected_interlacing)
357 /* Interlacing was found in half or more of the preview frames */
358 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
363 static int HandleEvents( hb_handle_t * h )
366 int tmp_num_audio_tracks;
368 hb_get_state( h, &s );
375 #define p s.param.scanning
376 case HB_STATE_SCANNING:
377 /* Show what title is currently being scanned */
378 fprintf( stderr, "Scanning title %d", p.title_cur );
380 fprintf( stderr, " of %d", p.title_count );
381 fprintf( stderr, "...\n" );
385 case HB_STATE_SCANDONE:
392 /* Audio argument string parsing variables */
396 int mixdown = HB_AMIXDOWN_DOLBYPLII;
398 /* Audio argument string parsing variables */
400 list = hb_get_titles( h );
402 if( !hb_list_count( list ) )
404 /* No valid title, stop right there */
405 fprintf( stderr, "No title found.\n" );
412 int longest_title_idx=0;
413 int longest_title_pos=-1;
414 int longest_title_time=0;
417 fprintf( stderr, "Searching for longest title...\n" );
419 for( i = 0; i < hb_list_count( list ); i++ )
421 title = hb_list_item( list, i );
422 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
423 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
424 i, title->index, title_time );
425 if( longest_title_time < title_time )
427 longest_title_time = title_time;
428 longest_title_pos = i;
429 longest_title_idx = title->index;
432 if( longest_title_pos == -1 )
434 fprintf( stderr, "No longest title found.\n" );
438 titleindex = longest_title_idx;
439 fprintf( stderr, "Found longest title, setting title to %d\n",
442 title = hb_list_item( list, longest_title_pos);
444 title = hb_list_item( list, 0 );
449 /* Scan-only mode, print infos and exit */
451 for( i = 0; i < hb_list_count( list ); i++ )
453 title = hb_list_item( list, i );
454 PrintTitleInfo( title );
460 /* Set job settings */
463 PrintTitleInfo( title );
465 if( chapter_start && chapter_end )
467 job->chapter_start = MAX( job->chapter_start,
469 job->chapter_end = MIN( job->chapter_end,
471 job->chapter_end = MAX( job->chapter_start,
477 fprintf( stderr, "+ Using preset: %s", preset_name);
479 if (!strcmp(preset_name, "Universal"))
482 vcodec = HB_VCODEC_X264;
483 job->vquality = 0.589999973773956;
487 atracks = strdup("1,1");
491 abitrates = strdup("160,auto");
495 arates = strdup("48,Auto");
499 acodecs = strdup("faac,ac3");
503 mixdowns = strdup("dpl2,auto");
508 x264opts = strdup("level=30:cabac=0:ref=3:mixed-refs=1:analyse=all:me=umh:no-fast-pskip=1");
511 job->chapter_markers = 1;
514 if (!strcmp(preset_name, "iPod"))
518 vcodec = HB_VCODEC_X264;
522 atracks = strdup("1");
526 abitrates = strdup("160");
530 arates = strdup("48");
534 acodecs = strdup("faac");
538 mixdowns = strdup("dpl2");
543 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
545 job->chapter_markers = 1;
548 if (!strcmp(preset_name, "iPhone & iPod Touch"))
551 vcodec = HB_VCODEC_X264;
552 job->vquality = 0.589999973773956;
556 atracks = strdup("1");
560 abitrates = strdup("128");
564 arates = strdup("48");
568 acodecs = strdup("faac");
572 mixdowns = strdup("dpl2");
577 x264opts = strdup("level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1");
579 job->chapter_markers = 1;
582 if (!strcmp(preset_name, "AppleTV"))
585 job->largeFileSize = 1;
586 vcodec = HB_VCODEC_X264;
587 job->vquality = 0.589999973773956;
591 atracks = strdup("1,1");
595 abitrates = strdup("160,auto");
599 arates = strdup("48,Auto");
603 acodecs = strdup("faac,ac3");
607 mixdowns = strdup("dpl2,auto");
612 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");
615 job->chapter_markers = 1;
618 if (!strcmp(preset_name, "QuickTime"))
621 vcodec = HB_VCODEC_X264;
622 job->vbitrate = 1800;
625 atracks = strdup("1");
629 abitrates = strdup("160");
633 arates = strdup("Auto");
637 acodecs = strdup("faac");
641 mixdowns = strdup("dpl2");
645 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");
648 job->chapter_markers = 1;
650 turbo_opts_enabled = 1;
653 if (!strcmp(preset_name, "AppleTV Legacy"))
656 job->largeFileSize = 1;
657 vcodec = HB_VCODEC_X264;
658 job->vbitrate = 2500;
661 atracks = strdup("1,1");
665 abitrates = strdup("160,auto");
669 arates = strdup("48,Auto");
673 acodecs = strdup("faac,ac3");
677 mixdowns = strdup("dpl2,auto");
681 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
684 job->chapter_markers = 1;
687 if (!strcmp(preset_name, "iPhone Legacy"))
691 vcodec = HB_VCODEC_X264;
695 atracks = strdup("1");
699 abitrates = strdup("128");
703 arates = strdup("48");
707 acodecs = strdup("faac");
711 mixdowns = strdup("dpl2");
716 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:trellis=1");
718 job->chapter_markers = 1;
721 if (!strcmp(preset_name, "iPod Legacy"))
725 vcodec = HB_VCODEC_X264;
726 job->vbitrate = 1500;
729 atracks = strdup("1");
733 abitrates = strdup("160");
737 arates = strdup("48");
741 acodecs = strdup("faac");
745 mixdowns = strdup("dpl2");
750 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
752 job->chapter_markers = 1;
755 if (!strcmp(preset_name, "Normal"))
758 vcodec = HB_VCODEC_X264;
759 job->vbitrate = 1500;
762 atracks = strdup("1");
766 abitrates = strdup("160");
770 arates = strdup("Auto");
774 acodecs = strdup("faac");
778 mixdowns = strdup("dpl2");
782 x264opts = strdup("ref=2:bframes=2:me=umh");
785 job->chapter_markers = 1;
787 turbo_opts_enabled = 1;
790 if (!strcmp(preset_name, "Classic"))
793 job->vbitrate = 1000;
796 atracks = strdup("1");
800 abitrates = strdup("160");
804 arates = strdup("Auto");
808 acodecs = strdup("faac");
812 mixdowns = strdup("dpl2");
816 if (!strcmp(preset_name, "Animation"))
819 vcodec = HB_VCODEC_X264;
820 job->vbitrate = 1000;
823 atracks = strdup("1");
827 abitrates = strdup("160");
831 arates = strdup("Auto");
835 acodecs = strdup("faac");
839 mixdowns = strdup("dpl2");
843 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");
848 job->chapter_markers = 1;
850 turbo_opts_enabled = 1;
853 if (!strcmp(preset_name, "Constant Quality Rate"))
856 vcodec = HB_VCODEC_X264;
857 job->vquality = 0.600000023841858;
861 atracks = strdup("1");
865 abitrates = strdup("auto");
869 arates = strdup("Auto");
873 acodecs = strdup("ac3");
877 mixdowns = strdup("auto");
881 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");
884 job->chapter_markers = 1;
887 if (!strcmp(preset_name, "Film"))
890 vcodec = HB_VCODEC_X264;
891 job->vbitrate = 1800;
894 atracks = strdup("1");
898 abitrates = strdup("auto");
902 arates = strdup("Auto");
906 acodecs = strdup("ac3");
910 mixdowns = strdup("auto");
914 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");
917 job->chapter_markers = 1;
919 turbo_opts_enabled = 1;
922 if (!strcmp(preset_name, "Television"))
925 vcodec = HB_VCODEC_X264;
926 job->vbitrate = 1300;
929 atracks = strdup("1");
933 abitrates = strdup("160");
937 arates = strdup("Auto");
941 acodecs = strdup("faac");
945 mixdowns = strdup("dpl2");
949 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");
954 job->chapter_markers = 1;
956 turbo_opts_enabled = 1;
959 if (!strcmp(preset_name, "PSP"))
962 job->vbitrate = 1024;
965 atracks = strdup("1");
969 abitrates = strdup("128");
973 arates = strdup("48");
977 acodecs = strdup("faac");
981 mixdowns = strdup("dpl2");
985 job->chapter_markers = 1;
988 if (!strcmp(preset_name, "PS3"))
991 vcodec = HB_VCODEC_X264;
992 job->vbitrate = 2500;
995 atracks = strdup("1");
999 abitrates = strdup("160");
1003 arates = strdup("48");
1007 acodecs = strdup("faac");
1011 mixdowns = strdup("dpl2");
1019 x264opts = strdup("level=41:me=umh");
1024 if (!strcmp(preset_name, "Xbox 360"))
1027 vcodec = HB_VCODEC_X264;
1028 job->vbitrate = 2000;
1031 atracks = strdup("1");
1035 abitrates = strdup("160");
1039 arates = strdup("48");
1043 acodecs = strdup("faac");
1047 mixdowns = strdup("dpl2");
1051 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");
1057 if ( chapter_markers )
1059 job->chapter_markers = chapter_markers;
1061 if( marker_file != NULL )
1063 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1064 hb_csv_cell_t * cell;
1068 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1072 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1076 /* Parse the cells */
1077 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1079 /* We have a chapter number */
1080 if( cell->cell_col == 0 )
1082 row = cell->cell_row;
1083 chapter = atoi( cell->cell_text );
1086 /* We have a chapter name */
1087 if( cell->cell_col == 1 && row == cell->cell_row )
1089 /* If we have a valid chapter, copy the string an terminate it */
1090 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1092 hb_chapter_t * chapter_s;
1094 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1095 strncpy(chapter_s->title, cell->cell_text, 1023);
1096 chapter_s->title[1023] = '\0';
1101 hb_dispose_cell( cell );
1104 hb_close_csv_file( file );
1109 /* No marker file */
1111 int number_of_chapters = hb_list_count(job->title->list_chapter);
1114 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1116 hb_chapter_t * chapter_s;
1117 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1118 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1119 chapter_s->title[1023] = '\0';
1124 if( crop[0] >= 0 && crop[1] >= 0 &&
1125 crop[2] >= 0 && crop[3] >= 0 )
1127 memcpy( job->crop, crop, 4 * sizeof( int ) );
1130 job->deinterlace = deinterlace;
1131 job->grayscale = grayscale;
1132 if (loosePixelratio)
1134 job->pixel_ratio = 2;
1137 job->modulus = modulus;
1139 if( par_width && par_height )
1141 job->pixel_ratio = 3;
1142 job->pixel_aspect_width = par_width;
1143 job->pixel_aspect_height = par_height;
1148 job->pixel_ratio = pixelratio;
1151 /* Add selected filters */
1152 job->filters = hb_list_init();
1155 hb_filter_detelecine.settings = detelecine_opt;
1156 hb_list_add( job->filters, &hb_filter_detelecine );
1160 /* No framerate specified, so using same as source.
1161 That means VFR, so set detelecine up to drop frames. */
1167 hb_filter_decomb.settings = decomb_opt;
1168 hb_list_add( job->filters, &hb_filter_decomb );
1172 hb_filter_deinterlace.settings = deinterlace_opt;
1173 hb_list_add( job->filters, &hb_filter_deinterlace );
1177 hb_filter_deblock.settings = deblock_opt;
1178 hb_list_add( job->filters, &hb_filter_deblock );
1182 hb_filter_denoise.settings = denoise_opt;
1183 hb_list_add( job->filters, &hb_filter_denoise );
1186 if( width && height )
1189 job->height = height;
1194 hb_fix_aspect( job, HB_KEEP_WIDTH );
1196 else if( height && !loosePixelratio)
1198 job->height = height;
1199 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1201 else if( !width && !height && !pixelratio && !loosePixelratio )
1203 hb_fix_aspect( job, HB_KEEP_WIDTH );
1205 else if (!width && loosePixelratio)
1207 /* Default to full width when one isn't specified for loose anamorphic */
1208 job->width = title->width - job->crop[2] - job->crop[3];
1209 /* The height will be thrown away in hb.c but calculate it anyway */
1210 hb_fix_aspect( job, HB_KEEP_WIDTH );
1213 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1215 job->vquality = vquality;
1220 job->vquality = -1.0;
1221 job->vbitrate = vbitrate;
1225 job->vcodec = vcodec;
1229 job->h264_level = 13;
1233 job->h264_level = 30;
1238 job->vrate = 27000000;
1239 job->vrate_base = vrate;
1242 /* Grab audio tracks */
1245 char * token = strtok(atracks, ",");
1248 int track_start, track_end;
1249 while( token != NULL )
1251 audio = calloc(1, sizeof(*audio));
1252 hb_audio_config_init(audio);
1253 if (strlen(token) >= 3)
1255 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1258 for (i = track_start - 1; i < track_end; i++)
1260 if (i != track_start - 1)
1262 audio = calloc(1, sizeof(*audio));
1263 hb_audio_config_init(audio);
1265 audio->in.track = i;
1266 audio->out.track = num_audio_tracks++;
1267 hb_list_add(audios, audio);
1270 else if( !strcasecmp(token, "none" ) )
1272 audio->in.track = audio->out.track = -1;
1273 audio->out.codec = 0;
1274 hb_list_add(audios, audio);
1279 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1286 audio->in.track = atoi(token) - 1;
1287 audio->out.track = num_audio_tracks++;
1288 hb_list_add(audios, audio);
1290 token = strtok(NULL, ",");
1294 /* Parse audio tracks */
1295 if( hb_list_count(audios) == 0 )
1297 /* Create a new audio track with default settings */
1298 audio = calloc(1, sizeof(*audio));
1299 hb_audio_config_init(audio);
1300 /* Add it to our audios */
1301 hb_list_add(audios, audio);
1304 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1305 for (i = 0; i < tmp_num_audio_tracks; i++)
1307 audio = hb_list_item(audios, 0);
1308 if( (audio == NULL) || (audio->in.track == -1) ||
1309 (audio->out.track == -1) || (audio->out.codec == 0) )
1315 if( hb_audio_add( job, audio ) == 0 )
1317 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1318 audio->in.track + 1 );
1323 hb_list_rem(audios, audio);
1325 if( audio->out.name )
1327 free( audio->out.name);
1336 char * token = strtok(acodecs, ",");
1339 while ( token != NULL )
1341 if ((acodec = get_acodec_for_string(token)) == -1)
1343 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1344 acodec = default_acodec;
1346 if( i < num_audio_tracks )
1348 audio = hb_list_audio_config_item(job->list_audio, i);
1349 audio->out.codec = acodec;
1353 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1354 hb_audio_config_t audio;
1358 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1360 hb_audio_config_init(&audio);
1361 audio.in.track = last_audio->in.track;
1362 audio.out.track = num_audio_tracks++;
1363 audio.out.codec = acodec;
1364 hb_audio_add(job, &audio);
1368 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1371 token = strtok(NULL, ",");
1375 if( i < num_audio_tracks )
1377 /* We have fewer inputs than audio tracks, use the default codec for
1378 * this container for the remaining tracks. Unless we only have one input
1379 * then use that codec instead.
1382 acodec = default_acodec;
1383 for ( ; i < num_audio_tracks; i++)
1385 audio = hb_list_audio_config_item(job->list_audio, i);
1386 audio->out.codec = acodec;
1395 char * token = strtok(arates, ",");
1398 while ( token != NULL )
1400 arate = atoi(token);
1401 audio = hb_list_audio_config_item(job->list_audio, i);
1404 for( j = 0; j < hb_audio_rates_count; j++ )
1406 if( !strcmp( token, hb_audio_rates[j].string ) )
1408 arate = hb_audio_rates[j].rate;
1415 if (!is_sample_rate_valid(arate))
1417 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1418 arate = audio->in.samplerate;
1421 audio->out.samplerate = arate;
1422 if( (++i) >= num_audio_tracks )
1423 break; /* We have more inputs than audio tracks, oops */
1427 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1429 token = strtok(NULL, ",");
1432 if (i < num_audio_tracks)
1434 /* We have fewer inputs than audio tracks, use default sample rate.
1435 * Unless we only have one input, then use that for all tracks.
1438 arate = audio->in.samplerate;
1439 for ( ; i < num_audio_tracks; i++)
1441 audio = hb_list_audio_config_item(job->list_audio, i);
1442 audio->out.samplerate = arate;
1451 char * token = strtok(abitrates, ",");
1454 while ( token != NULL )
1456 abitrate = atoi(token);
1457 audio = hb_list_audio_config_item(job->list_audio, i);
1461 audio->out.bitrate = abitrate;
1462 if( (++i) >= num_audio_tracks )
1463 break; /* We have more inputs than audio tracks, oops */
1467 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1469 token = strtok(NULL, ",");
1472 if (i < num_audio_tracks)
1474 /* We have fewer inputs than audio tracks, use the default bitrate
1475 * for the remaining tracks. Unless we only have one input, then use
1476 * that for all tracks.
1479 abitrate = default_abitrate;
1480 for (; i < num_audio_tracks; i++)
1482 audio = hb_list_audio_config_item(job->list_audio, i);
1483 audio->out.bitrate = abitrate;
1490 if ( dynamic_range_compression )
1492 char * token = strtok(dynamic_range_compression, ",");
1494 token = dynamic_range_compression;
1495 while ( token != NULL )
1497 d_r_c = atof(token);
1498 audio = hb_list_audio_config_item(job->list_audio, i);
1501 audio->out.dynamic_range_compression = d_r_c;
1502 if( (++i) >= num_audio_tracks )
1503 break; /* We have more inputs than audio tracks, oops */
1507 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1509 token = strtok(NULL, ",");
1512 if (i < num_audio_tracks)
1514 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1515 * tracks. Unless we only have one input, then use the same DRC for all
1520 for (; i < num_audio_tracks; i++)
1522 audio = hb_list_audio_config_item(job->list_audio, i);
1523 audio->out.dynamic_range_compression = d_r_c;
1532 char * token = strtok(mixdowns, ",");
1535 while ( token != NULL )
1537 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1538 audio = hb_list_audio_config_item(job->list_audio, i);
1541 audio->out.mixdown = mixdown;
1542 if( (++i) >= num_audio_tracks )
1543 break; /* We have more inputs than audio tracks, oops */
1547 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1549 token = strtok(NULL, ",");
1552 if (i < num_audio_tracks)
1554 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1555 * we only have one input, then use that.
1558 mixdown = HB_AMIXDOWN_DOLBYPLII;
1559 for (; i < num_audio_tracks; i++)
1561 audio = hb_list_audio_config_item(job->list_audio, i);
1562 audio->out.mixdown = mixdown;
1567 /* Audio Track Names */
1571 char * token = strtok(anames, ",");
1574 while ( token != NULL )
1576 audio = hb_list_audio_config_item(job->list_audio, i);
1579 audio->out.name = strdup(token);
1580 if( (++i) >= num_audio_tracks )
1581 break; /* We have more names than audio tracks, oops */
1585 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1588 token = strtok(NULL, ",");
1591 if( i < num_audio_tracks && i == 1 )
1593 /* We have exactly one name and more than one audio track. Use the same
1594 * name for all tracks. */
1595 for ( ; i < num_audio_tracks; i++)
1597 audio = hb_list_audio_config_item(job->list_audio, i);
1598 audio->out.name = strdup(anames);
1601 /* Audio Track Names */
1605 job->vbitrate = hb_calc_bitrate( job, size );
1606 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1612 job->subtitle = sub - 1;
1615 if( native_language )
1617 job->native_language = strdup( native_language );
1625 if ( largeFileSize )
1627 job->largeFileSize = 1;
1631 job->mp4_optimize = 1;
1638 job->file = strdup( output );
1647 job->color_matrix = color_matrix;
1650 if( x264opts != NULL && *x264opts != '\0' )
1652 job->x264opts = x264opts;
1654 else /*avoids a bus error crash when options aren't specified*/
1656 job->x264opts = NULL;
1659 job->maxWidth = maxWidth;
1661 job->maxHeight = maxHeight;
1663 if( subtitle_force )
1665 job->subtitle_force = subtitle_force;
1673 * When subtitle scan is enabled do a fast pre-scan job
1674 * which will determine which subtitles to enable, if any.
1678 x264opts_tmp = job->x264opts;
1680 job->x264opts = NULL;
1682 job->indepth_scan = subtitle_scan;
1683 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1684 "subtitles if found for foreign language segments\n");
1685 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1686 *(job->select_subtitle) = NULL;
1689 * Add the pre-scan job
1693 job->x264opts = x264opts_tmp;
1699 * If subtitle_scan is enabled then only turn it on
1700 * for the first pass and then off again for the
1703 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1705 job->select_subtitle = NULL;
1709 job->indepth_scan = 0;
1713 x264opts2 = strdup(x264opts);
1717 * If turbo options have been selected then append them
1718 * to the x264opts now (size includes one ':' and the '\0')
1720 if( turbo_opts_enabled )
1722 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1725 tmp_x264opts = malloc(size * sizeof(char));
1728 snprintf( tmp_x264opts, size, "%s:%s",
1729 x264opts, turbo_opts );
1733 * No x264opts to modify, but apply the turbo options
1734 * anyway as they may be modifying defaults
1736 snprintf( tmp_x264opts, size, "%s",
1739 x264opts = tmp_x264opts;
1741 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1744 job->x264opts = x264opts;
1748 job->select_subtitle = subtitle_tmp;
1752 * On the second pass we turn off subtitle scan so that we
1753 * can actually encode using any subtitles that were auto
1754 * selected in the first pass (using the whacky select-subtitle
1755 * attribute of the job).
1757 job->indepth_scan = 0;
1759 job->x264opts = x264opts2;
1766 * Turn on subtitle scan if requested, note that this option
1767 * precludes encoding of any actual subtitles.
1770 job->indepth_scan = 0;
1778 #define p s.param.working
1779 case HB_STATE_WORKING:
1780 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1781 p.job_cur, p.job_count, 100.0 * p.progress );
1782 if( p.seconds > -1 )
1784 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1785 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1786 p.hours, p.minutes, p.seconds );
1792 #define p s.param.muxing
1793 case HB_STATE_MUXING:
1795 if (show_mux_warning)
1797 fprintf( stdout, "\rMuxing: this may take awhile..." );
1799 show_mux_warning = 0;
1805 #define p s.param.workdone
1806 case HB_STATE_WORKDONE:
1807 /* Print error if any, then exit */
1811 fprintf( stderr, "\nRip done!\n" );
1813 case HB_ERROR_CANCELED:
1814 fprintf( stderr, "\nRip canceled.\n" );
1817 fprintf( stderr, "\nRip failed (error %x).\n",
1827 /****************************************************************************
1829 ****************************************************************************/
1830 static volatile int64_t i_die_date = 0;
1831 void SigHandler( int i_signal )
1836 i_die_date = hb_get_date();
1837 fprintf( stderr, "Signal %d received, terminating - do it "
1838 "again in case it gets stuck\n", i_signal );
1840 else if( i_die_date + 500 < hb_get_date() )
1842 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1847 /****************************************************************************
1849 ****************************************************************************/
1850 static void ShowHelp()
1855 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1857 "### General Handbrake Options------------------------------------------------\n\n"
1858 " -h, --help Print help\n"
1859 " -u, --update Check for updates and exit\n"
1860 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1861 " -C, --cpu Set CPU count (default: autodetected)\n"
1862 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1863 " if the preset name has spaces, surround it with\n"
1864 " double quotation marks\n"
1865 " -z, --preset-list See a list of available built-in presets\n"
1868 "### Source Options-----------------------------------------------------------\n\n"
1869 " -i, --input <string> Set input device\n"
1870 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1872 " -L, --longest Select the longest title\n"
1873 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1874 " 1 to 3, or \"3\" for chapter 3 only,\n"
1875 " default: all chapters)\n"
1878 "### Destination Options------------------------------------------------------\n\n"
1879 " -o, --output <string> Set output file name\n"
1880 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1881 " autodetected from file name)\n"
1882 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1883 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1884 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1885 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1886 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1890 "### Video Options------------------------------------------------------------\n\n"
1891 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1892 " x264,theora default: ffmpeg)\n"
1893 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1894 " same style as mencoder:\n"
1895 " option1=value1:option2=value2\n"
1896 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1897 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1898 " -S, --size <MB> Set target size\n"
1899 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1900 " -2, --two-pass Use two-pass mode\n"
1901 " -T, --turbo When using 2-pass use the turbo options\n"
1902 " on the first pass to improve speed\n"
1903 " (only works with x264, affects PSNR by about 0.05dB,\n"
1904 " and increases first pass speed two to four times)\n"
1905 " -r, --rate Set video framerate (" );
1906 for( i = 0; i < hb_video_rates_count; i++ )
1908 fprintf( stderr, hb_video_rates[i].string );
1909 if( i != hb_video_rates_count - 1 )
1910 fprintf( stderr, "/" );
1912 fprintf( stderr, ")\n"
1913 " Be aware that not specifying a framerate lets\n"
1914 " HandBrake preserve a source's time stamps,\n"
1915 " potentially creating variable framerate video\n"
1918 "### Audio Options-----------------------------------------------------------\n\n"
1919 " -a, --audio <string> Select audio track(s), separated by commas\n"
1920 " More than one output track can be used for one\n"
1922 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1923 " tracks, default: first one)\n"
1924 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1925 " ac3 meaning passthrough\n"
1926 " Separated by commas for more than one audio track.\n"
1927 " (default: guessed)\n"
1928 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
1929 " Separated by commas for more than one audio track.\n"
1930 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1931 " Separated by commas for more than one audio track.\n"
1932 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1933 " -R, --arate Set audio samplerate(s) (" );
1934 for( i = 0; i < hb_audio_rates_count; i++ )
1936 fprintf( stderr, hb_audio_rates[i].string );
1937 if( i != hb_audio_rates_count - 1 )
1938 fprintf( stderr, "/" );
1940 fprintf( stderr, " kHz)\n"
1941 " Separated by commas for more than one audio track.\n"
1942 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1943 " making soft sounds louder. Range is 1.0 to 4.0\n"
1944 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1945 " Separated by commas for more than one audio track.\n"
1946 " -A, --aname <string> Audio track name(s),\n"
1947 " Separated by commas for more than one audio track.\n"
1950 "### Picture Settings---------------------------------------------------------\n\n"
1951 " -w, --width <number> Set picture width\n"
1952 " -l, --height <number> Set picture height\n"
1953 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1954 " -Y, --maxHeight <#> Set maximum height\n"
1955 " -X, --maxWidth <#> Set maximum width\n"
1956 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1957 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1958 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
1959 " the dimensions to divide cleanly by (default 16)\n"
1960 " and the pixel ratio to use (default autodetected)\n"
1961 " -M --color-matrix Set the color space signaled by the output\n"
1962 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
1963 " default: set by resolution)\n"
1966 "### Filters---------------------------------------------------------\n\n"
1968 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1969 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1971 " <fast/slow/slower>\n"
1972 " -5, --decomb Selectively deinterlaces when it detects combing\n"
1973 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
1974 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
1975 " Note: this filter drops duplicate frames to\n"
1976 " restore the pre-telecine framerate, unless you\n"
1977 " specify a constant framerate (--rate 29.97)\n"
1978 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1979 " -8, --denoise Denoise video with hqdn3d filter\n"
1980 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1982 " <weak/medium/strong>\n"
1983 " -7, --deblock Deblock video with pp7 filter\n"
1984 " <QP:M> (default 5:2)\n"
1985 " -g, --grayscale Grayscale encoding\n"
1988 "### Subtitle Options------------------------------------------------------------\n\n"
1989 " -s, --subtitle <number> Select subtitle (default: none)\n"
1990 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1991 " the one that's only used 10 percent of the time\n"
1992 " or less. This should locate subtitles for short\n"
1993 " foreign language segments. Best used in conjunction\n"
1994 " with --subtitle-forced.\n"
1995 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1996 " the subtitle has the forced flag set. May be used in\n"
1997 " conjunction with --subtitle-scan to auto-select\n"
1998 " a stream if it contains forced subtitles.\n"
1999 " -N, --native-language Select subtitles with this language if it does not\n"
2000 " <string> match the Audio language. Provide the language's\n"
2001 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2010 /****************************************************************************
2012 ****************************************************************************/
2013 static void ShowPresets()
2015 printf("\n< Apple\n");
2017 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");
2019 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");
2021 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");
2023 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");
2025 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");
2027 printf("\n << Legacy\n");
2029 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");
2031 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");
2033 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");
2039 printf("\n< Basic\n");
2041 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");
2043 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4\n");
2047 printf("\n< High Profile\n");
2049 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");
2051 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");
2053 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");
2055 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");
2059 printf("\n< Gaming Consoles\n");
2061 printf("\n + PSP: -b 1024 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 368 -Y 208 -m\n");
2063 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");
2065 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");
2071 /****************************************************************************
2073 ****************************************************************************/
2074 static int ParseOptions( int argc, char ** argv )
2078 static struct option long_options[] =
2080 { "help", no_argument, NULL, 'h' },
2081 { "update", no_argument, NULL, 'u' },
2082 { "verbose", optional_argument, NULL, 'v' },
2083 { "cpu", required_argument, NULL, 'C' },
2085 { "format", required_argument, NULL, 'f' },
2086 { "input", required_argument, NULL, 'i' },
2087 { "output", required_argument, NULL, 'o' },
2088 { "large-file", no_argument, NULL, '4' },
2089 { "optimize", no_argument, NULL, 'O' },
2090 { "ipod-atom", no_argument, NULL, 'I' },
2092 { "title", required_argument, NULL, 't' },
2093 { "longest", no_argument, NULL, 'L' },
2094 { "chapters", required_argument, NULL, 'c' },
2095 { "markers", optional_argument, NULL, 'm' },
2096 { "audio", required_argument, NULL, 'a' },
2097 { "mixdown", required_argument, NULL, '6' },
2098 { "drc", required_argument, NULL, 'D' },
2099 { "subtitle", required_argument, NULL, 's' },
2100 { "subtitle-scan", no_argument, NULL, 'U' },
2101 { "subtitle-forced", no_argument, NULL, 'F' },
2102 { "native-language", required_argument, NULL,'N' },
2104 { "encoder", required_argument, NULL, 'e' },
2105 { "aencoder", required_argument, NULL, 'E' },
2106 { "two-pass", no_argument, NULL, '2' },
2107 { "deinterlace", optional_argument, NULL, 'd' },
2108 { "deblock", optional_argument, NULL, '7' },
2109 { "denoise", optional_argument, NULL, '8' },
2110 { "detelecine", optional_argument, NULL, '9' },
2111 { "decomb", optional_argument, NULL, '5' },
2112 { "grayscale", no_argument, NULL, 'g' },
2113 { "pixelratio", no_argument, NULL, 'p' },
2114 { "loosePixelratio", optional_argument, NULL, 'P' },
2115 { "width", required_argument, NULL, 'w' },
2116 { "height", required_argument, NULL, 'l' },
2117 { "crop", required_argument, NULL, 'n' },
2119 { "vb", required_argument, NULL, 'b' },
2120 { "quality", required_argument, NULL, 'q' },
2121 { "size", required_argument, NULL, 'S' },
2122 { "ab", required_argument, NULL, 'B' },
2123 { "rate", required_argument, NULL, 'r' },
2124 { "arate", required_argument, NULL, 'R' },
2125 { "cqp", no_argument, NULL, 'Q' },
2126 { "x264opts", required_argument, NULL, 'x' },
2127 { "turbo", no_argument, NULL, 'T' },
2128 { "maxHeight", required_argument, NULL, 'Y' },
2129 { "maxWidth", required_argument, NULL, 'X' },
2130 { "preset", required_argument, NULL, 'Z' },
2131 { "preset-list", no_argument, NULL, 'z' },
2133 { "aname", required_argument, NULL, 'A' },
2134 { "color-matrix",required_argument, NULL, 'M' },
2139 int option_index = 0;
2142 c = getopt_long( argc, argv,
2143 "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",
2144 long_options, &option_index );
2159 if( optarg != NULL )
2161 debug = atoi( optarg );
2169 cpu = atoi( optarg );
2174 preset_name = strdup(optarg);
2181 format = strdup( optarg );
2184 input = strdup( optarg );
2186 char *devName = bsd_name_for_path( input );
2187 if( devName == NULL )
2191 if( device_is_dvd( devName ) )
2193 char *newInput = malloc( strlen("/dev/") + strlen( devName ) + 1);
2194 sprintf( newInput, "/dev/%s", devName );
2201 output = strdup( optarg );
2214 titleindex = atoi( optarg );
2222 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2224 chapter_start = start;
2227 else if( sscanf( optarg, "%d", &start ) == 1 )
2229 chapter_start = start;
2230 chapter_end = chapter_start;
2234 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2241 if( optarg != NULL )
2243 marker_file = strdup( optarg );
2245 chapter_markers = 1;
2248 if( optarg != NULL )
2250 atracks = strdup( optarg );
2258 if( optarg != NULL )
2260 mixdowns = strdup( optarg );
2264 if( optarg != NULL )
2266 dynamic_range_compression = strdup( optarg );
2270 sub = atoi( optarg );
2279 native_language = strdup( optarg );
2285 if( optarg != NULL )
2287 if (!( strcmp( optarg, "fast" ) ))
2289 deinterlace_opt = "-1";
2291 else if (!( strcmp( optarg, "slow" ) ))
2293 deinterlace_opt = "2";
2295 else if (!( strcmp( optarg, "slower" ) ))
2297 deinterlace_opt = "0";
2301 deinterlace_opt = strdup( optarg );
2307 if( optarg != NULL )
2309 deblock_opt = strdup( optarg );
2314 if( optarg != NULL )
2316 if (!( strcmp( optarg, "weak" ) ))
2318 denoise_opt = "2:1:2:3";
2320 else if (!( strcmp( optarg, "medium" ) ))
2322 denoise_opt = "3:2:2:3";
2324 else if (!( strcmp( optarg, "strong" ) ))
2326 denoise_opt = "7:7:5:5";
2330 denoise_opt = strdup( optarg );
2336 if( optarg != NULL )
2338 detelecine_opt = strdup( optarg );
2343 if( optarg != NULL )
2345 decomb_opt = strdup( optarg );
2356 loosePixelratio = 1;
2357 if( optarg != NULL )
2359 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
2363 if( !strcasecmp( optarg, "ffmpeg" ) )
2365 vcodec = HB_VCODEC_FFMPEG;
2367 else if( !strcasecmp( optarg, "xvid" ) )
2369 vcodec = HB_VCODEC_XVID;
2371 else if( !strcasecmp( optarg, "x264" ) )
2373 vcodec = HB_VCODEC_X264;
2375 else if( !strcasecmp( optarg, "x264b13" ) )
2377 vcodec = HB_VCODEC_X264;
2380 else if( !strcasecmp( optarg, "x264b30" ) )
2382 vcodec = HB_VCODEC_X264;
2385 else if( !strcasecmp( optarg, "theora" ) )
2387 vcodec = HB_VCODEC_THEORA;
2391 fprintf( stderr, "invalid codec (%s)\n", optarg );
2396 if( optarg != NULL )
2398 acodecs = strdup( optarg );
2402 width = atoi( optarg );
2405 height = atoi( optarg );
2410 char * tmp = optarg;
2411 for( i = 0; i < 4; i++ )
2415 crop[i] = strtol( tmp, &tmp, 0 );
2424 for( i = 0; i < hb_video_rates_count; i++ )
2426 if( !strcmp( optarg, hb_video_rates[i].string ) )
2428 vrate = hb_video_rates[i].rate;
2434 fprintf( stderr, "invalid framerate %s\n", optarg );
2439 if( optarg != NULL )
2441 arates = strdup( optarg );
2445 vbitrate = atoi( optarg );
2448 vquality = atof( optarg );
2451 size = atoi( optarg );
2454 if( optarg != NULL )
2456 abitrates = strdup( optarg );
2463 x264opts = strdup( optarg );
2466 turbo_opts_enabled = 1;
2469 maxHeight = atoi( optarg );
2472 maxWidth = atoi (optarg );
2475 if( optarg != NULL )
2477 anames = strdup( optarg );
2481 if( atoi( optarg ) == 601 )
2483 else if( atoi( optarg ) == 709 )
2487 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2495 static int CheckOptions( int argc, char ** argv )
2502 if( input == NULL || *input == '\0' )
2504 fprintf( stderr, "Missing input device. Run %s --help for "
2505 "syntax.\n", argv[0] );
2510 if( titleindex > 0 )
2512 if( output == NULL || *output == '\0' )
2514 fprintf( stderr, "Missing output file name. Run %s --help "
2515 "for syntax.\n", argv[0] );
2521 char * p = strrchr( output, '.' );
2524 if( p && !strcasecmp( p, ".avi" ) )
2527 default_acodec = HB_ACODEC_LAME;
2529 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2530 !strcasecmp( p, ".m4v" ) ) )
2536 default_acodec = HB_ACODEC_FAAC;
2538 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2539 !strcasecmp( p, ".ogg" ) ) )
2542 default_acodec = HB_ACODEC_VORBIS;
2544 else if( p && !strcasecmp(p, ".mkv" ) )
2547 default_acodec = HB_ACODEC_AC3;
2551 fprintf( stderr, "Output format couldn't be guessed "
2552 "from file name, using default.\n" );
2556 else if( !strcasecmp( format, "avi" ) )
2559 default_acodec = HB_ACODEC_LAME;
2561 else if( !strcasecmp( format, "mp4" ) ||
2562 !strcasecmp( format, "m4v" ) )
2568 default_acodec = HB_ACODEC_FAAC;
2570 else if( !strcasecmp( format, "ogm" ) ||
2571 !strcasecmp( format, "ogg" ) )
2574 default_acodec = HB_ACODEC_VORBIS;
2576 else if( !strcasecmp( format, "mkv" ) )
2579 default_acodec = HB_ACODEC_AC3;
2583 fprintf( stderr, "Invalid output format (%s). Possible "
2584 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2592 static int get_acodec_for_string( char *codec )
2594 if( !strcasecmp( codec, "ac3" ) )
2596 return HB_ACODEC_AC3;
2598 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2600 return HB_ACODEC_DCA;
2602 else if( !strcasecmp( codec, "lame" ) )
2604 return HB_ACODEC_LAME;
2606 else if( !strcasecmp( codec, "faac" ) )
2608 return HB_ACODEC_FAAC;
2610 else if( !strcasecmp( codec, "vorbis") )
2612 return HB_ACODEC_VORBIS;
2620 static int is_sample_rate_valid(int rate)
2623 for( i = 0; i < hb_audio_rates_count; i++ )
2625 if (rate == hb_audio_rates[i].rate)
2632 /****************************************************************************
2635 * Returns the BSD device name for the block device that contains the
2636 * passed-in path. Returns NULL on failure.
2637 ****************************************************************************/
2638 static char* bsd_name_for_path(char *path)
2642 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2648 // Get the volume reference number.
2649 FSCatalogInfo catalogInfo;
2650 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2656 FSVolumeRefNum volRefNum = catalogInfo.volume;
2658 // Now let's get the device name
2659 GetVolParmsInfoBuffer volumeParms;
2660 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2666 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the
2667 // vMDeviceID field. It is actually a char * value. This is mentioned in the
2668 // header CoreServices/CarbonCore/Files.h.
2669 return volumeParms.vMDeviceID;
2672 /****************************************************************************
2675 * Returns whether or not the passed in BSD device represents a DVD, or other
2677 ****************************************************************************/
2678 static int device_is_dvd(char *device)
2680 io_service_t service = get_iokit_service(device);
2681 if( service == IO_OBJECT_NULL )
2685 int result = is_dvd_service(service);
2686 IOObjectRelease(service);
2690 /****************************************************************************
2693 * Returns the IOKit service object for the passed in BSD device name.
2694 ****************************************************************************/
2695 static io_service_t get_iokit_service( char *device )
2697 CFMutableDictionaryRef matchingDict;
2698 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2699 if( matchingDict == NULL )
2701 return IO_OBJECT_NULL;
2703 // Fetch the object with the matching BSD node name. There should only be
2704 // one match, so IOServiceGetMatchingService is used instead of
2705 // IOServiceGetMatchingServices to simplify the code.
2706 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2709 /****************************************************************************
2712 * Returns whether or not the service passed in is a DVD.
2714 * Searches for an IOMedia object that represents the entire (whole) media that
2715 * the volume is on. If the volume is on partitioned media, the whole media
2716 * object will be a parent of the volume's media object. If the media is not
2717 * partitioned, the volume's media object will be the whole media object.
2718 ****************************************************************************/
2719 static int is_dvd_service( io_service_t service )
2721 kern_return_t kernResult;
2724 // Create an iterator across all parents of the service object passed in.
2725 kernResult = IORegistryEntryCreateIterator( service,
2727 kIORegistryIterateRecursively | kIORegistryIterateParents,
2729 if( kernResult != KERN_SUCCESS )
2733 if( iter == IO_OBJECT_NULL )
2738 // A reference on the initial service object is released in the do-while
2739 // loop below, so add a reference to balance.
2740 IOObjectRetain( service );
2745 if( is_whole_media_service( service ) &&
2746 IOObjectConformsTo( service, kIODVDMediaClass) )
2750 IOObjectRelease( service );
2751 } while( !result && (service = IOIteratorNext( iter )) );
2752 IOObjectRelease( iter );
2757 /****************************************************************************
2758 * is_whole_media_service
2760 * Returns whether or not the service passed in is an IOMedia service and
2761 * represents the "whole" media instead of just a partition.
2763 * The whole media object is indicated in the IORegistry by the presence of a
2764 * property with the key "Whole" and value "Yes".
2765 ****************************************************************************/
2766 static is_whole_media_service( io_service_t service )
2770 if( IOObjectConformsTo( service, kIOMediaClass ) )
2772 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2773 CFSTR( kIOMediaWholeKey ),
2774 kCFAllocatorDefault,
2780 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2781 CFRelease( wholeMedia );
2786 #endif // __APPLE_CC__