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.

185 lines
5.7KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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. /**
  44. * Find the end of the current frame in the bitstream.
  45. * @return the position of the first byte of the next frame, or -1
  46. */
  47. static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
  48. int buf_size)
  49. {
  50. int start_found, i;
  51. uint64_t state;
  52. ParseContext *pc = &pc1->pc;
  53. start_found = pc->frame_start_found;
  54. state = pc->state64;
  55. i = 0;
  56. if (!start_found) {
  57. for (i = 0; i < buf_size; i++) {
  58. state = (state << 8) | buf[i];
  59. if (IS_MARKER(state)) {
  60. if (!pc1->lastmarker ||
  61. pc1->lastmarker == CORE_MARKER(state) ||
  62. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM) {
  63. start_found = 1;
  64. if (IS_EXSS_MARKER(state))
  65. pc1->lastmarker = EXSS_MARKER(state);
  66. else
  67. pc1->lastmarker = CORE_MARKER(state);
  68. i++;
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. if (start_found) {
  75. for (; i < buf_size; i++) {
  76. pc1->size++;
  77. state = (state << 8) | buf[i];
  78. if (IS_MARKER(state) &&
  79. (pc1->lastmarker == CORE_MARKER(state) ||
  80. pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
  81. if (pc1->framesize > pc1->size)
  82. continue;
  83. pc->frame_start_found = 0;
  84. pc->state64 = -1;
  85. pc1->size = 0;
  86. return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
  87. }
  88. }
  89. }
  90. pc->frame_start_found = start_found;
  91. pc->state64 = state;
  92. return END_NOT_FOUND;
  93. }
  94. static av_cold int dca_parse_init(AVCodecParserContext *s)
  95. {
  96. DCAParseContext *pc1 = s->priv_data;
  97. pc1->lastmarker = 0;
  98. return 0;
  99. }
  100. static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
  101. int *sample_rate, int *framesize)
  102. {
  103. GetBitContext gb;
  104. uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  105. int ret, sample_blocks, sr_code;
  106. if (buf_size < 12)
  107. return AVERROR_INVALIDDATA;
  108. if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  109. return ret;
  110. init_get_bits(&gb, hdr, 96);
  111. skip_bits_long(&gb, 39);
  112. sample_blocks = get_bits(&gb, 7) + 1;
  113. if (sample_blocks < 8)
  114. return AVERROR_INVALIDDATA;
  115. *duration = 256 * (sample_blocks / 8);
  116. *framesize = get_bits(&gb, 14) + 1;
  117. if (*framesize < 95)
  118. return AVERROR_INVALIDDATA;
  119. skip_bits(&gb, 6);
  120. sr_code = get_bits(&gb, 4);
  121. *sample_rate = avpriv_dca_sample_rates[sr_code];
  122. if (*sample_rate == 0)
  123. return AVERROR_INVALIDDATA;
  124. return 0;
  125. }
  126. static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  127. const uint8_t **poutbuf, int *poutbuf_size,
  128. const uint8_t *buf, int buf_size)
  129. {
  130. DCAParseContext *pc1 = s->priv_data;
  131. ParseContext *pc = &pc1->pc;
  132. int next, duration, sample_rate;
  133. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  134. next = buf_size;
  135. } else {
  136. next = dca_find_frame_end(pc1, buf, buf_size);
  137. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  138. *poutbuf = NULL;
  139. *poutbuf_size = 0;
  140. return buf_size;
  141. }
  142. }
  143. /* read the duration and sample rate from the frame header */
  144. if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
  145. s->duration = duration;
  146. avctx->sample_rate = sample_rate;
  147. } else
  148. s->duration = 0;
  149. *poutbuf = buf;
  150. *poutbuf_size = buf_size;
  151. return next;
  152. }
  153. AVCodecParser ff_dca_parser = {
  154. .codec_ids = { AV_CODEC_ID_DTS },
  155. .priv_data_size = sizeof(DCAParseContext),
  156. .parser_init = dca_parse_init,
  157. .parser_parse = dca_parse,
  158. .parser_close = ff_parse_close,
  159. };