Statistics
| Branch: | Revision:

ffmpeg / libavformat / id3v1.c @ f59d8ff8

History | View | Annotate | Download (6.1 KB)

1
/*
2
 * ID3v1 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 "id3v1.h"
23
#include "libavcodec/avcodec.h"
24

    
25
const char * const ff_id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
26
      [0] = "Blues",
27
      [1] = "Classic Rock",
28
      [2] = "Country",
29
      [3] = "Dance",
30
      [4] = "Disco",
31
      [5] = "Funk",
32
      [6] = "Grunge",
33
      [7] = "Hip-Hop",
34
      [8] = "Jazz",
35
      [9] = "Metal",
36
     [10] = "New Age",
37
     [11] = "Oldies",
38
     [12] = "Other",
39
     [13] = "Pop",
40
     [14] = "R&B",
41
     [15] = "Rap",
42
     [16] = "Reggae",
43
     [17] = "Rock",
44
     [18] = "Techno",
45
     [19] = "Industrial",
46
     [20] = "Alternative",
47
     [21] = "Ska",
48
     [22] = "Death Metal",
49
     [23] = "Pranks",
50
     [24] = "Soundtrack",
51
     [25] = "Euro-Techno",
52
     [26] = "Ambient",
53
     [27] = "Trip-Hop",
54
     [28] = "Vocal",
55
     [29] = "Jazz+Funk",
56
     [30] = "Fusion",
57
     [31] = "Trance",
58
     [32] = "Classical",
59
     [33] = "Instrumental",
60
     [34] = "Acid",
61
     [35] = "House",
62
     [36] = "Game",
63
     [37] = "Sound Clip",
64
     [38] = "Gospel",
65
     [39] = "Noise",
66
     [40] = "AlternRock",
67
     [41] = "Bass",
68
     [42] = "Soul",
69
     [43] = "Punk",
70
     [44] = "Space",
71
     [45] = "Meditative",
72
     [46] = "Instrumental Pop",
73
     [47] = "Instrumental Rock",
74
     [48] = "Ethnic",
75
     [49] = "Gothic",
76
     [50] = "Darkwave",
77
     [51] = "Techno-Industrial",
78
     [52] = "Electronic",
79
     [53] = "Pop-Folk",
80
     [54] = "Eurodance",
81
     [55] = "Dream",
82
     [56] = "Southern Rock",
83
     [57] = "Comedy",
84
     [58] = "Cult",
85
     [59] = "Gangsta",
86
     [60] = "Top 40",
87
     [61] = "Christian Rap",
88
     [62] = "Pop/Funk",
89
     [63] = "Jungle",
90
     [64] = "Native American",
91
     [65] = "Cabaret",
92
     [66] = "New Wave",
93
     [67] = "Psychadelic",
94
     [68] = "Rave",
95
     [69] = "Showtunes",
96
     [70] = "Trailer",
97
     [71] = "Lo-Fi",
98
     [72] = "Tribal",
99
     [73] = "Acid Punk",
100
     [74] = "Acid Jazz",
101
     [75] = "Polka",
102
     [76] = "Retro",
103
     [77] = "Musical",
104
     [78] = "Rock & Roll",
105
     [79] = "Hard Rock",
106
     [80] = "Folk",
107
     [81] = "Folk-Rock",
108
     [82] = "National Folk",
109
     [83] = "Swing",
110
     [84] = "Fast Fusion",
111
     [85] = "Bebob",
112
     [86] = "Latin",
113
     [87] = "Revival",
114
     [88] = "Celtic",
115
     [89] = "Bluegrass",
116
     [90] = "Avantgarde",
117
     [91] = "Gothic Rock",
118
     [92] = "Progressive Rock",
119
     [93] = "Psychedelic Rock",
120
     [94] = "Symphonic Rock",
121
     [95] = "Slow Rock",
122
     [96] = "Big Band",
123
     [97] = "Chorus",
124
     [98] = "Easy Listening",
125
     [99] = "Acoustic",
126
    [100] = "Humour",
127
    [101] = "Speech",
128
    [102] = "Chanson",
129
    [103] = "Opera",
130
    [104] = "Chamber Music",
131
    [105] = "Sonata",
132
    [106] = "Symphony",
133
    [107] = "Booty Bass",
134
    [108] = "Primus",
135
    [109] = "Porn Groove",
136
    [110] = "Satire",
137
    [111] = "Slow Jam",
138
    [112] = "Club",
139
    [113] = "Tango",
140
    [114] = "Samba",
141
    [115] = "Folklore",
142
    [116] = "Ballad",
143
    [117] = "Power Ballad",
144
    [118] = "Rhythmic Soul",
145
    [119] = "Freestyle",
146
    [120] = "Duet",
147
    [121] = "Punk Rock",
148
    [122] = "Drum Solo",
149
    [123] = "A capella",
150
    [124] = "Euro-House",
151
    [125] = "Dance Hall",
152
    [126] = "Goa",
153
    [127] = "Drum & Bass",
154
    [128] = "Club-House",
155
    [129] = "Hardcore",
156
    [130] = "Terror",
157
    [131] = "Indie",
158
    [132] = "BritPop",
159
    [133] = "Negerpunk",
160
    [134] = "Polsk Punk",
161
    [135] = "Beat",
162
    [136] = "Christian Gangsta",
163
    [137] = "Heavy Metal",
164
    [138] = "Black Metal",
165
    [139] = "Crossover",
166
    [140] = "Contemporary Christian",
167
    [141] = "Christian Rock",
168
    [142] = "Merengue",
169
    [143] = "Salsa",
170
    [144] = "Thrash Metal",
171
    [145] = "Anime",
172
    [146] = "JPop",
173
    [147] = "SynthPop",
174
};
175

    
176
static void get_string(AVFormatContext *s, const char *key,
177
                       const uint8_t *buf, int buf_size)
178
{
179
    int i, c;
180
    char *q, str[512];
181

    
182
    q = str;
183
    for(i = 0; i < buf_size; i++) {
184
        c = buf[i];
185
        if (c == '\0')
186
            break;
187
        if ((q - str) >= sizeof(str) - 1)
188
            break;
189
        *q++ = c;
190
    }
191
    *q = '\0';
192

    
193
    if (*str)
194
        av_metadata_set2(&s->metadata, key, str, 0);
195
}
196

    
197
/**
198
 * Parse an ID3v1 tag
199
 *
200
 * @param buf ID3v1_TAG_SIZE long buffer containing the tag
201
 */
202
static int parse_tag(AVFormatContext *s, const uint8_t *buf)
203
{
204
    char str[5];
205
    int genre;
206

    
207
    if (!(buf[0] == 'T' &&
208
          buf[1] == 'A' &&
209
          buf[2] == 'G'))
210
        return -1;
211
    get_string(s, "title",   buf +  3, 30);
212
    get_string(s, "artist",  buf + 33, 30);
213
    get_string(s, "album",   buf + 63, 30);
214
    get_string(s, "date",    buf + 93,  4);
215
    get_string(s, "comment", buf + 97, 30);
216
    if (buf[125] == 0 && buf[126] != 0) {
217
        snprintf(str, sizeof(str), "%d", buf[126]);
218
        av_metadata_set2(&s->metadata, "track", str, 0);
219
    }
220
    genre = buf[127];
221
    if (genre <= ID3v1_GENRE_MAX)
222
        av_metadata_set2(&s->metadata, "genre", ff_id3v1_genre_str[genre], 0);
223
    return 0;
224
}
225

    
226
void ff_id3v1_read(AVFormatContext *s)
227
{
228
    int ret, filesize;
229
    uint8_t buf[ID3v1_TAG_SIZE];
230

    
231
    if (!url_is_streamed(s->pb)) {
232
        /* XXX: change that */
233
        filesize = url_fsize(s->pb);
234
        if (filesize > 128) {
235
            avio_seek(s->pb, filesize - 128, SEEK_SET);
236
            ret = avio_read(s->pb, buf, ID3v1_TAG_SIZE);
237
            if (ret == ID3v1_TAG_SIZE) {
238
                parse_tag(s, buf);
239
            }
240
            avio_seek(s->pb, 0, SEEK_SET);
241
        }
242
    }
243
}