00001
00025 #include <stdlib.h>
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/bswap.h"
00028 #include "libavutil/dict.h"
00029 #include "libavcodec/get_bits.h"
00030 #include "libavcodec/bytestream.h"
00031 #include "libavcodec/vorbis_parser.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034 #include "oggdec.h"
00035 #include "vorbiscomment.h"
00036
00037 static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
00038 {
00039 int i, cnum, h, m, s, ms, keylen = strlen(key);
00040 AVChapter *chapter = NULL;
00041
00042 if (keylen < 9 || sscanf(key, "CHAPTER%02d", &cnum) != 1)
00043 return 0;
00044
00045 if (keylen == 9) {
00046 if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
00047 return 0;
00048
00049 avpriv_new_chapter(as, cnum, (AVRational){1,1000},
00050 ms + 1000*(s + 60*(m + 60*h)),
00051 AV_NOPTS_VALUE, NULL);
00052 av_free(val);
00053 } else if (!strcmp(key+9, "NAME")) {
00054 for(i = 0; i < as->nb_chapters; i++)
00055 if (as->chapters[i]->id == cnum) {
00056 chapter = as->chapters[i];
00057 break;
00058 }
00059 if (!chapter)
00060 return 0;
00061
00062 av_dict_set(&chapter->metadata, "title", val,
00063 AV_DICT_DONT_STRDUP_VAL);
00064 } else
00065 return 0;
00066
00067 av_free(key);
00068 return 1;
00069 }
00070
00071 int
00072 ff_vorbis_comment(AVFormatContext * as, AVDictionary **m, const uint8_t *buf, int size)
00073 {
00074 const uint8_t *p = buf;
00075 const uint8_t *end = buf + size;
00076 unsigned n, j;
00077 int s;
00078
00079 if (size < 8)
00080 return -1;
00081
00082 s = bytestream_get_le32(&p);
00083
00084 if (end - p - 4 < s || s < 0)
00085 return -1;
00086
00087 p += s;
00088
00089 n = bytestream_get_le32(&p);
00090
00091 while (end - p >= 4 && n > 0) {
00092 const char *t, *v;
00093 int tl, vl;
00094
00095 s = bytestream_get_le32(&p);
00096
00097 if (end - p < s || s < 0)
00098 break;
00099
00100 t = p;
00101 p += s;
00102 n--;
00103
00104 v = memchr(t, '=', s);
00105 if (!v)
00106 continue;
00107
00108 tl = v - t;
00109 vl = s - tl - 1;
00110 v++;
00111
00112 if (tl && vl) {
00113 char *tt, *ct;
00114
00115 tt = av_malloc(tl + 1);
00116 ct = av_malloc(vl + 1);
00117 if (!tt || !ct) {
00118 av_freep(&tt);
00119 av_freep(&ct);
00120 av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
00121 continue;
00122 }
00123
00124 for (j = 0; j < tl; j++)
00125 tt[j] = toupper(t[j]);
00126 tt[tl] = 0;
00127
00128 memcpy(ct, v, vl);
00129 ct[vl] = 0;
00130
00131 if (!ogm_chapter(as, tt, ct))
00132 av_dict_set(m, tt, ct,
00133 AV_DICT_DONT_STRDUP_KEY |
00134 AV_DICT_DONT_STRDUP_VAL);
00135 }
00136 }
00137
00138 if (p != end)
00139 av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", end-p);
00140 if (n > 0)
00141 av_log(as, AV_LOG_INFO,
00142 "truncated comment header, %i comments not found\n", n);
00143
00144 ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
00145
00146 return 0;
00147 }
00148
00149
00163 struct oggvorbis_private {
00164 unsigned int len[3];
00165 unsigned char *packet[3];
00166 VorbisParseContext vp;
00167 int64_t final_pts;
00168 int final_duration;
00169 };
00170
00171
00172 static unsigned int
00173 fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
00174 uint8_t **buf)
00175 {
00176 int i,offset, len;
00177 unsigned char *ptr;
00178
00179 len = priv->len[0] + priv->len[1] + priv->len[2];
00180 ptr = *buf = av_mallocz(len + len/255 + 64);
00181
00182 ptr[0] = 2;
00183 offset = 1;
00184 offset += av_xiphlacing(&ptr[offset], priv->len[0]);
00185 offset += av_xiphlacing(&ptr[offset], priv->len[1]);
00186 for (i = 0; i < 3; i++) {
00187 memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
00188 offset += priv->len[i];
00189 av_freep(&priv->packet[i]);
00190 }
00191 *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
00192 return offset;
00193 }
00194
00195
00196 static int
00197 vorbis_header (AVFormatContext * s, int idx)
00198 {
00199 struct ogg *ogg = s->priv_data;
00200 struct ogg_stream *os = ogg->streams + idx;
00201 AVStream *st = s->streams[idx];
00202 struct oggvorbis_private *priv;
00203 int pkt_type = os->buf[os->pstart];
00204
00205 if (!(pkt_type & 1))
00206 return os->private ? 0 : -1;
00207
00208 if (!os->private) {
00209 os->private = av_mallocz(sizeof(struct oggvorbis_private));
00210 if (!os->private)
00211 return -1;
00212 }
00213
00214 if (os->psize < 1 || pkt_type > 5)
00215 return -1;
00216
00217 priv = os->private;
00218
00219 if (priv->packet[pkt_type>>1])
00220 return -1;
00221 if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
00222 return -1;
00223
00224 priv->len[pkt_type >> 1] = os->psize;
00225 priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
00226 if (!priv->packet[pkt_type >> 1])
00227 return AVERROR(ENOMEM);
00228 memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
00229 if (os->buf[os->pstart] == 1) {
00230 const uint8_t *p = os->buf + os->pstart + 7;
00231 unsigned blocksize, bs0, bs1;
00232 int srate;
00233
00234 if (os->psize != 30)
00235 return -1;
00236
00237 if (bytestream_get_le32(&p) != 0)
00238 return -1;
00239
00240 st->codec->channels = bytestream_get_byte(&p);
00241 srate = bytestream_get_le32(&p);
00242 p += 4;
00243 st->codec->bit_rate = bytestream_get_le32(&p);
00244 p += 4;
00245
00246 blocksize = bytestream_get_byte(&p);
00247 bs0 = blocksize & 15;
00248 bs1 = blocksize >> 4;
00249
00250 if (bs0 > bs1)
00251 return -1;
00252 if (bs0 < 6 || bs1 > 13)
00253 return -1;
00254
00255 if (bytestream_get_byte(&p) != 1)
00256 return -1;
00257
00258 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00259 st->codec->codec_id = AV_CODEC_ID_VORBIS;
00260
00261 if (srate > 0) {
00262 st->codec->sample_rate = srate;
00263 avpriv_set_pts_info(st, 64, 1, srate);
00264 }
00265 } else if (os->buf[os->pstart] == 3) {
00266 if (os->psize > 8 &&
00267 ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7, os->psize - 8) >= 0) {
00268
00269 unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
00270 if (new_len >= 16 && new_len < os->psize) {
00271 AV_WL32(priv->packet[1] + new_len - 5, 0);
00272 priv->packet[1][new_len - 1] = 1;
00273 priv->len[1] = new_len;
00274 }
00275 }
00276 } else {
00277 int ret;
00278 st->codec->extradata_size =
00279 fixup_vorbis_headers(s, priv, &st->codec->extradata);
00280 if ((ret = avpriv_vorbis_parse_extradata(st->codec, &priv->vp))) {
00281 av_freep(&st->codec->extradata);
00282 st->codec->extradata_size = 0;
00283 return ret;
00284 }
00285 }
00286
00287 return 1;
00288 }
00289
00290 static int vorbis_packet(AVFormatContext *s, int idx)
00291 {
00292 struct ogg *ogg = s->priv_data;
00293 struct ogg_stream *os = ogg->streams + idx;
00294 struct oggvorbis_private *priv = os->private;
00295 int duration;
00296
00297
00298
00299
00300
00301 if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
00302 int seg, d;
00303 uint8_t *last_pkt = os->buf + os->pstart;
00304 uint8_t *next_pkt = last_pkt;
00305
00306 avpriv_vorbis_parse_reset(&priv->vp);
00307 duration = 0;
00308 seg = os->segp;
00309 d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
00310 if (d < 0) {
00311 os->pflags |= AV_PKT_FLAG_CORRUPT;
00312 return 0;
00313 }
00314 duration += d;
00315 last_pkt = next_pkt = next_pkt + os->psize;
00316 for (; seg < os->nsegs; seg++) {
00317 if (os->segments[seg] < 255) {
00318 int d = avpriv_vorbis_parse_frame(&priv->vp, last_pkt, 1);
00319 if (d < 0) {
00320 duration = os->granule;
00321 break;
00322 }
00323 duration += d;
00324 last_pkt = next_pkt + os->segments[seg];
00325 }
00326 next_pkt += os->segments[seg];
00327 }
00328 os->lastpts = os->lastdts = os->granule - duration;
00329 if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
00330 s->streams[idx]->start_time = FFMAX(os->lastpts, 0);
00331 if (s->streams[idx]->duration)
00332 s->streams[idx]->duration -= s->streams[idx]->start_time;
00333 }
00334 priv->final_pts = AV_NOPTS_VALUE;
00335 avpriv_vorbis_parse_reset(&priv->vp);
00336 }
00337
00338
00339 if (os->psize > 0) {
00340 duration = avpriv_vorbis_parse_frame(&priv->vp, os->buf + os->pstart, 1);
00341 if (duration < 0) {
00342 os->pflags |= AV_PKT_FLAG_CORRUPT;
00343 return 0;
00344 }
00345 os->pduration = duration;
00346 }
00347
00348
00349
00350
00351
00352 if (os->flags & OGG_FLAG_EOS) {
00353 if (os->lastpts != AV_NOPTS_VALUE) {
00354 priv->final_pts = os->lastpts;
00355 priv->final_duration = 0;
00356 }
00357 if (os->segp == os->nsegs)
00358 os->pduration = os->granule - priv->final_pts - priv->final_duration;
00359 priv->final_duration += os->pduration;
00360 }
00361
00362 return 0;
00363 }
00364
00365 const struct ogg_codec ff_vorbis_codec = {
00366 .magic = "\001vorbis",
00367 .magicsize = 7,
00368 .header = vorbis_header,
00369 .packet = vorbis_packet,
00370 .nb_header = 3,
00371 };