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 static int debug = HB_DEBUG_NONE;
34 static int update = 0;
35 static int dvdnav = 0;
36 static char * input = NULL;
37 static char * output = NULL;
38 static char * format = NULL;
39 static int titleindex = 1;
40 static int longest_title = 0;
41 static char * native_language = NULL;
42 static int twoPass = 0;
43 static int deinterlace = 0;
44 static char * deinterlace_opt = 0;
45 static int deblock = 0;
46 static char * deblock_opt = 0;
47 static int denoise = 0;
48 static char * denoise_opt = 0;
49 static int detelecine = 0;
50 static char * detelecine_opt = 0;
51 static int decomb = 0;
52 static char * decomb_opt = 0;
53 static int grayscale = 0;
54 static int vcodec = HB_VCODEC_FFMPEG;
55 static int h264_13 = 0;
56 static int h264_30 = 0;
57 static hb_list_t * audios = NULL;
58 static hb_audio_config_t * audio = NULL;
59 static int num_audio_tracks = 0;
60 static char * mixdowns = NULL;
61 static char * dynamic_range_compression = NULL;
62 static char * atracks = NULL;
63 static char * arates = NULL;
64 static char * abitrates = NULL;
65 static char * acodecs = NULL;
66 static char * anames = NULL;
67 static int default_acodec = HB_ACODEC_FAAC;
68 static int default_abitrate = 160;
69 static char ** subtracks = NULL;
70 static char ** subforce = NULL;
71 static char * subburn = NULL;
72 static char * subdefault = NULL;
73 static int subtitle_scan = 0;
75 static int height = 0;
76 static int crop[4] = { -1,-1,-1,-1 };
79 static float vquality = -1.0;
80 static int vbitrate = 0;
83 static int anamorphic_mode = 0;
84 static int modulus = 0;
85 static int par_height = 0;
86 static int par_width = 0;
87 static int display_width = 0;
88 static int keep_display_aspect = 0;
89 static int itu_par = 0;
91 static int chapter_start = 0;
92 static int chapter_end = 0;
93 static int chapter_markers = 0;
94 static char * marker_file = NULL;
96 static char *x264opts = NULL;
97 static char *x264opts2 = NULL;
98 static int maxHeight = 0;
99 static int maxWidth = 0;
100 static int turbo_opts_enabled = 0;
101 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
102 static int largeFileSize = 0;
103 static int preset = 0;
104 static char * preset_name = 0;
106 static int mp4_optimize = 0;
107 static int ipod_atom = 0;
108 static int color_matrix = 0;
109 static int preview_count = 10;
110 static int store_previews = 0;
111 static int start_at_preview = 0;
112 static int64_t stop_at_pts = 0;
113 static int stop_at_frame = 0;
114 static char * stop_at_string = NULL;
115 static char * stop_at_token = NULL;
117 /* Exit cleanly on Ctrl-C */
118 static volatile int die = 0;
119 static void SigHandler( int );
122 static void ShowCommands();
123 static void ShowHelp();
124 static void ShowPresets();
126 static int ParseOptions( int argc, char ** argv );
127 static int CheckOptions( int argc, char ** argv );
128 static int HandleEvents( hb_handle_t * h );
130 static int get_acodec_for_string( char *codec );
131 static int is_sample_rate_valid(int rate);
134 static char* bsd_name_for_path(char *path);
135 static int device_is_dvd(char *device);
136 static io_service_t get_iokit_service( char *device );
137 static int is_dvd_service( io_service_t service );
138 static is_whole_media_service( io_service_t service );
141 /* Only print the "Muxing..." message once */
142 static int show_mux_warning = 1;
144 /****************************************************************************
147 * When using the CLI just display using hb_log as we always did in the past
148 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
149 ****************************************************************************/
150 static void hb_cli_error_handler ( const char *errmsg )
152 fprintf( stderr, "ERROR: %s\n", errmsg );
155 int main( int argc, char ** argv )
161 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
162 * force it to unbuffered otherwise informative output is not easily parsed.
164 #if defined( _WIN32 ) || defined( __MINGW32__ )
165 setvbuf( stdout, NULL, _IONBF, 0 );
166 setvbuf( stderr, NULL, _IONBF, 0 );
169 audios = hb_list_init();
171 /* Parse command line */
172 if( ParseOptions( argc, argv ) ||
173 CheckOptions( argc, argv ) )
178 #if defined( PTW32_STATIC_LIB )
179 pthread_win32_process_attach_np();
180 pthread_win32_thread_attach_np();
183 /* Register our error handler */
184 hb_register_error_handler(&hb_cli_error_handler);
187 h = hb_init( debug, update );
188 hb_dvd_set_dvdnav( dvdnav );
191 fprintf( stderr, "%s - %s - %s\n",
192 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
194 /* Check for update */
197 if( ( build = hb_check_update( h, &version ) ) > -1 )
199 fprintf( stderr, "You are using an old version of "
200 "HandBrake.\nLatest is %s (build %d).\n", version,
205 fprintf( stderr, "Your version of HandBrake is up to "
213 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
214 hb_get_cpu_count( h ) > 1 ? "s" : "" );
217 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
218 cpu > 1 ? "s" : "" );
219 hb_set_cpu_count( h, cpu );
222 /* Exit ASAP on Ctrl-C */
223 signal( SIGINT, SigHandler );
225 /* Feed libhb with a DVD to scan */
226 fprintf( stderr, "Opening %s...\n", input );
230 * We need to scan for all the titles in order to find the longest
235 hb_scan( h, input, titleindex, preview_count, store_previews );
240 #if defined( __MINGW32__ )
244 case 0x03: /* ctrl-c */
246 fprintf( stdout, "\nEncoding Quit by user command\n" );
250 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
262 #elif !defined(SYS_BEOS)
272 FD_SET( STDIN_FILENO, &fds );
273 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
280 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
282 if( buf[size] == '\n' )
289 if( size >= 256 || buf[size] == '\n' )
294 fprintf( stdout, "\nEncoding Quit by user command\n" );
298 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
320 if( input ) free( input );
321 if( output ) free( output );
322 if( format ) free( format );
325 while( ( audio = hb_list_item( audios, 0 ) ) )
327 hb_list_rem( audios, audio );
328 if( audio->out.name )
330 free( audio->out.name );
334 hb_list_close( &audios );
336 if( mixdowns ) free( mixdowns );
337 if( dynamic_range_compression ) free( dynamic_range_compression );
338 if( atracks ) free( atracks );
339 if( arates ) free( arates );
340 if( abitrates ) free( abitrates );
341 if( acodecs ) free( acodecs );
342 if( anames ) free( anames );
343 if (native_language ) free (native_language );
344 if( x264opts ) free (x264opts );
345 if( x264opts2 ) free (x264opts2 );
346 if (preset_name) free (preset_name);
347 if( stop_at_string ) free( stop_at_string );
349 fprintf( stderr, "HandBrake has exited.\n" );
351 #if defined( PTW32_STATIC_LIB )
352 pthread_win32_thread_detach_np();
353 pthread_win32_process_detach_np();
359 static void ShowCommands()
361 fprintf( stdout, "\nCommands:\n" );
362 fprintf( stdout, " [h]elp Show this message\n" );
363 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
364 fprintf( stdout, " [p]ause Pause encoding\n" );
365 fprintf( stdout, " [r]esume Resume encoding\n" );
368 static void PrintTitleInfo( hb_title_t * title )
370 hb_chapter_t * chapter;
371 hb_audio_config_t * audio;
372 hb_subtitle_t * subtitle;
375 fprintf( stderr, "+ title %d:\n", title->index );
376 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
377 title->vts, title->ttn, title->cell_start, title->cell_end,
378 title->block_count );
380 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
381 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
382 title->hours, title->minutes, title->seconds );
383 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
384 title->width, title->height,
385 title->pixel_aspect_width,
386 title->pixel_aspect_height,
387 (float) title->aspect,
388 (float) title->rate / title->rate_base );
389 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
390 title->crop[1], title->crop[2], title->crop[3] );
391 fprintf( stderr, " + chapters:\n" );
392 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
394 chapter = hb_list_item( title->list_chapter, i );
395 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
396 "%02d:%02d:%02d\n", chapter->index,
397 chapter->cell_start, chapter->cell_end,
398 chapter->block_count, chapter->hours, chapter->minutes,
401 fprintf( stderr, " + audio tracks:\n" );
402 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
404 audio = hb_list_audio_config_item( title->list_audio, i );
405 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
407 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
408 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
412 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
415 fprintf( stderr, " + subtitle tracks:\n" );
416 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
418 subtitle = hb_list_item( title->list_subtitle, i );
419 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
420 i + 1, subtitle->lang,
422 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
425 if(title->detected_interlacing)
427 /* Interlacing was found in half or more of the preview frames */
428 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
433 static int test_sub_list( char ** list, char * needle, int pos )
437 if ( list == NULL || needle == NULL )
440 if ( list[0] == NULL && pos == 1 )
443 for ( i = 0; list[i] != NULL; i++ )
445 if ( strcasecmp( list[i], needle ) == 0 )
451 static int HandleEvents( hb_handle_t * h )
454 int tmp_num_audio_tracks;
456 hb_get_state( h, &s );
463 #define p s.param.scanning
464 case HB_STATE_SCANNING:
465 /* Show what title is currently being scanned */
466 fprintf( stderr, "Scanning title %d", p.title_cur );
468 fprintf( stderr, " of %d", p.title_count );
469 fprintf( stderr, "...\n" );
473 case HB_STATE_SCANDONE:
481 /* Audio argument string parsing variables */
485 int mixdown = HB_AMIXDOWN_DOLBYPLII;
487 /* Audio argument string parsing variables */
489 list = hb_get_titles( h );
491 if( !hb_list_count( list ) )
493 /* No valid title, stop right there */
494 fprintf( stderr, "No title found.\n" );
501 int longest_title_idx=0;
502 int longest_title_pos=-1;
503 int longest_title_time=0;
506 fprintf( stderr, "Searching for longest title...\n" );
508 for( i = 0; i < hb_list_count( list ); i++ )
510 title = hb_list_item( list, i );
511 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
512 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
513 i, title->index, title_time );
514 if( longest_title_time < title_time )
516 longest_title_time = title_time;
517 longest_title_pos = i;
518 longest_title_idx = title->index;
521 if( longest_title_pos == -1 )
523 fprintf( stderr, "No longest title found.\n" );
527 titleindex = longest_title_idx;
528 fprintf( stderr, "Found longest title, setting title to %d\n",
531 title = hb_list_item( list, longest_title_pos);
533 title = hb_list_item( list, 0 );
538 /* Scan-only mode, print infos and exit */
540 for( i = 0; i < hb_list_count( list ); i++ )
542 title = hb_list_item( list, i );
543 PrintTitleInfo( title );
549 /* Set job settings */
552 PrintTitleInfo( title );
554 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame )
556 job->chapter_start = MAX( job->chapter_start,
558 job->chapter_end = MIN( job->chapter_end,
560 job->chapter_end = MAX( job->chapter_start,
571 fprintf( stderr, "+ Using preset: %s", preset_name);
573 if (!strcmp(preset_name, "Universal"))
579 vcodec = HB_VCODEC_X264;
580 job->vquality = 20.0;
584 atracks = strdup("1,1");
588 acodecs = strdup("faac,ac3");
592 abitrates = strdup("160,160");
596 mixdowns = strdup("dpl2,auto");
600 arates = strdup("48,Auto");
602 if( !dynamic_range_compression )
604 dynamic_range_compression = strdup("0.0,0.0");
609 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:me=umh");
612 job->chapter_markers = 1;
615 if (!strcmp(preset_name, "iPod"))
622 vcodec = HB_VCODEC_X264;
626 atracks = strdup("1");
630 acodecs = strdup("faac");
634 abitrates = strdup("160");
638 mixdowns = strdup("dpl2");
642 arates = strdup("48");
644 if( !dynamic_range_compression )
646 dynamic_range_compression = strdup("0.0");
651 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1");
653 job->chapter_markers = 1;
656 if (!strcmp(preset_name, "iPhone & iPod Touch"))
662 vcodec = HB_VCODEC_X264;
663 job->vquality = 20.0;
667 atracks = strdup("1");
671 acodecs = strdup("faac");
675 abitrates = strdup("128");
679 mixdowns = strdup("dpl2");
683 arates = strdup("48");
685 if( !dynamic_range_compression )
687 dynamic_range_compression = strdup("0.0");
692 x264opts = strdup("cabac=0:ref=2:mixed-refs:me=umh");
694 job->chapter_markers = 1;
697 if (!strcmp(preset_name, "AppleTV"))
703 job->largeFileSize = 1;
704 vcodec = HB_VCODEC_X264;
705 job->vquality = 20.0;
709 atracks = strdup("1,1");
713 acodecs = strdup("faac,ac3");
717 abitrates = strdup("160,160");
721 mixdowns = strdup("dpl2,auto");
725 arates = strdup("48,Auto");
727 if( !dynamic_range_compression )
729 dynamic_range_compression = strdup("0.0,0.0");
734 x264opts = strdup("cabac=0:ref=2:mixed-refs=1:bframes=3:me=umh:subme=7:b-adapt=2:8x8dct=1");
737 job->chapter_markers = 1;
740 if (!strcmp(preset_name, "Normal"))
746 vcodec = HB_VCODEC_X264;
747 job->vquality = 20.0;
751 atracks = strdup("1");
755 acodecs = strdup("faac");
759 abitrates = strdup("160");
763 mixdowns = strdup("dpl2");
767 arates = strdup("48");
769 if( !dynamic_range_compression )
771 dynamic_range_compression = strdup("0.0");
775 x264opts = strdup("ref=2:bframes=2:me=umh");
778 job->chapter_markers = 1;
781 if (!strcmp(preset_name, "High Profile"))
787 vcodec = HB_VCODEC_X264;
788 job->vquality = 20.0;
792 atracks = strdup("1,1");
796 acodecs = strdup("faac,ac3");
800 abitrates = strdup("160,160");
804 mixdowns = strdup("dpl2,auto");
808 arates = strdup("48,Auto");
810 if( !dynamic_range_compression )
812 dynamic_range_compression = strdup("0.0,0.0");
816 x264opts = strdup("ref=3:mixed-refs:bframes=3:weightb:b-pyramid:b-adapt=2:me=umh:subme=9:analyse=all:8x8dct");
821 job->chapter_markers = 1;
824 if (!strcmp(preset_name, "Classic"))
830 job->vbitrate = 1000;
833 atracks = strdup("1");
837 acodecs = strdup("faac");
841 abitrates = strdup("160");
845 mixdowns = strdup("dpl2");
849 arates = strdup("48");
851 if( !dynamic_range_compression )
853 dynamic_range_compression = strdup("0.0");
857 if (!strcmp(preset_name, "AppleTV Legacy"))
863 job->largeFileSize = 1;
864 vcodec = HB_VCODEC_X264;
865 job->vbitrate = 2500;
868 atracks = strdup("1,1");
872 acodecs = strdup("faac,ac3");
876 abitrates = strdup("160,160");
880 mixdowns = strdup("dpl2,auto");
884 arates = strdup("48,Auto");
886 if( !dynamic_range_compression )
888 dynamic_range_compression = strdup("0.0,0.0");
892 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0");
895 job->chapter_markers = 1;
898 if (!strcmp(preset_name, "iPhone Legacy"))
905 vcodec = HB_VCODEC_X264;
909 atracks = strdup("1");
913 acodecs = strdup("faac");
917 abitrates = strdup("128");
921 mixdowns = strdup("dpl2");
925 arates = strdup("48");
927 if( !dynamic_range_compression )
929 dynamic_range_compression = strdup("0.0");
934 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0");
936 job->chapter_markers = 1;
939 if (!strcmp(preset_name, "iPod Legacy"))
946 vcodec = HB_VCODEC_X264;
947 job->vbitrate = 1500;
950 atracks = strdup("1");
954 acodecs = strdup("faac");
958 abitrates = strdup("160");
962 mixdowns = strdup("dpl2");
966 arates = strdup("48");
968 if( !dynamic_range_compression )
970 dynamic_range_compression = strdup("0.0");
975 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");
977 job->chapter_markers = 1;
981 if ( chapter_markers )
983 job->chapter_markers = chapter_markers;
985 if( marker_file != NULL )
987 hb_csv_file_t * file = hb_open_csv_file( marker_file );
988 hb_csv_cell_t * cell;
992 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
996 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1000 /* Parse the cells */
1001 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1003 /* We have a chapter number */
1004 if( cell->cell_col == 0 )
1006 row = cell->cell_row;
1007 chapter = atoi( cell->cell_text );
1010 /* We have a chapter name */
1011 if( cell->cell_col == 1 && row == cell->cell_row )
1013 /* If we have a valid chapter, copy the string an terminate it */
1014 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1016 hb_chapter_t * chapter_s;
1018 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1019 strncpy(chapter_s->title, cell->cell_text, 1023);
1020 chapter_s->title[1023] = '\0';
1025 hb_dispose_cell( cell );
1028 hb_close_csv_file( file );
1033 /* No marker file */
1035 int number_of_chapters = hb_list_count(job->title->list_chapter);
1038 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1040 hb_chapter_t * chapter_s;
1041 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1042 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1043 chapter_s->title[1023] = '\0';
1048 if( crop[0] >= 0 && crop[1] >= 0 &&
1049 crop[2] >= 0 && crop[3] >= 0 )
1051 memcpy( job->crop, crop, 4 * sizeof( int ) );
1054 job->deinterlace = deinterlace;
1055 job->grayscale = grayscale;
1057 /* Add selected filters */
1058 job->filters = hb_list_init();
1061 hb_filter_detelecine.settings = detelecine_opt;
1062 hb_list_add( job->filters, &hb_filter_detelecine );
1066 hb_filter_decomb.settings = decomb_opt;
1067 hb_list_add( job->filters, &hb_filter_decomb );
1071 hb_filter_deinterlace.settings = deinterlace_opt;
1072 hb_list_add( job->filters, &hb_filter_deinterlace );
1076 hb_filter_deblock.settings = deblock_opt;
1077 hb_list_add( job->filters, &hb_filter_deblock );
1081 hb_filter_denoise.settings = denoise_opt;
1082 hb_list_add( job->filters, &hb_filter_denoise );
1085 switch( anamorphic_mode )
1087 case 0: // Non-anamorphic
1089 if( width && height )
1092 job->height = height;
1097 hb_fix_aspect( job, HB_KEEP_WIDTH );
1101 job->height = height;
1102 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1104 else if( !width && !height )
1106 hb_fix_aspect( job, HB_KEEP_WIDTH );
1111 case 1: // Strict anammorphic
1112 job->anamorphic.mode = anamorphic_mode;
1115 case 2: // Loose anamorphic
1116 job->anamorphic.mode = 2;
1120 job->anamorphic.modulus = modulus;
1125 job->anamorphic.itu_par = itu_par;
1132 else if( !width && !height )
1134 /* Default to full width when one isn't specified for loose anamorphic */
1135 job->width = title->width - job->crop[2] - job->crop[3];
1140 case 3: // Custom Anamorphic 3: Power User Jamboree
1141 job->anamorphic.mode = 3;
1145 job->anamorphic.modulus = modulus;
1150 job->anamorphic.itu_par = itu_par;
1153 if( par_width && par_height )
1155 job->anamorphic.par_width = par_width;
1156 job->anamorphic.par_height = par_height;
1159 if( keep_display_aspect )
1161 job->anamorphic.keep_display_aspect = 1;
1163 /* First, what *is* the display aspect? */
1164 int cropped_width = title->width - job->crop[2] - job->crop[3];
1165 int cropped_height = title->height - job->crop[0] - job->crop[1];
1167 /* XXX -- I'm assuming people want to keep the source
1168 display AR even though they might have already
1169 asked for ITU values instead. */
1170 float source_display_width = (float)cropped_width *
1171 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1172 float display_aspect = source_display_width / (float)cropped_height;
1173 /* When keeping display aspect, we have to rank some values
1174 by priority in order to consistently handle situations
1175 when more than one might be specified by default.
1177 * First off, PAR gets ignored. (err make this reality)
1178 * Height will be respected over all other settings,
1179 * If it isn't set, display_width will be followed.
1180 * If it isn't set, width will be followed. */
1183 /* We scale the display width to the new height */
1184 display_width = (int)( (double)height * display_aspect );
1186 else if( display_width )
1188 /* We scale the height to the new display width */
1189 height = (int)( (double)display_width / display_aspect );
1195 /* Adjust the PAR to create the new display width
1196 from the default job width. */
1197 job->anamorphic.dar_width = display_width;
1199 job->anamorphic.dar_height = height ?
1201 title->height - job->crop[0] - job->crop[1];
1204 if( width && height )
1206 /* Use these storage dimensions */
1208 job->height = height;
1212 /* Use just this storage width */
1214 job->height = title->height - job->crop[0] - job->crop[1];
1218 /* Use just this storage height. */
1219 job->height = height;
1220 job->width = title->width - job->crop[2] - job->crop[3];
1222 else if( !width && !height )
1224 /* Assume source dimensions after cropping. */
1225 job->width = title->width - job->crop[2] - job->crop[3];
1226 job->height = title->height - job->crop[0] - job->crop[1];
1232 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1234 job->vquality = vquality;
1239 job->vquality = -1.0;
1240 job->vbitrate = vbitrate;
1244 job->vcodec = vcodec;
1248 job->h264_level = 13;
1252 job->h264_level = 30;
1257 job->vrate = 27000000;
1258 job->vrate_base = vrate;
1262 // cfr or pfr flag with no rate specified implies
1263 // use the title rate.
1265 job->vrate = title->rate;
1266 job->vrate_base = title->rate_base;
1269 /* Grab audio tracks */
1272 char * token = strtok(atracks, ",");
1275 int track_start, track_end;
1276 while( token != NULL )
1278 audio = calloc(1, sizeof(*audio));
1279 hb_audio_config_init(audio);
1280 if (strlen(token) >= 3)
1282 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1285 for (i = track_start - 1; i < track_end; i++)
1287 if (i != track_start - 1)
1289 audio = calloc(1, sizeof(*audio));
1290 hb_audio_config_init(audio);
1292 audio->in.track = i;
1293 audio->out.track = num_audio_tracks++;
1294 hb_list_add(audios, audio);
1297 else if( !strcasecmp(token, "none" ) )
1299 audio->in.track = audio->out.track = -1;
1300 audio->out.codec = 0;
1301 hb_list_add(audios, audio);
1306 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1313 audio->in.track = atoi(token) - 1;
1314 audio->out.track = num_audio_tracks++;
1315 hb_list_add(audios, audio);
1317 token = strtok(NULL, ",");
1321 /* Parse audio tracks */
1322 if( hb_list_count(audios) == 0 )
1324 /* Create a new audio track with default settings */
1325 audio = calloc(1, sizeof(*audio));
1326 hb_audio_config_init(audio);
1327 /* Add it to our audios */
1328 hb_list_add(audios, audio);
1331 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1332 for (i = 0; i < tmp_num_audio_tracks; i++)
1334 audio = hb_list_item(audios, 0);
1335 if( (audio == NULL) || (audio->in.track == -1) ||
1336 (audio->out.track == -1) || (audio->out.codec == 0) )
1342 if( hb_audio_add( job, audio ) == 0 )
1344 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1345 audio->in.track + 1 );
1350 hb_list_rem(audios, audio);
1352 if( audio->out.name )
1354 free( audio->out.name);
1363 char * token = strtok(acodecs, ",");
1366 while ( token != NULL )
1368 if ((acodec = get_acodec_for_string(token)) == -1)
1370 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1371 acodec = default_acodec;
1373 if( i < num_audio_tracks )
1375 audio = hb_list_audio_config_item(job->list_audio, i);
1376 audio->out.codec = acodec;
1380 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1381 hb_audio_config_t audio;
1385 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1387 hb_audio_config_init(&audio);
1388 audio.in.track = last_audio->in.track;
1389 audio.out.track = num_audio_tracks++;
1390 audio.out.codec = acodec;
1391 hb_audio_add(job, &audio);
1395 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1398 token = strtok(NULL, ",");
1402 if( i < num_audio_tracks )
1404 /* We have fewer inputs than audio tracks, use the default codec for
1405 * this container for the remaining tracks. Unless we only have one input
1406 * then use that codec instead.
1409 acodec = default_acodec;
1410 for ( ; i < num_audio_tracks; i++)
1412 audio = hb_list_audio_config_item(job->list_audio, i);
1413 audio->out.codec = acodec;
1422 char * token = strtok(arates, ",");
1425 while ( token != NULL )
1427 arate = atoi(token);
1428 audio = hb_list_audio_config_item(job->list_audio, i);
1431 for( j = 0; j < hb_audio_rates_count; j++ )
1433 if( !strcmp( token, hb_audio_rates[j].string ) )
1435 arate = hb_audio_rates[j].rate;
1442 if (!is_sample_rate_valid(arate))
1444 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1445 arate = audio->in.samplerate;
1448 audio->out.samplerate = arate;
1449 if( (++i) >= num_audio_tracks )
1450 break; /* We have more inputs than audio tracks, oops */
1454 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1456 token = strtok(NULL, ",");
1459 if (i < num_audio_tracks)
1461 /* We have fewer inputs than audio tracks, use default sample rate.
1462 * Unless we only have one input, then use that for all tracks.
1465 arate = audio->in.samplerate;
1466 for ( ; i < num_audio_tracks; i++)
1468 audio = hb_list_audio_config_item(job->list_audio, i);
1469 audio->out.samplerate = arate;
1478 char * token = strtok(abitrates, ",");
1481 while ( token != NULL )
1483 abitrate = atoi(token);
1484 audio = hb_list_audio_config_item(job->list_audio, i);
1488 audio->out.bitrate = abitrate;
1489 if( (++i) >= num_audio_tracks )
1490 break; /* We have more inputs than audio tracks, oops */
1494 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1496 token = strtok(NULL, ",");
1499 if (i < num_audio_tracks)
1501 /* We have fewer inputs than audio tracks, use the default bitrate
1502 * for the remaining tracks. Unless we only have one input, then use
1503 * that for all tracks.
1506 abitrate = default_abitrate;
1507 for (; i < num_audio_tracks; i++)
1509 audio = hb_list_audio_config_item(job->list_audio, i);
1510 audio->out.bitrate = abitrate;
1517 if ( dynamic_range_compression )
1519 char * token = strtok(dynamic_range_compression, ",");
1521 token = dynamic_range_compression;
1522 while ( token != NULL )
1524 d_r_c = atof(token);
1525 audio = hb_list_audio_config_item(job->list_audio, i);
1528 audio->out.dynamic_range_compression = d_r_c;
1529 if( (++i) >= num_audio_tracks )
1530 break; /* We have more inputs than audio tracks, oops */
1534 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1536 token = strtok(NULL, ",");
1539 if (i < num_audio_tracks)
1541 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1542 * tracks. Unless we only have one input, then use the same DRC for all
1547 for (; i < num_audio_tracks; i++)
1549 audio = hb_list_audio_config_item(job->list_audio, i);
1550 audio->out.dynamic_range_compression = d_r_c;
1559 char * token = strtok(mixdowns, ",");
1562 while ( token != NULL )
1564 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1565 audio = hb_list_audio_config_item(job->list_audio, i);
1568 audio->out.mixdown = mixdown;
1569 if( (++i) >= num_audio_tracks )
1570 break; /* We have more inputs than audio tracks, oops */
1574 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1576 token = strtok(NULL, ",");
1579 if (i < num_audio_tracks)
1581 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1582 * we only have one input, then use that.
1585 mixdown = HB_AMIXDOWN_DOLBYPLII;
1586 for (; i < num_audio_tracks; i++)
1588 audio = hb_list_audio_config_item(job->list_audio, i);
1589 audio->out.mixdown = mixdown;
1594 /* Audio Track Names */
1598 char * token = strtok(anames, ",");
1601 while ( token != NULL )
1603 audio = hb_list_audio_config_item(job->list_audio, i);
1606 audio->out.name = strdup(token);
1607 if( (++i) >= num_audio_tracks )
1608 break; /* We have more names than audio tracks, oops */
1612 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1615 token = strtok(NULL, ",");
1618 if( i < num_audio_tracks && i == 1 )
1620 /* We have exactly one name and more than one audio track. Use the same
1621 * name for all tracks. */
1622 for ( ; i < num_audio_tracks; i++)
1624 audio = hb_list_audio_config_item(job->list_audio, i);
1625 audio->out.name = strdup(anames);
1628 /* Audio Track Names */
1632 job->vbitrate = hb_calc_bitrate( job, size );
1633 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1643 for ( i = 0; subtracks[i] != NULL; i++ )
1646 token = subtracks[i];
1647 if( strcasecmp(token, "scan" ) == 0 )
1649 int burn = 0, force = 0, def = 0;
1651 if ( subburn != NULL )
1653 burn = ( pos == 1 && subburn[0] == 0 ) ||
1654 ( strcmp( "scan", subburn ) == 0 );
1656 if ( subdefault != NULL )
1658 def = ( pos == 1 && subdefault[0] == 0 ) ||
1659 ( strcmp( "scan", subdefault ) == 0 );
1661 force = test_sub_list( subforce, "scan", pos );
1663 if ( !burn && mux == HB_MUX_MKV )
1665 job->select_subtitle_config.dest = PASSTHRUSUB;
1675 if ( !( !burn && mux == HB_MUX_MP4 ) )
1677 job->select_subtitle_config.force = force;
1678 job->select_subtitle_config.default_track = def;
1684 hb_subtitle_t * subtitle;
1685 hb_subtitle_config_t sub_config;
1687 int burn = 0, force = 0, def = 0;
1689 track = atoi(token) - 1;
1690 subtitle = hb_list_item(title->list_subtitle, track);
1691 if( subtitle == NULL )
1693 fprintf( stderr, "Could not find subtitle track %d, skipped\n", track );
1696 sub_config = subtitle->config;
1698 if ( subburn != NULL )
1700 burn = ( pos == 1 && subburn[0] == 0 ) ||
1701 ( strcmp( token, subburn ) == 0 );
1703 if ( subdefault != NULL )
1705 def = ( pos == 1 && subdefault[0] == 0 ) ||
1706 ( strcmp( token, subdefault ) == 0 );
1709 force = test_sub_list(subforce, token, pos);
1711 if ( !burn && mux == HB_MUX_MKV &&
1712 subtitle->format == PICTURESUB)
1714 sub_config.dest = PASSTHRUSUB;
1716 else if (!burn && mux == HB_MUX_MP4 &&
1717 subtitle->format == PICTURESUB)
1719 // Skip any non-burned vobsubs when output is mp4
1722 else if ( burn && subtitle->format == PICTURESUB )
1724 // Only allow one subtitle to be burned into video
1732 sub_config.force = force;
1733 sub_config.default_track = def;
1734 hb_subtitle_add( job, &sub_config, track );
1739 if( native_language )
1741 job->native_language = strdup( native_language );
1742 if ( subtracks == NULL )
1745 job->select_subtitle_config.force = 1;
1747 if ( subburn == NULL && mux != HB_MUX_MP4 )
1748 job->select_subtitle_config.force = 1;
1751 job->select_subtitle_config.default_track = 1;
1760 if ( largeFileSize )
1762 job->largeFileSize = 1;
1766 job->mp4_optimize = 1;
1773 job->file = strdup( output );
1782 job->color_matrix = color_matrix;
1785 if( x264opts != NULL && *x264opts != '\0' )
1787 job->x264opts = x264opts;
1789 else /*avoids a bus error crash when options aren't specified*/
1791 job->x264opts = NULL;
1794 job->maxWidth = maxWidth;
1796 job->maxHeight = maxHeight;
1798 if( start_at_preview )
1800 job->start_at_preview = start_at_preview - 1;
1801 job->seek_points = preview_count;
1806 job->pts_to_stop = stop_at_pts;
1812 job->frame_to_stop = stop_at_frame;
1821 * When subtitle scan is enabled do a fast pre-scan job
1822 * which will determine which subtitles to enable, if any.
1826 x264opts_tmp = job->x264opts;
1828 job->x264opts = NULL;
1830 job->indepth_scan = subtitle_scan;
1831 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1832 "subtitles if found for foreign language segments\n");
1835 * Add the pre-scan job
1839 job->x264opts = x264opts_tmp;
1845 * If subtitle_scan is enabled then only turn it on
1846 * for the first pass and then off again for the
1851 job->indepth_scan = 0;
1855 x264opts2 = strdup(x264opts);
1859 * If turbo options have been selected then append them
1860 * to the x264opts now (size includes one ':' and the '\0')
1862 if( turbo_opts_enabled )
1864 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1867 tmp_x264opts = malloc(size * sizeof(char));
1870 snprintf( tmp_x264opts, size, "%s:%s",
1871 x264opts, turbo_opts );
1875 * No x264opts to modify, but apply the turbo options
1876 * anyway as they may be modifying defaults
1878 snprintf( tmp_x264opts, size, "%s",
1881 x264opts = tmp_x264opts;
1883 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1886 job->x264opts = x264opts;
1892 * On the second pass we turn off subtitle scan so that we
1893 * can actually encode using any subtitles that were auto
1894 * selected in the first pass (using the whacky select-subtitle
1895 * attribute of the job).
1897 job->indepth_scan = 0;
1899 job->x264opts = x264opts2;
1906 * Turn on subtitle scan if requested, note that this option
1907 * precludes encoding of any actual subtitles.
1910 job->indepth_scan = 0;
1918 #define p s.param.working
1919 case HB_STATE_WORKING:
1920 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1921 p.job_cur, p.job_count, 100.0 * p.progress );
1922 if( p.seconds > -1 )
1924 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1925 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1926 p.hours, p.minutes, p.seconds );
1932 #define p s.param.muxing
1933 case HB_STATE_MUXING:
1935 if (show_mux_warning)
1937 fprintf( stdout, "\rMuxing: this may take awhile..." );
1939 show_mux_warning = 0;
1945 #define p s.param.workdone
1946 case HB_STATE_WORKDONE:
1947 /* Print error if any, then exit */
1951 fprintf( stderr, "\nRip done!\n" );
1953 case HB_ERROR_CANCELED:
1954 fprintf( stderr, "\nRip canceled.\n" );
1957 fprintf( stderr, "\nRip failed (error %x).\n",
1967 /****************************************************************************
1969 ****************************************************************************/
1970 static volatile int64_t i_die_date = 0;
1971 void SigHandler( int i_signal )
1976 i_die_date = hb_get_date();
1977 fprintf( stderr, "Signal %d received, terminating - do it "
1978 "again in case it gets stuck\n", i_signal );
1980 else if( i_die_date + 500 < hb_get_date() )
1982 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1987 /****************************************************************************
1989 ****************************************************************************/
1990 static void ShowHelp()
1993 FILE* const out = stdout;
1996 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1998 "### General Handbrake Options------------------------------------------------\n\n"
1999 " -h, --help Print help\n"
2000 " -u, --update Check for updates and exit\n"
2001 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2002 " -C, --cpu Set CPU count (default: autodetected)\n"
2003 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2004 " if the preset name has spaces, surround it with\n"
2005 " double quotation marks\n"
2006 " -z, --preset-list See a list of available built-in presets\n"
2007 " --dvdnav Use dvdnav (Experimental)\n"
2010 "### Source Options-----------------------------------------------------------\n\n"
2011 " -i, --input <string> Set input device\n"
2012 " -t, --title <number> Select a title to encode (0 to scan only,\n"
2014 " -L, --longest Select the longest title\n"
2015 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2016 " 1 to 3, or \"3\" for chapter 3 only,\n"
2017 " default: all chapters)\n"
2018 " --angle <number> Select the DVD angle\n"
2019 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2020 " and whether or not they're stored to disk (0 or 1).\n"
2021 " (default: 10:0)\n"
2022 " --start-at-preview <#> Start encoding at a given preview.\n"
2023 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2024 " or pts (on a 90kHz clock)"
2027 "### Destination Options------------------------------------------------------\n\n"
2028 " -o, --output <string> Set output file name\n"
2029 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
2030 " autodetected from file name)\n"
2031 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2032 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2033 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2034 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2035 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2039 "### Video Options------------------------------------------------------------\n\n"
2040 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2041 " (default: ffmpeg)\n"
2042 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2043 " same style as mencoder:\n"
2044 " option1=value1:option2=value2\n"
2045 " -q, --quality <float> Set video quality (0.0..1.0)\n"
2046 " -Q, --cqp Use with -q for CQP instead of CRF\n"
2047 " -S, --size <MB> Set target size\n"
2048 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2049 " -2, --two-pass Use two-pass mode\n"
2050 " -T, --turbo When using 2-pass use the turbo options\n"
2051 " on the first pass to improve speed\n"
2052 " (only works with x264, affects PSNR by about 0.05dB,\n"
2053 " and increases first pass speed two to four times)\n"
2054 " -r, --rate Set video framerate (" );
2055 for( i = 0; i < hb_video_rates_count; i++ )
2057 fprintf( out, hb_video_rates[i].string );
2058 if( i != hb_video_rates_count - 1 )
2059 fprintf( out, "/" );
2062 " Be aware that not specifying a framerate lets\n"
2063 " HandBrake preserve a source's time stamps,\n"
2064 " potentially creating variable framerate video\n"
2065 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2066 " frame rate control. VFR preserves the source\n"
2067 " timing. CFR makes the output constant rate at\n"
2068 " the rate given by the -r flag (or the source's\n"
2069 " average rate if no -r is given). PFR doesn't\n"
2070 " allow the rate to go over the rate specified\n"
2071 " with the -r flag but won't change the source\n"
2072 " timing if it's below that rate.\n"
2073 " If none of these flags are given, the default\n"
2074 " is --cfr when -r is given and --vfr otherwise\n"
2077 "### Audio Options-----------------------------------------------------------\n\n"
2078 " -a, --audio <string> Select audio track(s), separated by commas\n"
2079 " More than one output track can be used for one\n"
2081 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2082 " tracks, default: first one)\n" );
2086 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2087 " ac3 and dts meaning passthrough\n"
2088 " Separated by commas for more than one audio track.\n"
2089 " (default: guessed)\n" );
2092 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2093 " ac3 and dts meaning passthrough\n"
2094 " Separated by commas for more than one audio track.\n"
2095 " (default: guessed)\n" );
2098 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2099 " Separated by commas for more than one audio track.\n"
2100 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2101 " Separated by commas for more than one audio track.\n"
2102 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2103 " -R, --arate Set audio samplerate(s) (" );
2104 for( i = 0; i < hb_audio_rates_count; i++ )
2106 fprintf( out, hb_audio_rates[i].string );
2107 if( i != hb_audio_rates_count - 1 )
2108 fprintf( out, "/" );
2110 fprintf( out, " kHz)\n"
2111 " Separated by commas for more than one audio track.\n"
2112 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2113 " making soft sounds louder. Range is 1.0 to 4.0\n"
2114 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2115 " Separated by commas for more than one audio track.\n"
2116 " -A, --aname <string> Audio track name(s),\n"
2117 " Separated by commas for more than one audio track.\n"
2120 "### Picture Settings---------------------------------------------------------\n\n"
2121 " -w, --width <number> Set picture width\n"
2122 " -l, --height <number> Set picture height\n"
2123 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2124 " -Y, --maxHeight <#> Set maximum height\n"
2125 " -X, --maxWidth <#> Set maximum width\n"
2126 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2127 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2128 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2129 " directly control all parameters.\n"
2130 " --display-width Set the width to scale the actual pixels to\n"
2131 " <number> at playback, for custom anamorphic.\n"
2132 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2133 " when using custom anamorphic\n"
2134 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2136 " (--display-width and --pixel-aspect are mutually\n"
2137 " exclusive and the former will override the latter)\n"
2138 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2139 " custom anamorphic, useful with underscanned sources\n"
2140 " --modulus Set the number you want the scaled pixel dimensions\n"
2141 " <number> to divide cleanly by, for loose and custom\n"
2142 " anamorphic modes (default: 16)\n"
2143 " -M --color-matrix Set the color space signaled by the output\n"
2144 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2145 " default: set by resolution)\n"
2148 "### Filters---------------------------------------------------------\n\n"
2150 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2151 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2153 " <fast/slow/slower>\n"
2154 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2155 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
2156 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2157 " Note: this filter drops duplicate frames to\n"
2158 " restore the pre-telecine framerate, unless you\n"
2159 " specify a constant framerate (--rate 29.97)\n"
2160 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
2161 " -8, --denoise Denoise video with hqdn3d filter\n"
2162 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2164 " <weak/medium/strong>\n"
2165 " -7, --deblock Deblock video with pp7 filter\n"
2166 " <QP:M> (default 5:2)\n"
2167 " -g, --grayscale Grayscale encoding\n"
2170 "### Subtitle Options------------------------------------------------------------\n\n"
2171 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2172 " More than one output track can be used for one\n"
2174 " (\"1,2,3\" for multiple tracks.\n"
2175 " A special track name \"scan\" adds an extra 1st pass.\n"
2176 " This extra pass scans subtitles matching the\n"
2177 " language of the first audio or the language \n"
2178 " selected by --native-language.\n"
2179 " The one that's only used 10 percent of the time\n"
2180 " or less is selected. This should locate subtitles\n"
2181 " for short foreign language segments. Best used in\n"
2182 " conjunction with --subtitle-forced.\n"
2183 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2184 " <string> the subtitle has the forced flag set. May be used in\n"
2185 " conjunction with \"scan\" track to auto-select\n"
2186 " a stream if it contains forced subtitles.\n"
2187 " Separated by commas for more than one audio track.\n"
2188 " (\"1,2,3\" for multiple tracks.\n"
2189 " If \"string\" is omitted, the first trac is forced.\n"
2190 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2191 " <number> If \"number\" is omitted, the first trac is burned.\n"
2192 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2193 " <number> to be displayed upon playback. Settings no default\n"
2194 " means no subtitle will be automatically displayed\n"
2195 " If \"number\" is omitted, the first trac is default.\n"
2196 " -N, --native-language Select subtitles with this language if it does not\n"
2197 " <string> match the Audio language. Provide the language's\n"
2198 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2199 " --native-language may be used in conjunction with\n"
2200 " a subtitle \"scan\", in which case all tracks\n"
2201 " matching native language will be scanned.\n"
2202 " Otherwise --native-language is mutually exclusive\n"
2203 " with --subtitle\n"
2212 /****************************************************************************
2214 ****************************************************************************/
2215 static void ShowPresets()
2217 printf("\n< Apple\n");
2219 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");
2221 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");
2223 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");
2225 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");
2229 printf("\n< Regular\n");
2231 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");
2233 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");
2237 printf("\n< Legacy\n");
2239 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2241 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");
2243 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");
2245 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");
2250 static char** str_split( char *str, char *delem )
2258 if ( str == NULL || delem == NULL || str[0] == 0 )
2260 ret = malloc( sizeof(char*) );
2265 // Find number of elements in the string
2268 while ( ( pos = strstr( pos+1, delem ) ) != NULL )
2270 if ( *(pos+1) != 0 )
2273 ret = calloc( ( count + 1 ), sizeof(char*) );
2275 copy_str = strdup( str );
2276 token = strtok( copy_str, delem );
2279 while( token != NULL && i < count )
2281 ret[i] = strdup( token );
2282 token = strtok(NULL, ",");
2289 /****************************************************************************
2291 ****************************************************************************/
2292 static int ParseOptions( int argc, char ** argv )
2295 #define PREVIEWS 257
2296 #define START_AT_PREVIEW 258
2300 #define DISPLAY_WIDTH 262
2301 #define PIXEL_ASPECT 263
2303 #define KEEP_DISPLAY_ASPECT 265
2304 #define SUB_BURNED 266
2305 #define SUB_DEFAULT 267
2309 static struct option long_options[] =
2311 { "help", no_argument, NULL, 'h' },
2312 { "update", no_argument, NULL, 'u' },
2313 { "verbose", optional_argument, NULL, 'v' },
2314 { "cpu", required_argument, NULL, 'C' },
2315 { "dvdnav", no_argument, NULL, DVDNAV },
2317 { "format", required_argument, NULL, 'f' },
2318 { "input", required_argument, NULL, 'i' },
2319 { "output", required_argument, NULL, 'o' },
2320 { "large-file", no_argument, NULL, '4' },
2321 { "optimize", no_argument, NULL, 'O' },
2322 { "ipod-atom", no_argument, NULL, 'I' },
2324 { "title", required_argument, NULL, 't' },
2325 { "longest", no_argument, NULL, 'L' },
2326 { "chapters", required_argument, NULL, 'c' },
2327 { "angle", required_argument, NULL, ANGLE },
2328 { "markers", optional_argument, NULL, 'm' },
2329 { "audio", required_argument, NULL, 'a' },
2330 { "mixdown", required_argument, NULL, '6' },
2331 { "drc", required_argument, NULL, 'D' },
2332 { "subtitle", required_argument, NULL, 's' },
2333 { "subtitle-forced", optional_argument, NULL, 'F' },
2334 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2335 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2336 { "native-language", required_argument, NULL,'N' },
2338 { "encoder", required_argument, NULL, 'e' },
2339 { "aencoder", required_argument, NULL, 'E' },
2340 { "two-pass", no_argument, NULL, '2' },
2341 { "deinterlace", optional_argument, NULL, 'd' },
2342 { "deblock", optional_argument, NULL, '7' },
2343 { "denoise", optional_argument, NULL, '8' },
2344 { "detelecine", optional_argument, NULL, '9' },
2345 { "decomb", optional_argument, NULL, '5' },
2346 { "grayscale", no_argument, NULL, 'g' },
2347 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2348 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2349 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2350 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2351 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2352 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2353 { "modulus", required_argument, NULL, MODULUS },
2354 { "itu-par", no_argument, &itu_par, 1 },
2355 { "width", required_argument, NULL, 'w' },
2356 { "height", required_argument, NULL, 'l' },
2357 { "crop", required_argument, NULL, 'n' },
2359 { "vb", required_argument, NULL, 'b' },
2360 { "quality", required_argument, NULL, 'q' },
2361 { "size", required_argument, NULL, 'S' },
2362 { "ab", required_argument, NULL, 'B' },
2363 { "rate", required_argument, NULL, 'r' },
2364 { "arate", required_argument, NULL, 'R' },
2365 { "cqp", no_argument, NULL, 'Q' },
2366 { "x264opts", required_argument, NULL, 'x' },
2367 { "turbo", no_argument, NULL, 'T' },
2368 { "maxHeight", required_argument, NULL, 'Y' },
2369 { "maxWidth", required_argument, NULL, 'X' },
2370 { "preset", required_argument, NULL, 'Z' },
2371 { "preset-list", no_argument, NULL, 'z' },
2373 { "aname", required_argument, NULL, 'A' },
2374 { "color-matrix",required_argument, NULL, 'M' },
2375 { "previews", required_argument, NULL, PREVIEWS },
2376 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2377 { "stop-at", required_argument, NULL, STOP_AT },
2378 { "vfr", no_argument, &cfr, 0 },
2379 { "cfr", no_argument, &cfr, 1 },
2380 { "pfr", no_argument, &cfr, 2 },
2384 int option_index = 0;
2387 c = getopt_long( argc, argv,
2388 "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",
2389 long_options, &option_index );
2398 /* option was handled entirely in getopt_long */
2407 if( optarg != NULL )
2409 debug = atoi( optarg );
2417 cpu = atoi( optarg );
2422 preset_name = strdup(optarg);
2432 format = strdup( optarg );
2435 input = strdup( optarg );
2437 char *devName = bsd_name_for_path( input ); // alloc
2440 if( device_is_dvd( devName ))
2443 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2444 sprintf( input, "/dev/%s", devName );
2451 output = strdup( optarg );
2464 titleindex = atoi( optarg );
2472 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2474 chapter_start = start;
2477 else if( sscanf( optarg, "%d", &start ) == 1 )
2479 chapter_start = start;
2480 chapter_end = chapter_start;
2484 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2491 angle = atoi( optarg );
2494 if( optarg != NULL )
2496 marker_file = strdup( optarg );
2498 chapter_markers = 1;
2501 if( optarg != NULL )
2503 atracks = strdup( optarg );
2511 if( optarg != NULL )
2513 mixdowns = strdup( optarg );
2517 if( optarg != NULL )
2519 dynamic_range_compression = strdup( optarg );
2523 subtracks = str_split( optarg, "," );
2526 subtracks = str_split( optarg, "," );
2529 if( optarg != NULL )
2531 subburn = strdup( optarg );
2539 if( optarg != NULL )
2541 subdefault = strdup( optarg );
2549 native_language = strdup( optarg );
2555 if( optarg != NULL )
2557 if (!( strcmp( optarg, "fast" ) ))
2559 deinterlace_opt = "-1";
2561 else if (!( strcmp( optarg, "slow" ) ))
2563 deinterlace_opt = "2";
2565 else if (!( strcmp( optarg, "slower" ) ))
2567 deinterlace_opt = "0";
2571 deinterlace_opt = strdup( optarg );
2577 if( optarg != NULL )
2579 deblock_opt = strdup( optarg );
2584 if( optarg != NULL )
2586 if (!( strcmp( optarg, "weak" ) ))
2588 denoise_opt = "2:1:2:3";
2590 else if (!( strcmp( optarg, "medium" ) ))
2592 denoise_opt = "3:2:2:3";
2594 else if (!( strcmp( optarg, "strong" ) ))
2596 denoise_opt = "7:7:5:5";
2600 denoise_opt = strdup( optarg );
2606 if( optarg != NULL )
2608 detelecine_opt = strdup( optarg );
2613 if( optarg != NULL )
2615 decomb_opt = strdup( optarg );
2623 if( optarg != NULL )
2625 sscanf( optarg, "%i", &display_width );
2629 if( optarg != NULL )
2631 sscanf( optarg, "%i:%i", &par_width, &par_height );
2635 if( optarg != NULL )
2637 sscanf( optarg, "%i", &modulus );
2641 if( !strcasecmp( optarg, "ffmpeg" ) )
2643 vcodec = HB_VCODEC_FFMPEG;
2645 else if( !strcasecmp( optarg, "x264" ) )
2647 vcodec = HB_VCODEC_X264;
2649 else if( !strcasecmp( optarg, "x264b13" ) )
2651 vcodec = HB_VCODEC_X264;
2654 else if( !strcasecmp( optarg, "x264b30" ) )
2656 vcodec = HB_VCODEC_X264;
2659 else if( !strcasecmp( optarg, "theora" ) )
2661 vcodec = HB_VCODEC_THEORA;
2665 fprintf( stderr, "invalid codec (%s)\n", optarg );
2670 if( optarg != NULL )
2672 acodecs = strdup( optarg );
2676 width = atoi( optarg );
2679 height = atoi( optarg );
2684 char * tmp = optarg;
2685 for( i = 0; i < 4; i++ )
2689 crop[i] = strtol( tmp, &tmp, 0 );
2698 for( i = 0; i < hb_video_rates_count; i++ )
2700 if( !strcmp( optarg, hb_video_rates[i].string ) )
2702 vrate = hb_video_rates[i].rate;
2708 fprintf( stderr, "invalid framerate %s\n", optarg );
2710 else if ( cfr == 0 )
2717 if( optarg != NULL )
2719 arates = strdup( optarg );
2723 vbitrate = atoi( optarg );
2726 vquality = atof( optarg );
2729 size = atoi( optarg );
2732 if( optarg != NULL )
2734 abitrates = strdup( optarg );
2741 x264opts = strdup( optarg );
2744 turbo_opts_enabled = 1;
2747 maxHeight = atoi( optarg );
2750 maxWidth = atoi (optarg );
2753 if( optarg != NULL )
2755 anames = strdup( optarg );
2759 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
2761 case START_AT_PREVIEW:
2762 start_at_preview = atoi( optarg );
2765 stop_at_string = strdup( optarg );
2766 stop_at_token = strtok( stop_at_string, ":");
2767 if( !strcmp( stop_at_token, "frame" ) )
2769 stop_at_token = strtok( NULL, ":");
2770 stop_at_frame = atoi(stop_at_token);
2772 else if( !strcmp( stop_at_token, "pts" ) )
2774 stop_at_token = strtok( NULL, ":");
2775 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2777 else if( !strcmp( stop_at_token, "duration" ) )
2779 stop_at_token = strtok( NULL, ":");
2780 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
2781 stop_at_pts *= 90000LL;
2785 if( atoi( optarg ) == 601 )
2787 else if( atoi( optarg ) == 709 )
2791 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2799 static int CheckOptions( int argc, char ** argv )
2806 if( input == NULL || *input == '\0' )
2808 fprintf( stderr, "Missing input device. Run %s --help for "
2809 "syntax.\n", argv[0] );
2814 if( titleindex > 0 )
2816 if( output == NULL || *output == '\0' )
2818 fprintf( stderr, "Missing output file name. Run %s --help "
2819 "for syntax.\n", argv[0] );
2825 char * p = strrchr( output, '.' );
2828 if( p && !strcasecmp( p, ".avi" ) )
2831 default_acodec = HB_ACODEC_LAME;
2833 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2834 !strcasecmp( p, ".m4v" ) ) )
2840 default_acodec = HB_ACODEC_FAAC;
2842 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2843 !strcasecmp( p, ".ogg" ) ) )
2846 default_acodec = HB_ACODEC_VORBIS;
2848 else if( p && !strcasecmp(p, ".mkv" ) )
2851 default_acodec = HB_ACODEC_AC3;
2855 fprintf( stderr, "Output format couldn't be guessed "
2856 "from file name, using default.\n" );
2860 else if( !strcasecmp( format, "avi" ) )
2863 default_acodec = HB_ACODEC_LAME;
2865 else if( !strcasecmp( format, "mp4" ) ||
2866 !strcasecmp( format, "m4v" ) )
2872 default_acodec = HB_ACODEC_FAAC;
2874 else if( !strcasecmp( format, "ogm" ) ||
2875 !strcasecmp( format, "ogg" ) )
2878 default_acodec = HB_ACODEC_VORBIS;
2880 else if( !strcasecmp( format, "mkv" ) )
2883 default_acodec = HB_ACODEC_AC3;
2887 fprintf( stderr, "Invalid output format (%s). Possible "
2888 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2896 static int get_acodec_for_string( char *codec )
2898 if( !strcasecmp( codec, "ac3" ) )
2900 return HB_ACODEC_AC3;
2902 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2904 return HB_ACODEC_DCA;
2906 else if( !strcasecmp( codec, "lame" ) )
2908 return HB_ACODEC_LAME;
2910 else if( !strcasecmp( codec, "faac" ) )
2912 return HB_ACODEC_FAAC;
2914 else if( !strcasecmp( codec, "vorbis") )
2916 return HB_ACODEC_VORBIS;
2919 else if( !strcasecmp( codec, "ca_aac") )
2921 return HB_ACODEC_CA_AAC;
2930 static int is_sample_rate_valid(int rate)
2933 for( i = 0; i < hb_audio_rates_count; i++ )
2935 if (rate == hb_audio_rates[i].rate)
2942 /****************************************************************************
2945 * Returns the BSD device name for the block device that contains the
2946 * passed-in path. Returns NULL on failure.
2947 ****************************************************************************/
2948 static char* bsd_name_for_path(char *path)
2952 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2958 // Get the volume reference number.
2959 FSCatalogInfo catalogInfo;
2960 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2966 FSVolumeRefNum volRefNum = catalogInfo.volume;
2968 // Now let's get the device name
2969 GetVolParmsInfoBuffer volumeParms;
2970 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2976 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
2977 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
2978 if( volumeParms.vMVersion < 4 )
2983 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
2984 if( !volumeParms.vMDeviceID )
2989 return strdup( volumeParms.vMDeviceID );
2992 /****************************************************************************
2995 * Returns whether or not the passed in BSD device represents a DVD, or other
2997 ****************************************************************************/
2998 static int device_is_dvd(char *device)
3000 io_service_t service = get_iokit_service(device);
3001 if( service == IO_OBJECT_NULL )
3005 int result = is_dvd_service(service);
3006 IOObjectRelease(service);
3010 /****************************************************************************
3013 * Returns the IOKit service object for the passed in BSD device name.
3014 ****************************************************************************/
3015 static io_service_t get_iokit_service( char *device )
3017 CFMutableDictionaryRef matchingDict;
3018 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3019 if( matchingDict == NULL )
3021 return IO_OBJECT_NULL;
3023 // Fetch the object with the matching BSD node name. There should only be
3024 // one match, so IOServiceGetMatchingService is used instead of
3025 // IOServiceGetMatchingServices to simplify the code.
3026 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3029 /****************************************************************************
3032 * Returns whether or not the service passed in is a DVD.
3034 * Searches for an IOMedia object that represents the entire (whole) media that
3035 * the volume is on. If the volume is on partitioned media, the whole media
3036 * object will be a parent of the volume's media object. If the media is not
3037 * partitioned, the volume's media object will be the whole media object.
3038 ****************************************************************************/
3039 static int is_dvd_service( io_service_t service )
3041 kern_return_t kernResult;
3044 // Create an iterator across all parents of the service object passed in.
3045 kernResult = IORegistryEntryCreateIterator( service,
3047 kIORegistryIterateRecursively | kIORegistryIterateParents,
3049 if( kernResult != KERN_SUCCESS )
3053 if( iter == IO_OBJECT_NULL )
3058 // A reference on the initial service object is released in the do-while
3059 // loop below, so add a reference to balance.
3060 IOObjectRetain( service );
3065 if( is_whole_media_service( service ) &&
3066 IOObjectConformsTo( service, kIODVDMediaClass) )
3070 IOObjectRelease( service );
3071 } while( !result && (service = IOIteratorNext( iter )) );
3072 IOObjectRelease( iter );
3077 /****************************************************************************
3078 * is_whole_media_service
3080 * Returns whether or not the service passed in is an IOMedia service and
3081 * represents the "whole" media instead of just a partition.
3083 * The whole media object is indicated in the IORegistry by the presence of a
3084 * property with the key "Whole" and value "Yes".
3085 ****************************************************************************/
3086 static is_whole_media_service( io_service_t service )
3090 if( IOObjectConformsTo( service, kIOMediaClass ) )
3092 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3093 CFSTR( kIOMediaWholeKey ),
3094 kCFAllocatorDefault,
3100 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3101 CFRelease( wholeMedia );
3106 #endif // __APPLE_CC__