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.

411 lines
13KB

  1. /*
  2. * Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <stdlib.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/base64.h"
  27. #include "libavutil/bswap.h"
  28. #include "libavutil/dict.h"
  29. #include "libavcodec/bytestream.h"
  30. #include "libavcodec/get_bits.h"
  31. #include "libavcodec/vorbis_parser.h"
  32. #include "avformat.h"
  33. #include "flac_picture.h"
  34. #include "internal.h"
  35. #include "oggdec.h"
  36. #include "vorbiscomment.h"
  37. static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
  38. {
  39. int i, cnum, h, m, s, ms, keylen = strlen(key);
  40. AVChapter *chapter = NULL;
  41. if (keylen < 9 || sscanf(key, "CHAPTER%03d", &cnum) != 1)
  42. return 0;
  43. if (keylen <= 10) {
  44. if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
  45. return 0;
  46. avpriv_new_chapter(as, cnum, (AVRational) { 1, 1000 },
  47. ms + 1000 * (s + 60 * (m + 60 * h)),
  48. AV_NOPTS_VALUE, NULL);
  49. av_free(val);
  50. } else if (!strcmp(key + keylen - 4, "NAME")) {
  51. for (i = 0; i < as->nb_chapters; i++)
  52. if (as->chapters[i]->id == cnum) {
  53. chapter = as->chapters[i];
  54. break;
  55. }
  56. if (!chapter)
  57. return 0;
  58. av_dict_set(&chapter->metadata, "title", val, AV_DICT_DONT_STRDUP_VAL);
  59. } else
  60. return 0;
  61. av_free(key);
  62. return 1;
  63. }
  64. int ff_vorbis_comment(AVFormatContext *as, AVDictionary **m,
  65. const uint8_t *buf, int size)
  66. {
  67. const uint8_t *p = buf;
  68. const uint8_t *end = buf + size;
  69. unsigned n, j;
  70. int s;
  71. /* must have vendor_length and user_comment_list_length */
  72. if (size < 8)
  73. return AVERROR_INVALIDDATA;
  74. s = bytestream_get_le32(&p);
  75. if (end - p - 4 < s || s < 0)
  76. return AVERROR_INVALIDDATA;
  77. p += s;
  78. n = bytestream_get_le32(&p);
  79. while (end - p >= 4 && n > 0) {
  80. const char *t, *v;
  81. int tl, vl;
  82. s = bytestream_get_le32(&p);
  83. if (end - p < s || s < 0)
  84. break;
  85. t = p;
  86. p += s;
  87. n--;
  88. v = memchr(t, '=', s);
  89. if (!v)
  90. continue;
  91. tl = v - t;
  92. vl = s - tl - 1;
  93. v++;
  94. if (tl && vl) {
  95. char *tt, *ct;
  96. tt = av_malloc(tl + 1);
  97. ct = av_malloc(vl + 1);
  98. if (!tt || !ct) {
  99. av_freep(&tt);
  100. av_freep(&ct);
  101. return AVERROR(ENOMEM);
  102. }
  103. for (j = 0; j < tl; j++)
  104. tt[j] = av_toupper(t[j]);
  105. tt[tl] = 0;
  106. memcpy(ct, v, vl);
  107. ct[vl] = 0;
  108. /* The format in which the pictures are stored is the FLAC format.
  109. * Xiph says: "The binary FLAC picture structure is base64 encoded
  110. * and placed within a VorbisComment with the tag name
  111. * 'METADATA_BLOCK_PICTURE'. This is the preferred and
  112. * recommended way of embedding cover art within VorbisComments."
  113. */
  114. if (!strcmp(tt, "METADATA_BLOCK_PICTURE")) {
  115. int ret;
  116. char *pict = av_malloc(vl);
  117. if (!pict) {
  118. av_freep(&tt);
  119. av_freep(&ct);
  120. return AVERROR(ENOMEM);
  121. }
  122. if ((ret = av_base64_decode(pict, ct, vl)) > 0)
  123. ret = ff_flac_parse_picture(as, pict, ret);
  124. av_freep(&tt);
  125. av_freep(&ct);
  126. av_freep(&pict);
  127. if (ret < 0) {
  128. av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n");
  129. continue;
  130. }
  131. } else if (!ogm_chapter(as, tt, ct))
  132. av_dict_set(m, tt, ct,
  133. AV_DICT_DONT_STRDUP_KEY |
  134. AV_DICT_DONT_STRDUP_VAL);
  135. }
  136. }
  137. if (p != end)
  138. av_log(as, AV_LOG_INFO,
  139. "%ti bytes of comment header remain\n", end - p);
  140. if (n > 0)
  141. av_log(as, AV_LOG_INFO,
  142. "truncated comment header, %i comments not found\n", n);
  143. ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
  144. return 0;
  145. }
  146. /*
  147. * Parse the vorbis header
  148. *
  149. * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
  150. * [vorbis_version] = read 32 bits as unsigned integer | Not used
  151. * [audio_channels] = read 8 bit integer as unsigned | Used
  152. * [audio_sample_rate] = read 32 bits as unsigned integer | Used
  153. * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
  154. * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
  155. * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
  156. * [blocksize_0] = read 4 bits as unsigned integer | Not Used
  157. * [blocksize_1] = read 4 bits as unsigned integer | Not Used
  158. * [framing_flag] = read one bit | Not Used
  159. */
  160. struct oggvorbis_private {
  161. unsigned int len[3];
  162. unsigned char *packet[3];
  163. VorbisParseContext vp;
  164. int64_t final_pts;
  165. int final_duration;
  166. };
  167. static int fixup_vorbis_headers(AVFormatContext *as,
  168. struct oggvorbis_private *priv,
  169. uint8_t **buf)
  170. {
  171. int i, offset, len, err;
  172. unsigned char *ptr;
  173. len = priv->len[0] + priv->len[1] + priv->len[2];
  174. ptr = *buf = av_mallocz(len + len / 255 + 64);
  175. if (!ptr)
  176. return AVERROR(ENOMEM);
  177. ptr[0] = 2;
  178. offset = 1;
  179. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  180. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  181. for (i = 0; i < 3; i++) {
  182. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  183. offset += priv->len[i];
  184. av_freep(&priv->packet[i]);
  185. }
  186. if ((err = av_reallocp(buf, offset + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
  187. return err;
  188. return offset;
  189. }
  190. static void vorbis_cleanup(AVFormatContext *s, int idx)
  191. {
  192. struct ogg *ogg = s->priv_data;
  193. struct ogg_stream *os = ogg->streams + idx;
  194. struct oggvorbis_private *priv = os->private;
  195. int i;
  196. if (os->private)
  197. for (i = 0; i < 3; i++)
  198. av_freep(&priv->packet[i]);
  199. }
  200. static int vorbis_header(AVFormatContext *s, int idx)
  201. {
  202. struct ogg *ogg = s->priv_data;
  203. AVStream *st = s->streams[idx];
  204. struct ogg_stream *os = ogg->streams + idx;
  205. struct oggvorbis_private *priv;
  206. int pkt_type = os->buf[os->pstart];
  207. if (!os->private) {
  208. os->private = av_mallocz(sizeof(struct oggvorbis_private));
  209. if (!os->private)
  210. return AVERROR(ENOMEM);
  211. }
  212. if (!(pkt_type & 1))
  213. return 0;
  214. if (os->psize < 1 || pkt_type > 5)
  215. return AVERROR_INVALIDDATA;
  216. priv = os->private;
  217. if (priv->packet[pkt_type >> 1])
  218. return AVERROR_INVALIDDATA;
  219. if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
  220. return AVERROR_INVALIDDATA;
  221. priv->len[pkt_type >> 1] = os->psize;
  222. priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
  223. if (!priv->packet[pkt_type >> 1])
  224. return AVERROR(ENOMEM);
  225. memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
  226. if (os->buf[os->pstart] == 1) {
  227. const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
  228. unsigned blocksize, bs0, bs1;
  229. int srate;
  230. if (os->psize != 30)
  231. return AVERROR_INVALIDDATA;
  232. if (bytestream_get_le32(&p) != 0) /* vorbis_version */
  233. return AVERROR_INVALIDDATA;
  234. st->codec->channels = bytestream_get_byte(&p);
  235. srate = bytestream_get_le32(&p);
  236. p += 4; // skip maximum bitrate
  237. st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
  238. p += 4; // skip minimum bitrate
  239. blocksize = bytestream_get_byte(&p);
  240. bs0 = blocksize & 15;
  241. bs1 = blocksize >> 4;
  242. if (bs0 > bs1)
  243. return AVERROR_INVALIDDATA;
  244. if (bs0 < 6 || bs1 > 13)
  245. return AVERROR_INVALIDDATA;
  246. if (bytestream_get_byte(&p) != 1) /* framing_flag */
  247. return AVERROR_INVALIDDATA;
  248. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  249. st->codec->codec_id = AV_CODEC_ID_VORBIS;
  250. if (srate > 0) {
  251. st->codec->sample_rate = srate;
  252. avpriv_set_pts_info(st, 64, 1, srate);
  253. }
  254. } else if (os->buf[os->pstart] == 3) {
  255. if (os->psize > 8 &&
  256. ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7,
  257. os->psize - 8) >= 0) {
  258. // drop all metadata we parsed and which is not required by libvorbis
  259. unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
  260. if (new_len >= 16 && new_len < os->psize) {
  261. AV_WL32(priv->packet[1] + new_len - 5, 0);
  262. priv->packet[1][new_len - 1] = 1;
  263. priv->len[1] = new_len;
  264. }
  265. }
  266. } else {
  267. int ret = fixup_vorbis_headers(s, priv, &st->codec->extradata);
  268. if (ret < 0) {
  269. st->codec->extradata_size = 0;
  270. return ret;
  271. }
  272. st->codec->extradata_size = ret;
  273. if ((ret = avpriv_vorbis_parse_extradata(st->codec, &priv->vp))) {
  274. av_freep(&st->codec->extradata);
  275. st->codec->extradata_size = 0;
  276. return ret;
  277. }
  278. }
  279. return 1;
  280. }
  281. static int vorbis_packet(AVFormatContext *s, int idx)
  282. {
  283. struct ogg *ogg = s->priv_data;
  284. struct ogg_stream *os = ogg->streams + idx;
  285. struct oggvorbis_private *priv = os->private;
  286. int duration;
  287. /* first packet handling
  288. * here we parse the duration of each packet in the first page and compare
  289. * the total duration to the page granule to find the encoder delay and
  290. * set the first timestamp */
  291. if (!os->lastpts) {
  292. int seg;
  293. uint8_t *last_pkt = os->buf + os->pstart;
  294. uint8_t *next_pkt = last_pkt;
  295. int first_duration = 0;
  296. avpriv_vorbis_parse_reset(&priv->vp);
  297. duration = 0;
  298. for (seg = 0; seg < os->nsegs; seg++) {
  299. if (os->segments[seg] < 255) {
  300. int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
  301. if (d < 0) {
  302. duration = os->granule;
  303. break;
  304. }
  305. if (!duration)
  306. first_duration = d;
  307. duration += d;
  308. last_pkt = next_pkt + os->segments[seg];
  309. }
  310. next_pkt += os->segments[seg];
  311. }
  312. os->lastpts =
  313. os->lastdts = os->granule - duration;
  314. s->streams[idx]->start_time = os->lastpts + first_duration;
  315. if (s->streams[idx]->duration)
  316. s->streams[idx]->duration -= s->streams[idx]->start_time;
  317. s->streams[idx]->cur_dts = AV_NOPTS_VALUE;
  318. priv->final_pts = AV_NOPTS_VALUE;
  319. avpriv_vorbis_parse_reset(&priv->vp);
  320. }
  321. /* parse packet duration */
  322. if (os->psize > 0) {
  323. duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1);
  324. if (duration <= 0) {
  325. os->pflags |= AV_PKT_FLAG_CORRUPT;
  326. return 0;
  327. }
  328. os->pduration = duration;
  329. }
  330. /* final packet handling
  331. * here we save the pts of the first packet in the final page, sum up all
  332. * packet durations in the final page except for the last one, and compare
  333. * to the page granule to find the duration of the final packet */
  334. if (os->flags & OGG_FLAG_EOS) {
  335. if (os->lastpts != AV_NOPTS_VALUE) {
  336. priv->final_pts = os->lastpts;
  337. priv->final_duration = 0;
  338. }
  339. if (os->segp == os->nsegs)
  340. os->pduration = os->granule - priv->final_pts - priv->final_duration;
  341. priv->final_duration += os->pduration;
  342. }
  343. return 0;
  344. }
  345. const struct ogg_codec ff_vorbis_codec = {
  346. .magic = "\001vorbis",
  347. .magicsize = 7,
  348. .header = vorbis_header,
  349. .packet = vorbis_packet,
  350. .cleanup = vorbis_cleanup,
  351. .nb_header = 3,
  352. };