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 * @short_description: MPEG audio parser
25 * @see_also: #GstAmrParse, #GstAACParse
27 * Parses and frames mpeg1 audio streams. Provides seeking.
30 * <title>Example launch line</title>
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 "gstmpegaudioparse.h"
52 #include <gst/base/gstbytereader.h>
53 #include <gst/pbutils/pbutils.h>
55 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
56 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
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
64 #define CRC_UNKNOWN -1
65 #define CRC_PROTECTED 0
66 #define CRC_NOT_PROTECTED 1
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
73 #define MIN_FRAME_SIZE 6
75 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
76 #define DEFAULT_CHECK_HTTP_SEEK FALSE
86 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
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")
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
103 static void gst_mpeg_audio_parse_finalize (GObject * object);
105 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
106 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
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,
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,
127 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
128 mp3parse, GstBuffer * buf);
130 #define gst_mpeg_audio_parse_parent_class parent_class
131 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
133 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
134 (gst_mpeg_audio_channel_mode_get_type())
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"},
146 gst_mpeg_audio_channel_mode_get_type (void)
148 static GType mpeg_audio_channel_mode_type = 0;
150 if (!mpeg_audio_channel_mode_type) {
151 mpeg_audio_channel_mode_type =
152 g_enum_register_static ("GstMpegAudioChannelMode",
153 mpeg_audio_channel_mode);
155 return mpeg_audio_channel_mode_type;
159 gst_mpeg_audio_channel_mode_get_nick (gint mode)
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;
170 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
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);
176 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
177 "MPEG1 audio stream parser");
179 object_class->finalize = gst_mpeg_audio_parse_finalize;
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);
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);
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;
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;
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));
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;
259 mp3parse->encoder_delay = 0;
260 mp3parse->encoder_padding = 0;
264 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
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));
272 gst_mpeg_audio_parse_finalize (GObject * object)
274 G_OBJECT_CLASS (parent_class)->finalize (object);
278 gst_mpeg_audio_parse_start (GstBaseParse * parse)
280 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
282 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
283 GST_DEBUG_OBJECT (parse, "starting");
285 gst_mpeg_audio_parse_reset (mp3parse);
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");
292 GST_INFO_OBJECT (parse, "Disable (0) : mp3parse->http_seek_flag");
299 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
301 gst_mpeg_audio_parse_set_property (GObject * object, guint prop_id,
302 const GValue * value, GParamSpec * pspec)
304 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
305 GST_INFO_OBJECT (mp3parse, "set_property() START- prop_id(%d)", 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);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 gst_mpeg_audio_parse_get_property (GObject * object, guint prop_id,
320 GValue * value, GParamSpec * pspec)
322 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
323 GST_INFO_OBJECT (mp3parse, "get_property() START- prop_id(%d)", 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);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
340 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
342 GST_DEBUG_OBJECT (parse, "stopping");
344 gst_mpeg_audio_parse_reset (mp3parse);
349 static const guint mp3types_bitrates[2][3][16] = {
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,}
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,}
362 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
363 {22050, 24000, 16000},
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,
374 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
378 if (header & (1 << 20)) {
379 lsf = (header & (1 << 19)) ? 0 : 1;
386 version = 1 + lsf + mpg25;
388 layer = 4 - ((header >> 17) & 0x3);
390 crc = (header >> 16) & 0x1;
392 bitrate = (header >> 12) & 0xF;
393 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
395 GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
396 bitrate = mp3parse->freerate;
399 samplerate = (header >> 10) & 0x3;
400 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
402 /* force 0 length if 0 bitrate */
403 padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
405 mode = (header >> 6) & 0x3;
406 channels = (mode == 3) ? 1 : 2;
410 length = 4 * ((bitrate * 12) / samplerate + padding);
413 length = (bitrate * 144) / samplerate + padding;
417 length = (bitrate * 144) / (samplerate << lsf) + padding;
421 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
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));
428 *put_version = version;
432 *put_channels = channels;
434 *put_bitrate = bitrate;
436 *put_samplerate = samplerate;
445 /* Minimum number of consecutive, valid-looking frames to consider
447 #define MIN_RESYNC_FRAMES 3
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.
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.
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.
464 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
465 guint32 header, int bpf, gboolean at_eos, gint * valid)
470 int frames_found = 1;
473 gst_buffer_map (buf, &map, GST_MAP_READ);
475 while (frames_found < MIN_RESYNC_FRAMES) {
476 /* Check if we have enough data for all these frames, plus the next
478 if (map.size < offset + 4) {
480 /* Running out of data at EOS is fine; just accept it */
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);
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 */
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);
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)");
516 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
517 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
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)");
533 gst_buffer_unmap (buf, &map);
538 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
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");
547 /* if it's an invalid MPEG version */
548 if (((head >> 19) & 3) == 0x1) {
549 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
553 /* if it's an invalid layer */
554 if (!((head >> 17) & 3)) {
555 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
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);
563 /* if it's an invalid samplerate */
564 if (((head >> 10) & 0x3) == 0x3) {
565 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
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);
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).
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.
588 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
589 guint32 header, gboolean at_eos, gint * _rate)
595 gulong samplerate, rate, layer, padding;
599 available = map->size;
604 /* pick apart header again partially */
605 if (header & (1 << 20)) {
606 lsf = (header & (1 << 19)) ? 0 : 1;
612 layer = 4 - ((header >> 17) & 0x3);
613 samplerate = (header >> 10) & 0x3;
614 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
615 padding = (header >> 9) & 0x1;
617 for (; offset < available; ++offset) {
618 /* Check if we have enough data for all these frames, plus the next
620 if (available < offset + 4) {
622 /* Running out of data; failed to determine size */
630 next_header = GST_READ_UINT32_BE (data + offset);
631 if ((next_header & 0xFFE00000) != 0xFFE00000)
634 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
635 offset, (unsigned int) header, (unsigned int) next_header);
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);
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)");
654 /* almost accept as free frame */
656 rate = samplerate * (offset - 4 * padding + 4) / 48000;
658 rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
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");
666 /* maybe some hope */
669 GST_DEBUG_OBJECT (mp3parse, "aborting");
674 *_rate = rate * 1000;
677 /* avoid indefinite searching */
679 GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
689 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
690 GstBaseParseFrame * frame, gint * skipsize)
692 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
693 GstBuffer *buf = frame->buffer;
694 GstByteReader reader;
696 gboolean lost_sync, draining, valid, caps_change;
698 guint bitrate, layer, rate, channels, version, mode, crc;
700 gboolean res = FALSE;
702 gst_buffer_map (buf, &map, GST_MAP_READ);
703 if (G_UNLIKELY (map.size < 6)) {
708 gst_byte_reader_init (&reader, map.data, map.size);
710 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
713 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
715 /* didn't find anything that looks like a sync word, skip */
717 *skipsize = map.size - 3;
721 /* possible frame header, but not at offset 0? skip bytes before sync */
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)) {
734 GST_LOG_OBJECT (parse, "got frame");
736 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
737 draining = GST_BASE_PARSE_DRAINING (parse);
739 if (G_UNLIKELY (lost_sync))
740 mp3parse->freerate = 0;
742 bpf = mp3_type_frame_length_from_header (mp3parse, header,
743 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
745 if (channels != mp3parse->channels || rate != mp3parse->rate ||
746 layer != mp3parse->layer || version != mp3parse->version)
751 /* maybe free format */
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,
758 /* not enough data */
759 gst_base_parse_set_min_frame_size (parse, valid);
763 GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
764 mp3parse->freerate = valid;
768 bpf = mp3_type_frame_length_from_header (mp3parse, header,
769 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
771 /* did not come up with valid freeform length, reject after all */
777 if (!draining && (lost_sync || caps_change)) {
778 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
780 /* not enough data */
781 gst_base_parse_set_min_frame_size (parse, valid);
790 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
791 /* avoid caps jitter that we can't be sure of */
796 /* restore default minimum */
797 gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
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);
812 mp3parse->rate = rate;
813 mp3parse->channels = channels;
814 mp3parse->layer = layer;
815 mp3parse->version = version;
817 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
818 if (mp3parse->layer == 1)
820 else if (mp3parse->layer == 2)
821 mp3parse->spf = 1152;
822 else if (mp3parse->version == 1) {
823 mp3parse->spf = 1152;
825 /* MPEG-2 or "2.5" */
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).
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);
843 mp3parse->hdr_bitrate = bitrate;
845 /* For first frame; check for seek tables and output a codec tag */
846 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
848 /* store some frame info for later processing */
849 mp3parse->last_crc = crc;
850 mp3parse->last_mode = mode;
853 gst_buffer_unmap (buf, &map);
855 if (res && bpf <= map.size) {
856 return gst_base_parse_finish_frame (parse, frame, bpf);
863 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
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;
872 gint64 upstream_total_bytes = 0;
873 guint32 read_id_xing = 0, read_id_vbri = 0;
878 if (mp3parse->sent_codec_tag)
881 /* Check first frame for Xing info */
882 if (mp3parse->version == 1) { /* MPEG-1 file */
883 if (mp3parse->channels == 1)
887 } else { /* MPEG-2 header */
888 if (mp3parse->channels == 1)
894 /* The VBRI tag is always at offset 0x20 */
897 /* Skip the 4 bytes of the MP3 header too */
901 /* Check if we have enough data to read the Xing header */
902 gst_buffer_map (buf, &map, GST_MAP_READ);
906 if (avail >= offset_xing + 4) {
907 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
909 if (avail >= offset_vbri + 4) {
910 read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
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;
918 if (read_id_xing == xing_id || read_id_xing == info_id) {
920 guint bytes_needed = offset_xing + 8;
922 GstClockTime total_time;
924 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
926 /* Move data after Xing header */
927 data += offset_xing + 4;
929 /* Read 4 base bytes of flags, big-endian */
930 xing_flags = GST_READ_UINT32_BE (data);
932 if (xing_flags & XING_FRAMES_FLAG)
934 if (xing_flags & XING_BYTES_FLAG)
936 if (xing_flags & XING_TOC_FLAG)
938 if (xing_flags & XING_VBR_SCALE_FLAG)
940 if (avail < bytes_needed) {
941 GST_DEBUG_OBJECT (mp3parse,
942 "Not enough data to read Xing header (need %d)", bytes_needed);
946 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
947 mp3parse->xing_flags = xing_flags;
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;
956 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
957 (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
963 mp3parse->xing_frames = 0;
964 mp3parse->xing_total_time = 0;
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;
975 mp3parse->xing_bytes = 0;
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;
988 if (xing_flags & XING_TOC_FLAG) {
990 guchar *table = mp3parse->xing_seek_table;
995 GST_DEBUG_OBJECT (mp3parse,
996 "Subtracting initial offset of %d bytes from Xing TOC", first);
998 /* xing seek table: percent time -> 1/256 bytepos */
999 for (i = 0; i < 100; i++) {
1000 new = data[i] - first;
1002 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
1003 mp3parse->xing_flags &= ~XING_TOC_FLAG;
1006 mp3parse->xing_seek_table[i] = old = new;
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)
1014 if (table[percent] == i) {
1015 mp3parse->xing_seek_table_inverse[i] = percent * 100;
1016 } else if (percent < 99 && table[percent]) {
1018 gint a = percent, b = percent + 1;
1022 fx = (b - a) / (fb - fa) * (i - fa) + a;
1023 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1024 } else if (percent == 99) {
1026 gint a = percent, b = 100;
1030 fx = (b - a) / (fb - fa) * (i - fa) + a;
1031 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
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));
1042 if (xing_flags & XING_VBR_SCALE_FLAG) {
1043 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
1046 mp3parse->xing_vbr_scale = 0;
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);
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;
1062 /* Optional LAME tag? */
1063 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
1064 gchar lame_version[10] = { 0, };
1066 guint32 encoder_delay, encoder_padding;
1068 memcpy (lame_version, 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);
1074 /* Skip all the information we're not interested in */
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;
1082 mp3parse->encoder_delay = encoder_delay;
1083 mp3parse->encoder_padding = encoder_padding;
1085 GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
1086 encoder_delay, encoder_padding);
1088 } else if (read_id_vbri == vbri_id) {
1089 gint64 total_bytes, total_frames;
1090 GstClockTime total_time;
1091 guint16 nseek_points;
1093 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1095 if (avail < offset_vbri + 26) {
1096 GST_DEBUG_OBJECT (mp3parse,
1097 "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1101 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1103 /* Move data after VBRI header */
1104 data += offset_vbri + 4;
1106 if (GST_READ_UINT16_BE (data) != 0x0001) {
1107 GST_WARNING_OBJECT (mp3parse,
1108 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1113 /* Skip encoder delay */
1119 total_bytes = GST_READ_UINT32_BE (data);
1120 if (total_bytes != 0)
1121 mp3parse->vbri_bytes = total_bytes;
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);
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;
1142 nseek_points = GST_READ_UINT16_BE (data);
1145 if (nseek_points > 0) {
1146 guint scale, seek_bytes, seek_frames;
1149 mp3parse->vbri_seek_points = nseek_points;
1151 scale = GST_READ_UINT16_BE (data);
1154 seek_bytes = GST_READ_UINT16_BE (data);
1157 seek_frames = GST_READ_UINT16_BE (data);
1159 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1160 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
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);
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");
1179 data += offset_vbri + 26;
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;
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;
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;
1195 } else /* seek_bytes == 1 */
1196 for (i = 0; i < nseek_points; i++) {
1197 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
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);
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;
1214 mp3parse->vbri_valid = TRUE;
1217 GST_DEBUG_OBJECT (mp3parse,
1218 "Xing, LAME or VBRI header not found in first frame");
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);
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);
1231 /* tell baseclass how nicely we can seek, and a bitrate if one found */
1232 /* FIXME: fill index with seek table */
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;
1239 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1240 mp3parse->vbri_total_time)
1241 seekable = GST_BASE_PARSE_SEEK_TABLE;
1244 if (mp3parse->xing_bitrate)
1245 bitrate = mp3parse->xing_bitrate;
1246 else if (mp3parse->vbri_bitrate)
1247 bitrate = mp3parse->vbri_bitrate;
1251 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1254 gst_buffer_unmap (buf, &map);
1258 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1259 GstClockTime ts, gint64 * bytepos)
1262 GstClockTime total_time;
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)) {
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);
1274 fa = mp3parse->xing_seek_table[index];
1276 fb = mp3parse->xing_seek_table[index + 1];
1280 fx = fa + (fb - fa) * (percent - index);
1282 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1287 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1288 (total_time = mp3parse->vbri_total_time)) {
1290 gdouble a, b, fa, fb;
1292 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1293 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1295 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1296 mp3parse->vbri_seek_points));
1298 for (j = i; j >= 0; j--)
1299 fa += mp3parse->vbri_seek_table[j];
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];
1306 b = gst_guint64_to_gdouble (total_time);
1310 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1319 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1320 gint64 bytepos, GstClockTime * ts)
1323 GstClockTime total_time;
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)) {
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];
1337 fb = mp3parse->xing_seek_table_inverse[index + 1];
1341 fx = fa + (fb - fa) * (pos - index);
1343 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1348 if (mp3parse->vbri_seek_table &&
1349 (total_bytes = mp3parse->vbri_bytes) &&
1350 (total_time = mp3parse->vbri_total_time)) {
1353 gdouble a, b, fa, fb;
1356 sum += mp3parse->vbri_seek_table[i];
1358 } while (i + 1 < mp3parse->vbri_seek_points
1359 && sum + mp3parse->vbri_seek_table[i] < bytepos);
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));
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));
1372 fb = gst_guint64_to_gdouble (total_time);
1375 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1384 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1385 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1387 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1388 gboolean res = FALSE;
1390 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
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);
1397 /* if no tables, fall back to default estimated rate based conversion */
1399 return gst_base_parse_convert_default (parse, src_format, src_value,
1400 dest_format, dest_value);
1405 static GstFlowReturn
1406 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1407 GstBaseParseFrame * frame)
1409 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1410 GstTagList *taglist = NULL;
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) {
1418 taglist = gst_tag_list_new_empty ();
1420 mp3parse->last_posted_crc = mp3parse->last_crc;
1421 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1426 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1430 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1432 taglist = gst_tag_list_new_empty ();
1434 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
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);
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) {
1445 if (taglist == NULL)
1446 taglist = gst_tag_list_new_empty ();
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);
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;
1457 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1458 return GST_FLOW_NOT_NEGOTIATED;
1461 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1462 GST_TAG_AUDIO_CODEC, caps);
1463 gst_caps_unref (caps);
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);
1473 /* also signals the end of first-frame processing */
1474 mp3parse->sent_codec_tag = TRUE;
1477 /* if the taglist exists, we need to update it so it gets sent out */
1479 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1480 gst_tag_list_unref (taglist);
1483 /* usual clipping applies */
1484 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1490 remove_fields (GstCaps * caps)
1494 n = gst_caps_get_size (caps);
1495 for (i = 0; i < n; i++) {
1496 GstStructure *s = gst_caps_get_structure (caps, i);
1498 gst_structure_remove_field (s, "parsed");
1503 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1505 GstCaps *peercaps, *templ;
1508 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
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);
1516 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1519 /* Remove the parsed field */
1520 peercaps = gst_caps_make_writable (peercaps);
1521 remove_fields (peercaps);
1523 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1524 gst_caps_unref (peercaps);
1525 gst_caps_unref (templ);
1531 GstCaps *intersection;
1534 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1535 gst_caps_unref (res);
1542 #ifdef TIZEN_FEATURE_MP3PARSE_MODIFICATION
1544 * gst_mpeg_audio_parse_src_eventfunc:
1545 * @parse: #GstBaseParse. #event
1547 * before baseparse handles seek event, check any mode and flag.
1549 * Returns: TRUE on success.
1552 gst_mpeg_audio_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
1554 gboolean handled = FALSE;
1555 GstMpegAudioParse *mp3parse;
1556 mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1558 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
1559 GST_EVENT_TYPE_NAME (event));
1561 switch (GST_EVENT_TYPE (event)) {
1562 case GST_EVENT_SEEK:
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;
1572 GST_INFO_OBJECT (mp3parse, "GST_EVENT_SEEK leave");
1580 /* call baseparse src_event function to handle event */
1581 handled = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);