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

« back to all changes in this revision

Viewing changes to gst/audioparsers/gstmpegaudioparse.c

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2011-07-19 14:32:43 UTC
  • mfrom: (18.4.21 sid)
  • Revision ID: james.westby@ubuntu.com-20110719143243-p7pnkh45akfp0ihk
Tags: 0.10.22-2ubuntu1
* Rebased on debian unstable, remaining changes:
  - debian/gstreamer-plugins-bad.install
    * don't include dtmf, liveadder, rtpmux, autoconvert and shm, we include 
      them in -good

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GStreamer MPEG audio parser
2
 
 * Copyright (C) 2006-2007 Jan Schmidt <thaytan@mad.scientist.com>
3
 
 * Copyright (C) 2010 Mark Nauwelaerts <mnauw users sf net>
4
 
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
 
 *   Contact: Stefan Kost <stefan.kost@nokia.com>
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Library General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Library General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Library General Public
18
 
 * License along with this library; if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
 * Boston, MA 02111-1307, USA.
21
 
 */
22
 
/**
23
 
 * SECTION:element-mpegaudioparse
24
 
 * @short_description: MPEG audio parser
25
 
 * @see_also: #GstAmrParse, #GstAACParse
26
 
 *
27
 
 * Parses and frames mpeg1 audio streams. Provides seeking.
28
 
 *
29
 
 * <refsect2>
30
 
 * <title>Example launch line</title>
31
 
 * |[
32
 
 * gst-launch filesrc location=test.mp3 ! mpegaudioparse ! mad ! autoaudiosink
33
 
 * ]|
34
 
 * </refsect2>
35
 
 */
36
 
 
37
 
#ifdef HAVE_CONFIG_H
38
 
#include "config.h"
39
 
#endif
40
 
 
41
 
#include <string.h>
42
 
 
43
 
#include "gstmpegaudioparse.h"
44
 
#include <gst/base/gstbytereader.h>
45
 
 
46
 
GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
47
 
#define GST_CAT_DEFAULT mpeg_audio_parse_debug
48
 
 
49
 
#define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
50
 
#define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
51
 
#define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
52
 
#define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
53
 
#define MPEG_AUDIO_CHANNEL_MODE_MONO 3
54
 
 
55
 
#define CRC_UNKNOWN -1
56
 
#define CRC_PROTECTED 0
57
 
#define CRC_NOT_PROTECTED 1
58
 
 
59
 
#define XING_FRAMES_FLAG     0x0001
60
 
#define XING_BYTES_FLAG      0x0002
61
 
#define XING_TOC_FLAG        0x0004
62
 
#define XING_VBR_SCALE_FLAG  0x0008
63
 
 
64
 
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
65
 
    GST_PAD_SRC,
66
 
    GST_PAD_ALWAYS,
67
 
    GST_STATIC_CAPS ("audio/mpeg, "
68
 
        "mpegversion = (int) 1, "
69
 
        "layer = (int) [ 1, 3 ], "
70
 
        "rate = (int) [ 8000, 48000 ], channels = (int) [ 1, 2 ],"
71
 
        "parsed=(boolean) true")
72
 
    );
73
 
 
74
 
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
75
 
    GST_PAD_SINK,
76
 
    GST_PAD_ALWAYS,
77
 
    GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1, parsed=(boolean)false")
78
 
    );
79
 
 
80
 
static void gst_mpeg_audio_parse_finalize (GObject * object);
81
 
 
82
 
static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
83
 
static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
84
 
static gboolean gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
85
 
    GstBuffer * buffer, guint * size, gint * skipsize);
86
 
static GstFlowReturn gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
87
 
    GstBuffer * buf);
88
 
static GstFlowReturn gst_mpeg_audio_parse_pre_push_buffer (GstBaseParse * parse,
89
 
    GstBuffer * buf);
90
 
static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
91
 
    GstFormat src_format, gint64 src_value,
92
 
    GstFormat dest_format, gint64 * dest_value);
93
 
 
94
 
GST_BOILERPLATE (GstMpegAudioParse, gst_mpeg_audio_parse, GstBaseParse,
95
 
    GST_TYPE_BASE_PARSE);
96
 
 
97
 
#define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE  \
98
 
    (gst_mpeg_audio_channel_mode_get_type())
99
 
 
100
 
static const GEnumValue mpeg_audio_channel_mode[] = {
101
 
  {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
102
 
  {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
103
 
  {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
104
 
  {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
105
 
  {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
106
 
  {0, NULL, NULL},
107
 
};
108
 
 
109
 
static GType
110
 
gst_mpeg_audio_channel_mode_get_type (void)
111
 
{
112
 
  static GType mpeg_audio_channel_mode_type = 0;
113
 
 
114
 
  if (!mpeg_audio_channel_mode_type) {
115
 
    mpeg_audio_channel_mode_type =
116
 
        g_enum_register_static ("GstMpegAudioChannelMode",
117
 
        mpeg_audio_channel_mode);
118
 
  }
119
 
  return mpeg_audio_channel_mode_type;
120
 
}
121
 
 
122
 
static const gchar *
123
 
gst_mpeg_audio_channel_mode_get_nick (gint mode)
124
 
{
125
 
  guint i;
126
 
  for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
127
 
    if (mpeg_audio_channel_mode[i].value == mode)
128
 
      return mpeg_audio_channel_mode[i].value_nick;
129
 
  }
130
 
  return NULL;
131
 
}
132
 
 
133
 
static void
134
 
gst_mpeg_audio_parse_base_init (gpointer klass)
135
 
{
136
 
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137
 
 
138
 
  gst_element_class_add_pad_template (element_class,
139
 
      gst_static_pad_template_get (&sink_template));
140
 
  gst_element_class_add_pad_template (element_class,
141
 
      gst_static_pad_template_get (&src_template));
142
 
 
143
 
  gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
144
 
      "Codec/Parser/Audio",
145
 
      "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
146
 
      "Jan Schmidt <thaytan@mad.scientist.com>,"
147
 
      "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
148
 
}
149
 
 
150
 
static void
151
 
gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
152
 
{
153
 
  GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
154
 
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
155
 
 
156
 
  GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
157
 
      "MPEG1 audio stream parser");
158
 
 
159
 
  object_class->finalize = gst_mpeg_audio_parse_finalize;
160
 
 
161
 
  parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
162
 
  parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
163
 
  parse_class->check_valid_frame =
164
 
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_check_valid_frame);
165
 
  parse_class->parse_frame =
166
 
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_parse_frame);
167
 
  parse_class->pre_push_buffer =
168
 
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_buffer);
169
 
  parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
170
 
 
171
 
  /* register tags */
172
 
#define GST_TAG_CRC      "has-crc"
173
 
#define GST_TAG_MODE     "channel-mode"
174
 
 
175
 
  gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
176
 
      "has crc", "Using CRC", NULL);
177
 
  gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
178
 
      "channel mode", "MPEG audio channel mode", NULL);
179
 
 
180
 
  g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
181
 
}
182
 
 
183
 
static void
184
 
gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
185
 
{
186
 
  mp3parse->channels = -1;
187
 
  mp3parse->rate = -1;
188
 
  mp3parse->sent_codec_tag = FALSE;
189
 
  mp3parse->last_posted_crc = CRC_UNKNOWN;
190
 
  mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
191
 
 
192
 
  mp3parse->xing_flags = 0;
193
 
  mp3parse->xing_bitrate = 0;
194
 
  mp3parse->xing_frames = 0;
195
 
  mp3parse->xing_total_time = 0;
196
 
  mp3parse->xing_bytes = 0;
197
 
  mp3parse->xing_vbr_scale = 0;
198
 
  memset (mp3parse->xing_seek_table, 0, 100);
199
 
  memset (mp3parse->xing_seek_table_inverse, 0, 256);
200
 
 
201
 
  mp3parse->vbri_bitrate = 0;
202
 
  mp3parse->vbri_frames = 0;
203
 
  mp3parse->vbri_total_time = 0;
204
 
  mp3parse->vbri_bytes = 0;
205
 
  mp3parse->vbri_seek_points = 0;
206
 
  g_free (mp3parse->vbri_seek_table);
207
 
  mp3parse->vbri_seek_table = NULL;
208
 
}
209
 
 
210
 
static void
211
 
gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse,
212
 
    GstMpegAudioParseClass * klass)
213
 
{
214
 
  gst_mpeg_audio_parse_reset (mp3parse);
215
 
}
216
 
 
217
 
static void
218
 
gst_mpeg_audio_parse_finalize (GObject * object)
219
 
{
220
 
  G_OBJECT_CLASS (parent_class)->finalize (object);
221
 
}
222
 
 
223
 
static gboolean
224
 
gst_mpeg_audio_parse_start (GstBaseParse * parse)
225
 
{
226
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
227
 
 
228
 
  gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), 1024);
229
 
  GST_DEBUG_OBJECT (parse, "starting");
230
 
 
231
 
  gst_mpeg_audio_parse_reset (mp3parse);
232
 
 
233
 
  return TRUE;
234
 
}
235
 
 
236
 
static gboolean
237
 
gst_mpeg_audio_parse_stop (GstBaseParse * parse)
238
 
{
239
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
240
 
 
241
 
  GST_DEBUG_OBJECT (parse, "stopping");
242
 
 
243
 
  gst_mpeg_audio_parse_reset (mp3parse);
244
 
 
245
 
  return TRUE;
246
 
}
247
 
 
248
 
static const guint mp3types_bitrates[2][3][16] = {
249
 
  {
250
 
        {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
251
 
        {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
252
 
        {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
253
 
      },
254
 
  {
255
 
        {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
256
 
        {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
257
 
        {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
258
 
      },
259
 
};
260
 
 
261
 
static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
262
 
{22050, 24000, 16000},
263
 
{11025, 12000, 8000}
264
 
};
265
 
 
266
 
static inline guint
267
 
mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
268
 
    guint * put_version, guint * put_layer, guint * put_channels,
269
 
    guint * put_bitrate, guint * put_samplerate, guint * put_mode,
270
 
    guint * put_crc)
271
 
{
272
 
  guint length;
273
 
  gulong mode, samplerate, bitrate, layer, channels, padding, crc;
274
 
  gulong version;
275
 
  gint lsf, mpg25;
276
 
 
277
 
  if (header & (1 << 20)) {
278
 
    lsf = (header & (1 << 19)) ? 0 : 1;
279
 
    mpg25 = 0;
280
 
  } else {
281
 
    lsf = 1;
282
 
    mpg25 = 1;
283
 
  }
284
 
 
285
 
  version = 1 + lsf + mpg25;
286
 
 
287
 
  layer = 4 - ((header >> 17) & 0x3);
288
 
 
289
 
  crc = (header >> 16) & 0x1;
290
 
 
291
 
  bitrate = (header >> 12) & 0xF;
292
 
  bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
293
 
  /* The caller has ensured we have a valid header, so bitrate can't be
294
 
     zero here. */
295
 
  g_assert (bitrate != 0);
296
 
 
297
 
  samplerate = (header >> 10) & 0x3;
298
 
  samplerate = mp3types_freqs[lsf + mpg25][samplerate];
299
 
 
300
 
  padding = (header >> 9) & 0x1;
301
 
 
302
 
  mode = (header >> 6) & 0x3;
303
 
  channels = (mode == 3) ? 1 : 2;
304
 
 
305
 
  switch (layer) {
306
 
    case 1:
307
 
      length = 4 * ((bitrate * 12) / samplerate + padding);
308
 
      break;
309
 
    case 2:
310
 
      length = (bitrate * 144) / samplerate + padding;
311
 
      break;
312
 
    default:
313
 
    case 3:
314
 
      length = (bitrate * 144) / (samplerate << lsf) + padding;
315
 
      break;
316
 
  }
317
 
 
318
 
  GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
319
 
      length);
320
 
  GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
321
 
      "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
322
 
      layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
323
 
 
324
 
  if (put_version)
325
 
    *put_version = version;
326
 
  if (put_layer)
327
 
    *put_layer = layer;
328
 
  if (put_channels)
329
 
    *put_channels = channels;
330
 
  if (put_bitrate)
331
 
    *put_bitrate = bitrate;
332
 
  if (put_samplerate)
333
 
    *put_samplerate = samplerate;
334
 
  if (put_mode)
335
 
    *put_mode = mode;
336
 
  if (put_crc)
337
 
    *put_crc = crc;
338
 
 
339
 
  return length;
340
 
}
341
 
 
342
 
/* Minimum number of consecutive, valid-looking frames to consider
343
 
 * for resyncing */
344
 
#define MIN_RESYNC_FRAMES 3
345
 
 
346
 
/* Perform extended validation to check that subsequent headers match
347
 
 * the first header given here in important characteristics, to avoid
348
 
 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
349
 
 * frames to match their major characteristics.
350
 
 *
351
 
 * If at_eos is set to TRUE, we just check that we don't find any invalid
352
 
 * frames in whatever data is available, rather than requiring a full
353
 
 * MIN_RESYNC_FRAMES of data.
354
 
 *
355
 
 * Returns TRUE if we've seen enough data to validate or reject the frame.
356
 
 * If TRUE is returned, then *valid contains TRUE if it validated, or false
357
 
 * if we decided it was false sync.
358
 
 * If FALSE is returned, then *valid contains minimum needed data.
359
 
 */
360
 
static gboolean
361
 
gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
362
 
    guint32 header, int bpf, gboolean at_eos, gint * valid)
363
 
{
364
 
  guint32 next_header;
365
 
  const guint8 *data;
366
 
  guint available;
367
 
  int frames_found = 1;
368
 
  int offset = bpf;
369
 
 
370
 
  available = GST_BUFFER_SIZE (buf);
371
 
  data = GST_BUFFER_DATA (buf);
372
 
 
373
 
  while (frames_found < MIN_RESYNC_FRAMES) {
374
 
    /* Check if we have enough data for all these frames, plus the next
375
 
       frame header. */
376
 
    if (available < offset + 4) {
377
 
      if (at_eos) {
378
 
        /* Running out of data at EOS is fine; just accept it */
379
 
        *valid = TRUE;
380
 
        return TRUE;
381
 
      } else {
382
 
        *valid = offset + 4;
383
 
        return FALSE;
384
 
      }
385
 
    }
386
 
 
387
 
    next_header = GST_READ_UINT32_BE (data + offset);
388
 
    GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
389
 
        offset, (unsigned int) header, (unsigned int) next_header, bpf);
390
 
 
391
 
/* mask the bits which are allowed to differ between frames */
392
 
#define HDRMASK ~((0xF << 12)  /* bitrate */ | \
393
 
                  (0x1 <<  9)  /* padding */ | \
394
 
                  (0xf <<  4)  /* mode|mode extension */ | \
395
 
                  (0xf))        /* copyright|emphasis */
396
 
 
397
 
    if ((next_header & HDRMASK) != (header & HDRMASK)) {
398
 
      /* If any of the unmasked bits don't match, then it's not valid */
399
 
      GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
400
 
          "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
401
 
          (guint) header, (guint) header & HDRMASK, (guint) next_header,
402
 
          (guint) next_header & HDRMASK, bpf);
403
 
      *valid = FALSE;
404
 
      return TRUE;
405
 
    } else if ((((next_header >> 12) & 0xf) == 0) ||
406
 
        (((next_header >> 12) & 0xf) == 0xf)) {
407
 
      /* The essential parts were the same, but the bitrate held an
408
 
         invalid value - also reject */
409
 
      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
410
 
      *valid = FALSE;
411
 
      return TRUE;
412
 
    }
413
 
 
414
 
    bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
415
 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL);
416
 
 
417
 
    offset += bpf;
418
 
    frames_found++;
419
 
  }
420
 
 
421
 
  *valid = TRUE;
422
 
  return TRUE;
423
 
}
424
 
 
425
 
static gboolean
426
 
gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
427
 
    unsigned long head)
428
 
{
429
 
  GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
430
 
  /* if it's not a valid sync */
431
 
  if ((head & 0xffe00000) != 0xffe00000) {
432
 
    GST_WARNING_OBJECT (mp3parse, "invalid sync");
433
 
    return FALSE;
434
 
  }
435
 
  /* if it's an invalid MPEG version */
436
 
  if (((head >> 19) & 3) == 0x1) {
437
 
    GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
438
 
        (head >> 19) & 3);
439
 
    return FALSE;
440
 
  }
441
 
  /* if it's an invalid layer */
442
 
  if (!((head >> 17) & 3)) {
443
 
    GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
444
 
    return FALSE;
445
 
  }
446
 
  /* if it's an invalid bitrate */
447
 
  if (((head >> 12) & 0xf) == 0x0) {
448
 
    GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx."
449
 
        "Free format files are not supported yet", (head >> 12) & 0xf);
450
 
    return FALSE;
451
 
  }
452
 
  if (((head >> 12) & 0xf) == 0xf) {
453
 
    GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
454
 
    return FALSE;
455
 
  }
456
 
  /* if it's an invalid samplerate */
457
 
  if (((head >> 10) & 0x3) == 0x3) {
458
 
    GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
459
 
        (head >> 10) & 0x3);
460
 
    return FALSE;
461
 
  }
462
 
 
463
 
  if ((head & 0x3) == 0x2) {
464
 
    /* Ignore this as there are some files with emphasis 0x2 that can
465
 
     * be played fine. See BGO #537235 */
466
 
    GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
467
 
  }
468
 
 
469
 
  return TRUE;
470
 
}
471
 
 
472
 
static gboolean
473
 
gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse, GstBuffer * buf,
474
 
    guint * framesize, gint * skipsize)
475
 
{
476
 
  GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
477
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
478
 
  gint off, bpf;
479
 
  gboolean sync, drain, valid, caps_change;
480
 
  guint32 header;
481
 
  guint bitrate, layer, rate, channels, version, mode, crc;
482
 
 
483
 
  if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
484
 
    return FALSE;
485
 
 
486
 
  off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
487
 
      0, GST_BUFFER_SIZE (buf));
488
 
 
489
 
  GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
490
 
 
491
 
  /* didn't find anything that looks like a sync word, skip */
492
 
  if (off < 0) {
493
 
    *skipsize = GST_BUFFER_SIZE (buf) - 3;
494
 
    return FALSE;
495
 
  }
496
 
 
497
 
  /* possible frame header, but not at offset 0? skip bytes before sync */
498
 
  if (off > 0) {
499
 
    *skipsize = off;
500
 
    return FALSE;
501
 
  }
502
 
 
503
 
  /* make sure the values in the frame header look sane */
504
 
  header = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf));
505
 
  if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
506
 
    *skipsize = 1;
507
 
    return FALSE;
508
 
  }
509
 
 
510
 
  GST_LOG_OBJECT (parse, "got frame");
511
 
 
512
 
  bpf = mp3_type_frame_length_from_header (mp3parse, header,
513
 
      &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
514
 
  g_assert (bpf != 0);
515
 
 
516
 
  if (channels != mp3parse->channels || rate != mp3parse->rate ||
517
 
      layer != mp3parse->layer || version != mp3parse->version)
518
 
    caps_change = TRUE;
519
 
  else
520
 
    caps_change = FALSE;
521
 
 
522
 
  sync = gst_base_parse_get_sync (parse);
523
 
  drain = gst_base_parse_get_drain (parse);
524
 
 
525
 
  if (!drain && (!sync || caps_change)) {
526
 
    if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, drain,
527
 
            &valid)) {
528
 
      /* not enough data */
529
 
      gst_base_parse_set_min_frame_size (parse, valid);
530
 
      *skipsize = 0;
531
 
      return FALSE;
532
 
    } else {
533
 
      if (!valid) {
534
 
        *skipsize = off + 2;
535
 
        return FALSE;
536
 
      }
537
 
    }
538
 
  }
539
 
 
540
 
  *framesize = bpf;
541
 
  return TRUE;
542
 
}
543
 
 
544
 
static void
545
 
gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
546
 
    GstBuffer * buf)
547
 
{
548
 
  const guint32 xing_id = 0x58696e67;   /* 'Xing' in hex */
549
 
  const guint32 info_id = 0x496e666f;   /* 'Info' in hex - found in LAME CBR files */
550
 
  const guint32 vbri_id = 0x56425249;   /* 'VBRI' in hex */
551
 
  gint offset;
552
 
  guint64 avail;
553
 
  gint64 upstream_total_bytes = 0;
554
 
  GstFormat fmt = GST_FORMAT_BYTES;
555
 
  guint32 read_id;
556
 
  const guint8 *data;
557
 
  GstBaseParseSeekable seekable;
558
 
  guint bitrate;
559
 
 
560
 
  if (mp3parse->sent_codec_tag)
561
 
    return;
562
 
 
563
 
  /* Check first frame for Xing info */
564
 
  if (mp3parse->version == 1) { /* MPEG-1 file */
565
 
    if (mp3parse->channels == 1)
566
 
      offset = 0x11;
567
 
    else
568
 
      offset = 0x20;
569
 
  } else {                      /* MPEG-2 header */
570
 
    if (mp3parse->channels == 1)
571
 
      offset = 0x09;
572
 
    else
573
 
      offset = 0x11;
574
 
  }
575
 
  /* Skip the 4 bytes of the MP3 header too */
576
 
  offset += 4;
577
 
 
578
 
  /* Check if we have enough data to read the Xing header */
579
 
  avail = GST_BUFFER_SIZE (buf);
580
 
  data = GST_BUFFER_DATA (buf);
581
 
  if (avail < offset + 8)
582
 
    return;
583
 
 
584
 
  /* The header starts at the provided offset */
585
 
  data += offset;
586
 
 
587
 
  /* obtain real upstream total bytes */
588
 
  fmt = GST_FORMAT_BYTES;
589
 
  if (!gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE
590
 
              (mp3parse)), &fmt, &upstream_total_bytes))
591
 
    upstream_total_bytes = 0;
592
 
 
593
 
  read_id = GST_READ_UINT32_BE (data);
594
 
  if (read_id == xing_id || read_id == info_id) {
595
 
    guint32 xing_flags;
596
 
    guint bytes_needed = offset + 8;
597
 
    gint64 total_bytes;
598
 
    GstClockTime total_time;
599
 
 
600
 
    GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
601
 
 
602
 
    /* Read 4 base bytes of flags, big-endian */
603
 
    xing_flags = GST_READ_UINT32_BE (data + 4);
604
 
    if (xing_flags & XING_FRAMES_FLAG)
605
 
      bytes_needed += 4;
606
 
    if (xing_flags & XING_BYTES_FLAG)
607
 
      bytes_needed += 4;
608
 
    if (xing_flags & XING_TOC_FLAG)
609
 
      bytes_needed += 100;
610
 
    if (xing_flags & XING_VBR_SCALE_FLAG)
611
 
      bytes_needed += 4;
612
 
    if (avail < bytes_needed) {
613
 
      GST_DEBUG_OBJECT (mp3parse,
614
 
          "Not enough data to read Xing header (need %d)", bytes_needed);
615
 
      return;
616
 
    }
617
 
 
618
 
    GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
619
 
    mp3parse->xing_flags = xing_flags;
620
 
 
621
 
    data = GST_BUFFER_DATA (buf);
622
 
    data += offset + 8;
623
 
 
624
 
    if (xing_flags & XING_FRAMES_FLAG) {
625
 
      mp3parse->xing_frames = GST_READ_UINT32_BE (data);
626
 
      if (mp3parse->xing_frames == 0) {
627
 
        GST_WARNING_OBJECT (mp3parse,
628
 
            "Invalid number of frames in Xing header");
629
 
        mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
630
 
      } else {
631
 
        mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
632
 
            (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
633
 
            mp3parse->rate);
634
 
      }
635
 
 
636
 
      data += 4;
637
 
    } else {
638
 
      mp3parse->xing_frames = 0;
639
 
      mp3parse->xing_total_time = 0;
640
 
    }
641
 
 
642
 
    if (xing_flags & XING_BYTES_FLAG) {
643
 
      mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
644
 
      if (mp3parse->xing_bytes == 0) {
645
 
        GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
646
 
        mp3parse->xing_flags &= ~XING_BYTES_FLAG;
647
 
      }
648
 
      data += 4;
649
 
    } else {
650
 
      mp3parse->xing_bytes = 0;
651
 
    }
652
 
 
653
 
    /* If we know the upstream size and duration, compute the
654
 
     * total bitrate, rounded up to the nearest kbit/sec */
655
 
    if ((total_time = mp3parse->xing_total_time) &&
656
 
        (total_bytes = mp3parse->xing_bytes)) {
657
 
      mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
658
 
          8 * GST_SECOND, total_time);
659
 
      mp3parse->xing_bitrate += 500;
660
 
      mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
661
 
    }
662
 
 
663
 
    if (xing_flags & XING_TOC_FLAG) {
664
 
      int i, percent = 0;
665
 
      guchar *table = mp3parse->xing_seek_table;
666
 
      guchar old = 0, new;
667
 
      guint first;
668
 
 
669
 
      first = data[0];
670
 
      GST_DEBUG_OBJECT (mp3parse,
671
 
          "Subtracting initial offset of %d bytes from Xing TOC", first);
672
 
 
673
 
      /* xing seek table: percent time -> 1/256 bytepos */
674
 
      for (i = 0; i < 100; i++) {
675
 
        new = data[i] - first;
676
 
        if (old > new) {
677
 
          GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
678
 
          mp3parse->xing_flags &= ~XING_TOC_FLAG;
679
 
          goto skip_toc;
680
 
        }
681
 
        mp3parse->xing_seek_table[i] = old = new;
682
 
      }
683
 
 
684
 
      /* build inverse table: 1/256 bytepos -> 1/100 percent time */
685
 
      for (i = 0; i < 256; i++) {
686
 
        while (percent < 99 && table[percent + 1] <= i)
687
 
          percent++;
688
 
 
689
 
        if (table[percent] == i) {
690
 
          mp3parse->xing_seek_table_inverse[i] = percent * 100;
691
 
        } else if (table[percent] < i && percent < 99) {
692
 
          gdouble fa, fb, fx;
693
 
          gint a = percent, b = percent + 1;
694
 
 
695
 
          fa = table[a];
696
 
          fb = table[b];
697
 
          fx = (b - a) / (fb - fa) * (i - fa) + a;
698
 
          mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
699
 
        } else if (percent == 99) {
700
 
          gdouble fa, fb, fx;
701
 
          gint a = percent, b = 100;
702
 
 
703
 
          fa = table[a];
704
 
          fb = 256.0;
705
 
          fx = (b - a) / (fb - fa) * (i - fa) + a;
706
 
          mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
707
 
        }
708
 
      }
709
 
    skip_toc:
710
 
      data += 100;
711
 
    } else {
712
 
      memset (mp3parse->xing_seek_table, 0, 100);
713
 
      memset (mp3parse->xing_seek_table_inverse, 0, 256);
714
 
    }
715
 
 
716
 
    if (xing_flags & XING_VBR_SCALE_FLAG) {
717
 
      mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
718
 
    } else
719
 
      mp3parse->xing_vbr_scale = 0;
720
 
 
721
 
    GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
722
 
        GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
723
 
        GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
724
 
        mp3parse->xing_vbr_scale);
725
 
 
726
 
    /* check for truncated file */
727
 
    if (upstream_total_bytes && mp3parse->xing_bytes &&
728
 
        mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
729
 
      GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
730
 
          "invalidating Xing header duration and size");
731
 
      mp3parse->xing_flags &= ~XING_BYTES_FLAG;
732
 
      mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
733
 
    }
734
 
  } else if (read_id == vbri_id) {
735
 
    gint64 total_bytes, total_frames;
736
 
    GstClockTime total_time;
737
 
    guint16 nseek_points;
738
 
 
739
 
    GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
740
 
    if (avail < offset + 26) {
741
 
      GST_DEBUG_OBJECT (mp3parse,
742
 
          "Not enough data to read VBRI header (need %d)", offset + 26);
743
 
      return;
744
 
    }
745
 
 
746
 
    GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
747
 
    data = GST_BUFFER_DATA (buf);
748
 
    data += offset + 4;
749
 
 
750
 
    if (GST_READ_UINT16_BE (data) != 0x0001) {
751
 
      GST_WARNING_OBJECT (mp3parse,
752
 
          "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
753
 
      return;
754
 
    }
755
 
    data += 2;
756
 
 
757
 
    /* Skip encoder delay */
758
 
    data += 2;
759
 
 
760
 
    /* Skip quality */
761
 
    data += 2;
762
 
 
763
 
    total_bytes = GST_READ_UINT32_BE (data);
764
 
    if (total_bytes != 0)
765
 
      mp3parse->vbri_bytes = total_bytes;
766
 
    data += 4;
767
 
 
768
 
    total_frames = GST_READ_UINT32_BE (data);
769
 
    if (total_frames != 0) {
770
 
      mp3parse->vbri_frames = total_frames;
771
 
      mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
772
 
          (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
773
 
    }
774
 
    data += 4;
775
 
 
776
 
    /* If we know the upstream size and duration, compute the
777
 
     * total bitrate, rounded up to the nearest kbit/sec */
778
 
    if ((total_time = mp3parse->vbri_total_time) &&
779
 
        (total_bytes = mp3parse->vbri_bytes)) {
780
 
      mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
781
 
          8 * GST_SECOND, total_time);
782
 
      mp3parse->vbri_bitrate += 500;
783
 
      mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
784
 
    }
785
 
 
786
 
    nseek_points = GST_READ_UINT16_BE (data);
787
 
    data += 2;
788
 
 
789
 
    if (nseek_points > 0) {
790
 
      guint scale, seek_bytes, seek_frames;
791
 
      gint i;
792
 
 
793
 
      mp3parse->vbri_seek_points = nseek_points;
794
 
 
795
 
      scale = GST_READ_UINT16_BE (data);
796
 
      data += 2;
797
 
 
798
 
      seek_bytes = GST_READ_UINT16_BE (data);
799
 
      data += 2;
800
 
 
801
 
      seek_frames = GST_READ_UINT16_BE (data);
802
 
 
803
 
      if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
804
 
        GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
805
 
        goto out_vbri;
806
 
      }
807
 
 
808
 
      if (avail < offset + 26 + nseek_points * seek_bytes) {
809
 
        GST_WARNING_OBJECT (mp3parse,
810
 
            "Not enough data to read VBRI seek table (need %d)",
811
 
            offset + 26 + nseek_points * seek_bytes);
812
 
        goto out_vbri;
813
 
      }
814
 
 
815
 
      if (seek_frames * nseek_points < total_frames - seek_frames ||
816
 
          seek_frames * nseek_points > total_frames + seek_frames) {
817
 
        GST_WARNING_OBJECT (mp3parse,
818
 
            "VBRI seek table doesn't cover the complete file");
819
 
        goto out_vbri;
820
 
      }
821
 
 
822
 
      if (avail < offset + 26) {
823
 
        GST_DEBUG_OBJECT (mp3parse,
824
 
            "Not enough data to read VBRI header (need %d)",
825
 
            offset + 26 + nseek_points * seek_bytes);
826
 
        return;
827
 
      }
828
 
 
829
 
      data = GST_BUFFER_DATA (buf);
830
 
      data += offset + 26;
831
 
 
832
 
      /* VBRI seek table: frame/seek_frames -> byte */
833
 
      mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
834
 
      if (seek_bytes == 4)
835
 
        for (i = 0; i < nseek_points; i++) {
836
 
          mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
837
 
          data += 4;
838
 
      } else if (seek_bytes == 3)
839
 
        for (i = 0; i < nseek_points; i++) {
840
 
          mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
841
 
          data += 3;
842
 
      } else if (seek_bytes == 2)
843
 
        for (i = 0; i < nseek_points; i++) {
844
 
          mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
845
 
          data += 2;
846
 
      } else                    /* seek_bytes == 1 */
847
 
        for (i = 0; i < nseek_points; i++) {
848
 
          mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
849
 
          data += 1;
850
 
        }
851
 
    }
852
 
  out_vbri:
853
 
 
854
 
    GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
855
 
        GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
856
 
        GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
857
 
 
858
 
    /* check for truncated file */
859
 
    if (upstream_total_bytes && mp3parse->vbri_bytes &&
860
 
        mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
861
 
      GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
862
 
          "invalidating VBRI header duration and size");
863
 
      mp3parse->vbri_valid = FALSE;
864
 
    } else {
865
 
      mp3parse->vbri_valid = TRUE;
866
 
    }
867
 
  } else {
868
 
    GST_DEBUG_OBJECT (mp3parse,
869
 
        "Xing, LAME or VBRI header not found in first frame");
870
 
  }
871
 
 
872
 
  /* set duration if tables provided a valid one */
873
 
  if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
874
 
    gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
875
 
        mp3parse->xing_total_time, 0);
876
 
  }
877
 
  if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
878
 
    gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
879
 
        mp3parse->vbri_total_time, 0);
880
 
  }
881
 
 
882
 
  /* tell baseclass how nicely we can seek, and a bitrate if one found */
883
 
  seekable = GST_BASE_PARSE_SEEK_DEFAULT;
884
 
  if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
885
 
      mp3parse->xing_total_time)
886
 
    seekable = GST_BASE_PARSE_SEEK_TABLE;
887
 
 
888
 
  if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
889
 
      mp3parse->vbri_total_time)
890
 
    seekable = GST_BASE_PARSE_SEEK_TABLE;
891
 
 
892
 
  if (mp3parse->xing_bitrate)
893
 
    bitrate = mp3parse->xing_bitrate;
894
 
  else if (mp3parse->vbri_bitrate)
895
 
    bitrate = mp3parse->vbri_bitrate;
896
 
  else
897
 
    bitrate = 0;
898
 
 
899
 
  gst_base_parse_set_seek (GST_BASE_PARSE (mp3parse), seekable, bitrate);
900
 
}
901
 
 
902
 
static GstFlowReturn
903
 
gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse, GstBuffer * buf)
904
 
{
905
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
906
 
  guint bitrate, layer, rate, channels, version, mode, crc;
907
 
 
908
 
  g_return_val_if_fail (GST_BUFFER_SIZE (buf) >= 4, GST_FLOW_ERROR);
909
 
 
910
 
  if (!mp3_type_frame_length_from_header (mp3parse,
911
 
          GST_READ_UINT32_BE (GST_BUFFER_DATA (buf)),
912
 
          &version, &layer, &channels, &bitrate, &rate, &mode, &crc))
913
 
    goto broken_header;
914
 
 
915
 
  if (G_UNLIKELY (channels != mp3parse->channels || rate != mp3parse->rate ||
916
 
          layer != mp3parse->layer || version != mp3parse->version)) {
917
 
    GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
918
 
        "mpegversion", G_TYPE_INT, 1,
919
 
        "mpegaudioversion", G_TYPE_INT, version,
920
 
        "layer", G_TYPE_INT, layer,
921
 
        "rate", G_TYPE_INT, rate,
922
 
        "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
923
 
    gst_buffer_set_caps (buf, caps);
924
 
    gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
925
 
    gst_caps_unref (caps);
926
 
 
927
 
    mp3parse->rate = rate;
928
 
    mp3parse->channels = channels;
929
 
    mp3parse->layer = layer;
930
 
    mp3parse->version = version;
931
 
 
932
 
    /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
933
 
    if (mp3parse->layer == 1)
934
 
      mp3parse->spf = 384;
935
 
    else if (mp3parse->layer == 2)
936
 
      mp3parse->spf = 1152;
937
 
    else if (mp3parse->version == 1) {
938
 
      mp3parse->spf = 1152;
939
 
    } else {
940
 
      /* MPEG-2 or "2.5" */
941
 
      mp3parse->spf = 576;
942
 
    }
943
 
 
944
 
    /* lead_in:
945
 
     * We start pushing 9 frames earlier (29 frames for MPEG2) than
946
 
     * segment start to be able to decode the first frame we want.
947
 
     * 9 (29) frames are the theoretical maximum of frames that contain
948
 
     * data for the current frame (bit reservoir).
949
 
     *
950
 
     * lead_out:
951
 
     * Some mp3 streams have an offset in the timestamps, for which we have to
952
 
     * push the frame *after* the end position in order for the decoder to be
953
 
     * able to decode everything up until the segment.stop position. */
954
 
    gst_base_parse_set_frame_props (parse, mp3parse->rate, mp3parse->spf,
955
 
        (version == 1) ? 10 : 30, 2);
956
 
  }
957
 
 
958
 
  /* For first frame; check for seek tables and output a codec tag */
959
 
  gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
960
 
 
961
 
  /* store some frame info for later processing */
962
 
  mp3parse->last_crc = crc;
963
 
  mp3parse->last_mode = mode;
964
 
 
965
 
  return GST_FLOW_OK;
966
 
 
967
 
/* ERRORS */
968
 
broken_header:
969
 
  {
970
 
    /* this really shouldn't ever happen */
971
 
    GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
972
 
    return GST_FLOW_ERROR;
973
 
  }
974
 
}
975
 
 
976
 
static gboolean
977
 
gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
978
 
    GstClockTime ts, gint64 * bytepos)
979
 
{
980
 
  gint64 total_bytes;
981
 
  GstClockTime total_time;
982
 
 
983
 
  /* If XING seek table exists use this for time->byte conversion */
984
 
  if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
985
 
      (total_bytes = mp3parse->xing_bytes) &&
986
 
      (total_time = mp3parse->xing_total_time)) {
987
 
    gdouble fa, fb, fx;
988
 
    gdouble percent =
989
 
        CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
990
 
        gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
991
 
    gint index = CLAMP (percent, 0, 99);
992
 
 
993
 
    fa = mp3parse->xing_seek_table[index];
994
 
    if (index < 99)
995
 
      fb = mp3parse->xing_seek_table[index + 1];
996
 
    else
997
 
      fb = 256.0;
998
 
 
999
 
    fx = fa + (fb - fa) * (percent - index);
1000
 
 
1001
 
    *bytepos = (1.0 / 256.0) * fx * total_bytes;
1002
 
 
1003
 
    return TRUE;
1004
 
  }
1005
 
 
1006
 
  if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1007
 
      (total_time = mp3parse->vbri_total_time)) {
1008
 
    gint i, j;
1009
 
    gdouble a, b, fa, fb;
1010
 
 
1011
 
    i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1012
 
    i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1013
 
 
1014
 
    a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1015
 
            mp3parse->vbri_seek_points));
1016
 
    fa = 0.0;
1017
 
    for (j = i; j >= 0; j--)
1018
 
      fa += mp3parse->vbri_seek_table[j];
1019
 
 
1020
 
    if (i + 1 < mp3parse->vbri_seek_points) {
1021
 
      b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1022
 
              mp3parse->vbri_seek_points));
1023
 
      fb = fa + mp3parse->vbri_seek_table[i + 1];
1024
 
    } else {
1025
 
      b = gst_guint64_to_gdouble (total_time);
1026
 
      fb = total_bytes;
1027
 
    }
1028
 
 
1029
 
    *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1030
 
 
1031
 
    return TRUE;
1032
 
  }
1033
 
 
1034
 
  return FALSE;
1035
 
}
1036
 
 
1037
 
static gboolean
1038
 
gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1039
 
    gint64 bytepos, GstClockTime * ts)
1040
 
{
1041
 
  gint64 total_bytes;
1042
 
  GstClockTime total_time;
1043
 
 
1044
 
  /* If XING seek table exists use this for byte->time conversion */
1045
 
  if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1046
 
      (total_bytes = mp3parse->xing_bytes) &&
1047
 
      (total_time = mp3parse->xing_total_time)) {
1048
 
    gdouble fa, fb, fx;
1049
 
    gdouble pos;
1050
 
    gint index;
1051
 
 
1052
 
    pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1053
 
    index = CLAMP (pos, 0, 255);
1054
 
    fa = mp3parse->xing_seek_table_inverse[index];
1055
 
    if (index < 255)
1056
 
      fb = mp3parse->xing_seek_table_inverse[index + 1];
1057
 
    else
1058
 
      fb = 10000.0;
1059
 
 
1060
 
    fx = fa + (fb - fa) * (pos - index);
1061
 
 
1062
 
    *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1063
 
 
1064
 
    return TRUE;
1065
 
  }
1066
 
 
1067
 
  if (mp3parse->vbri_seek_table &&
1068
 
      (total_bytes = mp3parse->vbri_bytes) &&
1069
 
      (total_time = mp3parse->vbri_total_time)) {
1070
 
    gint i = 0;
1071
 
    guint64 sum = 0;
1072
 
    gdouble a, b, fa, fb;
1073
 
 
1074
 
    do {
1075
 
      sum += mp3parse->vbri_seek_table[i];
1076
 
      i++;
1077
 
    } while (i + 1 < mp3parse->vbri_seek_points
1078
 
        && sum + mp3parse->vbri_seek_table[i] < bytepos);
1079
 
    i--;
1080
 
 
1081
 
    a = gst_guint64_to_gdouble (sum);
1082
 
    fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1083
 
            mp3parse->vbri_seek_points));
1084
 
 
1085
 
    if (i + 1 < mp3parse->vbri_seek_points) {
1086
 
      b = a + mp3parse->vbri_seek_table[i + 1];
1087
 
      fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1088
 
              mp3parse->vbri_seek_points));
1089
 
    } else {
1090
 
      b = total_bytes;
1091
 
      fb = gst_guint64_to_gdouble (total_time);
1092
 
    }
1093
 
 
1094
 
    *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1095
 
 
1096
 
    return TRUE;
1097
 
  }
1098
 
 
1099
 
  return FALSE;
1100
 
}
1101
 
 
1102
 
static gboolean
1103
 
gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1104
 
    gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1105
 
{
1106
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1107
 
  gboolean res = FALSE;
1108
 
 
1109
 
  if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1110
 
    res =
1111
 
        gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1112
 
  else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1113
 
    res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1114
 
        (GstClockTime *) dest_value);
1115
 
 
1116
 
  /* if no tables, fall back to default estimated rate based conversion */
1117
 
  if (!res)
1118
 
    return gst_base_parse_convert_default (parse, src_format, src_value,
1119
 
        dest_format, dest_value);
1120
 
 
1121
 
  return res;
1122
 
}
1123
 
 
1124
 
static GstFlowReturn
1125
 
gst_mpeg_audio_parse_pre_push_buffer (GstBaseParse * parse, GstBuffer * buf)
1126
 
{
1127
 
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1128
 
  GstTagList *taglist;
1129
 
 
1130
 
  /* tag sending done late enough in hook to ensure pending events
1131
 
   * have already been sent */
1132
 
 
1133
 
  if (!mp3parse->sent_codec_tag) {
1134
 
    gchar *codec;
1135
 
 
1136
 
    /* codec tag */
1137
 
    if (mp3parse->layer == 3) {
1138
 
      codec = g_strdup_printf ("MPEG %d Audio, Layer %d (MP3)",
1139
 
          mp3parse->version, mp3parse->layer);
1140
 
    } else {
1141
 
      codec = g_strdup_printf ("MPEG %d Audio, Layer %d",
1142
 
          mp3parse->version, mp3parse->layer);
1143
 
    }
1144
 
    taglist = gst_tag_list_new ();
1145
 
    gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1146
 
        GST_TAG_AUDIO_CODEC, codec, NULL);
1147
 
    gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse),
1148
 
        GST_BASE_PARSE_SRC_PAD (mp3parse), taglist);
1149
 
    g_free (codec);
1150
 
 
1151
 
    /* also signals the end of first-frame processing */
1152
 
    mp3parse->sent_codec_tag = TRUE;
1153
 
  }
1154
 
 
1155
 
  /* we will create a taglist (if any of the parameters has changed)
1156
 
   * to add the tags that changed */
1157
 
  taglist = NULL;
1158
 
  if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1159
 
    gboolean using_crc;
1160
 
 
1161
 
    if (!taglist) {
1162
 
      taglist = gst_tag_list_new ();
1163
 
    }
1164
 
    mp3parse->last_posted_crc = mp3parse->last_crc;
1165
 
    if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1166
 
      using_crc = TRUE;
1167
 
    } else {
1168
 
      using_crc = FALSE;
1169
 
    }
1170
 
    gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1171
 
        using_crc, NULL);
1172
 
  }
1173
 
 
1174
 
  if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1175
 
    if (!taglist) {
1176
 
      taglist = gst_tag_list_new ();
1177
 
    }
1178
 
    mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1179
 
 
1180
 
    gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1181
 
        gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1182
 
  }
1183
 
 
1184
 
  /* if the taglist exists, we need to send it */
1185
 
  if (taglist) {
1186
 
    gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse),
1187
 
        GST_BASE_PARSE_SRC_PAD (mp3parse), taglist);
1188
 
  }
1189
 
 
1190
 
  /* usual clipping applies */
1191
 
  return GST_BASE_PARSE_FLOW_CLIP;
1192
 
}