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.
23 * SECTION:gsttagvorbis
24 * @short_description: tag mappings and support functions for plugins
25 * dealing with vorbiscomments
26 * @see_also: #GstTagList
30 * Contains various utility functions for plugins to parse or create
31 * vorbiscomments and map them to and from #GstTagList<!-- -->s.
39 #include <gst/gsttagsetter.h>
40 #include "gsttageditingprivate.h"
44 static GstTagEntryMatch tag_matches[] = {
45 {GST_TAG_TITLE, "TITLE"},
46 {GST_TAG_VERSION, "VERSION"},
47 {GST_TAG_ALBUM, "ALBUM"},
48 {GST_TAG_TRACK_NUMBER, "TRACKNUMBER"},
49 {GST_TAG_ALBUM_VOLUME_NUMBER, "DISCNUMBER"},
50 {GST_TAG_TRACK_COUNT, "TRACKTOTAL"},
51 {GST_TAG_ALBUM_VOLUME_COUNT, "DISCTOTAL"},
52 {GST_TAG_ARTIST, "ARTIST"},
53 {GST_TAG_PERFORMER, "PERFORMER"},
54 {GST_TAG_COPYRIGHT, "COPYRIGHT"},
55 {GST_TAG_LICENSE, "LICENSE"},
56 {GST_TAG_ORGANIZATION, "ORGANIZATION"},
57 {GST_TAG_DESCRIPTION, "DESCRIPTION"},
58 {GST_TAG_GENRE, "GENRE"},
59 {GST_TAG_DATE, "DATE"},
60 {GST_TAG_CONTACT, "CONTACT"},
61 {GST_TAG_ISRC, "ISRC"},
62 {GST_TAG_COMMENT, "COMMENT"},
63 {GST_TAG_TRACK_GAIN, "REPLAYGAIN_TRACK_GAIN"},
64 {GST_TAG_TRACK_PEAK, "REPLAYGAIN_TRACK_PEAK"},
65 {GST_TAG_ALBUM_GAIN, "REPLAYGAIN_ALBUM_GAIN"},
66 {GST_TAG_ALBUM_PEAK, "REPLAYGAIN_ALBUM_PEAK"},
67 {GST_TAG_MUSICBRAINZ_TRACKID, "MUSICBRAINZ_TRACKID"},
68 {GST_TAG_MUSICBRAINZ_ARTISTID, "MUSICBRAINZ_ARTISTID"},
69 {GST_TAG_MUSICBRAINZ_ALBUMID, "MUSICBRAINZ_ALBUMID"},
70 {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "MUSICBRAINZ_ALBUMARTISTID"},
71 {GST_TAG_MUSICBRAINZ_TRMID, "MUSICBRAINZ_TRMID"},
72 {GST_TAG_MUSICBRAINZ_SORTNAME, "MUSICBRAINZ_SORTNAME"},
73 {GST_TAG_LANGUAGE_CODE, "LANGUAGE"},
78 * gst_tag_from_vorbis_tag:
79 * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
81 * Looks up the GStreamer tag for a vorbiscommment tag.
83 * Returns: The corresponding GStreamer tag or NULL if none exists.
85 G_CONST_RETURN gchar *
86 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
89 gchar *real_vorbis_tag;
91 g_return_val_if_fail (vorbis_tag != NULL, NULL);
93 real_vorbis_tag = g_ascii_strup (vorbis_tag, -1);
94 while (tag_matches[i].gstreamer_tag != NULL) {
95 if (strcmp (real_vorbis_tag, tag_matches[i].original_tag) == 0) {
100 g_free (real_vorbis_tag);
101 return tag_matches[i].gstreamer_tag;
105 * gst_tag_to_vorbis_tag:
106 * @gst_tag: GStreamer tag to convert to vorbiscomment tag
108 * Looks up the vorbiscommment tag for a GStreamer tag.
110 * Returns: The corresponding vorbiscommment tag or NULL if none exists.
112 G_CONST_RETURN gchar *
113 gst_tag_to_vorbis_tag (const gchar * gst_tag)
117 g_return_val_if_fail (gst_tag != NULL, NULL);
119 while (tag_matches[i].gstreamer_tag != NULL) {
120 if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
121 return tag_matches[i].original_tag;
130 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
132 const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
138 tag_type = gst_tag_get_type (gst_tag);
143 gboolean is_track_number_tag;
144 gboolean is_disc_number_tag;
146 is_track_number_tag = (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0);
147 is_disc_number_tag = (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0);
148 tmp = strtoul (value, &check, 10);
149 if (*check == '/' && (is_track_number_tag || is_disc_number_tag)) {
153 count = strtoul (check, &check, 10);
154 if (*check != '\0' || count == 0)
156 if (is_track_number_tag) {
157 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
160 gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
161 GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
164 if (*check == '\0') {
165 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
172 /* specialcase for language code */
173 if (strcmp (tag, "LANGUAGE") == 0) {
174 const gchar *s = strchr (value, '[');
176 /* FIXME: gsttaglist.h says our language tag contains ISO-639-1
177 * codes, which are 2 letter codes. The code below extracts 3-letter
178 * identifiers, which would be ISO-639-2. Mixup? Oversight? Wrong core
179 * docs? What do files in the wild contain? (tpm) */
180 if (s && strchr (s, ']') == s + 4) {
181 valid = g_strndup (s + 1, 3);
186 if (!g_utf8_validate (value, -1, (const gchar **) &valid)) {
187 valid = g_strndup (value, valid - value);
188 GST_DEBUG ("Invalid vorbis comment tag, truncated it to %s", valid);
190 valid = g_strdup (value);
193 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
198 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
203 if (tag_type == GST_TYPE_DATE) {
204 guint y, d = 1, m = 1;
205 gchar *check = (gchar *) value;
207 y = strtoul (check, &check, 10);
210 m = strtoul (check, &check, 10);
213 d = strtoul (check, &check, 10);
216 if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
219 date = g_date_new_dmy (d, m, y);
220 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
223 GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
226 GST_WARNING ("Unhandled tag of type '%s' (%d)",
227 g_type_name (tag_type), (gint) tag_type);
235 * gst_tag_list_from_vorbiscomment_buffer:
236 * @buffer: buffer to convert
237 * @id_data: identification data at start of stream
238 * @id_data_length: length of identification data
239 * @vendor_string: pointer to a string that should take the vendor string
240 * of this vorbis comment or NULL if you don't need it.
242 * Creates a new tag list that contains the information parsed out of a
243 * vorbiscomment packet.
245 * Returns: A new #GstTagList with all tags that could be extracted from the
246 * given vorbiscomment buffer or NULL on error.
249 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
250 const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
252 #define ADVANCE(x) G_STMT_START{ \
255 if (size < 4) goto error; \
256 cur_size = GST_READ_UINT32_LE (data); \
259 if (cur_size > size) goto error; \
260 cur = (gchar*)data; \
269 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
270 g_return_val_if_fail (id_data != NULL, NULL);
271 g_return_val_if_fail (id_data_length > 0, NULL);
273 data = GST_BUFFER_DATA (buffer);
274 size = GST_BUFFER_SIZE (buffer);
275 list = gst_tag_list_new ();
279 if (memcmp (data, id_data, id_data_length) != 0)
281 ADVANCE (id_data_length);
283 *vendor_string = g_strndup (cur, cur_size);
285 iterations = cur_size;
290 cur = g_strndup (cur, cur_size);
291 value = strchr (cur, '=');
298 if (!g_utf8_validate (value, -1, NULL)) {
302 gst_vorbis_tag_add (list, cur, value);
309 gst_tag_list_free (list);
322 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
326 const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
331 for (i = 0; i < gst_tag_list_get_tag_size (list, tag); i++) {
332 GType tag_type = gst_tag_get_type (tag);
333 gchar *result = NULL;
339 if (!gst_tag_list_get_uint_index (list, tag, i, &u))
340 g_return_val_if_reached (NULL);
341 result = g_strdup_printf ("%s=%u", vorbis_tag, u);
347 if (!gst_tag_list_get_string_index (list, tag, i, &str))
348 g_return_val_if_reached (NULL);
349 result = g_strdup_printf ("%s=%s", vorbis_tag, str);
356 if (!gst_tag_list_get_double_index (list, tag, i, &value))
357 g_return_val_if_reached (NULL);
358 /* FIXME: what about locale-specific floating point separators? */
359 result = g_strdup_printf ("%s=%f", vorbis_tag, value);
363 if (tag_type == GST_TYPE_DATE) {
366 if (gst_tag_list_get_date_index (list, tag, i, &date)) {
367 /* vorbis suggests using ISO date formats */
369 g_strdup_printf ("%s=%04d-%02d-%02d", vorbis_tag,
370 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
371 (gint) g_date_get_day (date));
375 GST_DEBUG ("Couldn't write tag %s", tag);
381 l = g_list_prepend (l, result);
384 return g_list_reverse (l);
388 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
390 MyForEach *data = (MyForEach *) user_data;
394 comments = gst_tag_to_vorbis_comments (list, tag);
396 for (it = comments; it != NULL; it = it->next) {
397 gchar *result = it->data;
400 data->data_count += strlen (result);
401 data->entries = g_list_prepend (data->entries, result);
406 * gst_tag_list_to_vorbiscomment_buffer:
407 * @list: tag list to convert
408 * @id_data: identification data at start of stream
409 * @id_data_length: length of identification data
410 * @vendor_string: string that describes the vendor string or NULL
412 * Creates a new vorbiscomment buffer from a tag list.
414 * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that
415 * could be converted from the given tag list.
418 gst_tag_list_to_vorbiscomment_buffer (const GstTagList * list,
419 const guint8 * id_data, const guint id_data_length,
420 const gchar * vendor_string)
426 MyForEach my_data = { 0, 0, NULL };
430 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
431 g_return_val_if_fail (id_data != NULL, NULL);
432 g_return_val_if_fail (id_data_length > 0, NULL);
434 if (vendor_string == NULL)
435 vendor_string = "GStreamer encoded vorbiscomment";
436 vendor_len = strlen (vendor_string);
437 required_size = id_data_length + 4 + vendor_len + 4 + 1;
438 gst_tag_list_foreach ((GstTagList *) list, write_one_tag, &my_data);
439 required_size += 4 * my_data.count + my_data.data_count;
440 buffer = gst_buffer_new_and_alloc (required_size);
441 data = GST_BUFFER_DATA (buffer);
442 memcpy (data, id_data, id_data_length);
443 data += id_data_length;
444 *((guint32 *) data) = GUINT32_TO_LE (vendor_len);
446 memcpy (data, vendor_string, vendor_len);
448 l = my_data.entries = g_list_reverse (my_data.entries);
449 *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
451 for (i = 0; i < my_data.count; i++) {
455 g_assert (l != NULL);
459 *((guint32 *) data) = GUINT32_TO_LE (size);
461 memcpy (data, cur, size);
464 g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
465 g_list_free (my_data.entries);