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.

313 lines
10KB

  1. /*
  2. * DCA parser
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "dca.h"
  25. #include "dca_exss.h"
  26. #include "dca_syncwords.h"
  27. #include "get_bits.h"
  28. #include "parser.h"
  29. typedef struct DCAParseContext {
  30. ParseContext pc;
  31. uint32_t lastmarker;
  32. int size;
  33. int framesize;
  34. DCAExssParser exss;
  35. unsigned int sr_code;
  36. } DCAParseContext;
  37. #define IS_CORE_MARKER(state) \
  38. (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
  39. ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
  40. ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
  41. ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
  42. #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
  43. #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
  44. #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
  45. #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
  46. #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
  47. #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
  48. #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
  49. #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
  50. ((state >> 5) & 0xFFFFF) + 1 : \
  51. ((state >> 13) & 0x0FFFF) + 1)
  52. /**
  53. * Find the end of the current frame in the bitstream.
  54. * @return the position of the first byte of the next frame, or -1
  55. */
  56. static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
  57. int buf_size)
  58. {
  59. int start_found, size, i;
  60. uint64_t state;
  61. ParseContext *pc = &pc1->pc;
  62. start_found = pc->frame_start_found;
  63. state = pc->state64;
  64. size = pc1->size;
  65. i = 0;
  66. if (!start_found) {
  67. for (i = 0; i < buf_size; i++) {
  68. state = (state << 8) | buf[i];
  69. if (IS_MARKER(state)) {
  70. if (!pc1->lastmarker ||
  71. pc1->lastmarker == CORE_MARKER(state) ||
  72. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM) {
  73. start_found = 1;
  74. if (IS_EXSS_MARKER(state))
  75. pc1->lastmarker = EXSS_MARKER(state);
  76. else
  77. pc1->lastmarker = CORE_MARKER(state);
  78. i++;
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. if (start_found) {
  85. for (; i < buf_size; i++) {
  86. size++;
  87. state = (state << 8) | buf[i];
  88. if (start_found == 1) {
  89. switch (pc1->lastmarker) {
  90. case DCA_SYNCWORD_CORE_BE:
  91. if (size == 2) {
  92. pc1->framesize = CORE_FRAMESIZE(state);
  93. start_found = 2;
  94. }
  95. break;
  96. case DCA_SYNCWORD_CORE_LE:
  97. if (size == 2) {
  98. pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
  99. start_found = 2;
  100. }
  101. break;
  102. case DCA_SYNCWORD_CORE_14B_BE:
  103. if (size == 4) {
  104. pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
  105. start_found = 2;
  106. }
  107. break;
  108. case DCA_SYNCWORD_CORE_14B_LE:
  109. if (size == 4) {
  110. pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
  111. start_found = 2;
  112. }
  113. break;
  114. case DCA_SYNCWORD_SUBSTREAM:
  115. if (size == 6) {
  116. pc1->framesize = EXSS_FRAMESIZE(state);
  117. start_found = 2;
  118. }
  119. break;
  120. default:
  121. av_assert0(0);
  122. }
  123. continue;
  124. }
  125. if (pc1->lastmarker == DCA_SYNCWORD_CORE_BE) {
  126. if (pc1->framesize > size + 2)
  127. continue;
  128. if (start_found == 2 && IS_EXSS_MARKER(state)) {
  129. pc1->framesize = size + 2;
  130. start_found = 3;
  131. continue;
  132. }
  133. if (start_found == 3) {
  134. if (size == pc1->framesize + 4) {
  135. pc1->framesize += EXSS_FRAMESIZE(state);
  136. start_found = 4;
  137. }
  138. continue;
  139. }
  140. }
  141. if (pc1->framesize > size)
  142. continue;
  143. if (IS_MARKER(state) &&
  144. (pc1->lastmarker == CORE_MARKER(state) ||
  145. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
  146. pc->frame_start_found = 0;
  147. pc->state64 = -1;
  148. pc1->size = 0;
  149. return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
  150. }
  151. }
  152. }
  153. pc->frame_start_found = start_found;
  154. pc->state64 = state;
  155. pc1->size = size;
  156. return END_NOT_FOUND;
  157. }
  158. static av_cold int dca_parse_init(AVCodecParserContext *s)
  159. {
  160. DCAParseContext *pc1 = s->priv_data;
  161. pc1->lastmarker = 0;
  162. pc1->sr_code = -1;
  163. return 0;
  164. }
  165. static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
  166. int buf_size, int *duration, int *sample_rate)
  167. {
  168. GetBitContext gb;
  169. uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  170. int ret, sample_blocks;
  171. if (buf_size < 12)
  172. return AVERROR_INVALIDDATA;
  173. if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
  174. DCAExssAsset *asset = &pc1->exss.assets[0];
  175. if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
  176. return ret;
  177. if (asset->extension_mask & DCA_EXSS_LBR) {
  178. if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
  179. return ret;
  180. if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
  181. return AVERROR_INVALIDDATA;
  182. switch (get_bits(&gb, 8)) {
  183. case 2:
  184. pc1->sr_code = get_bits(&gb, 8);
  185. case 1:
  186. break;
  187. default:
  188. return AVERROR_INVALIDDATA;
  189. }
  190. if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
  191. return AVERROR_INVALIDDATA;
  192. *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
  193. *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
  194. return 0;
  195. }
  196. if (asset->extension_mask & DCA_EXSS_XLL) {
  197. int nsamples_log2;
  198. if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
  199. return ret;
  200. if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
  201. return AVERROR_INVALIDDATA;
  202. if (get_bits(&gb, 4))
  203. return AVERROR_INVALIDDATA;
  204. skip_bits(&gb, 8);
  205. skip_bits_long(&gb, get_bits(&gb, 5) + 1);
  206. skip_bits(&gb, 4);
  207. nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
  208. if (nsamples_log2 > 24)
  209. return AVERROR_INVALIDDATA;
  210. *sample_rate = asset->max_sample_rate;
  211. *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
  212. return 0;
  213. }
  214. return AVERROR_INVALIDDATA;
  215. }
  216. if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  217. return ret;
  218. init_get_bits(&gb, hdr, 96);
  219. skip_bits_long(&gb, 39);
  220. sample_blocks = get_bits(&gb, 7) + 1;
  221. if (sample_blocks < 8)
  222. return AVERROR_INVALIDDATA;
  223. *duration = 256 * (sample_blocks / 8);
  224. skip_bits(&gb, 20);
  225. *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)];
  226. if (*sample_rate == 0)
  227. return AVERROR_INVALIDDATA;
  228. return 0;
  229. }
  230. static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  231. const uint8_t **poutbuf, int *poutbuf_size,
  232. const uint8_t *buf, int buf_size)
  233. {
  234. DCAParseContext *pc1 = s->priv_data;
  235. ParseContext *pc = &pc1->pc;
  236. int next, duration, sample_rate;
  237. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  238. next = buf_size;
  239. } else {
  240. next = dca_find_frame_end(pc1, buf, buf_size);
  241. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  242. *poutbuf = NULL;
  243. *poutbuf_size = 0;
  244. return buf_size;
  245. }
  246. }
  247. /* read the duration and sample rate from the frame header */
  248. if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate)) {
  249. if (!avctx->sample_rate)
  250. avctx->sample_rate = sample_rate;
  251. s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
  252. } else
  253. s->duration = 0;
  254. *poutbuf = buf;
  255. *poutbuf_size = buf_size;
  256. return next;
  257. }
  258. AVCodecParser ff_dca_parser = {
  259. .codec_ids = { AV_CODEC_ID_DTS },
  260. .priv_data_size = sizeof(DCAParseContext),
  261. .parser_init = dca_parse_init,
  262. .parser_parse = dca_parse,
  263. .parser_close = ff_parse_close,
  264. };