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.

324 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. unsigned int startpos;
  35. DCAExssParser exss;
  36. unsigned int sr_code;
  37. } DCAParseContext;
  38. #define IS_CORE_MARKER(state) \
  39. (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
  40. ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
  41. ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
  42. ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
  43. #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
  44. #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
  45. #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
  46. #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
  47. #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
  48. #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
  49. #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
  50. #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
  51. ((state >> 5) & 0xFFFFF) + 1 : \
  52. ((state >> 13) & 0x0FFFF) + 1)
  53. /**
  54. * Find the end of the current frame in the bitstream.
  55. * @return the position of the first byte of the next frame, or -1
  56. */
  57. static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
  58. int buf_size)
  59. {
  60. int start_found, size, i;
  61. uint64_t state;
  62. ParseContext *pc = &pc1->pc;
  63. start_found = pc->frame_start_found;
  64. state = pc->state64;
  65. size = pc1->size;
  66. i = 0;
  67. if (!start_found) {
  68. for (; i < buf_size; i++) {
  69. size++;
  70. state = (state << 8) | buf[i];
  71. if (IS_MARKER(state) &&
  72. (!pc1->lastmarker ||
  73. pc1->lastmarker == CORE_MARKER(state) ||
  74. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
  75. if (!pc1->lastmarker)
  76. pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
  77. if (IS_EXSS_MARKER(state))
  78. pc1->lastmarker = EXSS_MARKER(state);
  79. else
  80. pc1->lastmarker = CORE_MARKER(state);
  81. start_found = 1;
  82. size = 0;
  83. i++;
  84. break;
  85. }
  86. }
  87. }
  88. if (start_found) {
  89. for (; i < buf_size; i++) {
  90. size++;
  91. state = (state << 8) | buf[i];
  92. if (start_found == 1) {
  93. switch (pc1->lastmarker) {
  94. case DCA_SYNCWORD_CORE_BE:
  95. if (size == 2) {
  96. pc1->framesize = CORE_FRAMESIZE(state);
  97. start_found = 2;
  98. }
  99. break;
  100. case DCA_SYNCWORD_CORE_LE:
  101. if (size == 2) {
  102. pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
  103. start_found = 4;
  104. }
  105. break;
  106. case DCA_SYNCWORD_CORE_14B_BE:
  107. if (size == 4) {
  108. pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
  109. start_found = 4;
  110. }
  111. break;
  112. case DCA_SYNCWORD_CORE_14B_LE:
  113. if (size == 4) {
  114. pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
  115. start_found = 4;
  116. }
  117. break;
  118. case DCA_SYNCWORD_SUBSTREAM:
  119. if (size == 6) {
  120. pc1->framesize = EXSS_FRAMESIZE(state);
  121. start_found = 4;
  122. }
  123. break;
  124. default:
  125. av_assert0(0);
  126. }
  127. continue;
  128. }
  129. if (start_found == 2 && IS_EXSS_MARKER(state) &&
  130. pc1->framesize <= size + 2) {
  131. pc1->framesize = size + 2;
  132. start_found = 3;
  133. continue;
  134. }
  135. if (start_found == 3) {
  136. if (size == pc1->framesize + 4) {
  137. pc1->framesize += EXSS_FRAMESIZE(state);
  138. start_found = 4;
  139. }
  140. continue;
  141. }
  142. if (pc1->framesize > size)
  143. continue;
  144. if (IS_MARKER(state) &&
  145. (pc1->lastmarker == CORE_MARKER(state) ||
  146. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
  147. pc->frame_start_found = 0;
  148. pc->state64 = -1;
  149. pc1->size = 0;
  150. return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
  151. }
  152. }
  153. }
  154. pc->frame_start_found = start_found;
  155. pc->state64 = state;
  156. pc1->size = size;
  157. return END_NOT_FOUND;
  158. }
  159. static av_cold int dca_parse_init(AVCodecParserContext *s)
  160. {
  161. DCAParseContext *pc1 = s->priv_data;
  162. pc1->lastmarker = 0;
  163. pc1->sr_code = -1;
  164. return 0;
  165. }
  166. static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
  167. int buf_size, int *duration, int *sample_rate)
  168. {
  169. GetBitContext gb;
  170. uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  171. int ret, sample_blocks;
  172. if (buf_size < 12)
  173. return AVERROR_INVALIDDATA;
  174. if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
  175. DCAExssAsset *asset = &pc1->exss.assets[0];
  176. if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
  177. return ret;
  178. if (asset->extension_mask & DCA_EXSS_LBR) {
  179. if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
  180. return ret;
  181. if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
  182. return AVERROR_INVALIDDATA;
  183. switch (get_bits(&gb, 8)) {
  184. case 2:
  185. pc1->sr_code = get_bits(&gb, 8);
  186. case 1:
  187. break;
  188. default:
  189. return AVERROR_INVALIDDATA;
  190. }
  191. if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
  192. return AVERROR_INVALIDDATA;
  193. *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
  194. *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
  195. return 0;
  196. }
  197. if (asset->extension_mask & DCA_EXSS_XLL) {
  198. int nsamples_log2;
  199. if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
  200. return ret;
  201. if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
  202. return AVERROR_INVALIDDATA;
  203. if (get_bits(&gb, 4))
  204. return AVERROR_INVALIDDATA;
  205. skip_bits(&gb, 8);
  206. skip_bits_long(&gb, get_bits(&gb, 5) + 1);
  207. skip_bits(&gb, 4);
  208. nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
  209. if (nsamples_log2 > 24)
  210. return AVERROR_INVALIDDATA;
  211. *sample_rate = asset->max_sample_rate;
  212. *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
  213. return 0;
  214. }
  215. return AVERROR_INVALIDDATA;
  216. }
  217. if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  218. return ret;
  219. init_get_bits(&gb, hdr, 96);
  220. skip_bits_long(&gb, 39);
  221. sample_blocks = get_bits(&gb, 7) + 1;
  222. if (sample_blocks < 8)
  223. return AVERROR_INVALIDDATA;
  224. *duration = 256 * (sample_blocks / 8);
  225. skip_bits(&gb, 20);
  226. *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)];
  227. if (*sample_rate == 0)
  228. return AVERROR_INVALIDDATA;
  229. return 0;
  230. }
  231. static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  232. const uint8_t **poutbuf, int *poutbuf_size,
  233. const uint8_t *buf, int buf_size)
  234. {
  235. DCAParseContext *pc1 = s->priv_data;
  236. ParseContext *pc = &pc1->pc;
  237. int next, duration, sample_rate;
  238. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  239. next = buf_size;
  240. } else {
  241. next = dca_find_frame_end(pc1, buf, buf_size);
  242. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  243. *poutbuf = NULL;
  244. *poutbuf_size = 0;
  245. return buf_size;
  246. }
  247. /* skip initial padding */
  248. if (buf_size > pc1->startpos) {
  249. buf += pc1->startpos;
  250. buf_size -= pc1->startpos;
  251. }
  252. pc1->startpos = 0;
  253. }
  254. /* read the duration and sample rate from the frame header */
  255. if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate)) {
  256. if (!avctx->sample_rate)
  257. avctx->sample_rate = sample_rate;
  258. s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
  259. } else
  260. s->duration = 0;
  261. *poutbuf = buf;
  262. *poutbuf_size = buf_size;
  263. return next;
  264. }
  265. AVCodecParser ff_dca_parser = {
  266. .codec_ids = { AV_CODEC_ID_DTS },
  267. .priv_data_size = sizeof(DCAParseContext),
  268. .parser_init = dca_parse_init,
  269. .parser_parse = dca_parse,
  270. .parser_close = ff_parse_close,
  271. };