/*********************************************************************** * hb_work_encfaac_init *********************************************************************** * **********************************************************************/ int encfaacInit( hb_work_object_t * w, hb_job_t * job ) { hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) ); hb_audio_t * audio = w->audio; faacEncConfigurationPtr cfg; uint8_t * bytes; unsigned long length; w->private_data = pv; pv->job = job; /* pass the number of channels used into the private work data */ pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown); /* if the sample rate is 'auto' and that has given us an invalid output */ /* rate, map it to the next highest output rate or 48K if above the highest. */ int rate_index = find_samplerate(audio->config.out.samplerate); if ( audio->config.out.samplerate != valid_rates[rate_index] ) { int rate = valid_rates[valid_rates[rate_index]? rate_index : rate_index - 1]; hb_log( "encfaac changing output samplerate from %d to %d", audio->config.out.samplerate, rate ); audio->config.out.samplerate = rate; /* if the new rate is over the max bandwidth per channel limit */ /* lower the bandwidth. */ double bw = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; if ( bw > (double)rate * (6144./1024.) ) { int newbr = (double)rate * (6.144/1024.) * pv->out_discrete_channels; hb_log( "encfaac changing output bitrate from %d to %d", audio->config.out.bitrate, newbr ); audio->config.out.bitrate = newbr; } } pv->faac = faacEncOpen( audio->config.out.samplerate, pv->out_discrete_channels, &pv->input_samples, &pv->output_bytes ); pv->buf = malloc( pv->input_samples * sizeof( float ) ); pv->obuf = malloc( pv->output_bytes ); pv->framedur = 90000.0 * pv->input_samples / ( audio->config.out.samplerate * pv->out_discrete_channels ); audio->config.out.samples_per_frame = pv->input_samples / pv->out_discrete_channels; cfg = faacEncGetCurrentConfiguration( pv->faac ); cfg->mpegVersion = MPEG4; cfg->aacObjectType = LOW; cfg->allowMidside = 1; if (pv->out_discrete_channels == 6) { /* we are preserving 5.1 audio into 6-channel AAC, so indicate that we have an lfe channel */ cfg->useLfe = 1; } else { cfg->useLfe = 0; } cfg->useTns = 0; cfg->bitRate = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; /* Per channel */ cfg->bandWidth = 0; cfg->outputFormat = 0; cfg->inputFormat = FAAC_INPUT_FLOAT; if( ( audio->config.out.mixdown == HB_AMIXDOWN_6CH ) && ( audio->config.in.channel_map != &hb_qt_chan_map ) ) { if( audio->config.in.channel_map == &hb_ac3_chan_map ) { cfg->channel_map[0] = 2; cfg->channel_map[1] = 1; cfg->channel_map[2] = 3; cfg->channel_map[3] = 4; cfg->channel_map[4] = 5; cfg->channel_map[5] = 0; } else if( audio->config.in.channel_map == &hb_smpte_chan_map ) { cfg->channel_map[0] = 2; cfg->channel_map[1] = 0; cfg->channel_map[2] = 1; cfg->channel_map[3] = 4; cfg->channel_map[4] = 5; cfg->channel_map[5] = 3; } } if( !faacEncSetConfiguration( pv->faac, cfg ) ) { hb_log( "faacEncSetConfiguration failed" ); *job->die = 1; return 0; } if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 ) { hb_log( "faacEncGetDecoderSpecificInfo failed" ); *job->die = 1; return 0; } memcpy( w->config->extradata.bytes, bytes, length ); w->config->extradata.length = length; free( bytes ); pv->list = hb_list_init(); return 0; }
/*********************************************************************** * hb_work_encfaac_init *********************************************************************** * **********************************************************************/ int encfaacInit( hb_work_object_t * w, hb_job_t * job ) { hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) ); hb_audio_t * audio = w->audio; faacEncConfigurationPtr cfg; uint8_t * bytes; unsigned long length; w->private_data = pv; pv->job = job; /* pass the number of channels used into the private work data */ pv->out_discrete_channels = hb_mixdown_get_discrete_channel_count(audio->config.out.mixdown); /* if the sample rate is 'auto' and that has given us an invalid output */ /* rate, map it to the next highest output rate or 48K if above the highest. */ int rate_index = find_samplerate(audio->config.out.samplerate); if ( audio->config.out.samplerate != valid_rates[rate_index] ) { int rate = valid_rates[valid_rates[rate_index]? rate_index : rate_index - 1]; hb_log( "encfaac changing output samplerate from %d to %d", audio->config.out.samplerate, rate ); audio->config.out.samplerate = rate; /* if the new rate is over the max bandwidth per channel limit */ /* lower the bandwidth. */ double bw = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; if ( bw > (double)rate * (6144./1024.) ) { int newbr = (double)rate * (6.144/1024.) * pv->out_discrete_channels; hb_log( "encfaac changing output bitrate from %d to %d", audio->config.out.bitrate, newbr ); audio->config.out.bitrate = newbr; } } pv->faac = faacEncOpen( audio->config.out.samplerate, pv->out_discrete_channels, &pv->input_samples, &pv->output_bytes ); pv->buf = malloc( pv->input_samples * sizeof( float ) ); pv->obuf = malloc( pv->output_bytes ); pv->framedur = 90000.0 * pv->input_samples / ( audio->config.out.samplerate * pv->out_discrete_channels ); audio->config.out.samples_per_frame = pv->input_samples / pv->out_discrete_channels; cfg = faacEncGetCurrentConfiguration( pv->faac ); cfg->mpegVersion = MPEG4; cfg->aacObjectType = LOW; cfg->allowMidside = 1; // channel remapping, LFE uint64_t layout; int *remap_table; layout = hb_ff_mixdown_xlat(audio->config.out.mixdown, NULL); remap_table = hb_audio_remap_build_table(layout, &hb_aac_chan_map, audio->config.in.channel_map); if (remap_table != NULL) { // faac does its own remapping memcpy(cfg->channel_map, remap_table, pv->out_discrete_channels * sizeof(int)); free(remap_table); } cfg->useLfe = !!(layout & AV_CH_LOW_FREQUENCY); cfg->useTns = 0; cfg->bitRate = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; /* Per channel */ cfg->bandWidth = 0; cfg->outputFormat = 0; cfg->inputFormat = FAAC_INPUT_FLOAT; if( !faacEncSetConfiguration( pv->faac, cfg ) ) { hb_log( "faacEncSetConfiguration failed" ); *job->die = 1; return 0; } if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 ) { hb_log( "faacEncGetDecoderSpecificInfo failed" ); *job->die = 1; return 0; } memcpy( w->config->extradata.bytes, bytes, length ); w->config->extradata.length = length; free( bytes ); pv->list = hb_list_init(); return 0; }