From d7fa53d5d420fbdd49e0e1a1ed51db02a61b4331 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Fri, 3 Aug 2012 11:13:48 +0100 Subject: [PATCH 01/33] mpg123: add new libmpg123-based mp3 decoder plugin Needs a bit of cleaning up. https://bugzilla.gnome.org/show_bug.cgi?id=681003 --- ext/mpg123/gstmpg123audiodec.c | 631 +++++++++++++++++++++++++++++++++ ext/mpg123/gstmpg123audiodec.h | 62 ++++ 2 files changed, 693 insertions(+) create mode 100644 ext/mpg123/gstmpg123audiodec.c create mode 100644 ext/mpg123/gstmpg123audiodec.h diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c new file mode 100644 index 0000000000..9e7f61dfe3 --- /dev/null +++ b/ext/mpg123/gstmpg123audiodec.c @@ -0,0 +1,631 @@ +/* MP3 decoding plugin for GStreamer using the mpg123 library + * Copyright (C) 2012 Carlos Rafael Giani + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "gstmpg123audiodec.h" + +#include +#include + +GST_DEBUG_CATEGORY_STATIC (mpg123_debug); +#define GST_CAT_DEFAULT mpg123_debug + +/* + * Omitted sample formats that mpg123 supports (or at least can support): + * - 8bit integer signed + * - 8bit integer unsigned + * - a-law + * - mu-law + * - 64bit float + * + * The first four formats are not supported by the GstAudioDecoder base class. + * (The internal gst_audio_format_from_caps_structure() call fails.) + * + * The 64bit float issue is tricky. mpg123 actually decodes to "real", + * not necessarily to "float". + * + * "real" can be fixed point, 32bit float, 64bit float. There seems to be + * no way how to find out which one of them is actually used. + * + * However, in all known installations, "real" equals 32bit float, so that's + * what is used. + */ + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, " + "mpegversion = (int) { 1 }, " + "layer = (int) [ 1, 3 ], " + "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, " + "channels = (int) [ 1, 2 ], " "parsed = (boolean) true ") + ); + +static void gst_mpg123_audio_dec_finalize (GObject * object); +static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec); +static gboolean gst_mpg123_audio_dec_stop (GstAudioDecoder * dec); +static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec + * mpg123_decoder, unsigned char const *decoded_bytes, + size_t const num_decoded_bytes); +static GstFlowReturn gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, + GstBuffer * buffer); +static gboolean gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, + GstCaps * incoming_caps); +static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard); + +G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER); + +static void +gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) +{ + GObjectClass *object_class; + GstAudioDecoderClass *base_class; + GstElementClass *element_class; + GstCaps *src_caps; + GstPadTemplate *src_template; + int error; + + GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder"); + + object_class = G_OBJECT_CLASS (klass); + base_class = GST_AUDIO_DECODER_CLASS (klass); + element_class = GST_ELEMENT_CLASS (klass); + + object_class->finalize = gst_mpg123_audio_dec_finalize; + + gst_element_class_set_static_metadata (element_class, + "mpg123 mp3 decoder", + "Codec/Decoder/Audio", + "Decodes mp3 streams using the mpg123 library", + "Carlos Rafael Giani "); + + /* + Not using static pad template for srccaps, since the comma-separated list of formats needs to be + created depending on whatever mpg123 supports + */ + { + gchar *format_string; + gchar *caps_string; + + static gchar const *src_caps_begin = "audio/x-raw, " "format = { "; + static gchar const *src_caps_end = + " }, " + "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, " + "channels = (int) [ 1, 2 ], " "layout = (string) interleaved; "; + +#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) +#define ENDIAN_POSTFIX "LE" +#else +#define ENDIAN_POSTFIX "BE" +#endif + + static gchar *supported_formats[] = { +#ifdef MPG123_ENC_SIGNED_16 + "S16" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_UNSIGNED_16 + "U16" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_SIGNED_24 + "S24" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_UNSIGNED_24 + "U24" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_SIGNED_32 + "S32" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_UNSIGNED_32 + "U32" ENDIAN_POSTFIX, +#endif +#ifdef MPG123_ENC_FLOAT_32 + "F32" ENDIAN_POSTFIX, +#endif + NULL + }; + + format_string = g_strjoinv (", ", supported_formats); + caps_string = + g_strjoin (NULL, src_caps_begin, format_string, src_caps_end, NULL); + + src_caps = gst_caps_from_string (caps_string); + src_template = + gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, + gst_caps_ref (src_caps)); + + g_free (format_string); + g_free (caps_string); + } + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, src_template); + + base_class->start = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_start); + base_class->stop = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_stop); + base_class->handle_frame = + GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_handle_frame); + base_class->set_format = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_set_format); + base_class->flush = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_flush); + + error = mpg123_init (); + if (G_UNLIKELY (error != MPG123_OK)) + GST_ERROR ("Could not initialize mpg123 library: %s", + mpg123_plain_strerror (error)); + else + GST_TRACE ("mpg123 library initialized"); +} + + +void +gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder) +{ + mpg123_decoder->handle = NULL; +} + + +static void +gst_mpg123_audio_dec_finalize (GObject * object) +{ + GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (object); + if (G_LIKELY (mpg123_decoder->handle != NULL)) { + mpg123_delete (mpg123_decoder->handle); + mpg123_decoder->handle = NULL; + } +} + + +static gboolean +gst_mpg123_audio_dec_start (GstAudioDecoder * dec) +{ + GstMpg123AudioDec *mpg123_decoder; + int error; + + mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); + error = 0; + + mpg123_decoder->handle = mpg123_new (NULL, &error); + mpg123_decoder->has_next_audioinfo = FALSE; + mpg123_decoder->frame_offset = 0; + + /* + Initially, the mpg123 handle comes with a set of default formats supported. This clears this set. + This is necessary, since only one format shall be supported (see set_format for more). + */ + mpg123_format_none (mpg123_decoder->handle); + + mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); /* Built-in mpg123 support for gapless decoding is disabled for now, since it does not work well with seeking */ + mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; essential for MP3 radio streams */ + mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Sets the resync limit to the end of the stream (e.g. don't give up prematurely) */ + + /* Open in feed mode (= encoded data is fed manually into the handle). */ + error = mpg123_open_feed (mpg123_decoder->handle); + + if (G_UNLIKELY (error != MPG123_OK)) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("Error opening mpg123 feed: %s", mpg123_plain_strerror (error))); + mpg123_close (mpg123_decoder->handle); + mpg123_delete (mpg123_decoder->handle); + mpg123_decoder->handle = NULL; + return FALSE; + } + + GST_DEBUG_OBJECT (dec, "mpg123 decoder started"); + + return TRUE; +} + + +static gboolean +gst_mpg123_audio_dec_stop (GstAudioDecoder * dec) +{ + GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); + + if (G_LIKELY (mpg123_decoder->handle != NULL)) { + mpg123_close (mpg123_decoder->handle); + mpg123_delete (mpg123_decoder->handle); + mpg123_decoder->handle = NULL; + } + + GST_DEBUG_OBJECT (dec, "mpg123 decoder stopped"); + + return TRUE; +} + + +static GstFlowReturn +gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, + unsigned char const *decoded_bytes, size_t const num_decoded_bytes) +{ + GstBuffer *output_buffer; + GstFlowReturn alloc_error; + GstAudioDecoder *dec; + + output_buffer = NULL; + dec = GST_AUDIO_DECODER (mpg123_decoder); + + if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) { + /* This occurs in the first few frames, which do not carry data; once MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */ + GST_TRACE_OBJECT (mpg123_decoder, + "Nothing was decoded -> no output buffer to push"); + return GST_FLOW_OK; + } + + output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL); + alloc_error = (output_buffer == NULL) ? GST_FLOW_ERROR : GST_FLOW_OK; + + if (alloc_error != GST_FLOW_OK) { + /* This is necessary to advance playback in time, even when nothing was decoded. */ + return gst_audio_decoder_finish_frame (dec, NULL, 1); + } else { + GstMapInfo info; + + if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) { + if (info.size != num_decoded_bytes) + GST_ERROR_OBJECT (mpg123_decoder, + "Mapped memory region has size %u instead of expected size %u", + info.size, num_decoded_bytes); + else + memcpy (info.data, decoded_bytes, num_decoded_bytes); + + gst_buffer_unmap (output_buffer, &info); + } else + GST_ERROR_OBJECT (mpg123_decoder, "Could not map buffer"); + + return gst_audio_decoder_finish_frame (dec, output_buffer, 1); + } +} + + +static GstFlowReturn +gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) +{ + GstMpg123AudioDec *mpg123_decoder; + int decode_error; + unsigned char *decoded_bytes; + size_t num_decoded_bytes; + + if (G_UNLIKELY (!buffer)) + return GST_FLOW_OK; + + mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); + + if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("mpg123 handle is NULL")); + return GST_FLOW_ERROR; + } + + /* The actual decoding */ + { + unsigned char const *inmemory; + size_t inmemsize; + GstMemory *memory; + GstMapInfo info; + + memory = gst_buffer_get_all_memory (buffer); + if (memory == NULL) + return GST_FLOW_ERROR; + + if (!gst_memory_map (memory, &info, GST_MAP_WRITE)) { + gst_memory_unref (memory); + return GST_FLOW_ERROR; + } + + inmemory = info.data; + inmemsize = info.size; + + mpg123_feed (mpg123_decoder->handle, inmemory, inmemsize); + decoded_bytes = NULL; + num_decoded_bytes = 0; + decode_error = mpg123_decode_frame (mpg123_decoder->handle, + &mpg123_decoder->frame_offset, &decoded_bytes, &num_decoded_bytes); + + gst_memory_unmap (memory, &info); + gst_memory_unref (memory); + } + + switch (decode_error) { + case MPG123_NEW_FORMAT: + /* + As mentioned in gst_mpg123_audio_dec_set_format(), the next audioinfo is not set immediately; + instead, the code waits for mpg123 to take note of the new format, and then sets the audioinfo + This fixes glitches with mp3s containing several format headers (for example, first half using 44.1kHz, second half 32 kHz) + */ + + GST_DEBUG_OBJECT (dec, + "mpg123 reported a new format -> setting next srccaps"); + + gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes, + num_decoded_bytes); + + /* + If there is a next audioinfo, use it, then set has_next_audioinfo to FALSE, to make sure + gst_audio_decoder_set_output_format() isn't called again until set_format is called by the base class + */ + if (mpg123_decoder->has_next_audioinfo) { + if (!gst_audio_decoder_set_output_format (dec, + &(mpg123_decoder->next_audioinfo))) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("Unable to set output format")); + } + mpg123_decoder->has_next_audioinfo = FALSE; + } + + break; + + case MPG123_NEED_MORE: + case MPG123_OK: + return gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, + decoded_bytes, num_decoded_bytes); + + /* If this happens, then the upstream parser somehow missed the ending of the bitstream */ + case MPG123_DONE: + GST_DEBUG_OBJECT (dec, "mpg123 is done decoding"); + gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes, + num_decoded_bytes); + return GST_FLOW_EOS; + + /* Anything else is considered an error */ + default: + { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Decoding error: %s", + mpg123_plain_strerror (decode_error))); + + return GST_FLOW_ERROR; + } + } + + return GST_FLOW_OK; +} + + +static gboolean +gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) +{ +/* + Using the parsed information upstream, and the list of allowed caps downstream, this code + tries to find a suitable audio info. It is important to keep in mind that the rate and number of channels + should never deviate from the one the bitstream has, otherwise mpg123 has to mix channels and/or + resample (and as its docs say, its internal resampler is very crude). The sample format, however, + can be chosen freely, because the MPEG specs do not mandate any special format. + Therefore, rate and number of channels are taken from upstream (which parsed the MPEG frames, so + the incoming_caps contain exactly the rate and number of channels the bitstream actually has), while + the sample format is chosen by trying out all caps that are allowed by downstream. This way, the output + is adjusted to what the downstream prefers. + + Also, the new output audio info is not set immediately. Instead, it is considered the "next audioinfo". + The code waits for mpg123 to notice the new format (= when mpg123_decode_frame() returns MPG123_AUDIO_DEC_NEW_FORMAT), + and then sets the next audioinfo. Otherwise, the next audioinfo is set too soon, which may cause problems with + mp3s containing several format headers. One example would be an mp3 with the first 30 seconds using 44.1 kHz, + then the next 30 seconds using 32 kHz. Rare, but possible. + + STEPS: + + 1. get rate and channels from incoming_caps + 2. get allowed caps from src pad + 3. for each structure in allowed caps: + 3.1. take format + 3.2. if the combination of format with rate and channels is unsupported by mpg123, go to (3), + or exit with error if there are no more structures to try + 3.3. create next audioinfo out of rate,channels,format, and exit +*/ + + + int rate, channels; + GstMpg123AudioDec *mpg123_decoder; + GstCaps *allowed_srccaps; + guint structure_nr; + gboolean match_found = FALSE; + + mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); + + if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("mpg123 handle is NULL")); + return FALSE; + } + + mpg123_decoder->has_next_audioinfo = FALSE; + + /* Get rate and channels from incoming_caps */ + { + GstStructure *structure; + gboolean err = FALSE; + + /* Only the first structure is used (multiple incoming structures don't make sense */ + structure = gst_caps_get_structure (incoming_caps, 0); + + if (!gst_structure_get_int (structure, "rate", &rate)) { + err = TRUE; + GST_ERROR_OBJECT (dec, "Incoming caps do not have a rate value"); + } + if (!gst_structure_get_int (structure, "channels", &channels)) { + err = TRUE; + GST_ERROR_OBJECT (dec, "Incoming caps do not have a channel value"); + } + + if (err) + return FALSE; + } + + /* Get the caps that are allowed by downstream */ + { + GstCaps *allowed_srccaps_unnorm = + gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec)); + allowed_srccaps = gst_caps_normalize (allowed_srccaps_unnorm); + /* TODO: this causes errors with 1.0 - perhaps a bug? */ + /*gst_caps_unref(allowed_srccaps_unnorm); */ + } + + /* Go through all allowed caps, pick the first one that matches */ + for (structure_nr = 0; structure_nr < gst_caps_get_size (allowed_srccaps); + ++structure_nr) { + GstStructure *structure; + gchar const *format_str; + GstAudioFormat format; + int encoding; + + structure = gst_caps_get_structure (allowed_srccaps, structure_nr); + + format_str = gst_structure_get_string (structure, "format"); + if (format_str == NULL) { + GST_DEBUG_OBJECT (dec, "Could not get format from src caps"); + continue; + } + + format = gst_audio_format_from_string (format_str); + if (format == GST_AUDIO_FORMAT_UNKNOWN) { + GST_DEBUG_OBJECT (dec, "Unknown format %s", format_str); + continue; + } + + switch (format) { +#ifdef MPG123_ENC_SIGNED_16 + case GST_AUDIO_FORMAT_S16: + encoding = MPG123_ENC_SIGNED_16; + break; +#endif +#ifdef MPG123_ENC_SIGNED_24 + case GST_AUDIO_FORMAT_S24: + encoding = MPG123_ENC_SIGNED_24; + break; +#endif +#ifdef MPG123_ENC_SIGNED_32 + case GST_AUDIO_FORMAT_S32: + encoding = MPG123_ENC_SIGNED_32; + break; +#endif +#ifdef MPG123_ENC_UNSIGNED_16 + case GST_AUDIO_FORMAT_U16: + encoding = MPG123_ENC_UNSIGNED_16; + break; +#endif +#ifdef MPG123_ENC_UNSIGNED_24 + case GST_AUDIO_FORMAT_U24: + encoding = MPG123_ENC_UNSIGNED_24; + break; +#endif +#ifdef MPG123_ENC_UNSIGNED_32 + case GST_AUDIO_FORMAT_U32: + encoding = MPG123_ENC_UNSIGNED_32; + break; +#endif +#ifdef MPG123_ENC_FLOAT_32 + case GST_AUDIO_FORMAT_F32: + encoding = MPG123_ENC_FLOAT_32; + break; +#endif + default: + GST_DEBUG_OBJECT (dec, + "Format %s in srccaps is not supported by mpg123", format_str); + continue; + } + + { + int err; + + /* Cleanup old formats & set new one */ + mpg123_format_none (mpg123_decoder->handle); + err = mpg123_format (mpg123_decoder->handle, rate, channels, encoding); + if (err != MPG123_OK) { + GST_DEBUG_OBJECT (dec, + "mpg123 cannot use caps %" GST_PTR_FORMAT + " because mpg123_format() failed: %s", structure, + mpg123_plain_strerror (err)); + continue; + } + } + + gst_audio_info_init (&(mpg123_decoder->next_audioinfo)); + gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, rate, + channels, NULL); + GST_DEBUG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels", + format_str, rate, channels); + mpg123_decoder->has_next_audioinfo = TRUE; + + match_found = TRUE; + + break; + } + + gst_caps_unref (allowed_srccaps); + + return match_found; +} + + +static void +gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) +{ + int error; + GstMpg123AudioDec *mpg123_decoder; + + hard = hard; + + GST_DEBUG_OBJECT (dec, "Flushing decoder"); + + mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); + + if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("mpg123 handle is NULL")); + return; + } + + /* Flush by reopening the feed */ + mpg123_close (mpg123_decoder->handle); + error = mpg123_open_feed (mpg123_decoder->handle); + + if (G_UNLIKELY (error != MPG123_OK)) { + GstElement *element = GST_ELEMENT (dec); + GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), + ("Error reopening mpg123 feed: %s", mpg123_plain_strerror (error))); + mpg123_close (mpg123_decoder->handle); + mpg123_delete (mpg123_decoder->handle); + mpg123_decoder->handle = NULL; + } + + mpg123_decoder->has_next_audioinfo = FALSE; + + /* + opening/closing feeds do not affect the format defined by the mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), + and since the up/downstream caps are not expected to change here, no mpg123_format() calls are done + */ +} + +static gboolean +plugin_init (GstPlugin * plugin) +{ + return gst_element_register (plugin, "mpg123audiodec", + GST_RANK_NONE, gst_mpg123_audio_dec_get_type ()); +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + mpg123, "mp3 decoding based on the mpg123 library", + plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) diff --git a/ext/mpg123/gstmpg123audiodec.h b/ext/mpg123/gstmpg123audiodec.h new file mode 100644 index 0000000000..e837a56b0c --- /dev/null +++ b/ext/mpg123/gstmpg123audiodec.h @@ -0,0 +1,62 @@ +/* MP3 decoding plugin for GStreamer using the mpg123 library + * Copyright (C) 2012 Carlos Rafael Giani + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __GST_MPG123_AUDIO_DEC_H__ +#define __GST_MPG123_AUDIO_DEC_H__ + +#include +#include +#include + + +G_BEGIN_DECLS + + +typedef struct _GstMpg123AudioDec GstMpg123AudioDec; +typedef struct _GstMpg123AudioDecClass GstMpg123AudioDecClass; + + +#define GST_TYPE_MPG123_AUDIO_DEC (gst_mpg123_audio_dec_get_type()) +#define GST_MPG123_AUDIO_DEC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPG123_AUDIO_DEC,GstMpg123AudioDec)) +#define GST_MPG123_AUDIO_DEC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_MPG123_AUDIO_DEC,GstMpg123AudioDecClass)) +#define GST_IS_MPG123_AUDIO_DEC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_MPG123_AUDIO_DEC)) +#define GST_IS_MPG123_AUDIO_DEC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_MPG123_AUDIO_DEC)) + +struct _GstMpg123AudioDec +{ + GstAudioDecoder parent; + + mpg123_handle *handle; + + GstAudioInfo next_audioinfo; + gboolean has_next_audioinfo; + + off_t frame_offset; +}; + + +struct _GstMpg123AudioDecClass +{ + GstAudioDecoderClass parent_class; +}; + +G_GNUC_INTERNAL GType gst_mpg123_audio_dec_get_type (void); + +G_END_DECLS + +#endif From 1b3603c0f974f76b81bd2efbc14c7d43364aa40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 3 Aug 2012 11:48:02 +0100 Subject: [PATCH 02/33] mpg123: hook up to build system --- ext/mpg123/Makefile.am | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ext/mpg123/Makefile.am diff --git a/ext/mpg123/Makefile.am b/ext/mpg123/Makefile.am new file mode 100644 index 0000000000..6e6f4385e8 --- /dev/null +++ b/ext/mpg123/Makefile.am @@ -0,0 +1,27 @@ +plugin_LTLIBRARIES = libgstmpg123.la + +libgstmpg123_la_SOURCES = gstmpg123audiodec.c +libgstmpg123_la_CFLAGS = -DGST_USE_UNSTABLE_API \ + $(GST_PLUGINS_BASE_CFLAGS) \ + $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(MPG123_CFLAGS) +libgstmpg123_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstaudio-@GST_API_VERSION@ \ + $(GST_BASE_LIBS) $(GST_LIBS) $(MPG123_LIBS) +libgstmpg123_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) +libgstmpg123_la_LIBTOOLFLAGS = --tag=disable-static + +noinst_HEADERS = gstmpg123audiodec.c + +Android.mk: Makefile.am $(BUILT_SOURCES) + androgenizer \ + -:PROJECT libgstmpg123 -:SHARED libgstmpg123 \ + -:TAGS eng debug \ + -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \ + -:SOURCES $(libgstmpg123_la_SOURCES) \ + -:CPPFLAGS $(CPPFLAGS) \ + -:CFLAGS $(DEFS) $(DEFAULT_INCLUDES) $(libgstmpg123_la_CFLAGS) \ + -:LDFLAGS $(libgstmpg123_la_LDFLAGS) \ + $(libgstmpg123_la_LIBADD) \ + -ldl \ + -:PASSTHROUGH LOCAL_ARM_MODE:=arm \ + LOCAL_MODULE_PATH:='$$(TARGET_OUT)/lib/gstreamer-@GST_API_VERSION@' \ + > $@ From 99c6ceff03326f9b550fc8ba18d11d5109b0289f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 3 Aug 2012 11:50:10 +0100 Subject: [PATCH 03/33] mpg123: query supported output formats at run-time Fixes stuff. We use a string here since we can't be bothered with GValue. --- ext/mpg123/gstmpg123audiodec.c | 116 ++++++++++++++++----------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 9e7f61dfe3..ac0a61be19 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -79,7 +79,6 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) GObjectClass *object_class; GstAudioDecoderClass *base_class; GstElementClass *element_class; - GstCaps *src_caps; GstPadTemplate *src_template; int error; @@ -102,57 +101,66 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) created depending on whatever mpg123 supports */ { - gchar *format_string; - gchar *caps_string; + const int *format_list; + const long *rates_list; + size_t num, i; + GString *s; - static gchar const *src_caps_begin = "audio/x-raw, " "format = { "; - static gchar const *src_caps_end = - " }, " - "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, " - "channels = (int) [ 1, 2 ], " "layout = (string) interleaved; "; + s = g_string_new ("audio/x-raw, "); -#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) -#define ENDIAN_POSTFIX "LE" -#else -#define ENDIAN_POSTFIX "BE" -#endif + mpg123_encodings (&format_list, &num); + g_string_append (s, "format = { "); + for (i = 0; i < num; ++i) { + switch (format_list[i]) { + case MPG123_ENC_SIGNED_16: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (S16)); + break; + case MPG123_ENC_UNSIGNED_16: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (U16)); + break; + case MPG123_ENC_SIGNED_24: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (S24)); + break; + case MPG123_ENC_UNSIGNED_24: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (U24)); + break; + case MPG123_ENC_SIGNED_32: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (S32)); + break; + case MPG123_ENC_UNSIGNED_32: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (U32)); + break; + case MPG123_ENC_FLOAT_32: + g_string_append (s, (i > 0) ? ", " : ""); + g_string_append (s, GST_AUDIO_NE (F32)); + break; + default: + GST_DEBUG ("Ignoring mpg123 format %d", format_list[i]); + break; + } + } + g_string_append (s, " }, "); - static gchar *supported_formats[] = { -#ifdef MPG123_ENC_SIGNED_16 - "S16" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_UNSIGNED_16 - "U16" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_SIGNED_24 - "S24" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_UNSIGNED_24 - "U24" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_SIGNED_32 - "S32" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_UNSIGNED_32 - "U32" ENDIAN_POSTFIX, -#endif -#ifdef MPG123_ENC_FLOAT_32 - "F32" ENDIAN_POSTFIX, -#endif - NULL - }; + mpg123_rates (&rates_list, &num); + g_string_append (s, "rate = (int) { "); + for (i = 0; i < num; ++i) { + g_string_append_printf (s, "%s%lu", (i > 0) ? ", " : "", rates_list[i]); + } + g_string_append (s, "}, "); - format_string = g_strjoinv (", ", supported_formats); - caps_string = - g_strjoin (NULL, src_caps_begin, format_string, src_caps_end, NULL); + g_string_append (s, "channels = (int) [ 1, 2 ], "); + g_string_append (s, "layout = (string) interleaved"); - src_caps = gst_caps_from_string (caps_string); - src_template = - gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - gst_caps_ref (src_caps)); + src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, + gst_caps_from_string (s->str)); - g_free (format_string); - g_free (caps_string); + g_string_free (s, TRUE); } gst_element_class_add_pad_template (element_class, @@ -504,44 +512,30 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) } switch (format) { -#ifdef MPG123_ENC_SIGNED_16 case GST_AUDIO_FORMAT_S16: encoding = MPG123_ENC_SIGNED_16; break; -#endif -#ifdef MPG123_ENC_SIGNED_24 case GST_AUDIO_FORMAT_S24: encoding = MPG123_ENC_SIGNED_24; break; -#endif -#ifdef MPG123_ENC_SIGNED_32 case GST_AUDIO_FORMAT_S32: encoding = MPG123_ENC_SIGNED_32; break; -#endif -#ifdef MPG123_ENC_UNSIGNED_16 case GST_AUDIO_FORMAT_U16: encoding = MPG123_ENC_UNSIGNED_16; break; -#endif -#ifdef MPG123_ENC_UNSIGNED_24 case GST_AUDIO_FORMAT_U24: encoding = MPG123_ENC_UNSIGNED_24; break; -#endif -#ifdef MPG123_ENC_UNSIGNED_32 case GST_AUDIO_FORMAT_U32: encoding = MPG123_ENC_UNSIGNED_32; break; -#endif -#ifdef MPG123_ENC_FLOAT_32 case GST_AUDIO_FORMAT_F32: encoding = MPG123_ENC_FLOAT_32; break; -#endif default: GST_DEBUG_OBJECT (dec, - "Format %s in srccaps is not supported by mpg123", format_str); + "Format %s in srccaps is not supported", format_str); continue; } From cef7f52b80d14572c519636202ccae96585cdda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 3 Aug 2012 13:43:31 +0100 Subject: [PATCH 04/33] mpg123: map input buffer in READ mode, not WRITE mode Makes things actually work. --- ext/mpg123/gstmpg123audiodec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index ac0a61be19..ec6a2fc166 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -335,7 +335,7 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) if (memory == NULL) return GST_FLOW_ERROR; - if (!gst_memory_map (memory, &info, GST_MAP_WRITE)) { + if (!gst_memory_map (memory, &info, GST_MAP_READ)) { gst_memory_unref (memory); return GST_FLOW_ERROR; } From 189e8d5a5027a396d8348b6ddfba8acc2fc4a821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 8 Aug 2012 12:58:50 +0100 Subject: [PATCH 05/33] mpg123: dist header file --- ext/mpg123/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/Makefile.am b/ext/mpg123/Makefile.am index 6e6f4385e8..365fc764f8 100644 --- a/ext/mpg123/Makefile.am +++ b/ext/mpg123/Makefile.am @@ -9,7 +9,7 @@ libgstmpg123_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstaudio-@GST_API_VERSION@ \ libgstmpg123_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstmpg123_la_LIBTOOLFLAGS = --tag=disable-static -noinst_HEADERS = gstmpg123audiodec.c +noinst_HEADERS = gstmpg123audiodec.h Android.mk: Makefile.am $(BUILT_SOURCES) androgenizer \ From 264ec5c74ba7117a244dbcf0bc924d6e34b4ea63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 9 Aug 2012 11:48:39 +0200 Subject: [PATCH 06/33] mpg123: Give MARGINAL rank to the mpg123 decoder element --- ext/mpg123/gstmpg123audiodec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index ec6a2fc166..adaa54f0a3 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -616,7 +616,7 @@ static gboolean plugin_init (GstPlugin * plugin) { return gst_element_register (plugin, "mpg123audiodec", - GST_RANK_NONE, gst_mpg123_audio_dec_get_type ()); + GST_RANK_MARGINAL, gst_mpg123_audio_dec_get_type ()); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, From 7a801e1022e335bf0df2c24c931e861ba14495c5 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Wed, 24 Oct 2012 00:21:45 +0200 Subject: [PATCH 07/33] mpg123: cleaned up comments, formatting, and logging lines also replaced mpg123decoder->handle != NULL checks with asserts https://bugzilla.gnome.org/show_bug.cgi?id=686595 --- ext/mpg123/gstmpg123audiodec.c | 304 +++++++++++++++++---------------- 1 file changed, 156 insertions(+), 148 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index adaa54f0a3..5b05dbeb85 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -28,8 +28,7 @@ GST_DEBUG_CATEGORY_STATIC (mpg123_debug); #define GST_CAT_DEFAULT mpg123_debug -/* - * Omitted sample formats that mpg123 supports (or at least can support): +/* Omitted sample formats that mpg123 supports (or at least can support): * - 8bit integer signed * - 8bit integer unsigned * - a-law @@ -46,10 +45,10 @@ GST_DEBUG_CATEGORY_STATIC (mpg123_debug); * no way how to find out which one of them is actually used. * * However, in all known installations, "real" equals 32bit float, so that's - * what is used. - */ + * what is used. */ -static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", +static GstStaticPadTemplate static_sink_template = +GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/mpeg, " @@ -66,9 +65,9 @@ static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, unsigned char const *decoded_bytes, size_t const num_decoded_bytes); static GstFlowReturn gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, - GstBuffer * buffer); + GstBuffer * input_buffer); static gboolean gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, - GstCaps * incoming_caps); + GstCaps * input_caps); static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard); G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER); @@ -79,7 +78,7 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) GObjectClass *object_class; GstAudioDecoderClass *base_class; GstElementClass *element_class; - GstPadTemplate *src_template; + GstPadTemplate *src_template, *sink_template; int error; GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder"); @@ -96,15 +95,14 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) "Decodes mp3 streams using the mpg123 library", "Carlos Rafael Giani "); - /* - Not using static pad template for srccaps, since the comma-separated list of formats needs to be - created depending on whatever mpg123 supports - */ + /* Not using static pad template for srccaps, since the comma-separated list + * of formats needs to be created depending on whatever mpg123 supports */ { const int *format_list; const long *rates_list; size_t num, i; GString *s; + GstCaps *src_template_caps; s = g_string_new ("audio/x-raw, "); @@ -157,14 +155,16 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) g_string_append (s, "channels = (int) [ 1, 2 ], "); g_string_append (s, "layout = (string) interleaved"); + src_template_caps = gst_caps_from_string (s->str); src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - gst_caps_from_string (s->str)); + src_template_caps); g_string_free (s, TRUE); } - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&sink_template)); + sink_template = gst_static_pad_template_get (&static_sink_template); + + gst_element_class_add_pad_template (element_class, sink_template); gst_element_class_add_pad_template (element_class, src_template); base_class->start = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_start); @@ -179,7 +179,7 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) GST_ERROR ("Could not initialize mpg123 library: %s", mpg123_plain_strerror (error)); else - GST_TRACE ("mpg123 library initialized"); + GST_INFO ("mpg123 library initialized"); } @@ -214,30 +214,34 @@ gst_mpg123_audio_dec_start (GstAudioDecoder * dec) mpg123_decoder->has_next_audioinfo = FALSE; mpg123_decoder->frame_offset = 0; - /* - Initially, the mpg123 handle comes with a set of default formats supported. This clears this set. - This is necessary, since only one format shall be supported (see set_format for more). - */ + /* Initially, the mpg123 handle comes with a set of default formats + * supported. This clears this set. This is necessary, since only one + * format shall be supported (see set_format for more). */ mpg123_format_none (mpg123_decoder->handle); - mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); /* Built-in mpg123 support for gapless decoding is disabled for now, since it does not work well with seeking */ - mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; essential for MP3 radio streams */ - mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Sets the resync limit to the end of the stream (e.g. don't give up prematurely) */ + /* Built-in mpg123 support for gapless decoding is disabled for now, + * since it does not work well with seeking */ + mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); + /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; + * essential for MP3 radio streams */ + mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); + /* Sets the resync limit to the end of the stream (otherwise mpg123 may give + * up on decoding prematurely, especially with mp3 web radios) */ + mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Open in feed mode (= encoded data is fed manually into the handle). */ error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("Error opening mpg123 feed: %s", mpg123_plain_strerror (error))); + GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL), + ("%s", mpg123_strerror (mpg123_decoder->handle))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; return FALSE; } - GST_DEBUG_OBJECT (dec, "mpg123 decoder started"); + GST_INFO_OBJECT (dec, "mpg123 decoder started"); return TRUE; } @@ -254,7 +258,7 @@ gst_mpg123_audio_dec_stop (GstAudioDecoder * dec) mpg123_decoder->handle = NULL; } - GST_DEBUG_OBJECT (dec, "mpg123 decoder stopped"); + GST_INFO_OBJECT (dec, "mpg123 decoder stopped"); return TRUE; } @@ -265,39 +269,36 @@ gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, unsigned char const *decoded_bytes, size_t const num_decoded_bytes) { GstBuffer *output_buffer; - GstFlowReturn alloc_error; GstAudioDecoder *dec; output_buffer = NULL; dec = GST_AUDIO_DECODER (mpg123_decoder); if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) { - /* This occurs in the first few frames, which do not carry data; once MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */ - GST_TRACE_OBJECT (mpg123_decoder, - "Nothing was decoded -> no output buffer to push"); + /* This occurs in the first few frames, which do not carry data; once + * MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */ + GST_DEBUG_OBJECT (mpg123_decoder, + "cannot decode yet, need more data -> no output buffer to push"); return GST_FLOW_OK; } output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL); - alloc_error = (output_buffer == NULL) ? GST_FLOW_ERROR : GST_FLOW_OK; - if (alloc_error != GST_FLOW_OK) { - /* This is necessary to advance playback in time, even when nothing was decoded. */ + if (output_buffer == NULL) { + /* This is necessary to advance playback in time, + * even when nothing was decoded. */ return gst_audio_decoder_finish_frame (dec, NULL, 1); } else { GstMapInfo info; if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) { - if (info.size != num_decoded_bytes) - GST_ERROR_OBJECT (mpg123_decoder, - "Mapped memory region has size %u instead of expected size %u", - info.size, num_decoded_bytes); - else - memcpy (info.data, decoded_bytes, num_decoded_bytes); - + memcpy (info.data, decoded_bytes, num_decoded_bytes); gst_buffer_unmap (output_buffer, &info); - } else - GST_ERROR_OBJECT (mpg123_decoder, "Could not map buffer"); + } else { + GST_ERROR_OBJECT (mpg123_decoder, "gst_buffer_map() returned NULL"); + gst_buffer_unref (output_buffer); + output_buffer = NULL; + } return gst_audio_decoder_finish_frame (dec, output_buffer, 1); } @@ -305,78 +306,66 @@ gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, static GstFlowReturn -gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) +gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, + GstBuffer * input_buffer) { GstMpg123AudioDec *mpg123_decoder; int decode_error; unsigned char *decoded_bytes; size_t num_decoded_bytes; + GstFlowReturn retval; - if (G_UNLIKELY (!buffer)) + if (G_UNLIKELY (input_buffer == NULL)) return GST_FLOW_OK; mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); - if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("mpg123 handle is NULL")); - return GST_FLOW_ERROR; - } + g_assert (mpg123_decoder->handle != NULL); /* The actual decoding */ { - unsigned char const *inmemory; - size_t inmemsize; - GstMemory *memory; GstMapInfo info; - memory = gst_buffer_get_all_memory (buffer); - if (memory == NULL) - return GST_FLOW_ERROR; - - if (!gst_memory_map (memory, &info, GST_MAP_READ)) { - gst_memory_unref (memory); + /* feed input data */ + if (gst_buffer_map (input_buffer, &info, GST_MAP_READ)) { + mpg123_feed (mpg123_decoder->handle, info.data, info.size); + gst_buffer_unmap (input_buffer, &info); + } else { + GST_ERROR_OBJECT (mpg123_decoder, "gst_memory_map() failed"); return GST_FLOW_ERROR; } - inmemory = info.data; - inmemsize = info.size; - - mpg123_feed (mpg123_decoder->handle, inmemory, inmemsize); + /* Try to decode a frame */ decoded_bytes = NULL; num_decoded_bytes = 0; decode_error = mpg123_decode_frame (mpg123_decoder->handle, &mpg123_decoder->frame_offset, &decoded_bytes, &num_decoded_bytes); - - gst_memory_unmap (memory, &info); - gst_memory_unref (memory); } + retval = GST_FLOW_OK; + switch (decode_error) { case MPG123_NEW_FORMAT: - /* - As mentioned in gst_mpg123_audio_dec_set_format(), the next audioinfo is not set immediately; - instead, the code waits for mpg123 to take note of the new format, and then sets the audioinfo - This fixes glitches with mp3s containing several format headers (for example, first half using 44.1kHz, second half 32 kHz) - */ + /* As mentioned in gst_mpg123_audio_dec_set_format(), the next audioinfo + * is not set immediately; instead, the code waits for mpg123 to take + * note of the new format, and then sets the audioinfo. This fixes glitches + * with mp3s containing several format headers (for example, first half + * using 44.1kHz, second half 32 kHz) */ - GST_DEBUG_OBJECT (dec, + GST_LOG_OBJECT (dec, "mpg123 reported a new format -> setting next srccaps"); gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes, num_decoded_bytes); - /* - If there is a next audioinfo, use it, then set has_next_audioinfo to FALSE, to make sure - gst_audio_decoder_set_output_format() isn't called again until set_format is called by the base class - */ + /* If there is a next audioinfo, use it, then set has_next_audioinfo to + * FALSE, to make sure gst_audio_decoder_set_output_format() isn't called + * again until set_format is called by the base class */ if (mpg123_decoder->has_next_audioinfo) { if (!gst_audio_decoder_set_output_format (dec, &(mpg123_decoder->next_audioinfo))) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("Unable to set output format")); + GST_WARNING_OBJECT (dec, "Unable to set output format"); + retval = GST_FLOW_NOT_NEGOTIATED; } mpg123_decoder->has_next_audioinfo = FALSE; } @@ -385,61 +374,91 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) case MPG123_NEED_MORE: case MPG123_OK: - return gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, + retval = gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes, num_decoded_bytes); + break; - /* If this happens, then the upstream parser somehow missed the ending of the bitstream */ case MPG123_DONE: - GST_DEBUG_OBJECT (dec, "mpg123 is done decoding"); + /* If this happens, then the upstream parser somehow missed the ending + * of the bitstream */ + GST_LOG_OBJECT (dec, "mpg123 is done decoding"); gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes, num_decoded_bytes); - return GST_FLOW_EOS; + retval = GST_FLOW_EOS; + break; - /* Anything else is considered an error */ default: { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Decoding error: %s", - mpg123_plain_strerror (decode_error))); + /* Anything else is considered an error */ + int errcode; + switch (decode_error) { + case MPG123_ERR: + errcode = mpg123_errcode (mpg123_decoder->handle); + break; + default: + errcode = decode_error; + } + switch (errcode) { + case MPG123_BAD_OUTFORMAT:{ + GstCaps *input_caps = + gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (dec)); + GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL), + ("Output sample format could not be used when trying to decode frame. " + "This is typically caused when the input caps (often the sample " + "rate) do not match the actual format of the audio data. " + "Input caps: %" GST_PTR_FORMAT, input_caps) + ); + gst_caps_unref (input_caps); + break; + } + default:{ + char const *errmsg = mpg123_plain_strerror (errcode); + GST_ERROR_OBJECT (dec, "Reported error: %s", errmsg); + } + } - return GST_FLOW_ERROR; + retval = GST_FLOW_ERROR; } } - return GST_FLOW_OK; + return retval; } static gboolean -gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) +gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) { -/* - Using the parsed information upstream, and the list of allowed caps downstream, this code - tries to find a suitable audio info. It is important to keep in mind that the rate and number of channels - should never deviate from the one the bitstream has, otherwise mpg123 has to mix channels and/or - resample (and as its docs say, its internal resampler is very crude). The sample format, however, - can be chosen freely, because the MPEG specs do not mandate any special format. - Therefore, rate and number of channels are taken from upstream (which parsed the MPEG frames, so - the incoming_caps contain exactly the rate and number of channels the bitstream actually has), while - the sample format is chosen by trying out all caps that are allowed by downstream. This way, the output - is adjusted to what the downstream prefers. - - Also, the new output audio info is not set immediately. Instead, it is considered the "next audioinfo". - The code waits for mpg123 to notice the new format (= when mpg123_decode_frame() returns MPG123_AUDIO_DEC_NEW_FORMAT), - and then sets the next audioinfo. Otherwise, the next audioinfo is set too soon, which may cause problems with - mp3s containing several format headers. One example would be an mp3 with the first 30 seconds using 44.1 kHz, - then the next 30 seconds using 32 kHz. Rare, but possible. - - STEPS: - - 1. get rate and channels from incoming_caps - 2. get allowed caps from src pad - 3. for each structure in allowed caps: - 3.1. take format - 3.2. if the combination of format with rate and channels is unsupported by mpg123, go to (3), - or exit with error if there are no more structures to try - 3.3. create next audioinfo out of rate,channels,format, and exit -*/ +/* Using the parsed information upstream, and the list of allowed caps + * downstream, this code tries to find a suitable audio info. It is important + * to keep in mind that the rate and number of channels should never deviate + * from the one the bitstream has, otherwise mpg123 has to mix channels and/or + * resample (and as its docs say, its internal resampler is very crude). The + * sample format, however, can be chosen freely, because the MPEG specs do not + * mandate any special format. Therefore, rate and number of channels are taken + * from upstream (which parsed the MPEG frames, so the input_caps contain + * exactly the rate and number of channels the bitstream actually has), while + * the sample format is chosen by trying out all caps that are allowed by + * downstream. This way, the output is adjusted to what the downstream prefers. + * + * Also, the new output audio info is not set immediately. Instead, it is + * considered the "next audioinfo". The code waits for mpg123 to notice the new + * format (= when mpg123_decode_frame() returns MPG123_AUDIO_DEC_NEW_FORMAT), + * and then sets the next audioinfo. Otherwise, the next audioinfo is set too + * soon, which may cause problems with mp3s containing several format headers. + * One example would be an mp3 with the first 30 seconds using 44.1 kHz, then + * the next 30 seconds using 32 kHz. Rare, but possible. + * + * STEPS: + * + * 1. get rate and channels from input_caps + * 2. get allowed caps from src pad + * 3. for each structure in allowed caps: + * 3.1. take format + * 3.2. if the combination of format with rate and channels is unsupported by + * mpg123, go to (3), or exit with error if there are no more structures + * to try + * 3.3. create next audioinfo out of rate,channels,format, and exit + */ int rate, channels; @@ -450,30 +469,26 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); - if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("mpg123 handle is NULL")); - return FALSE; - } + g_assert (mpg123_decoder->handle != NULL); mpg123_decoder->has_next_audioinfo = FALSE; - /* Get rate and channels from incoming_caps */ + /* Get rate and channels from input_caps */ { GstStructure *structure; gboolean err = FALSE; - /* Only the first structure is used (multiple incoming structures don't make sense */ - structure = gst_caps_get_structure (incoming_caps, 0); + /* Only the first structure is used (multiple + * input caps structures don't make sense */ + structure = gst_caps_get_structure (input_caps, 0); if (!gst_structure_get_int (structure, "rate", &rate)) { err = TRUE; - GST_ERROR_OBJECT (dec, "Incoming caps do not have a rate value"); + GST_ERROR_OBJECT (dec, "Input caps do not have a rate value"); } if (!gst_structure_get_int (structure, "channels", &channels)) { err = TRUE; - GST_ERROR_OBJECT (dec, "Incoming caps do not have a channel value"); + GST_ERROR_OBJECT (dec, "Input caps do not have a channel value"); } if (err) @@ -485,8 +500,6 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) GstCaps *allowed_srccaps_unnorm = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec)); allowed_srccaps = gst_caps_normalize (allowed_srccaps_unnorm); - /* TODO: this causes errors with 1.0 - perhaps a bug? */ - /*gst_caps_unref(allowed_srccaps_unnorm); */ } /* Go through all allowed caps, pick the first one that matches */ @@ -549,7 +562,7 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) GST_DEBUG_OBJECT (dec, "mpg123 cannot use caps %" GST_PTR_FORMAT " because mpg123_format() failed: %s", structure, - mpg123_plain_strerror (err)); + mpg123_strerror (mpg123_decoder->handle)); continue; } } @@ -557,7 +570,7 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps) gst_audio_info_init (&(mpg123_decoder->next_audioinfo)); gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, rate, channels, NULL); - GST_DEBUG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels", + GST_LOG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels", format_str, rate, channels); mpg123_decoder->has_next_audioinfo = TRUE; @@ -580,25 +593,20 @@ gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) hard = hard; - GST_DEBUG_OBJECT (dec, "Flushing decoder"); + GST_LOG_OBJECT (dec, "Flushing decoder"); mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); - if (G_UNLIKELY (mpg123_decoder->handle == NULL)) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("mpg123 handle is NULL")); - return; - } + g_assert (mpg123_decoder->handle != NULL); /* Flush by reopening the feed */ mpg123_close (mpg123_decoder->handle); error = mpg123_open_feed (mpg123_decoder->handle); if (G_UNLIKELY (error != MPG123_OK)) { - GstElement *element = GST_ELEMENT (dec); - GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), - ("Error reopening mpg123 feed: %s", mpg123_plain_strerror (error))); + GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL), + ("Error while reopening mpg123 feed: %s", + mpg123_plain_strerror (error))); mpg123_close (mpg123_decoder->handle); mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; @@ -606,10 +614,10 @@ gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) mpg123_decoder->has_next_audioinfo = FALSE; - /* - opening/closing feeds do not affect the format defined by the mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), - and since the up/downstream caps are not expected to change here, no mpg123_format() calls are done - */ + /* opening/closing feeds do not affect the format defined by the + * mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), + * and since the up/downstream caps are not expected to change here, no + * mpg123_format() calls are done */ } static gboolean From 27e4af5c84e95357ca4696ae84e737bd8be60a69 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Wed, 24 Oct 2012 00:22:05 +0200 Subject: [PATCH 08/33] mpg123: fixed bug with last frame, disabled internal resampler & chatter * The last MP3 frame wasn't being pushed when base class was draining * Made sure mpg123 cannot ever use its (crude) internal resampler * Disabled mpg123 stderr output https://bugzilla.gnome.org/show_bug.cgi?id=686595 --- ext/mpg123/gstmpg123audiodec.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 5b05dbeb85..5052eaba1b 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -228,6 +228,11 @@ gst_mpg123_audio_dec_start (GstAudioDecoder * dec) /* Sets the resync limit to the end of the stream (otherwise mpg123 may give * up on decoding prematurely, especially with mp3 web radios) */ mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); + /* Don't let mpg123 resample output */ + mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, + MPG123_AUTO_RESAMPLE, 0); + /* Don't let mpg123 print messages to stdout/stderr */ + mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0); /* Open in feed mode (= encoded data is fed manually into the handle). */ error = mpg123_open_feed (mpg123_decoder->handle); @@ -315,24 +320,23 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, size_t num_decoded_bytes; GstFlowReturn retval; - if (G_UNLIKELY (input_buffer == NULL)) - return GST_FLOW_OK; - mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); g_assert (mpg123_decoder->handle != NULL); /* The actual decoding */ { - GstMapInfo info; + /* feed input data (if there is any) */ + if (G_LIKELY (input_buffer != NULL)) { + GstMapInfo info; - /* feed input data */ - if (gst_buffer_map (input_buffer, &info, GST_MAP_READ)) { - mpg123_feed (mpg123_decoder->handle, info.data, info.size); - gst_buffer_unmap (input_buffer, &info); - } else { - GST_ERROR_OBJECT (mpg123_decoder, "gst_memory_map() failed"); - return GST_FLOW_ERROR; + if (gst_buffer_map (input_buffer, &info, GST_MAP_READ)) { + mpg123_feed (mpg123_decoder->handle, info.data, info.size); + gst_buffer_unmap (input_buffer, &info); + } else { + GST_ERROR_OBJECT (mpg123_decoder, "gst_memory_map() failed"); + return GST_FLOW_ERROR; + } } /* Try to decode a frame */ From 98b2303a3d3a9d8446e8a63306199184d51138e6 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Wed, 24 Oct 2012 00:36:42 +0200 Subject: [PATCH 09/33] mpg123: added gtkdoc section https://bugzilla.gnome.org/show_bug.cgi?id=686595 --- ext/mpg123/gstmpg123audiodec.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 5052eaba1b..16bcfce06e 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -16,6 +16,20 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/** + * SECTION: element-mpg123audiodec + * @see_also: lamemp3enc, mad + * + * Audio decoder for MPEG-1 layer 1/2/3 audio data. + * + * + * Example pipelines + * |[ + * gst-launch filesrc location=music.mp3 ! mpegaudioparse ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink + * ]| Decode and play the mp3 file + * + */ + #ifdef HAVE_CONFIG_H #include #endif From a5ee599af785cfbbeada69be1d54913a66fb45a5 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Wed, 24 Oct 2012 12:30:10 +0200 Subject: [PATCH 10/33] tets: add unit test for mpg123audiodec https://bugzilla.gnome.org/show_bug.cgi?id=686595 --- tests/check/elements/mpg123audiodec.c | 581 ++++++++++++++++++++++++++ 1 file changed, 581 insertions(+) create mode 100644 tests/check/elements/mpg123audiodec.c diff --git a/tests/check/elements/mpg123audiodec.c b/tests/check/elements/mpg123audiodec.c new file mode 100644 index 0000000000..fd7c620e30 --- /dev/null +++ b/tests/check/elements/mpg123audiodec.c @@ -0,0 +1,581 @@ +/* GStreamer + * + * unit test for mpg123audiodec + * + * Copyright (c) 2012 Carlos Rafael Giani + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + +/* For ease of programming we use globals to keep refs for our floating + * src and sink pads we create; otherwise we always have to do get_pad, + * get_peer, and then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + + +#define MP2_STREAM_FILENAME "stream.mp2" +#define MP3_CBR_STREAM_FILENAME "cbr_stream.mp3" +#define MP3_VBR_STREAM_FILENAME "vbr_stream.mp3" + + +/* mpeg 1 layer 2 stream created with: + * gst-launch-1.0 -v audiotestsrc wave=sine freq=440 volume=1 num-buffers=32 ! \ + * "audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1" ! \ + * avenc_mp2 bitrate=32000 ! tee name=t \ + * t. ! queue ! fakesink silent=false \ + * t. ! queue ! filesink location=test.mp2 + * + * mpeg 1 layer 3 CBR stream created with: + * gst-launch-1.0 -v audiotestsrc wave=sine freq=440 volume=1 num-buffers=32 ! \ + * "audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1" ! \ + * lamemp3enc encoding-engine-quality=high cbr=true target=bitrate bitrate=32 ! \ + * "audio/mpeg, rate=(int)44100, channels=(int)1" ! tee name=t \ + * t. ! queue ! fakesink silent=false \ + * t. ! queue ! filesink location=test.mp3 + * + * mpeg 1 layer 3 VBR stream created with: + * gst-launch-1.0 -v audiotestsrc wave=sine freq=440 volume=1 num-buffers=32 ! \ + * "audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int)44100, channels=(int)1" ! \ + * lamemp3enc encoding-engine-quality=high cbr=false target=quality quality=7 ! \ + * "audio/mpeg, rate=(int)44100, channels=(int)1" ! tee name=t \ + * t. ! queue ! fakesink silent=false \ + * t. ! queue ! filesink location=test.mp3 + */ + + +/* FFT test helpers taken from gst-plugins-base tests/check/audioresample.c */ + +#define FFT_HELPERS(type,ffttag,ffttag2,scale) \ +static gdouble magnitude##ffttag (const GstFFT##ffttag##Complex *c) \ +{ \ + gdouble mag = (gdouble) c->r * (gdouble) c->r; \ + mag += (gdouble) c->i * (gdouble) c->i; \ + mag /= scale * scale; \ + mag = 10.0 * log10 (mag); \ + return mag; \ +} \ +static gdouble find_main_frequency_spot_##ffttag ( \ + const GstFFT##ffttag##Complex *v, int elements) \ +{ \ + int i; \ + gdouble maxmag = -9999; \ + int maxidx = 0; \ + for (i=0; i maxmag) { \ + maxmag = mag; \ + maxidx = i; \ + } \ + } \ + return maxidx / (gdouble) elements; \ +} \ +static gboolean is_zero_except_##ffttag (const GstFFT##ffttag##Complex *v, \ + int elements, gdouble spot) \ +{ \ + int i; \ + for (i=0; i 0.01) { \ + if (mag > -35.0) { \ + GST_LOG("Found magnitude at %f : %f (peak at %f)\n", pos, mag, spot); \ + return FALSE; \ + } \ + } \ + } \ + return TRUE; \ +} \ +static void check_main_frequency_spot_##ffttag (GstBuffer *buffer, gdouble \ + expected_spot) \ +{ \ + GstMapInfo map; \ + int num_samples; \ + gdouble actual_spot; \ + GstFFT##ffttag *ctx; \ + GstFFT##ffttag##Complex *fftdata; \ + \ + gst_buffer_map (buffer, &map, GST_MAP_READ); \ + \ + num_samples = map.size / sizeof(type) & ~1; \ + ctx = gst_fft_##ffttag2##_new (num_samples, FALSE); \ + fftdata = g_new (GstFFT##ffttag##Complex, num_samples / 2 + 1); \ + \ + gst_fft_##ffttag2##_window (ctx, (type*)map.data, \ + GST_FFT_WINDOW_HAMMING); \ + gst_fft_##ffttag2##_fft (ctx, (type*)map.data, fftdata); \ + \ + actual_spot = find_main_frequency_spot_##ffttag (fftdata, \ + num_samples / 2 + 1); \ + GST_LOG ("Expected spot: %.3f actual: %.3f %f", expected_spot, actual_spot, \ + fabs (expected_spot - actual_spot)); \ + fail_unless (fabs (expected_spot - actual_spot) < 0.05, \ + "Actual main frequency spot is too far away from expected one"); \ + fail_unless (is_zero_except_##ffttag (fftdata, num_samples / 2 + 1, \ + actual_spot), "One secondary peak in spectrum exceeds threshold"); \ + \ + gst_buffer_unmap (buffer, &map); \ + \ + gst_fft_##ffttag2##_free (ctx); \ + g_free (fftdata); \ +} +FFT_HELPERS (gint32, S32, s32, 2147483647.0); + + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw, format = (string) S32LE ") + ); +static GstStaticPadTemplate layer2_srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS_ANY); +static GstStaticPadTemplate layer3_srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS_ANY); + + +static void +setup_input_pipeline (gchar const *stream_filename, GstElement ** pipeline, + GstElement ** appsink) +{ + GstElement *source, *parser; + + *pipeline = gst_pipeline_new (NULL); + source = gst_element_factory_make ("filesrc", NULL); + parser = gst_element_factory_make ("mpegaudioparse", NULL); + *appsink = gst_element_factory_make ("appsink", NULL); + + gst_bin_add_many (GST_BIN (*pipeline), source, parser, *appsink, NULL); + gst_element_link_many (source, parser, *appsink, NULL); + + { + char *full_filename = + g_build_filename (GST_TEST_FILES_PATH, stream_filename, NULL); + g_object_set (G_OBJECT (source), "location", full_filename, NULL); + g_free (full_filename); + } + + gst_element_set_state (*pipeline, GST_STATE_PLAYING); +} + +static void +cleanup_input_pipeline (GstElement * pipeline) +{ + gst_element_set_state (pipeline, GST_STATE_NULL); + gst_object_unref (pipeline); +} + +static GstElement * +setup_mpeg1layer2dec (void) +{ + GstElement *mpg123audiodec; + GstSegment seg; + GstCaps *caps; + + GST_DEBUG ("setup_mpeg1layer2dec"); + mpg123audiodec = gst_check_setup_element ("mpg123audiodec"); + mysrcpad = gst_check_setup_src_pad (mpg123audiodec, &layer2_srctemplate); + mysinkpad = gst_check_setup_sink_pad (mpg123audiodec, &sinktemplate); + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + gst_segment_init (&seg, GST_FORMAT_TIME); + gst_pad_push_event (mysrcpad, gst_event_new_segment (&seg)); + + /* This is necessary to trigger a set_format call in the decoder; + * fixed caps don't trigger it */ + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 1, + "layer", G_TYPE_INT, 2, + "rate", G_TYPE_INT, 44100, + "channels", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); + gst_pad_set_caps (mysrcpad, caps); + gst_caps_unref (caps); + + return mpg123audiodec; +} + +static GstElement * +setup_mpeg1layer3dec (void) +{ + GstElement *mpg123audiodec; + GstSegment seg; + GstCaps *caps; + + GST_DEBUG ("setup_mpeg1layer3dec"); + mpg123audiodec = gst_check_setup_element ("mpg123audiodec"); + mysrcpad = gst_check_setup_src_pad (mpg123audiodec, &layer3_srctemplate); + mysinkpad = gst_check_setup_sink_pad (mpg123audiodec, &sinktemplate); + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + gst_segment_init (&seg, GST_FORMAT_TIME); + gst_pad_push_event (mysrcpad, gst_event_new_segment (&seg)); + + /* This is necessary to trigger a set_format call in the decoder; + * fixed caps don't trigger it */ + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 1, + "layer", G_TYPE_INT, 3, + "rate", G_TYPE_INT, 44100, + "channels", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); + gst_pad_set_caps (mysrcpad, caps); + gst_caps_unref (caps); + + return mpg123audiodec; +} + +static void +cleanup_mpg123audiodec (GstElement * mpg123audiodec) +{ + GST_DEBUG ("cleanup_mpeg1layer2dec"); + gst_element_set_state (mpg123audiodec, GST_STATE_NULL); + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (mpg123audiodec); + gst_check_teardown_sink_pad (mpg123audiodec); + gst_check_teardown_element (mpg123audiodec); +} + +static void +run_decoding_test (GstElement * mpg123audiodec, gchar const *filename) +{ + GstBus *bus; + unsigned int num_input_buffers, num_decoded_buffers; + gint expected_size; + GstCaps *out_caps, *caps; + GstAudioInfo audioinfo; + GstElement *input_pipeline, *input_appsink; + int i; + GstBuffer *outbuffer; + + /* 440 Hz = frequency of sine wave in audio data + * 44100 Hz = sample rate + * (44100 / 2) Hz = Nyquist frequency */ + static double const expected_frequency_spot = 440.0 / (44100.0 / 2.0); + + fail_unless (gst_element_set_state (mpg123audiodec, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + bus = gst_bus_new (); + + gst_element_set_bus (mpg123audiodec, bus); + + setup_input_pipeline (filename, &input_pipeline, &input_appsink); + + num_input_buffers = 0; + while (TRUE) { + GstSample *sample; + GstBuffer *input_buffer; + + sample = gst_app_sink_pull_sample (GST_APP_SINK (input_appsink)); + if (sample == NULL) + break; + + fail_unless (GST_IS_SAMPLE (sample)); + + input_buffer = gst_sample_get_buffer (sample); + fail_if (input_buffer == NULL); + + /* This is done to be on the safe side - docs say lifetime of the input buffer + * depends *solely* on the sample */ + input_buffer = gst_buffer_copy (input_buffer); + + fail_unless_equals_int (gst_pad_push (mysrcpad, input_buffer), GST_FLOW_OK); + + ++num_input_buffers; + } + + num_decoded_buffers = g_list_length (buffers); + + /* check number of decoded buffers */ + fail_unless_equals_int (num_decoded_buffers, num_input_buffers - 2); + + caps = gst_pad_get_current_caps (mysinkpad); + GST_LOG ("output caps %" GST_PTR_FORMAT, caps); + fail_unless (gst_audio_info_from_caps (&audioinfo, caps), + "Getting audio info from caps failed"); + + /* check caps */ + out_caps = gst_caps_new_simple ("audio/x-raw", + "format", G_TYPE_STRING, "S32LE", + "layout", G_TYPE_STRING, "interleaved", + "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, NULL); + + fail_unless (gst_caps_is_equal_fixed (caps, out_caps), "Incorrect out caps"); + + gst_caps_unref (out_caps); + gst_caps_unref (caps); + + /* here, test if decoded data is a sine tone, and if the sine frequency is at the + * right spot in the spectrum */ + for (i = 0; i < num_decoded_buffers; ++i) { + outbuffer = GST_BUFFER (buffers->data); + fail_if (outbuffer == NULL, "Invalid buffer retrieved"); + + /* MPEG 1 layer 2 uses 1152 samples per frame */ + expected_size = 1152 * GST_AUDIO_INFO_BPF (&audioinfo); + fail_unless_equals_int (gst_buffer_get_size (outbuffer), expected_size); + + check_main_frequency_spot_S32 (outbuffer, expected_frequency_spot); + + buffers = g_list_remove (buffers, outbuffer); + gst_buffer_unref (outbuffer); + outbuffer = NULL; + } + + g_list_free (buffers); + buffers = NULL; + + cleanup_input_pipeline (input_pipeline); + gst_bus_set_flushing (bus, TRUE); + gst_element_set_bus (mpg123audiodec, NULL); + gst_object_unref (GST_OBJECT (bus)); +} + + +GST_START_TEST (test_decode_mpeg1layer2) +{ + GstElement *mpg123audiodec; + mpg123audiodec = setup_mpeg1layer2dec (); + run_decoding_test (mpg123audiodec, MP2_STREAM_FILENAME); + cleanup_mpg123audiodec (mpg123audiodec); + mpg123audiodec = NULL; +} + +GST_END_TEST; + + +GST_START_TEST (test_decode_mpeg1layer3_cbr) +{ + GstElement *mpg123audiodec; + mpg123audiodec = setup_mpeg1layer3dec (); + run_decoding_test (mpg123audiodec, MP3_CBR_STREAM_FILENAME); + cleanup_mpg123audiodec (mpg123audiodec); +} + +GST_END_TEST; + + +GST_START_TEST (test_decode_mpeg1layer3_vbr) +{ + GstElement *mpg123audiodec; + mpg123audiodec = setup_mpeg1layer3dec (); + run_decoding_test (mpg123audiodec, MP3_VBR_STREAM_FILENAME); + cleanup_mpg123audiodec (mpg123audiodec); +} + +GST_END_TEST; + + +GST_START_TEST (test_decode_garbage_mpeg1layer2) +{ + GstElement *mpg123audiodec; + GstBuffer *inbuffer; + GstBus *bus; + int i, num_buffers; + guint32 *tmpbuf; + + mpg123audiodec = setup_mpeg1layer2dec (); + + fail_unless (gst_element_set_state (mpg123audiodec, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + bus = gst_bus_new (); + + /* initialize the buffer with something that is no mpeg2 */ + tmpbuf = g_new (guint32, 4096); + for (i = 0; i < 4096; i++) { + tmpbuf[i] = i; + } + inbuffer = gst_buffer_new_wrapped (tmpbuf, 4096 * sizeof (guint32)); + + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + gst_element_set_bus (mpg123audiodec, bus); + + /* should be possible to push without problems but nothing gets decoded */ + fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK); + + num_buffers = g_list_length (buffers); + + /* should be 0 buffers as decoding should've been impossible */ + fail_unless_equals_int (num_buffers, 0); + + g_list_free (buffers); + buffers = NULL; + + gst_bus_set_flushing (bus, TRUE); + gst_element_set_bus (mpg123audiodec, NULL); + gst_object_unref (GST_OBJECT (bus)); + cleanup_mpg123audiodec (mpg123audiodec); + mpg123audiodec = NULL; +} + +GST_END_TEST; + + +GST_START_TEST (test_decode_garbage_mpeg1layer3) +{ + GstElement *mpg123audiodec; + GstBuffer *inbuffer; + GstBus *bus; + int i, num_buffers; + guint32 *tmpbuf; + + mpg123audiodec = setup_mpeg1layer3dec (); + + fail_unless (gst_element_set_state (mpg123audiodec, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + bus = gst_bus_new (); + + /* initialize the buffer with something that is no mpeg2 */ + tmpbuf = g_new (guint32, 4096); + for (i = 0; i < 4096; i++) { + tmpbuf[i] = i; + } + inbuffer = gst_buffer_new_wrapped (tmpbuf, 4096 * sizeof (guint32)); + + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + + gst_element_set_bus (mpg123audiodec, bus); + + /* should be possible to push without problems but nothing gets decoded */ + fail_unless_equals_int (gst_pad_push (mysrcpad, inbuffer), GST_FLOW_OK); + + num_buffers = g_list_length (buffers); + + /* should be 0 buffers as decoding should've been impossible */ + fail_unless_equals_int (num_buffers, 0); + + g_list_free (buffers); + buffers = NULL; + + gst_bus_set_flushing (bus, TRUE); + gst_element_set_bus (mpg123audiodec, NULL); + gst_object_unref (GST_OBJECT (bus)); + cleanup_mpg123audiodec (mpg123audiodec); + mpg123audiodec = NULL; +} + +GST_END_TEST; + + +static gboolean +is_test_file_available (gchar const *filename) +{ + gboolean ret; + gchar *full_filename; + gchar *cwd; + + cwd = g_get_current_dir (); + full_filename = g_build_filename (cwd, GST_TEST_FILES_PATH, filename, NULL); + ret = + g_file_test (full_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS); + g_free (full_filename); + g_free (cwd); + return ret; +} + + +static Suite * +mpg123audiodec_suite (void) +{ + gboolean has_necessary_elements = TRUE; + Suite *s = suite_create ("mpg123audiodec"); + TCase *tc_chain = tcase_create ("general"); + + /* check if mpegaudioparse, appsink, and filesrc elments are available */ + { + gchar const **element; + gchar const *elements[] = { "filesrc", "mpegaudioparse", "appsink", NULL }; + + for (element = elements; *element != NULL; ++element) { + GstElement *e; + GstStateChangeReturn ret; + + e = gst_element_factory_make (*element, NULL); + if (e == NULL) { + has_necessary_elements = FALSE; + break; + } + + ret = gst_element_set_state (e, GST_STATE_READY); + if (ret == GST_STATE_CHANGE_SUCCESS) { + gst_element_set_state (e, GST_STATE_NULL); + gst_object_unref (GST_OBJECT (e)); + } else { + gst_object_unref (GST_OBJECT (e)); + has_necessary_elements = FALSE; + break; + } + } + } + + suite_add_tcase (s, tc_chain); + if (has_necessary_elements) { + if (is_test_file_available (MP2_STREAM_FILENAME)) + tcase_add_test (tc_chain, test_decode_mpeg1layer2); + if (is_test_file_available (MP3_CBR_STREAM_FILENAME)) + tcase_add_test (tc_chain, test_decode_mpeg1layer3_cbr); + if (is_test_file_available (MP3_VBR_STREAM_FILENAME)) + tcase_add_test (tc_chain, test_decode_mpeg1layer3_vbr); + } + tcase_add_test (tc_chain, test_decode_garbage_mpeg1layer2); + tcase_add_test (tc_chain, test_decode_garbage_mpeg1layer3); + + return s; +} + + +int +main (int argc, char **argv) +{ + int nf; + Suite *s; + SRunner *sr; + + gst_check_init (&argc, &argv); + + s = mpg123audiodec_suite (); + if (s == NULL) + return 0; + + sr = srunner_create (s); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} From ae84696ec5e0a5260d543001f0b95e44878fcb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 24 Oct 2012 13:41:00 +0100 Subject: [PATCH 11/33] tests: fix up mpg123 test a little - dist input files - fix sample leak - simplify check for elements - only run mpg123 test if mpg123 is available and selected - fix build in uninstalled setup https://bugzilla.gnome.org/show_bug.cgi?id=686595 --- tests/check/elements/mpg123audiodec.c | 61 +++++---------------------- 1 file changed, 11 insertions(+), 50 deletions(-) diff --git a/tests/check/elements/mpg123audiodec.c b/tests/check/elements/mpg123audiodec.c index fd7c620e30..57567e53c5 100644 --- a/tests/check/elements/mpg123audiodec.c +++ b/tests/check/elements/mpg123audiodec.c @@ -313,6 +313,8 @@ run_decoding_test (GstElement * mpg123audiodec, gchar const *filename) fail_unless_equals_int (gst_pad_push (mysrcpad, input_buffer), GST_FLOW_OK); ++num_input_buffers; + + gst_sample_unref (sample); } num_decoded_buffers = g_list_length (buffers); @@ -507,43 +509,22 @@ is_test_file_available (gchar const *filename) return ret; } - static Suite * mpg123audiodec_suite (void) { - gboolean has_necessary_elements = TRUE; + GstRegistry *registry; Suite *s = suite_create ("mpg123audiodec"); TCase *tc_chain = tcase_create ("general"); - /* check if mpegaudioparse, appsink, and filesrc elments are available */ - { - gchar const **element; - gchar const *elements[] = { "filesrc", "mpegaudioparse", "appsink", NULL }; - - for (element = elements; *element != NULL; ++element) { - GstElement *e; - GstStateChangeReturn ret; - - e = gst_element_factory_make (*element, NULL); - if (e == NULL) { - has_necessary_elements = FALSE; - break; - } - - ret = gst_element_set_state (e, GST_STATE_READY); - if (ret == GST_STATE_CHANGE_SUCCESS) { - gst_element_set_state (e, GST_STATE_NULL); - gst_object_unref (GST_OBJECT (e)); - } else { - gst_object_unref (GST_OBJECT (e)); - has_necessary_elements = FALSE; - break; - } - } - } + registry = gst_registry_get (); suite_add_tcase (s, tc_chain); - if (has_necessary_elements) { + if (gst_registry_check_feature_version (registry, "filesrc", + GST_VERSION_MAJOR, GST_VERSION_MINOR, 0) && + gst_registry_check_feature_version (registry, "mpegaudioparse", + GST_VERSION_MAJOR, GST_VERSION_MINOR, 0) && + gst_registry_check_feature_version (registry, "appsrc", + GST_VERSION_MAJOR, GST_VERSION_MINOR, 0)) { if (is_test_file_available (MP2_STREAM_FILENAME)) tcase_add_test (tc_chain, test_decode_mpeg1layer2); if (is_test_file_available (MP3_CBR_STREAM_FILENAME)) @@ -558,24 +539,4 @@ mpg123audiodec_suite (void) } -int -main (int argc, char **argv) -{ - int nf; - Suite *s; - SRunner *sr; - - gst_check_init (&argc, &argv); - - s = mpg123audiodec_suite (); - if (s == NULL) - return 0; - - sr = srunner_create (s); - - srunner_run_all (sr, CK_NORMAL); - nf = srunner_ntests_failed (sr); - srunner_free (sr); - - return nf; -} +GST_CHECK_MAIN (mpg123audiodec) From df5a176b5b2e17646150a9ab29d04fc1049c7594 Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Tue, 30 Oct 2012 10:20:09 +1100 Subject: [PATCH 12/33] mpg123: Fix leaks from not chaining up in the finalize function --- ext/mpg123/gstmpg123audiodec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 16bcfce06e..3107d551c0 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -212,6 +212,7 @@ gst_mpg123_audio_dec_finalize (GObject * object) mpg123_delete (mpg123_decoder->handle); mpg123_decoder->handle = NULL; } + G_OBJECT_CLASS (gst_mpg123_audio_dec_parent_class)->finalize (object); } From 3b3d75ecf65761a573e39f79a5e129d61e3c53b9 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Tue, 30 Oct 2012 09:27:24 +0100 Subject: [PATCH 13/33] mpg123: removed unnecessary finalize function https://bugzilla.gnome.org/show_bug.cgi?id=687176 --- ext/mpg123/gstmpg123audiodec.c | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 3107d551c0..2234fe340b 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -72,7 +72,6 @@ GST_STATIC_PAD_TEMPLATE ("sink", "channels = (int) [ 1, 2 ], " "parsed = (boolean) true ") ); -static void gst_mpg123_audio_dec_finalize (GObject * object); static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec); static gboolean gst_mpg123_audio_dec_stop (GstAudioDecoder * dec); static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec @@ -89,7 +88,6 @@ G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER); static void gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) { - GObjectClass *object_class; GstAudioDecoderClass *base_class; GstElementClass *element_class; GstPadTemplate *src_template, *sink_template; @@ -97,12 +95,9 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder"); - object_class = G_OBJECT_CLASS (klass); base_class = GST_AUDIO_DECODER_CLASS (klass); element_class = GST_ELEMENT_CLASS (klass); - object_class->finalize = gst_mpg123_audio_dec_finalize; - gst_element_class_set_static_metadata (element_class, "mpg123 mp3 decoder", "Codec/Decoder/Audio", @@ -204,18 +199,6 @@ gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder) } -static void -gst_mpg123_audio_dec_finalize (GObject * object) -{ - GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (object); - if (G_LIKELY (mpg123_decoder->handle != NULL)) { - mpg123_delete (mpg123_decoder->handle); - mpg123_decoder->handle = NULL; - } - G_OBJECT_CLASS (gst_mpg123_audio_dec_parent_class)->finalize (object); -} - - static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec) { From ced45a61bd6814191bf8aa4d15a1a03dd53a7e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sat, 3 Nov 2012 20:38:00 +0000 Subject: [PATCH 14/33] Fix FSF address https://bugzilla.gnome.org/show_bug.cgi?id=687520 --- tests/check/elements/mpg123audiodec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/check/elements/mpg123audiodec.c b/tests/check/elements/mpg123audiodec.c index 57567e53c5..069f122d56 100644 --- a/tests/check/elements/mpg123audiodec.c +++ b/tests/check/elements/mpg123audiodec.c @@ -16,8 +16,8 @@ * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. */ #include From 773092940d76c50fc5ad15d9b6340f33d1bc2f9e Mon Sep 17 00:00:00 2001 From: David Schleef Date: Mon, 15 Apr 2013 00:22:39 -0700 Subject: [PATCH 15/33] mpg123: Add conditional on API version for new enum --- ext/mpg123/gstmpg123audiodec.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 2234fe340b..930d984c37 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -226,9 +226,13 @@ gst_mpg123_audio_dec_start (GstAudioDecoder * dec) /* Sets the resync limit to the end of the stream (otherwise mpg123 may give * up on decoding prematurely, especially with mp3 web radios) */ mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); +#if MPG123_API_VERSION >= 36 + /* The precise API version where MPG123_AUTO_RESAMPLE appeared is + * somewhere between 29 and 36 */ /* Don't let mpg123 resample output */ mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0); +#endif /* Don't let mpg123 print messages to stdout/stderr */ mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0); From 0f5461be15b74bc5b720be3febe641c7deb87972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 24 Oct 2012 12:16:39 +0200 Subject: [PATCH 16/33] gst: Add better support for static plugins --- ext/mpg123/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/Makefile.am b/ext/mpg123/Makefile.am index 365fc764f8..3acdfa47e3 100644 --- a/ext/mpg123/Makefile.am +++ b/ext/mpg123/Makefile.am @@ -7,7 +7,7 @@ libgstmpg123_la_CFLAGS = -DGST_USE_UNSTABLE_API \ libgstmpg123_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstaudio-@GST_API_VERSION@ \ $(GST_BASE_LIBS) $(GST_LIBS) $(MPG123_LIBS) libgstmpg123_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) -libgstmpg123_la_LIBTOOLFLAGS = --tag=disable-static +libgstmpg123_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS) noinst_HEADERS = gstmpg123audiodec.h From 265ecb5171186e563959b2090af25eb535f14c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 15 May 2013 11:25:07 +0200 Subject: [PATCH 17/33] mpg123audiodec: Fix event handling in unit test --- tests/check/elements/mpg123audiodec.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/check/elements/mpg123audiodec.c b/tests/check/elements/mpg123audiodec.c index 069f122d56..1085b49929 100644 --- a/tests/check/elements/mpg123audiodec.c +++ b/tests/check/elements/mpg123audiodec.c @@ -197,7 +197,6 @@ static GstElement * setup_mpeg1layer2dec (void) { GstElement *mpg123audiodec; - GstSegment seg; GstCaps *caps; GST_DEBUG ("setup_mpeg1layer2dec"); @@ -207,9 +206,6 @@ setup_mpeg1layer2dec (void) gst_pad_set_active (mysrcpad, TRUE); gst_pad_set_active (mysinkpad, TRUE); - gst_segment_init (&seg, GST_FORMAT_TIME); - gst_pad_push_event (mysrcpad, gst_event_new_segment (&seg)); - /* This is necessary to trigger a set_format call in the decoder; * fixed caps don't trigger it */ caps = gst_caps_new_simple ("audio/mpeg", @@ -217,7 +213,7 @@ setup_mpeg1layer2dec (void) "layer", G_TYPE_INT, 2, "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); - gst_pad_set_caps (mysrcpad, caps); + gst_check_setup_events (mysrcpad, mpg123audiodec, caps, GST_FORMAT_TIME); gst_caps_unref (caps); return mpg123audiodec; @@ -227,7 +223,6 @@ static GstElement * setup_mpeg1layer3dec (void) { GstElement *mpg123audiodec; - GstSegment seg; GstCaps *caps; GST_DEBUG ("setup_mpeg1layer3dec"); @@ -237,9 +232,6 @@ setup_mpeg1layer3dec (void) gst_pad_set_active (mysrcpad, TRUE); gst_pad_set_active (mysinkpad, TRUE); - gst_segment_init (&seg, GST_FORMAT_TIME); - gst_pad_push_event (mysrcpad, gst_event_new_segment (&seg)); - /* This is necessary to trigger a set_format call in the decoder; * fixed caps don't trigger it */ caps = gst_caps_new_simple ("audio/mpeg", @@ -247,7 +239,7 @@ setup_mpeg1layer3dec (void) "layer", G_TYPE_INT, 3, "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); - gst_pad_set_caps (mysrcpad, caps); + gst_check_setup_events (mysrcpad, mpg123audiodec, caps, GST_FORMAT_TIME); gst_caps_unref (caps); return mpg123audiodec; From f383e63bebe47b4ac98f529f089d26288e762451 Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Fri, 26 Jul 2013 17:25:42 +0200 Subject: [PATCH 18/33] mpg123: Remove dead assignment harder ? :) --- ext/mpg123/gstmpg123audiodec.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 930d984c37..bdbb3e9fcd 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -597,8 +597,6 @@ gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) int error; GstMpg123AudioDec *mpg123_decoder; - hard = hard; - GST_LOG_OBJECT (dec, "Flushing decoder"); mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); From 04e05eb93f9d5c701e6e2f39d4c97df75b403442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 5 Dec 2013 12:04:39 +0100 Subject: [PATCH 19/33] mpg123audiodec: Require caps to be set before any data processing --- ext/mpg123/gstmpg123audiodec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index bdbb3e9fcd..2be008245e 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -196,6 +196,7 @@ void gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder) { mpg123_decoder->handle = NULL; + gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mpg123_decoder), TRUE); } From 387e6480d68e9bd56cb25ecd8f3acdcb69aacd7a Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Tue, 4 Feb 2014 17:22:27 +0100 Subject: [PATCH 20/33] mpg123: improved error report and checks Signed-off-by: Carlos Rafael Giani --- ext/mpg123/gstmpg123audiodec.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 2be008245e..791dba9b30 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -337,8 +337,9 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, mpg123_feed (mpg123_decoder->handle, info.data, info.size); gst_buffer_unmap (input_buffer, &info); } else { - GST_ERROR_OBJECT (mpg123_decoder, "gst_memory_map() failed"); - return GST_FLOW_ERROR; + GST_AUDIO_DECODER_ERROR (mpg123_decoder, 1, RESOURCE, READ, (NULL), + ("gst_memory_map() failed"), retval); + return retval; } } @@ -398,6 +399,7 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, { /* Anything else is considered an error */ int errcode; + retval = GST_FLOW_ERROR; /* use error by default */ switch (decode_error) { case MPG123_ERR: errcode = mpg123_errcode (mpg123_decoder->handle); @@ -420,11 +422,12 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, } default:{ char const *errmsg = mpg123_plain_strerror (errcode); - GST_ERROR_OBJECT (dec, "Reported error: %s", errmsg); + /* GST_AUDIO_DECODER_ERROR sets a new return value according to + * its estimations */ + GST_AUDIO_DECODER_ERROR (mpg123_decoder, 1, STREAM, DECODE, (NULL), + ("mpg123 decoding error: %s", errmsg), retval); } } - - retval = GST_FLOW_ERROR; } } @@ -506,6 +509,10 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) { GstCaps *allowed_srccaps_unnorm = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec)); + if (!allowed_srccaps_unnorm) { + GST_ERROR_OBJECT (dec, "Allowed src caps are NULL"); + return FALSE; + } allowed_srccaps = gst_caps_normalize (allowed_srccaps_unnorm); } From 52bd182e987a7cf3ed1b92317579564e32b9d5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 5 Mar 2014 00:51:04 +0000 Subject: [PATCH 21/33] tests: fix mpg123audiodec test for big-endian architectures --- tests/check/elements/mpg123audiodec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/check/elements/mpg123audiodec.c b/tests/check/elements/mpg123audiodec.c index 1085b49929..20d6e779dd 100644 --- a/tests/check/elements/mpg123audiodec.c +++ b/tests/check/elements/mpg123audiodec.c @@ -150,7 +150,7 @@ FFT_HELPERS (gint32, S32, s32, 2147483647.0); static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw, format = (string) S32LE ") + GST_STATIC_CAPS ("audio/x-raw, format = (string) " GST_AUDIO_NE (S32)) ); static GstStaticPadTemplate layer2_srctemplate = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, @@ -321,7 +321,7 @@ run_decoding_test (GstElement * mpg123audiodec, gchar const *filename) /* check caps */ out_caps = gst_caps_new_simple ("audio/x-raw", - "format", G_TYPE_STRING, "S32LE", + "format", G_TYPE_STRING, GST_AUDIO_NE (S32), "layout", G_TYPE_STRING, "interleaved", "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, NULL); From bbd6b14d46bcd3fc1d9ca7aeb64d68620e183524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 10 Sep 2014 17:24:39 +0100 Subject: [PATCH 22/33] Fix up one-element lists in template caps --- ext/mpg123/gstmpg123audiodec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 791dba9b30..702226bb7c 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -66,7 +66,7 @@ GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/mpeg, " - "mpegversion = (int) { 1 }, " + "mpegversion = (int) 1, " "layer = (int) [ 1, 3 ], " "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, " "channels = (int) [ 1, 2 ], " "parsed = (boolean) true ") From 2fe38328d28c5d519e5537a31738b5ec3e253b29 Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Sat, 3 Jan 2015 13:06:45 +0100 Subject: [PATCH 23/33] mpg123: rework set_format code so mpg123audiodec works with decodebin/playbin The old code was using gst_caps_normalize() and was generally overly complex. Simplify by picking sample rate and number of channels from upstream and the sample format from the allowed caps. If the format caps is a list of strins, just pick the first one. And if the srcpad isn't linked yet, use the default format (S16). https://bugzilla.gnome.org/show_bug.cgi?id=740195 --- ext/mpg123/gstmpg123audiodec.c | 235 ++++++++++++++++----------------- 1 file changed, 112 insertions(+), 123 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 702226bb7c..5513b5513c 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -438,44 +438,12 @@ gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, static gboolean gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) { -/* Using the parsed information upstream, and the list of allowed caps - * downstream, this code tries to find a suitable audio info. It is important - * to keep in mind that the rate and number of channels should never deviate - * from the one the bitstream has, otherwise mpg123 has to mix channels and/or - * resample (and as its docs say, its internal resampler is very crude). The - * sample format, however, can be chosen freely, because the MPEG specs do not - * mandate any special format. Therefore, rate and number of channels are taken - * from upstream (which parsed the MPEG frames, so the input_caps contain - * exactly the rate and number of channels the bitstream actually has), while - * the sample format is chosen by trying out all caps that are allowed by - * downstream. This way, the output is adjusted to what the downstream prefers. - * - * Also, the new output audio info is not set immediately. Instead, it is - * considered the "next audioinfo". The code waits for mpg123 to notice the new - * format (= when mpg123_decode_frame() returns MPG123_AUDIO_DEC_NEW_FORMAT), - * and then sets the next audioinfo. Otherwise, the next audioinfo is set too - * soon, which may cause problems with mp3s containing several format headers. - * One example would be an mp3 with the first 30 seconds using 44.1 kHz, then - * the next 30 seconds using 32 kHz. Rare, but possible. - * - * STEPS: - * - * 1. get rate and channels from input_caps - * 2. get allowed caps from src pad - * 3. for each structure in allowed caps: - * 3.1. take format - * 3.2. if the combination of format with rate and channels is unsupported by - * mpg123, go to (3), or exit with error if there are no more structures - * to try - * 3.3. create next audioinfo out of rate,channels,format, and exit - */ - - - int rate, channels; + /* "encoding" is the sample format specifier for mpg123 */ + int encoding; + int sample_rate, num_channels; + GstAudioFormat format; GstMpg123AudioDec *mpg123_decoder; - GstCaps *allowed_srccaps; - guint structure_nr; - gboolean match_found = FALSE; + gboolean retval = FALSE; mpg123_decoder = GST_MPG123_AUDIO_DEC (dec); @@ -483,7 +451,7 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) mpg123_decoder->has_next_audioinfo = FALSE; - /* Get rate and channels from input_caps */ + /* Get sample rate and number of channels from input_caps */ { GstStructure *structure; gboolean err = FALSE; @@ -492,110 +460,131 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) * input caps structures don't make sense */ structure = gst_caps_get_structure (input_caps, 0); - if (!gst_structure_get_int (structure, "rate", &rate)) { + if (!gst_structure_get_int (structure, "rate", &sample_rate)) { err = TRUE; GST_ERROR_OBJECT (dec, "Input caps do not have a rate value"); } - if (!gst_structure_get_int (structure, "channels", &channels)) { + if (!gst_structure_get_int (structure, "channels", &num_channels)) { err = TRUE; GST_ERROR_OBJECT (dec, "Input caps do not have a channel value"); } - if (err) - return FALSE; + if (G_UNLIKELY (err)) + goto done; } - /* Get the caps that are allowed by downstream */ + /* Get sample format from the allowed src caps */ { - GstCaps *allowed_srccaps_unnorm = + GstCaps *allowed_srccaps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec)); - if (!allowed_srccaps_unnorm) { - GST_ERROR_OBJECT (dec, "Allowed src caps are NULL"); - return FALSE; - } - allowed_srccaps = gst_caps_normalize (allowed_srccaps_unnorm); - } - /* Go through all allowed caps, pick the first one that matches */ - for (structure_nr = 0; structure_nr < gst_caps_get_size (allowed_srccaps); - ++structure_nr) { - GstStructure *structure; - gchar const *format_str; - GstAudioFormat format; - int encoding; + if (allowed_srccaps == NULL) { + /* srcpad is not linked (yet), so no peer information is available; + * just use the default sample format (16 bit signed integer) */ + GST_DEBUG_OBJECT (mpg123_decoder, + "srcpad is not linked (yet) -> using S16 sample format"); + format = GST_AUDIO_FORMAT_S16; + encoding = MPG123_ENC_SIGNED_16; + } else { + gchar const *format_str; + GValue const *format_value; - structure = gst_caps_get_structure (allowed_srccaps, structure_nr); + /* Look at the sample format values from the first structure */ + GstStructure *structure = gst_caps_get_structure (allowed_srccaps, 0); + format_value = gst_structure_get_value (structure, "format"); - format_str = gst_structure_get_string (structure, "format"); - if (format_str == NULL) { - GST_DEBUG_OBJECT (dec, "Could not get format from src caps"); - continue; - } - - format = gst_audio_format_from_string (format_str); - if (format == GST_AUDIO_FORMAT_UNKNOWN) { - GST_DEBUG_OBJECT (dec, "Unknown format %s", format_str); - continue; - } - - switch (format) { - case GST_AUDIO_FORMAT_S16: - encoding = MPG123_ENC_SIGNED_16; - break; - case GST_AUDIO_FORMAT_S24: - encoding = MPG123_ENC_SIGNED_24; - break; - case GST_AUDIO_FORMAT_S32: - encoding = MPG123_ENC_SIGNED_32; - break; - case GST_AUDIO_FORMAT_U16: - encoding = MPG123_ENC_UNSIGNED_16; - break; - case GST_AUDIO_FORMAT_U24: - encoding = MPG123_ENC_UNSIGNED_24; - break; - case GST_AUDIO_FORMAT_U32: - encoding = MPG123_ENC_UNSIGNED_32; - break; - case GST_AUDIO_FORMAT_F32: - encoding = MPG123_ENC_FLOAT_32; - break; - default: - GST_DEBUG_OBJECT (dec, - "Format %s in srccaps is not supported", format_str); - continue; - } - - { - int err; - - /* Cleanup old formats & set new one */ - mpg123_format_none (mpg123_decoder->handle); - err = mpg123_format (mpg123_decoder->handle, rate, channels, encoding); - if (err != MPG123_OK) { - GST_DEBUG_OBJECT (dec, - "mpg123 cannot use caps %" GST_PTR_FORMAT - " because mpg123_format() failed: %s", structure, - mpg123_strerror (mpg123_decoder->handle)); - continue; + if (GST_VALUE_HOLDS_LIST (format_value)) { + /* if value is a format list, pick the first entry */ + GValue const *fmt_list_value = + gst_value_list_get_value (format_value, 0); + format_str = g_value_get_string (fmt_list_value); + } else if (G_VALUE_HOLDS_STRING (format_value)) { + /* if value is a string, use it directly */ + format_str = g_value_get_string (format_value); + } else { + GST_ERROR_OBJECT (mpg123_decoder, + "format value in caps structure %" GST_PTR_FORMAT + " is of invalid type (must be plain string or string list)", + structure); + format_str = NULL; } + + format = GST_AUDIO_FORMAT_UNKNOWN; + + /* get the format value from the string */ + if (G_LIKELY (format_str != NULL)) { + format = gst_audio_format_from_string (format_str); + if (G_UNLIKELY (format == GST_AUDIO_FORMAT_UNKNOWN)) { + GST_ERROR_OBJECT (mpg123_decoder, "format \"%s\" is invalid", + format_str); + } + } + + /* convert format to mpg123 encoding */ + if (G_LIKELY (format != GST_AUDIO_FORMAT_UNKNOWN)) { + switch (format) { + case GST_AUDIO_FORMAT_S16: + encoding = MPG123_ENC_SIGNED_16; + break; + case GST_AUDIO_FORMAT_S24: + encoding = MPG123_ENC_SIGNED_24; + break; + case GST_AUDIO_FORMAT_S32: + encoding = MPG123_ENC_SIGNED_32; + break; + case GST_AUDIO_FORMAT_U16: + encoding = MPG123_ENC_UNSIGNED_16; + break; + case GST_AUDIO_FORMAT_U24: + encoding = MPG123_ENC_UNSIGNED_24; + break; + case GST_AUDIO_FORMAT_U32: + encoding = MPG123_ENC_UNSIGNED_32; + break; + case GST_AUDIO_FORMAT_F32: + encoding = MPG123_ENC_FLOAT_32; + break; + default: + GST_DEBUG_OBJECT (dec, + "Format %s in srccaps is not supported", format_str); + goto done; + } + } + + gst_caps_unref (allowed_srccaps); } - - gst_audio_info_init (&(mpg123_decoder->next_audioinfo)); - gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, rate, - channels, NULL); - GST_LOG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels", - format_str, rate, channels); - mpg123_decoder->has_next_audioinfo = TRUE; - - match_found = TRUE; - - break; } - gst_caps_unref (allowed_srccaps); + /* Sample rate, number of channels, and sample format are known at this point. + * Set the audioinfo structure's values and the mpg123 format. */ + { + int err; - return match_found; + /* clear all existing format settings from the mpg123 instance */ + mpg123_format_none (mpg123_decoder->handle); + /* set the chosen format */ + err = + mpg123_format (mpg123_decoder->handle, sample_rate, num_channels, + encoding); + + if (err != MPG123_OK) { + GST_WARNING_OBJECT (dec, + "mpg123_format() failed: %s", + mpg123_strerror (mpg123_decoder->handle)); + } else { + gst_audio_info_init (&(mpg123_decoder->next_audioinfo)); + gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, + sample_rate, num_channels, NULL); + GST_LOG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels", + gst_audio_format_to_string (format), sample_rate, num_channels); + mpg123_decoder->has_next_audioinfo = TRUE; + + retval = TRUE; + } + } + +done: + return retval; } From e42f8627b6c8209ebc3a90b63980c119a21e3096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sun, 11 Jan 2015 01:08:08 +0000 Subject: [PATCH 24/33] mpg123: fix compiler warning and simplify checks in set_caps https://bugzilla.gnome.org/show_bug.cgi?id=740195 --- ext/mpg123/gstmpg123audiodec.c | 85 ++++++++++++++++------------------ 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 5513b5513c..60d1979712 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -485,6 +485,9 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) "srcpad is not linked (yet) -> using S16 sample format"); format = GST_AUDIO_FORMAT_S16; encoding = MPG123_ENC_SIGNED_16; + } else if (gst_caps_is_empty (allowed_srccaps)) { + gst_caps_unref (allowed_srccaps); + goto done; } else { gchar const *format_str; GValue const *format_value; @@ -493,7 +496,10 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) GstStructure *structure = gst_caps_get_structure (allowed_srccaps, 0); format_value = gst_structure_get_value (structure, "format"); - if (GST_VALUE_HOLDS_LIST (format_value)) { + if (format_value == NULL) { + gst_caps_unref (allowed_srccaps); + goto done; + } else if (GST_VALUE_HOLDS_LIST (format_value)) { /* if value is a format list, pick the first entry */ GValue const *fmt_list_value = gst_value_list_get_value (format_value, 0); @@ -502,56 +508,45 @@ gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps) /* if value is a string, use it directly */ format_str = g_value_get_string (format_value); } else { - GST_ERROR_OBJECT (mpg123_decoder, - "format value in caps structure %" GST_PTR_FORMAT - " is of invalid type (must be plain string or string list)", - structure); - format_str = NULL; + GST_ERROR_OBJECT (mpg123_decoder, "unexpected type for 'format' field " + "in caps structure %" GST_PTR_FORMAT, structure); + gst_caps_unref (allowed_srccaps); + goto done; } - format = GST_AUDIO_FORMAT_UNKNOWN; - /* get the format value from the string */ - if (G_LIKELY (format_str != NULL)) { - format = gst_audio_format_from_string (format_str); - if (G_UNLIKELY (format == GST_AUDIO_FORMAT_UNKNOWN)) { - GST_ERROR_OBJECT (mpg123_decoder, "format \"%s\" is invalid", - format_str); - } - } + format = gst_audio_format_from_string (format_str); + gst_caps_unref (allowed_srccaps); + + g_assert (format != GST_AUDIO_FORMAT_UNKNOWN); /* convert format to mpg123 encoding */ - if (G_LIKELY (format != GST_AUDIO_FORMAT_UNKNOWN)) { - switch (format) { - case GST_AUDIO_FORMAT_S16: - encoding = MPG123_ENC_SIGNED_16; - break; - case GST_AUDIO_FORMAT_S24: - encoding = MPG123_ENC_SIGNED_24; - break; - case GST_AUDIO_FORMAT_S32: - encoding = MPG123_ENC_SIGNED_32; - break; - case GST_AUDIO_FORMAT_U16: - encoding = MPG123_ENC_UNSIGNED_16; - break; - case GST_AUDIO_FORMAT_U24: - encoding = MPG123_ENC_UNSIGNED_24; - break; - case GST_AUDIO_FORMAT_U32: - encoding = MPG123_ENC_UNSIGNED_32; - break; - case GST_AUDIO_FORMAT_F32: - encoding = MPG123_ENC_FLOAT_32; - break; - default: - GST_DEBUG_OBJECT (dec, - "Format %s in srccaps is not supported", format_str); - goto done; - } + switch (format) { + case GST_AUDIO_FORMAT_S16: + encoding = MPG123_ENC_SIGNED_16; + break; + case GST_AUDIO_FORMAT_S24: + encoding = MPG123_ENC_SIGNED_24; + break; + case GST_AUDIO_FORMAT_S32: + encoding = MPG123_ENC_SIGNED_32; + break; + case GST_AUDIO_FORMAT_U16: + encoding = MPG123_ENC_UNSIGNED_16; + break; + case GST_AUDIO_FORMAT_U24: + encoding = MPG123_ENC_UNSIGNED_24; + break; + case GST_AUDIO_FORMAT_U32: + encoding = MPG123_ENC_UNSIGNED_32; + break; + case GST_AUDIO_FORMAT_F32: + encoding = MPG123_ENC_FLOAT_32; + break; + default: + g_assert_not_reached (); + goto done; } - - gst_caps_unref (allowed_srccaps); } } From b344d7ba47618429dfc87a42f47e026af5a24400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sun, 26 Apr 2015 18:04:16 +0100 Subject: [PATCH 25/33] Remove obsolete Android build cruft This is not needed any longer. --- ext/mpg123/Makefile.am | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/ext/mpg123/Makefile.am b/ext/mpg123/Makefile.am index 3acdfa47e3..6c96207c86 100644 --- a/ext/mpg123/Makefile.am +++ b/ext/mpg123/Makefile.am @@ -10,18 +10,3 @@ libgstmpg123_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstmpg123_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS) noinst_HEADERS = gstmpg123audiodec.h - -Android.mk: Makefile.am $(BUILT_SOURCES) - androgenizer \ - -:PROJECT libgstmpg123 -:SHARED libgstmpg123 \ - -:TAGS eng debug \ - -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \ - -:SOURCES $(libgstmpg123_la_SOURCES) \ - -:CPPFLAGS $(CPPFLAGS) \ - -:CFLAGS $(DEFS) $(DEFAULT_INCLUDES) $(libgstmpg123_la_CFLAGS) \ - -:LDFLAGS $(libgstmpg123_la_LDFLAGS) \ - $(libgstmpg123_la_LIBADD) \ - -ldl \ - -:PASSTHROUGH LOCAL_ARM_MODE:=arm \ - LOCAL_MODULE_PATH:='$$(TARGET_OUT)/lib/gstreamer-@GST_API_VERSION@' \ - > $@ From e8110b7167360946ef3eebc4a3dc0e9ac1d53983 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Sat, 15 Aug 2015 12:58:40 -0300 Subject: [PATCH 26/33] audiodecoders: use default pad accept-caps handling Avoids useless check of downstream caps when handling an accept-caps query Elements: dtsdec, faad, gsmdec, mpg123audiodec, opusdec, sbcdec, adpcmdec, sirendec --- ext/mpg123/gstmpg123audiodec.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 60d1979712..46f6db9628 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -197,6 +197,9 @@ gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder) { mpg123_decoder->handle = NULL; gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mpg123_decoder), TRUE); + gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST + (mpg123_decoder), TRUE); + GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (mpg123_decoder)); } From 9ffe409138326e8b967360268ef69b67beec53d8 Mon Sep 17 00:00:00 2001 From: Jason Litzinger Date: Wed, 15 Jul 2015 10:44:02 -0600 Subject: [PATCH 27/33] mpg123: fix handling of sample rate change during playback If the sample rate of the media changes, the resulting flush will clear the has_next_audioinfo flag, and the caps won't be sent downstream. https://bugzilla.gnome.org/show_bug.cgi?id=752431 --- ext/mpg123/gstmpg123audiodec.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 46f6db9628..5a7faa0cb2 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -611,7 +611,6 @@ gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) mpg123_decoder->handle = NULL; } - mpg123_decoder->has_next_audioinfo = FALSE; /* opening/closing feeds do not affect the format defined by the * mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), From 1ab089dfc1232dab8ba9438c795fc4414c99b4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Mon, 17 Aug 2015 11:50:28 +0100 Subject: [PATCH 28/33] mpg123: still reset pending audio info on hard flush Follow-up to previous commit. https://bugzilla.gnome.org/show_bug.cgi?id=752431 --- ext/mpg123/gstmpg123audiodec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index 5a7faa0cb2..aa8598c053 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -611,6 +611,8 @@ gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard) mpg123_decoder->handle = NULL; } + if (hard) + mpg123_decoder->has_next_audioinfo = FALSE; /* opening/closing feeds do not affect the format defined by the * mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(), From 777d773c23fc9e8f5ff336ef29059c027406b05b Mon Sep 17 00:00:00 2001 From: Vineeth TM Date: Mon, 14 Dec 2015 11:09:46 +0900 Subject: [PATCH 29/33] plugins-bad: Fix example pipelines rename gst-launch --> gst-launch-1.0 replace old elements with new elements(ffmpegcolorspace -> videoconvert, ffenc_** -> avenc_**) fix caps in examples https://bugzilla.gnome.org/show_bug.cgi?id=759432 --- ext/mpg123/gstmpg123audiodec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index aa8598c053..cfd017ea32 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -25,7 +25,7 @@ * * Example pipelines * |[ - * gst-launch filesrc location=music.mp3 ! mpegaudioparse ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink + * gst-launch-1.0 filesrc location=music.mp3 ! mpegaudioparse ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink * ]| Decode and play the mp3 file * */ From 5e7a4b4d62f257172f049c2748e5437c68bf056a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 15 Apr 2016 19:55:03 +0100 Subject: [PATCH 30/33] mpg123: fix build with msvc Fix syntax errors when compiling against cerbero-provided libmpg123 headers. We do the same as the libmpg123 internal visual studio build here. mpg123.h(1378): error C2143: syntax error: missing ')' before '(' mpg123.h(1378): error C2081: 'ssize_t': name in formal parameter list illegal mpg123.h(1378): error C2143: syntax error: missing ')' before '*' mpg123.h(1378): error C2091: function returns function mpg123.h(1378): error C2143: syntax error: missing '{' before '*' mpg123.h(1378): error C2059: syntax error: ')' mpg123.h(1379): error C2143: syntax error: missing ')' before '*' mpg123.h(1379): error C2365: 'off_t': redefinition; previous definition was 'typedef' ... --- ext/mpg123/gstmpg123audiodec.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ext/mpg123/gstmpg123audiodec.h b/ext/mpg123/gstmpg123audiodec.h index e837a56b0c..b865c417ae 100644 --- a/ext/mpg123/gstmpg123audiodec.h +++ b/ext/mpg123/gstmpg123audiodec.h @@ -19,6 +19,18 @@ #ifndef __GST_MPG123_AUDIO_DEC_H__ #define __GST_MPG123_AUDIO_DEC_H__ +/* This is what the visual studio build in mpg123 does before including the + * original header file. Without this we get syntax errors in the + * replace_reader function declarations because it doesn't know ssize_t etc. + * It doesn't realy matter for us if the ssize_t typedef here is correct. */ +#ifdef _MSC_VER +#include +#include +#include +typedef long ssize_t; +#include +#endif + #include #include #include From 16f70ab421b63138ea0f7f2a3c00b819fecdd528 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Fri, 12 Aug 2016 21:25:34 +0530 Subject: [PATCH 31/33] Add support for Meson as alternative/parallel build system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/mesonbuild/meson With contributions from: Tim-Philipp Müller Jussi Pakkanen (original port) Highlights of the features provided are: * Faster builds on Linux (~40-50% faster) * The ability to build with MSVC on Windows * Generate Visual Studio project files * Generate XCode project files * Much faster builds on Windows (on-par with Linux) * Seriously fast configure and building on embedded ... and many more. For more details see: http://blog.nirbheek.in/2016/05/gstreamer-and-meson-new-hope.html http://blog.nirbheek.in/2016/07/building-and-developing-gstreamer-using.html Building with Meson should work on both Linux and Windows, but may need a few more tweaks on other operating systems. --- ext/mpg123/meson.build | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ext/mpg123/meson.build diff --git a/ext/mpg123/meson.build b/ext/mpg123/meson.build new file mode 100644 index 0000000000..a575449bdf --- /dev/null +++ b/ext/mpg123/meson.build @@ -0,0 +1,16 @@ +mpg123_sources = [ + 'gstmpg123audiodec.c', +] + +mpg123_dep = dependency('libmpg123', version : '>= 1.3', required : false) + +if mpg123_dep.found() + gstmpg123 = library('gstmpg123', + mpg123_sources, + c_args : ugly_args, + include_directories : [configinc], + dependencies : [gstaudio_dep, mpg123_dep], + install : true, + install_dir : plugins_install_dir, + ) +endif From d68fc85d605c0fa680baf3010323ce4539c6dbc4 Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Tue, 16 May 2017 14:07:56 -0400 Subject: [PATCH 32/33] Remove plugin specific static build option Static and dynamic plugins now have the same interface. The standard --enable-static/--enable-shared toggle are sufficient. --- ext/mpg123/Makefile.am | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/mpg123/Makefile.am b/ext/mpg123/Makefile.am index 6c96207c86..465f325973 100644 --- a/ext/mpg123/Makefile.am +++ b/ext/mpg123/Makefile.am @@ -7,6 +7,5 @@ libgstmpg123_la_CFLAGS = -DGST_USE_UNSTABLE_API \ libgstmpg123_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstaudio-@GST_API_VERSION@ \ $(GST_BASE_LIBS) $(GST_LIBS) $(MPG123_LIBS) libgstmpg123_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) -libgstmpg123_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS) noinst_HEADERS = gstmpg123audiodec.h From e0714f67b30e82e87efece4a5e3d0bc9ada13472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sat, 15 Jul 2017 14:57:49 +0100 Subject: [PATCH 33/33] mpg123audiodec: fix caps leak The pad template takes its own ref, so we should unref the caps. https://bugzilla.gnome.org/show_bug.cgi?id=784982 --- ext/mpg123/gstmpg123audiodec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/mpg123/gstmpg123audiodec.c b/ext/mpg123/gstmpg123audiodec.c index cfd017ea32..fa6743cb90 100644 --- a/ext/mpg123/gstmpg123audiodec.c +++ b/ext/mpg123/gstmpg123audiodec.c @@ -167,6 +167,7 @@ gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass) src_template_caps = gst_caps_from_string (s->str); src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, src_template_caps); + gst_caps_unref (src_template_caps); g_string_free (s, TRUE); }