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, GstObject * parent,
95 static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
96 GstStateChange transition);
98 static gboolean gst_flac_tag_sink_event (GstPad * pad, GstObject * parent,
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));
107 gst_flac_tag_class_init (GstFlacTagClass * klass)
109 GstElementClass *gstelement_class;
110 GObjectClass *gobject_class;
112 GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
114 gstelement_class = (GstElementClass *) klass;
115 gobject_class = (GObjectClass *) klass;
117 gobject_class->dispose = gst_flac_tag_dispose;
118 gstelement_class->change_state = gst_flac_tag_change_state;
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>");
124 gst_element_class_add_pad_template (gstelement_class,
125 gst_static_pad_template_get (&flac_tag_sink_template));
127 gst_element_class_add_pad_template (gstelement_class,
128 gst_static_pad_template_get (&flac_tag_src_template));
132 gst_flac_tag_dispose (GObject * object)
134 GstFlacTag *tag = GST_FLAC_TAG (object);
137 g_object_unref (tag->adapter);
140 if (tag->vorbiscomment) {
141 gst_buffer_unref (tag->vorbiscomment);
142 tag->vorbiscomment = NULL;
145 gst_tag_list_free (tag->tags);
149 G_OBJECT_CLASS (parent_class)->dispose (object);
154 gst_flac_tag_init (GstFlacTag * tag)
156 /* create the sink and src pads */
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);
166 gst_pad_new_from_static_template (&flac_tag_src_template, "src");
167 gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
169 tag->adapter = gst_adapter_new ();
173 gst_flac_tag_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
177 switch (GST_EVENT_TYPE (event)) {
179 ret = gst_pad_event_default (pad, parent, event);
186 #define FLAC_MAGIC "fLaC"
187 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
190 gst_flac_tag_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
198 tag = GST_FLAC_TAG (parent);
200 gst_adapter_push (tag->adapter, buffer);
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;
206 if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
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) {
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)
218 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
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;
228 /* The fLaC magic string has been skipped, try to detect the beginning
229 * of a metadata block
231 if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
234 const guint8 *block_header;
236 g_assert (tag->metadata_block_size == 0);
237 g_assert (tag->metadata_last_block == FALSE);
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)
244 if (gst_adapter_available (tag->adapter) < 4)
247 block_header = gst_adapter_map (tag->adapter, 4);
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)
254 gst_adapter_unmap (tag->adapter);
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;
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);
265 /* Metadata blocks of type 4 are vorbis comment blocks */
267 tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
269 tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
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;
279 if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
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.
287 gst_buffer_map (metadata_buffer, &map, GST_MAP_READWRITE);
288 map.data[0] &= (~0x80);
289 gst_buffer_unmap (metadata_buffer, &map);
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)
297 tag->vorbiscomment = metadata_buffer;
299 tag->metadata_block_size = 0;
300 tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
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
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
311 if (tag->vorbiscomment != NULL) {
313 /* We found some tags, try to parse them and notify the other elements
314 * that we encountered some tags
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,
320 if (tag->tags != NULL) {
321 gst_pad_push_event (tag->srcpad,
322 gst_event_new_tag (gst_tag_list_copy (tag->tags)));
325 gst_buffer_unref (tag->vorbiscomment);
326 tag->vorbiscomment = NULL;
329 /* Skip to next state */
330 if (tag->metadata_last_block == FALSE) {
331 tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
333 tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
338 /* Creates a vorbis comment block from the metadata which was set
339 * on the gstreamer element, and add it to the flac stream
341 if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
343 const GstTagList *user_tags;
344 GstTagList *merged_tags;
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)));
352 merged_tags = gst_tag_list_copy (tag->tags);
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.
360 GST_WARNING_OBJECT (tag, "No tags found");
361 buffer = gst_buffer_new_and_alloc (12);
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);
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);
384 size = gst_buffer_get_size (buffer);
385 if ((size < 4) || ((size - 4) > 0xFFFFFF))
386 goto comment_too_long;
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
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);
399 /* The 4 byte metadata block header isn't accounted for in the total
400 * size of the metadata block
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);
408 GST_DEBUG_OBJECT (tag, "pushing %" G_GSIZE_FORMAT " byte vorbiscomment "
411 ret = gst_pad_push (tag->srcpad, buffer);
412 if (ret != GST_FLOW_OK) {
415 tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
418 /* The metadata blocks have been read, now we are reading audio data */
419 if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
423 avail = gst_adapter_available (tag->adapter);
425 buffer = gst_adapter_take_buffer (tag->adapter, avail);
426 ret = gst_pad_push (tag->srcpad, buffer);
437 GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
438 ("Error creating 12-byte buffer for padding block"));
439 ret = GST_FLOW_ERROR;
444 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
445 ("Error converting tag list to vorbiscomment buffer"));
446 ret = GST_FLOW_ERROR;
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
455 GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
456 ("Vorbis comment of size %" G_GSIZE_FORMAT " too long", size));
457 ret = GST_FLOW_ERROR;
462 static GstStateChangeReturn
463 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
467 tag = GST_FLAC_TAG (element);
469 switch (transition) {
470 case GST_STATE_CHANGE_NULL_TO_READY:
472 case GST_STATE_CHANGE_READY_TO_PAUSED:
474 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
475 /* do something to get out of the chain function faster */
477 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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;
486 gst_tag_list_free (tag->tags);
489 tag->metadata_block_size = 0;
490 tag->metadata_last_block = FALSE;
491 tag->state = GST_FLAC_TAG_STATE_INIT;
493 case GST_STATE_CHANGE_READY_TO_NULL:
497 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);