diff options
Diffstat (limited to 'gst/audioparsers')
-rw-r--r-- | gst/audioparsers/Makefile.am | 18 | ||||
-rw-r--r-- | gst/audioparsers/gstaacparse.c | 717 | ||||
-rw-r--r-- | gst/audioparsers/gstaacparse.h | 109 | ||||
-rw-r--r-- | gst/audioparsers/gstac3parse.c | 507 | ||||
-rw-r--r-- | gst/audioparsers/gstac3parse.h | 73 | ||||
-rw-r--r-- | gst/audioparsers/gstamrparse.c | 378 | ||||
-rw-r--r-- | gst/audioparsers/gstamrparse.h | 82 | ||||
-rw-r--r-- | gst/audioparsers/gstdcaparse.c | 451 | ||||
-rw-r--r-- | gst/audioparsers/gstdcaparse.h | 78 | ||||
-rw-r--r-- | gst/audioparsers/gstflacparse.c | 1355 | ||||
-rw-r--r-- | gst/audioparsers/gstflacparse.h | 92 | ||||
-rw-r--r-- | gst/audioparsers/gstmpegaudioparse.c | 1265 | ||||
-rw-r--r-- | gst/audioparsers/gstmpegaudioparse.h | 111 | ||||
-rw-r--r-- | gst/audioparsers/plugin.c | 57 |
14 files changed, 5293 insertions, 0 deletions
diff --git a/gst/audioparsers/Makefile.am b/gst/audioparsers/Makefile.am new file mode 100644 index 000000000..22bc81fa0 --- /dev/null +++ b/gst/audioparsers/Makefile.am @@ -0,0 +1,18 @@ +plugin_LTLIBRARIES = libgstaudioparsers.la + +libgstaudioparsers_la_SOURCES = \ + gstaacparse.c gstamrparse.c gstac3parse.c \ + gstdcaparse.c gstflacparse.c gstmpegaudioparse.c \ + plugin.c + +libgstaudioparsers_la_CFLAGS = \ + $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) +libgstaudioparsers_la_LIBADD = \ + $(GST_PLUGINS_BASE_LIBS) -lgsttag-$(GST_MAJORMINOR) \ + -lgstaudio-$(GST_MAJORMINOR) \ + $(GST_BASE_LIBS) $(GST_LIBS) +libgstaudioparsers_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) +libgstaudioparsers_la_LIBTOOLFLAGS = --tag=disable-static + +noinst_HEADERS = gstaacparse.h gstamrparse.h gstac3parse.h \ + gstdcaparse.h gstflacparse.h gstmpegaudioparse.h diff --git a/gst/audioparsers/gstaacparse.c b/gst/audioparsers/gstaacparse.c new file mode 100644 index 000000000..df7c401ab --- /dev/null +++ b/gst/audioparsers/gstaacparse.c @@ -0,0 +1,717 @@ +/* GStreamer AAC parser plugin + * Copyright (C) 2008 Nokia Corporation. All rights reserved. + * + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +/** + * SECTION:element-aacparse + * @short_description: AAC parser + * @see_also: #GstAmrParse + * + * This is an AAC parser which handles both ADIF and ADTS stream formats. + * + * As ADIF format is not framed, it is not seekable and stream duration cannot + * be determined either. However, ADTS format AAC clips can be seeked, and parser + * can also estimate playback position and clip duration. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch filesrc location=abc.aac ! aacparse ! faad ! audioresample ! audioconvert ! alsasink + * ]| + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstaacparse.h" + + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, " + "framed = (boolean) true, " "mpegversion = (int) { 2, 4 }, " + "stream-format = (string) { raw, adts, adif };")); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, " + "framed = (boolean) false, " "mpegversion = (int) { 2, 4 };")); + +GST_DEBUG_CATEGORY_STATIC (aacparse_debug); +#define GST_CAT_DEFAULT aacparse_debug + + +#define ADIF_MAX_SIZE 40 /* Should be enough */ +#define ADTS_MAX_SIZE 10 /* Should be enough */ + + +#define AAC_FRAME_DURATION(parse) (GST_SECOND/parse->frames_per_sec) + +gboolean gst_aac_parse_start (GstBaseParse * parse); +gboolean gst_aac_parse_stop (GstBaseParse * parse); + +static gboolean gst_aac_parse_sink_setcaps (GstBaseParse * parse, + GstCaps * caps); + +gboolean gst_aac_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * size, gint * skipsize); + +GstFlowReturn gst_aac_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); + +gboolean gst_aac_parse_convert (GstBaseParse * parse, + GstFormat src_format, + gint64 src_value, GstFormat dest_format, gint64 * dest_value); + +gint gst_aac_parse_get_frame_overhead (GstBaseParse * parse, + GstBuffer * buffer); + +gboolean gst_aac_parse_event (GstBaseParse * parse, GstEvent * event); + +#define _do_init(bla) \ + GST_DEBUG_CATEGORY_INIT (aacparse_debug, "aacparse", 0, \ + "AAC audio stream parser"); + +GST_BOILERPLATE_FULL (GstAacParse, gst_aac_parse, GstBaseParse, + GST_TYPE_BASE_PARSE, _do_init); + +static inline gint +gst_aac_parse_get_sample_rate_from_index (guint sr_idx) +{ + static const guint aac_sample_rates[] = { 96000, 88200, 64000, 48000, 44100, + 32000, 24000, 22050, 16000, 12000, 11025, 8000 + }; + + if (sr_idx < G_N_ELEMENTS (aac_sample_rates)) + return aac_sample_rates[sr_idx]; + GST_WARNING ("Invalid sample rate index %u", sr_idx); + return 0; +} + +/** + * gst_aac_parse_base_init: + * @klass: #GstElementClass. + * + */ +static void +gst_aac_parse_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, + "AAC audio stream parser", "Codec/Parser/Audio", + "Advanced Audio Coding parser", "Stefan Kost <stefan.kost@nokia.com>"); +} + + +/** + * gst_aac_parse_class_init: + * @klass: #GstAacParseClass. + * + */ +static void +gst_aac_parse_class_init (GstAacParseClass * klass) +{ + GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); + + parse_class->start = GST_DEBUG_FUNCPTR (gst_aac_parse_start); + parse_class->stop = GST_DEBUG_FUNCPTR (gst_aac_parse_stop); + parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_aac_parse_sink_setcaps); + parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_aac_parse_parse_frame); + parse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_aac_parse_check_valid_frame); +} + + +/** + * gst_aac_parse_init: + * @aacparse: #GstAacParse. + * @klass: #GstAacParseClass. + * + */ +static void +gst_aac_parse_init (GstAacParse * aacparse, GstAacParseClass * klass) +{ + GST_DEBUG ("initialized"); +} + + +/** + * gst_aac_parse_set_src_caps: + * @aacparse: #GstAacParse. + * @sink_caps: (proposed) caps of sink pad + * + * Set source pad caps according to current knowledge about the + * audio stream. + * + * Returns: TRUE if caps were successfully set. + */ +static gboolean +gst_aac_parse_set_src_caps (GstAacParse * aacparse, GstCaps * sink_caps) +{ + GstStructure *s; + GstCaps *src_caps = NULL; + gboolean res = FALSE; + const gchar *stream_format; + + GST_DEBUG_OBJECT (aacparse, "sink caps: %" GST_PTR_FORMAT, sink_caps); + if (sink_caps) + src_caps = gst_caps_copy (sink_caps); + else + src_caps = gst_caps_new_simple ("audio/mpeg", NULL); + + gst_caps_set_simple (src_caps, "framed", G_TYPE_BOOLEAN, TRUE, + "mpegversion", G_TYPE_INT, aacparse->mpegversion, NULL); + + switch (aacparse->header_type) { + case DSPAAC_HEADER_NONE: + stream_format = "raw"; + break; + case DSPAAC_HEADER_ADTS: + stream_format = "adts"; + break; + case DSPAAC_HEADER_ADIF: + stream_format = "adif"; + break; + default: + stream_format = NULL; + } + + s = gst_caps_get_structure (src_caps, 0); + if (aacparse->sample_rate > 0) + gst_structure_set (s, "rate", G_TYPE_INT, aacparse->sample_rate, NULL); + if (aacparse->channels > 0) + gst_structure_set (s, "channels", G_TYPE_INT, aacparse->channels, NULL); + if (stream_format) + gst_structure_set (s, "stream-format", G_TYPE_STRING, stream_format, NULL); + + GST_DEBUG_OBJECT (aacparse, "setting src caps: %" GST_PTR_FORMAT, src_caps); + + res = gst_pad_set_caps (GST_BASE_PARSE (aacparse)->srcpad, src_caps); + gst_caps_unref (src_caps); + return res; +} + + +/** + * gst_aac_parse_sink_setcaps: + * @sinkpad: GstPad + * @caps: GstCaps + * + * Implementation of "set_sink_caps" vmethod in #GstBaseParse class. + * + * Returns: TRUE on success. + */ +static gboolean +gst_aac_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps) +{ + GstAacParse *aacparse; + GstStructure *structure; + gchar *caps_str; + const GValue *value; + + aacparse = GST_AAC_PARSE (parse); + structure = gst_caps_get_structure (caps, 0); + caps_str = gst_caps_to_string (caps); + + GST_DEBUG_OBJECT (aacparse, "setcaps: %s", caps_str); + g_free (caps_str); + + /* This is needed at least in case of RTP + * Parses the codec_data information to get ObjectType, + * number of channels and samplerate */ + value = gst_structure_get_value (structure, "codec_data"); + if (value) { + GstBuffer *buf = gst_value_get_buffer (value); + + if (buf) { + const guint8 *buffer = GST_BUFFER_DATA (buf); + guint sr_idx; + + sr_idx = ((buffer[0] & 0x07) << 1) | ((buffer[1] & 0x80) >> 7); + aacparse->object_type = (buffer[0] & 0xf8) >> 3; + aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx); + aacparse->channels = (buffer[1] & 0x78) >> 3; + aacparse->header_type = DSPAAC_HEADER_NONE; + aacparse->mpegversion = 4; + + GST_DEBUG ("codec_data: object_type=%d, sample_rate=%d, channels=%d", + aacparse->object_type, aacparse->sample_rate, aacparse->channels); + + /* arrange for metadata and get out of the way */ + gst_aac_parse_set_src_caps (aacparse, caps); + gst_base_parse_set_passthrough (parse, TRUE); + } else + return FALSE; + + /* caps info overrides */ + gst_structure_get_int (structure, "rate", &aacparse->sample_rate); + gst_structure_get_int (structure, "channels", &aacparse->channels); + } else { + gst_base_parse_set_passthrough (parse, FALSE); + } + + return TRUE; +} + + +/** + * gst_aac_parse_adts_get_frame_len: + * @data: block of data containing an ADTS header. + * + * This function calculates ADTS frame length from the given header. + * + * Returns: size of the ADTS frame. + */ +static inline guint +gst_aac_parse_adts_get_frame_len (const guint8 * data) +{ + return ((data[3] & 0x03) << 11) | (data[4] << 3) | ((data[5] & 0xe0) >> 5); +} + + +/** + * gst_aac_parse_check_adts_frame: + * @aacparse: #GstAacParse. + * @data: Data to be checked. + * @avail: Amount of data passed. + * @framesize: If valid ADTS frame was found, this will be set to tell the + * found frame size in bytes. + * @needed_data: If frame was not found, this may be set to tell how much + * more data is needed in the next round to detect the frame + * reliably. This may happen when a frame header candidate + * is found but it cannot be guaranteed to be the header without + * peeking the following data. + * + * Check if the given data contains contains ADTS frame. The algorithm + * will examine ADTS frame header and calculate the frame size. Also, another + * consecutive ADTS frame header need to be present after the found frame. + * Otherwise the data is not considered as a valid ADTS frame. However, this + * "extra check" is omitted when EOS has been received. In this case it is + * enough when data[0] contains a valid ADTS header. + * + * This function may set the #needed_data to indicate that a possible frame + * candidate has been found, but more data (#needed_data bytes) is needed to + * be absolutely sure. When this situation occurs, FALSE will be returned. + * + * When a valid frame is detected, this function will use + * gst_base_parse_set_min_frame_size() function from #GstBaseParse class + * to set the needed bytes for next frame.This way next data chunk is already + * of correct size. + * + * Returns: TRUE if the given data contains a valid ADTS header. + */ +static gboolean +gst_aac_parse_check_adts_frame (GstAacParse * aacparse, + const guint8 * data, const guint avail, gboolean drain, + guint * framesize, guint * needed_data) +{ + if (G_UNLIKELY (avail < 2)) + return FALSE; + + if ((data[0] == 0xff) && ((data[1] & 0xf6) == 0xf0)) { + *framesize = gst_aac_parse_adts_get_frame_len (data); + + /* In EOS mode this is enough. No need to examine the data further */ + if (drain) { + return TRUE; + } + + if (*framesize + ADTS_MAX_SIZE > avail) { + /* We have found a possible frame header candidate, but can't be + sure since we don't have enough data to check the next frame */ + GST_DEBUG ("NEED MORE DATA: we need %d, available %d", + *framesize + ADTS_MAX_SIZE, avail); + *needed_data = *framesize + ADTS_MAX_SIZE; + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), + *framesize + ADTS_MAX_SIZE); + return FALSE; + } + + if ((data[*framesize] == 0xff) && ((data[*framesize + 1] & 0xf6) == 0xf0)) { + guint nextlen = gst_aac_parse_adts_get_frame_len (data + (*framesize)); + + GST_LOG ("ADTS frame found, len: %d bytes", *framesize); + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), + nextlen + ADTS_MAX_SIZE); + return TRUE; + } + } + return FALSE; +} + +/* caller ensure sufficient data */ +static inline void +gst_aac_parse_parse_adts_header (GstAacParse * aacparse, const guint8 * data, + gint * rate, gint * channels, gint * object, gint * version) +{ + + if (rate) { + gint sr_idx = (data[2] & 0x3c) >> 2; + + *rate = gst_aac_parse_get_sample_rate_from_index (sr_idx); + } + if (channels) + *channels = ((data[2] & 0x01) << 2) | ((data[3] & 0xc0) >> 6); + + if (version) + *version = (data[1] & 0x08) ? 2 : 4; + if (object) + *object = (data[2] & 0xc0) >> 6; +} + +/** + * gst_aac_parse_detect_stream: + * @aacparse: #GstAacParse. + * @data: A block of data that needs to be examined for stream characteristics. + * @avail: Size of the given datablock. + * @framesize: If valid stream was found, this will be set to tell the + * first frame size in bytes. + * @skipsize: If valid stream was found, this will be set to tell the first + * audio frame position within the given data. + * + * Examines the given piece of data and try to detect the format of it. It + * checks for "ADIF" header (in the beginning of the clip) and ADTS frame + * header. If the stream is detected, TRUE will be returned and #framesize + * is set to indicate the found frame size. Additionally, #skipsize might + * be set to indicate the number of bytes that need to be skipped, a.k.a. the + * position of the frame inside given data chunk. + * + * Returns: TRUE on success. + */ +static gboolean +gst_aac_parse_detect_stream (GstAacParse * aacparse, + const guint8 * data, const guint avail, gboolean drain, + guint * framesize, gint * skipsize) +{ + gboolean found = FALSE; + guint need_data = 0; + guint i = 0; + + GST_DEBUG_OBJECT (aacparse, "Parsing header data"); + + /* FIXME: No need to check for ADIF if we are not in the beginning of the + stream */ + + /* Can we even parse the header? */ + if (avail < ADTS_MAX_SIZE) + return FALSE; + + for (i = 0; i < avail - 4; i++) { + if (((data[i] == 0xff) && ((data[i + 1] & 0xf6) == 0xf0)) || + strncmp ((char *) data + i, "ADIF", 4) == 0) { + found = TRUE; + + if (i) { + /* Trick: tell the parent class that we didn't find the frame yet, + but make it skip 'i' amount of bytes. Next time we arrive + here we have full frame in the beginning of the data. */ + *skipsize = i; + return FALSE; + } + break; + } + } + if (!found) { + if (i) + *skipsize = i; + return FALSE; + } + + if (gst_aac_parse_check_adts_frame (aacparse, data, avail, drain, + framesize, &need_data)) { + gint rate, channels; + + GST_INFO ("ADTS ID: %d, framesize: %d", (data[1] & 0x08) >> 3, *framesize); + + aacparse->header_type = DSPAAC_HEADER_ADTS; + gst_aac_parse_parse_adts_header (aacparse, data, &rate, &channels, + &aacparse->object_type, &aacparse->mpegversion); + + gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), rate, 1024, 2, 2); + + GST_DEBUG ("ADTS: samplerate %d, channels %d, objtype %d, version %d", + rate, channels, aacparse->object_type, aacparse->mpegversion); + + gst_base_parse_set_syncable (GST_BASE_PARSE (aacparse), TRUE); + + return TRUE; + } else if (need_data) { + /* This tells the parent class not to skip any data */ + *skipsize = 0; + return FALSE; + } + + if (avail < ADIF_MAX_SIZE) + return FALSE; + + if (memcmp (data + i, "ADIF", 4) == 0) { + const guint8 *adif; + int skip_size = 0; + int bitstream_type; + int sr_idx; + + aacparse->header_type = DSPAAC_HEADER_ADIF; + aacparse->mpegversion = 4; + + /* Skip the "ADIF" bytes */ + adif = data + i + 4; + + /* copyright string */ + if (adif[0] & 0x80) + skip_size += 9; /* skip 9 bytes */ + + bitstream_type = adif[0 + skip_size] & 0x10; + aacparse->bitrate = + ((unsigned int) (adif[0 + skip_size] & 0x0f) << 19) | + ((unsigned int) adif[1 + skip_size] << 11) | + ((unsigned int) adif[2 + skip_size] << 3) | + ((unsigned int) adif[3 + skip_size] & 0xe0); + + /* CBR */ + if (bitstream_type == 0) { +#if 0 + /* Buffer fullness parsing. Currently not needed... */ + guint num_elems = 0; + guint fullness = 0; + + num_elems = (adif[3 + skip_size] & 0x1e); + GST_INFO ("ADIF num_config_elems: %d", num_elems); + + fullness = ((unsigned int) (adif[3 + skip_size] & 0x01) << 19) | + ((unsigned int) adif[4 + skip_size] << 11) | + ((unsigned int) adif[5 + skip_size] << 3) | + ((unsigned int) (adif[6 + skip_size] & 0xe0) >> 5); + + GST_INFO ("ADIF buffer fullness: %d", fullness); +#endif + aacparse->object_type = ((adif[6 + skip_size] & 0x01) << 1) | + ((adif[7 + skip_size] & 0x80) >> 7); + sr_idx = (adif[7 + skip_size] & 0x78) >> 3; + } + /* VBR */ + else { + aacparse->object_type = (adif[4 + skip_size] & 0x18) >> 3; + sr_idx = ((adif[4 + skip_size] & 0x07) << 1) | + ((adif[5 + skip_size] & 0x80) >> 7); + } + + /* FIXME: This gives totally wrong results. Duration calculation cannot + be based on this */ + aacparse->sample_rate = gst_aac_parse_get_sample_rate_from_index (sr_idx); + + /* baseparse is not given any fps, + * so it will give up on timestamps, seeking, etc */ + + /* FIXME: Can we assume this? */ + aacparse->channels = 2; + + GST_INFO ("ADIF: br=%d, samplerate=%d, objtype=%d", + aacparse->bitrate, aacparse->sample_rate, aacparse->object_type); + + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 512); + + /* arrange for metadata and get out of the way */ + gst_aac_parse_set_src_caps (aacparse, + GST_PAD_CAPS (GST_BASE_PARSE_SINK_PAD (aacparse))); + + /* not syncable, not easily seekable (unless we push data from start */ + gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (aacparse), FALSE); + gst_base_parse_set_passthrough (GST_BASE_PARSE_CAST (aacparse), TRUE); + gst_base_parse_set_average_bitrate (GST_BASE_PARSE_CAST (aacparse), 0); + + *framesize = avail; + return TRUE; + } + + /* This should never happen */ + return FALSE; +} + + +/** + * gst_aac_parse_check_valid_frame: + * @parse: #GstBaseParse. + * @buffer: #GstBuffer. + * @framesize: If the buffer contains a valid frame, its size will be put here + * @skipsize: How much data parent class should skip in order to find the + * frame header. + * + * Implementation of "check_valid_frame" vmethod in #GstBaseParse class. + * + * Returns: TRUE if buffer contains a valid frame. + */ +gboolean +gst_aac_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + const guint8 *data; + GstAacParse *aacparse; + gboolean ret = FALSE; + gboolean lost_sync; + GstBuffer *buffer; + + aacparse = GST_AAC_PARSE (parse); + buffer = frame->buffer; + data = GST_BUFFER_DATA (buffer); + + lost_sync = GST_BASE_PARSE_LOST_SYNC (parse); + + if (aacparse->header_type == DSPAAC_HEADER_ADIF || + aacparse->header_type == DSPAAC_HEADER_NONE) { + /* There is nothing to parse */ + *framesize = GST_BUFFER_SIZE (buffer); + ret = TRUE; + + } else if (aacparse->header_type == DSPAAC_HEADER_NOT_PARSED || lost_sync) { + + ret = gst_aac_parse_detect_stream (aacparse, data, GST_BUFFER_SIZE (buffer), + GST_BASE_PARSE_DRAINING (parse), framesize, skipsize); + + } else if (aacparse->header_type == DSPAAC_HEADER_ADTS) { + guint needed_data = 1024; + + ret = gst_aac_parse_check_adts_frame (aacparse, data, + GST_BUFFER_SIZE (buffer), GST_BASE_PARSE_DRAINING (parse), + framesize, &needed_data); + + if (!ret) { + GST_DEBUG ("buffer didn't contain valid frame"); + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), + needed_data); + } + + } else { + GST_DEBUG ("buffer didn't contain valid frame"); + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 1024); + } + + return ret; +} + + +/** + * gst_aac_parse_parse_frame: + * @parse: #GstBaseParse. + * @buffer: #GstBuffer. + * + * Implementation of "parse_frame" vmethod in #GstBaseParse class. + * + * Also determines frame overhead. + * ADTS streams have a 7 byte header in each frame. MP4 and ADIF streams don't have + * a per-frame header. + * + * We're making a couple of simplifying assumptions: + * + * 1. We count Program Configuration Elements rather than searching for them + * in the streams to discount them - the overhead is negligible. + * + * 2. We ignore CRC. This has a worst-case impact of (num_raw_blocks + 1)*16 + * bits, which should still not be significant enough to warrant the + * additional parsing through the headers + * + * Returns: GST_FLOW_OK if frame was successfully parsed and can be pushed + * forward. Otherwise appropriate error is returned. + */ +GstFlowReturn +gst_aac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + GstAacParse *aacparse; + GstBuffer *buffer; + GstFlowReturn ret = GST_FLOW_OK; + gint rate, channels; + + aacparse = GST_AAC_PARSE (parse); + buffer = frame->buffer; + + if (G_UNLIKELY (aacparse->header_type != DSPAAC_HEADER_ADTS)) + return ret; + + /* see above */ + frame->overhead = 7; + + gst_aac_parse_parse_adts_header (aacparse, GST_BUFFER_DATA (buffer), + &rate, &channels, NULL, NULL); + GST_LOG_OBJECT (aacparse, "rate: %d, chans: %d", rate, channels); + + if (G_UNLIKELY (rate != aacparse->sample_rate + || channels != aacparse->channels)) { + aacparse->sample_rate = rate; + aacparse->channels = channels; + + if (!gst_aac_parse_set_src_caps (aacparse, + GST_PAD_CAPS (GST_BASE_PARSE (aacparse)->sinkpad))) { + /* If linking fails, we need to return appropriate error */ + ret = GST_FLOW_NOT_LINKED; + } + + gst_base_parse_set_frame_rate (GST_BASE_PARSE (aacparse), + aacparse->sample_rate, 1024, 2, 2); + } + + return ret; +} + + +/** + * gst_aac_parse_start: + * @parse: #GstBaseParse. + * + * Implementation of "start" vmethod in #GstBaseParse class. + * + * Returns: TRUE if startup succeeded. + */ +gboolean +gst_aac_parse_start (GstBaseParse * parse) +{ + GstAacParse *aacparse; + + aacparse = GST_AAC_PARSE (parse); + GST_DEBUG ("start"); + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (aacparse), 1024); + return TRUE; +} + + +/** + * gst_aac_parse_stop: + * @parse: #GstBaseParse. + * + * Implementation of "stop" vmethod in #GstBaseParse class. + * + * Returns: TRUE is stopping succeeded. + */ +gboolean +gst_aac_parse_stop (GstBaseParse * parse) +{ + GST_DEBUG ("stop"); + return TRUE; +} diff --git a/gst/audioparsers/gstaacparse.h b/gst/audioparsers/gstaacparse.h new file mode 100644 index 000000000..4020d8fc7 --- /dev/null +++ b/gst/audioparsers/gstaacparse.h @@ -0,0 +1,109 @@ +/* GStreamer AAC parser + * Copyright (C) 2008 Nokia Corporation. All rights reserved. + * + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +#ifndef __GST_AAC_PARSE_H__ +#define __GST_AAC_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_AAC_PARSE \ + (gst_aac_parse_get_type()) +#define GST_AAC_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AAC_PARSE, GstAacParse)) +#define GST_AAC_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AAC_PARSE, GstAacParseClass)) +#define GST_IS_AAC_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AAC_PARSE)) +#define GST_IS_AAC_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AAC_PARSE)) + + +/** + * GstAacHeaderType: + * @DSPAAC_HEADER_NOT_PARSED: Header not parsed yet. + * @DSPAAC_HEADER_UNKNOWN: Unknown (not recognized) header. + * @DSPAAC_HEADER_ADIF: ADIF header found. + * @DSPAAC_HEADER_ADTS: ADTS header found. + * @DSPAAC_HEADER_NONE: Raw stream, no header. + * + * Type header enumeration set in #header_type. + */ +typedef enum { + DSPAAC_HEADER_NOT_PARSED, + DSPAAC_HEADER_UNKNOWN, + DSPAAC_HEADER_ADIF, + DSPAAC_HEADER_ADTS, + DSPAAC_HEADER_NONE +} GstAacHeaderType; + + +typedef struct _GstAacParse GstAacParse; +typedef struct _GstAacParseClass GstAacParseClass; + +/** + * GstAacParse: + * @element: the parent element. + * @object_type: AAC object type of the stream. + * @bitrate: Current media bitrate. + * @sample_rate: Current media samplerate. + * @channels: Current media channel count. + * @frames_per_sec: FPS value of the current stream. + * @header_type: #GstAacHeaderType indicating the current stream type. + * @framecount: The amount of frames that has been processed this far. + * @bytecount: The amount of bytes that has been processed this far. + * @sync: Tells whether the parser is in sync (a.k.a. not searching for header) + * @eos: End-of-Stream indicator. Set when EOS event arrives. + * @duration: Duration of the current stream. + * @ts: Current stream timestamp. + * + * The opaque GstAacParse data structure. + */ +struct _GstAacParse { + GstBaseParse element; + + /* Stream type -related info */ + gint object_type; + gint bitrate; + gint sample_rate; + gint channels; + gint mpegversion; + + GstAacHeaderType header_type; +}; + +/** + * GstAacParseClass: + * @parent_class: Element parent class. + * + * The opaque GstAacParseClass data structure. + */ +struct _GstAacParseClass { + GstBaseParseClass parent_class; +}; + +GType gst_aac_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_AAC_PARSE_H__ */ diff --git a/gst/audioparsers/gstac3parse.c b/gst/audioparsers/gstac3parse.c new file mode 100644 index 000000000..ee22e3db7 --- /dev/null +++ b/gst/audioparsers/gstac3parse.c @@ -0,0 +1,507 @@ +/* GStreamer AC3 parser + * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net> + * Copyright (C) 2009 Mark Nauwelaerts <mnauw users sf net> + * Copyright (C) 2009 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ +/** + * SECTION:element-ac3parse + * @short_description: AC3 parser + * @see_also: #GstAmrParse, #GstAACParse + * + * This is an AC3 parser. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink + * ]| + * </refsect2> + */ + +/* TODO: + * - add support for audio/x-private1-ac3 as well + * - should accept framed and unframed input (needs decodebin fixes first) + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstac3parse.h" +#include <gst/base/gstbytereader.h> +#include <gst/base/gstbitreader.h> + +GST_DEBUG_CATEGORY_STATIC (ac3_parse_debug); +#define GST_CAT_DEFAULT ac3_parse_debug + +static const struct +{ + const guint bit_rate; /* nominal bit rate */ + const guint frame_size[3]; /* frame size for 32kHz, 44kHz, and 48kHz */ +} frmsizcod_table[38] = { + { + 32, { + 64, 69, 96}}, { + 32, { + 64, 70, 96}}, { + 40, { + 80, 87, 120}}, { + 40, { + 80, 88, 120}}, { + 48, { + 96, 104, 144}}, { + 48, { + 96, 105, 144}}, { + 56, { + 112, 121, 168}}, { + 56, { + 112, 122, 168}}, { + 64, { + 128, 139, 192}}, { + 64, { + 128, 140, 192}}, { + 80, { + 160, 174, 240}}, { + 80, { + 160, 175, 240}}, { + 96, { + 192, 208, 288}}, { + 96, { + 192, 209, 288}}, { + 112, { + 224, 243, 336}}, { + 112, { + 224, 244, 336}}, { + 128, { + 256, 278, 384}}, { + 128, { + 256, 279, 384}}, { + 160, { + 320, 348, 480}}, { + 160, { + 320, 349, 480}}, { + 192, { + 384, 417, 576}}, { + 192, { + 384, 418, 576}}, { + 224, { + 448, 487, 672}}, { + 224, { + 448, 488, 672}}, { + 256, { + 512, 557, 768}}, { + 256, { + 512, 558, 768}}, { + 320, { + 640, 696, 960}}, { + 320, { + 640, 697, 960}}, { + 384, { + 768, 835, 1152}}, { + 384, { + 768, 836, 1152}}, { + 448, { + 896, 975, 1344}}, { + 448, { + 896, 976, 1344}}, { + 512, { + 1024, 1114, 1536}}, { + 512, { + 1024, 1115, 1536}}, { + 576, { + 1152, 1253, 1728}}, { + 576, { + 1152, 1254, 1728}}, { + 640, { + 1280, 1393, 1920}}, { + 640, { + 1280, 1394, 1920}} +}; + +static const guint fscod_rates[4] = { 48000, 44100, 32000, 0 }; +static const guint acmod_chans[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; +static const guint numblks[4] = { 1, 2, 3, 6 }; + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) true, " + " channels = (int) [ 1, 6 ], rate = (int) [ 32000, 48000 ]; " + "audio/x-eac3, framed = (boolean) true, " + " channels = (int) [ 1, 6 ], rate = (int) [ 32000, 48000 ] ")); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) false; " + "audio/x-eac3, framed = (boolean) false; " + "audio/ac3, framed = (boolean) false ")); + +static void gst_ac3_parse_finalize (GObject * object); + +static gboolean gst_ac3_parse_start (GstBaseParse * parse); +static gboolean gst_ac3_parse_stop (GstBaseParse * parse); +static gboolean gst_ac3_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * size, gint * skipsize); +static GstFlowReturn gst_ac3_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); + +GST_BOILERPLATE (GstAc3Parse, gst_ac3_parse, GstBaseParse, GST_TYPE_BASE_PARSE); + +static void +gst_ac3_parse_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, + "AC3 audio stream parser", "Codec/Parser/Audio", + "AC3 parser", "Tim-Philipp Müller <tim centricular net>"); +} + +static void +gst_ac3_parse_class_init (GstAc3ParseClass * klass) +{ + GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0, + "AC3 audio stream parser"); + + object_class->finalize = gst_ac3_parse_finalize; + + parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start); + parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop); + parse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_ac3_parse_check_valid_frame); + parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_parse_frame); +} + +static void +gst_ac3_parse_reset (GstAc3Parse * ac3parse) +{ + ac3parse->channels = -1; + ac3parse->sample_rate = -1; + ac3parse->eac = FALSE; +} + +static void +gst_ac3_parse_init (GstAc3Parse * ac3parse, GstAc3ParseClass * klass) +{ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 64 * 2); + gst_ac3_parse_reset (ac3parse); +} + +static void +gst_ac3_parse_finalize (GObject * object) +{ + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_ac3_parse_start (GstBaseParse * parse) +{ + GstAc3Parse *ac3parse = GST_AC3_PARSE (parse); + + GST_DEBUG_OBJECT (parse, "starting"); + + gst_ac3_parse_reset (ac3parse); + + return TRUE; +} + +static gboolean +gst_ac3_parse_stop (GstBaseParse * parse) +{ + GST_DEBUG_OBJECT (parse, "stopping"); + + return TRUE; +} + +static gboolean +gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf, + guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid) +{ + GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf); + guint8 fscod, frmsizcod, bsid, acmod, lfe_on; + + GST_LOG_OBJECT (ac3parse, "parsing ac3"); + + gst_bit_reader_skip_unchecked (&bits, 16 + 16); + fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); + frmsizcod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 6); + + if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) { + GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod); + return FALSE; + } + + bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5); + gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */ + acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); + + /* spec not quite clear here: decoder should decode if less than 8, + * but seemingly only defines 6 and 8 cases */ + if (bsid > 8) { + GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid); + return FALSE; + } else if (bsid != 8 && bsid != 6) { + GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid); + } + + if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */ + gst_bit_reader_skip_unchecked (&bits, 2); + if ((acmod & 0x4)) /* if a surround channel exists */ + gst_bit_reader_skip_unchecked (&bits, 2); + if (acmod == 0x2) /* if in 2/0 mode */ + gst_bit_reader_skip_unchecked (&bits, 2); + + lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); + + if (frame_size) + *frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2; + if (rate) + *rate = fscod_rates[fscod]; + if (chans) + *chans = acmod_chans[acmod] + lfe_on; + if (blks) + *blks = 6; + if (sid) + *sid = 0; + + return TRUE; +} + +static gboolean +gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf, + guint * frame_size, guint * rate, guint * chans, guint * blks, guint * sid) +{ + GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf); + guint16 frmsiz, sample_rate, blocks; + guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod; + + GST_LOG_OBJECT (ac3parse, "parsing e-ac3"); + + gst_bit_reader_skip_unchecked (&bits, 16); + strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */ + if (G_UNLIKELY (strmtyp == 3)) { + GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp); + return FALSE; + } + + strmid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* substreamid */ + frmsiz = gst_bit_reader_get_bits_uint16_unchecked (&bits, 11); /* frmsiz */ + fscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod */ + if (fscod == 3) { + fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */ + if (G_UNLIKELY (fscod2 == 3)) { + GST_DEBUG_OBJECT (ac3parse, "invalid fscod2"); + return FALSE; + } + sample_rate = fscod_rates[fscod2] / 2; + blocks = 6; + } else { + numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */ + sample_rate = fscod_rates[fscod]; + blocks = numblks[numblkscod]; + } + + acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */ + lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */ + + gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */ + + if (frame_size) + *frame_size = (frmsiz + 1) * 2; + if (rate) + *rate = sample_rate; + if (chans) + *chans = acmod_chans[acmod] + lfe_on; + if (blks) + *blks = blocks; + if (sid) + *sid = (strmtyp & 0x1) << 3 | strmid; + + return TRUE; +} + +static gboolean +gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, + guint * framesize, guint * rate, guint * chans, guint * blocks, + guint * sid, gboolean * eac) +{ + GstBitReader bits = GST_BIT_READER_INIT_FROM_BUFFER (buf); + guint16 sync; + guint8 bsid; + + GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", GST_BUFFER_DATA (buf), 16); + + sync = gst_bit_reader_get_bits_uint16_unchecked (&bits, 16); + gst_bit_reader_skip_unchecked (&bits, 16 + 8); + bsid = gst_bit_reader_peek_bits_uint8_unchecked (&bits, 5); + + if (G_UNLIKELY (sync != 0x0b77)) + return FALSE; + + GST_LOG_OBJECT (parse, "bsid = %d", bsid); + + if (bsid <= 10) { + if (eac) + *eac = FALSE; + return gst_ac3_parse_frame_header_ac3 (parse, buf, framesize, rate, chans, + blocks, sid); + } else if (bsid <= 16) { + if (eac) + *eac = TRUE; + return gst_ac3_parse_frame_header_eac3 (parse, buf, framesize, rate, chans, + blocks, sid); + } else { + GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid); + return FALSE; + } +} + +static gboolean +gst_ac3_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + GstAc3Parse *ac3parse = GST_AC3_PARSE (parse); + GstBuffer *buf = frame->buffer; + GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf); + gint off; + gboolean lost_sync, draining; + + if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6)) + return FALSE; + + off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000, + 0, GST_BUFFER_SIZE (buf)); + + GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off); + + /* didn't find anything that looks like a sync word, skip */ + if (off < 0) { + *skipsize = GST_BUFFER_SIZE (buf) - 3; + return FALSE; + } + + /* possible frame header, but not at offset 0? skip bytes before sync */ + if (off > 0) { + *skipsize = off; + return FALSE; + } + + /* make sure the values in the frame header look sane */ + if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, NULL, NULL, + NULL, NULL, NULL)) { + *skipsize = off + 2; + return FALSE; + } + + GST_LOG_OBJECT (parse, "got frame"); + + lost_sync = GST_BASE_PARSE_LOST_SYNC (parse); + draining = GST_BASE_PARSE_DRAINING (parse); + + if (lost_sync && !draining) { + guint16 word = 0; + + GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword"); + + if (!gst_byte_reader_skip (&reader, *framesize) || + !gst_byte_reader_get_uint16_be (&reader, &word)) { + GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data"); + gst_base_parse_set_min_frame_size (parse, *framesize + 6); + *skipsize = 0; + return FALSE; + } else { + if (word != 0x0b77) { + GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word); + *skipsize = off + 2; + return FALSE; + } else { + /* ok, got sync now, let's assume constant frame size */ + gst_base_parse_set_min_frame_size (parse, *framesize); + } + } + } + + return TRUE; +} + +static GstFlowReturn +gst_ac3_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + GstAc3Parse *ac3parse = GST_AC3_PARSE (parse); + GstBuffer *buf = frame->buffer; + guint fsize, rate, chans, blocks, sid; + gboolean eac; + + if (!gst_ac3_parse_frame_header (ac3parse, buf, &fsize, &rate, &chans, + &blocks, &sid, &eac)) + goto broken_header; + + GST_LOG_OBJECT (parse, "size: %u, rate: %u, chans: %u", fsize, rate, chans); + + if (G_UNLIKELY (sid)) { + /* dependent frame, no need to (ac)count for or consider further */ + GST_LOG_OBJECT (parse, "sid: %d", sid); + frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME; + /* TODO maybe also mark as DELTA_UNIT, + * if that does not surprise baseparse elsewhere */ + /* occupies same time space as previous base frame */ + if (G_LIKELY (GST_BUFFER_TIMESTAMP (buf) >= GST_BUFFER_DURATION (buf))) + GST_BUFFER_TIMESTAMP (buf) -= GST_BUFFER_DURATION (buf); + /* only return if we already arranged for caps */ + if (G_LIKELY (ac3parse->sample_rate > 0)) + return GST_FLOW_OK; + } + + if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans + || ac3parse->eac != ac3parse->eac)) { + GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3", + "framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate, + "channels", G_TYPE_INT, chans, NULL); + gst_buffer_set_caps (buf, caps); + gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps); + gst_caps_unref (caps); + + ac3parse->sample_rate = rate; + ac3parse->channels = chans; + ac3parse->eac = eac; + + gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2); + } + + return GST_FLOW_OK; + +/* ERRORS */ +broken_header: + { + /* this really shouldn't ever happen */ + GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL)); + return GST_FLOW_ERROR; + } +} diff --git a/gst/audioparsers/gstac3parse.h b/gst/audioparsers/gstac3parse.h new file mode 100644 index 000000000..6ed01ddf5 --- /dev/null +++ b/gst/audioparsers/gstac3parse.h @@ -0,0 +1,73 @@ +/* GStreamer AC3 parser + * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net> + * Copyright (C) 2009 Mark Nauwelaerts <mnauw users sf net> + * Copyright (C) 2009 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +#ifndef __GST_AC3_PARSE_H__ +#define __GST_AC3_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_AC3_PARSE \ + (gst_ac3_parse_get_type()) +#define GST_AC3_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AC3_PARSE, GstAc3Parse)) +#define GST_AC3_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AC3_PARSE, GstAc3ParseClass)) +#define GST_IS_AC3_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AC3_PARSE)) +#define GST_IS_AC3_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AC3_PARSE)) + +typedef struct _GstAc3Parse GstAc3Parse; +typedef struct _GstAc3ParseClass GstAc3ParseClass; + +/** + * GstAc3Parse: + * + * The opaque GstAc3Parse object + */ +struct _GstAc3Parse { + GstBaseParse baseparse; + + /*< private >*/ + gint sample_rate; + gint channels; + gboolean eac; +}; + +/** + * GstAc3ParseClass: + * @parent_class: Element parent class. + * + * The opaque GstAc3ParseClass data structure. + */ +struct _GstAc3ParseClass { + GstBaseParseClass baseparse_class; +}; + +GType gst_ac3_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_AC3_PARSE_H__ */ diff --git a/gst/audioparsers/gstamrparse.c b/gst/audioparsers/gstamrparse.c new file mode 100644 index 000000000..99d31b9ef --- /dev/null +++ b/gst/audioparsers/gstamrparse.c @@ -0,0 +1,378 @@ +/* GStreamer Adaptive Multi-Rate parser plugin + * Copyright (C) 2006 Edgard Lima <edgard.lima@indt.org.br> + * Copyright (C) 2008 Nokia Corporation. All rights reserved. + * + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +/** + * SECTION:element-amrparse + * @short_description: AMR parser + * @see_also: #GstAmrnbDec, #GstAmrnbEnc + * + * This is an AMR parser capable of handling both narrow-band and wideband + * formats. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch filesrc location=abc.amr ! amrparse ! amrdec ! audioresample ! audioconvert ! alsasink + * ]| + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstamrparse.h" + + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1;" + "audio/AMR-WB, " "rate = (int) 16000, " "channels = (int) 1;") + ); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-amr-nb-sh; audio/x-amr-wb-sh")); + +GST_DEBUG_CATEGORY_STATIC (amrparse_debug); +#define GST_CAT_DEFAULT amrparse_debug + +static const gint block_size_nb[16] = + { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; + +static const gint block_size_wb[16] = + { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, 0, 0 }; + +/* AMR has a "hardcoded" framerate of 50fps */ +#define AMR_FRAMES_PER_SECOND 50 +#define AMR_FRAME_DURATION (GST_SECOND/AMR_FRAMES_PER_SECOND) +#define AMR_MIME_HEADER_SIZE 9 + +gboolean gst_amr_parse_start (GstBaseParse * parse); +gboolean gst_amr_parse_stop (GstBaseParse * parse); + +static gboolean gst_amr_parse_sink_setcaps (GstBaseParse * parse, + GstCaps * caps); + +gboolean gst_amr_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize); + +GstFlowReturn gst_amr_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); + +#define _do_init(bla) \ + GST_DEBUG_CATEGORY_INIT (amrparse_debug, "amrparse", 0, \ + "AMR-NB audio stream parser"); + +GST_BOILERPLATE_FULL (GstAmrParse, gst_amr_parse, GstBaseParse, + GST_TYPE_BASE_PARSE, _do_init); + + +/** + * gst_amr_parse_base_init: + * @klass: #GstElementClass. + * + */ +static void +gst_amr_parse_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, + "AMR audio stream parser", "Codec/Parser/Audio", + "Adaptive Multi-Rate audio parser", + "Ronald Bultje <rbultje@ronald.bitfreak.net>"); +} + + +/** + * gst_amr_parse_class_init: + * @klass: GstAmrParseClass. + * + */ +static void +gst_amr_parse_class_init (GstAmrParseClass * klass) +{ + GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); + + parse_class->start = GST_DEBUG_FUNCPTR (gst_amr_parse_start); + parse_class->stop = GST_DEBUG_FUNCPTR (gst_amr_parse_stop); + parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_amr_parse_sink_setcaps); + parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_amr_parse_parse_frame); + parse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_amr_parse_check_valid_frame); +} + + +/** + * gst_amr_parse_init: + * @amrparse: #GstAmrParse + * @klass: #GstAmrParseClass. + * + */ +static void +gst_amr_parse_init (GstAmrParse * amrparse, GstAmrParseClass * klass) +{ + /* init rest */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 62); + GST_DEBUG ("initialized"); + +} + + +/** + * gst_amr_parse_set_src_caps: + * @amrparse: #GstAmrParse. + * + * Set source pad caps according to current knowledge about the + * audio stream. + * + * Returns: TRUE if caps were successfully set. + */ +static gboolean +gst_amr_parse_set_src_caps (GstAmrParse * amrparse) +{ + GstCaps *src_caps = NULL; + gboolean res = FALSE; + + if (amrparse->wide) { + GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-WB"); + src_caps = gst_caps_new_simple ("audio/AMR-WB", + "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 16000, NULL); + } else { + GST_DEBUG_OBJECT (amrparse, "setting srcpad caps to AMR-NB"); + /* Max. size of NB frame is 31 bytes, so we can set the min. frame + size to 32 (+1 for next frame header) */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (amrparse), 32); + src_caps = gst_caps_new_simple ("audio/AMR", + "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL); + } + gst_pad_use_fixed_caps (GST_BASE_PARSE (amrparse)->srcpad); + res = gst_pad_set_caps (GST_BASE_PARSE (amrparse)->srcpad, src_caps); + gst_caps_unref (src_caps); + return res; +} + + +/** + * gst_amr_parse_sink_setcaps: + * @sinkpad: GstPad + * @caps: GstCaps + * + * Returns: TRUE on success. + */ +static gboolean +gst_amr_parse_sink_setcaps (GstBaseParse * parse, GstCaps * caps) +{ + GstAmrParse *amrparse; + GstStructure *structure; + const gchar *name; + + amrparse = GST_AMR_PARSE (parse); + structure = gst_caps_get_structure (caps, 0); + name = gst_structure_get_name (structure); + + GST_DEBUG_OBJECT (amrparse, "setcaps: %s", name); + + if (!strncmp (name, "audio/x-amr-wb-sh", 17)) { + amrparse->block_size = block_size_wb; + amrparse->wide = 1; + } else if (!strncmp (name, "audio/x-amr-nb-sh", 17)) { + amrparse->block_size = block_size_nb; + amrparse->wide = 0; + } else { + GST_WARNING ("Unknown caps"); + return FALSE; + } + + amrparse->need_header = FALSE; + gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2); + gst_amr_parse_set_src_caps (amrparse); + return TRUE; +} + +/** + * gst_amr_parse_parse_header: + * @amrparse: #GstAmrParse + * @data: Header data to be parsed. + * @skipsize: Output argument where the frame size will be stored. + * + * Check if the given data contains an AMR mime header. + * + * Returns: TRUE on success. + */ +static gboolean +gst_amr_parse_parse_header (GstAmrParse * amrparse, + const guint8 * data, gint * skipsize) +{ + GST_DEBUG_OBJECT (amrparse, "Parsing header data"); + + if (!memcmp (data, "#!AMR-WB\n", 9)) { + GST_DEBUG_OBJECT (amrparse, "AMR-WB detected"); + amrparse->block_size = block_size_wb; + amrparse->wide = TRUE; + *skipsize = amrparse->header = 9; + } else if (!memcmp (data, "#!AMR\n", 6)) { + GST_DEBUG_OBJECT (amrparse, "AMR-NB detected"); + amrparse->block_size = block_size_nb; + amrparse->wide = FALSE; + *skipsize = amrparse->header = 6; + } else + return FALSE; + + gst_amr_parse_set_src_caps (amrparse); + return TRUE; +} + + +/** + * gst_amr_parse_check_valid_frame: + * @parse: #GstBaseParse. + * @buffer: #GstBuffer. + * @framesize: Output variable where the found frame size is put. + * @skipsize: Output variable which tells how much data needs to be skipped + * until a frame header is found. + * + * Implementation of "check_valid_frame" vmethod in #GstBaseParse class. + * + * Returns: TRUE if the given data contains valid frame. + */ +gboolean +gst_amr_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + GstBuffer *buffer; + const guint8 *data; + gint fsize, mode, dsize; + GstAmrParse *amrparse; + + amrparse = GST_AMR_PARSE (parse); + buffer = frame->buffer; + data = GST_BUFFER_DATA (buffer); + dsize = GST_BUFFER_SIZE (buffer); + + GST_LOG ("buffer: %d bytes", dsize); + + if (amrparse->need_header) { + if (dsize >= AMR_MIME_HEADER_SIZE && + gst_amr_parse_parse_header (amrparse, data, skipsize)) { + amrparse->need_header = FALSE; + gst_base_parse_set_frame_rate (GST_BASE_PARSE (amrparse), 50, 1, 2, 2); + } else { + GST_WARNING ("media doesn't look like a AMR format"); + } + /* We return FALSE, so this frame won't get pushed forward. Instead, + the "skip" value is set, so next time we will receive a valid frame. */ + return FALSE; + } + + /* Does this look like a possible frame header candidate? */ + if ((data[0] & 0x83) == 0) { + /* Yep. Retrieve the frame size */ + mode = (data[0] >> 3) & 0x0F; + fsize = amrparse->block_size[mode] + 1; /* +1 for the header byte */ + + /* We recognize this data as a valid frame when: + * - We are in sync. There is no need for extra checks then + * - We are in EOS. There might not be enough data to check next frame + * - Sync is lost, but the following data after this frame seem + * to contain a valid header as well (and there is enough data to + * perform this check) + */ + if (fsize && + (!GST_BASE_PARSE_LOST_SYNC (parse) || GST_BASE_PARSE_DRAINING (parse) + || (dsize > fsize && (data[fsize] & 0x83) == 0))) { + *framesize = fsize; + return TRUE; + } + } + + GST_LOG ("sync lost"); + return FALSE; +} + + +/** + * gst_amr_parse_parse_frame: + * @parse: #GstBaseParse. + * @buffer: #GstBuffer. + * + * Implementation of "parse" vmethod in #GstBaseParse class. + * + * Returns: #GstFlowReturn defining the parsing status. + */ +GstFlowReturn +gst_amr_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + return GST_FLOW_OK; +} + + +/** + * gst_amr_parse_start: + * @parse: #GstBaseParse. + * + * Implementation of "start" vmethod in #GstBaseParse class. + * + * Returns: TRUE on success. + */ +gboolean +gst_amr_parse_start (GstBaseParse * parse) +{ + GstAmrParse *amrparse; + + amrparse = GST_AMR_PARSE (parse); + GST_DEBUG ("start"); + amrparse->need_header = TRUE; + amrparse->header = 0; + return TRUE; +} + + +/** + * gst_amr_parse_stop: + * @parse: #GstBaseParse. + * + * Implementation of "stop" vmethod in #GstBaseParse class. + * + * Returns: TRUE on success. + */ +gboolean +gst_amr_parse_stop (GstBaseParse * parse) +{ + GstAmrParse *amrparse; + + amrparse = GST_AMR_PARSE (parse); + GST_DEBUG ("stop"); + amrparse->need_header = TRUE; + amrparse->header = 0; + return TRUE; +} diff --git a/gst/audioparsers/gstamrparse.h b/gst/audioparsers/gstamrparse.h new file mode 100644 index 000000000..86a26e026 --- /dev/null +++ b/gst/audioparsers/gstamrparse.h @@ -0,0 +1,82 @@ +/* GStreamer Adaptive Multi-Rate parser + * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net> + * Copyright (C) 2008 Nokia Corporation. All rights reserved. + * + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +#ifndef __GST_AMR_PARSE_H__ +#define __GST_AMR_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_AMR_PARSE \ + (gst_amr_parse_get_type()) +#define GST_AMR_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AMR_PARSE, GstAmrParse)) +#define GST_AMR_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AMR_PARSE, GstAmrParseClass)) +#define GST_IS_AMR_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AMR_PARSE)) +#define GST_IS_AMR_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AMR_PARSE)) + + +typedef struct _GstAmrParse GstAmrParse; +typedef struct _GstAmrParseClass GstAmrParseClass; + +/** + * GstAmrParse: + * @element: the parent element. + * @block_size: Pointer to frame size lookup table. + * @need_header: Tells whether the MIME header should be read in the beginning. + * @wide: Wideband mode. + * @eos: Indicates the EOS situation. Set when EOS event is received. + * @sync: Tells whether the parser is in sync. + * @framecount: Total amount of frames handled. + * @bytecount: Total amount of bytes handled. + * @ts: Timestamp of the current media. + * + * The opaque GstAacParse data structure. + */ +struct _GstAmrParse { + GstBaseParse element; + const gint *block_size; + gboolean need_header; + gint header; + gboolean wide; +}; + +/** + * GstAmrParseClass: + * @parent_class: Element parent class. + * + * The opaque GstAmrParseClass data structure. + */ +struct _GstAmrParseClass { + GstBaseParseClass parent_class; +}; + +GType gst_amr_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_AMR_PARSE_H__ */ diff --git a/gst/audioparsers/gstdcaparse.c b/gst/audioparsers/gstdcaparse.c new file mode 100644 index 000000000..2bf0e3882 --- /dev/null +++ b/gst/audioparsers/gstdcaparse.c @@ -0,0 +1,451 @@ +/* GStreamer DCA parser + * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net> + * + * 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. + */ + +/** + * SECTION:element-dcaparse + * @short_description: DCA (DTS Coherent Acoustics) parser + * @see_also: #GstAmrParse, #GstAACParse, #GstAc3Parse + * + * This is a DCA (DTS Coherent Acoustics) parser. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink + * ]| + * </refsect2> + */ + +/* TODO: + * - should accept framed and unframed input (needs decodebin fixes first) + * - seeking in raw .dts files doesn't seem to work, but duration estimate ok + * + * - if frames have 'odd' durations, the frame durations (plus timestamps) + * aren't adjusted up occasionally to make up for rounding error gaps. + * (e.g. if 512 samples per frame @ 48kHz = 10.666666667 ms/frame) + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstdcaparse.h" +#include <gst/base/gstbytereader.h> +#include <gst/base/gstbitreader.h> + +GST_DEBUG_CATEGORY_STATIC (dca_parse_debug); +#define GST_CAT_DEFAULT dca_parse_debug + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-dts," + " framed = (boolean) true," + " channels = (int) [ 1, 8 ]," + " rate = (int) [ 8000, 192000 ]," + " depth = (int) { 14, 16 }," + " endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }")); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-dts, framed = (boolean) false")); + +static void gst_dca_parse_finalize (GObject * object); + +static gboolean gst_dca_parse_start (GstBaseParse * parse); +static gboolean gst_dca_parse_stop (GstBaseParse * parse); +static gboolean gst_dca_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * size, gint * skipsize); +static GstFlowReturn gst_dca_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); + +GST_BOILERPLATE (GstDcaParse, gst_dca_parse, GstBaseParse, GST_TYPE_BASE_PARSE); + +static void +gst_dca_parse_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, + "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio", + "DCA parser", "Tim-Philipp Müller <tim centricular net>"); +} + +static void +gst_dca_parse_class_init (GstDcaParseClass * klass) +{ + GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + GST_DEBUG_CATEGORY_INIT (dca_parse_debug, "dcaparse", 0, + "DCA audio stream parser"); + + object_class->finalize = gst_dca_parse_finalize; + + parse_class->start = GST_DEBUG_FUNCPTR (gst_dca_parse_start); + parse_class->stop = GST_DEBUG_FUNCPTR (gst_dca_parse_stop); + parse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_dca_parse_check_valid_frame); + parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_parse_frame); +} + +static void +gst_dca_parse_reset (GstDcaParse * dcaparse) +{ + dcaparse->channels = -1; + dcaparse->rate = -1; + dcaparse->depth = -1; + dcaparse->endianness = -1; + dcaparse->block_size = -1; + dcaparse->frame_size = -1; + dcaparse->last_sync = 0; +} + +static void +gst_dca_parse_init (GstDcaParse * dcaparse, GstDcaParseClass * klass) +{ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse), + DCA_MIN_FRAMESIZE); + gst_dca_parse_reset (dcaparse); +} + +static void +gst_dca_parse_finalize (GObject * object) +{ + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_dca_parse_start (GstBaseParse * parse) +{ + GstDcaParse *dcaparse = GST_DCA_PARSE (parse); + + GST_DEBUG_OBJECT (parse, "starting"); + + gst_dca_parse_reset (dcaparse); + + return TRUE; +} + +static gboolean +gst_dca_parse_stop (GstBaseParse * parse) +{ + GST_DEBUG_OBJECT (parse, "stopping"); + + return TRUE; +} + +static gboolean +gst_dca_parse_parse_header (GstDcaParse * dcaparse, + const GstByteReader * reader, guint * frame_size, + guint * sample_rate, guint * channels, guint * depth, + gint * endianness, guint * num_blocks, guint * samples_per_block, + gboolean * terminator) +{ + static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025, + 22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000 + }; + static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, + 6, 6, 6, 7, 8, 8 + }; + GstByteReader r = *reader; + guint16 hdr[8]; + guint32 marker; + guint chans, lfe, i; + + if (gst_byte_reader_get_remaining (&r) < (4 + sizeof (hdr))) + return FALSE; + + marker = gst_byte_reader_peek_uint32_be_unchecked (&r); + + /* raw big endian or 14-bit big endian */ + if (marker == 0x7FFE8001 || marker == 0x1FFFE800) { + for (i = 0; i < G_N_ELEMENTS (hdr); ++i) + hdr[i] = gst_byte_reader_get_uint16_be_unchecked (&r); + } else + /* raw little endian or 14-bit little endian */ + if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) { + for (i = 0; i < G_N_ELEMENTS (hdr); ++i) + hdr[i] = gst_byte_reader_get_uint16_le_unchecked (&r); + } else { + return FALSE; + } + + GST_LOG_OBJECT (dcaparse, "dts sync marker 0x%08x at offset %u", marker, + gst_byte_reader_get_pos (reader)); + + /* 14-bit mode */ + if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) { + if ((hdr[2] & 0xFFF0) != 0x07F0) + return FALSE; + /* discard top 2 bits (2 void), shift in 2 */ + hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003); + /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */ + hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F); + hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F); + hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF); + hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF); + hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF); + hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF); + g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001); + } + + GST_LOG_OBJECT (dcaparse, "frame header: %04x%04x%04x%04x", + hdr[2], hdr[3], hdr[4], hdr[5]); + + *terminator = (hdr[2] & 0x80) ? FALSE : TRUE; + *samples_per_block = ((hdr[2] >> 10) & 0x1f) + 1; + *num_blocks = ((hdr[2] >> 2) & 0x7F) + 1; + *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1; + chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14); + *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F]; + lfe = (hdr[5] >> 9) & 0x03; + + GST_TRACE_OBJECT (dcaparse, "frame size %u, num_blocks %u, rate %u, " + "samples per block %u", *frame_size, *num_blocks, *sample_rate, + *samples_per_block); + + if (*num_blocks < 6 || *frame_size < 96 || *sample_rate == 0) + return FALSE; + + if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) + *frame_size = (*frame_size * 16) / 14; /* FIXME: round up? */ + + if (chans < G_N_ELEMENTS (channels_table)) + *channels = channels_table[chans] + ((lfe) ? 1 : 0); + else + *channels = 0; + + if (depth) + *depth = (marker == 0x1FFFE800 || marker == 0xFF1F00E8) ? 14 : 16; + if (endianness) + *endianness = (marker == 0xFE7F0180 || marker == 0xFF1F00E8) ? + G_LITTLE_ENDIAN : G_BIG_ENDIAN; + + GST_TRACE_OBJECT (dcaparse, "frame size %u, channels %u, rate %u, " + "num_blocks %u, samples_per_block %u", *frame_size, *channels, + *sample_rate, *num_blocks, *samples_per_block); + + return TRUE; +} + +static gint +gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader, + const GstBuffer * buf, guint32 * sync) +{ + guint32 best_sync = 0; + guint best_offset = G_MAXUINT; + gint off; + + /* FIXME: verify syncs via _parse_header() here already */ + + /* Raw little endian */ + off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xfe7f0180, + 0, GST_BUFFER_SIZE (buf)); + if (off >= 0 && off < best_offset) { + best_offset = off; + best_sync = 0xfe7f0180; + } + + /* Raw big endian */ + off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x7ffe8001, + 0, GST_BUFFER_SIZE (buf)); + if (off >= 0 && off < best_offset) { + best_offset = off; + best_sync = 0x7ffe8001; + } + + /* FIXME: check next 2 bytes as well for 14-bit formats (but then don't + * forget to adjust the *skipsize= in _check_valid_frame() */ + + /* 14-bit little endian */ + off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0xff1f00e8, + 0, GST_BUFFER_SIZE (buf)); + if (off >= 0 && off < best_offset) { + best_offset = off; + best_sync = 0xff1f00e8; + } + + /* 14-bit big endian */ + off = gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x1fffe800, + 0, GST_BUFFER_SIZE (buf)); + if (off >= 0 && off < best_offset) { + best_offset = off; + best_sync = 0x1fffe800; + } + + if (best_offset == G_MAXUINT) + return -1; + + *sync = best_sync; + return best_offset; +} + +static gboolean +gst_dca_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + GstDcaParse *dcaparse = GST_DCA_PARSE (parse); + GstBuffer *buf = frame->buffer; + GstByteReader r = GST_BYTE_READER_INIT_FROM_BUFFER (buf); + gboolean parser_draining; + gboolean parser_in_sync; + gboolean terminator; + guint32 sync = 0; + guint size, rate, chans, num_blocks, samples_per_block; + gint off = -1; + + if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 16)) + return FALSE; + + parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse); + + if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) { + off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff, + dcaparse->last_sync, 0, GST_BUFFER_SIZE (buf)); + } + + if (G_UNLIKELY (off < 0)) { + off = gst_dca_parse_find_sync (dcaparse, &r, buf, &sync); + } + + /* didn't find anything that looks like a sync word, skip */ + if (off < 0) { + *skipsize = GST_BUFFER_SIZE (buf) - 3; + GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize); + return FALSE; + } + + GST_LOG_OBJECT (parse, "possible sync %08x at buffer offset %d", sync, off); + + /* possible frame header, but not at offset 0? skip bytes before sync */ + if (off > 0) { + *skipsize = off; + return FALSE; + } + + /* make sure the values in the frame header look sane */ + if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, NULL, + NULL, &num_blocks, &samples_per_block, &terminator)) { + *skipsize = 4; + return FALSE; + } + + GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d", + sync, size, rate, chans); + + *framesize = size; + + dcaparse->last_sync = sync; + + parser_draining = GST_BASE_PARSE_DRAINING (parse); + + if (!parser_in_sync && !parser_draining) { + /* check for second frame to be sure */ + GST_DEBUG_OBJECT (dcaparse, "resyncing; checking next frame syncword"); + if (GST_BUFFER_SIZE (buf) >= (size + 16)) { + guint s2, r2, c2, n2, s3; + gboolean t; + + GST_MEMDUMP ("buf", GST_BUFFER_DATA (buf), size + 16); + gst_byte_reader_init_from_buffer (&r, buf); + gst_byte_reader_skip_unchecked (&r, size); + + if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL, + &n2, &s3, &t)) { + GST_DEBUG_OBJECT (dcaparse, "didn't find second syncword"); + *skipsize = 4; + return FALSE; + } + + /* ok, got sync now, let's assume constant frame size */ + gst_base_parse_set_min_frame_size (parse, size); + } else { + /* FIXME: baseparse always seems to hand us buffers of min_frame_size + * bytes, which is unhelpful here */ + GST_LOG_OBJECT (dcaparse, "next sync out of reach (%u < %u)", + GST_BUFFER_SIZE (buf), size + 16); + /* *skipsize = 0; */ + /* return FALSE; */ + } + } + + return TRUE; +} + +static GstFlowReturn +gst_dca_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + GstDcaParse *dcaparse = GST_DCA_PARSE (parse); + GstBuffer *buf = frame->buffer; + GstByteReader r = GST_BYTE_READER_INIT_FROM_BUFFER (buf); + guint size, rate, chans, depth, block_size, num_blocks, samples_per_block; + gint endianness; + gboolean terminator; + + if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth, + &endianness, &num_blocks, &samples_per_block, &terminator)) + goto broken_header; + + block_size = num_blocks * samples_per_block; + + if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans + || dcaparse->depth != depth || dcaparse->endianness != endianness + || (!terminator && dcaparse->block_size != block_size) + || (size != dcaparse->frame_size))) { + GstCaps *caps; + + caps = gst_caps_new_simple ("audio/x-dts", + "framed", G_TYPE_BOOLEAN, TRUE, + "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans, + "endianness", G_TYPE_INT, endianness, "depth", G_TYPE_INT, depth, + "block-size", G_TYPE_INT, block_size, "frame-size", G_TYPE_INT, size, + NULL); + gst_buffer_set_caps (buf, caps); + gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps); + gst_caps_unref (caps); + + dcaparse->rate = rate; + dcaparse->channels = chans; + dcaparse->depth = depth; + dcaparse->endianness = endianness; + dcaparse->block_size = block_size; + dcaparse->frame_size = size; + + gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0); + } + + return GST_FLOW_OK; + +/* ERRORS */ +broken_header: + { + /* this really shouldn't ever happen */ + GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL)); + return GST_FLOW_ERROR; + } +} diff --git a/gst/audioparsers/gstdcaparse.h b/gst/audioparsers/gstdcaparse.h new file mode 100644 index 000000000..b3e066bd0 --- /dev/null +++ b/gst/audioparsers/gstdcaparse.h @@ -0,0 +1,78 @@ +/* GStreamer DCA parser + * Copyright (C) 2010 Tim-Philipp Müller <tim centricular net> + * + * 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. + */ + +#ifndef __GST_DCA_PARSE_H__ +#define __GST_DCA_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_DCA_PARSE \ + (gst_dca_parse_get_type()) +#define GST_DCA_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DCA_PARSE, GstDcaParse)) +#define GST_DCA_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_DCA_PARSE, GstDcaParseClass)) +#define GST_IS_DCA_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_DCA_PARSE)) +#define GST_IS_DCA_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_DCA_PARSE)) + +#define DCA_MIN_FRAMESIZE 96 +#define DCA_MAX_FRAMESIZE 18725 /* 16384*16/14 */ + +typedef struct _GstDcaParse GstDcaParse; +typedef struct _GstDcaParseClass GstDcaParseClass; + +/** + * GstDcaParse: + * + * The opaque GstDcaParse object + */ +struct _GstDcaParse { + GstBaseParse baseparse; + + /*< private >*/ + gint rate; + gint channels; + gint depth; + gint endianness; + gint block_size; + gint frame_size; + + guint32 last_sync; +}; + +/** + * GstDcaParseClass: + * @parent_class: Element parent class. + * + * The opaque GstDcaParseClass data structure. + */ +struct _GstDcaParseClass { + GstBaseParseClass baseparse_class; +}; + +GType gst_dca_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_DCA_PARSE_H__ */ diff --git a/gst/audioparsers/gstflacparse.c b/gst/audioparsers/gstflacparse.c new file mode 100644 index 000000000..0249e88a2 --- /dev/null +++ b/gst/audioparsers/gstflacparse.c @@ -0,0 +1,1355 @@ +/* GStreamer + * + * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>. + * Copyright (C) 2009 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk> + * Copyright (C) 2009 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +/** + * SECTION:element-flacparse + * @see_also: flacdec, oggdemux, vorbisparse + * + * The flacparse element will parse the header packets of the FLAC + * stream and put them as the streamheader in the caps. This is used in the + * multifdsink case where you want to stream live FLAC streams to multiple + * clients, each client has to receive the streamheaders first before they can + * consume the FLAC packets. + * + * This element also makes sure that the buffers that it pushes out are properly + * timestamped and that their offset and offset_end are set. The buffers that + * flacparse outputs have all of the metadata that oggmux expects to receive, + * which allows you to (for example) remux an ogg/flac or convert a native FLAC + * format file to an ogg bitstream. + * + * <refsect2> + * <title>Example pipelines</title> + * |[ + * gst-launch -v filesrc location=sine.flac ! flacparse ! identity \ + * ! oggmux ! filesink location=sine-remuxed.ogg + * ]| This pipeline converts a native FLAC format file to an ogg bitstream. + * It also illustrates that the streamheader is set in the caps, and that each + * buffer has the timestamp, duration, offset, and offset_end set. + * </refsect2> + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstflacparse.h" + +#include <string.h> +#include <gst/tag/tag.h> +#include <gst/audio/audio.h> + +#include <gst/base/gstbitreader.h> +#include <gst/base/gstbytereader.h> + +GST_DEBUG_CATEGORY_STATIC (flacparse_debug); +#define GST_CAT_DEFAULT flacparse_debug + +/* CRC-8, poly = x^8 + x^2 + x^1 + x^0, init = 0 */ +static const guint8 crc8_table[256] = { + 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, + 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, + 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, + 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, + 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, + 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, + 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, + 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, + 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, + 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, + 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, + 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A, + 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, + 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, + 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, + 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, + 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, + 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, + 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, + 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, + 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, + 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, + 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, + 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34, + 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, + 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, + 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, + 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, + 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, + 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, + 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, + 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 +}; + +static guint8 +gst_flac_calculate_crc8 (const guint8 * data, guint length) +{ + guint8 crc = 0; + + while (length--) { + crc = crc8_table[crc ^ *data]; + ++data; + } + + return crc; +} + +/* CRC-16, poly = x^16 + x^15 + x^2 + x^0, init = 0 */ +static const guint16 crc16_table[256] = { + 0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011, + 0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022, + 0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072, + 0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041, + 0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2, + 0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1, + 0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1, + 0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082, + 0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192, + 0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1, + 0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1, + 0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2, + 0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151, + 0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162, + 0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132, + 0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101, + 0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312, + 0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321, + 0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371, + 0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342, + 0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1, + 0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2, + 0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2, + 0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381, + 0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291, + 0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2, + 0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2, + 0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1, + 0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252, + 0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261, + 0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231, + 0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202 +}; + +static guint16 +gst_flac_calculate_crc16 (const guint8 * data, guint length) +{ + guint16 crc = 0; + + while (length--) { + crc = ((crc << 8) ^ crc16_table[(crc >> 8) ^ *data]) & 0xffff; + data++; + } + + return crc; +} + +enum +{ + PROP_0, + PROP_CHECK_FRAME_CHECKSUMS +}; + +#define DEFAULT_CHECK_FRAME_CHECKSUMS FALSE + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) true, " + "channels = (int) [ 1, 8 ], " "rate = (int) [ 1, 655350 ]") + ); + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-flac, framed = (boolean) false") + ); + +static void gst_flac_parse_finalize (GObject * object); +static void gst_flac_parse_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_flac_parse_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static gboolean gst_flac_parse_start (GstBaseParse * parse); +static gboolean gst_flac_parse_stop (GstBaseParse * parse); +static gboolean gst_flac_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize); +static GstFlowReturn gst_flac_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); +static GstFlowReturn gst_flac_parse_pre_push_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); + +GST_BOILERPLATE (GstFlacParse, gst_flac_parse, GstBaseParse, + GST_TYPE_BASE_PARSE); + +static void +gst_flac_parse_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_factory)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_factory)); + + gst_element_class_set_details_simple (element_class, "FLAC audio parser", + "Codec/Parser/Audio", + "Parses audio with the FLAC lossless audio codec", + "Sebastian Dröge <sebastian.droege@collabora.co.uk>"); + + GST_DEBUG_CATEGORY_INIT (flacparse_debug, "flacparse", 0, + "Flac parser element"); +} + +static void +gst_flac_parse_class_init (GstFlacParseClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass); + + gobject_class->finalize = gst_flac_parse_finalize; + gobject_class->set_property = gst_flac_parse_set_property; + gobject_class->get_property = gst_flac_parse_get_property; + + g_object_class_install_property (gobject_class, PROP_CHECK_FRAME_CHECKSUMS, + g_param_spec_boolean ("check-frame-checksums", "Check Frame Checksums", + "Check the overall checksums of every frame", + DEFAULT_CHECK_FRAME_CHECKSUMS, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + baseparse_class->start = GST_DEBUG_FUNCPTR (gst_flac_parse_start); + baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_flac_parse_stop); + baseparse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_flac_parse_check_valid_frame); + baseparse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_flac_parse_parse_frame); + baseparse_class->pre_push_frame = + GST_DEBUG_FUNCPTR (gst_flac_parse_pre_push_frame); +} + +static void +gst_flac_parse_init (GstFlacParse * flacparse, GstFlacParseClass * klass) +{ + flacparse->check_frame_checksums = DEFAULT_CHECK_FRAME_CHECKSUMS; +} + +static void +gst_flac_parse_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (object); + + switch (prop_id) { + case PROP_CHECK_FRAME_CHECKSUMS: + flacparse->check_frame_checksums = g_value_get_boolean (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_flac_parse_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (object); + + switch (prop_id) { + case PROP_CHECK_FRAME_CHECKSUMS: + g_value_set_boolean (value, flacparse->check_frame_checksums); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_flac_parse_finalize (GObject * object) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (object); + + if (flacparse->tags) { + gst_tag_list_free (flacparse->tags); + flacparse->tags = NULL; + } + + g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (flacparse->headers); + flacparse->headers = NULL; + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_flac_parse_start (GstBaseParse * parse) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (parse); + + flacparse->state = GST_FLAC_PARSE_STATE_INIT; + flacparse->min_blocksize = 0; + flacparse->max_blocksize = 0; + flacparse->min_framesize = 0; + flacparse->max_framesize = 0; + + flacparse->upstream_length = -1; + + flacparse->samplerate = 0; + flacparse->channels = 0; + flacparse->bps = 0; + flacparse->total_samples = 0; + + flacparse->offset = GST_CLOCK_TIME_NONE; + flacparse->blocking_strategy = 0; + flacparse->block_size = 0; + flacparse->sample_number = 0; + + /* "fLaC" marker */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4); + + /* inform baseclass we can come up with ts, based on counters in packets */ + gst_base_parse_set_has_timing_info (GST_BASE_PARSE_CAST (flacparse), TRUE); + gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (flacparse), TRUE); + + return TRUE; +} + +static gboolean +gst_flac_parse_stop (GstBaseParse * parse) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (parse); + + if (flacparse->tags) { + gst_tag_list_free (flacparse->tags); + flacparse->tags = NULL; + } + + g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (flacparse->headers); + flacparse->headers = NULL; + + return TRUE; +} + +static const guint8 sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 }; + +static const guint16 blocksize_table[16] = { + 0, 192, 576 << 0, 576 << 1, 576 << 2, 576 << 3, 0, 0, + 256 << 0, 256 << 1, 256 << 2, 256 << 3, 256 << 4, 256 << 5, 256 << 6, + 256 << 7, +}; + +static const guint32 sample_rate_table[16] = { + 0, + 88200, 176400, 192000, + 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, + 0, 0, 0, 0, +}; + +typedef enum +{ + FRAME_HEADER_VALID, + FRAME_HEADER_INVALID, + FRAME_HEADER_MORE_DATA +} FrameHeaderCheckReturn; + +static FrameHeaderCheckReturn +gst_flac_parse_frame_header_is_valid (GstFlacParse * flacparse, + const guint8 * data, guint size, gboolean set, guint16 * block_size_ret) +{ + GstBitReader reader = GST_BIT_READER_INIT (data, size); + guint8 blocking_strategy; + guint16 block_size; + guint32 samplerate = 0; + guint64 sample_number; + guint8 channels, bps; + guint8 tmp = 0; + guint8 actual_crc, expected_crc = 0; + + /* Skip 14 bit sync code */ + gst_bit_reader_skip_unchecked (&reader, 14); + + /* Must be 0 */ + if (gst_bit_reader_get_bits_uint8_unchecked (&reader, 1) != 0) + goto error; + + /* 0 == fixed block size, 1 == variable block size */ + blocking_strategy = gst_bit_reader_get_bits_uint8_unchecked (&reader, 1); + + /* block size index, calculation of the real blocksize below */ + block_size = gst_bit_reader_get_bits_uint16_unchecked (&reader, 4); + if (block_size == 0) + goto error; + + /* sample rate index, calculation of the real samplerate below */ + samplerate = gst_bit_reader_get_bits_uint16_unchecked (&reader, 4); + if (samplerate == 0x0f) + goto error; + + /* channel assignment */ + channels = gst_bit_reader_get_bits_uint8_unchecked (&reader, 4); + if (channels < 8) { + channels++; + } else if (channels <= 10) { + channels = 2; + } else if (channels > 10) { + goto error; + } + if (flacparse->channels && flacparse->channels != channels) + goto error; + + /* bits per sample */ + bps = gst_bit_reader_get_bits_uint8_unchecked (&reader, 3); + if (bps == 0x03 || bps == 0x07) { + goto error; + } else if (bps == 0 && flacparse->bps == 0) { + goto need_streaminfo; + } + bps = sample_size_table[bps]; + if (flacparse->bps && bps != flacparse->bps) + goto error; + + /* reserved, must be 0 */ + if (gst_bit_reader_get_bits_uint8_unchecked (&reader, 1) != 0) + goto error; + + /* read "utf8" encoded sample/frame number */ + { + gint len = 0; + + len = gst_bit_reader_get_bits_uint8_unchecked (&reader, 8); + + /* This is slightly faster than a loop */ + if (!(len & 0x80)) { + sample_number = len; + len = 0; + } else if ((len & 0xc0) && !(len & 0x20)) { + sample_number = len & 0x1f; + len = 1; + } else if ((len & 0xe0) && !(len & 0x10)) { + sample_number = len & 0x0f; + len = 2; + } else if ((len & 0xf0) && !(len & 0x08)) { + sample_number = len & 0x07; + len = 3; + } else if ((len & 0xf8) && !(len & 0x04)) { + sample_number = len & 0x03; + len = 4; + } else if ((len & 0xfc) && !(len & 0x02)) { + sample_number = len & 0x01; + len = 5; + } else if ((len & 0xfe) && !(len & 0x01)) { + sample_number = len & 0x0; + len = 6; + } else { + goto error; + } + + if ((blocking_strategy == 0 && len > 5) || + (blocking_strategy == 1 && len > 6)) + goto error; + + while (len > 0) { + if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp, 8)) + goto need_more_data; + + if ((tmp & 0xc0) != 0x80) + goto error; + + sample_number <<= 6; + sample_number |= (tmp & 0x3f); + len--; + } + } + + /* calculate real blocksize from the blocksize index */ + if (block_size == 0) { + goto error; + } else if (block_size == 6) { + if (!gst_bit_reader_get_bits_uint16 (&reader, &block_size, 8)) + goto need_more_data; + block_size++; + } else if (block_size == 7) { + if (!gst_bit_reader_get_bits_uint16 (&reader, &block_size, 16)) + goto need_more_data; + block_size++; + } else { + block_size = blocksize_table[block_size]; + } + + /* calculate the real samplerate from the samplerate index */ + if (samplerate == 0 && flacparse->samplerate == 0) { + goto need_streaminfo; + } else if (samplerate < 12) { + samplerate = sample_rate_table[samplerate]; + } else if (samplerate == 12) { + if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 8)) + goto need_more_data; + samplerate *= 1000; + } else if (samplerate == 13) { + if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 16)) + goto need_more_data; + } else if (samplerate == 14) { + if (!gst_bit_reader_get_bits_uint32 (&reader, &samplerate, 16)) + goto need_more_data; + samplerate *= 10; + } + + if (flacparse->samplerate && flacparse->samplerate != samplerate) + goto error; + + /* check crc-8 for the header */ + if (!gst_bit_reader_get_bits_uint8 (&reader, &expected_crc, 8)) + goto need_more_data; + + actual_crc = + gst_flac_calculate_crc8 (data, + (gst_bit_reader_get_pos (&reader) / 8) - 1); + if (actual_crc != expected_crc) + goto error; + + if (set) { + flacparse->block_size = block_size; + if (!flacparse->samplerate) + flacparse->samplerate = samplerate; + if (!flacparse->bps) + flacparse->bps = bps; + if (!flacparse->blocking_strategy) + flacparse->blocking_strategy = blocking_strategy; + if (!flacparse->channels) + flacparse->channels = channels; + if (!flacparse->sample_number) + flacparse->sample_number = sample_number; + + GST_DEBUG_OBJECT (flacparse, + "Parsed frame at offset %" G_GUINT64_FORMAT ":\n" "Block size: %u\n" + "Sample/Frame number: %" G_GUINT64_FORMAT, flacparse->offset, + flacparse->block_size, flacparse->sample_number); + } + + if (block_size_ret) + *block_size_ret = block_size; + + return FRAME_HEADER_VALID; + +need_streaminfo: + GST_ERROR_OBJECT (flacparse, "Need STREAMINFO"); + return FRAME_HEADER_INVALID; +error: + return FRAME_HEADER_INVALID; + +need_more_data: + return FRAME_HEADER_MORE_DATA; +} + +static gboolean +gst_flac_parse_frame_is_valid (GstFlacParse * flacparse, + GstBaseParseFrame * frame, guint * ret) +{ + GstBuffer *buffer; + const guint8 *data; + guint max, size, remaining; + guint i, search_start, search_end; + FrameHeaderCheckReturn header_ret; + guint16 block_size; + + buffer = frame->buffer; + data = GST_BUFFER_DATA (buffer); + size = GST_BUFFER_SIZE (buffer); + + if (size <= flacparse->min_framesize) + goto need_more; + + header_ret = + gst_flac_parse_frame_header_is_valid (flacparse, data, size, TRUE, + &block_size); + if (header_ret == FRAME_HEADER_INVALID) { + *ret = 0; + return FALSE; + } else if (header_ret == FRAME_HEADER_MORE_DATA) { + goto need_more; + } + + /* mind unknown framesize */ + search_start = MAX (2, flacparse->min_framesize); + if (flacparse->max_framesize) + search_end = MIN (size, flacparse->max_framesize + 9 + 2); + else + search_end = size; + search_end -= 2; + + remaining = size; + + for (i = search_start; i < search_end; i++, remaining--) { + if ((GST_READ_UINT16_BE (data + i) & 0xfffe) == 0xfff8) { + header_ret = + gst_flac_parse_frame_header_is_valid (flacparse, data + i, remaining, + FALSE, NULL); + if (header_ret == FRAME_HEADER_VALID) { + if (flacparse->check_frame_checksums) { + guint16 actual_crc = gst_flac_calculate_crc16 (data, i - 2); + guint16 expected_crc = GST_READ_UINT16_BE (data + i - 2); + + if (actual_crc != expected_crc) + continue; + } + *ret = i; + flacparse->block_size = block_size; + return TRUE; + } else if (header_ret == FRAME_HEADER_MORE_DATA) { + goto need_more; + } + } + } + + /* For the last frame output everything to the end */ + if (G_UNLIKELY (GST_BASE_PARSE_DRAINING (flacparse))) { + if (flacparse->check_frame_checksums) { + guint16 actual_crc = gst_flac_calculate_crc16 (data, size - 2); + guint16 expected_crc = GST_READ_UINT16_BE (data + size - 2); + + if (actual_crc == expected_crc) { + *ret = size; + flacparse->block_size = block_size; + return TRUE; + } + } else { + *ret = size; + flacparse->block_size = block_size; + return TRUE; + } + } + +need_more: + max = flacparse->max_framesize + 16; + if (max == 16) + max = 1 << 24; + *ret = MIN (size + 4096, max); + return FALSE; +} + +static gboolean +gst_flac_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (parse); + GstBuffer *buffer = frame->buffer; + const guint8 *data = GST_BUFFER_DATA (buffer); + + if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 4)) + return FALSE; + + if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) { + if (memcmp (GST_BUFFER_DATA (buffer), "fLaC", 4) == 0) { + GST_DEBUG_OBJECT (flacparse, "fLaC marker found"); + *framesize = 4; + return TRUE; + } else if (data[0] == 0xff && (data[1] >> 2) == 0x3e) { + GST_DEBUG_OBJECT (flacparse, "Found headerless FLAC"); + /* Minimal size of a frame header */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 9); + flacparse->state = GST_FLAC_PARSE_STATE_GENERATE_HEADERS; + *skipsize = 0; + return FALSE; + } else { + GST_DEBUG_OBJECT (flacparse, "fLaC marker not found"); + return FALSE; + } + } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) { + guint size = 4 + ((data[1] << 16) | (data[2] << 8) | (data[3])); + + GST_DEBUG_OBJECT (flacparse, "Found metadata block of size %u", size); + *framesize = size; + return TRUE; + } else { + if ((GST_READ_UINT16_BE (data) & 0xfffe) == 0xfff8) { + gboolean ret; + guint next; + + flacparse->offset = GST_BUFFER_OFFSET (buffer); + flacparse->blocking_strategy = 0; + flacparse->block_size = 0; + flacparse->sample_number = 0; + + GST_DEBUG_OBJECT (flacparse, "Found sync code"); + ret = gst_flac_parse_frame_is_valid (flacparse, frame, &next); + if (ret) { + *framesize = next; + return TRUE; + } else { + /* If we're at EOS and the frame was not valid, drop it! */ + if (G_UNLIKELY (GST_BASE_PARSE_DRAINING (flacparse))) { + GST_WARNING_OBJECT (flacparse, "EOS"); + return FALSE; + } + + if (next == 0) { + } else if (next > GST_BUFFER_SIZE (buffer)) { + GST_DEBUG_OBJECT (flacparse, "Requesting %u bytes", next); + *skipsize = 0; + gst_base_parse_set_min_frame_size (parse, next); + return FALSE; + } else { + GST_ERROR_OBJECT (flacparse, + "Giving up on invalid frame (%d bytes)", + GST_BUFFER_SIZE (buffer)); + return FALSE; + } + } + } else { + GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer); + gint off; + + off = + gst_byte_reader_masked_scan_uint32 (&reader, 0xfffc0000, 0xfff80000, + 0, GST_BUFFER_SIZE (buffer)); + + if (off > 0) { + GST_DEBUG_OBJECT (parse, "Possible sync at buffer offset %d", off); + *skipsize = off; + return FALSE; + } else { + GST_DEBUG_OBJECT (flacparse, "Sync code not found"); + *skipsize = GST_BUFFER_SIZE (buffer) - 3; + return FALSE; + } + } + } + + return FALSE; +} + +static gboolean +gst_flac_parse_handle_streaminfo (GstFlacParse * flacparse, GstBuffer * buffer) +{ + GstBitReader reader = GST_BIT_READER_INIT_FROM_BUFFER (buffer); + + if (GST_BUFFER_SIZE (buffer) != 4 + 34) { + GST_ERROR_OBJECT (flacparse, "Invalid metablock size for STREAMINFO: %u", + GST_BUFFER_SIZE (buffer)); + return FALSE; + } + + /* Skip metadata block header */ + gst_bit_reader_skip (&reader, 32); + + if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->min_blocksize, 16)) + goto error; + if (flacparse->min_blocksize < 16) { + GST_ERROR_OBJECT (flacparse, "Invalid minimum block size: %u", + flacparse->min_blocksize); + return FALSE; + } + + if (!gst_bit_reader_get_bits_uint16 (&reader, &flacparse->max_blocksize, 16)) + goto error; + if (flacparse->max_blocksize < 16) { + GST_ERROR_OBJECT (flacparse, "Invalid maximum block size: %u", + flacparse->max_blocksize); + return FALSE; + } + + if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->min_framesize, 24)) + goto error; + if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->max_framesize, 24)) + goto error; + + if (!gst_bit_reader_get_bits_uint32 (&reader, &flacparse->samplerate, 20)) + goto error; + if (flacparse->samplerate == 0) { + GST_ERROR_OBJECT (flacparse, "Invalid sample rate 0"); + return FALSE; + } + + if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->channels, 3)) + goto error; + flacparse->channels++; + if (flacparse->channels > 8) { + GST_ERROR_OBJECT (flacparse, "Invalid number of channels %u", + flacparse->channels); + return FALSE; + } + + if (!gst_bit_reader_get_bits_uint8 (&reader, &flacparse->bps, 5)) + goto error; + flacparse->bps++; + + if (!gst_bit_reader_get_bits_uint64 (&reader, &flacparse->total_samples, 36)) + goto error; + if (flacparse->total_samples) + gst_base_parse_set_duration (GST_BASE_PARSE (flacparse), GST_FORMAT_TIME, + GST_FRAMES_TO_CLOCK_TIME (flacparse->total_samples, + flacparse->samplerate), 0); + + GST_DEBUG_OBJECT (flacparse, "STREAMINFO:\n" + "\tmin/max blocksize: %u/%u,\n" + "\tmin/max framesize: %u/%u,\n" + "\tsamplerate: %u,\n" + "\tchannels: %u,\n" + "\tbits per sample: %u,\n" + "\ttotal samples: %" G_GUINT64_FORMAT, + flacparse->min_blocksize, flacparse->max_blocksize, + flacparse->min_framesize, flacparse->max_framesize, + flacparse->samplerate, + flacparse->channels, flacparse->bps, flacparse->total_samples); + + return TRUE; + +error: + GST_ERROR_OBJECT (flacparse, "Failed to read data"); + return FALSE; +} + +static gboolean +gst_flac_parse_handle_vorbiscomment (GstFlacParse * flacparse, + GstBuffer * buffer) +{ + flacparse->tags = gst_tag_list_from_vorbiscomment_buffer (buffer, + GST_BUFFER_DATA (buffer), 4, NULL); + + if (flacparse->tags == NULL) { + GST_ERROR_OBJECT (flacparse, "Invalid vorbiscomment block"); + } else if (gst_tag_list_is_empty (flacparse->tags)) { + gst_tag_list_free (flacparse->tags); + flacparse->tags = NULL; + } + + return TRUE; +} + +static gboolean +gst_flac_parse_handle_picture (GstFlacParse * flacparse, GstBuffer * buffer) +{ + GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buffer); + const guint8 *data = GST_BUFFER_DATA (buffer); + guint32 img_len = 0, img_type = 0; + guint32 img_mimetype_len = 0, img_description_len = 0; + + if (!gst_byte_reader_skip (&reader, 4)) + goto error; + + if (!gst_byte_reader_get_uint32_be (&reader, &img_type)) + goto error; + + if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len)) + goto error; + if (!gst_byte_reader_skip (&reader, img_mimetype_len)) + goto error; + + if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len)) + goto error; + if (!gst_byte_reader_skip (&reader, img_description_len)) + goto error; + + if (!gst_byte_reader_skip (&reader, 4 * 4)) + goto error; + + if (!gst_byte_reader_get_uint32_be (&reader, &img_len)) + goto error; + + if (!flacparse->tags) + flacparse->tags = gst_tag_list_new (); + + gst_tag_list_add_id3_image (flacparse->tags, + data + gst_byte_reader_get_pos (&reader), img_len, img_type); + + if (gst_tag_list_is_empty (flacparse->tags)) { + gst_tag_list_free (flacparse->tags); + flacparse->tags = NULL; + } + + return TRUE; + +error: + GST_ERROR_OBJECT (flacparse, "Error reading data"); + return FALSE; +} + +static gboolean +gst_flac_parse_handle_seektable (GstFlacParse * flacparse, GstBuffer * buffer) +{ + + GST_DEBUG_OBJECT (flacparse, "storing seektable"); + /* only store for now; + * offset of the first frame is needed to get real info */ + flacparse->seektable = gst_buffer_ref (buffer); + + return TRUE; +} + +static void +gst_flac_parse_process_seektable (GstFlacParse * flacparse, gint64 boffset) +{ + GstByteReader br; + gint64 offset = 0, samples = 0; + + GST_DEBUG_OBJECT (flacparse, + "parsing seektable; base offset %" G_GINT64_FORMAT, boffset); + + if (boffset <= 0) + goto done; + + gst_byte_reader_init_from_buffer (&br, flacparse->seektable); + /* skip header */ + if (!gst_byte_reader_skip (&br, 4)) + goto done; + + /* seekpoints */ + while (gst_byte_reader_get_remaining (&br)) { + if (!gst_byte_reader_get_int64_be (&br, &samples)) + break; + if (!gst_byte_reader_get_int64_be (&br, &offset)) + break; + if (!gst_byte_reader_skip (&br, 2)) + break; + + GST_LOG_OBJECT (flacparse, "samples %" G_GINT64_FORMAT " -> offset %" + G_GINT64_FORMAT, samples, offset); + + /* sanity check */ + if (G_LIKELY (offset > 0 && samples > 0)) { + gst_base_parse_add_index_entry (GST_BASE_PARSE (flacparse), + boffset + offset, gst_util_uint64_scale (samples, GST_SECOND, + flacparse->samplerate), TRUE, FALSE); + } + } + +done: + gst_buffer_unref (flacparse->seektable); + flacparse->seektable = NULL; +} + +static void +_value_array_append_buffer (GValue * array_val, GstBuffer * buf) +{ + GValue value = { 0, }; + + g_value_init (&value, GST_TYPE_BUFFER); + /* copy buffer to avoid problems with circular refcounts */ + buf = gst_buffer_copy (buf); + /* again, for good measure */ + GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS); + gst_value_set_buffer (&value, buf); + gst_buffer_unref (buf); + gst_value_array_append_value (array_val, &value); + g_value_unset (&value); +} + +static gboolean +gst_flac_parse_handle_headers (GstFlacParse * flacparse) +{ + GstBuffer *vorbiscomment = NULL; + GstBuffer *streaminfo = NULL; + GstBuffer *marker = NULL; + GValue array = { 0, }; + GstCaps *caps; + GList *l; + gboolean res = TRUE; + + caps = gst_caps_new_simple ("audio/x-flac", + "channels", G_TYPE_INT, flacparse->channels, + "framed", G_TYPE_BOOLEAN, TRUE, + "rate", G_TYPE_INT, flacparse->samplerate, NULL); + + if (!flacparse->headers) + goto push_headers; + + for (l = flacparse->headers; l; l = l->next) { + GstBuffer *header = l->data; + const guint8 *data = GST_BUFFER_DATA (header); + guint size = GST_BUFFER_SIZE (header); + + GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_IN_CAPS); + + if (size == 4 && memcmp (data, "fLaC", 4) == 0) { + marker = header; + } else if (size > 1 && (data[0] & 0x7f) == 0) { + streaminfo = header; + } else if (size > 1 && (data[0] & 0x7f) == 4) { + vorbiscomment = header; + } + } + + if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) { + GST_WARNING_OBJECT (flacparse, + "missing header %p %p %p, muxing into container " + "formats may be broken", marker, streaminfo, vorbiscomment); + goto push_headers; + } + + g_value_init (&array, GST_TYPE_ARRAY); + + /* add marker including STREAMINFO header */ + { + GstBuffer *buf; + guint16 num; + + /* minus one for the marker that is merged with streaminfo here */ + num = g_list_length (flacparse->headers) - 1; + + buf = gst_buffer_new_and_alloc (13 + GST_BUFFER_SIZE (streaminfo)); + GST_BUFFER_DATA (buf)[0] = 0x7f; + memcpy (GST_BUFFER_DATA (buf) + 1, "FLAC", 4); + GST_BUFFER_DATA (buf)[5] = 0x01; /* mapping version major */ + GST_BUFFER_DATA (buf)[6] = 0x00; /* mapping version minor */ + GST_BUFFER_DATA (buf)[7] = (num & 0xFF00) >> 8; + GST_BUFFER_DATA (buf)[8] = (num & 0x00FF) >> 0; + memcpy (GST_BUFFER_DATA (buf) + 9, "fLaC", 4); + memcpy (GST_BUFFER_DATA (buf) + 13, GST_BUFFER_DATA (streaminfo), + GST_BUFFER_SIZE (streaminfo)); + _value_array_append_buffer (&array, buf); + gst_buffer_unref (buf); + } + + /* add VORBISCOMMENT header */ + _value_array_append_buffer (&array, vorbiscomment); + + /* add other headers, if there are any */ + for (l = flacparse->headers; l; l = l->next) { + if (GST_BUFFER_CAST (l->data) != marker && + GST_BUFFER_CAST (l->data) != streaminfo && + GST_BUFFER_CAST (l->data) != vorbiscomment) { + _value_array_append_buffer (&array, GST_BUFFER_CAST (l->data)); + } + } + + gst_structure_set_value (gst_caps_get_structure (caps, 0), + "streamheader", &array); + g_value_unset (&array); + +push_headers: + + gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)), caps); + gst_caps_unref (caps); + + /* push header buffers; update caps, so when we push the first buffer the + * negotiated caps will change to caps that include the streamheader field */ + while (flacparse->headers) { + GstBuffer *buf = GST_BUFFER (flacparse->headers->data); + GstFlowReturn ret; + GstBaseParseFrame frame; + + flacparse->headers = + g_list_delete_link (flacparse->headers, flacparse->headers); + buf = gst_buffer_make_metadata_writable (buf); + gst_buffer_set_caps (buf, + GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (GST_BASE_PARSE (flacparse)))); + + /* init, set and give away frame */ + gst_base_parse_frame_init (&frame); + frame.buffer = buf; + frame.overhead = -1; + ret = gst_base_parse_push_frame (GST_BASE_PARSE (flacparse), &frame); + if (ret != GST_FLOW_OK) { + res = FALSE; + break; + } + } + g_list_foreach (flacparse->headers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (flacparse->headers); + flacparse->headers = NULL; + + return res; +} + +static gboolean +gst_flac_parse_generate_headers (GstFlacParse * flacparse) +{ + GstBuffer *marker, *streaminfo, *vorbiscomment; + guint8 *data; + + marker = gst_buffer_new_and_alloc (4); + memcpy (GST_BUFFER_DATA (marker), "fLaC", 4); + GST_BUFFER_TIMESTAMP (marker) = GST_CLOCK_TIME_NONE; + GST_BUFFER_DURATION (marker) = GST_CLOCK_TIME_NONE; + GST_BUFFER_OFFSET (marker) = 0; + GST_BUFFER_OFFSET_END (marker) = 0; + flacparse->headers = g_list_append (flacparse->headers, marker); + + streaminfo = gst_buffer_new_and_alloc (4 + 34); + data = GST_BUFFER_DATA (streaminfo); + memset (data, 0, 4 + 34); + + /* metadata block header */ + data[0] = 0x00; /* is_last = 0; type = 0; */ + data[1] = 0x00; /* length = 34; */ + data[2] = 0x00; + data[3] = 0x22; + + /* streaminfo */ + + data[4] = (flacparse->block_size >> 8) & 0xff; /* min blocksize = blocksize; */ + data[5] = (flacparse->block_size) & 0xff; + data[6] = (flacparse->block_size >> 8) & 0xff; /* max blocksize = blocksize; */ + data[7] = (flacparse->block_size) & 0xff; + + data[8] = 0x00; /* min framesize = 0; */ + data[9] = 0x00; + data[10] = 0x00; + data[11] = 0x00; /* max framesize = 0; */ + data[12] = 0x00; + data[13] = 0x00; + + data[14] = (flacparse->samplerate >> 12) & 0xff; + data[15] = (flacparse->samplerate >> 4) & 0xff; + data[16] = (flacparse->samplerate >> 0) & 0xf0; + + data[16] |= (flacparse->channels - 1) << 1; + + data[16] |= ((flacparse->bps - 1) >> 4) & 0x01; + data[17] = (((flacparse->bps - 1)) & 0x0f) << 4; + + { + gint64 duration; + GstFormat fmt = GST_FORMAT_TIME; + + if (gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE + (flacparse)), &fmt, &duration) && fmt == GST_FORMAT_TIME) { + duration = GST_CLOCK_TIME_TO_FRAMES (duration, flacparse->samplerate); + + data[17] |= (duration >> 32) & 0xff; + data[18] |= (duration >> 24) & 0xff; + data[19] |= (duration >> 16) & 0xff; + data[20] |= (duration >> 8) & 0xff; + data[21] |= (duration >> 0) & 0xff; + } + } + /* MD5 = 0; */ + + GST_BUFFER_TIMESTAMP (streaminfo) = GST_CLOCK_TIME_NONE; + GST_BUFFER_DURATION (streaminfo) = GST_CLOCK_TIME_NONE; + GST_BUFFER_OFFSET (streaminfo) = 0; + GST_BUFFER_OFFSET_END (streaminfo) = 0; + flacparse->headers = g_list_append (flacparse->headers, streaminfo); + + /* empty vorbiscomment */ + { + GstTagList *taglist = gst_tag_list_new (); + guchar header[4]; + guint size; + + header[0] = 0x84; /* is_last = 1; type = 4; */ + + vorbiscomment = + gst_tag_list_to_vorbiscomment_buffer (taglist, header, + sizeof (header), NULL); + gst_tag_list_free (taglist); + + /* Get rid of framing bit */ + if (GST_BUFFER_DATA (vorbiscomment)[GST_BUFFER_SIZE (vorbiscomment) - + 1] == 1) { + GstBuffer *sub; + + sub = + gst_buffer_create_sub (vorbiscomment, 0, + GST_BUFFER_SIZE (vorbiscomment) - 1); + gst_buffer_unref (vorbiscomment); + vorbiscomment = sub; + } + + size = GST_BUFFER_SIZE (vorbiscomment) - 4; + GST_BUFFER_DATA (vorbiscomment)[1] = ((size & 0xFF0000) >> 16); + GST_BUFFER_DATA (vorbiscomment)[2] = ((size & 0x00FF00) >> 8); + GST_BUFFER_DATA (vorbiscomment)[3] = (size & 0x0000FF); + + GST_BUFFER_TIMESTAMP (vorbiscomment) = GST_CLOCK_TIME_NONE; + GST_BUFFER_DURATION (vorbiscomment) = GST_CLOCK_TIME_NONE; + GST_BUFFER_OFFSET (vorbiscomment) = 0; + GST_BUFFER_OFFSET_END (vorbiscomment) = 0; + flacparse->headers = g_list_append (flacparse->headers, vorbiscomment); + } + + return TRUE; +} + +static GstFlowReturn +gst_flac_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (parse); + GstBuffer *buffer = frame->buffer; + const guint8 *data = GST_BUFFER_DATA (buffer); + + if (flacparse->state == GST_FLAC_PARSE_STATE_INIT) { + GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE; + GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE; + GST_BUFFER_OFFSET (buffer) = 0; + GST_BUFFER_OFFSET_END (buffer) = 0; + + /* 32 bits metadata block */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), 4); + flacparse->state = GST_FLAC_PARSE_STATE_HEADERS; + + flacparse->headers = + g_list_append (flacparse->headers, gst_buffer_ref (buffer)); + + return GST_BASE_PARSE_FLOW_DROPPED; + } else if (flacparse->state == GST_FLAC_PARSE_STATE_HEADERS) { + gboolean is_last = ((data[0] & 0x80) == 0x80); + guint type = (data[0] & 0x7F); + + if (type == 127) { + GST_WARNING_OBJECT (flacparse, "Invalid metadata block type"); + return GST_BASE_PARSE_FLOW_DROPPED; + } + + GST_DEBUG_OBJECT (flacparse, "Handling metadata block of type %u", type); + + switch (type) { + case 0: /* STREAMINFO */ + if (!gst_flac_parse_handle_streaminfo (flacparse, buffer)) + return GST_FLOW_ERROR; + break; + case 3: /* SEEKTABLE */ + if (!gst_flac_parse_handle_seektable (flacparse, buffer)) + return GST_FLOW_ERROR; + break; + case 4: /* VORBIS_COMMENT */ + if (!gst_flac_parse_handle_vorbiscomment (flacparse, buffer)) + return GST_FLOW_ERROR; + break; + case 6: /* PICTURE */ + if (!gst_flac_parse_handle_picture (flacparse, buffer)) + return GST_FLOW_ERROR; + break; + case 1: /* PADDING */ + case 2: /* APPLICATION */ + case 5: /* CUESHEET */ + default: /* RESERVED */ + break; + } + + GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE; + GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE; + GST_BUFFER_OFFSET (buffer) = 0; + GST_BUFFER_OFFSET_END (buffer) = 0; + + flacparse->headers = + g_list_append (flacparse->headers, gst_buffer_ref (buffer)); + + if (is_last) { + if (!gst_flac_parse_handle_headers (flacparse)) + return GST_FLOW_ERROR; + + /* Minimal size of a frame header */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (9, + flacparse->min_framesize)); + flacparse->state = GST_FLAC_PARSE_STATE_DATA; + } + + /* DROPPED because we pushed already or will push all headers manually */ + return GST_BASE_PARSE_FLOW_DROPPED; + } else { + if (flacparse->offset != GST_BUFFER_OFFSET (buffer)) { + FrameHeaderCheckReturn ret; + + flacparse->offset = GST_BUFFER_OFFSET (buffer); + ret = + gst_flac_parse_frame_header_is_valid (flacparse, + GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), TRUE, NULL); + if (ret != FRAME_HEADER_VALID) { + GST_ERROR_OBJECT (flacparse, + "Baseclass didn't provide a complete frame"); + return GST_FLOW_ERROR; + } + } + + if (flacparse->block_size == 0) { + GST_ERROR_OBJECT (flacparse, "Unparsed frame"); + return GST_FLOW_ERROR; + } + + if (flacparse->seektable) + gst_flac_parse_process_seektable (flacparse, GST_BUFFER_OFFSET (buffer)); + + if (flacparse->state == GST_FLAC_PARSE_STATE_GENERATE_HEADERS) { + if (flacparse->blocking_strategy == 1) { + GST_WARNING_OBJECT (flacparse, + "Generating headers for variable blocksize streams not supported"); + + if (!gst_flac_parse_handle_headers (flacparse)) + return GST_FLOW_ERROR; + } else { + GST_DEBUG_OBJECT (flacparse, "Generating headers"); + + if (!gst_flac_parse_generate_headers (flacparse)) + return GST_FLOW_ERROR; + + if (!gst_flac_parse_handle_headers (flacparse)) + return GST_FLOW_ERROR; + } + flacparse->state = GST_FLAC_PARSE_STATE_DATA; + } + + /* also cater for oggmux metadata */ + if (flacparse->blocking_strategy == 0) { + GST_BUFFER_TIMESTAMP (buffer) = + gst_util_uint64_scale (flacparse->sample_number, + flacparse->block_size * GST_SECOND, flacparse->samplerate); + GST_BUFFER_OFFSET_END (buffer) = + flacparse->sample_number * flacparse->block_size + + flacparse->block_size; + } else { + GST_BUFFER_TIMESTAMP (buffer) = + gst_util_uint64_scale (flacparse->sample_number, GST_SECOND, + flacparse->samplerate); + GST_BUFFER_OFFSET_END (buffer) = + flacparse->sample_number + flacparse->block_size; + } + GST_BUFFER_OFFSET (buffer) = + gst_util_uint64_scale (GST_BUFFER_OFFSET_END (buffer), GST_SECOND, + flacparse->samplerate); + GST_BUFFER_DURATION (buffer) = + GST_BUFFER_OFFSET (buffer) - GST_BUFFER_TIMESTAMP (buffer); + + /* To simplify, we just assume that it's a fixed size header and ignore + * subframe headers. The first could lead us to being off by 88 bits and + * the second even less, so the total inaccuracy is negligible. */ + frame->overhead = 7; + + /* Minimal size of a frame header */ + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (flacparse), MAX (9, + flacparse->min_framesize)); + + flacparse->offset = -1; + flacparse->blocking_strategy = 0; + flacparse->block_size = 0; + flacparse->sample_number = 0; + return GST_FLOW_OK; + } +} + +static GstFlowReturn +gst_flac_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) +{ + GstFlacParse *flacparse = GST_FLAC_PARSE (parse); + + /* Push tags */ + if (flacparse->tags) { + gst_element_found_tags (GST_ELEMENT (flacparse), flacparse->tags); + flacparse->tags = NULL; + } + + frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; + + return GST_FLOW_OK; +} diff --git a/gst/audioparsers/gstflacparse.h b/gst/audioparsers/gstflacparse.h new file mode 100644 index 000000000..1c6db0e58 --- /dev/null +++ b/gst/audioparsers/gstflacparse.h @@ -0,0 +1,92 @@ +/* GStreamer + * + * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>. + * Copyright (C) 2009 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk> + * Copyright (C) 2009 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +#ifndef __GST_FLAC_PARSE_H__ +#define __GST_FLAC_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_FLAC_PARSE (gst_flac_parse_get_type()) +#define GST_FLAC_PARSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FLAC_PARSE,GstFlacParse)) +#define GST_FLAC_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FLAC_PARSE,GstFlacParseClass)) +#define GST_FLAC_PARSE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_FLAC_PARSE,GstFlacParseClass)) +#define GST_IS_FLAC_PARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FLAC_PARSE)) +#define GST_IS_FLAC_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FLAC_PARSE)) +#define GST_FLAC_PARSE_CAST(obj) ((GstFlacParse *)(obj)) + +typedef struct _GstFlacParse GstFlacParse; +typedef struct _GstFlacParseClass GstFlacParseClass; + +typedef enum { + GST_FLAC_PARSE_STATE_INIT, + GST_FLAC_PARSE_STATE_HEADERS, + GST_FLAC_PARSE_STATE_GENERATE_HEADERS, + GST_FLAC_PARSE_STATE_DATA +} GstFlacParseState; + +typedef struct { + guint8 type; +} GstFlacParseSubFrame; + +struct _GstFlacParse { + GstBaseParse parent; + + /* Properties */ + gboolean check_frame_checksums; + + GstFlacParseState state; + + gint64 upstream_length; + + /* STREAMINFO content */ + guint16 min_blocksize, max_blocksize; + guint32 min_framesize, max_framesize; + guint32 samplerate; + guint8 channels; + guint8 bps; + guint64 total_samples; + + /* Current frame */ + guint64 offset; + guint8 blocking_strategy; + guint16 block_size; + guint64 sample_number; + + GstTagList *tags; + + GList *headers; + GstBuffer *seektable; +}; + +struct _GstFlacParseClass { + GstBaseParseClass parent_class; +}; + +GType gst_flac_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_FLAC_PARSE_H__ */ diff --git a/gst/audioparsers/gstmpegaudioparse.c b/gst/audioparsers/gstmpegaudioparse.c new file mode 100644 index 000000000..0c55704a9 --- /dev/null +++ b/gst/audioparsers/gstmpegaudioparse.c @@ -0,0 +1,1265 @@ +/* GStreamer MPEG audio parser + * Copyright (C) 2006-2007 Jan Schmidt <thaytan@mad.scientist.com> + * Copyright (C) 2010 Mark Nauwelaerts <mnauw users sf net> + * Copyright (C) 2010 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ +/** + * SECTION:element-mpegaudioparse + * @short_description: MPEG audio parser + * @see_also: #GstAmrParse, #GstAACParse + * + * Parses and frames mpeg1 audio streams. Provides seeking. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch filesrc location=test.mp3 ! mpegaudioparse ! mad ! autoaudiosink + * ]| + * </refsect2> + */ + +/* FIXME: we should make the base class (GstBaseParse) aware of the + * XING seek table somehow, so it can use it properly for things like + * accurate seeks. Currently it can only do a lookup via the convert function, + * but then doesn't know what the result represents exactly. One could either + * add a vfunc for index lookup, or just make mpegaudioparse populate the + * base class's index via the API provided. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> + +#include "gstmpegaudioparse.h" +#include <gst/base/gstbytereader.h> + +GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug); +#define GST_CAT_DEFAULT mpeg_audio_parse_debug + +#define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1 +#define MPEG_AUDIO_CHANNEL_MODE_STEREO 0 +#define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1 +#define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2 +#define MPEG_AUDIO_CHANNEL_MODE_MONO 3 + +#define CRC_UNKNOWN -1 +#define CRC_PROTECTED 0 +#define CRC_NOT_PROTECTED 1 + +#define XING_FRAMES_FLAG 0x0001 +#define XING_BYTES_FLAG 0x0002 +#define XING_TOC_FLAG 0x0004 +#define XING_VBR_SCALE_FLAG 0x0008 + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, " + "mpegversion = (int) 1, " + "layer = (int) [ 1, 3 ], " + "rate = (int) [ 8000, 48000 ], channels = (int) [ 1, 2 ]," + "parsed=(boolean) true") + ); + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1, parsed=(boolean)false") + ); + +static void gst_mpeg_audio_parse_finalize (GObject * object); + +static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse); +static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse); +static gboolean gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * size, gint * skipsize); +static GstFlowReturn gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); +static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse, + GstBaseParseFrame * frame); +static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse, + GstFormat src_format, gint64 src_value, + GstFormat dest_format, gint64 * dest_value); + +GST_BOILERPLATE (GstMpegAudioParse, gst_mpeg_audio_parse, GstBaseParse, + GST_TYPE_BASE_PARSE); + +#define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE \ + (gst_mpeg_audio_channel_mode_get_type()) + +static const GEnumValue mpeg_audio_channel_mode[] = { + {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"}, + {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"}, + {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"}, + {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"}, + {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"}, + {0, NULL, NULL}, +}; + +static GType +gst_mpeg_audio_channel_mode_get_type (void) +{ + static GType mpeg_audio_channel_mode_type = 0; + + if (!mpeg_audio_channel_mode_type) { + mpeg_audio_channel_mode_type = + g_enum_register_static ("GstMpegAudioChannelMode", + mpeg_audio_channel_mode); + } + return mpeg_audio_channel_mode_type; +} + +static const gchar * +gst_mpeg_audio_channel_mode_get_nick (gint mode) +{ + guint i; + for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) { + if (mpeg_audio_channel_mode[i].value == mode) + return mpeg_audio_channel_mode[i].value_nick; + } + return NULL; +} + +static void +gst_mpeg_audio_parse_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser", + "Codec/Parser/Audio", + "Parses and frames mpeg1 audio streams (levels 1-3), provides seek", + "Jan Schmidt <thaytan@mad.scientist.com>," + "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>"); +} + +static void +gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass) +{ + GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0, + "MPEG1 audio stream parser"); + + object_class->finalize = gst_mpeg_audio_parse_finalize; + + parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start); + parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop); + parse_class->check_valid_frame = + GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_check_valid_frame); + parse_class->parse_frame = + GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_parse_frame); + parse_class->pre_push_frame = + GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame); + parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert); + + /* register tags */ +#define GST_TAG_CRC "has-crc" +#define GST_TAG_MODE "channel-mode" + + gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN, + "has crc", "Using CRC", NULL); + gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING, + "channel mode", "MPEG audio channel mode", NULL); + + g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE); +} + +static void +gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse) +{ + mp3parse->channels = -1; + mp3parse->rate = -1; + mp3parse->sent_codec_tag = FALSE; + mp3parse->last_posted_crc = CRC_UNKNOWN; + mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN; + + mp3parse->hdr_bitrate = 0; + + mp3parse->xing_flags = 0; + mp3parse->xing_bitrate = 0; + mp3parse->xing_frames = 0; + mp3parse->xing_total_time = 0; + mp3parse->xing_bytes = 0; + mp3parse->xing_vbr_scale = 0; + memset (mp3parse->xing_seek_table, 0, 100); + memset (mp3parse->xing_seek_table_inverse, 0, 256); + + mp3parse->vbri_bitrate = 0; + mp3parse->vbri_frames = 0; + mp3parse->vbri_total_time = 0; + mp3parse->vbri_bytes = 0; + mp3parse->vbri_seek_points = 0; + g_free (mp3parse->vbri_seek_table); + mp3parse->vbri_seek_table = NULL; + + mp3parse->encoder_delay = 0; + mp3parse->encoder_padding = 0; +} + +static void +gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse, + GstMpegAudioParseClass * klass) +{ + gst_mpeg_audio_parse_reset (mp3parse); +} + +static void +gst_mpeg_audio_parse_finalize (GObject * object) +{ + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_mpeg_audio_parse_start (GstBaseParse * parse) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + + gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), 1024); + GST_DEBUG_OBJECT (parse, "starting"); + + gst_mpeg_audio_parse_reset (mp3parse); + + return TRUE; +} + +static gboolean +gst_mpeg_audio_parse_stop (GstBaseParse * parse) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + + GST_DEBUG_OBJECT (parse, "stopping"); + + gst_mpeg_audio_parse_reset (mp3parse); + + return TRUE; +} + +static const guint mp3types_bitrates[2][3][16] = { + { + {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,}, + {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,}, + {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,} + }, + { + {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,}, + {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}, + {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,} + }, +}; + +static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000}, +{22050, 24000, 16000}, +{11025, 12000, 8000} +}; + +static inline guint +mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header, + guint * put_version, guint * put_layer, guint * put_channels, + guint * put_bitrate, guint * put_samplerate, guint * put_mode, + guint * put_crc) +{ + guint length; + gulong mode, samplerate, bitrate, layer, channels, padding, crc; + gulong version; + gint lsf, mpg25; + + if (header & (1 << 20)) { + lsf = (header & (1 << 19)) ? 0 : 1; + mpg25 = 0; + } else { + lsf = 1; + mpg25 = 1; + } + + version = 1 + lsf + mpg25; + + layer = 4 - ((header >> 17) & 0x3); + + crc = (header >> 16) & 0x1; + + bitrate = (header >> 12) & 0xF; + bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000; + /* The caller has ensured we have a valid header, so bitrate can't be + zero here. */ + g_assert (bitrate != 0); + + samplerate = (header >> 10) & 0x3; + samplerate = mp3types_freqs[lsf + mpg25][samplerate]; + + padding = (header >> 9) & 0x1; + + mode = (header >> 6) & 0x3; + channels = (mode == 3) ? 1 : 2; + + switch (layer) { + case 1: + length = 4 * ((bitrate * 12) / samplerate + padding); + break; + case 2: + length = (bitrate * 144) / samplerate + padding; + break; + default: + case 3: + length = (bitrate * 144) / (samplerate << lsf) + padding; + break; + } + + GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes", + length); + GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, " + "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version, + layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode)); + + if (put_version) + *put_version = version; + if (put_layer) + *put_layer = layer; + if (put_channels) + *put_channels = channels; + if (put_bitrate) + *put_bitrate = bitrate; + if (put_samplerate) + *put_samplerate = samplerate; + if (put_mode) + *put_mode = mode; + if (put_crc) + *put_crc = crc; + + return length; +} + +/* Minimum number of consecutive, valid-looking frames to consider + * for resyncing */ +#define MIN_RESYNC_FRAMES 3 + +/* Perform extended validation to check that subsequent headers match + * the first header given here in important characteristics, to avoid + * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive + * frames to match their major characteristics. + * + * If at_eos is set to TRUE, we just check that we don't find any invalid + * frames in whatever data is available, rather than requiring a full + * MIN_RESYNC_FRAMES of data. + * + * Returns TRUE if we've seen enough data to validate or reject the frame. + * If TRUE is returned, then *valid contains TRUE if it validated, or false + * if we decided it was false sync. + * If FALSE is returned, then *valid contains minimum needed data. + */ +static gboolean +gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf, + guint32 header, int bpf, gboolean at_eos, gint * valid) +{ + guint32 next_header; + const guint8 *data; + guint available; + int frames_found = 1; + int offset = bpf; + + available = GST_BUFFER_SIZE (buf); + data = GST_BUFFER_DATA (buf); + + while (frames_found < MIN_RESYNC_FRAMES) { + /* Check if we have enough data for all these frames, plus the next + frame header. */ + if (available < offset + 4) { + if (at_eos) { + /* Running out of data at EOS is fine; just accept it */ + *valid = TRUE; + return TRUE; + } else { + *valid = offset + 4; + return FALSE; + } + } + + next_header = GST_READ_UINT32_BE (data + offset); + GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d", + offset, (unsigned int) header, (unsigned int) next_header, bpf); + +/* mask the bits which are allowed to differ between frames */ +#define HDRMASK ~((0xF << 12) /* bitrate */ | \ + (0x1 << 9) /* padding */ | \ + (0xf << 4) /* mode|mode extension */ | \ + (0xf)) /* copyright|emphasis */ + + if ((next_header & HDRMASK) != (header & HDRMASK)) { + /* If any of the unmasked bits don't match, then it's not valid */ + GST_DEBUG_OBJECT (mp3parse, "next header doesn't match " + "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)", + (guint) header, (guint) header & HDRMASK, (guint) next_header, + (guint) next_header & HDRMASK, bpf); + *valid = FALSE; + return TRUE; + } else if ((((next_header >> 12) & 0xf) == 0) || + (((next_header >> 12) & 0xf) == 0xf)) { + /* The essential parts were the same, but the bitrate held an + invalid value - also reject */ + GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)"); + *valid = FALSE; + return TRUE; + } + + bpf = mp3_type_frame_length_from_header (mp3parse, next_header, + NULL, NULL, NULL, NULL, NULL, NULL, NULL); + + offset += bpf; + frames_found++; + } + + *valid = TRUE; + return TRUE; +} + +static gboolean +gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse, + unsigned long head) +{ + GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head); + /* if it's not a valid sync */ + if ((head & 0xffe00000) != 0xffe00000) { + GST_WARNING_OBJECT (mp3parse, "invalid sync"); + return FALSE; + } + /* if it's an invalid MPEG version */ + if (((head >> 19) & 3) == 0x1) { + GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx", + (head >> 19) & 3); + return FALSE; + } + /* if it's an invalid layer */ + if (!((head >> 17) & 3)) { + GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3); + return FALSE; + } + /* if it's an invalid bitrate */ + if (((head >> 12) & 0xf) == 0x0) { + GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx." + "Free format files are not supported yet", (head >> 12) & 0xf); + return FALSE; + } + if (((head >> 12) & 0xf) == 0xf) { + GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf); + return FALSE; + } + /* if it's an invalid samplerate */ + if (((head >> 10) & 0x3) == 0x3) { + GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx", + (head >> 10) & 0x3); + return FALSE; + } + + if ((head & 0x3) == 0x2) { + /* Ignore this as there are some files with emphasis 0x2 that can + * be played fine. See BGO #537235 */ + GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3); + } + + return TRUE; +} + +static gboolean +gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse, + GstBaseParseFrame * frame, guint * framesize, gint * skipsize) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + GstBuffer *buf = frame->buffer; + GstByteReader reader = GST_BYTE_READER_INIT_FROM_BUFFER (buf); + gint off, bpf; + gboolean lost_sync, draining, valid, caps_change; + guint32 header; + guint bitrate, layer, rate, channels, version, mode, crc; + + if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < 6)) + return FALSE; + + off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000, + 0, GST_BUFFER_SIZE (buf)); + + GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off); + + /* didn't find anything that looks like a sync word, skip */ + if (off < 0) { + *skipsize = GST_BUFFER_SIZE (buf) - 3; + return FALSE; + } + + /* possible frame header, but not at offset 0? skip bytes before sync */ + if (off > 0) { + *skipsize = off; + return FALSE; + } + + /* make sure the values in the frame header look sane */ + header = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf)); + if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) { + *skipsize = 1; + return FALSE; + } + + GST_LOG_OBJECT (parse, "got frame"); + + bpf = mp3_type_frame_length_from_header (mp3parse, header, + &version, &layer, &channels, &bitrate, &rate, &mode, &crc); + g_assert (bpf != 0); + + if (channels != mp3parse->channels || rate != mp3parse->rate || + layer != mp3parse->layer || version != mp3parse->version) + caps_change = TRUE; + else + caps_change = FALSE; + + lost_sync = GST_BASE_PARSE_LOST_SYNC (parse); + draining = GST_BASE_PARSE_DRAINING (parse); + + if (!draining && (lost_sync || caps_change)) { + if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining, + &valid)) { + /* not enough data */ + gst_base_parse_set_min_frame_size (parse, valid); + *skipsize = 0; + return FALSE; + } else { + if (!valid) { + *skipsize = off + 2; + return FALSE; + } + } + } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) { + /* avoid caps jitter that we can't be sure of */ + *skipsize = off + 2; + return FALSE; + } + + *framesize = bpf; + return TRUE; +} + +static void +gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse, + GstBuffer * buf) +{ + const guint32 xing_id = 0x58696e67; /* 'Xing' in hex */ + const guint32 info_id = 0x496e666f; /* 'Info' in hex - found in LAME CBR files */ + const guint32 vbri_id = 0x56425249; /* 'VBRI' in hex */ + const guint32 lame_id = 0x4c414d45; /* 'LAME' in hex */ + gint offset_xing, offset_vbri; + guint64 avail; + gint64 upstream_total_bytes = 0; + GstFormat fmt = GST_FORMAT_BYTES; + guint32 read_id_xing = 0, read_id_vbri = 0; + const guint8 *data; + guint bitrate; + + if (mp3parse->sent_codec_tag) + return; + + /* Check first frame for Xing info */ + if (mp3parse->version == 1) { /* MPEG-1 file */ + if (mp3parse->channels == 1) + offset_xing = 0x11; + else + offset_xing = 0x20; + } else { /* MPEG-2 header */ + if (mp3parse->channels == 1) + offset_xing = 0x09; + else + offset_xing = 0x11; + } + + /* The VBRI tag is always at offset 0x20 */ + offset_vbri = 0x20; + + /* Skip the 4 bytes of the MP3 header too */ + offset_xing += 4; + offset_vbri += 4; + + /* Check if we have enough data to read the Xing header */ + avail = GST_BUFFER_SIZE (buf); + data = GST_BUFFER_DATA (buf); + + if (avail >= offset_xing + 4) { + read_id_xing = GST_READ_UINT32_BE (data + offset_xing); + } + if (avail >= offset_vbri + 4) { + read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri); + } + + /* obtain real upstream total bytes */ + fmt = GST_FORMAT_BYTES; + if (!gst_pad_query_peer_duration (GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE + (mp3parse)), &fmt, &upstream_total_bytes)) + upstream_total_bytes = 0; + + if (read_id_xing == xing_id || read_id_xing == info_id) { + guint32 xing_flags; + guint bytes_needed = offset_xing + 8; + gint64 total_bytes; + GstClockTime total_time; + + GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id); + + /* Move data after Xing header */ + data += offset_xing + 4; + + /* Read 4 base bytes of flags, big-endian */ + xing_flags = GST_READ_UINT32_BE (data); + data += 4; + if (xing_flags & XING_FRAMES_FLAG) + bytes_needed += 4; + if (xing_flags & XING_BYTES_FLAG) + bytes_needed += 4; + if (xing_flags & XING_TOC_FLAG) + bytes_needed += 100; + if (xing_flags & XING_VBR_SCALE_FLAG) + bytes_needed += 4; + if (avail < bytes_needed) { + GST_DEBUG_OBJECT (mp3parse, + "Not enough data to read Xing header (need %d)", bytes_needed); + return; + } + + GST_DEBUG_OBJECT (mp3parse, "Reading Xing header"); + mp3parse->xing_flags = xing_flags; + + if (xing_flags & XING_FRAMES_FLAG) { + mp3parse->xing_frames = GST_READ_UINT32_BE (data); + if (mp3parse->xing_frames == 0) { + GST_WARNING_OBJECT (mp3parse, + "Invalid number of frames in Xing header"); + mp3parse->xing_flags &= ~XING_FRAMES_FLAG; + } else { + mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND, + (guint64) (mp3parse->xing_frames) * (mp3parse->spf), + mp3parse->rate); + } + + data += 4; + } else { + mp3parse->xing_frames = 0; + mp3parse->xing_total_time = 0; + } + + if (xing_flags & XING_BYTES_FLAG) { + mp3parse->xing_bytes = GST_READ_UINT32_BE (data); + if (mp3parse->xing_bytes == 0) { + GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header"); + mp3parse->xing_flags &= ~XING_BYTES_FLAG; + } + data += 4; + } else { + mp3parse->xing_bytes = 0; + } + + /* If we know the upstream size and duration, compute the + * total bitrate, rounded up to the nearest kbit/sec */ + if ((total_time = mp3parse->xing_total_time) && + (total_bytes = mp3parse->xing_bytes)) { + mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes, + 8 * GST_SECOND, total_time); + mp3parse->xing_bitrate += 500; + mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000; + } + + if (xing_flags & XING_TOC_FLAG) { + int i, percent = 0; + guchar *table = mp3parse->xing_seek_table; + guchar old = 0, new; + guint first; + + first = data[0]; + GST_DEBUG_OBJECT (mp3parse, + "Subtracting initial offset of %d bytes from Xing TOC", first); + + /* xing seek table: percent time -> 1/256 bytepos */ + for (i = 0; i < 100; i++) { + new = data[i] - first; + if (old > new) { + GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC"); + mp3parse->xing_flags &= ~XING_TOC_FLAG; + goto skip_toc; + } + mp3parse->xing_seek_table[i] = old = new; + } + + /* build inverse table: 1/256 bytepos -> 1/100 percent time */ + for (i = 0; i < 256; i++) { + while (percent < 99 && table[percent + 1] <= i) + percent++; + + if (table[percent] == i) { + mp3parse->xing_seek_table_inverse[i] = percent * 100; + } else if (table[percent] < i && percent < 99) { + gdouble fa, fb, fx; + gint a = percent, b = percent + 1; + + fa = table[a]; + fb = table[b]; + fx = (b - a) / (fb - fa) * (i - fa) + a; + mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100); + } else if (percent == 99) { + gdouble fa, fb, fx; + gint a = percent, b = 100; + + fa = table[a]; + fb = 256.0; + fx = (b - a) / (fb - fa) * (i - fa) + a; + mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100); + } + } + skip_toc: + data += 100; + } else { + memset (mp3parse->xing_seek_table, 0, 100); + memset (mp3parse->xing_seek_table_inverse, 0, 256); + } + + if (xing_flags & XING_VBR_SCALE_FLAG) { + mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data); + data += 4; + } else + mp3parse->xing_vbr_scale = 0; + + GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %" + GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames, + GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes, + mp3parse->xing_vbr_scale); + + /* check for truncated file */ + if (upstream_total_bytes && mp3parse->xing_bytes && + mp3parse->xing_bytes * 0.8 > upstream_total_bytes) { + GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; " + "invalidating Xing header duration and size"); + mp3parse->xing_flags &= ~XING_BYTES_FLAG; + mp3parse->xing_flags &= ~XING_FRAMES_FLAG; + } + + /* Optional LAME tag? */ + if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) { + gchar lame_version[10] = { 0, }; + guint tag_rev; + guint32 encoder_delay, encoder_padding; + + memcpy (lame_version, data, 9); + data += 9; + tag_rev = data[0] >> 4; + GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'", + tag_rev, lame_version); + + /* Skip all the information we're not interested in */ + data += 12; + /* Encoder delay and end padding */ + encoder_delay = GST_READ_UINT24_BE (data); + encoder_delay >>= 12; + encoder_padding = GST_READ_UINT24_BE (data); + encoder_padding &= 0x000fff; + + mp3parse->encoder_delay = encoder_delay; + mp3parse->encoder_padding = encoder_padding; + + GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u", + encoder_delay, encoder_padding); + } + } + + if (read_id_vbri == vbri_id) { + gint64 total_bytes, total_frames; + GstClockTime total_time; + guint16 nseek_points; + + GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id); + + if (avail < offset_vbri + 26) { + GST_DEBUG_OBJECT (mp3parse, + "Not enough data to read VBRI header (need %d)", offset_vbri + 26); + return; + } + + GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header"); + + /* Move data after VBRI header */ + data += offset_vbri + 4; + + if (GST_READ_UINT16_BE (data) != 0x0001) { + GST_WARNING_OBJECT (mp3parse, + "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data)); + return; + } + data += 2; + + /* Skip encoder delay */ + data += 2; + + /* Skip quality */ + data += 2; + + total_bytes = GST_READ_UINT32_BE (data); + if (total_bytes != 0) + mp3parse->vbri_bytes = total_bytes; + data += 4; + + total_frames = GST_READ_UINT32_BE (data); + if (total_frames != 0) { + mp3parse->vbri_frames = total_frames; + mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND, + (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate); + } + data += 4; + + /* If we know the upstream size and duration, compute the + * total bitrate, rounded up to the nearest kbit/sec */ + if ((total_time = mp3parse->vbri_total_time) && + (total_bytes = mp3parse->vbri_bytes)) { + mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes, + 8 * GST_SECOND, total_time); + mp3parse->vbri_bitrate += 500; + mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000; + } + + nseek_points = GST_READ_UINT16_BE (data); + data += 2; + + if (nseek_points > 0) { + guint scale, seek_bytes, seek_frames; + gint i; + + mp3parse->vbri_seek_points = nseek_points; + + scale = GST_READ_UINT16_BE (data); + data += 2; + + seek_bytes = GST_READ_UINT16_BE (data); + data += 2; + + seek_frames = GST_READ_UINT16_BE (data); + + if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) { + GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table"); + goto out_vbri; + } + + if (avail < offset_vbri + 26 + nseek_points * seek_bytes) { + GST_WARNING_OBJECT (mp3parse, + "Not enough data to read VBRI seek table (need %d)", + offset_vbri + 26 + nseek_points * seek_bytes); + goto out_vbri; + } + + if (seek_frames * nseek_points < total_frames - seek_frames || + seek_frames * nseek_points > total_frames + seek_frames) { + GST_WARNING_OBJECT (mp3parse, + "VBRI seek table doesn't cover the complete file"); + goto out_vbri; + } + + if (avail < offset_vbri + 26) { + GST_DEBUG_OBJECT (mp3parse, + "Not enough data to read VBRI header (need %d)", + offset_vbri + 26 + nseek_points * seek_bytes); + return; + } + + data = GST_BUFFER_DATA (buf); + data += offset_vbri + 26; + + /* VBRI seek table: frame/seek_frames -> byte */ + mp3parse->vbri_seek_table = g_new (guint32, nseek_points); + if (seek_bytes == 4) + for (i = 0; i < nseek_points; i++) { + mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale; + data += 4; + } else if (seek_bytes == 3) + for (i = 0; i < nseek_points; i++) { + mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale; + data += 3; + } else if (seek_bytes == 2) + for (i = 0; i < nseek_points; i++) { + mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale; + data += 2; + } else /* seek_bytes == 1 */ + for (i = 0; i < nseek_points; i++) { + mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale; + data += 1; + } + } + out_vbri: + + GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %" + GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames, + GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes); + + /* check for truncated file */ + if (upstream_total_bytes && mp3parse->vbri_bytes && + mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) { + GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; " + "invalidating VBRI header duration and size"); + mp3parse->vbri_valid = FALSE; + } else { + mp3parse->vbri_valid = TRUE; + } + } else { + GST_DEBUG_OBJECT (mp3parse, + "Xing, LAME or VBRI header not found in first frame"); + } + + /* set duration if tables provided a valid one */ + if (mp3parse->xing_flags & XING_FRAMES_FLAG) { + gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME, + mp3parse->xing_total_time, 0); + } + if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) { + gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME, + mp3parse->vbri_total_time, 0); + } + + /* tell baseclass how nicely we can seek, and a bitrate if one found */ + /* FIXME: fill index with seek table */ +#if 0 + seekable = GST_BASE_PARSE_SEEK_DEFAULT; + if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes && + mp3parse->xing_total_time) + seekable = GST_BASE_PARSE_SEEK_TABLE; + + if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes && + mp3parse->vbri_total_time) + seekable = GST_BASE_PARSE_SEEK_TABLE; +#endif + + if (mp3parse->xing_bitrate) + bitrate = mp3parse->xing_bitrate; + else if (mp3parse->vbri_bitrate) + bitrate = mp3parse->vbri_bitrate; + else + bitrate = 0; + + gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate); +} + +static GstFlowReturn +gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse, + GstBaseParseFrame * frame) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + GstBuffer *buf = frame->buffer; + guint bitrate, layer, rate, channels, version, mode, crc; + + g_return_val_if_fail (GST_BUFFER_SIZE (buf) >= 4, GST_FLOW_ERROR); + + if (!mp3_type_frame_length_from_header (mp3parse, + GST_READ_UINT32_BE (GST_BUFFER_DATA (buf)), + &version, &layer, &channels, &bitrate, &rate, &mode, &crc)) + goto broken_header; + + if (G_UNLIKELY (channels != mp3parse->channels || rate != mp3parse->rate || + layer != mp3parse->layer || version != mp3parse->version)) { + GstCaps *caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, 1, + "mpegaudioversion", G_TYPE_INT, version, + "layer", G_TYPE_INT, layer, + "rate", G_TYPE_INT, rate, + "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); + gst_buffer_set_caps (buf, caps); + gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps); + gst_caps_unref (caps); + + mp3parse->rate = rate; + mp3parse->channels = channels; + mp3parse->layer = layer; + mp3parse->version = version; + + /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */ + if (mp3parse->layer == 1) + mp3parse->spf = 384; + else if (mp3parse->layer == 2) + mp3parse->spf = 1152; + else if (mp3parse->version == 1) { + mp3parse->spf = 1152; + } else { + /* MPEG-2 or "2.5" */ + mp3parse->spf = 576; + } + + /* lead_in: + * We start pushing 9 frames earlier (29 frames for MPEG2) than + * segment start to be able to decode the first frame we want. + * 9 (29) frames are the theoretical maximum of frames that contain + * data for the current frame (bit reservoir). + * + * lead_out: + * Some mp3 streams have an offset in the timestamps, for which we have to + * push the frame *after* the end position in order for the decoder to be + * able to decode everything up until the segment.stop position. */ + gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf, + (version == 1) ? 10 : 30, 2); + } + + mp3parse->hdr_bitrate = bitrate; + + /* For first frame; check for seek tables and output a codec tag */ + gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf); + + /* store some frame info for later processing */ + mp3parse->last_crc = crc; + mp3parse->last_mode = mode; + + return GST_FLOW_OK; + +/* ERRORS */ +broken_header: + { + /* this really shouldn't ever happen */ + GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL)); + return GST_FLOW_ERROR; + } +} + +static gboolean +gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse, + GstClockTime ts, gint64 * bytepos) +{ + gint64 total_bytes; + GstClockTime total_time; + + /* If XING seek table exists use this for time->byte conversion */ + if ((mp3parse->xing_flags & XING_TOC_FLAG) && + (total_bytes = mp3parse->xing_bytes) && + (total_time = mp3parse->xing_total_time)) { + gdouble fa, fb, fx; + gdouble percent = + CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) / + gst_util_guint64_to_gdouble (total_time), 0.0, 100.0); + gint index = CLAMP (percent, 0, 99); + + fa = mp3parse->xing_seek_table[index]; + if (index < 99) + fb = mp3parse->xing_seek_table[index + 1]; + else + fb = 256.0; + + fx = fa + (fb - fa) * (percent - index); + + *bytepos = (1.0 / 256.0) * fx * total_bytes; + + return TRUE; + } + + if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) && + (total_time = mp3parse->vbri_total_time)) { + gint i, j; + gdouble a, b, fa, fb; + + i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time); + i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1); + + a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time, + mp3parse->vbri_seek_points)); + fa = 0.0; + for (j = i; j >= 0; j--) + fa += mp3parse->vbri_seek_table[j]; + + if (i + 1 < mp3parse->vbri_seek_points) { + b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time, + mp3parse->vbri_seek_points)); + fb = fa + mp3parse->vbri_seek_table[i + 1]; + } else { + b = gst_guint64_to_gdouble (total_time); + fb = total_bytes; + } + + *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a); + + return TRUE; + } + + return FALSE; +} + +static gboolean +gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse, + gint64 bytepos, GstClockTime * ts) +{ + gint64 total_bytes; + GstClockTime total_time; + + /* If XING seek table exists use this for byte->time conversion */ + if ((mp3parse->xing_flags & XING_TOC_FLAG) && + (total_bytes = mp3parse->xing_bytes) && + (total_time = mp3parse->xing_total_time)) { + gdouble fa, fb, fx; + gdouble pos; + gint index; + + pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0); + index = CLAMP (pos, 0, 255); + fa = mp3parse->xing_seek_table_inverse[index]; + if (index < 255) + fb = mp3parse->xing_seek_table_inverse[index + 1]; + else + fb = 10000.0; + + fx = fa + (fb - fa) * (pos - index); + + *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time); + + return TRUE; + } + + if (mp3parse->vbri_seek_table && + (total_bytes = mp3parse->vbri_bytes) && + (total_time = mp3parse->vbri_total_time)) { + gint i = 0; + guint64 sum = 0; + gdouble a, b, fa, fb; + + do { + sum += mp3parse->vbri_seek_table[i]; + i++; + } while (i + 1 < mp3parse->vbri_seek_points + && sum + mp3parse->vbri_seek_table[i] < bytepos); + i--; + + a = gst_guint64_to_gdouble (sum); + fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time, + mp3parse->vbri_seek_points)); + + if (i + 1 < mp3parse->vbri_seek_points) { + b = a + mp3parse->vbri_seek_table[i + 1]; + fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time, + mp3parse->vbri_seek_points)); + } else { + b = total_bytes; + fb = gst_guint64_to_gdouble (total_time); + } + + *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a)); + + return TRUE; + } + + return FALSE; +} + +static gboolean +gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format, + gint64 src_value, GstFormat dest_format, gint64 * dest_value) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + gboolean res = FALSE; + + if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) + res = + gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value); + else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) + res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value, + (GstClockTime *) dest_value); + + /* if no tables, fall back to default estimated rate based conversion */ + if (!res) + return gst_base_parse_convert_default (parse, src_format, src_value, + dest_format, dest_value); + + return res; +} + +static GstFlowReturn +gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse, + GstBaseParseFrame * frame) +{ + GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse); + GstTagList *taglist; + + /* tag sending done late enough in hook to ensure pending events + * have already been sent */ + + if (!mp3parse->sent_codec_tag) { + gchar *codec; + + /* codec tag */ + if (mp3parse->layer == 3) { + codec = g_strdup_printf ("MPEG %d Audio, Layer %d (MP3)", + mp3parse->version, mp3parse->layer); + } else { + codec = g_strdup_printf ("MPEG %d Audio, Layer %d", + mp3parse->version, mp3parse->layer); + } + taglist = gst_tag_list_new (); + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, + GST_TAG_AUDIO_CODEC, codec, NULL); + if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 && + mp3parse->vbri_bitrate == 0) { + /* We don't have a VBR bitrate, so post the available bitrate as + * nominal and let baseparse calculate the real bitrate */ + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, + GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL); + } + gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse), + GST_BASE_PARSE_SRC_PAD (mp3parse), taglist); + g_free (codec); + + /* also signals the end of first-frame processing */ + mp3parse->sent_codec_tag = TRUE; + } + + /* we will create a taglist (if any of the parameters has changed) + * to add the tags that changed */ + taglist = NULL; + if (mp3parse->last_posted_crc != mp3parse->last_crc) { + gboolean using_crc; + + if (!taglist) { + taglist = gst_tag_list_new (); + } + mp3parse->last_posted_crc = mp3parse->last_crc; + if (mp3parse->last_posted_crc == CRC_PROTECTED) { + using_crc = TRUE; + } else { + using_crc = FALSE; + } + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC, + using_crc, NULL); + } + + if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) { + if (!taglist) { + taglist = gst_tag_list_new (); + } + mp3parse->last_posted_channel_mode = mp3parse->last_mode; + + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE, + gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL); + } + + /* if the taglist exists, we need to send it */ + if (taglist) { + gst_element_found_tags_for_pad (GST_ELEMENT (mp3parse), + GST_BASE_PARSE_SRC_PAD (mp3parse), taglist); + } + + /* usual clipping applies */ + frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; + + return GST_FLOW_OK; +} diff --git a/gst/audioparsers/gstmpegaudioparse.h b/gst/audioparsers/gstmpegaudioparse.h new file mode 100644 index 000000000..758000130 --- /dev/null +++ b/gst/audioparsers/gstmpegaudioparse.h @@ -0,0 +1,111 @@ +/* GStreamer MPEG audio parser + * Copyright (C) 2006-2007 Jan Schmidt <thaytan@mad.scientist.com> + * Copyright (C) 2010 Mark Nauwelaerts <mnauw users sf net> + * Copyright (C) 2010 Nokia Corporation. All rights reserved. + * Contact: Stefan Kost <stefan.kost@nokia.com> + * + * 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. + */ + +#ifndef __GST_MPEG_AUDIO_PARSE_H__ +#define __GST_MPEG_AUDIO_PARSE_H__ + +#include <gst/gst.h> +#include <gst/base/gstbaseparse.h> + +G_BEGIN_DECLS + +#define GST_TYPE_MPEG_AUDIO_PARSE \ + (gst_mpeg_audio_parse_get_type()) +#define GST_MPEG_AUDIO_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_AUDIO_PARSE, GstMpegAudioParse)) +#define GST_MPEG_AUDIO_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_MPEG_AUDIO_PARSE, GstMpegAudioParseClass)) +#define GST_IS_MPEG_AUDIO_PARSE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_MPEG_AUDIO_PARSE)) +#define GST_IS_MPEG_AUDIO_PARSE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_MPEG_AUDIO_PARSE)) + +typedef struct _GstMpegAudioParse GstMpegAudioParse; +typedef struct _GstMpegAudioParseClass GstMpegAudioParseClass; + +/** + * GstMpegAudioParse: + * + * The opaque GstMpegAudioParse object + */ +struct _GstMpegAudioParse { + GstBaseParse baseparse; + + /*< private >*/ + gint rate; + gint channels; + gint layer; + gint version; + + GstClockTime max_bitreservoir; + /* samples per frame */ + gint spf; + + gboolean sent_codec_tag; + guint last_posted_bitrate; + gint last_posted_crc, last_crc; + guint last_posted_channel_mode, last_mode; + + /* Bitrate from non-vbr headers */ + guint32 hdr_bitrate; + + /* Xing info */ + guint32 xing_flags; + guint32 xing_frames; + GstClockTime xing_total_time; + guint32 xing_bytes; + /* percent -> filepos mapping */ + guchar xing_seek_table[100]; + /* filepos -> percent mapping */ + guint16 xing_seek_table_inverse[256]; + guint32 xing_vbr_scale; + guint xing_bitrate; + + /* VBRI info */ + guint32 vbri_frames; + GstClockTime vbri_total_time; + guint32 vbri_bytes; + guint vbri_bitrate; + guint vbri_seek_points; + guint32 *vbri_seek_table; + gboolean vbri_valid; + + /* LAME info */ + guint32 encoder_delay; + guint32 encoder_padding; +}; + +/** + * GstMpegAudioParseClass: + * @parent_class: Element parent class. + * + * The opaque GstMpegAudioParseClass data structure. + */ +struct _GstMpegAudioParseClass { + GstBaseParseClass baseparse_class; +}; + +GType gst_mpeg_audio_parse_get_type (void); + +G_END_DECLS + +#endif /* __GST_MPEG_AUDIO_PARSE_H__ */ diff --git a/gst/audioparsers/plugin.c b/gst/audioparsers/plugin.c new file mode 100644 index 000000000..ae8332d3f --- /dev/null +++ b/gst/audioparsers/plugin.c @@ -0,0 +1,57 @@ +/* GStreamer audio parsers + * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net> + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstaacparse.h" +#include "gstamrparse.h" +#include "gstac3parse.h" +#include "gstdcaparse.h" +#include "gstflacparse.h" +#include "gstmpegaudioparse.h" + +static gboolean +plugin_init (GstPlugin * plugin) +{ + gboolean ret; + + ret = gst_element_register (plugin, "aacparse", + GST_RANK_PRIMARY + 1, GST_TYPE_AAC_PARSE); + ret &= gst_element_register (plugin, "amrparse", + GST_RANK_PRIMARY + 1, GST_TYPE_AMR_PARSE); + ret &= gst_element_register (plugin, "ac3parse", + GST_RANK_PRIMARY + 1, GST_TYPE_AC3_PARSE); + ret &= gst_element_register (plugin, "dcaparse", + GST_RANK_PRIMARY + 1, GST_TYPE_DCA_PARSE); + ret &= gst_element_register (plugin, "flacparse", + GST_RANK_PRIMARY + 1, GST_TYPE_FLAC_PARSE); + ret &= gst_element_register (plugin, "mpegaudioparse", + GST_RANK_PRIMARY + 2, GST_TYPE_MPEG_AUDIO_PARSE); + + return ret; +} + + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "audioparsers", + "Parsers for various audio formats", + plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN); |