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