oggparsevorbis.c 11 KB

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