3c73b475afd7cd18543bf29d52bedfa912a3904e
[libav.git] / libavcodec / dtsdec.c
1 /*
2 * dtsdec.c : free DTS Coherent Acoustics stream decoder.
3 * Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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.
11 *
12 * FFmpeg 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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_AV_CONFIG_H
23 #undef HAVE_AV_CONFIG_H
24 #endif
25
26 #include "avcodec.h"
27 #include <dts.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef HAVE_MALLOC_H
33 #include <malloc.h>
34 #endif
35
36 #define BUFFER_SIZE 18726
37 #define HEADER_SIZE 14
38
39 #ifdef LIBDTS_FIXED
40 #define CONVERT_LEVEL (1 << 26)
41 #define CONVERT_BIAS 0
42 #else
43 #define CONVERT_LEVEL 1
44 #define CONVERT_BIAS 384
45 #endif
46
47 static inline int16_t
48 convert(int32_t i)
49 {
50 #ifdef LIBDTS_FIXED
51 i >>= 15;
52 #else
53 i -= 0x43c00000;
54 #endif
55 return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i);
56 }
57
58 static void
59 convert2s16_2(sample_t * _f, int16_t * s16)
60 {
61 int i;
62 int32_t *f = (int32_t *) _f;
63
64 for(i = 0; i < 256; i++) {
65 s16[2 * i] = convert(f[i]);
66 s16[2 * i + 1] = convert(f[i + 256]);
67 }
68 }
69
70 static void
71 convert2s16_4(sample_t * _f, int16_t * s16)
72 {
73 int i;
74 int32_t *f = (int32_t *) _f;
75
76 for(i = 0; i < 256; i++) {
77 s16[4 * i] = convert(f[i]);
78 s16[4 * i + 1] = convert(f[i + 256]);
79 s16[4 * i + 2] = convert(f[i + 512]);
80 s16[4 * i + 3] = convert(f[i + 768]);
81 }
82 }
83
84 static void
85 convert2s16_5(sample_t * _f, int16_t * s16)
86 {
87 int i;
88 int32_t *f = (int32_t *) _f;
89
90 for(i = 0; i < 256; i++) {
91 s16[5 * i] = convert(f[i]);
92 s16[5 * i + 1] = convert(f[i + 256]);
93 s16[5 * i + 2] = convert(f[i + 512]);
94 s16[5 * i + 3] = convert(f[i + 768]);
95 s16[5 * i + 4] = convert(f[i + 1024]);
96 }
97 }
98
99 static void
100 convert2s16_multi(sample_t * _f, int16_t * s16, int flags)
101 {
102 int i;
103 int32_t *f = (int32_t *) _f;
104
105 switch (flags) {
106 case DTS_MONO:
107 for(i = 0; i < 256; i++) {
108 s16[5 * i] = s16[5 * i + 1] = s16[5 * i + 2] = s16[5 * i + 3] =
109 0;
110 s16[5 * i + 4] = convert(f[i]);
111 }
112 break;
113 case DTS_CHANNEL:
114 case DTS_STEREO:
115 case DTS_DOLBY:
116 convert2s16_2(_f, s16);
117 break;
118 case DTS_3F:
119 for(i = 0; i < 256; i++) {
120 s16[5 * i] = convert(f[i]);
121 s16[5 * i + 1] = convert(f[i + 512]);
122 s16[5 * i + 2] = s16[5 * i + 3] = 0;
123 s16[5 * i + 4] = convert(f[i + 256]);
124 }
125 break;
126 case DTS_2F2R:
127 convert2s16_4(_f, s16);
128 break;
129 case DTS_3F2R:
130 convert2s16_5(_f, s16);
131 break;
132 case DTS_MONO | DTS_LFE:
133 for(i = 0; i < 256; i++) {
134 s16[6 * i] = s16[6 * i + 1] = s16[6 * i + 2] = s16[6 * i + 3] =
135 0;
136 s16[6 * i + 4] = convert(f[i + 256]);
137 s16[6 * i + 5] = convert(f[i]);
138 }
139 break;
140 case DTS_CHANNEL | DTS_LFE:
141 case DTS_STEREO | DTS_LFE:
142 case DTS_DOLBY | DTS_LFE:
143 for(i = 0; i < 256; i++) {
144 s16[6 * i] = convert(f[i + 256]);
145 s16[6 * i + 1] = convert(f[i + 512]);
146 s16[6 * i + 2] = s16[6 * i + 3] = s16[6 * i + 4] = 0;
147 s16[6 * i + 5] = convert(f[i]);
148 }
149 break;
150 case DTS_3F | DTS_LFE:
151 for(i = 0; i < 256; i++) {
152 s16[6 * i] = convert(f[i + 256]);
153 s16[6 * i + 1] = convert(f[i + 768]);
154 s16[6 * i + 2] = s16[6 * i + 3] = 0;
155 s16[6 * i + 4] = convert(f[i + 512]);
156 s16[6 * i + 5] = convert(f[i]);
157 }
158 break;
159 case DTS_2F2R | DTS_LFE:
160 for(i = 0; i < 256; i++) {
161 s16[6 * i] = convert(f[i + 256]);
162 s16[6 * i + 1] = convert(f[i + 512]);
163 s16[6 * i + 2] = convert(f[i + 768]);
164 s16[6 * i + 3] = convert(f[i + 1024]);
165 s16[6 * i + 4] = 0;
166 s16[6 * i + 5] = convert(f[i]);
167 }
168 break;
169 case DTS_3F2R | DTS_LFE:
170 for(i = 0; i < 256; i++) {
171 s16[6 * i] = convert(f[i + 256]);
172 s16[6 * i + 1] = convert(f[i + 768]);
173 s16[6 * i + 2] = convert(f[i + 1024]);
174 s16[6 * i + 3] = convert(f[i + 1280]);
175 s16[6 * i + 4] = convert(f[i + 512]);
176 s16[6 * i + 5] = convert(f[i]);
177 }
178 break;
179 }
180 }
181
182 static int
183 channels_multi(int flags)
184 {
185 if(flags & DTS_LFE)
186 return 6;
187 else if(flags & 1) /* center channel */
188 return 5;
189 else if((flags & DTS_CHANNEL_MASK) == DTS_2F2R)
190 return 4;
191 else
192 return 2;
193 }
194
195 static int
196 dts_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
197 uint8_t * buff, int buff_size)
198 {
199 uint8_t *start = buff;
200 uint8_t *end = buff + buff_size;
201 static uint8_t buf[BUFFER_SIZE];
202 static uint8_t *bufptr = buf;
203 static uint8_t *bufpos = buf + HEADER_SIZE;
204 int16_t *out_samples = data;
205 static int sample_rate;
206 static int frame_length;
207 static int flags;
208 int bit_rate;
209 int len;
210 dts_state_t *state = avctx->priv_data;
211 level_t level;
212 sample_t bias;
213 int i;
214
215 *data_size = 0;
216
217 while(1) {
218 int length;
219
220 len = end - start;
221 if(!len)
222 break;
223 if(len > bufpos - bufptr)
224 len = bufpos - bufptr;
225 memcpy(bufptr, start, len);
226 bufptr += len;
227 start += len;
228 if(bufptr != bufpos)
229 return start - buff;
230 if(bufpos != buf + HEADER_SIZE)
231 break;
232
233 length = dts_syncinfo(state, buf, &flags, &sample_rate, &bit_rate,
234 &frame_length);
235 if(!length) {
236 av_log(NULL, AV_LOG_INFO, "skip\n");
237 for(bufptr = buf; bufptr < buf + HEADER_SIZE - 1; bufptr++)
238 bufptr[0] = bufptr[1];
239 continue;
240 }
241 bufpos = buf + length;
242 }
243
244 flags = 2; /* ???????????? */
245 level = CONVERT_LEVEL;
246 bias = CONVERT_BIAS;
247
248 flags |= DTS_ADJUST_LEVEL;
249 if(dts_frame(state, buf, &flags, &level, bias)) {
250 av_log(avctx, AV_LOG_ERROR, "dts_frame() failed\n");
251 goto end;
252 }
253
254 avctx->sample_rate = sample_rate;
255 avctx->channels = channels_multi(flags);
256 avctx->bit_rate = bit_rate;
257
258 for(i = 0; i < dts_blocks_num(state); i++) {
259 int chans;
260
261 if(dts_block(state)) {
262 av_log(avctx, AV_LOG_ERROR, "dts_block() failed\n");
263 goto end;
264 }
265
266 chans = channels_multi(flags);
267 convert2s16_multi(dts_samples(state), out_samples,
268 flags & (DTS_CHANNEL_MASK | DTS_LFE));
269
270 out_samples += 256 * chans;
271 *data_size += 256 * sizeof(int16_t) * chans;
272 }
273
274 end:
275 bufptr = buf;
276 bufpos = buf + HEADER_SIZE;
277 return start - buff;
278 }
279
280 static int
281 dts_decode_init(AVCodecContext * avctx)
282 {
283 avctx->priv_data = dts_init(0);
284 if(avctx->priv_data == NULL)
285 return -1;
286
287 return 0;
288 }
289
290 static int
291 dts_decode_end(AVCodecContext * s)
292 {
293 return 0;
294 }
295
296 AVCodec dts_decoder = {
297 "dts",
298 CODEC_TYPE_AUDIO,
299 CODEC_ID_DTS,
300 sizeof(dts_state_t *),
301 dts_decode_init,
302 NULL,
303 dts_decode_end,
304 dts_decode_frame,
305 };