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.

234 lines
6.6KB

  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 "avformat.h"
  23. #include "bitstream.h"
  24. #include "bytestream.h"
  25. #include "bswap.h"
  26. #include "ogg2.h"
  27. #include "avstring.h"
  28. extern int
  29. vorbis_comment(AVFormatContext * as, uint8_t *buf, int size)
  30. {
  31. uint8_t *p = buf;
  32. unsigned s, n, j;
  33. if (size < 8) /* must have vendor_length and user_comment_list_length */
  34. return -1;
  35. s = AV_RL32(p);
  36. p += 4;
  37. size -= 4;
  38. if (size - 4 < s)
  39. return -1;
  40. p += s;
  41. size -= s;
  42. n = AV_RL32(p);
  43. p += 4;
  44. size -= 4;
  45. while (size >= 4) {
  46. char *t, *v;
  47. int tl, vl;
  48. s = AV_RL32(p);
  49. p += 4;
  50. size -= 4;
  51. if (size < s)
  52. break;
  53. t = p;
  54. p += s;
  55. size -= s;
  56. n--;
  57. v = memchr(t, '=', s);
  58. if (!v)
  59. continue;
  60. tl = v - t;
  61. vl = s - tl - 1;
  62. v++;
  63. if (tl && vl) {
  64. char tt[tl + 1];
  65. char ct[vl + 1];
  66. for (j = 0; j < tl; j++)
  67. tt[j] = toupper(t[j]);
  68. tt[tl] = 0;
  69. memcpy(ct, v, vl);
  70. ct[vl] = 0;
  71. // took from Vorbis_I_spec
  72. if (!strcmp(tt, "AUTHOR") || !strcmp(tt, "ARTIST"))
  73. av_strlcpy(as->author, ct, sizeof(as->author));
  74. else if (!strcmp(tt, "TITLE"))
  75. av_strlcpy(as->title, ct, sizeof(as->title));
  76. else if (!strcmp(tt, "COPYRIGHT"))
  77. av_strlcpy(as->copyright, ct, sizeof(as->copyright));
  78. else if (!strcmp(tt, "DESCRIPTION"))
  79. av_strlcpy(as->comment, ct, sizeof(as->comment));
  80. else if (!strcmp(tt, "GENRE"))
  81. av_strlcpy(as->genre, ct, sizeof(as->genre));
  82. else if (!strcmp(tt, "TRACKNUMBER"))
  83. as->track = atoi(ct);
  84. else if (!strcmp(tt, "ALBUM"))
  85. av_strlcpy(as->album, ct, sizeof(as->album));
  86. }
  87. }
  88. if (size > 0)
  89. av_log(as, AV_LOG_INFO, "%i bytes of comment header remain\n", size);
  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. typedef struct {
  108. unsigned int len[3];
  109. unsigned char *packet[3];
  110. } oggvorbis_private_t;
  111. static unsigned int
  112. fixup_vorbis_headers(AVFormatContext * as, oggvorbis_private_t *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);
  128. return offset;
  129. }
  130. static int
  131. vorbis_header (AVFormatContext * s, int idx)
  132. {
  133. ogg_t *ogg = s->priv_data;
  134. ogg_stream_t *os = ogg->streams + idx;
  135. AVStream *st = s->streams[idx];
  136. oggvorbis_private_t *priv;
  137. if (os->seq > 2)
  138. return 0;
  139. if (os->seq == 0) {
  140. os->private = av_mallocz(sizeof(oggvorbis_private_t));
  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. 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. ogg_codec_t vorbis_codec = {
  185. .magic = "\001vorbis",
  186. .magicsize = 7,
  187. .header = vorbis_header
  188. };