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.

353 lines
11KB

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