oggparsevorbis.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "bswap.h"
  25. #include "ogg2.h"
  26. extern int
  27. vorbis_comment (AVFormatContext * as, uint8_t *buf, int size)
  28. {
  29. char *p = buf;
  30. int s, n, j;
  31. if (size < 4)
  32. return -1;
  33. s = le2me_32 (unaligned32 (p));
  34. p += 4;
  35. size -= 4;
  36. if (size < s + 4)
  37. return -1;
  38. p += s;
  39. size -= s;
  40. n = le2me_32 (unaligned32 (p));
  41. p += 4;
  42. size -= 4;
  43. while (size >= 4){
  44. char *t, *v;
  45. int tl, vl;
  46. s = le2me_32 (unaligned32 (p));
  47. p += 4;
  48. size -= 4;
  49. if (size < s)
  50. break;
  51. t = p;
  52. p += s;
  53. size -= s;
  54. n--;
  55. v = memchr (t, '=', s);
  56. if (!v)
  57. continue;
  58. tl = v - t;
  59. vl = s - tl - 1;
  60. v++;
  61. if (tl && vl){
  62. char tt[tl + 1];
  63. char ct[vl + 1];
  64. for (j = 0; j < tl; j++)
  65. tt[j] = toupper (t[j]);
  66. tt[tl] = 0;
  67. memcpy (ct, v, vl);
  68. ct[vl] = 0;
  69. // took from Vorbis_I_spec
  70. if (!strcmp (tt, "AUTHOR"))
  71. strncpy (as->author, ct, FFMIN(sizeof (as->author), vl));
  72. else if (!strcmp (tt, "TITLE"))
  73. strncpy (as->title, ct, FFMIN(sizeof (as->title), vl));
  74. else if (!strcmp (tt, "COPYRIGHT"))
  75. strncpy (as->copyright, ct, FFMIN(sizeof (as->copyright), vl));
  76. else if (!strcmp (tt, "DESCRIPTION"))
  77. strncpy (as->comment, ct, FFMIN(sizeof (as->comment), vl));
  78. else if (!strcmp (tt, "GENRE"))
  79. strncpy (as->genre, ct, FFMIN(sizeof (as->genre), vl));
  80. else if (!strcmp (tt, "TRACKNUMBER"))
  81. as->track = atoi (ct);
  82. //Too bored to add others for today
  83. }
  84. }
  85. if (size > 0)
  86. av_log (as, AV_LOG_INFO, "%i bytes of comment header remain\n", size);
  87. if (n > 0)
  88. av_log (as, AV_LOG_INFO,
  89. "truncated comment header, %i comments not found\n", n);
  90. return 0;
  91. }
  92. /** Parse the vorbis header
  93. * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
  94. * [vorbis_version] = read 32 bits as unsigned integer | Not used
  95. * [audio_channels] = read 8 bit integer as unsigned | Used
  96. * [audio_sample_rate] = read 32 bits as unsigned integer | Used
  97. * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
  98. * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
  99. * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
  100. * [blocksize_0] = read 4 bits as unsigned integer | Not Used
  101. * [blocksize_1] = read 4 bits as unsigned integer | Not Used
  102. * [framing_flag] = read one bit | Not Used
  103. * */
  104. typedef struct {
  105. unsigned int len[3];
  106. unsigned char *packet[3];
  107. } oggvorbis_private_t;
  108. static unsigned int
  109. fixup_vorbis_headers(AVFormatContext * as, oggvorbis_private_t *priv,
  110. void **buf)
  111. {
  112. int i,offset, len;
  113. unsigned char *ptr;
  114. len = priv->len[0] + priv->len[1] + priv->len[2];
  115. ptr = *buf = av_mallocz(len + len/255 + 64);
  116. ptr[0] = 2;
  117. offset = 1;
  118. offset += av_xiphlacing(&ptr[offset], priv->len[0]);
  119. offset += av_xiphlacing(&ptr[offset], priv->len[1]);
  120. for(i = 0; i < 3; i++) {
  121. memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
  122. offset += priv->len[i];
  123. }
  124. *buf = av_realloc(*buf, offset);
  125. return offset;
  126. }
  127. static int
  128. vorbis_header (AVFormatContext * s, int idx)
  129. {
  130. ogg_t *ogg = s->priv_data;
  131. ogg_stream_t *os = ogg->streams + idx;
  132. AVStream *st = s->streams[idx];
  133. oggvorbis_private_t *priv;
  134. if (os->seq > 2)
  135. return 0;
  136. if(os->seq == 0) {
  137. os->private = av_mallocz(sizeof(oggvorbis_private_t));
  138. if(!os->private)
  139. return 0;
  140. }
  141. priv = os->private;
  142. priv->len[os->seq] = os->psize;
  143. priv->packet[os->seq] = av_mallocz(os->psize);
  144. memcpy(priv->packet[os->seq], os->buf + os->pstart, os->psize);
  145. if (os->buf[os->pstart] == 1) {
  146. uint8_t *p = os->buf + os->pstart + 11; //skip up to the audio channels
  147. st->codec->channels = *p++;
  148. st->codec->sample_rate = le2me_32 (unaligned32 (p));
  149. p += 8; //skip maximum and and nominal bitrate
  150. st->codec->bit_rate = le2me_32 (unaligned32 (p)); //Minimum bitrate
  151. st->codec->codec_type = CODEC_TYPE_AUDIO;
  152. st->codec->codec_id = CODEC_ID_VORBIS;
  153. st->time_base.num = 1;
  154. st->time_base.den = st->codec->sample_rate;
  155. } else if (os->buf[os->pstart] == 3) {
  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. ogg_codec_t vorbis_codec = {
  164. .magic = "\001vorbis",
  165. .magicsize = 7,
  166. .header = vorbis_header
  167. };