gstcmmldec.c 20.4 KB
Newer Older
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
/*
 * 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>.
32
33
 *
 * <refsect2>
34
 * <title>Example pipeline</title>
35
 * |[
36
 * gst-launch -v filesrc location=annotated.ogg ! oggdemux ! cmmldec ! filesink location=annotations.cmml
37
 * ]|
38
39
40
41
42
43
44
45
46
47
48
 * </refsect2>
 */


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

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

49
#include <gst/tag/tag.h>
50
51
52
53
54
#include "gstannodex.h"
#include "gstcmmltag.h"
#include "gstcmmldec.h"
#include "gstcmmlutils.h"

55
GST_DEBUG_CATEGORY_STATIC (cmmldec);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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,
75
    GST_STATIC_CAPS ("text/x-cmml, encoded = (boolean) false")
76
77
78
79
80
81
    );

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

/* GstCmmlDec */
Wim Taymans's avatar
Wim Taymans committed
86
87
88
#define gst_cmml_dec_parent_class parent_class
G_DEFINE_TYPE (GstCmmlDec, gst_cmml_dec, GST_TYPE_ELEMENT);

89
90
91
92
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);
Wim Taymans's avatar
Wim Taymans committed
93
94
static gboolean gst_cmml_dec_sink_query (GstPad * pad, GstObject * parent,
    GstQuery * query);
Wim Taymans's avatar
Wim Taymans committed
95
96
static gboolean gst_cmml_dec_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event);
97
98
99
100
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);
Wim Taymans's avatar
Wim Taymans committed
101
102
static GstFlowReturn gst_cmml_dec_chain (GstPad * pad, GstObject * parent,
    GstBuffer * buffer);
103
104

static GstCmmlPacketType gst_cmml_dec_parse_packet_type (GstCmmlDec * dec,
Wim Taymans's avatar
Wim Taymans committed
105
106
107
108
109
110
111
112
113
    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);
114
115
116
117
118
119
120
121
122
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);

123
124
static void gst_cmml_dec_finalize (GObject * object);

125
static void
Wim Taymans's avatar
Wim Taymans committed
126
gst_cmml_dec_class_init (GstCmmlDecClass * klass)
127
{
Wim Taymans's avatar
Wim Taymans committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  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;
143
144
145
146
147

  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));
148
  gst_element_class_set_static_metadata (element_class, "CMML stream decoder",
149
150
      "Codec/Decoder",
      "Decodes CMML streams", "Alessandro Decina <alessandro@nnva.org>");
151
152
153
}

static void
Wim Taymans's avatar
Wim Taymans committed
154
gst_cmml_dec_init (GstCmmlDec * dec)
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
{
  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);
  }
}

200
201
202
203
204
205
206
207
208
209
210
211
212
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);
}

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
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;
  }

Wim Taymans's avatar
Wim Taymans committed
243
  res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
244
245
246
247
248

  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_cmml_parser_free (dec->parser);
      gst_cmml_track_list_destroy (dec->tracks);
249
      dec->tracks = NULL;
250
251
252
253
254
255
256
257
258
      break;
    default:
      break;
  }

  return res;
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
259
gst_cmml_dec_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
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
{
  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
Wim Taymans's avatar
Wim Taymans committed
311
gst_cmml_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
312
{
Wim Taymans's avatar
Wim Taymans committed
313
  GstCmmlDec *dec = GST_CMML_DEC (parent);
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

  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);
344
345
      if (dec->flow_return == GST_FLOW_NOT_LINKED)
        dec->flow_return = GST_FLOW_OK; /* Ignore NOT_LINKED */
346
347
348
349
350
351
352

      break;
    }
    default:
      break;
  }

Wim Taymans's avatar
Wim Taymans committed
353
  return gst_pad_event_default (pad, parent, event);
354
355
356
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
357
gst_cmml_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
358
{
Wim Taymans's avatar
Wim Taymans committed
359
  GstCmmlDec *dec = GST_CMML_DEC (parent);
360
  GstCmmlPacketType packet;
Wim Taymans's avatar
Wim Taymans committed
361
  GstMapInfo map;
Wim Taymans's avatar
Wim Taymans committed
362
363
364
  guint8 *data;
  gsize size;

Wim Taymans's avatar
Wim Taymans committed
365
366
367
  gst_buffer_map (buffer, &map, GST_MAP_READ);
  data = map.data;
  size = map.size;
368

Wim Taymans's avatar
Wim Taymans committed
369
  if (size == 0) {
370
371
372
373
374
375
376
377
378
379
    /* 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 */
Wim Taymans's avatar
Wim Taymans committed
380
  packet = gst_cmml_dec_parse_packet_type (dec, (gchar *) data, size);
381
382
383
384

  /* handle the packet. the handler will set dec->flow_return */
  switch (packet) {
    case GST_CMML_PACKET_IDENT_HEADER:
385
386
      if (dec->sent_root == FALSE)
        /* don't parse the ident again in case of seeking to the beginning */
Wim Taymans's avatar
Wim Taymans committed
387
        gst_cmml_dec_parse_ident_header (dec, data, size);
388
389
      break;
    case GST_CMML_PACKET_FIRST_HEADER:
390
391
392
      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 */
Wim Taymans's avatar
Wim Taymans committed
393
        gst_cmml_dec_parse_first_header (dec, data, size);
394
395
396
      break;
    case GST_CMML_PACKET_SECOND_HEADER:
    case GST_CMML_PACKET_CLIP:
Wim Taymans's avatar
Wim Taymans committed
397
      gst_cmml_dec_parse_xml (dec, data, size);
398
399
400
401
402
403
404
405
      break;
    case GST_CMML_PACKET_UNKNOWN:
    default:
      GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("unknown packet type"));
      dec->flow_return = GST_FLOW_ERROR;
  }

done:
Wim Taymans's avatar
Wim Taymans committed
406
  gst_buffer_unmap (buffer, &map);
407
  gst_buffer_unref (buffer);
Wim Taymans's avatar
Wim Taymans committed
408

409
410
411
412
413
414
  return dec->flow_return;
}

/* finds the packet type of the buffer
 */
static GstCmmlPacketType
Wim Taymans's avatar
Wim Taymans committed
415
gst_cmml_dec_parse_packet_type (GstCmmlDec * dec, gchar * data, gsize size)
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
{
  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;

Wim Taymans's avatar
Wim Taymans committed
441
  *buffer = gst_buffer_new_allocate (NULL, size, NULL);
Wim Taymans's avatar
Wim Taymans committed
442
  if (*buffer != NULL) {
443
    if (data)
Wim Taymans's avatar
Wim Taymans committed
444
      gst_buffer_fill (*buffer, 0, data, size);
445
    GST_BUFFER_TIMESTAMP (*buffer) = dec->timestamp;
Wim Taymans's avatar
Wim Taymans committed
446
    res = GST_FLOW_OK;
447
  } else {
Wim Taymans's avatar
Wim Taymans committed
448
449
    GST_WARNING_OBJECT (dec, "could not allocate buffer");
    res = GST_FLOW_ERROR;
450
451
452
453
454
455
456
457
  }

  return res;
}

/* parses the first CMML packet (the ident header)
 */
static void
Wim Taymans's avatar
Wim Taymans committed
458
gst_cmml_dec_parse_ident_header (GstCmmlDec * dec, guint8 * data, gsize size)
459
460
{
  /* the ident header has a fixed length */
Wim Taymans's avatar
Wim Taymans committed
461
  if (size != CMML_IDENT_HEADER_SIZE) {
462
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
463
        (NULL), ("wrong ident header size: %" G_GSIZE_FORMAT, size));
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
    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
Wim Taymans's avatar
Wim Taymans committed
495
gst_cmml_dec_parse_first_header (GstCmmlDec * dec, guint8 * data, gsize size)
496
{
Wim Taymans's avatar
Wim Taymans committed
497
  gst_cmml_dec_parse_xml (dec, data, size);
498
499
500
501

  /* if there is a processing instruction, gst_cmml_dec_parse_preamble
   * will be triggered. Otherwise we need to call it manually.
   */
502
  if (dec->flow_return == GST_FLOW_OK && !dec->sent_root) {
Wim Taymans's avatar
Wim Taymans committed
503
    guchar *preamble = (guchar *) g_strndup ((gchar *) data, size);
504
505
506

    gst_cmml_dec_parse_preamble (dec, preamble, (guchar *) "<cmml>");
    g_free (preamble);
507
508
509
510
511
512
513
514
515
516
517
  }
}

/* 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)) {
Josep Torra's avatar
Josep Torra committed
518
    GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("%s", err->message));
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    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));
536
  dec->sent_root = TRUE;
537
538
539
540

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

545
  if (dec->flow_return == GST_FLOW_OK) {
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    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);

569
  /* FIXME: what's the point of all this GValue transform crack? */
570
571
572
573
574
575
  /* 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);

576
  tags = gst_tag_list_new_empty ();
577
578
579
  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);
Wim Taymans's avatar
Wim Taymans committed
580
  gst_pad_push_event (dec->srcpad, gst_event_new_tag (tags));
581
582
583
584
585
586
587
588
589
590
591

  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);
592
593
  if (dec->flow_return == GST_FLOW_NOT_LINKED)
    dec->flow_return = GST_FLOW_OK;     /* Ignore NOT_LINKED */
594
595
596
597
598
599
600
601
602
603
}

/* 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);

604
  tags = gst_tag_list_new (GST_TAG_CMML_CLIP, clip, NULL);
Wim Taymans's avatar
Wim Taymans committed
605
  gst_pad_push_event (dec->srcpad, gst_event_new_tag (tags));
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
}

/* 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);
622
623
  if (dec->flow_return == GST_FLOW_NOT_LINKED)
    dec->flow_return = GST_FLOW_OK;     /* Ignore NOT_LINKED */
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

  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;
}