annotate ac3dec.c @ 52:1d796bdb2c2a libavcodec

added 422P, 444P support - added deinterlace support - added xxx to RGB24 convertion
author glantau
date Sat, 11 Aug 2001 19:03:02 +0000
parents 1bdbd869c1f0
children 5aa6292a1660
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 <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
23
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include <inttypes.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "libac3/ac3.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 released under the GPL license. I may reimplement it someday... */
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 typedef struct AC3DecodeState {
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 UINT8 inbuf[4096]; /* input buffer */
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 UINT8 *inbuf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 int frame_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 int flags;
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 ac3_state_t state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 } AC3DecodeState;
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 static int ac3_decode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 AC3DecodeState *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
40
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 ac3_init ();
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
46
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 stream_samples_t samples;
986e461dc072 Initial revision
glantau
parents:
diff changeset
48
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 /**** the following two functions comes from ac3dec */
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 static inline int blah (int32_t i)
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 if (i > 0x43c07fff)
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 return 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 else if (i < 0x43bf8000)
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 return -32768;
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 return i - 0x43c00000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
59
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 static inline void float_to_int (float * _f, INT16 * s16)
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
986e461dc072 Initial revision
glantau
parents:
diff changeset
64
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 for (i = 0; i < 256; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 s16[2*i] = blah (f[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 s16[2*i+1] = blah (f[i+256]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 static inline void float_to_int_mono (float * _f, INT16 * s16)
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
986e461dc072 Initial revision
glantau
parents:
diff changeset
75
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 for (i = 0; i < 256; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 s16[i] = blah (f[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
80
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 /**** end */
986e461dc072 Initial revision
glantau
parents:
diff changeset
82
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 #define HEADER_SIZE 7
986e461dc072 Initial revision
glantau
parents:
diff changeset
84
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 static int ac3_decode_frame(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 void *data, int *data_size,
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 UINT8 *buf, int buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 AC3DecodeState *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 UINT8 *buf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 int flags, i, len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 int sample_rate, bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 short *out_samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 float level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 *data_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 buf_ptr = buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 while (buf_size > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 len = s->inbuf_ptr - s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 if (s->frame_size == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 /* no header seen : find one. We need at least 7 bytes to parse it */
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 len = HEADER_SIZE - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 len = buf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 if (len == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 /* no sync found : move by one byte (inefficient, but simple!) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 s->inbuf_ptr--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 s->frame_size = len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 /* update codec info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 avctx->sample_rate = sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 avctx->channels = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 avctx->channels = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 avctx->bit_rate = bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 } else if (len < s->frame_size) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 len = s->frame_size - len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 if (len > buf_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 len = buf_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 memcpy(s->inbuf_ptr, buf_ptr, len);
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 buf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 s->inbuf_ptr += len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 buf_size -= len;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 if (avctx->channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 flags = AC3_MONO;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 flags = AC3_STEREO;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 flags |= AC3_ADJUST_LEVEL;
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 level = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 fail:
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 for (i = 0; i < 6; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (ac3_block (&s->state))
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 if (avctx->channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 float_to_int_mono (*samples, out_samples + i * 256);
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 float_to_int (*samples, out_samples + i * 512);
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 s->inbuf_ptr = s->inbuf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 s->frame_size = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 return buf_ptr - buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
165
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 static int ac3_decode_end(AVCodecContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
170
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 AVCodec ac3_decoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 "ac3",
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 CODEC_ID_AC3,
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 sizeof(AC3DecodeState),
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 ac3_decode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 NULL,
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 ac3_decode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 ac3_decode_frame,
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 };