00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025
00026
00027 #include "avcodec.h"
00028 #include "libac3/ac3.h"
00029
00030
00031
00032 typedef struct AC3DecodeState {
00033 uint8_t inbuf[4096];
00034 uint8_t *inbuf_ptr;
00035 int frame_size;
00036 int flags;
00037 int channels;
00038 ac3_state_t state;
00039 } AC3DecodeState;
00040
00041 static int ac3_decode_init(AVCodecContext *avctx)
00042 {
00043 AC3DecodeState *s = avctx->priv_data;
00044
00045 ac3_init ();
00046 s->inbuf_ptr = s->inbuf;
00047 s->frame_size = 0;
00048 return 0;
00049 }
00050
00051 stream_samples_t samples;
00052
00053
00054 static inline int blah (int32_t i)
00055 {
00056 if (i > 0x43c07fff)
00057 return 32767;
00058 else if (i < 0x43bf8000)
00059 return -32768;
00060 else
00061 return i - 0x43c00000;
00062 }
00063
00064 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
00065 {
00066 int i, j, c;
00067 int32_t * f = (int32_t *) _f;
00068
00069 j = 0;
00070 nchannels *= 256;
00071 for (i = 0; i < 256; i++) {
00072 for (c = 0; c < nchannels; c += 256)
00073 s16[j++] = blah (f[i + c]);
00074 }
00075 }
00076
00077
00078
00079 #define HEADER_SIZE 7
00080
00081 static int ac3_decode_frame(AVCodecContext *avctx,
00082 void *data, int *data_size,
00083 uint8_t *buf, int buf_size)
00084 {
00085 AC3DecodeState *s = avctx->priv_data;
00086 uint8_t *buf_ptr;
00087 int flags, i, len;
00088 int sample_rate, bit_rate;
00089 short *out_samples = data;
00090 float level;
00091 static const int ac3_channels[8] = {
00092 2, 1, 2, 3, 3, 4, 4, 5
00093 };
00094
00095 buf_ptr = buf;
00096 while (buf_size > 0) {
00097 len = s->inbuf_ptr - s->inbuf;
00098 if (s->frame_size == 0) {
00099
00100 len = HEADER_SIZE - len;
00101 if (len > buf_size)
00102 len = buf_size;
00103 memcpy(s->inbuf_ptr, buf_ptr, len);
00104 buf_ptr += len;
00105 s->inbuf_ptr += len;
00106 buf_size -= len;
00107 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
00108 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
00109 if (len == 0) {
00110
00111 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
00112 s->inbuf_ptr--;
00113 } else {
00114 s->frame_size = len;
00115
00116 avctx->sample_rate = sample_rate;
00117 s->channels = ac3_channels[s->flags & 7];
00118 if (s->flags & AC3_LFE)
00119 s->channels++;
00120 if (avctx->channels == 0)
00121
00122 avctx->channels = s->channels;
00123 else if (s->channels < avctx->channels) {
00124 fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
00125 avctx->channels = s->channels;
00126 }
00127 avctx->bit_rate = bit_rate;
00128 }
00129 }
00130 } else if (len < s->frame_size) {
00131 len = s->frame_size - len;
00132 if (len > buf_size)
00133 len = buf_size;
00134
00135 memcpy(s->inbuf_ptr, buf_ptr, len);
00136 buf_ptr += len;
00137 s->inbuf_ptr += len;
00138 buf_size -= len;
00139 } else {
00140 flags = s->flags;
00141 if (avctx->channels == 1)
00142 flags = AC3_MONO;
00143 else if (avctx->channels == 2)
00144 flags = AC3_STEREO;
00145 else
00146 flags |= AC3_ADJUST_LEVEL;
00147 level = 1;
00148 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
00149 fail:
00150 s->inbuf_ptr = s->inbuf;
00151 s->frame_size = 0;
00152 continue;
00153 }
00154 for (i = 0; i < 6; i++) {
00155 if (ac3_block (&s->state))
00156 goto fail;
00157 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
00158 }
00159 s->inbuf_ptr = s->inbuf;
00160 s->frame_size = 0;
00161 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
00162 break;
00163 }
00164 }
00165 return buf_ptr - buf;
00166 }
00167
00168 static int ac3_decode_end(AVCodecContext *s)
00169 {
00170 return 0;
00171 }
00172
00173 AVCodec ac3_decoder = {
00174 "ac3",
00175 CODEC_TYPE_AUDIO,
00176 CODEC_ID_AC3,
00177 sizeof(AC3DecodeState),
00178 ac3_decode_init,
00179 NULL,
00180 ac3_decode_end,
00181 ac3_decode_frame,
00182 };