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.

252 lines
8.1KB

  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_syncwords.h"
  26. #include "get_bits.h"
  27. #include "parser.h"
  28. typedef struct DCAParseContext {
  29. ParseContext pc;
  30. uint32_t lastmarker;
  31. int size;
  32. int framesize;
  33. } DCAParseContext;
  34. #define IS_CORE_MARKER(state) \
  35. (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
  36. ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
  37. ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
  38. ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
  39. #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
  40. #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
  41. #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
  42. #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
  43. #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
  44. #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
  45. #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
  46. #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
  47. ((state >> 5) & 0xFFFFF) + 1 : \
  48. ((state >> 13) & 0x0FFFF) + 1)
  49. /**
  50. * Find the end of the current frame in the bitstream.
  51. * @return the position of the first byte of the next frame, or -1
  52. */
  53. static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
  54. int buf_size)
  55. {
  56. int start_found, size, i;
  57. uint64_t state;
  58. ParseContext *pc = &pc1->pc;
  59. start_found = pc->frame_start_found;
  60. state = pc->state64;
  61. size = pc1->size;
  62. i = 0;
  63. if (!start_found) {
  64. for (i = 0; i < buf_size; i++) {
  65. state = (state << 8) | buf[i];
  66. if (IS_MARKER(state)) {
  67. if (!pc1->lastmarker ||
  68. pc1->lastmarker == CORE_MARKER(state) ||
  69. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM) {
  70. start_found = 1;
  71. if (IS_EXSS_MARKER(state))
  72. pc1->lastmarker = EXSS_MARKER(state);
  73. else
  74. pc1->lastmarker = CORE_MARKER(state);
  75. i++;
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. if (start_found) {
  82. for (; i < buf_size; i++) {
  83. size++;
  84. state = (state << 8) | buf[i];
  85. if (start_found == 1) {
  86. switch (pc1->lastmarker) {
  87. case DCA_SYNCWORD_CORE_BE:
  88. if (size == 2) {
  89. pc1->framesize = CORE_FRAMESIZE(state);
  90. start_found = 2;
  91. }
  92. break;
  93. case DCA_SYNCWORD_CORE_LE:
  94. if (size == 2) {
  95. pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
  96. start_found = 2;
  97. }
  98. break;
  99. case DCA_SYNCWORD_CORE_14B_BE:
  100. if (size == 4) {
  101. pc1->framesize = CORE_FRAMESIZE(STATE_14(state)) * 8 / 14 * 2;
  102. start_found = 2;
  103. }
  104. break;
  105. case DCA_SYNCWORD_CORE_14B_LE:
  106. if (size == 4) {
  107. pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state))) * 8 / 14 * 2;
  108. start_found = 2;
  109. }
  110. break;
  111. case DCA_SYNCWORD_SUBSTREAM:
  112. if (size == 6) {
  113. pc1->framesize = EXSS_FRAMESIZE(state);
  114. start_found = 2;
  115. }
  116. break;
  117. default:
  118. av_assert0(0);
  119. }
  120. continue;
  121. }
  122. if (pc1->lastmarker == DCA_SYNCWORD_CORE_BE) {
  123. if (pc1->framesize > size + 2)
  124. continue;
  125. if (start_found == 2 && IS_EXSS_MARKER(state)) {
  126. pc1->framesize = size + 2;
  127. start_found = 3;
  128. continue;
  129. }
  130. if (start_found == 3) {
  131. if (size == pc1->framesize + 4) {
  132. pc1->framesize += EXSS_FRAMESIZE(state);
  133. start_found = 4;
  134. }
  135. continue;
  136. }
  137. }
  138. if (pc1->framesize > size)
  139. continue;
  140. if (IS_MARKER(state) &&
  141. (pc1->lastmarker == CORE_MARKER(state) ||
  142. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
  143. pc->frame_start_found = 0;
  144. pc->state64 = -1;
  145. pc1->size = 0;
  146. return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
  147. }
  148. }
  149. }
  150. pc->frame_start_found = start_found;
  151. pc->state64 = state;
  152. pc1->size = size;
  153. return END_NOT_FOUND;
  154. }
  155. static av_cold int dca_parse_init(AVCodecParserContext *s)
  156. {
  157. DCAParseContext *pc1 = s->priv_data;
  158. pc1->lastmarker = 0;
  159. return 0;
  160. }
  161. static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
  162. int *sample_rate)
  163. {
  164. GetBitContext gb;
  165. uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  166. int ret, sample_blocks;
  167. if (buf_size < 12)
  168. return AVERROR_INVALIDDATA;
  169. if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  170. return ret;
  171. init_get_bits(&gb, hdr, 96);
  172. skip_bits_long(&gb, 39);
  173. sample_blocks = get_bits(&gb, 7) + 1;
  174. if (sample_blocks < 8)
  175. return AVERROR_INVALIDDATA;
  176. *duration = 256 * (sample_blocks / 8);
  177. skip_bits(&gb, 20);
  178. *sample_rate = avpriv_dca_sample_rates[get_bits(&gb, 4)];
  179. if (*sample_rate == 0)
  180. return AVERROR_INVALIDDATA;
  181. return 0;
  182. }
  183. static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  184. const uint8_t **poutbuf, int *poutbuf_size,
  185. const uint8_t *buf, int buf_size)
  186. {
  187. DCAParseContext *pc1 = s->priv_data;
  188. ParseContext *pc = &pc1->pc;
  189. int next, duration, sample_rate;
  190. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  191. next = buf_size;
  192. } else {
  193. next = dca_find_frame_end(pc1, buf, buf_size);
  194. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  195. *poutbuf = NULL;
  196. *poutbuf_size = 0;
  197. return buf_size;
  198. }
  199. }
  200. /* read the duration and sample rate from the frame header */
  201. if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
  202. if (!avctx->sample_rate)
  203. avctx->sample_rate = sample_rate;
  204. s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
  205. } else
  206. s->duration = 0;
  207. *poutbuf = buf;
  208. *poutbuf_size = buf_size;
  209. return next;
  210. }
  211. AVCodecParser ff_dca_parser = {
  212. .codec_ids = { AV_CODEC_ID_DTS },
  213. .priv_data_size = sizeof(DCAParseContext),
  214. .parser_init = dca_parse_init,
  215. .parser_parse = dca_parse,
  216. .parser_close = ff_parse_close,
  217. };