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
30 #include "libac3/ac3.h"
32 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
33 released under the GPL license. I may reimplement it someday... */
34 typedef struct AC3DecodeState {
35 uint8_t inbuf[4096]; /* input buffer */
43 static int ac3_decode_init(AVCodecContext *avctx)
45 AC3DecodeState *s = avctx->priv_data;
48 s->inbuf_ptr = s->inbuf;
53 stream_samples_t samples;
55 /**** the following two functions comes from ac3dec */
56 static inline int blah (int32_t i)
60 else if (i < 0x43bf8000)
63 return i - 0x43c00000;
66 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
69 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
73 for (i = 0; i < 256; i++) {
74 for (c = 0; c < nchannels; c += 256)
75 s16[j++] = blah (f[i + c]);
83 static int ac3_decode_frame(AVCodecContext *avctx,
84 void *data, int *data_size,
85 uint8_t *buf, int buf_size)
87 AC3DecodeState *s = avctx->priv_data;
90 int sample_rate, bit_rate;
91 short *out_samples = data;
93 static const int ac3_channels[8] = {
94 2, 1, 2, 3, 3, 4, 4, 5
98 while (buf_size > 0) {
99 len = s->inbuf_ptr - s->inbuf;
100 if (s->frame_size == 0) {
101 /* no header seen : find one. We need at least 7 bytes to parse it */
102 len = HEADER_SIZE - len;
105 memcpy(s->inbuf_ptr, buf_ptr, len);
109 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
110 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
112 /* no sync found : move by one byte (inefficient, but simple!) */
113 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
117 /* update codec info */
118 avctx->sample_rate = sample_rate;
119 s->channels = ac3_channels[s->flags & 7];
120 if (s->flags & AC3_LFE)
122 if (avctx->channels == 0)
123 /* No specific number of channel requested */
124 avctx->channels = s->channels;
125 else if (s->channels < avctx->channels) {
126 av_log( avctx, AV_LOG_INFO, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
127 avctx->channels = s->channels;
129 avctx->bit_rate = bit_rate;
132 } else if (len < s->frame_size) {
133 len = s->frame_size - len;
137 memcpy(s->inbuf_ptr, buf_ptr, len);
143 if (avctx->channels == 1)
145 else if (avctx->channels == 2)
148 flags |= AC3_ADJUST_LEVEL;
150 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
152 s->inbuf_ptr = s->inbuf;
156 for (i = 0; i < 6; i++) {
157 if (ac3_block (&s->state))
159 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
161 s->inbuf_ptr = s->inbuf;
163 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
167 return buf_ptr - buf;
170 static int ac3_decode_end(AVCodecContext *s)
175 AVCodec ac3_decoder = {
179 sizeof(AC3DecodeState),