typo fixes
[mplayer/greg.git] / libmpcodecs / ad_hwac3.c
blob1b6f2da3351b494ef4062fa532db304d493ac6d4
2 // Reference: DOCS/tech/hwac3.txt !!!!!
4 /* DTS code based on "ac3/decode_dts.c" and "ac3/conversion.c" from "ogle 0.9"
5 (see http://www.dtek.chalmers.se/~dvd/)
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "config.h"
14 #ifdef USE_LIBA52
16 #include "mp_msg.h"
17 #include "help_mp.h"
19 #include "ad_internal.h"
21 #include "liba52/a52.h"
24 static int isdts = -1;
26 static ad_info_t info =
28 "AC3/DTS pass-through S/PDIF",
29 "hwac3",
30 "Nick Kurshev/Peter Schüller",
31 "???",
35 LIBAD_EXTERN(hwac3)
38 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate);
39 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf);
42 static int ac3dts_fillbuff(sh_audio_t *sh_audio)
44 int length = 0;
45 int flags = 0;
46 int sample_rate = 0;
47 int bit_rate = 0;
49 sh_audio->a_in_buffer_len = 0;
50 /* sync frame:*/
51 while(1)
53 // DTS has a 10 byte header
54 while(sh_audio->a_in_buffer_len < 10)
56 int c = demux_getc(sh_audio->ds);
57 if(c<0)
58 return -1; /* EOF*/
59 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++] = c;
62 length = dts_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
63 if(length >= 10)
65 if(isdts != 1)
67 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
68 isdts = 1;
70 break;
72 length = a52_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
73 if(length >= 7 && length <= 3840)
75 if(isdts != 0)
77 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
78 isdts = 0;
80 break; /* we're done.*/
82 /* bad file => resync*/
83 memcpy(sh_audio->a_in_buffer, sh_audio->a_in_buffer + 1, 9);
84 --sh_audio->a_in_buffer_len;
86 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "ac3dts: %s len=%d flags=0x%X %d Hz %d bit/s\n", isdts == 1 ? "DTS" : isdts == 0 ? "AC3" : "unknown", length, flags, sample_rate, bit_rate);
88 sh_audio->samplerate = sample_rate;
89 sh_audio->i_bps = bit_rate / 8;
90 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer + 10, length - 10);
91 sh_audio->a_in_buffer_len = length;
93 // TODO: is DTS also checksummed?
94 if(isdts == 0 && crc16_block(sh_audio->a_in_buffer + 2, length - 2) != 0)
95 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "a52: CRC check failed! \n");
97 return length;
101 static int preinit(sh_audio_t *sh)
103 /* Dolby AC3 audio: */
104 sh->audio_out_minsize = 128 * 32 * 2 * 2; // DTS seems to need more than AC3
105 sh->audio_in_minsize = 8192;
106 sh->channels = 2;
107 sh->samplesize = 2;
108 sh->sample_format = AF_FORMAT_AC3;
109 return 1;
112 static int init(sh_audio_t *sh_audio)
114 /* Dolby AC3 passthrough:*/
115 a52_state_t *a52_state = a52_init(0);
116 if(a52_state == NULL)
118 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "A52 init failed\n");
119 return 0;
121 if(ac3dts_fillbuff(sh_audio) < 0)
123 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "AC3/DTS sync failed\n");
124 return 0;
126 return 1;
129 static void uninit(sh_audio_t *sh)
133 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
135 switch(cmd)
137 case ADCTRL_RESYNC_STREAM:
138 case ADCTRL_SKIP_FRAME:
139 ac3dts_fillbuff(sh);
140 return CONTROL_TRUE;
142 return CONTROL_UNKNOWN;
146 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
148 int len = sh_audio->a_in_buffer_len;
150 if(len <= 0)
151 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
152 return len; /*EOF*/
153 sh_audio->a_in_buffer_len = 0;
155 if(isdts == 1)
157 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
159 else if(isdts == 0)
161 buf[0] = 0x72;
162 buf[1] = 0xF8;
163 buf[2] = 0x1F;
164 buf[3] = 0x4E;
165 buf[4] = 0x01; //(length) ? data_type : 0; /* & 0x1F; */
166 buf[5] = 0x00;
167 buf[6] = (len << 3) & 0xFF;
168 buf[7] = (len >> 5) & 0xFF;
169 #ifdef WORDS_BIGENDIAN
170 memcpy(buf + 8, sh_audio->a_in_buffer, len); // untested
171 #else
172 swab(sh_audio->a_in_buffer, buf + 8, len);
173 #endif
174 memset(buf + 8 + len, 0, 6144 - 8 - len);
176 return 6144;
178 else
179 return -1;
183 static int DTS_SAMPLEFREQS[16] =
186 8000,
187 16000,
188 32000,
189 64000,
190 128000,
191 11025,
192 22050,
193 44100,
194 88200,
195 176400,
196 12000,
197 24000,
198 48000,
199 96000,
200 192000
203 static int DTS_BITRATES[30] =
205 32000,
206 56000,
207 64000,
208 96000,
209 112000,
210 128000,
211 192000,
212 224000,
213 256000,
214 320000,
215 384000,
216 448000,
217 512000,
218 576000,
219 640000,
220 768000,
221 896000,
222 1024000,
223 1152000,
224 1280000,
225 1344000,
226 1408000,
227 1411200,
228 1472000,
229 1536000,
230 1920000,
231 2048000,
232 3072000,
233 3840000,
234 4096000
237 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
239 int ftype;
240 int surp;
241 int unknown_bit;
242 int fsize;
243 int amode;
245 if(((indata_ptr[0] << 24) | (indata_ptr[1] << 16) | (indata_ptr[2] << 8)
246 | (indata_ptr[3])) != 0x7ffe8001)
247 return -1;
249 ftype = indata_ptr[4] >> 7;
251 surp = (indata_ptr[4] >> 2) & 0x1f;
252 surp = (surp + 1) % 32;
254 unknown_bit = (indata_ptr[4] >> 1) & 0x01;
256 *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2);
257 *nblks = *nblks + 1;
259 fsize = (indata_ptr[5] & 0x03) << 12 | (indata_ptr[6] << 4) | (indata_ptr[7] >> 4);
260 fsize = fsize + 1;
262 amode = (indata_ptr[7] & 0x0f) << 2 | (indata_ptr[8] >> 6);
263 *sfreq = (indata_ptr[8] >> 2) & 0x0f;
264 *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07);
266 if(ftype != 1)
268 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
269 return -1;
272 if(*sfreq != 13)
274 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
275 return -1;
278 if((fsize > 8192) || (fsize < 96))
280 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
281 return -1;
284 if(*nblks != 8 &&
285 *nblks != 16 &&
286 *nblks != 32 &&
287 *nblks != 64 &&
288 *nblks != 128 &&
289 ftype == 1)
291 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
292 return -1;
295 return fsize;
298 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
300 int nblks;
301 int fsize;
302 int rate;
303 int sfreq;
305 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
306 if(fsize >= 0)
308 if(rate >= 0 && rate <= 29)
309 *bit_rate = DTS_BITRATES[rate];
310 else
311 *bit_rate = 0;
312 if(sfreq >= 1 && sfreq <= 15)
313 *sample_rate = DTS_SAMPLEFREQS[sfreq];
314 else
315 *sample_rate = 0;
317 return fsize;
321 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
323 int nblks;
324 int fsize;
325 int rate;
326 int sfreq;
327 int burst_len;
328 int nr_samples;
330 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
331 if(fsize < 0)
332 return -1;
334 burst_len = fsize * 8;
335 nr_samples = nblks * 32;
337 buf[0] = 0x72; buf[1] = 0xf8; /* iec 61937 */
338 buf[2] = 0x1f; buf[3] = 0x4e; /* syncword */
339 switch(nr_samples)
341 case 512:
342 buf[4] = 0x0b; /* DTS-1 (512-sample bursts) */
343 break;
344 case 1024:
345 buf[4] = 0x0c; /* DTS-2 (1024-sample bursts) */
346 break;
347 case 2048:
348 buf[4] = 0x0d; /* DTS-3 (2048-sample bursts) */
349 break;
350 default:
351 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
352 buf[4] = 0x00;
353 break;
355 buf[5] = 0; /* ?? */
356 buf[6] = (burst_len) & 0xff;
357 buf[7] = (burst_len >> 8) & 0xff;
359 if(fsize + 8 > nr_samples * 2 * 2)
361 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: more data than fits\n");
363 #ifdef WORDS_BIGENDIAN
364 memcpy(&buf[8], indata_ptr, fsize); // untested
365 #else
366 //TODO if fzise is odd, swab doesn't copy the last byte
367 swab(indata_ptr, &buf[8], fsize);
368 if (fsize & 1)
369 buf[8+fsize] = indata_ptr[fsize];
370 #endif
371 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
373 return nr_samples * 2 * 2;
375 #endif