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.

302 lines
9.5KB

  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 "libavutil/log.h"
  27. #include "get_bits.h"
  28. #include "parser.h"
  29. #include "xiph.h"
  30. #include "vorbis_parser.h"
  31. static const AVClass vorbis_parser_class = {
  32. .class_name = "Vorbis parser",
  33. .item_name = av_default_item_name,
  34. .version = LIBAVUTIL_VERSION_INT,
  35. };
  36. static int parse_id_header(VorbisParseContext *s,
  37. const uint8_t *buf, int buf_size)
  38. {
  39. /* Id header should be 30 bytes */
  40. if (buf_size < 30) {
  41. av_log(s, AV_LOG_ERROR, "Id header is too short\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. /* make sure this is the Id header */
  45. if (buf[0] != 1) {
  46. av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
  47. return AVERROR_INVALIDDATA;
  48. }
  49. /* check for header signature */
  50. if (memcmp(&buf[1], "vorbis", 6)) {
  51. av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
  52. return AVERROR_INVALIDDATA;
  53. }
  54. if (!(buf[29] & 0x1)) {
  55. av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
  56. return AVERROR_INVALIDDATA;
  57. }
  58. s->blocksize[0] = 1 << (buf[28] & 0xF);
  59. s->blocksize[1] = 1 << (buf[28] >> 4);
  60. return 0;
  61. }
  62. static int parse_setup_header(VorbisParseContext *s,
  63. const uint8_t *buf, int buf_size)
  64. {
  65. GetBitContext gb, gb0;
  66. uint8_t *rev_buf;
  67. int i, ret = 0;
  68. int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
  69. /* avoid overread */
  70. if (buf_size < 7) {
  71. av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
  72. return AVERROR_INVALIDDATA;
  73. }
  74. /* make sure this is the Setup header */
  75. if (buf[0] != 5) {
  76. av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
  77. return AVERROR_INVALIDDATA;
  78. }
  79. /* check for header signature */
  80. if (memcmp(&buf[1], "vorbis", 6)) {
  81. av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
  82. return AVERROR_INVALIDDATA;
  83. }
  84. /* reverse bytes so we can easily read backwards with get_bits() */
  85. if (!(rev_buf = av_malloc(buf_size))) {
  86. av_log(s, AV_LOG_ERROR, "Out of memory\n");
  87. return AVERROR(ENOMEM);
  88. }
  89. for (i = 0; i < buf_size; i++)
  90. rev_buf[i] = buf[buf_size - 1 - i];
  91. init_get_bits(&gb, rev_buf, buf_size * 8);
  92. got_framing_bit = 0;
  93. while (get_bits_left(&gb) > 97) {
  94. if (get_bits1(&gb)) {
  95. got_framing_bit = get_bits_count(&gb);
  96. break;
  97. }
  98. }
  99. if (!got_framing_bit) {
  100. av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
  101. ret = AVERROR_INVALIDDATA;
  102. goto bad_header;
  103. }
  104. /* Now we search backwards to find possible valid mode counts. This is not
  105. * fool-proof because we could have false positive matches and read too
  106. * far, but there isn't really any way to be sure without parsing through
  107. * all the many variable-sized fields before the modes. This approach seems
  108. * to work well in testing, and it is similar to how it is handled in
  109. * liboggz. */
  110. mode_count = 0;
  111. got_mode_header = 0;
  112. while (get_bits_left(&gb) >= 97) {
  113. if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
  114. break;
  115. skip_bits(&gb, 1);
  116. mode_count++;
  117. if (mode_count > 64)
  118. break;
  119. gb0 = gb;
  120. if (get_bits(&gb0, 6) + 1 == mode_count) {
  121. got_mode_header = 1;
  122. last_mode_count = mode_count;
  123. }
  124. }
  125. if (!got_mode_header) {
  126. av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
  127. ret = AVERROR_INVALIDDATA;
  128. goto bad_header;
  129. }
  130. /* All samples I've seen use <= 2 modes, so ask for a sample if we find
  131. * more than that, as it is most likely a false positive. If we get any
  132. * we may need to approach this the long way and parse the whole Setup
  133. * header, but I hope very much that it never comes to that. */
  134. if (last_mode_count > 2) {
  135. avpriv_request_sample(s,
  136. "%d modes (either a false positive or a "
  137. "sample from an unknown encoder)",
  138. last_mode_count);
  139. }
  140. /* We're limiting the mode count to 63 so that we know that the previous
  141. * block flag will be in the first packet byte. */
  142. if (last_mode_count > 63) {
  143. av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
  144. last_mode_count);
  145. ret = AVERROR_INVALIDDATA;
  146. goto bad_header;
  147. }
  148. s->mode_count = mode_count = last_mode_count;
  149. /* Determine the number of bits required to code the mode and turn that
  150. * into a bitmask to directly access the mode from the first frame byte. */
  151. s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
  152. /* The previous window flag is the next bit after the mode */
  153. s->prev_mask = (s->mode_mask | 0x1) + 1;
  154. init_get_bits(&gb, rev_buf, buf_size * 8);
  155. skip_bits_long(&gb, got_framing_bit);
  156. for (i = mode_count - 1; i >= 0; i--) {
  157. skip_bits_long(&gb, 40);
  158. s->mode_blocksize[i] = get_bits1(&gb);
  159. }
  160. bad_header:
  161. av_free(rev_buf);
  162. return ret;
  163. }
  164. int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
  165. {
  166. uint8_t *header_start[3];
  167. int header_len[3];
  168. int ret;
  169. s->class = &vorbis_parser_class;
  170. s->extradata_parsed = 1;
  171. if ((ret = avpriv_split_xiph_headers(avctx->extradata,
  172. avctx->extradata_size, 30,
  173. header_start, header_len)) < 0) {
  174. av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
  175. return ret;
  176. }
  177. if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
  178. return ret;
  179. if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
  180. return ret;
  181. s->valid_extradata = 1;
  182. s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
  183. return 0;
  184. }
  185. int avpriv_vorbis_parse_frame_flags(VorbisParseContext *s, const uint8_t *buf,
  186. int buf_size, int *flags)
  187. {
  188. int duration = 0;
  189. if (s->valid_extradata && buf_size > 0) {
  190. int mode, current_blocksize;
  191. int previous_blocksize = s->previous_blocksize;
  192. if (buf[0] & 1) {
  193. /* If the user doesn't care about special packets, it's a bad one. */
  194. if (!flags)
  195. goto bad_packet;
  196. /* Set the flag for which kind of special packet it is. */
  197. if (buf[0] == 1)
  198. *flags |= VORBIS_FLAG_HEADER;
  199. else if (buf[0] == 3)
  200. *flags |= VORBIS_FLAG_COMMENT;
  201. else
  202. goto bad_packet;
  203. /* Special packets have no duration. */
  204. return 0;
  205. bad_packet:
  206. av_log(s, AV_LOG_ERROR, "Invalid packet\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. if (s->mode_count == 1)
  210. mode = 0;
  211. else
  212. mode = (buf[0] & s->mode_mask) >> 1;
  213. if (mode >= s->mode_count) {
  214. av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
  215. return AVERROR_INVALIDDATA;
  216. }
  217. if(s->mode_blocksize[mode]){
  218. int flag = !!(buf[0] & s->prev_mask);
  219. previous_blocksize = s->blocksize[flag];
  220. }
  221. current_blocksize = s->blocksize[s->mode_blocksize[mode]];
  222. duration = (previous_blocksize + current_blocksize) >> 2;
  223. s->previous_blocksize = current_blocksize;
  224. }
  225. return duration;
  226. }
  227. int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
  228. int buf_size)
  229. {
  230. return avpriv_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
  231. }
  232. void avpriv_vorbis_parse_reset(VorbisParseContext *s)
  233. {
  234. if (s->valid_extradata)
  235. s->previous_blocksize = s->blocksize[0];
  236. }
  237. #if CONFIG_VORBIS_PARSER
  238. static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
  239. const uint8_t **poutbuf, int *poutbuf_size,
  240. const uint8_t *buf, int buf_size)
  241. {
  242. VorbisParseContext *s = s1->priv_data;
  243. int duration;
  244. if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
  245. if (avpriv_vorbis_parse_extradata(avctx, s))
  246. goto end;
  247. if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
  248. s1->duration = duration;
  249. end:
  250. /* always return the full packet. this parser isn't doing any splitting or
  251. combining, only packet analysis */
  252. *poutbuf = buf;
  253. *poutbuf_size = buf_size;
  254. return buf_size;
  255. }
  256. AVCodecParser ff_vorbis_parser = {
  257. .codec_ids = { AV_CODEC_ID_VORBIS },
  258. .priv_data_size = sizeof(VorbisParseContext),
  259. .parser_parse = vorbis_parse,
  260. };
  261. #endif /* CONFIG_VORBIS_PARSER */