00001
00025 #include <stdlib.h>
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/bswap.h"
00028 #include "libavcodec/bitstream.h"
00029 #include "libavcodec/bytestream.h"
00030 #include "avformat.h"
00031 #include "oggdec.h"
00032
00033 int
00034 vorbis_comment(AVFormatContext * as, uint8_t *buf, int size)
00035 {
00036 const uint8_t *p = buf;
00037 const uint8_t *end = buf + size;
00038 unsigned s, n, j;
00039
00040 if (size < 8)
00041 return -1;
00042
00043 s = bytestream_get_le32(&p);
00044
00045 if (end - p < s)
00046 return -1;
00047
00048 p += s;
00049
00050 n = bytestream_get_le32(&p);
00051
00052 while (p < end && n > 0) {
00053 const char *t, *v;
00054 int tl, vl;
00055
00056 s = bytestream_get_le32(&p);
00057
00058 if (end - p < s)
00059 break;
00060
00061 t = p;
00062 p += s;
00063 n--;
00064
00065 v = memchr(t, '=', s);
00066 if (!v)
00067 continue;
00068
00069 tl = v - t;
00070 vl = s - tl - 1;
00071 v++;
00072
00073 if (tl && vl) {
00074 char *tt, *ct;
00075
00076 tt = av_malloc(tl + 1);
00077 ct = av_malloc(vl + 1);
00078 if (!tt || !ct) {
00079 av_freep(&tt);
00080 av_freep(&ct);
00081 av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
00082 continue;
00083 }
00084
00085 for (j = 0; j < tl; j++)
00086 tt[j] = toupper(t[j]);
00087 tt[tl] = 0;
00088
00089 memcpy(ct, v, vl);
00090 ct[vl] = 0;
00091
00092 av_metadata_set(&as->metadata, tt, ct);
00093
00094 av_freep(&tt);
00095 av_freep(&ct);
00096 }
00097 }
00098
00099 if (p != end)
00100 av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", p-end);
00101 if (n > 0)
00102 av_log(as, AV_LOG_INFO,
00103 "truncated comment header, %i comments not found\n", n);
00104
00105 return 0;
00106 }
00107
00108
00122 struct oggvorbis_private {
00123 unsigned int len[3];
00124 unsigned char *packet[3];
00125 };
00126
00127
00128 static unsigned int
00129 fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
00130 uint8_t **buf)
00131 {
00132 int i,offset, len;
00133 unsigned char *ptr;
00134
00135 len = priv->len[0] + priv->len[1] + priv->len[2];
00136 ptr = *buf = av_mallocz(len + len/255 + 64);
00137
00138 ptr[0] = 2;
00139 offset = 1;
00140 offset += av_xiphlacing(&ptr[offset], priv->len[0]);
00141 offset += av_xiphlacing(&ptr[offset], priv->len[1]);
00142 for (i = 0; i < 3; i++) {
00143 memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
00144 offset += priv->len[i];
00145 }
00146 *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
00147 return offset;
00148 }
00149
00150
00151 static int
00152 vorbis_header (AVFormatContext * s, int idx)
00153 {
00154 struct ogg *ogg = s->priv_data;
00155 struct ogg_stream *os = ogg->streams + idx;
00156 AVStream *st = s->streams[idx];
00157 struct oggvorbis_private *priv;
00158
00159 if (os->seq > 2)
00160 return 0;
00161
00162 if (os->seq == 0) {
00163 os->private = av_mallocz(sizeof(struct oggvorbis_private));
00164 if (!os->private)
00165 return 0;
00166 }
00167
00168 if (os->psize < 1)
00169 return -1;
00170
00171 priv = os->private;
00172 priv->len[os->seq] = os->psize;
00173 priv->packet[os->seq] = av_mallocz(os->psize);
00174 memcpy(priv->packet[os->seq], os->buf + os->pstart, os->psize);
00175 if (os->buf[os->pstart] == 1) {
00176 const uint8_t *p = os->buf + os->pstart + 7;
00177 unsigned blocksize, bs0, bs1;
00178
00179 if (os->psize != 30)
00180 return -1;
00181
00182 if (bytestream_get_le32(&p) != 0)
00183 return -1;
00184
00185 st->codec->channels = bytestream_get_byte(&p);
00186 st->codec->sample_rate = bytestream_get_le32(&p);
00187 p += 4;
00188 st->codec->bit_rate = bytestream_get_le32(&p);
00189 p += 4;
00190
00191 blocksize = bytestream_get_byte(&p);
00192 bs0 = blocksize & 15;
00193 bs1 = blocksize >> 4;
00194
00195 if (bs0 > bs1)
00196 return -1;
00197 if (bs0 < 6 || bs1 > 13)
00198 return -1;
00199
00200 if (bytestream_get_byte(&p) != 1)
00201 return -1;
00202
00203 st->codec->codec_type = CODEC_TYPE_AUDIO;
00204 st->codec->codec_id = CODEC_ID_VORBIS;
00205
00206 st->time_base.num = 1;
00207 st->time_base.den = st->codec->sample_rate;
00208 } else if (os->buf[os->pstart] == 3) {
00209 if (os->psize > 8)
00210 vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8);
00211 } else {
00212 st->codec->extradata_size =
00213 fixup_vorbis_headers(s, priv, &st->codec->extradata);
00214 }
00215
00216 return os->seq < 3;
00217 }
00218
00219 const struct ogg_codec ff_vorbis_codec = {
00220 .magic = "\001vorbis",
00221 .magicsize = 7,
00222 .header = vorbis_header
00223 };