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.

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