summaryrefslogtreecommitdiff
path: root/ext/mimic/gstmimdec.c
blob: a0878a136d3bab307ab310d38ed972fa831ffdfd (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
/*
 * GStreamer
 * Copyright (c) 2005 INdT.
 * @author Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
 * @author Rob Taylor <robtaylor@fastmail.fm>
 * @author Philippe Khalaf <burger@speedy.org>
 * @author Ole André Vadla Ravnås <oleavr@gmail.com>
 *
 * 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-mimdec
 * @see_also: mimenc
 *
 * The MIMIC codec is used by MSN Messenger's webcam support. It consumes the
 * TCP header for the MIMIC codec.
 *
 * Its fourcc is ML20.
 */

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

#include <gst/gst.h>

#include "gstmimdec.h"

GST_DEBUG_CATEGORY (mimdec_debug);
#define GST_CAT_DEFAULT (mimdec_debug)

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-mimic")
    );

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-rgb, "
        "bpp = (int) 24, "
        "depth = (int) 24, "
        "endianness = (int) 4321, "
        "framerate = (fraction) [ 0/1, 30/1 ], "
        "red_mask = (int) 16711680, "
        "green_mask = (int) 65280, "
        "blue_mask = (int) 255, "
        "height = (int) [16, 4096], " "width = (int) [16, 4096]")
    );

static void gst_mimdec_finalize (GObject * object);

static GstFlowReturn gst_mimdec_chain (GstPad * pad, GstBuffer * in);
static GstStateChangeReturn
gst_mimdec_change_state (GstElement * element, GstStateChange transition);

static gboolean gst_mimdec_sink_event (GstPad * pad, GstEvent * event);


GST_BOILERPLATE (GstMimDec, gst_mimdec, GstElement, GST_TYPE_ELEMENT);

static void
gst_mimdec_base_init (gpointer klass)
{
  static GstElementDetails plugin_details = {
    "MimDec",
    "Codec/Decoder/Video",
    "Mimic decoder",
    "Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>, "
        "Rob Taylor <robtaylor@fastmail.fm>, "
        "Philippe Khalaf <burger@speedy.org>, "
        "Ole André Vadla Ravnås <oleavr@gmail.com>"
  };
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));

  gst_element_class_set_details (element_class, &plugin_details);
}

static void
gst_mimdec_class_init (GstMimDecClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
  gstelement_class->change_state = gst_mimdec_change_state;

  gobject_class->finalize = gst_mimdec_finalize;

  GST_DEBUG_CATEGORY_INIT (mimdec_debug, "mimdec", 0, "Mimic decoder plugin");
}

static void
gst_mimdec_init (GstMimDec * mimdec, GstMimDecClass * klass)
{
  mimdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
  gst_element_add_pad (GST_ELEMENT (mimdec), mimdec->sinkpad);
  gst_pad_set_chain_function (mimdec->sinkpad, gst_mimdec_chain);
  gst_pad_set_event_function (mimdec->sinkpad, gst_mimdec_sink_event);

  mimdec->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
  gst_element_add_pad (GST_ELEMENT (mimdec), mimdec->srcpad);

  mimdec->adapter = gst_adapter_new ();

  mimdec->dec = NULL;
  mimdec->buffer_size = -1;
  mimdec->have_header = FALSE;
  mimdec->payload_size = -1;
  mimdec->current_ts = -1;
}

static void
gst_mimdec_finalize (GObject * object)
{
  GstMimDec *mimdec = GST_MIMDEC (object);

  gst_adapter_clear (mimdec->adapter);
  g_object_unref (mimdec->adapter);

  GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static GstFlowReturn
gst_mimdec_chain (GstPad * pad, GstBuffer * in)
{
  GstMimDec *mimdec;
  GstBuffer *out_buf, *buf;
  guchar *header, *frame_body;
  guint32 fourcc;
  guint16 header_size;
  gint width, height;
  GstCaps *caps;
  GstFlowReturn res = GST_FLOW_OK;
  GstClockTime in_time = GST_BUFFER_TIMESTAMP (in);

  GST_DEBUG ("in gst_mimdec_chain");

  g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);

  mimdec = GST_MIMDEC (gst_pad_get_parent (pad));
  g_return_val_if_fail (GST_IS_MIMDEC (mimdec), GST_FLOW_ERROR);

  buf = GST_BUFFER (in);
  gst_adapter_push (mimdec->adapter, buf);

  GST_OBJECT_LOCK (mimdec);

  // do we have enough bytes to read a header
  while (gst_adapter_available (mimdec->adapter) >=
      (mimdec->have_header ? mimdec->payload_size : 24)) {
    if (!mimdec->have_header) {
      header = (guchar *) gst_adapter_peek (mimdec->adapter, 24);
      header_size = header[0];
      if (header_size != 24) {
        GST_WARNING_OBJECT (mimdec,
            "invalid frame: header size %d incorrect", header_size);
        gst_adapter_flush (mimdec->adapter, 24);
        res = GST_FLOW_ERROR;
        goto out;
      }

      if (header[1] == 1) {
        /* This is a a paused frame, skip it */
        gst_adapter_flush (mimdec->adapter, 24);
        continue;
      }

      fourcc = GUINT32_FROM_LE (*((guint32 *) (header + 12)));
      if (GST_MAKE_FOURCC ('M', 'L', '2', '0') != fourcc) {
        GST_WARNING_OBJECT (mimdec, "invalid frame: unknown FOURCC code"
            " %X (%" GST_FOURCC_FORMAT ")", fourcc, GST_FOURCC_ARGS (fourcc));
        gst_adapter_flush (mimdec->adapter, 24);
        res = GST_FLOW_ERROR;
        goto out;
      }

      mimdec->payload_size = GUINT32_FROM_LE (*((guint32 *) (header + 8)));

      mimdec->current_ts = GUINT32_FROM_LE (*((guint32 *) (header + 20)));

      GST_DEBUG ("Got packet, payload size %d", mimdec->payload_size);

      gst_adapter_flush (mimdec->adapter, 24);

      mimdec->have_header = TRUE;
    }

    if (gst_adapter_available (mimdec->adapter) < mimdec->payload_size) {
      goto out;
    }

    frame_body =
        (guchar *) gst_adapter_peek (mimdec->adapter, mimdec->payload_size);

    if (mimdec->dec == NULL) {
      GstEvent *event = NULL;
      gboolean result = TRUE;

      /* Check if its a keyframe, otherwise skip it */
      if (GUINT32_FROM_LE (*((guint32 *) (frame_body + 12))) != 0) {
        gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
        mimdec->have_header = FALSE;
        res = GST_FLOW_OK;
        goto out;
      }

      mimdec->dec = mimic_open ();
      if (mimdec->dec == NULL) {
        GST_WARNING_OBJECT (mimdec, "mimic_open error\n");

        gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
        mimdec->have_header = FALSE;
        res = GST_FLOW_ERROR;
        goto out;
      }

      if (!mimic_decoder_init (mimdec->dec, frame_body)) {
        GST_WARNING_OBJECT (mimdec, "mimic_decoder_init error\n");
        mimic_close (mimdec->dec);
        mimdec->dec = NULL;

        gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
        mimdec->have_header = FALSE;
        res = GST_FLOW_ERROR;
        goto out;
      }

      if (!mimic_get_property (mimdec->dec, "buffer_size",
              &mimdec->buffer_size)) {
        GST_WARNING_OBJECT (mimdec,
            "mimic_get_property('buffer_size') error\n");
        mimic_close (mimdec->dec);
        mimdec->dec = NULL;

        gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
        mimdec->have_header = FALSE;
        res = GST_FLOW_ERROR;
        goto out;
      }

      if (mimdec->need_newsegment)
        event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
            mimdec->current_ts * GST_MSECOND, -1, 0);
      mimdec->need_newsegment = FALSE;
      GST_OBJECT_UNLOCK (mimdec);
      if (event)
        result = gst_pad_push_event (mimdec->srcpad, event);
      GST_OBJECT_LOCK (mimdec);
      if (!result) {
        GST_WARNING_OBJECT (mimdec, "gst_pad_push_event failed");
        res = GST_FLOW_ERROR;
        goto out;
      }
    }

    out_buf = gst_buffer_new_and_alloc (mimdec->buffer_size);

    if (!mimic_decode_frame (mimdec->dec, frame_body,
            GST_BUFFER_DATA (out_buf))) {
      GST_WARNING_OBJECT (mimdec, "mimic_decode_frame error\n");

      gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
      mimdec->have_header = FALSE;

      gst_buffer_unref (out_buf);
      res = GST_FLOW_ERROR;
      goto out;
    }

    if (GST_CLOCK_TIME_IS_VALID (in_time))
      GST_BUFFER_TIMESTAMP (out_buf) = in_time;
    else
      GST_BUFFER_TIMESTAMP (out_buf) = mimdec->current_ts * GST_MSECOND;

    mimic_get_property (mimdec->dec, "width", &width);
    mimic_get_property (mimdec->dec, "height", &height);
    GST_DEBUG_OBJECT (mimdec,
        "got WxH %d x %d payload size %d buffer_size %d",
        width, height, mimdec->payload_size, mimdec->buffer_size);
    caps = gst_caps_new_simple ("video/x-raw-rgb",
        "bpp", G_TYPE_INT, 24,
        "depth", G_TYPE_INT, 24,
        "endianness", G_TYPE_INT, 4321,
        "framerate", GST_TYPE_FRACTION, 7, 1,
        "red_mask", G_TYPE_INT, 16711680,
        "green_mask", G_TYPE_INT, 65280,
        "blue_mask", G_TYPE_INT, 255,
        "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
    gst_buffer_set_caps (out_buf, caps);
    gst_caps_unref (caps);
    GST_OBJECT_UNLOCK (mimdec);
    res = gst_pad_push (mimdec->srcpad, out_buf);
    GST_OBJECT_LOCK (mimdec);

    gst_adapter_flush (mimdec->adapter, mimdec->payload_size);
    mimdec->have_header = FALSE;
  }

out:
  GST_OBJECT_UNLOCK (mimdec);
  gst_object_unref (mimdec);

  return res;
}

static GstStateChangeReturn
gst_mimdec_change_state (GstElement * element, GstStateChange transition)
{
  GstMimDec *mimdec;

  mimdec = GST_MIMDEC (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      GST_OBJECT_LOCK (element);
      if (mimdec->dec != NULL) {
        mimic_close (mimdec->dec);
        mimdec->dec = NULL;
        mimdec->buffer_size = -1;
        mimdec->have_header = FALSE;
        mimdec->payload_size = -1;
        mimdec->current_ts = -1;
      }
      GST_OBJECT_UNLOCK (element);
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      GST_OBJECT_LOCK (element);
      mimdec->need_newsegment = TRUE;
      GST_OBJECT_UNLOCK (element);
      break;
    default:
      break;
  }

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

static gboolean
gst_mimdec_sink_event (GstPad * pad, GstEvent * event)
{
  gboolean res = TRUE;
  GstMimDec *mimdec = GST_MIMDEC (gst_pad_get_parent (pad));

  /*
   * Ignore upstream newsegment event, its EVIL, we should implement
   * proper seeking instead
   */
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NEWSEGMENT:
    {
      gboolean update;
      GstFormat format;
      gdouble rate, arate;
      gint64 start, stop, time;

      gst_event_parse_new_segment_full (event, &update, &rate, &arate,
          &format, &start, &stop, &time);

      /* we need TIME and a positive rate */
      if (format != GST_FORMAT_TIME)
        goto newseg_wrong_format;

      if (rate <= 0.0)
        goto newseg_wrong_rate;

      GST_OBJECT_LOCK (mimdec);
      mimdec->need_newsegment = FALSE;
      GST_OBJECT_UNLOCK (mimdec);

      res = gst_pad_push_event (mimdec->srcpad, event);
    }
      break;
    case GST_EVENT_FLUSH_STOP:
      GST_OBJECT_LOCK (mimdec);
      mimdec->need_newsegment = TRUE;
      GST_OBJECT_UNLOCK (mimdec);

      res = gst_pad_push_event (mimdec->srcpad, event);
      break;
    default:
      res = gst_pad_push_event (mimdec->srcpad, event);
      break;
  }

done:

  gst_object_unref (mimdec);

  return res;

newseg_wrong_format:
  {
    GST_DEBUG_OBJECT (mimdec, "received non TIME newsegment");
    gst_event_unref (event);
    goto done;
  }
newseg_wrong_rate:
  {
    GST_DEBUG_OBJECT (mimdec, "negative rates not supported yet");
    gst_event_unref (event);
    goto done;
  }


}