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.

237 lines
6.7KB

  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 "libavcodec/get_bits.h"
  25. #include "libavcodec/bytestream.h"
  26. #include "avformat.h"
  27. #include "oggdec.h"
  28. /**
  29. * VorbisComment metadata conversion mapping.
  30. * from Ogg Vorbis I format specification: comment field and header specification
  31. * http://xiph.org/vorbis/doc/v-comment.html
  32. */
  33. const AVMetadataConv ff_vorbiscomment_metadata_conv[] = {
  34. { "ARTIST" , "author" },
  35. { "DATE" , "year" },
  36. { "TRACKNUMBER", "track" },
  37. { 0 }
  38. };
  39. int
  40. vorbis_comment(AVFormatContext * as, uint8_t *buf, int size)
  41. {
  42. const uint8_t *p = buf;
  43. const uint8_t *end = buf + size;
  44. unsigned n, j;
  45. int s;
  46. if (size < 8) /* must have vendor_length and user_comment_list_length */
  47. return -1;
  48. s = bytestream_get_le32(&p);
  49. if (end - p - 4 < s || s < 0)
  50. return -1;
  51. p += s;
  52. n = bytestream_get_le32(&p);
  53. while (end - p >= 4 && n > 0) {
  54. const char *t, *v;
  55. int tl, vl;
  56. s = bytestream_get_le32(&p);
  57. if (end - p < s || s < 0)
  58. break;
  59. t = p;
  60. p += s;
  61. n--;
  62. v = memchr(t, '=', s);
  63. if (!v)
  64. continue;
  65. tl = v - t;
  66. vl = s - tl - 1;
  67. v++;
  68. if (tl && vl) {
  69. char *tt, *ct;
  70. tt = av_malloc(tl + 1);
  71. ct = av_malloc(vl + 1);
  72. if (!tt || !ct) {
  73. av_freep(&tt);
  74. av_freep(&ct);
  75. av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
  76. continue;
  77. }
  78. for (j = 0; j < tl; j++)
  79. tt[j] = toupper(t[j]);
  80. tt[tl] = 0;
  81. memcpy(ct, v, vl);
  82. ct[vl] = 0;
  83. av_metadata_set(&as->metadata, tt, ct);
  84. av_freep(&tt);
  85. av_freep(&ct);
  86. }
  87. }
  88. if (p != end)
  89. av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", end-p);
  90. if (n > 0)
  91. av_log(as, AV_LOG_INFO,
  92. "truncated comment header, %i comments not found\n", n);
  93. return 0;
  94. }
  95. /** Parse the vorbis header
  96. * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
  97. * [vorbis_version] = read 32 bits as unsigned integer | Not used
  98. * [audio_channels] = read 8 bit integer as unsigned | Used
  99. * [audio_sample_rate] = read 32 bits as unsigned integer | Used
  100. * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
  101. * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
  102. * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
  103. * [blocksize_0] = read 4 bits as unsigned integer | Not Used
  104. * [blocksize_1] = read 4 bits as unsigned integer | Not Used
  105. * [framing_flag] = read one bit | Not Used
  106. * */
  107. struct oggvorbis_private {
  108. unsigned int len[3];
  109. unsigned char *packet[3];
  110. };
  111. static unsigned int
  112. fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
  113. uint8_t **buf)
  114. {
  115. int i,offset, len;
  116. unsigned char *ptr;
  117. len = priv->len[0] + priv->len[1] + priv->len[2];
  118. ptr = *buf = av_mallocz(len + len/255 + 64);
  119. ptr[0] = 2;
  120. offset = 1;
  121. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  122. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  123. for (i = 0; i < 3; i++) {
  124. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  125. offset += priv->len[i];
  126. }
  127. *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
  128. return offset;
  129. }
  130. static int
  131. vorbis_header (AVFormatContext * s, int idx)
  132. {
  133. struct ogg *ogg = s->priv_data;
  134. struct ogg_stream *os = ogg->streams + idx;
  135. AVStream *st = s->streams[idx];
  136. struct oggvorbis_private *priv;
  137. if (os->seq > 2)
  138. return 0;
  139. if (os->seq == 0) {
  140. os->private = av_mallocz(sizeof(struct oggvorbis_private));
  141. if (!os->private)
  142. return 0;
  143. }
  144. if (os->psize < 1)
  145. return -1;
  146. priv = os->private;
  147. priv->len[os->seq] = os->psize;
  148. priv->packet[os->seq] = av_mallocz(os->psize);
  149. memcpy(priv->packet[os->seq], os->buf + os->pstart, os->psize);
  150. if (os->buf[os->pstart] == 1) {
  151. const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
  152. unsigned blocksize, bs0, bs1;
  153. if (os->psize != 30)
  154. return -1;
  155. if (bytestream_get_le32(&p) != 0) /* vorbis_version */
  156. return -1;
  157. st->codec->channels = bytestream_get_byte(&p);
  158. st->codec->sample_rate = bytestream_get_le32(&p);
  159. p += 4; // skip maximum bitrate
  160. st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
  161. p += 4; // skip minimum bitrate
  162. blocksize = bytestream_get_byte(&p);
  163. bs0 = blocksize & 15;
  164. bs1 = blocksize >> 4;
  165. if (bs0 > bs1)
  166. return -1;
  167. if (bs0 < 6 || bs1 > 13)
  168. return -1;
  169. if (bytestream_get_byte(&p) != 1) /* framing_flag */
  170. return -1;
  171. st->codec->codec_type = CODEC_TYPE_AUDIO;
  172. st->codec->codec_id = CODEC_ID_VORBIS;
  173. st->time_base.num = 1;
  174. st->time_base.den = st->codec->sample_rate;
  175. } else if (os->buf[os->pstart] == 3) {
  176. if (os->psize > 8)
  177. vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8);
  178. } else {
  179. st->codec->extradata_size =
  180. fixup_vorbis_headers(s, priv, &st->codec->extradata);
  181. }
  182. return os->seq < 3;
  183. }
  184. const struct ogg_codec ff_vorbis_codec = {
  185. .magic = "\001vorbis",
  186. .magicsize = 7,
  187. .header = vorbis_header
  188. };