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