Statistics
| Branch: | Revision:

ffmpeg / libavformat / id3v1.c @ e8a9d018

History | View | Annotate | Download (6.09 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
#include "libavutil/avstring.h"
25

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

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

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

    
194
    if (*str)
195
        av_metadata_set(&s->metadata, key, str);
196
}
197

    
198
/**
199
 * Parse an ID3v1 tag
200
 *
201
 * @param buf ID3v1_TAG_SIZE long buffer containing the tag
202
 */
203
static int parse_tag(AVFormatContext *s, const uint8_t *buf)
204
{
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, "year",    buf + 93,  4);
215
    get_string(s, "comment", buf + 97, 30);
216
    if (buf[125] == 0 && buf[126] != 0)
217
        av_metadata_set2(&s->metadata, "track", av_d2str(buf[126]), AV_METADATA_DONT_STRDUP_VAL);
218
    genre = buf[127];
219
    if (genre <= ID3v1_GENRE_MAX)
220
        av_metadata_set(&s->metadata, "genre", ff_id3v1_genre_str[genre]);
221
    return 0;
222
}
223

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

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