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. */
14 #ifdef PTW32_STATIC_LIB
22 #import <CoreServices/CoreServices.h>
23 #include <IOKit/IOKitLib.h>
24 #include <IOKit/storage/IOMedia.h>
25 #include <IOKit/storage/IODVDMedia.h>
29 static int debug = HB_DEBUG_NONE;
30 static int update = 0;
31 static int dvdnav = 0;
32 static char * input = NULL;
33 static char * output = NULL;
34 static char * format = NULL;
35 static int titleindex = 1;
36 static int longest_title = 0;
37 static int subtitle_scan = 0;
38 static int subtitle_force = 0;
39 static char * native_language = NULL;
40 static int twoPass = 0;
41 static int deinterlace = 0;
42 static char * deinterlace_opt = 0;
43 static int deblock = 0;
44 static char * deblock_opt = 0;
45 static int denoise = 0;
46 static char * denoise_opt = 0;
47 static int detelecine = 0;
48 static char * detelecine_opt = 0;
49 static int decomb = 0;
50 static char * decomb_opt = 0;
51 static int grayscale = 0;
52 static int vcodec = HB_VCODEC_FFMPEG;
53 static int h264_13 = 0;
54 static int h264_30 = 0;
55 static hb_list_t * audios = NULL;
56 static hb_audio_config_t * audio = NULL;
57 static int num_audio_tracks = 0;
58 static char * mixdowns = NULL;
59 static char * dynamic_range_compression = NULL;
60 static char * atracks = NULL;
61 static char * arates = NULL;
62 static char * abitrates = NULL;
63 static char * acodecs = NULL;
64 static char * anames = NULL;
65 static int default_acodec = HB_ACODEC_FAAC;
66 static int default_abitrate = 160;
69 static int height = 0;
70 static int crop[4] = { -1,-1,-1,-1 };
73 static float vquality = -1.0;
74 static int vbitrate = 0;
77 static int anamorphic_mode = 0;
78 static int modulus = 0;
79 static int par_height = 0;
80 static int par_width = 0;
81 static int display_width = 0;
82 static int keep_display_aspect = 0;
83 static int itu_par = 0;
85 static int chapter_start = 0;
86 static int chapter_end = 0;
87 static int chapter_markers = 0;
88 static char * marker_file = NULL;
90 static char *x264opts = NULL;
91 static char *x264opts2 = NULL;
92 static int maxHeight = 0;
93 static int maxWidth = 0;
94 static int turbo_opts_enabled = 0;
95 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
96 static int largeFileSize = 0;
97 static int preset = 0;
98 static char * preset_name = 0;
100 static int mp4_optimize = 0;
101 static int ipod_atom = 0;
102 static int color_matrix = 0;
103 static int preview_count = 10;
104 static int store_previews = 0;
105 static int start_at_preview = 0;
106 static int64_t stop_at_pts = 0;
107 static int stop_at_frame = 0;
108 static char * stop_at_string = NULL;
109 static char * stop_at_token = NULL;
111 /* Exit cleanly on Ctrl-C */
112 static volatile int die = 0;
113 static void SigHandler( int );
116 static void ShowCommands();
117 static void ShowHelp();
118 static void ShowPresets();
120 static int ParseOptions( int argc, char ** argv );
121 static int CheckOptions( int argc, char ** argv );
122 static int HandleEvents( hb_handle_t * h );
124 static int get_acodec_for_string( char *codec );
125 static int is_sample_rate_valid(int rate);
128 static char* bsd_name_for_path(char *path);
129 static int device_is_dvd(char *device);
130 static io_service_t get_iokit_service( char *device );
131 static int is_dvd_service( io_service_t service );
132 static is_whole_media_service( io_service_t service );
135 /* Only print the "Muxing..." message once */
136 static int show_mux_warning = 1;
138 /****************************************************************************
141 * When using the CLI just display using hb_log as we always did in the past
142 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
143 ****************************************************************************/
144 static void hb_cli_error_handler ( const char *errmsg )
146 fprintf( stderr, "ERROR: %s\n", errmsg );
149 int main( int argc, char ** argv )
155 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
156 * force it to unbuffered otherwise informative output is not easily parsed.
158 #if defined( _WIN32 ) || defined( __MINGW32__ )
159 setvbuf( stdout, NULL, _IONBF, 0 );
160 setvbuf( stderr, NULL, _IONBF, 0 );
163 audios = hb_list_init();
165 /* Parse command line */
166 if( ParseOptions( argc, argv ) ||
167 CheckOptions( argc, argv ) )
172 #ifdef PTW32_STATIC_LIB
173 pthread_win32_process_attach_np();
174 pthread_win32_thread_attach_np();
177 /* Register our error handler */
178 hb_register_error_handler(&hb_cli_error_handler);
181 h = hb_init( debug, update );
182 hb_dvd_set_dvdnav( dvdnav );
185 fprintf( stderr, "%s - %s - %s\n",
186 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
188 /* Check for update */
191 if( ( build = hb_check_update( h, &version ) ) > -1 )
193 fprintf( stderr, "You are using an old version of "
194 "HandBrake.\nLatest is %s (build %d).\n", version,
199 fprintf( stderr, "Your version of HandBrake is up to "
207 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
208 hb_get_cpu_count( h ) > 1 ? "s" : "" );
211 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
212 cpu > 1 ? "s" : "" );
213 hb_set_cpu_count( h, cpu );
216 /* Exit ASAP on Ctrl-C */
217 signal( SIGINT, SigHandler );
219 /* Feed libhb with a DVD to scan */
220 fprintf( stderr, "Opening %s...\n", input );
224 * We need to scan for all the titles in order to find the longest
229 hb_scan( h, input, titleindex, preview_count, store_previews );
234 #if !defined(SYS_BEOS) && !defined(__MINGW32__)
244 FD_SET( STDIN_FILENO, &fds );
245 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
252 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
254 if( buf[size] == '\n' )
261 if( size >= 256 || buf[size] == '\n' )
266 fprintf( stdout, "\nEncoding Quit by user command\n" );
270 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
292 if( input ) free( input );
293 if( output ) free( output );
294 if( format ) free( format );
297 while( ( audio = hb_list_item( audios, 0 ) ) )
299 hb_list_rem( audios, audio );
300 if( audio->out.name )
302 free( audio->out.name );
306 hb_list_close( &audios );
308 if( mixdowns ) free( mixdowns );
309 if( dynamic_range_compression ) free( dynamic_range_compression );
310 if( atracks ) free( atracks );
311 if( arates ) free( arates );
312 if( abitrates ) free( abitrates );
313 if( acodecs ) free( acodecs );
314 if( anames ) free( anames );
315 if (native_language ) free (native_language );
316 if( x264opts ) free (x264opts );
317 if( x264opts2 ) free (x264opts2 );
318 if (preset_name) free (preset_name);
319 if( stop_at_string ) free( stop_at_string );
321 fprintf( stderr, "HandBrake has exited.\n" );
323 #ifdef PTW32_STATIC_LIB
324 pthread_win32_thread_detach_np();
325 pthread_win32_process_detach_np();
331 static void ShowCommands()
333 fprintf( stdout, "\nCommands:\n" );
334 fprintf( stdout, " [h]elp Show this message\n" );
335 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
336 fprintf( stdout, " [p]ause Pause encoding\n" );
337 fprintf( stdout, " [r]esume Resume encoding\n" );
340 static void PrintTitleInfo( hb_title_t * title )
342 hb_chapter_t * chapter;
343 hb_audio_config_t * audio;
344 hb_subtitle_t * subtitle;
347 fprintf( stderr, "+ title %d:\n", title->index );
348 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
349 title->vts, title->ttn, title->cell_start, title->cell_end,
350 title->block_count );
352 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
353 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
354 title->hours, title->minutes, title->seconds );
355 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
356 title->width, title->height,
357 title->pixel_aspect_width,
358 title->pixel_aspect_height,
359 (float) title->aspect,
360 (float) title->rate / title->rate_base );
361 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
362 title->crop[1], title->crop[2], title->crop[3] );
363 fprintf( stderr, " + chapters:\n" );
364 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
366 chapter = hb_list_item( title->list_chapter, i );
367 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
368 "%02d:%02d:%02d\n", chapter->index,
369 chapter->cell_start, chapter->cell_end,
370 chapter->block_count, chapter->hours, chapter->minutes,
373 fprintf( stderr, " + audio tracks:\n" );
374 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
376 audio = hb_list_audio_config_item( title->list_audio, i );
377 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
379 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
380 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
384 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
387 fprintf( stderr, " + subtitle tracks:\n" );
388 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
390 subtitle = hb_list_item( title->list_subtitle, i );
391 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
395 if(title->detected_interlacing)
397 /* Interlacing was found in half or more of the preview frames */
398 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
403 static int HandleEvents( hb_handle_t * h )
406 int tmp_num_audio_tracks;
408 hb_get_state( h, &s );
415 #define p s.param.scanning
416 case HB_STATE_SCANNING:
417 /* Show what title is currently being scanned */
418 fprintf( stderr, "Scanning title %d", p.title_cur );
420 fprintf( stderr, " of %d", p.title_count );
421 fprintf( stderr, "...\n" );
425 case HB_STATE_SCANDONE:
432 /* Audio argument string parsing variables */
436 int mixdown = HB_AMIXDOWN_DOLBYPLII;
438 /* Audio argument string parsing variables */
440 list = hb_get_titles( h );
442 if( !hb_list_count( list ) )
444 /* No valid title, stop right there */
445 fprintf( stderr, "No title found.\n" );
452 int longest_title_idx=0;
453 int longest_title_pos=-1;
454 int longest_title_time=0;
457 fprintf( stderr, "Searching for longest title...\n" );
459 for( i = 0; i < hb_list_count( list ); i++ )
461 title = hb_list_item( list, i );
462 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
463 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
464 i, title->index, title_time );
465 if( longest_title_time < title_time )
467 longest_title_time = title_time;
468 longest_title_pos = i;
469 longest_title_idx = title->index;
472 if( longest_title_pos == -1 )
474 fprintf( stderr, "No longest title found.\n" );
478 titleindex = longest_title_idx;
479 fprintf( stderr, "Found longest title, setting title to %d\n",
482 title = hb_list_item( list, longest_title_pos);
484 title = hb_list_item( list, 0 );
489 /* Scan-only mode, print infos and exit */
491 for( i = 0; i < hb_list_count( list ); i++ )
493 title = hb_list_item( list, i );
494 PrintTitleInfo( title );
500 /* Set job settings */
503 PrintTitleInfo( title );
505 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
507 job->chapter_start = MAX( job->chapter_start,
509 job->chapter_end = MIN( job->chapter_end,
511 job->chapter_end = MAX( job->chapter_start,
522 fprintf( stderr, "+ Using preset: %s", preset_name);
524 if (!strcmp(preset_name, "Universal"))
530 vcodec = HB_VCODEC_X264;
531 job->vquality = 20.0;
535 atracks = strdup("1,1");
539 acodecs = strdup("faac,ac3");
543 abitrates = strdup("160,160");
547 mixdowns = strdup("dpl2,auto");
551 arates = strdup("48,Auto");
553 if( !dynamic_range_compression )
555 dynamic_range_compression = strdup("0.0,0.0");
560 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:me=umh");
563 job->chapter_markers = 1;
566 if (!strcmp(preset_name, "iPod"))
573 vcodec = HB_VCODEC_X264;
577 atracks = strdup("1");
581 acodecs = strdup("faac");
585 abitrates = strdup("160");
589 mixdowns = strdup("dpl2");
593 arates = strdup("48");
595 if( !dynamic_range_compression )
597 dynamic_range_compression = strdup("0.0");
602 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
604 job->chapter_markers = 1;
607 if (!strcmp(preset_name, "iPhone & iPod Touch"))
613 vcodec = HB_VCODEC_X264;
614 job->vquality = 20.0;
618 atracks = strdup("1");
622 acodecs = strdup("faac");
626 abitrates = strdup("128");
630 mixdowns = strdup("dpl2");
634 arates = strdup("48");
636 if( !dynamic_range_compression )
638 dynamic_range_compression = strdup("0.0");
643 x264opts = strdup("cabac=0:ref=2:mixed-refs:me=umh");
645 job->chapter_markers = 1;
648 if (!strcmp(preset_name, "AppleTV"))
654 job->largeFileSize = 1;
655 vcodec = HB_VCODEC_X264;
656 job->vquality = 20.0;
660 atracks = strdup("1,1");
664 acodecs = strdup("faac,ac3");
668 abitrates = strdup("160,160");
672 mixdowns = strdup("dpl2,auto");
676 arates = strdup("48,Auto");
678 if( !dynamic_range_compression )
680 dynamic_range_compression = strdup("0.0,0.0");
685 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1");
688 job->chapter_markers = 1;
691 if (!strcmp(preset_name, "Normal"))
697 vcodec = HB_VCODEC_X264;
698 job->vquality = 20.0;
702 atracks = strdup("1");
706 acodecs = strdup("faac");
710 abitrates = strdup("160");
714 mixdowns = strdup("dpl2");
718 arates = strdup("48");
720 if( !dynamic_range_compression )
722 dynamic_range_compression = strdup("0.0");
726 x264opts = strdup("ref=2:bframes=2:me=umh");
729 job->chapter_markers = 1;
732 if (!strcmp(preset_name, "High Profile"))
738 vcodec = HB_VCODEC_X264;
739 job->vquality = 20.0;
743 atracks = strdup("1,1");
747 acodecs = strdup("faac,ac3");
751 abitrates = strdup("160,160");
755 mixdowns = strdup("dpl2,auto");
759 arates = strdup("48,Auto");
761 if( !dynamic_range_compression )
763 dynamic_range_compression = strdup("0.0,0.0");
767 x264opts = strdup("ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct");
772 job->chapter_markers = 1;
775 if (!strcmp(preset_name, "Classic"))
781 job->vbitrate = 1000;
784 atracks = strdup("1");
788 acodecs = strdup("faac");
792 abitrates = strdup("160");
796 mixdowns = strdup("dpl2");
800 arates = strdup("48");
802 if( !dynamic_range_compression )
804 dynamic_range_compression = strdup("0.0");
808 if (!strcmp(preset_name, "AppleTV Legacy"))
814 job->largeFileSize = 1;
815 vcodec = HB_VCODEC_X264;
816 job->vbitrate = 2500;
819 atracks = strdup("1,1");
823 acodecs = strdup("faac,ac3");
827 abitrates = strdup("160,160");
831 mixdowns = strdup("dpl2,auto");
835 arates = strdup("48,Auto");
837 if( !dynamic_range_compression )
839 dynamic_range_compression = strdup("0.0,0.0");
843 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0");
846 job->chapter_markers = 1;
849 if (!strcmp(preset_name, "iPhone Legacy"))
856 vcodec = HB_VCODEC_X264;
860 atracks = strdup("1");
864 acodecs = strdup("faac");
868 abitrates = strdup("128");
872 mixdowns = strdup("dpl2");
876 arates = strdup("48");
878 if( !dynamic_range_compression )
880 dynamic_range_compression = strdup("0.0");
885 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0");
887 job->chapter_markers = 1;
890 if (!strcmp(preset_name, "iPod Legacy"))
897 vcodec = HB_VCODEC_X264;
898 job->vbitrate = 1500;
901 atracks = strdup("1");
905 acodecs = strdup("faac");
909 abitrates = strdup("160");
913 mixdowns = strdup("dpl2");
917 arates = strdup("48");
919 if( !dynamic_range_compression )
921 dynamic_range_compression = strdup("0.0");
926 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0");
928 job->chapter_markers = 1;
932 if ( chapter_markers )
934 job->chapter_markers = chapter_markers;
936 if( marker_file != NULL )
938 hb_csv_file_t * file = hb_open_csv_file( marker_file );
939 hb_csv_cell_t * cell;
943 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
947 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
951 /* Parse the cells */
952 while( NULL != ( cell = hb_read_next_cell( file ) ) )
954 /* We have a chapter number */
955 if( cell->cell_col == 0 )
957 row = cell->cell_row;
958 chapter = atoi( cell->cell_text );
961 /* We have a chapter name */
962 if( cell->cell_col == 1 && row == cell->cell_row )
964 /* If we have a valid chapter, copy the string an terminate it */
965 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
967 hb_chapter_t * chapter_s;
969 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
970 strncpy(chapter_s->title, cell->cell_text, 1023);
971 chapter_s->title[1023] = '\0';
976 hb_dispose_cell( cell );
979 hb_close_csv_file( file );
986 int number_of_chapters = hb_list_count(job->title->list_chapter);
989 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
991 hb_chapter_t * chapter_s;
992 chapter_s = hb_list_item( job->title->list_chapter, chapter);
993 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
994 chapter_s->title[1023] = '\0';
999 if( crop[0] >= 0 && crop[1] >= 0 &&
1000 crop[2] >= 0 && crop[3] >= 0 )
1002 memcpy( job->crop, crop, 4 * sizeof( int ) );
1005 job->deinterlace = deinterlace;
1006 job->grayscale = grayscale;
1008 /* Add selected filters */
1009 job->filters = hb_list_init();
1012 hb_filter_detelecine.settings = detelecine_opt;
1013 hb_list_add( job->filters, &hb_filter_detelecine );
1017 hb_filter_decomb.settings = decomb_opt;
1018 hb_list_add( job->filters, &hb_filter_decomb );
1022 hb_filter_deinterlace.settings = deinterlace_opt;
1023 hb_list_add( job->filters, &hb_filter_deinterlace );
1027 hb_filter_deblock.settings = deblock_opt;
1028 hb_list_add( job->filters, &hb_filter_deblock );
1032 hb_filter_denoise.settings = denoise_opt;
1033 hb_list_add( job->filters, &hb_filter_denoise );
1036 switch( anamorphic_mode )
1038 case 0: // Non-anamorphic
1040 if( width && height )
1043 job->height = height;
1048 hb_fix_aspect( job, HB_KEEP_WIDTH );
1052 job->height = height;
1053 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1055 else if( !width && !height )
1057 hb_fix_aspect( job, HB_KEEP_WIDTH );
1062 case 1: // Strict anammorphic
1063 job->anamorphic.mode = anamorphic_mode;
1066 case 2: // Loose anamorphic
1067 job->anamorphic.mode = 2;
1071 job->anamorphic.modulus = modulus;
1076 job->anamorphic.itu_par = itu_par;
1083 else if( !width && !height )
1085 /* Default to full width when one isn't specified for loose anamorphic */
1086 job->width = title->width - job->crop[2] - job->crop[3];
1091 case 3: // Custom Anamorphic 3: Power User Jamboree
1092 job->anamorphic.mode = 3;
1096 job->anamorphic.modulus = modulus;
1101 job->anamorphic.itu_par = itu_par;
1104 if( par_width && par_height )
1106 job->anamorphic.par_width = par_width;
1107 job->anamorphic.par_height = par_height;
1110 if( keep_display_aspect )
1112 job->anamorphic.keep_display_aspect = 1;
1114 /* First, what *is* the display aspect? */
1115 int cropped_width = title->width - job->crop[2] - job->crop[3];
1116 int cropped_height = title->height - job->crop[0] - job->crop[1];
1118 /* XXX -- I'm assuming people want to keep the source
1119 display AR even though they might have already
1120 asked for ITU values instead. */
1121 float source_display_width = (float)cropped_width *
1122 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1123 float display_aspect = source_display_width / (float)cropped_height;
1124 /* When keeping display aspect, we have to rank some values
1125 by priority in order to consistently handle situations
1126 when more than one might be specified by default.
1128 * First off, PAR gets ignored. (err make this reality)
1129 * Height will be respected over all other settings,
1130 * If it isn't set, display_width will be followed.
1131 * If it isn't set, width will be followed. */
1134 /* We scale the display width to the new height */
1135 display_width = (int)( (double)height * display_aspect );
1137 else if( display_width )
1139 /* We scale the height to the new display width */
1140 height = (int)( (double)display_width / display_aspect );
1146 /* Adjust the PAR to create the new display width
1147 from the default job width. */
1148 job->anamorphic.dar_width = display_width;
1150 job->anamorphic.dar_height = height ?
1152 title->height - job->crop[0] - job->crop[1];
1155 if( width && height )
1157 /* Use these storage dimensions */
1159 job->height = height;
1163 /* Use just this storage width */
1165 job->height = title->height - job->crop[0] - job->crop[1];
1169 /* Use just this storage height. */
1170 job->height = height;
1171 job->width = title->width - job->crop[2] - job->crop[3];
1173 else if( !width && !height )
1175 /* Assume source dimensions after cropping. */
1176 job->width = title->width - job->crop[2] - job->crop[3];
1177 job->height = title->height - job->crop[0] - job->crop[1];
1183 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1185 job->vquality = vquality;
1190 job->vquality = -1.0;
1191 job->vbitrate = vbitrate;
1195 job->vcodec = vcodec;
1199 job->h264_level = 13;
1203 job->h264_level = 30;
1208 job->vrate = 27000000;
1209 job->vrate_base = vrate;
1213 // cfr or pfr flag with no rate specified implies
1214 // use the title rate.
1216 job->vrate = title->rate;
1217 job->vrate_base = title->rate_base;
1220 /* Grab audio tracks */
1223 char * token = strtok(atracks, ",");
1226 int track_start, track_end;
1227 while( token != NULL )
1229 audio = calloc(1, sizeof(*audio));
1230 hb_audio_config_init(audio);
1231 if (strlen(token) >= 3)
1233 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1236 for (i = track_start - 1; i < track_end; i++)
1238 if (i != track_start - 1)
1240 audio = calloc(1, sizeof(*audio));
1241 hb_audio_config_init(audio);
1243 audio->in.track = i;
1244 audio->out.track = num_audio_tracks++;
1245 hb_list_add(audios, audio);
1248 else if( !strcasecmp(token, "none" ) )
1250 audio->in.track = audio->out.track = -1;
1251 audio->out.codec = 0;
1252 hb_list_add(audios, audio);
1257 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1264 audio->in.track = atoi(token) - 1;
1265 audio->out.track = num_audio_tracks++;
1266 hb_list_add(audios, audio);
1268 token = strtok(NULL, ",");
1272 /* Parse audio tracks */
1273 if( hb_list_count(audios) == 0 )
1275 /* Create a new audio track with default settings */
1276 audio = calloc(1, sizeof(*audio));
1277 hb_audio_config_init(audio);
1278 /* Add it to our audios */
1279 hb_list_add(audios, audio);
1282 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1283 for (i = 0; i < tmp_num_audio_tracks; i++)
1285 audio = hb_list_item(audios, 0);
1286 if( (audio == NULL) || (audio->in.track == -1) ||
1287 (audio->out.track == -1) || (audio->out.codec == 0) )
1293 if( hb_audio_add( job, audio ) == 0 )
1295 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1296 audio->in.track + 1 );
1301 hb_list_rem(audios, audio);
1303 if( audio->out.name )
1305 free( audio->out.name);
1314 char * token = strtok(acodecs, ",");
1317 while ( token != NULL )
1319 if ((acodec = get_acodec_for_string(token)) == -1)
1321 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1322 acodec = default_acodec;
1324 if( i < num_audio_tracks )
1326 audio = hb_list_audio_config_item(job->list_audio, i);
1327 audio->out.codec = acodec;
1331 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1332 hb_audio_config_t audio;
1336 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1338 hb_audio_config_init(&audio);
1339 audio.in.track = last_audio->in.track;
1340 audio.out.track = num_audio_tracks++;
1341 audio.out.codec = acodec;
1342 hb_audio_add(job, &audio);
1346 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1349 token = strtok(NULL, ",");
1353 if( i < num_audio_tracks )
1355 /* We have fewer inputs than audio tracks, use the default codec for
1356 * this container for the remaining tracks. Unless we only have one input
1357 * then use that codec instead.
1360 acodec = default_acodec;
1361 for ( ; i < num_audio_tracks; i++)
1363 audio = hb_list_audio_config_item(job->list_audio, i);
1364 audio->out.codec = acodec;
1373 char * token = strtok(arates, ",");
1376 while ( token != NULL )
1378 arate = atoi(token);
1379 audio = hb_list_audio_config_item(job->list_audio, i);
1382 for( j = 0; j < hb_audio_rates_count; j++ )
1384 if( !strcmp( token, hb_audio_rates[j].string ) )
1386 arate = hb_audio_rates[j].rate;
1393 if (!is_sample_rate_valid(arate))
1395 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1396 arate = audio->in.samplerate;
1399 audio->out.samplerate = arate;
1400 if( (++i) >= num_audio_tracks )
1401 break; /* We have more inputs than audio tracks, oops */
1405 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1407 token = strtok(NULL, ",");
1410 if (i < num_audio_tracks)
1412 /* We have fewer inputs than audio tracks, use default sample rate.
1413 * Unless we only have one input, then use that for all tracks.
1416 arate = audio->in.samplerate;
1417 for ( ; i < num_audio_tracks; i++)
1419 audio = hb_list_audio_config_item(job->list_audio, i);
1420 audio->out.samplerate = arate;
1429 char * token = strtok(abitrates, ",");
1432 while ( token != NULL )
1434 abitrate = atoi(token);
1435 audio = hb_list_audio_config_item(job->list_audio, i);
1439 audio->out.bitrate = abitrate;
1440 if( (++i) >= num_audio_tracks )
1441 break; /* We have more inputs than audio tracks, oops */
1445 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1447 token = strtok(NULL, ",");
1450 if (i < num_audio_tracks)
1452 /* We have fewer inputs than audio tracks, use the default bitrate
1453 * for the remaining tracks. Unless we only have one input, then use
1454 * that for all tracks.
1457 abitrate = default_abitrate;
1458 for (; i < num_audio_tracks; i++)
1460 audio = hb_list_audio_config_item(job->list_audio, i);
1461 audio->out.bitrate = abitrate;
1468 if ( dynamic_range_compression )
1470 char * token = strtok(dynamic_range_compression, ",");
1472 token = dynamic_range_compression;
1473 while ( token != NULL )
1475 d_r_c = atof(token);
1476 audio = hb_list_audio_config_item(job->list_audio, i);
1479 audio->out.dynamic_range_compression = d_r_c;
1480 if( (++i) >= num_audio_tracks )
1481 break; /* We have more inputs than audio tracks, oops */
1485 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1487 token = strtok(NULL, ",");
1490 if (i < num_audio_tracks)
1492 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1493 * tracks. Unless we only have one input, then use the same DRC for all
1498 for (; i < num_audio_tracks; i++)
1500 audio = hb_list_audio_config_item(job->list_audio, i);
1501 audio->out.dynamic_range_compression = d_r_c;
1510 char * token = strtok(mixdowns, ",");
1513 while ( token != NULL )
1515 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1516 audio = hb_list_audio_config_item(job->list_audio, i);
1519 audio->out.mixdown = mixdown;
1520 if( (++i) >= num_audio_tracks )
1521 break; /* We have more inputs than audio tracks, oops */
1525 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1527 token = strtok(NULL, ",");
1530 if (i < num_audio_tracks)
1532 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1533 * we only have one input, then use that.
1536 mixdown = HB_AMIXDOWN_DOLBYPLII;
1537 for (; i < num_audio_tracks; i++)
1539 audio = hb_list_audio_config_item(job->list_audio, i);
1540 audio->out.mixdown = mixdown;
1545 /* Audio Track Names */
1549 char * token = strtok(anames, ",");
1552 while ( token != NULL )
1554 audio = hb_list_audio_config_item(job->list_audio, i);
1557 audio->out.name = strdup(token);
1558 if( (++i) >= num_audio_tracks )
1559 break; /* We have more names than audio tracks, oops */
1563 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1566 token = strtok(NULL, ",");
1569 if( i < num_audio_tracks && i == 1 )
1571 /* We have exactly one name and more than one audio track. Use the same
1572 * name for all tracks. */
1573 for ( ; i < num_audio_tracks; i++)
1575 audio = hb_list_audio_config_item(job->list_audio, i);
1576 audio->out.name = strdup(anames);
1579 /* Audio Track Names */
1583 job->vbitrate = hb_calc_bitrate( job, size );
1584 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1590 hb_subtitle_t *subtitle;
1592 * Find the subtitle with the same track as "sub" and
1593 * add that to the job subtitle list
1595 subtitle = hb_list_item( title->list_subtitle, sub-1 );
1597 if( subtitle_force ) {
1598 subtitle->config.force = subtitle_force;
1600 hb_list_add( job->list_subtitle, subtitle );
1602 fprintf( stderr, "Could not find subtitle track %d, skipped\n", sub );
1606 if( native_language )
1608 job->native_language = strdup( native_language );
1616 if ( largeFileSize )
1618 job->largeFileSize = 1;
1622 job->mp4_optimize = 1;
1629 job->file = strdup( output );
1638 job->color_matrix = color_matrix;
1641 if( x264opts != NULL && *x264opts != '\0' )
1643 job->x264opts = x264opts;
1645 else /*avoids a bus error crash when options aren't specified*/
1647 job->x264opts = NULL;
1650 job->maxWidth = maxWidth;
1652 job->maxHeight = maxHeight;
1654 if( start_at_preview )
1656 job->start_at_preview = start_at_preview - 1;
1657 job->seek_points = preview_count;
1662 job->pts_to_stop = stop_at_pts;
1668 job->frame_to_stop = stop_at_frame;
1677 * When subtitle scan is enabled do a fast pre-scan job
1678 * which will determine which subtitles to enable, if any.
1682 x264opts_tmp = job->x264opts;
1684 job->x264opts = NULL;
1686 job->indepth_scan = subtitle_scan;
1687 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1688 "subtitles if found for foreign language segments\n");
1689 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1690 *(job->select_subtitle) = NULL;
1692 job->select_subtitle_config.dest = RENDERSUB;
1693 job->select_subtitle_config.default_track = 0;
1694 job->select_subtitle_config.force = subtitle_force;
1697 * Add the pre-scan job
1701 job->x264opts = x264opts_tmp;
1707 * If subtitle_scan is enabled then only turn it on
1708 * for the first pass and then off again for the
1711 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1713 job->select_subtitle = NULL;
1717 job->indepth_scan = 0;
1721 x264opts2 = strdup(x264opts);
1725 * If turbo options have been selected then append them
1726 * to the x264opts now (size includes one ':' and the '\0')
1728 if( turbo_opts_enabled )
1730 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1733 tmp_x264opts = malloc(size * sizeof(char));
1736 snprintf( tmp_x264opts, size, "%s:%s",
1737 x264opts, turbo_opts );
1741 * No x264opts to modify, but apply the turbo options
1742 * anyway as they may be modifying defaults
1744 snprintf( tmp_x264opts, size, "%s",
1747 x264opts = tmp_x264opts;
1749 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1752 job->x264opts = x264opts;
1756 job->select_subtitle = subtitle_tmp;
1760 * On the second pass we turn off subtitle scan so that we
1761 * can actually encode using any subtitles that were auto
1762 * selected in the first pass (using the whacky select-subtitle
1763 * attribute of the job).
1765 job->indepth_scan = 0;
1767 job->x264opts = x264opts2;
1774 * Turn on subtitle scan if requested, note that this option
1775 * precludes encoding of any actual subtitles.
1778 job->indepth_scan = 0;
1786 #define p s.param.working
1787 case HB_STATE_WORKING:
1788 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1789 p.job_cur, p.job_count, 100.0 * p.progress );
1790 if( p.seconds > -1 )
1792 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1793 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1794 p.hours, p.minutes, p.seconds );
1800 #define p s.param.muxing
1801 case HB_STATE_MUXING:
1803 if (show_mux_warning)
1805 fprintf( stdout, "\rMuxing: this may take awhile..." );
1807 show_mux_warning = 0;
1813 #define p s.param.workdone
1814 case HB_STATE_WORKDONE:
1815 /* Print error if any, then exit */
1819 fprintf( stderr, "\nRip done!\n" );
1821 case HB_ERROR_CANCELED:
1822 fprintf( stderr, "\nRip canceled.\n" );
1825 fprintf( stderr, "\nRip failed (error %x).\n",
1835 /****************************************************************************
1837 ****************************************************************************/
1838 static volatile int64_t i_die_date = 0;
1839 void SigHandler( int i_signal )
1844 i_die_date = hb_get_date();
1845 fprintf( stderr, "Signal %d received, terminating - do it "
1846 "again in case it gets stuck\n", i_signal );
1848 else if( i_die_date + 500 < hb_get_date() )
1850 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1855 /****************************************************************************
1857 ****************************************************************************/
1858 static void ShowHelp()
1861 FILE* const out = stdout;
1864 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1866 "### General Handbrake Options------------------------------------------------\n\n"
1867 " -h, --help Print help\n"
1868 " -u, --update Check for updates and exit\n"
1869 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
1870 " -C, --cpu Set CPU count (default: autodetected)\n"
1871 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1872 " if the preset name has spaces, surround it with\n"
1873 " double quotation marks\n"
1874 " -z, --preset-list See a list of available built-in presets\n"
1875 " --dvdnav Use dvdnav (Experimental)\n"
1878 "### Source Options-----------------------------------------------------------\n\n"
1879 " -i, --input <string> Set input device\n"
1880 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1882 " -L, --longest Select the longest title\n"
1883 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1884 " 1 to 3, or \"3\" for chapter 3 only,\n"
1885 " default: all chapters)\n"
1886 " --angle <number> Select the DVD angle\n"
1887 " --previews <#:B> Select how many preview images are generated (max 30),\n"
1888 " and whether or not they're stored to disk (0 or 1).\n"
1889 " (default: 10:0)\n"
1890 " --start-at-preview <#> Start encoding at a given preview.\n"
1891 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
1892 " or pts (on a 90kHz clock)"
1895 "### Destination Options------------------------------------------------------\n\n"
1896 " -o, --output <string> Set output file name\n"
1897 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1898 " autodetected from file name)\n"
1899 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
1900 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1901 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
1902 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1903 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
1907 "### Video Options------------------------------------------------------------\n\n"
1908 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
1909 " (default: ffmpeg)\n"
1910 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1911 " same style as mencoder:\n"
1912 " option1=value1:option2=value2\n"
1913 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1914 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1915 " -S, --size <MB> Set target size\n"
1916 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1917 " -2, --two-pass Use two-pass mode\n"
1918 " -T, --turbo When using 2-pass use the turbo options\n"
1919 " on the first pass to improve speed\n"
1920 " (only works with x264, affects PSNR by about 0.05dB,\n"
1921 " and increases first pass speed two to four times)\n"
1922 " -r, --rate Set video framerate (" );
1923 for( i = 0; i < hb_video_rates_count; i++ )
1925 fprintf( out, hb_video_rates[i].string );
1926 if( i != hb_video_rates_count - 1 )
1927 fprintf( out, "/" );
1930 " Be aware that not specifying a framerate lets\n"
1931 " HandBrake preserve a source's time stamps,\n"
1932 " potentially creating variable framerate video\n"
1933 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
1934 " frame rate control. VFR preserves the source\n"
1935 " timing. CFR makes the output constant rate at\n"
1936 " the rate given by the -r flag (or the source's\n"
1937 " average rate if no -r is given). PFR doesn't\n"
1938 " allow the rate to go over the rate specified\n"
1939 " with the -r flag but won't change the source\n"
1940 " timing if it's below that rate.\n"
1941 " If none of these flags are given, the default\n"
1942 " is --cfr when -r is given and --vfr otherwise\n"
1945 "### Audio Options-----------------------------------------------------------\n\n"
1946 " -a, --audio <string> Select audio track(s), separated by commas\n"
1947 " More than one output track can be used for one\n"
1949 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1950 " tracks, default: first one)\n" );
1954 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
1955 " ac3 and dts meaning passthrough\n"
1956 " Separated by commas for more than one audio track.\n"
1957 " (default: guessed)\n" );
1960 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
1961 " ac3 and dts meaning passthrough\n"
1962 " Separated by commas for more than one audio track.\n"
1963 " (default: guessed)\n" );
1966 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
1967 " Separated by commas for more than one audio track.\n"
1968 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1969 " Separated by commas for more than one audio track.\n"
1970 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1971 " -R, --arate Set audio samplerate(s) (" );
1972 for( i = 0; i < hb_audio_rates_count; i++ )
1974 fprintf( out, hb_audio_rates[i].string );
1975 if( i != hb_audio_rates_count - 1 )
1976 fprintf( out, "/" );
1978 fprintf( out, " kHz)\n"
1979 " Separated by commas for more than one audio track.\n"
1980 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1981 " making soft sounds louder. Range is 1.0 to 4.0\n"
1982 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1983 " Separated by commas for more than one audio track.\n"
1984 " -A, --aname <string> Audio track name(s),\n"
1985 " Separated by commas for more than one audio track.\n"
1988 "### Picture Settings---------------------------------------------------------\n\n"
1989 " -w, --width <number> Set picture width\n"
1990 " -l, --height <number> Set picture height\n"
1991 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1992 " -Y, --maxHeight <#> Set maximum height\n"
1993 " -X, --maxWidth <#> Set maximum width\n"
1994 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
1995 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
1996 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
1997 " directly control all parameters.\n"
1998 " --display-width Set the width to scale the actual pixels to\n"
1999 " <number> at playback, for custom anamorphic.\n"
2000 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2001 " when using custom anamorphic\n"
2002 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2004 " (--display-width and --pixel-aspect are mutually\n"
2005 " exclusive and the former will override the latter)\n"
2006 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2007 " custom anamorphic, useful with underscanned sources\n"
2008 " --modulus Set the number you want the scaled pixel dimensions\n"
2009 " <number> to divide cleanly by, for loose and custom\n"
2010 " anamorphic modes (default: 16)\n"
2011 " -M --color-matrix Set the color space signaled by the output\n"
2012 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2013 " default: set by resolution)\n"
2016 "### Filters---------------------------------------------------------\n\n"
2018 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2019 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2021 " <fast/slow/slower>\n"
2022 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2023 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2024 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2025 " Note: this filter drops duplicate frames to\n"
2026 " restore the pre-telecine framerate, unless you\n"
2027 " specify a constant framerate (--rate 29.97)\n"
2028 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2029 " -8, --denoise Denoise video with hqdn3d filter\n"
2030 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2032 " <weak/medium/strong>\n"
2033 " -7, --deblock Deblock video with pp7 filter\n"
2034 " <QP:M> (default 5:2)\n"
2035 " -g, --grayscale Grayscale encoding\n"
2038 "### Subtitle Options------------------------------------------------------------\n\n"
2039 " -s, --subtitle <number> Select subtitle (default: none)\n"
2040 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
2041 " the one that's only used 10 percent of the time\n"
2042 " or less. This should locate subtitles for short\n"
2043 " foreign language segments. Best used in conjunction\n"
2044 " with --subtitle-forced.\n"
2045 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2046 " the subtitle has the forced flag set. May be used in\n"
2047 " conjunction with --subtitle-scan to auto-select\n"
2048 " a stream if it contains forced subtitles.\n"
2049 " -N, --native-language Select subtitles with this language if it does not\n"
2050 " <string> match the Audio language. Provide the language's\n"
2051 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2060 /****************************************************************************
2062 ****************************************************************************/
2063 static void ShowPresets()
2065 printf("\n< Apple\n");
2067 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:mixed-refs=1:me=umh\n");
2069 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1\n");
2071 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:mixed-refs:me=umh\n");
2073 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1\n");
2077 printf("\n< Regular\n");
2079 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 --strict-anamorphic -m -x ref=2:bframes=2:me=umh\n");
2081 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct\n");
2085 printf("\n< Legacy\n");
2087 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2089 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0\n");
2091 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0\n");
2093 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0\n");
2098 /****************************************************************************
2100 ****************************************************************************/
2101 static int ParseOptions( int argc, char ** argv )
2104 #define PREVIEWS 257
2105 #define START_AT_PREVIEW 258
2109 #define DISPLAY_WIDTH 262
2110 #define PIXEL_ASPECT 263
2112 #define KEEP_DISPLAY_ASPECT 265
2116 static struct option long_options[] =
2118 { "help", no_argument, NULL, 'h' },
2119 { "update", no_argument, NULL, 'u' },
2120 { "verbose", optional_argument, NULL, 'v' },
2121 { "cpu", required_argument, NULL, 'C' },
2122 { "dvdnav", no_argument, NULL, DVDNAV },
2124 { "format", required_argument, NULL, 'f' },
2125 { "input", required_argument, NULL, 'i' },
2126 { "output", required_argument, NULL, 'o' },
2127 { "large-file", no_argument, NULL, '4' },
2128 { "optimize", no_argument, NULL, 'O' },
2129 { "ipod-atom", no_argument, NULL, 'I' },
2131 { "title", required_argument, NULL, 't' },
2132 { "longest", no_argument, NULL, 'L' },
2133 { "chapters", required_argument, NULL, 'c' },
2134 { "angle", required_argument, NULL, ANGLE },
2135 { "markers", optional_argument, NULL, 'm' },
2136 { "audio", required_argument, NULL, 'a' },
2137 { "mixdown", required_argument, NULL, '6' },
2138 { "drc", required_argument, NULL, 'D' },
2139 { "subtitle", required_argument, NULL, 's' },
2140 { "subtitle-scan", no_argument, NULL, 'U' },
2141 { "subtitle-forced", no_argument, NULL, 'F' },
2142 { "native-language", required_argument, NULL,'N' },
2144 { "encoder", required_argument, NULL, 'e' },
2145 { "aencoder", required_argument, NULL, 'E' },
2146 { "two-pass", no_argument, NULL, '2' },
2147 { "deinterlace", optional_argument, NULL, 'd' },
2148 { "deblock", optional_argument, NULL, '7' },
2149 { "denoise", optional_argument, NULL, '8' },
2150 { "detelecine", optional_argument, NULL, '9' },
2151 { "decomb", optional_argument, NULL, '5' },
2152 { "grayscale", no_argument, NULL, 'g' },
2153 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2154 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2155 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2156 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2157 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2158 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2159 { "modulus", required_argument, NULL, MODULUS },
2160 { "itu-par", no_argument, &itu_par, 1 },
2161 { "width", required_argument, NULL, 'w' },
2162 { "height", required_argument, NULL, 'l' },
2163 { "crop", required_argument, NULL, 'n' },
2165 { "vb", required_argument, NULL, 'b' },
2166 { "quality", required_argument, NULL, 'q' },
2167 { "size", required_argument, NULL, 'S' },
2168 { "ab", required_argument, NULL, 'B' },
2169 { "rate", required_argument, NULL, 'r' },
2170 { "arate", required_argument, NULL, 'R' },
2171 { "cqp", no_argument, NULL, 'Q' },
2172 { "x264opts", required_argument, NULL, 'x' },
2173 { "turbo", no_argument, NULL, 'T' },
2174 { "maxHeight", required_argument, NULL, 'Y' },
2175 { "maxWidth", required_argument, NULL, 'X' },
2176 { "preset", required_argument, NULL, 'Z' },
2177 { "preset-list", no_argument, NULL, 'z' },
2179 { "aname", required_argument, NULL, 'A' },
2180 { "color-matrix",required_argument, NULL, 'M' },
2181 { "previews", required_argument, NULL, PREVIEWS },
2182 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2183 { "stop-at", required_argument, NULL, STOP_AT },
2184 { "vfr", no_argument, &cfr, 0 },
2185 { "cfr", no_argument, &cfr, 1 },
2186 { "pfr", no_argument, &cfr, 2 },
2190 int option_index = 0;
2193 c = getopt_long( argc, argv,
2194 "hv::uC:f:4i:Io:t:Lc:m::M:a:A:6:s:UFN:e:E:2dD:7895gOw:l:n:b:q:S:B:r:R:Qx:TY:X:Z:z",
2195 long_options, &option_index );
2204 /* option was handled entirely in getopt_long */
2213 if( optarg != NULL )
2215 debug = atoi( optarg );
2223 cpu = atoi( optarg );
2228 preset_name = strdup(optarg);
2238 format = strdup( optarg );
2241 input = strdup( optarg );
2243 char *devName = bsd_name_for_path( input ); // alloc
2246 if( device_is_dvd( devName ))
2249 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2250 sprintf( input, "/dev/%s", devName );
2257 output = strdup( optarg );
2270 titleindex = atoi( optarg );
2278 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2280 chapter_start = start;
2283 else if( sscanf( optarg, "%d", &start ) == 1 )
2285 chapter_start = start;
2286 chapter_end = chapter_start;
2290 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2297 angle = atoi( optarg );
2300 if( optarg != NULL )
2302 marker_file = strdup( optarg );
2304 chapter_markers = 1;
2307 if( optarg != NULL )
2309 atracks = strdup( optarg );
2317 if( optarg != NULL )
2319 mixdowns = strdup( optarg );
2323 if( optarg != NULL )
2325 dynamic_range_compression = strdup( optarg );
2329 sub = atoi( optarg );
2338 native_language = strdup( optarg );
2344 if( optarg != NULL )
2346 if (!( strcmp( optarg, "fast" ) ))
2348 deinterlace_opt = "-1";
2350 else if (!( strcmp( optarg, "slow" ) ))
2352 deinterlace_opt = "2";
2354 else if (!( strcmp( optarg, "slower" ) ))
2356 deinterlace_opt = "0";
2360 deinterlace_opt = strdup( optarg );
2366 if( optarg != NULL )
2368 deblock_opt = strdup( optarg );
2373 if( optarg != NULL )
2375 if (!( strcmp( optarg, "weak" ) ))
2377 denoise_opt = "2:1:2:3";
2379 else if (!( strcmp( optarg, "medium" ) ))
2381 denoise_opt = "3:2:2:3";
2383 else if (!( strcmp( optarg, "strong" ) ))
2385 denoise_opt = "7:7:5:5";
2389 denoise_opt = strdup( optarg );
2395 if( optarg != NULL )
2397 detelecine_opt = strdup( optarg );
2402 if( optarg != NULL )
2404 decomb_opt = strdup( optarg );
2412 if( optarg != NULL )
2414 sscanf( optarg, "%i", &display_width );
2418 if( optarg != NULL )
2420 sscanf( optarg, "%i:%i", &par_width, &par_height );
2424 if( optarg != NULL )
2426 sscanf( optarg, "%i", &modulus );
2430 if( !strcasecmp( optarg, "ffmpeg" ) )
2432 vcodec = HB_VCODEC_FFMPEG;
2434 else if( !strcasecmp( optarg, "x264" ) )
2436 vcodec = HB_VCODEC_X264;
2438 else if( !strcasecmp( optarg, "x264b13" ) )
2440 vcodec = HB_VCODEC_X264;
2443 else if( !strcasecmp( optarg, "x264b30" ) )
2445 vcodec = HB_VCODEC_X264;
2448 else if( !strcasecmp( optarg, "theora" ) )
2450 vcodec = HB_VCODEC_THEORA;
2454 fprintf( stderr, "invalid codec (%s)\n", optarg );
2459 if( optarg != NULL )
2461 acodecs = strdup( optarg );
2465 width = atoi( optarg );
2468 height = atoi( optarg );
2473 char * tmp = optarg;
2474 for( i = 0; i < 4; i++ )
2478 crop[i] = strtol( tmp, &tmp, 0 );
2487 for( i = 0; i < hb_video_rates_count; i++ )
2489 if( !strcmp( optarg, hb_video_rates[i].string ) )
2491 vrate = hb_video_rates[i].rate;
2497 fprintf( stderr, "invalid framerate %s\n", optarg );
2499 else if ( cfr == 0 )
2506 if( optarg != NULL )
2508 arates = strdup( optarg );
2512 vbitrate = atoi( optarg );
2515 vquality = atof( optarg );
2518 size = atoi( optarg );
2521 if( optarg != NULL )
2523 abitrates = strdup( optarg );
2530 x264opts = strdup( optarg );
2533 turbo_opts_enabled = 1;
2536 maxHeight = atoi( optarg );
2539 maxWidth = atoi (optarg );
2542 if( optarg != NULL )
2544 anames = strdup( optarg );
2548 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2550 case START_AT_PREVIEW:
2551 start_at_preview = atoi( optarg );
2554 stop_at_string = strdup( optarg );
2555 stop_at_token = strtok( stop_at_string, ":");
2556 if( !strcmp( stop_at_token, "frame" ) )
2558 stop_at_token = strtok( NULL, ":");
2559 stop_at_frame = atoi(stop_at_token);
2561 else if( !strcmp( stop_at_token, "pts" ) )
2563 stop_at_token = strtok( NULL, ":");
2564 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2566 else if( !strcmp( stop_at_token, "duration" ) )
2568 stop_at_token = strtok( NULL, ":");
2569 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2570 stop_at_pts *= 90000LL;
2574 if( atoi( optarg ) == 601 )
2576 else if( atoi( optarg ) == 709 )
2580 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2588 static int CheckOptions( int argc, char ** argv )
2595 if( input == NULL || *input == '\0' )
2597 fprintf( stderr, "Missing input device. Run %s --help for "
2598 "syntax.\n", argv[0] );
2603 if( titleindex > 0 )
2605 if( output == NULL || *output == '\0' )
2607 fprintf( stderr, "Missing output file name. Run %s --help "
2608 "for syntax.\n", argv[0] );
2614 char * p = strrchr( output, '.' );
2617 if( p && !strcasecmp( p, ".avi" ) )
2620 default_acodec = HB_ACODEC_LAME;
2622 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2623 !strcasecmp( p, ".m4v" ) ) )
2629 default_acodec = HB_ACODEC_FAAC;
2631 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2632 !strcasecmp( p, ".ogg" ) ) )
2635 default_acodec = HB_ACODEC_VORBIS;
2637 else if( p && !strcasecmp(p, ".mkv" ) )
2640 default_acodec = HB_ACODEC_AC3;
2644 fprintf( stderr, "Output format couldn't be guessed "
2645 "from file name, using default.\n" );
2649 else if( !strcasecmp( format, "avi" ) )
2652 default_acodec = HB_ACODEC_LAME;
2654 else if( !strcasecmp( format, "mp4" ) ||
2655 !strcasecmp( format, "m4v" ) )
2661 default_acodec = HB_ACODEC_FAAC;
2663 else if( !strcasecmp( format, "ogm" ) ||
2664 !strcasecmp( format, "ogg" ) )
2667 default_acodec = HB_ACODEC_VORBIS;
2669 else if( !strcasecmp( format, "mkv" ) )
2672 default_acodec = HB_ACODEC_AC3;
2676 fprintf( stderr, "Invalid output format (%s). Possible "
2677 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2685 static int get_acodec_for_string( char *codec )
2687 if( !strcasecmp( codec, "ac3" ) )
2689 return HB_ACODEC_AC3;
2691 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2693 return HB_ACODEC_DCA;
2695 else if( !strcasecmp( codec, "lame" ) )
2697 return HB_ACODEC_LAME;
2699 else if( !strcasecmp( codec, "faac" ) )
2701 return HB_ACODEC_FAAC;
2703 else if( !strcasecmp( codec, "vorbis") )
2705 return HB_ACODEC_VORBIS;
2708 else if( !strcasecmp( codec, "ca_aac") )
2710 return HB_ACODEC_CA_AAC;
2719 static int is_sample_rate_valid(int rate)
2722 for( i = 0; i < hb_audio_rates_count; i++ )
2724 if (rate == hb_audio_rates[i].rate)
2731 /****************************************************************************
2734 * Returns the BSD device name for the block device that contains the
2735 * passed-in path. Returns NULL on failure.
2736 ****************************************************************************/
2737 static char* bsd_name_for_path(char *path)
2741 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2747 // Get the volume reference number.
2748 FSCatalogInfo catalogInfo;
2749 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2755 FSVolumeRefNum volRefNum = catalogInfo.volume;
2757 // Now let's get the device name
2758 GetVolParmsInfoBuffer volumeParms;
2759 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2765 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
2766 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
2767 if( volumeParms.vMVersion < 4 )
2772 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
2773 if( !volumeParms.vMDeviceID )
2778 return strdup( volumeParms.vMDeviceID );
2781 /****************************************************************************
2784 * Returns whether or not the passed in BSD device represents a DVD, or other
2786 ****************************************************************************/
2787 static int device_is_dvd(char *device)
2789 io_service_t service = get_iokit_service(device);
2790 if( service == IO_OBJECT_NULL )
2794 int result = is_dvd_service(service);
2795 IOObjectRelease(service);
2799 /****************************************************************************
2802 * Returns the IOKit service object for the passed in BSD device name.
2803 ****************************************************************************/
2804 static io_service_t get_iokit_service( char *device )
2806 CFMutableDictionaryRef matchingDict;
2807 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2808 if( matchingDict == NULL )
2810 return IO_OBJECT_NULL;
2812 // Fetch the object with the matching BSD node name. There should only be
2813 // one match, so IOServiceGetMatchingService is used instead of
2814 // IOServiceGetMatchingServices to simplify the code.
2815 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2818 /****************************************************************************
2821 * Returns whether or not the service passed in is a DVD.
2823 * Searches for an IOMedia object that represents the entire (whole) media that
2824 * the volume is on. If the volume is on partitioned media, the whole media
2825 * object will be a parent of the volume's media object. If the media is not
2826 * partitioned, the volume's media object will be the whole media object.
2827 ****************************************************************************/
2828 static int is_dvd_service( io_service_t service )
2830 kern_return_t kernResult;
2833 // Create an iterator across all parents of the service object passed in.
2834 kernResult = IORegistryEntryCreateIterator( service,
2836 kIORegistryIterateRecursively | kIORegistryIterateParents,
2838 if( kernResult != KERN_SUCCESS )
2842 if( iter == IO_OBJECT_NULL )
2847 // A reference on the initial service object is released in the do-while
2848 // loop below, so add a reference to balance.
2849 IOObjectRetain( service );
2854 if( is_whole_media_service( service ) &&
2855 IOObjectConformsTo( service, kIODVDMediaClass) )
2859 IOObjectRelease( service );
2860 } while( !result && (service = IOIteratorNext( iter )) );
2861 IOObjectRelease( iter );
2866 /****************************************************************************
2867 * is_whole_media_service
2869 * Returns whether or not the service passed in is an IOMedia service and
2870 * represents the "whole" media instead of just a partition.
2872 * The whole media object is indicated in the IORegistry by the presence of a
2873 * property with the key "Whole" and value "Yes".
2874 ****************************************************************************/
2875 static is_whole_media_service( io_service_t service )
2879 if( IOObjectConformsTo( service, kIOMediaClass ) )
2881 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2882 CFSTR( kIOMediaWholeKey ),
2883 kCFAllocatorDefault,
2889 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2890 CFRelease( wholeMedia );
2895 #endif // __APPLE_CC__