3 * Copyright (c) 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "liba52/a52.h"
30 #ifdef CONFIG_LIBA52BIN
32 static const char* liba52name = "liba52.so.0";
36 * liba52 - Copyright (C) Aaron Holtzman
37 * released under the GPL license.
39 typedef struct AC3DecodeState {
40 uint8_t inbuf[4096]; /* input buffer */
49 * virtual method table
51 * using this function table so the liba52 doesn't
52 * have to be really linked together with ffmpeg
53 * and might be linked in runtime - this allows binary
54 * distribution of ffmpeg library which doens't depend
55 * on liba52 library - but if user has it installed
56 * it will be used - user might install such library
60 a52_state_t* (*a52_init)(uint32_t mm_accel);
61 sample_t* (*a52_samples)(a52_state_t * state);
62 int (*a52_syncinfo)(uint8_t * buf, int * flags,
63 int * sample_rate, int * bit_rate);
64 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
65 sample_t * level, sample_t bias);
66 void (*a52_dynrng)(a52_state_t * state,
67 sample_t (* call) (sample_t, void *), void * data);
68 int (*a52_block)(a52_state_t * state);
69 void (*a52_free)(a52_state_t * state);
73 #ifdef CONFIG_LIBA52BIN
74 static void* dlsymm(void* handle, const char* symbol)
76 void* f = dlsym(handle, symbol);
78 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
83 static int a52_decode_init(AVCodecContext *avctx)
85 AC3DecodeState *s = avctx->priv_data;
87 #ifdef CONFIG_LIBA52BIN
88 s->handle = dlopen(liba52name, RTLD_LAZY);
91 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
94 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
95 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
96 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
97 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
98 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
99 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
100 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
101 || !s->a52_frame || !s->a52_block || !s->a52_free)
107 /* static linked version */
109 s->a52_init = a52_init;
110 s->a52_samples = a52_samples;
111 s->a52_syncinfo = a52_syncinfo;
112 s->a52_frame = a52_frame;
113 s->a52_block = a52_block;
114 s->a52_free = a52_free;
116 s->state = s->a52_init(0); /* later use CPU flags */
117 s->samples = s->a52_samples(s->state);
118 s->inbuf_ptr = s->inbuf;
124 /**** the following two functions comes from a52dec */
125 static inline int blah (int32_t i)
129 else if (i < 0x43bf8000)
131 return i - 0x43c00000;
134 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
137 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
141 for (i = 0; i < 256; i++) {
142 for (c = 0; c < nchannels; c += 256)
143 s16[j++] = blah (f[i + c]);
149 #define HEADER_SIZE 7
151 static int a52_decode_frame(AVCodecContext *avctx,
152 void *data, int *data_size,
153 uint8_t *buf, int buf_size)
155 AC3DecodeState *s = avctx->priv_data;
158 int sample_rate, bit_rate;
159 short *out_samples = data;
161 static const int ac3_channels[8] = {
162 2, 1, 2, 3, 3, 4, 4, 5
168 while (buf_size > 0) {
169 len = s->inbuf_ptr - s->inbuf;
170 if (s->frame_size == 0) {
171 /* no header seen : find one. We need at least 7 bytes to parse it */
172 len = HEADER_SIZE - len;
175 memcpy(s->inbuf_ptr, buf_ptr, len);
179 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
180 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
182 /* no sync found : move by one byte (inefficient, but simple!) */
183 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
187 /* update codec info */
188 avctx->sample_rate = sample_rate;
189 s->channels = ac3_channels[s->flags & 7];
190 if (s->flags & A52_LFE)
192 if (avctx->channels == 0)
193 /* No specific number of channel requested */
194 avctx->channels = s->channels;
195 else if (s->channels < avctx->channels) {
196 av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
197 avctx->channels = s->channels;
199 avctx->bit_rate = bit_rate;
202 } else if (len < s->frame_size) {
203 len = s->frame_size - len;
207 memcpy(s->inbuf_ptr, buf_ptr, len);
213 if (avctx->channels == 1)
215 else if (avctx->channels == 2)
218 flags |= A52_ADJUST_LEVEL;
220 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
222 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
223 s->inbuf_ptr = s->inbuf;
227 for (i = 0; i < 6; i++) {
228 if (s->a52_block(s->state))
230 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
232 s->inbuf_ptr = s->inbuf;
234 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
238 return buf_ptr - buf;
241 static int a52_decode_end(AVCodecContext *avctx)
243 AC3DecodeState *s = avctx->priv_data;
244 s->a52_free(s->state);
245 #ifdef CONFIG_LIBA52BIN
251 AVCodec ac3_decoder = {
255 sizeof(AC3DecodeState),