Mercurial > libavcodec.hg
annotate ac3dec.c @ 625:bb6a69f9d409 libavcodec
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
per context DCT selection
author | michaelni |
---|---|
date | Thu, 29 Aug 2002 23:55:32 +0000 |
parents | 718a22dc121f |
children | b32afefe7d33 |
rev | line source |
---|---|
0 | 1 /* |
2 * AC3 decoder | |
429 | 3 * Copyright (c) 2001 Fabrice Bellard. |
0 | 4 * |
429 | 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. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 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 | |
0 | 18 */ |
19 #include "avcodec.h" | |
20 #include "libac3/ac3.h" | |
21 | |
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 */ | |
26 UINT8 *inbuf_ptr; | |
27 int frame_size; | |
28 int flags; | |
314 | 29 int channels; |
0 | 30 ac3_state_t state; |
31 } AC3DecodeState; | |
32 | |
33 static int ac3_decode_init(AVCodecContext *avctx) | |
34 { | |
35 AC3DecodeState *s = avctx->priv_data; | |
36 | |
37 ac3_init (); | |
38 s->inbuf_ptr = s->inbuf; | |
39 s->frame_size = 0; | |
40 return 0; | |
41 } | |
42 | |
43 stream_samples_t samples; | |
44 | |
45 /**** the following two functions comes from ac3dec */ | |
46 static inline int blah (int32_t i) | |
47 { | |
48 if (i > 0x43c07fff) | |
49 return 32767; | |
50 else if (i < 0x43bf8000) | |
51 return -32768; | |
52 else | |
53 return i - 0x43c00000; | |
54 } | |
55 | |
314 | 56 static inline void float_to_int (float * _f, INT16 * s16, int nchannels) |
0 | 57 { |
314 | 58 int i, j, c; |
0 | 59 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
60 | |
314 | 61 j = 0; |
62 nchannels *= 256; | |
0 | 63 for (i = 0; i < 256; i++) { |
314 | 64 for (c = 0; c < nchannels; c += 256) |
65 s16[j++] = blah (f[i + c]); | |
0 | 66 } |
67 } | |
68 | |
69 /**** end */ | |
70 | |
71 #define HEADER_SIZE 7 | |
72 | |
73 static int ac3_decode_frame(AVCodecContext *avctx, | |
74 void *data, int *data_size, | |
75 UINT8 *buf, int buf_size) | |
76 { | |
77 AC3DecodeState *s = avctx->priv_data; | |
78 UINT8 *buf_ptr; | |
79 int flags, i, len; | |
80 int sample_rate, bit_rate; | |
81 short *out_samples = data; | |
82 float level; | |
323 | 83 static const int ac3_channels[8] = { |
314 | 84 2, 1, 2, 3, 3, 4, 4, 5 |
85 }; | |
0 | 86 |
87 *data_size = 0; | |
88 buf_ptr = buf; | |
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; | |
94 if (len > buf_size) | |
95 len = buf_size; | |
96 memcpy(s->inbuf_ptr, buf_ptr, len); | |
97 buf_ptr += len; | |
98 s->inbuf_ptr += len; | |
99 buf_size -= len; | |
100 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { | |
101 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
102 if (len == 0) { | |
103 /* no sync found : move by one byte (inefficient, but simple!) */ | |
104 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); | |
105 s->inbuf_ptr--; | |
106 } else { | |
323 | 107 s->frame_size = len; |
0 | 108 /* update codec info */ |
109 avctx->sample_rate = sample_rate; | |
314 | 110 s->channels = ac3_channels[s->flags & 7]; |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
111 if (s->flags & AC3_LFE) |
323 | 112 s->channels++; |
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; | |
119 } | |
120 avctx->bit_rate = bit_rate; | |
0 | 121 } |
122 } | |
123 } else if (len < s->frame_size) { | |
124 len = s->frame_size - len; | |
125 if (len > buf_size) | |
126 len = buf_size; | |
323 | 127 |
0 | 128 memcpy(s->inbuf_ptr, buf_ptr, len); |
129 buf_ptr += len; | |
130 s->inbuf_ptr += len; | |
131 buf_size -= len; | |
132 } else { | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
133 flags = s->flags; |
0 | 134 if (avctx->channels == 1) |
135 flags = AC3_MONO; | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
136 else if (avctx->channels == 2) |
0 | 137 flags = AC3_STEREO; |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
138 else |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
139 flags |= AC3_ADJUST_LEVEL; |
0 | 140 level = 1; |
141 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) { | |
142 fail: | |
143 s->inbuf_ptr = s->inbuf; | |
144 s->frame_size = 0; | |
145 continue; | |
146 } | |
147 for (i = 0; i < 6; i++) { | |
148 if (ac3_block (&s->state)) | |
149 goto fail; | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
150 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels); |
0 | 151 } |
152 s->inbuf_ptr = s->inbuf; | |
153 s->frame_size = 0; | |
154 *data_size = 6 * avctx->channels * 256 * sizeof(INT16); | |
155 break; | |
156 } | |
157 } | |
158 return buf_ptr - buf; | |
159 } | |
160 | |
161 static int ac3_decode_end(AVCodecContext *s) | |
162 { | |
163 return 0; | |
164 } | |
165 | |
166 AVCodec ac3_decoder = { | |
167 "ac3", | |
168 CODEC_TYPE_AUDIO, | |
169 CODEC_ID_AC3, | |
170 sizeof(AC3DecodeState), | |
171 ac3_decode_init, | |
172 NULL, | |
173 ac3_decode_end, | |
174 ac3_decode_frame, | |
175 }; |