1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gstid3tag.c: plugin for reading / modifying id3 tags
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * SECTION:gsttagid3
24  * @title: ID3 tag utils
25  * @short_description: tag mappings and support functions for plugins
26  *                     dealing with ID3v1 and ID3v2 tags
27  * @see_also: #GstTagList
28  *
29  * Contains various utility functions for plugins to parse or create
30  * ID3 tags and map ID3v2 identifiers to and from GStreamer identifiers.
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include "gsttageditingprivate.h"
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "id3v2.h"
43 
44 #ifndef GST_DISABLE_GST_DEBUG
45 #define GST_CAT_DEFAULT id3v2_ensure_debug_category()
46 #endif
47 
48 static const gchar genres[] =
49     "Blues\000Classic Rock\000Country\000Dance\000Disco\000Funk\000Grunge\000"
50     "Hip-Hop\000Jazz\000Metal\000New Age\000Oldies\000Other\000Pop\000R&B\000"
51     "Rap\000Reggae\000Rock\000Techno\000Industrial\000Alternative\000Ska\000"
52     "Death Metal\000Pranks\000Soundtrack\000Euro-Techno\000Ambient\000Trip-Hop"
53     "\000Vocal\000Jazz+Funk\000Fusion\000Trance\000Classical\000Instrumental\000"
54     "Acid\000House\000Game\000Sound Clip\000Gospel\000Noise\000Alternative Rock"
55     "\000Bass\000Soul\000Punk\000Space\000Meditative\000Instrumental Pop\000"
56     "Instrumental Rock\000Ethnic\000Gothic\000Darkwave\000Techno-Industrial\000"
57     "Electronic\000Pop-Folk\000Eurodance\000Dream\000Southern Rock\000Comedy"
58     "\000Cult\000Gangsta\000Top 40\000Christian Rap\000Pop/Funk\000Jungle\000"
59     "Native American\000Cabaret\000New Wave\000Psychedelic\000Rave\000Showtunes"
60     "\000Trailer\000Lo-Fi\000Tribal\000Acid Punk\000Acid Jazz\000Polka\000"
61     "Retro\000Musical\000Rock & Roll\000Hard Rock\000Folk\000Folk/Rock\000"
62     "National Folk\000Swing\000Bebob\000Latin\000Revival\000Celtic\000Bluegrass"
63     "\000Avantgarde\000Gothic Rock\000Progressive Rock\000Psychedelic Rock\000"
64     "Symphonic Rock\000Slow Rock\000Big Band\000Chorus\000Easy Listening\000"
65     "Acoustic\000Humour\000Speech\000Chanson\000Opera\000Chamber Music\000"
66     "Sonata\000Symphony\000Booty Bass\000Primus\000Porn Groove\000Satire\000"
67     "Slow Jam\000Club\000Tango\000Samba\000Folklore\000Ballad\000Power Ballad\000"
68     "Rhythmic Soul\000Freestyle\000Duet\000Punk Rock\000Drum Solo\000A Capella"
69     "\000Euro-House\000Dance Hall\000Goa\000Drum & Bass\000Club-House\000"
70     "Hardcore\000Terror\000Indie\000BritPop\000Negerpunk\000Polsk Punk\000"
71     "Beat\000Christian Gangsta Rap\000Heavy Metal\000Black Metal\000"
72     "Crossover\000Contemporary Christian\000Christian Rock\000Merengue\000"
73     "Salsa\000Thrash Metal\000Anime\000Jpop\000Synthpop";
74 
75 static const guint16 genres_idx[] = {
76   0, 6, 19, 27, 33, 39, 44, 51, 59, 64, 70, 78, 85, 91, 95, 99, 103, 110, 115,
77   122, 133, 145, 149, 161, 168, 179, 191, 199, 208, 214, 224, 231, 238, 248,
78   261, 266, 272, 277, 288, 295, 301, 318, 323, 328, 333, 339, 350, 367, 385,
79   392, 399, 408, 426, 437, 446, 456, 462, 476, 483, 488, 496, 503, 517, 526,
80   533, 549, 557, 566, 578, 583, 593, 601, 607, 614, 624, 634, 640, 646, 654,
81   666, 676, 681, 691, 705, 224, 711, 717, 723, 731, 738, 748, 759, 771, 788,
82   805, 820, 830, 839, 846, 861, 870, 877, 884, 892, 898, 912, 919, 928, 939,
83   946, 958, 965, 974, 979, 985, 991, 1000, 1007, 1020, 1034, 1044, 1049,
84   1059, 1069, 1079, 1090, 1101, 1105, 1117, 1128, 1137, 1144, 1150, 1158,
85   1168, 1179, 1184, 1206, 1218, 1230, 1240, 1263, 1278, 1287, 1293, 1306,
86   1312, 1317
87 };
88 
89 static const GstTagEntryMatch tag_matches[] = {
90   {GST_TAG_TITLE, "TIT2"},
91   {GST_TAG_ALBUM, "TALB"},
92   {GST_TAG_TRACK_NUMBER, "TRCK"},
93   {GST_TAG_ARTIST, "TPE1"},
94   {GST_TAG_ALBUM_ARTIST, "TPE2"},
95   {GST_TAG_COMPOSER, "TCOM"},
96   {GST_TAG_CONDUCTOR, "TPE3"},
97   {GST_TAG_COPYRIGHT, "TCOP"},
98   {GST_TAG_COPYRIGHT_URI, "WCOP"},
99   {GST_TAG_ENCODED_BY, "TENC"},
100   {GST_TAG_GENRE, "TCON"},
101   {GST_TAG_DATE_TIME, "TDRC"},
102   {GST_TAG_COMMENT, "COMM"},
103   {GST_TAG_ALBUM_VOLUME_NUMBER, "TPOS"},
104   {GST_TAG_DURATION, "TLEN"},
105   {GST_TAG_ISRC, "TSRC"},
106   {GST_TAG_IMAGE, "APIC"},
107   {GST_TAG_ENCODER, "TSSE"},
108   {GST_TAG_BEATS_PER_MINUTE, "TBPM"},
109   {GST_TAG_ARTIST_SORTNAME, "TSOP"},
110   {GST_TAG_ALBUM_SORTNAME, "TSOA"},
111   {GST_TAG_TITLE_SORTNAME, "TSOT"},
112   {GST_TAG_PUBLISHER, "TPUB"},
113   {GST_TAG_INTERPRETED_BY, "TPE4"},
114   {GST_TAG_MUSICAL_KEY, "TKEY"},
115   {GST_TAG_PRIVATE_DATA, "PRIV"},
116   {NULL, NULL}
117 };
118 
119 /**
120  * gst_tag_from_id3_tag:
121  * @id3_tag: ID3v2 tag to convert to GStreamer tag
122  *
123  * Looks up the GStreamer tag for a ID3v2 tag.
124  *
125  * Returns: The corresponding GStreamer tag or NULL if none exists.
126  */
127 const gchar *
gst_tag_from_id3_tag(const gchar * id3_tag)128 gst_tag_from_id3_tag (const gchar * id3_tag)
129 {
130   int i = 0;
131 
132   g_return_val_if_fail (id3_tag != NULL, NULL);
133 
134   while (tag_matches[i].gstreamer_tag != NULL) {
135     if (strncmp (id3_tag, tag_matches[i].original_tag, 5) == 0) {
136       return tag_matches[i].gstreamer_tag;
137     }
138     i++;
139   }
140 
141   GST_FIXME ("Cannot map ID3v2 tag '%c%c%c%c' to GStreamer tag",
142       id3_tag[0], id3_tag[1], id3_tag[2], id3_tag[3]);
143 
144   return NULL;
145 }
146 
147 static const GstTagEntryMatch user_tag_matches[] = {
148   /* musicbrainz identifiers being used in the real world (foobar2000) */
149   {GST_TAG_MUSICBRAINZ_ARTISTID, "TXXX|musicbrainz_artistid"},
150   {GST_TAG_MUSICBRAINZ_ALBUMID, "TXXX|musicbrainz_albumid"},
151   {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "TXXX|musicbrainz_albumartistid"},
152   {GST_TAG_MUSICBRAINZ_TRMID, "TXXX|musicbrainz_trmid"},
153   {GST_TAG_CDDA_MUSICBRAINZ_DISCID, "TXXX|musicbrainz_discid"},
154   /* musicbrainz identifiers according to spec no one pays
155    * attention to (http://musicbrainz.org/docs/specs/metadata_tags.html) */
156   {GST_TAG_MUSICBRAINZ_ARTISTID, "TXXX|MusicBrainz Artist Id"},
157   {GST_TAG_MUSICBRAINZ_ALBUMID, "TXXX|MusicBrainz Album Id"},
158   {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "TXXX|MusicBrainz Album Artist Id"},
159   {GST_TAG_MUSICBRAINZ_TRMID, "TXXX|MusicBrainz TRM Id"},
160   /* according to: http://wiki.musicbrainz.org/MusicBrainzTag (yes, no space
161    * before 'ID' and not 'Id' either this time, yay for consistency) */
162   {GST_TAG_CDDA_MUSICBRAINZ_DISCID, "TXXX|MusicBrainz DiscID"},
163   /* foobar2000 uses these identifiers to store gain/peak information in
164    * ID3v2 tags <= v2.3.0. In v2.4.0 there's the RVA2 frame for that */
165   {GST_TAG_TRACK_GAIN, "TXXX|replaygain_track_gain"},
166   {GST_TAG_TRACK_PEAK, "TXXX|replaygain_track_peak"},
167   {GST_TAG_ALBUM_GAIN, "TXXX|replaygain_album_gain"},
168   {GST_TAG_ALBUM_PEAK, "TXXX|replaygain_album_peak"},
169   /* the following two are more or less made up, there seems to be little
170    * evidence that any popular application is actually putting this info
171    * into TXXX frames; the first one comes from a musicbrainz wiki 'proposed
172    * tags' page, the second one is analogue to the vorbis/ape/flac tag. */
173   {GST_TAG_CDDA_CDDB_DISCID, "TXXX|discid"},
174   {GST_TAG_CDDA_CDDB_DISCID, "TXXX|CDDB DiscID"}
175 };
176 
177 /**
178  * gst_tag_from_id3_user_tag:
179  * @type: the type of ID3v2 user tag (e.g. "TXXX" or "UDIF")
180  * @id3_user_tag: ID3v2 user tag to convert to GStreamer tag
181  *
182  * Looks up the GStreamer tag for an ID3v2 user tag (e.g. description in
183  * TXXX frame or owner in UFID frame).
184  *
185  * Returns: The corresponding GStreamer tag or NULL if none exists.
186  */
187 const gchar *
gst_tag_from_id3_user_tag(const gchar * type,const gchar * id3_user_tag)188 gst_tag_from_id3_user_tag (const gchar * type, const gchar * id3_user_tag)
189 {
190   int i = 0;
191 
192   g_return_val_if_fail (type != NULL && strlen (type) == 4, NULL);
193   g_return_val_if_fail (id3_user_tag != NULL, NULL);
194 
195   for (i = 0; i < G_N_ELEMENTS (user_tag_matches); ++i) {
196     if (strncmp (type, user_tag_matches[i].original_tag, 4) == 0 &&
197         g_ascii_strcasecmp (id3_user_tag,
198             user_tag_matches[i].original_tag + 5) == 0) {
199       GST_LOG ("Mapped ID3v2 user tag '%s' to GStreamer tag '%s'",
200           user_tag_matches[i].original_tag, user_tag_matches[i].gstreamer_tag);
201       return user_tag_matches[i].gstreamer_tag;
202     }
203   }
204 
205   GST_FIXME ("Cannot map ID3v2 user tag '%s' of type '%s' to GStreamer tag",
206       id3_user_tag, type);
207 
208   return NULL;
209 }
210 
211 /**
212  * gst_tag_to_id3_tag:
213  * @gst_tag: GStreamer tag to convert to vorbiscomment tag
214  *
215  * Looks up the ID3v2 tag for a GStreamer tag.
216  *
217  * Returns: The corresponding ID3v2 tag or NULL if none exists.
218  */
219 const gchar *
gst_tag_to_id3_tag(const gchar * gst_tag)220 gst_tag_to_id3_tag (const gchar * gst_tag)
221 {
222   int i = 0;
223 
224   g_return_val_if_fail (gst_tag != NULL, NULL);
225 
226   while (tag_matches[i].gstreamer_tag != NULL) {
227     if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
228       return tag_matches[i].original_tag;
229     }
230     i++;
231   }
232   return NULL;
233 }
234 
235 static void
gst_tag_extract_id3v1_string(GstTagList * list,const gchar * tag,const gchar * start,const guint size)236 gst_tag_extract_id3v1_string (GstTagList * list, const gchar * tag,
237     const gchar * start, const guint size)
238 {
239   const gchar *env_vars[] = { "GST_ID3V1_TAG_ENCODING",
240     "GST_ID3_TAG_ENCODING", "GST_TAG_ENCODING", NULL
241   };
242   gchar *utf8;
243 
244   utf8 = gst_tag_freeform_string_to_utf8 (start, size, env_vars);
245 
246   if (utf8 && *utf8 != '\0') {
247     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, tag, utf8, NULL);
248   }
249 
250   g_free (utf8);
251 }
252 
253 /**
254  * gst_tag_list_new_from_id3v1:
255  * @data: (array fixed-size=128): 128 bytes of data containing the ID3v1 tag
256  *
257  * Parses the data containing an ID3v1 tag and returns a #GstTagList from the
258  * parsed data.
259  *
260  * Returns: A new tag list or NULL if the data was not an ID3v1 tag.
261  */
262 GstTagList *
gst_tag_list_new_from_id3v1(const guint8 * data)263 gst_tag_list_new_from_id3v1 (const guint8 * data)
264 {
265   gint64 year;
266   gchar *ystr;
267   GstTagList *list;
268 
269   g_return_val_if_fail (data != NULL, NULL);
270 
271   if (data[0] != 'T' || data[1] != 'A' || data[2] != 'G')
272     return NULL;
273   list = gst_tag_list_new_empty ();
274   gst_tag_extract_id3v1_string (list, GST_TAG_TITLE, (gchar *) & data[3], 30);
275   gst_tag_extract_id3v1_string (list, GST_TAG_ARTIST, (gchar *) & data[33], 30);
276   gst_tag_extract_id3v1_string (list, GST_TAG_ALBUM, (gchar *) & data[63], 30);
277   ystr = g_strndup ((gchar *) & data[93], 4);
278   year = g_ascii_strtoll (ystr, NULL, 10);
279   g_free (ystr);
280   if (year > 0 && year <= 9999) {
281     GstDateTime *dt = gst_date_time_new_y (year);
282 
283     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME, dt, NULL);
284     gst_date_time_unref (dt);
285   }
286   if (data[125] == 0 && data[126] != 0) {
287     gst_tag_extract_id3v1_string (list, GST_TAG_COMMENT, (gchar *) & data[97],
288         28);
289     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_TRACK_NUMBER,
290         (guint) data[126], NULL);
291   } else {
292     gst_tag_extract_id3v1_string (list, GST_TAG_COMMENT, (gchar *) & data[97],
293         30);
294   }
295   if (data[127] < gst_tag_id3_genre_count () && !gst_tag_list_is_empty (list)) {
296     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE,
297         gst_tag_id3_genre_get (data[127]), NULL);
298   }
299 
300   return list;
301 }
302 
303 /**
304  * gst_tag_id3_genre_count:
305  *
306  * Gets the number of ID3v1 genres that can be identified. Winamp genres are
307  * included.
308  *
309  * Returns: the number of ID3v1 genres that can be identified
310  */
311 guint
gst_tag_id3_genre_count(void)312 gst_tag_id3_genre_count (void)
313 {
314   return G_N_ELEMENTS (genres_idx);
315 }
316 
317 /**
318  * gst_tag_id3_genre_get:
319  * @id: ID of genre to query
320  *
321  * Gets the ID3v1 genre name for a given ID.
322  *
323  * Returns: the genre or NULL if no genre is associated with that ID.
324  */
325 const gchar *
gst_tag_id3_genre_get(const guint id)326 gst_tag_id3_genre_get (const guint id)
327 {
328   guint idx;
329 
330   if (id >= G_N_ELEMENTS (genres_idx))
331     return NULL;
332   idx = genres_idx[id];
333   g_assert (idx < sizeof (genres));
334   return &genres[idx];
335 }
336 
337 /**
338  * gst_tag_list_add_id3_image:
339  * @tag_list: a tag list
340  * @image_data: (array length=image_data_len): the (encoded) image
341  * @image_data_len: the length of the encoded image data at @image_data
342  * @id3_picture_type: picture type as per the ID3 (v2.4.0) specification for
343  *    the APIC frame (0 = unknown/other)
344  *
345  * Adds an image from an ID3 APIC frame (or similar, such as used in FLAC)
346  * to the given tag list. Also see gst_tag_image_data_to_image_sample() for
347  * more information on image tags in GStreamer.
348  *
349  * Returns: %TRUE if the image was processed, otherwise %FALSE
350  */
351 gboolean
gst_tag_list_add_id3_image(GstTagList * tag_list,const guint8 * image_data,guint image_data_len,guint id3_picture_type)352 gst_tag_list_add_id3_image (GstTagList * tag_list, const guint8 * image_data,
353     guint image_data_len, guint id3_picture_type)
354 {
355   GstTagImageType tag_image_type;
356   const gchar *tag_name;
357   GstSample *image;
358 
359   g_return_val_if_fail (GST_IS_TAG_LIST (tag_list), FALSE);
360   g_return_val_if_fail (image_data != NULL, FALSE);
361   g_return_val_if_fail (image_data_len > 0, FALSE);
362 
363   if (id3_picture_type == 0x01 || id3_picture_type == 0x02) {
364     /* file icon for preview. Don't add image-type to caps, since there
365      * is only supposed to be one of these, and the type is already indicated
366      * via the special tag */
367     tag_name = GST_TAG_PREVIEW_IMAGE;
368     tag_image_type = GST_TAG_IMAGE_TYPE_NONE;
369   } else {
370     tag_name = GST_TAG_IMAGE;
371 
372     /* Remap the ID3v2 APIC type our ImageType enum */
373     if (id3_picture_type >= 0x3 && id3_picture_type <= 0x14)
374       tag_image_type = (GstTagImageType) (id3_picture_type - 2);
375     else
376       tag_image_type = GST_TAG_IMAGE_TYPE_UNDEFINED;
377   }
378 
379   image = gst_tag_image_data_to_image_sample (image_data, image_data_len,
380       tag_image_type);
381 
382   if (image == NULL)
383     return FALSE;
384 
385   gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, tag_name, image, NULL);
386   gst_sample_unref (image);
387   return TRUE;
388 }
389