1 /* $Id: test.c,v 1.82 2005/11/19 08:25:54 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
18 #import <CoreServices/CoreServices.h>
19 #include <IOKit/IOKitLib.h>
20 #include <IOKit/storage/IOMedia.h>
21 #include <IOKit/storage/IODVDMedia.h>
25 static int debug = HB_DEBUG_NONE;
26 static int update = 0;
27 static int dvdnav = 0;
28 static char * input = NULL;
29 static char * output = NULL;
30 static char * format = NULL;
31 static int titleindex = 1;
32 static int longest_title = 0;
33 static int subtitle_scan = 0;
34 static int subtitle_force = 0;
35 static char * native_language = NULL;
36 static int twoPass = 0;
37 static int deinterlace = 0;
38 static char * deinterlace_opt = 0;
39 static int deblock = 0;
40 static char * deblock_opt = 0;
41 static int denoise = 0;
42 static char * denoise_opt = 0;
43 static int detelecine = 0;
44 static char * detelecine_opt = 0;
45 static int decomb = 0;
46 static char * decomb_opt = 0;
47 static int grayscale = 0;
48 static int vcodec = HB_VCODEC_FFMPEG;
49 static int h264_13 = 0;
50 static int h264_30 = 0;
51 static hb_list_t * audios = NULL;
52 static hb_audio_config_t * audio = NULL;
53 static int num_audio_tracks = 0;
54 static char * mixdowns = NULL;
55 static char * dynamic_range_compression = NULL;
56 static char * atracks = NULL;
57 static char * arates = NULL;
58 static char * abitrates = NULL;
59 static char * acodecs = NULL;
60 static char * anames = NULL;
61 static int default_acodec = HB_ACODEC_FAAC;
62 static int default_arate = 48000;
63 static int default_abitrate = 160;
66 static int height = 0;
67 static int crop[4] = { -1,-1,-1,-1 };
70 static float vquality = -1.0;
71 static int vbitrate = 0;
74 static int pixelratio = 0;
75 static int loosePixelratio = 0;
76 static int modulus = 0;
77 static int par_height = 0;
78 static int par_width = 0;
80 static int chapter_start = 0;
81 static int chapter_end = 0;
82 static int chapter_markers = 0;
83 static char * marker_file = NULL;
85 static char *x264opts = NULL;
86 static char *x264opts2 = NULL;
87 static int maxHeight = 0;
88 static int maxWidth = 0;
89 static int turbo_opts_enabled = 0;
90 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
91 static int largeFileSize = 0;
92 static int preset = 0;
93 static char * preset_name = 0;
95 static int mp4_optimize = 0;
96 static int ipod_atom = 0;
97 static int color_matrix = 0;
98 static int preview_count = 10;
99 static int store_previews = 0;
100 static int start_at_preview = 0;
101 static int64_t stop_at_pts = 0;
102 static int stop_at_frame = 0;
103 static char * stop_at_string = NULL;
104 static char * stop_at_token = NULL;
106 /* Exit cleanly on Ctrl-C */
107 static volatile int die = 0;
108 static void SigHandler( int );
111 static void ShowCommands();
112 static void ShowHelp();
113 static void ShowPresets();
115 static int ParseOptions( int argc, char ** argv );
116 static int CheckOptions( int argc, char ** argv );
117 static int HandleEvents( hb_handle_t * h );
119 static int get_acodec_for_string( char *codec );
120 static int is_sample_rate_valid(int rate);
123 static char* bsd_name_for_path(char *path);
124 static int device_is_dvd(char *device);
125 static io_service_t get_iokit_service( char *device );
126 static int is_dvd_service( io_service_t service );
127 static is_whole_media_service( io_service_t service );
130 /* Only print the "Muxing..." message once */
131 static int show_mux_warning = 1;
133 /****************************************************************************
136 * When using the CLI just display using hb_log as we always did in the past
137 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
138 ****************************************************************************/
139 static void hb_cli_error_handler ( const char *errmsg )
141 fprintf( stderr, "ERROR: %s\n", errmsg );
144 int main( int argc, char ** argv )
150 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
151 * force it to unbuffered otherwise informative output is not easily parsed.
153 #if defined( _WIN32 ) || defined( __MINGW32__ )
154 setvbuf( stdout, NULL, _IONBF, 0 );
155 setvbuf( stderr, NULL, _IONBF, 0 );
158 audios = hb_list_init();
160 /* Parse command line */
161 if( ParseOptions( argc, argv ) ||
162 CheckOptions( argc, argv ) )
167 #ifdef PTW32_STATIC_LIB
168 pthread_win32_process_attach_np();
169 pthread_win32_thread_attach_np();
172 /* Register our error handler */
173 hb_register_error_handler(&hb_cli_error_handler);
176 h = hb_init( debug, update );
177 hb_dvd_set_dvdnav( dvdnav );
180 fprintf( stderr, "%s - %s - %s\n",
181 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
183 /* Check for update */
186 if( ( build = hb_check_update( h, &version ) ) > -1 )
188 fprintf( stderr, "You are using an old version of "
189 "HandBrake.\nLatest is %s (build %d).\n", version,
194 fprintf( stderr, "Your version of HandBrake is up to "
202 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
203 hb_get_cpu_count( h ) > 1 ? "s" : "" );
206 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
207 cpu > 1 ? "s" : "" );
208 hb_set_cpu_count( h, cpu );
211 /* Exit ASAP on Ctrl-C */
212 signal( SIGINT, SigHandler );
214 /* Feed libhb with a DVD to scan */
215 fprintf( stderr, "Opening %s...\n", input );
219 * We need to scan for all the titles in order to find the longest
224 hb_scan( h, input, titleindex, preview_count, store_previews );
229 #if !defined(SYS_BEOS) && !defined(__MINGW32__)
239 FD_SET( STDIN_FILENO, &fds );
240 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
247 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
249 if( buf[size] == '\n' )
256 if( size >= 256 || buf[size] == '\n' )
261 fprintf( stdout, "\nEncoding Quit by user command\n" );
265 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
287 if( input ) free( input );
288 if( output ) free( output );
289 if( format ) free( format );
292 while( ( audio = hb_list_item( audios, 0 ) ) )
294 hb_list_rem( audios, audio );
295 if( audio->out.name )
297 free( audio->out.name );
301 hb_list_close( &audios );
303 if( mixdowns ) free( mixdowns );
304 if( dynamic_range_compression ) free( dynamic_range_compression );
305 if( atracks ) free( atracks );
306 if( arates ) free( arates );
307 if( abitrates ) free( abitrates );
308 if( acodecs ) free( acodecs );
309 if( anames ) free( anames );
310 if (native_language ) free (native_language );
311 if( x264opts ) free (x264opts );
312 if( x264opts2 ) free (x264opts2 );
313 if (preset_name) free (preset_name);
314 if( stop_at_string ) free( stop_at_string );
316 fprintf( stderr, "HandBrake has exited.\n" );
318 #ifdef PTW32_STATIC_LIB
319 pthread_win32_thread_detach_np();
320 pthread_win32_process_detach_np();
326 static void ShowCommands()
328 fprintf( stdout, "\nCommands:\n" );
329 fprintf( stdout, " [h]elp Show this message\n" );
330 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
331 fprintf( stdout, " [p]ause Pause encoding\n" );
332 fprintf( stdout, " [r]esume Resume encoding\n" );
335 static void PrintTitleInfo( hb_title_t * title )
337 hb_chapter_t * chapter;
338 hb_audio_config_t * audio;
339 hb_subtitle_t * subtitle;
342 fprintf( stderr, "+ title %d:\n", title->index );
343 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
344 title->vts, title->ttn, title->cell_start, title->cell_end,
345 title->block_count );
347 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
348 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
349 title->hours, title->minutes, title->seconds );
350 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
351 title->width, title->height,
352 (float) title->aspect,
353 (float) title->rate / title->rate_base );
354 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
355 title->crop[1], title->crop[2], title->crop[3] );
356 fprintf( stderr, " + chapters:\n" );
357 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
359 chapter = hb_list_item( title->list_chapter, i );
360 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
361 "%02d:%02d:%02d\n", chapter->index,
362 chapter->cell_start, chapter->cell_end,
363 chapter->block_count, chapter->hours, chapter->minutes,
366 fprintf( stderr, " + audio tracks:\n" );
367 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
369 audio = hb_list_audio_config_item( title->list_audio, i );
370 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
372 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
373 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
377 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
380 fprintf( stderr, " + subtitle tracks:\n" );
381 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
383 subtitle = hb_list_item( title->list_subtitle, i );
384 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
388 if(title->detected_interlacing)
390 /* Interlacing was found in half or more of the preview frames */
391 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
396 static int HandleEvents( hb_handle_t * h )
399 int tmp_num_audio_tracks;
401 hb_get_state( h, &s );
408 #define p s.param.scanning
409 case HB_STATE_SCANNING:
410 /* Show what title is currently being scanned */
411 fprintf( stderr, "Scanning title %d", p.title_cur );
413 fprintf( stderr, " of %d", p.title_count );
414 fprintf( stderr, "...\n" );
418 case HB_STATE_SCANDONE:
425 /* Audio argument string parsing variables */
429 int mixdown = HB_AMIXDOWN_DOLBYPLII;
431 /* Audio argument string parsing variables */
433 list = hb_get_titles( h );
435 if( !hb_list_count( list ) )
437 /* No valid title, stop right there */
438 fprintf( stderr, "No title found.\n" );
445 int longest_title_idx=0;
446 int longest_title_pos=-1;
447 int longest_title_time=0;
450 fprintf( stderr, "Searching for longest title...\n" );
452 for( i = 0; i < hb_list_count( list ); i++ )
454 title = hb_list_item( list, i );
455 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
456 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
457 i, title->index, title_time );
458 if( longest_title_time < title_time )
460 longest_title_time = title_time;
461 longest_title_pos = i;
462 longest_title_idx = title->index;
465 if( longest_title_pos == -1 )
467 fprintf( stderr, "No longest title found.\n" );
471 titleindex = longest_title_idx;
472 fprintf( stderr, "Found longest title, setting title to %d\n",
475 title = hb_list_item( list, longest_title_pos);
477 title = hb_list_item( list, 0 );
482 /* Scan-only mode, print infos and exit */
484 for( i = 0; i < hb_list_count( list ); i++ )
486 title = hb_list_item( list, i );
487 PrintTitleInfo( title );
493 /* Set job settings */
496 PrintTitleInfo( title );
498 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
500 job->chapter_start = MAX( job->chapter_start,
502 job->chapter_end = MIN( job->chapter_end,
504 job->chapter_end = MAX( job->chapter_start,
515 fprintf( stderr, "+ Using preset: %s", preset_name);
517 if (!strcmp(preset_name, "Universal"))
520 vcodec = HB_VCODEC_X264;
521 job->vquality = 0.589999973773956;
525 atracks = strdup("1,1");
529 abitrates = strdup("160,auto");
533 arates = strdup("48,Auto");
537 acodecs = strdup("faac,ac3");
541 mixdowns = strdup("dpl2,auto");
546 x264opts = strdup("level=30:cabac=0:ref=3:mixed-refs=1:analyse=all:me=umh:no-fast-pskip=1");
549 job->chapter_markers = 1;
552 if (!strcmp(preset_name, "iPod"))
556 vcodec = HB_VCODEC_X264;
560 atracks = strdup("1");
564 abitrates = strdup("160");
568 arates = strdup("48");
572 acodecs = strdup("faac");
576 mixdowns = strdup("dpl2");
581 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
583 job->chapter_markers = 1;
586 if (!strcmp(preset_name, "iPhone & iPod Touch"))
589 vcodec = HB_VCODEC_X264;
590 job->vquality = 0.589999973773956;
594 atracks = strdup("1");
598 abitrates = strdup("128");
602 arates = strdup("48");
606 acodecs = strdup("faac");
610 mixdowns = strdup("dpl2");
615 x264opts = strdup("level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1");
617 job->chapter_markers = 1;
620 if (!strcmp(preset_name, "AppleTV"))
623 job->largeFileSize = 1;
624 vcodec = HB_VCODEC_X264;
625 job->vquality = 0.589999973773956;
629 atracks = strdup("1,1");
633 abitrates = strdup("160,auto");
637 arates = strdup("48,Auto");
641 acodecs = strdup("faac,ac3");
645 mixdowns = strdup("dpl2,auto");
650 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");
653 job->chapter_markers = 1;
656 if (!strcmp(preset_name, "QuickTime"))
659 vcodec = HB_VCODEC_X264;
660 job->vbitrate = 1800;
663 atracks = strdup("1");
667 abitrates = strdup("160");
671 arates = strdup("Auto");
675 acodecs = strdup("faac");
679 mixdowns = strdup("dpl2");
683 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");
686 job->chapter_markers = 1;
688 turbo_opts_enabled = 1;
691 if (!strcmp(preset_name, "AppleTV Legacy"))
694 job->largeFileSize = 1;
695 vcodec = HB_VCODEC_X264;
696 job->vbitrate = 2500;
699 atracks = strdup("1,1");
703 abitrates = strdup("160,auto");
707 arates = strdup("48,Auto");
711 acodecs = strdup("faac,ac3");
715 mixdowns = strdup("dpl2,auto");
719 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
722 job->chapter_markers = 1;
725 if (!strcmp(preset_name, "iPhone Legacy"))
729 vcodec = HB_VCODEC_X264;
733 atracks = strdup("1");
737 abitrates = strdup("128");
741 arates = strdup("48");
745 acodecs = strdup("faac");
749 mixdowns = strdup("dpl2");
754 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:trellis=1");
756 job->chapter_markers = 1;
759 if (!strcmp(preset_name, "iPod Legacy"))
763 vcodec = HB_VCODEC_X264;
764 job->vbitrate = 1500;
767 atracks = strdup("1");
771 abitrates = strdup("160");
775 arates = strdup("48");
779 acodecs = strdup("faac");
783 mixdowns = strdup("dpl2");
788 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
790 job->chapter_markers = 1;
793 if (!strcmp(preset_name, "Normal"))
796 vcodec = HB_VCODEC_X264;
797 job->vbitrate = 1500;
800 atracks = strdup("1");
804 abitrates = strdup("160");
808 arates = strdup("Auto");
812 acodecs = strdup("faac");
816 mixdowns = strdup("dpl2");
820 x264opts = strdup("ref=2:bframes=2:me=umh");
823 job->chapter_markers = 1;
825 turbo_opts_enabled = 1;
828 if (!strcmp(preset_name, "Classic"))
831 job->vbitrate = 1000;
834 atracks = strdup("1");
838 abitrates = strdup("160");
842 arates = strdup("Auto");
846 acodecs = strdup("faac");
850 mixdowns = strdup("dpl2");
854 if (!strcmp(preset_name, "Animation"))
857 vcodec = HB_VCODEC_X264;
858 job->vbitrate = 1000;
861 atracks = strdup("1");
865 abitrates = strdup("160");
869 arates = strdup("Auto");
873 acodecs = strdup("faac");
877 mixdowns = strdup("dpl2");
881 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");
886 job->chapter_markers = 1;
888 turbo_opts_enabled = 1;
891 if (!strcmp(preset_name, "Constant Quality Rate"))
894 vcodec = HB_VCODEC_X264;
895 job->vquality = 0.600000023841858;
899 atracks = strdup("1");
903 abitrates = strdup("auto");
907 arates = strdup("Auto");
911 acodecs = strdup("ac3");
915 mixdowns = strdup("auto");
919 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");
922 job->chapter_markers = 1;
925 if (!strcmp(preset_name, "Film"))
928 vcodec = HB_VCODEC_X264;
929 job->vbitrate = 1800;
932 atracks = strdup("1");
936 abitrates = strdup("auto");
940 arates = strdup("Auto");
944 acodecs = strdup("ac3");
948 mixdowns = strdup("auto");
952 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");
955 job->chapter_markers = 1;
957 turbo_opts_enabled = 1;
960 if (!strcmp(preset_name, "Television"))
963 vcodec = HB_VCODEC_X264;
964 job->vbitrate = 1300;
967 atracks = strdup("1");
971 abitrates = strdup("160");
975 arates = strdup("Auto");
979 acodecs = strdup("faac");
983 mixdowns = strdup("dpl2");
987 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");
992 job->chapter_markers = 1;
994 turbo_opts_enabled = 1;
997 if (!strcmp(preset_name, "PSP"))
1000 job->vbitrate = 1024;
1003 atracks = strdup("1");
1007 abitrates = strdup("128");
1011 arates = strdup("48");
1015 acodecs = strdup("faac");
1019 mixdowns = strdup("dpl2");
1023 job->chapter_markers = 1;
1026 if (!strcmp(preset_name, "PS3"))
1029 vcodec = HB_VCODEC_X264;
1030 job->vbitrate = 2500;
1033 atracks = strdup("1");
1037 abitrates = strdup("160");
1041 arates = strdup("48");
1045 acodecs = strdup("faac");
1049 mixdowns = strdup("dpl2");
1057 x264opts = strdup("level=41:me=umh");
1062 if (!strcmp(preset_name, "Xbox 360"))
1065 vcodec = HB_VCODEC_X264;
1066 job->vbitrate = 2000;
1069 atracks = strdup("1");
1073 abitrates = strdup("160");
1077 arates = strdup("48");
1081 acodecs = strdup("faac");
1085 mixdowns = strdup("dpl2");
1089 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");
1095 if ( chapter_markers )
1097 job->chapter_markers = chapter_markers;
1099 if( marker_file != NULL )
1101 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1102 hb_csv_cell_t * cell;
1106 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1110 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1114 /* Parse the cells */
1115 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1117 /* We have a chapter number */
1118 if( cell->cell_col == 0 )
1120 row = cell->cell_row;
1121 chapter = atoi( cell->cell_text );
1124 /* We have a chapter name */
1125 if( cell->cell_col == 1 && row == cell->cell_row )
1127 /* If we have a valid chapter, copy the string an terminate it */
1128 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1130 hb_chapter_t * chapter_s;
1132 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1133 strncpy(chapter_s->title, cell->cell_text, 1023);
1134 chapter_s->title[1023] = '\0';
1139 hb_dispose_cell( cell );
1142 hb_close_csv_file( file );
1147 /* No marker file */
1149 int number_of_chapters = hb_list_count(job->title->list_chapter);
1152 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1154 hb_chapter_t * chapter_s;
1155 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1156 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1157 chapter_s->title[1023] = '\0';
1162 if( crop[0] >= 0 && crop[1] >= 0 &&
1163 crop[2] >= 0 && crop[3] >= 0 )
1165 memcpy( job->crop, crop, 4 * sizeof( int ) );
1168 job->deinterlace = deinterlace;
1169 job->grayscale = grayscale;
1170 if (loosePixelratio)
1172 job->anamorphic.mode = 2;
1175 job->anamorphic.modulus = modulus;
1177 if( par_width && par_height )
1179 job->anamorphic.mode = 3;
1180 job->anamorphic.par_width = par_width;
1181 job->anamorphic.par_height = par_height;
1186 job->anamorphic.mode = pixelratio;
1189 /* Add selected filters */
1190 job->filters = hb_list_init();
1193 hb_filter_detelecine.settings = detelecine_opt;
1194 hb_list_add( job->filters, &hb_filter_detelecine );
1198 hb_filter_decomb.settings = decomb_opt;
1199 hb_list_add( job->filters, &hb_filter_decomb );
1203 hb_filter_deinterlace.settings = deinterlace_opt;
1204 hb_list_add( job->filters, &hb_filter_deinterlace );
1208 hb_filter_deblock.settings = deblock_opt;
1209 hb_list_add( job->filters, &hb_filter_deblock );
1213 hb_filter_denoise.settings = denoise_opt;
1214 hb_list_add( job->filters, &hb_filter_denoise );
1217 if( width && height )
1220 job->height = height;
1225 hb_fix_aspect( job, HB_KEEP_WIDTH );
1227 else if( height && !loosePixelratio)
1229 job->height = height;
1230 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1232 else if( !width && !height && !pixelratio && !loosePixelratio )
1234 hb_fix_aspect( job, HB_KEEP_WIDTH );
1236 else if (!width && loosePixelratio)
1238 /* Default to full width when one isn't specified for loose anamorphic */
1239 job->width = title->width - job->crop[2] - job->crop[3];
1240 /* The height will be thrown away in hb.c but calculate it anyway */
1241 hb_fix_aspect( job, HB_KEEP_WIDTH );
1244 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1246 job->vquality = vquality;
1251 job->vquality = -1.0;
1252 job->vbitrate = vbitrate;
1256 job->vcodec = vcodec;
1260 job->h264_level = 13;
1264 job->h264_level = 30;
1269 job->vrate = 27000000;
1270 job->vrate_base = vrate;
1274 // cfr or pfr flag with no rate specified implies
1275 // use the title rate.
1277 job->vrate = title->rate;
1278 job->vrate_base = title->rate_base;
1281 /* Grab audio tracks */
1284 char * token = strtok(atracks, ",");
1287 int track_start, track_end;
1288 while( token != NULL )
1290 audio = calloc(1, sizeof(*audio));
1291 hb_audio_config_init(audio);
1292 if (strlen(token) >= 3)
1294 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1297 for (i = track_start - 1; i < track_end; i++)
1299 if (i != track_start - 1)
1301 audio = calloc(1, sizeof(*audio));
1302 hb_audio_config_init(audio);
1304 audio->in.track = i;
1305 audio->out.track = num_audio_tracks++;
1306 hb_list_add(audios, audio);
1309 else if( !strcasecmp(token, "none" ) )
1311 audio->in.track = audio->out.track = -1;
1312 audio->out.codec = 0;
1313 hb_list_add(audios, audio);
1318 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1325 audio->in.track = atoi(token) - 1;
1326 audio->out.track = num_audio_tracks++;
1327 hb_list_add(audios, audio);
1329 token = strtok(NULL, ",");
1333 /* Parse audio tracks */
1334 if( hb_list_count(audios) == 0 )
1336 /* Create a new audio track with default settings */
1337 audio = calloc(1, sizeof(*audio));
1338 hb_audio_config_init(audio);
1339 /* Add it to our audios */
1340 hb_list_add(audios, audio);
1343 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1344 for (i = 0; i < tmp_num_audio_tracks; i++)
1346 audio = hb_list_item(audios, 0);
1347 if( (audio == NULL) || (audio->in.track == -1) ||
1348 (audio->out.track == -1) || (audio->out.codec == 0) )
1354 if( hb_audio_add( job, audio ) == 0 )
1356 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1357 audio->in.track + 1 );
1362 hb_list_rem(audios, audio);
1364 if( audio->out.name )
1366 free( audio->out.name);
1375 char * token = strtok(acodecs, ",");
1378 while ( token != NULL )
1380 if ((acodec = get_acodec_for_string(token)) == -1)
1382 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1383 acodec = default_acodec;
1385 if( i < num_audio_tracks )
1387 audio = hb_list_audio_config_item(job->list_audio, i);
1388 audio->out.codec = acodec;
1392 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1393 hb_audio_config_t audio;
1397 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1399 hb_audio_config_init(&audio);
1400 audio.in.track = last_audio->in.track;
1401 audio.out.track = num_audio_tracks++;
1402 audio.out.codec = acodec;
1403 hb_audio_add(job, &audio);
1407 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1410 token = strtok(NULL, ",");
1414 if( i < num_audio_tracks )
1416 /* We have fewer inputs than audio tracks, use the default codec for
1417 * this container for the remaining tracks. Unless we only have one input
1418 * then use that codec instead.
1421 acodec = default_acodec;
1422 for ( ; i < num_audio_tracks; i++)
1424 audio = hb_list_audio_config_item(job->list_audio, i);
1425 audio->out.codec = acodec;
1434 char * token = strtok(arates, ",");
1437 while ( token != NULL )
1439 arate = atoi(token);
1440 audio = hb_list_audio_config_item(job->list_audio, i);
1443 for( j = 0; j < hb_audio_rates_count; j++ )
1445 if( !strcmp( token, hb_audio_rates[j].string ) )
1447 arate = hb_audio_rates[j].rate;
1454 if (!is_sample_rate_valid(arate))
1456 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1457 arate = audio->in.samplerate;
1460 audio->out.samplerate = arate;
1461 if( (++i) >= num_audio_tracks )
1462 break; /* We have more inputs than audio tracks, oops */
1466 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1468 token = strtok(NULL, ",");
1471 if (i < num_audio_tracks)
1473 /* We have fewer inputs than audio tracks, use default sample rate.
1474 * Unless we only have one input, then use that for all tracks.
1477 arate = audio->in.samplerate;
1478 for ( ; i < num_audio_tracks; i++)
1480 audio = hb_list_audio_config_item(job->list_audio, i);
1481 audio->out.samplerate = arate;
1490 char * token = strtok(abitrates, ",");
1493 while ( token != NULL )
1495 abitrate = atoi(token);
1496 audio = hb_list_audio_config_item(job->list_audio, i);
1500 audio->out.bitrate = abitrate;
1501 if( (++i) >= num_audio_tracks )
1502 break; /* We have more inputs than audio tracks, oops */
1506 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1508 token = strtok(NULL, ",");
1511 if (i < num_audio_tracks)
1513 /* We have fewer inputs than audio tracks, use the default bitrate
1514 * for the remaining tracks. Unless we only have one input, then use
1515 * that for all tracks.
1518 abitrate = default_abitrate;
1519 for (; i < num_audio_tracks; i++)
1521 audio = hb_list_audio_config_item(job->list_audio, i);
1522 audio->out.bitrate = abitrate;
1529 if ( dynamic_range_compression )
1531 char * token = strtok(dynamic_range_compression, ",");
1533 token = dynamic_range_compression;
1534 while ( token != NULL )
1536 d_r_c = atof(token);
1537 audio = hb_list_audio_config_item(job->list_audio, i);
1540 audio->out.dynamic_range_compression = d_r_c;
1541 if( (++i) >= num_audio_tracks )
1542 break; /* We have more inputs than audio tracks, oops */
1546 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1548 token = strtok(NULL, ",");
1551 if (i < num_audio_tracks)
1553 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1554 * tracks. Unless we only have one input, then use the same DRC for all
1559 for (; i < num_audio_tracks; i++)
1561 audio = hb_list_audio_config_item(job->list_audio, i);
1562 audio->out.dynamic_range_compression = d_r_c;
1571 char * token = strtok(mixdowns, ",");
1574 while ( token != NULL )
1576 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1577 audio = hb_list_audio_config_item(job->list_audio, i);
1580 audio->out.mixdown = mixdown;
1581 if( (++i) >= num_audio_tracks )
1582 break; /* We have more inputs than audio tracks, oops */
1586 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1588 token = strtok(NULL, ",");
1591 if (i < num_audio_tracks)
1593 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1594 * we only have one input, then use that.
1597 mixdown = HB_AMIXDOWN_DOLBYPLII;
1598 for (; i < num_audio_tracks; i++)
1600 audio = hb_list_audio_config_item(job->list_audio, i);
1601 audio->out.mixdown = mixdown;
1606 /* Audio Track Names */
1610 char * token = strtok(anames, ",");
1613 while ( token != NULL )
1615 audio = hb_list_audio_config_item(job->list_audio, i);
1618 audio->out.name = strdup(token);
1619 if( (++i) >= num_audio_tracks )
1620 break; /* We have more names than audio tracks, oops */
1624 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1627 token = strtok(NULL, ",");
1630 if( i < num_audio_tracks && i == 1 )
1632 /* We have exactly one name and more than one audio track. Use the same
1633 * name for all tracks. */
1634 for ( ; i < num_audio_tracks; i++)
1636 audio = hb_list_audio_config_item(job->list_audio, i);
1637 audio->out.name = strdup(anames);
1640 /* Audio Track Names */
1644 job->vbitrate = hb_calc_bitrate( job, size );
1645 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1651 hb_subtitle_t *subtitle;
1653 * Find the subtitle with the same track as "sub" and
1654 * add that to the job subtitle list
1656 subtitle = hb_list_item( title->list_subtitle, sub-1 );
1658 if( subtitle_force ) {
1659 subtitle->force = subtitle_force;
1661 hb_list_add( job->list_subtitle, subtitle );
1663 fprintf( stderr, "Could not find subtitle track %d, skipped\n", sub );
1667 if( native_language )
1669 job->native_language = strdup( native_language );
1677 if ( largeFileSize )
1679 job->largeFileSize = 1;
1683 job->mp4_optimize = 1;
1690 job->file = strdup( output );
1699 job->color_matrix = color_matrix;
1702 if( x264opts != NULL && *x264opts != '\0' )
1704 job->x264opts = x264opts;
1706 else /*avoids a bus error crash when options aren't specified*/
1708 job->x264opts = NULL;
1711 job->maxWidth = maxWidth;
1713 job->maxHeight = maxHeight;
1715 if( start_at_preview )
1717 job->start_at_preview = start_at_preview - 1;
1718 job->seek_points = preview_count;
1723 job->pts_to_stop = stop_at_pts;
1729 job->frame_to_stop = stop_at_frame;
1738 * When subtitle scan is enabled do a fast pre-scan job
1739 * which will determine which subtitles to enable, if any.
1743 x264opts_tmp = job->x264opts;
1745 job->x264opts = NULL;
1747 job->indepth_scan = subtitle_scan;
1748 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1749 "subtitles if found for foreign language segments\n");
1750 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1751 *(job->select_subtitle) = NULL;
1754 * Add the pre-scan job
1758 job->x264opts = x264opts_tmp;
1764 * If subtitle_scan is enabled then only turn it on
1765 * for the first pass and then off again for the
1768 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1770 job->select_subtitle = NULL;
1774 job->indepth_scan = 0;
1778 x264opts2 = strdup(x264opts);
1782 * If turbo options have been selected then append them
1783 * to the x264opts now (size includes one ':' and the '\0')
1785 if( turbo_opts_enabled )
1787 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1790 tmp_x264opts = malloc(size * sizeof(char));
1793 snprintf( tmp_x264opts, size, "%s:%s",
1794 x264opts, turbo_opts );
1798 * No x264opts to modify, but apply the turbo options
1799 * anyway as they may be modifying defaults
1801 snprintf( tmp_x264opts, size, "%s",
1804 x264opts = tmp_x264opts;
1806 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1809 job->x264opts = x264opts;
1813 job->select_subtitle = subtitle_tmp;
1817 * On the second pass we turn off subtitle scan so that we
1818 * can actually encode using any subtitles that were auto
1819 * selected in the first pass (using the whacky select-subtitle
1820 * attribute of the job).
1822 job->indepth_scan = 0;
1824 job->x264opts = x264opts2;
1831 * Turn on subtitle scan if requested, note that this option
1832 * precludes encoding of any actual subtitles.
1835 job->indepth_scan = 0;
1843 #define p s.param.working
1844 case HB_STATE_WORKING:
1845 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1846 p.job_cur, p.job_count, 100.0 * p.progress );
1847 if( p.seconds > -1 )
1849 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1850 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1851 p.hours, p.minutes, p.seconds );
1857 #define p s.param.muxing
1858 case HB_STATE_MUXING:
1860 if (show_mux_warning)
1862 fprintf( stdout, "\rMuxing: this may take awhile..." );
1864 show_mux_warning = 0;
1870 #define p s.param.workdone
1871 case HB_STATE_WORKDONE:
1872 /* Print error if any, then exit */
1876 fprintf( stderr, "\nRip done!\n" );
1878 case HB_ERROR_CANCELED:
1879 fprintf( stderr, "\nRip canceled.\n" );
1882 fprintf( stderr, "\nRip failed (error %x).\n",
1892 /****************************************************************************
1894 ****************************************************************************/
1895 static volatile int64_t i_die_date = 0;
1896 void SigHandler( int i_signal )
1901 i_die_date = hb_get_date();
1902 fprintf( stderr, "Signal %d received, terminating - do it "
1903 "again in case it gets stuck\n", i_signal );
1905 else if( i_die_date + 500 < hb_get_date() )
1907 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1912 /****************************************************************************
1914 ****************************************************************************/
1915 static void ShowHelp()
1918 FILE* const out = stdout;
1921 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1923 "### General Handbrake Options------------------------------------------------\n\n"
1924 " -h, --help Print help\n"
1925 " -u, --update Check for updates and exit\n"
1926 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1927 " -C, --cpu Set CPU count (default: autodetected)\n"
1928 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1929 " if the preset name has spaces, surround it with\n"
1930 " double quotation marks\n"
1931 " -z, --preset-list See a list of available built-in presets\n"
1932 " --dvdnav Use dvdnav (Experimental)\n"
1935 "### Source Options-----------------------------------------------------------\n\n"
1936 " -i, --input <string> Set input device\n"
1937 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1939 " -L, --longest Select the longest title\n"
1940 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1941 " 1 to 3, or \"3\" for chapter 3 only,\n"
1942 " default: all chapters)\n"
1943 " --angle <number> Select the DVD angle\n"
1944 " --previews <#:B> Select how many preview images are generated (max 30),\n"
1945 " and whether or not they're stored to disk (0 or 1).\n"
1946 " (default: 10:0)\n"
1947 " --start-at-preview <#> Start encoding at a given preview.\n"
1948 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
1949 " or pts (on a 90kHz clock)"
1952 "### Destination Options------------------------------------------------------\n\n"
1953 " -o, --output <string> Set output file name\n"
1954 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1955 " autodetected from file name)\n"
1956 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1957 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1958 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1959 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1960 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1964 "### Video Options------------------------------------------------------------\n\n"
1965 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
1966 " (default: ffmpeg)\n"
1967 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1968 " same style as mencoder:\n"
1969 " option1=value1:option2=value2\n"
1970 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1971 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1972 " -S, --size <MB> Set target size\n"
1973 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1974 " -2, --two-pass Use two-pass mode\n"
1975 " -T, --turbo When using 2-pass use the turbo options\n"
1976 " on the first pass to improve speed\n"
1977 " (only works with x264, affects PSNR by about 0.05dB,\n"
1978 " and increases first pass speed two to four times)\n"
1979 " -r, --rate Set video framerate (" );
1980 for( i = 0; i < hb_video_rates_count; i++ )
1982 fprintf( out, hb_video_rates[i].string );
1983 if( i != hb_video_rates_count - 1 )
1984 fprintf( out, "/" );
1987 " Be aware that not specifying a framerate lets\n"
1988 " HandBrake preserve a source's time stamps,\n"
1989 " potentially creating variable framerate video\n"
1990 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
1991 " frame rate control. VFR preserves the source\n"
1992 " timing. CFR makes the output constant rate at\n"
1993 " the rate given by the -r flag (or the source's\n"
1994 " average rate if no -r is given). PFR doesn't\n"
1995 " allow the rate to go over the rate specified\n"
1996 " with the -r flag but won't change the source\n"
1997 " timing if it's below that rate.\n"
1998 " If none of these flags are given, the default\n"
1999 " is --cfr when -r is given and --vfr otherwise\n"
2002 "### Audio Options-----------------------------------------------------------\n\n"
2003 " -a, --audio <string> Select audio track(s), separated by commas\n"
2004 " More than one output track can be used for one\n"
2006 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2007 " tracks, default: first one)\n"
2008 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2009 " ac3 and dts meaning passthrough\n"
2010 " Separated by commas for more than one audio track.\n"
2011 " (default: guessed)\n"
2012 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2013 " Separated by commas for more than one audio track.\n"
2014 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2015 " Separated by commas for more than one audio track.\n"
2016 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2017 " -R, --arate Set audio samplerate(s) (" );
2018 for( i = 0; i < hb_audio_rates_count; i++ )
2020 fprintf( out, hb_audio_rates[i].string );
2021 if( i != hb_audio_rates_count - 1 )
2022 fprintf( out, "/" );
2024 fprintf( out, " kHz)\n"
2025 " Separated by commas for more than one audio track.\n"
2026 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2027 " making soft sounds louder. Range is 1.0 to 4.0\n"
2028 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2029 " Separated by commas for more than one audio track.\n"
2030 " -A, --aname <string> Audio track name(s),\n"
2031 " Separated by commas for more than one audio track.\n"
2034 "### Picture Settings---------------------------------------------------------\n\n"
2035 " -w, --width <number> Set picture width\n"
2036 " -l, --height <number> Set picture height\n"
2037 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2038 " -Y, --maxHeight <#> Set maximum height\n"
2039 " -X, --maxWidth <#> Set maximum width\n"
2040 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
2041 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
2042 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
2043 " the dimensions to divide cleanly by (default 16)\n"
2044 " and the pixel ratio to use (default autodetected)\n"
2045 " -M --color-matrix Set the color space signaled by the output\n"
2046 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2047 " default: set by resolution)\n"
2050 "### Filters---------------------------------------------------------\n\n"
2052 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2053 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2055 " <fast/slow/slower>\n"
2056 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2057 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2058 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2059 " Note: this filter drops duplicate frames to\n"
2060 " restore the pre-telecine framerate, unless you\n"
2061 " specify a constant framerate (--rate 29.97)\n"
2062 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2063 " -8, --denoise Denoise video with hqdn3d filter\n"
2064 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2066 " <weak/medium/strong>\n"
2067 " -7, --deblock Deblock video with pp7 filter\n"
2068 " <QP:M> (default 5:2)\n"
2069 " -g, --grayscale Grayscale encoding\n"
2072 "### Subtitle Options------------------------------------------------------------\n\n"
2073 " -s, --subtitle <number> Select subtitle (default: none)\n"
2074 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
2075 " the one that's only used 10 percent of the time\n"
2076 " or less. This should locate subtitles for short\n"
2077 " foreign language segments. Best used in conjunction\n"
2078 " with --subtitle-forced.\n"
2079 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2080 " the subtitle has the forced flag set. May be used in\n"
2081 " conjunction with --subtitle-scan to auto-select\n"
2082 " a stream if it contains forced subtitles.\n"
2083 " -N, --native-language Select subtitles with this language if it does not\n"
2084 " <string> match the Audio language. Provide the language's\n"
2085 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2094 /****************************************************************************
2096 ****************************************************************************/
2097 static void ShowPresets()
2099 printf("\n< Apple\n");
2101 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");
2103 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");
2105 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");
2107 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");
2109 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");
2111 printf("\n << Legacy\n");
2113 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");
2115 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");
2117 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");
2123 printf("\n< Basic\n");
2125 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");
2127 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -R Auto -6 dpl2 -f mp4\n");
2131 printf("\n< High Profile\n");
2133 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");
2135 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");
2137 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");
2139 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");
2143 printf("\n< Gaming Consoles\n");
2145 printf("\n + PSP: -b 1024 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 368 -Y 208 -m\n");
2147 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");
2149 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");
2155 /****************************************************************************
2157 ****************************************************************************/
2158 static int ParseOptions( int argc, char ** argv )
2161 #define PREVIEWS 257
2162 #define START_AT_PREVIEW 258
2169 static struct option long_options[] =
2171 { "help", no_argument, NULL, 'h' },
2172 { "update", no_argument, NULL, 'u' },
2173 { "verbose", optional_argument, NULL, 'v' },
2174 { "cpu", required_argument, NULL, 'C' },
2175 { "dvdnav", no_argument, NULL, DVDNAV },
2177 { "format", required_argument, NULL, 'f' },
2178 { "input", required_argument, NULL, 'i' },
2179 { "output", required_argument, NULL, 'o' },
2180 { "large-file", no_argument, NULL, '4' },
2181 { "optimize", no_argument, NULL, 'O' },
2182 { "ipod-atom", no_argument, NULL, 'I' },
2184 { "title", required_argument, NULL, 't' },
2185 { "longest", no_argument, NULL, 'L' },
2186 { "chapters", required_argument, NULL, 'c' },
2187 { "angle", required_argument, NULL, ANGLE },
2188 { "markers", optional_argument, NULL, 'm' },
2189 { "audio", required_argument, NULL, 'a' },
2190 { "mixdown", required_argument, NULL, '6' },
2191 { "drc", required_argument, NULL, 'D' },
2192 { "subtitle", required_argument, NULL, 's' },
2193 { "subtitle-scan", no_argument, NULL, 'U' },
2194 { "subtitle-forced", no_argument, NULL, 'F' },
2195 { "native-language", required_argument, NULL,'N' },
2197 { "encoder", required_argument, NULL, 'e' },
2198 { "aencoder", required_argument, NULL, 'E' },
2199 { "two-pass", no_argument, NULL, '2' },
2200 { "deinterlace", optional_argument, NULL, 'd' },
2201 { "deblock", optional_argument, NULL, '7' },
2202 { "denoise", optional_argument, NULL, '8' },
2203 { "detelecine", optional_argument, NULL, '9' },
2204 { "decomb", optional_argument, NULL, '5' },
2205 { "grayscale", no_argument, NULL, 'g' },
2206 { "pixelratio", no_argument, NULL, 'p' },
2207 { "loosePixelratio", optional_argument, NULL, 'P' },
2208 { "width", required_argument, NULL, 'w' },
2209 { "height", required_argument, NULL, 'l' },
2210 { "crop", required_argument, NULL, 'n' },
2212 { "vb", required_argument, NULL, 'b' },
2213 { "quality", required_argument, NULL, 'q' },
2214 { "size", required_argument, NULL, 'S' },
2215 { "ab", required_argument, NULL, 'B' },
2216 { "rate", required_argument, NULL, 'r' },
2217 { "arate", required_argument, NULL, 'R' },
2218 { "cqp", no_argument, NULL, 'Q' },
2219 { "x264opts", required_argument, NULL, 'x' },
2220 { "turbo", no_argument, NULL, 'T' },
2221 { "maxHeight", required_argument, NULL, 'Y' },
2222 { "maxWidth", required_argument, NULL, 'X' },
2223 { "preset", required_argument, NULL, 'Z' },
2224 { "preset-list", no_argument, NULL, 'z' },
2226 { "aname", required_argument, NULL, 'A' },
2227 { "color-matrix",required_argument, NULL, 'M' },
2228 { "previews", required_argument, NULL, PREVIEWS },
2229 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2230 { "stop-at", required_argument, NULL, STOP_AT },
2231 { "vfr", no_argument, &cfr, 0 },
2232 { "cfr", no_argument, &cfr, 1 },
2233 { "pfr", no_argument, &cfr, 2 },
2237 int option_index = 0;
2240 c = getopt_long( argc, argv,
2241 "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",
2242 long_options, &option_index );
2251 /* option was handled entirely in getopt_long */
2260 if( optarg != NULL )
2262 debug = atoi( optarg );
2270 cpu = atoi( optarg );
2275 preset_name = strdup(optarg);
2285 format = strdup( optarg );
2288 input = strdup( optarg );
2290 char *devName = bsd_name_for_path( input ); // alloc
2293 if( device_is_dvd( devName ))
2296 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2297 sprintf( input, "/dev/%s", devName );
2304 output = strdup( optarg );
2317 titleindex = atoi( optarg );
2325 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2327 chapter_start = start;
2330 else if( sscanf( optarg, "%d", &start ) == 1 )
2332 chapter_start = start;
2333 chapter_end = chapter_start;
2337 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2344 angle = atoi( optarg );
2347 if( optarg != NULL )
2349 marker_file = strdup( optarg );
2351 chapter_markers = 1;
2354 if( optarg != NULL )
2356 atracks = strdup( optarg );
2364 if( optarg != NULL )
2366 mixdowns = strdup( optarg );
2370 if( optarg != NULL )
2372 dynamic_range_compression = strdup( optarg );
2376 sub = atoi( optarg );
2385 native_language = strdup( optarg );
2391 if( optarg != NULL )
2393 if (!( strcmp( optarg, "fast" ) ))
2395 deinterlace_opt = "-1";
2397 else if (!( strcmp( optarg, "slow" ) ))
2399 deinterlace_opt = "2";
2401 else if (!( strcmp( optarg, "slower" ) ))
2403 deinterlace_opt = "0";
2407 deinterlace_opt = strdup( optarg );
2413 if( optarg != NULL )
2415 deblock_opt = strdup( optarg );
2420 if( optarg != NULL )
2422 if (!( strcmp( optarg, "weak" ) ))
2424 denoise_opt = "2:1:2:3";
2426 else if (!( strcmp( optarg, "medium" ) ))
2428 denoise_opt = "3:2:2:3";
2430 else if (!( strcmp( optarg, "strong" ) ))
2432 denoise_opt = "7:7:5:5";
2436 denoise_opt = strdup( optarg );
2442 if( optarg != NULL )
2444 detelecine_opt = strdup( optarg );
2449 if( optarg != NULL )
2451 decomb_opt = strdup( optarg );
2462 loosePixelratio = 1;
2463 if( optarg != NULL )
2465 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
2469 if( !strcasecmp( optarg, "ffmpeg" ) )
2471 vcodec = HB_VCODEC_FFMPEG;
2473 else if( !strcasecmp( optarg, "x264" ) )
2475 vcodec = HB_VCODEC_X264;
2477 else if( !strcasecmp( optarg, "x264b13" ) )
2479 vcodec = HB_VCODEC_X264;
2482 else if( !strcasecmp( optarg, "x264b30" ) )
2484 vcodec = HB_VCODEC_X264;
2487 else if( !strcasecmp( optarg, "theora" ) )
2489 vcodec = HB_VCODEC_THEORA;
2493 fprintf( stderr, "invalid codec (%s)\n", optarg );
2498 if( optarg != NULL )
2500 acodecs = strdup( optarg );
2504 width = atoi( optarg );
2507 height = atoi( optarg );
2512 char * tmp = optarg;
2513 for( i = 0; i < 4; i++ )
2517 crop[i] = strtol( tmp, &tmp, 0 );
2526 for( i = 0; i < hb_video_rates_count; i++ )
2528 if( !strcmp( optarg, hb_video_rates[i].string ) )
2530 vrate = hb_video_rates[i].rate;
2536 fprintf( stderr, "invalid framerate %s\n", optarg );
2538 else if ( cfr == 0 )
2545 if( optarg != NULL )
2547 arates = strdup( optarg );
2551 vbitrate = atoi( optarg );
2554 vquality = atof( optarg );
2557 size = atoi( optarg );
2560 if( optarg != NULL )
2562 abitrates = strdup( optarg );
2569 x264opts = strdup( optarg );
2572 turbo_opts_enabled = 1;
2575 maxHeight = atoi( optarg );
2578 maxWidth = atoi (optarg );
2581 if( optarg != NULL )
2583 anames = strdup( optarg );
2587 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2589 case START_AT_PREVIEW:
2590 start_at_preview = atoi( optarg );
2593 stop_at_string = strdup( optarg );
2594 stop_at_token = strtok( stop_at_string, ":");
2595 if( !strcmp( stop_at_token, "frame" ) )
2597 stop_at_token = strtok( NULL, ":");
2598 stop_at_frame = atoi(stop_at_token);
2600 else if( !strcmp( stop_at_token, "pts" ) )
2602 stop_at_token = strtok( NULL, ":");
2603 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2605 else if( !strcmp( stop_at_token, "duration" ) )
2607 stop_at_token = strtok( NULL, ":");
2608 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2609 stop_at_pts *= 90000LL;
2613 if( atoi( optarg ) == 601 )
2615 else if( atoi( optarg ) == 709 )
2619 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2627 static int CheckOptions( int argc, char ** argv )
2634 if( input == NULL || *input == '\0' )
2636 fprintf( stderr, "Missing input device. Run %s --help for "
2637 "syntax.\n", argv[0] );
2642 if( titleindex > 0 )
2644 if( output == NULL || *output == '\0' )
2646 fprintf( stderr, "Missing output file name. Run %s --help "
2647 "for syntax.\n", argv[0] );
2653 char * p = strrchr( output, '.' );
2656 if( p && !strcasecmp( p, ".avi" ) )
2659 default_acodec = HB_ACODEC_LAME;
2661 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2662 !strcasecmp( p, ".m4v" ) ) )
2668 default_acodec = HB_ACODEC_FAAC;
2670 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2671 !strcasecmp( p, ".ogg" ) ) )
2674 default_acodec = HB_ACODEC_VORBIS;
2676 else if( p && !strcasecmp(p, ".mkv" ) )
2679 default_acodec = HB_ACODEC_AC3;
2683 fprintf( stderr, "Output format couldn't be guessed "
2684 "from file name, using default.\n" );
2688 else if( !strcasecmp( format, "avi" ) )
2691 default_acodec = HB_ACODEC_LAME;
2693 else if( !strcasecmp( format, "mp4" ) ||
2694 !strcasecmp( format, "m4v" ) )
2700 default_acodec = HB_ACODEC_FAAC;
2702 else if( !strcasecmp( format, "ogm" ) ||
2703 !strcasecmp( format, "ogg" ) )
2706 default_acodec = HB_ACODEC_VORBIS;
2708 else if( !strcasecmp( format, "mkv" ) )
2711 default_acodec = HB_ACODEC_AC3;
2715 fprintf( stderr, "Invalid output format (%s). Possible "
2716 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2724 static int get_acodec_for_string( char *codec )
2726 if( !strcasecmp( codec, "ac3" ) )
2728 return HB_ACODEC_AC3;
2730 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2732 return HB_ACODEC_DCA;
2734 else if( !strcasecmp( codec, "lame" ) )
2736 return HB_ACODEC_LAME;
2738 else if( !strcasecmp( codec, "faac" ) )
2740 return HB_ACODEC_FAAC;
2742 else if( !strcasecmp( codec, "vorbis") )
2744 return HB_ACODEC_VORBIS;
2747 else if( !strcasecmp( codec, "ca_aac") )
2749 return HB_ACODEC_CA_AAC;
2758 static int is_sample_rate_valid(int rate)
2761 for( i = 0; i < hb_audio_rates_count; i++ )
2763 if (rate == hb_audio_rates[i].rate)
2770 /****************************************************************************
2773 * Returns the BSD device name for the block device that contains the
2774 * passed-in path. Returns NULL on failure.
2775 ****************************************************************************/
2776 static char* bsd_name_for_path(char *path)
2780 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2786 // Get the volume reference number.
2787 FSCatalogInfo catalogInfo;
2788 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2794 FSVolumeRefNum volRefNum = catalogInfo.volume;
2796 // Now let's get the device name
2797 GetVolParmsInfoBuffer volumeParms;
2798 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2804 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
2805 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
2806 if( volumeParms.vMVersion < 4 )
2811 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
2812 if( !volumeParms.vMDeviceID )
2817 return strdup( volumeParms.vMDeviceID );
2820 /****************************************************************************
2823 * Returns whether or not the passed in BSD device represents a DVD, or other
2825 ****************************************************************************/
2826 static int device_is_dvd(char *device)
2828 io_service_t service = get_iokit_service(device);
2829 if( service == IO_OBJECT_NULL )
2833 int result = is_dvd_service(service);
2834 IOObjectRelease(service);
2838 /****************************************************************************
2841 * Returns the IOKit service object for the passed in BSD device name.
2842 ****************************************************************************/
2843 static io_service_t get_iokit_service( char *device )
2845 CFMutableDictionaryRef matchingDict;
2846 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2847 if( matchingDict == NULL )
2849 return IO_OBJECT_NULL;
2851 // Fetch the object with the matching BSD node name. There should only be
2852 // one match, so IOServiceGetMatchingService is used instead of
2853 // IOServiceGetMatchingServices to simplify the code.
2854 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2857 /****************************************************************************
2860 * Returns whether or not the service passed in is a DVD.
2862 * Searches for an IOMedia object that represents the entire (whole) media that
2863 * the volume is on. If the volume is on partitioned media, the whole media
2864 * object will be a parent of the volume's media object. If the media is not
2865 * partitioned, the volume's media object will be the whole media object.
2866 ****************************************************************************/
2867 static int is_dvd_service( io_service_t service )
2869 kern_return_t kernResult;
2872 // Create an iterator across all parents of the service object passed in.
2873 kernResult = IORegistryEntryCreateIterator( service,
2875 kIORegistryIterateRecursively | kIORegistryIterateParents,
2877 if( kernResult != KERN_SUCCESS )
2881 if( iter == IO_OBJECT_NULL )
2886 // A reference on the initial service object is released in the do-while
2887 // loop below, so add a reference to balance.
2888 IOObjectRetain( service );
2893 if( is_whole_media_service( service ) &&
2894 IOObjectConformsTo( service, kIODVDMediaClass) )
2898 IOObjectRelease( service );
2899 } while( !result && (service = IOIteratorNext( iter )) );
2900 IOObjectRelease( iter );
2905 /****************************************************************************
2906 * is_whole_media_service
2908 * Returns whether or not the service passed in is an IOMedia service and
2909 * represents the "whole" media instead of just a partition.
2911 * The whole media object is indicated in the IORegistry by the presence of a
2912 * property with the key "Whole" and value "Yes".
2913 ****************************************************************************/
2914 static is_whole_media_service( io_service_t service )
2918 if( IOObjectConformsTo( service, kIOMediaClass ) )
2920 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2921 CFSTR( kIOMediaWholeKey ),
2922 kCFAllocatorDefault,
2928 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2929 CFRelease( wholeMedia );
2934 #endif // __APPLE_CC__