gstfaac.c 20.2 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
24
/* 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

#include "gstfaac.h"
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
#include <string.h>

#define SINK_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]; "       \
\
    "audio/x-raw-int, "                \
    "endianness = (int) BYTE_ORDER, "  \
    "signed = (boolean) true, "        \
    "width = (int) 32, "               \
    "depth = (int) { 24, 32 }, "       \
    "rate = (int) [ 8000, 96000], "    \
    "channels = (int) [ 1, 6]; "       \
\
    "audio/x-raw-float, "              \
    "endianness = (int) BYTE_ORDER, "  \
    "width = (int) 32, "               \
    "rate = (int) [ 8000, 96000], "    \
    "channels = (int) [ 1, 6]"

#define SRC_CAPS \
    "audio/mpeg, "                     \
    "mpegversion = (int) { 4, 2 }, "   \
    "channels = (int) [ 1, 6 ], "      \
    "rate = (int) [ 8000, 96000 ]"
55

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56
57
58
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
59
    GST_STATIC_CAPS (SRC_CAPS));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
60
61
62
63

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
64
    GST_STATIC_CAPS (SINK_CAPS));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
65
66
67

enum
{
68
  ARG_0,
69
  ARG_OUTPUTFORMAT,
70
71
72
73
74
  ARG_BITRATE,
  ARG_PROFILE,
  ARG_TNS,
  ARG_MIDSIDE,
  ARG_SHORTCTL
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
75
      /* FILL ME */
76
77
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78
79
80
static void gst_faac_base_init (GstFaacClass * klass);
static void gst_faac_class_init (GstFaacClass * klass);
static void gst_faac_init (GstFaac * faac);
81

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82
83
84
85
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);
86

87
88
89
static gboolean gst_faac_sink_setcaps (GstPad * pad, GstCaps * caps);
static gboolean gst_faac_src_setcaps (GstPad * pad, GstCaps * caps);
static GstFlowReturn gst_faac_chain (GstPad * pad, GstBuffer * data);
90
91
static GstStateChangeReturn gst_faac_change_state (GstElement * element,
    GstStateChange transition);
92
93

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

95
96
97
98
99
100
101
102
103
/* 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
104
      sizeof (GstFaacClass),
105
106
107
108
109
      (GBaseInitFunc) gst_faac_base_init,
      NULL,
      (GClassInitFunc) gst_faac_class_init,
      NULL,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
110
      sizeof (GstFaac),
111
112
113
114
115
      0,
      (GInstanceInitFunc) gst_faac_init,
    };

    gst_faac_type = g_type_register_static (GST_TYPE_ELEMENT,
116
        "GstFaac", &gst_faac_info, 0);
117
118
119
120
121
122
  }

  return gst_faac_type;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
123
gst_faac_base_init (GstFaacClass * klass)
124
125
126
{
  GstElementDetails gst_faac_details = {
    "Free AAC Encoder (FAAC)",
127
    "Codec/Encoder/Audio",
128
129
130
131
132
133
    "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
134
      gst_static_pad_template_get (&src_template));
135
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
136
      gst_static_pad_template_get (&sink_template));
137
138
139
140
141
142
143
144
145
146
147
148

  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
149
150
151
152
153
      {MAIN, "MAIN", "Main profile"},
      {LOW, "LOW", "Low complexity profile"},
      {SSR, "SSR", "Scalable sampling rate profile"},
      {LTP, "LTP", "Long term prediction profile"},
      {0, NULL, NULL},
154
155
156
    };

    gst_faac_profile_type = g_enum_register_static ("GstFaacProfile",
157
        gst_faac_profile);
158
159
160
161
162
163
164
165
166
167
168
169
170
  }

  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
171
172
173
174
      {SHORTCTL_NORMAL, "SHORTCTL_NORMAL", "Normal block type"},
      {SHORTCTL_NOSHORT, "SHORTCTL_NOSHORT", "No short blocks"},
      {SHORTCTL_NOLONG, "SHORTCTL_NOLONG", "No long blocks"},
      {0, NULL, NULL},
175
176
177
    };

    gst_faac_shortctl_type = g_enum_register_static ("GstFaacShortCtl",
178
        gst_faac_shortctl);
179
180
181
182
183
  }

  return gst_faac_shortctl_type;
}

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#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;
}

204
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
205
gst_faac_class_init (GstFaacClass * klass)
206
207
208
209
210
211
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

212
213
214
  gobject_class->set_property = gst_faac_set_property;
  gobject_class->get_property = gst_faac_get_property;

215
216
  /* properties */
  g_object_class_install_property (gobject_class, ARG_BITRATE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
217
      g_param_spec_int ("bitrate", "Bitrate (bps)", "Bitrate in bits/sec",
218
          8 * 1024, 320 * 1024, 128 * 1024, G_PARAM_READWRITE));
219
  g_object_class_install_property (gobject_class, ARG_PROFILE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220
      g_param_spec_enum ("profile", "Profile", "MPEG/AAC encoding profile",
221
          GST_TYPE_FAAC_PROFILE, MAIN, G_PARAM_READWRITE));
222
  g_object_class_install_property (gobject_class, ARG_TNS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
223
      g_param_spec_boolean ("tns", "TNS", "Use temporal noise shaping",
224
          FALSE, G_PARAM_READWRITE));
225
  g_object_class_install_property (gobject_class, ARG_MIDSIDE,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
226
      g_param_spec_boolean ("midside", "Midside", "Allow mid/side encoding",
227
          TRUE, G_PARAM_READWRITE));
228
  g_object_class_install_property (gobject_class, ARG_SHORTCTL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
229
      g_param_spec_enum ("shortctl", "Block type",
230
231
          "Block type encorcing",
          GST_TYPE_FAAC_SHORTCTL, MAIN, G_PARAM_READWRITE));
232
233
234
235
  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));
236
237
238
239
240
241

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

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
242
gst_faac_init (GstFaac * faac)
243
244
245
246
247
248
249
250
{
  faac->handle = NULL;
  faac->samplerate = -1;
  faac->channels = -1;
  faac->cache = NULL;
  faac->cache_time = GST_CLOCK_TIME_NONE;
  faac->cache_duration = 0;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
251
252
253
  faac->sinkpad =
      gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
      "sink");
254
  gst_pad_set_chain_function (faac->sinkpad, gst_faac_chain);
255
256
  gst_pad_set_setcaps_function (faac->sinkpad, gst_faac_sink_setcaps);
  gst_element_add_pad (GST_ELEMENT (faac), faac->sinkpad);
257

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
258
259
260
  faac->srcpad =
      gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
      "src");
261
  gst_pad_set_setcaps_function (faac->srcpad, gst_faac_src_setcaps);
262
263
264
265
266
267
  gst_element_add_pad (GST_ELEMENT (faac), faac->srcpad);

  /* default properties */
  faac->bitrate = 1024 * 128;
  faac->profile = MAIN;
  faac->shortctl = SHORTCTL_NORMAL;
268
  faac->outputformat = 0;
269
270
271
272
  faac->tns = FALSE;
  faac->midside = TRUE;
}

273
274
static gboolean
gst_faac_sink_setcaps (GstPad * pad, GstCaps * caps)
275
276
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
277
278
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  faacEncHandle *handle;
279
  gint channels, samplerate, width;
280
  gulong samples, bytes, fmt = 0, bps = 0;
281
  gboolean result = FALSE;
282

283
  if (!gst_caps_is_fixed (caps))
284
    goto done;                  /* GST_PAD_LINK_DELAYED; */
285
286
287
288
289
290
291
292
293
294

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

295
296
297
298
  if (!gst_structure_get_int (structure, "channels", &channels) ||
      !gst_structure_get_int (structure, "rate", &samplerate)) {
    goto done;
  }
299

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
300
  if (!(handle = faacEncOpen (samplerate, channels, &samples, &bytes)))
301
    goto done;
302

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  if (gst_structure_has_name (structure, "audio/x-raw-int")) {
    gst_structure_get_int (structure, "width", &width);
    switch (width) {
      case 16:
        fmt = FAAC_INPUT_16BIT;
        bps = 2;
        break;
      case 24:
      case 32:
        fmt = FAAC_INPUT_32BIT;
        bps = 4;
        break;
      default:
        g_return_val_if_reached (FALSE);
    }
  } else if (gst_structure_has_name (structure, "audio/x-raw-float")) {
    fmt = FAAC_INPUT_FLOAT;
    bps = 4;
321
  }
322

323
324
  if (!fmt) {
    faacEncClose (handle);
325
    goto done;
326
327
  }

328
329
330
331
332
333
334
335
336
  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 */
337
338
  if (GST_PAD_CAPS (faac->srcpad)) {
    result = gst_faac_src_setcaps (faac->srcpad,
339
        gst_pad_get_allowed_caps (faac->srcpad));
340
341
    goto done;
  }
342
343

  /* else, that'll be done later */
344
345
346
347
348
  result = TRUE;

done:
  gst_object_unref (faac);
  return result;
349
350
}

351
352
static gboolean
gst_faac_src_setcaps (GstPad * pad, GstCaps * caps)
353
354
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
355
  gint n;
356
  gboolean result = FALSE;
357

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
358
  if (!faac->handle || (faac->samplerate == -1 || faac->channels == -1)) {
359
    goto done;
360
361
362
  }

  /* we do samplerate/channels ourselves */
363
364
  for (n = 0; n < gst_caps_get_size (caps); n++) {
    GstStructure *structure = gst_caps_get_structure (caps, n);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
365

366
367
    gst_structure_remove_field (structure, "rate");
    gst_structure_remove_field (structure, "channels");
368
369
370
371
  }

  /* go through list */
  caps = gst_caps_normalize (caps);
372
373
  for (n = 0; n < gst_caps_get_size (caps); n++) {
    GstStructure *structure = gst_caps_get_structure (caps, n);
374
375
376
377
    faacEncConfiguration *conf;
    gint mpegversion = 0;
    GstCaps *newcaps;

378
    gst_structure_get_int (structure, "mpegversion", &mpegversion);
379
380
381
382
383
384
385
386
387
388

    /* 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;
389
    conf->outputFormat = faac->outputformat;
390
391
392
393
394
395
    conf->shortctl = faac->shortctl;
    if (!faacEncSetConfiguration (faac->handle, conf)) {
      GST_WARNING ("Faac doesn't support the current conf");
      continue;
    }

396
    newcaps = gst_caps_new_simple ("audio/mpeg",
397
398
399
        "mpegversion", G_TYPE_INT, mpegversion,
        "channels", G_TYPE_INT, faac->channels,
        "rate", G_TYPE_INT, faac->samplerate, NULL);
400
401
402
403

    /* negotiate with these caps */
    GST_DEBUG ("here are the caps: %" GST_PTR_FORMAT, newcaps);
    if (gst_pad_set_caps (faac->srcpad, newcaps) == TRUE)
404
      result = TRUE;
405
406
  }

407
408
409
done:
  gst_object_unref (faac);
  return result;
410
411
}

412
413
static GstFlowReturn
gst_faac_chain (GstPad * pad, GstBuffer * data)
414
415
416
417
{
  GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
  GstBuffer *inbuf, *outbuf, *subbuf;
  guint size, ret_size, in_size, frame_size;
418
  GstFlowReturn result = GST_FLOW_OK;
419
420
421
422
423
424

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

    switch (GST_EVENT_TYPE (event)) {
      case GST_EVENT_EOS:
425
426
427
428
429
430
431
432
        /* 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);
433
434
            result = GST_FLOW_ERROR;
            goto done;
435
436
437
438
439
440
          }

          if (ret_size > 0) {
            GST_BUFFER_SIZE (outbuf) = ret_size;
            GST_BUFFER_TIMESTAMP (outbuf) = 0;
            GST_BUFFER_DURATION (outbuf) = 0;
441
            gst_pad_push (faac->srcpad, outbuf);
442
443
444
445
446
          } else {
            break;
          }
        }

447
        gst_pad_push_event (faac->srcpad, gst_event_new_eos ());
448
        gst_pad_push (faac->srcpad, data);
449
        goto done;
450
      default:
451
        gst_pad_event_default (pad, event);
452
        goto done;
453
454
455
456
457
458
    }
  }

  inbuf = GST_BUFFER (data);

  if (!faac->handle) {
459
    GST_ELEMENT_ERROR (faac, CORE, NEGOTIATION, (NULL),
460
        ("format wasn't negotiated before chain function"));
461
    gst_buffer_unref (inbuf);
462
463
    result = GST_FLOW_ERROR;
    goto done;
464
465
466
  }

  if (!GST_PAD_CAPS (faac->srcpad)) {
467
    if (gst_faac_src_setcaps (faac->srcpad,
468
            gst_pad_get_allowed_caps (faac->srcpad)) <= 0) {
469
      GST_ELEMENT_ERROR (faac, CORE, NEGOTIATION, (NULL),
470
          ("failed to negotiate MPEG/AAC format with next element"));
471
      gst_buffer_unref (inbuf);
472
473
      result = GST_FLOW_ERROR;
      goto done;
474
475
476
477
478
479
480
481
482
483
484
485
486
    }
  }

  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) {
487
488
489
490
491
492
493
494
        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;
495
      } else if (in_size == size) {
496
497
        /* this shouldn't happen, but still... */
        faac->cache = inbuf;
498
      } else if (in_size > 0) {
499
500
501
502
503
504
505
        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);
506
      } else {
507
        gst_buffer_unref (inbuf);
508
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
509

510
      goto done;
511
512
513
514
    }

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

517
518
519
      /* merge */
      subbuf = gst_buffer_create_sub (inbuf, 0, frame_size - (in_size - size));
      GST_BUFFER_DURATION (subbuf) =
520
          GST_BUFFER_DURATION (inbuf) * GST_BUFFER_SIZE (subbuf) / size;
521
522
523
524
      merge = gst_buffer_merge (faac->cache, subbuf);
      gst_buffer_unref (faac->cache);
      gst_buffer_unref (subbuf);
      subbuf = merge;
525
526
527
528
      faac->cache = NULL;
    } else {
      subbuf = gst_buffer_create_sub (inbuf, size - in_size, frame_size);
      GST_BUFFER_DURATION (subbuf) =
529
          GST_BUFFER_DURATION (inbuf) * GST_BUFFER_SIZE (subbuf) / size;
530
      GST_BUFFER_TIMESTAMP (subbuf) =
531
532
          GST_BUFFER_TIMESTAMP (inbuf) + (GST_BUFFER_DURATION (inbuf) *
          (size - in_size) / size);
533
534
535
536
    }

    outbuf = gst_buffer_new_and_alloc (faac->bytes);
    if ((ret_size = faacEncEncode (faac->handle,
537
538
539
                (gint32 *) GST_BUFFER_DATA (subbuf),
                GST_BUFFER_SIZE (subbuf) / faac->bps,
                GST_BUFFER_DATA (outbuf), faac->bytes)) < 0) {
540
      GST_ELEMENT_ERROR (faac, LIBRARY, ENCODE, (NULL), (NULL));
541
542
      gst_buffer_unref (inbuf);
      gst_buffer_unref (subbuf);
543
544
      result = GST_FLOW_ERROR;
      goto done;
545
546
547
548
549
    }

    if (ret_size > 0) {
      GST_BUFFER_SIZE (outbuf) = ret_size;
      if (faac->cache_time != GST_CLOCK_TIME_NONE) {
550
551
        GST_BUFFER_TIMESTAMP (outbuf) = faac->cache_time;
        faac->cache_time = GST_CLOCK_TIME_NONE;
552
      } else
553
        GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (subbuf);
554
555
      GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (subbuf);
      if (faac->cache_duration) {
556
557
        GST_BUFFER_DURATION (outbuf) += faac->cache_duration;
        faac->cache_duration = 0;
558
      }
559
      gst_pad_push (faac->srcpad, outbuf);
560
561
562
563
564
565
566
567
568
569
570
    } else {
      /* FIXME: what I'm doing here isn't fully correct, but there
       * really isn't a better way yet.
       * Problem is that libfaac caches buffers (for encoding
       * purposes), so the timestamp of the outgoing buffer isn't
       * the same as the timestamp of the data that I pushed in.
       * However, I don't know the delay between those two so I
       * cannot really say aything about it. This is a bad guess. */

      gst_buffer_unref (outbuf);
      if (faac->cache_time != GST_CLOCK_TIME_NONE)
571
        faac->cache_time = GST_BUFFER_TIMESTAMP (subbuf);
572
573
574
575
576
577
      faac->cache_duration += GST_BUFFER_DURATION (subbuf);
    }

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

579
580
done:
  gst_object_unref (faac);
581
  return result;
582
583
584
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
585
586
gst_faac_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
{
  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;
606
607
608
    case ARG_OUTPUTFORMAT:
      faac->outputformat = g_value_get_enum (value);
      break;
609
610
611
612
613
614
615
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
616
617
gst_faac_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
{
  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;
637
638
639
    case ARG_OUTPUTFORMAT:
      g_value_set_enum (value, faac->outputformat);
      break;
640
641
642
643
644
645
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

646
647
static GstStateChangeReturn
gst_faac_change_state (GstElement * element, GstStateChange transition)
648
649
650
{
  GstFaac *faac = GST_FAAC (element);

651
652
  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
653
      if (faac->handle) {
654
655
        faacEncClose (faac->handle);
        faac->handle = NULL;
656
657
      }
      if (faac->cache) {
658
659
        gst_buffer_unref (faac->cache);
        faac->cache = NULL;
660
661
662
663
664
665
666
667
668
669
670
      }
      faac->cache_time = GST_CLOCK_TIME_NONE;
      faac->cache_duration = 0;
      faac->samplerate = -1;
      faac->channels = -1;
      break;
    default:
      break;
  }

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
671
    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
672

673
  return GST_STATE_CHANGE_SUCCESS;
674
675
676
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
677
plugin_init (GstPlugin * plugin)
678
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
679
  return gst_element_register (plugin, "faac", GST_RANK_NONE, GST_TYPE_FAAC);
680
681
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
682
683
684
685
686
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "faac",
    "Free AAC Encoder (FAAC)",
    plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)