gstdtsdec.c 16.5 KB
Newer Older
Ronald S. Bultje's avatar
Ronald S. Bultje 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
/* GStreamer DTS decoder plugin based on libdtsdec
 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * 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.
 */

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

#include <string.h>
#include "_stdint.h"
#include <stdlib.h>

#include <gst/gst.h>
Ronald S. Bultje's avatar
Ronald S. Bultje committed
29 30
#include <gst/audio/multichannel.h>

Ronald S. Bultje's avatar
Ronald S. Bultje committed
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
#include <dts.h>

#include "gstdtsdec.h"

GST_DEBUG_CATEGORY_STATIC (dtsdec_debug);
#define GST_CAT_DEFAULT (dtsdec_debug)

enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  ARG_0,
  ARG_DRC
      /* FILL ME */
};

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

#if defined(LIBDTS_FIXED)
#define DTS_CAPS "audio/x-raw-int, " \
    "endianness = (int) BYTE_ORDER, " \
    "signed = (boolean) true, " \
    "width = (int) 16, " \
    "depth = (int) 16"
#define SAMPLE_WIDTH 16
#elif defined(LIBDTS_DOUBLE)
#define DTS_CAPS "audio/x-raw-float, " \
    "endianness = (int) BYTE_ORDER, " \
    "width = (int) 64"
#define SAMPLE_WIDTH 64
#else
#define DTS_CAPS "audio/x-raw-float, " \
    "endianness = (int) BYTE_ORDER, " \
    "width = (int) 32"
#define SAMPLE_WIDTH 32
#endif

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (DTS_CAPS ", "
        "rate = (int) [ 4000, 96000 ], " "channels = (int) [ 1, 6 ]")
    );

static void gst_dtsdec_base_init (GstDtsDecClass * klass);
static void gst_dtsdec_class_init (GstDtsDecClass * klass);
static void gst_dtsdec_init (GstDtsDec * dtsdec);

87
static void gst_dtsdec_chain (GstPad * pad, GstData * data);
88 89
static GstStateChangeReturn gst_dtsdec_change_state (GstElement * element,
    GstStateChange transition);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
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

static void gst_dtsdec_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_dtsdec_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static GstElementClass *parent_class = NULL;

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

GType
gst_dtsdec_get_type (void)
{
  static GType dtsdec_type = 0;

  if (!dtsdec_type) {
    static const GTypeInfo dtsdec_info = {
      sizeof (GstDtsDecClass),
      (GBaseInitFunc) gst_dtsdec_base_init,
      NULL, (GClassInitFunc) gst_dtsdec_class_init,
      NULL,
      NULL,
      sizeof (GstDtsDec),
      0,
      (GInstanceInitFunc) gst_dtsdec_init,
    };

    dtsdec_type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstDtsDec", &dtsdec_info, 0);

    GST_DEBUG_CATEGORY_INIT (dtsdec_debug, "dtsdec", 0, "DTS audio decoder");
  }
  return dtsdec_type;
}

static void
gst_dtsdec_base_init (GstDtsDecClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  static GstElementDetails gst_dtsdec_details = {
    "DTS audio decoder",
131
    "Codec/Decoder/Audio",
Ronald S. Bultje's avatar
Ronald S. Bultje committed
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
    "Decodes DTS audio streams",
    "Ronald Bultje <rbultje@ronald.bitfreak.net>"
  };

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
  gst_element_class_set_details (element_class, &gst_dtsdec_details);
}

static void
gst_dtsdec_class_init (GstDtsDecClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DRC,
      g_param_spec_boolean ("drc", "Dynamic Range Compression",
          "Use Dynamic Range Compression", FALSE, G_PARAM_READWRITE));

  gobject_class->set_property = gst_dtsdec_set_property;
  gobject_class->get_property = gst_dtsdec_get_property;

  gstelement_class->change_state = gst_dtsdec_change_state;
}

static void
gst_dtsdec_init (GstDtsDec * dtsdec)
{
  GstElement *element = GST_ELEMENT (dtsdec);

  /* create the sink and src pads */
  dtsdec->sinkpad =
      gst_pad_new_from_template (gst_element_get_pad_template (GST_ELEMENT
          (dtsdec), "sink"), "sink");
172
  gst_pad_set_chain_function (dtsdec->sinkpad, gst_dtsdec_chain);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
173 174 175 176 177 178 179 180 181 182 183 184 185
  gst_element_add_pad (element, dtsdec->sinkpad);

  dtsdec->srcpad =
      gst_pad_new_from_template (gst_element_get_pad_template (element,
          "src"), "src");
  gst_pad_use_explicit_caps (dtsdec->srcpad);
  gst_element_add_pad (element, dtsdec->srcpad);

  GST_FLAG_SET (element, GST_ELEMENT_EVENT_AWARE);
  dtsdec->dynamic_range_compression = FALSE;
}

static gint
Ronald S. Bultje's avatar
Ronald S. Bultje committed
186
gst_dtsdec_channels (uint32_t flags, GstAudioChannelPosition ** pos)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
187 188
{
  gint chans = 0;
189 190 191 192 193 194 195 196
  GstAudioChannelPosition *tpos = NULL;

  if (pos) {
    /* Allocate the maximum, for ease */
    tpos = *pos = g_new (GstAudioChannelPosition, 7);
    if (!tpos)
      return 0;
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
197 198 199 200

  switch (flags & DTS_CHANNEL_MASK) {
    case DTS_MONO:
      chans = 1;
201 202
      if (tpos)
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
203
      break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
204
      /* case DTS_CHANNEL: */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
205 206 207 208 209
    case DTS_STEREO:
    case DTS_STEREO_SUMDIFF:
    case DTS_STEREO_TOTAL:
    case DTS_DOLBY:
      chans = 2;
210 211 212
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
213
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
214 215
      break;
    case DTS_3F:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
216
      chans = 3;
217 218 219 220
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
221 222
      }
      break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
223 224
    case DTS_2F1R:
      chans = 3;
225 226 227 228
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
229
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
230 231
      break;
    case DTS_3F1R:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
232
      chans = 4;
233 234 235 236 237
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        tpos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
238 239
      }
      break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
240 241
    case DTS_2F2R:
      chans = 4;
242 243 244 245 246
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
        tpos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
247
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
248 249 250
      break;
    case DTS_3F2R:
      chans = 5;
251 252 253 254 255 256
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        tpos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
        tpos[4] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
257
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
258 259 260
      break;
    case DTS_4F2R:
      chans = 6;
261 262 263 264 265 266 267
      if (tpos) {
        tpos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
        tpos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
        tpos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        tpos[3] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        tpos[4] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
        tpos[5] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
268
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
269 270 271 272 273 274
      break;
    default:
      /* error */
      g_warning ("dtsdec: invalid flags 0x%x", flags);
      return 0;
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
275
  if (flags & DTS_LFE) {
276 277
    if (tpos) {
      tpos[chans] = GST_AUDIO_CHANNEL_POSITION_LFE;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
278
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
279
    chans += 1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
280
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
281 282 283 284 285 286 287

  return chans;
}

static gboolean
gst_dtsdec_renegotiate (GstDtsDec * dts)
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
288
  GstAudioChannelPosition *pos;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
289
  GstCaps *caps = gst_caps_from_string (DTS_CAPS);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
290 291 292 293
  gint channels = gst_dtsdec_channels (dts->using_channels, &pos);

  if (!channels)
    return FALSE;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
294 295 296 297 298 299 300

  GST_INFO ("dtsdec renegotiate, channels=%d, rate=%d",
      channels, dts->sample_rate);

  gst_caps_set_simple (caps,
      "channels", G_TYPE_INT, channels,
      "rate", G_TYPE_INT, (gint) dts->sample_rate, NULL);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
301 302
  gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
  g_free (pos);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
303 304 305 306 307

  return gst_pad_set_explicit_caps (dts->srcpad, caps);
}

static void
308
gst_dtsdec_handle_event (GstDtsDec * dts, GstEvent * event)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
309 310 311 312 313 314 315 316 317 318 319
{
  if (!event) {
    GST_ELEMENT_ERROR (dts, RESOURCE, READ, (NULL), (NULL));
    return;
  }

  GST_LOG ("Handling event of type %d timestamp %llu", GST_EVENT_TYPE (event),
      GST_EVENT_TIMESTAMP (event));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_DISCONTINUOUS:
320 321 322 323 324 325 326 327 328 329 330
    {
      gint64 val;

      if (!gst_event_discont_get_value (event, GST_FORMAT_TIME, &val) ||
          !GST_CLOCK_TIME_IS_VALID (val)) {
        GST_WARNING ("No time discont value in event %p", event);
      } else {
        dts->current_ts = val;
      }
    }
      /* Fallthrough */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
331
    case GST_EVENT_FLUSH:
332 333 334 335
      if (dts->cache) {
        gst_buffer_unref (dts->cache);
        dts->cache = NULL;
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
      break;
    default:
      break;
  }

  gst_pad_event_default (dts->sinkpad, event);
}

static void
gst_dtsdec_update_streaminfo (GstDtsDec * dts)
{
  GstTagList *taglist;

  taglist = gst_tag_list_new ();

  gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
      GST_TAG_BITRATE, (guint) dts->bit_rate, NULL);

  gst_element_found_tags_for_pad (GST_ELEMENT (dts),
      dts->srcpad, dts->current_ts, taglist);
}

358 359 360
static gboolean
gst_dtsdec_handle_frame (GstDtsDec * dts, guint8 * data,
    guint length, gint flags, gint sample_rate, gint bit_rate)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
361 362 363
{
  gboolean need_renegotiation = FALSE;
  GstClockTime timestamp = 0;
364 365 366 367
  gint channels, num_blocks;
  GstBuffer *out;
  gint i, s, c, num_c;
  sample_t *samples;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  /* go over stream properties, update caps/streaminfo if needed */
  if (dts->sample_rate != sample_rate) {
    need_renegotiation = TRUE;
    dts->sample_rate = sample_rate;
  }

  dts->stream_channels = flags;

  if (bit_rate != dts->bit_rate) {
    dts->bit_rate = bit_rate;
    gst_dtsdec_update_streaminfo (dts);
  }

  /* process */
  flags = dts->request_channels | DTS_ADJUST_LEVEL;
  dts->level = 1;

  if (dts_frame (dts->state, data, &flags, &dts->level, dts->bias)) {
    GST_WARNING ("dts_frame error");
388
    return FALSE;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
389 390 391 392 393 394 395 396 397 398 399 400 401
  }

  channels = flags & (DTS_CHANNEL_MASK | DTS_LFE);

  if (dts->using_channels != channels) {
    need_renegotiation = TRUE;
    dts->using_channels = channels;
  }

  if (need_renegotiation == TRUE) {
    GST_DEBUG ("dtsdec: sample_rate:%d stream_chans:0x%x using_chans:0x%x",
        dts->sample_rate, dts->stream_channels, dts->using_channels);
    if (!gst_dtsdec_renegotiate (dts))
402
      return FALSE;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  }

  if (dts->dynamic_range_compression == FALSE) {
    dts_dynrng (dts->state, NULL, NULL);
  }

  /* handle decoded data, one block is 256 samples */
  num_blocks = dts_blocks_num (dts->state);
  for (i = 0; i < num_blocks; i++) {
    if (dts_block (dts->state)) {
      GST_WARNING ("dts_block error %d", i);
      continue;
    }

    samples = dts_samples (dts->state);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
418
    num_c = gst_dtsdec_channels (dts->using_channels, NULL);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
419
    out = gst_buffer_new_and_alloc ((SAMPLE_WIDTH / 8) * 256 * num_c);
420 421 422 423 424
    if (!out) {
      GST_ELEMENT_ERROR (dts, RESOURCE, FAILED, (NULL), ("Out of memory"));
      return FALSE;
    }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    GST_BUFFER_TIMESTAMP (out) = timestamp;
    GST_BUFFER_DURATION (out) = GST_SECOND * 256 / dts->sample_rate;

    /* libdts returns buffers in 256-sample-blocks per channel,
     * we want interleaved. And we need to copy anyway... */
    data = GST_BUFFER_DATA (out);
    for (s = 0; s < 256; s++) {
      for (c = 0; c < num_c; c++) {
        *(sample_t *) data = samples[s + c * 256];
        data += (SAMPLE_WIDTH / 8);
      }
    }

    /* push on */
    gst_pad_push (dts->srcpad, GST_DATA (out));
    timestamp += GST_SECOND * 256 / dts->sample_rate;
  }

  dts->current_ts = timestamp;
444 445 446 447 448 449 450 451 452 453 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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  return TRUE;
}

static void
gst_dtsdec_chain (GstPad * pad, GstData * _data)
{
  GstDtsDec *dts;
  guint8 *data;
  gint64 size;
  GstBuffer *buf;
  gint length, flags, sample_rate, bit_rate, frame_length;

  g_return_if_fail (pad != NULL);
  g_return_if_fail (_data != NULL);

  dts = GST_DTSDEC (gst_pad_get_parent (pad));

  if (GST_IS_EVENT (_data)) {
    gst_dtsdec_handle_event (dts, GST_EVENT (_data));
    return;
  }

  /* merge with cache, if any. Also make sure timestamps match */
  buf = GST_BUFFER (_data);
  if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
    dts->current_ts = GST_BUFFER_TIMESTAMP (buf);
    GST_DEBUG_OBJECT (dts, "Received buffer with ts %" GST_TIME_FORMAT
        " duration %" GST_TIME_FORMAT,
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
        GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
  }

  if (dts->cache) {
    buf = gst_buffer_join (dts->cache, buf);
    dts->cache = NULL;
  }

  data = GST_BUFFER_DATA (buf);
  size = GST_BUFFER_SIZE (buf);
  length = 0;
  while (size >= 7) {
    length = dts_syncinfo (dts->state, data, &flags,
        &sample_rate, &bit_rate, &frame_length);
    if (length == 0) {
      /* shift window to re-find sync */
      data++;
      size--;
    } else if (length <= size) {
      GST_DEBUG ("Sync: frame size %d", length);
      if (!gst_dtsdec_handle_frame (dts, data,
              length, flags, sample_rate, bit_rate)) {
        size = 0;
        break;
      }
      size -= length;
      data += length;
    } else {
      GST_LOG ("Not enough data available (needed %d had %d)", length, size);
      break;
    }
  }

  /* keep cache */
  if (length == 0) {
    GST_LOG ("No sync found");
  }
  if (size > 0) {
    dts->cache = gst_buffer_create_sub (buf,
        GST_BUFFER_SIZE (buf) - size, size);
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
514 515 516 517

  gst_buffer_unref (buf);
}

518 519
static GstStateChangeReturn
gst_dtsdec_change_state (GstElement * element, GstStateChange transition)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
520 521 522
{
  GstDtsDec *dts = GST_DTSDEC (element);

523 524
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538
      GstCPUFlags cpuflags;
      uint32_t mm_accel = 0;

      cpuflags = gst_cpu_get_flags ();
      if (cpuflags & GST_CPU_FLAG_MMX)
        mm_accel |= MM_ACCEL_X86_MMX;
      if (cpuflags & GST_CPU_FLAG_3DNOW)
        mm_accel |= MM_ACCEL_X86_3DNOW;
      if (cpuflags & GST_CPU_FLAG_MMXEXT)
        mm_accel |= MM_ACCEL_X86_MMXEXT;

      dts->state = dts_init (mm_accel);
      break;
    }
539
    case GST_STATE_CHANGE_READY_TO_PAUSED:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
540 541 542 543 544 545 546 547 548 549 550
      dts->samples = dts_samples (dts->state);
      dts->bit_rate = -1;
      dts->sample_rate = -1;
      dts->stream_channels = 0;
      /* FIXME force stereo for now */
      dts->request_channels = DTS_STEREO;
      dts->using_channels = 0;
      dts->level = 1;
      dts->bias = 0;
      dts->current_ts = 0;
      break;
551
    case GST_STATE_CHANGE_PAUSED_TO_READY:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
552 553
      dts->samples = NULL;
      break;
554
    case GST_STATE_CHANGE_READY_TO_NULL:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
555 556 557 558 559 560 561 562
      dts_free (dts->state);
      dts->state = NULL;
      break;
    default:
      break;
  }

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
563
    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
564

565
  return GST_STATE_CHANGE_SUCCESS;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
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 void
gst_dtsdec_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
{
  GstDtsDec *dts = GST_DTSDEC (object);

  switch (prop_id) {
    case ARG_DRC:
      dts->dynamic_range_compression = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_dtsdec_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstDtsDec *dts = GST_DTSDEC (object);

  switch (prop_id) {
    case ARG_DRC:
      g_value_set_boolean (value, dts->dynamic_range_compression);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
plugin_init (GstPlugin * plugin)
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
603
  if (!gst_library_load ("gstbytestream") || !gst_library_load ("gstaudio"))
Ronald S. Bultje's avatar
Ronald S. Bultje committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617
    return FALSE;

  if (!gst_element_register (plugin, "dtsdec", GST_RANK_PRIMARY,
          GST_TYPE_DTSDEC))
    return FALSE;

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "dtsdec",
    "Decodes DTS audio streams",
    plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN);