Mercurial > libavcodec.hg
annotate ac3dec.c @ 2497:69adfbbdcdeb libavcodec
- samples from mplayer ftp in the "adv" profile seem to have profile=2,
which isn't the advanced one; and indeed, using adv. profile parser fails.
Using normal parser works, and that's what is done
- attempt at taking care of stride for NORM2 bitplane decoding
- duplication of much code from msmpeg4.c; this code isn't yet used, but
goes down as far as the block layer (mainly Transform Type stuff, the
remains are wild editing without checking). Unusable yet, and lacks the AC
decoding (but a step further in bitstream parsing)
patch by anonymous
author | michael |
---|---|
date | Fri, 04 Feb 2005 02:20:38 +0000 |
parents | 141a9539e270 |
children | 40765c51a7a9 |
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 */ |
1106 | 19 |
20 /** | |
21 * @file ac3dec.c | |
22 * AC3 decoder. | |
23 */ | |
24 | |
25 //#define DEBUG | |
26 | |
0 | 27 #include "avcodec.h" |
28 #include "libac3/ac3.h" | |
29 | |
30 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and | |
31 released under the GPL license. I may reimplement it someday... */ | |
32 typedef struct AC3DecodeState { | |
1064 | 33 uint8_t inbuf[4096]; /* input buffer */ |
34 uint8_t *inbuf_ptr; | |
0 | 35 int frame_size; |
36 int flags; | |
314 | 37 int channels; |
0 | 38 ac3_state_t state; |
39 } AC3DecodeState; | |
40 | |
41 static int ac3_decode_init(AVCodecContext *avctx) | |
42 { | |
43 AC3DecodeState *s = avctx->priv_data; | |
44 | |
45 ac3_init (); | |
46 s->inbuf_ptr = s->inbuf; | |
47 s->frame_size = 0; | |
48 return 0; | |
49 } | |
50 | |
51 stream_samples_t samples; | |
52 | |
53 /**** the following two functions comes from ac3dec */ | |
54 static inline int blah (int32_t i) | |
55 { | |
56 if (i > 0x43c07fff) | |
57 return 32767; | |
58 else if (i < 0x43bf8000) | |
59 return -32768; | |
60 else | |
61 return i - 0x43c00000; | |
62 } | |
63 | |
1064 | 64 static inline void float_to_int (float * _f, int16_t * s16, int nchannels) |
0 | 65 { |
314 | 66 int i, j, c; |
0 | 67 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
68 | |
314 | 69 j = 0; |
70 nchannels *= 256; | |
0 | 71 for (i = 0; i < 256; i++) { |
314 | 72 for (c = 0; c < nchannels; c += 256) |
73 s16[j++] = blah (f[i + c]); | |
0 | 74 } |
75 } | |
76 | |
77 /**** end */ | |
78 | |
79 #define HEADER_SIZE 7 | |
80 | |
81 static int ac3_decode_frame(AVCodecContext *avctx, | |
82 void *data, int *data_size, | |
1064 | 83 uint8_t *buf, int buf_size) |
0 | 84 { |
85 AC3DecodeState *s = avctx->priv_data; | |
1064 | 86 uint8_t *buf_ptr; |
0 | 87 int flags, i, len; |
88 int sample_rate, bit_rate; | |
89 short *out_samples = data; | |
90 float level; | |
323 | 91 static const int ac3_channels[8] = { |
314 | 92 2, 1, 2, 3, 3, 4, 4, 5 |
93 }; | |
0 | 94 |
95 buf_ptr = buf; | |
96 while (buf_size > 0) { | |
97 len = s->inbuf_ptr - s->inbuf; | |
98 if (s->frame_size == 0) { | |
99 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
100 len = HEADER_SIZE - len; | |
101 if (len > buf_size) | |
102 len = buf_size; | |
103 memcpy(s->inbuf_ptr, buf_ptr, len); | |
104 buf_ptr += len; | |
105 s->inbuf_ptr += len; | |
106 buf_size -= len; | |
107 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { | |
108 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
109 if (len == 0) { | |
110 /* no sync found : move by one byte (inefficient, but simple!) */ | |
111 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); | |
112 s->inbuf_ptr--; | |
113 } else { | |
323 | 114 s->frame_size = len; |
0 | 115 /* update codec info */ |
116 avctx->sample_rate = sample_rate; | |
314 | 117 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
|
118 if (s->flags & AC3_LFE) |
323 | 119 s->channels++; |
120 if (avctx->channels == 0) | |
121 /* No specific number of channel requested */ | |
122 avctx->channels = s->channels; | |
123 else if (s->channels < avctx->channels) { | |
124 fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); | |
125 avctx->channels = s->channels; | |
126 } | |
127 avctx->bit_rate = bit_rate; | |
0 | 128 } |
129 } | |
130 } else if (len < s->frame_size) { | |
131 len = s->frame_size - len; | |
132 if (len > buf_size) | |
133 len = buf_size; | |
323 | 134 |
0 | 135 memcpy(s->inbuf_ptr, buf_ptr, len); |
136 buf_ptr += len; | |
137 s->inbuf_ptr += len; | |
138 buf_size -= len; | |
139 } else { | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
140 flags = s->flags; |
0 | 141 if (avctx->channels == 1) |
142 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
|
143 else if (avctx->channels == 2) |
0 | 144 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
|
145 else |
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
146 flags |= AC3_ADJUST_LEVEL; |
0 | 147 level = 1; |
148 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) { | |
149 fail: | |
150 s->inbuf_ptr = s->inbuf; | |
151 s->frame_size = 0; | |
152 continue; | |
153 } | |
154 for (i = 0; i < 6; i++) { | |
155 if (ac3_block (&s->state)) | |
156 goto fail; | |
318
21697f35a9ca
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents:
314
diff
changeset
|
157 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels); |
0 | 158 } |
159 s->inbuf_ptr = s->inbuf; | |
160 s->frame_size = 0; | |
1064 | 161 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); |
0 | 162 break; |
163 } | |
164 } | |
165 return buf_ptr - buf; | |
166 } | |
167 | |
168 static int ac3_decode_end(AVCodecContext *s) | |
169 { | |
170 return 0; | |
171 } | |
172 | |
173 AVCodec ac3_decoder = { | |
174 "ac3", | |
175 CODEC_TYPE_AUDIO, | |
176 CODEC_ID_AC3, | |
177 sizeof(AC3DecodeState), | |
178 ac3_decode_init, | |
179 NULL, | |
180 ac3_decode_end, | |
181 ac3_decode_frame, | |
182 }; |