2
* A52 decoder using liba52
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
24
* A52 decoder using liba52
28
#include <a52dec/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 {
46
* virtual method table
48
* using this function table so the liba52 doesn't
49
* have to be really linked together with ffmpeg
50
* and might be linked in runtime - this allows binary
51
* distribution of ffmpeg library which doens't depend
52
* on liba52 library - but if user has it installed
53
* it will be used - user might install such library
57
a52_state_t* (*a52_init)(uint32_t mm_accel);
58
sample_t* (*a52_samples)(a52_state_t * state);
59
int (*a52_syncinfo)(uint8_t * buf, int * flags,
60
int * sample_rate, int * bit_rate);
61
int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
62
sample_t * level, sample_t bias);
63
void (*a52_dynrng)(a52_state_t * state,
64
sample_t (* call) (sample_t, void *), void * data);
65
int (*a52_block)(a52_state_t * state);
66
void (*a52_free)(a52_state_t * state);
70
#ifdef CONFIG_LIBA52BIN
71
static void* dlsymm(void* handle, const char* symbol)
73
void* f = dlsym(handle, symbol);
75
av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
80
static av_cold int a52_decode_init(AVCodecContext *avctx)
82
AC3DecodeState *s = avctx->priv_data;
84
#ifdef CONFIG_LIBA52BIN
85
s->handle = dlopen(liba52name, RTLD_LAZY);
88
av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
91
s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
92
s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
93
s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
94
s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
95
s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
96
s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
97
if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
98
|| !s->a52_frame || !s->a52_block || !s->a52_free)
105
s->a52_init = a52_init;
106
s->a52_samples = a52_samples;
107
s->a52_syncinfo = a52_syncinfo;
108
s->a52_frame = a52_frame;
109
s->a52_block = a52_block;
110
s->a52_free = a52_free;
112
s->state = s->a52_init(0); /* later use CPU flags */
113
s->samples = s->a52_samples(s->state);
115
/* allow downmixing to stereo or mono */
116
if (avctx->channels > 0 && avctx->request_channels > 0 &&
117
avctx->request_channels < avctx->channels &&
118
avctx->request_channels <= 2) {
119
avctx->channels = avctx->request_channels;
125
/**** the following function comes from a52dec */
126
static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
129
int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
133
for (i = 0; i < 256; i++) {
134
for (c = 0; c < nchannels; c += 256)
135
s16[j++] = av_clip_int16(f[i + c] - 0x43c00000);
141
#define HEADER_SIZE 7
143
static int a52_decode_frame(AVCodecContext *avctx,
144
void *data, int *data_size,
145
uint8_t *buf, int buf_size)
147
AC3DecodeState *s = avctx->priv_data;
149
int sample_rate, bit_rate;
150
short *out_samples = data;
152
static const int ac3_channels[8] = {
153
2, 1, 2, 3, 3, 4, 4, 5
158
if (buf_size < HEADER_SIZE) {
159
av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes for header\n");
162
len = s->a52_syncinfo(buf, &s->flags, &sample_rate, &bit_rate);
164
av_log(avctx, AV_LOG_ERROR, "Error decoding frame, no sync byte at begin\n");
167
if (buf_size < len) {
168
av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes\n");
171
/* update codec info */
172
avctx->sample_rate = sample_rate;
173
s->channels = ac3_channels[s->flags & 7];
174
if (s->flags & A52_LFE)
176
if (avctx->request_channels > 0 &&
177
avctx->request_channels <= 2 &&
178
avctx->request_channels < s->channels) {
179
avctx->channels = avctx->request_channels;
181
avctx->channels = s->channels;
183
avctx->bit_rate = bit_rate;
185
if (avctx->channels == 1)
187
else if (avctx->channels == 2)
190
flags |= A52_ADJUST_LEVEL;
192
if (s->a52_frame(s->state, buf, &flags, &level, 384)) {
194
av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
197
for (i = 0; i < 6; i++) {
198
if (s->a52_block(s->state))
200
float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
202
*data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
206
static av_cold int a52_decode_end(AVCodecContext *avctx)
208
AC3DecodeState *s = avctx->priv_data;
209
s->a52_free(s->state);
210
#ifdef CONFIG_LIBA52BIN
216
AVCodec liba52_decoder = {
220
sizeof(AC3DecodeState),
225
.long_name = "liba52 ATSC A/52 / AC-3",