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.

181 lines
5.6KB

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