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