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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, 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 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>
53 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
54 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
56 #define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
57 #define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
58 #define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
59 #define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
60 #define MPEG_AUDIO_CHANNEL_MODE_MONO 3
62 #define CRC_UNKNOWN -1
63 #define CRC_PROTECTED 0
64 #define CRC_NOT_PROTECTED 1
66 #define XING_FRAMES_FLAG 0x0001
67 #define XING_BYTES_FLAG 0x0002
68 #define XING_TOC_FLAG 0x0004
69 #define XING_VBR_SCALE_FLAG 0x0008
71 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS ("audio/mpeg, "
75 "mpegversion = (int) 1, "
76 "layer = (int) [ 1, 3 ], "
77 "rate = (int) [ 8000, 48000 ], channels = (int) [ 1, 2 ],"
78 "parsed=(boolean) true")
81 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
84 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1, parsed=(boolean)false")
87 static void gst_mpeg_audio_parse_finalize (GObject * object);
89 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
90 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
91 static gboolean gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
92 GstBaseParseFrame * frame, guint * size, gint * skipsize);
93 static GstFlowReturn gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
94 GstBaseParseFrame * frame);
95 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
96 GstBaseParseFrame * frame);
97 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
98 GstFormat src_format, gint64 src_value,
99 GstFormat dest_format, gint64 * dest_value);
101 GST_BOILERPLATE (GstMpegAudioParse, gst_mpeg_audio_parse, GstBaseParse,
102 GST_TYPE_BASE_PARSE);
104 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
105 (gst_mpeg_audio_channel_mode_get_type())
107 static const GEnumValue mpeg_audio_channel_mode[] = {
108 {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
109 {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
110 {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
111 {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
112 {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
117 gst_mpeg_audio_channel_mode_get_type (void)
119 static GType mpeg_audio_channel_mode_type = 0;
121 if (!mpeg_audio_channel_mode_type) {
122 mpeg_audio_channel_mode_type =
123 g_enum_register_static ("GstMpegAudioChannelMode",
124 mpeg_audio_channel_mode);
126 return mpeg_audio_channel_mode_type;
130 gst_mpeg_audio_channel_mode_get_nick (gint mode)
133 for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
134 if (mpeg_audio_channel_mode[i].value == mode)
135 return mpeg_audio_channel_mode[i].value_nick;
141 gst_mpeg_audio_parse_base_init (gpointer klass)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
145 gst_element_class_add_pad_template (element_class,
146 gst_static_pad_template_get (&sink_template));
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&src_template));
150 gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
151 "Codec/Parser/Audio",
152 "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
153 "Jan Schmidt <thaytan@mad.scientist.com>,"
154 "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
158 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
160 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
161 GObjectClass *object_class = G_OBJECT_CLASS (klass);
163 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
164 "MPEG1 audio stream parser");
166 object_class->finalize = gst_mpeg_audio_parse_finalize;
168 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
169 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
170 parse_class->check_valid_frame =
171 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_check_valid_frame);
172 parse_class->parse_frame =
173 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_parse_frame);
174 parse_class->pre_push_frame =
175 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
176 parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
179 #define GST_TAG_CRC "has-crc"
180 #define GST_TAG_MODE "channel-mode"
182 gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
183 "has crc", "Using CRC", NULL);
184 gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
185 "channel mode", "MPEG audio channel mode", NULL);
187 g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
191 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
193 mp3parse->channels = -1;
195 mp3parse->sent_codec_tag = FALSE;
196 mp3parse->last_posted_crc = CRC_UNKNOWN;
197 mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
199 mp3parse->hdr_bitrate = 0;
201 mp3parse->xing_flags = 0;
202 mp3parse->xing_bitrate = 0;
203 mp3parse->xing_frames = 0;
204 mp3parse->xing_total_time = 0;
205 mp3parse->xing_bytes = 0;
206 mp3parse->xing_vbr_scale = 0;
207 memset (mp3parse->xing_seek_table, 0, 100);
208 memset (mp3parse->xing_seek_table_inverse, 0, 256);
210 mp3parse->vbri_bitrate = 0;
211 mp3parse->vbri_frames = 0;
212 mp3parse->vbri_total_time = 0;
213 mp3parse->vbri_bytes = 0;
214 mp3parse->vbri_seek_points = 0;
215 g_free (mp3parse->vbri_seek_table);
216 mp3parse->vbri_seek_table = NULL;
218 mp3parse->encoder_delay = 0;
219 mp3parse->encoder_padding = 0;
223 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse,
224 GstMpegAudioParseClass * klass)
226 gst_mpeg_audio_parse_reset (mp3parse);
230 gst_mpeg_audio_parse_finalize (GObject * object)
232 G_OBJECT_CLASS (parent_class)->finalize (object);
236 gst_mpeg_audio_parse_start (GstBaseParse * parse)
238 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
240 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), 1024);
241 GST_DEBUG_OBJECT (parse, "starting");
243 gst_mpeg_audio_parse_reset (mp3parse);
249 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
251 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
253 GST_DEBUG_OBJECT (parse, "stopping");
255 gst_mpeg_audio_parse_reset (mp3parse);
260 static const guint mp3types_bitrates[2][3][16] = {
262 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
263 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
264 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
267 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
268 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
269 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
273 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
274 {22050, 24000, 16000},
279 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
280 guint * put_version, guint * put_layer, guint * put_channels,
281 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
285 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
289 if (header & (1 << 20)) {
290 lsf = (header & (1 << 19)) ? 0 : 1;
297 version = 1 + lsf + mpg25;
299 layer = 4 - ((header >> 17) & 0x3);
301 crc = (header >> 16) & 0x1;
303 bitrate = (header >> 12) & 0xF;
304 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
305 /* The caller has ensured we have a valid header, so bitrate can't be
307 g_assert (bitrate != 0);
309 samplerate = (header >> 10) & 0x3;
310 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
312 padding = (header >> 9) & 0x1;
314 mode = (header >> 6) & 0x3;
315 channels = (mode == 3) ? 1 : 2;
319 length = 4 * ((bitrate * 12) / samplerate + padding);
322 length = (bitrate * 144) / samplerate + padding;
326 length = (bitrate * 144) / (samplerate << lsf) + padding;
330 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
332 GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
333 "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
334 layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
337 *put_version = version;
341 *put_channels = channels;
343 *put_bitrate = bitrate;
345 *put_samplerate = samplerate;
354 /* Minimum number of consecutive, valid-looking frames to consider
356 #define MIN_RESYNC_FRAMES 3
358 /* Perform extended validation to check that subsequent headers match
359 * the first header given here in important characteristics, to avoid
360 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
361 * frames to match their major characteristics.
363 * If at_eos is set to TRUE, we just check that we don't find any invalid
364 * frames in whatever data is available, rather than requiring a full
365 * MIN_RESYNC_FRAMES of data.
367 * Returns TRUE if we've seen enough data to validate or reject the frame.
368 * If TRUE is returned, then *valid contains TRUE if it validated, or false
369 * if we decided it was false sync.
370 * If FALSE is returned, then *valid contains minimum needed data.
373 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
374 guint32 header, int bpf, gboolean at_eos, gint * valid)
379 int frames_found = 1;
382 available = GST_BUFFER_SIZE (buf);
383 data = GST_BUFFER_DATA (buf);
385 while (frames_found < MIN_RESYNC_FRAMES) {
386 /* Check if we have enough data for all these frames, plus the next
388 if (available < offset + 4) {
390 /* Running out of data at EOS is fine; just accept it */
399 next_header = GST_READ_UINT32_BE (data + offset);
400 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
401 offset, (unsigned int) header, (unsigned int) next_header, bpf);
403 /* mask the bits which are allowed to differ between frames */
404 #define HDRMASK ~((0xF << 12) /* bitrate */ | \
405 (0x1 << 9) /* padding */ | \
406 (0xf << 4) /* mode|mode extension */ | \
407 (0xf)) /* copyright|emphasis */
409 if ((next_header & HDRMASK) != (header & HDRMASK)) {
410 /* If any of the unmasked bits don't match, then it's not valid */
411 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
412 "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
413 (guint) header, (guint) header & HDRMASK, (guint) next_header,
414 (guint) next_header & HDRMASK, bpf);
417 } else if ((((next_header >> 12) & 0xf) == 0) ||
418 (((next_header >> 12) & 0xf) == 0xf)) {
419 /* The essential parts were the same, but the bitrate held an
420 invalid value - also reject */
421 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
426 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
427 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
438 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
441 GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
442 /* if it's not a valid sync */
443 if ((head & 0xffe00000) != 0xffe00000) {
444 GST_WARNING_OBJECT (mp3parse, "invalid sync");
447 /* if it's an invalid MPEG version */
448 if (((head >> 19) & 3) == 0x1) {
449 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
453 /* if it's an invalid layer */
454 if (!((head >> 17) & 3)) {
455 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
458 /* if it's an invalid bitrate */
459 if (((head >> 12) & 0xf) == 0x0) {
460 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx."
461 "Free format files are not supported yet", (head >> 12) & 0xf);
464 if (((head >> 12) & 0xf) == 0xf) {
465 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
468 /* if it's an invalid samplerate */
469 if (((head >> 10) & 0x3) == 0x3) {
470 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
475 if ((head & 0x3) == 0x2) {
476 /* Ignore this as there are some files with emphasis 0x2 that can
477 * be played fine. See BGO #537235 */
478 GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
485 gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
486 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
488 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
489 GstBuffer *buf = frame->buffer;
490 GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf);
492 gboolean lost_sync, draining, valid, caps_change;
494 guint bitrate, layer, rate, channels, version, mode, crc;
496 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6))
499 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
500 0, GST_BUFFER_SIZE (buf));
502 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
504 /* didn't find anything that looks like a sync word, skip */
506 *skipsize = GST_BUFFER_SIZE (buf) - 3;
510 /* possible frame header, but not at offset 0? skip bytes before sync */
516 /* make sure the values in the frame header look sane */
517 header = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf));
518 if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
523 GST_LOG_OBJECT (parse, "got frame");
525 bpf = mp3_type_frame_length_from_header (mp3parse, header,
526 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
529 if (channels != mp3parse->channels || rate != mp3parse->rate ||
530 layer != mp3parse->layer || version != mp3parse->version)
535 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
536 draining = GST_BASE_PARSE_DRAINING (parse);
538 if (!draining && (lost_sync || caps_change)) {
539 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
541 /* not enough data */
542 gst_base_parse_set_min_frame_size (parse, valid);
551 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
552 /* avoid caps jitter that we can't be sure of */
562 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
565 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
566 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
567 const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */
568 const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */
571 gint64 upstream_total_bytes = 0;
572 GstFormat fmt = GST_FORMAT_BYTES;
577 if (mp3parse->sent_codec_tag)
580 /* Check first frame for Xing info */
581 if (mp3parse->version == 1) { /* MPEG-1 file */
582 if (mp3parse->channels == 1)
586 } else { /* MPEG-2 header */
587 if (mp3parse->channels == 1)
592 /* Skip the 4 bytes of the MP3 header too */
595 /* Check if we have enough data to read the Xing header */
596 avail = GST_BUFFER_SIZE (buf);
597 data = GST_BUFFER_DATA (buf);
598 if (avail < offset + 8)
601 /* The header starts at the provided offset */
604 /* obtain real upstream total bytes */
605 fmt = GST_FORMAT_BYTES;
606 if (!gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE
607 (mp3parse)), &fmt, &upstream_total_bytes))
608 upstream_total_bytes = 0;
610 read_id = GST_READ_UINT32_BE (data);
611 if (read_id == xing_id || read_id == info_id) {
613 guint bytes_needed = offset + 8;
615 GstClockTime total_time;
617 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
619 /* Read 4 base bytes of flags, big-endian */
620 xing_flags = GST_READ_UINT32_BE (data + 4);
621 if (xing_flags & XING_FRAMES_FLAG)
623 if (xing_flags & XING_BYTES_FLAG)
625 if (xing_flags & XING_TOC_FLAG)
627 if (xing_flags & XING_VBR_SCALE_FLAG)
629 if (avail < bytes_needed) {
630 GST_DEBUG_OBJECT (mp3parse,
631 "Not enough data to read Xing header (need %d)", bytes_needed);
635 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
636 mp3parse->xing_flags = xing_flags;
638 data = GST_BUFFER_DATA (buf);
641 if (xing_flags & XING_FRAMES_FLAG) {
642 mp3parse->xing_frames = GST_READ_UINT32_BE (data);
643 if (mp3parse->xing_frames == 0) {
644 GST_WARNING_OBJECT (mp3parse,
645 "Invalid number of frames in Xing header");
646 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
648 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
649 (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
655 mp3parse->xing_frames = 0;
656 mp3parse->xing_total_time = 0;
659 if (xing_flags & XING_BYTES_FLAG) {
660 mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
661 if (mp3parse->xing_bytes == 0) {
662 GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
663 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
667 mp3parse->xing_bytes = 0;
670 /* If we know the upstream size and duration, compute the
671 * total bitrate, rounded up to the nearest kbit/sec */
672 if ((total_time = mp3parse->xing_total_time) &&
673 (total_bytes = mp3parse->xing_bytes)) {
674 mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
675 8 * GST_SECOND, total_time);
676 mp3parse->xing_bitrate += 500;
677 mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
680 if (xing_flags & XING_TOC_FLAG) {
682 guchar *table = mp3parse->xing_seek_table;
687 GST_DEBUG_OBJECT (mp3parse,
688 "Subtracting initial offset of %d bytes from Xing TOC", first);
690 /* xing seek table: percent time -> 1/256 bytepos */
691 for (i = 0; i < 100; i++) {
692 new = data[i] - first;
694 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
695 mp3parse->xing_flags &= ~XING_TOC_FLAG;
698 mp3parse->xing_seek_table[i] = old = new;
701 /* build inverse table: 1/256 bytepos -> 1/100 percent time */
702 for (i = 0; i < 256; i++) {
703 while (percent < 99 && table[percent + 1] <= i)
706 if (table[percent] == i) {
707 mp3parse->xing_seek_table_inverse[i] = percent * 100;
708 } else if (table[percent] < i && percent < 99) {
710 gint a = percent, b = percent + 1;
714 fx = (b - a) / (fb - fa) * (i - fa) + a;
715 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
716 } else if (percent == 99) {
718 gint a = percent, b = 100;
722 fx = (b - a) / (fb - fa) * (i - fa) + a;
723 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
729 memset (mp3parse->xing_seek_table, 0, 100);
730 memset (mp3parse->xing_seek_table_inverse, 0, 256);
733 if (xing_flags & XING_VBR_SCALE_FLAG) {
734 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
737 mp3parse->xing_vbr_scale = 0;
739 GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
740 GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
741 GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
742 mp3parse->xing_vbr_scale);
744 /* check for truncated file */
745 if (upstream_total_bytes && mp3parse->xing_bytes &&
746 mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
747 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
748 "invalidating Xing header duration and size");
749 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
750 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
753 /* Optional LAME tag? */
754 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
755 gchar lame_version[10] = { 0, };
757 guint32 encoder_delay, encoder_padding;
759 memcpy (lame_version, data, 9);
761 tag_rev = data[0] >> 4;
762 GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
763 tag_rev, lame_version);
765 /* Skip all the information we're not interested in */
767 /* Encoder delay and end padding */
768 encoder_delay = GST_READ_UINT24_BE (data);
769 encoder_delay >>= 12;
770 encoder_padding = GST_READ_UINT24_BE (data);
771 encoder_padding &= 0x000fff;
773 mp3parse->encoder_delay = encoder_delay;
774 mp3parse->encoder_padding = encoder_padding;
776 GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
777 encoder_delay, encoder_padding);
779 } else if (read_id == vbri_id) {
780 gint64 total_bytes, total_frames;
781 GstClockTime total_time;
782 guint16 nseek_points;
784 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
785 if (avail < offset + 26) {
786 GST_DEBUG_OBJECT (mp3parse,
787 "Not enough data to read VBRI header (need %d)", offset + 26);
791 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
792 data = GST_BUFFER_DATA (buf);
795 if (GST_READ_UINT16_BE (data) != 0x0001) {
796 GST_WARNING_OBJECT (mp3parse,
797 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
802 /* Skip encoder delay */
808 total_bytes = GST_READ_UINT32_BE (data);
809 if (total_bytes != 0)
810 mp3parse->vbri_bytes = total_bytes;
813 total_frames = GST_READ_UINT32_BE (data);
814 if (total_frames != 0) {
815 mp3parse->vbri_frames = total_frames;
816 mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
817 (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
821 /* If we know the upstream size and duration, compute the
822 * total bitrate, rounded up to the nearest kbit/sec */
823 if ((total_time = mp3parse->vbri_total_time) &&
824 (total_bytes = mp3parse->vbri_bytes)) {
825 mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
826 8 * GST_SECOND, total_time);
827 mp3parse->vbri_bitrate += 500;
828 mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
831 nseek_points = GST_READ_UINT16_BE (data);
834 if (nseek_points > 0) {
835 guint scale, seek_bytes, seek_frames;
838 mp3parse->vbri_seek_points = nseek_points;
840 scale = GST_READ_UINT16_BE (data);
843 seek_bytes = GST_READ_UINT16_BE (data);
846 seek_frames = GST_READ_UINT16_BE (data);
848 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
849 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
853 if (avail < offset + 26 + nseek_points * seek_bytes) {
854 GST_WARNING_OBJECT (mp3parse,
855 "Not enough data to read VBRI seek table (need %d)",
856 offset + 26 + nseek_points * seek_bytes);
860 if (seek_frames * nseek_points < total_frames - seek_frames ||
861 seek_frames * nseek_points > total_frames + seek_frames) {
862 GST_WARNING_OBJECT (mp3parse,
863 "VBRI seek table doesn't cover the complete file");
867 if (avail < offset + 26) {
868 GST_DEBUG_OBJECT (mp3parse,
869 "Not enough data to read VBRI header (need %d)",
870 offset + 26 + nseek_points * seek_bytes);
874 data = GST_BUFFER_DATA (buf);
877 /* VBRI seek table: frame/seek_frames -> byte */
878 mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
880 for (i = 0; i < nseek_points; i++) {
881 mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
883 } else if (seek_bytes == 3)
884 for (i = 0; i < nseek_points; i++) {
885 mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
887 } else if (seek_bytes == 2)
888 for (i = 0; i < nseek_points; i++) {
889 mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
891 } else /* seek_bytes == 1 */
892 for (i = 0; i < nseek_points; i++) {
893 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
899 GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
900 GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
901 GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
903 /* check for truncated file */
904 if (upstream_total_bytes && mp3parse->vbri_bytes &&
905 mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
906 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
907 "invalidating VBRI header duration and size");
908 mp3parse->vbri_valid = FALSE;
910 mp3parse->vbri_valid = TRUE;
913 GST_DEBUG_OBJECT (mp3parse,
914 "Xing, LAME or VBRI header not found in first frame");
917 /* set duration if tables provided a valid one */
918 if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
919 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
920 mp3parse->xing_total_time, 0);
922 if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
923 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
924 mp3parse->vbri_total_time, 0);
927 /* tell baseclass how nicely we can seek, and a bitrate if one found */
928 /* FIXME: fill index with seek table */
930 seekable = GST_BASE_PARSE_SEEK_DEFAULT;
931 if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
932 mp3parse->xing_total_time)
933 seekable = GST_BASE_PARSE_SEEK_TABLE;
935 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
936 mp3parse->vbri_total_time)
937 seekable = GST_BASE_PARSE_SEEK_TABLE;
940 if (mp3parse->xing_bitrate)
941 bitrate = mp3parse->xing_bitrate;
942 else if (mp3parse->vbri_bitrate)
943 bitrate = mp3parse->vbri_bitrate;
947 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
951 gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
952 GstBaseParseFrame * frame)
954 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
955 GstBuffer *buf = frame->buffer;
956 guint bitrate, layer, rate, channels, version, mode, crc;
958 g_return_val_if_fail (GST_BUFFER_SIZE (buf) >= 4, GST_FLOW_ERROR);
960 if (!mp3_type_frame_length_from_header (mp3parse,
961 GST_READ_UINT32_BE (GST_BUFFER_DATA (buf)),
962 &version, &layer, &channels, &bitrate, &rate, &mode, &crc))
965 if (G_UNLIKELY (channels != mp3parse->channels || rate != mp3parse->rate ||
966 layer != mp3parse->layer || version != mp3parse->version)) {
967 GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
968 "mpegversion", G_TYPE_INT, 1,
969 "mpegaudioversion", G_TYPE_INT, version,
970 "layer", G_TYPE_INT, layer,
971 "rate", G_TYPE_INT, rate,
972 "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
973 gst_buffer_set_caps (buf, caps);
974 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
975 gst_caps_unref (caps);
977 mp3parse->rate = rate;
978 mp3parse->channels = channels;
979 mp3parse->layer = layer;
980 mp3parse->version = version;
982 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
983 if (mp3parse->layer == 1)
985 else if (mp3parse->layer == 2)
986 mp3parse->spf = 1152;
987 else if (mp3parse->version == 1) {
988 mp3parse->spf = 1152;
990 /* MPEG-2 or "2.5" */
995 * We start pushing 9 frames earlier (29 frames for MPEG2) than
996 * segment start to be able to decode the first frame we want.
997 * 9 (29) frames are the theoretical maximum of frames that contain
998 * data for the current frame (bit reservoir).
1001 * Some mp3 streams have an offset in the timestamps, for which we have to
1002 * push the frame *after* the end position in order for the decoder to be
1003 * able to decode everything up until the segment.stop position. */
1004 gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
1005 (version == 1) ? 10 : 30, 2);
1008 mp3parse->hdr_bitrate = bitrate;
1010 /* For first frame; check for seek tables and output a codec tag */
1011 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
1013 /* store some frame info for later processing */
1014 mp3parse->last_crc = crc;
1015 mp3parse->last_mode = mode;
1022 /* this really shouldn't ever happen */
1023 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
1024 return GST_FLOW_ERROR;
1029 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1030 GstClockTime ts, gint64 * bytepos)
1033 GstClockTime total_time;
1035 /* If XING seek table exists use this for time->byte conversion */
1036 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1037 (total_bytes = mp3parse->xing_bytes) &&
1038 (total_time = mp3parse->xing_total_time)) {
1041 CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1042 gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1043 gint index = CLAMP (percent, 0, 99);
1045 fa = mp3parse->xing_seek_table[index];
1047 fb = mp3parse->xing_seek_table[index + 1];
1051 fx = fa + (fb - fa) * (percent - index);
1053 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1058 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1059 (total_time = mp3parse->vbri_total_time)) {
1061 gdouble a, b, fa, fb;
1063 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1064 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1066 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1067 mp3parse->vbri_seek_points));
1069 for (j = i; j >= 0; j--)
1070 fa += mp3parse->vbri_seek_table[j];
1072 if (i + 1 < mp3parse->vbri_seek_points) {
1073 b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1074 mp3parse->vbri_seek_points));
1075 fb = fa + mp3parse->vbri_seek_table[i + 1];
1077 b = gst_guint64_to_gdouble (total_time);
1081 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1090 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1091 gint64 bytepos, GstClockTime * ts)
1094 GstClockTime total_time;
1096 /* If XING seek table exists use this for byte->time conversion */
1097 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1098 (total_bytes = mp3parse->xing_bytes) &&
1099 (total_time = mp3parse->xing_total_time)) {
1104 pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1105 index = CLAMP (pos, 0, 255);
1106 fa = mp3parse->xing_seek_table_inverse[index];
1108 fb = mp3parse->xing_seek_table_inverse[index + 1];
1112 fx = fa + (fb - fa) * (pos - index);
1114 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1119 if (mp3parse->vbri_seek_table &&
1120 (total_bytes = mp3parse->vbri_bytes) &&
1121 (total_time = mp3parse->vbri_total_time)) {
1124 gdouble a, b, fa, fb;
1127 sum += mp3parse->vbri_seek_table[i];
1129 } while (i + 1 < mp3parse->vbri_seek_points
1130 && sum + mp3parse->vbri_seek_table[i] < bytepos);
1133 a = gst_guint64_to_gdouble (sum);
1134 fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1135 mp3parse->vbri_seek_points));
1137 if (i + 1 < mp3parse->vbri_seek_points) {
1138 b = a + mp3parse->vbri_seek_table[i + 1];
1139 fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1140 mp3parse->vbri_seek_points));
1143 fb = gst_guint64_to_gdouble (total_time);
1146 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1155 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1156 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1158 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1159 gboolean res = FALSE;
1161 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1163 gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1164 else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1165 res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1166 (GstClockTime *) dest_value);
1168 /* if no tables, fall back to default estimated rate based conversion */
1170 return gst_base_parse_convert_default (parse, src_format, src_value,
1171 dest_format, dest_value);
1176 static GstFlowReturn
1177 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1178 GstBaseParseFrame * frame)
1180 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1181 GstTagList *taglist;
1183 /* tag sending done late enough in hook to ensure pending events
1184 * have already been sent */
1186 if (!mp3parse->sent_codec_tag) {
1190 if (mp3parse->layer == 3) {
1191 codec = g_strdup_printf ("MPEG %d Audio, Layer %d (MP3)",
1192 mp3parse->version, mp3parse->layer);
1194 codec = g_strdup_printf ("MPEG %d Audio, Layer %d",
1195 mp3parse->version, mp3parse->layer);
1197 taglist = gst_tag_list_new ();
1198 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1199 GST_TAG_AUDIO_CODEC, codec, NULL);
1200 if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1201 mp3parse->vbri_bitrate == 0) {
1202 /* We don't have a VBR bitrate, so post the available bitrate as
1203 * nominal and let baseparse calculate the real bitrate */
1204 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1205 GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1207 gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse),
1208 GST_BASE_PARSE_SRC_PAD (mp3parse), taglist);
1211 /* also signals the end of first-frame processing */
1212 mp3parse->sent_codec_tag = TRUE;
1215 /* we will create a taglist (if any of the parameters has changed)
1216 * to add the tags that changed */
1218 if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1222 taglist = gst_tag_list_new ();
1224 mp3parse->last_posted_crc = mp3parse->last_crc;
1225 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1230 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1234 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1236 taglist = gst_tag_list_new ();
1238 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1240 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1241 gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1244 /* if the taglist exists, we need to send it */
1246 gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse),
1247 GST_BASE_PARSE_SRC_PAD (mp3parse), taglist);
1250 /* usual clipping applies */
1251 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;