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;
95 static int preview_count = 10;
96 static int store_previews = 0;
98 /* Exit cleanly on Ctrl-C */
99 static volatile int die = 0;
100 static void SigHandler( int );
103 static void ShowCommands();
104 static void ShowHelp();
105 static void ShowPresets();
107 static int ParseOptions( int argc, char ** argv );
108 static int CheckOptions( int argc, char ** argv );
109 static int HandleEvents( hb_handle_t * h );
111 static int get_acodec_for_string( char *codec );
112 static int is_sample_rate_valid(int rate);
115 static char* bsd_name_for_path(char *path);
116 static int device_is_dvd(char *device);
117 static io_service_t get_iokit_service( char *device );
118 static int is_dvd_service( io_service_t service );
119 static is_whole_media_service( io_service_t service );
122 /* Only print the "Muxing..." message once */
123 static int show_mux_warning = 1;
125 /****************************************************************************
128 * When using the CLI just display using hb_log as we always did in the past
129 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
130 ****************************************************************************/
131 static void hb_cli_error_handler ( const char *errmsg )
133 fprintf( stderr, "ERROR: %s\n", errmsg );
136 int main( int argc, char ** argv )
142 audios = hb_list_init();
144 /* Parse command line */
145 if( ParseOptions( argc, argv ) ||
146 CheckOptions( argc, argv ) )
151 /* Register our error handler */
152 hb_register_error_handler(&hb_cli_error_handler);
155 h = hb_init( debug, update );
158 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
159 hb_get_version( h ), hb_get_build( h ) );
161 /* Check for update */
164 if( ( build = hb_check_update( h, &version ) ) > -1 )
166 fprintf( stderr, "You are using an old version of "
167 "HandBrake.\nLatest is %s (build %d).\n", version,
172 fprintf( stderr, "Your version of HandBrake is up to "
180 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
181 hb_get_cpu_count( h ) > 1 ? "s" : "" );
184 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
185 cpu > 1 ? "s" : "" );
186 hb_set_cpu_count( h, cpu );
189 /* Exit ASAP on Ctrl-C */
190 signal( SIGINT, SigHandler );
192 /* Feed libhb with a DVD to scan */
193 fprintf( stderr, "Opening %s...\n", input );
197 * We need to scan for all the titles in order to find the longest
202 hb_scan( h, input, titleindex, preview_count, store_previews );
207 #if !defined(SYS_BEOS)
217 FD_SET( STDIN_FILENO, &fds );
218 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
225 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
227 if( buf[size] == '\n' )
234 if( size >= 256 || buf[size] == '\n' )
239 fprintf( stdout, "\nEncoding Quit by user command\n" );
243 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
265 if( input ) free( input );
266 if( output ) free( output );
267 if( format ) free( format );
270 while( ( audio = hb_list_item( audios, 0 ) ) )
272 hb_list_rem( audios, audio );
273 if( audio->out.name )
275 free( audio->out.name );
279 hb_list_close( &audios );
281 if( mixdowns ) free( mixdowns );
282 if( dynamic_range_compression ) free( dynamic_range_compression );
283 if( atracks ) free( atracks );
284 if( arates ) free( arates );
285 if( abitrates ) free( abitrates );
286 if( acodecs ) free( acodecs );
287 if( anames ) free( anames );
288 if (native_language ) free (native_language );
289 if( x264opts ) free (x264opts );
290 if( x264opts2 ) free (x264opts2 );
291 if (preset_name) free (preset_name);
293 fprintf( stderr, "HandBrake has exited.\n" );
298 static void ShowCommands()
300 fprintf( stdout, "\nCommands:\n" );
301 fprintf( stdout, " [h]elp Show this message\n" );
302 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
303 fprintf( stdout, " [p]ause Pause encoding\n" );
304 fprintf( stdout, " [r]esume Resume encoding\n" );
307 static void PrintTitleInfo( hb_title_t * title )
309 hb_chapter_t * chapter;
310 hb_audio_config_t * audio;
311 hb_subtitle_t * subtitle;
314 fprintf( stderr, "+ title %d:\n", title->index );
315 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
316 title->vts, title->ttn, title->cell_start, title->cell_end,
317 title->block_count );
318 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
319 title->hours, title->minutes, title->seconds );
320 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
321 title->width, title->height,
322 (float) title->aspect,
323 (float) title->rate / title->rate_base );
324 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
325 title->crop[1], title->crop[2], title->crop[3] );
326 fprintf( stderr, " + chapters:\n" );
327 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
329 chapter = hb_list_item( title->list_chapter, i );
330 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
331 "%02d:%02d:%02d\n", chapter->index,
332 chapter->cell_start, chapter->cell_end,
333 chapter->block_count, chapter->hours, chapter->minutes,
336 fprintf( stderr, " + audio tracks:\n" );
337 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
339 audio = hb_list_audio_config_item( title->list_audio, i );
340 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
342 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
343 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
347 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
350 fprintf( stderr, " + subtitle tracks:\n" );
351 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
353 subtitle = hb_list_item( title->list_subtitle, i );
354 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
358 if(title->detected_interlacing)
360 /* Interlacing was found in half or more of the preview frames */
361 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
366 static int HandleEvents( hb_handle_t * h )
369 int tmp_num_audio_tracks;
371 hb_get_state( h, &s );
378 #define p s.param.scanning
379 case HB_STATE_SCANNING:
380 /* Show what title is currently being scanned */
381 fprintf( stderr, "Scanning title %d", p.title_cur );
383 fprintf( stderr, " of %d", p.title_count );
384 fprintf( stderr, "...\n" );
388 case HB_STATE_SCANDONE:
395 /* Audio argument string parsing variables */
399 int mixdown = HB_AMIXDOWN_DOLBYPLII;
401 /* Audio argument string parsing variables */
403 list = hb_get_titles( h );
405 if( !hb_list_count( list ) )
407 /* No valid title, stop right there */
408 fprintf( stderr, "No title found.\n" );
415 int longest_title_idx=0;
416 int longest_title_pos=-1;
417 int longest_title_time=0;
420 fprintf( stderr, "Searching for longest title...\n" );
422 for( i = 0; i < hb_list_count( list ); i++ )
424 title = hb_list_item( list, i );
425 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
426 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
427 i, title->index, title_time );
428 if( longest_title_time < title_time )
430 longest_title_time = title_time;
431 longest_title_pos = i;
432 longest_title_idx = title->index;
435 if( longest_title_pos == -1 )
437 fprintf( stderr, "No longest title found.\n" );
441 titleindex = longest_title_idx;
442 fprintf( stderr, "Found longest title, setting title to %d\n",
445 title = hb_list_item( list, longest_title_pos);
447 title = hb_list_item( list, 0 );
452 /* Scan-only mode, print infos and exit */
454 for( i = 0; i < hb_list_count( list ); i++ )
456 title = hb_list_item( list, i );
457 PrintTitleInfo( title );
463 /* Set job settings */
466 PrintTitleInfo( title );
468 if( chapter_start && chapter_end )
470 job->chapter_start = MAX( job->chapter_start,
472 job->chapter_end = MIN( job->chapter_end,
474 job->chapter_end = MAX( job->chapter_start,
480 fprintf( stderr, "+ Using preset: %s", preset_name);
482 if (!strcmp(preset_name, "Universal"))
485 vcodec = HB_VCODEC_X264;
486 job->vquality = 0.589999973773956;
490 atracks = strdup("1,1");
494 abitrates = strdup("160,auto");
498 arates = strdup("48,Auto");
502 acodecs = strdup("faac,ac3");
506 mixdowns = strdup("dpl2,auto");
511 x264opts = strdup("level=30:cabac=0:ref=3:mixed-refs=1:analyse=all:me=umh:no-fast-pskip=1");
514 job->chapter_markers = 1;
517 if (!strcmp(preset_name, "iPod"))
521 vcodec = HB_VCODEC_X264;
525 atracks = strdup("1");
529 abitrates = strdup("160");
533 arates = strdup("48");
537 acodecs = strdup("faac");
541 mixdowns = strdup("dpl2");
546 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
548 job->chapter_markers = 1;
551 if (!strcmp(preset_name, "iPhone & iPod Touch"))
554 vcodec = HB_VCODEC_X264;
555 job->vquality = 0.589999973773956;
559 atracks = strdup("1");
563 abitrates = strdup("128");
567 arates = strdup("48");
571 acodecs = strdup("faac");
575 mixdowns = strdup("dpl2");
580 x264opts = strdup("level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1");
582 job->chapter_markers = 1;
585 if (!strcmp(preset_name, "AppleTV"))
588 job->largeFileSize = 1;
589 vcodec = HB_VCODEC_X264;
590 job->vquality = 0.589999973773956;
594 atracks = strdup("1,1");
598 abitrates = strdup("160,auto");
602 arates = strdup("48,Auto");
606 acodecs = strdup("faac,ac3");
610 mixdowns = strdup("dpl2,auto");
615 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");
618 job->chapter_markers = 1;
621 if (!strcmp(preset_name, "QuickTime"))
624 vcodec = HB_VCODEC_X264;
625 job->vbitrate = 1800;
628 atracks = strdup("1");
632 abitrates = strdup("160");
636 arates = strdup("Auto");
640 acodecs = strdup("faac");
644 mixdowns = strdup("dpl2");
648 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");
651 job->chapter_markers = 1;
653 turbo_opts_enabled = 1;
656 if (!strcmp(preset_name, "AppleTV Legacy"))
659 job->largeFileSize = 1;
660 vcodec = HB_VCODEC_X264;
661 job->vbitrate = 2500;
664 atracks = strdup("1,1");
668 abitrates = strdup("160,auto");
672 arates = strdup("48,Auto");
676 acodecs = strdup("faac,ac3");
680 mixdowns = strdup("dpl2,auto");
684 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
687 job->chapter_markers = 1;
690 if (!strcmp(preset_name, "iPhone Legacy"))
694 vcodec = HB_VCODEC_X264;
698 atracks = strdup("1");
702 abitrates = strdup("128");
706 arates = strdup("48");
710 acodecs = strdup("faac");
714 mixdowns = strdup("dpl2");
719 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:trellis=1");
721 job->chapter_markers = 1;
724 if (!strcmp(preset_name, "iPod Legacy"))
728 vcodec = HB_VCODEC_X264;
729 job->vbitrate = 1500;
732 atracks = strdup("1");
736 abitrates = strdup("160");
740 arates = strdup("48");
744 acodecs = strdup("faac");
748 mixdowns = strdup("dpl2");
753 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
755 job->chapter_markers = 1;
758 if (!strcmp(preset_name, "Normal"))
761 vcodec = HB_VCODEC_X264;
762 job->vbitrate = 1500;
765 atracks = strdup("1");
769 abitrates = strdup("160");
773 arates = strdup("Auto");
777 acodecs = strdup("faac");
781 mixdowns = strdup("dpl2");
785 x264opts = strdup("ref=2:bframes=2:me=umh");
788 job->chapter_markers = 1;
790 turbo_opts_enabled = 1;
793 if (!strcmp(preset_name, "Classic"))
796 job->vbitrate = 1000;
799 atracks = strdup("1");
803 abitrates = strdup("160");
807 arates = strdup("Auto");
811 acodecs = strdup("faac");
815 mixdowns = strdup("dpl2");
819 if (!strcmp(preset_name, "Animation"))
822 vcodec = HB_VCODEC_X264;
823 job->vbitrate = 1000;
826 atracks = strdup("1");
830 abitrates = strdup("160");
834 arates = strdup("Auto");
838 acodecs = strdup("faac");
842 mixdowns = strdup("dpl2");
846 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");
851 job->chapter_markers = 1;
853 turbo_opts_enabled = 1;
856 if (!strcmp(preset_name, "Constant Quality Rate"))
859 vcodec = HB_VCODEC_X264;
860 job->vquality = 0.600000023841858;
864 atracks = strdup("1");
868 abitrates = strdup("auto");
872 arates = strdup("Auto");
876 acodecs = strdup("ac3");
880 mixdowns = strdup("auto");
884 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");
887 job->chapter_markers = 1;
890 if (!strcmp(preset_name, "Film"))
893 vcodec = HB_VCODEC_X264;
894 job->vbitrate = 1800;
897 atracks = strdup("1");
901 abitrates = strdup("auto");
905 arates = strdup("Auto");
909 acodecs = strdup("ac3");
913 mixdowns = strdup("auto");
917 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");
920 job->chapter_markers = 1;
922 turbo_opts_enabled = 1;
925 if (!strcmp(preset_name, "Television"))
928 vcodec = HB_VCODEC_X264;
929 job->vbitrate = 1300;
932 atracks = strdup("1");
936 abitrates = strdup("160");
940 arates = strdup("Auto");
944 acodecs = strdup("faac");
948 mixdowns = strdup("dpl2");
952 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");
957 job->chapter_markers = 1;
959 turbo_opts_enabled = 1;
962 if (!strcmp(preset_name, "PSP"))
965 job->vbitrate = 1024;
968 atracks = strdup("1");
972 abitrates = strdup("128");
976 arates = strdup("48");
980 acodecs = strdup("faac");
984 mixdowns = strdup("dpl2");
988 job->chapter_markers = 1;
991 if (!strcmp(preset_name, "PS3"))
994 vcodec = HB_VCODEC_X264;
995 job->vbitrate = 2500;
998 atracks = strdup("1");
1002 abitrates = strdup("160");
1006 arates = strdup("48");
1010 acodecs = strdup("faac");
1014 mixdowns = strdup("dpl2");
1022 x264opts = strdup("level=41:me=umh");
1027 if (!strcmp(preset_name, "Xbox 360"))
1030 vcodec = HB_VCODEC_X264;
1031 job->vbitrate = 2000;
1034 atracks = strdup("1");
1038 abitrates = strdup("160");
1042 arates = strdup("48");
1046 acodecs = strdup("faac");
1050 mixdowns = strdup("dpl2");
1054 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");
1060 if ( chapter_markers )
1062 job->chapter_markers = chapter_markers;
1064 if( marker_file != NULL )
1066 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1067 hb_csv_cell_t * cell;
1071 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1075 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1079 /* Parse the cells */
1080 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1082 /* We have a chapter number */
1083 if( cell->cell_col == 0 )
1085 row = cell->cell_row;
1086 chapter = atoi( cell->cell_text );
1089 /* We have a chapter name */
1090 if( cell->cell_col == 1 && row == cell->cell_row )
1092 /* If we have a valid chapter, copy the string an terminate it */
1093 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1095 hb_chapter_t * chapter_s;
1097 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1098 strncpy(chapter_s->title, cell->cell_text, 1023);
1099 chapter_s->title[1023] = '\0';
1104 hb_dispose_cell( cell );
1107 hb_close_csv_file( file );
1112 /* No marker file */
1114 int number_of_chapters = hb_list_count(job->title->list_chapter);
1117 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1119 hb_chapter_t * chapter_s;
1120 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1121 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1122 chapter_s->title[1023] = '\0';
1127 if( crop[0] >= 0 && crop[1] >= 0 &&
1128 crop[2] >= 0 && crop[3] >= 0 )
1130 memcpy( job->crop, crop, 4 * sizeof( int ) );
1133 job->deinterlace = deinterlace;
1134 job->grayscale = grayscale;
1135 if (loosePixelratio)
1137 job->pixel_ratio = 2;
1140 job->modulus = modulus;
1142 if( par_width && par_height )
1144 job->pixel_ratio = 3;
1145 job->pixel_aspect_width = par_width;
1146 job->pixel_aspect_height = par_height;
1151 job->pixel_ratio = pixelratio;
1154 /* Add selected filters */
1155 job->filters = hb_list_init();
1158 hb_filter_detelecine.settings = detelecine_opt;
1159 hb_list_add( job->filters, &hb_filter_detelecine );
1163 /* No framerate specified, so using same as source.
1164 That means VFR, so set detelecine up to drop frames. */
1170 hb_filter_decomb.settings = decomb_opt;
1171 hb_list_add( job->filters, &hb_filter_decomb );
1175 hb_filter_deinterlace.settings = deinterlace_opt;
1176 hb_list_add( job->filters, &hb_filter_deinterlace );
1180 hb_filter_deblock.settings = deblock_opt;
1181 hb_list_add( job->filters, &hb_filter_deblock );
1185 hb_filter_denoise.settings = denoise_opt;
1186 hb_list_add( job->filters, &hb_filter_denoise );
1189 if( width && height )
1192 job->height = height;
1197 hb_fix_aspect( job, HB_KEEP_WIDTH );
1199 else if( height && !loosePixelratio)
1201 job->height = height;
1202 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1204 else if( !width && !height && !pixelratio && !loosePixelratio )
1206 hb_fix_aspect( job, HB_KEEP_WIDTH );
1208 else if (!width && loosePixelratio)
1210 /* Default to full width when one isn't specified for loose anamorphic */
1211 job->width = title->width - job->crop[2] - job->crop[3];
1212 /* The height will be thrown away in hb.c but calculate it anyway */
1213 hb_fix_aspect( job, HB_KEEP_WIDTH );
1216 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1218 job->vquality = vquality;
1223 job->vquality = -1.0;
1224 job->vbitrate = vbitrate;
1228 job->vcodec = vcodec;
1232 job->h264_level = 13;
1236 job->h264_level = 30;
1241 job->vrate = 27000000;
1242 job->vrate_base = vrate;
1245 /* Grab audio tracks */
1248 char * token = strtok(atracks, ",");
1251 int track_start, track_end;
1252 while( token != NULL )
1254 audio = calloc(1, sizeof(*audio));
1255 hb_audio_config_init(audio);
1256 if (strlen(token) >= 3)
1258 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1261 for (i = track_start - 1; i < track_end; i++)
1263 if (i != track_start - 1)
1265 audio = calloc(1, sizeof(*audio));
1266 hb_audio_config_init(audio);
1268 audio->in.track = i;
1269 audio->out.track = num_audio_tracks++;
1270 hb_list_add(audios, audio);
1273 else if( !strcasecmp(token, "none" ) )
1275 audio->in.track = audio->out.track = -1;
1276 audio->out.codec = 0;
1277 hb_list_add(audios, audio);
1282 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1289 audio->in.track = atoi(token) - 1;
1290 audio->out.track = num_audio_tracks++;
1291 hb_list_add(audios, audio);
1293 token = strtok(NULL, ",");
1297 /* Parse audio tracks */
1298 if( hb_list_count(audios) == 0 )
1300 /* Create a new audio track with default settings */
1301 audio = calloc(1, sizeof(*audio));
1302 hb_audio_config_init(audio);
1303 /* Add it to our audios */
1304 hb_list_add(audios, audio);
1307 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1308 for (i = 0; i < tmp_num_audio_tracks; i++)
1310 audio = hb_list_item(audios, 0);
1311 if( (audio == NULL) || (audio->in.track == -1) ||
1312 (audio->out.track == -1) || (audio->out.codec == 0) )
1318 if( hb_audio_add( job, audio ) == 0 )
1320 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1321 audio->in.track + 1 );
1326 hb_list_rem(audios, audio);
1328 if( audio->out.name )
1330 free( audio->out.name);
1339 char * token = strtok(acodecs, ",");
1342 while ( token != NULL )
1344 if ((acodec = get_acodec_for_string(token)) == -1)
1346 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1347 acodec = default_acodec;
1349 if( i < num_audio_tracks )
1351 audio = hb_list_audio_config_item(job->list_audio, i);
1352 audio->out.codec = acodec;
1356 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1357 hb_audio_config_t audio;
1361 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1363 hb_audio_config_init(&audio);
1364 audio.in.track = last_audio->in.track;
1365 audio.out.track = num_audio_tracks++;
1366 audio.out.codec = acodec;
1367 hb_audio_add(job, &audio);
1371 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1374 token = strtok(NULL, ",");
1378 if( i < num_audio_tracks )
1380 /* We have fewer inputs than audio tracks, use the default codec for
1381 * this container for the remaining tracks. Unless we only have one input
1382 * then use that codec instead.
1385 acodec = default_acodec;
1386 for ( ; i < num_audio_tracks; i++)
1388 audio = hb_list_audio_config_item(job->list_audio, i);
1389 audio->out.codec = acodec;
1398 char * token = strtok(arates, ",");
1401 while ( token != NULL )
1403 arate = atoi(token);
1404 audio = hb_list_audio_config_item(job->list_audio, i);
1407 for( j = 0; j < hb_audio_rates_count; j++ )
1409 if( !strcmp( token, hb_audio_rates[j].string ) )
1411 arate = hb_audio_rates[j].rate;
1418 if (!is_sample_rate_valid(arate))
1420 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1421 arate = audio->in.samplerate;
1424 audio->out.samplerate = arate;
1425 if( (++i) >= num_audio_tracks )
1426 break; /* We have more inputs than audio tracks, oops */
1430 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1432 token = strtok(NULL, ",");
1435 if (i < num_audio_tracks)
1437 /* We have fewer inputs than audio tracks, use default sample rate.
1438 * Unless we only have one input, then use that for all tracks.
1441 arate = audio->in.samplerate;
1442 for ( ; i < num_audio_tracks; i++)
1444 audio = hb_list_audio_config_item(job->list_audio, i);
1445 audio->out.samplerate = arate;
1454 char * token = strtok(abitrates, ",");
1457 while ( token != NULL )
1459 abitrate = atoi(token);
1460 audio = hb_list_audio_config_item(job->list_audio, i);
1464 audio->out.bitrate = abitrate;
1465 if( (++i) >= num_audio_tracks )
1466 break; /* We have more inputs than audio tracks, oops */
1470 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1472 token = strtok(NULL, ",");
1475 if (i < num_audio_tracks)
1477 /* We have fewer inputs than audio tracks, use the default bitrate
1478 * for the remaining tracks. Unless we only have one input, then use
1479 * that for all tracks.
1482 abitrate = default_abitrate;
1483 for (; i < num_audio_tracks; i++)
1485 audio = hb_list_audio_config_item(job->list_audio, i);
1486 audio->out.bitrate = abitrate;
1493 if ( dynamic_range_compression )
1495 char * token = strtok(dynamic_range_compression, ",");
1497 token = dynamic_range_compression;
1498 while ( token != NULL )
1500 d_r_c = atof(token);
1501 audio = hb_list_audio_config_item(job->list_audio, i);
1504 audio->out.dynamic_range_compression = d_r_c;
1505 if( (++i) >= num_audio_tracks )
1506 break; /* We have more inputs than audio tracks, oops */
1510 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1512 token = strtok(NULL, ",");
1515 if (i < num_audio_tracks)
1517 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1518 * tracks. Unless we only have one input, then use the same DRC for all
1523 for (; i < num_audio_tracks; i++)
1525 audio = hb_list_audio_config_item(job->list_audio, i);
1526 audio->out.dynamic_range_compression = d_r_c;
1535 char * token = strtok(mixdowns, ",");
1538 while ( token != NULL )
1540 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1541 audio = hb_list_audio_config_item(job->list_audio, i);
1544 audio->out.mixdown = mixdown;
1545 if( (++i) >= num_audio_tracks )
1546 break; /* We have more inputs than audio tracks, oops */
1550 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1552 token = strtok(NULL, ",");
1555 if (i < num_audio_tracks)
1557 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1558 * we only have one input, then use that.
1561 mixdown = HB_AMIXDOWN_DOLBYPLII;
1562 for (; i < num_audio_tracks; i++)
1564 audio = hb_list_audio_config_item(job->list_audio, i);
1565 audio->out.mixdown = mixdown;
1570 /* Audio Track Names */
1574 char * token = strtok(anames, ",");
1577 while ( token != NULL )
1579 audio = hb_list_audio_config_item(job->list_audio, i);
1582 audio->out.name = strdup(token);
1583 if( (++i) >= num_audio_tracks )
1584 break; /* We have more names than audio tracks, oops */
1588 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1591 token = strtok(NULL, ",");
1594 if( i < num_audio_tracks && i == 1 )
1596 /* We have exactly one name and more than one audio track. Use the same
1597 * name for all tracks. */
1598 for ( ; i < num_audio_tracks; i++)
1600 audio = hb_list_audio_config_item(job->list_audio, i);
1601 audio->out.name = strdup(anames);
1604 /* Audio Track Names */
1608 job->vbitrate = hb_calc_bitrate( job, size );
1609 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1615 job->subtitle = sub - 1;
1618 if( native_language )
1620 job->native_language = strdup( native_language );
1628 if ( largeFileSize )
1630 job->largeFileSize = 1;
1634 job->mp4_optimize = 1;
1641 job->file = strdup( output );
1650 job->color_matrix = color_matrix;
1653 if( x264opts != NULL && *x264opts != '\0' )
1655 job->x264opts = x264opts;
1657 else /*avoids a bus error crash when options aren't specified*/
1659 job->x264opts = NULL;
1662 job->maxWidth = maxWidth;
1664 job->maxHeight = maxHeight;
1666 if( subtitle_force )
1668 job->subtitle_force = subtitle_force;
1676 * When subtitle scan is enabled do a fast pre-scan job
1677 * which will determine which subtitles to enable, if any.
1681 x264opts_tmp = job->x264opts;
1683 job->x264opts = NULL;
1685 job->indepth_scan = subtitle_scan;
1686 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1687 "subtitles if found for foreign language segments\n");
1688 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1689 *(job->select_subtitle) = NULL;
1692 * Add the pre-scan job
1696 job->x264opts = x264opts_tmp;
1702 * If subtitle_scan is enabled then only turn it on
1703 * for the first pass and then off again for the
1706 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1708 job->select_subtitle = NULL;
1712 job->indepth_scan = 0;
1716 x264opts2 = strdup(x264opts);
1720 * If turbo options have been selected then append them
1721 * to the x264opts now (size includes one ':' and the '\0')
1723 if( turbo_opts_enabled )
1725 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1728 tmp_x264opts = malloc(size * sizeof(char));
1731 snprintf( tmp_x264opts, size, "%s:%s",
1732 x264opts, turbo_opts );
1736 * No x264opts to modify, but apply the turbo options
1737 * anyway as they may be modifying defaults
1739 snprintf( tmp_x264opts, size, "%s",
1742 x264opts = tmp_x264opts;
1744 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1747 job->x264opts = x264opts;
1751 job->select_subtitle = subtitle_tmp;
1755 * On the second pass we turn off subtitle scan so that we
1756 * can actually encode using any subtitles that were auto
1757 * selected in the first pass (using the whacky select-subtitle
1758 * attribute of the job).
1760 job->indepth_scan = 0;
1762 job->x264opts = x264opts2;
1769 * Turn on subtitle scan if requested, note that this option
1770 * precludes encoding of any actual subtitles.
1773 job->indepth_scan = 0;
1781 #define p s.param.working
1782 case HB_STATE_WORKING:
1783 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1784 p.job_cur, p.job_count, 100.0 * p.progress );
1785 if( p.seconds > -1 )
1787 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1788 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1789 p.hours, p.minutes, p.seconds );
1795 #define p s.param.muxing
1796 case HB_STATE_MUXING:
1798 if (show_mux_warning)
1800 fprintf( stdout, "\rMuxing: this may take awhile..." );
1802 show_mux_warning = 0;
1808 #define p s.param.workdone
1809 case HB_STATE_WORKDONE:
1810 /* Print error if any, then exit */
1814 fprintf( stderr, "\nRip done!\n" );
1816 case HB_ERROR_CANCELED:
1817 fprintf( stderr, "\nRip canceled.\n" );
1820 fprintf( stderr, "\nRip failed (error %x).\n",
1830 /****************************************************************************
1832 ****************************************************************************/
1833 static volatile int64_t i_die_date = 0;
1834 void SigHandler( int i_signal )
1839 i_die_date = hb_get_date();
1840 fprintf( stderr, "Signal %d received, terminating - do it "
1841 "again in case it gets stuck\n", i_signal );
1843 else if( i_die_date + 500 < hb_get_date() )
1845 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1850 /****************************************************************************
1852 ****************************************************************************/
1853 static void ShowHelp()
1858 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1860 "### General Handbrake Options------------------------------------------------\n\n"
1861 " -h, --help Print help\n"
1862 " -u, --update Check for updates and exit\n"
1863 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1864 " -C, --cpu Set CPU count (default: autodetected)\n"
1865 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1866 " if the preset name has spaces, surround it with\n"
1867 " double quotation marks\n"
1868 " -z, --preset-list See a list of available built-in presets\n"
1871 "### Source Options-----------------------------------------------------------\n\n"
1872 " -i, --input <string> Set input device\n"
1873 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1875 " -L, --longest Select the longest title\n"
1876 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1877 " 1 to 3, or \"3\" for chapter 3 only,\n"
1878 " default: all chapters)\n"
1879 " --previews <#:B> Select how many preview images are generated (max 30),\n"
1880 " and whether or not they're stored to disk (0 or 1).\n"
1881 " (default: 10:0)\n"
1884 "### Destination Options------------------------------------------------------\n\n"
1885 " -o, --output <string> Set output file name\n"
1886 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1887 " autodetected from file name)\n"
1888 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1889 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1890 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1891 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1892 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1896 "### Video Options------------------------------------------------------------\n\n"
1897 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1898 " x264,theora default: ffmpeg)\n"
1899 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1900 " same style as mencoder:\n"
1901 " option1=value1:option2=value2\n"
1902 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1903 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1904 " -S, --size <MB> Set target size\n"
1905 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1906 " -2, --two-pass Use two-pass mode\n"
1907 " -T, --turbo When using 2-pass use the turbo options\n"
1908 " on the first pass to improve speed\n"
1909 " (only works with x264, affects PSNR by about 0.05dB,\n"
1910 " and increases first pass speed two to four times)\n"
1911 " -r, --rate Set video framerate (" );
1912 for( i = 0; i < hb_video_rates_count; i++ )
1914 fprintf( stderr, hb_video_rates[i].string );
1915 if( i != hb_video_rates_count - 1 )
1916 fprintf( stderr, "/" );
1918 fprintf( stderr, ")\n"
1919 " Be aware that not specifying a framerate lets\n"
1920 " HandBrake preserve a source's time stamps,\n"
1921 " potentially creating variable framerate video\n"
1924 "### Audio Options-----------------------------------------------------------\n\n"
1925 " -a, --audio <string> Select audio track(s), separated by commas\n"
1926 " More than one output track can be used for one\n"
1928 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1929 " tracks, default: first one)\n"
1930 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1931 " ac3 meaning passthrough\n"
1932 " Separated by commas for more than one audio track.\n"
1933 " (default: guessed)\n"
1934 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
1935 " Separated by commas for more than one audio track.\n"
1936 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1937 " Separated by commas for more than one audio track.\n"
1938 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1939 " -R, --arate Set audio samplerate(s) (" );
1940 for( i = 0; i < hb_audio_rates_count; i++ )
1942 fprintf( stderr, hb_audio_rates[i].string );
1943 if( i != hb_audio_rates_count - 1 )
1944 fprintf( stderr, "/" );
1946 fprintf( stderr, " kHz)\n"
1947 " Separated by commas for more than one audio track.\n"
1948 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1949 " making soft sounds louder. Range is 1.0 to 4.0\n"
1950 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1951 " Separated by commas for more than one audio track.\n"
1952 " -A, --aname <string> Audio track name(s),\n"
1953 " Separated by commas for more than one audio track.\n"
1956 "### Picture Settings---------------------------------------------------------\n\n"
1957 " -w, --width <number> Set picture width\n"
1958 " -l, --height <number> Set picture height\n"
1959 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1960 " -Y, --maxHeight <#> Set maximum height\n"
1961 " -X, --maxWidth <#> Set maximum width\n"
1962 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1963 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1964 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
1965 " the dimensions to divide cleanly by (default 16)\n"
1966 " and the pixel ratio to use (default autodetected)\n"
1967 " -M --color-matrix Set the color space signaled by the output\n"
1968 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
1969 " default: set by resolution)\n"
1972 "### Filters---------------------------------------------------------\n\n"
1974 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1975 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1977 " <fast/slow/slower>\n"
1978 " -5, --decomb Selectively deinterlaces when it detects combing\n"
1979 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
1980 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
1981 " Note: this filter drops duplicate frames to\n"
1982 " restore the pre-telecine framerate, unless you\n"
1983 " specify a constant framerate (--rate 29.97)\n"
1984 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1985 " -8, --denoise Denoise video with hqdn3d filter\n"
1986 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1988 " <weak/medium/strong>\n"
1989 " -7, --deblock Deblock video with pp7 filter\n"
1990 " <QP:M> (default 5:2)\n"
1991 " -g, --grayscale Grayscale encoding\n"
1994 "### Subtitle Options------------------------------------------------------------\n\n"
1995 " -s, --subtitle <number> Select subtitle (default: none)\n"
1996 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1997 " the one that's only used 10 percent of the time\n"
1998 " or less. This should locate subtitles for short\n"
1999 " foreign language segments. Best used in conjunction\n"
2000 " with --subtitle-forced.\n"
2001 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2002 " the subtitle has the forced flag set. May be used in\n"
2003 " conjunction with --subtitle-scan to auto-select\n"
2004 " a stream if it contains forced subtitles.\n"
2005 " -N, --native-language Select subtitles with this language if it does not\n"
2006 " <string> match the Audio language. Provide the language's\n"
2007 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2016 /****************************************************************************
2018 ****************************************************************************/
2019 static void ShowPresets()
2021 printf("\n< Apple\n");
2023 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");
2025 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");
2027 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");
2029 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");
2031 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");
2033 printf("\n << Legacy\n");
2035 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");
2037 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");
2039 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");
2045 printf("\n< Basic\n");
2047 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");
2049 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4\n");
2053 printf("\n< High Profile\n");
2055 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");
2057 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");
2059 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");
2061 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");
2065 printf("\n< Gaming Consoles\n");
2067 printf("\n + PSP: -b 1024 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 368 -Y 208 -m\n");
2069 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");
2071 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");
2077 /****************************************************************************
2079 ****************************************************************************/
2080 static int ParseOptions( int argc, char ** argv )
2083 #define PREVIEWS 257
2087 static struct option long_options[] =
2089 { "help", no_argument, NULL, 'h' },
2090 { "update", no_argument, NULL, 'u' },
2091 { "verbose", optional_argument, NULL, 'v' },
2092 { "cpu", required_argument, NULL, 'C' },
2094 { "format", required_argument, NULL, 'f' },
2095 { "input", required_argument, NULL, 'i' },
2096 { "output", required_argument, NULL, 'o' },
2097 { "large-file", no_argument, NULL, '4' },
2098 { "optimize", no_argument, NULL, 'O' },
2099 { "ipod-atom", no_argument, NULL, 'I' },
2101 { "title", required_argument, NULL, 't' },
2102 { "longest", no_argument, NULL, 'L' },
2103 { "chapters", required_argument, NULL, 'c' },
2104 { "markers", optional_argument, NULL, 'm' },
2105 { "audio", required_argument, NULL, 'a' },
2106 { "mixdown", required_argument, NULL, '6' },
2107 { "drc", required_argument, NULL, 'D' },
2108 { "subtitle", required_argument, NULL, 's' },
2109 { "subtitle-scan", no_argument, NULL, 'U' },
2110 { "subtitle-forced", no_argument, NULL, 'F' },
2111 { "native-language", required_argument, NULL,'N' },
2113 { "encoder", required_argument, NULL, 'e' },
2114 { "aencoder", required_argument, NULL, 'E' },
2115 { "two-pass", no_argument, NULL, '2' },
2116 { "deinterlace", optional_argument, NULL, 'd' },
2117 { "deblock", optional_argument, NULL, '7' },
2118 { "denoise", optional_argument, NULL, '8' },
2119 { "detelecine", optional_argument, NULL, '9' },
2120 { "decomb", optional_argument, NULL, '5' },
2121 { "grayscale", no_argument, NULL, 'g' },
2122 { "pixelratio", no_argument, NULL, 'p' },
2123 { "loosePixelratio", optional_argument, NULL, 'P' },
2124 { "width", required_argument, NULL, 'w' },
2125 { "height", required_argument, NULL, 'l' },
2126 { "crop", required_argument, NULL, 'n' },
2128 { "vb", required_argument, NULL, 'b' },
2129 { "quality", required_argument, NULL, 'q' },
2130 { "size", required_argument, NULL, 'S' },
2131 { "ab", required_argument, NULL, 'B' },
2132 { "rate", required_argument, NULL, 'r' },
2133 { "arate", required_argument, NULL, 'R' },
2134 { "cqp", no_argument, NULL, 'Q' },
2135 { "x264opts", required_argument, NULL, 'x' },
2136 { "turbo", no_argument, NULL, 'T' },
2137 { "maxHeight", required_argument, NULL, 'Y' },
2138 { "maxWidth", required_argument, NULL, 'X' },
2139 { "preset", required_argument, NULL, 'Z' },
2140 { "preset-list", no_argument, NULL, 'z' },
2142 { "aname", required_argument, NULL, 'A' },
2143 { "color-matrix",required_argument, NULL, 'M' },
2144 { "previews", required_argument, NULL, PREVIEWS },
2149 int option_index = 0;
2152 c = getopt_long( argc, argv,
2153 "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",
2154 long_options, &option_index );
2169 if( optarg != NULL )
2171 debug = atoi( optarg );
2179 cpu = atoi( optarg );
2184 preset_name = strdup(optarg);
2191 format = strdup( optarg );
2194 input = strdup( optarg );
2196 char *devName = bsd_name_for_path( input );
2197 if( devName == NULL )
2201 if( device_is_dvd( devName ) )
2203 char *newInput = malloc( strlen("/dev/") + strlen( devName ) + 1);
2204 sprintf( newInput, "/dev/%s", devName );
2211 output = strdup( optarg );
2224 titleindex = atoi( optarg );
2232 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2234 chapter_start = start;
2237 else if( sscanf( optarg, "%d", &start ) == 1 )
2239 chapter_start = start;
2240 chapter_end = chapter_start;
2244 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2251 if( optarg != NULL )
2253 marker_file = strdup( optarg );
2255 chapter_markers = 1;
2258 if( optarg != NULL )
2260 atracks = strdup( optarg );
2268 if( optarg != NULL )
2270 mixdowns = strdup( optarg );
2274 if( optarg != NULL )
2276 dynamic_range_compression = strdup( optarg );
2280 sub = atoi( optarg );
2289 native_language = strdup( optarg );
2295 if( optarg != NULL )
2297 if (!( strcmp( optarg, "fast" ) ))
2299 deinterlace_opt = "-1";
2301 else if (!( strcmp( optarg, "slow" ) ))
2303 deinterlace_opt = "2";
2305 else if (!( strcmp( optarg, "slower" ) ))
2307 deinterlace_opt = "0";
2311 deinterlace_opt = strdup( optarg );
2317 if( optarg != NULL )
2319 deblock_opt = strdup( optarg );
2324 if( optarg != NULL )
2326 if (!( strcmp( optarg, "weak" ) ))
2328 denoise_opt = "2:1:2:3";
2330 else if (!( strcmp( optarg, "medium" ) ))
2332 denoise_opt = "3:2:2:3";
2334 else if (!( strcmp( optarg, "strong" ) ))
2336 denoise_opt = "7:7:5:5";
2340 denoise_opt = strdup( optarg );
2346 if( optarg != NULL )
2348 detelecine_opt = strdup( optarg );
2353 if( optarg != NULL )
2355 decomb_opt = strdup( optarg );
2366 loosePixelratio = 1;
2367 if( optarg != NULL )
2369 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
2373 if( !strcasecmp( optarg, "ffmpeg" ) )
2375 vcodec = HB_VCODEC_FFMPEG;
2377 else if( !strcasecmp( optarg, "xvid" ) )
2379 vcodec = HB_VCODEC_XVID;
2381 else if( !strcasecmp( optarg, "x264" ) )
2383 vcodec = HB_VCODEC_X264;
2385 else if( !strcasecmp( optarg, "x264b13" ) )
2387 vcodec = HB_VCODEC_X264;
2390 else if( !strcasecmp( optarg, "x264b30" ) )
2392 vcodec = HB_VCODEC_X264;
2395 else if( !strcasecmp( optarg, "theora" ) )
2397 vcodec = HB_VCODEC_THEORA;
2401 fprintf( stderr, "invalid codec (%s)\n", optarg );
2406 if( optarg != NULL )
2408 acodecs = strdup( optarg );
2412 width = atoi( optarg );
2415 height = atoi( optarg );
2420 char * tmp = optarg;
2421 for( i = 0; i < 4; i++ )
2425 crop[i] = strtol( tmp, &tmp, 0 );
2434 for( i = 0; i < hb_video_rates_count; i++ )
2436 if( !strcmp( optarg, hb_video_rates[i].string ) )
2438 vrate = hb_video_rates[i].rate;
2444 fprintf( stderr, "invalid framerate %s\n", optarg );
2449 if( optarg != NULL )
2451 arates = strdup( optarg );
2455 vbitrate = atoi( optarg );
2458 vquality = atof( optarg );
2461 size = atoi( optarg );
2464 if( optarg != NULL )
2466 abitrates = strdup( optarg );
2473 x264opts = strdup( optarg );
2476 turbo_opts_enabled = 1;
2479 maxHeight = atoi( optarg );
2482 maxWidth = atoi (optarg );
2485 if( optarg != NULL )
2487 anames = strdup( optarg );
2491 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2494 if( atoi( optarg ) == 601 )
2496 else if( atoi( optarg ) == 709 )
2500 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2508 static int CheckOptions( int argc, char ** argv )
2515 if( input == NULL || *input == '\0' )
2517 fprintf( stderr, "Missing input device. Run %s --help for "
2518 "syntax.\n", argv[0] );
2523 if( titleindex > 0 )
2525 if( output == NULL || *output == '\0' )
2527 fprintf( stderr, "Missing output file name. Run %s --help "
2528 "for syntax.\n", argv[0] );
2534 char * p = strrchr( output, '.' );
2537 if( p && !strcasecmp( p, ".avi" ) )
2540 default_acodec = HB_ACODEC_LAME;
2542 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2543 !strcasecmp( p, ".m4v" ) ) )
2549 default_acodec = HB_ACODEC_FAAC;
2551 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2552 !strcasecmp( p, ".ogg" ) ) )
2555 default_acodec = HB_ACODEC_VORBIS;
2557 else if( p && !strcasecmp(p, ".mkv" ) )
2560 default_acodec = HB_ACODEC_AC3;
2564 fprintf( stderr, "Output format couldn't be guessed "
2565 "from file name, using default.\n" );
2569 else if( !strcasecmp( format, "avi" ) )
2572 default_acodec = HB_ACODEC_LAME;
2574 else if( !strcasecmp( format, "mp4" ) ||
2575 !strcasecmp( format, "m4v" ) )
2581 default_acodec = HB_ACODEC_FAAC;
2583 else if( !strcasecmp( format, "ogm" ) ||
2584 !strcasecmp( format, "ogg" ) )
2587 default_acodec = HB_ACODEC_VORBIS;
2589 else if( !strcasecmp( format, "mkv" ) )
2592 default_acodec = HB_ACODEC_AC3;
2596 fprintf( stderr, "Invalid output format (%s). Possible "
2597 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2605 static int get_acodec_for_string( char *codec )
2607 if( !strcasecmp( codec, "ac3" ) )
2609 return HB_ACODEC_AC3;
2611 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2613 return HB_ACODEC_DCA;
2615 else if( !strcasecmp( codec, "lame" ) )
2617 return HB_ACODEC_LAME;
2619 else if( !strcasecmp( codec, "faac" ) )
2621 return HB_ACODEC_FAAC;
2623 else if( !strcasecmp( codec, "vorbis") )
2625 return HB_ACODEC_VORBIS;
2633 static int is_sample_rate_valid(int rate)
2636 for( i = 0; i < hb_audio_rates_count; i++ )
2638 if (rate == hb_audio_rates[i].rate)
2645 /****************************************************************************
2648 * Returns the BSD device name for the block device that contains the
2649 * passed-in path. Returns NULL on failure.
2650 ****************************************************************************/
2651 static char* bsd_name_for_path(char *path)
2655 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2661 // Get the volume reference number.
2662 FSCatalogInfo catalogInfo;
2663 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2669 FSVolumeRefNum volRefNum = catalogInfo.volume;
2671 // Now let's get the device name
2672 GetVolParmsInfoBuffer volumeParms;
2673 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2679 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the
2680 // vMDeviceID field. It is actually a char * value. This is mentioned in the
2681 // header CoreServices/CarbonCore/Files.h.
2682 return volumeParms.vMDeviceID;
2685 /****************************************************************************
2688 * Returns whether or not the passed in BSD device represents a DVD, or other
2690 ****************************************************************************/
2691 static int device_is_dvd(char *device)
2693 io_service_t service = get_iokit_service(device);
2694 if( service == IO_OBJECT_NULL )
2698 int result = is_dvd_service(service);
2699 IOObjectRelease(service);
2703 /****************************************************************************
2706 * Returns the IOKit service object for the passed in BSD device name.
2707 ****************************************************************************/
2708 static io_service_t get_iokit_service( char *device )
2710 CFMutableDictionaryRef matchingDict;
2711 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2712 if( matchingDict == NULL )
2714 return IO_OBJECT_NULL;
2716 // Fetch the object with the matching BSD node name. There should only be
2717 // one match, so IOServiceGetMatchingService is used instead of
2718 // IOServiceGetMatchingServices to simplify the code.
2719 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2722 /****************************************************************************
2725 * Returns whether or not the service passed in is a DVD.
2727 * Searches for an IOMedia object that represents the entire (whole) media that
2728 * the volume is on. If the volume is on partitioned media, the whole media
2729 * object will be a parent of the volume's media object. If the media is not
2730 * partitioned, the volume's media object will be the whole media object.
2731 ****************************************************************************/
2732 static int is_dvd_service( io_service_t service )
2734 kern_return_t kernResult;
2737 // Create an iterator across all parents of the service object passed in.
2738 kernResult = IORegistryEntryCreateIterator( service,
2740 kIORegistryIterateRecursively | kIORegistryIterateParents,
2742 if( kernResult != KERN_SUCCESS )
2746 if( iter == IO_OBJECT_NULL )
2751 // A reference on the initial service object is released in the do-while
2752 // loop below, so add a reference to balance.
2753 IOObjectRetain( service );
2758 if( is_whole_media_service( service ) &&
2759 IOObjectConformsTo( service, kIODVDMediaClass) )
2763 IOObjectRelease( service );
2764 } while( !result && (service = IOIteratorNext( iter )) );
2765 IOObjectRelease( iter );
2770 /****************************************************************************
2771 * is_whole_media_service
2773 * Returns whether or not the service passed in is an IOMedia service and
2774 * represents the "whole" media instead of just a partition.
2776 * The whole media object is indicated in the IORegistry by the presence of a
2777 * property with the key "Whole" and value "Yes".
2778 ****************************************************************************/
2779 static is_whole_media_service( io_service_t service )
2783 if( IOObjectConformsTo( service, kIOMediaClass ) )
2785 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2786 CFSTR( kIOMediaWholeKey ),
2787 kCFAllocatorDefault,
2793 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2794 CFRelease( wholeMedia );
2799 #endif // __APPLE_CC__