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