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