3866b5a062b2e1a59536cae0c995d984c3cdd0c3
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gsttagsetter.h>
26 #include "gsttageditingprivate.h"
27 #include <stdlib.h>
28 #include <string.h>
29
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"},
60   {NULL, NULL}
61 };
62
63 /**
64  * gst_tag_from_vorbis_tag:
65  * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
66  *
67  * Looks up the GStreamer tag for a vorbiscommment tag.
68  *
69  * Returns: The corresponding GStreamer tag or NULL if none exists.
70  */
71 G_CONST_RETURN gchar *
72 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
73 {
74   int i = 0;
75   gchar *real_vorbis_tag;
76
77   g_return_val_if_fail (vorbis_tag != NULL, NULL);
78
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) {
82       break;
83     }
84     i++;
85   }
86   g_free (real_vorbis_tag);
87   return tag_matches[i].gstreamer_tag;
88 }
89
90 /**
91  * gst_tag_to_vorbis_tag:
92  * @gst_tag: GStreamer tag to convert to vorbiscomment tag
93  *
94  * Looks up the vorbiscommment tag for a GStreamer tag.
95  *
96  * Returns: The corresponding vorbiscommment tag or NULL if none exists.
97  */
98 G_CONST_RETURN gchar *
99 gst_tag_to_vorbis_tag (const gchar * gst_tag)
100 {
101   int i = 0;
102
103   g_return_val_if_fail (gst_tag != NULL, NULL);
104
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;
108     }
109     i++;
110   }
111   return NULL;
112 }
113
114
115 void
116 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
117 {
118   const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
119   GType tag_type;
120
121   if (gst_tag == NULL)
122     return;
123
124   tag_type = gst_tag_get_type (gst_tag);
125   switch (tag_type) {
126     case G_TYPE_UINT:{
127       guint tmp;
128       gchar *check;
129       gboolean is_track_number_tag;
130       gboolean is_disc_number_tag;
131
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)) {
136         guint count;
137
138         check++;
139         count = strtoul (check, &check, 10);
140         if (*check != '\0' || count == 0)
141           break;
142         if (is_track_number_tag) {
143           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
144               count, NULL);
145         } else {
146           gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
147               GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
148         }
149       }
150       if (*check == '\0') {
151         gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
152       }
153       break;
154     }
155     case G_TYPE_STRING:{
156       gchar *valid = NULL;
157
158       /* specialcase for language code */
159       if (strcmp (tag, "LANGUAGE") == 0) {
160         const gchar *s = strchr (value, '[');
161
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);
168         }
169       }
170
171       if (!valid) {
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);
175         } else {
176           valid = g_strdup (value);
177         }
178       }
179       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
180       g_free (valid);
181       break;
182     }
183     case G_TYPE_DOUBLE:{
184       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
185               NULL), NULL);
186       break;
187     }
188     default:{
189       if (tag_type == GST_TYPE_DATE) {
190         guint y, d = 1, m = 1;
191         gchar *check = (gchar *) value;
192
193         y = strtoul (check, &check, 10);
194         if (*check == '-') {
195           check++;
196           m = strtoul (check, &check, 10);
197           if (*check == '-') {
198             check++;
199             d = strtoul (check, &check, 10);
200           }
201         }
202         if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
203           GDate *date;
204
205           date = g_date_new_dmy (d, m, y);
206           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
207           g_date_free (date);
208         } else {
209           GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
210         }
211       } else {
212         GST_WARNING ("Unhandled tag of type '%s' (%d)",
213             g_type_name (tag_type), (gint) tag_type);
214       }
215       break;
216     }
217   }
218 }
219
220 /**
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.
227  *
228  * Creates a new tag list that contains the information parsed out of a 
229  * vorbiscomment packet.
230  *
231  * Returns: A new #GstTagList with all tags that could be extracted from the 
232  *          given vorbiscomment buffer or NULL on error.
233  */
234 GstTagList *
235 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
236     const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
237 {
238 #define ADVANCE(x) G_STMT_START{                                                \
239   data += x;                                                                    \
240   size -= x;                                                                    \
241   if (size < 4) goto error;                                                     \
242   cur_size = GST_READ_UINT32_LE (data);                                         \
243   data += 4;                                                                    \
244   size -= 4;                                                                    \
245   if (cur_size > size) goto error;                                              \
246   cur = (gchar*)data;                                                                   \
247 }G_STMT_END
248   gchar *cur, *value;
249   guint cur_size;
250   guint iterations;
251   guint8 *data;
252   guint size;
253   GstTagList *list;
254
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);
258
259   data = GST_BUFFER_DATA (buffer);
260   size = GST_BUFFER_SIZE (buffer);
261   list = gst_tag_list_new ();
262
263   if (size < 11)
264     goto error;
265   if (memcmp (data, id_data, id_data_length) != 0)
266     goto error;
267   ADVANCE (id_data_length);
268   if (vendor_string)
269     *vendor_string = g_strndup (cur, cur_size);
270   ADVANCE (cur_size);
271   iterations = cur_size;
272   cur_size = 0;
273   while (iterations) {
274     ADVANCE (cur_size);
275     iterations--;
276     cur = g_strndup (cur, cur_size);
277     value = strchr (cur, '=');
278     if (value == NULL) {
279       g_free (cur);
280       continue;
281     }
282     *value = '\0';
283     value++;
284     if (!g_utf8_validate (value, -1, NULL)) {
285       g_free (cur);
286       continue;
287     }
288     gst_vorbis_tag_add (list, cur, value);
289     g_free (cur);
290   }
291
292   return list;
293
294 error:
295   gst_tag_list_free (list);
296   return NULL;
297 #undef ADVANCE
298 }
299 typedef struct
300 {
301   guint count;
302   guint data_count;
303   GList *entries;
304 }
305 MyForEach;
306
307 GList *
308 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
309 {
310   GList *l = NULL;
311   guint i;
312   const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
313
314   if (!vorbis_tag)
315     return NULL;
316
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;
320
321     switch (tag_type) {
322       case G_TYPE_UINT:{
323         guint u;
324
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);
328         break;
329       }
330       case G_TYPE_STRING:{
331         gchar *str;
332
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);
336         g_free (str);
337         break;
338       }
339       case G_TYPE_DOUBLE:{
340         gdouble value;
341
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);
346         break;
347       }
348       default:{
349         if (tag_type == GST_TYPE_DATE) {
350           GDate *date;
351
352           if (gst_tag_list_get_date_index (list, tag, i, &date)) {
353             /* vorbis suggests using ISO date formats */
354             result =
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));
358             g_date_free (date);
359           }
360         } else {
361           GST_DEBUG ("Couldn't write tag %s", tag);
362           continue;
363         }
364         break;
365       }
366     }
367     l = g_list_prepend (l, result);
368   }
369
370   return g_list_reverse (l);
371 }
372
373 static void
374 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
375 {
376   MyForEach *data = (MyForEach *) user_data;
377   GList *comments;
378   GList *it;
379
380   comments = gst_tag_to_vorbis_comments (list, tag);
381
382   for (it = comments; it != NULL; it = it->next) {
383     gchar *result = it->data;
384
385     data->count++;
386     data->data_count += strlen (result);
387     data->entries = g_list_prepend (data->entries, result);
388   }
389 }
390
391 /**
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
397  *
398  * Creates a new vorbiscomment buffer from a tag list.
399  *
400  * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that 
401  *          could be converted from the given tag list.
402  */
403 GstBuffer *
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)
407 {
408   GstBuffer *buffer;
409   guint8 *data;
410   guint i;
411   GList *l;
412   MyForEach my_data = { 0, 0, NULL };
413   guint vendor_len;
414   int required_size;
415
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);
419
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);
431   data += 4;
432   memcpy (data, vendor_string, vendor_len);
433   data += vendor_len;
434   l = my_data.entries = g_list_reverse (my_data.entries);
435   *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
436   data += 4;
437   for (i = 0; i < my_data.count; i++) {
438     guint size;
439     gchar *cur;
440
441     g_assert (l != NULL);
442     cur = l->data;
443     l = g_list_next (l);
444     size = strlen (cur);
445     *((guint32 *) data) = GUINT32_TO_LE (size);
446     data += 4;
447     memcpy (data, cur, size);
448     data += size;
449   }
450   g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
451   g_list_free (my_data.entries);
452   *data = 1;
453
454   return buffer;
455 }