2
0

vorbis_parser.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_internal.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(AVVorbisParseContext *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(AVVorbisParseContext *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. static int vorbis_parse_init(AVVorbisParseContext *s,
  165. const uint8_t *extradata, int extradata_size)
  166. {
  167. const uint8_t *header_start[3];
  168. int header_len[3];
  169. int ret;
  170. s->class = &vorbis_parser_class;
  171. s->extradata_parsed = 1;
  172. if ((ret = avpriv_split_xiph_headers(extradata,
  173. extradata_size, 30,
  174. header_start, header_len)) < 0) {
  175. av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
  176. return ret;
  177. }
  178. if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
  179. return ret;
  180. if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
  181. return ret;
  182. s->valid_extradata = 1;
  183. s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
  184. return 0;
  185. }
  186. int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
  187. int buf_size, int *flags)
  188. {
  189. int duration = 0;
  190. if (s->valid_extradata && buf_size > 0) {
  191. int mode, current_blocksize;
  192. int previous_blocksize = s->previous_blocksize;
  193. if (buf[0] & 1) {
  194. /* If the user doesn't care about special packets, it's a bad one. */
  195. if (!flags)
  196. goto bad_packet;
  197. /* Set the flag for which kind of special packet it is. */
  198. if (buf[0] == 1)
  199. *flags |= VORBIS_FLAG_HEADER;
  200. else if (buf[0] == 3)
  201. *flags |= VORBIS_FLAG_COMMENT;
  202. else if (buf[0] == 5)
  203. *flags |= VORBIS_FLAG_SETUP;
  204. else
  205. goto bad_packet;
  206. /* Special packets have no duration. */
  207. return 0;
  208. bad_packet:
  209. av_log(s, AV_LOG_ERROR, "Invalid packet\n");
  210. return AVERROR_INVALIDDATA;
  211. }
  212. if (s->mode_count == 1)
  213. mode = 0;
  214. else
  215. mode = (buf[0] & s->mode_mask) >> 1;
  216. if (mode >= s->mode_count) {
  217. av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. if(s->mode_blocksize[mode]){
  221. int flag = !!(buf[0] & s->prev_mask);
  222. previous_blocksize = s->blocksize[flag];
  223. }
  224. current_blocksize = s->blocksize[s->mode_blocksize[mode]];
  225. duration = (previous_blocksize + current_blocksize) >> 2;
  226. s->previous_blocksize = current_blocksize;
  227. }
  228. return duration;
  229. }
  230. int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
  231. int buf_size)
  232. {
  233. return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
  234. }
  235. void av_vorbis_parse_reset(AVVorbisParseContext *s)
  236. {
  237. if (s->valid_extradata)
  238. s->previous_blocksize = s->blocksize[0];
  239. }
  240. void av_vorbis_parse_free(AVVorbisParseContext **s)
  241. {
  242. av_freep(s);
  243. }
  244. AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
  245. int extradata_size)
  246. {
  247. AVVorbisParseContext *s = av_mallocz(sizeof(*s));
  248. int ret;
  249. if (!s)
  250. return NULL;
  251. ret = vorbis_parse_init(s, extradata, extradata_size);
  252. if (ret < 0) {
  253. av_vorbis_parse_free(&s);
  254. return NULL;
  255. }
  256. return s;
  257. }
  258. #if CONFIG_VORBIS_PARSER
  259. typedef struct VorbisParseContext {
  260. AVVorbisParseContext *vp;
  261. } VorbisParseContext;
  262. static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
  263. const uint8_t **poutbuf, int *poutbuf_size,
  264. const uint8_t *buf, int buf_size)
  265. {
  266. VorbisParseContext *s = s1->priv_data;
  267. int duration;
  268. if (!s->vp && avctx->extradata && avctx->extradata_size) {
  269. s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
  270. }
  271. if (!s->vp)
  272. goto end;
  273. if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
  274. s1->duration = duration;
  275. end:
  276. /* always return the full packet. this parser isn't doing any splitting or
  277. combining, only packet analysis */
  278. *poutbuf = buf;
  279. *poutbuf_size = buf_size;
  280. return buf_size;
  281. }
  282. static void vorbis_parser_close(AVCodecParserContext *ctx)
  283. {
  284. VorbisParseContext *s = ctx->priv_data;
  285. av_vorbis_parse_free(&s->vp);
  286. }
  287. AVCodecParser ff_vorbis_parser = {
  288. .codec_ids = { AV_CODEC_ID_VORBIS },
  289. .priv_data_size = sizeof(VorbisParseContext),
  290. .parser_parse = vorbis_parse,
  291. .parser_close = vorbis_parser_close,
  292. };
  293. #endif /* CONFIG_VORBIS_PARSER */