00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "avcodec.h"
00026 #include "liba52/a52.h"
00027
00028 #ifdef CONFIG_A52BIN
00029 #include <dlfcn.h>
00030 static const char* liba52name = "liba52.so.0";
00031 #endif
00032
00037 typedef struct AC3DecodeState {
00038 uint8_t inbuf[4096];
00039 uint8_t *inbuf_ptr;
00040 int frame_size;
00041 int flags;
00042 int channels;
00043 a52_state_t* state;
00044 sample_t* samples;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 void* handle;
00058 a52_state_t* (*a52_init)(uint32_t mm_accel);
00059 sample_t* (*a52_samples)(a52_state_t * state);
00060 int (*a52_syncinfo)(uint8_t * buf, int * flags,
00061 int * sample_rate, int * bit_rate);
00062 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
00063 sample_t * level, sample_t bias);
00064 void (*a52_dynrng)(a52_state_t * state,
00065 sample_t (* call) (sample_t, void *), void * data);
00066 int (*a52_block)(a52_state_t * state);
00067 void (*a52_free)(a52_state_t * state);
00068
00069 } AC3DecodeState;
00070
00071 #ifdef CONFIG_A52BIN
00072 static void* dlsymm(void* handle, const char* symbol)
00073 {
00074 void* f = dlsym(handle, symbol);
00075 if (!f)
00076 fprintf(stderr, "A52 Decoder - function '%s' can't be resolved\n", symbol);
00077 return f;
00078 }
00079 #endif
00080
00081 static int a52_decode_init(AVCodecContext *avctx)
00082 {
00083 AC3DecodeState *s = avctx->priv_data;
00084
00085 #ifdef CONFIG_A52BIN
00086 s->handle = dlopen(liba52name, RTLD_LAZY);
00087 if (!s->handle)
00088 {
00089 fprintf(stderr, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
00090 return -1;
00091 }
00092 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
00093 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
00094 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
00095 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
00096 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
00097 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
00098 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
00099 || !s->a52_frame || !s->a52_block || !s->a52_free)
00100 {
00101 dlclose(s->handle);
00102 return -1;
00103 }
00104 #else
00105
00106 s->handle = 0;
00107 s->a52_init = a52_init;
00108 s->a52_samples = a52_samples;
00109 s->a52_syncinfo = a52_syncinfo;
00110 s->a52_frame = a52_frame;
00111 s->a52_block = a52_block;
00112 s->a52_free = a52_free;
00113 #endif
00114 s->state = s->a52_init(0);
00115 s->samples = s->a52_samples(s->state);
00116 s->inbuf_ptr = s->inbuf;
00117 s->frame_size = 0;
00118
00119 return 0;
00120 }
00121
00122
00123 static inline int blah (int32_t i)
00124 {
00125 if (i > 0x43c07fff)
00126 return 32767;
00127 else if (i < 0x43bf8000)
00128 return -32768;
00129 return i - 0x43c00000;
00130 }
00131
00132 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
00133 {
00134 int i, j, c;
00135 int32_t * f = (int32_t *) _f;
00136
00137 j = 0;
00138 nchannels *= 256;
00139 for (i = 0; i < 256; i++) {
00140 for (c = 0; c < nchannels; c += 256)
00141 s16[j++] = blah (f[i + c]);
00142 }
00143 }
00144
00145
00146
00147 #define HEADER_SIZE 7
00148
00149 static int a52_decode_frame(AVCodecContext *avctx,
00150 void *data, int *data_size,
00151 uint8_t *buf, int buf_size)
00152 {
00153 AC3DecodeState *s = avctx->priv_data;
00154 uint8_t *buf_ptr;
00155 int flags, i, len;
00156 int sample_rate, bit_rate;
00157 short *out_samples = data;
00158 float level;
00159 static const int ac3_channels[8] = {
00160 2, 1, 2, 3, 3, 4, 4, 5
00161 };
00162
00163 buf_ptr = buf;
00164 while (buf_size > 0) {
00165 len = s->inbuf_ptr - s->inbuf;
00166 if (s->frame_size == 0) {
00167
00168 len = HEADER_SIZE - len;
00169 if (len > buf_size)
00170 len = buf_size;
00171 memcpy(s->inbuf_ptr, buf_ptr, len);
00172 buf_ptr += len;
00173 s->inbuf_ptr += len;
00174 buf_size -= len;
00175 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
00176 len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
00177 if (len == 0) {
00178
00179 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
00180 s->inbuf_ptr--;
00181 } else {
00182 s->frame_size = len;
00183
00184 avctx->sample_rate = sample_rate;
00185 s->channels = ac3_channels[s->flags & 7];
00186 if (s->flags & A52_LFE)
00187 s->channels++;
00188 if (avctx->channels == 0)
00189
00190 avctx->channels = s->channels;
00191 else if (s->channels < avctx->channels) {
00192 av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
00193 avctx->channels = s->channels;
00194 }
00195 avctx->bit_rate = bit_rate;
00196 }
00197 }
00198 } else if (len < s->frame_size) {
00199 len = s->frame_size - len;
00200 if (len > buf_size)
00201 len = buf_size;
00202
00203 memcpy(s->inbuf_ptr, buf_ptr, len);
00204 buf_ptr += len;
00205 s->inbuf_ptr += len;
00206 buf_size -= len;
00207 } else {
00208 flags = s->flags;
00209 if (avctx->channels == 1)
00210 flags = A52_MONO;
00211 else if (avctx->channels == 2)
00212 flags = A52_STEREO;
00213 else
00214 flags |= A52_ADJUST_LEVEL;
00215 level = 1;
00216 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
00217 fail:
00218 s->inbuf_ptr = s->inbuf;
00219 s->frame_size = 0;
00220 continue;
00221 }
00222 for (i = 0; i < 6; i++) {
00223 if (s->a52_block(s->state))
00224 goto fail;
00225 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
00226 }
00227 s->inbuf_ptr = s->inbuf;
00228 s->frame_size = 0;
00229 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
00230 break;
00231 }
00232 }
00233 return buf_ptr - buf;
00234 }
00235
00236 static int a52_decode_end(AVCodecContext *avctx)
00237 {
00238 AC3DecodeState *s = avctx->priv_data;
00239 s->a52_free(s->state);
00240 #ifdef CONFIG_A52BIN
00241 dlclose(s->handle);
00242 #endif
00243 return 0;
00244 }
00245
00246 AVCodec ac3_decoder = {
00247 "ac3",
00248 CODEC_TYPE_AUDIO,
00249 CODEC_ID_AC3,
00250 sizeof(AC3DecodeState),
00251 a52_decode_init,
00252 NULL,
00253 a52_decode_end,
00254 a52_decode_frame,
00255 };