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.

387 lines
12KB

  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/bswap.h"
  27. #include "libavutil/dict.h"
  28. #include "libavcodec/bytestream.h"
  29. #include "libavcodec/get_bits.h"
  30. #include "libavcodec/vorbis_parser.h"
  31. #include "avformat.h"
  32. #include "internal.h"
  33. #include "oggdec.h"
  34. #include "vorbiscomment.h"
  35. static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
  36. {
  37. int i, cnum, h, m, s, ms, keylen = strlen(key);
  38. AVChapter *chapter = NULL;
  39. if (keylen < 9 || sscanf(key, "CHAPTER%03d", &cnum) != 1)
  40. return 0;
  41. if (keylen <= 10) {
  42. if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
  43. return 0;
  44. avpriv_new_chapter(as, cnum, (AVRational) { 1, 1000 },
  45. ms + 1000 * (s + 60 * (m + 60 * h)),
  46. AV_NOPTS_VALUE, NULL);
  47. av_free(val);
  48. } else if (!strcmp(key + keylen - 4, "NAME")) {
  49. for (i = 0; i < as->nb_chapters; i++)
  50. if (as->chapters[i]->id == cnum) {
  51. chapter = as->chapters[i];
  52. break;
  53. }
  54. if (!chapter)
  55. return 0;
  56. av_dict_set(&chapter->metadata, "title", val, AV_DICT_DONT_STRDUP_VAL);
  57. } else
  58. return 0;
  59. av_free(key);
  60. return 1;
  61. }
  62. int ff_vorbis_comment(AVFormatContext *as, AVDictionary **m,
  63. const uint8_t *buf, int size)
  64. {
  65. const uint8_t *p = buf;
  66. const uint8_t *end = buf + size;
  67. unsigned n, j;
  68. int s;
  69. /* must have vendor_length and user_comment_list_length */
  70. if (size < 8)
  71. return AVERROR_INVALIDDATA;
  72. s = bytestream_get_le32(&p);
  73. if (end - p - 4 < s || s < 0)
  74. return AVERROR_INVALIDDATA;
  75. p += s;
  76. n = bytestream_get_le32(&p);
  77. while (end - p >= 4 && n > 0) {
  78. const char *t, *v;
  79. int tl, vl;
  80. s = bytestream_get_le32(&p);
  81. if (end - p < s || s < 0)
  82. break;
  83. t = p;
  84. p += s;
  85. n--;
  86. v = memchr(t, '=', s);
  87. if (!v)
  88. continue;
  89. tl = v - t;
  90. vl = s - tl - 1;
  91. v++;
  92. if (tl && vl) {
  93. char *tt, *ct;
  94. tt = av_malloc(tl + 1);
  95. ct = av_malloc(vl + 1);
  96. if (!tt || !ct) {
  97. av_freep(&tt);
  98. av_freep(&ct);
  99. av_log(as, AV_LOG_WARNING,
  100. "out-of-memory error. skipping VorbisComment tag.\n");
  101. continue;
  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. if (!ogm_chapter(as, tt, ct))
  109. av_dict_set(m, tt, ct,
  110. AV_DICT_DONT_STRDUP_KEY |
  111. AV_DICT_DONT_STRDUP_VAL);
  112. }
  113. }
  114. if (p != end)
  115. av_log(as, AV_LOG_INFO,
  116. "%ti bytes of comment header remain\n", end - p);
  117. if (n > 0)
  118. av_log(as, AV_LOG_INFO,
  119. "truncated comment header, %i comments not found\n", n);
  120. ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
  121. return 0;
  122. }
  123. /*
  124. * Parse the vorbis header
  125. *
  126. * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
  127. * [vorbis_version] = read 32 bits as unsigned integer | Not used
  128. * [audio_channels] = read 8 bit integer as unsigned | Used
  129. * [audio_sample_rate] = read 32 bits as unsigned integer | Used
  130. * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
  131. * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
  132. * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
  133. * [blocksize_0] = read 4 bits as unsigned integer | Not Used
  134. * [blocksize_1] = read 4 bits as unsigned integer | Not Used
  135. * [framing_flag] = read one bit | Not Used
  136. */
  137. struct oggvorbis_private {
  138. unsigned int len[3];
  139. unsigned char *packet[3];
  140. VorbisParseContext vp;
  141. int64_t final_pts;
  142. int final_duration;
  143. };
  144. static int fixup_vorbis_headers(AVFormatContext *as,
  145. struct oggvorbis_private *priv,
  146. uint8_t **buf)
  147. {
  148. int i, offset, len, err;
  149. unsigned char *ptr;
  150. len = priv->len[0] + priv->len[1] + priv->len[2];
  151. ptr = *buf = av_mallocz(len + len / 255 + 64);
  152. if (!ptr)
  153. return AVERROR(ENOMEM);
  154. ptr[0] = 2;
  155. offset = 1;
  156. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  157. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  158. for (i = 0; i < 3; i++) {
  159. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  160. offset += priv->len[i];
  161. av_freep(&priv->packet[i]);
  162. }
  163. if ((err = av_reallocp(buf, offset + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
  164. return err;
  165. return offset;
  166. }
  167. static void vorbis_cleanup(AVFormatContext *s, int idx)
  168. {
  169. struct ogg *ogg = s->priv_data;
  170. struct ogg_stream *os = ogg->streams + idx;
  171. struct oggvorbis_private *priv = os->private;
  172. int i;
  173. if (os->private)
  174. for (i = 0; i < 3; i++)
  175. av_freep(&priv->packet[i]);
  176. }
  177. static int vorbis_header(AVFormatContext *s, int idx)
  178. {
  179. struct ogg *ogg = s->priv_data;
  180. AVStream *st = s->streams[idx];
  181. struct ogg_stream *os = ogg->streams + idx;
  182. struct oggvorbis_private *priv;
  183. int pkt_type = os->buf[os->pstart];
  184. if (!os->private) {
  185. os->private = av_mallocz(sizeof(struct oggvorbis_private));
  186. if (!os->private)
  187. return AVERROR(ENOMEM);
  188. }
  189. if (!(pkt_type & 1))
  190. return 0;
  191. if (os->psize < 1 || pkt_type > 5)
  192. return AVERROR_INVALIDDATA;
  193. priv = os->private;
  194. if (priv->packet[pkt_type >> 1])
  195. return AVERROR_INVALIDDATA;
  196. if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
  197. return AVERROR_INVALIDDATA;
  198. priv->len[pkt_type >> 1] = os->psize;
  199. priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
  200. if (!priv->packet[pkt_type >> 1])
  201. return AVERROR(ENOMEM);
  202. memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
  203. if (os->buf[os->pstart] == 1) {
  204. const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
  205. unsigned blocksize, bs0, bs1;
  206. int srate;
  207. if (os->psize != 30)
  208. return AVERROR_INVALIDDATA;
  209. if (bytestream_get_le32(&p) != 0) /* vorbis_version */
  210. return AVERROR_INVALIDDATA;
  211. st->codec->channels = bytestream_get_byte(&p);
  212. srate = bytestream_get_le32(&p);
  213. p += 4; // skip maximum bitrate
  214. st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
  215. p += 4; // skip minimum bitrate
  216. blocksize = bytestream_get_byte(&p);
  217. bs0 = blocksize & 15;
  218. bs1 = blocksize >> 4;
  219. if (bs0 > bs1)
  220. return AVERROR_INVALIDDATA;
  221. if (bs0 < 6 || bs1 > 13)
  222. return AVERROR_INVALIDDATA;
  223. if (bytestream_get_byte(&p) != 1) /* framing_flag */
  224. return AVERROR_INVALIDDATA;
  225. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  226. st->codec->codec_id = AV_CODEC_ID_VORBIS;
  227. if (srate > 0) {
  228. st->codec->sample_rate = srate;
  229. avpriv_set_pts_info(st, 64, 1, srate);
  230. }
  231. } else if (os->buf[os->pstart] == 3) {
  232. if (os->psize > 8 &&
  233. ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7,
  234. os->psize - 8) >= 0) {
  235. // drop all metadata we parsed and which is not required by libvorbis
  236. unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
  237. if (new_len >= 16 && new_len < os->psize) {
  238. AV_WL32(priv->packet[1] + new_len - 5, 0);
  239. priv->packet[1][new_len - 1] = 1;
  240. priv->len[1] = new_len;
  241. }
  242. }
  243. } else {
  244. int ret = fixup_vorbis_headers(s, priv, &st->codec->extradata);
  245. if (ret < 0) {
  246. st->codec->extradata_size = 0;
  247. return ret;
  248. }
  249. st->codec->extradata_size = ret;
  250. if ((ret = avpriv_vorbis_parse_extradata(st->codec, &priv->vp))) {
  251. av_freep(&st->codec->extradata);
  252. st->codec->extradata_size = 0;
  253. return ret;
  254. }
  255. }
  256. return 1;
  257. }
  258. static int vorbis_packet(AVFormatContext *s, int idx)
  259. {
  260. struct ogg *ogg = s->priv_data;
  261. struct ogg_stream *os = ogg->streams + idx;
  262. struct oggvorbis_private *priv = os->private;
  263. int duration;
  264. /* first packet handling
  265. * here we parse the duration of each packet in the first page and compare
  266. * the total duration to the page granule to find the encoder delay and
  267. * set the first timestamp */
  268. if (!os->lastpts) {
  269. int seg;
  270. uint8_t *last_pkt = os->buf + os->pstart;
  271. uint8_t *next_pkt = last_pkt;
  272. int first_duration = 0;
  273. avpriv_vorbis_parse_reset(&priv->vp);
  274. duration = 0;
  275. for (seg = 0; seg < os->nsegs; seg++) {
  276. if (os->segments[seg] < 255) {
  277. int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
  278. if (d < 0) {
  279. duration = os->granule;
  280. break;
  281. }
  282. if (!duration)
  283. first_duration = d;
  284. duration += d;
  285. last_pkt = next_pkt + os->segments[seg];
  286. }
  287. next_pkt += os->segments[seg];
  288. }
  289. os->lastpts =
  290. os->lastdts = os->granule - duration;
  291. s->streams[idx]->start_time = os->lastpts + first_duration;
  292. if (s->streams[idx]->duration)
  293. s->streams[idx]->duration -= s->streams[idx]->start_time;
  294. s->streams[idx]->cur_dts = AV_NOPTS_VALUE;
  295. priv->final_pts = AV_NOPTS_VALUE;
  296. avpriv_vorbis_parse_reset(&priv->vp);
  297. }
  298. /* parse packet duration */
  299. if (os->psize > 0) {
  300. duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1);
  301. if (duration <= 0) {
  302. os->pflags |= AV_PKT_FLAG_CORRUPT;
  303. return 0;
  304. }
  305. os->pduration = duration;
  306. }
  307. /* final packet handling
  308. * here we save the pts of the first packet in the final page, sum up all
  309. * packet durations in the final page except for the last one, and compare
  310. * to the page granule to find the duration of the final packet */
  311. if (os->flags & OGG_FLAG_EOS) {
  312. if (os->lastpts != AV_NOPTS_VALUE) {
  313. priv->final_pts = os->lastpts;
  314. priv->final_duration = 0;
  315. }
  316. if (os->segp == os->nsegs)
  317. os->pduration = os->granule - priv->final_pts - priv->final_duration;
  318. priv->final_duration += os->pduration;
  319. }
  320. return 0;
  321. }
  322. const struct ogg_codec ff_vorbis_codec = {
  323. .magic = "\001vorbis",
  324. .magicsize = 7,
  325. .header = vorbis_header,
  326. .packet = vorbis_packet,
  327. .cleanup = vorbis_cleanup,
  328. .nb_header = 3,
  329. };