comparison src/ffmpeg/libavcodec/ac3dec.c @ 808:e8776388b02a trunk

[svn] - add ffmpeg
author nenolod
date Mon, 12 Mar 2007 11:18:54 -0700
parents
children
comparison
equal deleted inserted replaced
807:0f9c8d4d3ac4 808:e8776388b02a
1 /*
2 * AC3 decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file ac3dec.c
24 * AC3 decoder.
25 */
26
27 //#define DEBUG
28
29 #include "avcodec.h"
30 #include "libac3/ac3.h"
31
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 */
36 uint8_t *inbuf_ptr;
37 int frame_size;
38 int flags;
39 int channels;
40 ac3_state_t state;
41 } AC3DecodeState;
42
43 static int ac3_decode_init(AVCodecContext *avctx)
44 {
45 AC3DecodeState *s = avctx->priv_data;
46
47 ac3_init ();
48 s->inbuf_ptr = s->inbuf;
49 s->frame_size = 0;
50 return 0;
51 }
52
53 stream_samples_t samples;
54
55 /**** the following two functions comes from ac3dec */
56 static inline int blah (int32_t i)
57 {
58 if (i > 0x43c07fff)
59 return 32767;
60 else if (i < 0x43bf8000)
61 return -32768;
62 else
63 return i - 0x43c00000;
64 }
65
66 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
67 {
68 int i, j, c;
69 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
70
71 j = 0;
72 nchannels *= 256;
73 for (i = 0; i < 256; i++) {
74 for (c = 0; c < nchannels; c += 256)
75 s16[j++] = blah (f[i + c]);
76 }
77 }
78
79 /**** end */
80
81 #define HEADER_SIZE 7
82
83 static int ac3_decode_frame(AVCodecContext *avctx,
84 void *data, int *data_size,
85 uint8_t *buf, int buf_size)
86 {
87 AC3DecodeState *s = avctx->priv_data;
88 uint8_t *buf_ptr;
89 int flags, i, len;
90 int sample_rate, bit_rate;
91 short *out_samples = data;
92 float level;
93 static const int ac3_channels[8] = {
94 2, 1, 2, 3, 3, 4, 4, 5
95 };
96
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 s->channels = ac3_channels[s->flags & 7];
120 if (s->flags & AC3_LFE)
121 s->channels++;
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;
128 }
129 avctx->bit_rate = bit_rate;
130 }
131 }
132 } else if (len < s->frame_size) {
133 len = s->frame_size - len;
134 if (len > buf_size)
135 len = buf_size;
136
137 memcpy(s->inbuf_ptr, buf_ptr, len);
138 buf_ptr += len;
139 s->inbuf_ptr += len;
140 buf_size -= len;
141 } else {
142 flags = s->flags;
143 if (avctx->channels == 1)
144 flags = AC3_MONO;
145 else if (avctx->channels == 2)
146 flags = AC3_STEREO;
147 else
148 flags |= AC3_ADJUST_LEVEL;
149 level = 1;
150 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
151 fail:
152 s->inbuf_ptr = s->inbuf;
153 s->frame_size = 0;
154 continue;
155 }
156 for (i = 0; i < 6; i++) {
157 if (ac3_block (&s->state))
158 goto fail;
159 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
160 }
161 s->inbuf_ptr = s->inbuf;
162 s->frame_size = 0;
163 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
164 break;
165 }
166 }
167 return buf_ptr - buf;
168 }
169
170 static int ac3_decode_end(AVCodecContext *s)
171 {
172 return 0;
173 }
174
175 AVCodec ac3_decoder = {
176 "ac3",
177 CODEC_TYPE_AUDIO,
178 CODEC_ID_AC3,
179 sizeof(AC3DecodeState),
180 ac3_decode_init,
181 NULL,
182 ac3_decode_end,
183 ac3_decode_frame,
184 };