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.

383 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%02d", &cnum) != 1)
  40. return 0;
  41. if (keylen == 9) {
  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 + 9, "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 unsigned 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. ptr[0] = 2;
  153. offset = 1;
  154. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  155. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  156. for (i = 0; i < 3; i++) {
  157. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  158. offset += priv->len[i];
  159. av_freep(&priv->packet[i]);
  160. }
  161. if ((err = av_reallocp(buf, offset + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
  162. return err;
  163. return offset;
  164. }
  165. static void vorbis_cleanup(AVFormatContext *s, int idx)
  166. {
  167. struct ogg *ogg = s->priv_data;
  168. struct ogg_stream *os = ogg->streams + idx;
  169. struct oggvorbis_private *priv = os->private;
  170. int i;
  171. if (os->private)
  172. for (i = 0; i < 3; i++)
  173. av_freep(&priv->packet[i]);
  174. }
  175. static int vorbis_header(AVFormatContext *s, int idx)
  176. {
  177. struct ogg *ogg = s->priv_data;
  178. AVStream *st = s->streams[idx];
  179. struct ogg_stream *os = ogg->streams + idx;
  180. struct oggvorbis_private *priv;
  181. int pkt_type = os->buf[os->pstart];
  182. if (!os->private) {
  183. os->private = av_mallocz(sizeof(struct oggvorbis_private));
  184. if (!os->private)
  185. return AVERROR(ENOMEM);
  186. }
  187. if (!(pkt_type & 1))
  188. return 0;
  189. if (os->psize < 1 || pkt_type > 5)
  190. return AVERROR_INVALIDDATA;
  191. priv = os->private;
  192. if (priv->packet[pkt_type >> 1])
  193. return AVERROR_INVALIDDATA;
  194. if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
  195. return AVERROR_INVALIDDATA;
  196. priv->len[pkt_type >> 1] = os->psize;
  197. priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
  198. memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
  199. if (os->buf[os->pstart] == 1) {
  200. const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
  201. unsigned blocksize, bs0, bs1;
  202. int srate;
  203. if (os->psize != 30)
  204. return AVERROR_INVALIDDATA;
  205. if (bytestream_get_le32(&p) != 0) /* vorbis_version */
  206. return AVERROR_INVALIDDATA;
  207. st->codec->channels = bytestream_get_byte(&p);
  208. srate = bytestream_get_le32(&p);
  209. p += 4; // skip maximum bitrate
  210. st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
  211. p += 4; // skip minimum bitrate
  212. blocksize = bytestream_get_byte(&p);
  213. bs0 = blocksize & 15;
  214. bs1 = blocksize >> 4;
  215. if (bs0 > bs1)
  216. return AVERROR_INVALIDDATA;
  217. if (bs0 < 6 || bs1 > 13)
  218. return AVERROR_INVALIDDATA;
  219. if (bytestream_get_byte(&p) != 1) /* framing_flag */
  220. return AVERROR_INVALIDDATA;
  221. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  222. st->codec->codec_id = AV_CODEC_ID_VORBIS;
  223. if (srate > 0) {
  224. st->codec->sample_rate = srate;
  225. avpriv_set_pts_info(st, 64, 1, srate);
  226. }
  227. } else if (os->buf[os->pstart] == 3) {
  228. if (os->psize > 8 &&
  229. ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7,
  230. os->psize - 8) >= 0) {
  231. // drop all metadata we parsed and which is not required by libvorbis
  232. unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
  233. if (new_len >= 16 && new_len < os->psize) {
  234. AV_WL32(priv->packet[1] + new_len - 5, 0);
  235. priv->packet[1][new_len - 1] = 1;
  236. priv->len[1] = new_len;
  237. }
  238. }
  239. } else {
  240. int ret = fixup_vorbis_headers(s, priv, &st->codec->extradata);
  241. if (ret < 0) {
  242. st->codec->extradata_size = 0;
  243. return ret;
  244. }
  245. st->codec->extradata_size = ret;
  246. if ((ret = avpriv_vorbis_parse_extradata(st->codec, &priv->vp))) {
  247. av_freep(&st->codec->extradata);
  248. st->codec->extradata_size = 0;
  249. return ret;
  250. }
  251. }
  252. return 1;
  253. }
  254. static int vorbis_packet(AVFormatContext *s, int idx)
  255. {
  256. struct ogg *ogg = s->priv_data;
  257. struct ogg_stream *os = ogg->streams + idx;
  258. struct oggvorbis_private *priv = os->private;
  259. int duration;
  260. /* first packet handling
  261. * here we parse the duration of each packet in the first page and compare
  262. * the total duration to the page granule to find the encoder delay and
  263. * set the first timestamp */
  264. if (!os->lastpts) {
  265. int seg;
  266. uint8_t *last_pkt = os->buf + os->pstart;
  267. uint8_t *next_pkt = last_pkt;
  268. int first_duration = 0;
  269. avpriv_vorbis_parse_reset(&priv->vp);
  270. duration = 0;
  271. for (seg = 0; seg < os->nsegs; seg++) {
  272. if (os->segments[seg] < 255) {
  273. int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
  274. if (d < 0) {
  275. duration = os->granule;
  276. break;
  277. }
  278. if (!duration)
  279. first_duration = d;
  280. duration += d;
  281. last_pkt = next_pkt + os->segments[seg];
  282. }
  283. next_pkt += os->segments[seg];
  284. }
  285. os->lastpts =
  286. os->lastdts = os->granule - duration;
  287. s->streams[idx]->start_time = os->lastpts + first_duration;
  288. if (s->streams[idx]->duration)
  289. s->streams[idx]->duration -= s->streams[idx]->start_time;
  290. s->streams[idx]->cur_dts = AV_NOPTS_VALUE;
  291. priv->final_pts = AV_NOPTS_VALUE;
  292. avpriv_vorbis_parse_reset(&priv->vp);
  293. }
  294. /* parse packet duration */
  295. if (os->psize > 0) {
  296. duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1);
  297. if (duration <= 0) {
  298. os->pflags |= AV_PKT_FLAG_CORRUPT;
  299. return 0;
  300. }
  301. os->pduration = duration;
  302. }
  303. /* final packet handling
  304. * here we save the pts of the first packet in the final page, sum up all
  305. * packet durations in the final page except for the last one, and compare
  306. * to the page granule to find the duration of the final packet */
  307. if (os->flags & OGG_FLAG_EOS) {
  308. if (os->lastpts != AV_NOPTS_VALUE) {
  309. priv->final_pts = os->lastpts;
  310. priv->final_duration = 0;
  311. }
  312. if (os->segp == os->nsegs)
  313. os->pduration = os->granule - priv->final_pts - priv->final_duration;
  314. priv->final_duration += os->pduration;
  315. }
  316. return 0;
  317. }
  318. const struct ogg_codec ff_vorbis_codec = {
  319. .magic = "\001vorbis",
  320. .magicsize = 7,
  321. .header = vorbis_header,
  322. .packet = vorbis_packet,
  323. .cleanup = vorbis_cleanup,
  324. .nb_header = 3,
  325. };