3 * Copyright (c) 2001 Gerard Lantau.
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.
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.
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.
20 #include "liba52/a52.h"
22 #ifdef LIBAVCODEC_A52BIN
24 static const char* liba52name = "liba52.so.0";
28 * liba52 - Copyright (C) Aaron Holtzman
29 * released under the GPL license.
31 typedef struct AC3DecodeState {
32 UINT8 inbuf[4096]; /* input buffer */
41 * virtual method table
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
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);
65 #ifdef LIBAVCODEC_A52BIN
66 static void* dlsymm(void* handle, const char* symbol)
68 void* f = dlsym(handle, symbol);
70 fprintf(stderr, "A52 Decoder - function '%s' can't be resolved\n", symbol);
75 static int a52_decode_init(AVCodecContext *avctx)
77 AC3DecodeState *s = avctx->priv_data;
79 #ifdef LIBAVCODEC_A52BIN
80 s->handle = dlopen(liba52name, RTLD_LAZY);
83 fprintf(stderr, "A52 library %s could not be opened: %s\n", liba52name, dlerror());
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)
98 printf("INITIALIZED\n");
100 /* static linked version */
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;
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;
117 /**** the following two functions comes from a52dec */
118 static inline int blah (int32_t i)
122 else if (i < 0x43bf8000)
124 return i - 0x43c00000;
127 static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
130 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
134 for (i = 0; i < 256; i++) {
135 for (c = 0; c < nchannels; c += 256)
136 s16[j++] = blah (f[i + c]);
142 #define HEADER_SIZE 7
144 static int a52_decode_frame(AVCodecContext *avctx,
145 void *data, int *data_size,
146 UINT8 *buf, int buf_size)
148 AC3DecodeState *s = avctx->priv_data;
151 int sample_rate, bit_rate;
152 short *out_samples = data;
154 static const int ac3_channels[8] = {
155 2, 1, 2, 3, 3, 4, 4, 5
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;
167 memcpy(s->inbuf_ptr, buf_ptr, len);
171 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
172 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
174 /* no sync found : move by one byte (inefficient, but simple!) */
175 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
179 /* update codec info */
180 avctx->sample_rate = sample_rate;
181 s->channels = ac3_channels[s->flags & 7];
182 if (s->flags & A52_LFE)
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;
191 avctx->bit_rate = bit_rate;
194 } else if (len < s->frame_size) {
195 len = s->frame_size - len;
199 memcpy(s->inbuf_ptr, buf_ptr, len);
205 if (avctx->channels == 1)
207 else if (avctx->channels == 2)
210 flags |= A52_ADJUST_LEVEL;
212 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
214 s->inbuf_ptr = s->inbuf;
218 for (i = 0; i < 6; i++) {
219 if (s->a52_block(s->state))
221 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
223 s->inbuf_ptr = s->inbuf;
225 *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
229 return buf_ptr - buf;
232 static int a52_decode_end(AVCodecContext *avctx)
234 AC3DecodeState *s = avctx->priv_data;
235 s->a52_free(s->state);
236 #ifdef LIBAVCODEC_A52BIN
242 AVCodec ac3_decoder = {
246 sizeof(AC3DecodeState),