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>
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.
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.
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.
23 * SECTION:element-mpegaudioparse
24 * @title: mpegaudioparse
25 * @short_description: MPEG audio parser
26 * @see_also: #GstAmrParse, #GstAACParse
28 * Parses and frames mpeg1 audio streams. Provides seeking.
30 * ## Example launch line
32 * gst-launch-1.0 filesrc location=test.mp3 ! mpegaudioparse ! mpg123audiodec
33 * ! audioconvert ! audioresample ! autoaudiosink
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.
51 #include "gstaudioparserselements.h"
52 #include "gstmpegaudioparse.h"
53 #include <gst/base/gstbytereader.h>
54 #include <gst/pbutils/pbutils.h>
56 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
57 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
59 #define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
60 #define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
61 #define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
62 #define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
63 #define MPEG_AUDIO_CHANNEL_MODE_MONO 3
65 #define CRC_UNKNOWN -1
66 #define CRC_PROTECTED 0
67 #define CRC_NOT_PROTECTED 1
69 #define XING_FRAMES_FLAG 0x0001
70 #define XING_BYTES_FLAG 0x0002
71 #define XING_TOC_FLAG 0x0004
72 #define XING_VBR_SCALE_FLAG 0x0008
74 #define MIN_FRAME_SIZE 6
76 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
77 #define DEFAULT_CHECK_HTTP_SEEK FALSE
87 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_STATIC_CAPS ("audio/mpeg, "
91 "mpegversion = (int) 1, "
92 "layer = (int) [ 1, 3 ], "
93 "mpegaudioversion = (int) [ 1, 3], "
94 "rate = (int) [ 8000, 48000 ], "
95 "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
98 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
101 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
104 static void gst_mpeg_audio_parse_finalize (GObject * object);
106 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
107 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
109 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
110 static void gst_mpeg_audio_parse_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_mpeg_audio_parse_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
114 static gboolean gst_mpeg_audio_parse_src_eventfunc (GstBaseParse * parse,
118 static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
119 GstBaseParseFrame * frame, gint * skipsize);
120 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
121 GstBaseParseFrame * frame);
122 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
123 GstFormat src_format, gint64 src_value,
124 GstFormat dest_format, gint64 * dest_value);
125 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
128 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
129 mp3parse, GstBuffer * buf);
131 #define gst_mpeg_audio_parse_parent_class parent_class
132 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
133 GST_ELEMENT_REGISTER_DEFINE (mpegaudioparse, "mpegaudioparse",
134 GST_RANK_PRIMARY + 2, GST_TYPE_MPEG_AUDIO_PARSE);
136 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
137 (gst_mpeg_audio_channel_mode_get_type())
139 static const GEnumValue mpeg_audio_channel_mode[] = {
140 {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
141 {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
142 {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
143 {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
144 {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
149 gst_mpeg_audio_channel_mode_get_type (void)
151 static GType mpeg_audio_channel_mode_type = 0;
153 if (!mpeg_audio_channel_mode_type) {
154 mpeg_audio_channel_mode_type =
155 g_enum_register_static ("GstMpegAudioChannelMode",
156 mpeg_audio_channel_mode);
158 return mpeg_audio_channel_mode_type;
162 gst_mpeg_audio_channel_mode_get_nick (gint mode)
165 for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
166 if (mpeg_audio_channel_mode[i].value == mode)
167 return mpeg_audio_channel_mode[i].value_nick;
173 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
175 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
176 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
177 GObjectClass *object_class = G_OBJECT_CLASS (klass);
179 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
180 "MPEG1 audio stream parser");
182 object_class->finalize = gst_mpeg_audio_parse_finalize;
184 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
185 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
186 parse_class->handle_frame =
187 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
188 parse_class->pre_push_frame =
189 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
190 parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
191 parse_class->get_sink_caps =
192 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
194 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
195 object_class->set_property =
196 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_set_property);
197 object_class->get_property =
198 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_property);
200 g_object_class_install_property (object_class, PROP_CHECK_HTTP_SEEK,
201 g_param_spec_boolean ("http-pull-mp3dec", "enable/disable",
202 "enable/disable mp3dec http seek pull mode",
203 DEFAULT_CHECK_HTTP_SEEK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 /* T.B.D : make full mp3 index table when seek */
205 parse_class->src_event = gst_mpeg_audio_parse_src_eventfunc;
209 #define GST_TAG_CRC "has-crc"
210 #define GST_TAG_MODE "channel-mode"
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);
217 g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
219 gst_element_class_add_static_pad_template (element_class, &sink_template);
220 gst_element_class_add_static_pad_template (element_class, &src_template);
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>");
230 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
232 mp3parse->channels = -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;
239 mp3parse->hdr_bitrate = 0;
240 mp3parse->bitrate_is_constant = TRUE;
242 mp3parse->xing_flags = 0;
243 mp3parse->xing_bitrate = 0;
244 mp3parse->xing_frames = 0;
245 mp3parse->xing_total_time = 0;
246 mp3parse->xing_bytes = 0;
247 mp3parse->xing_vbr_scale = 0;
248 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
249 memset (mp3parse->xing_seek_table_inverse, 0,
250 sizeof (mp3parse->xing_seek_table_inverse));
252 mp3parse->vbri_bitrate = 0;
253 mp3parse->vbri_frames = 0;
254 mp3parse->vbri_total_time = 0;
255 mp3parse->vbri_bytes = 0;
256 mp3parse->vbri_seek_points = 0;
257 g_free (mp3parse->vbri_seek_table);
258 mp3parse->vbri_seek_table = NULL;
260 mp3parse->encoder_delay = 0;
261 mp3parse->encoder_padding = 0;
265 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
267 gst_mpeg_audio_parse_reset (mp3parse);
268 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
269 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
273 gst_mpeg_audio_parse_finalize (GObject * object)
275 G_OBJECT_CLASS (parent_class)->finalize (object);
279 gst_mpeg_audio_parse_start (GstBaseParse * parse)
281 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
283 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
284 GST_DEBUG_OBJECT (parse, "starting");
286 gst_mpeg_audio_parse_reset (mp3parse);
288 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
289 if (mp3parse->http_seek_flag) {
290 /* Don't need Accurate Seek table (in http pull mode) */
291 GST_INFO_OBJECT (parse, "Enable (1) : mp3parse->http_seek_flag");
293 GST_INFO_OBJECT (parse, "Disable (0) : mp3parse->http_seek_flag");
300 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
302 gst_mpeg_audio_parse_set_property (GObject * object, guint prop_id,
303 const GValue * value, GParamSpec * pspec)
305 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
306 GST_INFO_OBJECT (mp3parse, "set_property() START- prop_id(%d)", prop_id);
308 case PROP_CHECK_HTTP_SEEK:
309 mp3parse->http_seek_flag = g_value_get_boolean (value);
310 GST_INFO_OBJECT (mp3parse, "http_seek_flag(%d)",
311 mp3parse->http_seek_flag);
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 gst_mpeg_audio_parse_get_property (GObject * object, guint prop_id,
321 GValue * value, GParamSpec * pspec)
323 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
324 GST_INFO_OBJECT (mp3parse, "get_property() START- prop_id(%d)", prop_id);
326 case PROP_CHECK_HTTP_SEEK:
327 g_value_set_boolean (value, mp3parse->http_seek_flag);
328 GST_INFO_OBJECT (mp3parse, "http_seek_flag(%d)",
329 mp3parse->http_seek_flag);
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
339 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
341 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
343 GST_DEBUG_OBJECT (parse, "stopping");
345 gst_mpeg_audio_parse_reset (mp3parse);
350 static const guint mp3types_bitrates[2][3][16] = {
352 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
353 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
354 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
357 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
358 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
359 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
363 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
364 {22050, 24000, 16000},
369 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
370 guint * put_version, guint * put_layer, guint * put_channels,
371 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
375 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
379 if (header & (1 << 20)) {
380 lsf = (header & (1 << 19)) ? 0 : 1;
387 version = 1 + lsf + mpg25;
389 layer = 4 - ((header >> 17) & 0x3);
391 crc = (header >> 16) & 0x1;
393 bitrate = (header >> 12) & 0xF;
394 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
396 GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
397 bitrate = mp3parse->freerate;
400 samplerate = (header >> 10) & 0x3;
401 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
403 /* force 0 length if 0 bitrate */
404 padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
406 mode = (header >> 6) & 0x3;
407 channels = (mode == 3) ? 1 : 2;
411 length = 4 * ((bitrate * 12) / samplerate + padding);
414 length = (bitrate * 144) / samplerate + padding;
418 length = (bitrate * 144) / (samplerate << lsf) + padding;
422 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
424 GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
425 "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
426 layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
429 *put_version = version;
433 *put_channels = channels;
435 *put_bitrate = bitrate;
437 *put_samplerate = samplerate;
446 /* Minimum number of consecutive, valid-looking frames to consider
448 #define MIN_RESYNC_FRAMES 3
450 /* Perform extended validation to check that subsequent headers match
451 * the first header given here in important characteristics, to avoid
452 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
453 * frames to match their major characteristics.
455 * If at_eos is set to TRUE, we just check that we don't find any invalid
456 * frames in whatever data is available, rather than requiring a full
457 * MIN_RESYNC_FRAMES of data.
459 * Returns TRUE if we've seen enough data to validate or reject the frame.
460 * If TRUE is returned, then *valid contains TRUE if it validated, or false
461 * if we decided it was false sync.
462 * If FALSE is returned, then *valid contains minimum needed data.
465 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
466 guint32 header, int bpf, gboolean at_eos, gint * valid)
471 int frames_found = 1;
474 gst_buffer_map (buf, &map, GST_MAP_READ);
476 while (frames_found < MIN_RESYNC_FRAMES) {
477 /* Check if we have enough data for all these frames, plus the next
479 if (map.size < offset + 4) {
481 /* Running out of data at EOS is fine; just accept it */
491 next_header = GST_READ_UINT32_BE (map.data + offset);
492 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
493 offset, (unsigned int) header, (unsigned int) next_header, bpf);
495 /* mask the bits which are allowed to differ between frames */
496 #define HDRMASK ~((0xF << 12) /* bitrate */ | \
497 (0x1 << 9) /* padding */ | \
498 (0xf << 4) /* mode|mode extension */ | \
499 (0xf)) /* copyright|emphasis */
501 if ((next_header & HDRMASK) != (header & HDRMASK)) {
502 /* If any of the unmasked bits don't match, then it's not valid */
503 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
504 "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
505 (guint) header, (guint) header & HDRMASK, (guint) next_header,
506 (guint) next_header & HDRMASK, bpf);
509 } else if (((next_header >> 12) & 0xf) == 0xf) {
510 /* The essential parts were the same, but the bitrate held an
511 invalid value - also reject */
512 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
517 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
518 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
520 /* if no bitrate, and no freeform rate known, then fail */
521 if (G_UNLIKELY (!bpf)) {
522 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
534 gst_buffer_unmap (buf, &map);
539 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
542 GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
543 /* if it's not a valid sync */
544 if ((head & 0xffe00000) != 0xffe00000) {
545 GST_WARNING_OBJECT (mp3parse, "invalid sync");
548 /* if it's an invalid MPEG version */
549 if (((head >> 19) & 3) == 0x1) {
550 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
554 /* if it's an invalid layer */
555 if (!((head >> 17) & 3)) {
556 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
559 /* if it's an invalid bitrate */
560 if (((head >> 12) & 0xf) == 0xf) {
561 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
564 /* if it's an invalid samplerate */
565 if (((head >> 10) & 0x3) == 0x3) {
566 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
571 if ((head & 0x3) == 0x2) {
572 /* Ignore this as there are some files with emphasis 0x2 that can
573 * be played fine. See BGO #537235 */
574 GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
580 /* Determines possible freeform frame rate/size by looking for next
581 * header with valid bitrate (0 or otherwise valid) (and sufficiently
582 * matching current header).
584 * Returns TRUE if we've found such one, and *rate then contains rate
585 * (or *rate contains 0 if decided no freeframe size could be determined).
586 * If not enough data, returns FALSE.
589 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
590 guint32 header, gboolean at_eos, gint * _rate)
596 gulong samplerate, rate, layer, padding;
600 available = map->size;
605 /* pick apart header again partially */
606 if (header & (1 << 20)) {
607 lsf = (header & (1 << 19)) ? 0 : 1;
613 layer = 4 - ((header >> 17) & 0x3);
614 samplerate = (header >> 10) & 0x3;
615 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
616 padding = (header >> 9) & 0x1;
618 for (; offset < available; ++offset) {
619 /* Check if we have enough data for all these frames, plus the next
621 if (available < offset + 4) {
623 /* Running out of data; failed to determine size */
631 next_header = GST_READ_UINT32_BE (data + offset);
632 if ((next_header & 0xFFE00000) != 0xFFE00000)
635 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
636 offset, (unsigned int) header, (unsigned int) next_header);
638 if ((next_header & HDRMASK) != (header & HDRMASK)) {
639 /* If any of the unmasked bits don't match, then it's not valid */
640 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
641 "(header=%08X (%08X), header2=%08X (%08X))",
642 (guint) header, (guint) header & HDRMASK, (guint) next_header,
643 (guint) next_header & HDRMASK);
645 } else if (((next_header >> 12) & 0xf) == 0xf) {
646 /* The essential parts were the same, but the bitrate held an
647 invalid value - also reject */
648 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
655 /* almost accept as free frame */
657 rate = samplerate * (offset - 4 * padding + 4) / 48000;
659 rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
663 GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
664 if (rate < 8 || (layer == 3 && rate > 640)) {
665 GST_DEBUG_OBJECT (mp3parse, "rate invalid");
667 /* maybe some hope */
670 GST_DEBUG_OBJECT (mp3parse, "aborting");
675 *_rate = rate * 1000;
678 /* avoid indefinite searching */
680 GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
690 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
691 GstBaseParseFrame * frame, gint * skipsize)
693 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
694 GstBuffer *buf = frame->buffer;
695 GstByteReader reader;
697 gboolean lost_sync, draining, valid, caps_change;
699 guint bitrate, layer, rate, channels, version, mode, crc;
701 gboolean res = FALSE;
703 gst_buffer_map (buf, &map, GST_MAP_READ);
704 if (G_UNLIKELY (map.size < 6)) {
709 gst_byte_reader_init (&reader, map.data, map.size);
711 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
714 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
716 /* didn't find anything that looks like a sync word, skip */
718 *skipsize = map.size - 3;
722 /* possible frame header, but not at offset 0? skip bytes before sync */
728 /* make sure the values in the frame header look sane */
729 header = GST_READ_UINT32_BE (map.data);
730 if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
735 GST_LOG_OBJECT (parse, "got frame");
737 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
738 draining = GST_BASE_PARSE_DRAINING (parse);
740 if (G_UNLIKELY (lost_sync))
741 mp3parse->freerate = 0;
743 bpf = mp3_type_frame_length_from_header (mp3parse, header,
744 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
746 if (channels != mp3parse->channels || rate != mp3parse->rate ||
747 layer != mp3parse->layer || version != mp3parse->version)
752 /* maybe free format */
754 GST_LOG_OBJECT (mp3parse, "possibly free format");
755 if (lost_sync || mp3parse->freerate == 0) {
756 GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
757 if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
759 /* not enough data */
760 gst_base_parse_set_min_frame_size (parse, valid);
764 GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
765 mp3parse->freerate = valid;
769 bpf = mp3_type_frame_length_from_header (mp3parse, header,
770 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
772 /* did not come up with valid freeform length, reject after all */
778 if (!draining && (lost_sync || caps_change)) {
779 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
781 /* not enough data */
782 gst_base_parse_set_min_frame_size (parse, valid);
791 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
792 /* avoid caps jitter that we can't be sure of */
797 /* restore default minimum */
798 gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
802 /* metadata handling */
803 if (G_UNLIKELY (caps_change)) {
804 GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
805 "mpegversion", G_TYPE_INT, 1,
806 "mpegaudioversion", G_TYPE_INT, version,
807 "layer", G_TYPE_INT, layer,
808 "rate", G_TYPE_INT, rate,
809 "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
810 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
811 gst_caps_unref (caps);
813 mp3parse->rate = rate;
814 mp3parse->channels = channels;
815 mp3parse->layer = layer;
816 mp3parse->version = version;
818 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
819 if (mp3parse->layer == 1)
821 else if (mp3parse->layer == 2)
822 mp3parse->spf = 1152;
823 else if (mp3parse->version == 1) {
824 mp3parse->spf = 1152;
826 /* MPEG-2 or "2.5" */
831 * We start pushing 9 frames earlier (29 frames for MPEG2) than
832 * segment start to be able to decode the first frame we want.
833 * 9 (29) frames are the theoretical maximum of frames that contain
834 * data for the current frame (bit reservoir).
837 * Some mp3 streams have an offset in the timestamps, for which we have to
838 * push the frame *after* the end position in order for the decoder to be
839 * able to decode everything up until the segment.stop position. */
840 gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
841 (version == 1) ? 10 : 30, 2);
844 if (mp3parse->hdr_bitrate && mp3parse->hdr_bitrate != bitrate) {
845 mp3parse->bitrate_is_constant = FALSE;
847 mp3parse->hdr_bitrate = bitrate;
849 /* For first frame; check for seek tables and output a codec tag */
850 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
852 /* store some frame info for later processing */
853 mp3parse->last_crc = crc;
854 mp3parse->last_mode = mode;
857 gst_buffer_unmap (buf, &map);
859 if (res && bpf <= map.size) {
860 return gst_base_parse_finish_frame (parse, frame, bpf);
867 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
870 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
871 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
872 const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */
873 const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */
874 gint offset_xing, offset_vbri;
876 gint64 upstream_total_bytes = 0;
877 guint32 read_id_xing = 0, read_id_vbri = 0;
882 if (mp3parse->sent_codec_tag)
885 /* Check first frame for Xing info */
886 if (mp3parse->version == 1) { /* MPEG-1 file */
887 if (mp3parse->channels == 1)
891 } else { /* MPEG-2 header */
892 if (mp3parse->channels == 1)
898 /* The VBRI tag is always at offset 0x20 */
901 /* Skip the 4 bytes of the MP3 header too */
905 /* Check if we have enough data to read the Xing header */
906 gst_buffer_map (buf, &map, GST_MAP_READ);
910 if (avail >= offset_xing + 4) {
911 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
913 if (avail >= offset_vbri + 4) {
914 read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
917 /* obtain real upstream total bytes */
918 if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
919 GST_FORMAT_BYTES, &upstream_total_bytes))
920 upstream_total_bytes = 0;
922 if (read_id_xing == xing_id || read_id_xing == info_id) {
924 guint bytes_needed = offset_xing + 8;
926 GstClockTime total_time;
928 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
930 /* Move data after Xing header */
931 data += offset_xing + 4;
933 /* Read 4 base bytes of flags, big-endian */
934 xing_flags = GST_READ_UINT32_BE (data);
936 if (xing_flags & XING_FRAMES_FLAG)
938 if (xing_flags & XING_BYTES_FLAG)
940 if (xing_flags & XING_TOC_FLAG)
942 if (xing_flags & XING_VBR_SCALE_FLAG)
944 if (avail < bytes_needed) {
945 GST_DEBUG_OBJECT (mp3parse,
946 "Not enough data to read Xing header (need %d)", bytes_needed);
950 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
951 mp3parse->xing_flags = xing_flags;
953 if (xing_flags & XING_FRAMES_FLAG) {
954 mp3parse->xing_frames = GST_READ_UINT32_BE (data);
955 if (mp3parse->xing_frames == 0) {
956 GST_WARNING_OBJECT (mp3parse,
957 "Invalid number of frames in Xing header");
958 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
960 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
961 (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
967 mp3parse->xing_frames = 0;
968 mp3parse->xing_total_time = 0;
971 if (xing_flags & XING_BYTES_FLAG) {
972 mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
973 if (mp3parse->xing_bytes == 0) {
974 GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
975 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
979 mp3parse->xing_bytes = 0;
982 /* If we know the upstream size and duration, compute the
983 * total bitrate, rounded up to the nearest kbit/sec */
984 if ((total_time = mp3parse->xing_total_time) &&
985 (total_bytes = mp3parse->xing_bytes)) {
986 mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
987 8 * GST_SECOND, total_time);
988 mp3parse->xing_bitrate += 500;
989 mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
992 if (xing_flags & XING_TOC_FLAG) {
994 guchar *table = mp3parse->xing_seek_table;
999 GST_DEBUG_OBJECT (mp3parse,
1000 "Subtracting initial offset of %d bytes from Xing TOC", first);
1002 /* xing seek table: percent time -> 1/256 bytepos */
1003 for (i = 0; i < 100; i++) {
1004 new = data[i] - first;
1006 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
1007 mp3parse->xing_flags &= ~XING_TOC_FLAG;
1010 mp3parse->xing_seek_table[i] = old = new;
1013 /* build inverse table: 1/256 bytepos -> 1/100 percent time */
1014 for (i = 0; i < 256; i++) {
1015 while (percent < 99 && table[percent + 1] <= i)
1018 if (table[percent] == i) {
1019 mp3parse->xing_seek_table_inverse[i] = percent * 100;
1020 } else if (percent < 99 && table[percent]) {
1022 gint a = percent, b = percent + 1;
1026 fx = (b - a) / (fb - fa) * (i - fa) + a;
1027 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1028 } else if (percent == 99) {
1030 gint a = percent, b = 100;
1034 fx = (b - a) / (fb - fa) * (i - fa) + a;
1035 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1041 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
1042 memset (mp3parse->xing_seek_table_inverse, 0,
1043 sizeof (mp3parse->xing_seek_table_inverse));
1046 if (xing_flags & XING_VBR_SCALE_FLAG) {
1047 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
1050 mp3parse->xing_vbr_scale = 0;
1052 GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
1053 GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
1054 GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
1055 mp3parse->xing_vbr_scale);
1057 /* check for truncated file */
1058 if (upstream_total_bytes && mp3parse->xing_bytes &&
1059 mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
1060 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1061 "invalidating Xing header duration and size");
1062 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
1063 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
1066 /* Optional LAME tag? */
1067 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
1068 gchar lame_version[10] = { 0, };
1070 guint32 encoder_delay, encoder_padding;
1072 memcpy (lame_version, data, 9);
1074 tag_rev = data[0] >> 4;
1075 GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
1076 tag_rev, lame_version);
1078 /* Skip all the information we're not interested in */
1080 /* Encoder delay and end padding */
1081 encoder_delay = GST_READ_UINT24_BE (data);
1082 encoder_delay >>= 12;
1083 encoder_padding = GST_READ_UINT24_BE (data);
1084 encoder_padding &= 0x000fff;
1086 mp3parse->encoder_delay = encoder_delay;
1087 mp3parse->encoder_padding = encoder_padding;
1089 GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
1090 encoder_delay, encoder_padding);
1092 } else if (read_id_vbri == vbri_id) {
1093 gint64 total_bytes, total_frames;
1094 GstClockTime total_time;
1095 guint16 nseek_points;
1097 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1099 if (avail < offset_vbri + 26) {
1100 GST_DEBUG_OBJECT (mp3parse,
1101 "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1105 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1107 /* Move data after VBRI header */
1108 data += offset_vbri + 4;
1110 if (GST_READ_UINT16_BE (data) != 0x0001) {
1111 GST_WARNING_OBJECT (mp3parse,
1112 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1117 /* Skip encoder delay */
1123 total_bytes = GST_READ_UINT32_BE (data);
1124 if (total_bytes != 0)
1125 mp3parse->vbri_bytes = total_bytes;
1128 total_frames = GST_READ_UINT32_BE (data);
1129 if (total_frames != 0) {
1130 mp3parse->vbri_frames = total_frames;
1131 mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
1132 (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
1136 /* If we know the upstream size and duration, compute the
1137 * total bitrate, rounded up to the nearest kbit/sec */
1138 if ((total_time = mp3parse->vbri_total_time) &&
1139 (total_bytes = mp3parse->vbri_bytes)) {
1140 mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
1141 8 * GST_SECOND, total_time);
1142 mp3parse->vbri_bitrate += 500;
1143 mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
1146 nseek_points = GST_READ_UINT16_BE (data);
1149 if (nseek_points > 0) {
1150 guint scale, seek_bytes, seek_frames;
1153 mp3parse->vbri_seek_points = nseek_points;
1155 scale = GST_READ_UINT16_BE (data);
1158 seek_bytes = GST_READ_UINT16_BE (data);
1161 seek_frames = GST_READ_UINT16_BE (data);
1163 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1164 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
1168 if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
1169 GST_WARNING_OBJECT (mp3parse,
1170 "Not enough data to read VBRI seek table (need %d)",
1171 offset_vbri + 26 + nseek_points * seek_bytes);
1175 if (seek_frames * nseek_points < total_frames - seek_frames ||
1176 seek_frames * nseek_points > total_frames + seek_frames) {
1177 GST_WARNING_OBJECT (mp3parse,
1178 "VBRI seek table doesn't cover the complete file");
1183 data += offset_vbri + 26;
1185 /* VBRI seek table: frame/seek_frames -> byte */
1186 mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
1187 if (seek_bytes == 4)
1188 for (i = 0; i < nseek_points; i++) {
1189 mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
1191 } else if (seek_bytes == 3)
1192 for (i = 0; i < nseek_points; i++) {
1193 mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
1195 } else if (seek_bytes == 2)
1196 for (i = 0; i < nseek_points; i++) {
1197 mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
1199 } else /* seek_bytes == 1 */
1200 for (i = 0; i < nseek_points; i++) {
1201 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
1207 GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
1208 GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
1209 GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
1211 /* check for truncated file */
1212 if (upstream_total_bytes && mp3parse->vbri_bytes &&
1213 mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
1214 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1215 "invalidating VBRI header duration and size");
1216 mp3parse->vbri_valid = FALSE;
1218 mp3parse->vbri_valid = TRUE;
1221 GST_DEBUG_OBJECT (mp3parse,
1222 "Xing, LAME or VBRI header not found in first frame");
1225 /* set duration if tables provided a valid one */
1226 if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
1227 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1228 mp3parse->xing_total_time, 0);
1230 if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
1231 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1232 mp3parse->vbri_total_time, 0);
1235 /* tell baseclass how nicely we can seek, and a bitrate if one found */
1236 /* FIXME: fill index with seek table */
1238 seekable = GST_BASE_PARSE_SEEK_DEFAULT;
1239 if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
1240 mp3parse->xing_total_time)
1241 seekable = GST_BASE_PARSE_SEEK_TABLE;
1243 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1244 mp3parse->vbri_total_time)
1245 seekable = GST_BASE_PARSE_SEEK_TABLE;
1248 if (mp3parse->xing_bitrate)
1249 bitrate = mp3parse->xing_bitrate;
1250 else if (mp3parse->vbri_bitrate)
1251 bitrate = mp3parse->vbri_bitrate;
1255 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1258 gst_buffer_unmap (buf, &map);
1262 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1263 GstClockTime ts, gint64 * bytepos)
1266 GstClockTime total_time;
1268 /* If XING seek table exists use this for time->byte conversion */
1269 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1270 (total_bytes = mp3parse->xing_bytes) &&
1271 (total_time = mp3parse->xing_total_time)) {
1274 CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1275 gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1276 gint index = CLAMP (percent, 0, 99);
1278 fa = mp3parse->xing_seek_table[index];
1280 fb = mp3parse->xing_seek_table[index + 1];
1284 fx = fa + (fb - fa) * (percent - index);
1286 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1291 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1292 (total_time = mp3parse->vbri_total_time)) {
1294 gdouble a, b, fa, fb;
1296 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1297 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1299 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1300 mp3parse->vbri_seek_points));
1302 for (j = i; j >= 0; j--)
1303 fa += mp3parse->vbri_seek_table[j];
1305 if (i + 1 < mp3parse->vbri_seek_points) {
1306 b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1307 mp3parse->vbri_seek_points));
1308 fb = fa + mp3parse->vbri_seek_table[i + 1];
1310 b = gst_guint64_to_gdouble (total_time);
1314 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1319 /* If we have had a constant bit rate (so far), use it directly, as it
1320 * may give slightly more accurate results than the base class. */
1321 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1322 *bytepos = gst_util_uint64_scale (ts, mp3parse->hdr_bitrate,
1331 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1332 gint64 bytepos, GstClockTime * ts)
1335 GstClockTime total_time;
1337 /* If XING seek table exists use this for byte->time conversion */
1338 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1339 (total_bytes = mp3parse->xing_bytes) &&
1340 (total_time = mp3parse->xing_total_time)) {
1345 pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1346 index = CLAMP (pos, 0, 255);
1347 fa = mp3parse->xing_seek_table_inverse[index];
1349 fb = mp3parse->xing_seek_table_inverse[index + 1];
1353 fx = fa + (fb - fa) * (pos - index);
1355 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1360 if (mp3parse->vbri_seek_table &&
1361 (total_bytes = mp3parse->vbri_bytes) &&
1362 (total_time = mp3parse->vbri_total_time)) {
1365 gdouble a, b, fa, fb;
1368 sum += mp3parse->vbri_seek_table[i];
1370 } while (i + 1 < mp3parse->vbri_seek_points
1371 && sum + mp3parse->vbri_seek_table[i] < bytepos);
1374 a = gst_guint64_to_gdouble (sum);
1375 fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1376 mp3parse->vbri_seek_points));
1378 if (i + 1 < mp3parse->vbri_seek_points) {
1379 b = a + mp3parse->vbri_seek_table[i + 1];
1380 fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1381 mp3parse->vbri_seek_points));
1384 fb = gst_guint64_to_gdouble (total_time);
1387 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1392 /* If we have had a constant bit rate (so far), use it directly, as it
1393 * may give slightly more accurate results than the base class. */
1394 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1395 *ts = gst_util_uint64_scale (bytepos, 8 * GST_SECOND,
1396 mp3parse->hdr_bitrate);
1404 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1405 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1407 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1408 gboolean res = FALSE;
1410 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1412 gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1413 else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1414 res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1415 (GstClockTime *) dest_value);
1417 /* if no tables, fall back to default estimated rate based conversion */
1419 return gst_base_parse_convert_default (parse, src_format, src_value,
1420 dest_format, dest_value);
1425 static GstFlowReturn
1426 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1427 GstBaseParseFrame * frame)
1429 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1430 GstTagList *taglist = NULL;
1432 /* we will create a taglist (if any of the parameters has changed)
1433 * to add the tags that changed */
1434 if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1438 taglist = gst_tag_list_new_empty ();
1440 mp3parse->last_posted_crc = mp3parse->last_crc;
1441 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1446 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1450 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1452 taglist = gst_tag_list_new_empty ();
1454 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1456 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1457 gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1460 /* tag sending done late enough in hook to ensure pending events
1461 * have already been sent */
1462 if (taglist != NULL || !mp3parse->sent_codec_tag) {
1465 if (taglist == NULL)
1466 taglist = gst_tag_list_new_empty ();
1469 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1470 if (G_UNLIKELY (caps == NULL)) {
1471 gst_tag_list_unref (taglist);
1473 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1474 GST_INFO_OBJECT (parse, "Src pad is flushing");
1475 return GST_FLOW_FLUSHING;
1477 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1478 return GST_FLOW_NOT_NEGOTIATED;
1481 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1482 GST_TAG_AUDIO_CODEC, caps);
1483 gst_caps_unref (caps);
1485 if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1486 mp3parse->vbri_bitrate == 0) {
1487 /* We don't have a VBR bitrate, so post the available bitrate as
1488 * nominal and let baseparse calculate the real bitrate */
1489 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1490 GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1493 /* also signals the end of first-frame processing */
1494 mp3parse->sent_codec_tag = TRUE;
1497 /* if the taglist exists, we need to update it so it gets sent out */
1499 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1500 gst_tag_list_unref (taglist);
1503 /* usual clipping applies */
1504 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1510 remove_fields (GstCaps * caps)
1514 n = gst_caps_get_size (caps);
1515 for (i = 0; i < n; i++) {
1516 GstStructure *s = gst_caps_get_structure (caps, i);
1518 gst_structure_remove_field (s, "parsed");
1523 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1525 GstCaps *peercaps, *templ;
1528 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1530 GstCaps *fcopy = gst_caps_copy (filter);
1531 /* Remove the fields we convert */
1532 remove_fields (fcopy);
1533 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1534 gst_caps_unref (fcopy);
1536 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1539 /* Remove the parsed field */
1540 peercaps = gst_caps_make_writable (peercaps);
1541 remove_fields (peercaps);
1543 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1544 gst_caps_unref (peercaps);
1545 gst_caps_unref (templ);
1551 GstCaps *intersection;
1554 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1555 gst_caps_unref (res);
1562 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
1564 * gst_mpeg_audio_parse_src_eventfunc:
1565 * @parse: #GstBaseParse. #event
1567 * before baseparse handles seek event, check any mode and flag.
1569 * Returns: TRUE on success.
1572 gst_mpeg_audio_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
1574 gboolean handled = FALSE;
1575 GstMpegAudioParse *mp3parse;
1576 mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1578 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
1579 GST_EVENT_TYPE_NAME (event));
1581 switch (GST_EVENT_TYPE (event)) {
1582 case GST_EVENT_SEEK:
1584 GST_INFO_OBJECT (mp3parse, "GST_EVENT_SEEK enter");
1585 if (mp3parse->http_seek_flag) {
1586 GST_INFO_OBJECT (mp3parse,
1587 "souphttpsrc is PULL MODE (so accurate seek mode is OFF)");
1588 /* Check the declaration of this function in the baseparse */
1589 gst_base_parse_set_seek_mode (parse, 0);
1590 goto mp3_seek_null_exit;
1592 GST_INFO_OBJECT (mp3parse, "GST_EVENT_SEEK leave");
1600 /* call baseparse src_event function to handle event */
1601 handled = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);