2012-09-14 11:05:15 +00:00
|
|
|
/*
|
|
|
|
* Initially based on gst-omx/omx/gstomxvideodec.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012, Collabora Ltd.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
|
|
*
|
2015-05-27 21:34:14 +00:00
|
|
|
* Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
*
|
2012-09-14 11:05:15 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
2012-10-18 14:41:07 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2012-09-14 11:05:15 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-09-25 08:32:26 +00:00
|
|
|
#ifdef HAVE_ORC
|
|
|
|
#include <orc/orc.h>
|
|
|
|
#else
|
|
|
|
#define orc_memcpy memcpy
|
|
|
|
#endif
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
#include "gstamcaudiodec.h"
|
|
|
|
#include "gstamc-constants.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_amc_audio_dec_debug_category);
|
|
|
|
#define GST_CAT_DEFAULT gst_amc_audio_dec_debug_category
|
|
|
|
|
2014-07-17 08:53:30 +00:00
|
|
|
#define GST_AUDIO_DECODER_ERROR_FROM_ERROR(el, err) G_STMT_START { \
|
|
|
|
gchar *__dbg = g_strdup (err->message); \
|
|
|
|
GstAudioDecoder *__dec = GST_AUDIO_DECODER (el); \
|
|
|
|
GST_WARNING_OBJECT (el, "error: %s", __dbg); \
|
|
|
|
_gst_audio_decoder_error (__dec, 1, \
|
|
|
|
err->domain, err->code, \
|
|
|
|
NULL, __dbg, __FILE__, GST_FUNCTION, __LINE__); \
|
|
|
|
g_clear_error (&err); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
/* prototypes */
|
|
|
|
static void gst_amc_audio_dec_finalize (GObject * object);
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_amc_audio_dec_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
|
|
|
|
|
|
|
static gboolean gst_amc_audio_dec_open (GstAudioDecoder * decoder);
|
|
|
|
static gboolean gst_amc_audio_dec_close (GstAudioDecoder * decoder);
|
|
|
|
static gboolean gst_amc_audio_dec_start (GstAudioDecoder * decoder);
|
|
|
|
static gboolean gst_amc_audio_dec_stop (GstAudioDecoder * decoder);
|
|
|
|
static gboolean gst_amc_audio_dec_set_format (GstAudioDecoder * decoder,
|
|
|
|
GstCaps * caps);
|
|
|
|
static void gst_amc_audio_dec_flush (GstAudioDecoder * decoder, gboolean hard);
|
|
|
|
static GstFlowReturn gst_amc_audio_dec_handle_frame (GstAudioDecoder * decoder,
|
|
|
|
GstBuffer * buffer);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_amc_audio_dec_drain (GstAmcAudioDec * self);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* class initialization */
|
|
|
|
|
2013-04-16 13:31:34 +00:00
|
|
|
static void gst_amc_audio_dec_class_init (GstAmcAudioDecClass * klass);
|
|
|
|
static void gst_amc_audio_dec_init (GstAmcAudioDec * self);
|
|
|
|
static void gst_amc_audio_dec_base_init (gpointer g_class);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2013-04-16 13:31:34 +00:00
|
|
|
static GstAudioDecoderClass *parent_class = NULL;
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_amc_audio_dec_get_type (void)
|
|
|
|
{
|
|
|
|
static volatile gsize type = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&type)) {
|
|
|
|
GType _type;
|
|
|
|
static const GTypeInfo info = {
|
|
|
|
sizeof (GstAmcAudioDecClass),
|
|
|
|
gst_amc_audio_dec_base_init,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_amc_audio_dec_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstAmcAudioDec),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_amc_audio_dec_init,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
_type = g_type_register_static (GST_TYPE_AUDIO_DECODER, "GstAmcAudioDec",
|
|
|
|
&info, 0);
|
|
|
|
|
2013-05-20 11:24:07 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_amc_audio_dec_debug_category, "amcaudiodec", 0,
|
|
|
|
"Android MediaCodec audio decoder");
|
2013-04-16 13:31:34 +00:00
|
|
|
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
caps_to_mime (GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
const gchar *name;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
name = gst_structure_get_name (s);
|
|
|
|
|
|
|
|
if (strcmp (name, "audio/mpeg") == 0) {
|
|
|
|
gint mpegversion;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (s, "mpegversion", &mpegversion))
|
|
|
|
return NULL;
|
|
|
|
|
2012-09-24 13:32:44 +00:00
|
|
|
if (mpegversion == 1) {
|
|
|
|
gint layer;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (s, "layer", &layer) || layer == 3)
|
|
|
|
return "audio/mpeg";
|
|
|
|
else if (layer == 2)
|
|
|
|
return "audio/mpeg-L2";
|
|
|
|
} else if (mpegversion == 2 || mpegversion == 4) {
|
2012-09-14 11:05:15 +00:00
|
|
|
return "audio/mp4a-latm";
|
2012-09-24 13:32:44 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
} else if (strcmp (name, "audio/AMR") == 0) {
|
|
|
|
return "audio/3gpp";
|
|
|
|
} else if (strcmp (name, "audio/AMR-WB") == 0) {
|
|
|
|
return "audio/amr-wb";
|
|
|
|
} else if (strcmp (name, "audio/x-alaw") == 0) {
|
|
|
|
return "audio/g711-alaw";
|
|
|
|
} else if (strcmp (name, "audio/x-mulaw") == 0) {
|
|
|
|
return "audio/g711-mlaw";
|
|
|
|
} else if (strcmp (name, "audio/x-vorbis") == 0) {
|
2012-09-17 09:28:58 +00:00
|
|
|
return "audio/vorbis";
|
2017-01-05 13:05:07 +00:00
|
|
|
} else if (strcmp (name, "audio/x-opus") == 0) {
|
|
|
|
return "audio/opus";
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-16 13:31:34 +00:00
|
|
|
gst_amc_audio_dec_base_init (gpointer g_class)
|
2012-09-14 11:05:15 +00:00
|
|
|
{
|
2013-04-16 13:31:34 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
GstAmcAudioDecClass *amcaudiodec_class = GST_AMC_AUDIO_DEC_CLASS (g_class);
|
2012-09-14 11:05:15 +00:00
|
|
|
const GstAmcCodecInfo *codec_info;
|
|
|
|
GstPadTemplate *templ;
|
2013-10-08 13:11:48 +00:00
|
|
|
GstCaps *sink_caps, *src_caps;
|
2012-09-14 11:05:15 +00:00
|
|
|
gchar *longname;
|
|
|
|
|
|
|
|
codec_info =
|
2013-04-16 13:31:34 +00:00
|
|
|
g_type_get_qdata (G_TYPE_FROM_CLASS (g_class), gst_amc_codec_info_quark);
|
2012-09-14 11:05:15 +00:00
|
|
|
/* This happens for the base class and abstract subclasses */
|
|
|
|
if (!codec_info)
|
|
|
|
return;
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
amcaudiodec_class->codec_info = codec_info;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2013-10-08 13:11:48 +00:00
|
|
|
gst_amc_codec_info_to_caps (codec_info, &sink_caps, &src_caps);
|
2012-09-14 11:05:15 +00:00
|
|
|
/* Add pad templates */
|
2013-10-08 13:11:48 +00:00
|
|
|
templ =
|
|
|
|
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, sink_caps);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, templ);
|
2013-10-08 13:11:48 +00:00
|
|
|
gst_caps_unref (sink_caps);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2013-10-08 13:11:48 +00:00
|
|
|
templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, src_caps);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_element_class_add_pad_template (element_class, templ);
|
2013-10-08 13:11:48 +00:00
|
|
|
gst_caps_unref (src_caps);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
longname = g_strdup_printf ("Android MediaCodec %s", codec_info->name);
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_element_class_set_metadata (element_class,
|
2012-09-14 11:05:15 +00:00
|
|
|
codec_info->name,
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
longname, "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
|
|
|
g_free (longname);
|
|
|
|
}
|
|
|
|
|
2013-04-16 13:31:34 +00:00
|
|
|
static void
|
|
|
|
gst_amc_audio_dec_class_init (GstAmcAudioDecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstAudioDecoderClass *audiodec_class = GST_AUDIO_DECODER_CLASS (klass);
|
|
|
|
|
2013-04-16 14:17:50 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2013-04-16 13:31:34 +00:00
|
|
|
gobject_class->finalize = gst_amc_audio_dec_finalize;
|
|
|
|
|
|
|
|
element_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_amc_audio_dec_change_state);
|
|
|
|
|
|
|
|
audiodec_class->start = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_start);
|
|
|
|
audiodec_class->stop = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_stop);
|
|
|
|
audiodec_class->open = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_open);
|
|
|
|
audiodec_class->close = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_close);
|
|
|
|
audiodec_class->flush = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_flush);
|
|
|
|
audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_amc_audio_dec_set_format);
|
|
|
|
audiodec_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_amc_audio_dec_handle_frame);
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
static void
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_amc_audio_dec_init (GstAmcAudioDec * self)
|
2012-09-14 11:05:15 +00:00
|
|
|
{
|
|
|
|
gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (self), TRUE);
|
|
|
|
gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (self), TRUE);
|
|
|
|
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_init (&self->drain_lock);
|
|
|
|
g_cond_init (&self->drain_cond);
|
2015-05-19 15:02:30 +00:00
|
|
|
self->output_adapter = gst_adapter_new ();
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_open (GstAudioDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
GstAmcAudioDecClass *klass = GST_AMC_AUDIO_DEC_GET_CLASS (self);
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Opening decoder");
|
|
|
|
|
2019-03-26 15:24:58 +00:00
|
|
|
self->codec = gst_amc_codec_new (klass->codec_info->name, FALSE, &err);
|
2014-05-30 14:35:30 +00:00
|
|
|
if (!self->codec) {
|
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
return FALSE;
|
2014-05-30 14:35:30 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
self->flushing = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Opened decoder");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_close (GstAudioDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Closing decoder");
|
|
|
|
|
2014-05-30 10:40:08 +00:00
|
|
|
if (self->codec) {
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
gst_amc_codec_release (self->codec, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_amc_codec_free (self->codec);
|
2014-05-30 10:40:08 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
self->codec = NULL;
|
|
|
|
|
|
|
|
self->started = FALSE;
|
|
|
|
self->flushing = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Closed decoder");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amc_audio_dec_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self = GST_AMC_AUDIO_DEC (object);
|
|
|
|
|
2015-05-19 15:02:30 +00:00
|
|
|
if (self->output_adapter)
|
|
|
|
gst_object_unref (self->output_adapter);
|
|
|
|
self->output_adapter = NULL;
|
|
|
|
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_clear (&self->drain_lock);
|
|
|
|
g_cond_clear (&self->drain_cond);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_amc_audio_dec_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
|
|
|
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_AMC_AUDIO_DEC (element),
|
|
|
|
GST_STATE_CHANGE_FAILURE);
|
|
|
|
self = GST_AMC_AUDIO_DEC (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
|
|
|
self->draining = FALSE;
|
|
|
|
self->started = FALSE;
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
self->flushing = TRUE;
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_codec_flush (self->codec, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
self->draining = FALSE;
|
2012-11-12 10:32:44 +00:00
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2012-10-18 14:41:07 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_FLUSHING;
|
2012-09-14 11:05:15 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_set_src_caps (GstAmcAudioDec * self, GstAmcFormat * format)
|
|
|
|
{
|
|
|
|
gint rate, channels;
|
|
|
|
guint32 channel_mask = 0;
|
2012-10-18 14:41:07 +00:00
|
|
|
GstAudioChannelPosition to[64];
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
if (!gst_amc_format_get_int (format, "sample-rate", &rate, &err) ||
|
|
|
|
!gst_amc_format_get_int (format, "channel-count", &channels, &err)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to get output format metadata: %s",
|
|
|
|
err->message);
|
|
|
|
g_clear_error (&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-09 08:14:50 +00:00
|
|
|
if (rate == 0 || channels == 0) {
|
|
|
|
GST_ERROR_OBJECT (self, "Rate or channels not set");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
/* Not always present */
|
2018-11-13 18:16:34 +00:00
|
|
|
gst_amc_format_get_int (format, "channel-mask", (gint *) & channel_mask,
|
|
|
|
NULL);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_amc_audio_channel_mask_to_positions (channel_mask, channels,
|
|
|
|
self->positions);
|
|
|
|
memcpy (to, self->positions, sizeof (to));
|
|
|
|
gst_audio_channel_positions_to_valid_order (to, channels);
|
|
|
|
self->needs_reorder =
|
|
|
|
(memcmp (self->positions, to,
|
|
|
|
sizeof (GstAudioChannelPosition) * channels) != 0);
|
|
|
|
if (self->needs_reorder)
|
|
|
|
gst_audio_get_channel_reorder_map (channels, self->positions, to,
|
|
|
|
self->reorder_map);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_audio_info_init (&self->info);
|
|
|
|
gst_audio_info_set_format (&self->info, GST_AUDIO_FORMAT_S16, rate, channels,
|
|
|
|
to);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2013-05-20 11:24:07 +00:00
|
|
|
if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (self),
|
|
|
|
&self->info))
|
2013-04-16 14:23:46 +00:00
|
|
|
return FALSE;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
self->input_caps_changed = FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amc_audio_dec_loop (GstAmcAudioDec * self)
|
|
|
|
{
|
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
|
|
|
gboolean is_eos;
|
2015-05-27 21:34:14 +00:00
|
|
|
GstAmcBuffer *buf;
|
2012-09-14 11:05:15 +00:00
|
|
|
GstAmcBufferInfo buffer_info;
|
|
|
|
gint idx;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
|
|
|
|
retry:
|
|
|
|
/*if (self->input_caps_changed) {
|
|
|
|
idx = INFO_OUTPUT_FORMAT_CHANGED;
|
|
|
|
} else { */
|
|
|
|
GST_DEBUG_OBJECT (self, "Waiting for available output buffer");
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2012-09-14 14:17:51 +00:00
|
|
|
/* Wait at most 100ms here, some codecs don't fail dequeueing if
|
|
|
|
* the codec is flushing, causing deadlocks during shutdown */
|
2014-05-30 14:35:30 +00:00
|
|
|
idx =
|
|
|
|
gst_amc_codec_dequeue_output_buffer (self->codec, &buffer_info, 100000,
|
|
|
|
&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
/*} */
|
|
|
|
|
|
|
|
if (idx < 0) {
|
2014-05-30 14:35:30 +00:00
|
|
|
if (self->flushing) {
|
|
|
|
g_clear_error (&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
goto flushing;
|
2014-05-30 14:35:30 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
switch (idx) {
|
2015-05-27 21:34:14 +00:00
|
|
|
case INFO_OUTPUT_BUFFERS_CHANGED:
|
|
|
|
/* Handled internally */
|
|
|
|
g_assert_not_reached ();
|
2012-09-14 11:05:15 +00:00
|
|
|
break;
|
|
|
|
case INFO_OUTPUT_FORMAT_CHANGED:{
|
|
|
|
GstAmcFormat *format;
|
|
|
|
gchar *format_string;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Output format has changed");
|
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
format = gst_amc_codec_get_output_format (self->codec, &err);
|
2012-09-14 11:05:15 +00:00
|
|
|
if (!format)
|
|
|
|
goto format_error;
|
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
format_string = gst_amc_format_to_string (format, &err);
|
|
|
|
if (err) {
|
|
|
|
gst_amc_format_free (format);
|
|
|
|
goto format_error;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Got new output format: %s", format_string);
|
|
|
|
g_free (format_string);
|
|
|
|
|
|
|
|
if (!gst_amc_audio_dec_set_src_caps (self, format)) {
|
|
|
|
gst_amc_format_free (format);
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
gst_amc_format_free (format);
|
|
|
|
|
|
|
|
goto retry;
|
2014-09-19 04:21:48 +00:00
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
case INFO_TRY_AGAIN_LATER:
|
|
|
|
GST_DEBUG_OBJECT (self, "Dequeueing output buffer timed out");
|
|
|
|
goto retry;
|
2014-09-19 04:21:48 +00:00
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
case G_MININT:
|
2012-09-14 14:09:48 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failure dequeueing output buffer");
|
2012-09-14 11:05:15 +00:00
|
|
|
goto dequeue_error;
|
2014-09-19 04:21:48 +00:00
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self,
|
2015-05-27 21:34:14 +00:00
|
|
|
"Got output buffer at index %d: offset %d size %d time %" G_GINT64_FORMAT
|
|
|
|
" flags 0x%08x", idx, buffer_info.offset, buffer_info.size,
|
|
|
|
buffer_info.presentation_time_us, buffer_info.flags);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
is_eos = ! !(buffer_info.flags & BUFFER_FLAG_END_OF_STREAM);
|
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
buf = gst_amc_codec_get_output_buffer (self->codec, idx, &err);
|
2017-12-01 13:02:12 +00:00
|
|
|
if (err) {
|
|
|
|
if (self->flushing) {
|
|
|
|
g_clear_error (&err);
|
|
|
|
goto flushing;
|
|
|
|
}
|
2015-05-27 21:34:14 +00:00
|
|
|
goto failed_to_get_output_buffer;
|
2017-12-01 13:02:12 +00:00
|
|
|
} else if (!buf) {
|
2016-01-22 14:13:45 +00:00
|
|
|
goto got_null_output_buffer;
|
2017-12-01 13:02:12 +00:00
|
|
|
}
|
2015-05-27 21:34:14 +00:00
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
if (buffer_info.size > 0) {
|
|
|
|
GstBuffer *outbuf;
|
2012-10-18 14:41:07 +00:00
|
|
|
GstMapInfo minfo;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
/* This sometimes happens at EOS or if the input is not properly framed,
|
|
|
|
* let's handle it gracefully by allocating a new buffer for the current
|
|
|
|
* caps and filling it
|
|
|
|
*/
|
|
|
|
|
2014-02-14 11:33:08 +00:00
|
|
|
if (buffer_info.size % self->info.bpf != 0)
|
|
|
|
goto invalid_buffer_size;
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
outbuf =
|
|
|
|
gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (self),
|
|
|
|
buffer_info.size);
|
2012-09-14 11:05:15 +00:00
|
|
|
if (!outbuf)
|
|
|
|
goto failed_allocate;
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_map (outbuf, &minfo, GST_MAP_WRITE);
|
|
|
|
if (self->needs_reorder) {
|
|
|
|
gint i, n_samples, c, n_channels;
|
|
|
|
gint *reorder_map = self->reorder_map;
|
|
|
|
gint16 *dest, *source;
|
|
|
|
|
|
|
|
dest = (gint16 *) minfo.data;
|
|
|
|
source = (gint16 *) (buf->data + buffer_info.offset);
|
|
|
|
n_samples = buffer_info.size / self->info.bpf;
|
|
|
|
n_channels = self->info.channels;
|
|
|
|
|
|
|
|
for (i = 0; i < n_samples; i++) {
|
|
|
|
for (c = 0; c < n_channels; c++) {
|
|
|
|
dest[i * n_channels + reorder_map[c]] = source[i * n_channels + c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
orc_memcpy (minfo.data, buf->data + buffer_info.offset, buffer_info.size);
|
|
|
|
}
|
|
|
|
gst_buffer_unmap (outbuf, &minfo);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2014-02-14 11:39:06 +00:00
|
|
|
if (self->spf != -1) {
|
2015-05-19 15:02:30 +00:00
|
|
|
gst_adapter_push (self->output_adapter, outbuf);
|
|
|
|
} else {
|
|
|
|
flow_ret =
|
|
|
|
gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (self), outbuf, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
gst_amc_buffer_free (buf);
|
|
|
|
buf = NULL;
|
|
|
|
|
2015-05-19 15:02:30 +00:00
|
|
|
if (self->spf != -1) {
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
guint avail = gst_adapter_available (self->output_adapter);
|
|
|
|
guint nframes;
|
|
|
|
|
|
|
|
/* On EOS we take the complete adapter content, no matter
|
|
|
|
* if it is a multiple of the codec frame size or not.
|
|
|
|
* Otherwise we take a multiple of codec frames and push
|
|
|
|
* them downstream
|
|
|
|
*/
|
|
|
|
avail /= self->info.bpf;
|
|
|
|
if (!is_eos) {
|
|
|
|
nframes = avail / self->spf;
|
|
|
|
avail = nframes * self->spf;
|
|
|
|
} else {
|
|
|
|
nframes = (avail + self->spf - 1) / self->spf;
|
2014-02-14 11:39:06 +00:00
|
|
|
}
|
2015-05-19 15:02:30 +00:00
|
|
|
avail *= self->info.bpf;
|
2014-02-14 11:39:06 +00:00
|
|
|
|
2015-05-19 15:02:30 +00:00
|
|
|
if (avail > 0) {
|
|
|
|
outbuf = gst_adapter_take_buffer (self->output_adapter, avail);
|
|
|
|
flow_ret =
|
|
|
|
gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (self), outbuf,
|
|
|
|
nframes);
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 08:33:56 +00:00
|
|
|
if (!gst_amc_codec_release_output_buffer (self->codec, idx, FALSE, &err)) {
|
2014-06-19 09:12:47 +00:00
|
|
|
if (self->flushing) {
|
|
|
|
g_clear_error (&err);
|
|
|
|
goto flushing;
|
|
|
|
}
|
2012-09-14 14:11:33 +00:00
|
|
|
goto failed_release;
|
2014-06-19 09:12:47 +00:00
|
|
|
}
|
2012-09-14 14:11:33 +00:00
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
if (is_eos || flow_ret == GST_FLOW_EOS) {
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
if (self->draining) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Drained");
|
|
|
|
self->draining = FALSE;
|
2012-11-12 10:32:44 +00:00
|
|
|
g_cond_broadcast (&self->drain_cond);
|
2012-09-14 11:05:15 +00:00
|
|
|
} else if (flow_ret == GST_FLOW_OK) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Component signalled EOS");
|
2012-10-18 14:41:07 +00:00
|
|
|
flow_ret = GST_FLOW_EOS;
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
self->downstream_flow_ret = flow_ret;
|
|
|
|
|
|
|
|
if (flow_ret != GST_FLOW_OK)
|
|
|
|
goto flow_error;
|
|
|
|
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
dequeue_error:
|
|
|
|
{
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_error:
|
|
|
|
{
|
2014-05-30 14:35:30 +00:00
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
|
|
|
else
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
|
|
|
|
("Failed to handle format"));
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
failed_release:
|
|
|
|
{
|
2014-07-17 08:53:30 +00:00
|
|
|
GST_AUDIO_DECODER_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
flushing:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
2012-10-18 14:41:07 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_FLUSHING;
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
flow_error:
|
|
|
|
{
|
2012-10-18 14:41:07 +00:00
|
|
|
if (flow_ret == GST_FLOW_EOS) {
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "EOS");
|
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self),
|
|
|
|
gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
2014-01-25 16:40:46 +00:00
|
|
|
} else if (flow_ret < GST_FLOW_EOS) {
|
2016-08-19 18:12:10 +00:00
|
|
|
GST_ELEMENT_FLOW_ERROR (self, flow_ret);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self),
|
|
|
|
gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
2014-01-25 16:40:46 +00:00
|
|
|
} else if (flow_ret == GST_FLOW_FLUSHING) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
failed_to_get_output_buffer:
|
2012-09-14 11:05:15 +00:00
|
|
|
{
|
2015-05-27 21:34:14 +00:00
|
|
|
GST_AUDIO_DECODER_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-22 14:13:45 +00:00
|
|
|
|
|
|
|
got_null_output_buffer:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
|
|
|
|
("Got no output buffer"));
|
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:33:08 +00:00
|
|
|
invalid_buffer_size:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
|
|
|
|
("Invalid buffer size %u (bfp %d)", buffer_info.size, self->info.bpf));
|
2014-06-05 08:33:56 +00:00
|
|
|
gst_amc_codec_release_output_buffer (self->codec, idx, FALSE, &err);
|
2014-06-19 09:26:41 +00:00
|
|
|
if (err && !self->flushing)
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2014-06-19 09:26:41 +00:00
|
|
|
g_clear_error (&err);
|
2014-02-14 11:33:08 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2014-02-14 11:33:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
failed_allocate:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
|
|
|
|
("Failed to allocate output buffer"));
|
2014-06-05 08:33:56 +00:00
|
|
|
gst_amc_codec_release_output_buffer (self->codec, idx, FALSE, &err);
|
2014-06-19 09:26:41 +00:00
|
|
|
if (err && !self->flushing)
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2014-06-19 09:26:41 +00:00
|
|
|
g_clear_error (&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (self), gst_event_new_eos ());
|
|
|
|
gst_pad_pause_task (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
self->downstream_flow_ret = GST_FLOW_ERROR;
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2014-08-14 12:26:53 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_start (GstAudioDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
|
|
|
|
|
|
|
self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
self->last_upstream_ts = 0;
|
2014-08-14 12:24:21 +00:00
|
|
|
self->drained = TRUE;
|
2012-09-14 11:05:15 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
|
|
|
self->started = FALSE;
|
|
|
|
self->flushing = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_stop (GstAudioDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
GST_DEBUG_OBJECT (self, "Stopping decoder");
|
2012-10-09 08:56:30 +00:00
|
|
|
self->flushing = TRUE;
|
2012-09-14 11:05:15 +00:00
|
|
|
if (self->started) {
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_codec_flush (self->codec, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
gst_amc_codec_stop (self->codec, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
self->started = FALSE;
|
|
|
|
}
|
2012-10-09 08:56:30 +00:00
|
|
|
gst_pad_stop_task (GST_AUDIO_DECODER_SRC_PAD (decoder));
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
memset (self->positions, 0, sizeof (self->positions));
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2015-05-19 15:02:30 +00:00
|
|
|
gst_adapter_flush (self->output_adapter,
|
|
|
|
gst_adapter_available (self->output_adapter));
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
g_list_foreach (self->codec_datas, (GFunc) g_free, NULL);
|
2012-09-14 11:05:15 +00:00
|
|
|
g_list_free (self->codec_datas);
|
|
|
|
self->codec_datas = NULL;
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_FLUSHING;
|
2014-08-14 12:24:21 +00:00
|
|
|
self->drained = TRUE;
|
2012-11-12 10:32:44 +00:00
|
|
|
g_mutex_lock (&self->drain_lock);
|
2012-09-14 11:05:15 +00:00
|
|
|
self->draining = FALSE;
|
2012-11-12 10:32:44 +00:00
|
|
|
g_cond_broadcast (&self->drain_cond);
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
2012-10-18 14:41:07 +00:00
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Stopped decoder");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amc_audio_dec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
|
|
|
GstStructure *s;
|
|
|
|
GstAmcFormat *format;
|
|
|
|
const gchar *mime;
|
|
|
|
gboolean is_format_change = FALSE;
|
|
|
|
gboolean needs_disable = FALSE;
|
|
|
|
gchar *format_string;
|
|
|
|
gint rate, channels;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Setting new caps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
/* Check if the caps change is a real format change or if only irrelevant
|
|
|
|
* parts of the caps have changed or nothing at all.
|
|
|
|
*/
|
|
|
|
is_format_change |= (!self->input_caps
|
|
|
|
|| !gst_caps_is_equal (self->input_caps, caps));
|
|
|
|
|
|
|
|
needs_disable = self->started;
|
|
|
|
|
|
|
|
/* If the component is not started and a real format change happens
|
|
|
|
* we have to restart the component. If no real format change
|
|
|
|
* happened we can just exit here.
|
|
|
|
*/
|
|
|
|
if (needs_disable && !is_format_change) {
|
|
|
|
/* Framerate or something minor changed */
|
|
|
|
self->input_caps_changed = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"Already running and caps did not change the format");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needs_disable && is_format_change) {
|
|
|
|
gst_amc_audio_dec_drain (self);
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
gst_amc_audio_dec_stop (GST_AUDIO_DECODER (self));
|
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
2012-10-09 08:56:30 +00:00
|
|
|
gst_amc_audio_dec_close (GST_AUDIO_DECODER (self));
|
|
|
|
if (!gst_amc_audio_dec_open (GST_AUDIO_DECODER (self))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to open codec again");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_amc_audio_dec_start (GST_AUDIO_DECODER (self))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to start codec again");
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
/* srcpad task is not running at this point */
|
|
|
|
|
|
|
|
mime = caps_to_mime (caps);
|
|
|
|
if (!mime) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to convert caps to mime");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
if (!gst_structure_get_int (s, "rate", &rate) ||
|
|
|
|
!gst_structure_get_int (s, "channels", &channels)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to get rate/channels");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
format = gst_amc_format_new_audio (mime, rate, channels, &err);
|
2012-09-14 11:05:15 +00:00
|
|
|
if (!format) {
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-15 12:05:09 +00:00
|
|
|
if (gst_structure_has_name (s, "audio/mpeg")) {
|
|
|
|
gint mpegversion;
|
|
|
|
const gchar *stream_format;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (s, "mpegversion", &mpegversion))
|
|
|
|
mpegversion = -1;
|
|
|
|
stream_format = gst_structure_get_string (s, "stream-format");
|
|
|
|
|
|
|
|
if (mpegversion == 4 && g_strcmp0 (stream_format, "adts") == 0) {
|
|
|
|
gst_amc_format_set_int (format, "is-adts", 1, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
/* FIXME: These buffers needs to be valid until the codec is stopped again */
|
|
|
|
g_list_foreach (self->codec_datas, (GFunc) gst_buffer_unref, NULL);
|
|
|
|
g_list_free (self->codec_datas);
|
|
|
|
self->codec_datas = NULL;
|
|
|
|
if (gst_structure_has_field (s, "codec_data")) {
|
|
|
|
const GValue *h = gst_structure_get_value (s, "codec_data");
|
|
|
|
GstBuffer *codec_data = gst_value_get_buffer (h);
|
2012-10-18 14:41:07 +00:00
|
|
|
GstMapInfo minfo;
|
|
|
|
guint8 *data;
|
|
|
|
|
|
|
|
gst_buffer_map (codec_data, &minfo, GST_MAP_READ);
|
|
|
|
data = g_memdup (minfo.data, minfo.size);
|
|
|
|
self->codec_datas = g_list_prepend (self->codec_datas, data);
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_format_set_buffer (format, "csd-0", data, minfo.size, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_unmap (codec_data, &minfo);
|
2012-09-14 11:05:15 +00:00
|
|
|
} else if (gst_structure_has_field (s, "streamheader")) {
|
|
|
|
const GValue *sh = gst_structure_get_value (s, "streamheader");
|
|
|
|
gint nsheaders = gst_value_array_get_size (sh);
|
|
|
|
GstBuffer *buf;
|
|
|
|
const GValue *h;
|
2012-09-17 09:28:58 +00:00
|
|
|
gint i, j;
|
2012-09-14 11:05:15 +00:00
|
|
|
gchar *fname;
|
2012-10-18 14:41:07 +00:00
|
|
|
GstMapInfo minfo;
|
|
|
|
guint8 *data;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2012-09-17 09:28:58 +00:00
|
|
|
for (i = 0, j = 0; i < nsheaders; i++) {
|
2012-09-14 11:05:15 +00:00
|
|
|
h = gst_value_array_get_value (sh, i);
|
|
|
|
buf = gst_value_get_buffer (h);
|
|
|
|
|
2012-09-17 09:28:58 +00:00
|
|
|
if (strcmp (mime, "audio/vorbis") == 0) {
|
2012-10-18 14:41:07 +00:00
|
|
|
guint8 header_type;
|
|
|
|
|
|
|
|
gst_buffer_extract (buf, 0, &header_type, 1);
|
2012-09-17 09:28:58 +00:00
|
|
|
|
|
|
|
/* Only use the identification and setup packets */
|
|
|
|
if (header_type != 0x01 && header_type != 0x05)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fname = g_strdup_printf ("csd-%d", j);
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_map (buf, &minfo, GST_MAP_READ);
|
|
|
|
data = g_memdup (minfo.data, minfo.size);
|
|
|
|
self->codec_datas = g_list_prepend (self->codec_datas, data);
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_format_set_buffer (format, fname, data, minfo.size, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_unmap (buf, &minfo);
|
2012-09-14 11:05:15 +00:00
|
|
|
g_free (fname);
|
2012-09-17 09:28:58 +00:00
|
|
|
j++;
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
format_string = gst_amc_format_to_string (format, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
GST_DEBUG_OBJECT (self, "Configuring codec with format: %s",
|
|
|
|
GST_STR_NULL (format_string));
|
2012-09-14 11:05:15 +00:00
|
|
|
g_free (format_string);
|
|
|
|
|
2019-03-26 15:24:58 +00:00
|
|
|
if (!gst_amc_codec_configure (self->codec, format, NULL, &err)) {
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to configure codec");
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_amc_format_free (format);
|
|
|
|
|
2014-05-30 14:35:30 +00:00
|
|
|
if (!gst_amc_codec_start (self->codec, &err)) {
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to start codec");
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:39:06 +00:00
|
|
|
self->spf = -1;
|
|
|
|
/* TODO: Implement for other codecs too */
|
|
|
|
if (gst_structure_has_name (s, "audio/mpeg")) {
|
|
|
|
gint mpegversion = -1;
|
|
|
|
|
|
|
|
gst_structure_get_int (s, "mpegversion", &mpegversion);
|
|
|
|
if (mpegversion == 1) {
|
|
|
|
gint layer = -1, mpegaudioversion = -1;
|
|
|
|
|
|
|
|
gst_structure_get_int (s, "layer", &layer);
|
|
|
|
gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion);
|
|
|
|
if (layer == 1)
|
|
|
|
self->spf = 384;
|
|
|
|
else if (layer == 2)
|
|
|
|
self->spf = 1152;
|
|
|
|
else if (layer == 3 && mpegaudioversion != -1)
|
|
|
|
self->spf = (mpegaudioversion == 1 ? 1152 : 576);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
self->started = TRUE;
|
|
|
|
self->input_caps_changed = TRUE;
|
|
|
|
|
|
|
|
/* Start the srcpad loop again */
|
|
|
|
self->flushing = FALSE;
|
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
|
|
|
gst_pad_start_task (GST_AUDIO_DECODER_SRC_PAD (self),
|
2012-10-18 14:41:07 +00:00
|
|
|
(GstTaskFunction) gst_amc_audio_dec_loop, decoder, NULL);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amc_audio_dec_flush (GstAudioDecoder * decoder, gboolean hard)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Resetting decoder");
|
|
|
|
|
|
|
|
if (!self->started) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Codec not started yet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->flushing = TRUE;
|
|
|
|
/* Wait until the srcpad loop is finished,
|
|
|
|
* unlock GST_AUDIO_DECODER_STREAM_LOCK to prevent deadlocks
|
|
|
|
* caused by using this lock from inside the loop function */
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
GST_PAD_STREAM_LOCK (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
GST_PAD_STREAM_UNLOCK (GST_AUDIO_DECODER_SRC_PAD (self));
|
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_codec_flush (self->codec, &err);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2015-05-19 15:02:30 +00:00
|
|
|
gst_adapter_flush (self->output_adapter,
|
|
|
|
gst_adapter_available (self->output_adapter));
|
2012-09-17 11:46:15 +00:00
|
|
|
self->flushing = FALSE;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
/* Start the srcpad loop again */
|
|
|
|
self->last_upstream_ts = 0;
|
2014-08-14 12:24:21 +00:00
|
|
|
self->drained = TRUE;
|
2012-09-14 11:05:15 +00:00
|
|
|
self->downstream_flow_ret = GST_FLOW_OK;
|
|
|
|
gst_pad_start_task (GST_AUDIO_DECODER_SRC_PAD (self),
|
2012-10-18 14:41:07 +00:00
|
|
|
(GstTaskFunction) gst_amc_audio_dec_loop, decoder, NULL);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Reset decoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_amc_audio_dec_handle_frame (GstAudioDecoder * decoder, GstBuffer * inbuf)
|
|
|
|
{
|
|
|
|
GstAmcAudioDec *self;
|
|
|
|
gint idx;
|
|
|
|
GstAmcBuffer *buf;
|
|
|
|
GstAmcBufferInfo buffer_info;
|
|
|
|
guint offset = 0;
|
|
|
|
GstClockTime timestamp, duration, timestamp_offset = 0;
|
2012-10-18 14:41:07 +00:00
|
|
|
GstMapInfo minfo;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-10-18 14:41:07 +00:00
|
|
|
|
|
|
|
memset (&minfo, 0, sizeof (minfo));
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
self = GST_AMC_AUDIO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Handling frame");
|
|
|
|
|
2012-09-14 14:11:04 +00:00
|
|
|
/* Make sure to keep a reference to the input here,
|
|
|
|
* it can be unreffed from the other thread if
|
|
|
|
* finish_frame() is called */
|
|
|
|
if (inbuf)
|
|
|
|
inbuf = gst_buffer_ref (inbuf);
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
if (!self->started) {
|
|
|
|
GST_ERROR_OBJECT (self, "Codec not started yet");
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->flushing)
|
|
|
|
goto flushing;
|
|
|
|
|
|
|
|
if (self->downstream_flow_ret != GST_FLOW_OK)
|
|
|
|
goto downstream_error;
|
|
|
|
|
|
|
|
if (!inbuf)
|
|
|
|
return gst_amc_audio_dec_drain (self);
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
timestamp = GST_BUFFER_PTS (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
duration = GST_BUFFER_DURATION (inbuf);
|
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_map (inbuf, &minfo, GST_MAP_READ);
|
|
|
|
|
|
|
|
while (offset < minfo.size) {
|
2012-09-14 11:05:15 +00:00
|
|
|
/* Make sure to release the base class stream lock, otherwise
|
|
|
|
* _loop() can't call _finish_frame() and we might block forever
|
|
|
|
* because no input buffers are released */
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
2012-09-14 14:17:51 +00:00
|
|
|
/* Wait at most 100ms here, some codecs don't fail dequeueing if
|
|
|
|
* the codec is flushing, causing deadlocks during shutdown */
|
2014-05-30 14:35:30 +00:00
|
|
|
idx = gst_amc_codec_dequeue_input_buffer (self->codec, 100000, &err);
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
2014-06-19 09:12:47 +00:00
|
|
|
if (self->flushing || self->downstream_flow_ret == GST_FLOW_FLUSHING) {
|
2014-05-30 14:35:30 +00:00
|
|
|
g_clear_error (&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
goto flushing;
|
2014-05-30 14:35:30 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
switch (idx) {
|
|
|
|
case INFO_TRY_AGAIN_LATER:
|
|
|
|
GST_DEBUG_OBJECT (self, "Dequeueing input buffer timed out");
|
|
|
|
continue; /* next try */
|
|
|
|
break;
|
|
|
|
case G_MININT:
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to dequeue input buffer");
|
|
|
|
goto dequeue_error;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-19 09:12:47 +00:00
|
|
|
if (self->flushing) {
|
|
|
|
memset (&buffer_info, 0, sizeof (buffer_info));
|
|
|
|
gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, NULL);
|
2012-09-14 11:05:15 +00:00
|
|
|
goto flushing;
|
2014-06-19 09:12:47 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
if (self->downstream_flow_ret != GST_FLOW_OK) {
|
|
|
|
memset (&buffer_info, 0, sizeof (buffer_info));
|
2014-05-30 14:35:30 +00:00
|
|
|
gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info, &err);
|
2014-06-19 09:26:41 +00:00
|
|
|
if (err && !self->flushing)
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2014-06-19 09:26:41 +00:00
|
|
|
g_clear_error (&err);
|
2012-09-14 11:05:15 +00:00
|
|
|
goto downstream_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now handle the frame */
|
|
|
|
|
|
|
|
/* Copy the buffer content in chunks of size as requested
|
|
|
|
* by the port */
|
2015-05-27 21:34:14 +00:00
|
|
|
buf = gst_amc_codec_get_input_buffer (self->codec, idx, &err);
|
2016-03-11 08:08:28 +00:00
|
|
|
if (err)
|
2015-05-27 21:34:14 +00:00
|
|
|
goto failed_to_get_input_buffer;
|
2016-03-11 08:08:28 +00:00
|
|
|
else if (!buf)
|
|
|
|
goto got_null_input_buffer;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
memset (&buffer_info, 0, sizeof (buffer_info));
|
|
|
|
buffer_info.offset = 0;
|
2012-10-18 14:41:07 +00:00
|
|
|
buffer_info.size = MIN (minfo.size - offset, buf->size);
|
2015-05-27 21:34:14 +00:00
|
|
|
gst_amc_buffer_set_position_and_limit (buf, NULL, buffer_info.offset,
|
|
|
|
buffer_info.size);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2012-10-18 14:41:07 +00:00
|
|
|
orc_memcpy (buf->data, minfo.data + offset, buffer_info.size);
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
gst_amc_buffer_free (buf);
|
|
|
|
buf = NULL;
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
/* Interpolate timestamps if we're passing the buffer
|
|
|
|
* in multiple chunks */
|
|
|
|
if (offset != 0 && duration != GST_CLOCK_TIME_NONE) {
|
2012-10-18 14:41:07 +00:00
|
|
|
timestamp_offset = gst_util_uint64_scale (offset, duration, minfo.size);
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timestamp != GST_CLOCK_TIME_NONE) {
|
|
|
|
buffer_info.presentation_time_us =
|
|
|
|
gst_util_uint64_scale (timestamp + timestamp_offset, 1, GST_USECOND);
|
|
|
|
self->last_upstream_ts = timestamp + timestamp_offset;
|
|
|
|
}
|
|
|
|
if (duration != GST_CLOCK_TIME_NONE)
|
|
|
|
self->last_upstream_ts += duration;
|
|
|
|
|
|
|
|
if (offset == 0) {
|
|
|
|
if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DELTA_UNIT))
|
|
|
|
buffer_info.flags |= BUFFER_FLAG_SYNC_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += buffer_info.size;
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"Queueing buffer %d: size %d time %" G_GINT64_FORMAT " flags 0x%08x",
|
|
|
|
idx, buffer_info.size, buffer_info.presentation_time_us,
|
|
|
|
buffer_info.flags);
|
2014-05-30 14:35:30 +00:00
|
|
|
if (!gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info,
|
2014-06-19 09:15:56 +00:00
|
|
|
&err)) {
|
|
|
|
if (self->flushing) {
|
|
|
|
g_clear_error (&err);
|
|
|
|
goto flushing;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
goto queue_error;
|
2014-06-19 09:15:56 +00:00
|
|
|
}
|
2014-08-14 12:24:21 +00:00
|
|
|
self->drained = FALSE;
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
2012-10-18 14:41:07 +00:00
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
gst_buffer_unref (inbuf);
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
return self->downstream_flow_ret;
|
|
|
|
|
|
|
|
downstream_error:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (self, "Downstream returned %s",
|
|
|
|
gst_flow_get_name (self->downstream_flow_ret));
|
2012-10-18 14:41:07 +00:00
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
return self->downstream_flow_ret;
|
|
|
|
}
|
2015-05-27 21:34:14 +00:00
|
|
|
failed_to_get_input_buffer:
|
2012-09-14 11:05:15 +00:00
|
|
|
{
|
2015-05-27 21:34:14 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-10-18 14:41:07 +00:00
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2016-03-11 08:08:28 +00:00
|
|
|
got_null_input_buffer:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
|
|
|
|
("Got no input buffer"));
|
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
dequeue_error:
|
|
|
|
{
|
2014-05-30 14:35:30 +00:00
|
|
|
GST_ELEMENT_ERROR_FROM_ERROR (self, err);
|
2012-10-18 14:41:07 +00:00
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
queue_error:
|
|
|
|
{
|
2014-07-17 08:53:30 +00:00
|
|
|
GST_AUDIO_DECODER_ERROR_FROM_ERROR (self, err);
|
2012-10-18 14:41:07 +00:00
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-09-14 11:05:15 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
flushing:
|
|
|
|
{
|
2012-10-18 14:41:07 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
|
|
|
|
if (minfo.data)
|
|
|
|
gst_buffer_unmap (inbuf, &minfo);
|
2012-09-14 14:11:04 +00:00
|
|
|
if (inbuf)
|
|
|
|
gst_buffer_unref (inbuf);
|
2012-10-18 14:41:07 +00:00
|
|
|
return GST_FLOW_FLUSHING;
|
2012-09-14 11:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_amc_audio_dec_drain (GstAmcAudioDec * self)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret;
|
|
|
|
gint idx;
|
2014-05-30 14:35:30 +00:00
|
|
|
GError *err = NULL;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Draining codec");
|
|
|
|
if (!self->started) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Codec not started yet");
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-14 12:24:21 +00:00
|
|
|
/* Don't send drain buffer twice, this doesn't work */
|
|
|
|
if (self->drained) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Codec is drained already");
|
2012-09-14 11:05:15 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure to release the base class stream lock, otherwise
|
|
|
|
* _loop() can't call _finish_frame() and we might block forever
|
|
|
|
* because no input buffers are released */
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
/* Send an EOS buffer to the component and let the base
|
|
|
|
* class drop the EOS event. We will send it later when
|
|
|
|
* the EOS buffer arrives on the output port.
|
|
|
|
* Wait at most 0.5s here. */
|
2014-05-30 14:35:30 +00:00
|
|
|
idx = gst_amc_codec_dequeue_input_buffer (self->codec, 500000, &err);
|
2012-09-14 11:05:15 +00:00
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
if (idx >= 0) {
|
|
|
|
GstAmcBuffer *buf;
|
2012-09-14 11:05:15 +00:00
|
|
|
GstAmcBufferInfo buffer_info;
|
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
buf = gst_amc_codec_get_input_buffer (self->codec, idx, &err);
|
|
|
|
if (buf) {
|
|
|
|
GST_AUDIO_DECODER_STREAM_UNLOCK (self);
|
|
|
|
g_mutex_lock (&self->drain_lock);
|
|
|
|
self->draining = TRUE;
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
memset (&buffer_info, 0, sizeof (buffer_info));
|
|
|
|
buffer_info.size = 0;
|
|
|
|
buffer_info.presentation_time_us =
|
|
|
|
gst_util_uint64_scale (self->last_upstream_ts, 1, GST_USECOND);
|
|
|
|
buffer_info.flags |= BUFFER_FLAG_END_OF_STREAM;
|
|
|
|
|
|
|
|
gst_amc_buffer_set_position_and_limit (buf, NULL, 0, 0);
|
|
|
|
gst_amc_buffer_free (buf);
|
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
if (gst_amc_codec_queue_input_buffer (self->codec, idx, &buffer_info,
|
|
|
|
&err)) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Waiting until codec is drained");
|
|
|
|
g_cond_wait (&self->drain_cond, &self->drain_lock);
|
|
|
|
GST_DEBUG_OBJECT (self, "Drained codec");
|
|
|
|
ret = GST_FLOW_OK;
|
2014-06-19 09:26:41 +00:00
|
|
|
} else {
|
2015-05-27 21:34:14 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to queue input buffer");
|
|
|
|
if (self->flushing) {
|
|
|
|
g_clear_error (&err);
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
}
|
2014-06-19 09:26:41 +00:00
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
|
2015-05-27 21:34:14 +00:00
|
|
|
self->drained = TRUE;
|
|
|
|
self->draining = FALSE;
|
|
|
|
g_mutex_unlock (&self->drain_lock);
|
|
|
|
GST_AUDIO_DECODER_STREAM_LOCK (self);
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to get buffer for EOS: %d", idx);
|
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
}
|
2012-09-14 11:05:15 +00:00
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to acquire buffer for EOS: %d", idx);
|
2014-05-30 14:35:30 +00:00
|
|
|
if (err)
|
|
|
|
GST_ELEMENT_WARNING_FROM_ERROR (self, err);
|
2012-09-14 11:05:15 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:02:30 +00:00
|
|
|
gst_adapter_flush (self->output_adapter,
|
|
|
|
gst_adapter_available (self->output_adapter));
|
|
|
|
|
2012-09-14 11:05:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|