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.

177 lines
5.3KB

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