00001
00025 #include <stdlib.h>
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/bswap.h"
00028 #include "libavcodec/get_bits.h"
00029 #include "libavcodec/bytestream.h"
00030 #include "avformat.h"
00031 #include "oggdec.h"
00032 #include "vorbiscomment.h"
00033
00034 static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
00035 {
00036 int i, cnum, h, m, s, ms, keylen = strlen(key);
00037 AVChapter *chapter = NULL;
00038
00039 if (keylen < 9 || sscanf(key, "CHAPTER%02d", &cnum) != 1)
00040 return 0;
00041
00042 if (keylen == 9) {
00043 if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
00044 return 0;
00045
00046 ff_new_chapter(as, cnum, (AVRational){1,1000},
00047 ms + 1000*(s + 60*(m + 60*h)),
00048 AV_NOPTS_VALUE, NULL);
00049 av_free(val);
00050 } else if (!strcmp(key+9, "NAME")) {
00051 for(i = 0; i < as->nb_chapters; i++)
00052 if (as->chapters[i]->id == cnum) {
00053 chapter = as->chapters[i];
00054 break;
00055 }
00056 if (!chapter)
00057 return 0;
00058
00059 av_metadata_set2(&chapter->metadata, "title", val,
00060 AV_METADATA_DONT_STRDUP_VAL);
00061 } else
00062 return 0;
00063
00064 av_free(key);
00065 return 1;
00066 }
00067
00068 int
00069 ff_vorbis_comment(AVFormatContext * as, AVMetadata **m, const uint8_t *buf, int size)
00070 {
00071 const uint8_t *p = buf;
00072 const uint8_t *end = buf + size;
00073 unsigned n, j;
00074 int s;
00075
00076 if (size < 8)
00077 return -1;
00078
00079 s = bytestream_get_le32(&p);
00080
00081 if (end - p - 4 < s || s < 0)
00082 return -1;
00083
00084 p += s;
00085
00086 n = bytestream_get_le32(&p);
00087
00088 while (end - p >= 4 && n > 0) {
00089 const char *t, *v;
00090 int tl, vl;
00091
00092 s = bytestream_get_le32(&p);
00093
00094 if (end - p < s || s < 0)
00095 break;
00096
00097 t = p;
00098 p += s;
00099 n--;
00100
00101 v = memchr(t, '=', s);
00102 if (!v)
00103 continue;
00104
00105 tl = v - t;
00106 vl = s - tl - 1;
00107 v++;
00108
00109 if (tl && vl) {
00110 char *tt, *ct;
00111
00112 tt = av_malloc(tl + 1);
00113 ct = av_malloc(vl + 1);
00114 if (!tt || !ct) {
00115 av_freep(&tt);
00116 av_freep(&ct);
00117 av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
00118 continue;
00119 }
00120
00121 for (j = 0; j < tl; j++)
00122 tt[j] = toupper(t[j]);
00123 tt[tl] = 0;
00124
00125 memcpy(ct, v, vl);
00126 ct[vl] = 0;
00127
00128 if (!ogm_chapter(as, tt, ct))
00129 av_metadata_set2(m, tt, ct,
00130 AV_METADATA_DONT_STRDUP_KEY |
00131 AV_METADATA_DONT_STRDUP_VAL);
00132 }
00133 }
00134
00135 if (p != end)
00136 av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", end-p);
00137 if (n > 0)
00138 av_log(as, AV_LOG_INFO,
00139 "truncated comment header, %i comments not found\n", n);
00140
00141 ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
00142
00143 return 0;
00144 }
00145
00146
00160 struct oggvorbis_private {
00161 unsigned int len[3];
00162 unsigned char *packet[3];
00163 };
00164
00165
00166 static unsigned int
00167 fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
00168 uint8_t **buf)
00169 {
00170 int i,offset, len;
00171 unsigned char *ptr;
00172
00173 len = priv->len[0] + priv->len[1] + priv->len[2];
00174 ptr = *buf = av_mallocz(len + len/255 + 64);
00175
00176 ptr[0] = 2;
00177 offset = 1;
00178 offset += av_xiphlacing(&ptr[offset], priv->len[0]);
00179 offset += av_xiphlacing(&ptr[offset], priv->len[1]);
00180 for (i = 0; i < 3; i++) {
00181 memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
00182 offset += priv->len[i];
00183 av_freep(&priv->packet[i]);
00184 }
00185 *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
00186 return offset;
00187 }
00188
00189
00190 static int
00191 vorbis_header (AVFormatContext * s, int idx)
00192 {
00193 struct ogg *ogg = s->priv_data;
00194 struct ogg_stream *os = ogg->streams + idx;
00195 AVStream *st = s->streams[idx];
00196 struct oggvorbis_private *priv;
00197 int pkt_type = os->buf[os->pstart];
00198
00199 if (!(pkt_type & 1))
00200 return 0;
00201
00202 if (!os->private) {
00203 os->private = av_mallocz(sizeof(struct oggvorbis_private));
00204 if (!os->private)
00205 return 0;
00206 }
00207
00208 if (os->psize < 1 || pkt_type > 5)
00209 return -1;
00210
00211 priv = os->private;
00212
00213 if (priv->packet[pkt_type>>1])
00214 return -1;
00215 if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
00216 return -1;
00217
00218 priv->len[pkt_type >> 1] = os->psize;
00219 priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
00220 memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
00221 if (os->buf[os->pstart] == 1) {
00222 const uint8_t *p = os->buf + os->pstart + 7;
00223 unsigned blocksize, bs0, bs1;
00224
00225 if (os->psize != 30)
00226 return -1;
00227
00228 if (bytestream_get_le32(&p) != 0)
00229 return -1;
00230
00231 st->codec->channels = bytestream_get_byte(&p);
00232 st->codec->sample_rate = bytestream_get_le32(&p);
00233 p += 4;
00234 st->codec->bit_rate = bytestream_get_le32(&p);
00235 p += 4;
00236
00237 blocksize = bytestream_get_byte(&p);
00238 bs0 = blocksize & 15;
00239 bs1 = blocksize >> 4;
00240
00241 if (bs0 > bs1)
00242 return -1;
00243 if (bs0 < 6 || bs1 > 13)
00244 return -1;
00245
00246 if (bytestream_get_byte(&p) != 1)
00247 return -1;
00248
00249 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00250 st->codec->codec_id = CODEC_ID_VORBIS;
00251
00252 st->time_base.num = 1;
00253 st->time_base.den = st->codec->sample_rate;
00254 } else if (os->buf[os->pstart] == 3) {
00255 if (os->psize > 8)
00256 ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 7, os->psize - 8);
00257 } else {
00258 st->codec->extradata_size =
00259 fixup_vorbis_headers(s, priv, &st->codec->extradata);
00260 }
00261
00262 return 1;
00263 }
00264
00265 const struct ogg_codec ff_vorbis_codec = {
00266 .magic = "\001vorbis",
00267 .magicsize = 7,
00268 .header = vorbis_header
00269 };