Statistics
| Branch: | Revision:

ffmpeg / libavcodec / a52dec.c @ 976f969b

History | View | Annotate | Download (7.64 KB)

1
/*
2
 * A52 decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include "avcodec.h"
20
#include "liba52/a52.h"
21

    
22
#ifdef LIBAVCODEC_A52BIN
23
#include <dlfcn.h>
24
static const char* liba52name = "liba52.so.0";
25
#endif
26

    
27
/**
28
 * liba52 - Copyright (C) Aaron Holtzman
29
 * released under the GPL license.
30
 */
31
typedef struct AC3DecodeState {
32
    UINT8 inbuf[4096]; /* input buffer */
33
    UINT8 *inbuf_ptr;
34
    int frame_size;
35
    int flags;
36
    int channels;
37
    a52_state_t* state;
38
    sample_t* samples;
39

    
40
    /*
41
     * virtual method table
42
     *
43
     * using this function table so the liba52 doesn't
44
     * have to be really linked together with ffmpeg
45
     * and might be linked in runtime - this allows binary
46
     * distribution of ffmpeg library which doens't depend
47
     * on liba52 library - but if user has it installed
48
     * it will be used - user might install such library
49
     * separately
50
     */
51
    void* handle;
52
    a52_state_t* (*a52_init)(uint32_t mm_accel);
53
    sample_t* (*a52_samples)(a52_state_t * state);
54
    int (*a52_syncinfo)(uint8_t * buf, int * flags,
55
                          int * sample_rate, int * bit_rate);
56
    int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
57
                       sample_t * level, sample_t bias);
58
    void (*a52_dynrng)(a52_state_t * state,
59
                         sample_t (* call) (sample_t, void *), void * data);
60
    int (*a52_block)(a52_state_t * state);
61
    void (*a52_free)(a52_state_t * state);
62

    
63
} AC3DecodeState;
64

    
65
#ifdef LIBAVCODEC_A52BIN
66
static void* dlsymm(void* handle, const char* symbol)
67
{
68
    void* f = dlsym(handle, symbol);
69
    if (!f)
70
        fprintf(stderr, "A52 Decoder - function '%s' can't be resolved\n", symbol);
71
    return f;
72
}
73
#endif
74

    
75
static int a52_decode_init(AVCodecContext *avctx)
76
{
77
    AC3DecodeState *s = avctx->priv_data;
78

    
79
#ifdef LIBAVCODEC_A52BIN
80
    s->handle = dlopen(liba52name, RTLD_LAZY);
81
    if (!s->handle)
82
    {
83
        fprintf(stderr, "A52 library %s  could not be opened: %s\n", liba52name, dlerror());
84
        return -1;
85
    }
86
    s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
87
    s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
88
    s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
89
    s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
90
    s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
91
    s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
92
    if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
93
        || !s->a52_frame || !s->a52_block || !s->a52_free)
94
    {
95
        dlclose(s->handle);
96
        return -1;
97
    }
98
    printf("INITIALIZED\n");
99
#else
100
    /* static linked version */
101
    s->handle = 0;
102
    s->a52_init = a52_init;
103
    s->a52_samples = a52_samples;
104
    s->a52_syncinfo = a52_syncinfo;
105
    s->a52_frame = a52_frame;
106
    s->a52_block = a52_block;
107
    s->a52_free = a52_free;
108
#endif
109
    s->state = s->a52_init(0); /* later use CPU flags */
110
    s->samples = s->a52_samples(s->state);
111
    s->inbuf_ptr = s->inbuf;
112
    s->frame_size = 0;
113

    
114
    return 0;
115
}
116

    
117
/**** the following two functions comes from a52dec */
118
static inline int blah (int32_t i)
119
{
120
    if (i > 0x43c07fff)
121
        return 32767;
122
    else if (i < 0x43bf8000)
123
        return -32768;
124
    return i - 0x43c00000;
125
}
126

    
127
static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
128
{
129
    int i, j, c;
130
    int32_t * f = (int32_t *) _f;        // XXX assumes IEEE float format
131

    
132
    j = 0;
133
    nchannels *= 256;
134
    for (i = 0; i < 256; i++) {
135
        for (c = 0; c < nchannels; c += 256)
136
            s16[j++] = blah (f[i + c]);
137
    }
138
}
139

    
140
/**** end */
141

    
142
#define HEADER_SIZE 7
143

    
144
static int a52_decode_frame(AVCodecContext *avctx,
145
                            void *data, int *data_size,
146
                            UINT8 *buf, int buf_size)
147
{
148
    AC3DecodeState *s = avctx->priv_data;
149
    UINT8 *buf_ptr;
150
    int flags, i, len;
151
    int sample_rate, bit_rate;
152
    short *out_samples = data;
153
    float level;
154
    static const int ac3_channels[8] = {
155
        2, 1, 2, 3, 3, 4, 4, 5
156
    };
157

    
158
    *data_size = 0;
159
    buf_ptr = buf;
160
    while (buf_size > 0) {
161
        len = s->inbuf_ptr - s->inbuf;
162
        if (s->frame_size == 0) {
163
            /* no header seen : find one. We need at least 7 bytes to parse it */
164
            len = HEADER_SIZE - len;
165
            if (len > buf_size)
166
                len = buf_size;
167
            memcpy(s->inbuf_ptr, buf_ptr, len);
168
            buf_ptr += len;
169
            s->inbuf_ptr += len;
170
            buf_size -= len;
171
            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
172
                len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
173
                if (len == 0) {
174
                    /* no sync found : move by one byte (inefficient, but simple!) */
175
                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
176
                    s->inbuf_ptr--;
177
                } else {
178
                    s->frame_size = len;
179
                    /* update codec info */
180
                    avctx->sample_rate = sample_rate;
181
                    s->channels = ac3_channels[s->flags & 7];
182
                    if (s->flags & A52_LFE)
183
                        s->channels++;
184
                    if (avctx->channels == 0)
185
                        /* No specific number of channel requested */
186
                        avctx->channels = s->channels;
187
                    else if (s->channels < avctx->channels) {
188
                        fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
189
                        avctx->channels = s->channels;
190
                    }
191
                    avctx->bit_rate = bit_rate;
192
                }
193
            }
194
        } else if (len < s->frame_size) {
195
            len = s->frame_size - len;
196
            if (len > buf_size)
197
                len = buf_size;
198

    
199
            memcpy(s->inbuf_ptr, buf_ptr, len);
200
            buf_ptr += len;
201
            s->inbuf_ptr += len;
202
            buf_size -= len;
203
        } else {
204
            flags = s->flags;
205
            if (avctx->channels == 1)
206
                flags = A52_MONO;
207
            else if (avctx->channels == 2)
208
                flags = A52_STEREO;
209
            else
210
                flags |= A52_ADJUST_LEVEL;
211
            level = 1;
212
            if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
213
            fail:
214
                s->inbuf_ptr = s->inbuf;
215
                s->frame_size = 0;
216
                continue;
217
            }
218
            for (i = 0; i < 6; i++) {
219
                if (s->a52_block(s->state))
220
                    goto fail;
221
                float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
222
            }
223
            s->inbuf_ptr = s->inbuf;
224
            s->frame_size = 0;
225
            *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
226
            break;
227
        }
228
    }
229
    return buf_ptr - buf;
230
}
231

    
232
static int a52_decode_end(AVCodecContext *avctx)
233
{
234
    AC3DecodeState *s = avctx->priv_data;
235
    s->a52_free(s->state);
236
#ifdef LIBAVCODEC_A52BIN
237
    dlclose(s->handle);
238
#endif
239
    return 0;
240
}
241

    
242
AVCodec ac3_decoder = {
243
    "ac3",
244
    CODEC_TYPE_AUDIO,
245
    CODEC_ID_AC3,
246
    sizeof(AC3DecodeState),
247
    a52_decode_init,
248
    NULL,
249
    a52_decode_end,
250
    a52_decode_frame,
251
};