You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
7.5KB

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