Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / ext / flac / gstflactag.c
1 /* GStreamer
2  * Copyright (C) 2003 Christophe Fergeau <teuf@gnome.org>
3  * Copyright (C) 2008 Jonathan Matthew <jonathan@d14n.org>
4  * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
6  * gstflactag.c: plug-in for reading/modifying vorbis comments in flac files
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:element-flactag
26  * @see_also: #flacenc, #flacdec, #GstTagSetter
27  *
28  * The flactag element can change the tag contained within a raw
29  * FLAC stream. Specifically, it modifies the comments header packet
30  * of the FLAC stream.
31  *
32  * Applications can set the tags to write using the #GstTagSetter interface.
33  * Tags contained withing the FLAC bitstream will be picked up
34  * automatically (and merged according to the merge mode set via the tag
35  * setter interface).
36  *
37  * <refsect2>
38  * <title>Example pipelines</title>
39  * |[
40  * gst-launch -v filesrc location=foo.flac ! flactag ! filesink location=bar.flac
41  * ]| This element is not useful with gst-launch, because it does not support
42  * setting the tags on a #GstTagSetter interface. Conceptually, the element
43  * will usually be used in this order though.
44  * </refsect2>
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <gst/gst.h>
52 #include <gst/gsttagsetter.h>
53 #include <gst/base/gstadapter.h>
54 #include <gst/tag/tag.h>
55 #include <string.h>
56
57 #include "gstflactag.h"
58
59 GST_DEBUG_CATEGORY_STATIC (flactag_debug);
60 #define GST_CAT_DEFAULT flactag_debug
61
62 /* elementfactory information */
63 static GstStaticPadTemplate flac_tag_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("audio/x-flac")
68     );
69
70 static GstStaticPadTemplate flac_tag_sink_template =
71 GST_STATIC_PAD_TEMPLATE ("sink",
72     GST_PAD_SINK,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS ("audio/x-flac")
75     );
76
77 /* signals and args */
78 enum
79 {
80   /* FILL ME */
81   LAST_SIGNAL
82 };
83
84 enum
85 {
86   ARG_0
87       /* FILL ME */
88 };
89
90 static void gst_flac_tag_dispose (GObject * object);
91
92 static GstFlowReturn gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer);
93
94 static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
95     GstStateChange transition);
96
97 static gboolean gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps);
98
99 #define gst_flac_tag_parent_class parent_class
100 G_DEFINE_TYPE_WITH_CODE (GstFlacTag, gst_flac_tag, GST_TYPE_ELEMENT,
101     G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
102
103
104 static void
105 gst_flac_tag_class_init (GstFlacTagClass * klass)
106 {
107   GstElementClass *gstelement_class;
108   GObjectClass *gobject_class;
109
110   GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
111
112   gstelement_class = (GstElementClass *) klass;
113   gobject_class = (GObjectClass *) klass;
114
115   gobject_class->dispose = gst_flac_tag_dispose;
116   gstelement_class->change_state = gst_flac_tag_change_state;
117
118   gst_element_class_set_details_simple (gstelement_class, "FLAC tagger",
119       "Formatter/Metadata",
120       "Rewrite tags in a FLAC file", "Christophe Fergeau <teuf@gnome.org>");
121
122   gst_element_class_add_pad_template (gstelement_class,
123       gst_static_pad_template_get (&flac_tag_sink_template));
124
125   gst_element_class_add_pad_template (gstelement_class,
126       gst_static_pad_template_get (&flac_tag_src_template));
127 }
128
129 static void
130 gst_flac_tag_dispose (GObject * object)
131 {
132   GstFlacTag *tag = GST_FLAC_TAG (object);
133
134   if (tag->adapter) {
135     g_object_unref (tag->adapter);
136     tag->adapter = NULL;
137   }
138   if (tag->vorbiscomment) {
139     gst_buffer_unref (tag->vorbiscomment);
140     tag->vorbiscomment = NULL;
141   }
142   if (tag->tags) {
143     gst_tag_list_free (tag->tags);
144     tag->tags = NULL;
145   }
146
147   G_OBJECT_CLASS (parent_class)->dispose (object);
148 }
149
150
151 static void
152 gst_flac_tag_init (GstFlacTag * tag)
153 {
154   /* create the sink and src pads */
155   tag->sinkpad =
156       gst_pad_new_from_static_template (&flac_tag_sink_template, "sink");
157   gst_pad_set_chain_function (tag->sinkpad,
158       GST_DEBUG_FUNCPTR (gst_flac_tag_chain));
159   gst_pad_set_setcaps_function (tag->sinkpad,
160       GST_DEBUG_FUNCPTR (gst_flac_tag_sink_setcaps));
161   gst_element_add_pad (GST_ELEMENT (tag), tag->sinkpad);
162
163   tag->srcpad =
164       gst_pad_new_from_static_template (&flac_tag_src_template, "src");
165   gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
166
167   tag->adapter = gst_adapter_new ();
168 }
169
170 static gboolean
171 gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps)
172 {
173   GstFlacTag *tag = GST_FLAC_TAG (GST_PAD_PARENT (pad));
174
175   return gst_pad_set_caps (tag->srcpad, caps);
176 }
177
178 #define FLAC_MAGIC "fLaC"
179 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
180
181 static GstFlowReturn
182 gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer)
183 {
184   GstFlacTag *tag;
185   GstFlowReturn ret;
186   guint8 *data;
187   gsize size;
188
189   ret = GST_FLOW_OK;
190   tag = GST_FLAC_TAG (gst_pad_get_parent (pad));
191
192   gst_adapter_push (tag->adapter, buffer);
193
194   /* Initial state, we don't even know if we are dealing with a flac file */
195   if (tag->state == GST_FLAC_TAG_STATE_INIT) {
196     GstBuffer *id_buffer;
197
198     if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
199       goto cleanup;
200
201     id_buffer = gst_adapter_take_buffer (tag->adapter, FLAC_MAGIC_SIZE);
202     GST_DEBUG_OBJECT (tag, "looking for " FLAC_MAGIC " identifier");
203     if (gst_buffer_memcmp (id_buffer, 0, FLAC_MAGIC, FLAC_MAGIC_SIZE) == 0) {
204
205       GST_DEBUG_OBJECT (tag, "pushing " FLAC_MAGIC " identifier buffer");
206       gst_buffer_set_caps (id_buffer, GST_PAD_CAPS (tag->srcpad));
207       ret = gst_pad_push (tag->srcpad, id_buffer);
208       if (ret != GST_FLOW_OK)
209         goto cleanup;
210
211       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
212     } else {
213       /* FIXME: does that work well with FLAC files containing ID3v2 tags ? */
214       gst_buffer_unref (id_buffer);
215       GST_ELEMENT_ERROR (tag, STREAM, WRONG_TYPE, (NULL), (NULL));
216       ret = GST_FLOW_ERROR;
217     }
218   }
219
220
221   /* The fLaC magic string has been skipped, try to detect the beginning
222    * of a metadata block
223    */
224   if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
225     guint type;
226     gboolean is_last;
227     const guint8 *block_header;
228
229     g_assert (tag->metadata_block_size == 0);
230     g_assert (tag->metadata_last_block == FALSE);
231
232     /* The header of a flac metadata block is 4 bytes long:
233      * 1st bit: indicates whether this is the last metadata info block
234      * 7 next bits: 4 if vorbis comment block
235      * 24 next bits: size of the metadata to follow (big endian)
236      */
237     if (gst_adapter_available (tag->adapter) < 4)
238       goto cleanup;
239
240     block_header = gst_adapter_map (tag->adapter, 4);
241
242     is_last = ((block_header[0] & 0x80) == 0x80);
243     type = block_header[0] & 0x7F;
244     size = (block_header[1] << 16)
245         | (block_header[2] << 8)
246         | block_header[3];
247     gst_adapter_unmap (tag->adapter, 0);
248
249     /* The 4 bytes long header isn't included in the metadata size */
250     tag->metadata_block_size = size + 4;
251     tag->metadata_last_block = is_last;
252
253     GST_DEBUG_OBJECT (tag,
254         "got metadata block: %d bytes, type %d, is vorbiscomment: %d, is last: %d",
255         size, type, (type == 0x04), is_last);
256
257     /* Metadata blocks of type 4 are vorbis comment blocks */
258     if (type == 0x04) {
259       tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
260     } else {
261       tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
262     }
263   }
264
265
266   /* Reads a metadata block */
267   if ((tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) ||
268       (tag->state == GST_FLAC_TAG_STATE_VC_METADATA_BLOCK)) {
269     GstBuffer *metadata_buffer;
270
271     if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
272       goto cleanup;
273
274     metadata_buffer = gst_adapter_take_buffer (tag->adapter,
275         tag->metadata_block_size);
276     /* clear the is-last flag, as the last metadata block will
277      * be the vorbis comment block which we will build ourselves.
278      */
279     data = gst_buffer_map (metadata_buffer, &size, NULL, GST_MAP_READWRITE);
280     data[0] &= (~0x80);
281     gst_buffer_unmap (metadata_buffer, data, size);
282
283     if (tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) {
284       GST_DEBUG_OBJECT (tag, "pushing metadata block buffer");
285       gst_buffer_set_caps (metadata_buffer, GST_PAD_CAPS (tag->srcpad));
286       ret = gst_pad_push (tag->srcpad, metadata_buffer);
287       if (ret != GST_FLOW_OK)
288         goto cleanup;
289     } else {
290       tag->vorbiscomment = metadata_buffer;
291     }
292     tag->metadata_block_size = 0;
293     tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
294   }
295
296   /* This state is mainly used to be able to stop as soon as we read
297    * a vorbiscomment block from the flac file if we are in an only output
298    * tags mode
299    */
300   if (tag->state == GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK) {
301     /* Check if in the previous iteration we read a vorbis comment metadata 
302      * block, and stop now if the user only wants to read tags
303      */
304     if (tag->vorbiscomment != NULL) {
305       guint8 id_data[4];
306       /* We found some tags, try to parse them and notify the other elements
307        * that we encountered some tags
308        */
309       GST_DEBUG_OBJECT (tag, "emitting vorbiscomment tags");
310       gst_buffer_extract (tag->vorbiscomment, 0, id_data, 4);
311       tag->tags = gst_tag_list_from_vorbiscomment_buffer (tag->vorbiscomment,
312           id_data, 4, NULL);
313       if (tag->tags != NULL) {
314         gst_element_found_tags (GST_ELEMENT (tag),
315             gst_tag_list_copy (tag->tags));
316       }
317
318       gst_buffer_unref (tag->vorbiscomment);
319       tag->vorbiscomment = NULL;
320     }
321
322     /* Skip to next state */
323     if (tag->metadata_last_block == FALSE) {
324       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
325     } else {
326       tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
327     }
328   }
329
330
331   /* Creates a vorbis comment block from the metadata which was set
332    * on the gstreamer element, and add it to the flac stream
333    */
334   if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
335     GstBuffer *buffer;
336     const GstTagList *user_tags;
337     GstTagList *merged_tags;
338
339     /* merge the tag lists */
340     user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tag));
341     if (user_tags != NULL) {
342       merged_tags = gst_tag_list_merge (user_tags, tag->tags,
343           gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tag)));
344     } else {
345       merged_tags = gst_tag_list_copy (tag->tags);
346     }
347
348     if (merged_tags == NULL) {
349       /* If we get a NULL list of tags, we must generate a padding block
350        * which is marked as the last metadata block, otherwise we'll
351        * end up with a corrupted flac file.
352        */
353       GST_WARNING_OBJECT (tag, "No tags found");
354       buffer = gst_buffer_new_and_alloc (12);
355       if (buffer == NULL)
356         goto no_buffer;
357
358       data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
359       memset (data, 0, size);
360       data[0] = 0x81;           /* 0x80 = Last metadata block, 
361                                  * 0x01 = padding block */
362       gst_buffer_unmap (buffer, data, size);
363     } else {
364       guchar header[4];
365       guint8 fbit[1];
366
367       memset (header, 0, sizeof (header));
368       header[0] = 0x84;         /* 0x80 = Last metadata block, 
369                                  * 0x04 = vorbiscomment block */
370       buffer = gst_tag_list_to_vorbiscomment_buffer (merged_tags, header,
371           sizeof (header), NULL);
372       GST_DEBUG_OBJECT (tag, "Writing tags %" GST_PTR_FORMAT, merged_tags);
373       gst_tag_list_free (merged_tags);
374       if (buffer == NULL)
375         goto no_comment;
376
377       size = gst_buffer_get_size (buffer);
378       if ((size < 4) || ((size - 4) > 0xFFFFFF))
379         goto comment_too_long;
380
381       fbit[0] = 1;
382       /* Get rid of the framing bit at the end of the vorbiscomment buffer 
383        * if it exists since libFLAC seems to lose sync because of this
384        * bit in gstflacdec
385        */
386       if (gst_buffer_memcmp (buffer, size - 1, fbit, 1) == 0) {
387         buffer = gst_buffer_make_writable (buffer);
388         gst_buffer_resize (buffer, 0, size - 1);
389       }
390     }
391
392     /* The 4 byte metadata block header isn't accounted for in the total
393      * size of the metadata block
394      */
395     data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
396     data[1] = (((size - 4) & 0xFF0000) >> 16);
397     data[2] = (((size - 4) & 0x00FF00) >> 8);
398     data[3] = ((size - 4) & 0x0000FF);
399     gst_buffer_unmap (buffer, data, size);
400
401     GST_DEBUG_OBJECT (tag, "pushing %d byte vorbiscomment buffer", size);
402
403     gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
404     ret = gst_pad_push (tag->srcpad, buffer);
405     if (ret != GST_FLOW_OK) {
406       goto cleanup;
407     }
408     tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
409   }
410
411   /* The metadata blocks have been read, now we are reading audio data */
412   if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
413     GstBuffer *buffer;
414     guint avail;
415
416     avail = gst_adapter_available (tag->adapter);
417     if (avail > 0) {
418       buffer = gst_adapter_take_buffer (tag->adapter, avail);
419       gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
420       ret = gst_pad_push (tag->srcpad, buffer);
421     }
422   }
423
424 cleanup:
425   gst_object_unref (tag);
426
427   return ret;
428
429   /* ERRORS */
430 no_buffer:
431   {
432     GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
433         ("Error creating 12-byte buffer for padding block"));
434     ret = GST_FLOW_ERROR;
435     goto cleanup;
436   }
437 no_comment:
438   {
439     GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
440         ("Error converting tag list to vorbiscomment buffer"));
441     ret = GST_FLOW_ERROR;
442     goto cleanup;
443   }
444 comment_too_long:
445   {
446     /* FLAC vorbis comment blocks are limited to 2^24 bytes, 
447      * while the vorbis specs allow more than that. Shouldn't 
448      * be a real world problem though
449      */
450     GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
451         ("Vorbis comment of size %d too long", size));
452     ret = GST_FLOW_ERROR;
453     goto cleanup;
454   }
455 }
456
457 static GstStateChangeReturn
458 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
459 {
460   GstFlacTag *tag;
461
462   tag = GST_FLAC_TAG (element);
463
464   switch (transition) {
465     case GST_STATE_CHANGE_NULL_TO_READY:
466       break;
467     case GST_STATE_CHANGE_READY_TO_PAUSED:
468       break;
469     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
470       /* do something to get out of the chain function faster */
471       break;
472     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
473       break;
474     case GST_STATE_CHANGE_PAUSED_TO_READY:
475       gst_adapter_clear (tag->adapter);
476       if (tag->vorbiscomment) {
477         gst_buffer_unref (tag->vorbiscomment);
478         tag->vorbiscomment = NULL;
479       }
480       if (tag->tags) {
481         gst_tag_list_free (tag->tags);
482         tag->tags = NULL;
483       }
484       tag->metadata_block_size = 0;
485       tag->metadata_last_block = FALSE;
486       tag->state = GST_FLAC_TAG_STATE_INIT;
487       break;
488     case GST_STATE_CHANGE_READY_TO_NULL:
489       break;
490   }
491
492   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
493 }