~ubuntu-branches/ubuntu/precise/gst-plugins-bad0.10/precise-proposed

« back to all changes in this revision

Viewing changes to gst/real/gstrealvideodec.c

Tags: upstream-0.10.5.3
Import upstream version 0.10.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "config.h"
24
24
#endif
25
25
 
 
26
#include "gstreal.h"
26
27
#include "gstrealvideodec.h"
27
28
 
28
29
#include <string.h>
45
46
        "framerate = (fraction) [0/1, MAX], "
46
47
        "width = (int) [ 16, 4096 ], " "height = (int) [ 16, 4096 ] "));
47
48
 
48
 
#ifdef HAVE_CPU_I386
49
 
#define DEFAULT_PATH_RV20 "/usr/lib/win32/drv2.so.6.0"
50
 
#define DEFAULT_PATH_RV30 "/usr/lib/win32/drv3.so.6.0"
51
 
#define DEFAULT_PATH_RV40 "/usr/lib/win32/drv4.so.6.0"
52
 
#endif
53
 
#ifdef HAVE_CPU_X86_64
54
 
#define DEFAULT_PATH_RV20 "/usr/lib/drvc.so"
55
 
#define DEFAULT_PATH_RV30 "/usr/lib/drvc.so"
56
 
#define DEFAULT_PATH_RV40 "/usr/lib/drvc.so"
57
 
#endif
 
49
#define DEFAULT_RV20_NAMES "drv2.so:drv2.so.6.0"
 
50
#define DEFAULT_RV30_NAMES "drvc.so:drv3.so.6.0"
 
51
#define DEFAULT_RV40_NAMES "drvc.so:drv4.so.6.0"
 
52
#define DEFAULT_MAX_ERRORS 25
58
53
 
59
54
enum
60
55
{
61
56
  PROP_0,
62
 
  PROP_PATH_RV20,
63
 
  PROP_PATH_RV30,
64
 
  PROP_PATH_RV40
 
57
  PROP_REAL_CODECS_PATH,
 
58
  PROP_RV20_NAMES,
 
59
  PROP_RV30_NAMES,
 
60
  PROP_RV40_NAMES,
 
61
  PROP_MAX_ERRORS
65
62
};
66
63
 
67
 
 
68
64
GST_BOILERPLATE (GstRealVideoDec, gst_real_video_dec, GstElement,
69
65
    GST_TYPE_ELEMENT);
70
66
 
92
88
} RVOutData;
93
89
 
94
90
static GstFlowReturn
95
 
gst_real_video_dec_alloc_buffer (GstRealVideoDec * dec, GstClockTime timestamp,
96
 
    GstBuffer ** buf)
 
91
gst_real_video_dec_chain (GstPad * pad, GstBuffer * in)
97
92
{
 
93
  GstRealVideoDec *dec;
 
94
  guint8 *data;
 
95
  guint size;
98
96
  GstFlowReturn ret;
99
 
  const guint8 *b;
100
 
  guint8 frame_type;
101
 
  guint16 seq;
102
 
  GstClockTime ts = timestamp;
103
 
 
104
 
  GST_LOG_OBJECT (dec, "timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
105
 
 
106
 
  /* Fix timestamp. */
107
 
  b = gst_adapter_peek (dec->adapter, 4);
108
 
  switch (dec->version) {
109
 
    case GST_REAL_VIDEO_DEC_VERSION_2:
110
 
    {
111
 
 
112
 
      /*
113
 
       * Bit  1- 2: frame type
114
 
       * Bit  3- 9: ?
115
 
       * Bit 10-22: sequence number
116
 
       * Bit 23-32: ?
117
 
       */
118
 
      frame_type = (b[0] >> 6) & 0x03;
119
 
      seq = ((b[1] & 0x7f) << 6) + ((b[2] & 0xfc) >> 2);
120
 
      break;
121
 
    }
122
 
    case GST_REAL_VIDEO_DEC_VERSION_3:
123
 
    {
124
 
      /*
125
 
       * Bit  1- 2: ?
126
 
       * Bit     3: skip packet if 1
127
 
       * Bit  4- 5: frame type
128
 
       * Bit  6-12: ?
129
 
       * Bit 13-25: sequence number
130
 
       * Bit 26-32: ?
131
 
       */
132
 
      frame_type = (b[0] >> 3) & 0x03;
133
 
      seq = ((b[1] & 0x0f) << 9) + (b[2] << 1) + ((b[3] & 0x80) >> 7);
134
 
      break;
135
 
    }
136
 
    case GST_REAL_VIDEO_DEC_VERSION_4:
137
 
    {
138
 
      /*
139
 
       * Bit     1: skip packet if 1
140
 
       * Bit  2- 3: frame type
141
 
       * Bit  4-13: ?
142
 
       * Bit 14-26: sequence number
143
 
       * Bit 27-32: ?
144
 
       */
145
 
      frame_type = (b[0] >> 5) & 0x03;
146
 
      seq = ((b[1] & 0x07) << 10) + (b[2] << 2) + ((b[3] & 0xc0) >> 6);
147
 
      break;
148
 
    }
149
 
    default:
150
 
      goto unknown_version;
151
 
  }
152
 
 
153
 
  GST_LOG_OBJECT (dec, "frame_type:%d", frame_type);
154
 
 
155
 
  switch (frame_type) {
156
 
    case 0:
157
 
    case 1:
158
 
    {
159
 
      /* I frame */
160
 
      timestamp = dec->next_ts;
161
 
      dec->last_ts = dec->next_ts;
162
 
      dec->next_ts = ts;
163
 
      dec->last_seq = dec->next_seq;
164
 
      dec->next_seq = seq;
165
 
 
166
 
      break;
167
 
    }
168
 
    case 2:
169
 
    {
170
 
      /* P frame */
171
 
      timestamp = dec->last_ts = dec->next_ts;
172
 
      if (seq < dec->next_seq)
173
 
        dec->next_ts += (seq + 0x2000 - dec->next_seq) * GST_MSECOND;
174
 
      else
175
 
        dec->next_ts += (seq - dec->next_seq) * GST_MSECOND;
176
 
      dec->last_seq = dec->next_seq;
177
 
      dec->next_seq = seq;
178
 
      break;
179
 
    }
180
 
    case 3:
181
 
    {
182
 
      /* B frame */
183
 
      if (seq < dec->last_seq) {
184
 
        timestamp = (seq + 0x2000 - dec->last_seq) * GST_MSECOND + dec->last_ts;
185
 
      } else {
186
 
        timestamp = (seq - dec->last_seq) * GST_MSECOND + dec->last_ts;
187
 
      }
188
 
 
189
 
      break;
190
 
    }
191
 
    default:
192
 
      goto unknown_frame_type;
193
 
  }
194
 
 
195
 
  ret = gst_pad_alloc_buffer (dec->src, GST_BUFFER_OFFSET_NONE,
196
 
      dec->width * dec->height * 3 / 2, GST_PAD_CAPS (dec->src), buf);
197
 
 
198
 
  if (ret == GST_FLOW_OK)
199
 
    GST_BUFFER_TIMESTAMP (*buf) = timestamp;
200
 
 
201
 
  return ret;
202
 
 
203
 
  /* Errors */
204
 
unknown_version:
205
 
  {
206
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
207
 
        ("Unknown version: %i.", dec->version), (NULL));
208
 
    return GST_FLOW_ERROR;
209
 
  }
210
 
 
211
 
unknown_frame_type:
212
 
  {
213
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Unknown frame type."), (NULL));
214
 
    return GST_FLOW_ERROR;
215
 
  }
216
 
}
217
 
 
218
 
static GstFlowReturn
219
 
gst_real_video_dec_decode (GstRealVideoDec * dec, GstBuffer * in, guint offset)
220
 
{
 
97
  RVInData tin;
 
98
  RVOutData tout;
 
99
  GstClockTime timestamp, duration;
 
100
  GstBuffer *out;
221
101
  guint32 result;
222
 
  GstBuffer *out = NULL;
223
 
  GstFlowReturn ret;
224
 
  guint8 *data, hdr_subseq, hdr_seqnum;
225
 
  guint32 hdr_offset, hdr_length;
226
 
  guint16 n;
227
 
  gboolean bres;
228
 
  guint8 *buf = GST_BUFFER_DATA (in) + offset;
229
 
  guint len = GST_BUFFER_SIZE (in) - offset;
230
 
  GstClockTime timestamp = GST_BUFFER_TIMESTAMP (in);
231
 
 
232
 
  GST_LOG_OBJECT (dec,
233
 
      "Got buffer %p with timestamp %" GST_TIME_FORMAT " offset %d", in,
234
 
      GST_TIME_ARGS (timestamp), offset);
235
 
 
236
 
  /* Subsequence */
237
 
  if (len < 1)
238
 
    goto not_enough_data;
239
 
  if (*buf != 0x40 && *buf != 0x41 && *buf != 0x42 &&
240
 
      *buf != 0x43 && *buf != 0x44 && *buf != 0x45) {
241
 
    hdr_subseq = *buf & 0x7f;
242
 
    buf++;
243
 
    len--;
244
 
    if (hdr_subseq == 64)
245
 
      hdr_subseq = 1;
246
 
  } else {
247
 
    hdr_subseq = 1;
248
 
  }
249
 
 
250
 
  /* Length */
251
 
  if (len < 2)
252
 
    goto not_enough_data;
253
 
  hdr_length = GST_READ_UINT16_BE (buf);
254
 
  if (!(hdr_length & 0xc000)) {
255
 
    if (len < 4)
256
 
      goto not_enough_data;
257
 
    hdr_length = GST_READ_UINT32_BE (buf);
258
 
    buf += 4;
259
 
    len -= 4;
260
 
  } else {
261
 
    hdr_length &= 0x3fff;
262
 
    buf += 2;
263
 
    len -= 2;
264
 
  }
265
 
 
266
 
  /* Offset */
267
 
  if (len < 2)
268
 
    goto not_enough_data;
269
 
  hdr_offset = GST_READ_UINT16_BE (buf);
270
 
  if (!(hdr_offset & 0xc000)) {
271
 
    if (len < 4)
272
 
      goto not_enough_data;
273
 
    hdr_offset = GST_READ_UINT32_BE (buf);
274
 
    buf += 4;
275
 
    len -= 4;
276
 
  } else {
277
 
    hdr_offset &= 0x3fff;
278
 
    buf += 2;
279
 
    len -= 2;
280
 
  }
281
 
 
282
 
  /* Sequence number */
283
 
  if (len < 1)
284
 
    goto not_enough_data;
285
 
  hdr_seqnum = *buf;
286
 
  buf++;
287
 
  len--;
288
 
  if (len < 1)
289
 
    goto not_enough_data;
290
 
 
291
 
  /* Verify the sequence numbers. */
292
 
  if (hdr_subseq == 1) {
293
 
    n = gst_adapter_available_fast (dec->adapter);
294
 
    if (n > 0) {
295
 
      GST_DEBUG_OBJECT (dec, "Dropping data for sequence %i "
296
 
          "because we are already receiving data for sequence %i.",
297
 
          dec->seqnum, hdr_seqnum);
298
 
      gst_adapter_clear (dec->adapter);
299
 
    }
300
 
    dec->seqnum = hdr_seqnum;
301
 
    dec->length = hdr_length;
302
 
    dec->fragment_count = 1;
303
 
  } else {
304
 
    if (dec->seqnum != hdr_seqnum) {
305
 
      GST_DEBUG_OBJECT (dec, "Expected sequence %i, got sequence %i "
306
 
          "(subseq=%i). Dropping packet.", dec->seqnum, hdr_seqnum, hdr_subseq);
307
 
      return GST_FLOW_OK;
308
 
    } else if (dec->subseq + 1 != hdr_subseq) {
309
 
      GST_DEBUG_OBJECT (dec, "Expected subsequence %i, got subseqence %i. "
310
 
          "Dropping packet.", dec->subseq + 1, hdr_subseq);
311
 
      return GST_FLOW_OK;
312
 
    }
313
 
    dec->fragment_count++;
314
 
  }
315
 
  dec->subseq = hdr_subseq;
316
 
 
317
 
  /* Remember the offset */
318
 
  if (sizeof (dec->fragments) < 2 * (dec->fragment_count - 1) + 1)
319
 
    goto too_many_fragments;
320
 
  dec->fragments[2 * (dec->fragment_count - 1)] = 1;
321
 
  dec->fragments[2 * (dec->fragment_count - 1) + 1] =
322
 
      gst_adapter_available (dec->adapter);
323
 
 
324
 
  /* Some buffers need to be skipped. */
325
 
  if (((dec->version == GST_REAL_VIDEO_DEC_VERSION_3) && (*buf & 0x20)) ||
326
 
      ((dec->version == GST_REAL_VIDEO_DEC_VERSION_4) && (*buf & 0x80))) {
327
 
    dec->fragment_count--;
328
 
    dec->length -= len;
329
 
  } else {
330
 
    gst_adapter_push (dec->adapter,
331
 
        gst_buffer_create_sub (in, GST_BUFFER_SIZE (in) - len, len));
332
 
  }
333
 
 
334
 
  /* All bytes received? */
335
 
  n = gst_adapter_available (dec->adapter);
336
 
  GST_LOG_OBJECT (dec, "We know have %d bytes, and we need %d", n, dec->length);
337
 
  if (dec->length <= n) {
338
 
    RVInData tin;
339
 
    RVOutData tout;
340
 
 
341
 
    if ((ret =
342
 
            gst_real_video_dec_alloc_buffer (dec, timestamp,
343
 
                &out)) != GST_FLOW_OK)
344
 
      return ret;
345
 
 
346
 
    /* Decode */
347
 
    tin.datalen = dec->length;
348
 
    tin.interpolate = 0;
349
 
    tin.nfragments = dec->fragment_count - 1;
350
 
    tin.fragments = dec->fragments;
351
 
    tin.flags = 0;
352
 
    tin.timestamp = GST_BUFFER_TIMESTAMP (out);
353
 
    data = gst_adapter_take (dec->adapter, dec->length);
354
 
 
355
 
    result = dec->hooks.transform (
356
 
        (gchar *) data,
357
 
        (gchar *) GST_BUFFER_DATA (out), &tin, &tout, dec->hooks.context);
358
 
 
359
 
    g_free (data);
360
 
    if (result)
361
 
      goto could_not_transform;
362
 
 
363
 
    /* Check for new dimensions */
364
 
    if (tout.frames && ((dec->width != tout.width)
365
 
            || (dec->height != tout.height))) {
366
 
      GstCaps *caps = gst_caps_copy (GST_PAD_CAPS (dec->src));
367
 
      GstStructure *s = gst_caps_get_structure (caps, 0);
368
 
 
369
 
      GST_DEBUG_OBJECT (dec, "New dimensions: %"
370
 
          G_GUINT32_FORMAT " x %" G_GUINT32_FORMAT, tout.width, tout.height);
371
 
      gst_structure_set (s, "width", G_TYPE_LONG, tout.width,
372
 
          "height", G_TYPE_LONG, tout.height, NULL);
373
 
      bres = gst_pad_set_caps (dec->src, caps);
374
 
      gst_caps_unref (caps);
375
 
      if (!bres)
376
 
        goto new_dimensions_failed;
377
 
      dec->width = tout.width;
378
 
      dec->height = tout.height;
379
 
    }
380
 
 
381
 
    GST_DEBUG_OBJECT (dec,
382
 
        "Pushing out buffer with timestamp %" GST_TIME_FORMAT,
383
 
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out)));
384
 
 
385
 
    if ((ret = gst_pad_push (dec->src, out)) != GST_FLOW_OK)
386
 
      goto could_not_push;
387
 
 
388
 
    n = gst_adapter_available (dec->adapter);
389
 
    if (n > 0) {
390
 
      GST_LOG_OBJECT (dec, "Data left in the adapter: %d", n);
391
 
      in = gst_adapter_take_buffer (dec->adapter, n);
392
 
      ret = gst_real_video_dec_decode (dec, in, 0);
393
 
      gst_buffer_unref (in);
394
 
      if (ret != GST_FLOW_OK)
395
 
        return ret;
396
 
    }
397
 
  }
398
 
 
399
 
  return GST_FLOW_OK;
400
 
 
401
 
  /* Errors */
402
 
not_enough_data:
403
 
  {
404
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Not enough data."), (NULL));
405
 
    return GST_FLOW_ERROR;
406
 
  }
407
 
 
408
 
too_many_fragments:
409
 
  {
410
 
    gst_buffer_unref (in);
411
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
412
 
        ("Got more fragments (%u) than can be handled (%u)",
413
 
            dec->fragment_count, (guint) G_N_ELEMENTS (dec->fragments)),
414
 
        (NULL));
415
 
    return GST_FLOW_ERROR;
416
 
  }
417
 
 
418
 
could_not_transform:
419
 
  {
420
 
    gst_buffer_unref (out);
421
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
422
 
        ("Could not decode buffer: %" G_GUINT32_FORMAT, result), (NULL));
423
 
    return GST_FLOW_ERROR;
424
 
  }
425
 
 
426
 
new_dimensions_failed:
427
 
  {
428
 
    gst_buffer_unref (out);
429
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE,
430
 
        ("Could not set new dimensions."), (NULL));
431
 
    return GST_FLOW_ERROR;
432
 
  }
433
 
 
434
 
could_not_push:
435
 
  {
436
 
    GST_DEBUG_OBJECT (dec, "Could not push buffer: %s",
437
 
        gst_flow_get_name (ret));
438
 
    return ret;
439
 
  }
440
 
}
441
 
 
442
 
static GstFlowReturn
443
 
gst_real_video_dec_chain (GstPad * pad, GstBuffer * in)
444
 
{
445
 
  GstRealVideoDec *dec = GST_REAL_VIDEO_DEC (GST_PAD_PARENT (pad));
446
 
  guint8 flags, *buf = GST_BUFFER_DATA (in);
447
 
  guint len = GST_BUFFER_SIZE (in);
448
 
  GstFlowReturn ret;
 
102
  guint frag_count, frag_size;
 
103
 
 
104
  dec = GST_REAL_VIDEO_DEC (GST_PAD_PARENT (pad));
449
105
 
450
106
  if (G_UNLIKELY (dec->hooks.transform == NULL || dec->hooks.module == NULL))
451
107
    goto not_negotiated;
452
108
 
453
 
  /* Flags */
454
 
  if (len < 1)
455
 
    goto not_enough_data;
456
 
  flags = *buf;
457
 
  buf++;
458
 
  len--;
459
 
 
460
 
  if (flags == 0x40) {
461
 
    GST_DEBUG_OBJECT (dec, "Don't know how to handle buffer of type 0x40 "
462
 
        "(size=%i).", len);
463
 
    return GST_FLOW_OK;
464
 
  }
465
 
 
466
 
  ret = gst_real_video_dec_decode (dec, in, 1);
 
109
  data = GST_BUFFER_DATA (in);
 
110
  size = GST_BUFFER_SIZE (in);
 
111
  timestamp = GST_BUFFER_TIMESTAMP (in);
 
112
  duration = GST_BUFFER_DURATION (in);
 
113
 
 
114
  GST_DEBUG_OBJECT (dec, "got buffer of size %u, timestamp %" GST_TIME_FORMAT,
 
115
      size, GST_TIME_ARGS (timestamp));
 
116
 
 
117
  /* alloc output buffer */
 
118
  ret = gst_pad_alloc_buffer (dec->src, GST_BUFFER_OFFSET_NONE,
 
119
      dec->width * dec->height * 3 / 2, GST_PAD_CAPS (dec->src), &out);
 
120
  if (ret != GST_FLOW_OK)
 
121
    goto alloc_failed;
 
122
 
 
123
  GST_BUFFER_TIMESTAMP (out) = timestamp;
 
124
  GST_BUFFER_DURATION (out) = duration;
 
125
 
 
126
  frag_count = *data++;
 
127
  frag_size = (frag_count + 1) * 8;
 
128
  size -= (frag_size + 1);
 
129
 
 
130
  GST_DEBUG_OBJECT (dec, "frag_count %u, frag_size %u, data size %u",
 
131
      frag_count, frag_size, size);
 
132
 
 
133
  /* Decode.
 
134
   *
 
135
   * The Buffers contain
 
136
   *
 
137
   *  0                   1                   2                   3
 
138
   *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 
139
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
140
   * |  nfragments   |   fragment1 ...                               |
 
141
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
142
   * |  ....                                                         |
 
143
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
144
   * |  ...          |   fragment2 ...                               |
 
145
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
146
   *    ....                                                          
 
147
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
148
   * |  ...          |   fragment data                               |
 
149
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
150
   *
 
151
   * nfragments: number of fragments 
 
152
   * fragmentN: 8 bytes of fragment data (nfragements + 1) of them
 
153
   * fragment data: the data of the fragments.
 
154
   */
 
155
  tin.datalen = size;
 
156
  tin.interpolate = 0;
 
157
  tin.nfragments = frag_count;
 
158
  tin.fragments = data;
 
159
  tin.flags = 0;
 
160
  tin.timestamp = timestamp;
 
161
 
 
162
  /* jump over the frag table to the fragments */
 
163
  data += frag_size;
 
164
 
 
165
  result = dec->hooks.transform (
 
166
      (gchar *) data,
 
167
      (gchar *) GST_BUFFER_DATA (out), &tin, &tout, dec->hooks.context);
 
168
  if (result)
 
169
    goto could_not_transform;
 
170
 
 
171
  /* When we decoded a frame, reset the error counter. We only fail after N
 
172
   * consecutive decoding errors. */
 
173
  dec->error_count = 0;
 
174
 
467
175
  gst_buffer_unref (in);
468
 
  if (ret != GST_FLOW_OK)
469
 
    return ret;
470
 
  return GST_FLOW_OK;
 
176
 
 
177
  /* Check for new dimensions */
 
178
  if (tout.frames && ((dec->width != tout.width)
 
179
          || (dec->height != tout.height))) {
 
180
    GstCaps *caps = gst_caps_copy (GST_PAD_CAPS (dec->src));
 
181
    GstStructure *s = gst_caps_get_structure (caps, 0);
 
182
 
 
183
    GST_DEBUG_OBJECT (dec, "New dimensions: %"
 
184
        G_GUINT32_FORMAT " x %" G_GUINT32_FORMAT, tout.width, tout.height);
 
185
 
 
186
    gst_structure_set (s, "width", G_TYPE_INT, (gint) tout.width,
 
187
        "height", G_TYPE_INT, (gint) tout.height, NULL);
 
188
 
 
189
    gst_pad_set_caps (dec->src, caps);
 
190
    gst_buffer_set_caps (out, caps);
 
191
    gst_caps_unref (caps);
 
192
 
 
193
    dec->width = tout.width;
 
194
    dec->height = tout.height;
 
195
    GST_BUFFER_SIZE (out) = dec->width * dec->height * 3 / 2;
 
196
  }
 
197
 
 
198
  GST_DEBUG_OBJECT (dec,
 
199
      "Pushing out buffer with timestamp %" GST_TIME_FORMAT,
 
200
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out)));
 
201
 
 
202
  if ((ret = gst_pad_push (dec->src, out)) != GST_FLOW_OK)
 
203
    goto could_not_push;
 
204
 
 
205
  return ret;
471
206
 
472
207
  /* Errors */
473
 
not_enough_data:
474
 
  {
475
 
    GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Not enough data."), (NULL));
476
 
    gst_buffer_unref (in);
477
 
    return GST_FLOW_ERROR;
478
 
  }
479
208
not_negotiated:
480
209
  {
481
210
    GST_WARNING_OBJECT (dec, "decoder not open, probably no input caps set "
483
212
    gst_buffer_unref (in);
484
213
    return GST_FLOW_NOT_NEGOTIATED;
485
214
  }
 
215
alloc_failed:
 
216
  {
 
217
    GST_DEBUG_OBJECT (dec, "buffer alloc failed: %s", gst_flow_get_name (ret));
 
218
    gst_buffer_unref (in);
 
219
    return ret;
 
220
  }
 
221
could_not_transform:
 
222
  {
 
223
    gst_buffer_unref (out);
 
224
    gst_buffer_unref (in);
 
225
 
 
226
    dec->error_count++;
 
227
 
 
228
    if (dec->max_errors && dec->error_count >= dec->max_errors) {
 
229
      GST_ELEMENT_ERROR (dec, STREAM, DECODE,
 
230
          ("Could not decode buffer: %" G_GUINT32_FORMAT, result), (NULL));
 
231
      return GST_FLOW_ERROR;
 
232
    } else {
 
233
      GST_ELEMENT_WARNING (dec, STREAM, DECODE,
 
234
          ("Could not decode buffer: %" G_GUINT32_FORMAT, result), (NULL));
 
235
      return GST_FLOW_OK;
 
236
    }
 
237
  }
 
238
could_not_push:
 
239
  {
 
240
    GST_DEBUG_OBJECT (dec, "Could not push buffer: %s",
 
241
        gst_flow_get_name (ret));
 
242
    return ret;
 
243
  }
486
244
}
487
245
 
488
246
static gboolean
489
247
gst_real_video_dec_activate_push (GstPad * pad, gboolean active)
490
248
{
491
 
  GstRealVideoDec *dec = GST_REAL_VIDEO_DEC (GST_PAD_PARENT (pad));
492
 
 
493
 
  gst_adapter_clear (dec->adapter);
494
 
 
495
249
  return TRUE;
496
250
}
497
251
 
528
282
  GST_WRITE_UINT16_LE (data + 6, 0);
529
283
  GST_WRITE_UINT32_LE (data + 8, 0);
530
284
  GST_WRITE_UINT32_LE (data + 12, subformat);
531
 
  GST_WRITE_UINT32_LE (data + 16, 0);
 
285
  GST_WRITE_UINT32_LE (data + 16, 1);
532
286
  GST_WRITE_UINT32_LE (data + 20, format);
 
287
 
533
288
  res = hooks.init (&data, &hooks.context);
534
289
  if (res)
535
290
    goto could_not_initialize;
538
293
    GstBuffer *buf;
539
294
    guint32 *msgdata;
540
295
    guint i;
 
296
    guint8 *bufdata;
 
297
    guint bufsize;
541
298
    struct
542
299
    {
543
300
      guint32 type;
548
305
 
549
306
    buf = g_value_peek_pointer (v);
550
307
 
551
 
    GST_LOG_OBJECT (dec, "Creating custom message of length %d",
552
 
        GST_BUFFER_SIZE (buf));
553
 
 
554
 
    msgdata = g_new0 (guint32, GST_BUFFER_SIZE (buf) + 2);
 
308
    bufdata = GST_BUFFER_DATA (buf);
 
309
    bufsize = GST_BUFFER_SIZE (buf);
 
310
 
 
311
    /* skip format and subformat */
 
312
    bufdata += 8;
 
313
    bufsize -= 8;
 
314
 
 
315
    GST_LOG_OBJECT (dec, "Creating custom message of length %d", bufsize);
 
316
 
 
317
    msgdata = g_new0 (guint32, bufsize + 2);
555
318
    if (!msgdata)
556
319
      goto could_not_allocate;
557
320
 
562
325
      msg.extra[i] = 0;
563
326
    msgdata[0] = width;
564
327
    msgdata[1] = height;
565
 
    for (i = 0; i < GST_BUFFER_SIZE (buf); i++)
566
 
      msgdata[i + 2] = 4 * GST_BUFFER_DATA (buf)[i];
 
328
    for (i = 0; i < bufsize; i++)
 
329
      msgdata[i + 2] = 4 * (guint32) bufdata[i];
567
330
 
568
331
    res = hooks.custom_message (&msg, hooks.context);
569
332
 
595
358
 
596
359
missing_keys:
597
360
  {
598
 
    GST_DEBUG_OBJECT (dec, "Could not find all necessary keys in structure.");
 
361
    GST_ERROR_OBJECT (dec, "Could not find all necessary keys in structure.");
599
362
    return FALSE;
600
363
  }
601
364
 
602
365
could_not_initialize:
603
366
  {
604
367
    close_library (hooks);
605
 
    GST_DEBUG_OBJECT (dec, "Initialization of REAL driver failed (%i).", res);
 
368
    GST_ERROR_OBJECT (dec, "Initialization of REAL driver failed (%i).", res);
606
369
    return FALSE;
607
370
  }
608
371
 
609
372
could_not_allocate:
610
373
  {
611
374
    close_library (hooks);
612
 
    GST_DEBUG_OBJECT (dec, "Could not allocate memory.");
 
375
    GST_ERROR_OBJECT (dec, "Could not allocate memory.");
613
376
    return FALSE;
614
377
  }
615
378
 
616
379
could_not_send_message:
617
380
  {
618
381
    close_library (hooks);
619
 
    GST_DEBUG_OBJECT (dec, "Failed to send custom message needed for "
 
382
    GST_ERROR_OBJECT (dec, "Failed to send custom message needed for "
620
383
        "initialization (%i).", res);
621
384
    return FALSE;
622
385
  }
624
387
could_not_set_caps:
625
388
  {
626
389
    close_library (hooks);
627
 
    GST_DEBUG_OBJECT (dec, "Could not convince peer to accept dimensions "
 
390
    GST_ERROR_OBJECT (dec, "Could not convince peer to accept dimensions "
628
391
        "%i x %i.", dec->width, dec->height);
629
392
    return FALSE;
630
393
  }
637
400
    GstRealVideoDecVersion version)
638
401
{
639
402
  gpointer rv_custom_msg, rv_free, rv_init, rv_transform;
640
 
  GModule *module;
641
 
  gchar *path = NULL;
 
403
  GModule *module = NULL;
 
404
  gchar *path, *names;
 
405
  gchar **split_names, **split_path;
 
406
  int i, j;
642
407
 
643
408
  GST_DEBUG_OBJECT (dec,
644
409
      "Attempting to open shared library for real video version %d", version);
645
410
 
646
 
  /* FIXME : Search for the correct library in various places if dec->path_rv20
647
 
   *  isn't set explicitely !
648
 
   * Library names can also be different (ex : drv30.so vs drvc.so)
649
 
   */
 
411
  path = dec->real_codecs_path ? dec->real_codecs_path :
 
412
      DEFAULT_REAL_CODECS_PATH;
650
413
 
651
414
  switch (version) {
652
415
    case GST_REAL_VIDEO_DEC_VERSION_2:
653
 
    {
654
 
      if (dec->path_rv20)
655
 
        path = dec->path_rv20;
656
 
      else if (g_file_test (DEFAULT_PATH_RV20, G_FILE_TEST_EXISTS))
657
 
        path = DEFAULT_PATH_RV20;
658
 
      else if (g_file_test ("/usr/lib/drv2.so.6.0", G_FILE_TEST_EXISTS))
659
 
        path = "/usr/lib/drv2.so.6.0";
660
 
      else
661
 
        goto no_known_libraries;
 
416
      names = dec->rv20_names ? dec->rv20_names : DEFAULT_RV20_NAMES;
662
417
      break;
663
 
    }
664
418
    case GST_REAL_VIDEO_DEC_VERSION_3:
665
 
    {
666
 
      if (dec->path_rv30)
667
 
        path = dec->path_rv30;
668
 
      else if (g_file_test (DEFAULT_PATH_RV30, G_FILE_TEST_EXISTS))
669
 
        path = DEFAULT_PATH_RV30;
670
 
      else if (g_file_test ("/usr/lib/drv3.so.6.0", G_FILE_TEST_EXISTS))
671
 
        path = "/usr/lib/drv3.so.6.0";
672
 
      else
673
 
        goto no_known_libraries;
 
419
      names = dec->rv30_names ? dec->rv30_names : DEFAULT_RV30_NAMES;
674
420
      break;
675
 
    }
676
421
    case GST_REAL_VIDEO_DEC_VERSION_4:
677
 
    {
678
 
      if (dec->path_rv40)
679
 
        path = dec->path_rv40;
680
 
      else if (g_file_test (DEFAULT_PATH_RV40, G_FILE_TEST_EXISTS))
681
 
        path = DEFAULT_PATH_RV40;
682
 
      else if (g_file_test ("/usr/lib/drv4.so.6.0", G_FILE_TEST_EXISTS))
683
 
        path = "/usr/lib/drv4.so.6.0";
684
 
      else
685
 
        goto no_known_libraries;
 
422
      names = dec->rv40_names ? dec->rv40_names : DEFAULT_RV40_NAMES;
686
423
      break;
687
 
    }
688
424
    default:
689
425
      goto unknown_version;
690
426
  }
691
427
 
692
 
  GST_LOG_OBJECT (dec, "Trying to open '%s'", path);
693
 
  hooks->module = g_module_open (path, G_MODULE_BIND_LAZY);
694
 
 
695
 
  if (hooks->module == NULL)
 
428
  split_path = g_strsplit (path, ":", 0);
 
429
  split_names = g_strsplit (names, ":", 0);
 
430
 
 
431
  for (i = 0; split_path[i]; i++) {
 
432
    for (j = 0; split_names[j]; j++) {
 
433
      gchar *codec = g_strconcat (split_path[i], "/", split_names[j], NULL);
 
434
 
 
435
      module = g_module_open (codec, G_MODULE_BIND_LAZY);
 
436
      g_free (codec);
 
437
      if (module)
 
438
        goto codec_search_done;
 
439
    }
 
440
  }
 
441
 
 
442
codec_search_done:
 
443
  g_strfreev (split_path);
 
444
  g_strfreev (split_names);
 
445
 
 
446
  if (module == NULL)
696
447
    goto could_not_open;
697
448
 
698
 
  module = hooks->module;
699
 
 
700
449
  /* First try opening legacy symbols, if that fails try loading new symbols */
701
450
  if (g_module_symbol (module, "RV20toYUV420Init", &rv_init) &&
702
451
      g_module_symbol (module, "RV20toYUV420Free", &rv_free) &&
712
461
    goto could_not_load;
713
462
  }
714
463
 
715
 
  hooks->init = rv_init;
716
 
  hooks->free = rv_free;
717
 
  hooks->transform = rv_transform;
718
 
  hooks->custom_message = rv_custom_msg;
 
464
  hooks->init = (GstRealVideoDecInitFunc) rv_init;
 
465
  hooks->free = (GstRealVideoDecFreeFunc) rv_free;
 
466
  hooks->transform = (GstRealVideoDecTransformFunc) rv_transform;
 
467
  hooks->custom_message = (GstRealVideoDecMessageFunc) rv_custom_msg;
 
468
  hooks->module = module;
 
469
 
 
470
  dec->error_count = 0;
719
471
 
720
472
  return TRUE;
721
473
 
722
 
no_known_libraries:
723
 
  {
724
 
    GST_ELEMENT_ERROR (dec, LIBRARY, INIT,
725
 
        ("Couldn't find a realvideo shared library for version %d",
726
 
            version), (NULL));
727
 
    return FALSE;
728
 
  }
729
 
 
730
474
unknown_version:
731
475
  {
732
476
    GST_ERROR_OBJECT (dec, "Cannot handle version %i.", version);
735
479
 
736
480
could_not_open:
737
481
  {
738
 
    GST_ERROR_OBJECT (dec, "Could not open library '%s':%s", path,
739
 
        g_module_error ());
 
482
    GST_ERROR_OBJECT (dec, "Could not open library '%s' in '%s': %s", names,
 
483
        path, g_module_error ());
740
484
    return FALSE;
741
485
  }
742
486
 
776
520
  gst_pad_use_fixed_caps (dec->src);
777
521
  gst_element_add_pad (GST_ELEMENT (dec), dec->src);
778
522
 
779
 
  dec->adapter = gst_adapter_new ();
 
523
  dec->max_errors = DEFAULT_MAX_ERRORS;
 
524
  dec->error_count = 0;
780
525
}
781
526
 
782
527
static void
794
539
{
795
540
  GstRealVideoDec *dec = GST_REAL_VIDEO_DEC (object);
796
541
 
797
 
  if (dec->adapter) {
798
 
    g_object_unref (G_OBJECT (dec->adapter));
799
 
    dec->adapter = NULL;
800
 
  }
801
 
 
802
542
  close_library (dec->hooks);
803
543
  memset (&dec->hooks, 0, sizeof (dec->hooks));
804
544
 
805
 
  if (dec->path_rv20) {
806
 
    g_free (dec->path_rv20);
807
 
    dec->path_rv20 = NULL;
808
 
  }
809
 
  if (dec->path_rv30) {
810
 
    g_free (dec->path_rv30);
811
 
    dec->path_rv30 = NULL;
812
 
  }
813
 
  if (dec->path_rv40) {
814
 
    g_free (dec->path_rv40);
815
 
    dec->path_rv40 = NULL;
 
545
  if (dec->real_codecs_path) {
 
546
    g_free (dec->real_codecs_path);
 
547
    dec->real_codecs_path = NULL;
 
548
  }
 
549
  if (dec->rv20_names) {
 
550
    g_free (dec->rv20_names);
 
551
    dec->rv20_names = NULL;
 
552
  }
 
553
  if (dec->rv30_names) {
 
554
    g_free (dec->rv30_names);
 
555
    dec->rv30_names = NULL;
 
556
  }
 
557
  if (dec->rv40_names) {
 
558
    g_free (dec->rv40_names);
 
559
    dec->rv40_names = NULL;
816
560
  }
817
561
 
818
562
  G_OBJECT_CLASS (parent_class)->finalize (object);
829
573
   */
830
574
 
831
575
  switch (prop_id) {
832
 
    case PROP_PATH_RV20:
833
 
      if (dec->path_rv20)
834
 
        g_free (dec->path_rv20);
835
 
      dec->path_rv20 = g_value_dup_string (value);
836
 
      break;
837
 
    case PROP_PATH_RV30:
838
 
      if (dec->path_rv30)
839
 
        g_free (dec->path_rv30);
840
 
      dec->path_rv30 = g_value_dup_string (value);
841
 
      break;
842
 
    case PROP_PATH_RV40:
843
 
      if (dec->path_rv40)
844
 
        g_free (dec->path_rv40);
845
 
      dec->path_rv40 = g_value_dup_string (value);
 
576
    case PROP_REAL_CODECS_PATH:
 
577
      if (dec->real_codecs_path)
 
578
        g_free (dec->real_codecs_path);
 
579
      dec->real_codecs_path = g_value_dup_string (value);
 
580
      break;
 
581
    case PROP_RV20_NAMES:
 
582
      if (dec->rv20_names)
 
583
        g_free (dec->rv20_names);
 
584
      dec->rv20_names = g_value_dup_string (value);
 
585
      break;
 
586
    case PROP_RV30_NAMES:
 
587
      if (dec->rv30_names)
 
588
        g_free (dec->rv30_names);
 
589
      dec->rv30_names = g_value_dup_string (value);
 
590
      break;
 
591
    case PROP_RV40_NAMES:
 
592
      if (dec->rv40_names)
 
593
        g_free (dec->rv40_names);
 
594
      dec->rv40_names = g_value_dup_string (value);
 
595
      break;
 
596
    case PROP_MAX_ERRORS:
 
597
      dec->max_errors = g_value_get_int (value);
846
598
      break;
847
599
    default:
848
600
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
857
609
  GstRealVideoDec *dec = GST_REAL_VIDEO_DEC (object);
858
610
 
859
611
  switch (prop_id) {
860
 
    case PROP_PATH_RV20:
861
 
      g_value_set_string (value, dec->path_rv20 ? dec->path_rv20 :
862
 
          DEFAULT_PATH_RV20);
863
 
      break;
864
 
    case PROP_PATH_RV30:
865
 
      g_value_set_string (value, dec->path_rv30 ? dec->path_rv30 :
866
 
          DEFAULT_PATH_RV30);
867
 
      break;
868
 
    case PROP_PATH_RV40:
869
 
      g_value_set_string (value, dec->path_rv40 ? dec->path_rv40 :
870
 
          DEFAULT_PATH_RV40);
 
612
    case PROP_REAL_CODECS_PATH:
 
613
      g_value_set_string (value, dec->real_codecs_path ? dec->real_codecs_path
 
614
          : DEFAULT_REAL_CODECS_PATH);
 
615
      break;
 
616
    case PROP_RV20_NAMES:
 
617
      g_value_set_string (value, dec->rv20_names ? dec->rv20_names :
 
618
          DEFAULT_RV20_NAMES);
 
619
      break;
 
620
    case PROP_RV30_NAMES:
 
621
      g_value_set_string (value, dec->rv30_names ? dec->rv30_names :
 
622
          DEFAULT_RV30_NAMES);
 
623
      break;
 
624
    case PROP_RV40_NAMES:
 
625
      g_value_set_string (value, dec->rv40_names ? dec->rv40_names :
 
626
          DEFAULT_RV40_NAMES);
 
627
      break;
 
628
    case PROP_MAX_ERRORS:
 
629
      g_value_set_int (value, dec->max_errors);
871
630
      break;
872
631
    default:
873
632
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
884
643
  object_class->get_property = gst_real_video_dec_get_property;
885
644
  object_class->finalize = gst_real_video_dec_finalize;
886
645
 
887
 
  g_object_class_install_property (object_class, PROP_PATH_RV20,
888
 
      g_param_spec_string ("path_rv20", "Path to rv20 driver",
889
 
          "Path to rv20 driver", DEFAULT_PATH_RV20, G_PARAM_READWRITE));
890
 
  g_object_class_install_property (object_class, PROP_PATH_RV30,
891
 
      g_param_spec_string ("path_rv30", "Path to rv30 driver",
892
 
          "Path to rv30 driver", DEFAULT_PATH_RV30, G_PARAM_READWRITE));
893
 
  g_object_class_install_property (object_class, PROP_PATH_RV40,
894
 
      g_param_spec_string ("path_rv40", "Path to rv40 driver",
895
 
          "Path to rv40 driver", DEFAULT_PATH_RV40, G_PARAM_READWRITE));
 
646
  g_object_class_install_property (object_class, PROP_REAL_CODECS_PATH,
 
647
      g_param_spec_string ("real-codecs-path",
 
648
          "Path where to search for RealPlayer codecs",
 
649
          "Path where to search for RealPlayer codecs",
 
650
          DEFAULT_REAL_CODECS_PATH, G_PARAM_READWRITE));
 
651
  g_object_class_install_property (object_class, PROP_RV20_NAMES,
 
652
      g_param_spec_string ("rv20-names", "Names of rv20 driver",
 
653
          "Names of rv20 driver", DEFAULT_RV20_NAMES, G_PARAM_READWRITE));
 
654
  g_object_class_install_property (object_class, PROP_RV30_NAMES,
 
655
      g_param_spec_string ("rv30-names", "Names of rv30 driver",
 
656
          "Names of rv30 driver", DEFAULT_RV30_NAMES, G_PARAM_READWRITE));
 
657
  g_object_class_install_property (object_class, PROP_RV40_NAMES,
 
658
      g_param_spec_string ("rv40-names", "Names of rv40 driver",
 
659
          "Names of rv40 driver", DEFAULT_RV40_NAMES, G_PARAM_READWRITE));
 
660
  g_object_class_install_property (object_class, PROP_MAX_ERRORS,
 
661
      g_param_spec_int ("max-errors", "Max errors",
 
662
          "Maximum number of consecutive errors (0 = unlimited)",
 
663
          0, G_MAXINT, DEFAULT_MAX_ERRORS, G_PARAM_READWRITE));
896
664
 
897
665
  GST_DEBUG_CATEGORY_INIT (realvideode_debug, "realvideodec", 0,
898
666
      "RealVideo decoder");