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>
6 * gstflactag.c: plug-in for reading/modifying vorbis comments in flac files
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.
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.
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.
25 * SECTION:element-flactag
26 * @see_also: #flacenc, #flacdec, #GstTagSetter
28 * The flactag element can change the tag contained within a raw
29 * FLAC stream. Specifically, it modifies the comments header packet
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
38 * <title>Example pipelines</title>
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.
52 #include <gst/gsttagsetter.h>
53 #include <gst/base/gstadapter.h>
54 #include <gst/tag/tag.h>
57 #include "gstflactag.h"
59 GST_DEBUG_CATEGORY_STATIC (flactag_debug);
60 #define GST_CAT_DEFAULT flactag_debug
62 /* elementfactory information */
63 static GstStaticPadTemplate flac_tag_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
67 GST_STATIC_CAPS ("audio/x-flac")
70 static GstStaticPadTemplate flac_tag_sink_template =
71 GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS ("audio/x-flac")
77 /* signals and args */
90 static void gst_flac_tag_dispose (GObject * object);
92 static GstFlowReturn gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer);
94 static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
95 GstStateChange transition);
97 static gboolean gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps);
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));
105 gst_flac_tag_class_init (GstFlacTagClass * klass)
107 GstElementClass *gstelement_class;
108 GObjectClass *gobject_class;
110 GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
112 gstelement_class = (GstElementClass *) klass;
113 gobject_class = (GObjectClass *) klass;
115 gobject_class->dispose = gst_flac_tag_dispose;
116 gstelement_class->change_state = gst_flac_tag_change_state;
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>");
122 gst_element_class_add_pad_template (gstelement_class,
123 gst_static_pad_template_get (&flac_tag_sink_template));
125 gst_element_class_add_pad_template (gstelement_class,
126 gst_static_pad_template_get (&flac_tag_src_template));
130 gst_flac_tag_dispose (GObject * object)
132 GstFlacTag *tag = GST_FLAC_TAG (object);
135 g_object_unref (tag->adapter);
138 if (tag->vorbiscomment) {
139 gst_buffer_unref (tag->vorbiscomment);
140 tag->vorbiscomment = NULL;
143 gst_tag_list_free (tag->tags);
147 G_OBJECT_CLASS (parent_class)->dispose (object);
152 gst_flac_tag_init (GstFlacTag * tag)
154 /* create the sink and src pads */
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);
164 gst_pad_new_from_static_template (&flac_tag_src_template, "src");
165 gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
167 tag->adapter = gst_adapter_new ();
171 gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps)
173 GstFlacTag *tag = GST_FLAC_TAG (GST_PAD_PARENT (pad));
175 return gst_pad_set_caps (tag->srcpad, caps);
178 #define FLAC_MAGIC "fLaC"
179 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
182 gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer)
190 tag = GST_FLAC_TAG (gst_pad_get_parent (pad));
192 gst_adapter_push (tag->adapter, buffer);
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;
198 if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
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) {
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)
211 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
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;
221 /* The fLaC magic string has been skipped, try to detect the beginning
222 * of a metadata block
224 if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
227 const guint8 *block_header;
229 g_assert (tag->metadata_block_size == 0);
230 g_assert (tag->metadata_last_block == FALSE);
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)
237 if (gst_adapter_available (tag->adapter) < 4)
240 block_header = gst_adapter_map (tag->adapter, 4);
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)
247 gst_adapter_unmap (tag->adapter, 0);
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;
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);
257 /* Metadata blocks of type 4 are vorbis comment blocks */
259 tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
261 tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
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;
271 if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
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.
279 data = gst_buffer_map (metadata_buffer, &size, NULL, GST_MAP_READWRITE);
281 gst_buffer_unmap (metadata_buffer, data, size);
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)
290 tag->vorbiscomment = metadata_buffer;
292 tag->metadata_block_size = 0;
293 tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
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
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
304 if (tag->vorbiscomment != NULL) {
306 /* We found some tags, try to parse them and notify the other elements
307 * that we encountered some tags
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,
313 if (tag->tags != NULL) {
314 gst_element_found_tags (GST_ELEMENT (tag),
315 gst_tag_list_copy (tag->tags));
318 gst_buffer_unref (tag->vorbiscomment);
319 tag->vorbiscomment = NULL;
322 /* Skip to next state */
323 if (tag->metadata_last_block == FALSE) {
324 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
326 tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
331 /* Creates a vorbis comment block from the metadata which was set
332 * on the gstreamer element, and add it to the flac stream
334 if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
336 const GstTagList *user_tags;
337 GstTagList *merged_tags;
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)));
345 merged_tags = gst_tag_list_copy (tag->tags);
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.
353 GST_WARNING_OBJECT (tag, "No tags found");
354 buffer = gst_buffer_new_and_alloc (12);
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);
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);
377 size = gst_buffer_get_size (buffer);
378 if ((size < 4) || ((size - 4) > 0xFFFFFF))
379 goto comment_too_long;
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
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);
392 /* The 4 byte metadata block header isn't accounted for in the total
393 * size of the metadata block
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);
401 GST_DEBUG_OBJECT (tag, "pushing %d byte vorbiscomment buffer", size);
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) {
408 tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
411 /* The metadata blocks have been read, now we are reading audio data */
412 if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
416 avail = gst_adapter_available (tag->adapter);
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);
425 gst_object_unref (tag);
432 GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
433 ("Error creating 12-byte buffer for padding block"));
434 ret = GST_FLOW_ERROR;
439 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
440 ("Error converting tag list to vorbiscomment buffer"));
441 ret = GST_FLOW_ERROR;
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
450 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
451 ("Vorbis comment of size %d too long", size));
452 ret = GST_FLOW_ERROR;
457 static GstStateChangeReturn
458 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
462 tag = GST_FLAC_TAG (element);
464 switch (transition) {
465 case GST_STATE_CHANGE_NULL_TO_READY:
467 case GST_STATE_CHANGE_READY_TO_PAUSED:
469 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
470 /* do something to get out of the chain function faster */
472 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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;
481 gst_tag_list_free (tag->tags);
484 tag->metadata_block_size = 0;
485 tag->metadata_last_block = FALSE;
486 tag->state = GST_FLAC_TAG_STATE_INIT;
488 case GST_STATE_CHANGE_READY_TO_NULL:
492 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);