gstbasetransform.c 21.5 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
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
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *                    2005 Wim Taymans <wim@fluendo.com>
 *
 * gstbasetransform.c: 
 *
 * 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.
 */


#include <stdlib.h>

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

#include "../gst-i18n-lib.h"
#include "gstbasetransform.h"
#include <gst/gstmarshal.h>

GST_DEBUG_CATEGORY_STATIC (gst_base_transform_debug);
#define GST_CAT_DEFAULT gst_base_transform_debug

/* BaseTransform signals and args */
enum
{
  SIGNAL_HANDOFF,
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  PROP_0,
};

static GstElementClass *parent_class = NULL;

static void gst_base_transform_base_init (gpointer g_class);
static void gst_base_transform_class_init (GstBaseTransformClass * klass);
static void gst_base_transform_init (GstBaseTransform * trans,
    gpointer g_class);

GType
gst_base_transform_get_type (void)
{
  static GType base_transform_type = 0;

  if (!base_transform_type) {
    static const GTypeInfo base_transform_info = {
      sizeof (GstBaseTransformClass),
      (GBaseInitFunc) gst_base_transform_base_init,
      NULL,
      (GClassInitFunc) gst_base_transform_class_init,
      NULL,
      NULL,
      sizeof (GstBaseTransform),
      0,
      (GInstanceInitFunc) gst_base_transform_init,
    };

    base_transform_type = g_type_register_static (GST_TYPE_ELEMENT,
        "GstBaseTransform", &base_transform_info, G_TYPE_FLAG_ABSTRACT);
  }
  return base_transform_type;
}

static void gst_base_transform_finalize (GObject * object);
static void gst_base_transform_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_base_transform_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
87 88 89 90
static gboolean gst_base_transform_src_activate_pull (GstPad * pad,
    gboolean active);
static gboolean gst_base_transform_sink_activate_push (GstPad * pad,
    gboolean active);
91 92
static guint gst_base_transform_get_size (GstBaseTransform * trans);

Wim Taymans's avatar
Wim Taymans committed
93 94 95 96 97 98 99 100
static GstElementStateReturn gst_base_transform_change_state (GstElement *
    element);

static gboolean gst_base_transform_event (GstPad * pad, GstEvent * event);
static GstFlowReturn gst_base_transform_getrange (GstPad * pad, guint64 offset,
    guint length, GstBuffer ** buffer);
static GstFlowReturn gst_base_transform_chain (GstPad * pad,
    GstBuffer * buffer);
101
static GstCaps *gst_base_transform_getcaps (GstPad * pad);
Wim Taymans's avatar
Wim Taymans committed
102
static gboolean gst_base_transform_setcaps (GstPad * pad, GstCaps * caps);
103 104
static GstFlowReturn gst_base_transform_buffer_alloc (GstPad * pad,
    guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
Wim Taymans's avatar
Wim Taymans committed
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

/* static guint gst_base_transform_signals[LAST_SIGNAL] = { 0 }; */

static void
gst_base_transform_base_init (gpointer g_class)
{
  GST_DEBUG_CATEGORY_INIT (gst_base_transform_debug, "basetransform", 0,
      "basetransform element");
}

static void
gst_base_transform_finalize (GObject * object)
{
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_base_transform_class_init (GstBaseTransformClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gstelement_class = GST_ELEMENT_CLASS (klass);

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  gobject_class->set_property =
      GST_DEBUG_FUNCPTR (gst_base_transform_set_property);
  gobject_class->get_property =
      GST_DEBUG_FUNCPTR (gst_base_transform_get_property);

  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_transform_finalize);

  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_base_transform_change_state);
}

static void
gst_base_transform_init (GstBaseTransform * trans, gpointer g_class)
{
  GstPadTemplate *pad_template;

  GST_DEBUG ("gst_base_transform_init");

  pad_template =
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
  g_return_if_fail (pad_template != NULL);
  trans->sinkpad = gst_pad_new_from_template (pad_template, "sink");
  gst_pad_set_getcaps_function (trans->sinkpad,
155
      GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
Wim Taymans's avatar
Wim Taymans committed
156 157 158 159 160 161
  gst_pad_set_setcaps_function (trans->sinkpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
  gst_pad_set_event_function (trans->sinkpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_event));
  gst_pad_set_chain_function (trans->sinkpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_chain));
162 163
  gst_pad_set_activatepush_function (trans->sinkpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_sink_activate_push));
164 165
  gst_pad_set_bufferalloc_function (trans->sinkpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_buffer_alloc));
Wim Taymans's avatar
Wim Taymans committed
166 167 168 169 170 171 172
  gst_element_add_pad (GST_ELEMENT (trans), trans->sinkpad);

  pad_template =
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
  g_return_if_fail (pad_template != NULL);
  trans->srcpad = gst_pad_new_from_template (pad_template, "src");
  gst_pad_set_getcaps_function (trans->srcpad,
173 174 175
      GST_DEBUG_FUNCPTR (gst_base_transform_getcaps));
  gst_pad_set_setcaps_function (trans->srcpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_setcaps));
Wim Taymans's avatar
Wim Taymans committed
176 177
  gst_pad_set_getrange_function (trans->srcpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_getrange));
178 179
  gst_pad_set_activatepull_function (trans->srcpad,
      GST_DEBUG_FUNCPTR (gst_base_transform_src_activate_pull));
Wim Taymans's avatar
Wim Taymans committed
180
  gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
181 182

  trans->passthrough = FALSE;
Wim Taymans's avatar
Wim Taymans committed
183 184 185
}

static GstCaps *
186 187 188
gst_base_transform_transform_caps (GstBaseTransform * trans, GstPad * pad,
    GstCaps * caps)
{
189
  GstCaps *ret;
190 191 192 193
  GstBaseTransformClass *klass;

  klass = GST_BASE_TRANSFORM_GET_CLASS (trans);

194 195
  GST_DEBUG_OBJECT (trans, "from: %" GST_PTR_FORMAT, caps);

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  /* if there is a custom transform function, use this */
  if (klass->transform_caps) {
    GstCaps *temp;
    gint i;

    ret = gst_caps_new_empty ();

    /* we send caps with just one structure to the transform 
     * function as this is easier for the element */
    for (i = 0; i < gst_caps_get_size (caps); i++) {
      GstCaps *nth;

      nth = gst_caps_copy_nth (caps, i);
      GST_DEBUG_OBJECT (trans, "  from: %" GST_PTR_FORMAT, nth);
      temp = klass->transform_caps (trans, pad, nth);
      gst_caps_unref (nth);
      GST_DEBUG_OBJECT (trans, "  to  : %" GST_PTR_FORMAT, temp);

      gst_caps_append (ret, temp);
    }
    gst_caps_do_simplify (ret);
  } else {
    /* else use the identity transform */
219
    ret = gst_caps_ref (caps);
220
  }
221 222 223 224

  GST_DEBUG_OBJECT (trans, "to:   %" GST_PTR_FORMAT, ret);

  return ret;
225 226 227 228
}

static GstCaps *
gst_base_transform_getcaps (GstPad * pad)
Wim Taymans's avatar
Wim Taymans committed
229
{
230
  GstBaseTransform *trans;
231
  GstPad *otherpad;
232 233
  GstCaps *caps;

234
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
235

236
  otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
Wim Taymans's avatar
Wim Taymans committed
237

238
  /* we can do what the peer can */
239
  caps = gst_pad_peer_get_caps (otherpad);
240 241
  if (caps) {
    GstCaps *temp;
242 243
    const GstCaps *templ;

244
    GST_DEBUG_OBJECT (trans, "peer caps  %" GST_PTR_FORMAT, caps);
245

246 247
    /* filtered against our padtemplate */
    templ = gst_pad_get_pad_template_caps (otherpad);
248
    GST_DEBUG_OBJECT (trans, "our template  %" GST_PTR_FORMAT, templ);
249
    temp = gst_caps_intersect (caps, templ);
250
    GST_DEBUG_OBJECT (trans, "intersected %" GST_PTR_FORMAT, temp);
251
    gst_caps_unref (caps);
252
    /* then see what we can tranform this to */
253
    caps = gst_base_transform_transform_caps (trans, otherpad, temp);
254
    GST_DEBUG_OBJECT (trans, "transformed  %" GST_PTR_FORMAT, caps);
255
    gst_caps_unref (temp);
256 257 258 259 260
    if (caps == NULL)
      goto done;

    /* and filter against the template again */
    templ = gst_pad_get_pad_template_caps (pad);
261
    GST_DEBUG_OBJECT (trans, "our template  %" GST_PTR_FORMAT, templ);
262
    temp = gst_caps_intersect (caps, templ);
263
    GST_DEBUG_OBJECT (trans, "intersected %" GST_PTR_FORMAT, temp);
264
    gst_caps_unref (caps);
265
    /* this is what we can do */
266
    caps = temp;
267 268 269
  } else {
    /* no peer, our padtemplate is enough then */
    caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
270
  }
271

272
done:
273
  GST_DEBUG_OBJECT (trans, "returning  %" GST_PTR_FORMAT, caps);
274

275 276
  gst_object_unref (trans);

277
  return caps;
Wim Taymans's avatar
Wim Taymans committed
278 279
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static gboolean
gst_base_transform_configure_caps (GstBaseTransform * trans, GstCaps * in,
    GstCaps * out)
{
  gboolean ret = TRUE;
  GstBaseTransformClass *klass;

  klass = GST_BASE_TRANSFORM_GET_CLASS (trans);

  /* no configure the element with the caps */
  if (klass->set_caps) {
    ret = klass->set_caps (trans, in, out);
  }

  /* if all goes well, get the size of the output buffer */
  if (ret) {
    trans->out_size = gst_base_transform_get_size (trans);
297
    GST_DEBUG_OBJECT (trans, "output buffer size %d", trans->out_size);
298 299 300 301
  }
  return ret;
}

Wim Taymans's avatar
Wim Taymans committed
302 303 304 305
static gboolean
gst_base_transform_setcaps (GstPad * pad, GstCaps * caps)
{
  GstBaseTransform *trans;
306 307
  GstBaseTransformClass *klass;
  GstPad *otherpad, *otherpeer;
308
  GstCaps *othercaps = NULL;
309
  gboolean ret = TRUE;
Wim Taymans's avatar
Wim Taymans committed
310

311
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
312 313 314 315 316
  klass = GST_BASE_TRANSFORM_GET_CLASS (trans);

  otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
  otherpeer = gst_pad_get_peer (otherpad);

317 318
  if (GST_PAD_IS_IN_SETCAPS (otherpad)) {
    ret = TRUE;
319
    goto done;
320
  }
321

322 323
  /* see how we can transform the input caps */
  othercaps = gst_base_transform_transform_caps (trans, pad, caps);
324

325 326 327
  /* check if transform is empty */
  if (!othercaps || gst_caps_is_empty (othercaps))
    goto no_transform;
328

329 330
  if (!gst_caps_is_fixed (othercaps)) {
    GstCaps *temp;
331

332 333
    GST_DEBUG_OBJECT (trans,
        "transform returned non fixed  %" GST_PTR_FORMAT, othercaps);
334

335
    temp = gst_caps_intersect (othercaps, caps);
336
    GST_DEBUG_OBJECT (trans, "intersect returned %" GST_PTR_FORMAT, temp);
337
    if (temp) {
338
      if (!gst_caps_is_empty (temp) && otherpeer) {
339
        GST_DEBUG_OBJECT (trans, "try passthrough with %" GST_PTR_FORMAT, caps);
340 341
        /* try passthrough. we know it's fixed, because caps is fixed */
        if (gst_pad_accept_caps (otherpeer, caps)) {
342
          GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
343 344 345 346
          gst_caps_unref (othercaps);
          othercaps = gst_caps_ref (caps);
          /* will fall though. calls accept_caps again, should fix that. */
        } else {
347 348
          GST_DEBUG_OBJECT (trans,
              "peer did not accept %" GST_PTR_FORMAT, caps);
349
        }
350 351 352 353
      }
      gst_caps_unref (temp);
    }
  }
354

355 356
  if (!gst_caps_is_fixed (othercaps) && otherpeer) {
    /* intersect against what the peer can do */
357 358
    GstCaps *peercaps;
    GstCaps *intersect;
359

360
    GST_DEBUG_OBJECT (trans, "othercaps now %" GST_PTR_FORMAT, othercaps);
361

362 363 364 365 366 367
    peercaps = gst_pad_get_caps (otherpeer);
    intersect = gst_caps_intersect (peercaps, othercaps);
    gst_caps_unref (peercaps);
    gst_caps_unref (othercaps);
    othercaps = intersect;

368 369
    GST_DEBUG_OBJECT (trans,
        "filtering against peer yields %" GST_PTR_FORMAT, othercaps);
370
  }
371

372 373
  if (!gst_caps_is_fixed (othercaps)) {
    GstCaps *temp;
374

375 376
    GST_DEBUG_OBJECT (trans,
        "othercaps now, trying to fixate %" GST_PTR_FORMAT, othercaps);
377

378 379 380 381
    /* take first possibility and fixate if necessary */
    temp = gst_caps_copy_nth (othercaps, 0);
    gst_caps_unref (othercaps);
    othercaps = temp;
382
    gst_pad_fixate_caps (otherpad, othercaps);
383

384
    GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
385
  }
Wim Taymans's avatar
Wim Taymans committed
386

387 388 389
  /* caps should be fixed now */
  if (!gst_caps_is_fixed (othercaps))
    goto could_not_fixate;
390

391 392 393
  /* and peer should accept */
  if (otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
    goto peer_no_accept;
394

395
  GST_DEBUG_OBJECT (trans, "got final caps %" GST_PTR_FORMAT, othercaps);
396

397 398
  /* we know this will work, we implement the setcaps */
  gst_pad_set_caps (otherpad, othercaps);
399

400
  trans->in_place = gst_caps_is_equal (caps, othercaps);
401
  GST_DEBUG_OBJECT (trans, "in_place: %d", trans->in_place);
402 403 404

  /* see if we have to configure the element now */
  if (!trans->delay_configure) {
405
    if (pad == trans->sinkpad)
406
      ret = gst_base_transform_configure_caps (trans, caps, othercaps);
407
    else
408
      ret = gst_base_transform_configure_caps (trans, othercaps, caps);
409 410 411 412 413
  }

done:
  if (otherpeer)
    gst_object_unref (otherpeer);
414 415 416
  if (othercaps)
    gst_caps_unref (othercaps);

417 418
  gst_object_unref (trans);

419
  return ret;
420 421 422 423

  /* ERRORS */
no_transform:
  {
424 425
    GST_DEBUG_OBJECT (trans,
        "transform returned useless  %" GST_PTR_FORMAT, othercaps);
426 427 428 429 430
    ret = FALSE;
    goto done;
  }
could_not_fixate:
  {
431
    GST_DEBUG_OBJECT (trans, "FAILED to fixate %" GST_PTR_FORMAT, othercaps);
432 433 434 435 436
    ret = FALSE;
    goto done;
  }
peer_no_accept:
  {
437
    GST_DEBUG_OBJECT (trans, "FAILED to get peer of %" GST_PTR_FORMAT
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
    ret = FALSE;
    goto done;
  }
}

static guint
gst_base_transform_get_size (GstBaseTransform * trans)
{
  guint res = -1;
  GstBaseTransformClass *bclass;

  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
  if (bclass->get_size) {
    res = bclass->get_size (trans);
453
    GST_DEBUG_OBJECT (trans, "get size function returned %d", res);
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
  }

  return res;
}

static GstFlowReturn
gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
    GstCaps * caps, GstBuffer ** buf)
{
  GstBaseTransform *trans;
  GstFlowReturn res;

  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));

  if (trans->in_place) {
    /* we can only proxy the bufferpool if we do in_place transforms */
    res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
  } else {
    /* else let the default alloc function allocate a buffer */
    *buf = NULL;
    res = GST_FLOW_OK;
  }

  gst_object_unref (trans);

  return res;
Wim Taymans's avatar
Wim Taymans committed
480 481
}

482

Wim Taymans's avatar
Wim Taymans committed
483 484 485 486 487 488
static gboolean
gst_base_transform_event (GstPad * pad, GstEvent * event)
{
  GstBaseTransform *trans;
  GstBaseTransformClass *bclass;
  gboolean ret = FALSE;
Wim Taymans's avatar
Wim Taymans committed
489
  gboolean unlock;
Wim Taymans's avatar
Wim Taymans committed
490

491
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
492 493 494 495 496
  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);

  if (bclass->event)
    bclass->event (trans, event);

Wim Taymans's avatar
Wim Taymans committed
497 498
  unlock = FALSE;

Wim Taymans's avatar
Wim Taymans committed
499
  switch (GST_EVENT_TYPE (event)) {
Wim Taymans's avatar
Wim Taymans committed
500 501 502 503 504
    case GST_EVENT_FLUSH_START:
      break;
    case GST_EVENT_FLUSH_STOP:
      GST_STREAM_LOCK (pad);
      unlock = TRUE;
Wim Taymans's avatar
Wim Taymans committed
505 506 507
      break;
    case GST_EVENT_EOS:
      GST_STREAM_LOCK (pad);
Wim Taymans's avatar
Wim Taymans committed
508
      unlock = TRUE;
Wim Taymans's avatar
Wim Taymans committed
509 510 511 512 513
      break;
    default:
      break;
  }
  ret = gst_pad_event_default (pad, event);
Wim Taymans's avatar
Wim Taymans committed
514 515
  if (unlock)
    GST_STREAM_UNLOCK (pad);
Wim Taymans's avatar
Wim Taymans committed
516

517 518
  gst_object_unref (trans);

Wim Taymans's avatar
Wim Taymans committed
519 520 521
  return ret;
}

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
static GstFlowReturn
gst_base_transform_handle_buffer (GstBaseTransform * trans, GstBuffer * inbuf,
    GstBuffer ** outbuf)
{
  GstBaseTransformClass *bclass;
  GstFlowReturn ret = GST_FLOW_OK;

  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);

  if (trans->in_place) {
    if (bclass->transform_ip) {
      gst_buffer_ref (inbuf);

      /* in place transform and subclass supports method */
      ret = bclass->transform_ip (trans, inbuf);

      *outbuf = inbuf;
    } else {
      /* in place transform and subclass does not support method */
      if (bclass->transform) {
        /* make a copy of the buffer */
        *outbuf = inbuf;
        inbuf = gst_buffer_copy (inbuf);

        ret = bclass->transform (trans, inbuf, *outbuf);
      } else {
        ret = GST_FLOW_NOT_SUPPORTED;
      }
    }
  } else {
    /* figure out the output size */
    if (trans->out_size == -1) {
      /* ask subclass */
      if ((trans->out_size = gst_base_transform_get_size (trans)) == -1)
        /* else we have an error */
        goto no_size;
    }

    /* we cannot reconfigure the element yet as we are still processing
     * the old buffer. We will therefore delay the reconfiguration of the
     * element until we have processed this last buffer. */
    trans->delay_configure = TRUE;

    /* no in place transform, get buffer, this might renegotiate. */
    ret = gst_pad_alloc_buffer (trans->srcpad,
        GST_BUFFER_OFFSET (inbuf), trans->out_size,
        GST_PAD_CAPS (trans->srcpad), outbuf);

    trans->delay_configure = FALSE;

    if (ret != GST_FLOW_OK)
      goto no_buffer;

    gst_buffer_stamp (*outbuf, inbuf);

    if (bclass->transform)
      ret = bclass->transform (trans, inbuf, *outbuf);
    else
      ret = GST_FLOW_NOT_SUPPORTED;

    if (ret)
      ret =
          gst_base_transform_configure_caps (trans,
          GST_PAD_CAPS (trans->sinkpad), GST_PAD_CAPS (trans->srcpad));
  }
  gst_buffer_unref (inbuf);

  return ret;

  /* ERRORS */
no_size:
  {
    gst_buffer_unref (inbuf);
    GST_ELEMENT_ERROR (trans, STREAM, NOT_IMPLEMENTED,
        ("subclass did not specify output size"),
        ("subclass did not specify output size"));
    return GST_FLOW_ERROR;
  }
no_buffer:
  {
    gst_buffer_unref (inbuf);
603
    GST_DEBUG_OBJECT (trans, "could not get buffer from pool");
604 605 606 607
    return ret;
  }
}

Wim Taymans's avatar
Wim Taymans committed
608 609 610 611 612 613 614 615
static GstFlowReturn
gst_base_transform_getrange (GstPad * pad, guint64 offset,
    guint length, GstBuffer ** buffer)
{
  GstBaseTransform *trans;
  GstFlowReturn ret;
  GstBuffer *inbuf;

616
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
617 618 619 620 621 622

  ret = gst_pad_pull_range (trans->sinkpad, offset, length, &inbuf);
  if (ret == GST_FLOW_OK) {
    ret = gst_base_transform_handle_buffer (trans, inbuf, buffer);
  }

623
  gst_object_unref (trans);
Wim Taymans's avatar
Wim Taymans committed
624 625 626 627 628 629 630 631

  return ret;
}

static GstFlowReturn
gst_base_transform_chain (GstPad * pad, GstBuffer * buffer)
{
  GstBaseTransform *trans;
632
  GstFlowReturn ret;
Wim Taymans's avatar
Wim Taymans committed
633 634
  GstBuffer *outbuf;

635
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
Wim Taymans's avatar
Wim Taymans committed
636 637 638 639 640 641

  ret = gst_base_transform_handle_buffer (trans, buffer, &outbuf);
  if (ret == GST_FLOW_OK) {
    ret = gst_pad_push (trans->srcpad, outbuf);
  }

642
  gst_object_unref (trans);
Wim Taymans's avatar
Wim Taymans committed
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

  return ret;
}

static void
gst_base_transform_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstBaseTransform *trans;

  trans = GST_BASE_TRANSFORM (object);

  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_base_transform_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstBaseTransform *trans;

  trans = GST_BASE_TRANSFORM (object);

  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
678
gst_base_transform_sink_activate_push (GstPad * pad, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
679
{
680
  gboolean result = TRUE;
Wim Taymans's avatar
Wim Taymans committed
681
  GstBaseTransform *trans;
682
  GstBaseTransformClass *bclass;
Wim Taymans's avatar
Wim Taymans committed
683

684
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
685
  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
Wim Taymans's avatar
Wim Taymans committed
686

687 688 689
  if (active) {
    if (bclass->start)
      result = bclass->start (trans);
Wim Taymans's avatar
Wim Taymans committed
690
  }
691
  gst_object_unref (trans);
692

Wim Taymans's avatar
Wim Taymans committed
693 694 695 696
  return result;
}

static gboolean
697
gst_base_transform_src_activate_pull (GstPad * pad, gboolean active)
Wim Taymans's avatar
Wim Taymans committed
698 699 700
{
  gboolean result = FALSE;
  GstBaseTransform *trans;
701
  GstBaseTransformClass *bclass;
Wim Taymans's avatar
Wim Taymans committed
702

703
  trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
704
  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
Wim Taymans's avatar
Wim Taymans committed
705

706 707 708 709 710
  result = gst_pad_activate_pull (trans->sinkpad, active);

  if (active) {
    if (result && bclass->start)
      result &= bclass->start (trans);
Wim Taymans's avatar
Wim Taymans committed
711
  }
712
  gst_object_unref (trans);
713

Wim Taymans's avatar
Wim Taymans committed
714 715 716 717 718 719 720
  return result;
}

static GstElementStateReturn
gst_base_transform_change_state (GstElement * element)
{
  GstBaseTransform *trans;
721
  GstBaseTransformClass *bclass;
Wim Taymans's avatar
Wim Taymans committed
722 723 724 725
  GstElementState transition;
  GstElementStateReturn result;

  trans = GST_BASE_TRANSFORM (element);
726 727
  bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);

Wim Taymans's avatar
Wim Taymans committed
728 729 730 731 732 733
  transition = GST_STATE_TRANSITION (element);

  switch (transition) {
    case GST_STATE_NULL_TO_READY:
      break;
    case GST_STATE_READY_TO_PAUSED:
734 735 736 737
      GST_LOCK (trans);
      trans->in_place = trans->passthrough;
      trans->out_size = -1;
      GST_UNLOCK (trans);
Wim Taymans's avatar
Wim Taymans committed
738 739 740 741 742 743 744 745 746 747 748 749 750
      break;
    case GST_STATE_PAUSED_TO_PLAYING:
      break;
    default:
      break;
  }

  result = GST_ELEMENT_CLASS (parent_class)->change_state (element);

  switch (transition) {
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
751 752
      if (bclass->stop)
        result = bclass->stop (trans);
Wim Taymans's avatar
Wim Taymans committed
753 754 755 756 757 758 759 760 761
      break;
    case GST_STATE_READY_TO_NULL:
      break;
    default:
      break;
  }

  return result;
}
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

/**
 * gst_base_transform_set_passthrough:
 * @trans: the #GstBaseTransform to set
 * @passthrough: boolean indicating passthrough mode.
 *
 * Set passthrough mode for this filter by default. This is mostly
 * usefull for filters that do not care about negotiation.
 *
 * MT safe.
 */
void
gst_base_transform_set_passthrough (GstBaseTransform * trans,
    gboolean passthrough)
{
  g_return_if_fail (trans != NULL);

  GST_LOCK (trans);
  trans->passthrough = passthrough;
  GST_UNLOCK (trans);
}

/**
 * gst_base_transform_is_passthrough:
 * @trans: the #GstBaseTransform to query
 *
 * See if @trans is configured as a passthrough transform.
 *
 * Returns: TRUE is the transform is configured in passthrough mode.
 *
 * MT safe.
 */
gboolean
gst_base_transform_is_passthrough (GstBaseTransform * trans)
{
  gboolean result;

  g_return_val_if_fail (trans != NULL, FALSE);

  GST_LOCK (trans);
  result = trans->passthrough;
  GST_UNLOCK (trans);

  return result;
}