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.

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