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