2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gstvorbistagsetter.c: plugin for reading / modifying vorbis 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.
25 #include <gst/gsttagsetter.h>
26 #include "gsttageditingprivate.h"
30 static GstTagEntryMatch tag_matches[] = {
31 {GST_TAG_TITLE, "TITLE"},
32 {GST_TAG_VERSION, "VERSION"},
33 {GST_TAG_ALBUM, "ALBUM"},
34 {GST_TAG_TRACK_NUMBER, "TRACKNUMBER"},
35 {GST_TAG_ALBUM_VOLUME_NUMBER, "DISCNUMBER"},
36 {GST_TAG_TRACK_COUNT, "TRACKTOTAL"},
37 {GST_TAG_ALBUM_VOLUME_COUNT, "DISCTOTAL"},
38 {GST_TAG_ARTIST, "ARTIST"},
39 {GST_TAG_PERFORMER, "PERFORMER"},
40 {GST_TAG_COPYRIGHT, "COPYRIGHT"},
41 {GST_TAG_LICENSE, "LICENSE"},
42 {GST_TAG_ORGANIZATION, "ORGANIZATION"},
43 {GST_TAG_DESCRIPTION, "DESCRIPTION"},
44 {GST_TAG_GENRE, "GENRE"},
45 {GST_TAG_DATE, "DATE"},
46 {GST_TAG_CONTACT, "CONTACT"},
47 {GST_TAG_ISRC, "ISRC"},
48 {GST_TAG_COMMENT, "COMMENT"},
49 {GST_TAG_TRACK_GAIN, "REPLAYGAIN_TRACK_GAIN"},
50 {GST_TAG_TRACK_PEAK, "REPLAYGAIN_TRACK_PEAK"},
51 {GST_TAG_ALBUM_GAIN, "REPLAYGAIN_ALBUM_GAIN"},
52 {GST_TAG_ALBUM_PEAK, "REPLAYGAIN_ALBUM_PEAK"},
53 {GST_TAG_MUSICBRAINZ_TRACKID, "MUSICBRAINZ_TRACKID"},
54 {GST_TAG_MUSICBRAINZ_ARTISTID, "MUSICBRAINZ_ARTISTID"},
55 {GST_TAG_MUSICBRAINZ_ALBUMID, "MUSICBRAINZ_ALBUMID"},
56 {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "MUSICBRAINZ_ALBUMARTISTID"},
57 {GST_TAG_MUSICBRAINZ_TRMID, "MUSICBRAINZ_TRMID"},
58 {GST_TAG_MUSICBRAINZ_SORTNAME, "MUSICBRAINZ_SORTNAME"},
59 {GST_TAG_LANGUAGE_CODE, "LANGUAGE"},
64 * gst_tag_from_vorbis_tag:
65 * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
67 * Looks up the GStreamer tag for a vorbiscommment tag.
69 * Returns: The corresponding GStreamer tag or NULL if none exists.
71 G_CONST_RETURN gchar *
72 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
75 gchar *real_vorbis_tag;
77 g_return_val_if_fail (vorbis_tag != NULL, NULL);
79 real_vorbis_tag = g_ascii_strup (vorbis_tag, -1);
80 while (tag_matches[i].gstreamer_tag != NULL) {
81 if (strcmp (real_vorbis_tag, tag_matches[i].original_tag) == 0) {
86 g_free (real_vorbis_tag);
87 return tag_matches[i].gstreamer_tag;
91 * gst_tag_to_vorbis_tag:
92 * @gst_tag: GStreamer tag to convert to vorbiscomment tag
94 * Looks up the vorbiscommment tag for a GStreamer tag.
96 * Returns: The corresponding vorbiscommment tag or NULL if none exists.
98 G_CONST_RETURN gchar *
99 gst_tag_to_vorbis_tag (const gchar * gst_tag)
103 g_return_val_if_fail (gst_tag != NULL, NULL);
105 while (tag_matches[i].gstreamer_tag != NULL) {
106 if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
107 return tag_matches[i].original_tag;
116 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
118 const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
124 tag_type = gst_tag_get_type (gst_tag);
129 gboolean is_track_number_tag;
130 gboolean is_disc_number_tag;
132 is_track_number_tag = (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0);
133 is_disc_number_tag = (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0);
134 tmp = strtoul (value, &check, 10);
135 if (*check == '/' && (is_track_number_tag || is_disc_number_tag)) {
139 count = strtoul (check, &check, 10);
140 if (*check != '\0' || count == 0)
142 if (is_track_number_tag) {
143 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
146 gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
147 GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
150 if (*check == '\0') {
151 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
158 /* specialcase for language code */
159 if (strcmp (tag, "LANGUAGE") == 0) {
160 const gchar *s = strchr (value, '[');
162 /* FIXME: gsttaglist.h says our language tag contains ISO-639-1
163 * codes, which are 2 letter codes. The code below extracts 3-letter
164 * identifiers, which would be ISO-639-2. Mixup? Oversight? Wrong core
165 * docs? What do files in the wild contain? (tpm) */
166 if (s && strchr (s, ']') == s + 4) {
167 valid = g_strndup (s + 1, 3);
172 if (!g_utf8_validate (value, -1, (const gchar **) &valid)) {
173 valid = g_strndup (value, valid - value);
174 GST_DEBUG ("Invalid vorbis comment tag, truncated it to %s", valid);
176 valid = g_strdup (value);
179 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
184 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
189 if (tag_type == GST_TYPE_DATE) {
190 guint y, d = 1, m = 1;
191 gchar *check = (gchar *) value;
193 y = strtoul (check, &check, 10);
196 m = strtoul (check, &check, 10);
199 d = strtoul (check, &check, 10);
202 if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
205 date = g_date_new_dmy (d, m, y);
206 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
209 GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
212 GST_WARNING ("Unhandled tag of type '%s' (%d)",
213 g_type_name (tag_type), (gint) tag_type);
221 * gst_tag_list_from_vorbiscomment_buffer:
222 * @buffer: buffer to convert
223 * @id_data: identification data at start of stream
224 * @id_data_length: length of identification data
225 * @vendor_string: pointer to a string that should take the vendor string
226 * of this vorbis comment or NULL if you don't need it.
228 * Creates a new tag list that contains the information parsed out of a
229 * vorbiscomment packet.
231 * Returns: A new #GstTagList with all tags that could be extracted from the
232 * given vorbiscomment buffer or NULL on error.
235 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
236 const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
238 #define ADVANCE(x) G_STMT_START{ \
241 if (size < 4) goto error; \
242 cur_size = GST_READ_UINT32_LE (data); \
245 if (cur_size > size) goto error; \
246 cur = (gchar*)data; \
255 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
256 g_return_val_if_fail (id_data != NULL, NULL);
257 g_return_val_if_fail (id_data_length > 0, NULL);
259 data = GST_BUFFER_DATA (buffer);
260 size = GST_BUFFER_SIZE (buffer);
261 list = gst_tag_list_new ();
265 if (memcmp (data, id_data, id_data_length) != 0)
267 ADVANCE (id_data_length);
269 *vendor_string = g_strndup (cur, cur_size);
271 iterations = cur_size;
276 cur = g_strndup (cur, cur_size);
277 value = strchr (cur, '=');
284 if (!g_utf8_validate (value, -1, NULL)) {
288 gst_vorbis_tag_add (list, cur, value);
295 gst_tag_list_free (list);
308 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
312 const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
317 for (i = 0; i < gst_tag_list_get_tag_size (list, tag); i++) {
318 GType tag_type = gst_tag_get_type (tag);
319 gchar *result = NULL;
325 if (!gst_tag_list_get_uint_index (list, tag, i, &u))
326 g_return_val_if_reached (NULL);
327 result = g_strdup_printf ("%s=%u", vorbis_tag, u);
333 if (!gst_tag_list_get_string_index (list, tag, i, &str))
334 g_return_val_if_reached (NULL);
335 result = g_strdup_printf ("%s=%s", vorbis_tag, str);
342 if (!gst_tag_list_get_double_index (list, tag, i, &value))
343 g_return_val_if_reached (NULL);
344 /* FIXME: what about locale-specific floating point separators? */
345 result = g_strdup_printf ("%s=%f", vorbis_tag, value);
349 if (tag_type == GST_TYPE_DATE) {
352 if (gst_tag_list_get_date_index (list, tag, i, &date)) {
353 /* vorbis suggests using ISO date formats */
355 g_strdup_printf ("%s=%04d-%02d-%02d", vorbis_tag,
356 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
357 (gint) g_date_get_day (date));
361 GST_DEBUG ("Couldn't write tag %s", tag);
367 l = g_list_prepend (l, result);
370 return g_list_reverse (l);
374 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
376 MyForEach *data = (MyForEach *) user_data;
380 comments = gst_tag_to_vorbis_comments (list, tag);
382 for (it = comments; it != NULL; it = it->next) {
383 gchar *result = it->data;
386 data->data_count += strlen (result);
387 data->entries = g_list_prepend (data->entries, result);
392 * gst_tag_list_to_vorbiscomment_buffer:
393 * @list: tag list to convert
394 * @id_data: identification data at start of stream
395 * @id_data_length: length of identification data
396 * @vendor_string: string that describes the vendor string or NULL
398 * Creates a new vorbiscomment buffer from a tag list.
400 * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that
401 * could be converted from the given tag list.
404 gst_tag_list_to_vorbiscomment_buffer (const GstTagList * list,
405 const guint8 * id_data, const guint id_data_length,
406 const gchar * vendor_string)
412 MyForEach my_data = { 0, 0, NULL };
416 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
417 g_return_val_if_fail (id_data != NULL, NULL);
418 g_return_val_if_fail (id_data_length > 0, NULL);
420 if (vendor_string == NULL)
421 vendor_string = "GStreamer encoded vorbiscomment";
422 vendor_len = strlen (vendor_string);
423 required_size = id_data_length + 4 + vendor_len + 4 + 1;
424 gst_tag_list_foreach ((GstTagList *) list, write_one_tag, &my_data);
425 required_size += 4 * my_data.count + my_data.data_count;
426 buffer = gst_buffer_new_and_alloc (required_size);
427 data = GST_BUFFER_DATA (buffer);
428 memcpy (data, id_data, id_data_length);
429 data += id_data_length;
430 *((guint32 *) data) = GUINT32_TO_LE (vendor_len);
432 memcpy (data, vendor_string, vendor_len);
434 l = my_data.entries = g_list_reverse (my_data.entries);
435 *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
437 for (i = 0; i < my_data.count; i++) {
441 g_assert (l != NULL);
445 *((guint32 *) data) = GUINT32_TO_LE (size);
447 memcpy (data, cur, size);
450 g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
451 g_list_free (my_data.entries);