added CONFIG_AC3, CONFIG_MPGLIB, CONFIG_DECODERS and CONFIG_ENCODERS (Arpi: don't...
[libav.git] / libavcodec / ac3dec.c
CommitLineData
de6d9b64
FB
1/*
2 * AC3 decoder
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include "avcodec.h"
23
24#include <inttypes.h>
25#include "libac3/ac3.h"
26
27/* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
28 released under the GPL license. I may reimplement it someday... */
29typedef struct AC3DecodeState {
30 UINT8 inbuf[4096]; /* input buffer */
31 UINT8 *inbuf_ptr;
32 int frame_size;
33 int flags;
34 ac3_state_t state;
35} AC3DecodeState;
36
37static int ac3_decode_init(AVCodecContext *avctx)
38{
39 AC3DecodeState *s = avctx->priv_data;
40
41 ac3_init ();
42 s->inbuf_ptr = s->inbuf;
43 s->frame_size = 0;
44 return 0;
45}
46
47stream_samples_t samples;
48
49/**** the following two functions comes from ac3dec */
50static inline int blah (int32_t i)
51{
52 if (i > 0x43c07fff)
53 return 32767;
54 else if (i < 0x43bf8000)
55 return -32768;
56 else
57 return i - 0x43c00000;
58}
59
60static inline void float_to_int (float * _f, INT16 * s16)
61{
62 int i;
63 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
64
65 for (i = 0; i < 256; i++) {
66 s16[2*i] = blah (f[i]);
67 s16[2*i+1] = blah (f[i+256]);
68 }
69}
70
71static inline void float_to_int_mono (float * _f, INT16 * s16)
72{
73 int i;
74 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
75
76 for (i = 0; i < 256; i++) {
77 s16[i] = blah (f[i]);
78 }
79}
80
81/**** end */
82
83#define HEADER_SIZE 7
84
85static int ac3_decode_frame(AVCodecContext *avctx,
86 void *data, int *data_size,
87 UINT8 *buf, int buf_size)
88{
89 AC3DecodeState *s = avctx->priv_data;
90 UINT8 *buf_ptr;
91 int flags, i, len;
92 int sample_rate, bit_rate;
93 short *out_samples = data;
94 float level;
95
96 *data_size = 0;
97 buf_ptr = buf;
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;
103 if (len > buf_size)
104 len = buf_size;
105 memcpy(s->inbuf_ptr, buf_ptr, len);
106 buf_ptr += len;
107 s->inbuf_ptr += len;
108 buf_size -= len;
109 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
110 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
111 if (len == 0) {
112 /* no sync found : move by one byte (inefficient, but simple!) */
113 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
114 s->inbuf_ptr--;
115 } else {
116 s->frame_size = len;
117 /* update codec info */
118 avctx->sample_rate = sample_rate;
119 if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
120 avctx->channels = 1;
121 else
122 avctx->channels = 2;
123 avctx->bit_rate = bit_rate;
124 }
125 }
126 } else if (len < s->frame_size) {
127 len = s->frame_size - len;
128 if (len > buf_size)
129 len = buf_size;
130
131 memcpy(s->inbuf_ptr, buf_ptr, len);
132 buf_ptr += len;
133 s->inbuf_ptr += len;
134 buf_size -= len;
135 } else {
136 if (avctx->channels == 1)
137 flags = AC3_MONO;
138 else
139 flags = AC3_STEREO;
140
141 flags |= AC3_ADJUST_LEVEL;
142 level = 1;
143 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
144 fail:
145 s->inbuf_ptr = s->inbuf;
146 s->frame_size = 0;
147 continue;
148 }
149 for (i = 0; i < 6; i++) {
150 if (ac3_block (&s->state))
151 goto fail;
152 if (avctx->channels == 1)
153 float_to_int_mono (*samples, out_samples + i * 256);
154 else
155 float_to_int (*samples, out_samples + i * 512);
156 }
157 s->inbuf_ptr = s->inbuf;
158 s->frame_size = 0;
159 *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
160 break;
161 }
162 }
163 return buf_ptr - buf;
164}
165
166static int ac3_decode_end(AVCodecContext *s)
167{
168 return 0;
169}
170
171AVCodec ac3_decoder = {
172 "ac3",
173 CODEC_TYPE_AUDIO,
174 CODEC_ID_AC3,
175 sizeof(AC3DecodeState),
176 ac3_decode_init,
177 NULL,
178 ac3_decode_end,
179 ac3_decode_frame,
180};