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 ! mad ! autoaudiosink
37 /* FIXME: we should make the base class (GstBaseParse) aware of the
38 * XING seek table somehow, so it can use it properly for things like
39 * accurate seeks. Currently it can only do a lookup via the convert function,
40 * but then doesn't know what the result represents exactly. One could either
41 * add a vfunc for index lookup, or just make mpegaudioparse populate the
42 * base class's index via the API provided.
50 #include "gstmpegaudioparse.h"
51 #include <gst/base/gstbytereader.h>
52 #include <gst/pbutils/pbutils.h>
54 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
55 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
57 #define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
58 #define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
59 #define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
60 #define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
61 #define MPEG_AUDIO_CHANNEL_MODE_MONO 3
63 #define CRC_UNKNOWN -1
64 #define CRC_PROTECTED 0
65 #define CRC_NOT_PROTECTED 1
67 #define XING_FRAMES_FLAG 0x0001
68 #define XING_BYTES_FLAG 0x0002
69 #define XING_TOC_FLAG 0x0004
70 #define XING_VBR_SCALE_FLAG 0x0008
72 #define MIN_FRAME_SIZE 6
74 #ifdef GST_EXT_MP3PARSE_MODIFICATION
75 #define DEFAULT_CHECK_HTTP_SEEK FALSE
85 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS ("audio/mpeg, "
89 "mpegversion = (int) 1, "
90 "layer = (int) [ 1, 3 ], "
91 "mpegaudioversion = (int) [ 1, 3], "
92 "rate = (int) [ 8000, 48000 ], "
93 "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
96 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
102 static void gst_mpeg_audio_parse_finalize (GObject * object);
104 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
105 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
107 #ifdef GST_EXT_MP3PARSE_MODIFICATION
108 static void gst_mpeg_audio_parse_set_property (GObject * object, guint prop_id,
109 const GValue * value, GParamSpec * pspec);
110 static void gst_mpeg_audio_parse_get_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static gboolean gst_mpeg_audio_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event);
115 static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
116 GstBaseParseFrame * frame, gint * skipsize);
117 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
118 GstBaseParseFrame * frame);
119 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
120 GstFormat src_format, gint64 src_value,
121 GstFormat dest_format, gint64 * dest_value);
122 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
125 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
126 mp3parse, GstBuffer * buf);
128 #define gst_mpeg_audio_parse_parent_class parent_class
129 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
131 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
132 (gst_mpeg_audio_channel_mode_get_type())
134 static const GEnumValue mpeg_audio_channel_mode[] = {
135 {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
136 {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
137 {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
138 {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
139 {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
144 gst_mpeg_audio_channel_mode_get_type (void)
146 static GType mpeg_audio_channel_mode_type = 0;
148 if (!mpeg_audio_channel_mode_type) {
149 mpeg_audio_channel_mode_type =
150 g_enum_register_static ("GstMpegAudioChannelMode",
151 mpeg_audio_channel_mode);
153 return mpeg_audio_channel_mode_type;
157 gst_mpeg_audio_channel_mode_get_nick (gint mode)
160 for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
161 if (mpeg_audio_channel_mode[i].value == mode)
162 return mpeg_audio_channel_mode[i].value_nick;
168 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
170 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
171 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
172 GObjectClass *object_class = G_OBJECT_CLASS (klass);
174 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
175 "MPEG1 audio stream parser");
177 object_class->finalize = gst_mpeg_audio_parse_finalize;
179 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
180 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
181 parse_class->handle_frame =
182 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
183 parse_class->pre_push_frame =
184 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
185 parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
186 parse_class->get_sink_caps =
187 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
189 #ifdef GST_EXT_MP3PARSE_MODIFICATION
190 object_class->set_property = gst_mpeg_audio_parse_set_property;
191 object_class->get_property = gst_mpeg_audio_parse_get_property;
193 g_object_class_install_property (object_class, PROP_CHECK_HTTP_SEEK,
194 g_param_spec_boolean ("http-pull-mp3dec", "enable/disable",
195 "enable/disable mp3dec http seek pull mode",
196 DEFAULT_CHECK_HTTP_SEEK,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 /* T.B.D : make full mp3 index table when seek */
199 parse_class->src_event = gst_mpeg_audio_parse_src_eventfunc;
206 #define GST_TAG_CRC "has-crc"
207 #define GST_TAG_MODE "channel-mode"
209 gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
210 "has crc", "Using CRC", NULL);
211 gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
212 "channel mode", "MPEG audio channel mode", NULL);
214 g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
216 gst_element_class_add_pad_template (element_class,
217 gst_static_pad_template_get (&sink_template));
218 gst_element_class_add_pad_template (element_class,
219 gst_static_pad_template_get (&src_template));
221 gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
222 "Codec/Parser/Audio",
223 "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
224 "Jan Schmidt <thaytan@mad.scientist.com>,"
225 "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
229 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
231 mp3parse->channels = -1;
233 mp3parse->sent_codec_tag = FALSE;
234 mp3parse->last_posted_crc = CRC_UNKNOWN;
235 mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
236 mp3parse->freerate = 0;
238 mp3parse->hdr_bitrate = 0;
240 mp3parse->xing_flags = 0;
241 mp3parse->xing_bitrate = 0;
242 mp3parse->xing_frames = 0;
243 mp3parse->xing_total_time = 0;
244 mp3parse->xing_bytes = 0;
245 mp3parse->xing_vbr_scale = 0;
246 memset (mp3parse->xing_seek_table, 0, 100);
247 memset (mp3parse->xing_seek_table_inverse, 0, 256);
249 mp3parse->vbri_bitrate = 0;
250 mp3parse->vbri_frames = 0;
251 mp3parse->vbri_total_time = 0;
252 mp3parse->vbri_bytes = 0;
253 mp3parse->vbri_seek_points = 0;
254 g_free (mp3parse->vbri_seek_table);
255 mp3parse->vbri_seek_table = NULL;
257 mp3parse->encoder_delay = 0;
258 mp3parse->encoder_padding = 0;
262 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
264 gst_mpeg_audio_parse_reset (mp3parse);
265 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
266 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
270 gst_mpeg_audio_parse_finalize (GObject * object)
272 G_OBJECT_CLASS (parent_class)->finalize (object);
276 gst_mpeg_audio_parse_start (GstBaseParse * parse)
278 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
280 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
281 GST_DEBUG_OBJECT (parse, "starting");
283 gst_mpeg_audio_parse_reset (mp3parse);
285 #ifdef GST_EXT_MP3PARSE_MODIFICATION
286 if (mp3parse->http_seek_flag) {
287 /* Don't need Accurate Seek table (in http pull mode) */
288 GST_INFO_OBJECT (parse, "Enable (1) : mp3parse->http_seek_flag");
290 GST_INFO_OBJECT (parse, "Disable (0) : mp3parse->http_seek_flag");
297 #ifdef GST_EXT_MP3PARSE_MODIFICATION
298 gst_mpeg_audio_parse_set_property (GObject * object, guint prop_id,
299 const GValue * value, GParamSpec * pspec)
301 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
302 GST_INFO_OBJECT (mp3parse, "set_property() START- prop_id(%d)",prop_id);
304 case PROP_CHECK_HTTP_SEEK:
305 mp3parse->http_seek_flag = g_value_get_boolean (value);
306 GST_INFO_OBJECT (mp3parse, "http_seek_flag(%d)", mp3parse->http_seek_flag);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 gst_mpeg_audio_parse_get_property (GObject * object, guint prop_id,
316 const GValue * value, GParamSpec * pspec)
318 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (object);
319 GST_INFO_OBJECT (mp3parse, "get_property() START- prop_id(%d)",prop_id);
321 case PROP_CHECK_HTTP_SEEK:
322 g_value_set_boolean (value, mp3parse->http_seek_flag);
323 GST_INFO_OBJECT (mp3parse, "http_seek_flag(%d)", mp3parse->http_seek_flag);
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
335 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
337 GST_DEBUG_OBJECT (parse, "stopping");
339 gst_mpeg_audio_parse_reset (mp3parse);
344 static const guint mp3types_bitrates[2][3][16] = {
346 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
347 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
348 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
351 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
352 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
353 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
357 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
358 {22050, 24000, 16000},
363 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
364 guint * put_version, guint * put_layer, guint * put_channels,
365 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
369 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
373 if (header & (1 << 20)) {
374 lsf = (header & (1 << 19)) ? 0 : 1;
381 version = 1 + lsf + mpg25;
383 layer = 4 - ((header >> 17) & 0x3);
385 crc = (header >> 16) & 0x1;
387 bitrate = (header >> 12) & 0xF;
388 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
390 GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
391 bitrate = mp3parse->freerate;
394 samplerate = (header >> 10) & 0x3;
395 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
397 /* force 0 length if 0 bitrate */
398 padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
400 mode = (header >> 6) & 0x3;
401 channels = (mode == 3) ? 1 : 2;
405 length = 4 * ((bitrate * 12) / samplerate + padding);
408 length = (bitrate * 144) / samplerate + padding;
412 length = (bitrate * 144) / (samplerate << lsf) + padding;
416 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
418 GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
419 "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
420 layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
423 *put_version = version;
427 *put_channels = channels;
429 *put_bitrate = bitrate;
431 *put_samplerate = samplerate;
440 /* Minimum number of consecutive, valid-looking frames to consider
442 #define MIN_RESYNC_FRAMES 3
444 /* Perform extended validation to check that subsequent headers match
445 * the first header given here in important characteristics, to avoid
446 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
447 * frames to match their major characteristics.
449 * If at_eos is set to TRUE, we just check that we don't find any invalid
450 * frames in whatever data is available, rather than requiring a full
451 * MIN_RESYNC_FRAMES of data.
453 * Returns TRUE if we've seen enough data to validate or reject the frame.
454 * If TRUE is returned, then *valid contains TRUE if it validated, or false
455 * if we decided it was false sync.
456 * If FALSE is returned, then *valid contains minimum needed data.
459 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
460 guint32 header, int bpf, gboolean at_eos, gint * valid)
465 int frames_found = 1;
468 gst_buffer_map (buf, &map, GST_MAP_READ);
470 while (frames_found < MIN_RESYNC_FRAMES) {
471 /* Check if we have enough data for all these frames, plus the next
473 if (map.size < offset + 4) {
475 /* Running out of data at EOS is fine; just accept it */
485 next_header = GST_READ_UINT32_BE (map.data + offset);
486 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
487 offset, (unsigned int) header, (unsigned int) next_header, bpf);
489 /* mask the bits which are allowed to differ between frames */
490 #define HDRMASK ~((0xF << 12) /* bitrate */ | \
491 (0x1 << 9) /* padding */ | \
492 (0xf << 4) /* mode|mode extension */ | \
493 (0xf)) /* copyright|emphasis */
495 if ((next_header & HDRMASK) != (header & HDRMASK)) {
496 /* If any of the unmasked bits don't match, then it's not valid */
497 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
498 "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
499 (guint) header, (guint) header & HDRMASK, (guint) next_header,
500 (guint) next_header & HDRMASK, bpf);
503 } else if (((next_header >> 12) & 0xf) == 0xf) {
504 /* The essential parts were the same, but the bitrate held an
505 invalid value - also reject */
506 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
511 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
512 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
514 /* if no bitrate, and no freeform rate known, then fail */
515 if (G_UNLIKELY (!bpf)) {
516 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
528 gst_buffer_unmap (buf, &map);
533 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
536 GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
537 /* if it's not a valid sync */
538 if ((head & 0xffe00000) != 0xffe00000) {
539 GST_WARNING_OBJECT (mp3parse, "invalid sync");
542 /* if it's an invalid MPEG version */
543 if (((head >> 19) & 3) == 0x1) {
544 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
548 /* if it's an invalid layer */
549 if (!((head >> 17) & 3)) {
550 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
553 /* if it's an invalid bitrate */
554 if (((head >> 12) & 0xf) == 0xf) {
555 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
558 /* if it's an invalid samplerate */
559 if (((head >> 10) & 0x3) == 0x3) {
560 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
565 if ((head & 0x3) == 0x2) {
566 /* Ignore this as there are some files with emphasis 0x2 that can
567 * be played fine. See BGO #537235 */
568 GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
574 /* Determines possible freeform frame rate/size by looking for next
575 * header with valid bitrate (0 or otherwise valid) (and sufficiently
576 * matching current header).
578 * Returns TRUE if we've found such one, and *rate then contains rate
579 * (or *rate contains 0 if decided no freeframe size could be determined).
580 * If not enough data, returns FALSE.
583 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
584 guint32 header, gboolean at_eos, gint * _rate)
590 gulong samplerate, rate, layer, padding;
594 available = map->size;
599 /* pick apart header again partially */
600 if (header & (1 << 20)) {
601 lsf = (header & (1 << 19)) ? 0 : 1;
607 layer = 4 - ((header >> 17) & 0x3);
608 samplerate = (header >> 10) & 0x3;
609 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
610 padding = (header >> 9) & 0x1;
612 for (; offset < available; ++offset) {
613 /* Check if we have enough data for all these frames, plus the next
615 if (available < offset + 4) {
617 /* Running out of data; failed to determine size */
625 next_header = GST_READ_UINT32_BE (data + offset);
626 if ((next_header & 0xFFE00000) != 0xFFE00000)
629 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
630 offset, (unsigned int) header, (unsigned int) next_header);
632 if ((next_header & HDRMASK) != (header & HDRMASK)) {
633 /* If any of the unmasked bits don't match, then it's not valid */
634 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
635 "(header=%08X (%08X), header2=%08X (%08X))",
636 (guint) header, (guint) header & HDRMASK, (guint) next_header,
637 (guint) next_header & HDRMASK);
639 } else if (((next_header >> 12) & 0xf) == 0xf) {
640 /* The essential parts were the same, but the bitrate held an
641 invalid value - also reject */
642 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
649 /* almost accept as free frame */
651 rate = samplerate * (offset - 4 * padding + 4) / 48000;
653 rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
657 GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
658 if (rate < 8 || (layer == 3 && rate > 640)) {
659 GST_DEBUG_OBJECT (mp3parse, "rate invalid");
661 /* maybe some hope */
664 GST_DEBUG_OBJECT (mp3parse, "aborting");
669 *_rate = rate * 1000;
672 /* avoid indefinite searching */
674 GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
684 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
685 GstBaseParseFrame * frame, gint * skipsize)
687 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
688 GstBuffer *buf = frame->buffer;
689 GstByteReader reader;
691 gboolean lost_sync, draining, valid, caps_change;
693 guint bitrate, layer, rate, channels, version, mode, crc;
695 gboolean res = FALSE;
697 gst_buffer_map (buf, &map, GST_MAP_READ);
698 if (G_UNLIKELY (map.size < 6)) {
703 gst_byte_reader_init (&reader, map.data, map.size);
705 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
708 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
710 /* didn't find anything that looks like a sync word, skip */
712 *skipsize = map.size - 3;
716 /* possible frame header, but not at offset 0? skip bytes before sync */
722 /* make sure the values in the frame header look sane */
723 header = GST_READ_UINT32_BE (map.data);
724 if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
729 GST_LOG_OBJECT (parse, "got frame");
731 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
732 draining = GST_BASE_PARSE_DRAINING (parse);
734 if (G_UNLIKELY (lost_sync))
735 mp3parse->freerate = 0;
737 bpf = mp3_type_frame_length_from_header (mp3parse, header,
738 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
740 if (channels != mp3parse->channels || rate != mp3parse->rate ||
741 layer != mp3parse->layer || version != mp3parse->version)
746 /* maybe free format */
748 GST_LOG_OBJECT (mp3parse, "possibly free format");
749 if (lost_sync || mp3parse->freerate == 0) {
750 GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
751 if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
753 /* not enough data */
754 gst_base_parse_set_min_frame_size (parse, valid);
758 GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
759 mp3parse->freerate = valid;
763 bpf = mp3_type_frame_length_from_header (mp3parse, header,
764 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
766 /* did not come up with valid freeform length, reject after all */
772 if (!draining && (lost_sync || caps_change)) {
773 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
775 /* not enough data */
776 gst_base_parse_set_min_frame_size (parse, valid);
785 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
786 /* avoid caps jitter that we can't be sure of */
791 /* restore default minimum */
792 gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
796 /* metadata handling */
797 if (G_UNLIKELY (caps_change)) {
798 GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
799 "mpegversion", G_TYPE_INT, 1,
800 "mpegaudioversion", G_TYPE_INT, version,
801 "layer", G_TYPE_INT, layer,
802 "rate", G_TYPE_INT, rate,
803 "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
804 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
805 gst_caps_unref (caps);
807 mp3parse->rate = rate;
808 mp3parse->channels = channels;
809 mp3parse->layer = layer;
810 mp3parse->version = version;
812 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
813 if (mp3parse->layer == 1)
815 else if (mp3parse->layer == 2)
816 mp3parse->spf = 1152;
817 else if (mp3parse->version == 1) {
818 mp3parse->spf = 1152;
820 /* MPEG-2 or "2.5" */
825 * We start pushing 9 frames earlier (29 frames for MPEG2) than
826 * segment start to be able to decode the first frame we want.
827 * 9 (29) frames are the theoretical maximum of frames that contain
828 * data for the current frame (bit reservoir).
831 * Some mp3 streams have an offset in the timestamps, for which we have to
832 * push the frame *after* the end position in order for the decoder to be
833 * able to decode everything up until the segment.stop position. */
834 gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
835 (version == 1) ? 10 : 30, 2);
838 mp3parse->hdr_bitrate = bitrate;
840 /* For first frame; check for seek tables and output a codec tag */
841 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
843 /* store some frame info for later processing */
844 mp3parse->last_crc = crc;
845 mp3parse->last_mode = mode;
848 gst_buffer_unmap (buf, &map);
850 if (res && bpf <= map.size) {
851 return gst_base_parse_finish_frame (parse, frame, bpf);
858 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
861 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
862 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
863 const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */
864 const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */
865 gint offset_xing, offset_vbri;
867 gint64 upstream_total_bytes = 0;
868 guint32 read_id_xing = 0, read_id_vbri = 0;
873 if (mp3parse->sent_codec_tag)
876 /* Check first frame for Xing info */
877 if (mp3parse->version == 1) { /* MPEG-1 file */
878 if (mp3parse->channels == 1)
882 } else { /* MPEG-2 header */
883 if (mp3parse->channels == 1)
889 /* The VBRI tag is always at offset 0x20 */
892 /* Skip the 4 bytes of the MP3 header too */
896 /* Check if we have enough data to read the Xing header */
897 gst_buffer_map (buf, &map, GST_MAP_READ);
901 if (avail >= offset_xing + 4) {
902 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
904 if (avail >= offset_vbri + 4) {
905 read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
908 /* obtain real upstream total bytes */
909 if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
910 GST_FORMAT_BYTES, &upstream_total_bytes))
911 upstream_total_bytes = 0;
913 if (read_id_xing == xing_id || read_id_xing == info_id) {
915 guint bytes_needed = offset_xing + 8;
917 GstClockTime total_time;
919 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
921 /* Move data after Xing header */
922 data += offset_xing + 4;
924 /* Read 4 base bytes of flags, big-endian */
925 xing_flags = GST_READ_UINT32_BE (data);
927 if (xing_flags & XING_FRAMES_FLAG)
929 if (xing_flags & XING_BYTES_FLAG)
931 if (xing_flags & XING_TOC_FLAG)
933 if (xing_flags & XING_VBR_SCALE_FLAG)
935 if (avail < bytes_needed) {
936 GST_DEBUG_OBJECT (mp3parse,
937 "Not enough data to read Xing header (need %d)", bytes_needed);
941 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
942 mp3parse->xing_flags = xing_flags;
944 if (xing_flags & XING_FRAMES_FLAG) {
945 mp3parse->xing_frames = GST_READ_UINT32_BE (data);
946 if (mp3parse->xing_frames == 0) {
947 GST_WARNING_OBJECT (mp3parse,
948 "Invalid number of frames in Xing header");
949 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
951 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
952 (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
958 mp3parse->xing_frames = 0;
959 mp3parse->xing_total_time = 0;
962 if (xing_flags & XING_BYTES_FLAG) {
963 mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
964 if (mp3parse->xing_bytes == 0) {
965 GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
966 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
970 mp3parse->xing_bytes = 0;
973 /* If we know the upstream size and duration, compute the
974 * total bitrate, rounded up to the nearest kbit/sec */
975 if ((total_time = mp3parse->xing_total_time) &&
976 (total_bytes = mp3parse->xing_bytes)) {
977 mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
978 8 * GST_SECOND, total_time);
979 mp3parse->xing_bitrate += 500;
980 mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
983 if (xing_flags & XING_TOC_FLAG) {
985 guchar *table = mp3parse->xing_seek_table;
990 GST_DEBUG_OBJECT (mp3parse,
991 "Subtracting initial offset of %d bytes from Xing TOC", first);
993 /* xing seek table: percent time -> 1/256 bytepos */
994 for (i = 0; i < 100; i++) {
995 new = data[i] - first;
997 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
998 mp3parse->xing_flags &= ~XING_TOC_FLAG;
1001 mp3parse->xing_seek_table[i] = old = new;
1004 /* build inverse table: 1/256 bytepos -> 1/100 percent time */
1005 for (i = 0; i < 256; i++) {
1006 while (percent < 99 && table[percent + 1] <= i)
1009 if (table[percent] == i) {
1010 mp3parse->xing_seek_table_inverse[i] = percent * 100;
1011 } else if (percent < 99 && table[percent]) {
1013 gint a = percent, b = percent + 1;
1017 fx = (b - a) / (fb - fa) * (i - fa) + a;
1018 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1019 } else if (percent == 99) {
1021 gint a = percent, b = 100;
1025 fx = (b - a) / (fb - fa) * (i - fa) + a;
1026 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1032 memset (mp3parse->xing_seek_table, 0, 100);
1033 memset (mp3parse->xing_seek_table_inverse, 0, 256);
1036 if (xing_flags & XING_VBR_SCALE_FLAG) {
1037 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
1040 mp3parse->xing_vbr_scale = 0;
1042 GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
1043 GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
1044 GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
1045 mp3parse->xing_vbr_scale);
1047 /* check for truncated file */
1048 if (upstream_total_bytes && mp3parse->xing_bytes &&
1049 mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
1050 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1051 "invalidating Xing header duration and size");
1052 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
1053 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
1056 /* Optional LAME tag? */
1057 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
1058 gchar lame_version[10] = { 0, };
1060 guint32 encoder_delay, encoder_padding;
1062 memcpy (lame_version, data, 9);
1064 tag_rev = data[0] >> 4;
1065 GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
1066 tag_rev, lame_version);
1068 /* Skip all the information we're not interested in */
1070 /* Encoder delay and end padding */
1071 encoder_delay = GST_READ_UINT24_BE (data);
1072 encoder_delay >>= 12;
1073 encoder_padding = GST_READ_UINT24_BE (data);
1074 encoder_padding &= 0x000fff;
1076 mp3parse->encoder_delay = encoder_delay;
1077 mp3parse->encoder_padding = encoder_padding;
1079 GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
1080 encoder_delay, encoder_padding);
1082 } else if (read_id_vbri == vbri_id) {
1083 gint64 total_bytes, total_frames;
1084 GstClockTime total_time;
1085 guint16 nseek_points;
1087 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1089 if (avail < offset_vbri + 26) {
1090 GST_DEBUG_OBJECT (mp3parse,
1091 "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1095 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1097 /* Move data after VBRI header */
1098 data += offset_vbri + 4;
1100 if (GST_READ_UINT16_BE (data) != 0x0001) {
1101 GST_WARNING_OBJECT (mp3parse,
1102 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1107 /* Skip encoder delay */
1113 total_bytes = GST_READ_UINT32_BE (data);
1114 if (total_bytes != 0)
1115 mp3parse->vbri_bytes = total_bytes;
1118 total_frames = GST_READ_UINT32_BE (data);
1119 if (total_frames != 0) {
1120 mp3parse->vbri_frames = total_frames;
1121 mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
1122 (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
1126 /* If we know the upstream size and duration, compute the
1127 * total bitrate, rounded up to the nearest kbit/sec */
1128 if ((total_time = mp3parse->vbri_total_time) &&
1129 (total_bytes = mp3parse->vbri_bytes)) {
1130 mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
1131 8 * GST_SECOND, total_time);
1132 mp3parse->vbri_bitrate += 500;
1133 mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
1136 nseek_points = GST_READ_UINT16_BE (data);
1139 if (nseek_points > 0) {
1140 guint scale, seek_bytes, seek_frames;
1143 mp3parse->vbri_seek_points = nseek_points;
1145 scale = GST_READ_UINT16_BE (data);
1148 seek_bytes = GST_READ_UINT16_BE (data);
1151 seek_frames = GST_READ_UINT16_BE (data);
1153 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1154 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
1158 if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
1159 GST_WARNING_OBJECT (mp3parse,
1160 "Not enough data to read VBRI seek table (need %d)",
1161 offset_vbri + 26 + nseek_points * seek_bytes);
1165 if (seek_frames * nseek_points < total_frames - seek_frames ||
1166 seek_frames * nseek_points > total_frames + seek_frames) {
1167 GST_WARNING_OBJECT (mp3parse,
1168 "VBRI seek table doesn't cover the complete file");
1173 data += offset_vbri + 26;
1175 /* VBRI seek table: frame/seek_frames -> byte */
1176 mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
1177 if (seek_bytes == 4)
1178 for (i = 0; i < nseek_points; i++) {
1179 mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
1181 } else if (seek_bytes == 3)
1182 for (i = 0; i < nseek_points; i++) {
1183 mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
1185 } else if (seek_bytes == 2)
1186 for (i = 0; i < nseek_points; i++) {
1187 mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
1189 } else /* seek_bytes == 1 */
1190 for (i = 0; i < nseek_points; i++) {
1191 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
1197 GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
1198 GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
1199 GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
1201 /* check for truncated file */
1202 if (upstream_total_bytes && mp3parse->vbri_bytes &&
1203 mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
1204 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1205 "invalidating VBRI header duration and size");
1206 mp3parse->vbri_valid = FALSE;
1208 mp3parse->vbri_valid = TRUE;
1211 GST_DEBUG_OBJECT (mp3parse,
1212 "Xing, LAME or VBRI header not found in first frame");
1215 /* set duration if tables provided a valid one */
1216 if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
1217 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1218 mp3parse->xing_total_time, 0);
1220 if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
1221 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1222 mp3parse->vbri_total_time, 0);
1225 /* tell baseclass how nicely we can seek, and a bitrate if one found */
1226 /* FIXME: fill index with seek table */
1228 seekable = GST_BASE_PARSE_SEEK_DEFAULT;
1229 if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
1230 mp3parse->xing_total_time)
1231 seekable = GST_BASE_PARSE_SEEK_TABLE;
1233 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1234 mp3parse->vbri_total_time)
1235 seekable = GST_BASE_PARSE_SEEK_TABLE;
1238 if (mp3parse->xing_bitrate)
1239 bitrate = mp3parse->xing_bitrate;
1240 else if (mp3parse->vbri_bitrate)
1241 bitrate = mp3parse->vbri_bitrate;
1245 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1248 gst_buffer_unmap (buf, &map);
1252 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1253 GstClockTime ts, gint64 * bytepos)
1256 GstClockTime total_time;
1258 /* If XING seek table exists use this for time->byte conversion */
1259 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1260 (total_bytes = mp3parse->xing_bytes) &&
1261 (total_time = mp3parse->xing_total_time)) {
1264 CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1265 gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1266 gint index = CLAMP (percent, 0, 99);
1268 fa = mp3parse->xing_seek_table[index];
1270 fb = mp3parse->xing_seek_table[index + 1];
1274 fx = fa + (fb - fa) * (percent - index);
1276 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1281 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1282 (total_time = mp3parse->vbri_total_time)) {
1284 gdouble a, b, fa, fb;
1286 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1287 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1289 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1290 mp3parse->vbri_seek_points));
1292 for (j = i; j >= 0; j--)
1293 fa += mp3parse->vbri_seek_table[j];
1295 if (i + 1 < mp3parse->vbri_seek_points) {
1296 b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1297 mp3parse->vbri_seek_points));
1298 fb = fa + mp3parse->vbri_seek_table[i + 1];
1300 b = gst_guint64_to_gdouble (total_time);
1304 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1313 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1314 gint64 bytepos, GstClockTime * ts)
1317 GstClockTime total_time;
1319 /* If XING seek table exists use this for byte->time conversion */
1320 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1321 (total_bytes = mp3parse->xing_bytes) &&
1322 (total_time = mp3parse->xing_total_time)) {
1327 pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1328 index = CLAMP (pos, 0, 255);
1329 fa = mp3parse->xing_seek_table_inverse[index];
1331 fb = mp3parse->xing_seek_table_inverse[index + 1];
1335 fx = fa + (fb - fa) * (pos - index);
1337 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1342 if (mp3parse->vbri_seek_table &&
1343 (total_bytes = mp3parse->vbri_bytes) &&
1344 (total_time = mp3parse->vbri_total_time)) {
1347 gdouble a, b, fa, fb;
1350 sum += mp3parse->vbri_seek_table[i];
1352 } while (i + 1 < mp3parse->vbri_seek_points
1353 && sum + mp3parse->vbri_seek_table[i] < bytepos);
1356 a = gst_guint64_to_gdouble (sum);
1357 fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1358 mp3parse->vbri_seek_points));
1360 if (i + 1 < mp3parse->vbri_seek_points) {
1361 b = a + mp3parse->vbri_seek_table[i + 1];
1362 fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1363 mp3parse->vbri_seek_points));
1366 fb = gst_guint64_to_gdouble (total_time);
1369 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1378 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1379 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1381 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1382 gboolean res = FALSE;
1384 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1386 gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1387 else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1388 res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1389 (GstClockTime *) dest_value);
1391 /* if no tables, fall back to default estimated rate based conversion */
1393 return gst_base_parse_convert_default (parse, src_format, src_value,
1394 dest_format, dest_value);
1399 static GstFlowReturn
1400 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1401 GstBaseParseFrame * frame)
1403 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1404 GstTagList *taglist = NULL;
1406 /* we will create a taglist (if any of the parameters has changed)
1407 * to add the tags that changed */
1408 if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1412 taglist = gst_tag_list_new_empty ();
1414 mp3parse->last_posted_crc = mp3parse->last_crc;
1415 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1420 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1424 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1426 taglist = gst_tag_list_new_empty ();
1428 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1430 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1431 gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1434 /* tag sending done late enough in hook to ensure pending events
1435 * have already been sent */
1436 if (taglist != NULL || !mp3parse->sent_codec_tag) {
1439 if (taglist == NULL)
1440 taglist = gst_tag_list_new_empty ();
1443 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1444 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1445 GST_TAG_AUDIO_CODEC, caps);
1446 gst_caps_unref (caps);
1448 if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1449 mp3parse->vbri_bitrate == 0) {
1450 /* We don't have a VBR bitrate, so post the available bitrate as
1451 * nominal and let baseparse calculate the real bitrate */
1452 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1453 GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1456 /* also signals the end of first-frame processing */
1457 mp3parse->sent_codec_tag = TRUE;
1460 /* if the taglist exists, we need to update it so it gets sent out */
1462 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1463 gst_tag_list_unref (taglist);
1466 /* usual clipping applies */
1467 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1473 remove_fields (GstCaps * caps)
1477 n = gst_caps_get_size (caps);
1478 for (i = 0; i < n; i++) {
1479 GstStructure *s = gst_caps_get_structure (caps, i);
1481 gst_structure_remove_field (s, "parsed");
1486 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1488 GstCaps *peercaps, *templ;
1491 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1493 GstCaps *fcopy = gst_caps_copy (filter);
1494 /* Remove the fields we convert */
1495 remove_fields (fcopy);
1496 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1497 gst_caps_unref (fcopy);
1499 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1502 /* Remove the parsed field */
1503 peercaps = gst_caps_make_writable (peercaps);
1504 remove_fields (peercaps);
1506 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1507 gst_caps_unref (peercaps);
1508 gst_caps_unref (templ);
1514 GstCaps *intersection;
1517 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1518 gst_caps_unref (res);
1525 #ifdef GST_EXT_MP3PARSE_MODIFICATION
1527 * gst_mpeg_audio_parse_src_eventfunc:
1528 * @parse: #GstBaseParse. #event
1530 * before baseparse handles seek event, check any mode and flag.
1532 * Returns: TRUE on success.
1535 gst_mpeg_audio_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
1537 gboolean handled = FALSE;
1538 GstMpegAudioParse *mp3parse;
1539 mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1541 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
1542 GST_EVENT_TYPE_NAME (event));
1544 switch (GST_EVENT_TYPE (event)) {
1545 case GST_EVENT_SEEK:
1547 GST_INFO_OBJECT (mp3parse, "GST_EVENT_SEEK enter");
1548 if (mp3parse->http_seek_flag) {
1549 GST_INFO_OBJECT (mp3parse, "souphttpsrc is PULL MODE (so accurate seek mode is OFF)");
1550 /* Check the declaration of this function in the baseparse */
1551 gst_base_parse_set_seek_mode(parse, 0);
1552 goto mp3_seek_null_exit;
1554 GST_INFO_OBJECT (mp3parse, "GST_EVENT_SEEK leave");
1562 /* call baseparse src_event function to handle event */
1563 handled = GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);