2 * Copyright (C) 2014, Sebastian Dröge <sebastian@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation
7 * version 2.1 of the License.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "gstomxmp3dec.h"
28 GST_DEBUG_CATEGORY_STATIC (gst_omx_mp3_dec_debug_category);
29 #define GST_CAT_DEFAULT gst_omx_mp3_dec_debug_category
32 static gboolean gst_omx_mp3_dec_set_format (GstOMXAudioDec * dec,
33 GstOMXPort * port, GstCaps * caps);
34 static gboolean gst_omx_mp3_dec_is_format_change (GstOMXAudioDec * dec,
35 GstOMXPort * port, GstCaps * caps);
36 static gint gst_omx_mp3_dec_get_samples_per_frame (GstOMXAudioDec * dec,
38 static gboolean gst_omx_mp3_dec_get_channel_positions (GstOMXAudioDec * dec,
39 GstOMXPort * port, GstAudioChannelPosition position[OMX_AUDIO_MAXCHANNELS]);
41 /* class initialization */
44 GST_DEBUG_CATEGORY_INIT (gst_omx_mp3_dec_debug_category, "omxmp3dec", 0, \
45 "debug category for gst-omx mp3 audio decoder");
47 G_DEFINE_TYPE_WITH_CODE (GstOMXMP3Dec, gst_omx_mp3_dec,
48 GST_TYPE_OMX_AUDIO_DEC, DEBUG_INIT);
52 gst_omx_mp3_dec_class_init (GstOMXMP3DecClass * klass)
54 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
55 GstOMXAudioDecClass *audiodec_class = GST_OMX_AUDIO_DEC_CLASS (klass);
57 audiodec_class->set_format = GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_set_format);
58 audiodec_class->is_format_change =
59 GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_is_format_change);
60 audiodec_class->get_samples_per_frame =
61 GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_get_samples_per_frame);
62 audiodec_class->get_channel_positions =
63 GST_DEBUG_FUNCPTR (gst_omx_mp3_dec_get_channel_positions);
65 audiodec_class->cdata.default_sink_template_caps = "audio/mpeg, "
66 "mpegversion=(int)1, "
68 "mpegaudioversion=(int)[1,3], "
69 "rate=(int)[8000,48000], "
70 "channels=(int)[1,2], " "parsed=(boolean) true";
72 gst_element_class_set_static_metadata (element_class,
73 "OpenMAX MP3 Audio Decoder",
74 "Codec/Decoder/Audio/Hardware",
75 "Decode MP3 audio streams",
76 "Sebastian Dröge <sebastian@centricular.com>");
78 gst_omx_set_default_role (&audiodec_class->cdata, "audio_decoder.mp3");
82 gst_omx_mp3_dec_init (GstOMXMP3Dec * self)
88 gst_omx_mp3_dec_set_format (GstOMXAudioDec * dec, GstOMXPort * port,
91 GstOMXMP3Dec *self = GST_OMX_MP3_DEC (dec);
92 OMX_PARAM_PORTDEFINITIONTYPE port_def;
93 OMX_AUDIO_PARAM_MP3TYPE mp3_param;
96 gint rate, channels, layer, mpegaudioversion;
98 gst_omx_port_get_port_definition (port, &port_def);
99 port_def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
100 err = gst_omx_port_update_port_definition (port, &port_def);
101 if (err != OMX_ErrorNone) {
102 GST_ERROR_OBJECT (self,
103 "Failed to set MP3 format on component: %s (0x%08x)",
104 gst_omx_error_to_string (err), err);
108 GST_OMX_INIT_STRUCT (&mp3_param);
109 mp3_param.nPortIndex = port->index;
112 gst_omx_component_get_parameter (dec->dec, OMX_IndexParamAudioMp3,
114 if (err != OMX_ErrorNone) {
115 GST_ERROR_OBJECT (self,
116 "Failed to get MP3 parameters from component: %s (0x%08x)",
117 gst_omx_error_to_string (err), err);
121 s = gst_caps_get_structure (caps, 0);
123 if (!gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion) ||
124 !gst_structure_get_int (s, "layer", &layer) ||
125 !gst_structure_get_int (s, "rate", &rate) ||
126 !gst_structure_get_int (s, "channels", &channels)) {
127 GST_ERROR_OBJECT (self, "Incomplete caps");
131 self->spf = (mpegaudioversion == 1 ? 1152 : 576);
133 mp3_param.nChannels = channels;
134 mp3_param.nBitRate = 0; /* unknown */
135 mp3_param.nSampleRate = rate;
136 mp3_param.nAudioBandWidth = 0; /* decoder decision */
137 mp3_param.eChannelMode = 0; /* FIXME */
138 if (mpegaudioversion == 1)
139 mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP1Layer3;
140 else if (mpegaudioversion == 2)
141 mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP2Layer3;
143 mp3_param.eFormat = OMX_AUDIO_MP3StreamFormatMP2_5Layer3;
146 gst_omx_component_set_parameter (dec->dec, OMX_IndexParamAudioMp3,
148 if (err != OMX_ErrorNone) {
149 GST_ERROR_OBJECT (self, "Error setting MP3 parameters: %s (0x%08x)",
150 gst_omx_error_to_string (err), err);
158 gst_omx_mp3_dec_is_format_change (GstOMXAudioDec * dec, GstOMXPort * port,
161 GstOMXMP3Dec *self = GST_OMX_MP3_DEC (dec);
162 OMX_AUDIO_PARAM_MP3TYPE mp3_param;
165 gint rate, channels, layer, mpegaudioversion;
167 GST_OMX_INIT_STRUCT (&mp3_param);
168 mp3_param.nPortIndex = port->index;
171 gst_omx_component_get_parameter (dec->dec, OMX_IndexParamAudioMp3,
173 if (err != OMX_ErrorNone) {
174 GST_ERROR_OBJECT (self,
175 "Failed to get MP3 parameters from component: %s (0x%08x)",
176 gst_omx_error_to_string (err), err);
180 s = gst_caps_get_structure (caps, 0);
182 if (!gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion) ||
183 !gst_structure_get_int (s, "layer", &layer) ||
184 !gst_structure_get_int (s, "rate", &rate) ||
185 !gst_structure_get_int (s, "channels", &channels)) {
186 GST_ERROR_OBJECT (self, "Incomplete caps");
190 if (mp3_param.nChannels != channels)
193 if (mp3_param.nSampleRate != rate)
196 if (mpegaudioversion == 1
197 && mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP1Layer3)
199 if (mpegaudioversion == 2
200 && mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP2Layer3)
202 if (mpegaudioversion == 3
203 && mp3_param.eFormat != OMX_AUDIO_MP3StreamFormatMP2_5Layer3)
210 gst_omx_mp3_dec_get_samples_per_frame (GstOMXAudioDec * dec, GstOMXPort * port)
212 return GST_OMX_MP3_DEC (dec)->spf;
216 gst_omx_mp3_dec_get_channel_positions (GstOMXAudioDec * dec,
217 GstOMXPort * port, GstAudioChannelPosition position[OMX_AUDIO_MAXCHANNELS])
219 OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
222 GST_OMX_INIT_STRUCT (&pcm_param);
223 pcm_param.nPortIndex = port->index;
225 gst_omx_component_get_parameter (dec->dec, OMX_IndexParamAudioPcm,
227 if (err != OMX_ErrorNone) {
228 GST_ERROR_OBJECT (dec, "Failed to get PCM parameters: %s (0x%08x)",
229 gst_omx_error_to_string (err), err);
233 switch (pcm_param.nChannels) {
235 position[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
238 position[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
239 position[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;