r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / dtsdec.c
blobfb1d50c10e4f95ddc6388fe8d146b86c53019b28
1 /*
2 * dtsdec.c : free DTS Coherent Acoustics stream decoder.
3 * Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
5 * This file is part of libavcodec.
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifdef HAVE_AV_CONFIG_H
23 #undef HAVE_AV_CONFIG_H
24 #endif
26 #include "avcodec.h"
27 #include <dts.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #ifdef HAVE_MALLOC_H
33 #include <malloc.h>
34 #endif
36 #define INBUF_SIZE 4096
37 #define BUFFER_SIZE 4096
38 #define HEADER_SIZE 14
40 #ifdef LIBDTS_FIXED
41 #define CONVERT_LEVEL (1 << 26)
42 #define CONVERT_BIAS 0
43 #else
44 #define CONVERT_LEVEL 1
45 #define CONVERT_BIAS 384
46 #endif
48 static inline
49 int16_t convert (int32_t i)
51 #ifdef LIBDTS_FIXED
52 i >>= 15;
53 #else
54 i -= 0x43c00000;
55 #endif
56 return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i);
59 void
60 convert2s16_2 (sample_t * _f, int16_t * s16)
62 int i;
63 int32_t * f = (int32_t *) _f;
65 for (i = 0; i < 256; i++)
67 s16[2*i] = convert (f[i]);
68 s16[2*i+1] = convert (f[i+256]);
72 void
73 convert2s16_4 (sample_t * _f, int16_t * s16)
75 int i;
76 int32_t * f = (int32_t *) _f;
78 for (i = 0; i < 256; i++)
80 s16[4*i] = convert (f[i]);
81 s16[4*i+1] = convert (f[i+256]);
82 s16[4*i+2] = convert (f[i+512]);
83 s16[4*i+3] = convert (f[i+768]);
87 void
88 convert2s16_5 (sample_t * _f, int16_t * s16)
90 int i;
91 int32_t * f = (int32_t *) _f;
93 for (i = 0; i < 256; i++)
95 s16[5*i] = convert (f[i]);
96 s16[5*i+1] = convert (f[i+256]);
97 s16[5*i+2] = convert (f[i+512]);
98 s16[5*i+3] = convert (f[i+768]);
99 s16[5*i+4] = convert (f[i+1024]);
103 static void
104 convert2s16_multi (sample_t * _f, int16_t * s16, int flags)
106 int i;
107 int32_t * f = (int32_t *) _f;
109 switch (flags)
111 case DTS_MONO:
112 for (i = 0; i < 256; i++)
114 s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
115 s16[5*i+4] = convert (f[i]);
117 break;
118 case DTS_CHANNEL:
119 case DTS_STEREO:
120 case DTS_DOLBY:
121 convert2s16_2 (_f, s16);
122 break;
123 case DTS_3F:
124 for (i = 0; i < 256; i++)
126 s16[5*i] = convert (f[i]);
127 s16[5*i+1] = convert (f[i+512]);
128 s16[5*i+2] = s16[5*i+3] = 0;
129 s16[5*i+4] = convert (f[i+256]);
131 break;
132 case DTS_2F2R:
133 convert2s16_4 (_f, s16);
134 break;
135 case DTS_3F2R:
136 convert2s16_5 (_f, s16);
137 break;
138 case DTS_MONO | DTS_LFE:
139 for (i = 0; i < 256; i++)
141 s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
142 s16[6*i+4] = convert (f[i+256]);
143 s16[6*i+5] = convert (f[i]);
145 break;
146 case DTS_CHANNEL | DTS_LFE:
147 case DTS_STEREO | DTS_LFE:
148 case DTS_DOLBY | DTS_LFE:
149 for (i = 0; i < 256; i++)
151 s16[6*i] = convert (f[i+256]);
152 s16[6*i+1] = convert (f[i+512]);
153 s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
154 s16[6*i+5] = convert (f[i]);
156 break;
157 case DTS_3F | DTS_LFE:
158 for (i = 0; i < 256; i++)
160 s16[6*i] = convert (f[i+256]);
161 s16[6*i+1] = convert (f[i+768]);
162 s16[6*i+2] = s16[6*i+3] = 0;
163 s16[6*i+4] = convert (f[i+512]);
164 s16[6*i+5] = convert (f[i]);
166 break;
167 case DTS_2F2R | DTS_LFE:
168 for (i = 0; i < 256; i++)
170 s16[6*i] = convert (f[i+256]);
171 s16[6*i+1] = convert (f[i+512]);
172 s16[6*i+2] = convert (f[i+768]);
173 s16[6*i+3] = convert (f[i+1024]);
174 s16[6*i+4] = 0;
175 s16[6*i+5] = convert (f[i]);
177 break;
178 case DTS_3F2R | DTS_LFE:
179 for (i = 0; i < 256; i++)
181 s16[6*i] = convert (f[i+256]);
182 s16[6*i+1] = convert (f[i+768]);
183 s16[6*i+2] = convert (f[i+1024]);
184 s16[6*i+3] = convert (f[i+1280]);
185 s16[6*i+4] = convert (f[i+512]);
186 s16[6*i+5] = convert (f[i]);
188 break;
192 static int
193 channels_multi (int flags)
195 if (flags & DTS_LFE)
196 return 6;
197 else if (flags & 1) /* center channel */
198 return 5;
199 else if ((flags & DTS_CHANNEL_MASK) == DTS_2F2R)
200 return 4;
201 else
202 return 2;
205 static int
206 dts_decode_frame (AVCodecContext *avctx, void *data, int *data_size,
207 uint8_t *buff, int buff_size)
209 uint8_t * start = buff;
210 uint8_t * end = buff + buff_size;
211 static uint8_t buf[BUFFER_SIZE];
212 static uint8_t * bufptr = buf;
213 static uint8_t * bufpos = buf + HEADER_SIZE;
215 static int sample_rate;
216 static int frame_length;
217 static int flags;
218 int bit_rate;
219 int len;
220 dts_state_t *state = avctx->priv_data;
222 *data_size = 0;
224 while (1)
226 len = end - start;
227 if (!len)
228 break;
229 if (len > bufpos - bufptr)
230 len = bufpos - bufptr;
231 memcpy (bufptr, start, len);
232 bufptr += len;
233 start += len;
234 if (bufptr == bufpos)
236 if (bufpos == buf + HEADER_SIZE)
238 int length;
240 length = dts_syncinfo (state, buf, &flags, &sample_rate,
241 &bit_rate, &frame_length);
242 if (!length)
244 av_log (NULL, AV_LOG_INFO, "skip\n");
245 for (bufptr = buf; bufptr < buf + HEADER_SIZE-1; bufptr++)
246 bufptr[0] = bufptr[1];
247 continue;
249 bufpos = buf + length;
251 else
253 level_t level;
254 sample_t bias;
255 int i;
257 flags = 2; /* ???????????? */
258 level = CONVERT_LEVEL;
259 bias = CONVERT_BIAS;
261 flags |= DTS_ADJUST_LEVEL;
262 if (dts_frame (state, buf, &flags, &level, bias))
263 goto error;
264 avctx->sample_rate = sample_rate;
265 avctx->channels = channels_multi (flags);
266 avctx->bit_rate = bit_rate;
267 for (i = 0; i < dts_blocks_num (state); i++)
269 if (dts_block (state))
270 goto error;
272 int chans;
273 chans = channels_multi (flags);
274 convert2s16_multi (dts_samples (state), data,
275 flags & (DTS_CHANNEL_MASK | DTS_LFE));
277 data += 256 * sizeof (int16_t) * chans;
278 *data_size += 256 * sizeof (int16_t) * chans;
281 bufptr = buf;
282 bufpos = buf + HEADER_SIZE;
283 continue;
284 error:
285 av_log (NULL, AV_LOG_ERROR, "error\n");
286 bufptr = buf;
287 bufpos = buf + HEADER_SIZE;
292 return buff_size;
295 static int
296 dts_decode_init (AVCodecContext *avctx)
298 avctx->priv_data = dts_init (0);
299 if (avctx->priv_data == NULL)
300 return 1;
302 return 0;
305 static int
306 dts_decode_end (AVCodecContext *s)
308 return 0;
311 AVCodec dts_decoder = {
312 "dts",
313 CODEC_TYPE_AUDIO,
314 CODEC_ID_DTS,
315 sizeof (dts_state_t *),
316 dts_decode_init,
317 NULL,
318 dts_decode_end,
319 dts_decode_frame,