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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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-1.0 -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")
78 static void gst_flac_tag_dispose (GObject * object);
80 static GstFlowReturn gst_flac_tag_chain (GstPad * pad, GstObject * parent,
83 static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
84 GstStateChange transition);
86 static gboolean gst_flac_tag_sink_event (GstPad * pad, GstObject * parent,
89 #define gst_flac_tag_parent_class parent_class
90 G_DEFINE_TYPE_WITH_CODE (GstFlacTag, gst_flac_tag, GST_TYPE_ELEMENT,
91 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
95 gst_flac_tag_class_init (GstFlacTagClass * klass)
97 GstElementClass *gstelement_class;
98 GObjectClass *gobject_class;
100 GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
102 gstelement_class = (GstElementClass *) klass;
103 gobject_class = (GObjectClass *) klass;
105 gobject_class->dispose = gst_flac_tag_dispose;
106 gstelement_class->change_state = gst_flac_tag_change_state;
108 gst_element_class_set_static_metadata (gstelement_class, "FLAC tagger",
109 "Formatter/Metadata",
110 "Rewrite tags in a FLAC file", "Christophe Fergeau <teuf@gnome.org>");
112 gst_element_class_add_static_pad_template (gstelement_class,
113 &flac_tag_sink_template);
114 gst_element_class_add_static_pad_template (gstelement_class,
115 &flac_tag_src_template);
119 gst_flac_tag_dispose (GObject * object)
121 GstFlacTag *tag = GST_FLAC_TAG (object);
124 g_object_unref (tag->adapter);
127 if (tag->vorbiscomment) {
128 gst_buffer_unref (tag->vorbiscomment);
129 tag->vorbiscomment = NULL;
132 gst_tag_list_unref (tag->tags);
136 G_OBJECT_CLASS (parent_class)->dispose (object);
141 gst_flac_tag_init (GstFlacTag * tag)
143 /* create the sink and src pads */
145 gst_pad_new_from_static_template (&flac_tag_sink_template, "sink");
146 gst_pad_set_chain_function (tag->sinkpad,
147 GST_DEBUG_FUNCPTR (gst_flac_tag_chain));
148 gst_pad_set_event_function (tag->sinkpad,
149 GST_DEBUG_FUNCPTR (gst_flac_tag_sink_event));
150 gst_element_add_pad (GST_ELEMENT (tag), tag->sinkpad);
153 gst_pad_new_from_static_template (&flac_tag_src_template, "src");
154 gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
156 tag->adapter = gst_adapter_new ();
160 gst_flac_tag_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
165 tag = GST_FLAC_TAG (parent);
167 GST_DEBUG_OBJECT (pad, "Received %s event on sinkpad, %" GST_PTR_FORMAT,
168 GST_EVENT_TYPE_NAME (event), event);
170 switch (GST_EVENT_TYPE (event)) {
172 /* FIXME: parse and store the caps. Once we parsed and built the headers,
173 * update the "streamheader" field in the caps and send a new caps event
175 ret = gst_pad_push_event (tag->srcpad, event);
178 ret = gst_pad_event_default (pad, parent, event);
185 #define FLAC_MAGIC "fLaC"
186 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
189 gst_flac_tag_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
197 tag = GST_FLAC_TAG (parent);
199 gst_adapter_push (tag->adapter, buffer);
201 GST_LOG_OBJECT (pad, "state: %d", tag->state);
203 /* Initial state, we don't even know if we are dealing with a flac file */
204 if (tag->state == GST_FLAC_TAG_STATE_INIT) {
205 GstBuffer *id_buffer;
207 if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
210 id_buffer = gst_adapter_take_buffer (tag->adapter, FLAC_MAGIC_SIZE);
211 GST_DEBUG_OBJECT (tag, "looking for " FLAC_MAGIC " identifier");
212 if (gst_buffer_memcmp (id_buffer, 0, FLAC_MAGIC, FLAC_MAGIC_SIZE) == 0) {
214 GST_DEBUG_OBJECT (tag, "pushing " FLAC_MAGIC " identifier buffer");
215 ret = gst_pad_push (tag->srcpad, id_buffer);
216 if (ret != GST_FLOW_OK)
219 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
221 /* FIXME: does that work well with FLAC files containing ID3v2 tags ? */
222 gst_buffer_unref (id_buffer);
223 GST_ELEMENT_ERROR (tag, STREAM, WRONG_TYPE, (NULL), (NULL));
224 ret = GST_FLOW_ERROR;
229 /* The fLaC magic string has been skipped, try to detect the beginning
230 * of a metadata block
232 if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
235 const guint8 *block_header;
237 g_assert (tag->metadata_block_size == 0);
238 g_assert (tag->metadata_last_block == FALSE);
240 /* The header of a flac metadata block is 4 bytes long:
241 * 1st bit: indicates whether this is the last metadata info block
242 * 7 next bits: 4 if vorbis comment block
243 * 24 next bits: size of the metadata to follow (big endian)
245 if (gst_adapter_available (tag->adapter) < 4)
248 block_header = gst_adapter_map (tag->adapter, 4);
250 is_last = ((block_header[0] & 0x80) == 0x80);
251 type = block_header[0] & 0x7F;
252 size = (block_header[1] << 16)
253 | (block_header[2] << 8)
255 gst_adapter_unmap (tag->adapter);
257 /* The 4 bytes long header isn't included in the metadata size */
258 tag->metadata_block_size = size + 4;
259 tag->metadata_last_block = is_last;
261 GST_DEBUG_OBJECT (tag,
262 "got metadata block: %" G_GSIZE_FORMAT " bytes, type %d, "
263 "is vorbiscomment: %d, is last: %d",
264 size, type, (type == 0x04), is_last);
266 /* Metadata blocks of type 4 are vorbis comment blocks */
268 tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
270 tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
275 /* Reads a metadata block */
276 if ((tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) ||
277 (tag->state == GST_FLAC_TAG_STATE_VC_METADATA_BLOCK)) {
278 GstBuffer *metadata_buffer;
280 if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
283 metadata_buffer = gst_adapter_take_buffer (tag->adapter,
284 tag->metadata_block_size);
285 /* clear the is-last flag, as the last metadata block will
286 * be the vorbis comment block which we will build ourselves.
288 gst_buffer_map (metadata_buffer, &map, GST_MAP_READWRITE);
289 map.data[0] &= (~0x80);
290 gst_buffer_unmap (metadata_buffer, &map);
292 if (tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) {
293 GST_DEBUG_OBJECT (tag, "pushing metadata block buffer");
294 ret = gst_pad_push (tag->srcpad, metadata_buffer);
295 if (ret != GST_FLOW_OK)
298 tag->vorbiscomment = metadata_buffer;
300 tag->metadata_block_size = 0;
301 tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
304 /* This state is mainly used to be able to stop as soon as we read
305 * a vorbiscomment block from the flac file if we are in an only output
308 if (tag->state == GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK) {
309 /* Check if in the previous iteration we read a vorbis comment metadata
310 * block, and stop now if the user only wants to read tags
312 if (tag->vorbiscomment != NULL) {
314 /* We found some tags, try to parse them and notify the other elements
315 * that we encountered some tags
317 GST_DEBUG_OBJECT (tag, "emitting vorbiscomment tags");
318 gst_buffer_extract (tag->vorbiscomment, 0, id_data, 4);
319 tag->tags = gst_tag_list_from_vorbiscomment_buffer (tag->vorbiscomment,
321 if (tag->tags != NULL) {
322 gst_pad_push_event (tag->srcpad,
323 gst_event_new_tag (gst_tag_list_ref (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) {
344 const GstTagList *user_tags;
345 GstTagList *merged_tags;
347 /* merge the tag lists */
348 user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tag));
349 if (user_tags != NULL) {
350 merged_tags = gst_tag_list_merge (user_tags, tag->tags,
351 gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tag)));
353 merged_tags = gst_tag_list_copy (tag->tags);
356 if (merged_tags == NULL) {
357 /* If we get a NULL list of tags, we must generate a padding block
358 * which is marked as the last metadata block, otherwise we'll
359 * end up with a corrupted flac file.
361 GST_WARNING_OBJECT (tag, "No tags found");
362 buffer = gst_buffer_new_and_alloc (12);
366 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
367 memset (map.data, 0, map.size);
368 map.data[0] = 0x81; /* 0x80 = Last metadata block,
369 * 0x01 = padding block */
370 gst_buffer_unmap (buffer, &map);
375 memset (header, 0, sizeof (header));
376 header[0] = 0x84; /* 0x80 = Last metadata block,
377 * 0x04 = vorbiscomment block */
378 buffer = gst_tag_list_to_vorbiscomment_buffer (merged_tags, header,
379 sizeof (header), NULL);
380 GST_DEBUG_OBJECT (tag, "Writing tags %" GST_PTR_FORMAT, merged_tags);
381 gst_tag_list_unref (merged_tags);
385 size = gst_buffer_get_size (buffer);
386 if ((size < 4) || ((size - 4) > 0xFFFFFF))
387 goto comment_too_long;
390 /* Get rid of the framing bit at the end of the vorbiscomment buffer
391 * if it exists since libFLAC seems to lose sync because of this
394 if (gst_buffer_memcmp (buffer, size - 1, fbit, 1) == 0) {
395 buffer = gst_buffer_make_writable (buffer);
396 gst_buffer_resize (buffer, 0, size - 1);
400 /* The 4 byte metadata block header isn't accounted for in the total
401 * size of the metadata block
403 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
404 map.data[1] = (((map.size - 4) & 0xFF0000) >> 16);
405 map.data[2] = (((map.size - 4) & 0x00FF00) >> 8);
406 map.data[3] = ((map.size - 4) & 0x0000FF);
407 gst_buffer_unmap (buffer, &map);
409 GST_DEBUG_OBJECT (tag, "pushing %" G_GSIZE_FORMAT " byte vorbiscomment "
412 ret = gst_pad_push (tag->srcpad, buffer);
413 if (ret != GST_FLOW_OK) {
416 tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
419 /* The metadata blocks have been read, now we are reading audio data */
420 if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
424 avail = gst_adapter_available (tag->adapter);
426 buffer = gst_adapter_take_buffer (tag->adapter, avail);
427 ret = gst_pad_push (tag->srcpad, buffer);
432 GST_LOG_OBJECT (pad, "state: %d, ret: %d", tag->state, ret);
438 GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
439 ("Error creating 12-byte buffer for padding block"));
440 ret = GST_FLOW_ERROR;
445 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
446 ("Error converting tag list to vorbiscomment buffer"));
447 ret = GST_FLOW_ERROR;
452 /* FLAC vorbis comment blocks are limited to 2^24 bytes,
453 * while the vorbis specs allow more than that. Shouldn't
454 * be a real world problem though
456 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
457 ("Vorbis comment of size %" G_GSIZE_FORMAT " too long", size));
458 ret = GST_FLOW_ERROR;
463 static GstStateChangeReturn
464 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
468 tag = GST_FLAC_TAG (element);
470 switch (transition) {
471 case GST_STATE_CHANGE_NULL_TO_READY:
473 case GST_STATE_CHANGE_READY_TO_PAUSED:
475 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
476 /* do something to get out of the chain function faster */
478 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
480 case GST_STATE_CHANGE_PAUSED_TO_READY:
481 gst_adapter_clear (tag->adapter);
482 if (tag->vorbiscomment) {
483 gst_buffer_unref (tag->vorbiscomment);
484 tag->vorbiscomment = NULL;
487 gst_tag_list_unref (tag->tags);
490 tag->metadata_block_size = 0;
491 tag->metadata_last_block = FALSE;
492 tag->state = GST_FLAC_TAG_STATE_INIT;
494 case GST_STATE_CHANGE_READY_TO_NULL:
500 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);