FFmpeg
id3v1.c
Go to the documentation of this file.
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 "libavutil/dict.h"
24 
25 /* See Genre List at http://id3.org/id3v2.3.0 */
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] = "Bebop",
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 Cappella",
151  [124] = "Euro-House",
152  [125] = "Dance Hall",
153  [126] = "Goa",
154  [127] = "Drum & Bass",
155  [128] = "Club-House",
156  [129] = "Hardcore Techno",
157  [130] = "Terror",
158  [131] = "Indie",
159  [132] = "BritPop",
160  [133] = "Negerpunk",
161  [134] = "Polsk Punk",
162  [135] = "Beat",
163  [136] = "Christian Gangsta Rap",
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  [148] = "Abstract",
176  [149] = "Art Rock",
177  [150] = "Baroque",
178  [151] = "Bhangra",
179  [152] = "Big Beat",
180  [153] = "Breakbeat",
181  [154] = "Chillout",
182  [155] = "Downtempo",
183  [156] = "Dub",
184  [157] = "EBM",
185  [158] = "Eclectic",
186  [159] = "Electro",
187  [160] = "Electroclash",
188  [161] = "Emo",
189  [162] = "Experimental",
190  [163] = "Garage",
191  [164] = "Global",
192  [165] = "IDM",
193  [166] = "Illbient",
194  [167] = "Industro-Goth",
195  [168] = "Jam Band",
196  [169] = "Krautrock",
197  [170] = "Leftfield",
198  [171] = "Lounge",
199  [172] = "Math Rock",
200  [173] = "New Romantic",
201  [174] = "Nu-Breakz",
202  [175] = "Post-Punk",
203  [176] = "Post-Rock",
204  [177] = "Psytrance",
205  [178] = "Shoegaze",
206  [179] = "Space Rock",
207  [180] = "Trop Rock",
208  [181] = "World Music",
209  [182] = "Neoclassical",
210  [183] = "Audiobook",
211  [184] = "Audio Theatre",
212  [185] = "Neue Deutsche Welle",
213  [186] = "Podcast",
214  [187] = "Indie Rock",
215  [188] = "G-Funk",
216  [189] = "Dubstep",
217  [190] = "Garage Rock",
218  [191] = "Psybient"
219 };
220 
221 static void get_string(AVFormatContext *s, const char *key,
222  const uint8_t *buf, int buf_size)
223 {
224  int i, c;
225  char *q, str[512], *first_free_space = NULL;
226 
227  q = str;
228  for(i = 0; i < buf_size; i++) {
229  c = buf[i];
230  if (c == '\0')
231  break;
232  if ((q - str) >= sizeof(str) - 1)
233  break;
234  if (c == ' ') {
235  if (!first_free_space)
236  first_free_space = q;
237  } else {
238  first_free_space = NULL;
239  }
240  *q++ = c;
241  }
242  *q = '\0';
243 
244  if (first_free_space)
245  *first_free_space = '\0';
246 
247  if (*str)
248  av_dict_set(&s->metadata, key, str, 0);
249 }
250 
251 /**
252  * Parse an ID3v1 tag
253  *
254  * @param buf ID3v1_TAG_SIZE long buffer containing the tag
255  */
256 static int parse_tag(AVFormatContext *s, const uint8_t *buf)
257 {
258  int genre;
259 
260  if (!(buf[0] == 'T' &&
261  buf[1] == 'A' &&
262  buf[2] == 'G'))
263  return -1;
264  get_string(s, "title", buf + 3, 30);
265  get_string(s, "artist", buf + 33, 30);
266  get_string(s, "album", buf + 63, 30);
267  get_string(s, "date", buf + 93, 4);
268  get_string(s, "comment", buf + 97, 30);
269  if (buf[125] == 0 && buf[126] != 0) {
270  av_dict_set_int(&s->metadata, "track", buf[126], 0);
271  }
272  genre = buf[127];
273  if (genre <= ID3v1_GENRE_MAX)
274  av_dict_set(&s->metadata, "genre", ff_id3v1_genre_str[genre], 0);
275  return 0;
276 }
277 
279 {
280  int ret;
281  uint8_t buf[ID3v1_TAG_SIZE];
282  int64_t filesize, position = avio_tell(s->pb);
283 
284  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
285  /* XXX: change that */
286  filesize = avio_size(s->pb);
287  if (filesize > 128) {
288  avio_seek(s->pb, filesize - 128, SEEK_SET);
289  ret = avio_read(s->pb, buf, ID3v1_TAG_SIZE);
290  if (ret == ID3v1_TAG_SIZE) {
291  parse_tag(s, buf);
292  }
293  avio_seek(s->pb, position, SEEK_SET);
294  }
295  }
296 }
ID3v1_TAG_SIZE
#define ID3v1_TAG_SIZE
Definition: id3v1.h:27
int64_t
long long int64_t
Definition: coverity.c:34
ID3v1_GENRE_MAX
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
ff_id3v1_read
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:278
get_string
static void get_string(AVFormatContext *s, const char *key, const uint8_t *buf, int buf_size)
Definition: id3v1.c:221
id3v1.h
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:322
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
s
#define s(width, name)
Definition: cbs_vp9.c:198
key
const char * key
Definition: hwcontext_opencl.c:189
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
filesize
static int64_t filesize(AVIOContext *pb)
Definition: ffmpeg_mux.c:51
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_id3v1_genre_str
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
dict.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:167
parse_tag
static int parse_tag(AVFormatContext *s, const uint8_t *buf)
Parse an ID3v1 tag.
Definition: id3v1.c:256
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88