2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gstid3tag.c: plugin for reading / modifying id3 tags
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: tag mappings and support functions for plugins
25 * dealing with ID3v1 and ID3v2 tags
26 * @see_also: #GstTagList
30 * Contains various utility functions for plugins to parse or create
31 * ID3 tags and map ID3v2 identifiers to and from GStreamer identifiers.
40 #include "gsttageditingprivate.h"
44 static const gchar genres[] =
45 "Blues\000Classic Rock\000Country\000Dance\000Disco\000Funk\000Grunge\000"
46 "Hip-Hop\000Jazz\000Metal\000New Age\000Oldies\000Other\000Pop\000R&B\000"
47 "Rap\000Reggae\000Rock\000Techno\000Industrial\000Alternative\000Ska\000"
48 "Death Metal\000Pranks\000Soundtrack\000Euro-Techno\000Ambient\000Trip-Hop"
49 "\000Vocal\000Jazz+Funk\000Fusion\000Trance\000Classical\000Instrumental\000"
50 "Acid\000House\000Game\000Sound Clip\000Gospel\000Noise\000Alternative Rock"
51 "\000Bass\000Soul\000Punk\000Space\000Meditative\000Instrumental Pop\000"
52 "Instrumental Rock\000Ethnic\000Gothic\000Darkwave\000Techno-Industrial\000"
53 "Electronic\000Pop-Folk\000Eurodance\000Dream\000Southern Rock\000Comedy"
54 "\000Cult\000Gangsta\000Top 40\000Christian Rap\000Pop/Funk\000Jungle\000"
55 "Native American\000Cabaret\000New Wave\000Psychedelic\000Rave\000Showtunes"
56 "\000Trailer\000Lo-Fi\000Tribal\000Acid Punk\000Acid Jazz\000Polka\000"
57 "Retro\000Musical\000Rock & Roll\000Hard Rock\000Folk\000Folk/Rock\000"
58 "National Folk\000Swing\000Bebob\000Latin\000Revival\000Celtic\000Bluegrass"
59 "\000Avantgarde\000Gothic Rock\000Progressive Rock\000Psychedelic Rock\000"
60 "Symphonic Rock\000Slow Rock\000Big Band\000Chorus\000Easy Listening\000"
61 "Acoustic\000Humour\000Speech\000Chanson\000Opera\000Chamber Music\000"
62 "Sonata\000Symphony\000Booty Bass\000Primus\000Porn Groove\000Satire\000"
63 "Slow Jam\000Club\000Tango\000Samba\000Folklore\000Ballad\000Power Ballad\000"
64 "Rhythmic Soul\000Freestyle\000Duet\000Punk Rock\000Drum Solo\000A Capella"
65 "\000Euro-House\000Dance Hall\000Goa\000Drum & Bass\000Club-House\000"
66 "Hardcore\000Terror\000Indie\000BritPop\000Negerpunk\000Polsk Punk\000"
67 "Beat\000Christian Gangsta Rap\000Heavy Metal\000Black Metal\000"
68 "Crossover\000Contemporary Christian\000Christian Rock\000Merengue\000"
69 "Salsa\000Thrash Metal\000Anime\000Jpop\000Synthpop";
71 static const guint16 genres_idx[] = {
72 0, 6, 19, 27, 33, 39, 44, 51, 59, 64, 70, 78, 85, 91, 95, 99, 103, 110, 115,
73 122, 133, 145, 149, 161, 168, 179, 191, 199, 208, 214, 224, 231, 238, 248,
74 261, 266, 272, 277, 288, 295, 301, 318, 323, 328, 333, 339, 350, 367, 385,
75 392, 399, 408, 426, 437, 446, 456, 462, 476, 483, 488, 496, 503, 517, 526,
76 533, 549, 557, 566, 578, 583, 593, 601, 607, 614, 624, 634, 640, 646, 654,
77 666, 676, 681, 691, 705, 224, 711, 717, 723, 731, 738, 748, 759, 771, 788,
78 805, 820, 830, 839, 846, 861, 870, 877, 884, 892, 898, 912, 919, 928, 939,
79 946, 958, 965, 974, 979, 985, 991, 1000, 1007, 1020, 1034, 1044, 1049,
80 1059, 1069, 1079, 1090, 1101, 1105, 1117, 1128, 1137, 1144, 1150, 1158,
81 1168, 1179, 1184, 1206, 1218, 1230, 1240, 1263, 1278, 1287, 1293, 1306,
85 static const GstTagEntryMatch tag_matches[] = {
86 {GST_TAG_TITLE, "TIT2"},
87 {GST_TAG_ALBUM, "TALB"},
88 {GST_TAG_TRACK_NUMBER, "TRCK"},
89 {GST_TAG_ARTIST, "TPE1"},
90 {GST_TAG_ALBUM_ARTIST, "TPE2"},
91 {GST_TAG_COMPOSER, "TCOM"},
92 {GST_TAG_COPYRIGHT, "TCOP"},
93 {GST_TAG_COPYRIGHT_URI, "WCOP"},
94 {GST_TAG_ENCODED_BY, "TENC"},
95 {GST_TAG_GENRE, "TCON"},
96 {GST_TAG_DATE, "TDRC"},
97 {GST_TAG_COMMENT, "COMM"},
98 {GST_TAG_ALBUM_VOLUME_NUMBER, "TPOS"},
99 {GST_TAG_DURATION, "TLEN"},
100 {GST_TAG_ISRC, "TSRC"},
101 {GST_TAG_IMAGE, "APIC"},
102 {GST_TAG_ENCODER, "TSSE"},
103 {GST_TAG_BEATS_PER_MINUTE, "TBPM"},
104 {GST_TAG_ARTIST_SORTNAME, "TSOP"},
105 {GST_TAG_ALBUM_SORTNAME, "TSOA"},
106 {GST_TAG_TITLE_SORTNAME, "TSOT"},
111 * gst_tag_from_id3_tag:
112 * @id3_tag: ID3v2 tag to convert to GStreamer tag
114 * Looks up the GStreamer tag for a ID3v2 tag.
116 * Returns: The corresponding GStreamer tag or NULL if none exists.
119 gst_tag_from_id3_tag (const gchar * id3_tag)
123 g_return_val_if_fail (id3_tag != NULL, NULL);
125 while (tag_matches[i].gstreamer_tag != NULL) {
126 if (strncmp (id3_tag, tag_matches[i].original_tag, 5) == 0) {
127 return tag_matches[i].gstreamer_tag;
132 GST_INFO ("Cannot map ID3v2 tag '%c%c%c%c' to GStreamer tag",
133 id3_tag[0], id3_tag[1], id3_tag[2], id3_tag[3]);
138 static const GstTagEntryMatch user_tag_matches[] = {
139 /* musicbrainz identifiers being used in the real world (foobar2000) */
140 {GST_TAG_MUSICBRAINZ_ARTISTID, "TXXX|musicbrainz_artistid"},
141 {GST_TAG_MUSICBRAINZ_ALBUMID, "TXXX|musicbrainz_albumid"},
142 {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "TXXX|musicbrainz_albumartistid"},
143 {GST_TAG_MUSICBRAINZ_TRMID, "TXXX|musicbrainz_trmid"},
144 {GST_TAG_CDDA_MUSICBRAINZ_DISCID, "TXXX|musicbrainz_discid"},
145 /* musicbrainz identifiers according to spec no one pays
146 * attention to (http://musicbrainz.org/docs/specs/metadata_tags.html) */
147 {GST_TAG_MUSICBRAINZ_ARTISTID, "TXXX|MusicBrainz Artist Id"},
148 {GST_TAG_MUSICBRAINZ_ALBUMID, "TXXX|MusicBrainz Album Id"},
149 {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "TXXX|MusicBrainz Album Artist Id"},
150 {GST_TAG_MUSICBRAINZ_TRMID, "TXXX|MusicBrainz TRM Id"},
151 /* according to: http://wiki.musicbrainz.org/MusicBrainzTag (yes, no space
152 * before 'ID' and not 'Id' either this time, yay for consistency) */
153 {GST_TAG_CDDA_MUSICBRAINZ_DISCID, "TXXX|MusicBrainz DiscID"},
154 /* foobar2000 uses these identifiers to store gain/peak information in
155 * ID3v2 tags <= v2.3.0. In v2.4.0 there's the RVA2 frame for that */
156 {GST_TAG_TRACK_GAIN, "TXXX|replaygain_track_gain"},
157 {GST_TAG_TRACK_PEAK, "TXXX|replaygain_track_peak"},
158 {GST_TAG_ALBUM_GAIN, "TXXX|replaygain_album_gain"},
159 {GST_TAG_ALBUM_PEAK, "TXXX|replaygain_album_peak"},
160 /* the following two are more or less made up, there seems to be little
161 * evidence that any popular application is actually putting this info
162 * into TXXX frames; the first one comes from a musicbrainz wiki 'proposed
163 * tags' page, the second one is analogue to the vorbis/ape/flac tag. */
164 {GST_TAG_CDDA_CDDB_DISCID, "TXXX|discid"},
165 {GST_TAG_CDDA_CDDB_DISCID, "TXXX|CDDB DiscID"}
169 * gst_tag_from_id3_user_tag:
170 * @type: the type of ID3v2 user tag (e.g. "TXXX" or "UDIF")
171 * @id3_user_tag: ID3v2 user tag to convert to GStreamer tag
173 * Looks up the GStreamer tag for an ID3v2 user tag (e.g. description in
174 * TXXX frame or owner in UFID frame).
176 * Returns: The corresponding GStreamer tag or NULL if none exists.
179 gst_tag_from_id3_user_tag (const gchar * type, const gchar * id3_user_tag)
183 g_return_val_if_fail (type != NULL && strlen (type) == 4, NULL);
184 g_return_val_if_fail (id3_user_tag != NULL, NULL);
186 for (i = 0; i < G_N_ELEMENTS (user_tag_matches); ++i) {
187 if (strncmp (type, user_tag_matches[i].original_tag, 4) == 0 &&
188 g_ascii_strcasecmp (id3_user_tag,
189 user_tag_matches[i].original_tag + 5) == 0) {
190 GST_LOG ("Mapped ID3v2 user tag '%s' to GStreamer tag '%s'",
191 user_tag_matches[i].original_tag, user_tag_matches[i].gstreamer_tag);
192 return user_tag_matches[i].gstreamer_tag;
196 GST_INFO ("Cannot map ID3v2 user tag '%s' of type '%s' to GStreamer tag",
203 * gst_tag_to_id3_tag:
204 * @gst_tag: GStreamer tag to convert to vorbiscomment tag
206 * Looks up the ID3v2 tag for a GStreamer tag.
208 * Returns: The corresponding ID3v2 tag or NULL if none exists.
211 gst_tag_to_id3_tag (const gchar * gst_tag)
215 g_return_val_if_fail (gst_tag != NULL, NULL);
217 while (tag_matches[i].gstreamer_tag != NULL) {
218 if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
219 return tag_matches[i].original_tag;
227 gst_tag_extract_id3v1_string (GstTagList * list, const gchar * tag,
228 const gchar * start, const guint size)
230 const gchar *env_vars[] = { "GST_ID3V1_TAG_ENCODING",
231 "GST_ID3_TAG_ENCODING", "GST_TAG_ENCODING", NULL
235 utf8 = gst_tag_freeform_string_to_utf8 (start, size, env_vars);
237 if (utf8 && *utf8 != '\0') {
238 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, tag, utf8, NULL);
245 * gst_tag_list_new_from_id3v1:
246 * @data: 128 bytes of data containing the ID3v1 tag
248 * Parses the data containing an ID3v1 tag and returns a #GstTagList from the
251 * Returns: A new tag list or NULL if the data was not an ID3v1 tag.
254 gst_tag_list_new_from_id3v1 (const guint8 * data)
260 g_return_val_if_fail (data != NULL, NULL);
262 if (data[0] != 'T' || data[1] != 'A' || data[2] != 'G')
264 list = gst_tag_list_new ();
265 gst_tag_extract_id3v1_string (list, GST_TAG_TITLE, (gchar *) & data[3], 30);
266 gst_tag_extract_id3v1_string (list, GST_TAG_ARTIST, (gchar *) & data[33], 30);
267 gst_tag_extract_id3v1_string (list, GST_TAG_ALBUM, (gchar *) & data[63], 30);
268 ystr = g_strndup ((gchar *) & data[93], 4);
269 year = strtoul (ystr, NULL, 10);
272 GDate *date = g_date_new_dmy (1, 1, year);
274 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date, NULL);
277 if (data[125] == 0 && data[126] != 0) {
278 gst_tag_extract_id3v1_string (list, GST_TAG_COMMENT, (gchar *) & data[97],
280 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_TRACK_NUMBER,
281 (guint) data[126], NULL);
283 gst_tag_extract_id3v1_string (list, GST_TAG_COMMENT, (gchar *) & data[97],
286 if (data[127] < gst_tag_id3_genre_count () && !gst_tag_list_is_empty (list)) {
287 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
288 gst_tag_id3_genre_get (data[127]), NULL);
295 * gst_tag_id3_genre_count:
297 * Gets the number of ID3v1 genres that can be identified. Winamp genres are
300 * Returns: the number of ID3v1 genres that can be identified
303 gst_tag_id3_genre_count (void)
305 return G_N_ELEMENTS (genres_idx);
309 * gst_tag_id3_genre_get:
310 * @id: ID of genre to query
312 * Gets the ID3v1 genre name for a given ID.
314 * Returns: the genre or NULL if no genre is associated with that ID.
317 gst_tag_id3_genre_get (const guint id)
321 if (id >= G_N_ELEMENTS (genres_idx))
323 idx = genres_idx[id];
324 g_assert (idx < sizeof (genres));
329 * gst_tag_list_add_id3_image:
330 * @tag_list: a tag list
331 * @image_data: the (encoded) image
332 * @image_data_len: the length of the encoded image data at @image_data
333 * @id3_picture_type: picture type as per the ID3 (v2.4.0) specification for
334 * the APIC frame (0 = unknown/other)
336 * Adds an image from an ID3 APIC frame (or similar, such as used in FLAC)
337 * to the given tag list. Also see gst_tag_image_data_to_image_buffer() for
338 * more information on image tags in GStreamer.
340 * Returns: %TRUE if the image was processed, otherwise %FALSE
345 gst_tag_list_add_id3_image (GstTagList * tag_list, const guint8 * image_data,
346 guint image_data_len, guint id3_picture_type)
348 GstTagImageType tag_image_type;
349 const gchar *tag_name;
352 g_return_val_if_fail (GST_IS_TAG_LIST (tag_list), FALSE);
353 g_return_val_if_fail (image_data != NULL, FALSE);
354 g_return_val_if_fail (image_data_len > 0, FALSE);
356 if (id3_picture_type == 0x01 || id3_picture_type == 0x02) {
357 /* file icon for preview. Don't add image-type to caps, since there
358 * is only supposed to be one of these, and the type is already indicated
359 * via the special tag */
360 tag_name = GST_TAG_PREVIEW_IMAGE;
361 tag_image_type = GST_TAG_IMAGE_TYPE_NONE;
363 tag_name = GST_TAG_IMAGE;
365 /* Remap the ID3v2 APIC type our ImageType enum */
366 if (id3_picture_type >= 0x3 && id3_picture_type <= 0x14)
367 tag_image_type = (GstTagImageType) (id3_picture_type - 2);
369 tag_image_type = GST_TAG_IMAGE_TYPE_UNDEFINED;
372 image = gst_tag_image_data_to_image_buffer (image_data, image_data_len,
378 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, tag_name, image, NULL);
379 gst_buffer_unref (image);