2009-11-27 16:59:03 +00:00
|
|
|
/* 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
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-11-27 16:59:03 +00:00
|
|
|
*/
|
|
|
|
/**
|
2010-01-05 14:04:38 +00:00
|
|
|
* SECTION:element-ac3parse
|
2018-10-22 09:39:24 +00:00
|
|
|
* @title: ac3parse
|
2009-11-27 16:59:03 +00:00
|
|
|
* @short_description: AC3 parser
|
|
|
|
* @see_also: #GstAmrParse, #GstAACParse
|
|
|
|
*
|
|
|
|
* This is an AC3 parser.
|
2010-12-03 17:11:56 +00:00
|
|
|
*
|
2018-10-22 09:39:24 +00:00
|
|
|
* ## Example launch line
|
2010-12-03 17:11:56 +00:00
|
|
|
* |[
|
2012-08-26 21:39:55 +00:00
|
|
|
* gst-launch-1.0 filesrc location=abc.ac3 ! ac3parse ! a52dec ! audioresample ! audioconvert ! autoaudiosink
|
2010-12-03 17:11:56 +00:00
|
|
|
* ]|
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2009-11-27 16:59:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO:
|
2013-05-02 12:56:02 +00:00
|
|
|
* - audio/ac3 to audio/x-private1-ac3 is not implemented (done in the muxer)
|
2009-11-27 16:59:03 +00:00
|
|
|
* - should accept framed and unframed input (needs decodebin fixes first)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-02-15 12:00:38 +00:00
|
|
|
#include "gstaudioparserselements.h"
|
2009-11-27 16:59:03 +00:00
|
|
|
#include "gstac3parse.h"
|
2013-12-16 08:49:48 +00:00
|
|
|
#include <gst/base/base.h>
|
|
|
|
#include <gst/pbutils/pbutils.h>
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
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 };
|
2009-10-29 14:19:04 +00:00
|
|
|
static const guint numblks[4] = { 1, 2, 3, 6 };
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("audio/x-ac3, framed = (boolean) true, "
|
2011-09-09 11:35:50 +00:00
|
|
|
" channels = (int) [ 1, 6 ], rate = (int) [ 8000, 48000 ], "
|
2011-09-07 10:10:48 +00:00
|
|
|
" alignment = (string) { iec61937, frame}; "
|
2010-12-01 14:28:53 +00:00
|
|
|
"audio/x-eac3, framed = (boolean) true, "
|
2011-09-09 11:35:50 +00:00
|
|
|
" channels = (int) [ 1, 6 ], rate = (int) [ 8000, 48000 ], "
|
2011-09-07 10:10:48 +00:00
|
|
|
" alignment = (string) { iec61937, frame}; "));
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2013-05-02 12:56:02 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-ac3; " "audio/x-eac3; " "audio/ac3; "
|
|
|
|
"audio/x-private1-ac3"));
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
static void gst_ac3_parse_finalize (GObject * object);
|
|
|
|
|
|
|
|
static gboolean gst_ac3_parse_start (GstBaseParse * parse);
|
|
|
|
static gboolean gst_ac3_parse_stop (GstBaseParse * parse);
|
2012-02-09 12:41:53 +00:00
|
|
|
static GstFlowReturn gst_ac3_parse_handle_frame (GstBaseParse * parse,
|
|
|
|
GstBaseParseFrame * frame, gint * skipsize);
|
2013-12-16 08:49:48 +00:00
|
|
|
static GstFlowReturn gst_ac3_parse_pre_push_frame (GstBaseParse * parse,
|
|
|
|
GstBaseParseFrame * frame);
|
2011-04-12 11:31:47 +00:00
|
|
|
static gboolean gst_ac3_parse_src_event (GstBaseParse * parse,
|
|
|
|
GstEvent * event);
|
2011-11-26 13:34:10 +00:00
|
|
|
static GstCaps *gst_ac3_parse_get_sink_caps (GstBaseParse * parse,
|
|
|
|
GstCaps * filter);
|
2013-05-02 12:56:02 +00:00
|
|
|
static gboolean gst_ac3_parse_set_sink_caps (GstBaseParse * parse,
|
|
|
|
GstCaps * caps);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2011-04-29 15:55:28 +00:00
|
|
|
#define gst_ac3_parse_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstAc3Parse, gst_ac3_parse, GST_TYPE_BASE_PARSE);
|
2021-02-15 12:00:38 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE (ac3parse, "ac3parse",
|
|
|
|
GST_RANK_PRIMARY + 1, GST_TYPE_AC3_PARSE);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
static void
|
2011-04-29 15:55:28 +00:00
|
|
|
gst_ac3_parse_class_init (GstAc3ParseClass * klass)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
2011-04-29 15:55:28 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2009-11-27 16:59:03 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2011-04-29 15:55:28 +00:00
|
|
|
GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (ac3_parse_debug, "ac3parse", 0,
|
|
|
|
"AC3 audio stream parser");
|
|
|
|
|
|
|
|
object_class->finalize = gst_ac3_parse_finalize;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class, &sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &src_template);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2011-09-07 10:11:39 +00:00
|
|
|
"AC3 audio stream parser", "Codec/Parser/Converter/Audio",
|
2009-11-27 16:59:03 +00:00
|
|
|
"AC3 parser", "Tim-Philipp Müller <tim centricular net>");
|
|
|
|
|
|
|
|
parse_class->start = GST_DEBUG_FUNCPTR (gst_ac3_parse_start);
|
|
|
|
parse_class->stop = GST_DEBUG_FUNCPTR (gst_ac3_parse_stop);
|
2012-02-09 12:41:53 +00:00
|
|
|
parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_ac3_parse_handle_frame);
|
2013-12-16 08:49:48 +00:00
|
|
|
parse_class->pre_push_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_ac3_parse_pre_push_frame);
|
2011-04-12 11:31:47 +00:00
|
|
|
parse_class->src_event = GST_DEBUG_FUNCPTR (gst_ac3_parse_src_event);
|
2011-11-24 08:48:33 +00:00
|
|
|
parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_get_sink_caps);
|
2013-05-02 12:56:02 +00:00
|
|
|
parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ac3_parse_set_sink_caps);
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_ac3_parse_reset (GstAc3Parse * ac3parse)
|
|
|
|
{
|
|
|
|
ac3parse->channels = -1;
|
|
|
|
ac3parse->sample_rate = -1;
|
2011-07-11 12:23:41 +00:00
|
|
|
ac3parse->blocks = -1;
|
2010-12-01 14:28:53 +00:00
|
|
|
ac3parse->eac = FALSE;
|
2013-12-16 08:49:48 +00:00
|
|
|
ac3parse->sent_codec_tag = FALSE;
|
2011-04-12 11:31:47 +00:00
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_NONE);
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-29 15:55:28 +00:00
|
|
|
gst_ac3_parse_init (GstAc3Parse * ac3parse)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
2015-01-04 14:57:10 +00:00
|
|
|
gst_base_parse_set_min_frame_size (GST_BASE_PARSE (ac3parse), 8);
|
2009-11-27 16:59:03 +00:00
|
|
|
gst_ac3_parse_reset (ac3parse);
|
2013-05-02 12:56:02 +00:00
|
|
|
ac3parse->baseparse_chainfunc =
|
|
|
|
GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE (ac3parse))->chainfunc;
|
2013-12-03 20:41:28 +00:00
|
|
|
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (ac3parse));
|
2015-08-14 14:40:22 +00:00
|
|
|
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (ac3parse));
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-09 06:56:56 +00:00
|
|
|
static void
|
|
|
|
gst_ac3_parse_set_alignment (GstAc3Parse * ac3parse, gboolean eac)
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
GstStructure *st;
|
|
|
|
const gchar *str = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (G_LIKELY (!eac))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
caps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (ac3parse));
|
|
|
|
|
|
|
|
if (!caps)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (caps); i++) {
|
|
|
|
st = gst_caps_get_structure (caps, i);
|
|
|
|
|
|
|
|
if (!g_str_equal (gst_structure_get_name (st), "audio/x-eac3"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((str = gst_structure_get_string (st, "alignment"))) {
|
2011-04-12 11:31:47 +00:00
|
|
|
if (g_str_equal (str, "iec61937")) {
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
|
2011-04-09 06:56:56 +00:00
|
|
|
GST_DEBUG_OBJECT (ac3parse, "picked iec61937 alignment");
|
2011-04-12 11:31:47 +00:00
|
|
|
} else if (g_str_equal (str, "frame") == 0) {
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "picked frame alignment");
|
|
|
|
} else {
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
|
|
|
|
GST_WARNING_OBJECT (ac3parse, "unknown alignment: %s", str);
|
|
|
|
}
|
2011-04-09 06:56:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caps)
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* default */
|
|
|
|
if (ac3parse->align == GST_AC3_PARSE_ALIGN_NONE) {
|
2011-04-12 11:31:47 +00:00
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
|
2011-04-09 06:56:56 +00:00
|
|
|
GST_DEBUG_OBJECT (ac3parse, "picked syncframe alignment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-27 16:59:03 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ac3_parse_frame_header_ac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
|
2011-04-09 06:56:56 +00:00
|
|
|
gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
|
|
|
|
guint * sid)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
2011-06-06 18:43:31 +00:00
|
|
|
GstBitReader bits;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2011-09-09 11:35:50 +00:00
|
|
|
guint8 fscod, frmsizcod, bsid, acmod, lfe_on, rate_scale;
|
2011-06-06 18:43:31 +00:00
|
|
|
gboolean ret = FALSE;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-10-14 14:48:21 +00:00
|
|
|
GST_LOG_OBJECT (ac3parse, "parsing ac3");
|
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
gst_bit_reader_init (&bits, map.data, map.size);
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, skip * 8);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
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);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (fscod == 3 || frmsizcod >= G_N_ELEMENTS (frmsizcod_table))) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "bad fscod=%d frmsizcod=%d", fscod, frmsizcod);
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
bsid = gst_bit_reader_get_bits_uint8_unchecked (&bits, 5);
|
2011-04-11 18:54:00 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 3); /* bsmod */
|
2010-10-03 13:41:20 +00:00
|
|
|
acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-12-13 14:17:29 +00:00
|
|
|
/* spec not quite clear here: decoder should decode if less than 8,
|
|
|
|
* but seemingly only defines 6 and 8 cases */
|
2011-09-09 10:42:09 +00:00
|
|
|
/* Files with 9 and 10 happen, and seem to comply with the <= 8
|
|
|
|
format, so let them through. The spec says nothing about 9 and 10 */
|
|
|
|
if (bsid > 10) {
|
2009-11-27 16:59:03 +00:00
|
|
|
GST_DEBUG_OBJECT (ac3parse, "unexpected bsid=%d", bsid);
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2010-12-13 14:17:29 +00:00
|
|
|
} else if (bsid != 8 && bsid != 6) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "undefined bsid=%d", bsid);
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((acmod & 0x1) && (acmod != 0x1)) /* 3 front channels */
|
2010-10-03 13:41:20 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 2);
|
2009-11-27 16:59:03 +00:00
|
|
|
if ((acmod & 0x4)) /* if a surround channel exists */
|
2010-10-03 13:41:20 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 2);
|
2009-11-27 16:59:03 +00:00
|
|
|
if (acmod == 0x2) /* if in 2/0 mode */
|
2010-10-03 13:41:20 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 2);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2011-09-09 11:35:50 +00:00
|
|
|
/* 6/8->0, 9->1, 10->2,
|
|
|
|
see http://matroska.org/technical/specs/codecid/index.html */
|
|
|
|
rate_scale = (CLAMP (bsid, 8, 10) - 8);
|
|
|
|
|
2009-11-27 16:59:03 +00:00
|
|
|
if (frame_size)
|
|
|
|
*frame_size = frmsizcod_table[frmsizcod].frame_size[fscod] * 2;
|
|
|
|
if (rate)
|
2011-09-09 11:35:50 +00:00
|
|
|
*rate = fscod_rates[fscod] >> rate_scale;
|
2009-11-27 16:59:03 +00:00
|
|
|
if (chans)
|
|
|
|
*chans = acmod_chans[acmod] + lfe_on;
|
2009-10-29 14:19:04 +00:00
|
|
|
if (blks)
|
|
|
|
*blks = 6;
|
|
|
|
if (sid)
|
|
|
|
*sid = 0;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2011-06-06 18:43:31 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
cleanup:
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
|
|
|
return ret;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_ac3_parse_frame_header_eac3 (GstAc3Parse * ac3parse, GstBuffer * buf,
|
2011-04-09 06:56:56 +00:00
|
|
|
gint skip, guint * frame_size, guint * rate, guint * chans, guint * blks,
|
|
|
|
guint * sid)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
2011-06-06 18:43:31 +00:00
|
|
|
GstBitReader bits;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2009-10-29 14:19:04 +00:00
|
|
|
guint16 frmsiz, sample_rate, blocks;
|
|
|
|
guint8 strmtyp, fscod, fscod2, acmod, lfe_on, strmid, numblkscod;
|
2011-06-06 18:43:31 +00:00
|
|
|
gboolean ret = FALSE;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-10-14 14:48:21 +00:00
|
|
|
GST_LOG_OBJECT (ac3parse, "parsing e-ac3");
|
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
gst_bit_reader_init (&bits, map.data, map.size);
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, skip * 8);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
2010-10-14 14:48:21 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 16);
|
2010-10-03 13:41:20 +00:00
|
|
|
strmtyp = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* strmtyp */
|
2009-11-27 16:59:03 +00:00
|
|
|
if (G_UNLIKELY (strmtyp == 3)) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "bad strmtyp %d", strmtyp);
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
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 */
|
2009-11-27 16:59:03 +00:00
|
|
|
if (fscod == 3) {
|
2010-10-03 13:41:20 +00:00
|
|
|
fscod2 = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* fscod2 */
|
2009-11-27 16:59:03 +00:00
|
|
|
if (G_UNLIKELY (fscod2 == 3)) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "invalid fscod2");
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
sample_rate = fscod_rates[fscod2] / 2;
|
2009-10-29 14:19:04 +00:00
|
|
|
blocks = 6;
|
2009-11-27 16:59:03 +00:00
|
|
|
} else {
|
2010-10-03 13:41:20 +00:00
|
|
|
numblkscod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 2); /* numblkscod */
|
2009-11-27 16:59:03 +00:00
|
|
|
sample_rate = fscod_rates[fscod];
|
2009-10-29 14:19:04 +00:00
|
|
|
blocks = numblks[numblkscod];
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
acmod = gst_bit_reader_get_bits_uint8_unchecked (&bits, 3); /* acmod */
|
|
|
|
lfe_on = gst_bit_reader_get_bits_uint8_unchecked (&bits, 1); /* lfeon */
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, 5); /* bsid */
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
if (frame_size)
|
|
|
|
*frame_size = (frmsiz + 1) * 2;
|
|
|
|
if (rate)
|
|
|
|
*rate = sample_rate;
|
|
|
|
if (chans)
|
|
|
|
*chans = acmod_chans[acmod] + lfe_on;
|
2009-10-29 14:19:04 +00:00
|
|
|
if (blks)
|
|
|
|
*blks = blocks;
|
|
|
|
if (sid)
|
|
|
|
*sid = (strmtyp & 0x1) << 3 | strmid;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2011-06-06 18:43:31 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
cleanup:
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
|
|
|
return ret;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_ac3_parse_frame_header (GstAc3Parse * parse, GstBuffer * buf, gint skip,
|
2010-12-01 14:28:53 +00:00
|
|
|
guint * framesize, guint * rate, guint * chans, guint * blocks,
|
|
|
|
guint * sid, gboolean * eac)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
2011-06-06 18:43:31 +00:00
|
|
|
GstBitReader bits;
|
2009-11-27 16:59:03 +00:00
|
|
|
guint16 sync;
|
|
|
|
guint8 bsid;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2011-06-06 18:43:31 +00:00
|
|
|
gboolean ret = FALSE;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
gst_bit_reader_init (&bits, map.data, map.size);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
GST_MEMDUMP_OBJECT (parse, "AC3 frame sync", map.data, MIN (map.size, 16));
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_bit_reader_skip_unchecked (&bits, skip * 8);
|
|
|
|
|
2010-10-03 13:41:20 +00:00
|
|
|
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);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (sync != 0x0b77))
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2010-10-14 14:48:21 +00:00
|
|
|
GST_LOG_OBJECT (parse, "bsid = %d", bsid);
|
|
|
|
|
2009-11-27 16:59:03 +00:00
|
|
|
if (bsid <= 10) {
|
2010-12-01 14:28:53 +00:00
|
|
|
if (eac)
|
|
|
|
*eac = FALSE;
|
2011-08-03 16:25:30 +00:00
|
|
|
ret = gst_ac3_parse_frame_header_ac3 (parse, buf, skip, framesize, rate,
|
2011-04-09 06:56:56 +00:00
|
|
|
chans, blocks, sid);
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
} else if (bsid <= 16) {
|
2010-12-01 14:28:53 +00:00
|
|
|
if (eac)
|
|
|
|
*eac = TRUE;
|
2011-08-03 16:25:30 +00:00
|
|
|
ret = gst_ac3_parse_frame_header_eac3 (parse, buf, skip, framesize, rate,
|
2011-04-09 06:56:56 +00:00
|
|
|
chans, blocks, sid);
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
|
2015-04-17 12:32:54 +00:00
|
|
|
ret = FALSE;
|
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
2011-06-06 18:43:31 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (parse, "unexpected bsid %d", bsid);
|
|
|
|
|
|
|
|
cleanup:
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
|
|
|
return ret;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_ac3_parse_handle_frame (GstBaseParse * parse,
|
|
|
|
GstBaseParseFrame * frame, gint * skipsize)
|
2009-11-27 16:59:03 +00:00
|
|
|
{
|
|
|
|
GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
|
2011-01-10 14:34:48 +00:00
|
|
|
GstBuffer *buf = frame->buffer;
|
2011-06-06 18:43:31 +00:00
|
|
|
GstByteReader reader;
|
2009-11-27 16:59:03 +00:00
|
|
|
gint off;
|
2011-04-09 06:56:56 +00:00
|
|
|
gboolean lost_sync, draining, eac, more = FALSE;
|
|
|
|
guint frmsiz, blocks, sid;
|
2012-02-09 12:41:53 +00:00
|
|
|
guint rate, chans;
|
|
|
|
gboolean update_rate = FALSE;
|
|
|
|
gint framesize = 0;
|
2011-04-09 06:56:56 +00:00
|
|
|
gint have_blocks = 0;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2011-06-06 18:43:31 +00:00
|
|
|
gboolean ret = FALSE;
|
2012-02-09 12:41:53 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
2011-06-06 18:43:31 +00:00
|
|
|
|
2015-01-04 14:57:10 +00:00
|
|
|
if (G_UNLIKELY (map.size < 8)) {
|
2012-02-09 12:41:53 +00:00
|
|
|
*skipsize = 1;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2012-02-09 12:41:53 +00:00
|
|
|
}
|
2009-11-23 15:34:50 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_byte_reader_init (&reader, map.data, map.size);
|
2009-11-27 16:59:03 +00:00
|
|
|
off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0x0b770000,
|
2012-01-23 16:25:37 +00:00
|
|
|
0, map.size);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
|
|
|
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) {
|
2012-01-23 16:25:37 +00:00
|
|
|
*skipsize = map.size - 3;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* possible frame header, but not at offset 0? skip bytes before sync */
|
|
|
|
if (off > 0) {
|
|
|
|
*skipsize = off;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the values in the frame header look sane */
|
2012-02-09 12:41:53 +00:00
|
|
|
if (!gst_ac3_parse_frame_header (ac3parse, buf, 0, &frmsiz, &rate, &chans,
|
2011-04-09 06:56:56 +00:00
|
|
|
&blocks, &sid, &eac)) {
|
2009-11-27 16:59:03 +00:00
|
|
|
*skipsize = off + 2;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
GST_LOG_OBJECT (parse, "size: %u, blocks: %u, rate: %u, chans: %u", frmsiz,
|
|
|
|
blocks, rate, chans);
|
|
|
|
|
|
|
|
framesize = frmsiz;
|
2011-04-09 06:56:56 +00:00
|
|
|
|
2011-04-12 11:31:47 +00:00
|
|
|
if (G_UNLIKELY (g_atomic_int_get (&ac3parse->align) ==
|
|
|
|
GST_AC3_PARSE_ALIGN_NONE))
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_ac3_parse_set_alignment (ac3parse, eac);
|
|
|
|
|
2009-11-27 16:59:03 +00:00
|
|
|
GST_LOG_OBJECT (parse, "got frame");
|
2009-10-28 13:08:43 +00:00
|
|
|
|
2011-04-07 12:26:41 +00:00
|
|
|
lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
|
|
|
|
draining = GST_BASE_PARSE_DRAINING (parse);
|
2009-10-28 13:08:43 +00:00
|
|
|
|
2011-04-12 11:31:47 +00:00
|
|
|
if (g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937) {
|
2011-04-09 06:56:56 +00:00
|
|
|
/* We need 6 audio blocks from each substream, so we keep going forwards
|
|
|
|
* till we have it */
|
|
|
|
|
|
|
|
g_assert (blocks > 0);
|
|
|
|
GST_LOG_OBJECT (ac3parse, "Need %d frames before pushing", 6 / blocks);
|
|
|
|
|
|
|
|
if (sid != 0) {
|
|
|
|
/* We need the first substream to be the one with id 0 */
|
|
|
|
GST_LOG_OBJECT (ac3parse, "Skipping till we find sid 0");
|
|
|
|
*skipsize = off + 2;
|
2012-02-09 21:10:07 +00:00
|
|
|
goto cleanup;
|
2011-04-09 06:56:56 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
framesize = 0;
|
2011-04-09 06:56:56 +00:00
|
|
|
|
|
|
|
/* Loop till we have 6 blocks per substream */
|
|
|
|
for (have_blocks = 0; !more && have_blocks < 6; have_blocks += blocks) {
|
|
|
|
/* Loop till we get one frame from each substream */
|
|
|
|
do {
|
2012-02-09 12:41:53 +00:00
|
|
|
framesize += frmsiz;
|
2011-04-09 06:56:56 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
if (!gst_byte_reader_skip (&reader, frmsiz)
|
2012-02-09 12:41:53 +00:00
|
|
|
|| map.size < (framesize + 6)) {
|
2011-04-09 06:56:56 +00:00
|
|
|
more = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
if (!gst_ac3_parse_frame_header (ac3parse, buf, framesize, &frmsiz,
|
2011-04-09 06:56:56 +00:00
|
|
|
NULL, NULL, NULL, &sid, &eac)) {
|
|
|
|
*skipsize = off + 2;
|
2012-02-09 21:10:07 +00:00
|
|
|
goto cleanup;
|
2011-04-09 06:56:56 +00:00
|
|
|
}
|
|
|
|
} while (sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're now at the next frame, so no need to skip if resyncing */
|
|
|
|
frmsiz = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-07 12:26:41 +00:00
|
|
|
if (lost_sync && !draining) {
|
2009-12-18 12:01:17 +00:00
|
|
|
guint16 word = 0;
|
2009-10-28 13:08:43 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "resyncing; checking next frame syncword");
|
|
|
|
|
2011-04-09 06:56:56 +00:00
|
|
|
if (more || !gst_byte_reader_skip (&reader, frmsiz) ||
|
2009-10-28 13:08:43 +00:00
|
|
|
!gst_byte_reader_get_uint16_be (&reader, &word)) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "... but not sufficient data");
|
2015-01-04 14:57:10 +00:00
|
|
|
gst_base_parse_set_min_frame_size (parse, framesize + 8);
|
2009-10-28 13:08:43 +00:00
|
|
|
*skipsize = 0;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-10-28 13:08:43 +00:00
|
|
|
} else {
|
|
|
|
if (word != 0x0b77) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "0x%x not OK", word);
|
|
|
|
*skipsize = off + 2;
|
2011-06-06 18:43:31 +00:00
|
|
|
goto cleanup;
|
2009-10-28 13:08:43 +00:00
|
|
|
} else {
|
|
|
|
/* ok, got sync now, let's assume constant frame size */
|
2012-02-09 12:41:53 +00:00
|
|
|
gst_base_parse_set_min_frame_size (parse, framesize);
|
2009-10-28 13:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
/* expect to have found a frame here */
|
|
|
|
g_assert (framesize);
|
2011-06-06 18:43:31 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
/* arrange for metadata setup */
|
2009-10-29 14:19:04 +00:00
|
|
|
if (G_UNLIKELY (sid)) {
|
2011-01-27 15:52:34 +00:00
|
|
|
/* dependent frame, no need to (ac)count for or consider further */
|
2009-10-29 14:19:04 +00:00
|
|
|
GST_LOG_OBJECT (parse, "sid: %d", sid);
|
2011-01-10 14:34:48 +00:00
|
|
|
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NO_FRAME;
|
2011-01-27 15:52:34 +00:00
|
|
|
/* 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);
|
2012-02-09 12:41:53 +00:00
|
|
|
/* only shortcut if we already arranged for caps */
|
2011-01-27 15:52:34 +00:00
|
|
|
if (G_LIKELY (ac3parse->sample_rate > 0))
|
2012-02-09 12:41:53 +00:00
|
|
|
goto cleanup;
|
2009-10-29 14:19:04 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 14:28:53 +00:00
|
|
|
if (G_UNLIKELY (ac3parse->sample_rate != rate || ac3parse->channels != chans
|
2011-09-23 13:36:36 +00:00
|
|
|
|| ac3parse->eac != eac)) {
|
2010-12-01 14:28:53 +00:00
|
|
|
GstCaps *caps = gst_caps_new_simple (eac ? "audio/x-eac3" : "audio/x-ac3",
|
2009-10-27 15:16:50 +00:00
|
|
|
"framed", G_TYPE_BOOLEAN, TRUE, "rate", G_TYPE_INT, rate,
|
|
|
|
"channels", G_TYPE_INT, chans, NULL);
|
2011-04-09 06:56:56 +00:00
|
|
|
gst_caps_set_simple (caps, "alignment", G_TYPE_STRING,
|
2011-04-12 11:31:47 +00:00
|
|
|
g_atomic_int_get (&ac3parse->align) == GST_AC3_PARSE_ALIGN_IEC61937 ?
|
|
|
|
"iec61937" : "frame", NULL);
|
2009-11-27 16:59:03 +00:00
|
|
|
gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
|
|
|
|
gst_caps_unref (caps);
|
2009-10-27 15:16:50 +00:00
|
|
|
|
|
|
|
ac3parse->sample_rate = rate;
|
|
|
|
ac3parse->channels = chans;
|
2010-12-01 14:28:53 +00:00
|
|
|
ac3parse->eac = eac;
|
2009-10-28 13:07:17 +00:00
|
|
|
|
2011-07-11 12:23:41 +00:00
|
|
|
update_rate = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY (ac3parse->blocks != blocks)) {
|
|
|
|
ac3parse->blocks = blocks;
|
|
|
|
|
|
|
|
update_rate = TRUE;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 12:23:41 +00:00
|
|
|
if (G_UNLIKELY (update_rate))
|
|
|
|
gst_base_parse_set_frame_rate (parse, rate, 256 * blocks, 2, 2);
|
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
cleanup:
|
|
|
|
gst_buffer_unmap (buf, &map);
|
2009-11-27 16:59:03 +00:00
|
|
|
|
2012-02-09 12:41:53 +00:00
|
|
|
if (ret && framesize <= map.size) {
|
|
|
|
res = gst_base_parse_finish_frame (parse, frame, framesize);
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
2012-02-09 12:41:53 +00:00
|
|
|
|
|
|
|
return res;
|
2009-11-27 16:59:03 +00:00
|
|
|
}
|
2011-04-12 11:31:47 +00:00
|
|
|
|
2013-05-02 12:56:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MPEG-PS private1 streams add a 2 bytes "Audio Substream Headers" for each
|
|
|
|
* buffer (not each frame) with the offset of the next frame's start.
|
|
|
|
*
|
|
|
|
* Buffer 1:
|
|
|
|
* -------------------------------------------
|
|
|
|
* |firstAccUnit|AC3SyncWord|xxxxxxxxxxxxxxxxx
|
|
|
|
* -------------------------------------------
|
|
|
|
* Buffer 2:
|
|
|
|
* -------------------------------------------
|
|
|
|
* |firstAccUnit|xxxxxx|AC3SyncWord|xxxxxxxxxx
|
|
|
|
* -------------------------------------------
|
|
|
|
*
|
|
|
|
* These 2 bytes can be dropped safely as they do not include any timing
|
|
|
|
* information, only the offset to the start of the next frame.
|
|
|
|
*
|
|
|
|
* From http://stnsoft.com/DVD/ass-hdr.html:
|
|
|
|
* "FirstAccUnit offset to frame which corresponds to PTS value offset 0 is the
|
|
|
|
* last byte of FirstAccUnit, ie add the offset of byte 2 to get the AU's offset
|
|
|
|
* The value 0000 indicates there is no first access unit"
|
|
|
|
* */
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2013-11-11 12:17:25 +00:00
|
|
|
gst_ac3_parse_chain_priv (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2013-05-02 12:56:02 +00:00
|
|
|
{
|
|
|
|
GstAc3Parse *ac3parse = GST_AC3_PARSE (parent);
|
2013-05-04 08:48:02 +00:00
|
|
|
GstFlowReturn ret;
|
|
|
|
gsize size;
|
2013-11-11 12:17:25 +00:00
|
|
|
guint8 data[2];
|
|
|
|
gint offset;
|
|
|
|
gint len;
|
|
|
|
GstBuffer *subbuf;
|
|
|
|
gint first_access;
|
|
|
|
|
|
|
|
size = gst_buffer_get_size (buf);
|
|
|
|
if (size < 2)
|
|
|
|
goto not_enough_data;
|
|
|
|
|
|
|
|
gst_buffer_extract (buf, 0, data, 2);
|
|
|
|
first_access = (data[0] << 8) | data[1];
|
|
|
|
|
|
|
|
/* Skip the first_access header */
|
|
|
|
offset = 2;
|
|
|
|
|
|
|
|
if (first_access > 1) {
|
|
|
|
/* Length of data before first_access */
|
|
|
|
len = first_access - 1;
|
|
|
|
|
|
|
|
if (len <= 0 || offset + len > size)
|
|
|
|
goto bad_first_access_parameter;
|
|
|
|
|
|
|
|
subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
|
|
|
|
GST_BUFFER_DTS (subbuf) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_PTS (subbuf) = GST_CLOCK_TIME_NONE;
|
|
|
|
ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
|
2017-08-14 07:28:22 +00:00
|
|
|
if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED) {
|
2013-11-11 12:17:25 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
len = size - offset;
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len);
|
|
|
|
GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
|
|
|
|
GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
|
2013-05-04 08:48:02 +00:00
|
|
|
|
2013-11-11 12:17:25 +00:00
|
|
|
ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
|
|
|
|
}
|
|
|
|
gst_buffer_unref (buf);
|
2013-05-04 08:48:02 +00:00
|
|
|
} else {
|
2013-11-11 12:17:25 +00:00
|
|
|
/* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */
|
|
|
|
subbuf =
|
|
|
|
gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset,
|
|
|
|
size - offset);
|
|
|
|
GST_BUFFER_PTS (subbuf) = GST_BUFFER_PTS (buf);
|
|
|
|
GST_BUFFER_DTS (subbuf) = GST_BUFFER_DTS (buf);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
ret = ac3parse->baseparse_chainfunc (pad, parent, subbuf);
|
2013-05-04 08:48:02 +00:00
|
|
|
}
|
2013-05-02 12:56:02 +00:00
|
|
|
|
2013-11-11 12:17:25 +00:00
|
|
|
done:
|
2013-05-02 12:56:02 +00:00
|
|
|
return ret;
|
2013-11-11 12:17:25 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_enough_data:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
|
|
|
|
("Insufficient data in buffer. Can't determine first_acess"));
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
bad_first_access_parameter:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (GST_ELEMENT (ac3parse), STREAM, FORMAT, (NULL),
|
|
|
|
("Bad first_access parameter (%d) in buffer", first_access));
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2013-05-02 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 08:49:48 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_ac3_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
|
|
|
|
{
|
|
|
|
GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
|
|
|
|
|
|
|
|
if (!ac3parse->sent_codec_tag) {
|
|
|
|
GstTagList *taglist;
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
/* codec tag */
|
|
|
|
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
|
2015-12-15 17:10:00 +00:00
|
|
|
if (G_UNLIKELY (caps == NULL)) {
|
|
|
|
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
|
|
|
|
GST_INFO_OBJECT (parse, "Src pad is flushing");
|
|
|
|
return GST_FLOW_FLUSHING;
|
|
|
|
} else {
|
|
|
|
GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
taglist = gst_tag_list_new_empty ();
|
2013-12-16 08:49:48 +00:00
|
|
|
gst_pb_utils_add_codec_description_to_tag_list (taglist,
|
|
|
|
GST_TAG_AUDIO_CODEC, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
2015-03-17 21:50:37 +00:00
|
|
|
gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
|
|
|
|
gst_tag_list_unref (taglist);
|
2013-12-16 08:49:48 +00:00
|
|
|
|
|
|
|
/* also signals the end of first-frame processing */
|
|
|
|
ac3parse->sent_codec_tag = TRUE;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:28:39 +00:00
|
|
|
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
|
|
|
|
|
2013-12-16 08:49:48 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2011-04-12 11:31:47 +00:00
|
|
|
static gboolean
|
|
|
|
gst_ac3_parse_src_event (GstBaseParse * parse, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM) &&
|
|
|
|
gst_event_has_name (event, "ac3parse-set-alignment")) {
|
|
|
|
const GstStructure *st = gst_event_get_structure (event);
|
|
|
|
const gchar *align = gst_structure_get_string (st, "alignment");
|
|
|
|
|
|
|
|
if (g_str_equal (align, "iec61937")) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "Switching to iec61937 alignment");
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_IEC61937);
|
|
|
|
} else if (g_str_equal (align, "frame")) {
|
|
|
|
GST_DEBUG_OBJECT (ac3parse, "Switching to frame alignment");
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
|
|
|
|
} else {
|
|
|
|
g_atomic_int_set (&ac3parse->align, GST_AC3_PARSE_ALIGN_FRAME);
|
|
|
|
GST_WARNING_OBJECT (ac3parse, "Got unknown alignment request (%s) "
|
|
|
|
"reverting to frame alignment.",
|
|
|
|
gst_structure_get_string (st, "alignment"));
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_BASE_PARSE_CLASS (parent_class)->src_event (parse, event);
|
|
|
|
}
|
2011-11-24 08:48:33 +00:00
|
|
|
|
2013-12-03 20:29:13 +00:00
|
|
|
static void
|
|
|
|
remove_fields (GstCaps * caps)
|
|
|
|
{
|
|
|
|
guint i, n;
|
|
|
|
|
|
|
|
n = gst_caps_get_size (caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
GstStructure *s = gst_caps_get_structure (caps, i);
|
|
|
|
|
|
|
|
gst_structure_remove_field (s, "framed");
|
|
|
|
gst_structure_remove_field (s, "alignment");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 19:05:42 +00:00
|
|
|
static GstCaps *
|
|
|
|
extend_caps (GstCaps * caps, gboolean add_private)
|
|
|
|
{
|
|
|
|
guint i, n;
|
|
|
|
GstCaps *ncaps = gst_caps_new_empty ();
|
|
|
|
|
|
|
|
n = gst_caps_get_size (caps);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
GstStructure *s = gst_caps_get_structure (caps, i);
|
|
|
|
|
|
|
|
if (add_private && !gst_structure_has_name (s, "audio/x-private1-ac3")) {
|
|
|
|
GstStructure *ns = gst_structure_copy (s);
|
|
|
|
gst_structure_set_name (ns, "audio/x-private1-ac3");
|
|
|
|
gst_caps_append_structure (ncaps, ns);
|
|
|
|
} else if (!add_private &&
|
|
|
|
gst_structure_has_name (s, "audio/x-private1-ac3")) {
|
|
|
|
GstStructure *ns = gst_structure_copy (s);
|
|
|
|
gst_structure_set_name (ns, "audio/x-ac3");
|
|
|
|
gst_caps_append_structure (ncaps, ns);
|
|
|
|
ns = gst_structure_copy (s);
|
|
|
|
gst_structure_set_name (ns, "audio/x-eac3");
|
|
|
|
gst_caps_append_structure (ncaps, ns);
|
|
|
|
} else if (!add_private) {
|
|
|
|
gst_caps_append_structure (ncaps, gst_structure_copy (s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add_private) {
|
|
|
|
gst_caps_append (caps, ncaps);
|
|
|
|
} else {
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = ncaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2011-11-24 08:48:33 +00:00
|
|
|
static GstCaps *
|
2011-11-26 13:34:10 +00:00
|
|
|
gst_ac3_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
|
2011-11-24 08:48:33 +00:00
|
|
|
{
|
2012-03-10 09:51:44 +00:00
|
|
|
GstCaps *peercaps, *templ;
|
2011-11-24 08:48:33 +00:00
|
|
|
GstCaps *res;
|
|
|
|
|
2012-06-05 07:18:12 +00:00
|
|
|
templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
|
2013-12-03 20:36:54 +00:00
|
|
|
if (filter) {
|
|
|
|
GstCaps *fcopy = gst_caps_copy (filter);
|
|
|
|
/* Remove the fields we convert */
|
|
|
|
remove_fields (fcopy);
|
2014-01-27 19:05:42 +00:00
|
|
|
/* we do not ask downstream to handle x-private1-ac3 */
|
|
|
|
fcopy = extend_caps (fcopy, FALSE);
|
2013-12-03 20:36:54 +00:00
|
|
|
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
|
|
|
|
gst_caps_unref (fcopy);
|
|
|
|
} else
|
|
|
|
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
|
2012-06-05 07:18:12 +00:00
|
|
|
|
2011-11-24 08:48:33 +00:00
|
|
|
if (peercaps) {
|
|
|
|
/* Remove the framed and alignment field. We can convert
|
|
|
|
* between different alignments. */
|
|
|
|
peercaps = gst_caps_make_writable (peercaps);
|
2013-12-03 20:29:13 +00:00
|
|
|
remove_fields (peercaps);
|
2014-01-27 19:05:42 +00:00
|
|
|
/* also allow for x-private1-ac3 input */
|
|
|
|
peercaps = extend_caps (peercaps, TRUE);
|
2011-11-24 08:48:33 +00:00
|
|
|
|
2012-03-10 09:51:44 +00:00
|
|
|
res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
|
2011-11-24 08:48:33 +00:00
|
|
|
gst_caps_unref (peercaps);
|
2013-12-04 08:12:07 +00:00
|
|
|
gst_caps_unref (templ);
|
2011-11-24 08:48:33 +00:00
|
|
|
} else {
|
2012-03-10 09:51:44 +00:00
|
|
|
res = templ;
|
2011-11-24 08:48:33 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 07:18:12 +00:00
|
|
|
if (filter) {
|
|
|
|
GstCaps *intersection;
|
|
|
|
|
|
|
|
intersection =
|
|
|
|
gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (res);
|
|
|
|
res = intersection;
|
|
|
|
}
|
|
|
|
|
2011-11-24 08:48:33 +00:00
|
|
|
return res;
|
|
|
|
}
|
2013-05-02 12:56:02 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_ac3_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
GstAc3Parse *ac3parse = GST_AC3_PARSE (parse);
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
if (gst_structure_has_name (s, "audio/x-private1-ac3")) {
|
|
|
|
gst_pad_set_chain_function (parse->sinkpad, gst_ac3_parse_chain_priv);
|
|
|
|
} else {
|
|
|
|
gst_pad_set_chain_function (parse->sinkpad, ac3parse->baseparse_chainfunc);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|