2 * Copyright (C) 2016 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 Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gstfdkaacdec.h"
26 #include <gst/pbutils/pbutils.h>
31 * - LOAS / LATM support
35 #ifndef HAVE_FDK_AAC_0_1_4
36 #define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
37 #define CHANNELS_CAPS_STR "channels = (int) [1, 6]"
39 #define CHANNELS_CAPS_STR "channels = (int) [1, 8]"
42 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("audio/mpeg, "
46 "mpegversion = (int) {2, 4}, "
47 "stream-format = (string) { adts, adif, raw }, " CHANNELS_CAPS_STR)
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("audio/x-raw, "
54 "format = (string) " GST_AUDIO_NE (S16) ", "
55 "layout = (string) interleaved, "
56 "rate = (int) [8000, 96000], " CHANNELS_CAPS_STR)
59 GST_DEBUG_CATEGORY_STATIC (gst_fdkaacdec_debug);
60 #define GST_CAT_DEFAULT gst_fdkaacdec_debug
62 static gboolean gst_fdkaacdec_start (GstAudioDecoder * dec);
63 static gboolean gst_fdkaacdec_stop (GstAudioDecoder * dec);
64 static gboolean gst_fdkaacdec_set_format (GstAudioDecoder * dec,
66 static GstFlowReturn gst_fdkaacdec_handle_frame (GstAudioDecoder * dec,
68 static void gst_fdkaacdec_flush (GstAudioDecoder * dec, gboolean hard);
70 G_DEFINE_TYPE (GstFdkAacDec, gst_fdkaacdec, GST_TYPE_AUDIO_DECODER);
73 gst_fdkaacdec_start (GstAudioDecoder * dec)
75 GstFdkAacDec *self = GST_FDKAACDEC (dec);
77 GST_DEBUG_OBJECT (self, "start");
83 gst_fdkaacdec_stop (GstAudioDecoder * dec)
85 GstFdkAacDec *self = GST_FDKAACDEC (dec);
87 GST_DEBUG_OBJECT (self, "stop");
89 g_free (self->decode_buffer);
90 self->decode_buffer = NULL;
93 aacDecoder_Close (self->dec);
100 gst_fdkaacdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
102 GstFdkAacDec *self = GST_FDKAACDEC (dec);
103 TRANSPORT_TYPE transport_format;
105 const gchar *stream_format;
106 AAC_DECODER_ERROR err;
110 gst_fdkaacdec_handle_frame (dec, NULL);
111 aacDecoder_Close (self->dec);
115 s = gst_caps_get_structure (caps, 0);
116 stream_format = gst_structure_get_string (s, "stream-format");
117 if (strcmp (stream_format, "raw") == 0) {
118 transport_format = TT_MP4_RAW;
119 } else if (strcmp (stream_format, "adif") == 0) {
120 transport_format = TT_MP4_ADIF;
121 } else if (strcmp (stream_format, "adts") == 0) {
122 transport_format = TT_MP4_ADTS;
124 g_assert_not_reached ();
127 self->dec = aacDecoder_Open (transport_format, 1);
129 GST_ERROR_OBJECT (self, "Failed to open decoder");
133 if (transport_format == TT_MP4_RAW) {
134 GstBuffer *codec_data = NULL;
139 gst_structure_get (s, "codec_data", GST_TYPE_BUFFER, &codec_data, NULL);
142 GST_ERROR_OBJECT (self, "Raw AAC without codec_data not supported");
146 gst_buffer_map (codec_data, &map, GST_MAP_READ);
150 if ((err = aacDecoder_ConfigRaw (self->dec, &data, &size)) != AAC_DEC_OK) {
151 gst_buffer_unmap (codec_data, &map);
152 gst_buffer_unref (codec_data);
153 GST_ERROR_OBJECT (self, "Invalid codec_data: %d", err);
157 gst_buffer_unmap (codec_data, &map);
158 gst_buffer_unref (codec_data);
161 err = aacDecoder_SetParam (self->dec, AAC_PCM_MAX_OUTPUT_CHANNELS, 0);
162 if (err != AAC_DEC_OK) {
163 GST_ERROR_OBJECT (self, "Failed to disable downmixing: %d", err);
167 /* Choose WAV channel mapping to get interleaving even with libfdk-aac 2.0.0
168 * The pChannelIndices retain the indices from the standard MPEG mapping so
169 * we're agnostic to the actual order. */
170 err = aacDecoder_SetParam (self->dec, AAC_PCM_OUTPUT_CHANNEL_MAPPING, 1);
171 if (err != AAC_DEC_OK) {
172 GST_ERROR_OBJECT (self, "Failed to set output channel mapping: %d", err);
176 /* 8 channels * 2 bytes per sample * 2048 samples */
177 if (!self->decode_buffer) {
178 self->decode_buffer_size = 8 * 2048;
179 self->decode_buffer = g_new (gint16, self->decode_buffer_size);
186 gst_fdkaacdec_handle_frame (GstAudioDecoder * dec, GstBuffer * inbuf)
188 GstFdkAacDec *self = GST_FDKAACDEC (dec);
189 GstFlowReturn ret = GST_FLOW_OK;
192 AAC_DECODER_ERROR err;
194 CStreamInfo *stream_info;
197 GstAudioChannelPosition pos[64], gst_pos[64];
198 gboolean need_reorder;
201 gst_buffer_ref (inbuf);
202 gst_buffer_map (inbuf, &imap, GST_MAP_READ);
203 valid = size = imap.size;
205 err = aacDecoder_Fill (self->dec, (guint8 **) & imap.data, &size, &valid);
206 if (err != AAC_DEC_OK) {
207 GST_AUDIO_DECODER_ERROR (self, 1, STREAM, DECODE, (NULL),
208 ("filling error: %d", err), ret);
212 if (GST_BUFFER_IS_DISCONT (inbuf)) {
213 flags |= AACDEC_INTR;
216 flags |= AACDEC_FLUSH;
219 err = aacDecoder_DecodeFrame (self->dec, self->decode_buffer,
220 self->decode_buffer_size, flags);
221 if (err == AAC_DEC_TRANSPORT_SYNC_ERROR) {
225 } else if (err != AAC_DEC_OK) {
226 GST_AUDIO_DECODER_ERROR (self, 1, STREAM, DECODE, (NULL),
227 ("decoding error: %d", err), ret);
231 stream_info = aacDecoder_GetStreamInfo (self->dec);
233 GST_AUDIO_DECODER_ERROR (self, 1, STREAM, DECODE, (NULL),
234 ("failed to get stream info"), ret);
238 /* FIXME: Don't recalculate this on every buffer */
239 if (stream_info->numChannels == 1) {
240 pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
242 gint n_front = 0, n_back = 0, n_lfe = 0;
244 /* FIXME: Can this be simplified somehow? */
245 for (i = 0; i < stream_info->numChannels; i++) {
246 if (stream_info->pChannelType[i] == ACT_FRONT) {
248 } else if (stream_info->pChannelType[i] == ACT_BACK) {
250 } else if (stream_info->pChannelType[i] == ACT_LFE) {
253 GST_ERROR_OBJECT (self, "Channel type %d not supported",
254 stream_info->pChannelType[i]);
255 ret = GST_FLOW_NOT_NEGOTIATED;
260 for (i = 0; i < stream_info->numChannels; i++) {
261 if (stream_info->pChannelType[i] == ACT_FRONT) {
262 if (stream_info->pChannelIndices[i] == 0) {
264 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
265 else if (n_front > 2)
266 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
268 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
269 } else if (stream_info->pChannelIndices[i] == 1) {
270 if ((n_front & 1) && n_front > 3)
271 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
272 else if (n_front & 1)
273 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
274 else if (n_front > 2)
275 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
277 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
278 } else if (stream_info->pChannelIndices[i] == 2) {
279 if ((n_front & 1) && n_front > 3)
280 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
281 else if (n_front & 1)
282 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
283 else if (n_front > 2)
284 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
286 g_assert_not_reached ();
287 } else if (stream_info->pChannelIndices[i] == 3) {
288 if ((n_front & 1) && n_front > 3)
289 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
290 else if (n_front & 1)
291 g_assert_not_reached ();
292 else if (n_front > 2)
293 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
295 g_assert_not_reached ();
296 } else if (stream_info->pChannelIndices[i] == 4) {
297 if ((n_front & 1) && n_front > 2)
298 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
299 else if (n_front & 1)
300 g_assert_not_reached ();
301 else if (n_front > 2)
302 g_assert_not_reached ();
304 g_assert_not_reached ();
306 GST_ERROR_OBJECT (self, "Front channel index %d not supported",
307 stream_info->pChannelIndices[i]);
308 ret = GST_FLOW_NOT_NEGOTIATED;
311 } else if (stream_info->pChannelType[i] == ACT_BACK) {
312 if (stream_info->pChannelIndices[i] == 0) {
314 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
316 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
318 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
319 } else if (stream_info->pChannelIndices[i] == 1) {
320 if ((n_back & 1) && n_back > 3)
321 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
323 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
325 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
327 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
328 } else if (stream_info->pChannelIndices[i] == 2) {
329 if ((n_back & 1) && n_back > 3)
330 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
332 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
334 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
336 g_assert_not_reached ();
337 } else if (stream_info->pChannelIndices[i] == 3) {
338 if ((n_back & 1) && n_back > 3)
339 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
341 g_assert_not_reached ();
343 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
345 g_assert_not_reached ();
346 } else if (stream_info->pChannelIndices[i] == 4) {
347 if ((n_back & 1) && n_back > 3)
348 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
350 g_assert_not_reached ();
352 g_assert_not_reached ();
354 g_assert_not_reached ();
356 GST_ERROR_OBJECT (self, "Side channel index %d not supported",
357 stream_info->pChannelIndices[i]);
358 ret = GST_FLOW_NOT_NEGOTIATED;
361 } else if (stream_info->pChannelType[i] == ACT_LFE) {
362 if (stream_info->pChannelIndices[i] == 0) {
363 pos[i] = GST_AUDIO_CHANNEL_POSITION_LFE1;
365 GST_ERROR_OBJECT (self, "LFE channel index %d not supported",
366 stream_info->pChannelIndices[i]);
367 ret = GST_FLOW_NOT_NEGOTIATED;
371 GST_ERROR_OBJECT (self, "Channel type %d not supported",
372 stream_info->pChannelType[i]);
373 ret = GST_FLOW_NOT_NEGOTIATED;
379 memcpy (gst_pos, pos,
380 sizeof (GstAudioChannelPosition) * stream_info->numChannels);
381 if (!gst_audio_channel_positions_to_valid_order (gst_pos,
382 stream_info->numChannels)) {
383 ret = GST_FLOW_NOT_NEGOTIATED;
388 memcmp (pos, gst_pos,
389 sizeof (GstAudioChannelPosition) * stream_info->numChannels) != 0;
391 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16,
392 stream_info->sampleRate, stream_info->numChannels, gst_pos);
393 if (!gst_audio_decoder_set_output_format (dec, &info)) {
394 GST_ERROR_OBJECT (self, "Failed to set output format");
395 ret = GST_FLOW_NOT_NEGOTIATED;
400 gst_audio_decoder_allocate_output_buffer (dec,
401 stream_info->frameSize * GST_AUDIO_INFO_BPF (&info));
402 gst_buffer_fill (outbuf, 0, self->decode_buffer,
403 gst_buffer_get_size (outbuf));
406 gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_INFO_FORMAT (&info),
407 GST_AUDIO_INFO_CHANNELS (&info), pos, gst_pos);
411 ret = gst_audio_decoder_finish_frame (dec, outbuf, 1);
416 gst_buffer_unmap (inbuf, &imap);
417 gst_buffer_unref (inbuf);
424 gst_fdkaacdec_flush (GstAudioDecoder * dec, gboolean hard)
426 GstFdkAacDec *self = GST_FDKAACDEC (dec);
429 AAC_DECODER_ERROR err;
430 err = aacDecoder_DecodeFrame (self->dec, self->decode_buffer,
431 self->decode_buffer_size, AACDEC_FLUSH);
432 if (err != AAC_DEC_OK) {
433 GST_ERROR_OBJECT (self, "flushing error: %d", err);
439 gst_fdkaacdec_init (GstFdkAacDec * self)
443 gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (self), TRUE);
444 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (self), TRUE);
448 gst_fdkaacdec_class_init (GstFdkAacDecClass * klass)
450 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
451 GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
453 base_class->start = GST_DEBUG_FUNCPTR (gst_fdkaacdec_start);
454 base_class->stop = GST_DEBUG_FUNCPTR (gst_fdkaacdec_stop);
455 base_class->set_format = GST_DEBUG_FUNCPTR (gst_fdkaacdec_set_format);
456 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_fdkaacdec_handle_frame);
457 base_class->flush = GST_DEBUG_FUNCPTR (gst_fdkaacdec_flush);
459 gst_element_class_add_static_pad_template (element_class, &sink_template);
460 gst_element_class_add_static_pad_template (element_class, &src_template);
462 gst_element_class_set_static_metadata (element_class, "FDK AAC audio decoder",
463 "Codec/Decoder/Audio", "FDK AAC audio decoder",
464 "Sebastian Dröge <sebastian@centricular.com>");
466 GST_DEBUG_CATEGORY_INIT (gst_fdkaacdec_debug, "fdkaacdec", 0,