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);
100 gst_flac_tag_setup_interfaces (GType flac_tag_type)
102 static const GInterfaceInfo tag_setter_info = { NULL, NULL, NULL };
104 g_type_add_interface_static (flac_tag_type, GST_TYPE_TAG_SETTER,
108 GST_BOILERPLATE_FULL (GstFlacTag, gst_flac_tag, GstElement, GST_TYPE_ELEMENT,
109 gst_flac_tag_setup_interfaces);
112 gst_flac_tag_base_init (gpointer g_class)
114 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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>");
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);
125 GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
129 gst_flac_tag_class_init (GstFlacTagClass * klass)
131 GstElementClass *gstelement_class;
132 GObjectClass *gobject_class;
134 gstelement_class = (GstElementClass *) klass;
135 gobject_class = (GObjectClass *) klass;
137 parent_class = g_type_class_peek_parent (klass);
139 gobject_class->dispose = gst_flac_tag_dispose;
140 gstelement_class->change_state = gst_flac_tag_change_state;
144 gst_flac_tag_dispose (GObject * object)
146 GstFlacTag *tag = GST_FLAC_TAG (object);
149 g_object_unref (tag->adapter);
152 if (tag->vorbiscomment) {
153 gst_buffer_unref (tag->vorbiscomment);
154 tag->vorbiscomment = NULL;
157 gst_tag_list_free (tag->tags);
161 G_OBJECT_CLASS (parent_class)->dispose (object);
166 gst_flac_tag_init (GstFlacTag * tag, GstFlacTagClass * klass)
168 /* create the sink and src pads */
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);
178 gst_pad_new_from_static_template (&flac_tag_src_template, "src");
179 gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
181 tag->adapter = gst_adapter_new ();
185 gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps)
187 GstFlacTag *tag = GST_FLAC_TAG (GST_PAD_PARENT (pad));
189 return gst_pad_set_caps (tag->srcpad, caps);
192 #define FLAC_MAGIC "fLaC"
193 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
196 gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer)
202 tag = GST_FLAC_TAG (gst_pad_get_parent (pad));
204 gst_adapter_push (tag->adapter, buffer);
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;
210 if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
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) {
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)
223 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
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;
233 /* The fLaC magic string has been skipped, try to detect the beginning
234 * of a metadata block
236 if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
240 const guint8 *block_header;
242 g_assert (tag->metadata_block_size == 0);
243 g_assert (tag->metadata_last_block == FALSE);
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)
250 if (gst_adapter_available (tag->adapter) < 4)
253 block_header = gst_adapter_peek (tag->adapter, 4);
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)
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;
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);
269 /* Metadata blocks of type 4 are vorbis comment blocks */
271 tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
273 tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
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;
283 if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
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.
291 GST_BUFFER_DATA (metadata_buffer)[0] &= (~0x80);
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)
300 tag->vorbiscomment = metadata_buffer;
302 tag->metadata_block_size = 0;
303 tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
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
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
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
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));
326 gst_buffer_unref (tag->vorbiscomment);
327 tag->vorbiscomment = NULL;
330 /* Skip to next state */
331 if (tag->metadata_last_block == FALSE) {
332 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
334 tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
339 /* Creates a vorbis comment block from the metadata which was set
340 * on the gstreamer element, and add it to the flac stream
342 if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
345 const GstTagList *user_tags;
346 GstTagList *merged_tags;
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)));
354 merged_tags = gst_tag_list_copy (tag->tags);
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.
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;
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
377 memset (header, 0, sizeof (header));
378 header[0] = 0x84; /* 0x80 = Last metadata block,
379 * 0x04 = vorbiscomment block
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;
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
397 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
398 ("Vorbis comment of size %d too long", size));
399 ret = GST_FLOW_ERROR;
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
407 if (GST_BUFFER_DATA (buffer)[GST_BUFFER_SIZE (buffer) - 1] == 1) {
410 sub = gst_buffer_create_sub (buffer, 0, GST_BUFFER_SIZE (buffer) - 1);
411 gst_buffer_unref (buffer);
416 /* The 4 byte metadata block header isn't accounted for in the total
417 * size of the metadata block
419 size = GST_BUFFER_SIZE (buffer) - 4;
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) {
431 tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
434 /* The metadata blocks have been read, now we are reading audio data */
435 if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
439 avail = gst_adapter_available (tag->adapter);
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);
448 gst_object_unref (tag);
454 static GstStateChangeReturn
455 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
459 tag = GST_FLAC_TAG (element);
461 switch (transition) {
462 case GST_STATE_CHANGE_NULL_TO_READY:
464 case GST_STATE_CHANGE_READY_TO_PAUSED:
466 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
467 /* do something to get out of the chain function faster */
469 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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;
478 gst_tag_list_free (tag->tags);
481 tag->metadata_block_size = 0;
482 tag->metadata_last_block = FALSE;
483 tag->state = GST_FLAC_TAG_STATE_INIT;
485 case GST_STATE_CHANGE_READY_TO_NULL:
489 return parent_class->change_state (element, transition);