3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavcodec/avcodec.h"
24 #include "libavutil/dict.h"
26 const char * const ff_id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
60 [33] = "Instrumental",
73 [46] = "Instrumental Pop",
74 [47] = "Instrumental Rock",
78 [51] = "Techno-Industrial",
83 [56] = "Southern Rock",
88 [61] = "Christian Rap",
91 [64] = "Native American",
105 [78] = "Rock & Roll",
109 [82] = "National Folk",
111 [84] = "Fast Fusion",
118 [91] = "Gothic Rock",
119 [92] = "Progressive Rock",
120 [93] = "Psychedelic Rock",
121 [94] = "Symphonic Rock",
125 [98] = "Easy Listening",
131 [104] = "Chamber Music",
134 [107] = "Booty Bass",
136 [109] = "Porn Groove",
144 [117] = "Power Ballad",
145 [118] = "Rhythmic Soul",
151 [124] = "Euro-House",
152 [125] = "Dance Hall",
154 [127] = "Drum & Bass",
155 [128] = "Club-House",
161 [134] = "Polsk Punk",
163 [136] = "Christian Gangsta",
164 [137] = "Heavy Metal",
165 [138] = "Black Metal",
167 [140] = "Contemporary Christian",
168 [141] = "Christian Rock",
171 [144] = "Thrash Metal",
177 static void get_string(AVFormatContext *s, const char *key,
178 const uint8_t *buf, int buf_size)
184 for(i = 0; i < buf_size; i++) {
188 if ((q - str) >= sizeof(str) - 1)
195 av_dict_set(&s->metadata, key, str, 0);
201 * @param buf ID3v1_TAG_SIZE long buffer containing the tag
203 static int parse_tag(AVFormatContext *s, const uint8_t *buf)
208 if (!(buf[0] == 'T' &&
212 get_string(s, "title", buf + 3, 30);
213 get_string(s, "artist", buf + 33, 30);
214 get_string(s, "album", buf + 63, 30);
215 get_string(s, "date", buf + 93, 4);
216 get_string(s, "comment", buf + 97, 30);
217 if (buf[125] == 0 && buf[126] != 0) {
218 snprintf(str, sizeof(str), "%d", buf[126]);
219 av_dict_set(&s->metadata, "track", str, 0);
222 if (genre <= ID3v1_GENRE_MAX)
223 av_dict_set(&s->metadata, "genre", ff_id3v1_genre_str[genre], 0);
227 void ff_id3v1_read(AVFormatContext *s)
230 uint8_t buf[ID3v1_TAG_SIZE];
231 int64_t filesize, position = avio_tell(s->pb);
233 if (s->pb->seekable) {
234 /* XXX: change that */
235 filesize = avio_size(s->pb);
236 if (filesize > 128) {
237 avio_seek(s->pb, filesize - 128, SEEK_SET);
238 ret = avio_read(s->pb, buf, ID3v1_TAG_SIZE);
239 if (ret == ID3v1_TAG_SIZE) {
242 avio_seek(s->pb, position, SEEK_SET);