Statistics
| Branch: | Revision:

ffmpeg / libavcodec / libfaad.c @ 0f44edaa

History | View | Annotate | Download (11.5 KB)

1
/*
2
 * Faad decoder
3
 * Copyright (c) 2003 Zdenek Kabelac.
4
 * Copyright (c) 2004 Thomas Raivio.
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file faad.c
25
 * AAC decoder.
26
 *
27
 * still a bit unfinished - but it plays something
28
 */
29

    
30
#include "avcodec.h"
31
#include "faad.h"
32

    
33
#ifndef FAADAPI
34
#define FAADAPI
35
#endif
36

    
37
/*
38
 * when CONFIG_LIBFAADBIN is defined the libfaad will be opened at runtime
39
 */
40
//#undef CONFIG_LIBFAADBIN
41
//#define CONFIG_LIBFAADBIN
42

    
43
#ifdef CONFIG_LIBFAADBIN
44
#include <dlfcn.h>
45
static const char* libfaadname = "libfaad.so";
46
#else
47
#define dlopen(a)
48
#define dlclose(a)
49
#endif
50

    
51
typedef struct {
52
    void* handle;               /* dlopen handle */
53
    void* faac_handle;          /* FAAD library handle */
54
    int sample_size;
55
    int init;
56

    
57
    /* faad calls */
58
    faacDecHandle FAADAPI (*faacDecOpen)(void);
59
    faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
60
#ifndef FAAD2_VERSION
61
    int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
62
                                           faacDecConfigurationPtr config);
63
    int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
64
                               unsigned char *buffer,
65
                               unsigned long *samplerate,
66
                               unsigned long *channels);
67
    int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
68
                                unsigned long SizeOfDecoderSpecificInfo,
69
                                unsigned long *samplerate, unsigned long *channels);
70
    int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
71
                                 unsigned char *buffer,
72
                                 unsigned long *bytesconsumed,
73
                                 short *sample_buffer,
74
                                 unsigned long *samples);
75
#else
76
    unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
77
                                                     faacDecConfigurationPtr config);
78
    long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
79
                                unsigned char *buffer,
80
                                unsigned long buffer_size,
81
                                unsigned long *samplerate,
82
                                unsigned char *channels);
83
    char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
84
                                 unsigned long SizeOfDecoderSpecificInfo,
85
                                 unsigned long *samplerate, unsigned char *channels);
86
    void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
87
                                   faacDecFrameInfo *hInfo,
88
                                   unsigned char *buffer,
89
                                   unsigned long buffer_size);
90
    char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
91
#endif
92

    
93
    void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
94

    
95

    
96
} FAACContext;
97

    
98
static const unsigned long faac_srates[] =
99
{
100
    96000, 88200, 64000, 48000, 44100, 32000,
101
    24000, 22050, 16000, 12000, 11025, 8000
102
};
103

    
104
static void channel_setup(AVCodecContext *avctx)
105
{
106
#ifdef FAAD2_VERSION
107
    FAACContext *s = avctx->priv_data;
108
    if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
109
        avctx->request_channels < avctx->channels) {
110
        faacDecConfigurationPtr faac_cfg;
111
        avctx->channels = 2;
112
        faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
113
        faac_cfg->downMatrix = 1;
114
        s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
115
    }
116
#endif
117
}
118

    
119
static int faac_init_mp4(AVCodecContext *avctx)
120
{
121
    FAACContext *s = avctx->priv_data;
122
    unsigned long samplerate;
123
#ifndef FAAD2_VERSION
124
    unsigned long channels;
125
#else
126
    unsigned char channels;
127
#endif
128
    int r = 0;
129

    
130
    if (avctx->extradata){
131
        r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
132
                            avctx->extradata_size,
133
                            &samplerate, &channels);
134
        if (r < 0){
135
            av_log(avctx, AV_LOG_ERROR,
136
                   "faacDecInit2 failed r:%d   sr:%ld  ch:%ld  s:%d\n",
137
                   r, samplerate, (long)channels, avctx->extradata_size);
138
        } else {
139
            avctx->sample_rate = samplerate;
140
            avctx->channels = channels;
141
            channel_setup(avctx);
142
            s->init = 1;
143
        }
144
    }
145

    
146
    return r;
147
}
148

    
149
static int faac_decode_frame(AVCodecContext *avctx,
150
                             void *data, int *data_size,
151
                             uint8_t *buf, int buf_size)
152
{
153
    FAACContext *s = avctx->priv_data;
154
#ifndef FAAD2_VERSION
155
    unsigned long bytesconsumed;
156
    short *sample_buffer = NULL;
157
    unsigned long samples;
158
    int out;
159
#else
160
    faacDecFrameInfo frame_info;
161
    void *out;
162
#endif
163
    if(buf_size == 0)
164
        return 0;
165
#ifndef FAAD2_VERSION
166
    out = s->faacDecDecode(s->faac_handle,
167
                           (unsigned char*)buf,
168
                           &bytesconsumed,
169
                           data,
170
                           &samples);
171
    samples *= s->sample_size;
172
    if (data_size)
173
        *data_size = samples;
174
    return (buf_size < (int)bytesconsumed)
175
        ? buf_size : (int)bytesconsumed;
176
#else
177

    
178
    if(!s->init){
179
        unsigned long srate;
180
        unsigned char channels;
181
        int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
182
        if(r < 0){
183
            av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
184
                   s->faacDecGetErrorMessage(frame_info.error));
185
            return -1;
186
        }
187
        avctx->sample_rate = srate;
188
        avctx->channels = channels;
189
        channel_setup(avctx);
190
        s->init = 1;
191
    }
192

    
193
    out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
194

    
195
    if (frame_info.error > 0) {
196
        av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
197
               s->faacDecGetErrorMessage(frame_info.error));
198
        return -1;
199
    }
200

    
201
    frame_info.samples *= s->sample_size;
202
    memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
203

    
204
    if (data_size)
205
        *data_size = frame_info.samples;
206

    
207
    return (buf_size < (int)frame_info.bytesconsumed)
208
        ? buf_size : (int)frame_info.bytesconsumed;
209
#endif
210
}
211

    
212
static int faac_decode_end(AVCodecContext *avctx)
213
{
214
    FAACContext *s = avctx->priv_data;
215

    
216
    s->faacDecClose(s->faac_handle);
217

    
218
    dlclose(s->handle);
219
    return 0;
220
}
221

    
222
static int faac_decode_init(AVCodecContext *avctx)
223
{
224
    FAACContext *s = avctx->priv_data;
225
    faacDecConfigurationPtr faac_cfg;
226

    
227
#ifdef CONFIG_LIBFAADBIN
228
    const char* err = 0;
229

    
230
    s->handle = dlopen(libfaadname, RTLD_LAZY);
231
    if (!s->handle)
232
    {
233
        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
234
               libfaadname, dlerror());
235
        return -1;
236
    }
237
#define dfaac(a, b)                                                     \
238
    do { static const char* n = AV_STRINGIFY(faacDec ## a);             \
239
        if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
240
    for(;;) {
241
#else  /* !CONFIG_LIBFAADBIN */
242
#define dfaac(a, b)     s->faacDec ## a = faacDec ## a
243
#endif /* CONFIG_LIBFAADBIN */
244

    
245
        // resolve all needed function calls
246
        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
247
        dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
248
        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
249
                                        FAADAPI (*)(faacDecHandle)));
250
#ifndef FAAD2_VERSION
251
        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
252
                                                 faacDecConfigurationPtr)));
253

    
254
        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
255
                                     unsigned long*, unsigned long*)));
256
        dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
257
                                      unsigned long, unsigned long*,
258
                                      unsigned long*)));
259
        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
260
                                       unsigned long*, short*, unsigned long*)));
261
#else
262
        dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
263
                                                           faacDecConfigurationPtr)));
264
        dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
265
                                      unsigned long, unsigned long*, unsigned char*)));
266
        dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
267
                                       unsigned long, unsigned long*,
268
                                       unsigned char*)));
269
        dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
270
                                         unsigned char*, unsigned long)));
271
        dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
272
#endif
273
#undef dfacc
274

    
275
#ifdef CONFIG_LIBFAADBIN
276
        break;
277
    }
278
    if (err) {
279
        dlclose(s->handle);
280
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
281
               err, libfaadname);
282
        return -1;
283
    }
284
#endif
285

    
286
    s->faac_handle = s->faacDecOpen();
287
    if (!s->faac_handle) {
288
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
289
        faac_decode_end(avctx);
290
        return -1;
291
    }
292

    
293

    
294
    faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
295

    
296
    if (faac_cfg) {
297
        switch (avctx->bits_per_sample) {
298
        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
299
        default:
300
        case 16:
301
#ifdef FAAD2_VERSION
302
            faac_cfg->outputFormat = FAAD_FMT_16BIT;
303
#endif
304
            s->sample_size = 2;
305
            break;
306
        case 24:
307
#ifdef FAAD2_VERSION
308
            faac_cfg->outputFormat = FAAD_FMT_24BIT;
309
#endif
310
            s->sample_size = 3;
311
            break;
312
        case 32:
313
#ifdef FAAD2_VERSION
314
            faac_cfg->outputFormat = FAAD_FMT_32BIT;
315
#endif
316
            s->sample_size = 4;
317
            break;
318
        }
319

    
320
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
321
        faac_cfg->defObjectType = LC;
322
    }
323

    
324
    s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
325

    
326
    faac_init_mp4(avctx);
327

    
328
    if(!s->init && avctx->channels > 0)
329
        channel_setup(avctx);
330

    
331
    return 0;
332
}
333

    
334
#define AAC_CODEC(id, name)     \
335
AVCodec name ## _decoder = {    \
336
    #name,                      \
337
    CODEC_TYPE_AUDIO,           \
338
    id,                         \
339
    sizeof(FAACContext),        \
340
    faac_decode_init,           \
341
    NULL,                       \
342
    faac_decode_end,            \
343
    faac_decode_frame,          \
344
}
345

    
346
// FIXME - raw AAC files - maybe just one entry will be enough
347
AAC_CODEC(CODEC_ID_AAC, libfaad);
348
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
349
// If it's mp4 file - usually embeded into Qt Mov
350
AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
351
#endif
352

    
353
#undef AAC_CODEC