00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "avcodec.h"
00028 #include <a52dec/a52.h>
00029
00030 #ifdef CONFIG_LIBA52BIN
00031 #include <dlfcn.h>
00032 static const char* liba52name = "liba52.so.0";
00033 #endif
00034
00035
00036
00037
00038
00039 typedef struct AC3DecodeState {
00040 int flags;
00041 int channels;
00042 a52_state_t* state;
00043 sample_t* samples;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 void* handle;
00057 a52_state_t* (*a52_init)(uint32_t mm_accel);
00058 sample_t* (*a52_samples)(a52_state_t * state);
00059 int (*a52_syncinfo)(uint8_t * buf, int * flags,
00060 int * sample_rate, int * bit_rate);
00061 int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
00062 sample_t * level, sample_t bias);
00063 void (*a52_dynrng)(a52_state_t * state,
00064 sample_t (* call) (sample_t, void *), void * data);
00065 int (*a52_block)(a52_state_t * state);
00066 void (*a52_free)(a52_state_t * state);
00067
00068 } AC3DecodeState;
00069
00070 #ifdef CONFIG_LIBA52BIN
00071 static void* dlsymm(void* handle, const char* symbol)
00072 {
00073 void* f = dlsym(handle, symbol);
00074 if (!f)
00075 av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
00076 return f;
00077 }
00078 #endif
00079
00080 static av_cold int a52_decode_init(AVCodecContext *avctx)
00081 {
00082 AC3DecodeState *s = avctx->priv_data;
00083
00084 #ifdef CONFIG_LIBA52BIN
00085 s->handle = dlopen(liba52name, RTLD_LAZY);
00086 if (!s->handle)
00087 {
00088 av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
00089 return -1;
00090 }
00091 s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
00092 s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
00093 s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
00094 s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
00095 s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
00096 s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
00097 if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
00098 || !s->a52_frame || !s->a52_block || !s->a52_free)
00099 {
00100 dlclose(s->handle);
00101 return -1;
00102 }
00103 #else
00104 s->handle = 0;
00105 s->a52_init = a52_init;
00106 s->a52_samples = a52_samples;
00107 s->a52_syncinfo = a52_syncinfo;
00108 s->a52_frame = a52_frame;
00109 s->a52_block = a52_block;
00110 s->a52_free = a52_free;
00111 #endif
00112 s->state = s->a52_init(0);
00113 s->samples = s->a52_samples(s->state);
00114
00115
00116 if (avctx->channels > 0 && avctx->request_channels > 0 &&
00117 avctx->request_channels < avctx->channels &&
00118 avctx->request_channels <= 2) {
00119 avctx->channels = avctx->request_channels;
00120 }
00121
00122 return 0;
00123 }
00124
00125
00126 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
00127 {
00128 int i, j, c;
00129 int32_t * f = (int32_t *) _f;
00130
00131 j = 0;
00132 nchannels *= 256;
00133 for (i = 0; i < 256; i++) {
00134 for (c = 0; c < nchannels; c += 256)
00135 s16[j++] = av_clip_int16(f[i + c] - 0x43c00000);
00136 }
00137 }
00138
00139
00140
00141 #define HEADER_SIZE 7
00142
00143 static int a52_decode_frame(AVCodecContext *avctx,
00144 void *data, int *data_size,
00145 uint8_t *buf, int buf_size)
00146 {
00147 AC3DecodeState *s = avctx->priv_data;
00148 int flags, i, len;
00149 int sample_rate, bit_rate;
00150 short *out_samples = data;
00151 float level;
00152 static const int ac3_channels[8] = {
00153 2, 1, 2, 3, 3, 4, 4, 5
00154 };
00155
00156 *data_size= 0;
00157
00158 if (buf_size < HEADER_SIZE) {
00159 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes for header\n");
00160 return -1;
00161 }
00162 len = s->a52_syncinfo(buf, &s->flags, &sample_rate, &bit_rate);
00163 if (len == 0) {
00164 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, no sync byte at begin\n");
00165 return -1;
00166 }
00167 if (buf_size < len) {
00168 av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes\n");
00169 return -1;
00170 }
00171
00172 avctx->sample_rate = sample_rate;
00173 s->channels = ac3_channels[s->flags & 7];
00174 if (s->flags & A52_LFE)
00175 s->channels++;
00176 if (avctx->request_channels > 0 &&
00177 avctx->request_channels <= 2 &&
00178 avctx->request_channels < s->channels) {
00179 avctx->channels = avctx->request_channels;
00180 } else {
00181 avctx->channels = s->channels;
00182 }
00183 avctx->bit_rate = bit_rate;
00184 flags = s->flags;
00185 if (avctx->channels == 1)
00186 flags = A52_MONO;
00187 else if (avctx->channels == 2)
00188 flags = A52_STEREO;
00189 else
00190 flags |= A52_ADJUST_LEVEL;
00191 level = 1;
00192 if (s->a52_frame(s->state, buf, &flags, &level, 384)) {
00193 fail:
00194 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
00195 return -1;
00196 }
00197 for (i = 0; i < 6; i++) {
00198 if (s->a52_block(s->state))
00199 goto fail;
00200 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
00201 }
00202 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
00203 return len;
00204 }
00205
00206 static av_cold int a52_decode_end(AVCodecContext *avctx)
00207 {
00208 AC3DecodeState *s = avctx->priv_data;
00209 s->a52_free(s->state);
00210 #ifdef CONFIG_LIBA52BIN
00211 dlclose(s->handle);
00212 #endif
00213 return 0;
00214 }
00215
00216 AVCodec liba52_decoder = {
00217 "liba52",
00218 CODEC_TYPE_AUDIO,
00219 CODEC_ID_AC3,
00220 sizeof(AC3DecodeState),
00221 a52_decode_init,
00222 NULL,
00223 a52_decode_end,
00224 a52_decode_frame,
00225 .long_name = NULL_IF_CONFIG_SMALL("liba52 ATSC A/52 / AC-3"),
00226 };