- w->name = strdup( "AAC encoder (libfaac)" );
- w->work = Work;
- w->close = Close;
-
- w->job = job;
- w->audio = audio;
-
- w->faac = faacEncOpen( job->arate, 2, &w->input_samples,
- &w->output_bytes );
- w->buf = malloc( w->input_samples * sizeof( float ) );
-
- cfg = faacEncGetCurrentConfiguration( w->faac );
+ 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 = 90000LL * pv->input_samples /
+ ( audio->config.out.samplerate * pv->out_discrete_channels );
+
+ cfg = faacEncGetCurrentConfiguration( pv->faac );