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.

272 lines
8.7KB

  1. /*
  2. * Copyright (c) 2012 Justin Ruggles
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Vorbis audio parser
  23. *
  24. * Determines the duration for each packet.
  25. */
  26. #include "get_bits.h"
  27. #include "parser.h"
  28. #include "xiph.h"
  29. #include "vorbis_parser.h"
  30. static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
  31. const uint8_t *buf, int buf_size)
  32. {
  33. /* Id header should be 30 bytes */
  34. if (buf_size < 30) {
  35. av_log(avctx, AV_LOG_ERROR, "Id header is too short\n");
  36. return AVERROR_INVALIDDATA;
  37. }
  38. /* make sure this is the Id header */
  39. if (buf[0] != 1) {
  40. av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Id header\n");
  41. return AVERROR_INVALIDDATA;
  42. }
  43. /* check for header signature */
  44. if (memcmp(&buf[1], "vorbis", 6)) {
  45. av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
  46. return AVERROR_INVALIDDATA;
  47. }
  48. if (!(buf[29] & 0x1)) {
  49. av_log(avctx, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
  50. return AVERROR_INVALIDDATA;
  51. }
  52. s->blocksize[0] = 1 << (buf[28] & 0xF);
  53. s->blocksize[1] = 1 << (buf[28] >> 4);
  54. return 0;
  55. }
  56. static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
  57. const uint8_t *buf, int buf_size)
  58. {
  59. GetBitContext gb, gb0;
  60. uint8_t *rev_buf;
  61. int i, ret = 0;
  62. int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
  63. /* avoid overread */
  64. if (buf_size < 7) {
  65. av_log(avctx, AV_LOG_ERROR, "Setup header is too short\n");
  66. return AVERROR_INVALIDDATA;
  67. }
  68. /* make sure this is the Setup header */
  69. if (buf[0] != 5) {
  70. av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
  71. return AVERROR_INVALIDDATA;
  72. }
  73. /* check for header signature */
  74. if (memcmp(&buf[1], "vorbis", 6)) {
  75. av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. /* reverse bytes so we can easily read backwards with get_bits() */
  79. if (!(rev_buf = av_malloc(buf_size))) {
  80. av_log(avctx, AV_LOG_ERROR, "Out of memory\n");
  81. return AVERROR(ENOMEM);
  82. }
  83. for (i = 0; i < buf_size; i++)
  84. rev_buf[i] = buf[buf_size - 1 - i];
  85. init_get_bits(&gb, rev_buf, buf_size * 8);
  86. got_framing_bit = 0;
  87. while (get_bits_left(&gb) > 97) {
  88. if (get_bits1(&gb)) {
  89. got_framing_bit = get_bits_count(&gb);
  90. break;
  91. }
  92. }
  93. if (!got_framing_bit) {
  94. av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
  95. ret = AVERROR_INVALIDDATA;
  96. goto bad_header;
  97. }
  98. /* Now we search backwards to find possible valid mode counts. This is not
  99. * fool-proof because we could have false positive matches and read too
  100. * far, but there isn't really any way to be sure without parsing through
  101. * all the many variable-sized fields before the modes. This approach seems
  102. * to work well in testing, and it is similar to how it is handled in
  103. * liboggz. */
  104. mode_count = 0;
  105. got_mode_header = 0;
  106. while (get_bits_left(&gb) >= 97) {
  107. if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
  108. break;
  109. skip_bits(&gb, 1);
  110. mode_count++;
  111. if (mode_count > 64)
  112. break;
  113. gb0 = gb;
  114. if (get_bits(&gb0, 6) + 1 == mode_count) {
  115. got_mode_header = 1;
  116. last_mode_count = mode_count;
  117. }
  118. }
  119. if (!got_mode_header) {
  120. av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
  121. ret = AVERROR_INVALIDDATA;
  122. goto bad_header;
  123. }
  124. /* All samples I've seen use <= 2 modes, so ask for a sample if we find
  125. * more than that, as it is most likely a false positive. If we get any
  126. * we may need to approach this the long way and parse the whole Setup
  127. * header, but I hope very much that it never comes to that. */
  128. if (last_mode_count > 2) {
  129. avpriv_request_sample(avctx,
  130. "%d modes (either a false positive or a "
  131. "sample from an unknown encoder)",
  132. last_mode_count);
  133. }
  134. /* We're limiting the mode count to 63 so that we know that the previous
  135. * block flag will be in the first packet byte. */
  136. if (last_mode_count > 63) {
  137. av_log(avctx, AV_LOG_ERROR, "Unsupported mode count: %d\n",
  138. last_mode_count);
  139. ret = AVERROR_INVALIDDATA;
  140. goto bad_header;
  141. }
  142. s->mode_count = mode_count = last_mode_count;
  143. /* Determine the number of bits required to code the mode and turn that
  144. * into a bitmask to directly access the mode from the first frame byte. */
  145. s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
  146. /* The previous window flag is the next bit after the mode */
  147. s->prev_mask = (s->mode_mask | 0x1) + 1;
  148. init_get_bits(&gb, rev_buf, buf_size * 8);
  149. skip_bits_long(&gb, got_framing_bit);
  150. for (i = mode_count - 1; i >= 0; i--) {
  151. skip_bits_long(&gb, 40);
  152. s->mode_blocksize[i] = get_bits1(&gb);
  153. }
  154. bad_header:
  155. av_free(rev_buf);
  156. return ret;
  157. }
  158. int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
  159. {
  160. uint8_t *header_start[3];
  161. int header_len[3];
  162. int ret;
  163. s->avctx = avctx;
  164. s->extradata_parsed = 1;
  165. if ((ret = avpriv_split_xiph_headers(avctx->extradata,
  166. avctx->extradata_size, 30,
  167. header_start, header_len)) < 0) {
  168. av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
  169. return ret;
  170. }
  171. if ((ret = parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
  172. return ret;
  173. if ((ret = parse_setup_header(avctx, s, header_start[2], header_len[2])) < 0)
  174. return ret;
  175. s->valid_extradata = 1;
  176. s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
  177. return 0;
  178. }
  179. int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
  180. int buf_size)
  181. {
  182. int duration = 0;
  183. if (s->valid_extradata && buf_size > 0) {
  184. int mode, current_blocksize;
  185. int previous_blocksize = s->previous_blocksize;
  186. if (buf[0] & 1) {
  187. av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. if (s->mode_count == 1)
  191. mode = 0;
  192. else
  193. mode = (buf[0] & s->mode_mask) >> 1;
  194. if (mode >= s->mode_count) {
  195. av_log(s->avctx, AV_LOG_ERROR, "Invalid mode in packet\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. if(s->mode_blocksize[mode]){
  199. int flag = !!(buf[0] & s->prev_mask);
  200. previous_blocksize = s->blocksize[flag];
  201. }
  202. current_blocksize = s->blocksize[s->mode_blocksize[mode]];
  203. duration = (previous_blocksize + current_blocksize) >> 2;
  204. s->previous_blocksize = current_blocksize;
  205. }
  206. return duration;
  207. }
  208. void avpriv_vorbis_parse_reset(VorbisParseContext *s)
  209. {
  210. if (s->valid_extradata)
  211. s->previous_blocksize = s->blocksize[0];
  212. }
  213. #if CONFIG_VORBIS_PARSER
  214. static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
  215. const uint8_t **poutbuf, int *poutbuf_size,
  216. const uint8_t *buf, int buf_size)
  217. {
  218. VorbisParseContext *s = s1->priv_data;
  219. int duration;
  220. if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
  221. if (avpriv_vorbis_parse_extradata(avctx, s))
  222. goto end;
  223. if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
  224. s1->duration = duration;
  225. end:
  226. /* always return the full packet. this parser isn't doing any splitting or
  227. combining, only packet analysis */
  228. *poutbuf = buf;
  229. *poutbuf_size = buf_size;
  230. return buf_size;
  231. }
  232. AVCodecParser ff_vorbis_parser = {
  233. .codec_ids = { AV_CODEC_ID_VORBIS },
  234. .priv_data_size = sizeof(VorbisParseContext),
  235. .parser_parse = vorbis_parse,
  236. };
  237. #endif /* CONFIG_VORBIS_PARSER */