2 * dtsdec.c : free DTS Coherent Acoustics stream decoder.
3 * Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
5 * This file is part of FFmpeg.
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.
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.
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
28 #define BUFFER_SIZE 18726
29 #define HEADER_SIZE 14
31 #define CONVERT_LEVEL 1
32 #define CONVERT_BIAS 0
34 typedef struct DTSContext
{
36 uint8_t buf
[BUFFER_SIZE
];
48 convert2s16_multi(sample_t
*f
, int16_t *s16
, int flags
)
52 switch(flags
& (DTS_CHANNEL_MASK
| DTS_LFE
)){
54 for(i
= 0; i
< 256; i
++){
55 s16
[5*i
] = s16
[5*i
+1] = s16
[5*i
+2] = s16
[5*i
+3] = 0;
56 s16
[5*i
+4] = convert(f
[i
]);
61 for(i
= 0; i
< 256; i
++){
62 s16
[2*i
] = convert(f
[i
]);
63 s16
[2*i
+1] = convert(f
[i
+256]);
66 for(i
= 0; i
< 256; i
++){
67 s16
[5*i
] = convert(f
[i
+256]);
68 s16
[5*i
+1] = convert(f
[i
+512]);
69 s16
[5*i
+2] = s16
[5*i
+3] = 0;
70 s16
[5*i
+4] = convert(f
[i
]);
73 for(i
= 0; i
< 256; i
++){
74 s16
[4*i
] = convert(f
[i
]);
75 s16
[4*i
+1] = convert(f
[i
+256]);
76 s16
[4*i
+2] = convert(f
[i
+512]);
77 s16
[4*i
+3] = convert(f
[i
+768]);
80 for(i
= 0; i
< 256; i
++){
81 s16
[5*i
] = convert(f
[i
+256]);
82 s16
[5*i
+1] = convert(f
[i
+512]);
83 s16
[5*i
+2] = convert(f
[i
+768]);
84 s16
[5*i
+3] = convert(f
[i
+1024]);
85 s16
[5*i
+4] = convert(f
[i
]);
87 case DTS_MONO
| DTS_LFE
:
88 for(i
= 0; i
< 256; i
++){
89 s16
[6*i
] = s16
[6*i
+1] = s16
[6*i
+2] = s16
[6*i
+3] = 0;
90 s16
[6*i
+4] = convert(f
[i
]);
91 s16
[6*i
+5] = convert(f
[i
+256]);
93 case DTS_CHANNEL
| DTS_LFE
:
94 case DTS_STEREO
| DTS_LFE
:
95 case DTS_DOLBY
| DTS_LFE
:
96 for(i
= 0; i
< 256; i
++){
97 s16
[6*i
] = convert(f
[i
]);
98 s16
[6*i
+1] = convert(f
[i
+256]);
99 s16
[6*i
+2] = s16
[6*i
+3] = s16
[6*i
+4] = 0;
100 s16
[6*i
+5] = convert(f
[i
+512]);
102 case DTS_3F
| DTS_LFE
:
103 for(i
= 0; i
< 256; i
++){
104 s16
[6*i
] = convert(f
[i
+256]);
105 s16
[6*i
+1] = convert(f
[i
+512]);
106 s16
[6*i
+2] = s16
[6*i
+3] = 0;
107 s16
[6*i
+4] = convert(f
[i
]);
108 s16
[6*i
+5] = convert(f
[i
+768]);
110 case DTS_2F2R
| DTS_LFE
:
111 for(i
= 0; i
< 256; i
++){
112 s16
[6*i
] = convert(f
[i
]);
113 s16
[6*i
+1] = convert(f
[i
+256]);
114 s16
[6*i
+2] = convert(f
[i
+512]);
115 s16
[6*i
+3] = convert(f
[i
+768]);
117 s16
[6*i
+5] = convert(f
[i
+1024]);
119 case DTS_3F2R
| DTS_LFE
:
120 for(i
= 0; i
< 256; i
++){
121 s16
[6*i
] = convert(f
[i
+256]);
122 s16
[6*i
+1] = convert(f
[i
+512]);
123 s16
[6*i
+2] = convert(f
[i
+768]);
124 s16
[6*i
+3] = convert(f
[i
+1024]);
125 s16
[6*i
+4] = convert(f
[i
]);
126 s16
[6*i
+5] = convert(f
[i
+1280]);
132 channels_multi(int flags
)
134 switch(flags
& (DTS_CHANNEL_MASK
| DTS_LFE
)){
145 case DTS_MONO
| DTS_LFE
:
146 case DTS_CHANNEL
| DTS_LFE
:
147 case DTS_STEREO
| DTS_LFE
:
148 case DTS_DOLBY
| DTS_LFE
:
149 case DTS_3F
| DTS_LFE
:
150 case DTS_2F2R
| DTS_LFE
:
151 case DTS_3F2R
| DTS_LFE
:
159 dts_decode_frame(AVCodecContext
* avctx
, void *data
, int *data_size
,
160 uint8_t * buff
, int buff_size
)
162 DTSContext
*s
= avctx
->priv_data
;
163 uint8_t *start
= buff
;
164 uint8_t *end
= buff
+ buff_size
;
165 int16_t *out_samples
= data
;
184 if(len
> s
->bufpos
- s
->bufptr
)
185 len
= s
->bufpos
- s
->bufptr
;
186 memcpy(s
->bufptr
, start
, len
);
189 if(s
->bufptr
!= s
->bufpos
)
191 if(s
->bufpos
!= s
->buf
+ HEADER_SIZE
)
194 length
= dts_syncinfo(s
->state
, s
->buf
, &flags
, &sample_rate
,
195 &bit_rate
, &frame_length
);
197 av_log(NULL
, AV_LOG_INFO
, "skip\n");
198 for(s
->bufptr
= s
->buf
; s
->bufptr
< s
->buf
+ HEADER_SIZE
- 1; s
->bufptr
++)
199 s
->bufptr
[0] = s
->bufptr
[1];
202 s
->bufpos
= s
->buf
+ length
;
205 level
= CONVERT_LEVEL
;
208 flags
|= DTS_ADJUST_LEVEL
;
209 if(dts_frame(s
->state
, s
->buf
, &flags
, &level
, bias
)) {
210 av_log(avctx
, AV_LOG_ERROR
, "dts_frame() failed\n");
214 avctx
->sample_rate
= sample_rate
;
215 avctx
->channels
= channels_multi(flags
);
216 avctx
->bit_rate
= bit_rate
;
218 nblocks
= dts_blocks_num(s
->state
);
220 for(i
= 0; i
< nblocks
; i
++) {
221 if(dts_block(s
->state
)) {
222 av_log(avctx
, AV_LOG_ERROR
, "dts_block() failed\n");
226 convert2s16_multi(dts_samples(s
->state
), out_samples
, flags
);
228 out_samples
+= 256 * avctx
->channels
;
229 *data_size
+= 256 * sizeof(int16_t) * avctx
->channels
;
234 s
->bufpos
= s
->buf
+ HEADER_SIZE
;
239 dts_decode_init(AVCodecContext
* avctx
)
241 DTSContext
*s
= avctx
->priv_data
;
243 s
->bufpos
= s
->buf
+ HEADER_SIZE
;
244 s
->state
= dts_init(0);
252 dts_decode_end(AVCodecContext
* avctx
)
254 DTSContext
*s
= avctx
->priv_data
;
259 AVCodec dts_decoder
= {