gstfaac.c 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GStreamer FAAC (Free AAC Encoder) plugin
 * Copyright (C) 2003 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

24 25
#include <string.h>

26 27
#include "gstfaac.h"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
28 29 30 31
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/mpeg, "
32 33
        "mpegversion = (int) { 4, 2 }, "
        "channels = (int) [ 1, 6 ], " "rate = (int) [ 8000, 96000 ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
34 35 36 37 38
    );

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
39 40 41 42 43 44
    GST_STATIC_CAPS ("audio/x-raw-int, "
        "endianness = (int) BYTE_ORDER, "
        "signed = (boolean) TRUE, "
        "width = (int) 16, "
        "depth = (int) 16, "
        "rate = (int) [ 8000, 96000 ], " "channels = (int) [ 1, 6]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45 46
    );

47 48 49 50 51 52 53 54 55 56 57 58 59 60
#define UNUSED \
    "audio/x-raw-int, " \
    "endianness = (int) BYTE_ORDER, " \
    "signed = (boolean) TRUE, " \
    "width = (int) 32, " \
    "depth = (int) 24, " \
    "rate = (int) [ 8000, 96000], " \
    "channels = (int) [ 1, 6]; " \
    "audio/x-raw-float, " \
    "endianness = (int) BYTE_ORDER, " \
    "depth = (int) 32, "    /* sizeof (gfloat) */ \
    "rate = (int) [ 8000, 96000], " \
    "channels = (int) [ 1, 6]" \

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62
enum
{
63
  ARG_0,
64
  ARG_OUTPUTFORMAT,
65 66 67 68 69
  ARG_BITRATE,
  ARG_PROFILE,
  ARG_TNS,
  ARG_MIDSIDE,
  ARG_SHORTCTL
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
70
      /* FILL ME */
71 72
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73 74 75
static void gst_faac_base_init (GstFaacClass * klass);
static void gst_faac_class_init (GstFaacClass * klass);
static void gst_faac_init (GstFaac * faac);
76

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
77 78 79 80
static void gst_faac_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_faac_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
81 82

static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
gst_faac_sinkconnect (GstPad * pad, const GstCaps * caps);
84
static GstPadLinkReturn gst_faac_srcconnect (GstPad * pad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
85 86
static void gst_faac_chain (GstPad * pad, GstData * data);
static GstElementStateReturn gst_faac_change_state (GstElement * element);
87 88

static GstElementClass *parent_class = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89

90 91 92 93 94 95 96 97 98
/* static guint gst_faac_signals[LAST_SIGNAL] = { 0 }; */

GType
gst_faac_get_type (void)
{
  static GType gst_faac_type = 0;

  if (!gst_faac_type) {
    static const GTypeInfo gst_faac_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
99
      sizeof (GstFaacClass),
100 101 102 103 104
      (GBaseInitFunc) gst_faac_base_init,
      NULL,
      (GClassInitFunc) gst_faac_class_init,
      NULL,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
105
      sizeof (GstFaac),
106 107 108 109 110
      0,
      (GInstanceInitFunc) gst_faac_init,
    };

    gst_faac_type = g_type_register_static (GST_TYPE_ELEMENT,
111
        "GstFaac", &gst_faac_info, 0);
112 113 114 115 116 117
  }

  return gst_faac_type;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118
gst_faac_base_init (GstFaacClass * klass)
119 120 121
{
  GstElementDetails gst_faac_details = {
    "Free AAC Encoder (FAAC)",
122
    "Codec/Encoder/Audio",
123 124 125 126 127 128
    "Free MPEG-2/4 AAC encoder",
    "Ronald Bultje <rbultje@ronald.bitfreak.net>",
  };
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
129
      gst_static_pad_template_get (&src_template));
130
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
131
      gst_static_pad_template_get (&sink_template));
132 133 134 135 136 137 138 139 140 141 142 143

  gst_element_class_set_details (element_class, &gst_faac_details);
}

#define GST_TYPE_FAAC_PROFILE (gst_faac_profile_get_type ())
static GType
gst_faac_profile_get_type (void)
{
  static GType gst_faac_profile_type = 0;

  if (!gst_faac_profile_type) {
    static GEnumValue gst_faac_profile[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
144 145 146 147 148
      {MAIN, "MAIN", "Main profile"},
      {LOW, "LOW", "Low complexity profile"},
      {SSR, "SSR", "Scalable sampling rate profile"},
      {LTP, "LTP", "Long term prediction profile"},
      {0, NULL, NULL},
149 150 151
    };

    gst_faac_profile_type = g_enum_register_static ("GstFaacProfile",
152
        gst_faac_profile);
153 154 155 156 157 158 159 160 161 162 163 164 165
  }

  return gst_faac_profile_type;
}

#define GST_TYPE_FAAC_SHORTCTL (gst_faac_shortctl_get_type ())
static GType
gst_faac_shortctl_get_type (void)
{
  static GType gst_faac_shortctl_type = 0;

  if (!gst_faac_shortctl_type) {
    static GEnumValue gst_faac_shortctl[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166 167 168 169
      {SHORTCTL_NORMAL, "SHORTCTL_NORMAL", "Normal block type"},
      {SHORTCTL_NOSHORT, "SHORTCTL_NOSHORT", "No short blocks"},
      {SHORTCTL_NOLONG, "SHORTCTL_NOLONG", "No long blocks"},
      {0, NULL, NULL},
170 171 172
    };

    gst_faac_shortctl_type = g_enum_register_static ("GstFaacShortCtl",
173
        gst_faac_shortctl);
174 175 176 177 178
  }

  return gst_faac_shortctl_type;
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#define GST_TYPE_FAAC_OUTPUTFORMAT (gst_faac_outputformat_get_type ())
static GType
gst_faac_outputformat_get_type (void)
{
  static GType gst_faac_outputformat_type = 0;

  if (!gst_faac_outputformat_type) {
    static GEnumValue gst_faac_outputformat[] = {
      {0, "OUTPUTFORMAT_RAW", "Raw AAC"},
      {1, "OUTPUTFORMAT_ADTS", "ADTS headers"},
      {0, NULL, NULL},
    };

    gst_faac_outputformat_type = g_enum_register_static ("GstFaacOutputFormat",
        gst_faac_outputformat);
  }

  return gst_faac_outputformat_type;
}

199
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
200
gst_faac_class_init (GstFaacClass * klass)
201 202 203 204 205 206 207 208
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  /* properties */
  g_object_class_install_property (gobject_class, ARG_BITRATE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
209
      g_param_spec_int ("bitrate", "Bitrate (bps)", "Bitrate in bits/sec",
210
          8 * 1024, 320 * 1024, 128 * 1024, G_PARAM_READWRITE));
211
  g_object_class_install_property (gobject_class, ARG_PROFILE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
212
      g_param_spec_enum ("profile", "Profile", "MPEG/AAC encoding profile",
213
          GST_TYPE_FAAC_PROFILE, MAIN, G_PARAM_READWRITE));
214
  g_object_class_install_property (gobject_class, ARG_TNS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
215
      g_param_spec_boolean ("tns", "TNS", "Use temporal noise shaping",
216
          FALSE, G_PARAM_READWRITE));
217
  g_object_class_install_property (gobject_class, ARG_MIDSIDE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
218
      g_param_spec_boolean ("midside", "Midside", "Allow mid/side encoding",
219
          TRUE, G_PARAM_READWRITE));
220
  g_object_class_install_property (gobject_class, ARG_SHORTCTL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
221
      g_param_spec_enum ("shortctl", "Block type",
222 223
          "Block type encorcing",
          GST_TYPE_FAAC_SHORTCTL, MAIN, G_PARAM_READWRITE));
224 225 226 227
  g_object_class_install_property (gobject_class, ARG_OUTPUTFORMAT,
      g_param_spec_enum ("outputformat", "Output format",
          "Format of output frames",
          GST_TYPE_FAAC_OUTPUTFORMAT, MAIN, G_PARAM_READWRITE));
228 229 230 231 232 233 234 235 236

  /* virtual functions */
  gstelement_class->change_state = gst_faac_change_state;

  gobject_class->set_property = gst_faac_set_property;
  gobject_class->get_property = gst_faac_get_property;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
237
gst_faac_init (GstFaac * faac)
238 239 240 241 242
{
  faac->handle = NULL;
  faac->samplerate = -1;
  faac->channels = -1;
  faac->cache = NULL;
243
  faac->out_time = 0;
244 245 246

  GST_FLAG_SET (faac, GST_ELEMENT_EVENT_AWARE);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
247 248 249
  faac->sinkpad =
      gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
      "sink");
250 251
  gst_pad_set_chain_function (faac->sinkpad, gst_faac_chain);
  gst_pad_set_link_function (faac->sinkpad, gst_faac_sinkconnect);
252
  gst_element_add_pad (GST_ELEMENT (faac), faac->sinkpad);
253

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
254 255 256
  faac->srcpad =
      gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
      "src");
257
  gst_pad_use_explicit_caps (faac->srcpad);
258 259 260 261 262 263
  gst_element_add_pad (GST_ELEMENT (faac), faac->srcpad);

  /* default properties */
  faac->bitrate = 1024 * 128;
  faac->profile = MAIN;
  faac->shortctl = SHORTCTL_NORMAL;
264
  faac->outputformat = 0;
265 266 267 268 269
  faac->tns = FALSE;
  faac->midside = TRUE;
}

static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
270
gst_faac_sinkconnect (GstPad * pad, const GstCaps * caps)
271 272
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
273 274 275 276
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  faacEncHandle *handle;
  gint channels, samplerate, depth;
  gulong samples, bytes, fmt = 0, bps = 0;
277

278
  if (!gst_caps_is_fixed (caps))
279 280 281 282 283 284 285 286 287 288 289
    return GST_PAD_LINK_DELAYED;

  if (faac->handle) {
    faacEncClose (faac->handle);
    faac->handle = NULL;
  }
  if (faac->cache) {
    gst_buffer_unref (faac->cache);
    faac->cache = NULL;
  }

290 291 292 293 294
  gst_structure_get_int (structure, "channels", &channels);
  gst_structure_get_int (structure, "rate", &samplerate);
  gst_structure_get_int (structure, "depth", &depth);

  /* open a new handle to the encoder */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
295
  if (!(handle = faacEncOpen (samplerate, channels, &samples, &bytes)))
296 297 298 299 300 301 302 303
    return GST_PAD_LINK_REFUSED;

  switch (depth) {
    case 16:
      fmt = FAAC_INPUT_16BIT;
      bps = 2;
      break;
    case 24:
304
      fmt = FAAC_INPUT_32BIT;   /* 24-in-32, actually */
305 306 307
      bps = 4;
      break;
    case 32:
308
      fmt = FAAC_INPUT_FLOAT;   /* see template, this is right */
309 310 311
      bps = 4;
      break;
  }
312

313 314 315
  if (!fmt) {
    faacEncClose (handle);
    return GST_PAD_LINK_REFUSED;
316 317
  }

318 319 320 321 322 323 324 325 326 327
  faac->format = fmt;
  faac->bps = bps;
  faac->handle = handle;
  faac->bytes = bytes;
  faac->samples = samples;
  faac->channels = channels;
  faac->samplerate = samplerate;

  /* if the other side was already set-up, redo that */
  if (GST_PAD_CAPS (faac->srcpad))
328
    return gst_faac_srcconnect (faac->srcpad);
329 330 331

  /* else, that'll be done later */
  return GST_PAD_LINK_OK;
332 333 334
}

static GstPadLinkReturn
335
gst_faac_srcconnect (GstPad * pad)
336 337
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
338
  GstCaps *caps;
339
  gint n;
340

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
341
  if (!faac->handle || (faac->samplerate == -1 || faac->channels == -1)) {
342 343 344 345
    return GST_PAD_LINK_DELAYED;
  }

  /* go through list */
346
  caps = gst_pad_get_allowed_caps (pad);
347 348
  for (n = 0; n < gst_caps_get_size (caps); n++) {
    GstStructure *structure = gst_caps_get_structure (caps, n);
349
    faacEncConfiguration *conf;
350
    gint mpegversion = 4;
351 352
    GstCaps *newcaps;
    GstPadLinkReturn ret;
353 354
    guint8 *c_data;
    gulong c_size;
355

356
    gst_structure_get_int (structure, "mpegversion", &mpegversion);
357 358 359 360 361 362 363 364 365 366

    /* new conf */
    conf = faacEncGetCurrentConfiguration (faac->handle);
    conf->mpegVersion = (mpegversion == 4) ? MPEG4 : MPEG2;
    conf->aacObjectType = faac->profile;
    conf->allowMidside = faac->midside;
    conf->useLfe = 0;
    conf->useTns = faac->tns;
    conf->bitRate = faac->bitrate;
    conf->inputFormat = faac->format;
367
    conf->outputFormat = faac->outputformat;
368 369 370 371 372 373
    conf->shortctl = faac->shortctl;
    if (!faacEncSetConfiguration (faac->handle, conf)) {
      GST_WARNING ("Faac doesn't support the current conf");
      continue;
    }

374
    newcaps = gst_caps_new_simple ("audio/mpeg",
375 376 377
        "mpegversion", G_TYPE_INT, mpegversion,
        "channels", G_TYPE_INT, faac->channels,
        "rate", G_TYPE_INT, faac->samplerate, NULL);
378 379 380
    if ((ret =
            faacEncGetDecoderSpecificInfo (faac->handle, &c_data,
                &c_size)) == 0) {
381 382 383 384 385 386
      GstBuffer *data = gst_buffer_new_and_alloc (c_size);

      memcpy (GST_BUFFER_DATA (data), c_data, c_size);
      gst_caps_set_simple (newcaps, "codec_data", GST_TYPE_BUFFER, data, NULL);
      gst_buffer_unref (data);
    }
387 388 389 390
    if (gst_pad_set_explicit_caps (faac->srcpad, newcaps)) {
      gst_caps_free (caps);
      gst_caps_free (newcaps);
      return GST_PAD_LINK_OK;
391
    }
392 393

    gst_caps_free (newcaps);
394 395
  }

396 397
  gst_caps_free (caps);

398 399 400 401
  return GST_PAD_LINK_REFUSED;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
402
gst_faac_chain (GstPad * pad, GstData * data)
403 404 405 406 407 408 409 410 411 412
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
  GstBuffer *inbuf, *outbuf, *subbuf;
  guint size, ret_size, in_size, frame_size;

  if (GST_IS_EVENT (data)) {
    GstEvent *event = GST_EVENT (data);

    switch (GST_EVENT_TYPE (event)) {
      case GST_EVENT_EOS:
413 414 415 416 417 418 419 420 421 422 423 424 425
        /* flush first */
        while (1) {
          outbuf = gst_buffer_new_and_alloc (faac->bytes);
          if ((ret_size = faacEncEncode (faac->handle,
                      NULL, 0, GST_BUFFER_DATA (outbuf), faac->bytes)) < 0) {
            GST_ELEMENT_ERROR (faac, LIBRARY, ENCODE, (NULL), (NULL));
            gst_event_unref (event);
            gst_buffer_unref (outbuf);
            return;
          }

          if (ret_size > 0) {
            GST_BUFFER_SIZE (outbuf) = ret_size;
426 427 428 429
            GST_BUFFER_TIMESTAMP (outbuf) = faac->out_time;
            GST_BUFFER_DURATION (outbuf) = GST_SECOND * faac->samples /
                (faac->channels * faac->samplerate);
            faac->out_time += GST_BUFFER_DURATION (outbuf);
430 431 432 433 434 435 436 437 438
            gst_pad_push (faac->srcpad, GST_DATA (outbuf));
          } else {
            break;
          }
        }

        gst_element_set_eos (GST_ELEMENT (faac));
        gst_pad_push (faac->srcpad, data);
        return;
439
      default:
440 441
        gst_pad_event_default (pad, event);
        return;
442 443 444 445 446 447
    }
  }

  inbuf = GST_BUFFER (data);

  if (!faac->handle) {
448
    GST_ELEMENT_ERROR (faac, CORE, NEGOTIATION, (NULL),
449
        ("format wasn't negotiated before chain function"));
450 451 452 453 454
    gst_buffer_unref (inbuf);
    return;
  }

  if (!GST_PAD_CAPS (faac->srcpad)) {
455
    if (gst_faac_srcconnect (faac->srcpad) <= 0) {
456
      GST_ELEMENT_ERROR (faac, CORE, NEGOTIATION, (NULL),
457
          ("failed to negotiate MPEG/AAC format with next element"));
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      gst_buffer_unref (inbuf);
      return;
    }
  }

  size = GST_BUFFER_SIZE (inbuf);
  in_size = size;
  if (faac->cache)
    in_size += GST_BUFFER_SIZE (faac->cache);
  frame_size = faac->samples * faac->bps;

  while (1) {
    /* do we have enough data for one frame? */
    if (in_size / faac->bps < faac->samples) {
      if (in_size > size) {
473 474 475 476 477 478 479 480
        GstBuffer *merge;

        /* this is panic! we got a buffer, but still don't have enough
         * data. Merge them and retry in the next cycle... */
        merge = gst_buffer_merge (faac->cache, inbuf);
        gst_buffer_unref (faac->cache);
        gst_buffer_unref (inbuf);
        faac->cache = merge;
481
      } else if (in_size == size) {
482 483
        /* this shouldn't happen, but still... */
        faac->cache = inbuf;
484
      } else if (in_size > 0) {
485 486 487 488 489 490 491
        faac->cache = gst_buffer_create_sub (inbuf, size - in_size, in_size);
        GST_BUFFER_DURATION (faac->cache) =
            GST_BUFFER_DURATION (inbuf) * GST_BUFFER_SIZE (faac->cache) / size;
        GST_BUFFER_TIMESTAMP (faac->cache) =
            GST_BUFFER_TIMESTAMP (inbuf) + (GST_BUFFER_DURATION (inbuf) *
            (size - in_size) / size);
        gst_buffer_unref (inbuf);
492
      } else {
493
        gst_buffer_unref (inbuf);
494
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
495

496 497 498 499 500
      return;
    }

    /* create the frame */
    if (in_size > size) {
501
      GstBuffer *merge;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
502

503 504 505
      /* merge */
      subbuf = gst_buffer_create_sub (inbuf, 0, frame_size - (in_size - size));
      GST_BUFFER_DURATION (subbuf) =
506
          GST_BUFFER_DURATION (inbuf) * GST_BUFFER_SIZE (subbuf) / size;
507 508 509 510
      merge = gst_buffer_merge (faac->cache, subbuf);
      gst_buffer_unref (faac->cache);
      gst_buffer_unref (subbuf);
      subbuf = merge;
511 512 513 514
      faac->cache = NULL;
    } else {
      subbuf = gst_buffer_create_sub (inbuf, size - in_size, frame_size);
      GST_BUFFER_DURATION (subbuf) =
515
          GST_BUFFER_DURATION (inbuf) * GST_BUFFER_SIZE (subbuf) / size;
516
      GST_BUFFER_TIMESTAMP (subbuf) =
517 518
          GST_BUFFER_TIMESTAMP (inbuf) + (GST_BUFFER_DURATION (inbuf) *
          (size - in_size) / size);
519 520 521 522
    }

    outbuf = gst_buffer_new_and_alloc (faac->bytes);
    if ((ret_size = faacEncEncode (faac->handle,
523 524 525
                (gint32 *) GST_BUFFER_DATA (subbuf),
                GST_BUFFER_SIZE (subbuf) / faac->bps,
                GST_BUFFER_DATA (outbuf), faac->bytes)) < 0) {
526
      GST_ELEMENT_ERROR (faac, LIBRARY, ENCODE, (NULL), (NULL));
527 528 529 530 531 532 533
      gst_buffer_unref (inbuf);
      gst_buffer_unref (subbuf);
      return;
    }

    if (ret_size > 0) {
      GST_BUFFER_SIZE (outbuf) = ret_size;
534 535 536 537
      GST_BUFFER_TIMESTAMP (outbuf) = faac->out_time;
      GST_BUFFER_DURATION (outbuf) = GST_SECOND * faac->samples /
          (faac->channels * faac->samplerate);
      faac->out_time += GST_BUFFER_DURATION (outbuf);
538 539 540 541 542 543 544 545 546
      gst_pad_push (faac->srcpad, GST_DATA (outbuf));
    }

    in_size -= frame_size;
    gst_buffer_unref (subbuf);
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
547 548
gst_faac_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
  GstFaac *faac = GST_FAAC (object);

  switch (prop_id) {
    case ARG_BITRATE:
      faac->bitrate = g_value_get_int (value);
      break;
    case ARG_PROFILE:
      faac->profile = g_value_get_enum (value);
      break;
    case ARG_TNS:
      faac->tns = g_value_get_boolean (value);
      break;
    case ARG_MIDSIDE:
      faac->midside = g_value_get_boolean (value);
      break;
    case ARG_SHORTCTL:
      faac->shortctl = g_value_get_enum (value);
      break;
568 569 570
    case ARG_OUTPUTFORMAT:
      faac->outputformat = g_value_get_enum (value);
      break;
571 572 573 574 575 576 577
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
578 579
gst_faac_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
{
  GstFaac *faac = GST_FAAC (object);

  switch (prop_id) {
    case ARG_BITRATE:
      g_value_set_int (value, faac->bitrate);
      break;
    case ARG_PROFILE:
      g_value_set_enum (value, faac->profile);
      break;
    case ARG_TNS:
      g_value_set_boolean (value, faac->tns);
      break;
    case ARG_MIDSIDE:
      g_value_set_boolean (value, faac->midside);
      break;
    case ARG_SHORTCTL:
      g_value_set_enum (value, faac->shortctl);
      break;
599 600 601
    case ARG_OUTPUTFORMAT:
      g_value_set_enum (value, faac->outputformat);
      break;
602 603 604 605 606 607 608
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstElementStateReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
609
gst_faac_change_state (GstElement * element)
610 611 612 613 614 615
{
  GstFaac *faac = GST_FAAC (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_PAUSED_TO_READY:
      if (faac->handle) {
616 617
        faacEncClose (faac->handle);
        faac->handle = NULL;
618 619
      }
      if (faac->cache) {
620 621
        gst_buffer_unref (faac->cache);
        faac->cache = NULL;
622
      }
623
      faac->out_time = 0;
624 625 626 627 628 629 630 631 632 633 634 635 636 637
      faac->samplerate = -1;
      faac->channels = -1;
      break;
    default:
      break;
  }

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element);

  return GST_STATE_SUCCESS;
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
638
plugin_init (GstPlugin * plugin)
639
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
640
  return gst_element_register (plugin, "faac", GST_RANK_NONE, GST_TYPE_FAAC);
641 642
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
643 644 645 646 647
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "faac",
    "Free AAC Encoder (FAAC)",
    plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)