Split libgsttag docs into multiple sections.
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / gstvorbistag.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gstvorbistagsetter.c: plugin for reading / modifying vorbis 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gsttagvorbis
24  * @short_description: tag mappings and support functions for plugins
25  *                     dealing with vorbiscomments
26  * @see_also: #GstTagList
27  * 
28  * <refsect2>
29  * <para>
30  * Contains various utility functions for plugins to parse or create
31  * vorbiscomments and map them to and from #GstTagList<!-- -->s.
32  * </para>
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include <gst/gsttagsetter.h>
40 #include "gsttageditingprivate.h"
41 #include <stdlib.h>
42 #include <string.h>
43
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"},
74   {NULL, NULL}
75 };
76
77 /**
78  * gst_tag_from_vorbis_tag:
79  * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
80  *
81  * Looks up the GStreamer tag for a vorbiscommment tag.
82  *
83  * Returns: The corresponding GStreamer tag or NULL if none exists.
84  */
85 G_CONST_RETURN gchar *
86 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
87 {
88   int i = 0;
89   gchar *real_vorbis_tag;
90
91   g_return_val_if_fail (vorbis_tag != NULL, NULL);
92
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) {
96       break;
97     }
98     i++;
99   }
100   g_free (real_vorbis_tag);
101   return tag_matches[i].gstreamer_tag;
102 }
103
104 /**
105  * gst_tag_to_vorbis_tag:
106  * @gst_tag: GStreamer tag to convert to vorbiscomment tag
107  *
108  * Looks up the vorbiscommment tag for a GStreamer tag.
109  *
110  * Returns: The corresponding vorbiscommment tag or NULL if none exists.
111  */
112 G_CONST_RETURN gchar *
113 gst_tag_to_vorbis_tag (const gchar * gst_tag)
114 {
115   int i = 0;
116
117   g_return_val_if_fail (gst_tag != NULL, NULL);
118
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;
122     }
123     i++;
124   }
125   return NULL;
126 }
127
128
129 void
130 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
131 {
132   const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
133   GType tag_type;
134
135   if (gst_tag == NULL)
136     return;
137
138   tag_type = gst_tag_get_type (gst_tag);
139   switch (tag_type) {
140     case G_TYPE_UINT:{
141       guint tmp;
142       gchar *check;
143       gboolean is_track_number_tag;
144       gboolean is_disc_number_tag;
145
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)) {
150         guint count;
151
152         check++;
153         count = strtoul (check, &check, 10);
154         if (*check != '\0' || count == 0)
155           break;
156         if (is_track_number_tag) {
157           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
158               count, NULL);
159         } else {
160           gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
161               GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
162         }
163       }
164       if (*check == '\0') {
165         gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
166       }
167       break;
168     }
169     case G_TYPE_STRING:{
170       gchar *valid = NULL;
171
172       /* specialcase for language code */
173       if (strcmp (tag, "LANGUAGE") == 0) {
174         const gchar *s = strchr (value, '[');
175
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);
182         }
183       }
184
185       if (!valid) {
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);
189         } else {
190           valid = g_strdup (value);
191         }
192       }
193       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
194       g_free (valid);
195       break;
196     }
197     case G_TYPE_DOUBLE:{
198       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
199               NULL), NULL);
200       break;
201     }
202     default:{
203       if (tag_type == GST_TYPE_DATE) {
204         guint y, d = 1, m = 1;
205         gchar *check = (gchar *) value;
206
207         y = strtoul (check, &check, 10);
208         if (*check == '-') {
209           check++;
210           m = strtoul (check, &check, 10);
211           if (*check == '-') {
212             check++;
213             d = strtoul (check, &check, 10);
214           }
215         }
216         if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
217           GDate *date;
218
219           date = g_date_new_dmy (d, m, y);
220           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
221           g_date_free (date);
222         } else {
223           GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
224         }
225       } else {
226         GST_WARNING ("Unhandled tag of type '%s' (%d)",
227             g_type_name (tag_type), (gint) tag_type);
228       }
229       break;
230     }
231   }
232 }
233
234 /**
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.
241  *
242  * Creates a new tag list that contains the information parsed out of a 
243  * vorbiscomment packet.
244  *
245  * Returns: A new #GstTagList with all tags that could be extracted from the 
246  *          given vorbiscomment buffer or NULL on error.
247  */
248 GstTagList *
249 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
250     const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
251 {
252 #define ADVANCE(x) G_STMT_START{                                                \
253   data += x;                                                                    \
254   size -= x;                                                                    \
255   if (size < 4) goto error;                                                     \
256   cur_size = GST_READ_UINT32_LE (data);                                         \
257   data += 4;                                                                    \
258   size -= 4;                                                                    \
259   if (cur_size > size) goto error;                                              \
260   cur = (gchar*)data;                                                                   \
261 }G_STMT_END
262   gchar *cur, *value;
263   guint cur_size;
264   guint iterations;
265   guint8 *data;
266   guint size;
267   GstTagList *list;
268
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);
272
273   data = GST_BUFFER_DATA (buffer);
274   size = GST_BUFFER_SIZE (buffer);
275   list = gst_tag_list_new ();
276
277   if (size < 11)
278     goto error;
279   if (memcmp (data, id_data, id_data_length) != 0)
280     goto error;
281   ADVANCE (id_data_length);
282   if (vendor_string)
283     *vendor_string = g_strndup (cur, cur_size);
284   ADVANCE (cur_size);
285   iterations = cur_size;
286   cur_size = 0;
287   while (iterations) {
288     ADVANCE (cur_size);
289     iterations--;
290     cur = g_strndup (cur, cur_size);
291     value = strchr (cur, '=');
292     if (value == NULL) {
293       g_free (cur);
294       continue;
295     }
296     *value = '\0';
297     value++;
298     if (!g_utf8_validate (value, -1, NULL)) {
299       g_free (cur);
300       continue;
301     }
302     gst_vorbis_tag_add (list, cur, value);
303     g_free (cur);
304   }
305
306   return list;
307
308 error:
309   gst_tag_list_free (list);
310   return NULL;
311 #undef ADVANCE
312 }
313 typedef struct
314 {
315   guint count;
316   guint data_count;
317   GList *entries;
318 }
319 MyForEach;
320
321 GList *
322 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
323 {
324   GList *l = NULL;
325   guint i;
326   const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
327
328   if (!vorbis_tag)
329     return NULL;
330
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;
334
335     switch (tag_type) {
336       case G_TYPE_UINT:{
337         guint u;
338
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);
342         break;
343       }
344       case G_TYPE_STRING:{
345         gchar *str;
346
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);
350         g_free (str);
351         break;
352       }
353       case G_TYPE_DOUBLE:{
354         gdouble value;
355
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);
360         break;
361       }
362       default:{
363         if (tag_type == GST_TYPE_DATE) {
364           GDate *date;
365
366           if (gst_tag_list_get_date_index (list, tag, i, &date)) {
367             /* vorbis suggests using ISO date formats */
368             result =
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));
372             g_date_free (date);
373           }
374         } else {
375           GST_DEBUG ("Couldn't write tag %s", tag);
376           continue;
377         }
378         break;
379       }
380     }
381     l = g_list_prepend (l, result);
382   }
383
384   return g_list_reverse (l);
385 }
386
387 static void
388 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
389 {
390   MyForEach *data = (MyForEach *) user_data;
391   GList *comments;
392   GList *it;
393
394   comments = gst_tag_to_vorbis_comments (list, tag);
395
396   for (it = comments; it != NULL; it = it->next) {
397     gchar *result = it->data;
398
399     data->count++;
400     data->data_count += strlen (result);
401     data->entries = g_list_prepend (data->entries, result);
402   }
403 }
404
405 /**
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
411  *
412  * Creates a new vorbiscomment buffer from a tag list.
413  *
414  * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that 
415  *          could be converted from the given tag list.
416  */
417 GstBuffer *
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)
421 {
422   GstBuffer *buffer;
423   guint8 *data;
424   guint i;
425   GList *l;
426   MyForEach my_data = { 0, 0, NULL };
427   guint vendor_len;
428   int required_size;
429
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);
433
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);
445   data += 4;
446   memcpy (data, vendor_string, vendor_len);
447   data += vendor_len;
448   l = my_data.entries = g_list_reverse (my_data.entries);
449   *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
450   data += 4;
451   for (i = 0; i < my_data.count; i++) {
452     guint size;
453     gchar *cur;
454
455     g_assert (l != NULL);
456     cur = l->data;
457     l = g_list_next (l);
458     size = strlen (cur);
459     *((guint32 *) data) = GUINT32_TO_LE (size);
460     data += 4;
461     memcpy (data, cur, size);
462     data += size;
463   }
464   g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
465   g_list_free (my_data.entries);
466   *data = 1;
467
468   return buffer;
469 }