gstfaad.c 41.9 KB
Newer Older
1
2
/* GStreamer FAAD (Free AAC Decoder) plugin
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * 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>
26
#include <gst/audio/audio.h>
Ronald S. Bultje's avatar
Ronald S. Bultje committed
27
#include <gst/audio/multichannel.h>
28

29
30
/* These are the correct types for these functions, as defined in the source,
 * with types changed to match glib types, since those are defined for us.
31
32
 * However, upstream FAAD is distributed with a broken header file that defined
 * these wrongly (in a way which was broken on 64 bit systems).
33
 *
34
 * Upstream CVS still has the bug, but has also renamed all the public symbols
35
36
 * for Better Corporate Branding (or whatever), so we need to take that
 * (FAAD_IS_NEAAC) into account as well.
37
38
39
 *
 * We must call them using these definitions. Most distributions now have the
 * corrected header file (they distribute a patch along with the source), 
40
41
42
43
 * but not all, hence this Truly Evil Hack.
 *
 * Note: The prototypes don't need to be defined conditionaly, as the cpp will
 * do that for us.
44
 */
45
46
47
48
#ifdef FAAD_IS_NEAAC
#define NeAACDecInit NeAACDecInit_no_definition
#define NeAACDecInit2 NeAACDecInit2_no_definition
#else
49
50
#define faacDecInit faacDecInit_no_definition
#define faacDecInit2 faacDecInit2_no_definition
51
#endif
52
#include "gstfaad.h"
53
54
55
56
#ifdef FAAD_IS_NEAAC
#undef NeAACDecInit
#undef NeAACDecInit2
#else
57
58
#undef faacDecInit
#undef faacDecInit2
59
#endif
60

61
62
63
extern long faacDecInit (faacDecHandle, guint8 *, guint32, guint32 *, guint8 *);
extern int8_t faacDecInit2 (faacDecHandle, guint8 *, guint32,
    guint32 *, guint8 *);
64

65
66
67
GST_DEBUG_CATEGORY_STATIC (faad_debug);
#define GST_CAT_DEFAULT faad_debug

68
69
#define MAX_DECODE_ERRORS 5

70
static const GstElementDetails faad_details =
71
72
73
74
GST_ELEMENT_DETAILS ("AAC audio decoder",
    "Codec/Decoder/Audio",
    "Free MPEG-2/4 AAC decoder",
    "Ronald Bultje <rbultje@ronald.bitfreak.net>");
75

Ronald S. Bultje's avatar
Ronald S. Bultje committed
76
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
77
78
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
79
    GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) { 2, 4 }")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80
81
    );

Ronald S. Bultje's avatar
Ronald S. Bultje committed
82
83
84
85
86
87
88
89
90
#define STATIC_INT_CAPS(bpp) \
  "audio/x-raw-int, " \
    "endianness = (int) BYTE_ORDER, " \
    "signed = (bool) TRUE, " \
    "width = (int) " G_STRINGIFY (bpp) ", " \
    "depth = (int) " G_STRINGIFY (bpp) ", " \
    "rate = (int) [ 8000, 96000 ], " \
    "channels = (int) [ 1, 8 ]"

91
#if 0
Ronald S. Bultje's avatar
Ronald S. Bultje committed
92
93
94
95
96
97
#define STATIC_FLOAT_CAPS(bpp) \
  "audio/x-raw-float, " \
    "endianness = (int) BYTE_ORDER, " \
    "depth = (int) " G_STRINGIFY (bpp) ", " \
    "rate = (int) [ 8000, 96000 ], " \
    "channels = (int) [ 1, 8 ]"
98
#endif
Ronald S. Bultje's avatar
Ronald S. Bultje committed
99
100
101
102
103
104
105
106
107
108
109
110

/*
 * All except 16-bit integer are disabled until someone fixes FAAD.
 * FAAD allocates approximately 8*1024*2 bytes bytes, which is enough
 * for 1 frame (1024 samples) of 6 channel (5.1) 16-bit integer 16bpp
 * audio, but not for any other. You'll get random segfaults, crashes
 * and even valgrind goes crazy.
 */

#define STATIC_CAPS \
  STATIC_INT_CAPS (16)
#if 0
111
112
113
114
115
116
117
#define NOTUSED "; " \
STATIC_INT_CAPS (24) \
    "; " \
STATIC_INT_CAPS (32) \
    "; " \
STATIC_FLOAT_CAPS (32) \
    "; " \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
118
119
STATIC_FLOAT_CAPS (64)
#endif
120

121
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
122
123
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
124
    GST_STATIC_CAPS (STATIC_CAPS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125
126
    );

127
128
129
static void gst_faad_base_init (GstFaadClass * klass);
static void gst_faad_class_init (GstFaadClass * klass);
static void gst_faad_init (GstFaad * faad);
130
static void gst_faad_dispose (GObject * object);
131

132
static gboolean gst_faad_setcaps (GstPad * pad, GstCaps * caps);
133
static GstCaps *gst_faad_srcgetcaps (GstPad * pad);
134
135
136
static gboolean gst_faad_src_event (GstPad * pad, GstEvent * event);
static gboolean gst_faad_sink_event (GstPad * pad, GstEvent * event);
static gboolean gst_faad_src_query (GstPad * pad, GstQuery * query);
137
static GstFlowReturn gst_faad_chain (GstPad * pad, GstBuffer * buffer);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
138
139
static GstStateChangeReturn gst_faad_change_state (GstElement * element,
    GstStateChange transition);
140
141
static gboolean gst_faad_src_convert (GstFaad * faad, GstFormat src_format,
    gint64 src_val, GstFormat dest_format, gint64 * dest_val);
142
143
static gboolean gst_faad_open_decoder (GstFaad * faad);
static void gst_faad_close_decoder (GstFaad * faad);
144

145
static GstElementClass *parent_class;   /* NULL */
146
147
148
149
150
151
152
153

GType
gst_faad_get_type (void)
{
  static GType gst_faad_type = 0;

  if (!gst_faad_type) {
    static const GTypeInfo gst_faad_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
154
      sizeof (GstFaadClass),
155
156
157
158
159
      (GBaseInitFunc) gst_faad_base_init,
      NULL,
      (GClassInitFunc) gst_faad_class_init,
      NULL,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
      sizeof (GstFaad),
161
162
163
164
165
      0,
      (GInstanceInitFunc) gst_faad_init,
    };

    gst_faad_type = g_type_register_static (GST_TYPE_ELEMENT,
166
        "GstFaad", &gst_faad_info, 0);
167
168
169
170
171
172
  }

  return gst_faad_type;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
173
gst_faad_base_init (GstFaadClass * klass)
174
175
176
177
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
178
      gst_static_pad_template_get (&src_template));
179
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
180
      gst_static_pad_template_get (&sink_template));
181

182
  gst_element_class_set_details (element_class, &faad_details);
183
184

  GST_DEBUG_CATEGORY_INIT (faad_debug, "faad", 0, "AAC decoding");
185
186
187
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
188
gst_faad_class_init (GstFaadClass * klass)
189
{
190
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191
192
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);

193
  parent_class = g_type_class_peek_parent (klass);
194

195
196
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_faad_dispose);

197
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_faad_change_state);
198
199
200
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
201
gst_faad_init (GstFaad * faad)
202
203
204
205
{
  faad->handle = NULL;
  faad->samplerate = -1;
  faad->channels = -1;
206
  faad->tempbuf = NULL;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
207
208
209
  faad->need_channel_setup = TRUE;
  faad->channel_positions = NULL;
  faad->init = FALSE;
210
  faad->next_ts = 0;
211
  faad->prev_ts = GST_CLOCK_TIME_NONE;
212
213
214
  faad->bytes_in = 0;
  faad->sum_dur_out = 0;
  faad->packetised = FALSE;
215
  faad->error_count = 0;
216
  faad->segment = gst_segment_new ();
217

218
  faad->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
219
  gst_element_add_pad (GST_ELEMENT (faad), faad->sinkpad);
220
  gst_pad_set_event_function (faad->sinkpad,
221
      GST_DEBUG_FUNCPTR (gst_faad_sink_event));
222
223
224
225
  gst_pad_set_setcaps_function (faad->sinkpad,
      GST_DEBUG_FUNCPTR (gst_faad_setcaps));
  gst_pad_set_chain_function (faad->sinkpad,
      GST_DEBUG_FUNCPTR (gst_faad_chain));
226

227
  faad->srcpad = gst_pad_new_from_static_template (&src_template, "src");
228
  gst_pad_use_fixed_caps (faad->srcpad);
229
230
  gst_pad_set_getcaps_function (faad->srcpad,
      GST_DEBUG_FUNCPTR (gst_faad_srcgetcaps));
231
232
233
234
235
236
237
  gst_pad_set_query_function (faad->srcpad,
      GST_DEBUG_FUNCPTR (gst_faad_src_query));
  gst_pad_set_event_function (faad->srcpad,
      GST_DEBUG_FUNCPTR (gst_faad_src_event));
  gst_element_add_pad (GST_ELEMENT (faad), faad->srcpad);
}

238
239
240
241
242
243
244
245
246
247
248
249
250
251
static void
gst_faad_dispose (GObject * object)
{
  GstFaad *faad = GST_FAAD (object);

  if (faad->segment) {
    gst_segment_free (faad->segment);
    faad->segment = NULL;
  }

  G_OBJECT_CLASS (parent_class)->dispose (object);

}

252
253
254
255
256
257
258
259
260
261
262
static void
gst_faad_send_tags (GstFaad * faad)
{
  GstTagList *tags;

  tags = gst_tag_list_new ();

  gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
      GST_TAG_AUDIO_CODEC, "MPEG-4 AAC audio", NULL);

  gst_element_found_tags (GST_ELEMENT (faad), tags);
263
264
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
static gint
aac_rate_idx (gint rate)
{
  if (92017 <= rate)
    return 0;
  else if (75132 <= rate)
    return 1;
  else if (55426 <= rate)
    return 2;
  else if (46009 <= rate)
    return 3;
  else if (37566 <= rate)
    return 4;
  else if (27713 <= rate)
    return 5;
  else if (23004 <= rate)
    return 6;
  else if (18783 <= rate)
    return 7;
  else if (13856 <= rate)
    return 8;
  else if (11502 <= rate)
    return 9;
  else if (9391 <= rate)
    return 10;
  else
    return 11;
}

294
295
296
static gboolean
gst_faad_setcaps (GstPad * pad, GstCaps * caps)
{
297
298
299
300
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));
  GstStructure *str = gst_caps_get_structure (caps, 0);
  GstBuffer *buf;
  const GValue *value;
301

302
303
  /* Assume raw stream */
  faad->packetised = FALSE;
304

305
  if ((value = gst_structure_get_value (str, "codec_data"))) {
306
307
    guint32 samplerate;
    guint8 channels;
308
309
    guint8 *cdata;
    guint csize;
310
311
312

    /* We have codec data, means packetised stream */
    faad->packetised = TRUE;
313
314
315
316
317
318
319
    buf = gst_value_get_buffer (value);

    cdata = GST_BUFFER_DATA (buf);
    csize = GST_BUFFER_SIZE (buf);

    if (csize < 2)
      goto wrong_length;
320

321
    /* someone forgot that char can be unsigned when writing the API */
322
    if ((gint8) faacDecInit2 (faad->handle, cdata, csize, &samplerate,
323
324
            &channels) < 0)
      goto init_failed;
325

326
327
    GST_DEBUG_OBJECT (faad, "codec_data init: channels=%u, rate=%u", channels,
        samplerate);
328

329
330
331
332
333
334
335
336
337
338
339
    /* not updating these here, so they are updated in the
     * chain function, and new caps are created etc. */
    faad->samplerate = 0;
    faad->channels = 0;

    faad->init = TRUE;

    if (faad->tempbuf) {
      gst_buffer_unref (faad->tempbuf);
      faad->tempbuf = NULL;
    }
340
341
342
  } else if ((value = gst_structure_get_value (str, "framed")) &&
      g_value_get_boolean (value) == TRUE) {
    faad->packetised = TRUE;
343
    GST_DEBUG_OBJECT (faad, "we have packetized audio");
344
345
346
  } else {
    faad->init = FALSE;
  }
347

348
349
350
351
352
353
354
355
356
357
  faad->fake_codec_data[0] = 0;
  faad->fake_codec_data[1] = 0;

  if (faad->packetised) {
    gint rate, channels;

    if (gst_structure_get_int (str, "rate", &rate) &&
        gst_structure_get_int (str, "channels", &channels)) {
      gint rate_idx, profile;

358
      profile = 3;              /* 0=MAIN, 1=LC, 2=SSR, 3=LTP */
359
360
361
362
      rate_idx = aac_rate_idx (rate);

      faad->fake_codec_data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
      faad->fake_codec_data[1] = ((rate_idx & 0x1) << 7) | (channels << 3);
363
364
365
      GST_LOG_OBJECT (faad, "created fake codec data (%u,%u): 0x%x 0x%x", rate,
          channels, (int) faad->fake_codec_data[0],
          (int) faad->fake_codec_data[1]);
366
367
368
    }
  }

369
  faad->need_channel_setup = TRUE;
370

371
372
373
  if (!faad->packetised)
    gst_faad_send_tags (faad);

374
  return TRUE;
375
376

  /* ERRORS */
377
378
379
380
381
wrong_length:
  {
    GST_DEBUG_OBJECT (faad, "codec_data less than 2 bytes long");
    return FALSE;
  }
382
383
384
385
386
init_failed:
  {
    GST_DEBUG_OBJECT (faad, "faacDecInit2() failed");
    return FALSE;
  }
387
388
389
}


Ronald S. Bultje's avatar
Ronald S. Bultje committed
390
391
392
/*
 * Channel identifier conversion - caller g_free()s result!
 */
393
/*
Ronald S. Bultje's avatar
Ronald S. Bultje committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
static guchar *
gst_faad_chanpos_from_gst (GstAudioChannelPosition * pos, guint num)
{
  guchar *fpos = g_new (guchar, num);
  guint n;

  for (n = 0; n < num; n++) {
    switch (pos[n]) {
      case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
        fpos[n] = FRONT_CHANNEL_LEFT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
        fpos[n] = FRONT_CHANNEL_RIGHT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
409
      case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
        fpos[n] = FRONT_CHANNEL_CENTER;
        break;
      case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
        fpos[n] = SIDE_CHANNEL_LEFT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
        fpos[n] = SIDE_CHANNEL_RIGHT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
        fpos[n] = BACK_CHANNEL_LEFT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
        fpos[n] = BACK_CHANNEL_RIGHT;
        break;
      case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
        fpos[n] = BACK_CHANNEL_CENTER;
        break;
      case GST_AUDIO_CHANNEL_POSITION_LFE:
        fpos[n] = LFE_CHANNEL;
        break;
      default:
        GST_WARNING ("Unsupported GST channel position 0x%x encountered",
            pos[n]);
        g_free (fpos);
        return NULL;
    }
  }

  return fpos;
}
440
*/
441

Ronald S. Bultje's avatar
Ronald S. Bultje committed
442
static GstAudioChannelPosition *
443
gst_faad_chanpos_to_gst (GstFaad * faad, guchar * fpos, guint num,
444
    gboolean * channel_map_failed)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
445
446
447
{
  GstAudioChannelPosition *pos = g_new (GstAudioChannelPosition, num);
  guint n;
448
  gboolean unknown_channel = FALSE;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
449

450
451
452
453
454
455
456
457
458
459
460
461
  *channel_map_failed = FALSE;

  /* special handling for the common cases for mono and stereo */
  if (num == 1 && fpos[0] == FRONT_CHANNEL_CENTER) {
    GST_DEBUG_OBJECT (faad, "mono common case; won't set channel positions");
    return NULL;
  } else if (num == 2 && fpos[0] == FRONT_CHANNEL_LEFT
      && fpos[1] == FRONT_CHANNEL_RIGHT) {
    GST_DEBUG_OBJECT (faad, "stereo common case; won't set channel positions");
    return NULL;
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
462
  for (n = 0; n < num; n++) {
463
    GST_DEBUG_OBJECT (faad, "faad channel %d as %d", n, fpos[n]);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
464
465
466
467
468
469
470
471
    switch (fpos[n]) {
      case FRONT_CHANNEL_LEFT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
        break;
      case FRONT_CHANNEL_RIGHT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
        break;
      case FRONT_CHANNEL_CENTER:
472
473
474
475
476
        /* argh, mono = center */
        if (num == 1)
          pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
        else
          pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
        break;
      case SIDE_CHANNEL_LEFT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
        break;
      case SIDE_CHANNEL_RIGHT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
        break;
      case BACK_CHANNEL_LEFT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
        break;
      case BACK_CHANNEL_RIGHT:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
        break;
      case BACK_CHANNEL_CENTER:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
        break;
      case LFE_CHANNEL:
        pos[n] = GST_AUDIO_CHANNEL_POSITION_LFE;
        break;
      default:
497
        GST_DEBUG_OBJECT (faad, "unknown channel %d at %d", fpos[n], n);
498
499
500
501
502
        unknown_channel = TRUE;
        break;
    }
  }
  if (unknown_channel) {
503
504
    g_free (pos);
    pos = NULL;
505
506
    switch (num) {
      case 1:{
507
508
        GST_DEBUG_OBJECT (faad,
            "FAAD reports unknown 1 channel mapping. Forcing to mono");
509
510
511
        break;
      }
      case 2:{
512
513
        GST_DEBUG_OBJECT (faad,
            "FAAD reports unknown 2 channel mapping. Forcing to stereo");
514
515
516
517
518
        break;
      }
      default:{
        GST_WARNING ("Unsupported FAAD channel position 0x%x encountered",
            fpos[n]);
519
        *channel_map_failed = TRUE;
520
521
        break;
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
522
523
524
525
526
527
    }
  }

  return pos;
}

528
static GstCaps *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
529
gst_faad_srcgetcaps (GstPad * pad)
530
{
531
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
532
  static GstAudioChannelPosition *supported_positions = NULL;
533
  static gint num_supported_positions = LFE_CHANNEL - FRONT_CHANNEL_CENTER + 1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
535
536
  GstCaps *templ;

  if (!supported_positions) {
537
    guchar *supported_fpos = g_new0 (guchar, num_supported_positions);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
538
    gint n;
539
    gboolean map_failed;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
540

Ronald S. Bultje's avatar
Ronald S. Bultje committed
541
    for (n = 0; n < num_supported_positions; n++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
542
543
      supported_fpos[n] = n + FRONT_CHANNEL_CENTER;
    }
544
545
    supported_positions = gst_faad_chanpos_to_gst (faad, supported_fpos,
        num_supported_positions, &map_failed);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
546
547
    g_free (supported_fpos);
  }
548

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
549
  if (faad->handle != NULL && faad->channels != -1 && faad->samplerate != -1) {
550
551
552
553
    GstCaps *caps = gst_caps_new_empty ();
    GstStructure *str;
    gint fmt[] = {
      FAAD_FMT_16BIT,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
554
#if 0
555
556
557
558
      FAAD_FMT_24BIT,
      FAAD_FMT_32BIT,
      FAAD_FMT_FLOAT,
      FAAD_FMT_DOUBLE,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
559
#endif
560
      -1
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
561
562
    }
    , n;
563
564

    for (n = 0; fmt[n] != -1; n++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
565
      switch (fmt[n]) {
566
567
568
569
570
        case FAAD_FMT_16BIT:
          str = gst_structure_new ("audio/x-raw-int",
              "signed", G_TYPE_BOOLEAN, TRUE,
              "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
          break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
571
#if 0
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
        case FAAD_FMT_24BIT:
          str = gst_structure_new ("audio/x-raw-int",
              "signed", G_TYPE_BOOLEAN, TRUE,
              "width", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24, NULL);
          break;
        case FAAD_FMT_32BIT:
          str = gst_structure_new ("audio/x-raw-int",
              "signed", G_TYPE_BOOLEAN, TRUE,
              "width", G_TYPE_INT, 32, "depth", G_TYPE_INT, 32, NULL);
          break;
        case FAAD_FMT_FLOAT:
          str = gst_structure_new ("audio/x-raw-float",
              "depth", G_TYPE_INT, 32, NULL);
          break;
        case FAAD_FMT_DOUBLE:
          str = gst_structure_new ("audio/x-raw-float",
              "depth", G_TYPE_INT, 64, NULL);
          break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
590
#endif
591
592
593
        default:
          str = NULL;
          break;
594
595
      }
      if (!str)
596
        continue;
597

598
      if (faad->samplerate > 0) {
599
        gst_structure_set (str, "rate", G_TYPE_INT, faad->samplerate, NULL);
600
      } else {
601
        gst_structure_set (str, "rate", GST_TYPE_INT_RANGE, 8000, 96000, NULL);
602
603
      }

604
      if (faad->channels > 0) {
605
        gst_structure_set (str, "channels", G_TYPE_INT, faad->channels, NULL);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
606

607
        /* put channel information here */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
608
609
        if (faad->channel_positions) {
          GstAudioChannelPosition *pos;
610
          gboolean map_failed;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
611

612
613
614
          pos = gst_faad_chanpos_to_gst (faad, faad->channel_positions,
              faad->channels, &map_failed);
          if (map_failed) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
615
616
617
            gst_structure_free (str);
            continue;
          }
618
619
620
621
          if (pos) {
            gst_audio_set_channel_positions (str, pos);
            g_free (pos);
          }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
622
623
624
625
        } else {
          gst_audio_set_structure_channel_positions_list (str,
              supported_positions, num_supported_positions);
        }
626
      } else {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
627
628
        gst_structure_set (str, "channels", GST_TYPE_INT_RANGE, 1, 8, NULL);
        /* we set channel positions later */
629
630
      }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
631
      gst_structure_set (str, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
632
633

      gst_caps_append_structure (caps, str);
634
635
    }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
636
637
638
639
    if (faad->channels == -1) {
      gst_audio_set_caps_channel_positions_list (caps,
          supported_positions, num_supported_positions);
    }
640
    gst_object_unref (faad);
641
642
643
    return caps;
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
644
645
646
647
648
  /* template with channel positions */
  templ = gst_caps_copy (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad)));
  gst_audio_set_caps_channel_positions_list (templ,
      supported_positions, num_supported_positions);

649
  gst_object_unref (faad);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
650
  return templ;
651
652
}

653
/*
654
static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
655
gst_faad_srcconnect (GstPad * pad, const GstCaps * caps)
656
{
657
658
  GstStructure *structure;
  const gchar *mimetype;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
659
  gint fmt = -1;
660
  gint depth, rate, channels;
661
662
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));

Ronald S. Bultje's avatar
Ronald S. Bultje committed
663
664
665
666
  structure = gst_caps_get_structure (caps, 0);

  if (!faad->handle || (faad->samplerate == -1 || faad->channels == -1) ||
      !faad->channel_positions) {
667
668
669
    return GST_PAD_LINK_DELAYED;
  }

670
  mimetype = gst_structure_get_name (structure);
671

672
673
  // Samplerate and channels are normally provided through
  // * the getcaps function 
674
675
676
677
678
  if (!gst_structure_get_int (structure, "channels", &channels) ||
      !gst_structure_get_int (structure, "rate", &rate) ||
      rate != faad->samplerate || channels != faad->channels) {
    return GST_PAD_LINK_REFUSED;
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
679

680
  // Another internal checkup. 
Ronald S. Bultje's avatar
Ronald S. Bultje committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  if (faad->need_channel_setup) {
    GstAudioChannelPosition *pos;
    guchar *fpos;
    guint i;

    pos = gst_audio_get_channel_positions (structure);
    if (!pos) {
      return GST_PAD_LINK_DELAYED;
    }
    fpos = gst_faad_chanpos_from_gst (pos, faad->channels);
    g_free (pos);
    if (!fpos)
      return GST_PAD_LINK_REFUSED;

    for (i = 0; i < faad->channels; i++) {
      if (fpos[i] != faad->channel_positions[i]) {
        g_free (fpos);
        return GST_PAD_LINK_REFUSED;
      }
    }
    g_free (fpos);
  }

704
705
706
707
  if (!strcmp (mimetype, "audio/x-raw-int")) {
    gint width;

    if (!gst_structure_get_int (structure, "depth", &depth) ||
708
        !gst_structure_get_int (structure, "width", &width))
709
710
711
712
713
714
      return GST_PAD_LINK_REFUSED;
    if (depth != width)
      return GST_PAD_LINK_REFUSED;

    switch (depth) {
      case 16:
715
716
        fmt = FAAD_FMT_16BIT;
        break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
717
#if 0
718
      case 24:
719
720
        fmt = FAAD_FMT_24BIT;
        break;
721
      case 32:
722
723
        fmt = FAAD_FMT_32BIT;
        break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
724
#endif
725
726
727
728
729
730
    }
  } else {
    if (!gst_structure_get_int (structure, "depth", &depth))
      return GST_PAD_LINK_REFUSED;

    switch (depth) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
731
#if 0
732
      case 32:
733
734
        fmt = FAAD_FMT_FLOAT;
        break;
735
      case 64:
736
737
        fmt = FAAD_FMT_DOUBLE;
        break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
738
#endif
739
    }
740
  }
741

Ronald S. Bultje's avatar
Ronald S. Bultje committed
742
  if (fmt != -1) {
743
    faacDecConfiguration *conf;
744

745
746
    conf = faacDecGetCurrentConfiguration (faad->handle);
    conf->outputFormat = fmt;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
747
748
    if (faacDecSetConfiguration (faad->handle, conf) == 0)
      return GST_PAD_LINK_REFUSED;
749

750
    // FIXME: handle return value, how? 
751
    faad->bps = depth / 8;
752

753
    return GST_PAD_LINK_OK;
754
  }
755

756
  return GST_PAD_LINK_REFUSED;
757
758
759
}*/

static gboolean
760
761
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
gst_faad_do_raw_seek (GstFaad * faad, GstEvent * event)
{
  GstSeekFlags flags;
  GstSeekType start_type, end_type;
  GstFormat format;
  gdouble rate;
  gint64 start, start_time;

  gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
      &start_time, &end_type, NULL);

  if (rate != 1.0 ||
      format != GST_FORMAT_TIME ||
      start_type != GST_SEEK_TYPE_SET || end_type != GST_SEEK_TYPE_NONE) {
    return FALSE;
  }

  if (!gst_faad_src_convert (faad, GST_FORMAT_TIME, start_time,
          GST_FORMAT_BYTES, &start)) {
    return FALSE;
  }

  event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
      GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);

  GST_DEBUG_OBJECT (faad, "seeking to %" GST_TIME_FORMAT " at byte offset %"
      G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);

  return gst_pad_send_event (GST_PAD_PEER (faad->sinkpad), event);
}

static gboolean
gst_faad_src_event (GstPad * pad, GstEvent * event)
{
  GstFaad *faad;
  gboolean res;

  faad = GST_FAAD (gst_pad_get_parent (pad));

  GST_LOG_OBJECT (faad, "Handling %s event", GST_EVENT_TYPE_NAME (event));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:{
      /* try upstream first, there might be a demuxer */
      gst_event_ref (event);
      if (!(res = gst_pad_event_default (pad, event))) {
        res = gst_faad_do_raw_seek (faad, event);
      }
      gst_event_unref (event);
      break;
    }
    default:
      res = gst_pad_event_default (pad, event);
      break;
  }

  gst_object_unref (faad);
  return res;
}

static gboolean
gst_faad_sink_event (GstPad * pad, GstEvent * event)
822
823
{
  GstFaad *faad;
824
  gboolean res = TRUE;
825
826
827

  faad = GST_FAAD (gst_pad_get_parent (pad));

828
  GST_LOG_OBJECT (faad, "Handling %s event", GST_EVENT_TYPE_NAME (event));
829

830
  /* FIXME: we should probably handle FLUSH */
831
832
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_EOS:
833
834
835
836
837
838
      if (faad->tempbuf != NULL) {
        gst_buffer_unref (faad->tempbuf);
        faad->tempbuf = NULL;
      }
      res = gst_pad_push_event (faad->srcpad, event);
      break;
839
    case GST_EVENT_NEWSEGMENT:
840
841
    {
      GstFormat fmt;
842
      gboolean is_update;
843
      gint64 start, end, base;
844
845
      gdouble rate;

846
      gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
847
          &end, &base);
848
      if (fmt == GST_FORMAT_TIME) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
849
850
        GST_DEBUG ("Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
            GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
851
            GST_TIME_ARGS (end));
852
853
        gst_segment_set_newsegment (faad->segment, is_update, rate, fmt, start,
            end, base);
854
      } else if (fmt == GST_FORMAT_BYTES) {
855
856
        gint64 new_start = 0;
        gint64 new_end = -1;
857
858
859
860

        GST_DEBUG ("Got NEWSEGMENT event in GST_FORMAT_BYTES (%"
            G_GUINT64_FORMAT " - %" G_GUINT64_FORMAT ")", start, end);

861
862
863
864
865
        if (gst_faad_src_convert (faad, GST_FORMAT_BYTES, start,
                GST_FORMAT_TIME, &new_start)) {
          if (end != -1) {
            gst_faad_src_convert (faad, GST_FORMAT_BYTES, end,
                GST_FORMAT_TIME, &new_end);
866
867
868
869
870
871
          }
        } else {
          GST_DEBUG
              ("no average bitrate yet, sending newsegment with start at 0");
        }
        gst_event_unref (event);
872
873
874
875

        event = gst_event_new_new_segment (is_update, rate,
            GST_FORMAT_TIME, new_start, new_end, new_start);

876
877
878
        gst_segment_set_newsegment (faad->segment, is_update, rate,
            GST_FORMAT_TIME, new_start, new_end, new_start);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
879
880
        GST_DEBUG ("Sending new NEWSEGMENT event, time %" GST_TIME_FORMAT
            " - %" GST_TIME_FORMAT, GST_TIME_ARGS (new_start),
881
            GST_TIME_ARGS (new_end));
882
883
884

        faad->next_ts = new_start;
        faad->prev_ts = GST_CLOCK_TIME_NONE;
885
886
887
888
889
      }

      res = gst_pad_push_event (faad->srcpad, event);
      break;
    }
890
    default:
891
      res = gst_pad_event_default (pad, event);
892
893
894
      break;
  }

895
  gst_object_unref (faad);
896
  return res;
897
898
}

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
static gboolean
gst_faad_src_convert (GstFaad * faad, GstFormat src_format, gint64 src_val,
    GstFormat dest_format, gint64 * dest_val)
{
  guint64 bytes_in, time_out, val;

  if (src_format == dest_format) {
    if (dest_val)
      *dest_val = src_val;
    return TRUE;
  }

  GST_OBJECT_LOCK (faad);
  bytes_in = faad->bytes_in;
  time_out = faad->sum_dur_out;
  GST_OBJECT_UNLOCK (faad);

  if (bytes_in == 0 || time_out == 0)
    return FALSE;

  /* convert based on the average bitrate so far */
  if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
    val = gst_util_uint64_scale (src_val, time_out, bytes_in);
  } else if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
    val = gst_util_uint64_scale (src_val, bytes_in, time_out);
  } else {
    return FALSE;
  }

  if (dest_val)
    *dest_val = (gint64) val;

  return TRUE;
}

static gboolean
gst_faad_src_query (GstPad * pad, GstQuery * query)
{
  gboolean res = FALSE;
  GstFaad *faad;
  GstPad *peer = NULL;

  faad = GST_FAAD (gst_pad_get_parent (pad));

  GST_LOG_OBJECT (faad, "processing %s query", GST_QUERY_TYPE_NAME (query));

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_DURATION:{
      GstFormat format;
      gint64 len_bytes, duration;

      /* try upstream first, in case there's a demuxer */
      if ((res = gst_pad_query_default (pad, query)))
        break;

      gst_query_parse_duration (query, &format, NULL);
      if (format != GST_FORMAT_TIME) {
        GST_DEBUG_OBJECT (faad, "query failed: can't handle format %s",
            gst_format_get_name (format));
        break;
      }

      peer = gst_pad_get_peer (faad->sinkpad);
      if (peer == NULL)
        break;

      format = GST_FORMAT_BYTES;
      if (!gst_pad_query_duration (peer, &format, &len_bytes)) {
        GST_DEBUG_OBJECT (faad, "query failed: failed to get upstream length");
        break;
      }

      res = gst_faad_src_convert (faad, GST_FORMAT_BYTES, len_bytes,
          GST_FORMAT_TIME, &duration);

      if (res) {
        gst_query_set_duration (query, GST_FORMAT_TIME, duration);

        GST_LOG_OBJECT (faad, "duration estimate: %" GST_TIME_FORMAT,
            GST_TIME_ARGS (duration));
      }
      break;
    }
    case GST_QUERY_POSITION:{
      GstFormat format;
      gint64 pos_bytes, pos;

      /* try upstream first, in case there's a demuxer */
      if ((res = gst_pad_query_default (pad, query)))
        break;

      gst_query_parse_position (query, &format, NULL);
      if (format != GST_FORMAT_TIME) {
        GST_DEBUG_OBJECT (faad, "query failed: can't handle format %s",
            gst_format_get_name (format));
        break;
      }

      peer = gst_pad_get_peer (faad->sinkpad);
      if (peer == NULL)
        break;

      format = GST_FORMAT_BYTES;
      if (!gst_pad_query_position (peer, &format, &pos_bytes)) {
        GST_OBJECT_LOCK (faad);
        pos = faad->next_ts;
        GST_OBJECT_UNLOCK (faad);
        res = TRUE;
      } else {
        res = gst_faad_src_convert (faad, GST_FORMAT_BYTES, pos_bytes,
            GST_FORMAT_TIME, &pos);
      }

      if (res) {
        gst_query_set_position (query, GST_FORMAT_TIME, pos);
      }
      break;
    }
    default:
      res = gst_pad_query_default (pad, query);
      break;
  }

  if (peer)
    gst_object_unref (peer);

  gst_object_unref (faad);
  return res;
}


1030
static gboolean
1031
gst_faad_update_caps (GstFaad * faad, faacDecFrameInfo * info)
1032
1033
{
  GstAudioChannelPosition *pos;
1034
  gboolean ret;
1035
  gboolean channel_map_failed;
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  GstCaps *caps;

  /* store new negotiation information */
  faad->samplerate = info->samplerate;
  faad->channels = info->channels;
  g_free (faad->channel_positions);
  faad->channel_positions = g_memdup (info->channel_position, faad->channels);

  caps = gst_caps_new_simple ("audio/x-raw-int",
      "endianness", G_TYPE_INT, G_BYTE_ORDER,
      "signed", G_TYPE_BOOLEAN, TRUE,
      "width", G_TYPE_INT, 16,
      "depth", G_TYPE_INT, 16,
      "rate", G_TYPE_INT, faad->samplerate,
      "channels", G_TYPE_INT, faad->channels, NULL);

  faad->bps = 16 / 8;

1054
1055
1056
1057
1058
  channel_map_failed = FALSE;
  pos =
      gst_faad_chanpos_to_gst (faad, faad->channel_positions, faad->channels,
      &channel_map_failed);
  if (channel_map_failed) {
1059
1060
1061
1062
    GST_DEBUG_OBJECT (faad, "Could not map channel positions");
    gst_caps_unref (caps);
    return FALSE;
  }
1063
1064
1065
1066
  if (pos) {
    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
    g_free (pos);
  }
1067

1068
  GST_DEBUG_OBJECT (faad, "New output caps: %" GST_PTR_FORMAT, caps);
1069

1070
1071
  ret = gst_pad_set_caps (faad->srcpad, caps);
  gst_caps_unref (caps);
1072

1073
  return ret;
1074
1075
}

1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/*
 * Find syncpoint in ADTS/ADIF stream. Doesn't work for raw,
 * packetized streams. Be careful when calling.
 * Returns FALSE on no-sync, fills offset/length if one/two
 * syncpoints are found, only returns TRUE when it finds two
 * subsequent syncpoints (similar to mp3 typefinding in
 * gst/typefind/) for ADTS because 12 bits isn't very reliable.
 */
static gboolean
gst_faad_sync (GstBuffer * buf, guint * off)
{
  guint8 *data = GST_BUFFER_DATA (buf);
  guint size = GST_BUFFER_SIZE (buf), n;
  gint snc;

  GST_DEBUG ("Finding syncpoint");

1093
1094
1095
1096
  /* check for too small a buffer */
  if (size < 3)
    return FALSE;

1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
  /* FIXME: for no-sync, we go over the same data for every new buffer.
   * We should save the information somewhere. */
  for (n = 0; n < size - 3; n++) {
    snc = GST_READ_UINT16_BE (&data[n]);
    if ((snc & 0xfff6) == 0xfff0) {
      /* we have an ADTS syncpoint. Parse length and find
       * next syncpoint. */
      guint len;

      GST_DEBUG ("Found one ADTS syncpoint at offset 0x%x, tracing next...", n);

      if (size - n < 5) {
        GST_DEBUG ("Not enough data to parse ADTS header");
        return FALSE;
      }

      *off = n;
      len = ((data[n + 3] & 0x03) << 11) |
          (data[n + 4] << 3) | ((data[n + 5] & 0xe0) >> 5);
      if (n + len + 2 >= size) {
        GST_DEBUG ("Next frame is not within reach");
        return FALSE;
      }

      snc = GST_READ_UINT16_BE (&data[n + len]);
      if ((snc & 0xfff6) == 0xfff0) {
        GST_DEBUG ("Found ADTS syncpoint at offset 0x%x (framelen %u)", n, len);
        return TRUE;
      }

      GST_DEBUG ("No next frame found... (should be at 0x%x)", n + len);
    } else if (!memcmp (&data[n], "ADIF", 4)) {
      /* we have an ADIF syncpoint. 4 bytes is enough. */
      *off = n;
      GST_DEBUG ("Found ADIF syncpoint at offset 0x%x", n);
      return TRUE;
    }
  }

  GST_DEBUG ("Found no syncpoint");

  return FALSE;
}

1141
1142
1143
static gboolean
looks_like_valid_header (guint8 * input_data, guint input_size)
{
1144
  if (input_size < 4)
1145
1146
    return FALSE;

1147
1148
1149
1150
  if (input_data[0] == 'A'
      && input_data[1] == 'D' && input_data[2] == 'I' && input_data[3] == 'F')
    /* ADIF type header */
    return TRUE;
1151

1152
1153
1154
  if (input_data[0] == 0xff && (input_data[1] >> 4) == 0xf)
    /* ADTS type header */
    return TRUE;
1155

1156
  return FALSE;
1157
1158
}

1159
1160
static GstFlowReturn
gst_faad_chain (GstPad * pad, GstBuffer * buffer)
1161
{
1162
  GstFlowReturn ret = GST_FLOW_OK;
1163
  guint input_size;
1164
  guint skip_bytes = 0;
1165
  guchar *input_data;
1166
  GstFaad *faad;
1167
  GstBuffer *outbuf;
1168
  faacDecFrameInfo info;
1169
  void *out;
1170
  gboolean run_loop = TRUE;
1171
  guint sync_off;
1172

1173
  faad = GST_FAAD (gst_pad_get_parent (pad));
1174

1175
1176
1177
1178
  GST_OBJECT_LOCK (faad);
  faad->bytes_in += GST_BUFFER_SIZE (buffer);
  GST_OBJECT_UNLOCK (faad);

1179
  if (GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE) {
1180
1181
1182
1183
1184
1185
    /* some demuxers send multiple buffers in a row
     *  with the same timestamp (e.g. matroskademux) */
    if (GST_BUFFER_TIMESTAMP (buffer) != faad->prev_ts) {
      faad->next_ts = GST_BUFFER_TIMESTAMP (buffer);
      faad->prev_ts = GST_BUFFER_TIMESTAMP (buffer);
    }
1186
    GST_LOG_OBJECT (faad, "Timestamp on incoming buffer: %" GST_TIME_FORMAT
1187
1188
        ", next_ts: %" GST_TIME_FORMAT,
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1189
        GST_TIME_ARGS (faad->next_ts));
1190
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1191
1192
  /* buffer + remaining data */
  if (faad->tempbuf) {
1193
    buffer = gst_buffer_join (faad->tempbuf, buffer);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1194
1195
    faad->tempbuf = NULL;
  }
1196

1197
1198
  input_data = GST_BUFFER_DATA (buffer);
  input_size = GST_BUFFER_SIZE (buffer);
1199

1200
1201
1202
1203
1204
1205
1206
1207
1208
  if (!faad->packetised) {
    if (!gst_faad_sync (buffer, &sync_off)) {
      goto next;
    } else {
      input_data += sync_off;
      input_size -= sync_off;
    }
  }

Ronald S. Bultje's avatar
Ronald S. Bultje committed
1209
1210
  /* init if not already done during capsnego */
  if (!faad->init) {
1211
1212
1213
1214
    guint32 rate;
    guint8 ch;

    GST_DEBUG_OBJECT (faad, "initialising ...");
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
    /* We check if the first data looks like it might plausibly contain
     * appropriate initialisation info... if not, we use our fake_codec_data
     */
    if (looks_like_valid_header (input_data, input_size) || !faad->packetised) {
      if (faacDecInit (faad->handle, input_data, input_size, &rate, &ch) < 0)
        goto init_failed;

      GST_DEBUG_OBJECT (faad, "faacDecInit() ok: rate=%u,channels=%u", rate,
          ch);
    } else {
      if ((gint8) faacDecInit2 (faad->handle, faad->fake_codec_data, 2,
              &rate, &ch) < 0) {
        goto init2_failed;
      }
      GST_DEBUG_OBJECT (faad, "faacDecInit2() ok: rate=%u,channels=%u", rate,
          ch);
    }
1232
1233

    skip_bytes = 0;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1234
    faad->init = TRUE;
1235

1236
1237
1238
    /* make sure we create new caps below */
    faad->samplerate = 0;
    faad->channels = 0;
1239
    gst_faad_send_tags (faad);
1240
  }
1241

Ronald S. Bultje's avatar
Ronald S. Bultje committed
1242
  /* decode cycle */
1243
  info.bytesconsumed = input_size - skip_bytes;
1244
  info.error = 0;
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256

  if (!faad->packetised) {
    /* We must check that ourselves for raw stream */
    run_loop = (input_size >= FAAD_MIN_STREAMSIZE);
  }

  while ((input_size > 0) && run_loop) {

    if (faad->packetised) {
      /* Only one packet per buffer, no matter how much is really consumed */
      run_loop = FALSE;
    } else {
1257
      if (input_size < FAAD_MIN_STREAMSIZE || info.bytesconsumed <= 0) {
1258
1259
1260
1261
        break;
      }
    }

1262
    out = faacDecDecode (faad->handle, &info, input_data + skip_bytes,
Maciej Katafiasz's avatar