1 /* $Id: encfaac.c,v 1.13 2005/03/03 17:21:57 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. */
11 struct hb_work_private_s
16 unsigned long input_samples;
17 unsigned long output_bytes;
23 int out_discrete_channels;
26 int encfaacInit( hb_work_object_t *, hb_job_t * );
27 int encfaacWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
28 void encfaacClose( hb_work_object_t * );
30 hb_work_object_t hb_encfaac =
33 "AAC encoder (libfaac)",
39 static const int valid_rates[] =
41 22050, 24000, 32000, 44100, 48000, 0
44 static int find_samplerate( int rate )
48 for ( i = 0; valid_rates[i] && rate > valid_rates[i]; ++i )
54 /***********************************************************************
55 * hb_work_encfaac_init
56 ***********************************************************************
58 **********************************************************************/
59 int encfaacInit( hb_work_object_t * w, hb_job_t * job )
61 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
62 hb_audio_t * audio = w->audio;
63 faacEncConfigurationPtr cfg;
71 /* pass the number of channels used into the private work data */
72 pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
74 /* if the sample rate is 'auto' and that has given us an invalid output */
75 /* rate, map it to the next highest output rate or 48K if above the highest. */
76 int rate_index = find_samplerate(audio->config.out.samplerate);
77 if ( audio->config.out.samplerate != valid_rates[rate_index] )
79 int rate = valid_rates[valid_rates[rate_index]? rate_index : rate_index - 1];
80 hb_log( "encfaac changing output samplerate from %d to %d",
81 audio->config.out.samplerate, rate );
82 audio->config.out.samplerate = rate;
84 /* if the new rate is over the max bandwidth per channel limit */
85 /* lower the bandwidth. */
86 double bw = audio->config.out.bitrate * 1000 / pv->out_discrete_channels;
87 if ( bw > (double)rate * (6144./1024.) )
89 int newbr = (double)rate * (6.144/1024.) * pv->out_discrete_channels;
90 hb_log( "encfaac changing output bitrate from %d to %d",
91 audio->config.out.bitrate, newbr );
92 audio->config.out.bitrate = newbr;
96 pv->faac = faacEncOpen( audio->config.out.samplerate, pv->out_discrete_channels,
97 &pv->input_samples, &pv->output_bytes );
98 pv->buf = malloc( pv->input_samples * sizeof( float ) );
99 pv->obuf = malloc( pv->output_bytes );
100 pv->framedur = 90000LL * pv->input_samples /
101 ( audio->config.out.samplerate * pv->out_discrete_channels );
103 cfg = faacEncGetCurrentConfiguration( pv->faac );
104 cfg->mpegVersion = MPEG4;
105 cfg->aacObjectType = LOW;
106 cfg->allowMidside = 1;
108 if (pv->out_discrete_channels == 6) {
109 /* we are preserving 5.1 audio into 6-channel AAC,
110 so indicate that we have an lfe channel */
117 cfg->bitRate = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; /* Per channel */
119 cfg->outputFormat = 0;
120 cfg->inputFormat = FAAC_INPUT_FLOAT;
122 if (audio->config.out.mixdown == HB_AMIXDOWN_6CH && audio->config.in.codec == HB_ACODEC_AC3)
124 /* we are preserving 5.1 AC-3 audio into 6-channel AAC, and need to
125 re-map the output of deca52 into our own mapping - the mapping
126 below is the default mapping expected by QuickTime */
127 /* DTS output from libdca is already in the right mapping for QuickTime */
128 /* This doesn't seem to be correct for VLC on Linux */
129 cfg->channel_map[0] = 2;
130 cfg->channel_map[1] = 1;
131 cfg->channel_map[2] = 3;
132 cfg->channel_map[3] = 4;
133 cfg->channel_map[4] = 5;
134 cfg->channel_map[5] = 0;
137 if( !faacEncSetConfiguration( pv->faac, cfg ) )
139 hb_log( "faacEncSetConfiguration failed" );
144 if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 )
146 hb_log( "faacEncGetDecoderSpecificInfo failed" );
150 memcpy( w->config->aac.bytes, bytes, length );
151 w->config->aac.length = length;
154 pv->list = hb_list_init();
159 /***********************************************************************
161 ***********************************************************************
163 **********************************************************************/
164 void encfaacClose( hb_work_object_t * w )
166 hb_work_private_t * pv = w->private_data;
171 faacEncClose( pv->faac );
185 hb_list_empty( &pv->list );
188 w->private_data = NULL;
192 /***********************************************************************
194 ***********************************************************************
196 **********************************************************************/
197 static hb_buffer_t * Encode( hb_work_object_t * w )
199 hb_work_private_t * pv = w->private_data;
201 if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
208 hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
210 int size = faacEncEncode( pv->faac, (int32_t *)pv->buf, pv->input_samples,
211 pv->obuf, pv->output_bytes );
213 // AAC needs four frames before it can start encoding so we'll get nothing
214 // on the first three calls to the encoder.
217 hb_buffer_t * buf = hb_buffer_init( size );
218 memcpy( buf->data, pv->obuf, size );
220 buf->start = pv->pts;
221 pv->pts += pv->framedur;
223 buf->frametype = HB_FRAME_AUDIO;
229 static hb_buffer_t *Flush( hb_work_object_t *w, hb_buffer_t *bufin )
231 hb_work_private_t *pv = w->private_data;
233 // pad whatever data we have out to four input frames.
234 int nbytes = hb_list_bytes( pv->list );
235 int pad = pv->input_samples * sizeof(float) * 4 - nbytes;
238 hb_buffer_t *tmp = hb_buffer_init( pad );
239 memset( tmp->data, 0, pad );
240 hb_list_add( pv->list, tmp );
243 // There are up to three frames buffered in the encoder plus one
244 // in our list buffer so four calls to Encode should get them all.
245 hb_buffer_t *bufout = NULL, *buf = NULL;
246 while ( hb_list_bytes( pv->list ) >= pv->input_samples * sizeof(float) )
248 hb_buffer_t *b = Encode( w );
251 if ( bufout == NULL )
262 // add the eof marker to the end of our buf chain
270 /***********************************************************************
272 ***********************************************************************
274 **********************************************************************/
275 int encfaacWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
276 hb_buffer_t ** buf_out )
278 hb_work_private_t * pv = w->private_data;
281 if ( (*buf_in)->size <= 0 )
283 // EOF on input. Finish encoding what we have buffered then send
284 // it & the eof downstream.
286 *buf_out = Flush( w, *buf_in );
291 hb_list_add( pv->list, *buf_in );
294 *buf_out = buf = Encode( w );
298 buf->next = Encode( w );