summaryrefslogtreecommitdiff
path: root/ext/annodex/gstcmmldec.c
blob: 4a04be08321eb044df49958ada04476fae54e433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
 * gstcmmldec.c - GStreamer annodex CMML decoder
 * Copyright (C) 2005 Alessandro Decina
 * 
 * Authors:
 *   Alessandro Decina <alessandro@nnva.org>
 *
 * 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.
 */

/**
 * SECTION:element-cmmldec
 * @see_also: cmmlenc, oggdemux
 *
 * Cmmldec extracts a CMML document from a CMML bitstream.<ulink
 * url="http://www.annodex.net/TR/draft-pfeiffer-cmml-02.html">CMML</ulink> is
 * an XML markup language for time-continuous data maintained by the <ulink
 * url="http:/www.annodex.org/">Annodex Foundation</ulink>.
 *
 * <refsect2>
 * <title>Example pipeline</title>
 * |[
 * gst-launch -v filesrc location=annotated.ogg ! oggdemux ! cmmldec ! filesink location=annotations.cmml
 * ]|
 * </refsect2>
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <glib.h>

#include <gst/tag/tag.h>
#include "gstannodex.h"
#include "gstcmmltag.h"
#include "gstcmmldec.h"
#include "gstcmmlutils.h"

GST_DEBUG_CATEGORY_STATIC (cmmldec);
#define GST_CAT_DEFAULT cmmldec

#define CMML_IDENT_HEADER_SIZE 29

enum
{
  ARG_0,
  GST_CMML_DEC_WAIT_CLIP_END
};

enum
{
  LAST_SIGNAL
};

static GstStaticPadTemplate gst_cmml_dec_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("text/x-cmml, encoded = (boolean) false")
    );

static GstStaticPadTemplate gst_cmml_dec_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("text/x-cmml, encoded = (boolean) true")
    );

/* GstCmmlDec */
#define gst_cmml_dec_parent_class parent_class
G_DEFINE_TYPE (GstCmmlDec, gst_cmml_dec, GST_TYPE_ELEMENT);

static void gst_cmml_dec_get_property (GObject * dec, guint property_id,
    GValue * value, GParamSpec * pspec);
static void gst_cmml_dec_set_property (GObject * dec, guint property_id,
    const GValue * value, GParamSpec * pspec);
static gboolean gst_cmml_dec_sink_query (GstPad * pad, GstObject * parent,
    GstQuery * query);
static gboolean gst_cmml_dec_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event);
static gboolean gst_cmml_dec_convert (GstPad * pad, GstFormat src_fmt,
    gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val);
static GstStateChangeReturn gst_cmml_dec_change_state (GstElement * element,
    GstStateChange transition);
static GstFlowReturn gst_cmml_dec_chain (GstPad * pad, GstObject * parent,
    GstBuffer * buffer);

static GstCmmlPacketType gst_cmml_dec_parse_packet_type (GstCmmlDec * dec,
    gchar * data, gsize size);
static void gst_cmml_dec_parse_ident_header (GstCmmlDec * dec, guint8 * data,
    gsize size);
static void gst_cmml_dec_parse_first_header (GstCmmlDec * dec, guint8 * data,
    gsize size);
static void gst_cmml_dec_parse_preamble (GstCmmlDec * dec, guchar * preamble,
    guchar * cmml_root_element);
static void gst_cmml_dec_parse_xml (GstCmmlDec * dec, guchar * data,
    guint size);
static void gst_cmml_dec_parse_head (GstCmmlDec * dec, GstCmmlTagHead * head);
static void gst_cmml_dec_parse_clip (GstCmmlDec * dec, GstCmmlTagClip * clip);

static GstFlowReturn gst_cmml_dec_new_buffer (GstCmmlDec * dec,
    guchar * data, gint size, GstBuffer ** buffer);
static void gst_cmml_dec_push_clip (GstCmmlDec * dec, GstCmmlTagClip * clip);
static void gst_cmml_dec_send_clip_tag (GstCmmlDec * dec,
    GstCmmlTagClip * clip);

static void gst_cmml_dec_finalize (GObject * object);

static void
gst_cmml_dec_class_init (GstCmmlDecClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  object_class->set_property = gst_cmml_dec_set_property;
  object_class->get_property = gst_cmml_dec_get_property;
  object_class->finalize = gst_cmml_dec_finalize;

  g_object_class_install_property (object_class, GST_CMML_DEC_WAIT_CLIP_END,
      g_param_spec_boolean ("wait-clip-end-time",
          "Wait clip end time",
          "Send a tag for a clip when the clip ends, setting its end-time. "
          "Use when you need to know both clip's start-time and end-time.",
          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  element_class->change_state = gst_cmml_dec_change_state;

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_cmml_dec_sink_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_cmml_dec_src_factory));
  gst_element_class_set_details_simple (element_class, "CMML stream decoder",
      "Codec/Decoder",
      "Decodes CMML streams", "Alessandro Decina <alessandro@nnva.org>");
}

static void
gst_cmml_dec_init (GstCmmlDec * dec)
{
  dec->sinkpad =
      gst_pad_new_from_static_template (&gst_cmml_dec_sink_factory, "sink");
  gst_pad_set_chain_function (dec->sinkpad, gst_cmml_dec_chain);
  gst_pad_set_query_function (dec->sinkpad, gst_cmml_dec_sink_query);
  gst_pad_set_event_function (dec->sinkpad, gst_cmml_dec_sink_event);
  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);

  dec->srcpad =
      gst_pad_new_from_static_template (&gst_cmml_dec_src_factory, "src");
  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);

  dec->wait_clip_end = FALSE;
}

static void
gst_cmml_dec_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GstCmmlDec *dec = GST_CMML_DEC (object);

  switch (property_id) {
    case GST_CMML_DEC_WAIT_CLIP_END:
      g_value_set_boolean (value, dec->wait_clip_end);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
gst_cmml_dec_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GstCmmlDec *dec = GST_CMML_DEC (object);

  switch (property_id) {
    case GST_CMML_DEC_WAIT_CLIP_END:
      dec->wait_clip_end = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (dec, property_id, pspec);
  }
}

static void
gst_cmml_dec_finalize (GObject * object)
{
  GstCmmlDec *dec = GST_CMML_DEC (object);

  if (dec->tracks) {
    gst_cmml_track_list_destroy (dec->tracks);
    dec->tracks = NULL;
  }

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

static GstStateChangeReturn
gst_cmml_dec_change_state (GstElement * element, GstStateChange transition)
{
  GstCmmlDec *dec = GST_CMML_DEC (element);
  GstStateChangeReturn res;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      dec->parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
      dec->parser->user_data = dec;
      dec->parser->preamble_callback =
          (GstCmmlParserPreambleCallback) gst_cmml_dec_parse_preamble;
      dec->parser->head_callback =
          (GstCmmlParserHeadCallback) gst_cmml_dec_parse_head;
      dec->parser->clip_callback =
          (GstCmmlParserClipCallback) gst_cmml_dec_parse_clip;
      dec->major = -1;
      dec->minor = -1;
      dec->granulerate_n = -1;
      dec->granulerate_d = -1;
      dec->granuleshift = 0;
      dec->granulepos = 0;
      dec->flow_return = GST_FLOW_OK;
      dec->sent_root = FALSE;
      dec->tracks = gst_cmml_track_list_new ();
      break;
    default:
      break;
  }

  res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_cmml_parser_free (dec->parser);
      gst_cmml_track_list_destroy (dec->tracks);
      dec->tracks = NULL;
      break;
    default:
      break;
  }

  return res;
}

static gboolean
gst_cmml_dec_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
{
  gboolean res = FALSE;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CONVERT:
    {
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;

      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
      res = gst_cmml_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
      if (res)
        gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
    default:
      break;
  }

  return res;
}

static gboolean
gst_cmml_dec_convert (GstPad * pad,
    GstFormat src_fmt, gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val)
{
  GstCmmlDec *dec = GST_CMML_DEC (GST_PAD_PARENT (pad));
  gboolean res = FALSE;

  switch (src_fmt) {
    case GST_FORMAT_DEFAULT:
      switch (*dest_fmt) {
        case GST_FORMAT_TIME:
        {
          *dest_val = gst_annodex_granule_to_time (src_val, dec->granulerate_n,
              dec->granulerate_d, dec->granuleshift);
          res = TRUE;
          break;
        }
        default:
          break;
      }
      break;
    default:
      break;
  }

  return res;
}

static gboolean
gst_cmml_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
  GstCmmlDec *dec = GST_CMML_DEC (parent);

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_EOS:
    {
      GstBuffer *buffer;
      GstCmmlTagClip *clip;
      GList *clips, *walk;

      GST_INFO_OBJECT (dec, "got EOS, flushing clips");

      /* since we output a clip when the next one in the same track is found, on
       * EOS we need to output the last clip (if any) of every track
       */
      clips = gst_cmml_track_list_get_clips (dec->tracks);
      for (walk = clips; walk; walk = g_list_next (walk)) {
        clip = GST_CMML_TAG_CLIP (walk->data);
        gst_cmml_dec_push_clip (dec, clip);
        if (dec->wait_clip_end) {
          clip->end_time = dec->timestamp;
          gst_cmml_dec_send_clip_tag (dec, clip);
        }
      }
      g_list_free (clips);

      /* send the cmml end tag */
      dec->flow_return = gst_cmml_dec_new_buffer (dec,
          (guchar *) "</cmml>", strlen ("</cmml>"), &buffer);

      if (dec->flow_return == GST_FLOW_OK)
        dec->flow_return = gst_pad_push (dec->srcpad, buffer);
      if (dec->flow_return == GST_FLOW_NOT_LINKED)
        dec->flow_return = GST_FLOW_OK; /* Ignore NOT_LINKED */

      break;
    }
    default:
      break;
  }

  return gst_pad_event_default (pad, parent, event);
}

static GstFlowReturn
gst_cmml_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
  GstCmmlDec *dec = GST_CMML_DEC (parent);
  GstCmmlPacketType packet;
  GstMapInfo map;
  guint8 *data;
  gsize size;

  gst_buffer_map (buffer, &map, GST_MAP_READ);
  data = map.data;
  size = map.size;

  if (size == 0) {
    /* the EOS page could be empty */
    dec->flow_return = GST_FLOW_OK;
    goto done;
  }

  dec->granulepos = GST_BUFFER_OFFSET_END (buffer);
  dec->timestamp = gst_annodex_granule_to_time (dec->granulepos,
      dec->granulerate_n, dec->granulerate_d, dec->granuleshift);

  /* identify the packet type */
  packet = gst_cmml_dec_parse_packet_type (dec, (gchar *) data, size);

  /* handle the packet. the handler will set dec->flow_return */
  switch (packet) {
    case GST_CMML_PACKET_IDENT_HEADER:
      if (dec->sent_root == FALSE)
        /* don't parse the ident again in case of seeking to the beginning */
        gst_cmml_dec_parse_ident_header (dec, data, size);
      break;
    case GST_CMML_PACKET_FIRST_HEADER:
      if (dec->sent_root == FALSE)
        /* don't parse the xml preamble if it has already been parsed because it
         * would error out, so seeking to the beginning would fail */
        gst_cmml_dec_parse_first_header (dec, data, size);
      break;
    case GST_CMML_PACKET_SECOND_HEADER:
    case GST_CMML_PACKET_CLIP:
      gst_cmml_dec_parse_xml (dec, data, size);
      break;
    case GST_CMML_PACKET_UNKNOWN:
    default:
      GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("unknown packet type"));
      dec->flow_return = GST_FLOW_ERROR;
  }

done:
  gst_buffer_unmap (buffer, &map);
  gst_buffer_unref (buffer);

  return dec->flow_return;
}

/* finds the packet type of the buffer
 */
static GstCmmlPacketType
gst_cmml_dec_parse_packet_type (GstCmmlDec * dec, gchar * data, gsize size)
{
  GstCmmlPacketType packet_type = GST_CMML_PACKET_UNKNOWN;

  if (size >= 8 && !memcmp (data, "CMML\0\0\0\0", 8)) {
    packet_type = GST_CMML_PACKET_IDENT_HEADER;
  } else if (size >= 5) {
    if (!strncmp (data, "<?xml", 5))
      packet_type = GST_CMML_PACKET_FIRST_HEADER;
    else if (!strncmp (data, "<head", 5))
      packet_type = GST_CMML_PACKET_SECOND_HEADER;
    else if (!strncmp (data, "<clip", 5))
      packet_type = GST_CMML_PACKET_CLIP;
  }

  return packet_type;
}

/* creates a new buffer and sets caps and timestamp on it
 */
static GstFlowReturn
gst_cmml_dec_new_buffer (GstCmmlDec * dec,
    guchar * data, gint size, GstBuffer ** buffer)
{
  GstFlowReturn res;

  *buffer = gst_buffer_new_allocate (NULL, size, 0);
  if (*buffer != NULL) {
    if (data)
      gst_buffer_fill (*buffer, 0, data, size);
    GST_BUFFER_TIMESTAMP (*buffer) = dec->timestamp;
    res = GST_FLOW_OK;
  } else {
    GST_WARNING_OBJECT (dec, "could not allocate buffer");
    res = GST_FLOW_ERROR;
  }

  return res;
}

/* parses the first CMML packet (the ident header)
 */
static void
gst_cmml_dec_parse_ident_header (GstCmmlDec * dec, guint8 * data, gsize size)
{
  /* the ident header has a fixed length */
  if (size != CMML_IDENT_HEADER_SIZE) {
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
        (NULL), ("wrong ident header size: %" G_GSIZE_FORMAT, size));
    dec->flow_return = GST_FLOW_ERROR;

    return;
  }

  data += 8;
  dec->major = GST_READ_UINT16_LE (data);
  data += 2;
  dec->minor = GST_READ_UINT16_LE (data);
  data += 2;
  dec->granulerate_n = GST_READ_UINT64_LE (data);
  data += 8;
  dec->granulerate_d = GST_READ_UINT64_LE (data);
  data += 8;
  dec->granuleshift = GST_READ_UINT8 (data);

  GST_INFO_OBJECT (dec, "bitstream initialized "
      "(major: %" G_GINT16_FORMAT " minor: %" G_GINT16_FORMAT
      " granulerate_n: %" G_GINT64_FORMAT " granulerate_d: %" G_GINT64_FORMAT
      " granuleshift: %d)",
      dec->major, dec->minor,
      dec->granulerate_n, dec->granulerate_d, dec->granuleshift);

  dec->flow_return = GST_FLOW_OK;
}

/* parses the first secondary header.
 * the first secondary header contains the xml version, the doctype and the
 * optional "cmml" processing instruction.
 */
static void
gst_cmml_dec_parse_first_header (GstCmmlDec * dec, guint8 * data, gsize size)
{
  gst_cmml_dec_parse_xml (dec, data, size);

  /* if there is a processing instruction, gst_cmml_dec_parse_preamble
   * will be triggered. Otherwise we need to call it manually.
   */
  if (dec->flow_return == GST_FLOW_OK && !dec->sent_root) {
    guchar *preamble = (guchar *) g_strndup ((gchar *) data, size);

    gst_cmml_dec_parse_preamble (dec, preamble, (guchar *) "<cmml>");
    g_free (preamble);
  }
}

/* feeds data into the cmml parser.
 */
static void
gst_cmml_dec_parse_xml (GstCmmlDec * dec, guchar * data, guint size)
{
  GError *err = NULL;

  if (!gst_cmml_parser_parse_chunk (dec->parser, (gchar *) data, size, &err)) {
    GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("%s", err->message));
    g_error_free (err);
    dec->flow_return = GST_FLOW_ERROR;
  }
}

static void
gst_cmml_dec_parse_preamble (GstCmmlDec * dec, guchar * preamble,
    guchar * root_element)
{
  GstBuffer *buffer;
  guchar *encoded_preamble;

  encoded_preamble = (guchar *) g_strconcat ((gchar *) preamble,
      (gchar *) root_element, NULL);

  /* send the root element to the internal parser */
  gst_cmml_dec_parse_xml (dec, root_element, strlen ((gchar *) root_element));
  dec->sent_root = TRUE;

  /* push the root element */
  dec->flow_return = gst_cmml_dec_new_buffer (dec,
      encoded_preamble, strlen ((gchar *) encoded_preamble), &buffer);
  if (dec->flow_return == GST_FLOW_OK) {
    dec->flow_return = gst_pad_push (dec->srcpad, buffer);
  }

  if (dec->flow_return == GST_FLOW_OK) {
    GST_INFO_OBJECT (dec, "preamble parsed");
  }

  g_free (encoded_preamble);
  return;
}

/* outputs the cmml head element and send TITLE and CMML_HEAD tags.
 * This callback is registered with dec->parser. It is called when the
 * head element is parsed.
 */
static void
gst_cmml_dec_parse_head (GstCmmlDec * dec, GstCmmlTagHead * head)
{
  GstTagList *tags;
  GValue str_val = { 0 }, title_val = {
  0};
  guchar *head_str;
  GstBuffer *buffer;

  GST_DEBUG_OBJECT (dec, "found CMML head (title: %s base: %s)",
      head->title, head->base);

  /* FIXME: what's the point of all this GValue transform crack? */
  /* create the GST_TAG_TITLE tag */
  g_value_init (&str_val, G_TYPE_STRING);
  g_value_init (&title_val, gst_tag_get_type (GST_TAG_TITLE));
  g_value_set_string (&str_val, (gchar *) head->title);
  g_value_transform (&str_val, &title_val);

  tags = gst_tag_list_new_empty ();
  gst_tag_list_add_values (tags, GST_TAG_MERGE_APPEND,
      GST_TAG_TITLE, &title_val, NULL);
  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_CMML_HEAD, head, NULL);
  gst_pad_push_event (dec->srcpad, gst_event_new_tag (tags));

  g_value_unset (&str_val);
  g_value_unset (&title_val);

  head_str = gst_cmml_parser_tag_head_to_string (dec->parser, head);

  dec->flow_return = gst_cmml_dec_new_buffer (dec,
      head_str, strlen ((gchar *) head_str), &buffer);
  g_free (head_str);
  if (dec->flow_return == GST_FLOW_OK)
    dec->flow_return = gst_pad_push (dec->srcpad, buffer);
  if (dec->flow_return == GST_FLOW_NOT_LINKED)
    dec->flow_return = GST_FLOW_OK;     /* Ignore NOT_LINKED */
}

/* send a TAG_MESSAGE event for a clip */
static void
gst_cmml_dec_send_clip_tag (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
  GstTagList *tags;

  GST_DEBUG_OBJECT (dec, "sending clip tag %s", clip->id);

  tags = gst_tag_list_new (GST_TAG_CMML_CLIP, clip, NULL);
  gst_pad_push_event (dec->srcpad, gst_event_new_tag (tags));
}

/* push the string representation of a clip */
static void
gst_cmml_dec_push_clip (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
  GstBuffer *buffer;
  guchar *clip_str;

  GST_DEBUG_OBJECT (dec, "pushing clip %s", clip->id);

  clip_str = gst_cmml_parser_tag_clip_to_string (dec->parser, clip);
  dec->flow_return = gst_cmml_dec_new_buffer (dec,
      clip_str, strlen ((gchar *) clip_str), &buffer);
  if (dec->flow_return == GST_FLOW_OK)
    dec->flow_return = gst_pad_push (dec->srcpad, buffer);
  if (dec->flow_return == GST_FLOW_NOT_LINKED)
    dec->flow_return = GST_FLOW_OK;     /* Ignore NOT_LINKED */

  g_free (clip_str);
}

/* decode a clip tag
 * this callback is registered with dec->parser. It is called whenever a
 * clip is parsed.
 */
static void
gst_cmml_dec_parse_clip (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
  GstCmmlTagClip *prev_clip;

  dec->flow_return = GST_FLOW_OK;

  if (clip->empty)
    GST_INFO_OBJECT (dec, "parsing empty clip");
  else
    GST_INFO_OBJECT (dec, "parsing clip (id: %s)", clip->id);

  clip->start_time = dec->timestamp;
  if (clip->start_time == GST_CLOCK_TIME_NONE) {
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
        (NULL), ("invalid clip start time"));

    dec->flow_return = GST_FLOW_ERROR;
    return;
  }

  /* get the last clip in the current track */
  prev_clip = gst_cmml_track_list_get_track_last_clip (dec->tracks,
      (gchar *) clip->track);
  if (prev_clip) {
    /* output the previous clip */
    if (clip->empty)
      /* the current clip marks the end of the previous one */
      prev_clip->end_time = clip->start_time;

    gst_cmml_dec_push_clip (dec, prev_clip);
  }

  if (dec->wait_clip_end) {
    /* now it's time to send the tag for the previous clip */
    if (prev_clip) {
      prev_clip->end_time = clip->start_time;
      gst_cmml_dec_send_clip_tag (dec, prev_clip);
    }
  } else if (!clip->empty) {
    /* send the tag for the current clip */
    gst_cmml_dec_send_clip_tag (dec, clip);
  }

  if (prev_clip)
    gst_cmml_track_list_del_clip (dec->tracks, prev_clip);

  if (!clip->empty)
    if (!gst_cmml_track_list_has_clip (dec->tracks, clip))
      gst_cmml_track_list_add_clip (dec->tracks, clip);
}

gboolean
gst_cmml_dec_plugin_init (GstPlugin * plugin)
{
  if (!gst_element_register (plugin, "cmmldec", GST_RANK_PRIMARY,
          GST_TYPE_CMML_DEC))
    return FALSE;

  GST_DEBUG_CATEGORY_INIT (cmmldec, "cmmldec", 0,
      "annodex CMML decoding element");

  return TRUE;
}