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