gstflactag.c 15.3 KB
Newer Older
1
2
/* GStreamer
 * Copyright (C) 2003 Christophe Fergeau <teuf@gnome.org>
3
4
 * Copyright (C) 2008 Jonathan Matthew <jonathan@d14n.org>
 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * gstflactag.c: plug-in for reading/modifying vorbis comments in flac files
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * SECTION:element-flactag
 * @see_also: #flacenc, #flacdec, #GstTagSetter
 *
 * The flactag element can change the tag contained within a raw
 * FLAC stream. Specifically, it modifies the comments header packet
 * of the FLAC stream.
 *
 * Applications can set the tags to write using the #GstTagSetter interface.
 * Tags contained withing the FLAC bitstream will be picked up
 * automatically (and merged according to the merge mode set via the tag
 * setter interface).
 *
 * <refsect2>
 * <title>Example pipelines</title>
 * |[
 * gst-launch -v filesrc location=foo.flac ! flactag ! filesink location=bar.flac
 * ]| This element is not useful with gst-launch, because it does not support
 * setting the tags on a #GstTagSetter interface. Conceptually, the element
 * will usually be used in this order though.
 * </refsect2>
 */

47
48
49
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
50
51

#include <gst/gst.h>
52
#include <gst/gsttagsetter.h>
53
#include <gst/base/gstadapter.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54
#include <gst/tag/tag.h>
55
56
#include <string.h>

57
#include "gstflactag.h"
58

59
60
GST_DEBUG_CATEGORY_STATIC (flactag_debug);
#define GST_CAT_DEFAULT flactag_debug
61
62

/* elementfactory information */
63
64
65
66
67
68
static GstStaticPadTemplate flac_tag_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-flac")
    );
69

70
71
72
73
74
75
static GstStaticPadTemplate flac_tag_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-flac")
    );
76
77

/* signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78
79
enum
{
80
81
82
83
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
84
85
enum
{
86
87
  ARG_0
      /* FILL ME */
88
89
};

90
static void gst_flac_tag_dispose (GObject * object);
91

92
static GstFlowReturn gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer);
93

94
95
static GstStateChangeReturn gst_flac_tag_change_state (GstElement * element,
    GstStateChange transition);
96

97
static gboolean gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps);
98

99
100
static void
gst_flac_tag_setup_interfaces (GType flac_tag_type)
101
{
102
  static const GInterfaceInfo tag_setter_info = { NULL, NULL, NULL };
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
103

104
105
  g_type_add_interface_static (flac_tag_type, GST_TYPE_TAG_SETTER,
      &tag_setter_info);
106
107
}

108
109
GST_BOILERPLATE_FULL (GstFlacTag, gst_flac_tag, GstElement, GST_TYPE_ELEMENT,
    gst_flac_tag_setup_interfaces);
110
111
112
113
114
115

static void
gst_flac_tag_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

116
117
118
  gst_element_class_set_details_simple (element_class, "FLAC tagger",
      "Formatter/Metadata",
      "Rewrite tags in a FLAC file", "Christophe Fergeau <teuf@gnome.org>");
119
120

  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121
      gst_static_pad_template_get (&flac_tag_sink_template));
122
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
123
      gst_static_pad_template_get (&flac_tag_src_template));
124

125
  GST_DEBUG_CATEGORY_INIT (flactag_debug, "flactag", 0, "flac tag rewriter");
126
127
}

128
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
129
gst_flac_tag_class_init (GstFlacTagClass * klass)
130
131
132
133
{
  GstElementClass *gstelement_class;
  GObjectClass *gobject_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134
135
136
  gstelement_class = (GstElementClass *) klass;
  gobject_class = (GObjectClass *) klass;

137
  parent_class = g_type_class_peek_parent (klass);
138

139
  gobject_class->dispose = gst_flac_tag_dispose;
140
141
142
  gstelement_class->change_state = gst_flac_tag_change_state;
}

143
144
145
146
147
148
static void
gst_flac_tag_dispose (GObject * object)
{
  GstFlacTag *tag = GST_FLAC_TAG (object);

  if (tag->adapter) {
149
    g_object_unref (tag->adapter);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    tag->adapter = NULL;
  }
  if (tag->vorbiscomment) {
    gst_buffer_unref (tag->vorbiscomment);
    tag->vorbiscomment = NULL;
  }
  if (tag->tags) {
    gst_tag_list_free (tag->tags);
    tag->tags = NULL;
  }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

164
165

static void
166
gst_flac_tag_init (GstFlacTag * tag, GstFlacTagClass * klass)
167
168
{
  /* create the sink and src pads */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
169
  tag->sinkpad =
Edward Hervey's avatar
Edward Hervey committed
170
      gst_pad_new_from_static_template (&flac_tag_sink_template, "sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
171
172
  gst_pad_set_chain_function (tag->sinkpad,
      GST_DEBUG_FUNCPTR (gst_flac_tag_chain));
173
174
175
  gst_pad_set_setcaps_function (tag->sinkpad,
      GST_DEBUG_FUNCPTR (gst_flac_tag_sink_setcaps));
  gst_element_add_pad (GST_ELEMENT (tag), tag->sinkpad);
176

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
177
  tag->srcpad =
Edward Hervey's avatar
Edward Hervey committed
178
      gst_pad_new_from_static_template (&flac_tag_src_template, "src");
179
180
  gst_element_add_pad (GST_ELEMENT (tag), tag->srcpad);

181
182
183
184
185
186
187
188
189
  tag->adapter = gst_adapter_new ();
}

static gboolean
gst_flac_tag_sink_setcaps (GstPad * pad, GstCaps * caps)
{
  GstFlacTag *tag = GST_FLAC_TAG (GST_PAD_PARENT (pad));

  return gst_pad_set_caps (tag->srcpad, caps);
190
191
192
193
194
}

#define FLAC_MAGIC "fLaC"
#define FLAC_MAGIC_SIZE (sizeof (FLAC_MAGIC) - 1)

195
196
static GstFlowReturn
gst_flac_tag_chain (GstPad * pad, GstBuffer * buffer)
197
198
{
  GstFlacTag *tag;
199
  GstFlowReturn ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
200

201
  ret = GST_FLOW_OK;
202
203
  tag = GST_FLAC_TAG (gst_pad_get_parent (pad));

204
  gst_adapter_push (tag->adapter, buffer);
205
206
207

  /* Initial state, we don't even know if we are dealing with a flac file */
  if (tag->state == GST_FLAC_TAG_STATE_INIT) {
208
    GstBuffer *id_buffer;
209

210
    if (gst_adapter_available (tag->adapter) < sizeof (FLAC_MAGIC))
211
      goto cleanup;
212

213
214
215
216
217
218
219
220
221
    id_buffer = gst_adapter_take_buffer (tag->adapter, FLAC_MAGIC_SIZE);
    GST_DEBUG_OBJECT (tag, "looking for " FLAC_MAGIC " identifier");
    if (memcmp (GST_BUFFER_DATA (id_buffer), FLAC_MAGIC, FLAC_MAGIC_SIZE) == 0) {

      GST_DEBUG_OBJECT (tag, "pushing " FLAC_MAGIC " identifier buffer");
      gst_buffer_set_caps (id_buffer, GST_PAD_CAPS (tag->srcpad));
      ret = gst_pad_push (tag->srcpad, id_buffer);
      if (ret != GST_FLOW_OK)
        goto cleanup;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
222

223
224
225
      tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
    } else {
      /* 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;
229
230
231
    }
  }

232

233
234
235
236
237
238
239
  /* The fLaC magic string has been skipped, try to detect the beginning
   * of a metadata block
   */
  if (tag->state == GST_FLAC_TAG_STATE_METADATA_BLOCKS) {
    guint size;
    guint type;
    gboolean is_last;
240
    const guint8 *block_header;
241

242
    g_assert (tag->metadata_block_size == 0);
243
244
245
246
247
248
249
    g_assert (tag->metadata_last_block == FALSE);

    /* The header of a flac metadata block is 4 bytes long:
     * 1st bit: indicates whether this is the last metadata info block
     * 7 next bits: 4 if vorbis comment block
     * 24 next bits: size of the metadata to follow (big endian)
     */
250
    if (gst_adapter_available (tag->adapter) < 4)
251
      goto cleanup;
252

253
254
255
256
257
258
259
    block_header = gst_adapter_peek (tag->adapter, 4);

    is_last = ((block_header[0] & 0x80) == 0x80);
    type = block_header[0] & 0x7F;
    size = (block_header[1] << 16)
        | (block_header[2] << 8)
        | block_header[3];
260
261

    /* The 4 bytes long header isn't included in the metadata size */
262
    tag->metadata_block_size = size + 4;
263
264
    tag->metadata_last_block = is_last;

265
266
267
268
    GST_DEBUG_OBJECT (tag,
        "got metadata block: %d bytes, type %d, is vorbiscomment: %d, is last: %d",
        size, type, (type == 0x04), is_last);

269
    /* Metadata blocks of type 4 are vorbis comment blocks */
270
271
    if (type == 0x04) {
      tag->state = GST_FLAC_TAG_STATE_VC_METADATA_BLOCK;
272
273
274
275
276
    } else {
      tag->state = GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK;
    }
  }

277

278
279
  /* Reads a metadata block */
  if ((tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) ||
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
280
      (tag->state == GST_FLAC_TAG_STATE_VC_METADATA_BLOCK)) {
281
    GstBuffer *metadata_buffer;
282

283
284
    if (gst_adapter_available (tag->adapter) < tag->metadata_block_size)
      goto cleanup;
285

286
287
288
289
290
291
    metadata_buffer = gst_adapter_take_buffer (tag->adapter,
        tag->metadata_block_size);
    /* clear the is-last flag, as the last metadata block will
     * be the vorbis comment block which we will build ourselves.
     */
    GST_BUFFER_DATA (metadata_buffer)[0] &= (~0x80);
292

293
    if (tag->state == GST_FLAC_TAG_STATE_WRITING_METADATA_BLOCK) {
294
295
296
297
298
      GST_DEBUG_OBJECT (tag, "pushing metadata block buffer");
      gst_buffer_set_caps (metadata_buffer, GST_PAD_CAPS (tag->srcpad));
      ret = gst_pad_push (tag->srcpad, metadata_buffer);
      if (ret != GST_FLOW_OK)
        goto cleanup;
299
    } else {
300
      tag->vorbiscomment = metadata_buffer;
301
    }
302
303
    tag->metadata_block_size = 0;
    tag->state = GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK;
304
305
  }

306
307
308
309
310
311
312
313
314
315
  /* This state is mainly used to be able to stop as soon as we read
   * a vorbiscomment block from the flac file if we are in an only output
   * tags mode
   */
  if (tag->state == GST_FLAC_TAG_STATE_METADATA_NEXT_BLOCK) {
    /* Check if in the previous iteration we read a vorbis comment metadata 
     * block, and stop now if the user only wants to read tags
     */
    if (tag->vorbiscomment != NULL) {
      /* We found some tags, try to parse them and notify the other elements
316
       * that we encountered some tags
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
317
       */
318
      GST_DEBUG_OBJECT (tag, "emitting vorbiscomment tags");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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
323
        gst_element_found_tags (GST_ELEMENT (tag),
            gst_tag_list_copy (tag->tags));
324
325
326
327
328
      }

      gst_buffer_unref (tag->vorbiscomment);
      tag->vorbiscomment = NULL;
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
329

330
331
332
333
    /* Skip to next state */
    if (tag->metadata_last_block == FALSE) {
      tag->state = GST_FLAC_TAG_STATE_METADATA_BLOCKS;
    } else {
334
      tag->state = GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT;
335
336
337
338
    }
  }


339
340
341
342
343
344
  /* Creates a vorbis comment block from the metadata which was set
   * on the gstreamer element, and add it to the flac stream
   */
  if (tag->state == GST_FLAC_TAG_STATE_ADD_VORBIS_COMMENT) {
    GstBuffer *buffer;
    gint size;
345
346
347
    const GstTagList *user_tags;
    GstTagList *merged_tags;

348
    /* merge the tag lists */
349
    user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tag));
350
351
352
353
354
355
    if (user_tags != NULL) {
      merged_tags = gst_tag_list_merge (user_tags, tag->tags,
          gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tag)));
    } else {
      merged_tags = gst_tag_list_copy (tag->tags);
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
356

357
358
359
360
    if (merged_tags == NULL) {
      /* If we get a NULL list of tags, we must generate a padding block
       * which is marked as the last metadata block, otherwise we'll
       * end up with a corrupted flac file.
361
       */
362
      GST_WARNING_OBJECT (tag, "No tags found");
363
364
      buffer = gst_buffer_new_and_alloc (12);
      if (buffer == NULL) {
365
366
        GST_ELEMENT_ERROR (tag, CORE, TOO_LAZY, (NULL),
            ("Error creating 12-byte buffer for padding block"));
367
368
        ret = GST_FLOW_ERROR;
        goto cleanup;
369
      }
370
      memset (GST_BUFFER_DATA (buffer), 0, GST_BUFFER_SIZE (buffer));
371
372
373
      GST_BUFFER_DATA (buffer)[0] = 0x81;       /* 0x80 = Last metadata block, 
                                                 * 0x01 = padding block
                                                 */
374
375
    } else {
      guchar header[4];
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
376

377
      memset (header, 0, sizeof (header));
378
379
380
      header[0] = 0x84;         /* 0x80 = Last metadata block, 
                                 * 0x04 = vorbiscomment block
                                 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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
385
      gst_tag_list_free (merged_tags);
      if (buffer == NULL) {
386
387
        GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
            ("Error converting tag list to vorbiscomment buffer"));
388
        ret = GST_FLOW_ERROR;
389
        goto cleanup;
390
391
392
      }
      size = GST_BUFFER_SIZE (buffer) - 4;
      if ((size > 0xFFFFFF) || (size < 0)) {
393
394
395
396
397
398
        /* FLAC vorbis comment blocks are limited to 2^24 bytes, 
         * while the vorbis specs allow more than that. Shouldn't 
         * be a real world problem though
         */
        GST_ELEMENT_ERROR (tag, CORE, TAG, (NULL),
            ("Vorbis comment of size %d too long", size));
399
        ret = GST_FLOW_ERROR;
400
        goto cleanup;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
401
      }
402
403
404
405
406
407
408
409
410
411
412
413

      /* Get rid of the framing bit at the end of the vorbiscomment buffer 
       * if it exists since libFLAC seems to lose sync because of this
       * bit in gstflacdec
       */
      if (GST_BUFFER_DATA (buffer)[GST_BUFFER_SIZE (buffer) - 1] == 1) {
        GstBuffer *sub;

        sub = gst_buffer_create_sub (buffer, 0, GST_BUFFER_SIZE (buffer) - 1);
        gst_buffer_unref (buffer);
        buffer = sub;
      }
414
415
416
417
418
419
420
    }

    /* The 4 byte metadata block header isn't accounted for in the total
     * size of the metadata block
     */
    size = GST_BUFFER_SIZE (buffer) - 4;

421
    GST_BUFFER_DATA (buffer)[1] = ((size & 0xFF0000) >> 16);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
422
423
    GST_BUFFER_DATA (buffer)[2] = ((size & 0x00FF00) >> 8);
    GST_BUFFER_DATA (buffer)[3] = (size & 0x0000FF);
424
425
426
427
428
429
430
    GST_DEBUG_OBJECT (tag, "pushing %d byte vorbiscomment buffer",
        GST_BUFFER_SIZE (buffer));
    gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
    ret = gst_pad_push (tag->srcpad, buffer);
    if (ret != GST_FLOW_OK) {
      goto cleanup;
    }
431
432
433
434
435
    tag->state = GST_FLAC_TAG_STATE_AUDIO_DATA;
  }

  /* The metadata blocks have been read, now we are reading audio data */
  if (tag->state == GST_FLAC_TAG_STATE_AUDIO_DATA) {
436
    GstBuffer *buffer;
437
438
439
440
441
442
443
444
    guint avail;

    avail = gst_adapter_available (tag->adapter);
    if (avail > 0) {
      buffer = gst_adapter_take_buffer (tag->adapter, avail);
      gst_buffer_set_caps (buffer, GST_PAD_CAPS (tag->srcpad));
      ret = gst_pad_push (tag->srcpad, buffer);
    }
445
  }
446
447
448

cleanup:
  gst_object_unref (tag);
449
450

  return ret;
451
452
453
}


454
455
static GstStateChangeReturn
gst_flac_tag_change_state (GstElement * element, GstStateChange transition)
456
457
458
459
460
{
  GstFlacTag *tag;

  tag = GST_FLAC_TAG (element);

461
462
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
463
      break;
464
    case GST_STATE_CHANGE_READY_TO_PAUSED:
465
      break;
466
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
467
468
      /* do something to get out of the chain function faster */
      break;
469
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
470
      break;
471
    case GST_STATE_CHANGE_PAUSED_TO_READY:
472
      gst_adapter_clear (tag->adapter);
473
      if (tag->vorbiscomment) {
474
475
        gst_buffer_unref (tag->vorbiscomment);
        tag->vorbiscomment = NULL;
476
477
      }
      if (tag->tags) {
478
        gst_tag_list_free (tag->tags);
479
        tag->tags = NULL;
480
      }
481
482
      tag->metadata_block_size = 0;
      tag->metadata_last_block = FALSE;
483
484
      tag->state = GST_FLAC_TAG_STATE_INIT;
      break;
485
    case GST_STATE_CHANGE_READY_TO_NULL:
486
487
488
      break;
  }

489
  return parent_class->change_state (element, transition);
490
}