~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to extras/ffmpeg/libavcodec/faad.c

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Faad decoder
 
3
 * Copyright (c) 2003 Zdenek Kabelac.
 
4
 * Copyright (c) 2004 Thomas Raivio.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file faad.c
 
23
 * AAC decoder.
 
24
 *
 
25
 * still a bit unfinished - but it plays something
 
26
 */
 
27
 
 
28
#include "avcodec.h"
 
29
#include "faad.h"
 
30
 
 
31
/*
 
32
 * when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
 
33
 */
 
34
//#undef CONFIG_FAADBIN
 
35
//#define CONFIG_FAADBIN
 
36
 
 
37
#ifdef CONFIG_FAADBIN
 
38
#include <dlfcn.h>
 
39
static const char* libfaadname = "libfaad.so.0";
 
40
#else
 
41
#define dlopen(a)
 
42
#define dlclose(a)
 
43
#endif
 
44
 
 
45
typedef struct {
 
46
    void* handle;               /* dlopen handle */
 
47
    void* faac_handle;          /* FAAD library handle */
 
48
    int frame_size;
 
49
    int sample_size;
 
50
    int flags;
 
51
 
 
52
    /* faad calls */
 
53
    faacDecHandle FAADAPI (*faacDecOpen)(void);
 
54
    faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
 
55
#ifndef FAAD2_VERSION
 
56
        int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
 
57
                                           faacDecConfigurationPtr config);
 
58
        int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
 
59
                                unsigned char *buffer,
 
60
                                unsigned long *samplerate,
 
61
                                unsigned long *channels);
 
62
        int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
 
63
                                unsigned long SizeOfDecoderSpecificInfo,
 
64
                                unsigned long *samplerate, unsigned long *channels);
 
65
        int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
 
66
                                unsigned char *buffer,
 
67
                                unsigned long *bytesconsumed,
 
68
                                short *sample_buffer,
 
69
                                unsigned long *samples);
 
70
#else
 
71
        unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
 
72
                                                     faacDecConfigurationPtr config);
 
73
        long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
 
74
                                 unsigned char *buffer,
 
75
                                 unsigned long buffer_size,
 
76
                                 unsigned long *samplerate,
 
77
                                 unsigned char *channels);
 
78
        char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
 
79
                                 unsigned long SizeOfDecoderSpecificInfo,
 
80
                                 unsigned long *samplerate, unsigned char *channels);
 
81
        void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
 
82
                                         faacDecFrameInfo *hInfo,
 
83
                                         unsigned char *buffer,
 
84
                                                                 unsigned long buffer_size);
 
85
        unsigned char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
 
86
#endif
 
87
    
 
88
    void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
 
89
    
 
90
    
 
91
} FAACContext;
 
92
 
 
93
static const unsigned long faac_srates[] =
 
94
{
 
95
    96000, 88200, 64000, 48000, 44100, 32000,
 
96
    24000, 22050, 16000, 12000, 11025, 8000
 
97
};
 
98
 
 
99
static int faac_init_mp4(AVCodecContext *avctx)
 
100
{
 
101
    FAACContext *s = (FAACContext *) avctx->priv_data;
 
102
    unsigned long samplerate;
 
103
#ifndef FAAD2_VERSION
 
104
    unsigned long channels;
 
105
#else
 
106
    unsigned char channels;
 
107
#endif
 
108
    int r = 0;
 
109
 
 
110
    if (avctx->extradata)
 
111
        r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
 
112
                            avctx->extradata_size,
 
113
                            &samplerate, &channels);
 
114
    // else r = s->faacDecInit(s->faac_handle ... );
 
115
 
 
116
    if (r < 0)
 
117
        av_log(avctx, AV_LOG_ERROR, "faacDecInit2 failed r:%d   sr:%ld  ch:%ld  s:%d\n",
 
118
                r, samplerate, (long)channels, avctx->extradata_size);
 
119
    return r;
 
120
}
 
121
 
 
122
static int faac_init_aac(AVCodecContext *avctx)
 
123
{
 
124
    return 0;
 
125
}
 
126
 
 
127
static int faac_decode_frame(AVCodecContext *avctx,
 
128
                             void *data, int *data_size,
 
129
                             uint8_t *buf, int buf_size)
 
130
{
 
131
    FAACContext *s = (FAACContext *) avctx->priv_data;
 
132
#ifndef FAAD2_VERSION
 
133
    unsigned long bytesconsumed;
 
134
    short *sample_buffer = NULL;
 
135
    unsigned long samples;
 
136
    int out;
 
137
#else
 
138
    faacDecFrameInfo frame_info;
 
139
    void *out;
 
140
#endif
 
141
    if(buf_size == 0)
 
142
        return 0;
 
143
#ifndef FAAD2_VERSION
 
144
    out = s->faacDecDecode(s->faac_handle, 
 
145
                           (unsigned char*)buf, 
 
146
                           &bytesconsumed, 
 
147
                           data, 
 
148
                           &samples);
 
149
    samples *= s->sample_size;
 
150
    if (data_size)
 
151
        *data_size = samples;
 
152
    return (buf_size < (int)bytesconsumed)
 
153
        ? buf_size : (int)bytesconsumed;
 
154
#else
 
155
        
 
156
    out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
 
157
 
 
158
    if (frame_info.error > 0) {
 
159
        av_log(avctx, AV_LOG_ERROR, "faac: frame decodinf failed: %s\n",
 
160
                s->faacDecGetErrorMessage(frame_info.error));
 
161
        return 0;
 
162
    }
 
163
 
 
164
    frame_info.samples *= s->sample_size;
 
165
    memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
 
166
 
 
167
    if (data_size)
 
168
        *data_size = frame_info.samples;
 
169
 
 
170
    return (buf_size < (int)frame_info.bytesconsumed)
 
171
        ? buf_size : (int)frame_info.bytesconsumed;
 
172
#endif
 
173
}
 
174
 
 
175
static int faac_decode_end(AVCodecContext *avctx)
 
176
{
 
177
    FAACContext *s = (FAACContext *) avctx->priv_data;
 
178
 
 
179
    if (s->faacDecClose)
 
180
        s->faacDecClose(s->faac_handle);
 
181
 
 
182
    dlclose(s->handle);
 
183
    return 0;
 
184
}
 
185
 
 
186
static int faac_decode_init(AVCodecContext *avctx)
 
187
{
 
188
    FAACContext *s = (FAACContext *) avctx->priv_data;
 
189
    faacDecConfigurationPtr faac_cfg;
 
190
 
 
191
#ifdef CONFIG_FAADBIN
 
192
    const char* err = 0;
 
193
 
 
194
    s->handle = dlopen(libfaadname, RTLD_LAZY);
 
195
    if (!s->handle)
 
196
    {
 
197
        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
 
198
                libfaadname, dlerror());
 
199
        return -1;
 
200
    }
 
201
#define dfaac(a, b) \
 
202
    do { static const char* n = "faacDec" #a; \
 
203
    if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
 
204
    for(;;) {
 
205
#else  /* !CONFIG_FAADBIN */
 
206
#define dfaac(a, b)     s->faacDec ## a = faacDec ## a
 
207
#endif /* CONFIG_FAADBIN */
 
208
 
 
209
        // resolve all needed function calls
 
210
        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
 
211
        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
 
212
                                        FAADAPI (*)(faacDecHandle)));
 
213
#ifndef FAAD2_VERSION
 
214
        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
 
215
                                                           faacDecConfigurationPtr)));
 
216
 
 
217
        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
 
218
                                     unsigned long*, unsigned long*)));
 
219
    dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
 
220
                                       unsigned long, unsigned long*,
 
221
                                       unsigned long*)));
 
222
    dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
 
223
        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
 
224
                             unsigned long*, short*, unsigned long*)));
 
225
#else
 
226
        dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
 
227
                                                           faacDecConfigurationPtr)));
 
228
        dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
 
229
                                     unsigned long, unsigned long*, unsigned char*)));
 
230
        dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
 
231
                                       unsigned long, unsigned long*,
 
232
                                       unsigned char*)));
 
233
        dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
 
234
                             unsigned char*, unsigned long)));
 
235
        dfaac(GetErrorMessage, (unsigned char* FAADAPI (*)(unsigned char)));
 
236
#endif
 
237
#undef dfacc
 
238
 
 
239
#ifdef CONFIG_FAADBIN
 
240
        break;
 
241
    }
 
242
    if (err) {
 
243
        dlclose(s->handle);
 
244
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
 
245
                err, libfaadname);
 
246
        return -1;
 
247
    }
 
248
#endif
 
249
 
 
250
    s->faac_handle = s->faacDecOpen();
 
251
    if (!s->faac_handle) {
 
252
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
 
253
        faac_decode_end(avctx);
 
254
        return -1;
 
255
    }
 
256
 
 
257
 
 
258
    faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
 
259
 
 
260
    if (faac_cfg) {
 
261
        switch (avctx->bits_per_sample) {
 
262
        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
 
263
        default:
 
264
        case 16:
 
265
#ifdef FAAD2_VERSION
 
266
            faac_cfg->outputFormat = FAAD_FMT_16BIT;
 
267
#endif
 
268
            s->sample_size = 2;
 
269
            break;
 
270
        case 24:
 
271
#ifdef FAAD2_VERSION
 
272
            faac_cfg->outputFormat = FAAD_FMT_24BIT;
 
273
#endif
 
274
            s->sample_size = 3;
 
275
            break;
 
276
        case 32:
 
277
#ifdef FAAD2_VERSION
 
278
            faac_cfg->outputFormat = FAAD_FMT_32BIT;
 
279
#endif
 
280
            s->sample_size = 4;
 
281
            break;
 
282
        }
 
283
 
 
284
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
 
285
        faac_cfg->defObjectType = LC;
 
286
    }
 
287
 
 
288
    s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
 
289
 
 
290
    faac_init_mp4(avctx);
 
291
 
 
292
    return 0;
 
293
}
 
294
 
 
295
#define AAC_CODEC(id, name)     \
 
296
AVCodec name ## _decoder = {    \
 
297
    #name,                      \
 
298
    CODEC_TYPE_AUDIO,           \
 
299
    id,                         \
 
300
    sizeof(FAACContext),        \
 
301
    faac_decode_init,           \
 
302
    NULL,                       \
 
303
    faac_decode_end,            \
 
304
    faac_decode_frame,          \
 
305
}
 
306
 
 
307
// FIXME - raw AAC files - maybe just one entry will be enough
 
308
AAC_CODEC(CODEC_ID_AAC, aac);
 
309
// If it's mp4 file - usually embeded into Qt Mov
 
310
AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
 
311
 
 
312
#undef AAC_CODEC