annotate ac3dec.c @ 1657:aaaa1767acc2 libavcodec

set has_b_frames correctly, fixes rv20 dr1 with mplayer
author michael
date Thu, 04 Dec 2003 19:15:37 +0000
parents 1e39f273ecd6
children 141a9539e270
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
429
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
3 * Copyright (c) 2001 Fabrice Bellard.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
429
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
5 * This library is free software; you can redistribute it and/or
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
7 * License as published by the Free Software Foundation; either
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
8 * version 2 of the License, or (at your option) any later version.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
429
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
10 * This library is distributed in the hope that it will be useful,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
429
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
13 * Lesser General Public License for more details.
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
429
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
16 * License along with this library; if not, write to the Free Software
718a22dc121f license/copyright change
glantau
parents: 323
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
19
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
20 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
21 * @file ac3dec.c
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
22 * AC3 decoder.
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
23 */
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
24
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
25 //#define DEBUG
1e39f273ecd6 per file doxy
michaelni
parents: 1064
diff changeset
26
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 #include "libac3/ac3.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
29
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 released under the GPL license. I may reimplement it someday... */
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 typedef struct AC3DecodeState {
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
33 uint8_t inbuf[4096]; /* input buffer */
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
34 uint8_t *inbuf_ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 int frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 int flags;
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
37 int channels;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 ac3_state_t state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 } AC3DecodeState;
986e461dc072 Initial revision
glantau
parents:
diff changeset
40
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 static int ac3_decode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 AC3DecodeState *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 ac3_init ();
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
50
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 stream_samples_t samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
52
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 /**** the following two functions comes from ac3dec */
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 static inline int blah (int32_t i)
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 if (i > 0x43c07fff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 return 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 else if (i < 0x43bf8000)
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 return -32768;
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 return i - 0x43c00000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
63
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
64 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 {
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
66 int i, j, c;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
986e461dc072 Initial revision
glantau
parents:
diff changeset
68
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
69 j = 0;
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
70 nchannels *= 256;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 for (i = 0; i < 256; i++) {
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
72 for (c = 0; c < nchannels; c += 256)
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
73 s16[j++] = blah (f[i + c]);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
76
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 /**** end */
986e461dc072 Initial revision
glantau
parents:
diff changeset
78
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 #define HEADER_SIZE 7
986e461dc072 Initial revision
glantau
parents:
diff changeset
80
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 static int ac3_decode_frame(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 void *data, int *data_size,
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
83 uint8_t *buf, int buf_size)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 AC3DecodeState *s = avctx->priv_data;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
86 uint8_t *buf_ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 int flags, i, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 int sample_rate, bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 short *out_samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 float level;
323
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
91 static const int ac3_channels[8] = {
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
92 2, 1, 2, 3, 3, 4, 4, 5
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
93 };
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
94
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 *data_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 buf_ptr = buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 while (buf_size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 len = s->inbuf_ptr - s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 if (s->frame_size == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 /* no header seen : find one. We need at least 7 bytes to parse it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 len = HEADER_SIZE - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 len = buf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 if (len == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 /* no sync found : move by one byte (inefficient, but simple!) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 s->inbuf_ptr--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 } else {
323
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
115 s->frame_size = len;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 /* update codec info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 avctx->sample_rate = sample_rate;
314
289eb941b8ba * encoding of AC3 with more than 2 channels
kabi
parents: 64
diff changeset
118 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
119 if (s->flags & AC3_LFE)
323
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
120 s->channels++;
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
121 if (avctx->channels == 0)
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
122 /* No specific number of channel requested */
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
123 avctx->channels = s->channels;
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
124 else if (s->channels < avctx->channels) {
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
125 fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
126 avctx->channels = s->channels;
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
127 }
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
128 avctx->bit_rate = bit_rate;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 } else if (len < s->frame_size) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 len = s->frame_size - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 len = buf_size;
323
68cc7650c645 * minor - slightly modified debug message
kabi
parents: 318
diff changeset
135
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 } else {
318
21697f35a9ca - Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents: 314
diff changeset
141 flags = s->flags;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 if (avctx->channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 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
144 else if (avctx->channels == 2)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 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
146 else
21697f35a9ca - Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents: 314
diff changeset
147 flags |= AC3_ADJUST_LEVEL;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 level = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 fail:
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 if (ac3_block (&s->state))
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 goto fail;
318
21697f35a9ca - Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
pulento
parents: 314
diff changeset
158 float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 s->frame_size = 0;
1064
b32afefe7d33 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 429
diff changeset
162 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 return buf_ptr - buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
168
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 static int ac3_decode_end(AVCodecContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
173
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 AVCodec ac3_decoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 "ac3",
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 CODEC_ID_AC3,
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 sizeof(AC3DecodeState),
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 ac3_decode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 NULL,
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 ac3_decode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 ac3_decode_frame,
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 };