X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=libhb%2Fcommon.c;h=52ea27f66bdb6ab5707685d1538cc472ddea49d1;hb=033e32de9c380f54c7d1362a3979da205ebc3a29;hp=d0b015505a8cf5196d5b736f66020fed970be034;hpb=817191a61c2a73b4d71a5863ae39e2a27eef98b7;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/libhb/common.c b/libhb/common.c index d0b01550..52ea27f6 100644 --- a/libhb/common.c +++ b/libhb/common.c @@ -1,14 +1,16 @@ /* $Id: common.c,v 1.15 2005/03/17 19:22:47 titer Exp $ This file is part of the HandBrake source code. - Homepage: . + Homepage: . It may be used under the terms of the GNU General Public License. */ #include -#include +#include #include #include "common.h" +#include "lang.h" +#include "hb.h" /********************************************************************** * Global variables @@ -31,17 +33,21 @@ hb_rate_t hb_audio_bitrates[] = { { "32", 32 }, { "40", 40 }, { "48", 48 }, { "56", 56 }, { "64", 64 }, { "80", 80 }, { "96", 96 }, { "112", 112 }, { "128", 128 }, { "160", 160 }, { "192", 192 }, { "224", 224 }, - { "256", 256 }, { "320", 320 }, { "384", 384 } }; + { "256", 256 }, { "320", 320 }, { "384", 384 }, { "448", 448 }, + { "768", 768 } }; int hb_audio_bitrates_count = sizeof( hb_audio_bitrates ) / sizeof( hb_rate_t ); int hb_audio_bitrates_default = 8; /* 128 kbps */ +static hb_error_handler_t *error_handler = NULL; + hb_mixdown_t hb_audio_mixdowns[] = { { "Mono", "HB_AMIXDOWN_MONO", "mono", HB_AMIXDOWN_MONO }, { "Stereo", "HB_AMIXDOWN_STEREO", "stereo", HB_AMIXDOWN_STEREO }, { "Dolby Surround", "HB_AMIXDOWN_DOLBY", "dpl1", HB_AMIXDOWN_DOLBY }, { "Dolby Pro Logic II", "HB_AMIXDOWN_DOLBYPLII", "dpl2", HB_AMIXDOWN_DOLBYPLII }, - { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH } }; + { "6-channel discrete", "HB_AMIXDOWN_6CH", "6ch", HB_AMIXDOWN_6CH } +}; int hb_audio_mixdowns_count = sizeof( hb_audio_mixdowns ) / sizeof( hb_mixdown_t ); @@ -79,18 +85,26 @@ const char * hb_mixdown_get_short_name_from_mixdown( int amixdown ) *********************************************************************/ void hb_reduce( int *x, int *y, int num, int den ) { - int lower = MIN( num, den ); - int i; - *x = num; - *y = den; - for( i = lower - 1; i > 1; --i ) + // find the greatest common divisor of num & den by Euclid's algorithm + int n = num, d = den; + while ( d ) { - if( ( num % i == 0 ) && ( den % i == 0 ) ) - { - *x = num / i; - *y = den / i; - break; - } + int t = d; + d = n % d; + n = t; + } + + // at this point n is the gcd. if it's non-zero remove it from num + // and den. Otherwise just return the original values. + if ( n ) + { + *x = num / n; + *y = den / n; + } + else + { + *x = num; + *y = den; } } @@ -106,6 +120,15 @@ void hb_fix_aspect( hb_job_t * job, int keep ) hb_title_t * title = job->title; int i; + /* don't do anything unless the title has complete size info */ + if ( title->height == 0 || title->width == 0 || title->aspect == 0 ) + { + hb_log( "hb_fix_aspect: incomplete info for title %d: " + "height = %d, width = %d, aspect = %.3f", + title->index, title->height, title->width, title->aspect ); + return; + } + /* Sanity checks: Widths and heights must be multiples of 16 and greater than or equal to 16 @@ -131,22 +154,18 @@ void hb_fix_aspect( hb_job_t * job, int keep ) } } + double par = (double)title->width / ( (double)title->height * title->aspect ); + double cropped_sar = (double)( title->height - job->crop[0] - job->crop[1] ) / + (double)(title->width - job->crop[2] - job->crop[3] ); + double ar = par * cropped_sar; if( keep == HB_KEEP_WIDTH ) { - job->height = MULTIPLE_16( - (uint64_t) job->width * title->width * HB_ASPECT_BASE * - ( title->height - job->crop[0] - job->crop[1] ) / - ( (uint64_t) title->height * title->aspect * - ( title->width - job->crop[2] - job->crop[3] ) ) ); + job->height = MULTIPLE_16( (uint64_t)( (double)job->width * ar ) ); job->height = MAX( 16, job->height ); } else { - job->width = MULTIPLE_16( - (uint64_t) job->height * title->height * title->aspect * - ( title->width - job->crop[2] - job->crop[3] ) / - ( (uint64_t) title->width * HB_ASPECT_BASE * - ( title->height - job->crop[0] - job->crop[1] ) ) ); + job->width = MULTIPLE_16( (uint64_t)( (double)job->height / ar ) ); job->width = MAX( 16, job->width ); } } @@ -175,11 +194,12 @@ int hb_calc_bitrate( hb_job_t * job, int size ) case HB_MUX_MP4: case HB_MUX_PSP: case HB_MUX_IPOD: + case HB_MUX_MKV: overhead = 6; - break; + break; case HB_MUX_AVI: overhead = 24; - break; + break; case HB_MUX_OGM: overhead = 6; break; @@ -187,23 +207,6 @@ int hb_calc_bitrate( hb_job_t * job, int size ) return 0; } - /* How many audio samples we put in each frame */ - switch( job->acodec ) - { - case HB_ACODEC_FAAC: - case HB_ACODEC_VORBIS: - samples_per_frame = 1024; - break; - case HB_ACODEC_LAME: - samples_per_frame = 1152; - break; - case HB_ACODEC_AC3: - samples_per_frame = 1536; - break; - default: - return 0; - } - /* Get the duration in seconds */ length = 0; for( i = job->chapter_start; i <= job->chapter_end; i++ ) @@ -214,28 +217,70 @@ int hb_calc_bitrate( hb_job_t * job, int size ) length += 135000; length /= 90000; + if( size == -1 ) + { + hb_interjob_t * interjob = hb_interjob_get( job->h ); + avail = job->vbitrate * 125 * length; + avail += length * interjob->vrate * overhead / interjob->vrate_base; + } + /* Video overhead */ avail -= length * job->vrate * overhead / job->vrate_base; - for( i = 0; job->audios[i] >= 0; i++ ) + if( size == -1 ) + { + goto ret; + } + + for( i = 0; i < hb_list_count(job->list_audio); i++ ) { /* Audio data */ int abitrate; - if( job->acodec & HB_ACODEC_AC3 ) + audio = hb_list_item( job->list_audio, i); + + /* How many audio samples we put in each frame */ + switch( audio->config.out.codec ) + { + case HB_ACODEC_FAAC: + case HB_ACODEC_CA_AAC: + case HB_ACODEC_VORBIS: + samples_per_frame = 1024; + break; + case HB_ACODEC_LAME: + samples_per_frame = 1152; + break; + case HB_ACODEC_AC3: + case HB_ACODEC_DCA: + samples_per_frame = 1536; + break; + default: + return 0; + } + + if( audio->config.out.codec == HB_ACODEC_AC3 || + audio->config.out.codec == HB_ACODEC_DCA) { - audio = hb_list_item( title->list_audio, job->audios[i] ); - abitrate = audio->bitrate / 8; + /* + * For pass through we take the bitrate from the input audio + * bitrate as we are simply passing it through. + */ + abitrate = audio->config.in.bitrate / 8; } else { - abitrate = job->abitrate * 1000 / 8; + /* + * Where we are transcoding the audio we use the destination + * bitrate. + */ + abitrate = audio->config.out.bitrate * 1000 / 8; } avail -= length * abitrate; - + /* Audio overhead */ - avail -= length * job->arate * overhead / samples_per_frame; + avail -= length * audio->config.out.samplerate * overhead / samples_per_frame; } +ret: if( avail < 0 ) { return 0; @@ -376,7 +421,7 @@ int hb_list_bytes( hb_list_t * l ) ret += buf->size - buf->cur; } - return ret; + return ret; } /********************************************************************** @@ -391,14 +436,14 @@ void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size ) int copied; int copying; int i; - + for( i = 0, copied = 0; copied < size; i++ ) { buf = hb_list_item( l, i ); copying = MIN( buf->size - buf->cur, size - copied ); memcpy( &dst[copied], &buf->data[buf->cur], copying ); copied += copying; - } + } } /********************************************************************** @@ -418,7 +463,7 @@ void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size, int copied; int copying; uint8_t has_pts; - + /* So we won't have to deal with NULL pointers */ uint64_t dummy1, dummy2; @@ -446,7 +491,7 @@ void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size, } copied += copying; - } + } } /********************************************************************** @@ -489,11 +534,11 @@ void hb_list_close( hb_list_t ** _l ) * hb_log ********************************************************************** * If verbose mode is one, print message with timestamp. Messages - * longer than 80 characters are stripped ;p + * longer than 180 characters are stripped ;p *********************************************************************/ void hb_log( char * log, ... ) { - char string[182]; /* 180 chars + \n + \0 */ + char string[362]; /* 360 chars + \n + \0 */ time_t _now; struct tm * now; va_list args; @@ -512,7 +557,45 @@ void hb_log( char * log, ... ) /* Convert the message to a string */ va_start( args, log ); - vsnprintf( string + 11, 169, log, args ); + vsnprintf( string + 11, 349, log, args ); + va_end( args ); + + /* Add the end of line */ + strcat( string, "\n" ); + + /* Print it */ + fprintf( stderr, "%s", string ); +} + +int global_verbosity_level; //Necessary for hb_deep_log +/********************************************************************** + * hb_deep_log + ********************************************************************** + * If verbose mode is >= level, print message with timestamp. Messages + * longer than 360 characters are stripped ;p + *********************************************************************/ +void hb_deep_log( hb_debug_level_t level, char * log, ... ) +{ + char string[362]; /* 360 chars + \n + \0 */ + time_t _now; + struct tm * now; + va_list args; + + if( global_verbosity_level < level ) + { + /* Hiding message */ + return; + } + + /* Get the time */ + _now = time( NULL ); + now = localtime( &_now ); + sprintf( string, "[%02d:%02d:%02d] ", + now->tm_hour, now->tm_min, now->tm_sec ); + + /* Convert the message to a string */ + va_start( args, log ); + vsnprintf( string + 11, 349, log, args ); va_end( args ); /* Add the end of line */ @@ -523,9 +606,40 @@ void hb_log( char * log, ... ) } /********************************************************************** + * hb_error + ********************************************************************** + * Using whatever output is available display this error. + *********************************************************************/ +void hb_error( char * log, ... ) +{ + char string[181]; /* 180 chars + \0 */ + va_list args; + + /* Convert the message to a string */ + va_start( args, log ); + vsnprintf( string, 180, log, args ); + va_end( args ); + + /* + * Got the error in a single string, send it off to be dispatched. + */ + if( error_handler ) + { + error_handler( string ); + } else { + hb_log( string ); + } +} + +void hb_register_error_handler( hb_error_handler_t * handler ) +{ + error_handler = handler; +} + +/********************************************************************** * hb_title_init ********************************************************************** - * + * *********************************************************************/ hb_title_t * hb_title_init( char * dvd, int index ) { @@ -538,6 +652,9 @@ hb_title_t * hb_title_init( char * dvd, int index ) t->list_chapter = hb_list_init(); t->list_subtitle = hb_list_init(); strcat( t->dvd, dvd ); + // default to decoding mpeg2 + t->video_id = 0xE0; + t->video_codec = WORK_DECMPEG2; return t; } @@ -545,7 +662,7 @@ hb_title_t * hb_title_init( char * dvd, int index ) /********************************************************************** * hb_title_close ********************************************************************** - * + * *********************************************************************/ void hb_title_close( hb_title_t ** _t ) { @@ -560,14 +677,14 @@ void hb_title_close( hb_title_t ** _t ) free( audio ); } hb_list_close( &t->list_audio ); - + while( ( chapter = hb_list_item( t->list_chapter, 0 ) ) ) { hb_list_rem( t->list_chapter, chapter ); free( chapter ); } hb_list_close( &t->list_chapter ); - + while( ( subtitle = hb_list_item( t->list_subtitle, 0 ) ) ) { hb_list_rem( t->list_subtitle, subtitle ); @@ -575,7 +692,211 @@ void hb_title_close( hb_title_t ** _t ) } hb_list_close( &t->list_subtitle ); + if( t->metadata ) + { + if( t->metadata->coverart ) + { + free( t->metadata->coverart ); + } + free( t->metadata ); + } + free( t ); *_t = NULL; } +/********************************************************************** + * hb_filter_close + ********************************************************************** + * + *********************************************************************/ +void hb_filter_close( hb_filter_object_t ** _f ) +{ + hb_filter_object_t * f = *_f; + + f->close( f->private_data ); + + if( f->name ) + free( f->name ); + if( f->settings ) + free( f->settings ); + + free( f ); + *_f = NULL; +} + +/********************************************************************** + * hb_audio_copy + ********************************************************************** + * + *********************************************************************/ +hb_audio_t *hb_audio_copy(const hb_audio_t *src) +{ + hb_audio_t *audio = NULL; + + if( src ) + { + audio = calloc(1, sizeof(*audio)); + memcpy(audio, src, sizeof(*audio)); + } + return audio; +} + +/********************************************************************** + * hb_audio_new + ********************************************************************** + * + *********************************************************************/ +void hb_audio_config_init(hb_audio_config_t * audiocfg) +{ + /* Set read only paramaters to invalid values */ + audiocfg->in.codec = 0xDEADBEEF; + audiocfg->in.bitrate = -1; + audiocfg->in.samplerate = -1; + audiocfg->in.channel_layout = 0; + audiocfg->in.version = 0; + audiocfg->in.mode = 0; + audiocfg->flags.ac3 = 0; + audiocfg->lang.description[0] = 0; + audiocfg->lang.simple[0] = 0; + audiocfg->lang.iso639_2[0] = 0; + + /* Initalize some sensable defaults */ + audiocfg->in.track = audiocfg->out.track = 0; + audiocfg->out.codec = HB_ACODEC_FAAC; + audiocfg->out.bitrate = 128; + audiocfg->out.samplerate = 44100; + audiocfg->out.mixdown = HB_AMIXDOWN_DOLBYPLII; + audiocfg->out.dynamic_range_compression = 0; + audiocfg->out.name = NULL; +} + +/********************************************************************** + * hb_audio_add + ********************************************************************** + * + *********************************************************************/ +int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg) +{ + hb_title_t *title = job->title; + hb_audio_t *audio; + + audio = hb_audio_copy( hb_list_item( title->list_audio, audiocfg->in.track ) ); + if( audio == NULL ) + { + /* We fail! */ + return 0; + } + + if( (audiocfg->in.bitrate != -1) && (audiocfg->in.codec != 0xDEADBEEF) ) + { + /* This most likely means the client didn't call hb_audio_config_init + * so bail. + */ + return 0; + } + + /* Really shouldn't ignore the passed out track, but there is currently no + * way to handle duplicates or out-of-order track numbers. + */ + audio->config.out.track = hb_list_count(job->list_audio) + 1; + audio->config.out.codec = audiocfg->out.codec; + if( audiocfg->out.codec == audio->config.in.codec ) + { + /* Pass-through, copy from input. */ + audio->config.out.samplerate = audio->config.in.samplerate; + audio->config.out.bitrate = audio->config.in.bitrate; + audio->config.out.dynamic_range_compression = 0; + audio->config.out.mixdown = 0; + } + else + { + /* Non pass-through, use what is given. */ + audio->config.out.samplerate = audiocfg->out.samplerate; + audio->config.out.bitrate = audiocfg->out.bitrate; + audio->config.out.dynamic_range_compression = audiocfg->out.dynamic_range_compression; + audio->config.out.mixdown = audiocfg->out.mixdown; + } + + hb_list_add(job->list_audio, audio); + return 1; +} + +hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i) +{ + hb_audio_t *audio = NULL; + + if( (audio = hb_list_item(list, i)) ) + return &(audio->config); + + return NULL; +} + +/********************************************************************** + * hb_subtitle_copy + ********************************************************************** + * + *********************************************************************/ +hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src) +{ + hb_subtitle_t *subtitle = NULL; + + if( src ) + { + subtitle = calloc(1, sizeof(*subtitle)); + memcpy(subtitle, src, sizeof(*subtitle)); + } + return subtitle; +} + +/********************************************************************** + * hb_subtitle_add + ********************************************************************** + * + *********************************************************************/ +int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track) +{ + hb_title_t *title = job->title; + hb_subtitle_t *subtitle; + + subtitle = hb_subtitle_copy( hb_list_item( title->list_subtitle, track ) ); + if( subtitle == NULL ) + { + /* We fail! */ + return 0; + } + subtitle->config = *subtitlecfg; + hb_list_add(job->list_subtitle, subtitle); + return 1; +} + +int hb_srt_add( const hb_job_t * job, + const hb_subtitle_config_t * subtitlecfg, + const char *lang ) +{ + hb_subtitle_t *subtitle; + iso639_lang_t *language = NULL; + int retval = 0; + + subtitle = calloc( 1, sizeof( *subtitle ) ); + + subtitle->id = (hb_list_count(job->list_subtitle) << 8) | 0xFF; + subtitle->format = TEXTSUB; + subtitle->source = SRTSUB; + + language = lang_for_code2( lang ); + + if( language ) + { + + strcpy( subtitle->lang, language->eng_name ); + strncpy( subtitle->iso639_2, lang, 4 ); + + subtitle->config = *subtitlecfg; + subtitle->config.dest = PASSTHRUSUB; + + hb_list_add(job->list_subtitle, subtitle); + retval = 1; + } + return retval; +}