Release 1.16.2
[platform/upstream/gst-plugins-good.git] / gst / audioparsers / gstmpegaudioparse.c
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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-1.0 filesrc location=test.mp3 ! mpegaudioparse ! mpg123audiodec
33  *  ! audioconvert ! audioresample ! autoaudiosink
34  * ]|
35  * </refsect2>
36  */
37
38 /* FIXME: we should make the base class (GstBaseParse) aware of the
39  * XING seek table somehow, so it can use it properly for things like
40  * accurate seeks. Currently it can only do a lookup via the convert function,
41  * but then doesn't know what the result represents exactly. One could either
42  * add a vfunc for index lookup, or just make mpegaudioparse populate the
43  * base class's index via the API provided.
44  */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <string.h>
50
51 #include "gstmpegaudioparse.h"
52 #include <gst/base/gstbytereader.h>
53 #include <gst/pbutils/pbutils.h>
54
55 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
56 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
57
58 #define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
59 #define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
60 #define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
61 #define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
62 #define MPEG_AUDIO_CHANNEL_MODE_MONO 3
63
64 #define CRC_UNKNOWN -1
65 #define CRC_PROTECTED 0
66 #define CRC_NOT_PROTECTED 1
67
68 #define XING_FRAMES_FLAG     0x0001
69 #define XING_BYTES_FLAG      0x0002
70 #define XING_TOC_FLAG        0x0004
71 #define XING_VBR_SCALE_FLAG  0x0008
72
73 #define MIN_FRAME_SIZE       6
74
75 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("audio/mpeg, "
79         "mpegversion = (int) 1, "
80         "layer = (int) [ 1, 3 ], "
81         "mpegaudioversion = (int) [ 1, 3], "
82         "rate = (int) [ 8000, 48000 ], "
83         "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
84     );
85
86 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
87     GST_PAD_SINK,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
90     );
91
92 static void gst_mpeg_audio_parse_finalize (GObject * object);
93
94 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
95 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
96 static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
97     GstBaseParseFrame * frame, gint * skipsize);
98 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
99     GstBaseParseFrame * frame);
100 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
101     GstFormat src_format, gint64 src_value,
102     GstFormat dest_format, gint64 * dest_value);
103 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
104     GstCaps * filter);
105
106 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
107     mp3parse, GstBuffer * buf);
108
109 #define gst_mpeg_audio_parse_parent_class parent_class
110 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
111
112 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE  \
113     (gst_mpeg_audio_channel_mode_get_type())
114
115 static const GEnumValue mpeg_audio_channel_mode[] = {
116   {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
117   {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
118   {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
119   {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
120   {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
121   {0, NULL, NULL},
122 };
123
124 static GType
125 gst_mpeg_audio_channel_mode_get_type (void)
126 {
127   static GType mpeg_audio_channel_mode_type = 0;
128
129   if (!mpeg_audio_channel_mode_type) {
130     mpeg_audio_channel_mode_type =
131         g_enum_register_static ("GstMpegAudioChannelMode",
132         mpeg_audio_channel_mode);
133   }
134   return mpeg_audio_channel_mode_type;
135 }
136
137 static const gchar *
138 gst_mpeg_audio_channel_mode_get_nick (gint mode)
139 {
140   guint i;
141   for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
142     if (mpeg_audio_channel_mode[i].value == mode)
143       return mpeg_audio_channel_mode[i].value_nick;
144   }
145   return NULL;
146 }
147
148 static void
149 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
150 {
151   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
152   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
153   GObjectClass *object_class = G_OBJECT_CLASS (klass);
154
155   GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
156       "MPEG1 audio stream parser");
157
158   object_class->finalize = gst_mpeg_audio_parse_finalize;
159
160   parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
161   parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
162   parse_class->handle_frame =
163       GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
164   parse_class->pre_push_frame =
165       GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
166   parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
167   parse_class->get_sink_caps =
168       GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
169
170   /* register tags */
171 #define GST_TAG_CRC      "has-crc"
172 #define GST_TAG_MODE     "channel-mode"
173
174   gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
175       "has crc", "Using CRC", NULL);
176   gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
177       "channel mode", "MPEG audio channel mode", NULL);
178
179   g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
180
181   gst_element_class_add_static_pad_template (element_class, &sink_template);
182   gst_element_class_add_static_pad_template (element_class, &src_template);
183
184   gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
185       "Codec/Parser/Audio",
186       "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
187       "Jan Schmidt <thaytan@mad.scientist.com>,"
188       "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
189 }
190
191 static void
192 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
193 {
194   mp3parse->channels = -1;
195   mp3parse->rate = -1;
196   mp3parse->sent_codec_tag = FALSE;
197   mp3parse->last_posted_crc = CRC_UNKNOWN;
198   mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
199   mp3parse->freerate = 0;
200
201   mp3parse->hdr_bitrate = 0;
202
203   mp3parse->xing_flags = 0;
204   mp3parse->xing_bitrate = 0;
205   mp3parse->xing_frames = 0;
206   mp3parse->xing_total_time = 0;
207   mp3parse->xing_bytes = 0;
208   mp3parse->xing_vbr_scale = 0;
209   memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
210   memset (mp3parse->xing_seek_table_inverse, 0,
211       sizeof (mp3parse->xing_seek_table_inverse));
212
213   mp3parse->vbri_bitrate = 0;
214   mp3parse->vbri_frames = 0;
215   mp3parse->vbri_total_time = 0;
216   mp3parse->vbri_bytes = 0;
217   mp3parse->vbri_seek_points = 0;
218   g_free (mp3parse->vbri_seek_table);
219   mp3parse->vbri_seek_table = NULL;
220
221   mp3parse->encoder_delay = 0;
222   mp3parse->encoder_padding = 0;
223 }
224
225 static void
226 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
227 {
228   gst_mpeg_audio_parse_reset (mp3parse);
229   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
230   GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
231 }
232
233 static void
234 gst_mpeg_audio_parse_finalize (GObject * object)
235 {
236   G_OBJECT_CLASS (parent_class)->finalize (object);
237 }
238
239 static gboolean
240 gst_mpeg_audio_parse_start (GstBaseParse * parse)
241 {
242   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
243
244   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
245   GST_DEBUG_OBJECT (parse, "starting");
246
247   gst_mpeg_audio_parse_reset (mp3parse);
248
249   return TRUE;
250 }
251
252 static gboolean
253 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
254 {
255   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
256
257   GST_DEBUG_OBJECT (parse, "stopping");
258
259   gst_mpeg_audio_parse_reset (mp3parse);
260
261   return TRUE;
262 }
263
264 static const guint mp3types_bitrates[2][3][16] = {
265   {
266         {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
267         {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
268         {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
269       },
270   {
271         {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
272         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
273         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
274       },
275 };
276
277 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
278 {22050, 24000, 16000},
279 {11025, 12000, 8000}
280 };
281
282 static inline guint
283 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
284     guint * put_version, guint * put_layer, guint * put_channels,
285     guint * put_bitrate, guint * put_samplerate, guint * put_mode,
286     guint * put_crc)
287 {
288   guint length;
289   gulong mode, samplerate, bitrate, layer, channels, padding, crc;
290   gulong version;
291   gint lsf, mpg25;
292
293   if (header & (1 << 20)) {
294     lsf = (header & (1 << 19)) ? 0 : 1;
295     mpg25 = 0;
296   } else {
297     lsf = 1;
298     mpg25 = 1;
299   }
300
301   version = 1 + lsf + mpg25;
302
303   layer = 4 - ((header >> 17) & 0x3);
304
305   crc = (header >> 16) & 0x1;
306
307   bitrate = (header >> 12) & 0xF;
308   bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
309   if (!bitrate) {
310     GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
311     bitrate = mp3parse->freerate;
312   }
313
314   samplerate = (header >> 10) & 0x3;
315   samplerate = mp3types_freqs[lsf + mpg25][samplerate];
316
317   /* force 0 length if 0 bitrate */
318   padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
319
320   mode = (header >> 6) & 0x3;
321   channels = (mode == 3) ? 1 : 2;
322
323   switch (layer) {
324     case 1:
325       length = 4 * ((bitrate * 12) / samplerate + padding);
326       break;
327     case 2:
328       length = (bitrate * 144) / samplerate + padding;
329       break;
330     default:
331     case 3:
332       length = (bitrate * 144) / (samplerate << lsf) + padding;
333       break;
334   }
335
336   GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
337       length);
338   GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
339       "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
340       layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
341
342   if (put_version)
343     *put_version = version;
344   if (put_layer)
345     *put_layer = layer;
346   if (put_channels)
347     *put_channels = channels;
348   if (put_bitrate)
349     *put_bitrate = bitrate;
350   if (put_samplerate)
351     *put_samplerate = samplerate;
352   if (put_mode)
353     *put_mode = mode;
354   if (put_crc)
355     *put_crc = crc;
356
357   return length;
358 }
359
360 /* Minimum number of consecutive, valid-looking frames to consider
361  * for resyncing */
362 #define MIN_RESYNC_FRAMES 3
363
364 /* Perform extended validation to check that subsequent headers match
365  * the first header given here in important characteristics, to avoid
366  * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
367  * frames to match their major characteristics.
368  *
369  * If at_eos is set to TRUE, we just check that we don't find any invalid
370  * frames in whatever data is available, rather than requiring a full
371  * MIN_RESYNC_FRAMES of data.
372  *
373  * Returns TRUE if we've seen enough data to validate or reject the frame.
374  * If TRUE is returned, then *valid contains TRUE if it validated, or false
375  * if we decided it was false sync.
376  * If FALSE is returned, then *valid contains minimum needed data.
377  */
378 static gboolean
379 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
380     guint32 header, int bpf, gboolean at_eos, gint * valid)
381 {
382   guint32 next_header;
383   GstMapInfo map;
384   gboolean res = TRUE;
385   int frames_found = 1;
386   int offset = bpf;
387
388   gst_buffer_map (buf, &map, GST_MAP_READ);
389
390   while (frames_found < MIN_RESYNC_FRAMES) {
391     /* Check if we have enough data for all these frames, plus the next
392        frame header. */
393     if (map.size < offset + 4) {
394       if (at_eos) {
395         /* Running out of data at EOS is fine; just accept it */
396         *valid = TRUE;
397         goto cleanup;
398       } else {
399         *valid = offset + 4;
400         res = FALSE;
401         goto cleanup;
402       }
403     }
404
405     next_header = GST_READ_UINT32_BE (map.data + offset);
406     GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
407         offset, (unsigned int) header, (unsigned int) next_header, bpf);
408
409 /* mask the bits which are allowed to differ between frames */
410 #define HDRMASK ~((0xF << 12)  /* bitrate */ | \
411                   (0x1 <<  9)  /* padding */ | \
412                   (0xf <<  4)  /* mode|mode extension */ | \
413                   (0xf))        /* copyright|emphasis */
414
415     if ((next_header & HDRMASK) != (header & HDRMASK)) {
416       /* If any of the unmasked bits don't match, then it's not valid */
417       GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
418           "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
419           (guint) header, (guint) header & HDRMASK, (guint) next_header,
420           (guint) next_header & HDRMASK, bpf);
421       *valid = FALSE;
422       goto cleanup;
423     } else if (((next_header >> 12) & 0xf) == 0xf) {
424       /* The essential parts were the same, but the bitrate held an
425          invalid value - also reject */
426       GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
427       *valid = FALSE;
428       goto cleanup;
429     }
430
431     bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
432         NULL, NULL, NULL, NULL, NULL, NULL, NULL);
433
434     /* if no bitrate, and no freeform rate known, then fail */
435     if (G_UNLIKELY (!bpf)) {
436       GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
437       *valid = FALSE;
438       goto cleanup;
439     }
440
441     offset += bpf;
442     frames_found++;
443   }
444
445   *valid = TRUE;
446
447 cleanup:
448   gst_buffer_unmap (buf, &map);
449   return res;
450 }
451
452 static gboolean
453 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
454     unsigned long head)
455 {
456   GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
457   /* if it's not a valid sync */
458   if ((head & 0xffe00000) != 0xffe00000) {
459     GST_WARNING_OBJECT (mp3parse, "invalid sync");
460     return FALSE;
461   }
462   /* if it's an invalid MPEG version */
463   if (((head >> 19) & 3) == 0x1) {
464     GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
465         (head >> 19) & 3);
466     return FALSE;
467   }
468   /* if it's an invalid layer */
469   if (!((head >> 17) & 3)) {
470     GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
471     return FALSE;
472   }
473   /* if it's an invalid bitrate */
474   if (((head >> 12) & 0xf) == 0xf) {
475     GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
476     return FALSE;
477   }
478   /* if it's an invalid samplerate */
479   if (((head >> 10) & 0x3) == 0x3) {
480     GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
481         (head >> 10) & 0x3);
482     return FALSE;
483   }
484
485   if ((head & 0x3) == 0x2) {
486     /* Ignore this as there are some files with emphasis 0x2 that can
487      * be played fine. See BGO #537235 */
488     GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
489   }
490
491   return TRUE;
492 }
493
494 /* Determines possible freeform frame rate/size by looking for next
495  * header with valid bitrate (0 or otherwise valid) (and sufficiently
496  * matching current header).
497  *
498  * Returns TRUE if we've found such one, and *rate then contains rate
499  * (or *rate contains 0 if decided no freeframe size could be determined).
500  * If not enough data, returns FALSE.
501  */
502 static gboolean
503 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
504     guint32 header, gboolean at_eos, gint * _rate)
505 {
506   guint32 next_header;
507   const guint8 *data;
508   guint available;
509   int offset = 4;
510   gulong samplerate, rate, layer, padding;
511   gboolean valid;
512   gint lsf, mpg25;
513
514   available = map->size;
515   data = map->data;
516
517   *_rate = 0;
518
519   /* pick apart header again partially */
520   if (header & (1 << 20)) {
521     lsf = (header & (1 << 19)) ? 0 : 1;
522     mpg25 = 0;
523   } else {
524     lsf = 1;
525     mpg25 = 1;
526   }
527   layer = 4 - ((header >> 17) & 0x3);
528   samplerate = (header >> 10) & 0x3;
529   samplerate = mp3types_freqs[lsf + mpg25][samplerate];
530   padding = (header >> 9) & 0x1;
531
532   for (; offset < available; ++offset) {
533     /* Check if we have enough data for all these frames, plus the next
534        frame header. */
535     if (available < offset + 4) {
536       if (at_eos) {
537         /* Running out of data; failed to determine size */
538         return TRUE;
539       } else {
540         return FALSE;
541       }
542     }
543
544     valid = FALSE;
545     next_header = GST_READ_UINT32_BE (data + offset);
546     if ((next_header & 0xFFE00000) != 0xFFE00000)
547       goto next;
548
549     GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
550         offset, (unsigned int) header, (unsigned int) next_header);
551
552     if ((next_header & HDRMASK) != (header & HDRMASK)) {
553       /* If any of the unmasked bits don't match, then it's not valid */
554       GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
555           "(header=%08X (%08X), header2=%08X (%08X))",
556           (guint) header, (guint) header & HDRMASK, (guint) next_header,
557           (guint) next_header & HDRMASK);
558       goto next;
559     } else if (((next_header >> 12) & 0xf) == 0xf) {
560       /* The essential parts were the same, but the bitrate held an
561          invalid value - also reject */
562       GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
563       goto next;
564     }
565
566     valid = TRUE;
567
568   next:
569     /* almost accept as free frame */
570     if (layer == 1) {
571       rate = samplerate * (offset - 4 * padding + 4) / 48000;
572     } else {
573       rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
574     }
575
576     if (valid) {
577       GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
578       if (rate < 8 || (layer == 3 && rate > 640)) {
579         GST_DEBUG_OBJECT (mp3parse, "rate invalid");
580         if (rate < 8) {
581           /* maybe some hope */
582           continue;
583         } else {
584           GST_DEBUG_OBJECT (mp3parse, "aborting");
585           /* give up */
586           break;
587         }
588       }
589       *_rate = rate * 1000;
590       break;
591     } else {
592       /* avoid indefinite searching */
593       if (rate > 1000) {
594         GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
595         break;
596       }
597     }
598   }
599
600   return TRUE;
601 }
602
603 static GstFlowReturn
604 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
605     GstBaseParseFrame * frame, gint * skipsize)
606 {
607   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
608   GstBuffer *buf = frame->buffer;
609   GstByteReader reader;
610   gint off, bpf = 0;
611   gboolean lost_sync, draining, valid, caps_change;
612   guint32 header;
613   guint bitrate, layer, rate, channels, version, mode, crc;
614   GstMapInfo map;
615   gboolean res = FALSE;
616
617   gst_buffer_map (buf, &map, GST_MAP_READ);
618   if (G_UNLIKELY (map.size < 6)) {
619     *skipsize = 1;
620     goto cleanup;
621   }
622
623   gst_byte_reader_init (&reader, map.data, map.size);
624
625   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
626       0, map.size);
627
628   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
629
630   /* didn't find anything that looks like a sync word, skip */
631   if (off < 0) {
632     *skipsize = map.size - 3;
633     goto cleanup;
634   }
635
636   /* possible frame header, but not at offset 0? skip bytes before sync */
637   if (off > 0) {
638     *skipsize = off;
639     goto cleanup;
640   }
641
642   /* make sure the values in the frame header look sane */
643   header = GST_READ_UINT32_BE (map.data);
644   if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
645     *skipsize = 1;
646     goto cleanup;
647   }
648
649   GST_LOG_OBJECT (parse, "got frame");
650
651   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
652   draining = GST_BASE_PARSE_DRAINING (parse);
653
654   if (G_UNLIKELY (lost_sync))
655     mp3parse->freerate = 0;
656
657   bpf = mp3_type_frame_length_from_header (mp3parse, header,
658       &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
659
660   if (channels != mp3parse->channels || rate != mp3parse->rate ||
661       layer != mp3parse->layer || version != mp3parse->version)
662     caps_change = TRUE;
663   else
664     caps_change = FALSE;
665
666   /* maybe free format */
667   if (bpf == 0) {
668     GST_LOG_OBJECT (mp3parse, "possibly free format");
669     if (lost_sync || mp3parse->freerate == 0) {
670       GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
671       if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
672               &valid)) {
673         /* not enough data */
674         gst_base_parse_set_min_frame_size (parse, valid);
675         *skipsize = 0;
676         goto cleanup;
677       } else {
678         GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
679         mp3parse->freerate = valid;
680       }
681     }
682     /* try again */
683     bpf = mp3_type_frame_length_from_header (mp3parse, header,
684         &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
685     if (!bpf) {
686       /* did not come up with valid freeform length, reject after all */
687       *skipsize = 1;
688       goto cleanup;
689     }
690   }
691
692   if (!draining && (lost_sync || caps_change)) {
693     if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
694             &valid)) {
695       /* not enough data */
696       gst_base_parse_set_min_frame_size (parse, valid);
697       *skipsize = 0;
698       goto cleanup;
699     } else {
700       if (!valid) {
701         *skipsize = off + 2;
702         goto cleanup;
703       }
704     }
705   } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
706     /* avoid caps jitter that we can't be sure of */
707     *skipsize = off + 2;
708     goto cleanup;
709   }
710
711   /* restore default minimum */
712   gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
713
714   res = TRUE;
715
716   /* metadata handling */
717   if (G_UNLIKELY (caps_change)) {
718     GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
719         "mpegversion", G_TYPE_INT, 1,
720         "mpegaudioversion", G_TYPE_INT, version,
721         "layer", G_TYPE_INT, layer,
722         "rate", G_TYPE_INT, rate,
723         "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
724     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
725     gst_caps_unref (caps);
726
727     mp3parse->rate = rate;
728     mp3parse->channels = channels;
729     mp3parse->layer = layer;
730     mp3parse->version = version;
731
732     /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
733     if (mp3parse->layer == 1)
734       mp3parse->spf = 384;
735     else if (mp3parse->layer == 2)
736       mp3parse->spf = 1152;
737     else if (mp3parse->version == 1) {
738       mp3parse->spf = 1152;
739     } else {
740       /* MPEG-2 or "2.5" */
741       mp3parse->spf = 576;
742     }
743
744     /* lead_in:
745      * We start pushing 9 frames earlier (29 frames for MPEG2) than
746      * segment start to be able to decode the first frame we want.
747      * 9 (29) frames are the theoretical maximum of frames that contain
748      * data for the current frame (bit reservoir).
749      *
750      * lead_out:
751      * Some mp3 streams have an offset in the timestamps, for which we have to
752      * push the frame *after* the end position in order for the decoder to be
753      * able to decode everything up until the segment.stop position. */
754     gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
755         (version == 1) ? 10 : 30, 2);
756   }
757
758   mp3parse->hdr_bitrate = bitrate;
759
760   /* For first frame; check for seek tables and output a codec tag */
761   gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
762
763   /* store some frame info for later processing */
764   mp3parse->last_crc = crc;
765   mp3parse->last_mode = mode;
766
767 cleanup:
768   gst_buffer_unmap (buf, &map);
769
770   if (res && bpf <= map.size) {
771     return gst_base_parse_finish_frame (parse, frame, bpf);
772   }
773
774   return GST_FLOW_OK;
775 }
776
777 static void
778 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
779     GstBuffer * buf)
780 {
781   const guint32 xing_id = 0x58696e67;   /* 'Xing' in hex */
782   const guint32 info_id = 0x496e666f;   /* 'Info' in hex - found in LAME CBR files */
783   const guint32 vbri_id = 0x56425249;   /* 'VBRI' in hex */
784   const guint32 lame_id = 0x4c414d45;   /* 'LAME' in hex */
785   gint offset_xing, offset_vbri;
786   guint64 avail;
787   gint64 upstream_total_bytes = 0;
788   guint32 read_id_xing = 0, read_id_vbri = 0;
789   GstMapInfo map;
790   guint8 *data;
791   guint bitrate;
792
793   if (mp3parse->sent_codec_tag)
794     return;
795
796   /* Check first frame for Xing info */
797   if (mp3parse->version == 1) { /* MPEG-1 file */
798     if (mp3parse->channels == 1)
799       offset_xing = 0x11;
800     else
801       offset_xing = 0x20;
802   } else {                      /* MPEG-2 header */
803     if (mp3parse->channels == 1)
804       offset_xing = 0x09;
805     else
806       offset_xing = 0x11;
807   }
808
809   /* The VBRI tag is always at offset 0x20 */
810   offset_vbri = 0x20;
811
812   /* Skip the 4 bytes of the MP3 header too */
813   offset_xing += 4;
814   offset_vbri += 4;
815
816   /* Check if we have enough data to read the Xing header */
817   gst_buffer_map (buf, &map, GST_MAP_READ);
818   data = map.data;
819   avail = map.size;
820
821   if (avail >= offset_xing + 4) {
822     read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
823   }
824   if (avail >= offset_vbri + 4) {
825     read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
826   }
827
828   /* obtain real upstream total bytes */
829   if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
830           GST_FORMAT_BYTES, &upstream_total_bytes))
831     upstream_total_bytes = 0;
832
833   if (read_id_xing == xing_id || read_id_xing == info_id) {
834     guint32 xing_flags;
835     guint bytes_needed = offset_xing + 8;
836     gint64 total_bytes;
837     GstClockTime total_time;
838
839     GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
840
841     /* Move data after Xing header */
842     data += offset_xing + 4;
843
844     /* Read 4 base bytes of flags, big-endian */
845     xing_flags = GST_READ_UINT32_BE (data);
846     data += 4;
847     if (xing_flags & XING_FRAMES_FLAG)
848       bytes_needed += 4;
849     if (xing_flags & XING_BYTES_FLAG)
850       bytes_needed += 4;
851     if (xing_flags & XING_TOC_FLAG)
852       bytes_needed += 100;
853     if (xing_flags & XING_VBR_SCALE_FLAG)
854       bytes_needed += 4;
855     if (avail < bytes_needed) {
856       GST_DEBUG_OBJECT (mp3parse,
857           "Not enough data to read Xing header (need %d)", bytes_needed);
858       goto cleanup;
859     }
860
861     GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
862     mp3parse->xing_flags = xing_flags;
863
864     if (xing_flags & XING_FRAMES_FLAG) {
865       mp3parse->xing_frames = GST_READ_UINT32_BE (data);
866       if (mp3parse->xing_frames == 0) {
867         GST_WARNING_OBJECT (mp3parse,
868             "Invalid number of frames in Xing header");
869         mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
870       } else {
871         mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
872             (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
873             mp3parse->rate);
874       }
875
876       data += 4;
877     } else {
878       mp3parse->xing_frames = 0;
879       mp3parse->xing_total_time = 0;
880     }
881
882     if (xing_flags & XING_BYTES_FLAG) {
883       mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
884       if (mp3parse->xing_bytes == 0) {
885         GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
886         mp3parse->xing_flags &= ~XING_BYTES_FLAG;
887       }
888       data += 4;
889     } else {
890       mp3parse->xing_bytes = 0;
891     }
892
893     /* If we know the upstream size and duration, compute the
894      * total bitrate, rounded up to the nearest kbit/sec */
895     if ((total_time = mp3parse->xing_total_time) &&
896         (total_bytes = mp3parse->xing_bytes)) {
897       mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
898           8 * GST_SECOND, total_time);
899       mp3parse->xing_bitrate += 500;
900       mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
901     }
902
903     if (xing_flags & XING_TOC_FLAG) {
904       int i, percent = 0;
905       guchar *table = mp3parse->xing_seek_table;
906       guchar old = 0, new;
907       guint first;
908
909       first = data[0];
910       GST_DEBUG_OBJECT (mp3parse,
911           "Subtracting initial offset of %d bytes from Xing TOC", first);
912
913       /* xing seek table: percent time -> 1/256 bytepos */
914       for (i = 0; i < 100; i++) {
915         new = data[i] - first;
916         if (old > new) {
917           GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
918           mp3parse->xing_flags &= ~XING_TOC_FLAG;
919           goto skip_toc;
920         }
921         mp3parse->xing_seek_table[i] = old = new;
922       }
923
924       /* build inverse table: 1/256 bytepos -> 1/100 percent time */
925       for (i = 0; i < 256; i++) {
926         while (percent < 99 && table[percent + 1] <= i)
927           percent++;
928
929         if (table[percent] == i) {
930           mp3parse->xing_seek_table_inverse[i] = percent * 100;
931         } else if (percent < 99 && table[percent]) {
932           gdouble fa, fb, fx;
933           gint a = percent, b = percent + 1;
934
935           fa = table[a];
936           fb = table[b];
937           fx = (b - a) / (fb - fa) * (i - fa) + a;
938           mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
939         } else if (percent == 99) {
940           gdouble fa, fb, fx;
941           gint a = percent, b = 100;
942
943           fa = table[a];
944           fb = 256.0;
945           fx = (b - a) / (fb - fa) * (i - fa) + a;
946           mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
947         }
948       }
949     skip_toc:
950       data += 100;
951     } else {
952       memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
953       memset (mp3parse->xing_seek_table_inverse, 0,
954           sizeof (mp3parse->xing_seek_table_inverse));
955     }
956
957     if (xing_flags & XING_VBR_SCALE_FLAG) {
958       mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
959       data += 4;
960     } else
961       mp3parse->xing_vbr_scale = 0;
962
963     GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
964         GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
965         GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
966         mp3parse->xing_vbr_scale);
967
968     /* check for truncated file */
969     if (upstream_total_bytes && mp3parse->xing_bytes &&
970         mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
971       GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
972           "invalidating Xing header duration and size");
973       mp3parse->xing_flags &= ~XING_BYTES_FLAG;
974       mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
975     }
976
977     /* Optional LAME tag? */
978     if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
979       gchar lame_version[10] = { 0, };
980       guint tag_rev;
981       guint32 encoder_delay, encoder_padding;
982
983       memcpy (lame_version, data, 9);
984       data += 9;
985       tag_rev = data[0] >> 4;
986       GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
987           tag_rev, lame_version);
988
989       /* Skip all the information we're not interested in */
990       data += 12;
991       /* Encoder delay and end padding */
992       encoder_delay = GST_READ_UINT24_BE (data);
993       encoder_delay >>= 12;
994       encoder_padding = GST_READ_UINT24_BE (data);
995       encoder_padding &= 0x000fff;
996
997       mp3parse->encoder_delay = encoder_delay;
998       mp3parse->encoder_padding = encoder_padding;
999
1000       GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
1001           encoder_delay, encoder_padding);
1002     }
1003   } else if (read_id_vbri == vbri_id) {
1004     gint64 total_bytes, total_frames;
1005     GstClockTime total_time;
1006     guint16 nseek_points;
1007
1008     GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1009
1010     if (avail < offset_vbri + 26) {
1011       GST_DEBUG_OBJECT (mp3parse,
1012           "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1013       goto cleanup;
1014     }
1015
1016     GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1017
1018     /* Move data after VBRI header */
1019     data += offset_vbri + 4;
1020
1021     if (GST_READ_UINT16_BE (data) != 0x0001) {
1022       GST_WARNING_OBJECT (mp3parse,
1023           "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1024       goto cleanup;
1025     }
1026     data += 2;
1027
1028     /* Skip encoder delay */
1029     data += 2;
1030
1031     /* Skip quality */
1032     data += 2;
1033
1034     total_bytes = GST_READ_UINT32_BE (data);
1035     if (total_bytes != 0)
1036       mp3parse->vbri_bytes = total_bytes;
1037     data += 4;
1038
1039     total_frames = GST_READ_UINT32_BE (data);
1040     if (total_frames != 0) {
1041       mp3parse->vbri_frames = total_frames;
1042       mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
1043           (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
1044     }
1045     data += 4;
1046
1047     /* If we know the upstream size and duration, compute the
1048      * total bitrate, rounded up to the nearest kbit/sec */
1049     if ((total_time = mp3parse->vbri_total_time) &&
1050         (total_bytes = mp3parse->vbri_bytes)) {
1051       mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
1052           8 * GST_SECOND, total_time);
1053       mp3parse->vbri_bitrate += 500;
1054       mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
1055     }
1056
1057     nseek_points = GST_READ_UINT16_BE (data);
1058     data += 2;
1059
1060     if (nseek_points > 0) {
1061       guint scale, seek_bytes, seek_frames;
1062       gint i;
1063
1064       mp3parse->vbri_seek_points = nseek_points;
1065
1066       scale = GST_READ_UINT16_BE (data);
1067       data += 2;
1068
1069       seek_bytes = GST_READ_UINT16_BE (data);
1070       data += 2;
1071
1072       seek_frames = GST_READ_UINT16_BE (data);
1073
1074       if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1075         GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
1076         goto out_vbri;
1077       }
1078
1079       if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
1080         GST_WARNING_OBJECT (mp3parse,
1081             "Not enough data to read VBRI seek table (need %d)",
1082             offset_vbri + 26 + nseek_points * seek_bytes);
1083         goto out_vbri;
1084       }
1085
1086       if (seek_frames * nseek_points < total_frames - seek_frames ||
1087           seek_frames * nseek_points > total_frames + seek_frames) {
1088         GST_WARNING_OBJECT (mp3parse,
1089             "VBRI seek table doesn't cover the complete file");
1090         goto out_vbri;
1091       }
1092
1093       data = map.data;
1094       data += offset_vbri + 26;
1095
1096       /* VBRI seek table: frame/seek_frames -> byte */
1097       mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
1098       if (seek_bytes == 4)
1099         for (i = 0; i < nseek_points; i++) {
1100           mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
1101           data += 4;
1102       } else if (seek_bytes == 3)
1103         for (i = 0; i < nseek_points; i++) {
1104           mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
1105           data += 3;
1106       } else if (seek_bytes == 2)
1107         for (i = 0; i < nseek_points; i++) {
1108           mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
1109           data += 2;
1110       } else                    /* seek_bytes == 1 */
1111         for (i = 0; i < nseek_points; i++) {
1112           mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
1113           data += 1;
1114         }
1115     }
1116   out_vbri:
1117
1118     GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
1119         GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
1120         GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
1121
1122     /* check for truncated file */
1123     if (upstream_total_bytes && mp3parse->vbri_bytes &&
1124         mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
1125       GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1126           "invalidating VBRI header duration and size");
1127       mp3parse->vbri_valid = FALSE;
1128     } else {
1129       mp3parse->vbri_valid = TRUE;
1130     }
1131   } else {
1132     GST_DEBUG_OBJECT (mp3parse,
1133         "Xing, LAME or VBRI header not found in first frame");
1134   }
1135
1136   /* set duration if tables provided a valid one */
1137   if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
1138     gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1139         mp3parse->xing_total_time, 0);
1140   }
1141   if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
1142     gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1143         mp3parse->vbri_total_time, 0);
1144   }
1145
1146   /* tell baseclass how nicely we can seek, and a bitrate if one found */
1147   /* FIXME: fill index with seek table */
1148 #if 0
1149   seekable = GST_BASE_PARSE_SEEK_DEFAULT;
1150   if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
1151       mp3parse->xing_total_time)
1152     seekable = GST_BASE_PARSE_SEEK_TABLE;
1153
1154   if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1155       mp3parse->vbri_total_time)
1156     seekable = GST_BASE_PARSE_SEEK_TABLE;
1157 #endif
1158
1159   if (mp3parse->xing_bitrate)
1160     bitrate = mp3parse->xing_bitrate;
1161   else if (mp3parse->vbri_bitrate)
1162     bitrate = mp3parse->vbri_bitrate;
1163   else
1164     bitrate = 0;
1165
1166   gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1167
1168 cleanup:
1169   gst_buffer_unmap (buf, &map);
1170 }
1171
1172 static gboolean
1173 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1174     GstClockTime ts, gint64 * bytepos)
1175 {
1176   gint64 total_bytes;
1177   GstClockTime total_time;
1178
1179   /* If XING seek table exists use this for time->byte conversion */
1180   if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1181       (total_bytes = mp3parse->xing_bytes) &&
1182       (total_time = mp3parse->xing_total_time)) {
1183     gdouble fa, fb, fx;
1184     gdouble percent =
1185         CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1186         gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1187     gint index = CLAMP (percent, 0, 99);
1188
1189     fa = mp3parse->xing_seek_table[index];
1190     if (index < 99)
1191       fb = mp3parse->xing_seek_table[index + 1];
1192     else
1193       fb = 256.0;
1194
1195     fx = fa + (fb - fa) * (percent - index);
1196
1197     *bytepos = (1.0 / 256.0) * fx * total_bytes;
1198
1199     return TRUE;
1200   }
1201
1202   if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1203       (total_time = mp3parse->vbri_total_time)) {
1204     gint i, j;
1205     gdouble a, b, fa, fb;
1206
1207     i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1208     i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1209
1210     a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1211             mp3parse->vbri_seek_points));
1212     fa = 0.0;
1213     for (j = i; j >= 0; j--)
1214       fa += mp3parse->vbri_seek_table[j];
1215
1216     if (i + 1 < mp3parse->vbri_seek_points) {
1217       b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1218               mp3parse->vbri_seek_points));
1219       fb = fa + mp3parse->vbri_seek_table[i + 1];
1220     } else {
1221       b = gst_guint64_to_gdouble (total_time);
1222       fb = total_bytes;
1223     }
1224
1225     *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1226
1227     return TRUE;
1228   }
1229
1230   return FALSE;
1231 }
1232
1233 static gboolean
1234 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1235     gint64 bytepos, GstClockTime * ts)
1236 {
1237   gint64 total_bytes;
1238   GstClockTime total_time;
1239
1240   /* If XING seek table exists use this for byte->time conversion */
1241   if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1242       (total_bytes = mp3parse->xing_bytes) &&
1243       (total_time = mp3parse->xing_total_time)) {
1244     gdouble fa, fb, fx;
1245     gdouble pos;
1246     gint index;
1247
1248     pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1249     index = CLAMP (pos, 0, 255);
1250     fa = mp3parse->xing_seek_table_inverse[index];
1251     if (index < 255)
1252       fb = mp3parse->xing_seek_table_inverse[index + 1];
1253     else
1254       fb = 10000.0;
1255
1256     fx = fa + (fb - fa) * (pos - index);
1257
1258     *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1259
1260     return TRUE;
1261   }
1262
1263   if (mp3parse->vbri_seek_table &&
1264       (total_bytes = mp3parse->vbri_bytes) &&
1265       (total_time = mp3parse->vbri_total_time)) {
1266     gint i = 0;
1267     guint64 sum = 0;
1268     gdouble a, b, fa, fb;
1269
1270     do {
1271       sum += mp3parse->vbri_seek_table[i];
1272       i++;
1273     } while (i + 1 < mp3parse->vbri_seek_points
1274         && sum + mp3parse->vbri_seek_table[i] < bytepos);
1275     i--;
1276
1277     a = gst_guint64_to_gdouble (sum);
1278     fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1279             mp3parse->vbri_seek_points));
1280
1281     if (i + 1 < mp3parse->vbri_seek_points) {
1282       b = a + mp3parse->vbri_seek_table[i + 1];
1283       fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1284               mp3parse->vbri_seek_points));
1285     } else {
1286       b = total_bytes;
1287       fb = gst_guint64_to_gdouble (total_time);
1288     }
1289
1290     *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1291
1292     return TRUE;
1293   }
1294
1295   return FALSE;
1296 }
1297
1298 static gboolean
1299 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1300     gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1301 {
1302   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1303   gboolean res = FALSE;
1304
1305   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1306     res =
1307         gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1308   else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1309     res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1310         (GstClockTime *) dest_value);
1311
1312   /* if no tables, fall back to default estimated rate based conversion */
1313   if (!res)
1314     return gst_base_parse_convert_default (parse, src_format, src_value,
1315         dest_format, dest_value);
1316
1317   return res;
1318 }
1319
1320 static GstFlowReturn
1321 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1322     GstBaseParseFrame * frame)
1323 {
1324   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1325   GstTagList *taglist = NULL;
1326
1327   /* we will create a taglist (if any of the parameters has changed)
1328    * to add the tags that changed */
1329   if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1330     gboolean using_crc;
1331
1332     if (!taglist)
1333       taglist = gst_tag_list_new_empty ();
1334
1335     mp3parse->last_posted_crc = mp3parse->last_crc;
1336     if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1337       using_crc = TRUE;
1338     } else {
1339       using_crc = FALSE;
1340     }
1341     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1342         using_crc, NULL);
1343   }
1344
1345   if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1346     if (!taglist)
1347       taglist = gst_tag_list_new_empty ();
1348
1349     mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1350
1351     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1352         gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1353   }
1354
1355   /* tag sending done late enough in hook to ensure pending events
1356    * have already been sent */
1357   if (taglist != NULL || !mp3parse->sent_codec_tag) {
1358     GstCaps *caps;
1359
1360     if (taglist == NULL)
1361       taglist = gst_tag_list_new_empty ();
1362
1363     /* codec tag */
1364     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1365     if (G_UNLIKELY (caps == NULL)) {
1366       gst_tag_list_unref (taglist);
1367
1368       if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1369         GST_INFO_OBJECT (parse, "Src pad is flushing");
1370         return GST_FLOW_FLUSHING;
1371       } else {
1372         GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1373         return GST_FLOW_NOT_NEGOTIATED;
1374       }
1375     }
1376     gst_pb_utils_add_codec_description_to_tag_list (taglist,
1377         GST_TAG_AUDIO_CODEC, caps);
1378     gst_caps_unref (caps);
1379
1380     if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1381         mp3parse->vbri_bitrate == 0) {
1382       /* We don't have a VBR bitrate, so post the available bitrate as
1383        * nominal and let baseparse calculate the real bitrate */
1384       gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1385           GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1386     }
1387
1388     /* also signals the end of first-frame processing */
1389     mp3parse->sent_codec_tag = TRUE;
1390   }
1391
1392   /* if the taglist exists, we need to update it so it gets sent out */
1393   if (taglist) {
1394     gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1395     gst_tag_list_unref (taglist);
1396   }
1397
1398   /* usual clipping applies */
1399   frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1400
1401   return GST_FLOW_OK;
1402 }
1403
1404 static void
1405 remove_fields (GstCaps * caps)
1406 {
1407   guint i, n;
1408
1409   n = gst_caps_get_size (caps);
1410   for (i = 0; i < n; i++) {
1411     GstStructure *s = gst_caps_get_structure (caps, i);
1412
1413     gst_structure_remove_field (s, "parsed");
1414   }
1415 }
1416
1417 static GstCaps *
1418 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1419 {
1420   GstCaps *peercaps, *templ;
1421   GstCaps *res;
1422
1423   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1424   if (filter) {
1425     GstCaps *fcopy = gst_caps_copy (filter);
1426     /* Remove the fields we convert */
1427     remove_fields (fcopy);
1428     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1429     gst_caps_unref (fcopy);
1430   } else
1431     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1432
1433   if (peercaps) {
1434     /* Remove the parsed field */
1435     peercaps = gst_caps_make_writable (peercaps);
1436     remove_fields (peercaps);
1437
1438     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1439     gst_caps_unref (peercaps);
1440     gst_caps_unref (templ);
1441   } else {
1442     res = templ;
1443   }
1444
1445   if (filter) {
1446     GstCaps *intersection;
1447
1448     intersection =
1449         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1450     gst_caps_unref (res);
1451     res = intersection;
1452   }
1453
1454   return res;
1455 }