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