expand tabs
[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   {NULL, NULL}
54 };
55
56 /**
57  * gst_tag_from_vorbis_tag:
58  * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
59  *
60  * Looks up the GStreamer tag for a vorbiscommment tag.
61  *
62  * Returns: The corresponding GStreamer tag or NULL if none exists.
63  */
64 G_CONST_RETURN gchar *
65 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
66 {
67   int i = 0;
68   gchar *real_vorbis_tag;
69
70   g_return_val_if_fail (vorbis_tag != NULL, NULL);
71
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) {
75       break;
76     }
77     i++;
78   }
79   g_free (real_vorbis_tag);
80   return tag_matches[i].gstreamer_tag;
81 }
82
83 /**
84  * gst_tag_to_vorbis_tag:
85  * @gst_tag: GStreamer tag to convert to vorbiscomment tag
86  *
87  * Looks up the vorbiscommment tag for a GStreamer tag.
88  *
89  * Returns: The corresponding vorbiscommment tag or NULL if none exists.
90  */
91 G_CONST_RETURN gchar *
92 gst_tag_to_vorbis_tag (const gchar * gst_tag)
93 {
94   int i = 0;
95
96   g_return_val_if_fail (gst_tag != NULL, NULL);
97
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;
101     }
102     i++;
103   }
104   return NULL;
105 }
106
107
108 void
109 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
110 {
111   const gchar *gst_tag = gst_tag_from_vorbis_tag (tag);
112   GType tag_type;
113
114   if (gst_tag == NULL)
115     return;
116
117   tag_type = gst_tag_get_type (gst_tag);
118   switch (tag_type) {
119     case G_TYPE_UINT:{
120       guint tmp;
121       gchar *check;
122       gboolean is_track_number_tag;
123       gboolean is_disc_number_tag;
124
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)) {
129         guint count;
130
131         check++;
132         count = strtoul (check, &check, 10);
133         if (*check != '\0' || count == 0)
134           break;
135         if (is_track_number_tag) {
136           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
137               count, NULL);
138         } else {
139           gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
140               GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
141         }
142       }
143       if (*check == '\0') {
144         gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
145       }
146       break;
147     }
148     case G_TYPE_STRING:{
149       gchar *valid;
150
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);
154       } else {
155         valid = g_strdup (value);
156       }
157       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
158       g_free (valid);
159       break;
160     }
161     case G_TYPE_DOUBLE:{
162       gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, g_strtod (value,
163               NULL), NULL);
164       break;
165     }
166     default:{
167       if (tag_type == GST_TYPE_DATE) {
168         guint y, d = 1, m = 1;
169         gchar *check = (gchar *) value;
170
171         y = strtoul (check, &check, 10);
172         if (*check == '-') {
173           check++;
174           m = strtoul (check, &check, 10);
175           if (*check == '-') {
176             check++;
177             d = strtoul (check, &check, 10);
178           }
179         }
180         if (*check == '\0' && y != 0 && g_date_valid_dmy (d, m, y)) {
181           GDate *date;
182
183           date = g_date_new_dmy (d, m, y);
184           gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, date, NULL);
185           g_date_free (date);
186         } else {
187           GST_DEBUG ("skipping invalid date '%s' (%u,%u,%u)", value, y, m, d);
188         }
189       } else {
190         GST_WARNING ("Unhandled tag of type '%s' (%d)",
191             g_type_name (tag_type), (gint) tag_type);
192       }
193       break;
194     }
195   }
196 }
197
198 /**
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.
205  *
206  * Creates a new tag list that contains the information parsed out of a 
207  * vorbiscomment packet.
208  *
209  * Returns: A new #GstTagList with all tags that could be extracted from the 
210  *          given vorbiscomment buffer or NULL on error.
211  */
212 GstTagList *
213 gst_tag_list_from_vorbiscomment_buffer (const GstBuffer * buffer,
214     const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
215 {
216 #define ADVANCE(x) G_STMT_START{                                                \
217   data += x;                                                                    \
218   size -= x;                                                                    \
219   if (size < 4) goto error;                                                     \
220   cur_size = GST_READ_UINT32_LE (data);                                         \
221   data += 4;                                                                    \
222   size -= 4;                                                                    \
223   if (cur_size > size) goto error;                                              \
224   cur = (gchar*)data;                                                                   \
225 }G_STMT_END
226   gchar *cur, *value;
227   guint cur_size;
228   guint iterations;
229   guint8 *data;
230   guint size;
231   GstTagList *list;
232
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);
236
237   data = GST_BUFFER_DATA (buffer);
238   size = GST_BUFFER_SIZE (buffer);
239   list = gst_tag_list_new ();
240
241   if (size < 11)
242     goto error;
243   if (memcmp (data, id_data, id_data_length) != 0)
244     goto error;
245   ADVANCE (id_data_length);
246   if (vendor_string)
247     *vendor_string = g_strndup (cur, cur_size);
248   ADVANCE (cur_size);
249   iterations = cur_size;
250   cur_size = 0;
251   while (iterations) {
252     ADVANCE (cur_size);
253     iterations--;
254     cur = g_strndup (cur, cur_size);
255     value = strchr (cur, '=');
256     if (value == NULL) {
257       g_free (cur);
258       continue;
259     }
260     *value = '\0';
261     value++;
262     if (!g_utf8_validate (value, -1, NULL)) {
263       g_free (cur);
264       continue;
265     }
266     gst_vorbis_tag_add (list, cur, value);
267     g_free (cur);
268   }
269
270   return list;
271
272 error:
273   gst_tag_list_free (list);
274   return NULL;
275 #undef ADVANCE
276 }
277 typedef struct
278 {
279   guint count;
280   guint data_count;
281   GList *entries;
282 }
283 MyForEach;
284
285 GList *
286 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
287 {
288   GList *l = NULL;
289   guint i;
290   const gchar *vorbis_tag = gst_tag_to_vorbis_tag (tag);
291
292   if (!vorbis_tag)
293     return NULL;
294
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;
298
299     switch (tag_type) {
300       case G_TYPE_UINT:{
301         guint u;
302
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);
306         break;
307       }
308       case G_TYPE_STRING:{
309         gchar *str;
310
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);
314         g_free (str);
315         break;
316       }
317       case G_TYPE_DOUBLE:{
318         gdouble value;
319
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);
324         break;
325       }
326       default:{
327         if (tag_type == GST_TYPE_DATE) {
328           GDate *date;
329
330           if (gst_tag_list_get_date_index (list, tag, i, &date)) {
331             /* vorbis suggests using ISO date formats */
332             result =
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));
336             g_date_free (date);
337           }
338         } else {
339           GST_DEBUG ("Couldn't write tag %s", tag);
340           continue;
341         }
342         break;
343       }
344     }
345     l = g_list_prepend (l, result);
346   }
347
348   return g_list_reverse (l);
349 }
350
351 static void
352 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
353 {
354   MyForEach *data = (MyForEach *) user_data;
355   GList *comments;
356   GList *it;
357
358   comments = gst_tag_to_vorbis_comments (list, tag);
359
360   for (it = comments; it != NULL; it = it->next) {
361     gchar *result = it->data;
362
363     data->count++;
364     data->data_count += strlen (result);
365     data->entries = g_list_prepend (data->entries, result);
366   }
367 }
368
369 /**
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
375  *
376  * Creates a new vorbiscomment buffer from a tag list.
377  *
378  * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags that 
379  *          could be converted from the given tag list.
380  */
381 GstBuffer *
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)
385 {
386   GstBuffer *buffer;
387   guint8 *data;
388   guint i;
389   GList *l;
390   MyForEach my_data = { 0, 0, NULL };
391   guint vendor_len;
392   int required_size;
393
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);
397
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);
409   data += 4;
410   memcpy (data, vendor_string, vendor_len);
411   data += vendor_len;
412   l = my_data.entries = g_list_reverse (my_data.entries);
413   *((guint32 *) data) = GUINT32_TO_LE (my_data.count);
414   data += 4;
415   for (i = 0; i < my_data.count; i++) {
416     guint size;
417     gchar *cur;
418
419     g_assert (l != NULL);
420     cur = l->data;
421     l = g_list_next (l);
422     size = strlen (cur);
423     *((guint32 *) data) = GUINT32_TO_LE (size);
424     data += 4;
425     memcpy (data, cur, size);
426     data += size;
427   }
428   g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
429   g_list_free (my_data.entries);
430   *data = 1;
431
432   return buffer;
433 }