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