Debian Codegraph @

gst/audioparsers/gstmpegaudioparse.c

raw
1 /* GStreamer MPEG audio parser
2 * Copyright (C) 2006-2007 Jan Schmidt <[email protected]>
3 * Copyright (C) 2010 Mark Nauwelaerts <mnauw users sf net>
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <[email protected]>
6 *
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.
11 *
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.
16 *
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.
21 */
22 /**
23 * SECTION:element-mpegaudioparse
24 * @title: mpegaudioparse
25 * @short_description: MPEG audio parser
26 * @see_also: #GstAmrParse, #GstAACParse
27 *
28 * Parses and frames mpeg1 audio streams. Provides seeking.
29 *
30 * ## Example launch line
31 * |[
32 * gst-launch-1.0 filesrc location=test.mp3 ! mpegaudioparse ! mpg123audiodec
33 * ! audioconvert ! audioresample ! autoaudiosink
34 * ]|
35 *
36 */
37
38 /* Notes about gapless playback, "Frankenstein" streams, and the Xing header frame:
39 *
40 * Gapless playback is based on the LAME tag, which is located in the Xing
41 * header frame. The tag contains the encoder delay and encoder padding.
42 * The encoder delay specifies how many padding nullsamples have been prepended
43 * by the encoder at the start of the mp3 stream, while the encoder padding
44 * specifies how many padding nullsamples got added at the end of the stream.
45 *
46 * In addition, there is also a "decoder delay". This affects all existing
47 * mp3 decoders - they themselves introduce a delay into the signal due to
48 * the way mp3 decoding works. This delay is 529 samples long in all known
49 * decoders. Unlike the encoder delay, the decoder delay is not specified
50 * anywhere in the mp3 stream. Players/decoders therefore hardcode the
51 * decoder delay as 529 samples.
52 *
53 * (The LAME tech FAQ mentions 528 samples instead of 529, but LAME seems to
54 * use 529 samples. Also, decoders like mpg123 use 529 samples instead of 528.
55 * The situation is a little unclear, but 529 samples seems to be standard.)
56 *
57 * For proper gapless playback, both mpegaudioparse and a downstream MPEG
58 * audio decoder must do their part. mpegaudioparse adjusts buffer PTS/DTS
59 * and durations, and adds GstAudioClippingMeta to outgoing buffers if
60 * clipping is necessary. MPEG decoders then clip decoded frames according
61 * to that meta (if present).
62 *
63 * To detect when to add GstAudioClippingMeta and when to adjust PTS/DTS/
64 * durations, the number of the current frame is retrieved. Based on that, the
65 * current stream position in samples is calculated. With the sample position,
66 * it is determined whether or not the current playback position is still
67 * if the actual playback range (= in the actual playback range of the stream
68 * that excludes padding samples), or if it is already outside, or partially
69 * outside.
70 *
71 * start_of_actual_samples and end_of_actual_samples define the start/end
72 * of this actual playback range, in samples. So:
73 * If sample_pos >= start_of_actual_samples and sample_pos end_of_actual_samples
74 * -> sample_pos is inside the actual playback range.
75 *
76 * (The decoder delay could in theory be left for the decoder to worry
77 * about. But then, the decoder would also have to adjust PTS/DTS/durations
78 * of decoded buffers, which is not something a GstAudioDecoder based element
79 * should have to deal with. So, for convenience, mpegaudioparse also factors
80 * that delay into its calculations.)
81 *
82 *
83 * "Frankenstein" streams are MPEG streams which have streams beyond
84 * what the Xing metadata indicates. Such streams typically are the
85 * result of poorly stitching individual mp3s together, like this:
86 *
87 * cat first.mp3 second.mp3 > joined.mp3
88 *
89 * The resulting mp3 is not guaranteed to be valid. In particular, this can
90 * cause confusion when first.mp3 contains a Xing header frame. Its length
91 * indicator then does not match the actual length (which is bigger). When
92 * this is detected, a log line about this being a Frankenstein stream is
93 * generated.
94 *
95 *
96 * Xing header frames are empty dummy MPEG frames. They only exist for
97 * supplying metadata. They are encoded as valid silent MPEG frames for
98 * backwards compatibility with older hardware MP3 players, but can be safely
99 * dropped.
100 *
101 * For more about Xing header frames, see:
102 * https://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header#XINGHeader
103 * https://www.compuphase.com/mp3/mp3loops.htm#PADDING_DELAYS
104 *
105 * To facilitate gapless playback and ensure that MPEG audio decoders don't
106 * actually decode this frame as an empty MPEG frame, it is marked here as
107 * GST_BUFFER_FLAG_DECODE_ONLY / GST_BUFFER_FLAG_DROPPABLE in mpegaudioparse
108 * after its metadata got extracted. It is also marked as such if it is
109 * encountered again after the user for example seeked back to the beginning
110 * of the mp3 stream. Its duration is also set to zero to make sure that the
111 * frame does not cause baseparse to increment the timestamp of the frame that
112 * follows this one.
113 *
114 */
115
116 /* FIXME: we should make the base class (GstBaseParse) aware of the
117 * XING seek table somehow, so it can use it properly for things like
118 * accurate seeks. Currently it can only do a lookup via the convert function,
119 * but then doesn't know what the result represents exactly. One could either
120 * add a vfunc for index lookup, or just make mpegaudioparse populate the
121 * base class's index via the API provided.
122 */
123 #ifdef HAVE_CONFIG_H
124 #include "config.h"
125 #endif
126
127 #include <string.h>
128
129 #include "gstaudioparserselements.h"
130 #include "gstmpegaudioparse.h"
131 #include <gst/base/gstbytereader.h>
132 #include <gst/pbutils/pbutils.h>
133
134 GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
135 #define GST_CAT_DEFAULT mpeg_audio_parse_debug
136
137 #define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
138 #define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
139 #define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
140 #define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
141 #define MPEG_AUDIO_CHANNEL_MODE_MONO 3
142
143 #define CRC_UNKNOWN -1
144 #define CRC_PROTECTED 0
145 #define CRC_NOT_PROTECTED 1
146
147 #define XING_FRAMES_FLAG 0x0001
148 #define XING_BYTES_FLAG 0x0002
149 #define XING_TOC_FLAG 0x0004
150 #define XING_VBR_SCALE_FLAG 0x0008
151
152 #define MIN_FRAME_SIZE 6
153
154 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
155 GST_PAD_SRC,
156 GST_PAD_ALWAYS,
157 GST_STATIC_CAPS ("audio/mpeg, "
158 "mpegversion = (int) 1, "
159 "layer = (int) [ 1, 3 ], "
160 "mpegaudioversion = (int) [ 1, 3], "
161 "rate = (int) [ 8000, 48000 ], "
162 "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
163 );
164
165 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
166 GST_PAD_SINK,
167 GST_PAD_ALWAYS,
168 GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
169 );
170
171 static void gst_mpeg_audio_parse_finalize (GObject * object);
172
173 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
174 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
175 static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
176 GstBaseParseFrame * frame, gint * skipsize);
177 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
178 GstBaseParseFrame * frame);
179 static gboolean gst_mpeg_audio_parse_src_query (GstBaseParse * parse,
180 GstQuery * query);
181 static gboolean gst_mpeg_audio_parse_sink_event (GstBaseParse * parse,
182 GstEvent * event);
183 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
184 GstFormat src_format, gint64 src_value,
185 GstFormat dest_format, gint64 * dest_value);
186 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
187 GstCaps * filter);
188
189 static gboolean
190 gst_mpeg_audio_parse_check_if_is_xing_header_frame (GstMpegAudioParse *
191 mp3parse, GstBuffer * buf);
192
193 static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
194 mp3parse, GstBuffer * buf);
195
196 #define gst_mpeg_audio_parse_parent_class parent_class
197 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
198 GST_ELEMENT_REGISTER_DEFINE (mpegaudioparse, "mpegaudioparse",
199 GST_RANK_PRIMARY + 2, GST_TYPE_MPEG_AUDIO_PARSE);
200
201 #define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \
202 (gst_mpeg_audio_channel_mode_get_type())
203
204 static const GEnumValue mpeg_audio_channel_mode[] = {
205 {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
206 {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
207 {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
208 {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
209 {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
210 {0, NULL, NULL},
211 };
212
213 static GType
214 gst_mpeg_audio_channel_mode_get_type (void)
215 {
216 static GType mpeg_audio_channel_mode_type = 0;
217
218 if (!mpeg_audio_channel_mode_type) {
219 mpeg_audio_channel_mode_type =
220 g_enum_register_static ("GstMpegAudioChannelMode",
221 mpeg_audio_channel_mode);
222 }
223 return mpeg_audio_channel_mode_type;
224 }
225
226 static const gchar *
227 gst_mpeg_audio_channel_mode_get_nick (gint mode)
228 {
229 guint i;
230 for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
231 if (mpeg_audio_channel_mode[i].value == mode)
232 return mpeg_audio_channel_mode[i].value_nick;
233 }
234 return NULL;
235 }
236
237 static void
238 gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
239 {
240 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
241 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
242 GObjectClass *object_class = G_OBJECT_CLASS (klass);
243
244 GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
245 "MPEG1 audio stream parser");
246
247 object_class->finalize = gst_mpeg_audio_parse_finalize;
248
249 parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
250 parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
251 parse_class->handle_frame =
252 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
253 parse_class->pre_push_frame =
254 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
255 parse_class->src_query = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_src_query);
256 parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_sink_event);
257 parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
258 parse_class->get_sink_caps =
259 GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
260
261 /* register tags */
262 #define GST_TAG_CRC "has-crc"
263 #define GST_TAG_MODE "channel-mode"
264
265 gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
266 "has crc", "Using CRC", NULL);
267 gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
268 "channel mode", "MPEG audio channel mode", NULL);
269
270 g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
271
272 gst_element_class_add_static_pad_template (element_class, &sink_template);
273 gst_element_class_add_static_pad_template (element_class, &src_template);
274
275 gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
276 "Codec/Parser/Audio",
277 "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
278 "Jan Schmidt <[email protected]>,"
279 "Mark Nauwelaerts <[email protected]>");
280 }
281
282 static void
283 gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
284 {
285 mp3parse->upstream_format = GST_FORMAT_UNDEFINED;
286 mp3parse->channels = -1;
287 mp3parse->rate = -1;
288 mp3parse->sent_codec_tag = FALSE;
289 mp3parse->last_posted_crc = CRC_UNKNOWN;
290 mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
291 mp3parse->freerate = 0;
292 mp3parse->spf = 0;
293
294 mp3parse->outgoing_frame_is_xing_header = FALSE;
295
296 mp3parse->hdr_bitrate = 0;
297 mp3parse->bitrate_is_constant = TRUE;
298
299 mp3parse->xing_flags = 0;
300 mp3parse->xing_bitrate = 0;
301 mp3parse->xing_frames = 0;
302 mp3parse->xing_total_time = 0;
303 mp3parse->xing_bytes = 0;
304 mp3parse->xing_vbr_scale = 0;
305 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
306 memset (mp3parse->xing_seek_table_inverse, 0,
307 sizeof (mp3parse->xing_seek_table_inverse));
308
309 mp3parse->vbri_bitrate = 0;
310 mp3parse->vbri_frames = 0;
311 mp3parse->vbri_total_time = 0;
312 mp3parse->vbri_bytes = 0;
313 mp3parse->vbri_seek_points = 0;
314 g_free (mp3parse->vbri_seek_table);
315 mp3parse->vbri_seek_table = NULL;
316
317 mp3parse->encoder_delay = 0;
318 mp3parse->encoder_padding = 0;
319 mp3parse->decoder_delay = 0;
320 mp3parse->start_of_actual_samples = 0;
321 mp3parse->end_of_actual_samples = 0;
322 mp3parse->total_padding_time = GST_CLOCK_TIME_NONE;
323 mp3parse->start_padding_time = GST_CLOCK_TIME_NONE;
324 mp3parse->end_padding_time = GST_CLOCK_TIME_NONE;
325 }
326
327 static void
328 gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
329 {
330 gst_mpeg_audio_parse_reset (mp3parse);
331 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
332 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
333 }
334
335 static void
336 gst_mpeg_audio_parse_finalize (GObject * object)
337 {
338 G_OBJECT_CLASS (parent_class)->finalize (object);
339 }
340
341 static gboolean
342 gst_mpeg_audio_parse_start (GstBaseParse * parse)
343 {
344 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
345
346 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
347 GST_DEBUG_OBJECT (parse, "starting");
348
349 gst_mpeg_audio_parse_reset (mp3parse);
350
351 return TRUE;
352 }
353
354 static gboolean
355 gst_mpeg_audio_parse_stop (GstBaseParse * parse)
356 {
357 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
358
359 GST_DEBUG_OBJECT (parse, "stopping");
360
361 gst_mpeg_audio_parse_reset (mp3parse);
362
363 return TRUE;
364 }
365
366 static const guint mp3types_bitrates[2][3][16] = {
367 {
368 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
369 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
370 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
371 },
372 {
373 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
374 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
375 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
376 },
377 };
378
379 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
380 {22050, 24000, 16000},
381 {11025, 12000, 8000}
382 };
383
384 static inline guint
385 mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
386 guint * put_version, guint * put_layer, guint * put_channels,
387 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
388 guint * put_crc)
389 {
390 guint length;
391 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
392 gulong version;
393 gint lsf, mpg25;
394
395 if (header & (1 << 20)) {
396 lsf = (header & (1 << 19)) ? 0 : 1;
397 mpg25 = 0;
398 } else {
399 lsf = 1;
400 mpg25 = 1;
401 }
402
403 version = 1 + lsf + mpg25;
404
405 layer = 4 - ((header >> 17) & 0x3);
406
407 crc = (header >> 16) & 0x1;
408
409 bitrate = (header >> 12) & 0xF;
410 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
411 if (!bitrate) {
412 GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
413 bitrate = mp3parse->freerate;
414 }
415
416 samplerate = (header >> 10) & 0x3;
417 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
418
419 /* force 0 length if 0 bitrate */
420 padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
421
422 mode = (header >> 6) & 0x3;
423 channels = (mode == 3) ? 1 : 2;
424
425 switch (layer) {
426 case 1:
427 length = 4 * ((bitrate * 12) / samplerate + padding);
428 break;
429 case 2:
430 length = (bitrate * 144) / samplerate + padding;
431 break;
432 default:
433 case 3:
434 length = (bitrate * 144) / (samplerate << lsf) + padding;
435 break;
436 }
437
438 GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
439 length);
440 GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
441 "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
442 layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
443
444 if (put_version)
445 *put_version = version;
446 if (put_layer)
447 *put_layer = layer;
448 if (put_channels)
449 *put_channels = channels;
450 if (put_bitrate)
451 *put_bitrate = bitrate;
452 if (put_samplerate)
453 *put_samplerate = samplerate;
454 if (put_mode)
455 *put_mode = mode;
456 if (put_crc)
457 *put_crc = crc;
458
459 return length;
460 }
461
462 /* Minimum number of consecutive, valid-looking frames to consider
463 * for resyncing */
464 #define MIN_RESYNC_FRAMES 3
465
466 /* Perform extended validation to check that subsequent headers match
467 * the first header given here in important characteristics, to avoid
468 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
469 * frames to match their major characteristics.
470 *
471 * If at_eos is set to TRUE, we just check that we don't find any invalid
472 * frames in whatever data is available, rather than requiring a full
473 * MIN_RESYNC_FRAMES of data.
474 *
475 * Returns TRUE if we've seen enough data to validate or reject the frame.
476 * If TRUE is returned, then *valid contains TRUE if it validated, or false
477 * if we decided it was false sync.
478 * If FALSE is returned, then *valid contains minimum needed data.
479 */
480 static gboolean
481 gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
482 guint32 header, int bpf, gboolean at_eos, gint * valid)
483 {
484 guint32 next_header;
485 GstMapInfo map;
486 gboolean res = TRUE;
487 int frames_found = 1;
488 int offset = bpf;
489
490 gst_buffer_map (buf, &map, GST_MAP_READ);
491
492 while (frames_found < MIN_RESYNC_FRAMES) {
493 /* Check if we have enough data for all these frames, plus the next
494 frame header. */
495 if (map.size < offset + 4) {
496 if (at_eos) {
497 /* Running out of data at EOS is fine; just accept it */
498 *valid = TRUE;
499 goto cleanup;
500 } else {
501 *valid = offset + 4;
502 res = FALSE;
503 goto cleanup;
504 }
505 }
506
507 next_header = GST_READ_UINT32_BE (map.data + offset);
508 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
509 offset, (unsigned int) header, (unsigned int) next_header, bpf);
510
511 /* mask the bits which are allowed to differ between frames */
512 #define HDRMASK ~((0xF << 12) /* bitrate */ | \
513 (0x1 << 9) /* padding */ | \
514 (0xf << 4) /* mode|mode extension */ | \
515 (0xf)) /* copyright|emphasis */
516
517 if ((next_header & HDRMASK) != (header & HDRMASK)) {
518 /* If any of the unmasked bits don't match, then it's not valid */
519 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
520 "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
521 (guint) header, (guint) header & HDRMASK, (guint) next_header,
522 (guint) next_header & HDRMASK, bpf);
523 *valid = FALSE;
524 goto cleanup;
525 } else if (((next_header >> 12) & 0xf) == 0xf) {
526 /* The essential parts were the same, but the bitrate held an
527 invalid value - also reject */
528 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
529 *valid = FALSE;
530 goto cleanup;
531 }
532
533 bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
534 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
535
536 /* if no bitrate, and no freeform rate known, then fail */
537 if (G_UNLIKELY (!bpf)) {
538 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
539 *valid = FALSE;
540 goto cleanup;
541 }
542
543 offset += bpf;
544 frames_found++;
545 }
546
547 *valid = TRUE;
548
549 cleanup:
550 gst_buffer_unmap (buf, &map);
551 return res;
552 }
553
554 static gboolean
555 gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
556 unsigned long head)
557 {
558 GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
559 /* if it's not a valid sync */
560 if ((head & 0xffe00000) != 0xffe00000) {
561 GST_WARNING_OBJECT (mp3parse, "invalid sync");
562 return FALSE;
563 }
564 /* if it's an invalid MPEG version */
565 if (((head >> 19) & 3) == 0x1) {
566 GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
567 (head >> 19) & 3);
568 return FALSE;
569 }
570 /* if it's an invalid layer */
571 if (!((head >> 17) & 3)) {
572 GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
573 return FALSE;
574 }
575 /* if it's an invalid bitrate */
576 if (((head >> 12) & 0xf) == 0xf) {
577 GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
578 return FALSE;
579 }
580 /* if it's an invalid samplerate */
581 if (((head >> 10) & 0x3) == 0x3) {
582 GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
583 (head >> 10) & 0x3);
584 return FALSE;
585 }
586
587 if ((head & 0x3) == 0x2) {
588 /* Ignore this as there are some files with emphasis 0x2 that can
589 * be played fine. See BGO #537235 */
590 GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
591 }
592
593 return TRUE;
594 }
595
596 /* Determines possible freeform frame rate/size by looking for next
597 * header with valid bitrate (0 or otherwise valid) (and sufficiently
598 * matching current header).
599 *
600 * Returns TRUE if we've found such one, and *rate then contains rate
601 * (or *rate contains 0 if decided no freeframe size could be determined).
602 * If not enough data, returns FALSE.
603 */
604 static gboolean
605 gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
606 guint32 header, gboolean at_eos, gint * _rate)
607 {
608 guint32 next_header;
609 const guint8 *data;
610 guint available;
611 int offset = 4;
612 gulong samplerate, rate, layer, padding;
613 gboolean valid;
614 gint lsf, mpg25;
615
616 available = map->size;
617 data = map->data;
618
619 *_rate = 0;
620
621 /* pick apart header again partially */
622 if (header & (1 << 20)) {
623 lsf = (header & (1 << 19)) ? 0 : 1;
624 mpg25 = 0;
625 } else {
626 lsf = 1;
627 mpg25 = 1;
628 }
629 layer = 4 - ((header >> 17) & 0x3);
630 samplerate = (header >> 10) & 0x3;
631 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
632 padding = (header >> 9) & 0x1;
633
634 for (; offset < available; ++offset) {
635 /* Check if we have enough data for all these frames, plus the next
636 frame header. */
637 if (available < offset + 4) {
638 if (at_eos) {
639 /* Running out of data; failed to determine size */
640 return TRUE;
641 } else {
642 return FALSE;
643 }
644 }
645
646 valid = FALSE;
647 next_header = GST_READ_UINT32_BE (data + offset);
648 if ((next_header & 0xFFE00000) != 0xFFE00000)
649 goto next;
650
651 GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
652 offset, (unsigned int) header, (unsigned int) next_header);
653
654 if ((next_header & HDRMASK) != (header & HDRMASK)) {
655 /* If any of the unmasked bits don't match, then it's not valid */
656 GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
657 "(header=%08X (%08X), header2=%08X (%08X))",
658 (guint) header, (guint) header & HDRMASK, (guint) next_header,
659 (guint) next_header & HDRMASK);
660 goto next;
661 } else if (((next_header >> 12) & 0xf) == 0xf) {
662 /* The essential parts were the same, but the bitrate held an
663 invalid value - also reject */
664 GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
665 goto next;
666 }
667
668 valid = TRUE;
669
670 next:
671 /* almost accept as free frame */
672 if (layer == 1) {
673 rate = samplerate * (offset - 4 * padding + 4) / 48000;
674 } else {
675 rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
676 }
677
678 if (valid) {
679 GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
680 if (rate < 8 || (layer == 3 && rate > 640)) {
681 GST_DEBUG_OBJECT (mp3parse, "rate invalid");
682 if (rate < 8) {
683 /* maybe some hope */
684 continue;
685 } else {
686 GST_DEBUG_OBJECT (mp3parse, "aborting");
687 /* give up */
688 break;
689 }
690 }
691 *_rate = rate * 1000;
692 break;
693 } else {
694 /* avoid indefinite searching */
695 if (rate > 1000) {
696 GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
697 break;
698 }
699 }
700 }
701
702 return TRUE;
703 }
704
705 static GstFlowReturn
706 gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
707 GstBaseParseFrame * frame, gint * skipsize)
708 {
709 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
710 GstBuffer *buf = frame->buffer;
711 GstByteReader reader;
712 gint off, bpf = 0;
713 gboolean lost_sync, draining, valid, caps_change;
714 guint32 header;
715 guint bitrate, layer, rate, channels, version, mode, crc;
716 GstMapInfo map;
717 gboolean res = FALSE;
718
719 gst_buffer_map (buf, &map, GST_MAP_READ);
720 if (G_UNLIKELY (map.size < 6)) {
721 *skipsize = 1;
722 goto cleanup;
723 }
724
725 gst_byte_reader_init (&reader, map.data, map.size);
726
727 off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
728 0, map.size);
729
730 GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
731
732 /* didn't find anything that looks like a sync word, skip */
733 if (off < 0) {
734 *skipsize = map.size - 3;
735 goto cleanup;
736 }
737
738 /* possible frame header, but not at offset 0? skip bytes before sync */
739 if (off > 0) {
740 *skipsize = off;
741 goto cleanup;
742 }
743
744 /* make sure the values in the frame header look sane */
745 header = GST_READ_UINT32_BE (map.data);
746 if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
747 *skipsize = 1;
748 goto cleanup;
749 }
750
751 GST_LOG_OBJECT (parse, "got frame");
752
753 lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
754 draining = GST_BASE_PARSE_DRAINING (parse);
755
756 if (G_UNLIKELY (lost_sync))
757 mp3parse->freerate = 0;
758
759 bpf = mp3_type_frame_length_from_header (mp3parse, header,
760 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
761
762 if (channels != mp3parse->channels || rate != mp3parse->rate ||
763 layer != mp3parse->layer || version != mp3parse->version)
764 caps_change = TRUE;
765 else
766 caps_change = FALSE;
767
768 /* maybe free format */
769 if (bpf == 0) {
770 GST_LOG_OBJECT (mp3parse, "possibly free format");
771 if (lost_sync || mp3parse->freerate == 0) {
772 GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
773 if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
774 &valid)) {
775 /* not enough data */
776 gst_base_parse_set_min_frame_size (parse, valid);
777 *skipsize = 0;
778 goto cleanup;
779 } else {
780 GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
781 mp3parse->freerate = valid;
782 }
783 }
784 /* try again */
785 bpf = mp3_type_frame_length_from_header (mp3parse, header,
786 &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
787 if (!bpf) {
788 /* did not come up with valid freeform length, reject after all */
789 *skipsize = 1;
790 goto cleanup;
791 }
792 }
793
794 if (!draining && (lost_sync || caps_change)) {
795 if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
796 &valid)) {
797 /* not enough data */
798 gst_base_parse_set_min_frame_size (parse, valid);
799 *skipsize = 0;
800 goto cleanup;
801 } else {
802 if (!valid) {
803 *skipsize = off + 2;
804 goto cleanup;
805 }
806 }
807 } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
808 /* avoid caps jitter that we can't be sure of */
809 *skipsize = off + 2;
810 goto cleanup;
811 }
812
813 /* restore default minimum */
814 gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
815
816 /* output one frame if we have enough data */
817 res = bpf <= map.size;
818
819 /* metadata handling */
820 if (G_UNLIKELY (caps_change)) {
821 GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
822 "mpegversion", G_TYPE_INT, 1,
823 "mpegaudioversion", G_TYPE_INT, version,
824 "layer", G_TYPE_INT, layer,
825 "rate", G_TYPE_INT, rate,
826 "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
827 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
828 gst_caps_unref (caps);
829
830 mp3parse->rate = rate;
831 mp3parse->channels = channels;
832 mp3parse->layer = layer;
833 mp3parse->version = version;
834
835 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
836 if (mp3parse->layer == 1)
837 mp3parse->spf = 384;
838 else if (mp3parse->layer == 2)
839 mp3parse->spf = 1152;
840 else if (mp3parse->version == 1) {
841 mp3parse->spf = 1152;
842 } else {
843 /* MPEG-2 or "2.5" */
844 mp3parse->spf = 576;
845 }
846
847 /* We need the frame duration for calculating the frame number later
848 * in gst_mpeg_audio_parse_pre_push_frame (). */
849 mp3parse->frame_duration = gst_util_uint64_scale (GST_SECOND,
850 mp3parse->spf, mp3parse->rate);
851
852 /* lead_in:
853 * We start pushing 9 frames earlier (29 frames for MPEG2) than
854 * segment start to be able to decode the first frame we want.
855 * 9 (29) frames are the theoretical maximum of frames that contain
856 * data for the current frame (bit reservoir).
857 *
858 * lead_out:
859 * Some mp3 streams have an offset in the timestamps, for which we have to
860 * push the frame *after* the end position in order for the decoder to be
861 * able to decode everything up until the segment.stop position. */
862 gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
863 (version == 1) ? 10 : 30, 2);
864 }
865
866 if (mp3parse->hdr_bitrate && mp3parse->hdr_bitrate != bitrate) {
867 mp3parse->bitrate_is_constant = FALSE;
868 }
869 mp3parse->hdr_bitrate = bitrate;
870
871 /* While during normal playback, the Xing header frame is seen only once
872 * (right at the beginning), we may see it again if the user seeked back
873 * to the beginning. To make sure it is dropped again and NOT pushed
874 * downstream, we have to check every frame for Xing IDs.
875 *
876 * (sent_codec_tag is TRUE after this Xing frame got parsed.) */
877 if (G_LIKELY (mp3parse->sent_codec_tag)) {
878 if (G_UNLIKELY (gst_mpeg_audio_parse_check_if_is_xing_header_frame
879 (mp3parse, buf))) {
880 GST_DEBUG_OBJECT (mp3parse, "This is a Xing header frame, which "
881 "contains no meaningful audio data, and can be safely dropped");
882 mp3parse->outgoing_frame_is_xing_header = TRUE;
883 }
884 }
885
886 /* For first frame; check for seek tables and output a codec tag */
887 gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
888
889 /* store some frame info for later processing */
890 mp3parse->last_crc = crc;
891 mp3parse->last_mode = mode;
892
893 cleanup:
894 gst_buffer_unmap (buf, &map);
895
896 /* We don't actually drop the frame right here, but rather in
897 * gst_mpeg_audio_parse_pre_push_frame (), since it is still important
898 * to let other code bits do their work there even if we want to drop
899 * the current frame. */
900 if (G_UNLIKELY (mp3parse->outgoing_frame_is_xing_header)) {
901 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
902 /* Set duration to zero to prevent the baseparse class
903 * from incrementing outgoing timestamps */
904 GST_BUFFER_DURATION (frame->buffer) = 0;
905 }
906
907 if (res) {
908 return gst_base_parse_finish_frame (parse, frame, bpf);
909 }
910
911 return GST_FLOW_OK;
912 }
913
914 static gboolean
915 gst_mpeg_audio_parse_check_if_is_xing_header_frame (GstMpegAudioParse *
916 mp3parse, GstBuffer * buf)
917 {
918 /* TODO: get rid of code duplication
919 * (see gst_mpeg_audio_parse_handle_first_frame ()) */
920
921 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
922 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
923
924 gint offset_xing;
925 GstMapInfo map;
926 guint8 *data;
927 guint64 avail;
928 guint32 read_id_xing = 0;
929 gboolean ret = FALSE;
930
931 /* Check first frame for Xing info */
932 if (mp3parse->version == 1) { /* MPEG-1 file */
933 if (mp3parse->channels == 1)
934 offset_xing = 0x11;
935 else
936 offset_xing = 0x20;
937 } else { /* MPEG-2 header */
938 if (mp3parse->channels == 1)
939 offset_xing = 0x09;
940 else
941 offset_xing = 0x11;
942 }
943
944 /* Skip the 4 bytes of the MP3 header too */
945 offset_xing += 4;
946
947 /* Check if we have enough data to read the Xing header */
948 gst_buffer_map (buf, &map, GST_MAP_READ);
949 data = map.data;
950 avail = map.size;
951
952 if (avail >= offset_xing + 4) {
953 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
954 ret = (read_id_xing == xing_id || read_id_xing == info_id);
955 }
956
957 gst_buffer_unmap (buf, &map);
958
959 return ret;
960 }
961
962 static void
963 gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
964 GstBuffer * buf)
965 {
966 const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */
967 const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */
968 const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */
969 const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */
970 gint offset_xing, offset_vbri;
971 guint64 avail;
972 gint64 upstream_total_bytes = 0;
973 guint32 read_id_xing = 0, read_id_vbri = 0;
974 GstMapInfo map;
975 guint8 *data;
976 guint bitrate;
977
978 if (mp3parse->sent_codec_tag)
979 return;
980
981 /* Check first frame for Xing info */
982 if (mp3parse->version == 1) { /* MPEG-1 file */
983 if (mp3parse->channels == 1)
984 offset_xing = 0x11;
985 else
986 offset_xing = 0x20;
987 } else { /* MPEG-2 header */
988 if (mp3parse->channels == 1)
989 offset_xing = 0x09;
990 else
991 offset_xing = 0x11;
992 }
993
994 /* The VBRI tag is always at offset 0x20 */
995 offset_vbri = 0x20;
996
997 /* Skip the 4 bytes of the MP3 header too */
998 offset_xing += 4;
999 offset_vbri += 4;
1000
1001 /* Check if we have enough data to read the Xing header */
1002 gst_buffer_map (buf, &map, GST_MAP_READ);
1003 data = map.data;
1004 avail = map.size;
1005
1006 if (avail >= offset_xing + 4) {
1007 read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
1008 }
1009 if (avail >= offset_vbri + 4) {
1010 read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
1011 }
1012
1013 /* obtain real upstream total bytes */
1014 if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
1015 GST_FORMAT_BYTES, &upstream_total_bytes))
1016 upstream_total_bytes = 0;
1017
1018 if (read_id_xing == xing_id || read_id_xing == info_id) {
1019 guint32 xing_flags;
1020 guint bytes_needed = offset_xing + 8;
1021 gint64 total_bytes;
1022 guint64 num_xing_samples = 0;
1023 GstClockTime total_time;
1024
1025 GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
1026
1027 GST_DEBUG_OBJECT (mp3parse, "This is a Xing header frame, which contains "
1028 "no meaningful audio data, and can be safely dropped");
1029 mp3parse->outgoing_frame_is_xing_header = TRUE;
1030
1031 /* Move data after Xing header */
1032 data += offset_xing + 4;
1033
1034 /* Read 4 base bytes of flags, big-endian */
1035 xing_flags = GST_READ_UINT32_BE (data);
1036 data += 4;
1037 if (xing_flags & XING_FRAMES_FLAG)
1038 bytes_needed += 4;
1039 if (xing_flags & XING_BYTES_FLAG)
1040 bytes_needed += 4;
1041 if (xing_flags & XING_TOC_FLAG)
1042 bytes_needed += 100;
1043 if (xing_flags & XING_VBR_SCALE_FLAG)
1044 bytes_needed += 4;
1045 if (avail < bytes_needed) {
1046 GST_DEBUG_OBJECT (mp3parse,
1047 "Not enough data to read Xing header (need %d)", bytes_needed);
1048 goto cleanup;
1049 }
1050
1051 GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
1052 mp3parse->xing_flags = xing_flags;
1053
1054 if (xing_flags & XING_FRAMES_FLAG) {
1055 mp3parse->xing_frames = GST_READ_UINT32_BE (data);
1056 if (mp3parse->xing_frames == 0) {
1057 GST_WARNING_OBJECT (mp3parse,
1058 "Invalid number of frames in Xing header");
1059 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
1060 } else {
1061 num_xing_samples = (guint64) (mp3parse->xing_frames) * (mp3parse->spf);
1062 mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
1063 num_xing_samples, mp3parse->rate);
1064 }
1065
1066 data += 4;
1067 } else {
1068 mp3parse->xing_frames = 0;
1069 mp3parse->xing_total_time = 0;
1070 }
1071
1072 /* Store the entire time as actual total time for now. Should there be
1073 * any padding present, this value will get adjusted accordingly. */
1074 mp3parse->xing_actual_total_time = mp3parse->xing_total_time;
1075
1076 if (xing_flags & XING_BYTES_FLAG) {
1077 mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
1078 if (mp3parse->xing_bytes == 0) {
1079 GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
1080 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
1081 }
1082 data += 4;
1083 } else {
1084 mp3parse->xing_bytes = 0;
1085 }
1086
1087 /* If we know the upstream size and duration, compute the
1088 * total bitrate, rounded up to the nearest kbit/sec */
1089 if ((total_time = mp3parse->xing_total_time) &&
1090 (total_bytes = mp3parse->xing_bytes)) {
1091 mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
1092 8 * GST_SECOND, total_time);
1093 mp3parse->xing_bitrate += 500;
1094 mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
1095 }
1096
1097 if (xing_flags & XING_TOC_FLAG) {
1098 int i, percent = 0;
1099 guchar *table = mp3parse->xing_seek_table;
1100 guchar old = 0, new;
1101 guint first;
1102
1103 first = data[0];
1104 GST_DEBUG_OBJECT (mp3parse,
1105 "Subtracting initial offset of %d bytes from Xing TOC", first);
1106
1107 /* xing seek table: percent time -> 1/256 bytepos */
1108 for (i = 0; i < 100; i++) {
1109 new = data[i] - first;
1110 if (old > new) {
1111 GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
1112 mp3parse->xing_flags &= ~XING_TOC_FLAG;
1113 goto skip_toc;
1114 }
1115 mp3parse->xing_seek_table[i] = old = new;
1116 }
1117
1118 /* build inverse table: 1/256 bytepos -> 1/100 percent time */
1119 for (i = 0; i < 256; i++) {
1120 while (percent < 99 && table[percent + 1] <= i)
1121 percent++;
1122
1123 if (table[percent] == i) {
1124 mp3parse->xing_seek_table_inverse[i] = percent * 100;
1125 } else if (percent < 99 && table[percent]) {
1126 gdouble fa, fb, fx;
1127 gint a = percent, b = percent + 1;
1128
1129 fa = table[a];
1130 fb = table[b];
1131 fx = (b - a) / (fb - fa) * (i - fa) + a;
1132 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1133 } else if (percent == 99) {
1134 gdouble fa, fb, fx;
1135 gint a = percent, b = 100;
1136
1137 fa = table[a];
1138 fb = 256.0;
1139 fx = (b - a) / (fb - fa) * (i - fa) + a;
1140 mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
1141 }
1142 }
1143 skip_toc:
1144 data += 100;
1145 } else {
1146 memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
1147 memset (mp3parse->xing_seek_table_inverse, 0,
1148 sizeof (mp3parse->xing_seek_table_inverse));
1149 }
1150
1151 if (xing_flags & XING_VBR_SCALE_FLAG) {
1152 mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
1153 data += 4;
1154 } else
1155 mp3parse->xing_vbr_scale = 0;
1156
1157 GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, %"
1158 G_GUINT64_FORMAT " samples, time %" GST_TIME_FORMAT
1159 " (this includes potentially present padding data), %u bytes,"
1160 " vbr scale %u", mp3parse->xing_frames, num_xing_samples,
1161 GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
1162 mp3parse->xing_vbr_scale);
1163
1164 /* check for truncated file */
1165 if (upstream_total_bytes && mp3parse->xing_bytes &&
1166 mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
1167 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1168 "invalidating Xing header duration and size");
1169 mp3parse->xing_flags &= ~XING_BYTES_FLAG;
1170 mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
1171 }
1172
1173 /* Optional LAME tag? */
1174 if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
1175 gchar lame_version[10] = { 0, };
1176 guint tag_rev;
1177 guint32 encoder_delay, encoder_padding;
1178 guint64 total_padding_samples;
1179 guint64 actual_num_xing_samples;
1180
1181 memcpy (lame_version, data, 9);
1182 data += 9;
1183 tag_rev = data[0] >> 4;
1184 GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
1185 tag_rev, lame_version);
1186
1187 /* Skip all the information we're not interested in */
1188 data += 12;
1189 /* Encoder delay and end padding */
1190 encoder_delay = GST_READ_UINT24_BE (data);
1191 encoder_delay >>= 12;
1192 encoder_padding = GST_READ_UINT24_BE (data);
1193 encoder_padding &= 0x000fff;
1194
1195 total_padding_samples = encoder_delay + encoder_padding;
1196
1197 mp3parse->encoder_delay = encoder_delay;
1198 mp3parse->encoder_padding = encoder_padding;
1199
1200 /* As mentioned in the overview at the beginning of this source
1201 * file, decoders exhibit a delay of 529 samples. */
1202 mp3parse->decoder_delay = 529;
1203
1204 /* Where the actual, non-padding samples start & end, in sample offsets. */
1205 mp3parse->start_of_actual_samples = mp3parse->encoder_delay +
1206 mp3parse->decoder_delay;
1207 mp3parse->end_of_actual_samples = num_xing_samples +
1208 mp3parse->decoder_delay - mp3parse->encoder_padding;
1209
1210 /* Length of padding at the start and at the end of the stream,
1211 * in nanoseconds. */
1212 mp3parse->start_padding_time = gst_util_uint64_scale_int (GST_SECOND,
1213 mp3parse->start_of_actual_samples, mp3parse->rate);
1214 mp3parse->end_padding_time = mp3parse->xing_total_time -
1215 gst_util_uint64_scale_int (mp3parse->end_of_actual_samples,
1216 GST_SECOND, mp3parse->rate);
1217
1218 /* Total length of all combined padding samples, in nanoseconds. */
1219 mp3parse->total_padding_time = gst_util_uint64_scale_int (GST_SECOND,
1220 total_padding_samples, mp3parse->rate);
1221
1222 /* Length of media, in samples, without the number of padding samples. */
1223 actual_num_xing_samples = (num_xing_samples >= total_padding_samples) ?
1224 (num_xing_samples - total_padding_samples) : 0;
1225 /* Length of media, converted to nanoseconds. This is used for setting
1226 * baseparse's duration. */
1227 mp3parse->xing_actual_total_time = gst_util_uint64_scale (GST_SECOND,
1228 actual_num_xing_samples, mp3parse->rate);
1229
1230 GST_DEBUG_OBJECT (mp3parse, "Encoder delay: %u samples",
1231 mp3parse->encoder_delay);
1232 GST_DEBUG_OBJECT (mp3parse, "Encoder padding: %u samples",
1233 mp3parse->encoder_padding);
1234 GST_DEBUG_OBJECT (mp3parse, "Decoder delay: %u samples",
1235 mp3parse->decoder_delay);
1236 GST_DEBUG_OBJECT (mp3parse, "Start of actual samples: %"
1237 G_GUINT64_FORMAT, mp3parse->start_of_actual_samples);
1238 GST_DEBUG_OBJECT (mp3parse, "End of actual samples: %"
1239 G_GUINT64_FORMAT, mp3parse->end_of_actual_samples);
1240 GST_DEBUG_OBJECT (mp3parse, "Total padding samples: %" G_GUINT64_FORMAT,
1241 total_padding_samples);
1242 GST_DEBUG_OBJECT (mp3parse, "Start padding time: %" GST_TIME_FORMAT,
1243 GST_TIME_ARGS (mp3parse->start_padding_time));
1244 GST_DEBUG_OBJECT (mp3parse, "End padding time: %" GST_TIME_FORMAT,
1245 GST_TIME_ARGS (mp3parse->end_padding_time));
1246 GST_DEBUG_OBJECT (mp3parse, "Total padding time: %" GST_TIME_FORMAT,
1247 GST_TIME_ARGS (mp3parse->total_padding_time));
1248 GST_DEBUG_OBJECT (mp3parse, "Actual total media samples: %"
1249 G_GUINT64_FORMAT, actual_num_xing_samples);
1250 GST_DEBUG_OBJECT (mp3parse, "Actual total media length: %"
1251 GST_TIME_FORMAT, GST_TIME_ARGS (mp3parse->xing_actual_total_time));
1252 }
1253 } else if (read_id_vbri == vbri_id) {
1254 gint64 total_bytes, total_frames;
1255 GstClockTime total_time;
1256 guint16 nseek_points;
1257
1258 GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
1259
1260 if (avail < offset_vbri + 26) {
1261 GST_DEBUG_OBJECT (mp3parse,
1262 "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
1263 goto cleanup;
1264 }
1265
1266 GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
1267
1268 /* Move data after VBRI header */
1269 data += offset_vbri + 4;
1270
1271 if (GST_READ_UINT16_BE (data) != 0x0001) {
1272 GST_WARNING_OBJECT (mp3parse,
1273 "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
1274 goto cleanup;
1275 }
1276 data += 2;
1277
1278 /* Skip encoder delay */
1279 data += 2;
1280
1281 /* Skip quality */
1282 data += 2;
1283
1284 total_bytes = GST_READ_UINT32_BE (data);
1285 if (total_bytes != 0)
1286 mp3parse->vbri_bytes = total_bytes;
1287 data += 4;
1288
1289 total_frames = GST_READ_UINT32_BE (data);
1290 if (total_frames != 0) {
1291 mp3parse->vbri_frames = total_frames;
1292 mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
1293 (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
1294 }
1295 data += 4;
1296
1297 /* If we know the upstream size and duration, compute the
1298 * total bitrate, rounded up to the nearest kbit/sec */
1299 if ((total_time = mp3parse->vbri_total_time) &&
1300 (total_bytes = mp3parse->vbri_bytes)) {
1301 mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
1302 8 * GST_SECOND, total_time);
1303 mp3parse->vbri_bitrate += 500;
1304 mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
1305 }
1306
1307 nseek_points = GST_READ_UINT16_BE (data);
1308 data += 2;
1309
1310 if (nseek_points > 0) {
1311 guint scale, seek_bytes, seek_frames;
1312 gint i;
1313
1314 mp3parse->vbri_seek_points = nseek_points;
1315
1316 scale = GST_READ_UINT16_BE (data);
1317 data += 2;
1318
1319 seek_bytes = GST_READ_UINT16_BE (data);
1320 data += 2;
1321
1322 seek_frames = GST_READ_UINT16_BE (data);
1323
1324 if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
1325 GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
1326 goto out_vbri;
1327 }
1328
1329 if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
1330 GST_WARNING_OBJECT (mp3parse,
1331 "Not enough data to read VBRI seek table (need %d)",
1332 offset_vbri + 26 + nseek_points * seek_bytes);
1333 goto out_vbri;
1334 }
1335
1336 if (seek_frames * nseek_points < total_frames - seek_frames ||
1337 seek_frames * nseek_points > total_frames + seek_frames) {
1338 GST_WARNING_OBJECT (mp3parse,
1339 "VBRI seek table doesn't cover the complete file");
1340 goto out_vbri;
1341 }
1342
1343 data = map.data;
1344 data += offset_vbri + 26;
1345
1346 /* VBRI seek table: frame/seek_frames -> byte */
1347 mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
1348 if (seek_bytes == 4)
1349 for (i = 0; i < nseek_points; i++) {
1350 mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
1351 data += 4;
1352 } else if (seek_bytes == 3)
1353 for (i = 0; i < nseek_points; i++) {
1354 mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
1355 data += 3;
1356 } else if (seek_bytes == 2)
1357 for (i = 0; i < nseek_points; i++) {
1358 mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
1359 data += 2;
1360 } else /* seek_bytes == 1 */
1361 for (i = 0; i < nseek_points; i++) {
1362 mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
1363 data += 1;
1364 }
1365 }
1366 out_vbri:
1367
1368 GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
1369 GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
1370 GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
1371
1372 /* check for truncated file */
1373 if (upstream_total_bytes && mp3parse->vbri_bytes &&
1374 mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
1375 GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
1376 "invalidating VBRI header duration and size");
1377 mp3parse->vbri_valid = FALSE;
1378 } else {
1379 mp3parse->vbri_valid = TRUE;
1380 }
1381 } else {
1382 GST_DEBUG_OBJECT (mp3parse,
1383 "Xing, LAME or VBRI header not found in first frame");
1384 }
1385
1386 /* set duration if tables provided a valid one */
1387 if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
1388 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1389 mp3parse->xing_actual_total_time, 0);
1390 }
1391 if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
1392 gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
1393 mp3parse->vbri_total_time, 0);
1394 }
1395
1396 /* tell baseclass how nicely we can seek, and a bitrate if one found */
1397 /* FIXME: fill index with seek table */
1398 #if 0
1399 seekable = GST_BASE_PARSE_SEEK_DEFAULT;
1400 if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
1401 mp3parse->xing_total_time)
1402 seekable = GST_BASE_PARSE_SEEK_TABLE;
1403
1404 if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
1405 mp3parse->vbri_total_time)
1406 seekable = GST_BASE_PARSE_SEEK_TABLE;
1407 #endif
1408
1409 if (mp3parse->xing_bitrate)
1410 bitrate = mp3parse->xing_bitrate;
1411 else if (mp3parse->vbri_bitrate)
1412 bitrate = mp3parse->vbri_bitrate;
1413 else
1414 bitrate = 0;
1415
1416 gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
1417
1418 cleanup:
1419 gst_buffer_unmap (buf, &map);
1420 }
1421
1422 static gboolean
1423 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
1424 GstClockTime ts, gint64 * bytepos)
1425 {
1426 gint64 total_bytes;
1427 GstClockTime total_time;
1428
1429 /* If XING seek table exists use this for time->byte conversion */
1430 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1431 (total_bytes = mp3parse->xing_bytes) &&
1432 (total_time = mp3parse->xing_total_time)) {
1433 gdouble fa, fb, fx;
1434 gdouble percent =
1435 CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
1436 gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
1437 gint index = CLAMP (percent, 0, 99);
1438
1439 fa = mp3parse->xing_seek_table[index];
1440 if (index < 99)
1441 fb = mp3parse->xing_seek_table[index + 1];
1442 else
1443 fb = 256.0;
1444
1445 fx = fa + (fb - fa) * (percent - index);
1446
1447 *bytepos = (1.0 / 256.0) * fx * total_bytes;
1448
1449 return TRUE;
1450 }
1451
1452 if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
1453 (total_time = mp3parse->vbri_total_time)) {
1454 gint i, j;
1455 gdouble a, b, fa, fb;
1456
1457 i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
1458 i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
1459
1460 a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1461 mp3parse->vbri_seek_points));
1462 fa = 0.0;
1463 for (j = i; j >= 0; j--)
1464 fa += mp3parse->vbri_seek_table[j];
1465
1466 if (i + 1 < mp3parse->vbri_seek_points) {
1467 b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1468 mp3parse->vbri_seek_points));
1469 fb = fa + mp3parse->vbri_seek_table[i + 1];
1470 } else {
1471 b = gst_guint64_to_gdouble (total_time);
1472 fb = total_bytes;
1473 }
1474
1475 *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
1476
1477 return TRUE;
1478 }
1479
1480 /* If we have had a constant bit rate (so far), use it directly, as it
1481 * may give slightly more accurate results than the base class. */
1482 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1483 *bytepos = gst_util_uint64_scale (ts, mp3parse->hdr_bitrate,
1484 8 * GST_SECOND);
1485 return TRUE;
1486 }
1487
1488 return FALSE;
1489 }
1490
1491 static gboolean
1492 gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
1493 gint64 bytepos, GstClockTime * ts)
1494 {
1495 gint64 total_bytes;
1496 GstClockTime total_time;
1497
1498 /* If XING seek table exists use this for byte->time conversion */
1499 if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
1500 (total_bytes = mp3parse->xing_bytes) &&
1501 (total_time = mp3parse->xing_total_time)) {
1502 gdouble fa, fb, fx;
1503 gdouble pos;
1504 gint index;
1505
1506 pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
1507 index = CLAMP (pos, 0, 255);
1508 fa = mp3parse->xing_seek_table_inverse[index];
1509 if (index < 255)
1510 fb = mp3parse->xing_seek_table_inverse[index + 1];
1511 else
1512 fb = 10000.0;
1513
1514 fx = fa + (fb - fa) * (pos - index);
1515
1516 *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
1517
1518 return TRUE;
1519 }
1520
1521 if (mp3parse->vbri_seek_table &&
1522 (total_bytes = mp3parse->vbri_bytes) &&
1523 (total_time = mp3parse->vbri_total_time)) {
1524 gint i = 0;
1525 guint64 sum = 0;
1526 gdouble a, b, fa, fb;
1527
1528 do {
1529 sum += mp3parse->vbri_seek_table[i];
1530 i++;
1531 } while (i + 1 < mp3parse->vbri_seek_points
1532 && sum + mp3parse->vbri_seek_table[i] < bytepos);
1533 i--;
1534
1535 a = gst_guint64_to_gdouble (sum);
1536 fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
1537 mp3parse->vbri_seek_points));
1538
1539 if (i + 1 < mp3parse->vbri_seek_points) {
1540 b = a + mp3parse->vbri_seek_table[i + 1];
1541 fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
1542 mp3parse->vbri_seek_points));
1543 } else {
1544 b = total_bytes;
1545 fb = gst_guint64_to_gdouble (total_time);
1546 }
1547
1548 *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
1549
1550 return TRUE;
1551 }
1552
1553 /* If we have had a constant bit rate (so far), use it directly, as it
1554 * may give slightly more accurate results than the base class. */
1555 if (mp3parse->bitrate_is_constant && mp3parse->hdr_bitrate) {
1556 *ts = gst_util_uint64_scale (bytepos, 8 * GST_SECOND,
1557 mp3parse->hdr_bitrate);
1558 return TRUE;
1559 }
1560
1561 return FALSE;
1562 }
1563
1564 static gboolean
1565 gst_mpeg_audio_parse_src_query (GstBaseParse * parse, GstQuery * query)
1566 {
1567 gboolean res = FALSE;
1568 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1569
1570 res = GST_BASE_PARSE_CLASS (parent_class)->src_query (parse, query);
1571 if (!res)
1572 return FALSE;
1573
1574 /* If upstream operates in BYTE format then consider any parsed Xing/LAME
1575 * header to remove encoder/decoder delay and padding samples from the
1576 * position query. */
1577 if (mp3parse->upstream_format == GST_FORMAT_BYTES
1578 || GST_PAD_MODE (GST_BASE_PARSE_SINK_PAD (parse)) == GST_PAD_MODE_PULL) {
1579 switch (GST_QUERY_TYPE (query)) {
1580 case GST_QUERY_POSITION:{
1581 GstFormat format;
1582 gint64 position, new_position;
1583 GstClockTime duration_to_skip;
1584 gst_query_parse_position (query, &format, &position);
1585
1586 /* Adjust the position to exclude padding samples. */
1587
1588 if ((position < 0) || (format != GST_FORMAT_TIME))
1589 break;
1590
1591 duration_to_skip = mp3parse->frame_duration +
1592 mp3parse->start_padding_time;
1593
1594 if (position < duration_to_skip)
1595 new_position = 0;
1596 else
1597 new_position = position - duration_to_skip;
1598
1599 if (new_position > (mp3parse->xing_actual_total_time))
1600 new_position = mp3parse->xing_actual_total_time;
1601
1602 GST_LOG_OBJECT (mp3parse, "applying gapless padding info to position "
1603 "query response: %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT,
1604 GST_TIME_ARGS (position), GST_TIME_ARGS (new_position));
1605
1606 gst_query_set_position (query, GST_FORMAT_TIME, new_position);
1607
1608 break;
1609 }
1610
1611 default:
1612 break;
1613 }
1614 }
1615
1616 return res;
1617 }
1618
1619 static gboolean
1620 gst_mpeg_audio_parse_sink_event (GstBaseParse * parse, GstEvent * event)
1621 {
1622 gboolean res = FALSE;
1623 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1624
1625 res =
1626 GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse,
1627 gst_event_ref (event));
1628 if (!res) {
1629 gst_event_unref (event);
1630 return FALSE;
1631 }
1632
1633 switch (GST_EVENT_TYPE (event)) {
1634 case GST_EVENT_SEGMENT:{
1635 const GstSegment *segment;
1636
1637 gst_event_parse_segment (event, &segment);
1638 mp3parse->upstream_format = segment->format;
1639 }
1640 default:
1641 break;
1642 }
1643
1644 gst_event_unref (event);
1645
1646 return res;
1647 }
1648
1649 static gboolean
1650 gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
1651 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1652 {
1653 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1654 gboolean res = FALSE;
1655
1656 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
1657 res =
1658 gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
1659 else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
1660 res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
1661 (GstClockTime *) dest_value);
1662
1663 /* if no tables, fall back to default estimated rate based conversion */
1664 if (!res)
1665 return gst_base_parse_convert_default (parse, src_format, src_value,
1666 dest_format, dest_value);
1667
1668 return res;
1669 }
1670
1671 static GstFlowReturn
1672 gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
1673 GstBaseParseFrame * frame)
1674 {
1675 GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
1676 GstTagList *taglist = NULL;
1677
1678 /* we will create a taglist (if any of the parameters has changed)
1679 * to add the tags that changed */
1680 if (mp3parse->last_posted_crc != mp3parse->last_crc) {
1681 gboolean using_crc;
1682
1683 if (!taglist)
1684 taglist = gst_tag_list_new_empty ();
1685
1686 mp3parse->last_posted_crc = mp3parse->last_crc;
1687 if (mp3parse->last_posted_crc == CRC_PROTECTED) {
1688 using_crc = TRUE;
1689 } else {
1690 using_crc = FALSE;
1691 }
1692 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
1693 using_crc, NULL);
1694 }
1695
1696 if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
1697 if (!taglist)
1698 taglist = gst_tag_list_new_empty ();
1699
1700 mp3parse->last_posted_channel_mode = mp3parse->last_mode;
1701
1702 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
1703 gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
1704 }
1705
1706 /* tag sending done late enough in hook to ensure pending events
1707 * have already been sent */
1708 if (taglist != NULL || !mp3parse->sent_codec_tag) {
1709 GstCaps *caps;
1710
1711 if (taglist == NULL)
1712 taglist = gst_tag_list_new_empty ();
1713
1714 /* codec tag */
1715 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
1716 if (G_UNLIKELY (caps == NULL)) {
1717 gst_tag_list_unref (taglist);
1718
1719 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
1720 GST_INFO_OBJECT (parse, "Src pad is flushing");
1721 return GST_FLOW_FLUSHING;
1722 } else {
1723 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
1724 return GST_FLOW_NOT_NEGOTIATED;
1725 }
1726 }
1727 gst_pb_utils_add_codec_description_to_tag_list (taglist,
1728 GST_TAG_AUDIO_CODEC, caps);
1729 gst_caps_unref (caps);
1730
1731 if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
1732 mp3parse->vbri_bitrate == 0) {
1733 /* We don't have a VBR bitrate, so post the available bitrate as
1734 * nominal and let baseparse calculate the real bitrate */
1735 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1736 GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
1737 }
1738
1739 /* also signals the end of first-frame processing */
1740 mp3parse->sent_codec_tag = TRUE;
1741 }
1742
1743 /* if the taglist exists, we need to update it so it gets sent out */
1744 if (taglist) {
1745 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
1746 gst_tag_list_unref (taglist);
1747 }
1748
1749 /* adjust buffer PTS/DTS/durations according to gapless playback info */
1750 if ((mp3parse->upstream_format == GST_FORMAT_BYTES
1751 || GST_PAD_MODE (GST_BASE_PARSE_SINK_PAD (parse)) ==
1752 GST_PAD_MODE_PULL)
1753 && GST_CLOCK_TIME_IS_VALID (mp3parse->total_padding_time)) {
1754 guint64 frame_nr;
1755 GstClockTime pts, dts;
1756 gboolean add_clipping_meta = FALSE;
1757 guint32 start_clip = 0, end_clip = 0;
1758 GstClockTime timestamp_decrement;
1759 guint64 sample_pos;
1760 guint64 sample_pos_end;
1761
1762 /* Get the number of the current frame so we can determine where we
1763 * currently are in the MPEG stream.
1764 *
1765 * Gapless playback is best done based on samples, not timestamps,
1766 * to avoid potential rounding errors that can otherwise cause a few
1767 * samples to be incorrectly clipped or not clipped.
1768 *
1769 * TODO: At the moment, there is no dedicated baseparse API for finding
1770 * out what frame we are currently in. The frame number is calculated
1771 * out of the PTS of the current frame. Each frame has the same duration,
1772 * and at this point, the buffer's PTS has not been adjusted to exclude
1773 * the padding samples, so the PTS will be an integer multiple of
1774 * frame_duration. However, this is not an ideal solution. Investigate
1775 * how to properly implement this. */
1776 frame_nr = GST_BUFFER_PTS (frame->buffer) / mp3parse->frame_duration;
1777 GST_LOG_OBJECT (mp3parse, "Handling MP3 frame #%" G_GUINT64_FORMAT,
1778 frame_nr);
1779
1780 /* By default, we subtract the start_padding_time from the timestamps.
1781 * start_padding_time specifies the duration of the padding samples
1782 * at the beginning of the MPEG stream. To factor out these padding
1783 * samples, we have to shift the timestamps back, which is done with
1784 * this decrement. */
1785 timestamp_decrement = mp3parse->start_padding_time;
1786
1787 pts = GST_BUFFER_PTS (frame->buffer);
1788 dts = GST_BUFFER_DTS (frame->buffer);
1789
1790 /* sample_pos specifies the current position of the beginning of the
1791 * current frame, while sample_pos_end specifies the current position
1792 * of 1 samples past the end of the current frame. Both values are
1793 * in samples. */
1794 sample_pos = frame_nr * mp3parse->spf;
1795 sample_pos_end = sample_pos + mp3parse->spf;
1796
1797 /* Check if the frame is not (fully) within the actual playback range. */
1798 if (G_UNLIKELY (sample_pos <= mp3parse->start_of_actual_samples ||
1799 (sample_pos_end >= mp3parse->end_of_actual_samples))) {
1800
1801 if (G_UNLIKELY (frame_nr >= mp3parse->xing_frames)) {
1802 /* Test #1: Check if the current position lies past the length
1803 * that is specified by the Xing frame header. This normally does
1804 * not happen, but does occur with "Frankenstein" streams (see
1805 * the explanation at the beginning of this source file for more).
1806 * Do this first, since the other test may yield false positives
1807 * in this case. */
1808 GST_LOG_OBJECT (mp3parse, "There are frames beyond what the Xing "
1809 "metadata indicates; this is a Frankenstein stream!");
1810
1811 /* The frames past the "officially" last one (= the last one according
1812 * to the Xing header frame) are located past the padding samples
1813 * that follow the actual playback range. The length of these
1814 * padding samples in nanoseconds is stored in end_padding_time.
1815 * We need to shift the PTS to compensate for these padding samples,
1816 * otherwise there would be a timestamp discontinuity between the
1817 * last "official" frame and the first "Frankenstein" frame. */
1818 timestamp_decrement += mp3parse->end_padding_time;
1819 } else if (sample_pos_end <= mp3parse->start_of_actual_samples) {
1820 /* Test #2: Check if the frame lies completely before the actual
1821 * playback range. This happens if the number of padding samples
1822 * at the start of the stream exceeds the size of a frame, meaning
1823 * that the entire frame will be filled with padding samples.
1824 * This has not been observed so far. However, it is in theory
1825 * possible, so handle it here. */
1826
1827 /* We want to clip all samples in the frame. Since this is a frame
1828 * at the start of the stream, set start_clip to the frame size.
1829 * Also set the buffer duration to 0 to make sure baseparse does not
1830 * increment timestamps after this current frame is finished. */
1831 start_clip = mp3parse->spf;
1832 GST_BUFFER_DURATION (frame->buffer) = 0;
1833
1834 add_clipping_meta = TRUE;
1835 } else if (sample_pos <= mp3parse->start_of_actual_samples) {
1836 /* Test #3: Check if a portion of the frame lies before the actual
1837 * playback range. Set the duration to the number of samples that
1838 * remain after clipping. */
1839
1840 start_clip = mp3parse->start_of_actual_samples - sample_pos;
1841 GST_BUFFER_DURATION (frame->buffer) =
1842 gst_util_uint64_scale_int (sample_pos_end -
1843 mp3parse->start_of_actual_samples, GST_SECOND, mp3parse->rate);
1844
1845 add_clipping_meta = TRUE;
1846 } else if (sample_pos >= mp3parse->end_of_actual_samples) {
1847 /* Test #4: Check if the frame lies completely after the actual
1848 * playback range. Similar to test #2, this happens if the number
1849 * of padding samples at the end of the stream exceeds the size of
1850 * a frame, meaning that the entire frame will be filled with padding
1851 * samples. Unlike test #2, this has been observed in mp3s several
1852 * times: The penultimate frame is partially clipped, the final
1853 * frame is fully clipped. */
1854
1855 GstClockTime padding_ns;
1856
1857 /* We want to clip all samples in the frame. Since this is a frame
1858 * at the end of the stream, set end_clip to the frame size.
1859 * Also set the buffer duration to 0 to make sure baseparse does not
1860 * increment timestamps after this current frame is finished. */
1861 end_clip = mp3parse->spf;
1862 GST_BUFFER_DURATION (frame->buffer) = 0;
1863
1864 /* Even though this frame will be fully clipped, we still have to
1865 * make sure its timestamps are not discontinuous with the preceding
1866 * ones. To that end, it is necessary to subtract the time range
1867 * between the current position and the last valid playback range
1868 * position from the PTS and DTS. */
1869 padding_ns = gst_util_uint64_scale_int (sample_pos -
1870 mp3parse->end_of_actual_samples, GST_SECOND, mp3parse->rate);
1871 timestamp_decrement += padding_ns;
1872
1873 add_clipping_meta = TRUE;
1874 } else if (sample_pos_end >= mp3parse->end_of_actual_samples) {
1875 /* Test #5: Check if a portion of the frame lies after the actual
1876 * playback range. Set the duration to the number of samples that
1877 * remain after clipping. */
1878
1879 end_clip = sample_pos_end - mp3parse->end_of_actual_samples;
1880 GST_BUFFER_DURATION (frame->buffer) =
1881 gst_util_uint64_scale_int (mp3parse->end_of_actual_samples -
1882 sample_pos, GST_SECOND, mp3parse->rate);
1883
1884 add_clipping_meta = TRUE;
1885 }
1886 }
1887
1888 if (G_UNLIKELY (add_clipping_meta)) {
1889 GST_DEBUG_OBJECT (mp3parse, "Adding clipping meta: start %"
1890 G_GUINT32_FORMAT " end %" G_GUINT32_FORMAT, start_clip, end_clip);
1891 gst_buffer_add_audio_clipping_meta (frame->buffer, GST_FORMAT_DEFAULT,
1892 start_clip, end_clip);
1893 }
1894
1895 /* Adjust the timestamps by subtracting from them. The decrement
1896 * is computed above. */
1897 GST_BUFFER_PTS (frame->buffer) = (pts >= timestamp_decrement) ? (pts -
1898 timestamp_decrement) : 0;
1899 GST_BUFFER_DTS (frame->buffer) = (dts >= timestamp_decrement) ? (dts -
1900 timestamp_decrement) : 0;
1901
1902 /* NOTE: We do not adjust the size here, just the timestamps and duration.
1903 * We also do not drop fully clipped frames. This is because downstream
1904 * MPEG audio decoders still need the data of the frame, even if it gets
1905 * fully clipped later. They do need these frames for their decoding process.
1906 * If these frames were dropped, the decoders would not fully decode all
1907 * of the data from the MPEG stream. */
1908
1909 /* TODO: Should offset/offset_end also be adjusted? */
1910 }
1911
1912 /* Check if this frame can safely be dropped (for example, because it is an
1913 * empty Xing header frame). */
1914 if (G_UNLIKELY (mp3parse->outgoing_frame_is_xing_header)) {
1915 GST_DEBUG_OBJECT (mp3parse, "Marking frame as decode-only / droppable");
1916 mp3parse->outgoing_frame_is_xing_header = FALSE;
1917 GST_BUFFER_DURATION (frame->buffer) = 0;
1918 GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DECODE_ONLY);
1919 GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DROPPABLE);
1920 }
1921
1922 /* usual clipping applies */
1923 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1924
1925 return GST_FLOW_OK;
1926 }
1927
1928 static void
1929 remove_fields (GstCaps * caps)
1930 {
1931 guint i, n;
1932
1933 n = gst_caps_get_size (caps);
1934 for (i = 0; i < n; i++) {
1935 GstStructure *s = gst_caps_get_structure (caps, i);
1936
1937 gst_structure_remove_field (s, "parsed");
1938 }
1939 }
1940
1941 static GstCaps *
1942 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
1943 {
1944 GstCaps *peercaps, *templ;
1945 GstCaps *res;
1946
1947 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
1948 if (filter) {
1949 GstCaps *fcopy = gst_caps_copy (filter);
1950 /* Remove the fields we convert */
1951 remove_fields (fcopy);
1952 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
1953 gst_caps_unref (fcopy);
1954 } else
1955 peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
1956
1957 if (peercaps) {
1958 /* Remove the parsed field */
1959 peercaps = gst_caps_make_writable (peercaps);
1960 remove_fields (peercaps);
1961
1962 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
1963 gst_caps_unref (peercaps);
1964 gst_caps_unref (templ);
1965 } else {
1966 res = templ;
1967 }
1968
1969 if (filter) {
1970 GstCaps *intersection;
1971
1972 intersection =
1973 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
1974 gst_caps_unref (res);
1975 res = intersection;
1976 }
1977
1978 return res;
1979 }