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.

279 lines
7.1KB

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