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