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 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
79 GST_STATIC_CAPS ("audio/mpeg, "
80 "mpegversion = (int) 1, "
81 "layer = (int) [ 1, 3 ], "
82 "mpegaudioversion = (int) [ 1, 3], "
83 "rate = (int) [ 8000, 48000 ], "
84 "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
87 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
90 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
93 static void gst_mpeg_audio_parse_finalize (GObject * object);
95 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
96 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
97 static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
98 GstBaseParseFrame * frame, gint * skipsize);
99 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
100 GstBaseParseFrame * frame);
101 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
102 GstFormat src_format, gint64 src_value,
103 GstFormat dest_format, gint64 * dest_value);
104 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
107 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
108 mp3parse, GstBuffer * buf);
110 #define gst_mpeg_audio_parse_parent_class parent_class
111 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
112 GST_ELEMENT_REGISTER_DEFINE (mpegaudioparse, "mpegaudioparse",
113 GST_RANK_PRIMARY + 2, GST_TYPE_MPEG_AUDIO_PARSE);
115 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
116 (gst_mpeg_audio_channel_mode_get_type())
118 static const GEnumValue mpeg_audio_channel_mode[] = {
119 {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
120 {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
121 {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
122 {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
123 {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
128 gst_mpeg_audio_channel_mode_get_type (void)
130 static GType mpeg_audio_channel_mode_type = 0;
132 if (!mpeg_audio_channel_mode_type) {
133 mpeg_audio_channel_mode_type =
134 g_enum_register_static ("GstMpegAudioChannelMode",
135 mpeg_audio_channel_mode);
137 return mpeg_audio_channel_mode_type;
141 gst_mpeg_audio_channel_mode_get_nick (gint mode)
144 for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
145 if (mpeg_audio_channel_mode[i].value == mode)
146 return mpeg_audio_channel_mode[i].value_nick;
152 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
154 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
155 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
156 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
159 "MPEG1 audio stream parser");
161 object_class->finalize = gst_mpeg_audio_parse_finalize;
163 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
164 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
165 parse_class->handle_frame =
166 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
167 parse_class->pre_push_frame =
168 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
169 parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
170 parse_class->get_sink_caps =
171 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
174 #define GST_TAG_CRC "has-crc"
175 #define GST_TAG_MODE "channel-mode"
177 gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
178 "has crc", "Using CRC", NULL);
179 gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
180 "channel mode", "MPEG audio channel mode", NULL);
182 g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
184 gst_element_class_add_static_pad_template (element_class, &sink_template);
185 gst_element_class_add_static_pad_template (element_class, &src_template);
187 gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
188 "Codec/Parser/Audio",
189 "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
190 "Jan Schmidt <thaytan@mad.scientist.com>,"
191 "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
195 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
197 mp3parse->channels = -1;
199 mp3parse->sent_codec_tag = FALSE;
200 mp3parse->last_posted_crc = CRC_UNKNOWN;
201 mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
202 mp3parse->freerate = 0;
204 mp3parse->hdr_bitrate = 0;
205 mp3parse->bitrate_is_constant = TRUE;
207 mp3parse->xing_flags = 0;
208 mp3parse->xing_bitrate = 0;
209 mp3parse->xing_frames = 0;
210 mp3parse->xing_total_time = 0;
211 mp3parse->xing_bytes = 0;
212 mp3parse->xing_vbr_scale = 0;
213 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
214 memset (mp3parse->xing_seek_table_inverse, 0,
215 sizeof (mp3parse->xing_seek_table_inverse));
217 mp3parse->vbri_bitrate = 0;
218 mp3parse->vbri_frames = 0;
219 mp3parse->vbri_total_time = 0;
220 mp3parse->vbri_bytes = 0;
221 mp3parse->vbri_seek_points = 0;
222 g_free (mp3parse->vbri_seek_table);
223 mp3parse->vbri_seek_table = NULL;
225 mp3parse->encoder_delay = 0;
226 mp3parse->encoder_padding = 0;
230 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
232 gst_mpeg_audio_parse_reset (mp3parse);
233 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
234 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
238 gst_mpeg_audio_parse_finalize (GObject * object)
240 G_OBJECT_CLASS (parent_class)->finalize (object);
244 gst_mpeg_audio_parse_start (GstBaseParse * parse)
246 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
248 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
249 GST_DEBUG_OBJECT (parse, "starting");
251 gst_mpeg_audio_parse_reset (mp3parse);
257 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
259 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
261 GST_DEBUG_OBJECT (parse, "stopping");
263 gst_mpeg_audio_parse_reset (mp3parse);
268 static const guint mp3types_bitrates[2][3][16] = {
270 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
271 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
272 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
275 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
276 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
277 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
281 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
282 {22050, 24000, 16000},
287 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
288 guint * put_version, guint * put_layer, guint * put_channels,
289 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
293 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
297 if (header & (1 << 20)) {
298 lsf = (header & (1 << 19)) ? 0 : 1;
305 version = 1 + lsf + mpg25;
307 layer = 4 - ((header >> 17) & 0x3);
309 crc = (header >> 16) & 0x1;
311 bitrate = (header >> 12) & 0xF;
312 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
314 GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
315 bitrate = mp3parse->freerate;
318 samplerate = (header >> 10) & 0x3;
319 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
321 /* force 0 length if 0 bitrate */
322 padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
324 mode = (header >> 6) & 0x3;
325 channels = (mode == 3) ? 1 : 2;
329 length = 4 * ((bitrate * 12) / samplerate + padding);
332 length = (bitrate * 144) / samplerate + padding;
336 length = (bitrate * 144) / (samplerate << lsf) + padding;
340 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
342 GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
343 "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
344 layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
347 *put_version = version;
351 *put_channels = channels;
353 *put_bitrate = bitrate;
355 *put_samplerate = samplerate;
364 /* Minimum number of consecutive, valid-looking frames to consider
366 #define MIN_RESYNC_FRAMES 3
368 /* Perform extended validation to check that subsequent headers match
369 * the first header given here in important characteristics, to avoid
370 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
371 * frames to match their major characteristics.
373 * If at_eos is set to TRUE, we just check that we don't find any invalid
374 * frames in whatever data is available, rather than requiring a full
375 * MIN_RESYNC_FRAMES of data.
377 * Returns TRUE if we've seen enough data to validate or reject the frame.
378 * If TRUE is returned, then *valid contains TRUE if it validated, or false
379 * if we decided it was false sync.
380 * If FALSE is returned, then *valid contains minimum needed data.
383 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
384 guint32 header, int bpf, gboolean at_eos, gint * valid)
389 int frames_found = 1;
392 gst_buffer_map (buf, &map, GST_MAP_READ);
394 while (frames_found < MIN_RESYNC_FRAMES) {
395 /* Check if we have enough data for all these frames, plus the next
397 if (map.size < offset + 4) {
399 /* Running out of data at EOS is fine; just accept it */
409 next_header = GST_READ_UINT32_BE (map.data + offset);
410 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
411 offset, (unsigned int) header, (unsigned int) next_header, bpf);
413 /* mask the bits which are allowed to differ between frames */
414 #define HDRMASK ~((0xF << 12) /* bitrate */ | \
415 (0x1 << 9) /* padding */ | \
416 (0xf << 4) /* mode|mode extension */ | \
417 (0xf)) /* copyright|emphasis */
419 if ((next_header & HDRMASK) != (header & HDRMASK)) {
420 /* If any of the unmasked bits don't match, then it's not valid */
421 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
422 "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
423 (guint) header, (guint) header & HDRMASK, (guint) next_header,
424 (guint) next_header & HDRMASK, bpf);
427 } else if (((next_header >> 12) & 0xf) == 0xf) {
428 /* The essential parts were the same, but the bitrate held an
429 invalid value - also reject */
430 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
435 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
436 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
438 /* if no bitrate, and no freeform rate known, then fail */
439 if (G_UNLIKELY (!bpf)) {
440 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
452 gst_buffer_unmap (buf, &map);
457 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
460 GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
461 /* if it's not a valid sync */
462 if ((head & 0xffe00000) != 0xffe00000) {
463 GST_WARNING_OBJECT (mp3parse, "invalid sync");
466 /* if it's an invalid MPEG version */
467 if (((head >> 19) & 3) == 0x1) {
468 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
472 /* if it's an invalid layer */
473 if (!((head >> 17) & 3)) {
474 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
477 /* if it's an invalid bitrate */
478 if (((head >> 12) & 0xf) == 0xf) {
479 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
482 /* if it's an invalid samplerate */
483 if (((head >> 10) & 0x3) == 0x3) {
484 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
489 if ((head & 0x3) == 0x2) {
490 /* Ignore this as there are some files with emphasis 0x2 that can
491 * be played fine. See BGO #537235 */
492 GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
498 /* Determines possible freeform frame rate/size by looking for next
499 * header with valid bitrate (0 or otherwise valid) (and sufficiently
500 * matching current header).
502 * Returns TRUE if we've found such one, and *rate then contains rate
503 * (or *rate contains 0 if decided no freeframe size could be determined).
504 * If not enough data, returns FALSE.
507 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
508 guint32 header, gboolean at_eos, gint * _rate)
514 gulong samplerate, rate, layer, padding;
518 available = map->size;
523 /* pick apart header again partially */
524 if (header & (1 << 20)) {
525 lsf = (header & (1 << 19)) ? 0 : 1;
531 layer = 4 - ((header >> 17) & 0x3);
532 samplerate = (header >> 10) & 0x3;
533 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
534 padding = (header >> 9) & 0x1;
536 for (; offset < available; ++offset) {
537 /* Check if we have enough data for all these frames, plus the next
539 if (available < offset + 4) {
541 /* Running out of data; failed to determine size */
549 next_header = GST_READ_UINT32_BE (data + offset);
550 if ((next_header & 0xFFE00000) != 0xFFE00000)
553 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
554 offset, (unsigned int) header, (unsigned int) next_header);
556 if ((next_header & HDRMASK) != (header & HDRMASK)) {
557 /* If any of the unmasked bits don't match, then it's not valid */
558 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
559 "(header=%08X (%08X), header2=%08X (%08X))",
560 (guint) header, (guint) header & HDRMASK, (guint) next_header,
561 (guint) next_header & HDRMASK);
563 } else if (((next_header >> 12) & 0xf) == 0xf) {
564 /* The essential parts were the same, but the bitrate held an
565 invalid value - also reject */
566 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
573 /* almost accept as free frame */
575 rate = samplerate * (offset - 4 * padding + 4) / 48000;
577 rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
581 GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
582 if (rate < 8 || (layer == 3 && rate > 640)) {
583 GST_DEBUG_OBJECT (mp3parse, "rate invalid");
585 /* maybe some hope */
588 GST_DEBUG_OBJECT (mp3parse, "aborting");
593 *_rate = rate * 1000;
596 /* avoid indefinite searching */
598 GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
608 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
609 GstBaseParseFrame * frame, gint * skipsize)
611 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
612 GstBuffer *buf = frame->buffer;
613 GstByteReader reader;
615 gboolean lost_sync, draining, valid, caps_change;
617 guint bitrate, layer, rate, channels, version, mode, crc;
619 gboolean res = FALSE;
621 gst_buffer_map (buf, &map, GST_MAP_READ);
622 if (G_UNLIKELY (map.size < 6)) {
627 gst_byte_reader_init (&reader, map.data, map.size);
629 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
632 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
634 /* didn't find anything that looks like a sync word, skip */
636 *skipsize = map.size - 3;
640 /* possible frame header, but not at offset 0? skip bytes before sync */
646 /* make sure the values in the frame header look sane */
647 header = GST_READ_UINT32_BE (map.data);
648 if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
653 GST_LOG_OBJECT (parse, "got frame");
655 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
656 draining = GST_BASE_PARSE_DRAINING (parse);
658 if (G_UNLIKELY (lost_sync))
659 mp3parse->freerate = 0;
661 bpf = mp3_type_frame_length_from_header (mp3parse, header,
662 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
664 if (channels != mp3parse->channels || rate != mp3parse->rate ||
665 layer != mp3parse->layer || version != mp3parse->version)
670 /* maybe free format */
672 GST_LOG_OBJECT (mp3parse, "possibly free format");
673 if (lost_sync || mp3parse->freerate == 0) {
674 GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
675 if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
677 /* not enough data */
678 gst_base_parse_set_min_frame_size (parse, valid);
682 GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
683 mp3parse->freerate = valid;
687 bpf = mp3_type_frame_length_from_header (mp3parse, header,
688 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
690 /* did not come up with valid freeform length, reject after all */
696 if (!draining && (lost_sync || caps_change)) {
697 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
699 /* not enough data */
700 gst_base_parse_set_min_frame_size (parse, valid);
709 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
710 /* avoid caps jitter that we can't be sure of */
715 /* restore default minimum */
716 gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
720 /* metadata handling */
721 if (G_UNLIKELY (caps_change)) {
722 GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
723 "mpegversion", G_TYPE_INT, 1,
724 "mpegaudioversion", G_TYPE_INT, version,
725 "layer", G_TYPE_INT, layer,
726 "rate", G_TYPE_INT, rate,
727 "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
728 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
729 gst_caps_unref (caps);
731 mp3parse->rate = rate;
732 mp3parse->channels = channels;
733 mp3parse->layer = layer;
734 mp3parse->version = version;
736 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
737 if (mp3parse->layer == 1)
739 else if (mp3parse->layer == 2)
740 mp3parse->spf = 1152;
741 else if (mp3parse->version == 1) {
742 mp3parse->spf = 1152;
744 /* MPEG-2 or "2.5" */
749 * We start pushing 9 frames earlier (29 frames for MPEG2) than
750 * segment start to be able to decode the first frame we want.
751 * 9 (29) frames are the theoretical maximum of frames that contain
752 * data for the current frame (bit reservoir).
755 * Some mp3 streams have an offset in the timestamps, for which we have to
756 * push the frame *after* the end position in order for the decoder to be
757 * able to decode everything up until the segment.stop position. */
758 gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
759 (version == 1) ? 10 : 30, 2);
762 if (mp3parse->hdr_bitrate && mp3parse->hdr_bitrate != bitrate) {
763 mp3parse->bitrate_is_constant = FALSE;
765 mp3parse->hdr_bitrate = bitrate;
767 /* For first frame; check for seek tables and output a codec tag */
768 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
770 /* store some frame info for later processing */
771 mp3parse->last_crc = crc;
772 mp3parse->last_mode = mode;
775 gst_buffer_unmap (buf, &map);
777 if (res && bpf <= map.size) {
778 return gst_base_parse_finish_frame (parse, frame, bpf);
785 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
788 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
789 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
790 const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */
791 const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */
792 gint offset_xing, offset_vbri;
794 gint64 upstream_total_bytes = 0;
795 guint32 read_id_xing = 0, read_id_vbri = 0;
800 if (mp3parse->sent_codec_tag)
803 /* Check first frame for Xing info */
804 if (mp3parse->version == 1) { /* MPEG-1 file */
805 if (mp3parse->channels == 1)
809 } else { /* MPEG-2 header */
810 if (mp3parse->channels == 1)
816 /* The VBRI tag is always at offset 0x20 */
819 /* Skip the 4 bytes of the MP3 header too */
823 /* Check if we have enough data to read the Xing header */
824 gst_buffer_map (buf, &map, GST_MAP_READ);
828 if (avail >= offset_xing + 4) {
829 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
831 if (avail >= offset_vbri + 4) {
832 read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
835 /* obtain real upstream total bytes */
836 if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
837 GST_FORMAT_BYTES, &upstream_total_bytes))
838 upstream_total_bytes = 0;
840 if (read_id_xing == xing_id || read_id_xing == info_id) {
842 guint bytes_needed = offset_xing + 8;
844 GstClockTime total_time;
846 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
848 /* Move data after Xing header */
849 data += offset_xing + 4;
851 /* Read 4 base bytes of flags, big-endian */
852 xing_flags = GST_READ_UINT32_BE (data);
854 if (xing_flags & XING_FRAMES_FLAG)
856 if (xing_flags & XING_BYTES_FLAG)
858 if (xing_flags & XING_TOC_FLAG)
860 if (xing_flags & XING_VBR_SCALE_FLAG)
862 if (avail < bytes_needed) {
863 GST_DEBUG_OBJECT (mp3parse,
864 "Not enough data to read Xing header (need %d)", bytes_needed);
868 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
869 mp3parse->xing_flags = xing_flags;
871 if (xing_flags & XING_FRAMES_FLAG) {
872 mp3parse->xing_frames = GST_READ_UINT32_BE (data);
873 if (mp3parse->xing_frames == 0) {
874 GST_WARNING_OBJECT (mp3parse,
875 "Invalid number of frames in Xing header");
876 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
878 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
879 (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
885 mp3parse->xing_frames = 0;
886 mp3parse->xing_total_time = 0;
889 if (xing_flags & XING_BYTES_FLAG) {
890 mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
891 if (mp3parse->xing_bytes == 0) {
892 GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
893 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
897 mp3parse->xing_bytes = 0;
900 /* If we know the upstream size and duration, compute the
901 * total bitrate, rounded up to the nearest kbit/sec */
902 if ((total_time = mp3parse->xing_total_time) &&
903 (total_bytes = mp3parse->xing_bytes)) {
904 mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
905 8 * GST_SECOND, total_time);
906 mp3parse->xing_bitrate += 500;
907 mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
910 if (xing_flags & XING_TOC_FLAG) {
912 guchar *table = mp3parse->xing_seek_table;
917 GST_DEBUG_OBJECT (mp3parse,
918 "Subtracting initial offset of %d bytes from Xing TOC", first);
920 /* xing seek table: percent time -> 1/256 bytepos */
921 for (i = 0; i < 100; i++) {
922 new = data[i] - first;
924 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
925 mp3parse->xing_flags &= ~XING_TOC_FLAG;
928 mp3parse->xing_seek_table[i] = old = new;
931 /* build inverse table: 1/256 bytepos -> 1/100 percent time */
932 for (i = 0; i < 256; i++) {
933 while (percent < 99 && table[percent + 1] <= i)
936 if (table[percent] == i) {
937 mp3parse->xing_seek_table_inverse[i] = percent * 100;
938 } else if (percent < 99 && table[percent]) {
940 gint a = percent, b = percent + 1;
944 fx = (b - a) / (fb - fa) * (i - fa) + a;
945 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
946 } else if (percent == 99) {
948 gint a = percent, b = 100;
952 fx = (b - a) / (fb - fa) * (i - fa) + a;
953 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
959 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
960 memset (mp3parse->xing_seek_table_inverse, 0,
961 sizeof (mp3parse->xing_seek_table_inverse));
964 if (xing_flags & XING_VBR_SCALE_FLAG) {
965 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
968 mp3parse->xing_vbr_scale = 0;
970 GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
971 GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
972 GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
973 mp3parse->xing_vbr_scale);
975 /* check for truncated file */
976 if (upstream_total_bytes && mp3parse->xing_bytes &&
977 mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
978 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
979 "invalidating Xing header duration and size");
980 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
981 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
984 /* Optional LAME tag? */
985 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
986 gchar lame_version[10] = { 0, };
988 guint32 encoder_delay, encoder_padding;
990 memcpy (lame_version, data, 9);
992 tag_rev = data[0] >> 4;
993 GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
994 tag_rev, lame_version);
996 /* Skip all the information we're not interested in */
998 /* Encoder delay and end padding */
999 encoder_delay = GST_READ_UINT24_BE (data);
1000 encoder_delay >>= 12;
1001 encoder_padding = GST_READ_UINT24_BE (data);
1002 encoder_padding &= 0x000fff;
1004 mp3parse->encoder_delay = encoder_delay;
1005 mp3parse->encoder_padding = encoder_padding;
1007 GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
1008 encoder_delay, encoder_padding);
1010 } else if (read_id_vbri == vbri_id) {
1011 gint64 total_bytes, total_frames;
1012 GstClockTime total_time;
1013 guint16 nseek_points;
1015 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1017 if (avail < offset_vbri + 26) {
1018 GST_DEBUG_OBJECT (mp3parse,
1019 "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1023 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1025 /* Move data after VBRI header */
1026 data += offset_vbri + 4;
1028 if (GST_READ_UINT16_BE (data) != 0x0001) {
1029 GST_WARNING_OBJECT (mp3parse,
1030 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1035 /* Skip encoder delay */
1041 total_bytes = GST_READ_UINT32_BE (data);
1042 if (total_bytes != 0)
1043 mp3parse->vbri_bytes = total_bytes;
1046 total_frames = GST_READ_UINT32_BE (data);
1047 if (total_frames != 0) {
1048 mp3parse->vbri_frames = total_frames;
1049 mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
1050 (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
1054 /* If we know the upstream size and duration, compute the
1055 * total bitrate, rounded up to the nearest kbit/sec */
1056 if ((total_time = mp3parse->vbri_total_time) &&
1057 (total_bytes = mp3parse->vbri_bytes)) {
1058 mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
1059 8 * GST_SECOND, total_time);
1060 mp3parse->vbri_bitrate += 500;
1061 mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
1064 nseek_points = GST_READ_UINT16_BE (data);
1067 if (nseek_points > 0) {
1068 guint scale, seek_bytes, seek_frames;
1071 mp3parse->vbri_seek_points = nseek_points;
1073 scale = GST_READ_UINT16_BE (data);
1076 seek_bytes = GST_READ_UINT16_BE (data);
1079 seek_frames = GST_READ_UINT16_BE (data);
1081 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1082 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
1086 if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
1087 GST_WARNING_OBJECT (mp3parse,
1088 "Not enough data to read VBRI seek table (need %d)",
1089 offset_vbri + 26 + nseek_points * seek_bytes);
1093 if (seek_frames * nseek_points < total_frames - seek_frames ||
1094 seek_frames * nseek_points > total_frames + seek_frames) {
1095 GST_WARNING_OBJECT (mp3parse,
1096 "VBRI seek table doesn't cover the complete file");
1101 data += offset_vbri + 26;
1103 /* VBRI seek table: frame/seek_frames -> byte */
1104 mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
1105 if (seek_bytes == 4)
1106 for (i = 0; i < nseek_points; i++) {
1107 mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
1109 } else if (seek_bytes == 3)
1110 for (i = 0; i < nseek_points; i++) {
1111 mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
1113 } else if (seek_bytes == 2)
1114 for (i = 0; i < nseek_points; i++) {
1115 mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
1117 } else /* seek_bytes == 1 */
1118 for (i = 0; i < nseek_points; i++) {
1119 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
1125 GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
1126 GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
1127 GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
1129 /* check for truncated file */
1130 if (upstream_total_bytes && mp3parse->vbri_bytes &&
1131 mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
1132 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1133 "invalidating VBRI header duration and size");
1134 mp3parse->vbri_valid = FALSE;
1136 mp3parse->vbri_valid = TRUE;
1139 GST_DEBUG_OBJECT (mp3parse,
1140 "Xing, LAME or VBRI header not found in first frame");
1143 /* set duration if tables provided a valid one */
1144 if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
1145 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1146 mp3parse->xing_total_time, 0);
1148 if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
1149 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1150 mp3parse->vbri_total_time, 0);
1153 /* tell baseclass how nicely we can seek, and a bitrate if one found */
1154 /* FIXME: fill index with seek table */
1156 seekable = GST_BASE_PARSE_SEEK_DEFAULT;
1157 if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
1158 mp3parse->xing_total_time)
1159 seekable = GST_BASE_PARSE_SEEK_TABLE;
1161 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1162 mp3parse->vbri_total_time)
1163 seekable = GST_BASE_PARSE_SEEK_TABLE;
1166 if (mp3parse->xing_bitrate)
1167 bitrate = mp3parse->xing_bitrate;
1168 else if (mp3parse->vbri_bitrate)
1169 bitrate = mp3parse->vbri_bitrate;
1173 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1176 gst_buffer_unmap (buf, &map);
1180 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1181 GstClockTime ts, gint64 * bytepos)
1184 GstClockTime total_time;
1186 /* If XING seek table exists use this for time->byte conversion */
1187 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1188 (total_bytes = mp3parse->xing_bytes) &&
1189 (total_time = mp3parse->xing_total_time)) {
1192 CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1193 gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1194 gint index = CLAMP (percent, 0, 99);
1196 fa = mp3parse->xing_seek_table[index];
1198 fb = mp3parse->xing_seek_table[index + 1];
1202 fx = fa + (fb - fa) * (percent - index);
1204 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1209 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1210 (total_time = mp3parse->vbri_total_time)) {
1212 gdouble a, b, fa, fb;
1214 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1215 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1217 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1218 mp3parse->vbri_seek_points));
1220 for (j = i; j >= 0; j--)
1221 fa += mp3parse->vbri_seek_table[j];
1223 if (i + 1 < mp3parse->vbri_seek_points) {
1224 b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1225 mp3parse->vbri_seek_points));
1226 fb = fa + mp3parse->vbri_seek_table[i + 1];
1228 b = gst_guint64_to_gdouble (total_time);
1232 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1237 /* If we have had a constant bit rate (so far), use it directly, as it
1238 * may give slightly more accurate results than the base class. */
1239 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1240 *bytepos = gst_util_uint64_scale (ts, mp3parse->hdr_bitrate,
1249 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1250 gint64 bytepos, GstClockTime * ts)
1253 GstClockTime total_time;
1255 /* If XING seek table exists use this for byte->time conversion */
1256 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1257 (total_bytes = mp3parse->xing_bytes) &&
1258 (total_time = mp3parse->xing_total_time)) {
1263 pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1264 index = CLAMP (pos, 0, 255);
1265 fa = mp3parse->xing_seek_table_inverse[index];
1267 fb = mp3parse->xing_seek_table_inverse[index + 1];
1271 fx = fa + (fb - fa) * (pos - index);
1273 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1278 if (mp3parse->vbri_seek_table &&
1279 (total_bytes = mp3parse->vbri_bytes) &&
1280 (total_time = mp3parse->vbri_total_time)) {
1283 gdouble a, b, fa, fb;
1286 sum += mp3parse->vbri_seek_table[i];
1288 } while (i + 1 < mp3parse->vbri_seek_points
1289 && sum + mp3parse->vbri_seek_table[i] < bytepos);
1292 a = gst_guint64_to_gdouble (sum);
1293 fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1294 mp3parse->vbri_seek_points));
1296 if (i + 1 < mp3parse->vbri_seek_points) {
1297 b = a + mp3parse->vbri_seek_table[i + 1];
1298 fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1299 mp3parse->vbri_seek_points));
1302 fb = gst_guint64_to_gdouble (total_time);
1305 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1310 /* If we have had a constant bit rate (so far), use it directly, as it
1311 * may give slightly more accurate results than the base class. */
1312 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1313 *ts = gst_util_uint64_scale (bytepos, 8 * GST_SECOND,
1314 mp3parse->hdr_bitrate);
1322 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1323 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1325 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1326 gboolean res = FALSE;
1328 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1330 gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1331 else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1332 res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1333 (GstClockTime *) dest_value);
1335 /* if no tables, fall back to default estimated rate based conversion */
1337 return gst_base_parse_convert_default (parse, src_format, src_value,
1338 dest_format, dest_value);
1343 static GstFlowReturn
1344 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1345 GstBaseParseFrame * frame)
1347 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1348 GstTagList *taglist = NULL;
1350 /* we will create a taglist (if any of the parameters has changed)
1351 * to add the tags that changed */
1352 if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1356 taglist = gst_tag_list_new_empty ();
1358 mp3parse->last_posted_crc = mp3parse->last_crc;
1359 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1364 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1368 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1370 taglist = gst_tag_list_new_empty ();
1372 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1374 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1375 gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1378 /* tag sending done late enough in hook to ensure pending events
1379 * have already been sent */
1380 if (taglist != NULL || !mp3parse->sent_codec_tag) {
1383 if (taglist == NULL)
1384 taglist = gst_tag_list_new_empty ();
1387 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1388 if (G_UNLIKELY (caps == NULL)) {
1389 gst_tag_list_unref (taglist);
1391 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1392 GST_INFO_OBJECT (parse, "Src pad is flushing");
1393 return GST_FLOW_FLUSHING;
1395 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1396 return GST_FLOW_NOT_NEGOTIATED;
1399 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1400 GST_TAG_AUDIO_CODEC, caps);
1401 gst_caps_unref (caps);
1403 if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1404 mp3parse->vbri_bitrate == 0) {
1405 /* We don't have a VBR bitrate, so post the available bitrate as
1406 * nominal and let baseparse calculate the real bitrate */
1407 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1408 GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1411 /* also signals the end of first-frame processing */
1412 mp3parse->sent_codec_tag = TRUE;
1415 /* if the taglist exists, we need to update it so it gets sent out */
1417 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1418 gst_tag_list_unref (taglist);
1421 /* usual clipping applies */
1422 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1428 remove_fields (GstCaps * caps)
1432 n = gst_caps_get_size (caps);
1433 for (i = 0; i < n; i++) {
1434 GstStructure *s = gst_caps_get_structure (caps, i);
1436 gst_structure_remove_field (s, "parsed");
1441 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1443 GstCaps *peercaps, *templ;
1446 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1448 GstCaps *fcopy = gst_caps_copy (filter);
1449 /* Remove the fields we convert */
1450 remove_fields (fcopy);
1451 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1452 gst_caps_unref (fcopy);
1454 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1457 /* Remove the parsed field */
1458 peercaps = gst_caps_make_writable (peercaps);
1459 remove_fields (peercaps);
1461 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1462 gst_caps_unref (peercaps);
1463 gst_caps_unref (templ);
1469 GstCaps *intersection;
1472 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1473 gst_caps_unref (res);