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 #if defined( __MINGW32__ )
18 #if defined( PTW32_STATIC_LIB )
25 #if defined( __APPLE_CC__ )
26 #import <CoreServices/CoreServices.h>
27 #include <IOKit/IOKitLib.h>
28 #include <IOKit/storage/IOMedia.h>
29 #include <IOKit/storage/IODVDMedia.h>
33 #if defined( __APPLE_CC__ )
34 #define EXTRA_VLC_DYLD_PATH "/Applications/VLC.app/Contents/MacOS/lib"
35 #define DEFAULT_DYLD_PATH "/usr/local/lib:/usr/lib"
37 static int no_vlc_dylib = 0;
39 static int debug = HB_DEBUG_ALL;
40 static int update = 0;
41 static int dvdnav = 1;
42 static char * input = NULL;
43 static char * output = NULL;
44 static char * format = NULL;
45 static int titleindex = 1;
46 static int titlescan = 0;
47 static int main_feature = 0;
48 static char * native_language = NULL;
49 static int native_dub = 0;
50 static int twoPass = 0;
51 static int deinterlace = 0;
52 static char * deinterlace_opt = 0;
53 static int deblock = 0;
54 static char * deblock_opt = 0;
55 static int denoise = 0;
56 static char * denoise_opt = 0;
57 static int detelecine = 0;
58 static char * detelecine_opt = 0;
59 static int decomb = 0;
60 static char * decomb_opt = 0;
61 static int rotate = 0;
62 static char * rotate_opt = 0;
63 static int grayscale = 0;
64 static int vcodec = HB_VCODEC_FFMPEG;
65 static hb_list_t * audios = NULL;
66 static hb_audio_config_t * audio = NULL;
67 static int num_audio_tracks = 0;
68 static char * mixdowns = NULL;
69 static char * dynamic_range_compression = NULL;
70 static char * atracks = NULL;
71 static char * arates = NULL;
72 static char * abitrates = NULL;
73 static char * acodecs = NULL;
74 static char * anames = NULL;
76 static int default_acodec = HB_ACODEC_CA_AAC;
78 static int default_acodec = HB_ACODEC_FAAC;
80 static int audio_explicit = 0;
81 static char ** subtracks = NULL;
82 static char ** subforce = NULL;
83 static char * subburn = NULL;
84 static char * subdefault = NULL;
85 static char ** srtfile = NULL;
86 static char ** srtcodeset = NULL;
87 static char ** srtoffset = NULL;
88 static char ** srtlang = NULL;
89 static int srtdefault = -1;
90 static int subtitle_scan = 0;
92 static int height = 0;
93 static int crop[4] = { -1,-1,-1,-1 };
96 static float vquality = -1.0;
97 static int vbitrate = 0;
100 static int anamorphic_mode = 0;
101 static int modulus = 0;
102 static int par_height = 0;
103 static int par_width = 0;
104 static int display_width = 0;
105 static int keep_display_aspect = 0;
106 static int itu_par = 0;
107 static int angle = 0;
108 static int chapter_start = 0;
109 static int chapter_end = 0;
110 static int chapter_markers = 0;
111 static char * marker_file = NULL;
112 static char *x264opts = NULL;
113 static char *x264opts2 = NULL;
114 static int maxHeight = 0;
115 static int maxWidth = 0;
116 static int turbo_opts_enabled = 0;
117 static char * turbo_opts = "ref=1:subme=2:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
118 static int largeFileSize = 0;
119 static int preset = 0;
120 static char * preset_name = 0;
122 static int mp4_optimize = 0;
123 static int ipod_atom = 0;
124 static int color_matrix = 0;
125 static int preview_count = 10;
126 static int store_previews = 0;
127 static int start_at_preview = 0;
128 static int64_t start_at_pts = 0;
129 static int start_at_frame = 0;
130 static char * start_at_string = NULL;
131 static char * start_at_token = NULL;
132 static int64_t stop_at_pts = 0;
133 static int stop_at_frame = 0;
134 static char * stop_at_string = NULL;
135 static char * stop_at_token = NULL;
136 static uint64_t min_title_duration = 900000LL;
138 /* Exit cleanly on Ctrl-C */
139 static volatile int die = 0;
140 static void SigHandler( int );
143 static void ShowCommands();
144 static void ShowHelp();
145 static void ShowPresets();
147 static int ParseOptions( int argc, char ** argv );
148 static int CheckOptions( int argc, char ** argv );
149 static int HandleEvents( hb_handle_t * h );
151 static int get_acodec_for_string( char *codec );
152 static int is_sample_rate_valid(int rate);
155 static char* bsd_name_for_path(char *path);
156 static int device_is_dvd(char *device);
157 static io_service_t get_iokit_service( char *device );
158 static int is_dvd_service( io_service_t service );
159 static int is_whole_media_service( io_service_t service );
162 /* Only print the "Muxing..." message once */
163 static int show_mux_warning = 1;
165 /****************************************************************************
168 * When using the CLI just display using hb_log as we always did in the past
169 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
170 ****************************************************************************/
171 static void hb_cli_error_handler ( const char *errmsg )
173 fprintf( stderr, "ERROR: %s\n", errmsg );
176 int main( int argc, char ** argv )
182 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
183 * force it to unbuffered otherwise informative output is not easily parsed.
185 #if defined( _WIN32 ) || defined( __MINGW32__ )
186 setvbuf( stdout, NULL, _IONBF, 0 );
187 setvbuf( stderr, NULL, _IONBF, 0 );
190 audios = hb_list_init();
192 /* Parse command line */
193 if( ParseOptions( argc, argv ) ||
194 CheckOptions( argc, argv ) )
199 /* Register our error handler */
200 hb_register_error_handler(&hb_cli_error_handler);
203 h = hb_init( debug, update );
204 hb_dvd_set_dvdnav( dvdnav );
207 fprintf( stderr, "%s - %s - %s\n",
208 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
210 /* Check for update */
213 if( ( build = hb_check_update( h, &version ) ) > -1 )
215 fprintf( stderr, "You are using an old version of "
216 "HandBrake.\nLatest is %s (build %d).\n", version,
221 fprintf( stderr, "Your version of HandBrake is up to "
230 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
231 hb_get_cpu_count( h ) > 1 ? "s" : "" );
234 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
235 cpu > 1 ? "s" : "" );
236 hb_set_cpu_count( h, cpu );
239 /* Exit ASAP on Ctrl-C */
240 signal( SIGINT, SigHandler );
242 /* Feed libhb with a DVD to scan */
243 fprintf( stderr, "Opening %s...\n", input );
247 * We need to scan for all the titles in order to find the main feature
252 hb_scan( h, input, titleindex, preview_count, store_previews, min_title_duration );
257 #if defined( __MINGW32__ )
261 case 0x03: /* ctrl-c */
263 fprintf( stdout, "\nEncoding Quit by user command\n" );
267 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
279 #elif !defined(SYS_BEOS)
289 FD_SET( STDIN_FILENO, &fds );
290 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
297 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
299 if( buf[size] == '\n' )
306 if( size >= 256 || buf[size] == '\n' )
311 fprintf( stdout, "\nEncoding Quit by user command\n" );
315 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
338 if( input ) free( input );
339 if( output ) free( output );
340 if( format ) free( format );
343 while( ( audio = hb_list_item( audios, 0 ) ) )
345 hb_list_rem( audios, audio );
346 if( audio->out.name )
348 free( audio->out.name );
352 hb_list_close( &audios );
354 if( mixdowns ) free( mixdowns );
355 if( dynamic_range_compression ) free( dynamic_range_compression );
356 if( atracks ) free( atracks );
357 if( arates ) free( arates );
358 if( abitrates ) free( abitrates );
359 if( acodecs ) free( acodecs );
360 if( anames ) free( anames );
361 if (native_language ) free (native_language );
362 if( x264opts ) free (x264opts );
363 if( x264opts2 ) free (x264opts2 );
364 if (preset_name) free (preset_name);
365 if( stop_at_string ) free( stop_at_string );
366 if( start_at_string ) free( start_at_string );
368 fprintf( stderr, "HandBrake has exited.\n" );
373 static void ShowCommands()
375 fprintf( stdout, "\nCommands:\n" );
376 fprintf( stdout, " [h]elp Show this message\n" );
377 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
378 fprintf( stdout, " [p]ause Pause encoding\n" );
379 fprintf( stdout, " [r]esume Resume encoding\n" );
382 static void PrintTitleInfo( hb_title_t * title )
384 hb_chapter_t * chapter;
385 hb_subtitle_t * subtitle;
388 fprintf( stderr, "+ title %d:\n", title->index );
389 if ( title->index == title->job->feature )
391 fprintf( stderr, " + Main Feature\n" );
393 if ( title->type == HB_STREAM_TYPE )
395 fprintf( stderr, " + stream: %s\n", title->path );
397 else if ( title->type == HB_DVD_TYPE )
399 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%"PRIu64" blocks)\n",
400 title->vts, title->ttn, title->cell_start, title->cell_end,
401 title->block_count );
403 if (title->angle_count > 1)
404 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
405 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
406 title->hours, title->minutes, title->seconds );
407 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
408 title->width, title->height,
409 title->pixel_aspect_width,
410 title->pixel_aspect_height,
411 (float) title->aspect,
412 (float) title->rate / title->rate_base );
413 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
414 title->crop[1], title->crop[2], title->crop[3] );
415 fprintf( stderr, " + chapters:\n" );
416 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
418 chapter = hb_list_item( title->list_chapter, i );
419 fprintf( stderr, " + %d: cells %d->%d, %"PRIu64" blocks, duration "
420 "%02d:%02d:%02d\n", chapter->index,
421 chapter->cell_start, chapter->cell_end,
422 chapter->block_count, chapter->hours, chapter->minutes,
425 fprintf( stderr, " + audio tracks:\n" );
426 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
428 audio = hb_list_audio_config_item( title->list_audio, i );
429 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
431 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
433 audio->lang.description,
434 audio->lang.iso639_2,
435 audio->in.samplerate,
440 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
442 audio->lang.description,
443 audio->lang.iso639_2 );
446 fprintf( stderr, " + subtitle tracks:\n" );
447 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
449 subtitle = hb_list_item( title->list_subtitle, i );
450 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)(%s)\n",
451 i + 1, subtitle->lang,
453 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap",
454 hb_subsource_name(subtitle->source));
457 if(title->detected_interlacing)
459 /* Interlacing was found in half or more of the preview frames */
460 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
465 static int test_sub_list( char ** list, int pos )
469 if ( list == NULL || pos == 0 )
472 if ( list[0] == NULL && pos == 1 )
475 for ( i = 0; list[i] != NULL; i++ )
477 int idx = strtol( list[i], NULL, 0 );
484 static int HandleEvents( hb_handle_t * h )
487 int tmp_num_audio_tracks;
489 hb_get_state( h, &s );
496 #define p s.param.scanning
497 case HB_STATE_SCANNING:
498 /* Show what title is currently being scanned */
499 fprintf( stderr, "Scanning title %d", p.title_cur );
500 if( !titleindex || titlescan )
501 fprintf( stderr, " of %d", p.title_count );
502 fprintf( stderr, "...\n" );
506 case HB_STATE_SCANDONE:
514 /* Audio argument string parsing variables */
518 int mixdown = HB_AMIXDOWN_DOLBYPLII;
520 /* Audio argument string parsing variables */
522 list = hb_get_titles( h );
524 if( !hb_list_count( list ) )
526 /* No valid title, stop right there */
527 fprintf( stderr, "No title found.\n" );
534 int main_feature_idx=0;
535 int main_feature_pos=-1;
536 int main_feature_time=0;
539 fprintf( stderr, "Searching for main feature title...\n" );
541 for( i = 0; i < hb_list_count( list ); i++ )
543 title = hb_list_item( list, i );
544 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
545 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
546 i, title->index, title_time );
547 if( main_feature_time < title_time )
549 main_feature_time = title_time;
550 main_feature_pos = i;
551 main_feature_idx = title->index;
553 if( title->job->feature == title->index )
555 main_feature_time = title_time;
556 main_feature_pos = i;
557 main_feature_idx = title->index;
561 if( main_feature_pos == -1 )
563 fprintf( stderr, "No main feature title found.\n" );
567 titleindex = main_feature_idx;
568 fprintf( stderr, "Found main feature title, setting title to %d\n",
571 title = hb_list_item( list, main_feature_pos);
573 title = hb_list_item( list, 0 );
576 if( !titleindex || titlescan )
578 /* Scan-only mode, print infos and exit */
580 for( i = 0; i < hb_list_count( list ); i++ )
582 title = hb_list_item( list, i );
583 PrintTitleInfo( title );
589 /* Set job settings */
592 PrintTitleInfo( title );
594 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame && !start_at_pts && !start_at_frame )
596 job->chapter_start = MAX( job->chapter_start,
598 job->chapter_end = MIN( job->chapter_end,
600 job->chapter_end = MAX( job->chapter_start,
611 fprintf( stderr, "+ Using preset: %s", preset_name);
613 if (!strcmp(preset_name, "Universal"))
619 vcodec = HB_VCODEC_X264;
620 job->vquality = 20.0;
623 atracks = strdup("1,1");
627 acodecs = strdup("faac,copy:ac3");
631 abitrates = strdup("160,160");
635 mixdowns = strdup("dpl2,auto");
639 arates = strdup("Auto,Auto");
641 if( !dynamic_range_compression )
643 dynamic_range_compression = strdup("0.0,0.0");
648 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6");
650 if( !anamorphic_mode )
654 job->chapter_markers = 1;
658 if (!strcmp(preset_name, "iPod"))
665 vcodec = HB_VCODEC_X264;
669 atracks = strdup("1");
673 acodecs = strdup("faac");
677 abitrates = strdup("160");
681 mixdowns = strdup("dpl2");
685 arates = strdup("Auto");
687 if( !dynamic_range_compression )
689 dynamic_range_compression = strdup("0.0");
694 x264opts = strdup("level=30:bframes=0:weightp=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0");
696 job->chapter_markers = 1;
700 if (!strcmp(preset_name, "iPhone & iPod Touch"))
706 vcodec = HB_VCODEC_X264;
707 job->vquality = 20.0;
710 atracks = strdup("1");
714 acodecs = strdup("faac");
718 abitrates = strdup("128");
722 mixdowns = strdup("dpl2");
726 arates = strdup("Auto");
728 if( !dynamic_range_compression )
730 dynamic_range_compression = strdup("0.0");
735 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0");
737 job->chapter_markers = 1;
741 if (!strcmp(preset_name, "iPhone 4"))
747 job->largeFileSize = 1;
748 vcodec = HB_VCODEC_X264;
749 job->vquality = 20.0;
750 job->vrate_base = 900900;
754 atracks = strdup("1");
758 acodecs = strdup("faac");
762 abitrates = strdup("160");
766 mixdowns = strdup("dpl2");
770 arates = strdup("Auto");
772 if( !dynamic_range_compression )
774 dynamic_range_compression = strdup("0.0");
777 if( !anamorphic_mode )
781 job->chapter_markers = 1;
785 if (!strcmp(preset_name, "iPad"))
791 job->largeFileSize = 1;
792 vcodec = HB_VCODEC_X264;
793 job->vquality = 20.0;
794 job->vrate_base = 900900;
798 atracks = strdup("1");
802 acodecs = strdup("faac");
806 abitrates = strdup("160");
810 mixdowns = strdup("dpl2");
814 arates = strdup("Auto");
816 if( !dynamic_range_compression )
818 dynamic_range_compression = strdup("0.0");
821 if( !anamorphic_mode )
825 job->chapter_markers = 1;
829 if (!strcmp(preset_name, "AppleTV"))
835 job->largeFileSize = 1;
836 vcodec = HB_VCODEC_X264;
837 job->vquality = 20.0;
840 atracks = strdup("1,1");
844 acodecs = strdup("faac,copy:ac3");
848 abitrates = strdup("160,160");
852 mixdowns = strdup("dpl2,auto");
856 arates = strdup("Auto,Auto");
858 if( !dynamic_range_compression )
860 dynamic_range_compression = strdup("0.0,0.0");
865 x264opts = strdup("cabac=0:ref=2:me=umh:b-pyramid=none:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500");
867 if( !anamorphic_mode )
871 job->chapter_markers = 1;
875 if (!strcmp(preset_name, "AppleTV 2"))
881 job->largeFileSize = 1;
882 vcodec = HB_VCODEC_X264;
883 job->vquality = 20.0;
884 job->vrate_base = 900900;
888 atracks = strdup("1,1");
892 acodecs = strdup("faac,copy:ac3");
896 abitrates = strdup("160,160");
900 mixdowns = strdup("dpl2,auto");
904 arates = strdup("Auto,Auto");
906 if( !dynamic_range_compression )
908 dynamic_range_compression = strdup("0.0,0.0");
911 if( !anamorphic_mode )
915 job->chapter_markers = 1;
919 if (!strcmp(preset_name, "Normal"))
925 vcodec = HB_VCODEC_X264;
926 job->vquality = 20.0;
929 atracks = strdup("1");
933 acodecs = strdup("faac");
937 abitrates = strdup("160");
941 mixdowns = strdup("dpl2");
945 arates = strdup("Auto");
947 if( !dynamic_range_compression )
949 dynamic_range_compression = strdup("0.0");
953 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
955 if( !anamorphic_mode )
959 job->chapter_markers = 1;
963 if (!strcmp(preset_name, "High Profile"))
969 vcodec = HB_VCODEC_X264;
970 job->vquality = 20.0;
973 atracks = strdup("1,1");
977 acodecs = strdup("faac,copy:ac3");
981 abitrates = strdup("160,160");
985 mixdowns = strdup("dpl2,auto");
989 arates = strdup("Auto,Auto");
991 if( !dynamic_range_compression )
993 dynamic_range_compression = strdup("0.0,0.0");
997 x264opts = strdup("b-adapt=2:rc-lookahead=50");
1001 if( !anamorphic_mode )
1003 anamorphic_mode = 2;
1005 job->chapter_markers = 1;
1009 if (!strcmp(preset_name, "Classic"))
1015 job->vbitrate = 1000;
1018 atracks = strdup("1");
1022 acodecs = strdup("faac");
1026 abitrates = strdup("160");
1030 mixdowns = strdup("dpl2");
1034 arates = strdup("Auto");
1036 if( !dynamic_range_compression )
1038 dynamic_range_compression = strdup("0.0");
1043 if (!strcmp(preset_name, "AppleTV Legacy"))
1049 job->largeFileSize = 1;
1050 vcodec = HB_VCODEC_X264;
1051 job->vbitrate = 2500;
1054 atracks = strdup("1,1");
1058 acodecs = strdup("faac,copy:ac3");
1062 abitrates = strdup("160,160");
1066 mixdowns = strdup("dpl2,auto");
1070 arates = strdup("Auto,Auto");
1072 if( !dynamic_range_compression )
1074 dynamic_range_compression = strdup("0.0,0.0");
1078 x264opts = strdup("ref=1:b-pyramid=none:weightp=0:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
1080 if( !anamorphic_mode )
1082 anamorphic_mode = 1;
1084 job->chapter_markers = 1;
1088 if (!strcmp(preset_name, "iPhone Legacy"))
1095 vcodec = HB_VCODEC_X264;
1096 job->vbitrate = 960;
1099 atracks = strdup("1");
1103 acodecs = strdup("faac");
1107 abitrates = strdup("128");
1111 mixdowns = strdup("dpl2");
1115 arates = strdup("Auto");
1117 if( !dynamic_range_compression )
1119 dynamic_range_compression = strdup("0.0");
1124 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0");
1126 job->chapter_markers = 1;
1130 if (!strcmp(preset_name, "iPod Legacy"))
1137 vcodec = HB_VCODEC_X264;
1138 job->vbitrate = 1500;
1141 atracks = strdup("1");
1145 acodecs = strdup("faac");
1149 abitrates = strdup("160");
1153 mixdowns = strdup("dpl2");
1157 arates = strdup("Auto");
1159 if( !dynamic_range_compression )
1161 dynamic_range_compression = strdup("0.0");
1166 x264opts = strdup("level=30:bframes=0:weightp=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0");
1168 job->chapter_markers = 1;
1174 if ( chapter_markers )
1176 job->chapter_markers = chapter_markers;
1178 if( marker_file != NULL )
1180 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1181 hb_csv_cell_t * cell;
1185 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1189 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1193 /* Parse the cells */
1194 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1196 /* We have a chapter number */
1197 if( cell->cell_col == 0 )
1199 row = cell->cell_row;
1200 chapter = atoi( cell->cell_text );
1203 /* We have a chapter name */
1204 if( cell->cell_col == 1 && row == cell->cell_row )
1206 /* If we have a valid chapter, copy the string an terminate it */
1207 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1209 hb_chapter_t * chapter_s;
1211 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1212 strncpy(chapter_s->title, cell->cell_text, 1023);
1213 chapter_s->title[1023] = '\0';
1218 hb_dispose_cell( cell );
1221 hb_close_csv_file( file );
1226 /* No marker file */
1228 int number_of_chapters = hb_list_count(job->title->list_chapter);
1231 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1233 hb_chapter_t * chapter_s;
1234 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1235 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1236 chapter_s->title[1023] = '\0';
1241 if( crop[0] >= 0 && crop[1] >= 0 &&
1242 crop[2] >= 0 && crop[3] >= 0 )
1244 memcpy( job->crop, crop, 4 * sizeof( int ) );
1247 job->deinterlace = deinterlace;
1248 job->grayscale = grayscale;
1250 /* Add selected filters */
1251 job->filters = hb_list_init();
1255 hb_filter_rotate.settings = rotate_opt;
1256 hb_list_add( job->filters, &hb_filter_rotate);
1260 hb_filter_detelecine.settings = detelecine_opt;
1261 hb_list_add( job->filters, &hb_filter_detelecine );
1265 hb_filter_decomb.settings = decomb_opt;
1266 hb_list_add( job->filters, &hb_filter_decomb );
1270 hb_filter_deinterlace.settings = deinterlace_opt;
1271 hb_list_add( job->filters, &hb_filter_deinterlace );
1275 hb_filter_deblock.settings = deblock_opt;
1276 hb_list_add( job->filters, &hb_filter_deblock );
1280 hb_filter_denoise.settings = denoise_opt;
1281 hb_list_add( job->filters, &hb_filter_denoise );
1284 switch( anamorphic_mode )
1286 case 0: // Non-anamorphic
1290 job->modulus = modulus;
1293 if( width && height )
1296 job->height = height;
1301 // do not exceed source dimensions by default
1303 job->maxHeight = title->height;
1304 hb_fix_aspect( job, HB_KEEP_WIDTH );
1308 job->height = height;
1309 // do not exceed source dimensions by default
1311 job->maxWidth = title->width;
1312 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1314 else if( !width && !height )
1316 /* Default to cropped width when one isn't specified
1317 * avoids rounding to mod 16 regardless of modulus */
1318 job->width = title->width - job->crop[2] - job->crop[3];
1319 // do not exceed source dimensions by default
1321 job->maxHeight = title->height;
1322 hb_fix_aspect( job, HB_KEEP_WIDTH );
1327 case 1: // Strict anammorphic
1328 job->anamorphic.mode = anamorphic_mode;
1331 case 2: // Loose anamorphic
1332 job->anamorphic.mode = 2;
1336 job->modulus = modulus;
1341 job->anamorphic.itu_par = itu_par;
1348 else if( !width && !height )
1350 /* Default to full width when one isn't specified for loose anamorphic */
1351 job->width = title->width - job->crop[2] - job->crop[3];
1356 case 3: // Custom Anamorphic 3: Power User Jamboree
1357 job->anamorphic.mode = 3;
1361 job->modulus = modulus;
1366 job->anamorphic.itu_par = itu_par;
1369 if( par_width && par_height )
1371 job->anamorphic.par_width = par_width;
1372 job->anamorphic.par_height = par_height;
1375 if( keep_display_aspect )
1377 job->anamorphic.keep_display_aspect = 1;
1379 /* First, what *is* the display aspect? */
1380 int cropped_width = title->width - job->crop[2] - job->crop[3];
1381 int cropped_height = title->height - job->crop[0] - job->crop[1];
1383 /* XXX -- I'm assuming people want to keep the source
1384 display AR even though they might have already
1385 asked for ITU values instead. */
1386 float source_display_width = (float)cropped_width *
1387 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1388 float display_aspect = source_display_width / (float)cropped_height;
1389 /* When keeping display aspect, we have to rank some values
1390 by priority in order to consistently handle situations
1391 when more than one might be specified by default.
1393 * First off, PAR gets ignored. (err make this reality)
1394 * Height will be respected over all other settings,
1395 * If it isn't set, display_width will be followed.
1396 * If it isn't set, width will be followed. */
1399 /* We scale the display width to the new height */
1400 display_width = (int)( (double)height * display_aspect );
1402 else if( display_width )
1404 /* We scale the height to the new display width */
1405 height = (int)( (double)display_width / display_aspect );
1411 /* Adjust the PAR to create the new display width
1412 from the default job width. */
1413 job->anamorphic.dar_width = display_width;
1415 job->anamorphic.dar_height = height ?
1417 title->height - job->crop[0] - job->crop[1];
1420 if( width && height )
1422 /* Use these storage dimensions */
1424 job->height = height;
1428 /* Use just this storage width */
1430 job->height = title->height - job->crop[0] - job->crop[1];
1434 /* Use just this storage height. */
1435 job->height = height;
1436 job->width = title->width - job->crop[2] - job->crop[3];
1438 else if( !width && !height )
1440 /* Assume source dimensions after cropping. */
1441 job->width = title->width - job->crop[2] - job->crop[3];
1442 job->height = title->height - job->crop[0] - job->crop[1];
1448 if( vquality >= 0.0 )
1450 job->vquality = vquality;
1455 job->vquality = -1.0;
1456 job->vbitrate = vbitrate;
1460 job->vcodec = vcodec;
1465 job->vrate = 27000000;
1466 job->vrate_base = vrate;
1470 // cfr or pfr flag with no rate specified implies
1471 // use the title rate.
1473 job->vrate = title->rate;
1474 job->vrate_base = title->rate_base;
1477 /* Grab audio tracks */
1480 char * token = strtok(atracks, ",");
1483 int track_start, track_end;
1484 while( token != NULL )
1486 audio = calloc(1, sizeof(*audio));
1487 hb_audio_config_init(audio);
1488 if (strlen(token) >= 3)
1490 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1493 for (i = track_start - 1; i < track_end; i++)
1495 if (i != track_start - 1)
1497 audio = calloc(1, sizeof(*audio));
1498 hb_audio_config_init(audio);
1500 audio->in.track = i;
1501 audio->out.track = num_audio_tracks++;
1502 hb_list_add(audios, audio);
1505 else if( !strcasecmp(token, "none" ) )
1507 audio->in.track = audio->out.track = -1;
1508 audio->out.codec = 0;
1509 hb_list_add(audios, audio);
1514 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1521 audio->in.track = atoi(token) - 1;
1522 audio->out.track = num_audio_tracks++;
1523 hb_list_add(audios, audio);
1525 token = strtok(NULL, ",");
1529 /* Parse audio tracks */
1530 if( native_language && native_dub )
1532 if( hb_list_count( audios ) == 0 || !audio_explicit )
1534 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1539 audio = hb_list_audio_config_item( title->list_audio, i );
1541 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1543 if( strncasecmp( native_language, audio_lang,
1544 sizeof( audio_lang ) ) == 0 &&
1545 audio->lang.type != 3 && // Directors 1
1546 audio->lang.type != 4) // Directors 2
1549 * Matched an audio to our native language - use it.
1550 * Replace any existing audio tracks that a preset may
1553 if( hb_list_count(audios) == 0) {
1554 audio = calloc(1, sizeof(*audio));
1555 hb_audio_config_init(audio);
1556 audio->in.track = track;
1557 audio->out.track = num_audio_tracks++;
1558 /* Add it to our audios */
1559 hb_list_add(audios, audio);
1562 * Update the track numbers on what is already in
1565 for( i=0; i < hb_list_count( audios ); i++ )
1567 audio = hb_list_item( audios, i );
1569 audio->in.track = track;
1576 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1580 if( hb_list_count(audios) == 0 &&
1581 hb_list_count(job->title->list_audio) > 0 )
1583 /* Create a new audio track with default settings */
1584 audio = calloc(1, sizeof(*audio));
1585 hb_audio_config_init(audio);
1586 /* Add it to our audios */
1587 hb_list_add(audios, audio);
1590 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1591 for (i = 0; i < tmp_num_audio_tracks; i++)
1593 audio = hb_list_item(audios, 0);
1594 if( (audio == NULL) || (audio->in.track == -1) ||
1595 (audio->out.track == -1) || (audio->out.codec == 0) )
1601 if( hb_audio_add( job, audio ) == 0 )
1603 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1604 audio->in.track + 1 );
1609 hb_list_rem(audios, audio);
1611 if( audio->out.name )
1613 free( audio->out.name);
1622 char * token = strtok(acodecs, ",");
1625 while ( token != NULL )
1627 if ((acodec = get_acodec_for_string(token)) == -1)
1629 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1630 acodec = default_acodec;
1632 if( i < num_audio_tracks )
1634 audio = hb_list_audio_config_item(job->list_audio, i);
1635 audio->out.codec = acodec;
1639 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1640 hb_audio_config_t audio;
1644 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1646 hb_audio_config_init(&audio);
1647 audio.in.track = last_audio->in.track;
1648 audio.out.track = num_audio_tracks++;
1649 audio.out.codec = acodec;
1650 hb_audio_add(job, &audio);
1654 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1657 token = strtok(NULL, ",");
1661 if( i < num_audio_tracks )
1663 /* We have fewer inputs than audio tracks, use the default codec for
1664 * this container for the remaining tracks. Unless we only have one input
1665 * then use that codec instead.
1668 acodec = default_acodec;
1669 for ( ; i < num_audio_tracks; i++)
1671 audio = hb_list_audio_config_item(job->list_audio, i);
1672 audio->out.codec = acodec;
1681 char * token = strtok(arates, ",");
1684 while ( token != NULL )
1686 arate = atoi(token);
1687 audio = hb_list_audio_config_item(job->list_audio, i);
1690 for( j = 0; j < hb_audio_rates_count; j++ )
1692 if( !strcmp( token, hb_audio_rates[j].string ) )
1694 arate = hb_audio_rates[j].rate;
1701 if ( !strcasecmp( token, "auto" ) )
1703 arate = audio->in.samplerate;
1705 if (!is_sample_rate_valid(arate))
1707 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1708 arate = audio->in.samplerate;
1711 audio->out.samplerate = arate;
1712 if( (++i) >= num_audio_tracks )
1713 break; /* We have more inputs than audio tracks, oops */
1717 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1719 token = strtok(NULL, ",");
1722 if (i < num_audio_tracks)
1724 /* We have fewer inputs than audio tracks, use default sample rate.
1725 * Unless we only have one input, then use that for all tracks.
1728 arate = audio->in.samplerate;
1729 for ( ; i < num_audio_tracks; i++)
1731 audio = hb_list_audio_config_item(job->list_audio, i);
1732 audio->out.samplerate = arate;
1741 char * token = strtok(mixdowns, ",");
1744 while ( token != NULL )
1746 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1747 audio = hb_list_audio_config_item(job->list_audio, i);
1750 audio->out.mixdown = mixdown;
1751 if( (++i) >= num_audio_tracks )
1752 break; /* We have more inputs than audio tracks, oops */
1756 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1758 token = strtok(NULL, ",");
1761 if (i < num_audio_tracks)
1763 /* We have fewer inputs than audio tracks, use the default mixdown for the rest. Unless
1764 * we only have one input, then use that.
1766 int use_default = 0;
1770 for (; i < num_audio_tracks; i++)
1772 audio = hb_list_audio_config_item(job->list_audio, i);
1775 // Get default for this tracks codec and layout
1776 mixdown = hb_get_default_mixdown( audio->out.codec, audio->in.channel_layout );
1778 audio->out.mixdown = mixdown;
1787 char * token = strtok(abitrates, ",");
1790 while ( token != NULL )
1792 abitrate = atoi(token);
1793 audio = hb_list_audio_config_item(job->list_audio, i);
1797 audio->out.bitrate = abitrate;
1798 if( (++i) >= num_audio_tracks )
1799 break; /* We have more inputs than audio tracks, oops */
1803 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1805 token = strtok(NULL, ",");
1808 if (i < num_audio_tracks)
1810 /* We have fewer inputs than audio tracks, use the default bitrate
1811 * for the remaining tracks. Unless we only have one input, then use
1812 * that for all tracks.
1814 int use_default = 0;
1818 for (; i < num_audio_tracks; i++)
1820 audio = hb_list_audio_config_item(job->list_audio, i);
1823 abitrate = hb_get_default_audio_bitrate(
1824 audio->out.codec, audio->out.samplerate,
1825 audio->out.mixdown );
1827 audio->out.bitrate = abitrate;
1834 if ( dynamic_range_compression )
1836 char * token = strtok(dynamic_range_compression, ",");
1838 token = dynamic_range_compression;
1839 while ( token != NULL )
1841 d_r_c = atof(token);
1842 audio = hb_list_audio_config_item(job->list_audio, i);
1845 audio->out.dynamic_range_compression = d_r_c;
1846 if( (++i) >= num_audio_tracks )
1847 break; /* We have more inputs than audio tracks, oops */
1851 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1853 token = strtok(NULL, ",");
1856 if (i < num_audio_tracks)
1858 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1859 * tracks. Unless we only have one input, then use the same DRC for all
1864 for (; i < num_audio_tracks; i++)
1866 audio = hb_list_audio_config_item(job->list_audio, i);
1867 audio->out.dynamic_range_compression = d_r_c;
1872 /* Audio Track Names */
1876 char * token = strtok(anames, ",");
1879 while ( token != NULL )
1881 audio = hb_list_audio_config_item(job->list_audio, i);
1884 audio->out.name = strdup(token);
1885 if( (++i) >= num_audio_tracks )
1886 break; /* We have more names than audio tracks, oops */
1890 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1893 token = strtok(NULL, ",");
1896 if( i < num_audio_tracks && i == 1 )
1898 /* We have exactly one name and more than one audio track. Use the same
1899 * name for all tracks. */
1900 for ( ; i < num_audio_tracks; i++)
1902 audio = hb_list_audio_config_item(job->list_audio, i);
1903 audio->out.name = strdup(anames);
1906 /* Audio Track Names */
1908 /* Fix up passthru that needs to fallback to ac3 encode */
1909 for( i = 0; i < hb_list_count( job->list_audio ); )
1911 audio = hb_list_audio_config_item( job->list_audio, i );
1912 if ( ( audio->out.codec & HB_ACODEC_AC3 ) &&
1913 ( audio->out.codec & HB_ACODEC_PASS_FLAG ) &&
1914 !( audio->out.codec & audio->in.codec ) )
1916 audio->out.codec = HB_ACODEC_AC3;
1917 audio->out.mixdown = hb_get_default_mixdown( audio->out.codec, audio->in.channel_layout );
1918 audio->out.bitrate = hb_get_default_audio_bitrate(
1919 audio->out.codec, audio->out.samplerate,
1920 audio->out.mixdown );
1922 // fix 'copy' to select a specific codec
1923 if ( audio->out.codec & HB_ACODEC_PASS_FLAG )
1925 audio->out.codec &= (audio->in.codec | HB_ACODEC_PASS_FLAG);
1926 if ( !( audio->out.codec & HB_ACODEC_MASK ) )
1928 // Passthru not possible, drop audio.
1929 fprintf( stderr, "Passthru requested and input codec is not the same as output codec for track %d\n",
1931 hb_audio_t * item = hb_list_item( job->list_audio, i );
1932 hb_list_rem( job->list_audio, item );
1941 job->vbitrate = hb_calc_bitrate( job, size );
1942 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1950 int burnpos = 0, defaultpos = 0;
1953 burnpos = strtol( subburn, NULL, 0 );
1955 defaultpos = strtol( subdefault, NULL, 0 );
1956 for ( i = 0; subtracks[i] != NULL; i++ )
1958 token = subtracks[i];
1959 if( strcasecmp(token, "scan" ) == 0 )
1961 int burn = 0, force = 0, def = 0;
1963 if ( subburn != NULL )
1965 burn = ( i == 0 && subburn[0] == 0 ) ||
1968 if ( subdefault != NULL )
1970 def = ( i == 0 && subdefault[0] == 0 ) ||
1971 ( defaultpos == i+1 );
1973 force = test_sub_list( subforce, i+1 );
1977 job->select_subtitle_config.dest = PASSTHRUSUB;
1987 job->select_subtitle_config.force = force;
1988 job->select_subtitle_config.default_track = def;
1993 hb_subtitle_t * subtitle;
1994 hb_subtitle_config_t sub_config;
1996 int burn = 0, force = 0, def = 0;
1998 track = atoi(token) - 1;
1999 subtitle = hb_list_item(title->list_subtitle, track);
2000 if( subtitle == NULL )
2002 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
2005 sub_config = subtitle->config;
2007 if ( subburn != NULL )
2009 burn = ( i == 0 && subburn[0] == 0 ) ||
2012 if ( subdefault != NULL )
2014 def = ( i == 0 && subdefault[0] == 0 ) ||
2015 ( defaultpos == i+1 );
2018 force = test_sub_list(subforce, i+1);
2021 ( subtitle->source == VOBSUB ) ||
2022 ( subtitle->source == SSASUB );
2024 if ( burn && supports_burn )
2026 // Only allow one subtitle to be burned into video
2029 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
2035 sub_config.dest = RENDERSUB;
2039 sub_config.dest = PASSTHRUSUB;
2041 sub_config.force = force;
2042 sub_config.default_track = def;
2043 hb_subtitle_add( job, &sub_config, track );
2052 hb_subtitle_config_t sub_config;
2054 for( i=0; srtfile[i] != NULL; i++ )
2056 char *codeset = "L1";
2061 if( srtcodeset && srtcodeset[i] )
2063 codeset = srtcodeset[i];
2065 if( srtoffset && srtoffset[i] )
2067 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
2069 if ( srtlang && srtlang[i] )
2073 sub_config.default_track =
2074 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
2075 sub_config.force = 0;
2076 strncpy( sub_config.src_filename, srtfile[i], 255);
2077 sub_config.src_filename[255] = 0;
2078 strncpy( sub_config.src_codeset, codeset, 39);
2079 sub_config.src_codeset[39] = 0;
2080 sub_config.offset = offset;
2082 hb_srt_add( job, &sub_config, lang);
2086 if( native_language )
2090 audio = hb_list_audio_config_item(job->list_audio, 0);
2094 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
2096 if( strncasecmp( native_language, audio_lang,
2097 sizeof( audio_lang ) ) != 0 )
2100 * Audio language is not the same as our native language.
2101 * If we have any subtitles in our native language they
2102 * should be selected here if they haven't already been.
2104 hb_subtitle_t *subtitle, *subtitle2 = NULL;
2105 int matched_track = 0;
2107 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
2109 subtitle = hb_list_item( title->list_subtitle, i );
2111 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
2114 * Found the first matching subtitle in our
2115 * native language. Is it already selected?
2117 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
2119 subtitle2 = hb_list_item( job->list_subtitle, i );
2121 if( subtitle2->track == subtitle->track) {
2130 if( subtitle2 == NULL )
2133 * Not already selected, so select it.
2135 hb_subtitle_config_t sub_config;
2139 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
2141 sub_config = subtitle->config;
2143 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
2145 sub_config.dest = PASSTHRUSUB;
2148 sub_config.force = 0;
2149 sub_config.default_track = 1;
2150 hb_subtitle_add( job, &sub_config, matched_track);
2167 if ( largeFileSize )
2169 job->largeFileSize = 1;
2173 job->mp4_optimize = 1;
2180 job->file = strdup( output );
2184 job->color_matrix = color_matrix;
2187 if( x264opts != NULL && *x264opts != '\0' )
2189 job->x264opts = x264opts;
2191 else /*avoids a bus error crash when options aren't specified*/
2193 job->x264opts = NULL;
2196 job->maxWidth = maxWidth;
2198 job->maxHeight = maxHeight;
2200 if( start_at_preview )
2202 job->start_at_preview = start_at_preview - 1;
2203 job->seek_points = preview_count;
2208 job->pts_to_stop = stop_at_pts;
2214 job->frame_to_stop = stop_at_frame;
2220 job->pts_to_start = start_at_pts;
2224 if( start_at_frame )
2226 job->frame_to_start = start_at_frame;
2235 * When subtitle scan is enabled do a fast pre-scan job
2236 * which will determine which subtitles to enable, if any.
2240 x264opts_tmp = job->x264opts;
2242 job->x264opts = NULL;
2244 job->indepth_scan = subtitle_scan;
2245 fprintf( stderr, "Subtitle Scan Enabled - enabling "
2246 "subtitles if found for foreign language segments\n");
2249 * Add the pre-scan job
2253 job->x264opts = x264opts_tmp;
2259 * If subtitle_scan is enabled then only turn it on
2260 * for the first pass and then off again for the
2265 job->indepth_scan = 0;
2269 x264opts2 = strdup(x264opts);
2273 * If turbo options have been selected then append them
2274 * to the x264opts now (size includes one ':' and the '\0')
2276 if( turbo_opts_enabled )
2278 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2281 tmp_x264opts = malloc(size * sizeof(char));
2284 snprintf( tmp_x264opts, size, "%s:%s",
2285 x264opts, turbo_opts );
2289 * No x264opts to modify, but apply the turbo options
2290 * anyway as they may be modifying defaults
2292 snprintf( tmp_x264opts, size, "%s",
2295 x264opts = tmp_x264opts;
2297 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2300 job->x264opts = x264opts;
2306 * On the second pass we turn off subtitle scan so that we
2307 * can actually encode using any subtitles that were auto
2308 * selected in the first pass (using the whacky select-subtitle
2309 * attribute of the job).
2311 job->indepth_scan = 0;
2313 job->x264opts = x264opts2;
2320 * Turn on subtitle scan if requested, note that this option
2321 * precludes encoding of any actual subtitles.
2324 job->indepth_scan = 0;
2332 #define p s.param.working
2333 case HB_STATE_SEARCHING:
2334 fprintf( stdout, "\rEncoding: task %d of %d, Searching for start time, %.2f %%",
2335 p.job_cur, p.job_count, 100.0 * p.progress );
2336 if( p.seconds > -1 )
2338 fprintf( stdout, " (ETA %02dh%02dm%02ds)",
2339 p.hours, p.minutes, p.seconds );
2344 case HB_STATE_WORKING:
2345 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2346 p.job_cur, p.job_count, 100.0 * p.progress );
2347 if( p.seconds > -1 )
2349 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2350 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2351 p.hours, p.minutes, p.seconds );
2357 #define p s.param.muxing
2358 case HB_STATE_MUXING:
2360 if (show_mux_warning)
2362 fprintf( stdout, "\rMuxing: this may take awhile..." );
2364 show_mux_warning = 0;
2370 #define p s.param.workdone
2371 case HB_STATE_WORKDONE:
2372 /* Print error if any, then exit */
2376 fprintf( stderr, "\nRip done!\n" );
2378 case HB_ERROR_CANCELED:
2379 fprintf( stderr, "\nRip canceled.\n" );
2382 fprintf( stderr, "\nRip failed (error %x).\n",
2392 /****************************************************************************
2394 ****************************************************************************/
2395 static volatile int64_t i_die_date = 0;
2396 void SigHandler( int i_signal )
2401 i_die_date = hb_get_date();
2402 fprintf( stderr, "Signal %d received, terminating - do it "
2403 "again in case it gets stuck\n", i_signal );
2405 else if( i_die_date + 500 < hb_get_date() )
2407 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2412 /****************************************************************************
2414 ****************************************************************************/
2415 static void ShowHelp()
2418 FILE* const out = stdout;
2421 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2423 "### General Handbrake Options------------------------------------------------\n\n"
2424 " -h, --help Print help\n"
2425 " -u, --update Check for updates and exit\n"
2426 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2427 " -C, --cpu Set CPU count (default: autodetected)\n"
2428 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2429 " if the preset name has spaces, surround it with\n"
2430 " double quotation marks\n"
2431 " -z, --preset-list See a list of available built-in presets\n"
2432 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2433 " (experimental, enabled by default for testing)\n"
2436 "### Source Options-----------------------------------------------------------\n\n"
2437 " -i, --input <string> Set input device\n"
2438 " -t, --title <number> Select a title to encode (0 to scan all titles only,\n"
2440 " --scan Scan selected title only.\n"
2441 " --main-feature Detect and select the main feature title.\n"
2442 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2443 " 1 to 3, or \"3\" for chapter 3 only,\n"
2444 " default: all chapters)\n"
2445 " --angle <number> Select the DVD angle\n"
2446 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2447 " and whether or not they're stored to disk (0 or 1).\n"
2448 " (default: 10:0)\n"
2449 " --start-at-preview <#> Start encoding at a given preview.\n"
2450 " --start-at <unit:#> Start encoding at a given frame, duration (in seconds),\n"
2451 " or pts (on a 90kHz clock)\n"
2452 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2453 " or pts (on a 90kHz clock)"
2456 "### Destination Options------------------------------------------------------\n\n"
2457 " -o, --output <string> Set output file name\n"
2458 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2459 " autodetected from file name)\n"
2460 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2461 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2462 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2463 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2464 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2468 "### Video Options------------------------------------------------------------\n\n"
2469 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2470 " (default: ffmpeg)\n"
2471 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2472 " same style as mencoder:\n"
2473 " option1=value1:option2=value2\n"
2474 " -q, --quality <number> Set video quality\n"
2475 " -S, --size <MB> Set target size\n"
2476 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2477 " -2, --two-pass Use two-pass mode\n"
2478 " -T, --turbo When using 2-pass use the turbo options\n"
2479 " on the first pass to improve speed\n"
2480 " (only works with x264, affects PSNR by about 0.05dB,\n"
2481 " and increases first pass speed two to four times)\n"
2482 " -r, --rate Set video framerate (" );
2483 for( i = 0; i < hb_video_rates_count; i++ )
2485 fprintf( out, hb_video_rates[i].string );
2486 if( i != hb_video_rates_count - 1 )
2487 fprintf( out, "/" );
2490 " Be aware that not specifying a framerate lets\n"
2491 " HandBrake preserve a source's time stamps,\n"
2492 " potentially creating variable framerate video\n"
2493 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2494 " frame rate control. VFR preserves the source\n"
2495 " timing. CFR makes the output constant rate at\n"
2496 " the rate given by the -r flag (or the source's\n"
2497 " average rate if no -r is given). PFR doesn't\n"
2498 " allow the rate to go over the rate specified\n"
2499 " with the -r flag but won't change the source\n"
2500 " timing if it's below that rate.\n"
2501 " If none of these flags are given, the default\n"
2502 " is --cfr when -r is given and --vfr otherwise\n"
2505 "### Audio Options-----------------------------------------------------------\n\n"
2506 " -a, --audio <string> Select audio track(s), separated by commas\n"
2507 " More than one output track can be used for one\n"
2509 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2510 " tracks, default: first one)\n" );
2514 " -E, --aencoder <string> Audio encoder(s)\n"
2515 " (ca_aac/faac/lame/vorbis/ac3/copy/copy:ac3/copy:dts)\n"
2516 " copy, copy:ac3 and copy:dts meaning passthrough.\n"
2517 " copy will passthrough either ac3 or dts.\n"
2518 " Separated by commas for more than one audio track.\n"
2519 " (default: ca_aac)\n" );
2522 " -E, --aencoder <string> Audio encoder(s):\n"
2523 " (faac/lame/vorbis/ac3/copy/copy:ac3/copy:dts)\n"
2524 " copy, copy:ac3 and copy:dts meaning passthrough.\n"
2525 " copy will passthrough either ac3 or dts.\n"
2526 " Separated by commas for more than one audio track.\n"
2527 " (default: faac for mp4, lame for mkv)\n" );
2530 " -B, --ab <kb/s> Set audio bitrate(s) (default: depends on the\n"
2531 " selected codec, mixdown and samplerate)\n"
2532 " Separated by commas for more than one audio track.\n"
2533 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2534 " Separated by commas for more than one audio track.\n"
2535 " (mono/stereo/dpl1/dpl2/6ch, default: up to 6ch for ac3,\n"
2536 " up to dpl2 for other encoders)\n"
2537 " -R, --arate Set audio samplerate(s) (" );
2538 for( i = 0; i < hb_audio_rates_count; i++ )
2540 fprintf( out, hb_audio_rates[i].string );
2541 if( i != hb_audio_rates_count - 1 )
2542 fprintf( out, "/" );
2544 fprintf( out, " kHz)\n"
2545 " Separated by commas for more than one audio track.\n"
2546 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2547 " making soft sounds louder. Range is 1.0 to 4.0\n"
2548 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2549 " Separated by commas for more than one audio track.\n"
2550 " -A, --aname <string> Audio track name(s),\n"
2551 " Separated by commas for more than one audio track.\n"
2554 "### Picture Settings---------------------------------------------------------\n\n"
2555 " -w, --width <number> Set picture width\n"
2556 " -l, --height <number> Set picture height\n"
2557 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2558 " -Y, --maxHeight <#> Set maximum height\n"
2559 " -X, --maxWidth <#> Set maximum width\n"
2560 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2561 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2562 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2563 " directly control all parameters.\n"
2564 " --display-width Set the width to scale the actual pixels to\n"
2565 " <number> at playback, for custom anamorphic.\n"
2566 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2567 " when using custom anamorphic\n"
2568 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2570 " (--display-width and --pixel-aspect are mutually\n"
2571 " exclusive and the former will override the latter)\n"
2572 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2573 " custom anamorphic, useful with underscanned sources\n"
2574 " --modulus Set the number you want the scaled pixel dimensions\n"
2575 " <number> to divide cleanly by. Does not affect strict\n"
2576 " anamorphic mode, which is always mod 2 (default: 16)\n"
2577 " -M --color-matrix Set the color space signaled by the output\n"
2578 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2579 " default: set by resolution)\n"
2582 "### Filters---------------------------------------------------------\n\n"
2584 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2585 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2587 " <fast/slow/slower>\n"
2588 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2589 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2590 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2591 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2592 " Note: this filter drops duplicate frames to\n"
2593 " restore the pre-telecine framerate, unless you\n"
2594 " specify a constant framerate (--rate 29.97)\n"
2595 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2596 " -8, --denoise Denoise video with hqdn3d filter\n"
2597 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2599 " <weak/medium/strong>\n"
2600 " -7, --deblock Deblock video with pp7 filter\n"
2601 " <QP:M> (default 5:2)\n"
2602 " --rotate Flips images axes\n"
2603 " <M> (default 3)\n"
2604 " -g, --grayscale Grayscale encoding\n"
2607 "### Subtitle Options------------------------------------------------------------\n\n"
2608 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2609 " More than one output track can be used for one\n"
2611 " Example: \"1,2,3\" for multiple tracks.\n"
2612 " A special track name \"scan\" adds an extra 1st pass.\n"
2613 " This extra pass scans subtitles matching the\n"
2614 " language of the first audio or the language \n"
2615 " selected by --native-language.\n"
2616 " The one that's only used 10 percent of the time\n"
2617 " or less is selected. This should locate subtitles\n"
2618 " for short foreign language segments. Best used in\n"
2619 " conjunction with --subtitle-forced.\n"
2620 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2621 " <string> the subtitle has the forced flag set. The values in\n"
2622 " \"string\" are indexes into the subtitle list\n"
2623 " specified with '--subtitle'.\n"
2624 " Separated by commas for more than one audio track.\n"
2625 " Example: \"1,2,3\" for multiple tracks.\n"
2626 " If \"string\" is omitted, the first track is forced.\n"
2627 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2628 " <number> If \"number\" is omitted, the first track is burned.\n"
2629 " \"number\" is an index into the subtitle list\n"
2630 " specified with '--subtitle'.\n"
2631 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2632 " <number> to be displayed upon playback. Setting no default\n"
2633 " means no subtitle will be automatically displayed\n"
2634 " If \"number\" is omitted, the first track is default.\n"
2635 " \"number\" is an index into the subtitle list\n"
2636 " specified with '--subtitle'.\n"
2637 " -N, --native-language Specifiy the your language preference. When the first\n"
2638 " <string> audio track does not match your native language then\n"
2639 " select the first subtitle that does. When used in\n"
2640 " conjunction with --native-dub the audio track is\n"
2641 " changed in preference to subtitles. Provide the\n"
2642 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2643 " --native-dub Used in conjunction with --native-language\n"
2644 " requests that if no audio tracks are selected the\n"
2645 " default selected audio track will be the first one\n"
2646 " that matches the --native-language. If there are no\n"
2647 " matching audio tracks then the first matching\n"
2648 " subtitle track is used instead.\n"
2649 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2650 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2651 " <string> encoded in, separted by commas.\n"
2652 " Use 'iconv -l' for a list of valid\n"
2653 " codesets. If not specified latin1 is assumed\n"
2654 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2655 " <string> separted by commas. If not specified zero is assumed.\n"
2656 " Offsets may be negative.\n"
2657 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2658 " for the SRT file(s) separated by commas. If not specified\n"
2659 " then 'und' is used.\n"
2660 " --srt-default Flag the selected srt as the default subtitle\n"
2661 " <number> to be displayed upon playback. Setting no default\n"
2662 " means no subtitle will be automatically displayed\n"
2663 " If \"number\" is omitted, the first srt is default.\n"
2664 " \"number\" is an 1 based index into the srt-file list\n"
2671 /****************************************************************************
2673 ****************************************************************************/
2674 static void ShowPresets()
2676 fprintf( stderr, "%s - %s - %s\n", HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
2678 printf("\n< Apple\n");
2680 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6\n");
2682 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 -I -X 320 -m -x level=30:bframes=0:weightp=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0\n");
2684 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R Auto -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0\n");
2686 printf("\n + iPhone 4: -e x264 -q 20.0 -r 29.97 --pfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 -4 -X 960 --loose-anamorphic -m\n");
2688 printf("\n + iPad: -e x264 -q 20.0 -r 29.97 --pfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 -4 -X 1024 --loose-anamorphic -m\n");
2690 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-pyramid=none:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500\n");
2692 printf("\n + AppleTV 2: -e x264 -q 20.0 -r 29.97 --pfr -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 -X 1280 --loose-anamorphic -m\n");
2696 printf("\n< Regular\n");
2698 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 --strict-anamorphic -m -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0\n");
2700 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50\n");
2704 printf("\n< Legacy\n");
2706 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4\n");
2708 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x ref=1:b-pyramid=none:weightp=0:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0\n");
2710 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R Auto -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:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0\n");
2712 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 -I -X 640 -m -x level=30:bframes=0:weightp=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0\n");
2718 static char * hb_strndup( char * str, int len )
2721 int str_len = strlen( str );
2723 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2724 strncpy( res, str, len );
2729 static char** str_split( char *str, char delem )
2736 if ( str == NULL || str[0] == 0 )
2738 ret = malloc( sizeof(char*) );
2743 // Find number of elements in the string
2746 while ( ( pos = strchr( pos, delem ) ) != NULL )
2752 ret = calloc( ( count + 1 ), sizeof(char*) );
2755 for ( i = 0; i < count - 1; i++ )
2757 end = strchr( pos, delem );
2758 ret[i] = hb_strndup(pos, end - pos);
2761 ret[i] = strdup(pos);
2766 /****************************************************************************
2768 ****************************************************************************/
2769 static int ParseOptions( int argc, char ** argv )
2772 #define PREVIEWS 257
2773 #define START_AT_PREVIEW 258
2774 #define START_AT 259
2778 #define DISPLAY_WIDTH 263
2779 #define PIXEL_ASPECT 264
2781 #define KEEP_DISPLAY_ASPECT 266
2782 #define SUB_BURNED 267
2783 #define SUB_DEFAULT 268
2784 #define NATIVE_DUB 269
2785 #define SRT_FILE 270
2786 #define SRT_CODESET 271
2787 #define SRT_OFFSET 272
2788 #define SRT_LANG 273
2789 #define SRT_DEFAULT 274
2790 #define ROTATE_FILTER 275
2791 #define SCAN_ONLY 276
2792 #define MAIN_FEATURE 277
2793 #define MIN_DURATION 278
2797 static struct option long_options[] =
2799 { "help", no_argument, NULL, 'h' },
2800 { "update", no_argument, NULL, 'u' },
2801 { "verbose", optional_argument, NULL, 'v' },
2802 { "cpu", required_argument, NULL, 'C' },
2803 { "no-dvdnav", no_argument, NULL, DVDNAV },
2805 { "format", required_argument, NULL, 'f' },
2806 { "input", required_argument, NULL, 'i' },
2807 { "output", required_argument, NULL, 'o' },
2808 { "large-file", no_argument, NULL, '4' },
2809 { "optimize", no_argument, NULL, 'O' },
2810 { "ipod-atom", no_argument, NULL, 'I' },
2812 { "title", required_argument, NULL, 't' },
2813 { "min-duration",required_argument, NULL, MIN_DURATION },
2814 { "scan", no_argument, NULL, SCAN_ONLY },
2815 { "main-feature",no_argument, NULL, MAIN_FEATURE },
2816 { "chapters", required_argument, NULL, 'c' },
2817 { "angle", required_argument, NULL, ANGLE },
2818 { "markers", optional_argument, NULL, 'm' },
2819 { "audio", required_argument, NULL, 'a' },
2820 { "mixdown", required_argument, NULL, '6' },
2821 { "drc", required_argument, NULL, 'D' },
2822 { "subtitle", required_argument, NULL, 's' },
2823 { "subtitle-forced", optional_argument, NULL, 'F' },
2824 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2825 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2826 { "srt-file", required_argument, NULL, SRT_FILE },
2827 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2828 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2829 { "srt-lang", required_argument, NULL, SRT_LANG },
2830 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2831 { "native-language", required_argument, NULL,'N' },
2832 { "native-dub", no_argument, NULL, NATIVE_DUB },
2833 { "encoder", required_argument, NULL, 'e' },
2834 { "aencoder", required_argument, NULL, 'E' },
2835 { "two-pass", no_argument, NULL, '2' },
2836 { "deinterlace", optional_argument, NULL, 'd' },
2837 { "deblock", optional_argument, NULL, '7' },
2838 { "denoise", optional_argument, NULL, '8' },
2839 { "detelecine", optional_argument, NULL, '9' },
2840 { "decomb", optional_argument, NULL, '5' },
2841 { "grayscale", no_argument, NULL, 'g' },
2842 { "rotate", optional_argument, NULL, ROTATE_FILTER },
2843 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2844 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2845 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2846 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2847 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2848 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2849 { "modulus", required_argument, NULL, MODULUS },
2850 { "itu-par", no_argument, &itu_par, 1 },
2851 { "width", required_argument, NULL, 'w' },
2852 { "height", required_argument, NULL, 'l' },
2853 { "crop", required_argument, NULL, 'n' },
2855 { "vb", required_argument, NULL, 'b' },
2856 { "quality", required_argument, NULL, 'q' },
2857 { "size", required_argument, NULL, 'S' },
2858 { "ab", required_argument, NULL, 'B' },
2859 { "rate", required_argument, NULL, 'r' },
2860 { "arate", required_argument, NULL, 'R' },
2861 { "x264opts", required_argument, NULL, 'x' },
2862 { "turbo", no_argument, NULL, 'T' },
2863 { "maxHeight", required_argument, NULL, 'Y' },
2864 { "maxWidth", required_argument, NULL, 'X' },
2865 { "preset", required_argument, NULL, 'Z' },
2866 { "preset-list", no_argument, NULL, 'z' },
2868 { "aname", required_argument, NULL, 'A' },
2869 { "color-matrix",required_argument, NULL, 'M' },
2870 { "previews", required_argument, NULL, PREVIEWS },
2871 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2872 { "start-at", required_argument, NULL, START_AT },
2873 { "stop-at", required_argument, NULL, STOP_AT },
2874 { "vfr", no_argument, &cfr, 0 },
2875 { "cfr", no_argument, &cfr, 1 },
2876 { "pfr", no_argument, &cfr, 2 },
2877 #if defined( __APPLE_CC__ )
2878 { "no-vlc-dylib-path", no_argument, &no_vlc_dylib, 1 },
2883 int option_index = 0;
2887 cur_optind = optind;
2888 c = getopt_long( argc, argv,
2889 "hv::uC:f:4i:Io:t:c:m::M:a:A:6:s:UF::N:e:E:"
2890 "2dD:7895gOw:l:n:b:q:S:B:r:R:x:TY:X:Z:z",
2891 long_options, &option_index );
2900 /* option was handled entirely in getopt_long */
2909 if( optarg != NULL )
2911 debug = atoi( optarg );
2919 cpu = atoi( optarg );
2924 preset_name = strdup(optarg);
2934 format = strdup( optarg );
2937 input = strdup( optarg );
2939 char *devName = bsd_name_for_path( input ); // alloc
2942 if( device_is_dvd( devName ))
2945 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2946 sprintf( input, "/dev/%s", devName );
2953 output = strdup( optarg );
2966 titleindex = atoi( optarg );
2977 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2979 chapter_start = start;
2982 else if( sscanf( optarg, "%d", &start ) == 1 )
2984 chapter_start = start;
2985 chapter_end = chapter_start;
2989 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2996 angle = atoi( optarg );
2999 if( optarg != NULL )
3001 marker_file = strdup( optarg );
3003 chapter_markers = 1;
3006 if( optarg != NULL )
3008 atracks = strdup( optarg );
3017 if( optarg != NULL )
3019 mixdowns = strdup( optarg );
3023 if( optarg != NULL )
3025 dynamic_range_compression = strdup( optarg );
3029 subtracks = str_split( optarg, ',' );
3032 subforce = str_split( optarg, ',' );
3035 if( optarg != NULL )
3037 subburn = strdup( optarg );
3045 if( optarg != NULL )
3047 subdefault = strdup( optarg );
3055 native_language = strdup( optarg );
3061 srtfile = str_split( optarg, ',' );
3064 srtcodeset = str_split( optarg, ',' );
3067 srtoffset = str_split( optarg, ',' );
3070 srtlang = str_split( optarg, ',' );
3073 if( optarg != NULL )
3075 srtdefault = atoi( optarg );
3086 if( optarg != NULL )
3088 if (!( strcmp( optarg, "fast" ) ))
3090 deinterlace_opt = "-1";
3092 else if (!( strcmp( optarg, "slow" ) ))
3094 deinterlace_opt = "2";
3096 else if (!( strcmp( optarg, "slower" ) ))
3098 deinterlace_opt = "0";
3102 deinterlace_opt = strdup( optarg );
3108 if( optarg != NULL )
3110 deblock_opt = strdup( optarg );
3115 if( optarg != NULL )
3117 if (!( strcmp( optarg, "weak" ) ))
3119 denoise_opt = "2:1:2:3";
3121 else if (!( strcmp( optarg, "medium" ) ))
3123 denoise_opt = "3:2:2:3";
3125 else if (!( strcmp( optarg, "strong" ) ))
3127 denoise_opt = "7:7:5:5";
3131 denoise_opt = strdup( optarg );
3137 if( optarg != NULL )
3139 detelecine_opt = strdup( optarg );
3144 if( optarg != NULL )
3146 decomb_opt = strdup( optarg );
3154 if( optarg != NULL )
3156 rotate_opt = strdup( optarg );
3161 if( optarg != NULL )
3163 sscanf( optarg, "%i", &display_width );
3167 if( optarg != NULL )
3169 sscanf( optarg, "%i:%i", &par_width, &par_height );
3173 if( optarg != NULL )
3175 sscanf( optarg, "%i", &modulus );
3179 if( !strcasecmp( optarg, "ffmpeg" ) )
3181 vcodec = HB_VCODEC_FFMPEG;
3183 else if( !strcasecmp( optarg, "x264" ) )
3185 vcodec = HB_VCODEC_X264;
3187 else if( !strcasecmp( optarg, "theora" ) )
3189 vcodec = HB_VCODEC_THEORA;
3193 fprintf( stderr, "invalid codec (%s)\n", optarg );
3198 if( optarg != NULL )
3200 acodecs = strdup( optarg );
3204 width = atoi( optarg );
3207 height = atoi( optarg );
3212 char * tmp = optarg;
3213 for( i = 0; i < 4; i++ )
3217 crop[i] = strtol( tmp, &tmp, 0 );
3226 for( i = 0; i < hb_video_rates_count; i++ )
3228 if( !strcmp( optarg, hb_video_rates[i].string ) )
3230 vrate = hb_video_rates[i].rate;
3236 fprintf( stderr, "invalid framerate %s\n", optarg );
3238 else if ( cfr == 0 )
3245 if( optarg != NULL )
3247 arates = strdup( optarg );
3251 vbitrate = atoi( optarg );
3254 vquality = atof( optarg );
3257 size = atoi( optarg );
3260 if( optarg != NULL )
3262 abitrates = strdup( optarg );
3266 x264opts = strdup( optarg );
3269 turbo_opts_enabled = 1;
3272 maxHeight = atoi( optarg );
3275 maxWidth = atoi (optarg );
3278 if( optarg != NULL )
3280 anames = strdup( optarg );
3284 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
3286 case START_AT_PREVIEW:
3287 start_at_preview = atoi( optarg );
3290 start_at_string = strdup( optarg );
3291 start_at_token = strtok( start_at_string, ":");
3292 if( !strcmp( start_at_token, "frame" ) )
3294 start_at_token = strtok( NULL, ":");
3295 start_at_frame = atoi(start_at_token);
3297 else if( !strcmp( start_at_token, "pts" ) )
3299 start_at_token = strtok( NULL, ":");
3300 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3302 else if( !strcmp( start_at_token, "duration" ) )
3304 start_at_token = strtok( NULL, ":");
3305 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3306 start_at_pts *= 90000LL;
3310 stop_at_string = strdup( optarg );
3311 stop_at_token = strtok( stop_at_string, ":");
3312 if( !strcmp( stop_at_token, "frame" ) )
3314 stop_at_token = strtok( NULL, ":");
3315 stop_at_frame = atoi(stop_at_token);
3317 else if( !strcmp( stop_at_token, "pts" ) )
3319 stop_at_token = strtok( NULL, ":");
3320 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3322 else if( !strcmp( stop_at_token, "duration" ) )
3324 stop_at_token = strtok( NULL, ":");
3325 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3326 stop_at_pts *= 90000LL;
3330 if( atoi( optarg ) == 601 )
3332 else if( atoi( optarg ) == 709 )
3336 min_title_duration = strtol( optarg, NULL, 0 );
3339 fprintf( stderr, "unknown option (%s)\n", argv[cur_optind] );
3347 char * str_printf(const char *fmt, ...)
3349 /* Guess we need no more than 100 bytes. */
3353 char *tmp, *str = NULL;
3355 str = (char*)malloc(size);
3358 /* Try to print in the allocated space. */
3360 len = vsnprintf(str, size, fmt, ap);
3363 /* If that worked, return the string. */
3364 if (len > -1 && len < size) {
3368 /* Else try again with more space. */
3369 if (len > -1) /* glibc 2.1 */
3370 size = len+1; /* precisely what is needed */
3371 else /* glibc 2.0 */
3372 size *= 2; /* twice the old size */
3374 tmp = (char*)realloc(str, size);
3382 static int CheckOptions( int argc, char ** argv )
3384 #if defined( __APPLE_CC__ )
3385 // If OSX, add VLC dylib path and exec to make it stick.
3388 if ( !no_vlc_dylib )
3390 dylib_path = getenv("DYLD_FALLBACK_LIBRARY_PATH");
3391 if ( dylib_path == NULL ||
3392 strstr( dylib_path, "/Applications/VLC.app/Contents/MacOS/lib" ) == NULL )
3398 home = getenv("HOME");
3400 if ( dylib_path == NULL )
3402 // Set the system default of $HOME/lib:/usr/local/lib:/usr/lib
3403 // And add our extra path
3406 path = str_printf("%s/lib:%s:%s:%s%s", home,
3408 EXTRA_VLC_DYLD_PATH,
3409 home, EXTRA_VLC_DYLD_PATH);
3413 path = str_printf("%s:%s", DEFAULT_DYLD_PATH, EXTRA_VLC_DYLD_PATH);
3416 result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
3420 // add our extra path
3423 path = str_printf("%s:%s:%s%s", dylib_path, EXTRA_VLC_DYLD_PATH,
3424 home, EXTRA_VLC_DYLD_PATH);
3428 path = str_printf("%s:%s", dylib_path, EXTRA_VLC_DYLD_PATH);
3431 result = setenv("DYLD_FALLBACK_LIBRARY_PATH", path, 1);
3435 const char ** new_argv;
3438 new_argv = (const char**)malloc( (argc + 2) * sizeof(char*) );
3439 new_argv[0] = argv[0];
3440 new_argv[1] = "--no-vlc-dylib-path";
3441 for (i = 1; i < argc; i++)
3442 new_argv[i+1] = argv[i];
3443 new_argv[i+1] = NULL;
3444 execv(new_argv[0], (char* const*)new_argv);
3455 if( input == NULL || *input == '\0' )
3457 fprintf( stderr, "Missing input device. Run %s --help for "
3458 "syntax.\n", argv[0] );
3463 if( titleindex > 0 && !titlescan )
3465 if( output == NULL || *output == '\0' )
3467 fprintf( stderr, "Missing output file name. Run %s --help "
3468 "for syntax.\n", argv[0] );
3474 char * p = strrchr( output, '.' );
3477 if( p && ( !strcasecmp( p, ".mp4" ) ||
3478 !strcasecmp( p, ".m4v" ) ) )
3482 else if( p && !strcasecmp(p, ".mkv" ) )
3485 #ifndef __APPLE_CC__
3486 // default to Lame for MKV (except under OS X where Core Audio is available)
3487 default_acodec = HB_ACODEC_LAME;
3492 fprintf( stderr, "Output format couldn't be guessed "
3493 "from file name, using default.\n" );
3497 else if( !strcasecmp( format, "mp4" ) ||
3498 !strcasecmp( format, "m4v" ) )
3502 else if( !strcasecmp( format, "mkv" ) )
3505 #ifndef __APPLE_CC__
3506 // default to Lame for MKV (except under OS X where Core Audio is available)
3507 default_acodec = HB_ACODEC_LAME;
3512 fprintf( stderr, "Invalid output format (%s). Possible "
3513 "choices are mp4, m4v and mkv\n.", format );
3521 static int get_acodec_for_string( char *codec )
3523 if( !strcasecmp( codec, "ac3" ) )
3525 return HB_ACODEC_AC3;
3527 else if( !strcasecmp( codec, "copy" ) )
3529 return HB_ACODEC_AC3_PASS | HB_ACODEC_DCA_PASS;
3531 else if( !strcasecmp( codec, "copy:ac3" ) )
3533 return HB_ACODEC_AC3_PASS;
3535 else if( !strcasecmp( codec, "copy:dts" ) || !strcasecmp( codec, "copy:dca" ) )
3537 return HB_ACODEC_DCA_PASS;
3539 else if( !strcasecmp( codec, "lame" ) )
3541 return HB_ACODEC_LAME;
3543 else if( !strcasecmp( codec, "faac" ) )
3545 return HB_ACODEC_FAAC;
3547 else if( !strcasecmp( codec, "vorbis") )
3549 return HB_ACODEC_VORBIS;
3552 else if( !strcasecmp( codec, "ca_aac") )
3554 return HB_ACODEC_CA_AAC;
3563 static int is_sample_rate_valid(int rate)
3566 for( i = 0; i < hb_audio_rates_count; i++ )
3568 if (rate == hb_audio_rates[i].rate)
3575 /****************************************************************************
3578 * Returns the BSD device name for the block device that contains the
3579 * passed-in path. Returns NULL on failure.
3580 ****************************************************************************/
3581 static char* bsd_name_for_path(char *path)
3585 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3591 // Get the volume reference number.
3592 FSCatalogInfo catalogInfo;
3593 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3599 FSVolumeRefNum volRefNum = catalogInfo.volume;
3601 // Now let's get the device name
3602 GetVolParmsInfoBuffer volumeParms;
3603 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3609 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3610 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3611 if( volumeParms.vMVersion < 4 )
3616 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3617 if( !volumeParms.vMDeviceID )
3622 return strdup( volumeParms.vMDeviceID );
3625 /****************************************************************************
3628 * Returns whether or not the passed in BSD device represents a DVD, or other
3630 ****************************************************************************/
3631 static int device_is_dvd(char *device)
3633 io_service_t service = get_iokit_service(device);
3634 if( service == IO_OBJECT_NULL )
3638 int result = is_dvd_service(service);
3639 IOObjectRelease(service);
3643 /****************************************************************************
3646 * Returns the IOKit service object for the passed in BSD device name.
3647 ****************************************************************************/
3648 static io_service_t get_iokit_service( char *device )
3650 CFMutableDictionaryRef matchingDict;
3651 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3652 if( matchingDict == NULL )
3654 return IO_OBJECT_NULL;
3656 // Fetch the object with the matching BSD node name. There should only be
3657 // one match, so IOServiceGetMatchingService is used instead of
3658 // IOServiceGetMatchingServices to simplify the code.
3659 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3662 /****************************************************************************
3665 * Returns whether or not the service passed in is a DVD.
3667 * Searches for an IOMedia object that represents the entire (whole) media that
3668 * the volume is on. If the volume is on partitioned media, the whole media
3669 * object will be a parent of the volume's media object. If the media is not
3670 * partitioned, the volume's media object will be the whole media object.
3671 ****************************************************************************/
3672 static int is_dvd_service( io_service_t service )
3674 kern_return_t kernResult;
3677 // Create an iterator across all parents of the service object passed in.
3678 kernResult = IORegistryEntryCreateIterator( service,
3680 kIORegistryIterateRecursively | kIORegistryIterateParents,
3682 if( kernResult != KERN_SUCCESS )
3686 if( iter == IO_OBJECT_NULL )
3691 // A reference on the initial service object is released in the do-while
3692 // loop below, so add a reference to balance.
3693 IOObjectRetain( service );
3698 if( is_whole_media_service( service ) &&
3699 IOObjectConformsTo( service, kIODVDMediaClass) )
3703 IOObjectRelease( service );
3704 } while( !result && (service = IOIteratorNext( iter )) );
3705 IOObjectRelease( iter );
3710 /****************************************************************************
3711 * is_whole_media_service
3713 * Returns whether or not the service passed in is an IOMedia service and
3714 * represents the "whole" media instead of just a partition.
3716 * The whole media object is indicated in the IORegistry by the presence of a
3717 * property with the key "Whole" and value "Yes".
3718 ****************************************************************************/
3719 static int is_whole_media_service( io_service_t service )
3723 if( IOObjectConformsTo( service, kIOMediaClass ) )
3725 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3726 CFSTR( kIOMediaWholeKey ),
3727 kCFAllocatorDefault,
3733 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3734 CFRelease( wholeMedia );
3739 #endif // __APPLE_CC__