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.

210 lines
6.4KB

  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 "dca_parser.h"
  27. #include "get_bits.h"
  28. #include "put_bits.h"
  29. typedef struct DCAParseContext {
  30. ParseContext pc;
  31. uint32_t lastmarker;
  32. int size;
  33. int framesize;
  34. int hd_pos;
  35. } DCAParseContext;
  36. #define IS_MARKER(state, i, buf, buf_size) \
  37. ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
  38. || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
  39. || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE || state == DCA_HD_MARKER)
  40. /**
  41. * Find the end of the current frame in the bitstream.
  42. * @return the position of the first byte of the next frame, or -1
  43. */
  44. static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
  45. int buf_size)
  46. {
  47. int start_found, i;
  48. uint32_t state;
  49. ParseContext *pc = &pc1->pc;
  50. start_found = pc->frame_start_found;
  51. state = pc->state;
  52. i = 0;
  53. if (!start_found) {
  54. for (i = 0; i < buf_size; i++) {
  55. state = (state << 8) | buf[i];
  56. if (IS_MARKER(state, i, buf, buf_size)) {
  57. if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER) {
  58. start_found = 1;
  59. pc1->lastmarker = state;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. if (start_found) {
  66. for (; i < buf_size; i++) {
  67. pc1->size++;
  68. state = (state << 8) | buf[i];
  69. if (state == DCA_HD_MARKER && !pc1->hd_pos)
  70. pc1->hd_pos = pc1->size;
  71. if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
  72. if(pc1->framesize > pc1->size)
  73. continue;
  74. // 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.
  75. if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
  76. pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
  77. }
  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. int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  96. int max_size)
  97. {
  98. uint32_t mrk;
  99. int i, tmp;
  100. const uint16_t *ssrc = (const uint16_t *) src;
  101. uint16_t *sdst = (uint16_t *) dst;
  102. PutBitContext pb;
  103. if ((unsigned) src_size > (unsigned) max_size)
  104. src_size = max_size;
  105. mrk = AV_RB32(src);
  106. switch (mrk) {
  107. case DCA_MARKER_RAW_BE:
  108. memcpy(dst, src, src_size);
  109. return src_size;
  110. case DCA_MARKER_RAW_LE:
  111. for (i = 0; i < (src_size + 1) >> 1; i++)
  112. *sdst++ = av_bswap16(*ssrc++);
  113. return src_size;
  114. case DCA_MARKER_14B_BE:
  115. case DCA_MARKER_14B_LE:
  116. init_put_bits(&pb, dst, max_size);
  117. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  118. tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  119. put_bits(&pb, 14, tmp);
  120. }
  121. flush_put_bits(&pb);
  122. return (put_bits_count(&pb) + 7) >> 3;
  123. default:
  124. return AVERROR_INVALIDDATA;
  125. }
  126. }
  127. static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
  128. int *sample_rate)
  129. {
  130. GetBitContext gb;
  131. uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  132. int ret, sample_blocks, sr_code;
  133. if (buf_size < 12)
  134. return AVERROR_INVALIDDATA;
  135. if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  136. return ret;
  137. init_get_bits(&gb, hdr, 96);
  138. skip_bits_long(&gb, 39);
  139. sample_blocks = get_bits(&gb, 7) + 1;
  140. if (sample_blocks < 8)
  141. return AVERROR_INVALIDDATA;
  142. *duration = 256 * (sample_blocks / 8);
  143. skip_bits(&gb, 20);
  144. sr_code = get_bits(&gb, 4);
  145. *sample_rate = avpriv_dca_sample_rates[sr_code];
  146. if (*sample_rate == 0)
  147. return AVERROR_INVALIDDATA;
  148. return 0;
  149. }
  150. static int dca_parse(AVCodecParserContext * s,
  151. AVCodecContext * avctx,
  152. const uint8_t ** poutbuf, int *poutbuf_size,
  153. const uint8_t * buf, int buf_size)
  154. {
  155. DCAParseContext *pc1 = s->priv_data;
  156. ParseContext *pc = &pc1->pc;
  157. int next, duration, sample_rate;
  158. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  159. next = buf_size;
  160. } else {
  161. next = dca_find_frame_end(pc1, buf, buf_size);
  162. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  163. *poutbuf = NULL;
  164. *poutbuf_size = 0;
  165. return buf_size;
  166. }
  167. }
  168. /* read the duration and sample rate from the frame header */
  169. if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
  170. s->duration = duration;
  171. if (!avctx->sample_rate)
  172. avctx->sample_rate = sample_rate;
  173. } else
  174. s->duration = 0;
  175. *poutbuf = buf;
  176. *poutbuf_size = buf_size;
  177. return next;
  178. }
  179. AVCodecParser ff_dca_parser = {
  180. .codec_ids = { AV_CODEC_ID_DTS },
  181. .priv_data_size = sizeof(DCAParseContext),
  182. .parser_init = dca_parse_init,
  183. .parser_parse = dca_parse,
  184. .parser_close = ff_parse_close,
  185. };