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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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-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.
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 
78 static void gst_flac_tag_dispose (GObject * object);
79 
80 static GstFlowReturn gst_flac_tag_chain (GstPad * pad, GstObject * parent,
81     GstBuffer * buffer);
82 
83 static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
84     GstStateChange transition);
85 
86 static gboolean gst_flac_tag_sink_event (GstPad * pad, GstObject * parent,
87     GstEvent * event);
88 
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));
92 
93 
94 static void
gst_flac_tag_class_init(GstFlacTagClass * klass)95 gst_flac_tag_class_init (GstFlacTagClass * klass)
96 {
97   GstElementClass *gstelement_class;
98   GObjectClass *gobject_class;
99 
100   GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
101 
102   gstelement_class = (GstElementClass *) klass;
103   gobject_class = (GObjectClass *) klass;
104 
105   gobject_class->dispose = gst_flac_tag_dispose;
106   gstelement_class->change_state = gst_flac_tag_change_state;
107 
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>");
111 
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);
116 }
117 
118 static void
gst_flac_tag_dispose(GObject * object)119 gst_flac_tag_dispose (GObject * object)
120 {
121   GstFlacTag *tag = GST_FLAC_TAG (object);
122 
123   if (tag->adapter) {
124     g_object_unref (tag->adapter);
125     tag->adapter = NULL;
126   }
127   if (tag->vorbiscomment) {
128     gst_buffer_unref (tag->vorbiscomment);
129     tag->vorbiscomment = NULL;
130   }
131   if (tag->tags) {
132     gst_tag_list_unref (tag->tags);
133     tag->tags = NULL;
134   }
135 
136   G_OBJECT_CLASS (parent_class)->dispose (object);
137 }
138 
139 
140 static void
gst_flac_tag_init(GstFlacTag * tag)141 gst_flac_tag_init (GstFlacTag * tag)
142 {
143   /* create the sink and src pads */
144   tag->sinkpad =
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);
151 
152   tag->srcpad =
153       gst_pad_new_from_static_template (&flac_tag_src_template, "src");
154   gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);
155 
156   tag->adapter = gst_adapter_new ();
157 }
158 
159 static gboolean
gst_flac_tag_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)160 gst_flac_tag_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
161 {
162   GstFlacTag *tag;
163   gboolean ret;
164 
165   tag = GST_FLAC_TAG (parent);
166 
167   GST_DEBUG_OBJECT (pad, "Received %s event on sinkpad, %" GST_PTR_FORMAT,
168       GST_EVENT_TYPE_NAME (event), event);
169 
170   switch (GST_EVENT_TYPE (event)) {
171     case GST_EVENT_CAPS:
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
174        */
175       ret = gst_pad_push_event (tag->srcpad, event);
176       break;
177     default:
178       ret = gst_pad_event_default (pad, parent, event);
179       break;
180   }
181 
182   return ret;
183 }
184 
185 #define FLAC_MAGIC "fLaC"
186 #define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)
187 
188 static GstFlowReturn
gst_flac_tag_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)189 gst_flac_tag_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
190 {
191   GstFlacTag *tag;
192   GstFlowReturn ret;
193   GstMapInfo map;
194   gsize size;
195 
196   ret = GST_FLOW_OK;
197   tag = GST_FLAC_TAG (parent);
198 
199   gst_adapter_push (tag->adapter, buffer);
200 
201   GST_LOG_OBJECT (pad, "state: %d", tag->state);
202 
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;
206 
207     if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
208       goto cleanup;
209 
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) {
213 
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)
217         goto cleanup;
218 
219       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
220     } else {
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;
225     }
226   }
227 
228 
229   /* The fLaC magic string has been skipped, try to detect the beginning
230    * of a metadata block
231    */
232   if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
233     guint type;
234     gboolean is_last;
235     const guint8 *block_header;
236 
237     g_assert (tag->metadata_block_size == 0);
238     g_assert (tag->metadata_last_block == FALSE);
239 
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)
244      */
245     if (gst_adapter_available (tag->adapter) < 4)
246       goto cleanup;
247 
248     block_header = gst_adapter_map (tag->adapter, 4);
249 
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)
254         | block_header[3];
255     gst_adapter_unmap (tag->adapter);
256 
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;
260 
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);
265 
266     /* Metadata blocks of type 4 are vorbis comment blocks */
267     if (type == 0x04) {
268       tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
269     } else {
270       tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
271     }
272   }
273 
274 
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;
279 
280     if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
281       goto cleanup;
282 
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.
287      */
288     gst_buffer_map (metadata_buffer, &map, GST_MAP_READWRITE);
289     map.data[0] &= (~0x80);
290     gst_buffer_unmap (metadata_buffer, &map);
291 
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)
296         goto cleanup;
297     } else {
298       tag->vorbiscomment = metadata_buffer;
299     }
300     tag->metadata_block_size = 0;
301     tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
302   }
303 
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
306    * tags mode
307    */
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
311      */
312     if (tag->vorbiscomment != NULL) {
313       guint8 id_data[4];
314       /* We found some tags, try to parse them and notify the other elements
315        * that we encountered some tags
316        */
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,
320           id_data, 4, NULL);
321       if (tag->tags != NULL) {
322         gst_pad_push_event (tag->srcpad,
323             gst_event_new_tag (gst_tag_list_ref (tag->tags)));
324       }
325 
326       gst_buffer_unref (tag->vorbiscomment);
327       tag->vorbiscomment = NULL;
328     }
329 
330     /* Skip to next state */
331     if (tag->metadata_last_block == FALSE) {
332       tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
333     } else {
334       tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
335     }
336   }
337 
338 
339   /* Creates a vorbis comment block from the metadata which was set
340    * on the gstreamer element, and add it to the flac stream
341    */
342   if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
343     GstBuffer *buffer;
344     const GstTagList *user_tags;
345     GstTagList *merged_tags;
346 
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)));
352     } else {
353       merged_tags = gst_tag_list_copy (tag->tags);
354     }
355 
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.
360        */
361       GST_WARNING_OBJECT (tag, "No tags found");
362       buffer = gst_buffer_new_and_alloc (12);
363       if (buffer == NULL)
364         goto no_buffer;
365 
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);
371     } else {
372       guchar header[4];
373       guint8 fbit[1];
374 
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);
382       if (buffer == NULL)
383         goto no_comment;
384 
385       size = gst_buffer_get_size (buffer);
386       if ((size < 4) || ((size - 4) > 0xFFFFFF))
387         goto comment_too_long;
388 
389       fbit[0] = 1;
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
392        * bit in gstflacdec
393        */
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);
397       }
398     }
399 
400     /* The 4 byte metadata block header isn't accounted for in the total
401      * size of the metadata block
402      */
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);
408 
409     GST_DEBUG_OBJECT (tag, "pushing %" G_GSIZE_FORMAT " byte vorbiscomment "
410         "buffer", map.size);
411 
412     ret = gst_pad_push (tag->srcpad, buffer);
413     if (ret != GST_FLOW_OK) {
414       goto cleanup;
415     }
416     tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
417   }
418 
419   /* The metadata blocks have been read, now we are reading audio data */
420   if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
421     GstBuffer *buffer;
422     guint avail;
423 
424     avail = gst_adapter_available (tag->adapter);
425     if (avail > 0) {
426       buffer = gst_adapter_take_buffer (tag->adapter, avail);
427       ret = gst_pad_push (tag->srcpad, buffer);
428     }
429   }
430 
431 cleanup:
432   GST_LOG_OBJECT (pad, "state: %d, ret: %d", tag->state, ret);
433   return ret;
434 
435   /* ERRORS */
436 no_buffer:
437   {
438     GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
439         ("Error creating 12-byte buffer for padding block"));
440     ret = GST_FLOW_ERROR;
441     goto cleanup;
442   }
443 no_comment:
444   {
445     GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
446         ("Error converting tag list to vorbiscomment buffer"));
447     ret = GST_FLOW_ERROR;
448     goto cleanup;
449   }
450 comment_too_long:
451   {
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
455      */
456     GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
457         ("Vorbis comment of size %" G_GSIZE_FORMAT " too long", size));
458     ret = GST_FLOW_ERROR;
459     goto cleanup;
460   }
461 }
462 
463 static GstStateChangeReturn
gst_flac_tag_change_state(GstElement * element,GstStateChange transition)464 gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
465 {
466   GstFlacTag *tag;
467 
468   tag = GST_FLAC_TAG (element);
469 
470   switch (transition) {
471     case GST_STATE_CHANGE_NULL_TO_READY:
472       break;
473     case GST_STATE_CHANGE_READY_TO_PAUSED:
474       break;
475     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
476       /* do something to get out of the chain function faster */
477       break;
478     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
479       break;
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;
485       }
486       if (tag->tags) {
487         gst_tag_list_unref (tag->tags);
488         tag->tags = NULL;
489       }
490       tag->metadata_block_size = 0;
491       tag->metadata_last_block = FALSE;
492       tag->state = GST_FLAC_TAG_STATE_INIT;
493       break;
494     case GST_STATE_CHANGE_READY_TO_NULL:
495       break;
496     default:
497       break;
498   }
499 
500   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
501 }
502