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.2KB

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