~ubuntu-branches/ubuntu/precise/gst-plugins-bad0.10/precise-proposed

1 by Sebastian Dröge
Import upstream version 0.10.0.3
1
/* GStreamer FAAD (Free AAC Decoder) plugin
2
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
3
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
1 by Sebastian Dröge
Import upstream version 0.10.0.3
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Library General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Library General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Library General Public
16
 * License along with this library; if not, write to the
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 * Boston, MA 02111-1307, USA.
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <string.h>
26
#include <gst/audio/audio.h>
27
#include <gst/audio/multichannel.h>
28
29
/* These are the correct types for these functions, as defined in the source,
30
 * with types changed to match glib types, since those are defined for us.
31
 * However, upstream FAAD is distributed with a broken header file that defined
32
 * these wrongly (in a way which was broken on 64 bit systems).
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
33
 *
1 by Sebastian Dröge
Import upstream version 0.10.0.3
34
 * Upstream CVS still has the bug, but has also renamed all the public symbols
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
35
 * for Better Corporate Branding (or whatever), so we need to take that
36
 * (FAAD_IS_NEAAC) into account as well.
1 by Sebastian Dröge
Import upstream version 0.10.0.3
37
 *
38
 * We must call them using these definitions. Most distributions now have the
39
 * corrected header file (they distribute a patch along with the source), 
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
40
 * but not all, hence this Truly Evil Hack.
41
 *
42
 * Note: The prototypes don't need to be defined conditionaly, as the cpp will
43
 * do that for us.
1 by Sebastian Dröge
Import upstream version 0.10.0.3
44
 */
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
45
#ifdef FAAD_IS_NEAAC
46
#define NeAACDecInit NeAACDecInit_no_definition
47
#define NeAACDecInit2 NeAACDecInit2_no_definition
48
#else
1 by Sebastian Dröge
Import upstream version 0.10.0.3
49
#define faacDecInit faacDecInit_no_definition
50
#define faacDecInit2 faacDecInit2_no_definition
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
51
#endif
1 by Sebastian Dröge
Import upstream version 0.10.0.3
52
#include "gstfaad.h"
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
53
#ifdef FAAD_IS_NEAAC
54
#undef NeAACDecInit
55
#undef NeAACDecInit2
56
#else
1 by Sebastian Dröge
Import upstream version 0.10.0.3
57
#undef faacDecInit
58
#undef faacDecInit2
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
59
#endif
1 by Sebastian Dröge
Import upstream version 0.10.0.3
60
61
extern long faacDecInit (faacDecHandle, guint8 *, guint32, guint32 *, guint8 *);
62
extern int8_t faacDecInit2 (faacDecHandle, guint8 *, guint32,
63
    guint32 *, guint8 *);
64
65
GST_DEBUG_CATEGORY_STATIC (faad_debug);
66
#define GST_CAT_DEFAULT faad_debug
67
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
68
#define MAX_DECODE_ERRORS 5
69
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
70
static const GstElementDetails faad_details =
71
GST_ELEMENT_DETAILS ("AAC audio decoder",
72
    "Codec/Decoder/Audio",
73
    "Free MPEG-2/4 AAC decoder",
74
    "Ronald Bultje <rbultje@ronald.bitfreak.net>");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
75
76
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
77
    GST_PAD_SINK,
78
    GST_PAD_ALWAYS,
79
    GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) { 2, 4 }")
80
    );
81
82
#define STATIC_INT_CAPS(bpp) \
83
  "audio/x-raw-int, " \
84
    "endianness = (int) BYTE_ORDER, " \
85
    "signed = (bool) TRUE, " \
86
    "width = (int) " G_STRINGIFY (bpp) ", " \
87
    "depth = (int) " G_STRINGIFY (bpp) ", " \
88
    "rate = (int) [ 8000, 96000 ], " \
89
    "channels = (int) [ 1, 8 ]"
90
91
#if 0
92
#define STATIC_FLOAT_CAPS(bpp) \
93
  "audio/x-raw-float, " \
94
    "endianness = (int) BYTE_ORDER, " \
95
    "depth = (int) " G_STRINGIFY (bpp) ", " \
96
    "rate = (int) [ 8000, 96000 ], " \
97
    "channels = (int) [ 1, 8 ]"
98
#endif
99
100
/*
101
 * All except 16-bit integer are disabled until someone fixes FAAD.
102
 * FAAD allocates approximately 8*1024*2 bytes bytes, which is enough
103
 * for 1 frame (1024 samples) of 6 channel (5.1) 16-bit integer 16bpp
104
 * audio, but not for any other. You'll get random segfaults, crashes
105
 * and even valgrind goes crazy.
106
 */
107
108
#define STATIC_CAPS \
109
  STATIC_INT_CAPS (16)
110
#if 0
111
#define NOTUSED "; " \
112
STATIC_INT_CAPS (24) \
113
    "; " \
114
STATIC_INT_CAPS (32) \
115
    "; " \
116
STATIC_FLOAT_CAPS (32) \
117
    "; " \
118
STATIC_FLOAT_CAPS (64)
119
#endif
120
121
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
122
    GST_PAD_SRC,
123
    GST_PAD_ALWAYS,
124
    GST_STATIC_CAPS (STATIC_CAPS)
125
    );
126
127
static void gst_faad_base_init (GstFaadClass * klass);
128
static void gst_faad_class_init (GstFaadClass * klass);
129
static void gst_faad_init (GstFaad * faad);
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
130
static void gst_faad_dispose (GObject * object);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
131
132
static gboolean gst_faad_setcaps (GstPad * pad, GstCaps * caps);
133
static GstCaps *gst_faad_srcgetcaps (GstPad * pad);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
134
static gboolean gst_faad_src_event (GstPad * pad, GstEvent * event);
135
static gboolean gst_faad_sink_event (GstPad * pad, GstEvent * event);
136
static gboolean gst_faad_src_query (GstPad * pad, GstQuery * query);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
137
static GstFlowReturn gst_faad_chain (GstPad * pad, GstBuffer * buffer);
138
static GstStateChangeReturn gst_faad_change_state (GstElement * element,
139
    GstStateChange transition);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
140
static gboolean gst_faad_src_convert (GstFaad * faad, GstFormat src_format,
141
    gint64 src_val, GstFormat dest_format, gint64 * dest_val);
142
static gboolean gst_faad_open_decoder (GstFaad * faad);
143
static void gst_faad_close_decoder (GstFaad * faad);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
144
145
static GstElementClass *parent_class;   /* NULL */
146
147
GType
148
gst_faad_get_type (void)
149
{
150
  static GType gst_faad_type = 0;
151
152
  if (!gst_faad_type) {
153
    static const GTypeInfo gst_faad_info = {
154
      sizeof (GstFaadClass),
155
      (GBaseInitFunc) gst_faad_base_init,
156
      NULL,
157
      (GClassInitFunc) gst_faad_class_init,
158
      NULL,
159
      NULL,
160
      sizeof (GstFaad),
161
      0,
162
      (GInstanceInitFunc) gst_faad_init,
163
    };
164
165
    gst_faad_type = g_type_register_static (GST_TYPE_ELEMENT,
166
        "GstFaad", &gst_faad_info, 0);
167
  }
168
169
  return gst_faad_type;
170
}
171
172
static void
173
gst_faad_base_init (GstFaadClass * klass)
174
{
175
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
176
177
  gst_element_class_add_pad_template (element_class,
178
      gst_static_pad_template_get (&src_template));
179
  gst_element_class_add_pad_template (element_class,
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
188
gst_faad_class_init (GstFaadClass * klass)
189
{
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
190
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
191
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
192
193
  parent_class = g_type_class_peek_parent (klass);
194
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
195
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_faad_dispose);
196
1 by Sebastian Dröge
Import upstream version 0.10.0.3
197
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_faad_change_state);
198
}
199
200
static void
201
gst_faad_init (GstFaad * faad)
202
{
203
  faad->handle = NULL;
204
  faad->samplerate = -1;
205
  faad->channels = -1;
206
  faad->tempbuf = NULL;
207
  faad->need_channel_setup = TRUE;
208
  faad->channel_positions = NULL;
209
  faad->init = FALSE;
210
  faad->next_ts = 0;
211
  faad->prev_ts = GST_CLOCK_TIME_NONE;
212
  faad->bytes_in = 0;
213
  faad->sum_dur_out = 0;
214
  faad->packetised = FALSE;
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
215
  faad->error_count = 0;
216
  faad->segment = gst_segment_new ();
1 by Sebastian Dröge
Import upstream version 0.10.0.3
217
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
218
  faad->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
219
  gst_element_add_pad (GST_ELEMENT (faad), faad->sinkpad);
220
  gst_pad_set_event_function (faad->sinkpad,
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
221
      GST_DEBUG_FUNCPTR (gst_faad_sink_event));
1 by Sebastian Dröge
Import upstream version 0.10.0.3
222
  gst_pad_set_setcaps_function (faad->sinkpad,
223
      GST_DEBUG_FUNCPTR (gst_faad_setcaps));
224
  gst_pad_set_chain_function (faad->sinkpad,
225
      GST_DEBUG_FUNCPTR (gst_faad_chain));
226
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
227
  faad->srcpad = gst_pad_new_from_static_template (&src_template, "src");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
228
  gst_pad_use_fixed_caps (faad->srcpad);
229
  gst_pad_set_getcaps_function (faad->srcpad,
230
      GST_DEBUG_FUNCPTR (gst_faad_srcgetcaps));
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
231
  gst_pad_set_query_function (faad->srcpad,
232
      GST_DEBUG_FUNCPTR (gst_faad_src_query));
233
  gst_pad_set_event_function (faad->srcpad,
234
      GST_DEBUG_FUNCPTR (gst_faad_src_event));
235
  gst_element_add_pad (GST_ELEMENT (faad), faad->srcpad);
236
}
237
238
static void
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
239
gst_faad_dispose (GObject * object)
240
{
241
  GstFaad *faad = GST_FAAD (object);
242
243
  if (faad->segment) {
244
    gst_segment_free (faad->segment);
245
    faad->segment = NULL;
246
  }
247
248
  G_OBJECT_CLASS (parent_class)->dispose (object);
249
250
}
251
252
static void
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
253
gst_faad_send_tags (GstFaad * faad)
254
{
255
  GstTagList *tags;
256
257
  tags = gst_tag_list_new ();
258
259
  gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
260
      GST_TAG_AUDIO_CODEC, "MPEG-4 AAC audio", NULL);
261
262
  gst_element_found_tags (GST_ELEMENT (faad), tags);
263
}
264
265
static gint
266
aac_rate_idx (gint rate)
267
{
268
  if (92017 <= rate)
269
    return 0;
270
  else if (75132 <= rate)
271
    return 1;
272
  else if (55426 <= rate)
273
    return 2;
274
  else if (46009 <= rate)
275
    return 3;
276
  else if (37566 <= rate)
277
    return 4;
278
  else if (27713 <= rate)
279
    return 5;
280
  else if (23004 <= rate)
281
    return 6;
282
  else if (18783 <= rate)
283
    return 7;
284
  else if (13856 <= rate)
285
    return 8;
286
  else if (11502 <= rate)
287
    return 9;
288
  else if (9391 <= rate)
289
    return 10;
290
  else
291
    return 11;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
292
}
293
294
static gboolean
295
gst_faad_setcaps (GstPad * pad, GstCaps * caps)
296
{
297
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));
298
  GstStructure *str = gst_caps_get_structure (caps, 0);
299
  GstBuffer *buf;
300
  const GValue *value;
301
302
  /* Assume raw stream */
303
  faad->packetised = FALSE;
304
305
  if ((value = gst_structure_get_value (str, "codec_data"))) {
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
306
    guint32 samplerate;
307
    guint8 channels;
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
308
    guint8 *cdata;
309
    guint csize;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
310
311
    /* We have codec data, means packetised stream */
312
    faad->packetised = TRUE;
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
313
    buf = gst_value_get_buffer (value);
314
315
    cdata = GST_BUFFER_DATA (buf);
316
    csize = GST_BUFFER_SIZE (buf);
317
318
    if (csize < 2)
319
      goto wrong_length;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
320
321
    /* someone forgot that char can be unsigned when writing the API */
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
322
    if ((gint8) faacDecInit2 (faad->handle, cdata, csize, &samplerate,
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
323
            &channels) < 0)
324
      goto init_failed;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
325
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
326
    GST_DEBUG_OBJECT (faad, "codec_data init: channels=%u, rate=%u", channels,
327
        samplerate);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
328
1 by Sebastian Dröge
Import upstream version 0.10.0.3
329
    /* not updating these here, so they are updated in the
330
     * chain function, and new caps are created etc. */
331
    faad->samplerate = 0;
332
    faad->channels = 0;
333
334
    faad->init = TRUE;
335
336
    if (faad->tempbuf) {
337
      gst_buffer_unref (faad->tempbuf);
338
      faad->tempbuf = NULL;
339
    }
340
  } else if ((value = gst_structure_get_value (str, "framed")) &&
341
      g_value_get_boolean (value) == TRUE) {
342
    faad->packetised = TRUE;
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
343
    GST_DEBUG_OBJECT (faad, "we have packetized audio");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
344
  } else {
345
    faad->init = FALSE;
346
  }
347
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
348
  faad->fake_codec_data[0] = 0;
349
  faad->fake_codec_data[1] = 0;
350
351
  if (faad->packetised) {
352
    gint rate, channels;
353
354
    if (gst_structure_get_int (str, "rate", &rate) &&
355
        gst_structure_get_int (str, "channels", &channels)) {
356
      gint rate_idx, profile;
357
1.1.3 by Sebastian Dröge
Import upstream version 0.10.3
358
      profile = 3;              /* 0=MAIN, 1=LC, 2=SSR, 3=LTP */
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
359
      rate_idx = aac_rate_idx (rate);
360
361
      faad->fake_codec_data[0] = ((profile + 1) << 3) | ((rate_idx & 0xE) >> 1);
362
      faad->fake_codec_data[1] = ((rate_idx & 0x1) << 7) | (channels << 3);
1.1.3 by Sebastian Dröge
Import upstream version 0.10.3
363
      GST_LOG_OBJECT (faad, "created fake codec data (%u,%u): 0x%x 0x%x", rate,
364
          channels, (int) faad->fake_codec_data[0],
365
          (int) faad->fake_codec_data[1]);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
366
    }
367
  }
368
1 by Sebastian Dröge
Import upstream version 0.10.0.3
369
  faad->need_channel_setup = TRUE;
370
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
371
  if (!faad->packetised)
372
    gst_faad_send_tags (faad);
373
1 by Sebastian Dröge
Import upstream version 0.10.0.3
374
  return TRUE;
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
375
376
  /* ERRORS */
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
377
wrong_length:
378
  {
379
    GST_DEBUG_OBJECT (faad, "codec_data less than 2 bytes long");
380
    return FALSE;
381
  }
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
382
init_failed:
383
  {
384
    GST_DEBUG_OBJECT (faad, "faacDecInit2() failed");
385
    return FALSE;
386
  }
1 by Sebastian Dröge
Import upstream version 0.10.0.3
387
}
388
389
390
/*
391
 * Channel identifier conversion - caller g_free()s result!
392
 */
393
/*
394
static guchar *
395
gst_faad_chanpos_from_gst (GstAudioChannelPosition * pos, guint num)
396
{
397
  guchar *fpos = g_new (guchar, num);
398
  guint n;
399
400
  for (n = 0; n < num; n++) {
401
    switch (pos[n]) {
402
      case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
403
        fpos[n] = FRONT_CHANNEL_LEFT;
404
        break;
405
      case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
406
        fpos[n] = FRONT_CHANNEL_RIGHT;
407
        break;
408
      case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
409
      case GST_AUDIO_CHANNEL_POSITION_FRONT_MONO:
410
        fpos[n] = FRONT_CHANNEL_CENTER;
411
        break;
412
      case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
413
        fpos[n] = SIDE_CHANNEL_LEFT;
414
        break;
415
      case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
416
        fpos[n] = SIDE_CHANNEL_RIGHT;
417
        break;
418
      case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
419
        fpos[n] = BACK_CHANNEL_LEFT;
420
        break;
421
      case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
422
        fpos[n] = BACK_CHANNEL_RIGHT;
423
        break;
424
      case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
425
        fpos[n] = BACK_CHANNEL_CENTER;
426
        break;
427
      case GST_AUDIO_CHANNEL_POSITION_LFE:
428
        fpos[n] = LFE_CHANNEL;
429
        break;
430
      default:
431
        GST_WARNING ("Unsupported GST channel position 0x%x encountered",
432
            pos[n]);
433
        g_free (fpos);
434
        return NULL;
435
    }
436
  }
437
438
  return fpos;
439
}
440
*/
441
442
static GstAudioChannelPosition *
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
443
gst_faad_chanpos_to_gst (GstFaad * faad, guchar * fpos, guint num,
444
    gboolean * channel_map_failed)
1 by Sebastian Dröge
Import upstream version 0.10.0.3
445
{
446
  GstAudioChannelPosition *pos = g_new (GstAudioChannelPosition, num);
447
  guint n;
448
  gboolean unknown_channel = FALSE;
449
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
450
  *channel_map_failed = FALSE;
451
452
  /* special handling for the common cases for mono and stereo */
453
  if (num == 1 && fpos[0] == FRONT_CHANNEL_CENTER) {
454
    GST_DEBUG_OBJECT (faad, "mono common case; won't set channel positions");
455
    return NULL;
456
  } else if (num == 2 && fpos[0] == FRONT_CHANNEL_LEFT
457
      && fpos[1] == FRONT_CHANNEL_RIGHT) {
458
    GST_DEBUG_OBJECT (faad, "stereo common case; won't set channel positions");
459
    return NULL;
460
  }
461
1 by Sebastian Dröge
Import upstream version 0.10.0.3
462
  for (n = 0; n < num; n++) {
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
463
    GST_DEBUG_OBJECT (faad, "faad channel %d as %d", n, fpos[n]);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
464
    switch (fpos[n]) {
465
      case FRONT_CHANNEL_LEFT:
466
        pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
467
        break;
468
      case FRONT_CHANNEL_RIGHT:
469
        pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
470
        break;
471
      case FRONT_CHANNEL_CENTER:
472
        /* argh, mono = center */
473
        if (num == 1)
474
          pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
475
        else
476
          pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
477
        break;
478
      case SIDE_CHANNEL_LEFT:
479
        pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
480
        break;
481
      case SIDE_CHANNEL_RIGHT:
482
        pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
483
        break;
484
      case BACK_CHANNEL_LEFT:
485
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
486
        break;
487
      case BACK_CHANNEL_RIGHT:
488
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
489
        break;
490
      case BACK_CHANNEL_CENTER:
491
        pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
492
        break;
493
      case LFE_CHANNEL:
494
        pos[n] = GST_AUDIO_CHANNEL_POSITION_LFE;
495
        break;
496
      default:
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
497
        GST_DEBUG_OBJECT (faad, "unknown channel %d at %d", fpos[n], n);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
498
        unknown_channel = TRUE;
499
        break;
500
    }
501
  }
502
  if (unknown_channel) {
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
503
    g_free (pos);
504
    pos = NULL;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
505
    switch (num) {
506
      case 1:{
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
507
        GST_DEBUG_OBJECT (faad,
508
            "FAAD reports unknown 1 channel mapping. Forcing to mono");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
509
        break;
510
      }
511
      case 2:{
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
512
        GST_DEBUG_OBJECT (faad,
513
            "FAAD reports unknown 2 channel mapping. Forcing to stereo");
1 by Sebastian Dröge
Import upstream version 0.10.0.3
514
        break;
515
      }
516
      default:{
517
        GST_WARNING ("Unsupported FAAD channel position 0x%x encountered",
518
            fpos[n]);
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
519
        *channel_map_failed = TRUE;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
520
        break;
521
      }
522
    }
523
  }
524
525
  return pos;
526
}
527
528
static GstCaps *
529
gst_faad_srcgetcaps (GstPad * pad)
530
{
531
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));
532
  static GstAudioChannelPosition *supported_positions = NULL;
533
  static gint num_supported_positions = LFE_CHANNEL - FRONT_CHANNEL_CENTER + 1;
534
  GstCaps *templ;
535
536
  if (!supported_positions) {
537
    guchar *supported_fpos = g_new0 (guchar, num_supported_positions);
538
    gint n;
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
539
    gboolean map_failed;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
540
541
    for (n = 0; n < num_supported_positions; n++) {
542
      supported_fpos[n] = n + FRONT_CHANNEL_CENTER;
543
    }
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
544
    supported_positions = gst_faad_chanpos_to_gst (faad, supported_fpos,
545
        num_supported_positions, &map_failed);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
546
    g_free (supported_fpos);
547
  }
548
549
  if (faad->handle != NULL && faad->channels != -1 && faad->samplerate != -1) {
550
    GstCaps *caps = gst_caps_new_empty ();
551
    GstStructure *str;
552
    gint fmt[] = {
553
      FAAD_FMT_16BIT,
554
#if 0
555
      FAAD_FMT_24BIT,
556
      FAAD_FMT_32BIT,
557
      FAAD_FMT_FLOAT,
558
      FAAD_FMT_DOUBLE,
559
#endif
560
      -1
561
    }
562
    , n;
563
564
    for (n = 0; fmt[n] != -1; n++) {
565
      switch (fmt[n]) {
566
        case FAAD_FMT_16BIT:
567
          str = gst_structure_new ("audio/x-raw-int",
568
              "signed", G_TYPE_BOOLEAN, TRUE,
569
              "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
570
          break;
571
#if 0
572
        case FAAD_FMT_24BIT:
573
          str = gst_structure_new ("audio/x-raw-int",
574
              "signed", G_TYPE_BOOLEAN, TRUE,
575
              "width", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24, NULL);
576
          break;
577
        case FAAD_FMT_32BIT:
578
          str = gst_structure_new ("audio/x-raw-int",
579
              "signed", G_TYPE_BOOLEAN, TRUE,
580
              "width", G_TYPE_INT, 32, "depth", G_TYPE_INT, 32, NULL);
581
          break;
582
        case FAAD_FMT_FLOAT:
583
          str = gst_structure_new ("audio/x-raw-float",
584
              "depth", G_TYPE_INT, 32, NULL);
585
          break;
586
        case FAAD_FMT_DOUBLE:
587
          str = gst_structure_new ("audio/x-raw-float",
588
              "depth", G_TYPE_INT, 64, NULL);
589
          break;
590
#endif
591
        default:
592
          str = NULL;
593
          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);
606
607
        /* put channel information here */
608
        if (faad->channel_positions) {
609
          GstAudioChannelPosition *pos;
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
610
          gboolean map_failed;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
611
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
612
          pos = gst_faad_chanpos_to_gst (faad, faad->channel_positions,
613
              faad->channels, &map_failed);
614
          if (map_failed) {
1 by Sebastian Dröge
Import upstream version 0.10.0.3
615
            gst_structure_free (str);
616
            continue;
617
          }
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
618
          if (pos) {
619
            gst_audio_set_channel_positions (str, pos);
620
            g_free (pos);
621
          }
1 by Sebastian Dröge
Import upstream version 0.10.0.3
622
        } else {
623
          gst_audio_set_structure_channel_positions_list (str,
624
              supported_positions, num_supported_positions);
625
        }
626
      } else {
627
        gst_structure_set (str, "channels", GST_TYPE_INT_RANGE, 1, 8, NULL);
628
        /* we set channel positions later */
629
      }
630
631
      gst_structure_set (str, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
632
633
      gst_caps_append_structure (caps, str);
634
    }
635
636
    if (faad->channels == -1) {
637
      gst_audio_set_caps_channel_positions_list (caps,
638
          supported_positions, num_supported_positions);
639
    }
640
    gst_object_unref (faad);
641
    return caps;
642
  }
643
644
  /* template with channel positions */
645
  templ = gst_caps_copy (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad)));
646
  gst_audio_set_caps_channel_positions_list (templ,
647
      supported_positions, num_supported_positions);
648
649
  gst_object_unref (faad);
650
  return templ;
651
}
652
653
/*
654
static GstPadLinkReturn
655
gst_faad_srcconnect (GstPad * pad, const GstCaps * caps)
656
{
657
  GstStructure *structure;
658
  const gchar *mimetype;
659
  gint fmt = -1;
660
  gint depth, rate, channels;
661
  GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad));
662
663
  structure = gst_caps_get_structure (caps, 0);
664
665
  if (!faad->handle || (faad->samplerate == -1 || faad->channels == -1) ||
666
      !faad->channel_positions) {
667
    return GST_PAD_LINK_DELAYED;
668
  }
669
670
  mimetype = gst_structure_get_name (structure);
671
672
  // Samplerate and channels are normally provided through
673
  // * the getcaps function 
674
  if (!gst_structure_get_int (structure, "channels", &channels) ||
675
      !gst_structure_get_int (structure, "rate", &rate) ||
676
      rate != faad->samplerate || channels != faad->channels) {
677
    return GST_PAD_LINK_REFUSED;
678
  }
679
680
  // Another internal checkup. 
681
  if (faad->need_channel_setup) {
682
    GstAudioChannelPosition *pos;
683
    guchar *fpos;
684
    guint i;
685
686
    pos = gst_audio_get_channel_positions (structure);
687
    if (!pos) {
688
      return GST_PAD_LINK_DELAYED;
689
    }
690
    fpos = gst_faad_chanpos_from_gst (pos, faad->channels);
691
    g_free (pos);
692
    if (!fpos)
693
      return GST_PAD_LINK_REFUSED;
694
695
    for (i = 0; i < faad->channels; i++) {
696
      if (fpos[i] != faad->channel_positions[i]) {
697
        g_free (fpos);
698
        return GST_PAD_LINK_REFUSED;
699
      }
700
    }
701
    g_free (fpos);
702
  }
703
704
  if (!strcmp (mimetype, "audio/x-raw-int")) {
705
    gint width;
706
707
    if (!gst_structure_get_int (structure, "depth", &depth) ||
708
        !gst_structure_get_int (structure, "width", &width))
709
      return GST_PAD_LINK_REFUSED;
710
    if (depth != width)
711
      return GST_PAD_LINK_REFUSED;
712
713
    switch (depth) {
714
      case 16:
715
        fmt = FAAD_FMT_16BIT;
716
        break;
717
#if 0
718
      case 24:
719
        fmt = FAAD_FMT_24BIT;
720
        break;
721
      case 32:
722
        fmt = FAAD_FMT_32BIT;
723
        break;
724
#endif
725
    }
726
  } else {
727
    if (!gst_structure_get_int (structure, "depth", &depth))
728
      return GST_PAD_LINK_REFUSED;
729
730
    switch (depth) {
731
#if 0
732
      case 32:
733
        fmt = FAAD_FMT_FLOAT;
734
        break;
735
      case 64:
736
        fmt = FAAD_FMT_DOUBLE;
737
        break;
738
#endif
739
    }
740
  }
741
742
  if (fmt != -1) {
743
    faacDecConfiguration *conf;
744
745
    conf = faacDecGetCurrentConfiguration (faad->handle);
746
    conf->outputFormat = fmt;
747
    if (faacDecSetConfiguration (faad->handle, conf) == 0)
748
      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
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
760
gst_faad_do_raw_seek (GstFaad * faad, GstEvent * event)
761
{
762
  GstSeekFlags flags;
763
  GstSeekType start_type, end_type;
764
  GstFormat format;
765
  gdouble rate;
766
  gint64 start, start_time;
767
768
  gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
769
      &start_time, &end_type, NULL);
770
771
  if (rate != 1.0 ||
772
      format != GST_FORMAT_TIME ||
773
      start_type != GST_SEEK_TYPE_SET || end_type != GST_SEEK_TYPE_NONE) {
774
    return FALSE;
775
  }
776
777
  if (!gst_faad_src_convert (faad, GST_FORMAT_TIME, start_time,
778
          GST_FORMAT_BYTES, &start)) {
779
    return FALSE;
780
  }
781
782
  event = gst_event_new_seek (1.0, GST_FORMAT_BYTES, flags,
783
      GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_NONE, -1);
784
785
  GST_DEBUG_OBJECT (faad, "seeking to %" GST_TIME_FORMAT " at byte offset %"
786
      G_GINT64_FORMAT, GST_TIME_ARGS (start_time), start);
787
788
  return gst_pad_send_event (GST_PAD_PEER (faad->sinkpad), event);
789
}
790
791
static gboolean
792
gst_faad_src_event (GstPad * pad, GstEvent * event)
793
{
794
  GstFaad *faad;
795
  gboolean res;
796
797
  faad = GST_FAAD (gst_pad_get_parent (pad));
798
799
  GST_LOG_OBJECT (faad, "Handling %s event", GST_EVENT_TYPE_NAME (event));
800
801
  switch (GST_EVENT_TYPE (event)) {
802
    case GST_EVENT_SEEK:{
803
      /* try upstream first, there might be a demuxer */
804
      gst_event_ref (event);
805
      if (!(res = gst_pad_event_default (pad, event))) {
806
        res = gst_faad_do_raw_seek (faad, event);
807
      }
808
      gst_event_unref (event);
809
      break;
810
    }
811
    default:
812
      res = gst_pad_event_default (pad, event);
813
      break;
814
  }
815
816
  gst_object_unref (faad);
817
  return res;
818
}
819
820
static gboolean
821
gst_faad_sink_event (GstPad * pad, GstEvent * event)
1 by Sebastian Dröge
Import upstream version 0.10.0.3
822
{
823
  GstFaad *faad;
824
  gboolean res = TRUE;
825
826
  faad = GST_FAAD (gst_pad_get_parent (pad));
827
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
828
  GST_LOG_OBJECT (faad, "Handling %s event", GST_EVENT_TYPE_NAME (event));
1 by Sebastian Dröge
Import upstream version 0.10.0.3
829
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
830
  /* FIXME: we should probably handle FLUSH */
1 by Sebastian Dröge
Import upstream version 0.10.0.3
831
  switch (GST_EVENT_TYPE (event)) {
832
    case GST_EVENT_EOS:
833
      if (faad->tempbuf != NULL) {
834
        gst_buffer_unref (faad->tempbuf);
835
        faad->tempbuf = NULL;
836
      }
837
      res = gst_pad_push_event (faad->srcpad, event);
838
      break;
839
    case GST_EVENT_NEWSEGMENT:
840
    {
841
      GstFormat fmt;
842
      gboolean is_update;
843
      gint64 start, end, base;
844
      gdouble rate;
845
846
      gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
847
          &end, &base);
848
      if (fmt == GST_FORMAT_TIME) {
849
        GST_DEBUG ("Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
850
            GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
851
            GST_TIME_ARGS (end));
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
852
        gst_segment_set_newsegment (faad->segment, is_update, rate, fmt, start,
853
            end, base);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
854
      } else if (fmt == GST_FORMAT_BYTES) {
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
855
        gint64 new_start = 0;
856
        gint64 new_end = -1;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
857
858
        GST_DEBUG ("Got NEWSEGMENT event in GST_FORMAT_BYTES (%"
859
            G_GUINT64_FORMAT " - %" G_GUINT64_FORMAT ")", start, end);
860
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
861
        if (gst_faad_src_convert (faad, GST_FORMAT_BYTES, start,
862
                GST_FORMAT_TIME, &new_start)) {
863
          if (end != -1) {
864
            gst_faad_src_convert (faad, GST_FORMAT_BYTES, end,
865
                GST_FORMAT_TIME, &new_end);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
866
          }
867
        } else {
868
          GST_DEBUG
869
              ("no average bitrate yet, sending newsegment with start at 0");
870
        }
871
        gst_event_unref (event);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
872
873
        event = gst_event_new_new_segment (is_update, rate,
874
            GST_FORMAT_TIME, new_start, new_end, new_start);
875
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
876
        gst_segment_set_newsegment (faad->segment, is_update, rate,
877
            GST_FORMAT_TIME, new_start, new_end, new_start);
878
1 by Sebastian Dröge
Import upstream version 0.10.0.3
879
        GST_DEBUG ("Sending new NEWSEGMENT event, time %" GST_TIME_FORMAT
880
            " - %" GST_TIME_FORMAT, GST_TIME_ARGS (new_start),
881
            GST_TIME_ARGS (new_end));
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
882
883
        faad->next_ts = new_start;
884
        faad->prev_ts = GST_CLOCK_TIME_NONE;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
885
      }
886
887
      res = gst_pad_push_event (faad->srcpad, event);
888
      break;
889
    }
890
    default:
891
      res = gst_pad_event_default (pad, event);
892
      break;
893
  }
894
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
895
  gst_object_unref (faad);
896
  return res;
897
}
898
899
static gboolean
900
gst_faad_src_convert (GstFaad * faad, GstFormat src_format, gint64 src_val,
901
    GstFormat dest_format, gint64 * dest_val)
902
{
903
  guint64 bytes_in, time_out, val;
904
905
  if (src_format == dest_format) {
906
    if (dest_val)
907
      *dest_val = src_val;
908
    return TRUE;
909
  }
910
911
  GST_OBJECT_LOCK (faad);
912
  bytes_in = faad->bytes_in;
913
  time_out = faad->sum_dur_out;
914
  GST_OBJECT_UNLOCK (faad);
915
916
  if (bytes_in == 0 || time_out == 0)
917
    return FALSE;
918
919
  /* convert based on the average bitrate so far */
920
  if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
921
    val = gst_util_uint64_scale (src_val, time_out, bytes_in);
922
  } else if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
923
    val = gst_util_uint64_scale (src_val, bytes_in, time_out);
924
  } else {
925
    return FALSE;
926
  }
927
928
  if (dest_val)
929
    *dest_val = (gint64) val;
930
931
  return TRUE;
932
}
933
934
static gboolean
935
gst_faad_src_query (GstPad * pad, GstQuery * query)
936
{
937
  gboolean res = FALSE;
938
  GstFaad *faad;
939
  GstPad *peer = NULL;
940
941
  faad = GST_FAAD (gst_pad_get_parent (pad));
942
943
  GST_LOG_OBJECT (faad, "processing %s query", GST_QUERY_TYPE_NAME (query));
944
945
  switch (GST_QUERY_TYPE (query)) {
946
    case GST_QUERY_DURATION:{
947
      GstFormat format;
948
      gint64 len_bytes, duration;
949
950
      /* try upstream first, in case there's a demuxer */
951
      if ((res = gst_pad_query_default (pad, query)))
952
        break;
953
954
      gst_query_parse_duration (query, &format, NULL);
955
      if (format != GST_FORMAT_TIME) {
956
        GST_DEBUG_OBJECT (faad, "query failed: can't handle format %s",
957
            gst_format_get_name (format));
958
        break;
959
      }
960
961
      peer = gst_pad_get_peer (faad->sinkpad);
962
      if (peer == NULL)
963
        break;
964
965
      format = GST_FORMAT_BYTES;
966
      if (!gst_pad_query_duration (peer, &format, &len_bytes)) {
967
        GST_DEBUG_OBJECT (faad, "query failed: failed to get upstream length");
968
        break;
969
      }
970
971
      res = gst_faad_src_convert (faad, GST_FORMAT_BYTES, len_bytes,
972
          GST_FORMAT_TIME, &duration);
973
974
      if (res) {
975
        gst_query_set_duration (query, GST_FORMAT_TIME, duration);
976
977
        GST_LOG_OBJECT (faad, "duration estimate: %" GST_TIME_FORMAT,
978
            GST_TIME_ARGS (duration));
979
      }
980
      break;
981
    }
982
    case GST_QUERY_POSITION:{
983
      GstFormat format;
984
      gint64 pos_bytes, pos;
985
986
      /* try upstream first, in case there's a demuxer */
987
      if ((res = gst_pad_query_default (pad, query)))
988
        break;
989
990
      gst_query_parse_position (query, &format, NULL);
991
      if (format != GST_FORMAT_TIME) {
992
        GST_DEBUG_OBJECT (faad, "query failed: can't handle format %s",
993
            gst_format_get_name (format));
994
        break;
995
      }
996
997
      peer = gst_pad_get_peer (faad->sinkpad);
998
      if (peer == NULL)
999
        break;
1000
1001
      format = GST_FORMAT_BYTES;
1002
      if (!gst_pad_query_position (peer, &format, &pos_bytes)) {
1003
        GST_OBJECT_LOCK (faad);
1004
        pos = faad->next_ts;
1005
        GST_OBJECT_UNLOCK (faad);
1006
        res = TRUE;
1007
      } else {
1008
        res = gst_faad_src_convert (faad, GST_FORMAT_BYTES, pos_bytes,
1009
            GST_FORMAT_TIME, &pos);
1010
      }
1011
1012
      if (res) {
1013
        gst_query_set_position (query, GST_FORMAT_TIME, pos);
1014
      }
1015
      break;
1016
    }
1017
    default:
1018
      res = gst_pad_query_default (pad, query);
1019
      break;
1020
  }
1021
1022
  if (peer)
1023
    gst_object_unref (peer);
1024
1025
  gst_object_unref (faad);
1026
  return res;
1027
}
1028
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1029
1030
static gboolean
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1031
gst_faad_update_caps (GstFaad * faad, faacDecFrameInfo * info)
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1032
{
1033
  GstAudioChannelPosition *pos;
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1034
  gboolean ret;
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1035
  gboolean channel_map_failed;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1036
  GstCaps *caps;
1037
1038
  /* store new negotiation information */
1039
  faad->samplerate = info->samplerate;
1040
  faad->channels = info->channels;
1041
  g_free (faad->channel_positions);
1042
  faad->channel_positions = g_memdup (info->channel_position, faad->channels);
1043
1044
  caps = gst_caps_new_simple ("audio/x-raw-int",
1045
      "endianness", G_TYPE_INT, G_BYTE_ORDER,
1046
      "signed", G_TYPE_BOOLEAN, TRUE,
1047
      "width", G_TYPE_INT, 16,
1048
      "depth", G_TYPE_INT, 16,
1049
      "rate", G_TYPE_INT, faad->samplerate,
1050
      "channels", G_TYPE_INT, faad->channels, NULL);
1051
1052
  faad->bps = 16 / 8;
1053
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1054
  channel_map_failed = FALSE;
1055
  pos =
1056
      gst_faad_chanpos_to_gst (faad, faad->channel_positions, faad->channels,
1057
      &channel_map_failed);
1058
  if (channel_map_failed) {
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1059
    GST_DEBUG_OBJECT (faad, "Could not map channel positions");
1060
    gst_caps_unref (caps);
1061
    return FALSE;
1062
  }
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1063
  if (pos) {
1064
    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
1065
    g_free (pos);
1066
  }
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1067
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1068
  GST_DEBUG_OBJECT (faad, "New output caps: %" GST_PTR_FORMAT, caps);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1069
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1070
  ret = gst_pad_set_caps (faad->srcpad, caps);
1071
  gst_caps_unref (caps);
1072
1073
  return ret;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1074
}
1075
1076
/*
1077
 * Find syncpoint in ADTS/ADIF stream. Doesn't work for raw,
1078
 * packetized streams. Be careful when calling.
1079
 * Returns FALSE on no-sync, fills offset/length if one/two
1080
 * syncpoints are found, only returns TRUE when it finds two
1081
 * subsequent syncpoints (similar to mp3 typefinding in
1082
 * gst/typefind/) for ADTS because 12 bits isn't very reliable.
1083
 */
1084
static gboolean
1085
gst_faad_sync (GstBuffer * buf, guint * off)
1086
{
1087
  guint8 *data = GST_BUFFER_DATA (buf);
1088
  guint size = GST_BUFFER_SIZE (buf), n;
1089
  gint snc;
1090
1091
  GST_DEBUG ("Finding syncpoint");
1092
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1093
  /* check for too small a buffer */
1094
  if (size < 3)
1095
    return FALSE;
1096
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1097
  /* FIXME: for no-sync, we go over the same data for every new buffer.
1098
   * We should save the information somewhere. */
1099
  for (n = 0; n < size - 3; n++) {
1100
    snc = GST_READ_UINT16_BE (&data[n]);
1101
    if ((snc & 0xfff6) == 0xfff0) {
1102
      /* we have an ADTS syncpoint. Parse length and find
1103
       * next syncpoint. */
1104
      guint len;
1105
1106
      GST_DEBUG ("Found one ADTS syncpoint at offset 0x%x, tracing next...", n);
1107
1108
      if (size - n < 5) {
1109
        GST_DEBUG ("Not enough data to parse ADTS header");
1110
        return FALSE;
1111
      }
1112
1113
      *off = n;
1114
      len = ((data[n + 3] & 0x03) << 11) |
1115
          (data[n + 4] << 3) | ((data[n + 5] & 0xe0) >> 5);
1116
      if (n + len + 2 >= size) {
1117
        GST_DEBUG ("Next frame is not within reach");
1118
        return FALSE;
1119
      }
1120
1121
      snc = GST_READ_UINT16_BE (&data[n + len]);
1122
      if ((snc & 0xfff6) == 0xfff0) {
1123
        GST_DEBUG ("Found ADTS syncpoint at offset 0x%x (framelen %u)", n, len);
1124
        return TRUE;
1125
      }
1126
1127
      GST_DEBUG ("No next frame found... (should be at 0x%x)", n + len);
1128
    } else if (!memcmp (&data[n], "ADIF", 4)) {
1129
      /* we have an ADIF syncpoint. 4 bytes is enough. */
1130
      *off = n;
1131
      GST_DEBUG ("Found ADIF syncpoint at offset 0x%x", n);
1132
      return TRUE;
1133
    }
1134
  }
1135
1136
  GST_DEBUG ("Found no syncpoint");
1137
1138
  return FALSE;
1139
}
1140
1.1.3 by Sebastian Dröge
Import upstream version 0.10.3
1141
static gboolean
1142
looks_like_valid_header (guint8 * input_data, guint input_size)
1143
{
1144
  guint32 rate;
1145
  guint32 channels;
1146
1147
  if (input_size < 2)
1148
    return FALSE;
1149
1150
  rate = ((input_data[0] & 0x7) << 1) | ((input_data[1] & 0x80) >> 7);
1151
  channels = (input_data[1] & 0x78) >> 3;
1152
1153
  if (rate == 0xd || rate == 0xe)       /* Reserved values */
1154
    return FALSE;
1155
1156
  if (channels == 0)            /* Extended specifier: never seen one of these */
1157
    return FALSE;
1158
1159
  return TRUE;
1160
}
1161
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1162
static GstFlowReturn
1163
gst_faad_chain (GstPad * pad, GstBuffer * buffer)
1164
{
1165
  GstFlowReturn ret = GST_FLOW_OK;
1166
  guint input_size;
1167
  guint skip_bytes = 0;
1168
  guchar *input_data;
1169
  GstFaad *faad;
1170
  GstBuffer *outbuf;
1171
  faacDecFrameInfo info;
1172
  void *out;
1173
  gboolean run_loop = TRUE;
1174
  guint sync_off;
1175
1176
  faad = GST_FAAD (gst_pad_get_parent (pad));
1177
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1178
  GST_OBJECT_LOCK (faad);
1179
  faad->bytes_in += GST_BUFFER_SIZE (buffer);
1180
  GST_OBJECT_UNLOCK (faad);
1181
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1182
  if (GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE) {
1183
    /* some demuxers send multiple buffers in a row
1184
     *  with the same timestamp (e.g. matroskademux) */
1185
    if (GST_BUFFER_TIMESTAMP (buffer) != faad->prev_ts) {
1186
      faad->next_ts = GST_BUFFER_TIMESTAMP (buffer);
1187
      faad->prev_ts = GST_BUFFER_TIMESTAMP (buffer);
1188
    }
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1189
    GST_LOG_OBJECT (faad, "Timestamp on incoming buffer: %" GST_TIME_FORMAT
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1190
        ", next_ts: %" GST_TIME_FORMAT,
1191
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1192
        GST_TIME_ARGS (faad->next_ts));
1193
  }
1194
  /* buffer + remaining data */
1195
  if (faad->tempbuf) {
1196
    buffer = gst_buffer_join (faad->tempbuf, buffer);
1197
    faad->tempbuf = NULL;
1198
  }
1199
1200
  input_data = GST_BUFFER_DATA (buffer);
1201
  input_size = GST_BUFFER_SIZE (buffer);
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1202
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1203
  if (!faad->packetised) {
1204
    if (!gst_faad_sync (buffer, &sync_off)) {
1205
      goto next;
1206
    } else {
1207
      input_data += sync_off;
1208
      input_size -= sync_off;
1209
    }
1210
  }
1211
1212
  /* init if not already done during capsnego */
1213
  if (!faad->init) {
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1214
    guint32 rate;
1215
    guint8 ch;
1216
1217
    GST_DEBUG_OBJECT (faad, "initialising ...");
1.1.3 by Sebastian Dröge
Import upstream version 0.10.3
1218
    /* We check if the first data looks like it might plausibly contain
1219
     * appropriate initialisation info... if not, we use our fake_codec_data
1220
     */
1221
    if (looks_like_valid_header (input_data, input_size) || !faad->packetised) {
1222
      if (faacDecInit (faad->handle, input_data, input_size, &rate, &ch) < 0)
1223
        goto init_failed;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1224
1.1.3 by Sebastian Dröge
Import upstream version 0.10.3
1225
      GST_DEBUG_OBJECT (faad, "faacDecInit() ok: rate=%u,channels=%u", rate,
1226
          ch);
1227
    } else {
1228
      if ((gint8) faacDecInit2 (faad->handle, faad->fake_codec_data, 2,
1229
              &rate, &ch) < 0) {
1230
        goto init2_failed;
1231
      }
1232
      GST_DEBUG_OBJECT (faad, "faacDecInit2() ok: rate=%u,channels=%u", rate,
1233
          ch);
1234
    }
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1235
1236
    skip_bytes = 0;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1237
    faad->init = TRUE;
1238
1239
    /* make sure we create new caps below */
1240
    faad->samplerate = 0;
1241
    faad->channels = 0;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1242
    gst_faad_send_tags (faad);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1243
  }
1244
1245
  /* decode cycle */
1246
  info.bytesconsumed = input_size - skip_bytes;
1247
  info.error = 0;
1248
1249
  if (!faad->packetised) {
1250
    /* We must check that ourselves for raw stream */
1251
    run_loop = (input_size >= FAAD_MIN_STREAMSIZE);
1252
  }
1253
1254
  while ((input_size > 0) && run_loop) {
1255
1256
    if (faad->packetised) {
1257
      /* Only one packet per buffer, no matter how much is really consumed */
1258
      run_loop = FALSE;
1259
    } else {
1260
      if (input_size < FAAD_MIN_STREAMSIZE || info.bytesconsumed <= 0) {
1261
        break;
1262
      }
1263
    }
1264
1265
    out = faacDecDecode (faad->handle, &info, input_data + skip_bytes,
1266
        input_size - skip_bytes);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1267
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1268
    if (info.error > 0) {
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1269
      guint32 rate;
1270
      guint8 ch;
1271
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1272
      GST_DEBUG_OBJECT (faad, "decoding error: %s",
1273
          faacDecGetErrorMessage (info.error));
1274
1275
      goto out;
1276
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1277
      if (!faad->packetised)
1278
        goto decode_error;
1279
1280
      /* decode error? try again using faacDecInit2 
1281
       * fabricated private codec data from sink caps */
1282
      gst_faad_close_decoder (faad);
1283
      if (!gst_faad_open_decoder (faad))
1284
        goto init2_failed;
1285
1286
      GST_DEBUG_OBJECT (faad, "decoding error, reopening with faacDecInit2()");
1287
      if ((gint8) faacDecInit2 (faad->handle, faad->fake_codec_data, 2,
1288
              &rate, &ch) < 0) {
1289
        goto init2_failed;
1290
      }
1291
1292
      GST_DEBUG_OBJECT (faad, "faacDecInit2(): rate=%d,channels=%d", rate, ch);
1293
1294
      /* let's try again */
1295
      info.error = 0;
1296
      out = faacDecDecode (faad->handle, &info, input_data + skip_bytes,
1297
          input_size - skip_bytes);
1298
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1299
      if (info.error) {
1300
        faad->error_count++;
1301
        if (faad->error_count >= MAX_DECODE_ERRORS)
1302
          goto decode_error;
1303
        GST_DEBUG_OBJECT (faad,
1304
            "Failed to decode buffer: %s, count = %d, trying to resync",
1305
            faacDecGetErrorMessage (info.error), faad->error_count);
1306
        continue;
1307
      }
1308
1309
      faad->error_count = 0;    /* all fine, reset error counter */
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1310
    }
1311
1312
    if (info.bytesconsumed > input_size)
1313
      info.bytesconsumed = input_size;
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1314
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1315
    input_size -= info.bytesconsumed;
1316
    input_data += info.bytesconsumed;
1317
1318
    if (out && info.samples > 0) {
1319
      gboolean fmt_change = FALSE;
1320
1321
      /* see if we need to renegotiate */
1322
      if (info.samplerate != faad->samplerate ||
1323
          info.channels != faad->channels || !faad->channel_positions) {
1324
        fmt_change = TRUE;
1325
      } else {
1326
        gint i;
1327
1328
        for (i = 0; i < info.channels; i++) {
1329
          if (info.channel_position[i] != faad->channel_positions[i])
1330
            fmt_change = TRUE;
1331
        }
1332
      }
1333
1334
      if (fmt_change) {
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1335
        if (!gst_faad_update_caps (faad, &info)) {
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1336
          GST_ELEMENT_ERROR (faad, CORE, NEGOTIATION, (NULL),
1337
              ("Setting caps on source pad failed"));
1338
          ret = GST_FLOW_ERROR;
1339
          goto out;
1340
        }
1341
      }
1342
1343
      /* play decoded data */
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1344
      if (info.samples > 0) {
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1345
        guint bufsize = info.samples * faad->bps;
1346
        guint num_samples = info.samples / faad->channels;
1347
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1348
        GST_DEBUG_OBJECT (faad, "decoded %d samples", info.samples);
1349
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1350
        /* note: info.samples is total samples, not per channel */
1351
        ret =
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1352
            gst_pad_alloc_buffer_and_set_caps (faad->srcpad, 0, bufsize,
1353
            GST_PAD_CAPS (faad->srcpad), &outbuf);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1354
        if (ret != GST_FLOW_OK)
1355
          goto out;
1356
1357
        memcpy (GST_BUFFER_DATA (outbuf), out, GST_BUFFER_SIZE (outbuf));
1358
        GST_BUFFER_OFFSET (outbuf) =
1359
            GST_CLOCK_TIME_TO_FRAMES (faad->next_ts, faad->samplerate);
1360
        GST_BUFFER_TIMESTAMP (outbuf) = faad->next_ts;
1361
        GST_BUFFER_DURATION (outbuf) =
1362
            GST_FRAMES_TO_CLOCK_TIME (num_samples, faad->samplerate);
1363
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1364
        GST_OBJECT_LOCK (faad);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1365
        faad->next_ts += GST_BUFFER_DURATION (outbuf);
1366
        faad->sum_dur_out += GST_BUFFER_DURATION (outbuf);
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1367
        GST_OBJECT_UNLOCK (faad);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1368
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1369
        if ((outbuf = gst_audio_buffer_clip (outbuf, faad->segment,
1370
                    faad->samplerate, faad->bps * faad->channels))) {
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1371
          GST_LOG_OBJECT (faad,
1372
              "pushing buffer, off=%" G_GUINT64_FORMAT ", ts=%" GST_TIME_FORMAT,
1373
              GST_BUFFER_OFFSET (outbuf),
1374
              GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1375
          ret = gst_pad_push (faad->srcpad, outbuf);
1376
          if (ret != GST_FLOW_OK)
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1377
            goto out;
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1378
        }
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1379
      }
1380
    }
1381
  }
1382
1383
next:
1384
1385
  /* Keep the leftovers in raw stream */
1386
  if (input_size > 0 && !faad->packetised) {
1387
    if (input_size < GST_BUFFER_SIZE (buffer)) {
1388
      faad->tempbuf = gst_buffer_create_sub (buffer,
1389
          GST_BUFFER_SIZE (buffer) - input_size, input_size);
1390
    } else {
1391
      faad->tempbuf = buffer;
1392
      gst_buffer_ref (buffer);
1393
    }
1394
  }
1395
1396
out:
1397
1398
  gst_buffer_unref (buffer);
1399
  gst_object_unref (faad);
1400
1401
  return ret;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1402
1403
/* ERRORS */
1404
init_failed:
1405
  {
1406
    GST_ELEMENT_ERROR (faad, STREAM, DECODE, (NULL),
1407
        ("Failed to init decoder from stream"));
1408
    ret = GST_FLOW_ERROR;
1409
    goto out;
1410
  }
1411
init2_failed:
1412
  {
1413
    GST_ELEMENT_ERROR (faad, STREAM, DECODE, (NULL),
1414
        ("%s() failed", (faad->handle) ? "faacDecInit2" : "faacDecOpen"));
1415
    ret = GST_FLOW_ERROR;
1416
    goto out;
1417
  }
1418
decode_error:
1419
  {
1420
    GST_ELEMENT_ERROR (faad, STREAM, DECODE, (NULL),
1421
        ("Failed to decode buffer: %s", faacDecGetErrorMessage (info.error)));
1422
    ret = GST_FLOW_ERROR;
1423
    goto out;
1424
  }
1425
}
1426
1427
static gboolean
1428
gst_faad_open_decoder (GstFaad * faad)
1429
{
1430
  faacDecConfiguration *conf;
1431
1432
  faad->handle = faacDecOpen ();
1433
1434
  if (faad->handle == NULL) {
1435
    GST_WARNING_OBJECT (faad, "faacDecOpen() failed");
1436
    return FALSE;
1437
  }
1438
1439
  conf = faacDecGetCurrentConfiguration (faad->handle);
1440
  conf->defObjectType = LC;
1.1.7 by Sebastian Dröge
Import upstream version 0.10.4+cvs20070502
1441
  conf->dontUpSampleImplicitSBR = 1;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1442
  conf->outputFormat = FAAD_FMT_16BIT;
1443
1444
  if (faacDecSetConfiguration (faad->handle, conf) == 0) {
1445
    GST_WARNING_OBJECT (faad, "faacDecSetConfiguration() failed");
1446
    return FALSE;
1447
  }
1448
1449
  return TRUE;
1450
}
1451
1452
static void
1453
gst_faad_close_decoder (GstFaad * faad)
1454
{
1.1.6 by Sebastian Dröge
Import upstream version 0.10.4
1455
  if (faad->handle) {
1456
    faacDecClose (faad->handle);
1457
    faad->handle = NULL;
1458
  }
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1459
}
1460
1461
static GstStateChangeReturn
1462
gst_faad_change_state (GstElement * element, GstStateChange transition)
1463
{
1464
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1465
  GstFaad *faad = GST_FAAD (element);
1466
1467
  switch (transition) {
1468
    case GST_STATE_CHANGE_NULL_TO_READY:
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1469
      if (!gst_faad_open_decoder (faad))
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1470
        return GST_STATE_CHANGE_FAILURE;
1471
      break;
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1472
    case GST_STATE_CHANGE_READY_TO_PAUSED:
1.1.10 by Sebastian Dröge
Import upstream version 0.10.5.3
1473
      gst_segment_init (faad->segment, GST_FORMAT_TIME);
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1474
      break;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1475
    default:
1476
      break;
1477
  }
1478
1479
  if (GST_ELEMENT_CLASS (parent_class)->change_state)
1480
    ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1481
1482
  switch (transition) {
1483
    case GST_STATE_CHANGE_PAUSED_TO_READY:
1484
      faad->samplerate = -1;
1485
      faad->channels = -1;
1486
      faad->need_channel_setup = TRUE;
1487
      faad->init = FALSE;
1488
      g_free (faad->channel_positions);
1489
      faad->channel_positions = NULL;
1490
      faad->next_ts = 0;
1491
      faad->prev_ts = GST_CLOCK_TIME_NONE;
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1492
      faad->bytes_in = 0;
1493
      faad->sum_dur_out = 0;
1.1.4 by Sebastian Dröge
Import upstream version 0.10.3+cvs20060814
1494
      faad->error_count = 0;
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1495
      break;
1496
    case GST_STATE_CHANGE_READY_TO_NULL:
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1497
      gst_faad_close_decoder (faad);
1 by Sebastian Dröge
Import upstream version 0.10.0.3
1498
      if (faad->tempbuf) {
1499
        gst_buffer_unref (faad->tempbuf);
1500
        faad->tempbuf = NULL;
1501
      }
1502
      break;
1503
    default:
1504
      break;
1505
  }
1506
1507
  return ret;
1508
}
1509
1510
static gboolean
1511
plugin_init (GstPlugin * plugin)
1512
{
1513
  return gst_element_register (plugin, "faad", GST_RANK_PRIMARY, GST_TYPE_FAAD);
1514
}
1515
1516
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1517
    GST_VERSION_MINOR,
1518
    "faad",
1519
    "Free AAC Decoder (FAAD)",
1.1.2 by Sebastian Dröge
Import upstream version 0.10.1+cvs20060428
1520
    plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)