libfaad.c 10.2 KB
Newer Older
1 2 3
/*
 * Faad decoder
 * Copyright (c) 2003 Zdenek Kabelac.
4
 * Copyright (c) 2004 Thomas Raivio.
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * 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 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 25 26 27 28 29 30 31 32
 */

/**
 * @file faad.c
 * AAC decoder.
 *
 * still a bit unfinished - but it plays something
 */

#include "avcodec.h"
#include "faad.h"

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

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

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

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

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

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


96 97 98 99 100 101 102 103
} FAACContext;

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

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

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

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

146 147 148 149 150 151 152
    return r;
}

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

178 179 180
    if(!s->init){
        unsigned long srate;
        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.\n");
184
            return -1;
185 186 187
        }
        avctx->sample_rate = srate;
        avctx->channels = channels;
188
        channel_setup(avctx);
189 190 191
        s->init = 1;
    }

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

    if (frame_info.error > 0) {
195
        av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
196
               s->faacDecGetErrorMessage(frame_info.error));
197
        return -1;
198
    }
199 200
    if (!avctx->frame_size)
        avctx->frame_size = frame_info.samples/avctx->channels;
201 202 203 204
    frame_info.samples *= s->sample_size;
    memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one

    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 av_cold int faac_decode_end(AVCodecContext *avctx)
213
{
214
    FAACContext *s = avctx->priv_data;
215

216
    s->faacDecClose(s->faac_handle);
217 218 219 220 221

    dlclose(s->handle);
    return 0;
}

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

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

    s->handle = dlopen(libfaadname, RTLD_LAZY);
    if (!s->handle)
    {
233
        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
234
               libfaadname, dlerror());
235 236
        return -1;
    }
237 238 239 240 241 242 243

#define dfaac(a) do {                                                   \
        const char* n = AV_STRINGIFY(faacDec ## a);                     \
        if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) {         \
            err = n;                                                    \
        }                                                               \
    } while(0)
244
#else  /* !CONFIG_LIBFAADBIN */
245
#define dfaac(a)     s->faacDec ## a = faacDec ## a
246
#endif /* CONFIG_LIBFAADBIN */
247

248 249 250 251 252 253 254 255 256 257
    // resolve all needed function calls
    dfaac(Open);
    dfaac(Close);
    dfaac(GetCurrentConfiguration);
    dfaac(SetConfiguration);
    dfaac(Init);
    dfaac(Init2);
    dfaac(Decode);
#ifdef FAAD2_VERSION
    dfaac(GetErrorMessage);
258
#endif
259 260

#undef dfaac
261

262
#ifdef CONFIG_LIBFAADBIN
263 264
    if (err) {
        dlclose(s->handle);
265
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
266
               err, libfaadname);
267 268 269 270 271 272
        return -1;
    }
#endif

    s->faac_handle = s->faacDecOpen();
    if (!s->faac_handle) {
273
        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
274 275 276 277 278 279 280 281
        faac_decode_end(avctx);
        return -1;
    }


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

    if (faac_cfg) {
282 283
        switch (avctx->bits_per_coded_sample) {
        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_coded_sample); break;
284 285
        default:
        case 16:
286
#ifdef FAAD2_VERSION
287
            faac_cfg->outputFormat = FAAD_FMT_16BIT;
288
#endif
289 290 291
            s->sample_size = 2;
            break;
        case 24:
292
#ifdef FAAD2_VERSION
293
            faac_cfg->outputFormat = FAAD_FMT_24BIT;
294
#endif
295 296 297
            s->sample_size = 3;
            break;
        case 32:
298
#ifdef FAAD2_VERSION
299
            faac_cfg->outputFormat = FAAD_FMT_32BIT;
300
#endif
301 302 303
            s->sample_size = 4;
            break;
        }
304

305 306
        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
        faac_cfg->defObjectType = LC;
307 308 309 310 311 312
    }

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

    faac_init_mp4(avctx);

313 314 315
    if(!s->init && avctx->channels > 0)
        channel_setup(avctx);

316
    avctx->sample_fmt = SAMPLE_FMT_S16;
317 318 319
    return 0;
}

320
#define AAC_CODEC(id, name, long_name_) \
321 322 323 324 325 326 327 328 329
AVCodec name ## _decoder = {    \
    #name,                      \
    CODEC_TYPE_AUDIO,           \
    id,                         \
    sizeof(FAACContext),        \
    faac_decode_init,           \
    NULL,                       \
    faac_decode_end,            \
    faac_decode_frame,          \
330
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
331 332 333
}

// FIXME - raw AAC files - maybe just one entry will be enough
334
AAC_CODEC(CODEC_ID_AAC, libfaad, "libfaad AAC (Advanced Audio Codec)");
335 336

#undef AAC_CODEC