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

« back to all changes in this revision

Viewing changes to sys/vdpau/gstvdpoutputsrcpad.c

Tags: upstream-0.10.17.2
Import upstream version 0.10.17.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * GStreamer
 
3
 * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "gstvdpvideobuffer.h"
 
22
 
 
23
#include "gstvdpoutputsrcpad.h"
 
24
 
 
25
GST_DEBUG_CATEGORY_STATIC (gst_vdp_output_src_pad_debug);
 
26
#define GST_CAT_DEFAULT gst_vdp_output_src_pad_debug
 
27
 
 
28
enum
 
29
{
 
30
  PROP_0,
 
31
  PROP_DISPLAY,
 
32
  PROP_TEMPL_CAPS
 
33
};
 
34
 
 
35
typedef enum _GstVdpOutputSrcPadFormat GstVdpOutputSrcPadFormat;
 
36
 
 
37
enum _GstVdpOutputSrcPadFormat
 
38
{
 
39
  GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB,
 
40
  GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU
 
41
};
 
42
 
 
43
struct _GstVdpOutputSrcPad
 
44
{
 
45
  GstPad pad;
 
46
 
 
47
  GstCaps *caps;
 
48
  GstVdpDevice *device;
 
49
 
 
50
  GstVdpOutputSrcPadFormat output_format;
 
51
  VdpRGBAFormat rgba_format;
 
52
  gint width, height;
 
53
 
 
54
  /* properties */
 
55
  gchar *display;
 
56
  GstCaps *templ_caps;
 
57
};
 
58
 
 
59
struct _GstVdpOutputSrcPadClass
 
60
{
 
61
  GstPadClass pad_class;
 
62
};
 
63
 
 
64
#define DEBUG_INIT(bla) \
 
65
GST_DEBUG_CATEGORY_INIT (gst_vdp_output_src_pad_debug, "vdpoutputsrcpad", 0, "GstVdpOutputSrcPad");
 
66
 
 
67
G_DEFINE_TYPE_WITH_CODE (GstVdpOutputSrcPad, gst_vdp_output_src_pad,
 
68
    GST_TYPE_PAD, DEBUG_INIT ());
 
69
 
 
70
GstFlowReturn
 
71
gst_vdp_output_src_pad_push (GstVdpOutputSrcPad * vdp_pad,
 
72
    GstVdpOutputBuffer * output_buf)
 
73
{
 
74
  GstPad *pad;
 
75
  GstBuffer *out_buf;
 
76
 
 
77
  g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
 
78
  g_return_val_if_fail (GST_IS_VDP_OUTPUT_BUFFER (output_buf), GST_FLOW_ERROR);
 
79
 
 
80
  pad = (GstPad *) vdp_pad;
 
81
 
 
82
  if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
 
83
    return GST_FLOW_NOT_NEGOTIATED;
 
84
 
 
85
  switch (vdp_pad->output_format) {
 
86
    case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
 
87
    {
 
88
      guint size;
 
89
 
 
90
      if (!gst_vdp_output_buffer_calculate_size (output_buf, &size)) {
 
91
        GST_ERROR_OBJECT (vdp_pad, "Couldn't calculate buffer size for caps");
 
92
        gst_buffer_unref (GST_BUFFER_CAST (output_buf));
 
93
        return GST_FLOW_ERROR;
 
94
      }
 
95
 
 
96
      out_buf = gst_buffer_new_and_alloc (size);
 
97
      gst_buffer_set_caps (out_buf, GST_PAD_CAPS (vdp_pad));
 
98
 
 
99
      if (!gst_vdp_output_buffer_download (output_buf, out_buf)) {
 
100
        GST_ERROR_OBJECT (vdp_pad,
 
101
            "Couldn't convert from GstVdpVideoBuffer to the requested format");
 
102
        gst_buffer_unref (GST_BUFFER_CAST (output_buf));
 
103
        gst_buffer_unref (out_buf);
 
104
      }
 
105
 
 
106
      gst_buffer_copy_metadata (out_buf, (const GstBuffer *) output_buf,
 
107
          GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
 
108
      gst_buffer_unref (GST_BUFFER_CAST (output_buf));
 
109
      break;
 
110
    }
 
111
 
 
112
    case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
 
113
    {
 
114
      out_buf = GST_BUFFER_CAST (output_buf);
 
115
      break;
 
116
    }
 
117
 
 
118
    default:
 
119
      g_assert_not_reached ();
 
120
      break;
 
121
  }
 
122
 
 
123
  gst_buffer_set_caps (out_buf, GST_PAD_CAPS (vdp_pad));
 
124
 
 
125
  return gst_pad_push (pad, out_buf);
 
126
}
 
127
 
 
128
static void
 
129
gst_vdp_output_src_pad_update_caps (GstVdpOutputSrcPad * vdp_pad)
 
130
{
 
131
  GstCaps *allowed_caps;
 
132
 
 
133
  allowed_caps = gst_vdp_output_buffer_get_allowed_caps (vdp_pad->device);
 
134
 
 
135
  if (vdp_pad->caps)
 
136
    gst_caps_unref (vdp_pad->caps);
 
137
 
 
138
  if (vdp_pad->templ_caps) {
 
139
    vdp_pad->caps = gst_caps_intersect (allowed_caps, vdp_pad->templ_caps);
 
140
    gst_caps_unref (allowed_caps);
 
141
  } else
 
142
    vdp_pad->caps = allowed_caps;
 
143
}
 
144
 
 
145
GstFlowReturn
 
146
gst_vdp_output_src_pad_alloc_buffer (GstVdpOutputSrcPad * vdp_pad,
 
147
    GstVdpOutputBuffer ** output_buf)
 
148
{
 
149
  GstCaps *caps;
 
150
  GstFlowReturn ret;
 
151
 
 
152
  g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
 
153
 
 
154
  caps = GST_PAD_CAPS (vdp_pad);
 
155
  if (!caps)
 
156
    return GST_FLOW_NOT_NEGOTIATED;
 
157
 
 
158
  switch (vdp_pad->output_format) {
 
159
    case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
 
160
    {
 
161
      GstVdpDevice *device;
 
162
 
 
163
      GstBuffer *neg_buf;
 
164
      GstStructure *structure;
 
165
      gint width, height;
 
166
 
 
167
      if (G_UNLIKELY (!vdp_pad->device)) {
 
168
        vdp_pad->device = gst_vdp_get_device (vdp_pad->display);
 
169
        if (G_UNLIKELY (!vdp_pad->device))
 
170
          goto device_error;
 
171
 
 
172
        gst_vdp_output_src_pad_update_caps (vdp_pad);
 
173
      }
 
174
      device = vdp_pad->device;
 
175
 
 
176
      /* negotiate */
 
177
      ret = gst_pad_alloc_buffer (GST_PAD_CAST (vdp_pad),
 
178
          GST_BUFFER_OFFSET_NONE, 0, caps, &neg_buf);
 
179
      if (ret != GST_FLOW_OK)
 
180
        return ret;
 
181
 
 
182
      structure = gst_caps_get_structure (GST_BUFFER_CAPS (neg_buf), 0);
 
183
      if (!gst_structure_get_int (structure, "width", &width) ||
 
184
          !gst_structure_get_int (structure, "height", &height)) {
 
185
        gst_buffer_unref (neg_buf);
 
186
        GST_ERROR_OBJECT (vdp_pad,
 
187
            "Sink element allocated buffer with invalid caps");
 
188
        return GST_FLOW_ERROR;
 
189
      }
 
190
 
 
191
      *output_buf = gst_vdp_output_buffer_new (device, vdp_pad->rgba_format,
 
192
          width, height);
 
193
      if (!*output_buf) {
 
194
        gst_buffer_unref (neg_buf);
 
195
        goto output_buf_error;
 
196
      }
 
197
 
 
198
      gst_buffer_set_caps (GST_BUFFER_CAST (*output_buf),
 
199
          GST_BUFFER_CAPS (neg_buf));
 
200
      gst_buffer_unref (neg_buf);
 
201
 
 
202
      break;
 
203
    }
 
204
 
 
205
    case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
 
206
    {
 
207
      GstStructure *structure;
 
208
 
 
209
      ret = gst_pad_alloc_buffer ((GstPad *) vdp_pad, 0, 0, caps,
 
210
          (GstBuffer **) output_buf);
 
211
      if (ret != GST_FLOW_OK)
 
212
        return ret;
 
213
 
 
214
      structure = gst_caps_get_structure (GST_BUFFER_CAPS (*output_buf), 0);
 
215
      if (!gst_structure_has_name (structure, "video/x-vdpau-output"))
 
216
        goto wrong_caps;
 
217
 
 
218
      if (G_UNLIKELY (!vdp_pad->device)) {
 
219
        vdp_pad->device =
 
220
            g_object_ref (GST_VDP_VIDEO_BUFFER (*output_buf)->device);
 
221
 
 
222
        gst_vdp_output_src_pad_update_caps (vdp_pad);
 
223
      }
 
224
 
 
225
      break;
 
226
    }
 
227
 
 
228
    default:
 
229
      g_assert_not_reached ();
 
230
      break;
 
231
  }
 
232
 
 
233
  return GST_FLOW_OK;
 
234
 
 
235
device_error:
 
236
  GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpDevice");
 
237
  return GST_FLOW_ERROR;
 
238
 
 
239
output_buf_error:
 
240
  GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpVideoBuffer");
 
241
  return GST_FLOW_ERROR;
 
242
 
 
243
wrong_caps:
 
244
  GST_ERROR_OBJECT (vdp_pad, "Sink element returned buffer with wrong caps");
 
245
  gst_buffer_unref (GST_BUFFER_CAST (*output_buf));
 
246
  return GST_FLOW_ERROR;
 
247
}
 
248
 
 
249
gboolean
 
250
gst_vdp_output_src_pad_set_caps (GstVdpOutputSrcPad * vdp_pad, GstCaps * caps)
 
251
{
 
252
  const GstStructure *structure;
 
253
 
 
254
  g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), FALSE);
 
255
 
 
256
  if (G_UNLIKELY (!caps))
 
257
    return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
 
258
 
 
259
  if (G_UNLIKELY (!GST_IS_CAPS (caps) || !gst_caps_is_fixed (caps)))
 
260
    return FALSE;
 
261
 
 
262
  structure = gst_caps_get_structure (caps, 0);
 
263
  if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
 
264
    if (!gst_vdp_caps_to_rgba_format (caps, &vdp_pad->rgba_format))
 
265
      return FALSE;
 
266
    if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
 
267
      return FALSE;
 
268
    if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
 
269
      return FALSE;
 
270
 
 
271
    vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB;
 
272
  } else if (gst_structure_has_name (structure, "video/x-vdpau-output")) {
 
273
    if (!gst_structure_get_int (structure, "rgba-format",
 
274
            (gint *) & vdp_pad->rgba_format))
 
275
      return FALSE;
 
276
    if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
 
277
      return FALSE;
 
278
    if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
 
279
      return FALSE;
 
280
 
 
281
    vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU;
 
282
  } else
 
283
    return FALSE;
 
284
 
 
285
  return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
 
286
}
 
287
 
 
288
GstFlowReturn
 
289
gst_vdp_output_src_pad_get_device (GstVdpOutputSrcPad * vdp_pad,
 
290
    GstVdpDevice ** device)
 
291
{
 
292
  g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), FALSE);
 
293
 
 
294
  if (G_UNLIKELY (!vdp_pad->device)) {
 
295
    GstCaps *src_caps;
 
296
    GstStructure *structure;
 
297
 
 
298
    src_caps = gst_pad_get_allowed_caps (GST_PAD (vdp_pad));
 
299
    gst_pad_fixate_caps (GST_PAD (vdp_pad), src_caps);
 
300
 
 
301
    if (gst_caps_is_empty (src_caps)) {
 
302
      gst_caps_unref (src_caps);
 
303
      return GST_FLOW_NOT_NEGOTIATED;
 
304
    }
 
305
 
 
306
    structure = gst_caps_get_structure (src_caps, 0);
 
307
    if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
 
308
      vdp_pad->device = gst_vdp_get_device (vdp_pad->display);
 
309
      if (G_UNLIKELY (!vdp_pad->device))
 
310
        goto device_error;
 
311
    }
 
312
 
 
313
    else {
 
314
      GstFlowReturn ret;
 
315
      GstBuffer *buf;
 
316
 
 
317
      ret = gst_pad_alloc_buffer (GST_PAD (vdp_pad), 0, 0, src_caps, &buf);
 
318
      if (ret != GST_FLOW_OK) {
 
319
        gst_caps_unref (src_caps);
 
320
        return ret;
 
321
      }
 
322
 
 
323
      if (!gst_caps_is_equal_fixed (src_caps, GST_BUFFER_CAPS (buf))) {
 
324
        gst_caps_unref (src_caps);
 
325
        gst_buffer_unref (buf);
 
326
        goto wrong_caps;
 
327
      }
 
328
 
 
329
      vdp_pad->device = g_object_ref (GST_VDP_OUTPUT_BUFFER (buf)->device);
 
330
    }
 
331
 
 
332
    gst_caps_unref (src_caps);
 
333
  }
 
334
 
 
335
  *device = vdp_pad->device;
 
336
  return GST_FLOW_OK;
 
337
 
 
338
device_error:
 
339
  GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpDevice");
 
340
  return GST_FLOW_ERROR;
 
341
 
 
342
wrong_caps:
 
343
  GST_ERROR_OBJECT (vdp_pad, "Sink element returned buffer with wrong caps");
 
344
  return GST_FLOW_ERROR;
 
345
}
 
346
 
 
347
static GstCaps *
 
348
gst_vdp_output_src_pad_getcaps (GstPad * pad)
 
349
{
 
350
  GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) pad;
 
351
 
 
352
  if (vdp_pad->caps)
 
353
    return gst_caps_ref (vdp_pad->caps);
 
354
 
 
355
  return gst_caps_ref (vdp_pad->templ_caps);
 
356
}
 
357
 
 
358
static gboolean
 
359
gst_vdp_output_src_pad_activate_push (GstPad * pad, gboolean active)
 
360
{
 
361
  GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
 
362
 
 
363
  if (!active) {
 
364
    if (vdp_pad->device)
 
365
      g_object_unref (vdp_pad->device);
 
366
    vdp_pad->device = NULL;
 
367
 
 
368
    if (vdp_pad->caps)
 
369
      gst_caps_unref (vdp_pad->caps);
 
370
    vdp_pad->caps = NULL;
 
371
  }
 
372
 
 
373
  return TRUE;
 
374
}
 
375
 
 
376
GstVdpOutputSrcPad *
 
377
gst_vdp_output_src_pad_new (GstCaps * templ_caps)
 
378
{
 
379
  return g_object_new (GST_TYPE_VDP_OUTPUT_SRC_PAD, "template-caps", templ_caps,
 
380
      "direction", GST_PAD_SRC, NULL);
 
381
}
 
382
 
 
383
static void
 
384
gst_vdp_output_src_pad_get_property (GObject * object, guint prop_id,
 
385
    GValue * value, GParamSpec * pspec)
 
386
{
 
387
  GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
 
388
 
 
389
  switch (prop_id) {
 
390
    case PROP_DISPLAY:
 
391
      g_value_set_string (value, vdp_pad->display);
 
392
      break;
 
393
    case PROP_TEMPL_CAPS:
 
394
      gst_value_set_caps (value, vdp_pad->templ_caps);
 
395
      break;
 
396
    default:
 
397
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
398
      break;
 
399
  }
 
400
}
 
401
 
 
402
static void
 
403
gst_vdp_output_src_pad_set_property (GObject * object, guint prop_id,
 
404
    const GValue * value, GParamSpec * pspec)
 
405
{
 
406
  GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
 
407
 
 
408
  switch (prop_id) {
 
409
    case PROP_DISPLAY:
 
410
      vdp_pad->display = g_value_dup_string (value);
 
411
      break;
 
412
    case PROP_TEMPL_CAPS:
 
413
      if (vdp_pad->templ_caps)
 
414
        gst_caps_unref (vdp_pad->templ_caps);
 
415
      vdp_pad->templ_caps = gst_caps_copy (gst_value_get_caps (value));
 
416
      break;
 
417
    default:
 
418
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
419
      break;
 
420
  }
 
421
}
 
422
 
 
423
static void
 
424
gst_vdp_output_src_pad_finalize (GObject * object)
 
425
{
 
426
  GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
 
427
 
 
428
  if (vdp_pad->templ_caps)
 
429
    gst_caps_unref (vdp_pad->templ_caps);
 
430
 
 
431
  g_free (vdp_pad->display);
 
432
 
 
433
  G_OBJECT_CLASS (gst_vdp_output_src_pad_parent_class)->finalize (object);
 
434
}
 
435
 
 
436
static void
 
437
gst_vdp_output_src_pad_init (GstVdpOutputSrcPad * vdp_pad)
 
438
{
 
439
  GstPad *pad = GST_PAD (vdp_pad);
 
440
 
 
441
  vdp_pad->device = NULL;
 
442
  vdp_pad->caps = NULL;
 
443
 
 
444
  vdp_pad->display = NULL;
 
445
  vdp_pad->templ_caps = NULL;
 
446
 
 
447
  gst_pad_set_getcaps_function (pad,
 
448
      GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_getcaps));
 
449
  gst_pad_set_activatepush_function (pad,
 
450
      GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_activate_push));
 
451
}
 
452
 
 
453
static void
 
454
gst_vdp_output_src_pad_class_init (GstVdpOutputSrcPadClass * klass)
 
455
{
 
456
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
457
 
 
458
  object_class->get_property = gst_vdp_output_src_pad_get_property;
 
459
  object_class->set_property = gst_vdp_output_src_pad_set_property;
 
460
  object_class->finalize = gst_vdp_output_src_pad_finalize;
 
461
 
 
462
  g_object_class_install_property (object_class, PROP_DISPLAY,
 
463
      g_param_spec_string ("display", "Display", "X Display name",
 
464
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
465
 
 
466
  g_object_class_install_property (object_class, PROP_TEMPL_CAPS,
 
467
      g_param_spec_boxed ("template-caps", "Template caps",
 
468
          "Template caps", GST_TYPE_CAPS,
 
469
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
470
 
 
471
}