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
36 #define INBUF_SIZE 4096
37 #define BUFFER_SIZE 4096
38 #define HEADER_SIZE 14
41 #define CONVERT_LEVEL (1 << 26)
42 #define CONVERT_BIAS 0
44 #define CONVERT_LEVEL 1
45 #define CONVERT_BIAS 384
49 int16_t convert (int32_t i
)
56 return (i
> 32767) ? 32767 : ((i
< -32768) ? -32768 : i
);
60 convert2s16_2 (sample_t
* _f
, int16_t * s16
)
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]);
73 convert2s16_4 (sample_t
* _f
, int16_t * s16
)
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]);
88 convert2s16_5 (sample_t
* _f
, int16_t * s16
)
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]);
104 convert2s16_multi (sample_t
* _f
, int16_t * s16
, int flags
)
107 int32_t * f
= (int32_t *) _f
;
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
]);
121 convert2s16_2 (_f
, s16
);
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]);
133 convert2s16_4 (_f
, s16
);
136 convert2s16_5 (_f
, s16
);
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
]);
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
]);
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
]);
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]);
175 s16
[6*i
+5] = convert (f
[i
]);
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
]);
193 channels_multi (int flags
)
197 else if (flags
& 1) /* center channel */
199 else if ((flags
& DTS_CHANNEL_MASK
) == DTS_2F2R
)
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
;
220 dts_state_t
*state
= avctx
->priv_data
;
229 if (len
> bufpos
- bufptr
)
230 len
= bufpos
- bufptr
;
231 memcpy (bufptr
, start
, len
);
234 if (bufptr
== bufpos
)
236 if (bufpos
== buf
+ HEADER_SIZE
)
240 length
= dts_syncinfo (state
, buf
, &flags
, &sample_rate
,
241 &bit_rate
, &frame_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];
249 bufpos
= buf
+ length
;
257 flags
= 2; /* ???????????? */
258 level
= CONVERT_LEVEL
;
261 flags
|= DTS_ADJUST_LEVEL
;
262 if (dts_frame (state
, buf
, &flags
, &level
, bias
))
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
))
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
;
282 bufpos
= buf
+ HEADER_SIZE
;
285 av_log (NULL
, AV_LOG_ERROR
, "error\n");
287 bufpos
= buf
+ HEADER_SIZE
;
296 dts_decode_init (AVCodecContext
*avctx
)
298 avctx
->priv_data
= dts_init (0);
299 if (avctx
->priv_data
== NULL
)
306 dts_decode_end (AVCodecContext
*s
)
311 AVCodec dts_decoder
= {
315 sizeof (dts_state_t
*),