oggparsevorbis.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "avformat.h"
  28. #include "internal.h"
  29. #include "oggdec.h"
  30. #include "vorbiscomment.h"
  31. static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
  32. {
  33. int i, cnum, h, m, s, ms, keylen = strlen(key);
  34. AVChapter *chapter = NULL;
  35. if (keylen < 9 || sscanf(key, "CHAPTER%02d", &cnum) != 1)
  36. return 0;
  37. if (keylen == 9) {
  38. if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
  39. return 0;
  40. ff_new_chapter(as, cnum, (AVRational){1,1000},
  41. ms + 1000*(s + 60*(m + 60*h)),
  42. AV_NOPTS_VALUE, NULL);
  43. av_free(val);
  44. } else if (!strcmp(key+9, "NAME")) {
  45. for(i = 0; i < as->nb_chapters; i++)
  46. if (as->chapters[i]->id == cnum) {
  47. chapter = as->chapters[i];
  48. break;
  49. }
  50. if (!chapter)
  51. return 0;
  52. av_dict_set(&chapter->metadata, "title", val,
  53. AV_DICT_DONT_STRDUP_VAL);
  54. } else
  55. return 0;
  56. av_free(key);
  57. return 1;
  58. }
  59. int
  60. ff_vorbis_comment(AVFormatContext * as, AVDictionary **m, const uint8_t *buf, int size)
  61. {
  62. const uint8_t *p = buf;
  63. const uint8_t *end = buf + size;
  64. unsigned n, j;
  65. int s;
  66. if (size < 8) /* must have vendor_length and user_comment_list_length */
  67. return -1;
  68. s = bytestream_get_le32(&p);
  69. if (end - p - 4 < s || s < 0)
  70. return -1;
  71. p += s;
  72. n = bytestream_get_le32(&p);
  73. while (end - p >= 4 && n > 0) {
  74. const char *t, *v;
  75. int tl, vl;
  76. s = bytestream_get_le32(&p);
  77. if (end - p < s || s < 0)
  78. break;
  79. t = p;
  80. p += s;
  81. n--;
  82. v = memchr(t, '=', s);
  83. if (!v)
  84. continue;
  85. tl = v - t;
  86. vl = s - tl - 1;
  87. v++;
  88. if (tl && vl) {
  89. char *tt, *ct;
  90. tt = av_malloc(tl + 1);
  91. ct = av_malloc(vl + 1);
  92. if (!tt || !ct) {
  93. av_freep(&tt);
  94. av_freep(&ct);
  95. av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
  96. continue;
  97. }
  98. for (j = 0; j < tl; j++)
  99. tt[j] = toupper(t[j]);
  100. tt[tl] = 0;
  101. memcpy(ct, v, vl);
  102. ct[vl] = 0;
  103. if (!ogm_chapter(as, tt, ct))
  104. av_dict_set(m, tt, ct,
  105. AV_DICT_DONT_STRDUP_KEY |
  106. AV_DICT_DONT_STRDUP_VAL);
  107. }
  108. }
  109. if (p != end)
  110. av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", end-p);
  111. if (n > 0)
  112. av_log(as, AV_LOG_INFO,
  113. "truncated comment header, %i comments not found\n", n);
  114. ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
  115. return 0;
  116. }
  117. /** Parse the vorbis header
  118. * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
  119. * [vorbis_version] = read 32 bits as unsigned integer | Not used
  120. * [audio_channels] = read 8 bit integer as unsigned | Used
  121. * [audio_sample_rate] = read 32 bits as unsigned integer | Used
  122. * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
  123. * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
  124. * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
  125. * [blocksize_0] = read 4 bits as unsigned integer | Not Used
  126. * [blocksize_1] = read 4 bits as unsigned integer | Not Used
  127. * [framing_flag] = read one bit | Not Used
  128. * */
  129. struct oggvorbis_private {
  130. unsigned int len[3];
  131. unsigned char *packet[3];
  132. };
  133. static unsigned int
  134. fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
  135. uint8_t **buf)
  136. {
  137. int i,offset, len;
  138. unsigned char *ptr;
  139. len = priv->len[0] + priv->len[1] + priv->len[2];
  140. ptr = *buf = av_mallocz(len + len/255 + 64);
  141. ptr[0] = 2;
  142. offset = 1;
  143. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  144. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  145. for (i = 0; i < 3; i++) {
  146. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  147. offset += priv->len[i];
  148. av_freep(&priv->packet[i]);
  149. }
  150. *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
  151. return offset;
  152. }
  153. static int
  154. vorbis_header (AVFormatContext * s, int idx)
  155. {
  156. struct ogg *ogg = s->priv_data;
  157. struct ogg_stream *os = ogg->streams + idx;
  158. AVStream *st = s->streams[idx];
  159. struct oggvorbis_private *priv;
  160. int pkt_type = os->buf[os->pstart];
  161. if (!(pkt_type & 1))
  162. return 0;
  163. if (!os->private) {
  164. os->private = av_mallocz(sizeof(struct oggvorbis_private));
  165. if (!os->private)
  166. return 0;
  167. }
  168. if (os->psize < 1 || pkt_type > 5)
  169. return -1;
  170. priv = os->private;
  171. if (priv->packet[pkt_type>>1])
  172. return -1;
  173. if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
  174. return -1;
  175. priv->len[pkt_type >> 1] = os->psize;
  176. priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
  177. memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
  178. if (os->buf[os->pstart] == 1) {
  179. const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
  180. unsigned blocksize, bs0, bs1;
  181. int srate;
  182. if (os->psize != 30)
  183. return -1;
  184. if (bytestream_get_le32(&p) != 0) /* vorbis_version */
  185. return -1;
  186. st->codec->channels = bytestream_get_byte(&p);
  187. srate = bytestream_get_le32(&p);
  188. p += 4; // skip maximum bitrate
  189. st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
  190. p += 4; // skip minimum bitrate
  191. blocksize = bytestream_get_byte(&p);
  192. bs0 = blocksize & 15;
  193. bs1 = blocksize >> 4;
  194. if (bs0 > bs1)
  195. return -1;
  196. if (bs0 < 6 || bs1 > 13)
  197. return -1;
  198. if (bytestream_get_byte(&p) != 1) /* framing_flag */
  199. return -1;
  200. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  201. st->codec->codec_id = CODEC_ID_VORBIS;
  202. if (srate > 0) {
  203. st->codec->sample_rate = srate;
  204. av_set_pts_info(st, 64, 1, srate);
  205. }
  206. } else if (os->buf[os->pstart] == 3) {
  207. if (os->psize > 8 &&
  208. ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7, os->psize - 8) >= 0) {
  209. // drop all metadata we parsed and which is not required by libvorbis
  210. unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
  211. if (new_len >= 16 && new_len < os->psize) {
  212. AV_WL32(priv->packet[1] + new_len - 5, 0);
  213. priv->packet[1][new_len - 1] = 1;
  214. priv->len[1] = new_len;
  215. }
  216. }
  217. } else {
  218. st->codec->extradata_size =
  219. fixup_vorbis_headers(s, priv, &st->codec->extradata);
  220. }
  221. return 1;
  222. }
  223. const struct ogg_codec ff_vorbis_codec = {
  224. .magic = "\001vorbis",
  225. .magicsize = 7,
  226. .header = vorbis_header
  227. };