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.

269 lines
6.9KB

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