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