2 * DTS Coherent Acoustics stream decoder using libdca
3 * This file is partially based on dtsdec.c r9036 from FFmpeg and ad_liba52.c
5 * Copyright (C) 2007 Roberto Togni
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "ad_internal.h"
36 static const ad_info_t info
=
38 "DTS decoding with libdca",
47 #define DTSBUFFER_SIZE 18726
48 #define HEADER_SIZE 14
50 #define CONVERT_LEVEL 1
51 #define CONVERT_BIAS 0
53 static const char ch2flags
[6] = {
62 static inline int16_t convert(sample_t s
)
66 return (i
> 32767) ? 32767 : ((i
< -32768) ? -32768 : i
);
69 static void convert2s16_multi(sample_t
*f
, int16_t *s16
, int flags
, int ch_out
)
73 switch(flags
& (DTS_CHANNEL_MASK
| DTS_LFE
)){
76 for(i
= 0; i
< 256; i
++)
77 s16
[i
] = convert(f
[i
]);
79 for(i
= 0; i
< 256; i
++){
80 s16
[5*i
] = s16
[5*i
+1] = s16
[5*i
+2] = s16
[5*i
+3] = 0;
81 s16
[5*i
+4] = convert(f
[i
]);
87 for(i
= 0; i
< 256; i
++){
88 s16
[2*i
] = convert(f
[i
]);
89 s16
[2*i
+1] = convert(f
[i
+256]);
93 for(i
= 0; i
< 256; i
++){
94 s16
[3*i
] = convert(f
[i
+256]);
95 s16
[3*i
+1] = convert(f
[i
+512]);
96 s16
[3*i
+2] = convert(f
[i
]);
100 for(i
= 0; i
< 256; i
++){
101 s16
[4*i
] = convert(f
[i
]);
102 s16
[4*i
+1] = convert(f
[i
+256]);
103 s16
[4*i
+2] = convert(f
[i
+512]);
104 s16
[4*i
+3] = convert(f
[i
+768]);
108 for(i
= 0; i
< 256; i
++){
109 s16
[5*i
] = convert(f
[i
+256]);
110 s16
[5*i
+1] = convert(f
[i
+512]);
111 s16
[5*i
+2] = convert(f
[i
+768]);
112 s16
[5*i
+3] = convert(f
[i
+1024]);
113 s16
[5*i
+4] = convert(f
[i
]);
116 case DTS_MONO
| DTS_LFE
:
117 for(i
= 0; i
< 256; i
++){
118 s16
[6*i
] = s16
[6*i
+1] = s16
[6*i
+2] = s16
[6*i
+3] = 0;
119 s16
[6*i
+4] = convert(f
[i
]);
120 s16
[6*i
+5] = convert(f
[i
+256]);
123 case DTS_CHANNEL
| DTS_LFE
:
124 case DTS_STEREO
| DTS_LFE
:
125 case DTS_DOLBY
| DTS_LFE
:
126 for(i
= 0; i
< 256; i
++){
127 s16
[6*i
] = convert(f
[i
]);
128 s16
[6*i
+1] = convert(f
[i
+256]);
129 s16
[6*i
+2] = s16
[6*i
+3] = s16
[6*i
+4] = 0;
130 s16
[6*i
+5] = convert(f
[i
+512]);
133 case DTS_3F
| DTS_LFE
:
134 for(i
= 0; i
< 256; i
++){
135 s16
[6*i
] = convert(f
[i
+256]);
136 s16
[6*i
+1] = convert(f
[i
+512]);
137 s16
[6*i
+2] = s16
[6*i
+3] = 0;
138 s16
[6*i
+4] = convert(f
[i
]);
139 s16
[6*i
+5] = convert(f
[i
+768]);
142 case DTS_2F2R
| DTS_LFE
:
143 for(i
= 0; i
< 256; i
++){
144 s16
[6*i
] = convert(f
[i
]);
145 s16
[6*i
+1] = convert(f
[i
+256]);
146 s16
[6*i
+2] = convert(f
[i
+512]);
147 s16
[6*i
+3] = convert(f
[i
+768]);
149 s16
[6*i
+5] = convert(f
[1024]);
152 case DTS_3F2R
| DTS_LFE
:
153 for(i
= 0; i
< 256; i
++){
154 s16
[6*i
] = convert(f
[i
+256]);
155 s16
[6*i
+1] = convert(f
[i
+512]);
156 s16
[6*i
+2] = convert(f
[i
+768]);
157 s16
[6*i
+3] = convert(f
[i
+1024]);
158 s16
[6*i
+4] = convert(f
[i
]);
159 s16
[6*i
+5] = convert(f
[i
+1280]);
165 static void channels_info(int flags
)
170 if (flags
& DTS_LFE
) {
172 strcpy(lfestr
, "+lfe");
174 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "DTS: ");
175 switch(flags
& DTS_CHANNEL_MASK
){
177 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "1.%d (mono%s)", lfe
, lfestr
);
180 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "2.%d (channel%s)", lfe
, lfestr
);
183 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "2.%d (stereo%s)", lfe
, lfestr
);
186 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "3.%d (3f%s)", lfe
, lfestr
);
189 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "4.%d (2f+2r%s)", lfe
, lfestr
);
192 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "5.%d (3f+2r%s)", lfe
, lfestr
);
195 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "x.%d (unknown%s)", lfe
, lfestr
);
197 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "\n");
200 static int dts_sync(sh_audio_t
*sh
, int *flags
)
202 dts_state_t
*s
= sh
->context
;
208 sh
->a_in_buffer_len
=0;
211 while(sh
->a_in_buffer_len
< HEADER_SIZE
) {
212 int c
= demux_getc(sh
->ds
);
216 sh
->a_in_buffer
[sh
->a_in_buffer_len
++] = c
;
219 length
= dts_syncinfo(s
, sh
->a_in_buffer
, flags
, &sample_rate
,
220 &bit_rate
, &frame_length
);
222 if(length
>= HEADER_SIZE
)
225 // mp_msg(MSGT_DECAUDIO, MSGL_V, "skip\n");
226 memmove(sh
->a_in_buffer
, sh
->a_in_buffer
+1, HEADER_SIZE
-1);
227 --sh
->a_in_buffer_len
;
230 demux_read_data(sh
->ds
, sh
->a_in_buffer
+ HEADER_SIZE
, length
- HEADER_SIZE
);
232 sh
->samplerate
= sample_rate
;
233 sh
->i_bps
= bit_rate
/8;
238 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
, int minlen
, int maxlen
)
240 dts_state_t
*s
= sh
->context
;
241 int16_t *out_samples
= (int16_t*)buf
;
249 if(!sh
->a_in_buffer_len
)
250 if(dts_sync(sh
, &flags
) < 0) return -1; /* EOF */
251 sh
->a_in_buffer_len
=0;
253 flags
&= ~(DTS_CHANNEL_MASK
| DTS_LFE
);
254 flags
|= ch2flags
[sh
->channels
- 1];
256 level
= CONVERT_LEVEL
;
258 flags
|= DTS_ADJUST_LEVEL
;
259 if(dts_frame(s
, sh
->a_in_buffer
, &flags
, &level
, bias
)) {
260 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "dts_frame() failed\n");
264 nblocks
= dts_blocks_num(s
);
266 for(i
= 0; i
< nblocks
; i
++) {
268 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "dts_block() failed\n");
272 convert2s16_multi(dts_samples(s
), out_samples
, flags
, sh
->channels
);
274 out_samples
+= 256 * sh
->channels
;
275 data_size
+= 256 * sizeof(int16_t) * sh
->channels
;
282 static int preinit(sh_audio_t
*sh
)
284 struct MPOpts
*opts
= sh
->opts
;
286 /* 256 = samples per block, 16 = max number of blocks */
287 sh
->audio_out_minsize
= opts
->audio_output_channels
* sizeof(int16_t) * 256 * 16;
288 sh
->audio_in_minsize
= DTSBUFFER_SIZE
;
294 static int init(sh_audio_t
*sh
)
302 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "dts_init() failed\n");
307 if(dts_sync(sh
, &flags
) < 0) {
308 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "dts sync failed\n");
312 channels_info(flags
);
314 assert(opts
->audio_output_channels
>= 1 && opts
->audio_output_channels
<= 6);
315 sh
->channels
= opts
->audio_output_channels
;
317 decoded_bytes
= decode_audio(sh
, sh
->a_buffer
, 1, sh
->a_buffer_size
);
318 if(decoded_bytes
> 0)
319 sh
->a_buffer_len
= decoded_bytes
;
321 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "dts decode failed on first frame (up/downmix problem?)\n");
329 static void uninit(sh_audio_t
*sh
)
331 dts_state_t
*s
= sh
->context
;
336 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
341 case ADCTRL_RESYNC_STREAM
:
342 dts_sync(sh
, &flags
);
345 return CONTROL_UNKNOWN
;