annotate ac3dec.c @ 225:ae145876789d libavcodec

use multiply instead of divides for DC prediction on X86
author michaelni
date Tue, 05 Feb 2002 22:51:23 +0000
parents 5aa6292a1660
children 289eb941b8ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * AC3 decoder
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include "libac3/ac3.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
21
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 released under the GPL license. I may reimplement it someday... */
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 typedef struct AC3DecodeState {
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 UINT8 inbuf[4096]; /* input buffer */
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 UINT8 *inbuf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 int frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 int flags;
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 ac3_state_t state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 } AC3DecodeState;
986e461dc072 Initial revision
glantau
parents:
diff changeset
31
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 static int ac3_decode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 AC3DecodeState *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
35
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 ac3_init ();
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
41
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 stream_samples_t samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 /**** the following two functions comes from ac3dec */
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 static inline int blah (int32_t i)
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 if (i > 0x43c07fff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 return 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 else if (i < 0x43bf8000)
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 return -32768;
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 return i - 0x43c00000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
54
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 static inline void float_to_int (float * _f, INT16 * s16)
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
986e461dc072 Initial revision
glantau
parents:
diff changeset
59
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 for (i = 0; i < 256; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 s16[2*i] = blah (f[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 s16[2*i+1] = blah (f[i+256]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
65
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 static inline void float_to_int_mono (float * _f, INT16 * s16)
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 for (i = 0; i < 256; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 s16[i] = blah (f[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
75
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 /**** end */
986e461dc072 Initial revision
glantau
parents:
diff changeset
77
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 #define HEADER_SIZE 7
986e461dc072 Initial revision
glantau
parents:
diff changeset
79
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 static int ac3_decode_frame(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 void *data, int *data_size,
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 UINT8 *buf, int buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 AC3DecodeState *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 UINT8 *buf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 int flags, i, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 int sample_rate, bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 short *out_samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 float level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
90
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 *data_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 buf_ptr = buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 while (buf_size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 len = s->inbuf_ptr - s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 if (s->frame_size == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 /* no header seen : find one. We need at least 7 bytes to parse it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 len = HEADER_SIZE - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 len = buf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 if (len == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 /* no sync found : move by one byte (inefficient, but simple!) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 s->inbuf_ptr--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 s->frame_size = len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 /* update codec info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 avctx->sample_rate = sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 avctx->channels = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 avctx->channels = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 avctx->bit_rate = bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 } else if (len < s->frame_size) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 len = s->frame_size - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 len = buf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 if (avctx->channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 flags = AC3_MONO;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 flags = AC3_STEREO;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 flags |= AC3_ADJUST_LEVEL;
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 level = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 fail:
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 if (ac3_block (&s->state))
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 if (avctx->channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 float_to_int_mono (*samples, out_samples + i * 256);
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 float_to_int (*samples, out_samples + i * 512);
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 return buf_ptr - buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
160
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 static int ac3_decode_end(AVCodecContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
165
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 AVCodec ac3_decoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 "ac3",
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 CODEC_ID_AC3,
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 sizeof(AC3DecodeState),
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 ac3_decode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 NULL,
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 ac3_decode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 ac3_decode_frame,
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 };