Statistics
| Branch: | Revision:

ffmpeg / libavformat / id3v2.c @ 787f8fad

History | View | Annotate | Download (5.71 KB)

1
/*
2
 * ID3v2 header parser
3
 * Copyright (c) 2003 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "id3v2.h"
23
#include "id3v1.h"
24
#include "libavutil/avstring.h"
25

    
26
int ff_id3v2_match(const uint8_t *buf)
27
{
28
    return  buf[0]         ==  'I' &&
29
            buf[1]         ==  'D' &&
30
            buf[2]         ==  '3' &&
31
            buf[3]         != 0xff &&
32
            buf[4]         != 0xff &&
33
           (buf[6] & 0x80) ==    0 &&
34
           (buf[7] & 0x80) ==    0 &&
35
           (buf[8] & 0x80) ==    0 &&
36
           (buf[9] & 0x80) ==    0;
37
}
38

    
39
int ff_id3v2_tag_len(const uint8_t * buf)
40
{
41
    int len = ((buf[6] & 0x7f) << 21) +
42
              ((buf[7] & 0x7f) << 14) +
43
              ((buf[8] & 0x7f) << 7) +
44
               (buf[9] & 0x7f) +
45
              ID3v2_HEADER_SIZE;
46
    if (buf[5] & 0x10)
47
        len += ID3v2_HEADER_SIZE;
48
    return len;
49
}
50

    
51
void ff_id3v2_read(AVFormatContext *s)
52
{
53
    int len, ret;
54
    uint8_t buf[ID3v2_HEADER_SIZE];
55

    
56
    ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
57
    if (ret != ID3v2_HEADER_SIZE)
58
        return;
59
    if (ff_id3v2_match(buf)) {
60
        /* parse ID3v2 header */
61
        len = ((buf[6] & 0x7f) << 21) |
62
            ((buf[7] & 0x7f) << 14) |
63
            ((buf[8] & 0x7f) << 7) |
64
            (buf[9] & 0x7f);
65
        ff_id3v2_parse(s, len, buf[3], buf[5]);
66
    } else {
67
        url_fseek(s->pb, 0, SEEK_SET);
68
    }
69
}
70

    
71
static unsigned int get_size(ByteIOContext *s, int len)
72
{
73
    int v = 0;
74
    while (len--)
75
        v = (v << 7) + (get_byte(s) & 0x7F);
76
    return v;
77
}
78

    
79
static void read_ttag(AVFormatContext *s, int taglen, const char *key)
80
{
81
    char *q, dst[512];
82
    int len, dstlen = sizeof(dst) - 1;
83
    unsigned genre;
84

    
85
    dst[0] = 0;
86
    if (taglen < 1)
87
        return;
88

    
89
    taglen--; /* account for encoding type byte */
90

    
91
    switch (get_byte(s->pb)) { /* encoding type */
92

    
93
    case 0:  /* ISO-8859-1 (0 - 255 maps directly into unicode) */
94
        q = dst;
95
        while (taglen-- && q - dst < dstlen - 7) {
96
            uint8_t tmp;
97
            PUT_UTF8(get_byte(s->pb), tmp, *q++ = tmp;)
98
        }
99
        *q = '\0';
100
        break;
101

    
102
    case 3:  /* UTF-8 */
103
        len = FFMIN(taglen, dstlen - 1);
104
        get_buffer(s->pb, dst, len);
105
        dst[len] = 0;
106
        break;
107
    }
108

    
109
    if (!strcmp(key, "genre")
110
        && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
111
        && genre <= ID3v1_GENRE_MAX)
112
        av_strlcpy(dst, ff_id3v1_genre_str[genre], sizeof(dst));
113

    
114
    if (*dst)
115
        av_metadata_set(&s->metadata, key, dst);
116
}
117

    
118
void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
119
{
120
    int isv34, tlen;
121
    uint32_t tag;
122
    int64_t next;
123
    int taghdrlen;
124
    const char *reason;
125

    
126
    switch (version) {
127
    case 2:
128
        if (flags & 0x40) {
129
            reason = "compression";
130
            goto error;
131
        }
132
        isv34 = 0;
133
        taghdrlen = 6;
134
        break;
135

    
136
    case 3:
137
    case 4:
138
        isv34 = 1;
139
        taghdrlen = 10;
140
        break;
141

    
142
    default:
143
        reason = "version";
144
        goto error;
145
    }
146

    
147
    if (flags & 0x80) {
148
        reason = "unsynchronization";
149
        goto error;
150
    }
151

    
152
    if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
153
        url_fskip(s->pb, get_size(s->pb, 4));
154

    
155
    while (len >= taghdrlen) {
156
        if (isv34) {
157
            tag  = get_be32(s->pb);
158
            if(version==3){
159
                tlen = get_be32(s->pb);
160
            }else
161
                tlen = get_size(s->pb, 4);
162
            get_be16(s->pb); /* flags */
163
        } else {
164
            tag  = get_be24(s->pb);
165
            tlen = get_be24(s->pb);
166
        }
167
        len -= taghdrlen + tlen;
168

    
169
        if (len < 0)
170
            break;
171

    
172
        next = url_ftell(s->pb) + tlen;
173

    
174
        switch (tag) {
175
        case MKBETAG('T', 'I', 'T', '2'):
176
        case MKBETAG(0,   'T', 'T', '2'):
177
            read_ttag(s, tlen, "title");
178
            break;
179
        case MKBETAG('T', 'P', 'E', '1'):
180
        case MKBETAG(0,   'T', 'P', '1'):
181
            read_ttag(s, tlen, "author");
182
            break;
183
        case MKBETAG('T', 'A', 'L', 'B'):
184
        case MKBETAG(0,   'T', 'A', 'L'):
185
            read_ttag(s, tlen, "album");
186
            break;
187
        case MKBETAG('T', 'C', 'O', 'N'):
188
        case MKBETAG(0,   'T', 'C', 'O'):
189
            read_ttag(s, tlen, "genre");
190
            break;
191
        case MKBETAG('T', 'C', 'O', 'P'):
192
        case MKBETAG(0,   'T', 'C', 'R'):
193
            read_ttag(s, tlen, "copyright");
194
            break;
195
        case MKBETAG('T', 'R', 'C', 'K'):
196
        case MKBETAG(0,   'T', 'R', 'K'):
197
            read_ttag(s, tlen, "track");
198
            break;
199
        case 0:
200
            /* padding, skip to end */
201
            url_fskip(s->pb, len);
202
            len = 0;
203
            continue;
204
        }
205
        /* Skip to end of tag */
206
        url_fseek(s->pb, next, SEEK_SET);
207
    }
208

    
209
    if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
210
        url_fskip(s->pb, 10);
211
    return;
212

    
213
  error:
214
    av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
215
    url_fskip(s->pb, len);
216
}