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.

213 lines
6.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 "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)
  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) {
  59. start_found = 1;
  60. break;
  61. } else if (!pc1->lastmarker) {
  62. start_found = 1;
  63. pc1->lastmarker = state;
  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_HD_MARKER && !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. if(!pc1->framesize){
  79. pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
  80. }
  81. pc->frame_start_found = 0;
  82. pc->state = -1;
  83. pc1->size = 0;
  84. return i - 3;
  85. }
  86. }
  87. }
  88. pc->frame_start_found = start_found;
  89. pc->state = state;
  90. return END_NOT_FOUND;
  91. }
  92. static av_cold int dca_parse_init(AVCodecParserContext * s)
  93. {
  94. DCAParseContext *pc1 = s->priv_data;
  95. pc1->lastmarker = 0;
  96. return 0;
  97. }
  98. int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  99. int max_size)
  100. {
  101. uint32_t mrk;
  102. int i, tmp;
  103. const uint16_t *ssrc = (const uint16_t *) src;
  104. uint16_t *sdst = (uint16_t *) dst;
  105. PutBitContext pb;
  106. if ((unsigned) src_size > (unsigned) max_size)
  107. src_size = max_size;
  108. mrk = AV_RB32(src);
  109. switch (mrk) {
  110. case DCA_MARKER_RAW_BE:
  111. memcpy(dst, src, src_size);
  112. return src_size;
  113. case DCA_MARKER_RAW_LE:
  114. for (i = 0; i < (src_size + 1) >> 1; i++)
  115. *sdst++ = av_bswap16(*ssrc++);
  116. return src_size;
  117. case DCA_MARKER_14B_BE:
  118. case DCA_MARKER_14B_LE:
  119. init_put_bits(&pb, dst, max_size);
  120. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  121. tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  122. put_bits(&pb, 14, tmp);
  123. }
  124. flush_put_bits(&pb);
  125. return (put_bits_count(&pb) + 7) >> 3;
  126. default:
  127. return AVERROR_INVALIDDATA;
  128. }
  129. }
  130. static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
  131. int *sample_rate)
  132. {
  133. GetBitContext gb;
  134. uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  135. int ret, sample_blocks, sr_code;
  136. if (buf_size < 12)
  137. return AVERROR_INVALIDDATA;
  138. if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
  139. return ret;
  140. init_get_bits(&gb, hdr, 96);
  141. skip_bits_long(&gb, 39);
  142. sample_blocks = get_bits(&gb, 7) + 1;
  143. if (sample_blocks < 8)
  144. return AVERROR_INVALIDDATA;
  145. *duration = 256 * (sample_blocks / 8);
  146. skip_bits(&gb, 20);
  147. sr_code = get_bits(&gb, 4);
  148. *sample_rate = dca_sample_rates[sr_code];
  149. if (*sample_rate == 0)
  150. return AVERROR_INVALIDDATA;
  151. return 0;
  152. }
  153. static int dca_parse(AVCodecParserContext * s,
  154. AVCodecContext * avctx,
  155. const uint8_t ** poutbuf, int *poutbuf_size,
  156. const uint8_t * buf, int buf_size)
  157. {
  158. DCAParseContext *pc1 = s->priv_data;
  159. ParseContext *pc = &pc1->pc;
  160. int next, duration, sample_rate;
  161. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  162. next = buf_size;
  163. } else {
  164. next = dca_find_frame_end(pc1, buf, buf_size);
  165. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  166. *poutbuf = NULL;
  167. *poutbuf_size = 0;
  168. return buf_size;
  169. }
  170. }
  171. /* read the duration and sample rate from the frame header */
  172. if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
  173. s->duration = duration;
  174. if (!avctx->sample_rate)
  175. avctx->sample_rate = sample_rate;
  176. } else
  177. s->duration = 0;
  178. *poutbuf = buf;
  179. *poutbuf_size = buf_size;
  180. return next;
  181. }
  182. AVCodecParser ff_dca_parser = {
  183. .codec_ids = { CODEC_ID_DTS },
  184. .priv_data_size = sizeof(DCAParseContext),
  185. .parser_init = dca_parse_init,
  186. .parser_parse = dca_parse,
  187. .parser_close = ff_parse_close,
  188. };