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.

213 lines
6.0KB

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