Statistics
| Branch: | Revision:

ffmpeg / libavformat / oggparsevorbis.c @ a351110e

History | View | Annotate | Download (8.21 KB)

1 9146ca37 Måns Rullgård
/**
2
      Copyright (C) 2005  Michael Ahlberg, Måns Rullgård
3

4
      Permission is hereby granted, free of charge, to any person
5
      obtaining a copy of this software and associated documentation
6
      files (the "Software"), to deal in the Software without
7
      restriction, including without limitation the rights to use, copy,
8
      modify, merge, publish, distribute, sublicense, and/or sell copies
9
      of the Software, and to permit persons to whom the Software is
10
      furnished to do so, subject to the following conditions:
11

12
      The above copyright notice and this permission notice shall be
13
      included in all copies or substantial portions of the Software.
14

15
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
      EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
      MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
      NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
      HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
      DEALINGS IN THE SOFTWARE.
23
**/
24
25
#include <stdlib.h>
26 245976da Diego Biurrun
#include "libavutil/avstring.h"
27
#include "libavutil/bswap.h"
28 9106a698 Stefano Sabatini
#include "libavcodec/get_bits.h"
29 245976da Diego Biurrun
#include "libavcodec/bytestream.h"
30 9146ca37 Måns Rullgård
#include "avformat.h"
31 a0ddef24 Diego Biurrun
#include "oggdec.h"
32 fb66c31d Martin Storsjö
#include "vorbiscomment.h"
33 9146ca37 Måns Rullgård
34 8730fad5 David Conrad
static int ogm_chapter(AVFormatContext *as, uint8_t *key, uint8_t *val)
35
{
36
    int i, cnum, h, m, s, ms, keylen = strlen(key);
37
    AVChapter *chapter = NULL;
38
39
    if (keylen < 9 || sscanf(key, "CHAPTER%02d", &cnum) != 1)
40
        return 0;
41
42
    if (keylen == 9) {
43
        if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4)
44
            return 0;
45
46
        ff_new_chapter(as, cnum, (AVRational){1,1000},
47
                       ms + 1000*(s + 60*(m + 60*h)),
48
                       AV_NOPTS_VALUE, NULL);
49
        av_free(val);
50
    } else if (!strcmp(key+9, "NAME")) {
51
        for(i = 0; i < as->nb_chapters; i++)
52
            if (as->chapters[i]->id == cnum) {
53
                chapter = as->chapters[i];
54
                break;
55
            }
56
        if (!chapter)
57
            return 0;
58
59
        av_metadata_set2(&chapter->metadata, "title", val,
60
                         AV_METADATA_DONT_STRDUP_VAL);
61
    } else
62
        return 0;
63
64
    av_free(key);
65
    return 1;
66
}
67
68 9686df2b Diego Biurrun
int
69 533c30fc David Conrad
ff_vorbis_comment(AVFormatContext * as, AVMetadata **m, const uint8_t *buf, int size)
70 9146ca37 Måns Rullgård
{
71 47a0513b Baptiste Coudurier
    const uint8_t *p = buf;
72
    const uint8_t *end = buf + size;
73 98422c44 Reimar Döffinger
    unsigned n, j;
74
    int s;
75 9146ca37 Måns Rullgård
76 f5475e1b Måns Rullgård
    if (size < 8) /* must have vendor_length and user_comment_list_length */
77 9146ca37 Måns Rullgård
        return -1;
78
79 0a770ae7 Måns Rullgård
    s = bytestream_get_le32(&p);
80 9146ca37 Måns Rullgård
81 98422c44 Reimar Döffinger
    if (end - p - 4 < s || s < 0)
82 9146ca37 Måns Rullgård
        return -1;
83
84
    p += s;
85
86 0a770ae7 Måns Rullgård
    n = bytestream_get_le32(&p);
87 9146ca37 Måns Rullgård
88 98422c44 Reimar Döffinger
    while (end - p >= 4 && n > 0) {
89 47a0513b Baptiste Coudurier
        const char *t, *v;
90 9146ca37 Måns Rullgård
        int tl, vl;
91
92 0a770ae7 Måns Rullgård
        s = bytestream_get_le32(&p);
93 9146ca37 Måns Rullgård
94 98422c44 Reimar Döffinger
        if (end - p < s || s < 0)
95 9146ca37 Måns Rullgård
            break;
96
97
        t = p;
98
        p += s;
99
        n--;
100
101 4bd684bc Måns Rullgård
        v = memchr(t, '=', s);
102 9146ca37 Måns Rullgård
        if (!v)
103
            continue;
104
105
        tl = v - t;
106
        vl = s - tl - 1;
107
        v++;
108
109 4bd684bc Måns Rullgård
        if (tl && vl) {
110 e3b44649 Justin Ruggles
            char *tt, *ct;
111
112
            tt = av_malloc(tl + 1);
113
            ct = av_malloc(vl + 1);
114
            if (!tt || !ct) {
115
                av_freep(&tt);
116
                av_freep(&ct);
117
                av_log(as, AV_LOG_WARNING, "out-of-memory error. skipping VorbisComment tag.\n");
118
                continue;
119
            }
120 9146ca37 Måns Rullgård
121
            for (j = 0; j < tl; j++)
122 4bd684bc Måns Rullgård
                tt[j] = toupper(t[j]);
123 9146ca37 Måns Rullgård
            tt[tl] = 0;
124
125 4bd684bc Måns Rullgård
            memcpy(ct, v, vl);
126 9146ca37 Måns Rullgård
            ct[vl] = 0;
127
128 8730fad5 David Conrad
            if (!ogm_chapter(as, tt, ct))
129
                av_metadata_set2(m, tt, ct,
130 12ad6671 Michael Niedermayer
                                   AV_METADATA_DONT_STRDUP_KEY |
131
                                   AV_METADATA_DONT_STRDUP_VAL);
132 9146ca37 Måns Rullgård
        }
133
    }
134
135 972c5f9e Måns Rullgård
    if (p != end)
136 7d507ceb Reimar Döffinger
        av_log(as, AV_LOG_INFO, "%ti bytes of comment header remain\n", end-p);
137 9146ca37 Måns Rullgård
    if (n > 0)
138 4bd684bc Måns Rullgård
        av_log(as, AV_LOG_INFO,
139
               "truncated comment header, %i comments not found\n", n);
140 9146ca37 Måns Rullgård
141 ad7768f4 Anton Khirnov
    ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv);
142 03700d39 Anton Khirnov
143 9146ca37 Måns Rullgård
    return 0;
144
}
145
146
147
/** Parse the vorbis header
148
 * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec
149
 * [vorbis_version] = read 32 bits as unsigned integer | Not used
150
 * [audio_channels] = read 8 bit integer as unsigned | Used
151 115329f1 Diego Biurrun
 * [audio_sample_rate] = read 32 bits as unsigned integer | Used
152 9146ca37 Måns Rullgård
 * [bitrate_maximum] = read 32 bits as signed integer | Not used yet
153
 * [bitrate_nominal] = read 32 bits as signed integer | Not used yet
154
 * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate
155
 * [blocksize_0] = read 4 bits as unsigned integer | Not Used
156
 * [blocksize_1] = read 4 bits as unsigned integer | Not Used
157
 * [framing_flag] = read one bit | Not Used
158
 *    */
159
160 77be08ee Måns Rullgård
struct oggvorbis_private {
161 ad2b531d Måns Rullgård
    unsigned int len[3];
162
    unsigned char *packet[3];
163 77be08ee Måns Rullgård
};
164 ad2b531d Måns Rullgård
165
166
static unsigned int
167 77be08ee Måns Rullgård
fixup_vorbis_headers(AVFormatContext * as, struct oggvorbis_private *priv,
168 19f4ceca Måns Rullgård
                     uint8_t **buf)
169 ad2b531d Måns Rullgård
{
170
    int i,offset, len;
171
    unsigned char *ptr;
172
173
    len = priv->len[0] + priv->len[1] + priv->len[2];
174
    ptr = *buf = av_mallocz(len + len/255 + 64);
175
176
    ptr[0] = 2;
177
    offset = 1;
178
    offset += av_xiphlacing(&ptr[offset], priv->len[0]);
179
    offset += av_xiphlacing(&ptr[offset], priv->len[1]);
180 4bd684bc Måns Rullgård
    for (i = 0; i < 3; i++) {
181 ad2b531d Måns Rullgård
        memcpy(&ptr[offset], priv->packet[i], priv->len[i]);
182
        offset += priv->len[i];
183 2ac41150 David Conrad
        av_freep(&priv->packet[i]);
184 ad2b531d Måns Rullgård
    }
185 589790c2 Siarhei Siamashka
    *buf = av_realloc(*buf, offset + FF_INPUT_BUFFER_PADDING_SIZE);
186 ad2b531d Måns Rullgård
    return offset;
187
}
188
189
190 9146ca37 Måns Rullgård
static int
191
vorbis_header (AVFormatContext * s, int idx)
192
{
193 77be08ee Måns Rullgård
    struct ogg *ogg = s->priv_data;
194
    struct ogg_stream *os = ogg->streams + idx;
195 9146ca37 Måns Rullgård
    AVStream *st = s->streams[idx];
196 77be08ee Måns Rullgård
    struct oggvorbis_private *priv;
197 8f8320d7 David Conrad
    int pkt_type = os->buf[os->pstart];
198 9146ca37 Måns Rullgård
199 8f8320d7 David Conrad
    if (!(pkt_type & 1))
200 9146ca37 Måns Rullgård
        return 0;
201
202 8f8320d7 David Conrad
    if (!os->private) {
203 77be08ee Måns Rullgård
        os->private = av_mallocz(sizeof(struct oggvorbis_private));
204 4bd684bc Måns Rullgård
        if (!os->private)
205 ad2b531d Måns Rullgård
            return 0;
206
    }
207 9146ca37 Måns Rullgård
208 8f8320d7 David Conrad
    if (os->psize < 1 || pkt_type > 5)
209 f5475e1b Måns Rullgård
        return -1;
210
211 ad2b531d Måns Rullgård
    priv = os->private;
212 73c44cb2 Alex Converse
213
    if (priv->packet[pkt_type>>1])
214
        return -1;
215
    if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1])
216
        return -1;
217
218 8f8320d7 David Conrad
    priv->len[pkt_type >> 1] = os->psize;
219
    priv->packet[pkt_type >> 1] = av_mallocz(os->psize);
220
    memcpy(priv->packet[pkt_type >> 1], os->buf + os->pstart, os->psize);
221 9146ca37 Måns Rullgård
    if (os->buf[os->pstart] == 1) {
222 47a0513b Baptiste Coudurier
        const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */
223 736e63ed Måns Rullgård
        unsigned blocksize, bs0, bs1;
224 ce20edb7 Reimar Döffinger
        int srate;
225 f5475e1b Måns Rullgård
226
        if (os->psize != 30)
227
            return -1;
228
229 736e63ed Måns Rullgård
        if (bytestream_get_le32(&p) != 0) /* vorbis_version */
230
            return -1;
231
232 739587bf Måns Rullgård
        st->codec->channels = bytestream_get_byte(&p);
233 ce20edb7 Reimar Döffinger
        srate = bytestream_get_le32(&p);
234 739587bf Måns Rullgård
        p += 4; // skip maximum bitrate
235
        st->codec->bit_rate = bytestream_get_le32(&p); // nominal bitrate
236 736e63ed Måns Rullgård
        p += 4; // skip minimum bitrate
237
238
        blocksize = bytestream_get_byte(&p);
239
        bs0 = blocksize & 15;
240
        bs1 = blocksize >> 4;
241
242
        if (bs0 > bs1)
243
            return -1;
244
        if (bs0 < 6 || bs1 > 13)
245
            return -1;
246
247
        if (bytestream_get_byte(&p) != 1) /* framing_flag */
248
            return -1;
249 9146ca37 Måns Rullgård
250 72415b2a Stefano Sabatini
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
251 01f4895c Michael Niedermayer
        st->codec->codec_id = CODEC_ID_VORBIS;
252 9146ca37 Måns Rullgård
253 ce20edb7 Reimar Döffinger
        if (srate > 0) {
254
            st->codec->sample_rate = srate;
255 a351110e Reimar Döffinger
            av_set_pts_info(st, 64, 1, srate);
256 ce20edb7 Reimar Döffinger
        }
257 9146ca37 Måns Rullgård
    } else if (os->buf[os->pstart] == 3) {
258 8cb3c557 Reimar Döffinger
        if (os->psize > 8 &&
259
            ff_vorbis_comment(s, &st->metadata, os->buf + os->pstart + 7, os->psize - 8) >= 0) {
260
            // drop all metadata we parsed and which is not required by libvorbis
261
            unsigned new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1;
262
            if (new_len >= 16 && new_len < os->psize) {
263
                AV_WL32(priv->packet[1] + new_len - 5, 0);
264
                priv->packet[1][new_len - 1] = 1;
265
                priv->len[1] = new_len;
266
            }
267
        }
268 ad2b531d Måns Rullgård
    } else {
269 01f4895c Michael Niedermayer
        st->codec->extradata_size =
270
            fixup_vorbis_headers(s, priv, &st->codec->extradata);
271 9146ca37 Måns Rullgård
    }
272
273 8f8320d7 David Conrad
    return 1;
274 9146ca37 Måns Rullgård
}
275
276 77be08ee Måns Rullgård
const struct ogg_codec ff_vorbis_codec = {
277 9146ca37 Måns Rullgård
    .magic = "\001vorbis",
278
    .magicsize = 7,
279
    .header = vorbis_header
280
};