omx: Add audio decoder base class and a subclass for MP3

This commit is contained in:
Sebastian Dröge 2014-05-10 23:12:54 +02:00
parent 3d94d3e44d
commit 0c3b3ef3d0
6 changed files with 1676 additions and 1 deletions

View file

@ -16,6 +16,7 @@ libgstomx_la_SOURCES = \
gstomxvideo.c \
gstomxvideodec.c \
gstomxvideoenc.c \
gstomxaudiodec.c \
gstomxaudioenc.c \
gstomxmjpegdec.c \
gstomxmpeg4videodec.c \
@ -28,6 +29,7 @@ libgstomx_la_SOURCES = \
gstomxmpeg4videoenc.c \
gstomxh264enc.c \
gstomxh263enc.c \
gstomxmp3dec.c \
gstomxaacenc.c \
gstomxaudiosink.c \
gstomxanalogaudiosink.c \
@ -38,6 +40,7 @@ noinst_HEADERS = \
gstomxvideo.h \
gstomxvideodec.h \
gstomxvideoenc.h \
gstomxaudiodec.h \
gstomxaudioenc.h \
gstomxmjpegdec.h \
gstomxmpeg2videodec.h \
@ -50,6 +53,7 @@ noinst_HEADERS = \
gstomxmpeg4videoenc.h \
gstomxh264enc.h \
gstomxh263enc.h \
gstomxmp3dec.h \
gstomxaacenc.h \
gstomxaudiosink.h \
gstomxanalogaudiosink.h \

View file

@ -39,6 +39,7 @@
#include "gstomxmpeg4videoenc.h"
#include "gstomxh264enc.h"
#include "gstomxh263enc.h"
#include "gstomxmp3dec.h"
#include "gstomxaacenc.h"
#include "gstomxanalogaudiosink.h"
#include "gstomxhdmiaudiosink.h"
@ -2244,7 +2245,8 @@ static const GGetTypeFunction types[] = {
gst_omx_h264_dec_get_type, gst_omx_h263_dec_get_type,
gst_omx_wmv_dec_get_type, gst_omx_mpeg4_video_enc_get_type,
gst_omx_h264_enc_get_type, gst_omx_h263_enc_get_type,
gst_omx_aac_enc_get_type, gst_omx_mjpeg_dec_get_type
gst_omx_aac_enc_get_type, gst_omx_mjpeg_dec_get_type,
gst_omx_mp3_dec_get_type
#ifdef HAVE_VP8
, gst_omx_vp8_dec_get_type
#endif
@ -2263,6 +2265,7 @@ static const struct TypeOffest base_types[] = {
{gst_omx_audio_sink_get_type, G_STRUCT_OFFSET (GstOMXAudioSinkClass, cdata)},
{gst_omx_video_dec_get_type, G_STRUCT_OFFSET (GstOMXVideoDecClass, cdata)},
{gst_omx_video_enc_get_type, G_STRUCT_OFFSET (GstOMXVideoEncClass, cdata)},
{gst_omx_audio_dec_get_type, G_STRUCT_OFFSET (GstOMXAudioDecClass, cdata)},
{gst_omx_audio_enc_get_type, G_STRUCT_OFFSET (GstOMXAudioEncClass, cdata)},
};

1299
omx/gstomxaudiodec.c Normal file

File diff suppressed because it is too large Load diff

100
omx/gstomxaudiodec.h Normal file
View file

@ -0,0 +1,100 @@
/*
* Copyright (C) 2014, Sebastian Dröge <sebastian@centricular.com>
*
* 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
*
*/
#ifndef __GST_OMX_AUDIO_DEC_H__
#define __GST_OMX_AUDIO_DEC_H__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include <gst/audio/gstaudiodecoder.h>
#include "gstomx.h"
G_BEGIN_DECLS
#define GST_TYPE_OMX_AUDIO_DEC \
(gst_omx_audio_dec_get_type())
#define GST_OMX_AUDIO_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OMX_AUDIO_DEC,GstOMXAudioDec))
#define GST_OMX_AUDIO_DEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OMX_AUDIO_DEC,GstOMXAudioDecClass))
#define GST_OMX_AUDIO_DEC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_OMX_AUDIO_DEC,GstOMXAudioDecClass))
#define GST_IS_OMX_AUDIO_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OMX_AUDIO_DEC))
#define GST_IS_OMX_AUDIO_DEC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OMX_AUDIO_DEC))
typedef struct _GstOMXAudioDec GstOMXAudioDec;
typedef struct _GstOMXAudioDecClass GstOMXAudioDecClass;
struct _GstOMXAudioDec
{
GstAudioDecoder parent;
/* < protected > */
GstOMXComponent *dec;
GstOMXPort *dec_in_port, *dec_out_port;
GstBufferPool *in_port_pool, *out_port_pool;
/* < private > */
GstAudioInfo info;
GstAudioChannelPosition position[OMX_AUDIO_MAXCHANNELS];
gint reorder_map[OMX_AUDIO_MAXCHANNELS];
gboolean needs_reorder;
GstBuffer *codec_data;
/* TRUE if the component is configured and saw
* the first buffer */
gboolean started;
GstClockTime last_upstream_ts;
/* Draining state */
GMutex drain_lock;
GCond drain_cond;
/* TRUE if EOS buffers shouldn't be forwarded */
gboolean draining;
/* TRUE if upstream is EOS */
gboolean eos;
GstFlowReturn downstream_flow_ret;
};
struct _GstOMXAudioDecClass
{
GstAudioDecoderClass parent_class;
GstOMXClassData cdata;
gboolean (*is_format_change) (GstOMXAudioDec * self, GstOMXPort * port, GstCaps * caps);
gboolean (*set_format) (GstOMXAudioDec * self, GstOMXPort * port, GstCaps * caps);
gint (*get_samples_per_frame) (GstOMXAudioDec * self, GstOMXPort * port);
};
GType gst_omx_audio_dec_get_type (void);
G_END_DECLS
#endif /* __GST_OMX_AUDIO_DEC_H__ */

209
omx/gstomxmp3dec.c Normal file
View file

@ -0,0 +1,209 @@
/*
* Copyright (C) 2014, Sebastian Dröge <sebastian@centricular.com>
*
* 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>
#include "gstomxmp3dec.h"
GST_DEBUG_CATEGORY_STATIC (gst_omx_mp3_dec_debug_category);
#define GST_CAT_DEFAULT gst_omx_mp3_dec_debug_category
/* prototypes */
static gboolean gst_omx_mp3_dec_set_format (GstOMXAudioDec * dec,
GstOMXPort * port, GstCaps * caps);
static gboolean gst_omx_mp3_dec_is_format_change (GstOMXAudioDec * dec,
GstOMXPort * port, GstCaps * caps);
static gint gst_omx_mp3_dec_get_samples_per_frame (GstOMXAudioDec * dec,
GstOMXPort * port);
/* class initialization */
#define DEBUG_INIT \
GST_DEBUG_CATEGORY_INIT (gst_omx_mp3_dec_debug_category, "omxmp3dec", 0, \
"debug category for gst-omx audio decoder base class");
G_DEFINE_TYPE_WITH_CODE (GstOMXMP3Dec, gst_omx_mp3_dec,
GST_TYPE_OMX_AUDIO_DEC, DEBUG_INIT);
static void
gst_omx_mp3_dec_class_init (GstOMXMP3DecClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstOMXAudioDecClass *audiodec_class = GST_OMX_AUDIO_DEC_CLASS (klass);
audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_set_format);
audiodec_class->is_format_change =
GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_is_format_change);
audiodec_class->get_samples_per_frame =
GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_get_samples_per_frame);
audiodec_class->cdata.default_sink_template_caps = "audio/mpeg, "
"mpegversion=(int)1, "
"layer=(int)3, "
"mpegaudioversion=(int)[1,3], "
"rate=(int)[8000,48000], "
"channels=(int)[1,2], " "parsed=(boolean) true";
gst_element_class_set_static_metadata (element_class,
"OpenMAX MP3 Audio Decoder",
"Codec/Decoder/Audio",
"Decode MP3 audio streams",
"Sebastian Dröge <sebastian@centricular.com>");
gst_omx_set_default_role (&audiodec_class->cdata, "audio_decoder.mp3");
}
static void
gst_omx_mp3_dec_init (GstOMXMP3Dec * self)
{
self->spf = -1;
}
static gboolean
gst_omx_mp3_dec_set_format (GstOMXAudioDec * dec, GstOMXPort * port,
GstCaps * caps)
{
GstOMXMP3Dec *self = GST_OMX_MP3_DEC (dec);
OMX_PARAM_PORTDEFINITIONTYPE port_def;
OMX_AUDIO_PARAM_MP3TYPE mp3_param;
OMX_ERRORTYPE err;
GstStructure *s;
gint rate, channels, layer, mpegaudioversion;
gst_omx_port_get_port_definition (port, &port_def);
port_def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
err = gst_omx_port_update_port_definition (port, &port_def);
if (err != OMX_ErrorNone) {
GST_ERROR_OBJECT (self,
"Failed to set MP3 format on component: %s (0x%08x)",
gst_omx_error_to_string (err), err);
return FALSE;
}
GST_OMX_INIT_STRUCT (&mp3_param);
mp3_param.nPortIndex = port->index;
err =
gst_omx_component_get_parameter (dec->dec, OMX_IndexParamAudioMp3,
&mp3_param);
if (err != OMX_ErrorNone) {
GST_ERROR_OBJECT (self,
"Failed to get MP3 parameters from component: %s (0x%08x)",
gst_omx_error_to_string (err), err);
return FALSE;
}
s = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion) ||
!gst_structure_get_int (s, "layer", &layer) ||
!gst_structure_get_int (s, "rate", &rate) ||
!gst_structure_get_int (s, "channels", &channels)) {
GST_ERROR_OBJECT (self, "Incomplete caps");
return FALSE;
}
self->spf = (mpegaudioversion == 1 ? 1152 : 576);
mp3_param.nChannels = channels;
mp3_param.nBitRate = 0; /* unknown */
mp3_param.nSampleRate = rate;
mp3_param.nAudioBandWidth = 0; /* decoder decision */
mp3_param.eChannelMode = 0; /* FIXME */
if (mpegaudioversion == 1)
mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP1Layer3;
else if (mpegaudioversion == 2)
mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP2Layer3;
else
mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP2_5Layer3;
err =
gst_omx_component_set_parameter (dec->dec, OMX_IndexParamAudioMp3,
&mp3_param);
if (err != OMX_ErrorNone) {
GST_ERROR_OBJECT (self, "Error setting MP3 parameters: %s (0x%08x)",
gst_omx_error_to_string (err), err);
return FALSE;
}
return TRUE;
}
static gboolean
gst_omx_mp3_dec_is_format_change (GstOMXAudioDec * dec, GstOMXPort * port,
GstCaps * caps)
{
GstOMXMP3Dec *self = GST_OMX_MP3_DEC (dec);
OMX_AUDIO_PARAM_MP3TYPE mp3_param;
OMX_ERRORTYPE err;
GstStructure *s;
gint rate, channels, layer, mpegaudioversion;
GST_OMX_INIT_STRUCT (&mp3_param);
mp3_param.nPortIndex = port->index;
err =
gst_omx_component_get_parameter (dec->dec, OMX_IndexParamAudioMp3,
&mp3_param);
if (err != OMX_ErrorNone) {
GST_ERROR_OBJECT (self,
"Failed to get MP3 parameters from component: %s (0x%08x)",
gst_omx_error_to_string (err), err);
return FALSE;
}
s = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion) ||
!gst_structure_get_int (s, "layer", &layer) ||
!gst_structure_get_int (s, "rate", &rate) ||
!gst_structure_get_int (s, "channels", &channels)) {
GST_ERROR_OBJECT (self, "Incomplete caps");
return FALSE;
}
if (mp3_param.nChannels != channels)
return TRUE;
if (mp3_param.nSampleRate != rate)
return TRUE;
if (mpegaudioversion == 1
&& mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP1Layer3)
return TRUE;
if (mpegaudioversion == 2
&& mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP2Layer3)
return TRUE;
if (mpegaudioversion == 3
&& mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP2_5Layer3)
return TRUE;
return FALSE;
}
static gint
gst_omx_mp3_dec_get_samples_per_frame (GstOMXAudioDec * dec, GstOMXPort * port)
{
return GST_OMX_MP3_DEC (dec)->spf;
}

60
omx/gstomxmp3dec.h Normal file
View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2014, Sebastian Dröge <sebastian@centricular.com>
*
* 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
*
*/
#ifndef __GST_OMX_MP3_DEC_H__
#define __GST_OMX_MP3_DEC_H__
#include <gst/gst.h>
#include "gstomxaudiodec.h"
G_BEGIN_DECLS
#define GST_TYPE_OMX_MP3_DEC \
(gst_omx_mp3_dec_get_type())
#define GST_OMX_MP3_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OMX_MP3_DEC,GstOMXMP3Dec))
#define GST_OMX_MP3_DEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OMX_MP3_DEC,GstOMXMP3DecClass))
#define GST_OMX_MP3_DEC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_OMX_MP3_DEC,GstOMXMP3DecClass))
#define GST_IS_OMX_MP3_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OMX_MP3_DEC))
#define GST_IS_OMX_MP3_DEC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OMX_MP3_DEC))
typedef struct _GstOMXMP3Dec GstOMXMP3Dec;
typedef struct _GstOMXMP3DecClass GstOMXMP3DecClass;
struct _GstOMXMP3Dec
{
GstOMXAudioDec parent;
gint spf;
};
struct _GstOMXMP3DecClass
{
GstOMXAudioDecClass parent_class;
};
GType gst_omx_mp3_dec_get_type (void);
G_END_DECLS
#endif /* __GST_OMX_MP3_DEC_H__ */