3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "libac3/ac3.h"
22 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
23 released under the GPL license. I may reimplement it someday... */
24 typedef struct AC3DecodeState {
25 UINT8 inbuf[4096]; /* input buffer */
33 static int ac3_decode_init(AVCodecContext *avctx)
35 AC3DecodeState *s = avctx->priv_data;
38 s->inbuf_ptr = s->inbuf;
43 stream_samples_t samples;
45 /**** the following two functions comes from ac3dec */
46 static inline int blah (int32_t i)
50 else if (i < 0x43bf8000)
53 return i - 0x43c00000;
56 static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
59 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
63 for (i = 0; i < 256; i++) {
64 for (c = 0; c < nchannels; c += 256)
65 s16[j++] = blah (f[i + c]);
73 static int ac3_decode_frame(AVCodecContext *avctx,
74 void *data, int *data_size,
75 UINT8 *buf, int buf_size)
77 AC3DecodeState *s = avctx->priv_data;
80 int sample_rate, bit_rate;
81 short *out_samples = data;
83 static const int ac3_channels[8] = {
84 2, 1, 2, 3, 3, 4, 4, 5
89 while (buf_size > 0) {
90 len = s->inbuf_ptr - s->inbuf;
91 if (s->frame_size == 0) {
92 /* no header seen : find one. We need at least 7 bytes to parse it */
93 len = HEADER_SIZE - len;
96 memcpy(s->inbuf_ptr, buf_ptr, len);
100 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
101 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
103 /* no sync found : move by one byte (inefficient, but simple!) */
104 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
108 /* update codec info */
109 avctx->sample_rate = sample_rate;
110 s->channels = ac3_channels[s->flags & 7];
111 if (s->flags & AC3_LFE)
113 if (avctx->channels == 0)
114 /* No specific number of channel requested */
115 avctx->channels = s->channels;
116 else if (s->channels < avctx->channels) {
117 fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
118 avctx->channels = s->channels;
120 avctx->bit_rate = bit_rate;
123 } else if (len < s->frame_size) {
124 len = s->frame_size - len;
128 memcpy(s->inbuf_ptr, buf_ptr, len);
134 if (avctx->channels == 1)
136 else if (avctx->channels == 2)
139 flags |= AC3_ADJUST_LEVEL;
141 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
143 s->inbuf_ptr = s->inbuf;
147 for (i = 0; i < 6; i++) {
148 if (ac3_block (&s->state))
150 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
152 s->inbuf_ptr = s->inbuf;
154 *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
158 return buf_ptr - buf;
161 static int ac3_decode_end(AVCodecContext *s)
166 AVCodec ac3_decoder = {
170 sizeof(AC3DecodeState),