OSDN Git Service

import 0.9.3
[handbrake-jp/handbrake-jp.git] / libhb / encfaac.c
1 /* $Id: encfaac.c,v 1.13 2005/03/03 17:21:57 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 #include "faac.h"
10
11 struct hb_work_private_s
12 {
13     hb_job_t   * job;
14
15     faacEncHandle * faac;
16     unsigned long   input_samples;
17     unsigned long   output_bytes;
18     uint8_t       * buf;
19     uint8_t       * obuf;
20     hb_list_t     * list;
21     int64_t         pts;
22     int64_t         framedur;
23         int             out_discrete_channels;
24 };
25
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 * );
29
30 hb_work_object_t hb_encfaac =
31 {
32     WORK_ENCFAAC,
33     "AAC encoder (libfaac)",
34     encfaacInit,
35     encfaacWork,
36     encfaacClose
37 };
38
39 static const int valid_rates[] =
40 {
41     22050, 24000, 32000, 44100, 48000, 0
42 };
43
44 static int find_samplerate( int rate )
45 {
46     int i;
47
48     for ( i = 0; valid_rates[i] && rate > valid_rates[i]; ++i )
49     {
50     }
51     return i;
52 }
53
54 /***********************************************************************
55  * hb_work_encfaac_init
56  ***********************************************************************
57  *
58  **********************************************************************/
59 int encfaacInit( hb_work_object_t * w, hb_job_t * job )
60 {
61     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
62     hb_audio_t * audio = w->audio;
63     faacEncConfigurationPtr cfg;
64     uint8_t * bytes;
65     unsigned long length;
66
67     w->private_data = pv;
68
69     pv->job   = job;
70
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);
73
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] )
78     {
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;
83
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.) )
88         {
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;
93         }
94     }
95
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 );
102
103     cfg                = faacEncGetCurrentConfiguration( pv->faac );
104     cfg->mpegVersion   = MPEG4;
105     cfg->aacObjectType = LOW;
106     cfg->allowMidside  = 1;
107
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 */
111                 cfg->useLfe    = 1;
112         } else {
113                 cfg->useLfe    = 0;
114         }
115
116     cfg->useTns        = 0;
117     cfg->bitRate       = audio->config.out.bitrate * 1000 / pv->out_discrete_channels; /* Per channel */
118     cfg->bandWidth     = 0;
119     cfg->outputFormat  = 0;
120     cfg->inputFormat   =  FAAC_INPUT_FLOAT;
121
122     if (audio->config.out.mixdown == HB_AMIXDOWN_6CH && audio->config.in.codec == HB_ACODEC_AC3)
123     {
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;
135         }
136
137     if( !faacEncSetConfiguration( pv->faac, cfg ) )
138     {
139         hb_log( "faacEncSetConfiguration failed" );
140         *job->die = 1;
141         return 0;
142     }
143
144     if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 )
145     {
146         hb_log( "faacEncGetDecoderSpecificInfo failed" );
147         *job->die = 1;
148         return 0;
149     }
150     memcpy( w->config->aac.bytes, bytes, length );
151     w->config->aac.length = length;
152     free( bytes );
153
154     pv->list = hb_list_init();
155
156     return 0;
157 }
158
159 /***********************************************************************
160  * Close
161  ***********************************************************************
162  *
163  **********************************************************************/
164 void encfaacClose( hb_work_object_t * w )
165 {
166     hb_work_private_t * pv = w->private_data;
167     if ( pv )
168     {
169         if ( pv->faac )
170         {
171             faacEncClose( pv->faac );
172             pv->faac = NULL;
173         }
174         if ( pv->buf )
175         {
176             free( pv->buf );
177             pv->buf = NULL;
178         }
179         if ( pv->obuf )
180         {
181             free( pv->obuf );
182             pv->obuf = NULL;
183         }
184         if ( pv->list )
185             hb_list_empty( &pv->list );
186
187         free( pv );
188         w->private_data = NULL;
189     }
190 }
191
192 /***********************************************************************
193  * Encode
194  ***********************************************************************
195  *
196  **********************************************************************/
197 static hb_buffer_t * Encode( hb_work_object_t * w )
198 {
199     hb_work_private_t * pv = w->private_data;
200
201     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
202     {
203         /* Need more data */
204         return NULL;
205     }
206
207     uint64_t pts, pos;
208     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
209                       &pts, &pos );
210     int size = faacEncEncode( pv->faac, (int32_t *)pv->buf, pv->input_samples,
211                               pv->obuf, pv->output_bytes );
212
213     // AAC needs four frames before it can start encoding so we'll get nothing
214     // on the first three calls to the encoder.
215     if ( size > 0 )
216     {
217         hb_buffer_t * buf = hb_buffer_init( size );
218         memcpy( buf->data, pv->obuf, size );
219         buf->size = size;
220         buf->start = pv->pts;
221         pv->pts += pv->framedur;
222         buf->stop = pv->pts;
223         buf->frametype   = HB_FRAME_AUDIO;
224         return buf;
225     }
226     return NULL;
227 }
228
229 static hb_buffer_t *Flush( hb_work_object_t *w, hb_buffer_t *bufin )
230 {
231     hb_work_private_t *pv = w->private_data;
232
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;
236     if ( pad > 0 )
237     {
238         hb_buffer_t *tmp = hb_buffer_init( pad );
239         memset( tmp->data, 0, pad );
240         hb_list_add( pv->list, tmp );
241     }
242
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) )
247     {
248         hb_buffer_t *b = Encode( w );
249         if ( b )
250         {
251             if ( bufout == NULL )
252             {
253                 bufout = b;
254             }
255             else
256             {
257                 buf->next = b;
258             }
259             buf = b;
260         }
261     }
262     // add the eof marker to the end of our buf chain
263     if ( buf )
264         buf->next = bufin;
265     else
266         bufout = bufin;
267     return bufout;
268 }
269
270 /***********************************************************************
271  * Work
272  ***********************************************************************
273  *
274  **********************************************************************/
275 int encfaacWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
276                  hb_buffer_t ** buf_out )
277 {
278     hb_work_private_t * pv = w->private_data;
279     hb_buffer_t * buf;
280
281     if ( (*buf_in)->size <= 0 )
282     {
283         // EOF on input. Finish encoding what we have buffered then send
284         // it & the eof downstream.
285
286         *buf_out = Flush( w, *buf_in );
287         *buf_in = NULL;
288         return HB_WORK_DONE;
289     }
290
291     hb_list_add( pv->list, *buf_in );
292     *buf_in = NULL;
293
294     *buf_out = buf = Encode( w );
295
296     while( buf )
297     {
298         buf->next = Encode( w );
299         buf       = buf->next;
300     }
301
302     return HB_WORK_OK;
303 }
304