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"},
57 * gst_tag_from_vorbis_tag:
58 * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
60 * Looks up the GStreamer tag for a vorbiscommment tag.
62 * Returns: The corresponding GStreamer tag or NULL if none exists.
64 G_CONST_RETURN gchar *
65 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
68 gchar *real_vorbis_tag;
70 g_return_val_if_fail (vorbis_tag != NULL, NULL);
72 real_vorbis_tag = g_ascii_strup (vorbis_tag, -1);
73 while (tag_matches[i].gstreamer_tag != NULL) {
74 if (strcmp (real_vorbis_tag, tag_matches[i].original_tag) == 0) {
79 g_free (real_vorbis_tag);
80 return tag_matches[i].gstreamer_tag;
84 * gst_tag_to_vorbis_tag:
85 * @gst_tag: GStreamer tag to convert to vorbiscomment tag
87 * Looks up the vorbiscommment tag for a GStreamer tag.
89 * Returns: The corresponding vorbiscommment tag or NULL if none exists.
91 G_CONST_RETURN gchar *
92 gst_tag_to_vorbis_tag (const gchar * gst_tag)
96 g_return_val_if_fail (gst_tag != NULL, NULL);
98 while (tag_matches[i].gstreamer_tag != NULL) {
99 if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
100 return tag_matches[i].original_tag;
109 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
111 const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
117 tag_type = gst_tag_get_type (gst_tag);
122 gboolean is_track_number_tag;
123 gboolean is_disc_number_tag;
125 is_track_number_tag = (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0);
126 is_disc_number_tag = (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0);
127 tmp = strtoul (value, &check, 10);
128 if (*check == '/' && (is_track_number_tag || is_disc_number_tag)) {
132 count = strtoul (check, &check, 10);
133 if (*check != '\0' || count == 0)
135 if (is_track_number_tag) {
136 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
139 gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
140 GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
143 if (*check == '\0') {
144 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
151 if (!g_utf8_validate (value, -1, (const gchar **) &valid)) {
152 valid = g_strndup (value, valid - value);
153 GST_DEBUG ("Invalid vorbis comment tag, truncated it to %s\n", valid);
155 valid = g_strdup (value);
157 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
162 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
167 if (tag_type == GST_TYPE_DATE) {
168 guint y, d = 1, m = 1;
169 gchar *check = (gchar *) value;
171 y = strtoul (check, &check, 10);
174 m = strtoul (check, &check, 10);
177 d = strtoul (check, &check, 10);
180 if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
183 date = g_date_new_dmy (d, m, y);
184 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
187 GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
190 GST_WARNING ("Unhandled tag of type '%s' (%d)",
191 g_type_name (tag_type), (gint) tag_type);
199 * gst_tag_list_from_vorbiscomment_buffer:
200 * @buffer: buffer to convert
201 * @id_data: identification data at start of stream
202 * @id_data_length: length of identification data
203 * @ vendor_string: pointer to a string that should take the vendor string of this
204 * vorbis comment or NULL if you don't need it.
206 * Creates a new tag list that contains the information parsed out of a
207 * vorbiscomment packet.
209 * Returns: A new #GstTagList with all tags that could be extracted from the
210 * given vorbiscomment buffer or NULL on error.
213 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
214 const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
216 #define ADVANCE(x) G_STMT_START{ \
219 if (size < 4) goto error; \
220 cur_size = GST_READ_UINT32_LE (data); \
223 if (cur_size > size) goto error; \
224 cur = (gchar*)data; \
233 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
234 g_return_val_if_fail (id_data != NULL, NULL);
235 g_return_val_if_fail (id_data_length > 0, NULL);
237 data = GST_BUFFER_DATA (buffer);
238 size = GST_BUFFER_SIZE (buffer);
239 list = gst_tag_list_new ();
243 if (memcmp (data, id_data, id_data_length) != 0)
245 ADVANCE (id_data_length);
247 *vendor_string = g_strndup (cur, cur_size);
249 iterations = cur_size;
254 cur = g_strndup (cur, cur_size);
255 value = strchr (cur, '=');
262 if (!g_utf8_validate (value, -1, NULL)) {
266 gst_vorbis_tag_add (list, cur, value);
273 gst_tag_list_free (list);
286 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
290 const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
295 for (i = 0; i < gst_tag_list_get_tag_size (list, tag); i++) {
296 GType tag_type = gst_tag_get_type (tag);
297 gchar *result = NULL;
303 if (!gst_tag_list_get_uint_index (list, tag, i, &u))
304 g_return_val_if_reached (NULL);
305 result = g_strdup_printf ("%s=%u", vorbis_tag, u);
311 if (!gst_tag_list_get_string_index (list, tag, i, &str))
312 g_return_val_if_reached (NULL);
313 result = g_strdup_printf ("%s=%s", vorbis_tag, str);
320 if (!gst_tag_list_get_double_index (list, tag, i, &value))
321 g_return_val_if_reached (NULL);
322 /* FIXME: what about locale-specific floating point separators? */
323 result = g_strdup_printf ("%s=%f", vorbis_tag, value);
327 if (tag_type == GST_TYPE_DATE) {
330 if (gst_tag_list_get_date_index (list, tag, i, &date)) {
331 /* vorbis suggests using ISO date formats */
333 g_strdup_printf ("%s=%04d-%02d-%02d", vorbis_tag,
334 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
335 (gint) g_date_get_day (date));
339 GST_DEBUG ("Couldn't write tag %s", tag);
345 l = g_list_prepend (l, result);
348 return g_list_reverse (l);
352 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
354 MyForEach *data = (MyForEach *) user_data;
358 comments = gst_tag_to_vorbis_comments (list, tag);
360 for (it = comments; it != NULL; it = it->next) {
361 gchar *result = it->data;
364 data->data_count += strlen (result);
365 data->entries = g_list_prepend (data->entries, result);
370 * gst_tag_list_to_vorbiscomment_buffer:
371 * @buffer: tag list to convert
372 * @id_data: identification data at start of stream
373 * @id_data_length: length of identification data
374 * @ vendor_string: string that describes the vendor string or NULL
376 * Creates a new vorbiscomment buffer from a tag list.
378 * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that
379 * could be converted from the given tag list.
382 gst_tag_list_to_vorbiscomment_buffer (const GstTagList * list,
383 const guint8 * id_data, const guint id_data_length,
384 const gchar * vendor_string)
390 MyForEach my_data = { 0, 0, NULL };
394 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
395 g_return_val_if_fail (id_data != NULL, NULL);
396 g_return_val_if_fail (id_data_length > 0, NULL);
398 if (vendor_string == NULL)
399 vendor_string = "GStreamer encoded vorbiscomment";
400 vendor_len = strlen (vendor_string);
401 required_size = id_data_length + 4 + vendor_len + 4 + 1;
402 gst_tag_list_foreach ((GstTagList *) list, write_one_tag, &my_data);
403 required_size += 4 * my_data.count + my_data.data_count;
404 buffer = gst_buffer_new_and_alloc (required_size);
405 data = GST_BUFFER_DATA (buffer);
406 memcpy (data, id_data, id_data_length);
407 data += id_data_length;
408 *((guint32 *) data) = GUINT32_TO_LE (vendor_len);
410 memcpy (data, vendor_string, vendor_len);
412 l = my_data.entries = g_list_reverse (my_data.entries);
413 *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
415 for (i = 0; i < my_data.count; i++) {
419 g_assert (l != NULL);
423 *((guint32 *) data) = GUINT32_TO_LE (size);
425 memcpy (data, cur, size);
428 g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
429 g_list_free (my_data.entries);