Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 static void
100 gst_flac_tag_setup_interfaces (GType flac_tag_type)
101 {
102   static const GInterfaceInfo tag_setter_info = { NULL, NULL, NULL };
103
104   g_type_add_interface_static (flac_tag_type, GST_TYPE_TAG_SETTER,
105       &tag_setter_info);
106 }
107
108 GST_BOILERPLATE_FULL (GstFlacTag, gst_flac_tag, GstElement, GST_TYPE_ELEMENT,
109     gst_flac_tag_setup_interfaces);
110
111 static void
112 gst_flac_tag_base_init (gpointer g_class)
113 {
114   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
115
116   gst_element_class_set_details_simple (element_class, "FLAC tagger",
117       "Formatter/Metadata",
118       "Rewrite tags in a FLAC file", "Christophe Fergeau <teuf@gnome.org>");
119
120   gst_element_class_add_static_pad_template (element_class,
121       &flac_tag_sink_template);
122   gst_element_class_add_static_pad_template (element_class,
123       &flac_tag_src_template);
124
125   GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
126 }
127
128 static void
129 gst_flac_tag_class_init (GstFlacTagClass * klass)
130 {
131   GstElementClass *gstelement_class;
132   GObjectClass *gobject_class;
133
134   gstelement_class = (GstElementClass *) klass;
135   gobject_class = (GObjectClass *) klass;
136
137   parent_class = g_type_class_peek_parent (klass);
138
139   gobject_class->dispose = gst_flac_tag_dispose;
140   gstelement_class->change_state = gst_flac_tag_change_state;
141 }
142
143 static void
144 gst_flac_tag_dispose (GObject * object)
145 {
146   GstFlacTag *tag = GST_FLAC_TAG (object);
147
148   if (tag->adapter) {
149     g_object_unref (tag->adapter);
150     tag->adapter = NULL;
151   }
152   if (tag->vorbiscomment) {
153     gst_buffer_unref (tag->vorbiscomment);
154     tag->vorbiscomment = NULL;
155   }
156   if (tag->tags) {
157     gst_tag_list_free (tag->tags);
158     tag->tags = NULL;
159   }
160
161   G_OBJECT_CLASS (parent_class)->dispose (object);
162 }
163
164
165 static void
166 gst_flac_tag_init (GstFlacTag * tag, GstFlacTagClass * klass)
167 {
168   /* create the sink and src pads */
169   tag->sinkpad =
170       gst_pad_new_from_static_template (&flac_tag_sink_template, "sink");
171   gst_pad_set_chain_function (tag->sinkpad,
172       GST_DEBUG_FUNCPTR (gst_flac_tag_chain));
173   gst_pad_set_setcaps_function (tag->sinkpad,
174       GST_DEBUG_FUNCPTR (gst_flac_tag_sink_setcaps));
175   gst_element_add_pad (GST_ELEMENT (tag), tag->sinkpad);
176
177   tag->srcpad =
178       gst_pad_new_from_static_template (&flac_tag_src_template, "src");
179   gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
180
181   tag->adapter = gst_adapter_new ();
182 }
183
184 static gboolean
185 gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps)
186 {
187   GstFlacTag *tag = GST_FLAC_TAG (GST_PAD_PARENT (pad));
188
189   return gst_pad_set_caps (tag->srcpad, caps);
190 }
191
192 #define FLAC_MAGIC "fLaC"
193 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
194
195 static GstFlowReturn
196 gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer)
197 {
198   GstFlacTag *tag;
199   GstFlowReturn ret;
200
201   ret = GST_FLOW_OK;
202   tag = GST_FLAC_TAG (gst_pad_get_parent (pad));
203
204   gst_adapter_push (tag->adapter, buffer);
205
206   /* Initial state, we don't even know if we are dealing with a flac file */
207   if (tag->state == GST_FLAC_TAG_STATE_INIT) {
208     GstBuffer *id_buffer;
209
210     if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
211       goto cleanup;
212
213     id_buffer = gst_adapter_take_buffer (tag->adapter, FLAC_MAGIC_SIZE);
214     GST_DEBUG_OBJECT (tag, "looking for " FLAC_MAGIC " identifier");
215     if (memcmp (GST_BUFFER_DATA (id_buffer), FLAC_MAGIC, FLAC_MAGIC_SIZE) == 0) {
216
217       GST_DEBUG_OBJECT (tag, "pushing " FLAC_MAGIC " identifier buffer");
218       gst_buffer_set_caps (id_buffer, GST_PAD_CAPS (tag->srcpad));
219       ret = gst_pad_push (tag->srcpad, id_buffer);
220       if (ret != GST_FLOW_OK)
221         goto cleanup;
222
223       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
224     } else {
225       /* FIXME: does that work well with FLAC files containing ID3v2 tags ? */
226       gst_buffer_unref (id_buffer);
227       GST_ELEMENT_ERROR (tag, STREAM, WRONG_TYPE, (NULL), (NULL));
228       ret = GST_FLOW_ERROR;
229     }
230   }
231
232
233   /* The fLaC magic string has been skipped, try to detect the beginning
234    * of a metadata block
235    */
236   if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
237     guint size;
238     guint type;
239     gboolean is_last;
240     const guint8 *block_header;
241
242     g_assert (tag->metadata_block_size == 0);
243     g_assert (tag->metadata_last_block == FALSE);
244
245     /* The header of a flac metadata block is 4 bytes long:
246      * 1st bit: indicates whether this is the last metadata info block
247      * 7 next bits: 4 if vorbis comment block
248      * 24 next bits: size of the metadata to follow (big endian)
249      */
250     if (gst_adapter_available (tag->adapter) < 4)
251       goto cleanup;
252
253     block_header = gst_adapter_peek (tag->adapter, 4);
254
255     is_last = ((block_header[0] & 0x80) == 0x80);
256     type = block_header[0] & 0x7F;
257     size = (block_header[1] << 16)
258         | (block_header[2] << 8)
259         | block_header[3];
260
261     /* The 4 bytes long header isn't included in the metadata size */
262     tag->metadata_block_size = size + 4;
263     tag->metadata_last_block = is_last;
264
265     GST_DEBUG_OBJECT (tag,
266         "got metadata block: %d bytes, type %d, is vorbiscomment: %d, is last: %d",
267         size, type, (type == 0x04), is_last);
268
269     /* Metadata blocks of type 4 are vorbis comment blocks */
270     if (type == 0x04) {
271       tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
272     } else {
273       tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
274     }
275   }
276
277
278   /* Reads a metadata block */
279   if ((tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) ||
280       (tag->state == GST_FLAC_TAG_STATE_VC_METADATA_BLOCK)) {
281     GstBuffer *metadata_buffer;
282
283     if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
284       goto cleanup;
285
286     metadata_buffer = gst_adapter_take_buffer (tag->adapter,
287         tag->metadata_block_size);
288     /* clear the is-last flag, as the last metadata block will
289      * be the vorbis comment block which we will build ourselves.
290      */
291     GST_BUFFER_DATA (metadata_buffer)[0] &= (~0x80);
292
293     if (tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) {
294       GST_DEBUG_OBJECT (tag, "pushing metadata block buffer");
295       gst_buffer_set_caps (metadata_buffer, GST_PAD_CAPS (tag->srcpad));
296       ret = gst_pad_push (tag->srcpad, metadata_buffer);
297       if (ret != GST_FLOW_OK)
298         goto cleanup;
299     } else {
300       tag->vorbiscomment = metadata_buffer;
301     }
302     tag->metadata_block_size = 0;
303     tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
304   }
305
306   /* This state is mainly used to be able to stop as soon as we read
307    * a vorbiscomment block from the flac file if we are in an only output
308    * tags mode
309    */
310   if (tag->state == GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK) {
311     /* Check if in the previous iteration we read a vorbis comment metadata 
312      * block, and stop now if the user only wants to read tags
313      */
314     if (tag->vorbiscomment != NULL) {
315       /* We found some tags, try to parse them and notify the other elements
316        * that we encountered some tags
317        */
318       GST_DEBUG_OBJECT (tag, "emitting vorbiscomment tags");
319       tag->tags = gst_tag_list_from_vorbiscomment_buffer (tag->vorbiscomment,
320           GST_BUFFER_DATA (tag->vorbiscomment), 4, NULL);
321       if (tag->tags != NULL) {
322         gst_element_found_tags (GST_ELEMENT (tag),
323             gst_tag_list_copy (tag->tags));
324       }
325
326       gst_buffer_unref (tag->vorbiscomment);
327       tag->vorbiscomment = NULL;
328     }
329
330     /* Skip to next state */
331     if (tag->metadata_last_block == FALSE) {
332       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
333     } else {
334       tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
335     }
336   }
337
338
339   /* Creates a vorbis comment block from the metadata which was set
340    * on the gstreamer element, and add it to the flac stream
341    */
342   if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
343     GstBuffer *buffer;
344     gint size;
345     const GstTagList *user_tags;
346     GstTagList *merged_tags;
347
348     /* merge the tag lists */
349     user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tag));
350     if (user_tags != NULL) {
351       merged_tags = gst_tag_list_merge (user_tags, tag->tags,
352           gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tag)));
353     } else {
354       merged_tags = gst_tag_list_copy (tag->tags);
355     }
356
357     if (merged_tags == NULL) {
358       /* If we get a NULL list of tags, we must generate a padding block
359        * which is marked as the last metadata block, otherwise we'll
360        * end up with a corrupted flac file.
361        */
362       GST_WARNING_OBJECT (tag, "No tags found");
363       buffer = gst_buffer_new_and_alloc (12);
364       if (buffer == NULL) {
365         GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
366             ("Error creating 12-byte buffer for padding block"));
367         ret = GST_FLOW_ERROR;
368         goto cleanup;
369       }
370       memset (GST_BUFFER_DATA (buffer), 0, GST_BUFFER_SIZE (buffer));
371       GST_BUFFER_DATA (buffer)[0] = 0x81;       /* 0x80 = Last metadata block, 
372                                                  * 0x01 = padding block
373                                                  */
374     } else {
375       guchar header[4];
376
377       memset (header, 0, sizeof (header));
378       header[0] = 0x84;         /* 0x80 = Last metadata block, 
379                                  * 0x04 = vorbiscomment block
380                                  */
381       buffer = gst_tag_list_to_vorbiscomment_buffer (merged_tags, header,
382           sizeof (header), NULL);
383       GST_DEBUG_OBJECT (tag, "Writing tags %" GST_PTR_FORMAT, merged_tags);
384       gst_tag_list_free (merged_tags);
385       if (buffer == NULL) {
386         GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
387             ("Error converting tag list to vorbiscomment buffer"));
388         ret = GST_FLOW_ERROR;
389         goto cleanup;
390       }
391       size = GST_BUFFER_SIZE (buffer) - 4;
392       if ((size > 0xFFFFFF) || (size < 0)) {
393         /* FLAC vorbis comment blocks are limited to 2^24 bytes, 
394          * while the vorbis specs allow more than that. Shouldn't 
395          * be a real world problem though
396          */
397         GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
398             ("Vorbis comment of size %d too long", size));
399         ret = GST_FLOW_ERROR;
400         goto cleanup;
401       }
402
403       /* Get rid of the framing bit at the end of the vorbiscomment buffer 
404        * if it exists since libFLAC seems to lose sync because of this
405        * bit in gstflacdec
406        */
407       if (GST_BUFFER_DATA (buffer)[GST_BUFFER_SIZE (buffer) - 1] == 1) {
408         GstBuffer *sub;
409
410         sub = gst_buffer_create_sub (buffer, 0, GST_BUFFER_SIZE (buffer) - 1);
411         gst_buffer_unref (buffer);
412         buffer = sub;
413       }
414     }
415
416     /* The 4 byte metadata block header isn't accounted for in the total
417      * size of the metadata block
418      */
419     size = GST_BUFFER_SIZE (buffer) - 4;
420
421     GST_BUFFER_DATA (buffer)[1] = ((size & 0xFF0000) >> 16);
422     GST_BUFFER_DATA (buffer)[2] = ((size & 0x00FF00) >> 8);
423     GST_BUFFER_DATA (buffer)[3] = (size & 0x0000FF);
424     GST_DEBUG_OBJECT (tag, "pushing %d byte vorbiscomment buffer",
425         GST_BUFFER_SIZE (buffer));
426     gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
427     ret = gst_pad_push (tag->srcpad, buffer);
428     if (ret != GST_FLOW_OK) {
429       goto cleanup;
430     }
431     tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
432   }
433
434   /* The metadata blocks have been read, now we are reading audio data */
435   if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
436     GstBuffer *buffer;
437     guint avail;
438
439     avail = gst_adapter_available (tag->adapter);
440     if (avail > 0) {
441       buffer = gst_adapter_take_buffer (tag->adapter, avail);
442       gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
443       ret = gst_pad_push (tag->srcpad, buffer);
444     }
445   }
446
447 cleanup:
448   gst_object_unref (tag);
449
450   return ret;
451 }
452
453
454 static GstStateChangeReturn
455 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
456 {
457   GstFlacTag *tag;
458
459   tag = GST_FLAC_TAG (element);
460
461   switch (transition) {
462     case GST_STATE_CHANGE_NULL_TO_READY:
463       break;
464     case GST_STATE_CHANGE_READY_TO_PAUSED:
465       break;
466     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
467       /* do something to get out of the chain function faster */
468       break;
469     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
470       break;
471     case GST_STATE_CHANGE_PAUSED_TO_READY:
472       gst_adapter_clear (tag->adapter);
473       if (tag->vorbiscomment) {
474         gst_buffer_unref (tag->vorbiscomment);
475         tag->vorbiscomment = NULL;
476       }
477       if (tag->tags) {
478         gst_tag_list_free (tag->tags);
479         tag->tags = NULL;
480       }
481       tag->metadata_block_size = 0;
482       tag->metadata_last_block = FALSE;
483       tag->state = GST_FLAC_TAG_STATE_INIT;
484       break;
485     case GST_STATE_CHANGE_READY_TO_NULL:
486       break;
487   }
488
489   return parent_class->change_state (element, transition);
490 }