2 * Copyright (c) 2006 Christophe Fergeau <teuf@gnome.org>
3 * Copyright (c) 2008 Sebastian Dröge <slomo@circular-chaos.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 /* Xing SDK: http://www.mp3-tech.org/programmer/sources/vbrheadersdk.zip */
25 * SECTION:element-xingmux
27 * xingmux adds a Xing header to MP3 files. This contains information about the duration and size
28 * of the file and a seek table and is very useful for getting an almost correct duration and better
29 * seeking on VBR MP3 files.
31 * This element will remove any existing Xing, LAME or VBRI headers from the beginning of the file.
34 * <title>Example launch line</title>
36 * gst-launch audiotestsrc num-buffers=1000 ! audioconvert ! lame ! xingmux ! filesink location=test.mp3
37 * gst-launch filesrc location=test.mp3 ! xingmux ! filesink location=test2.mp3
38 * gst-launch filesrc location=test.mp3 ! mp3parse ! xingmux ! filesink location=test2.mp3
48 #include "gstxingmux.h"
50 GST_DEBUG_CATEGORY_STATIC (xing_mux_debug);
51 #define GST_CAT_DEFAULT xing_mux_debug
53 GST_BOILERPLATE (GstXingMux, gst_xing_mux, GstElement, GST_TYPE_ELEMENT);
55 /* Xing Header stuff */
56 #define GST_XING_FRAME_FIELD (1 << 0)
57 #define GST_XING_BYTES_FIELD (1 << 1)
58 #define GST_XING_TOC_FIELD (1 << 2)
59 #define GST_XING_QUALITY_FIELD (1 << 3)
61 typedef struct _GstXingSeekEntry
67 static inline GstXingSeekEntry *
68 gst_xing_seek_entry_new (void)
70 return g_slice_new (GstXingSeekEntry);
74 gst_xing_seek_entry_free (GstXingSeekEntry * entry)
76 g_slice_free (GstXingSeekEntry, entry);
79 static void gst_xing_mux_finalize (GObject * obj);
80 static GstStateChangeReturn
81 gst_xing_mux_change_state (GstElement * element, GstStateChange transition);
82 static GstFlowReturn gst_xing_mux_chain (GstPad * pad, GstBuffer * buffer);
83 static gboolean gst_xing_mux_sink_event (GstPad * pad, GstEvent * event);
85 static GstStaticPadTemplate gst_xing_mux_sink_template =
86 GST_STATIC_PAD_TEMPLATE ("sink",
89 GST_STATIC_CAPS ("audio/mpeg, "
90 "mpegversion = (int) 1, " "layer = (int) [ 1, 3 ]"));
93 static GstStaticPadTemplate gst_xing_mux_src_template =
94 GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS ("audio/mpeg, "
98 "mpegversion = (int) 1, " "layer = (int) [ 1, 3 ]"));
99 static const guint mp3types_bitrates[2][3][16] = {
101 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
102 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
103 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
106 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
107 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
108 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
112 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
113 {22050, 24000, 16000},
118 parse_header (guint32 header, guint * ret_size, guint * ret_spf,
122 gulong samplerate, bitrate, layer, padding;
125 if ((header & 0xffe00000) != 0xffe00000) {
126 g_warning ("invalid sync");
130 if (((header >> 19) & 3) == 0x01) {
131 g_warning ("invalid MPEG version");
135 if (((header >> 17) & 3) == 0x00) {
136 g_warning ("invalid MPEG layer");
140 if (((header >> 12) & 0xf) == 0xf || ((header >> 12) & 0xf) == 0x0) {
141 g_warning ("invalid bitrate");
145 if (((header >> 10) & 0x3) == 0x3) {
146 g_warning ("invalid sampling rate");
150 if (header & 0x00000002) {
151 g_warning ("invalid emphasis");
155 if (header & (1 << 20)) {
156 lsf = (header & (1 << 19)) ? 0 : 1;
163 layer = 4 - ((header >> 17) & 0x3);
165 bitrate = (header >> 12) & 0xF;
166 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
170 samplerate = (header >> 10) & 0x3;
171 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
173 padding = (header >> 9) & 0x1;
177 length = 4 * ((bitrate * 12) / samplerate + padding);
180 length = (bitrate * 144) / samplerate + padding;
184 length = (bitrate * 144) / (samplerate << lsf) + padding;
190 else if (layer == 2 || lsf == 0)
200 *ret_rate = samplerate;
206 get_xing_offset (guint32 header)
208 guint mpeg_version = (header >> 19) & 0x3;
209 guint channel_mode = (header >> 6) & 0x3;
211 if (mpeg_version == 0x3) {
212 if (channel_mode == 0x3) {
218 if (channel_mode == 0x3) {
227 has_xing_header (guint32 header, guchar * data, gsize size)
230 data += get_xing_offset (header);
232 if (memcmp (data, "Xing", 4) == 0 ||
233 memcmp (data, "Info", 4) == 0 || memcmp (data, "VBRI", 4) == 0)
240 generate_xing_header (GstXingMux * xing)
243 guint32 xing_flags_tmp = 0;
244 GstBuffer *xing_header;
249 guint size, spf, xing_offset;
251 guint bitrate = 0x00;
256 header = xing->first_header;
258 /* Set bitrate and choose lowest possible size */
262 header &= 0xffff0fff;
263 header |= bitrate << 12;
265 parse_header (header, &size, &spf, &rate);
266 xing_offset = get_xing_offset (header);
267 } while (size < (4 + xing_offset + 4 + 4 + 4 + 4 + 100) && bitrate < 0xe);
269 if (bitrate == 0xe) {
270 GST_ERROR ("No usable bitrate found!");
274 if (gst_pad_alloc_buffer_and_set_caps (xing->srcpad, 0, size,
275 GST_PAD_CAPS (xing->srcpad), &xing_header) != GST_FLOW_OK) {
276 xing_header = gst_buffer_new_and_alloc (size);
277 gst_buffer_set_caps (xing_header, GST_PAD_CAPS (xing->srcpad));
280 data = GST_BUFFER_DATA (xing_header);
281 memset (data, 0, size);
282 header_be = GUINT32_TO_BE (header);
283 memcpy (data, &header_be, 4);
288 memcpy (data, "Xing", 4);
294 if (xing->duration != GST_CLOCK_TIME_NONE) {
295 duration = xing->duration;
297 GstFormat fmt = GST_FORMAT_TIME;
299 if (!gst_pad_query_peer_duration (xing->sinkpad, &fmt, &duration))
300 duration = GST_CLOCK_TIME_NONE;
303 if (duration != GST_CLOCK_TIME_NONE) {
304 guint32 number_of_frames;
306 /* The Xing Header contains a NumberOfFrames field, which verifies to:
307 * Duration = NumberOfFrames *SamplesPerFrame/SamplingRate
308 * SamplesPerFrame and SamplingRate are values for the current frame.
310 number_of_frames = gst_util_uint64_scale (duration, rate, GST_SECOND) / spf;
311 GST_DEBUG ("Setting number of frames to %u", number_of_frames);
312 number_of_frames = GUINT32_TO_BE (number_of_frames);
313 memcpy (data, &number_of_frames, 4);
314 xing_flags_tmp |= GST_XING_FRAME_FIELD;
318 if (xing->byte_count != 0) {
319 byte_count = xing->byte_count;
321 GstFormat fmt = GST_FORMAT_BYTES;
323 if (!gst_pad_query_peer_duration (xing->sinkpad, &fmt, &byte_count))
325 if (byte_count == -1)
329 if (byte_count != 0) {
332 if (byte_count > G_MAXUINT32) {
333 GST_DEBUG ("Too large stream: %" G_GINT64_FORMAT " > %u bytes",
334 byte_count, G_MAXUINT32);
337 GST_DEBUG ("Setting number of bytes to %u", nbytes);
338 nbytes = GUINT32_TO_BE (nbytes);
339 memcpy (data, &nbytes, 4);
340 xing_flags_tmp |= GST_XING_BYTES_FIELD;
345 if (xing->seek_table != NULL && byte_count != 0
346 && duration != GST_CLOCK_TIME_NONE) {
350 xing_flags_tmp |= GST_XING_TOC_FIELD;
352 GST_DEBUG ("Writing seek table");
353 for (it = xing->seek_table; it != NULL && percent < 100; it = it->next) {
354 GstXingSeekEntry *entry = (GstXingSeekEntry *) it->data;
358 while ((entry->timestamp * 100) / duration >= percent) {
359 pos = (entry->byte * 256) / byte_count;
360 GST_DEBUG (" %d %% -- %" G_GINT64_FORMAT " 1/256", percent, pos);
362 memcpy (data, &byte, 1);
372 memcpy (&b, data - 1, 1);
374 for (i = percent; i < 100; i++) {
375 GST_DEBUG (" %d %% -- %d 1/256", i, b);
376 memcpy (data, &b, 1);
382 GST_DEBUG ("Setting Xing flags to 0x%x\n", xing_flags_tmp);
383 xing_flags_tmp = GUINT32_TO_BE (xing_flags_tmp);
384 memcpy (xing_flags, &xing_flags_tmp, 4);
389 gst_xing_mux_base_init (gpointer g_class)
391 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
393 gst_element_class_add_static_pad_template (element_class,
394 &gst_xing_mux_src_template);
395 gst_element_class_add_static_pad_template (element_class,
396 &gst_xing_mux_sink_template);
398 GST_DEBUG_CATEGORY_INIT (xing_mux_debug, "xingmux", 0, "Xing Header Muxer");
400 gst_element_class_set_details_simple (element_class, "MP3 Xing muxer",
401 "Formatter/Metadata",
402 "Adds a Xing header to the beginning of a VBR MP3 file",
403 "Christophe Fergeau <teuf@gnome.org>");
407 gst_xing_mux_class_init (GstXingMuxClass * klass)
409 GObjectClass *gobject_class;
410 GstElementClass *gstelement_class;
412 gobject_class = (GObjectClass *) klass;
413 gstelement_class = (GstElementClass *) klass;
415 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_xing_mux_finalize);
416 gstelement_class->change_state =
417 GST_DEBUG_FUNCPTR (gst_xing_mux_change_state);
421 gst_xing_mux_finalize (GObject * obj)
423 GstXingMux *xing = GST_XING_MUX (obj);
426 g_object_unref (xing->adapter);
427 xing->adapter = NULL;
430 if (xing->seek_table) {
431 g_list_foreach (xing->seek_table, (GFunc) gst_xing_seek_entry_free, NULL);
432 g_list_free (xing->seek_table);
433 xing->seek_table = NULL;
436 G_OBJECT_CLASS (parent_class)->finalize (obj);
440 xing_reset (GstXingMux * xing)
442 xing->duration = GST_CLOCK_TIME_NONE;
443 xing->byte_count = 0;
445 gst_adapter_clear (xing->adapter);
447 if (xing->seek_table) {
448 g_list_foreach (xing->seek_table, (GFunc) gst_xing_seek_entry_free, NULL);
449 g_list_free (xing->seek_table);
450 xing->seek_table = NULL;
453 xing->sent_xing = FALSE;
458 gst_xing_mux_init (GstXingMux * xing, GstXingMuxClass * xingmux_class)
460 GstElementClass *klass = GST_ELEMENT_CLASS (xingmux_class);
462 /* pad through which data comes in to the element */
464 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
466 gst_pad_set_setcaps_function (xing->sinkpad,
467 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
468 gst_pad_set_chain_function (xing->sinkpad,
469 GST_DEBUG_FUNCPTR (gst_xing_mux_chain));
470 gst_pad_set_event_function (xing->sinkpad,
471 GST_DEBUG_FUNCPTR (gst_xing_mux_sink_event));
472 gst_element_add_pad (GST_ELEMENT (xing), xing->sinkpad);
474 /* pad through which data goes out of the element */
476 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
478 gst_element_add_pad (GST_ELEMENT (xing), xing->srcpad);
480 xing->adapter = gst_adapter_new ();
486 gst_xing_mux_chain (GstPad * pad, GstBuffer * buffer)
488 GstXingMux *xing = GST_XING_MUX (GST_PAD_PARENT (pad));
489 GstFlowReturn ret = GST_FLOW_OK;
491 gst_adapter_push (xing->adapter, buffer);
493 while (gst_adapter_available (xing->adapter) >= 4) {
494 const guchar *data = gst_adapter_peek (xing->adapter, 4);
497 GstClockTime duration;
500 GstXingSeekEntry *seek_entry;
502 header = GST_READ_UINT32_BE (data);
504 if (!parse_header (header, &size, &spf, &rate)) {
505 GST_DEBUG ("Lost sync, resyncing");
506 gst_adapter_flush (xing->adapter, 1);
510 if (gst_adapter_available (xing->adapter) < size)
513 outbuf = gst_adapter_take_buffer (xing->adapter, size);
514 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (xing->srcpad));
516 if (!xing->sent_xing) {
517 if (has_xing_header (header, GST_BUFFER_DATA (outbuf), size)) {
518 GST_LOG_OBJECT (xing, "Dropping old Xing header");
519 gst_buffer_unref (outbuf);
522 GstBuffer *xing_header;
523 guint64 xing_header_size;
525 xing->first_header = header;
527 xing_header = generate_xing_header (xing);
529 if (xing_header == NULL) {
530 GST_ERROR ("Can't generate Xing header");
531 gst_buffer_unref (outbuf);
532 return GST_FLOW_ERROR;
535 xing_header_size = GST_BUFFER_SIZE (xing_header);
537 if ((ret = gst_pad_push (xing->srcpad, xing_header)) != GST_FLOW_OK) {
538 GST_ERROR_OBJECT (xing, "Failed to push Xing header: %s",
539 gst_flow_get_name (ret));
540 gst_buffer_unref (xing_header);
541 gst_buffer_unref (outbuf);
545 xing->byte_count += xing_header_size;
546 xing->sent_xing = TRUE;
550 seek_entry = gst_xing_seek_entry_new ();
551 seek_entry->timestamp =
552 (xing->duration == GST_CLOCK_TIME_NONE) ? 0 : xing->duration;
553 /* Workaround for parsers checking that the first seek table entry is 0 */
554 seek_entry->byte = (seek_entry->timestamp == 0) ? 0 : xing->byte_count;
555 xing->seek_table = g_list_append (xing->seek_table, seek_entry);
557 duration = gst_util_uint64_scale (spf, GST_SECOND, rate);
559 GST_BUFFER_TIMESTAMP (outbuf) =
560 (xing->duration == GST_CLOCK_TIME_NONE) ? 0 : xing->duration;
561 GST_BUFFER_DURATION (outbuf) = duration;
562 GST_BUFFER_OFFSET (outbuf) = xing->byte_count;
563 GST_BUFFER_OFFSET_END (outbuf) =
564 xing->byte_count + GST_BUFFER_SIZE (outbuf);
566 xing->byte_count += GST_BUFFER_SIZE (outbuf);
568 if (xing->duration == GST_CLOCK_TIME_NONE)
569 xing->duration = duration;
571 xing->duration += duration;
573 if ((ret = gst_pad_push (xing->srcpad, outbuf)) != GST_FLOW_OK) {
574 GST_ERROR_OBJECT (xing, "Failed to push MP3 frame: %s",
575 gst_flow_get_name (ret));
584 gst_xing_mux_sink_event (GstPad * pad, GstEvent * event)
589 xing = GST_XING_MUX (gst_pad_get_parent (pad));
591 switch (GST_EVENT_TYPE (event)) {
592 case GST_EVENT_NEWSEGMENT:
593 if (xing->sent_xing) {
594 GST_ERROR ("Already sent Xing header, dropping NEWSEGMENT event!");
595 gst_event_unref (event);
600 gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
602 if (fmt == GST_FORMAT_BYTES) {
603 result = gst_pad_push_event (xing->srcpad, event);
605 gst_event_unref (event);
607 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
608 0, GST_CLOCK_TIME_NONE, 0);
610 result = gst_pad_push_event (xing->srcpad, event);
618 GST_DEBUG_OBJECT (xing, "handling EOS event");
620 if (xing->sent_xing) {
622 n_event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
623 0, GST_CLOCK_TIME_NONE, 0);
625 if (G_UNLIKELY (!gst_pad_push_event (xing->srcpad, n_event))) {
627 ("Failed to seek to position 0 for pushing the Xing header");
632 header = generate_xing_header (xing);
634 if (header == NULL) {
635 GST_ERROR ("Can't generate Xing header");
638 GST_INFO ("Writing real Xing header to beginning of stream");
640 if ((ret = gst_pad_push (xing->srcpad, header)) != GST_FLOW_OK)
641 GST_WARNING ("Failed to push updated Xing header: %s\n",
642 gst_flow_get_name (ret));
646 result = gst_pad_push_event (xing->srcpad, event);
650 result = gst_pad_event_default (pad, event);
653 gst_object_unref (GST_OBJECT (xing));
659 static GstStateChangeReturn
660 gst_xing_mux_change_state (GstElement * element, GstStateChange transition)
663 GstStateChangeReturn result;
665 xing = GST_XING_MUX (element);
667 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
669 switch (transition) {
670 case GST_STATE_CHANGE_PAUSED_TO_READY: