1 /*
2  * Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "gstfdkaacdec.h"
25 
26 #include <gst/pbutils/pbutils.h>
27 
28 #include <string.h>
29 
30 /* TODO:
31  * - LOAS / LATM support
32  * - Error concealment
33  */
34 
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]"
38 #else
39 #define CHANNELS_CAPS_STR "channels = (int) [1, 8]"
40 #endif
41 
42 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("audio/mpeg, "
46         "mpegversion = (int) 4, "
47         "stream-format = (string) { adts, adif, raw }, " CHANNELS_CAPS_STR)
48     );
49 
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
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)
57     );
58 
59 GST_DEBUG_CATEGORY_STATIC (gst_fdkaacdec_debug);
60 #define GST_CAT_DEFAULT gst_fdkaacdec_debug
61 
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,
65     GstCaps * caps);
66 static GstFlowReturn gst_fdkaacdec_handle_frame (GstAudioDecoder * dec,
67     GstBuffer * in_buf);
68 static void gst_fdkaacdec_flush (GstAudioDecoder * dec, gboolean hard);
69 
70 G_DEFINE_TYPE (GstFdkAacDec, gst_fdkaacdec, GST_TYPE_AUDIO_DECODER);
71 
72 static gboolean
gst_fdkaacdec_start(GstAudioDecoder * dec)73 gst_fdkaacdec_start (GstAudioDecoder * dec)
74 {
75   GstFdkAacDec *self = GST_FDKAACDEC (dec);
76 
77   GST_DEBUG_OBJECT (self, "start");
78 
79   return TRUE;
80 }
81 
82 static gboolean
gst_fdkaacdec_stop(GstAudioDecoder * dec)83 gst_fdkaacdec_stop (GstAudioDecoder * dec)
84 {
85   GstFdkAacDec *self = GST_FDKAACDEC (dec);
86 
87   GST_DEBUG_OBJECT (self, "stop");
88 
89   g_free (self->decode_buffer);
90   self->decode_buffer = NULL;
91 
92   if (self->dec)
93     aacDecoder_Close (self->dec);
94   self->dec = NULL;
95 
96   return TRUE;
97 }
98 
99 static gboolean
gst_fdkaacdec_set_format(GstAudioDecoder * dec,GstCaps * caps)100 gst_fdkaacdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
101 {
102   GstFdkAacDec *self = GST_FDKAACDEC (dec);
103   TRANSPORT_TYPE transport_format;
104   GstStructure *s;
105   const gchar *stream_format;
106   AAC_DECODER_ERROR err;
107 
108   if (self->dec) {
109     /* drain */
110     gst_fdkaacdec_handle_frame (dec, NULL);
111     aacDecoder_Close (self->dec);
112     self->dec = NULL;
113   }
114 
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;
123   } else {
124     g_assert_not_reached ();
125   }
126 
127   self->dec = aacDecoder_Open (transport_format, 1);
128   if (!self->dec) {
129     GST_ERROR_OBJECT (self, "Failed to open decoder");
130     return FALSE;
131   }
132 
133   if (transport_format == TT_MP4_RAW) {
134     GstBuffer *codec_data = NULL;
135     GstMapInfo map;
136     guint8 *data;
137     guint size;
138 
139     gst_structure_get (s, "codec_data", GST_TYPE_BUFFER, &codec_data, NULL);
140 
141     if (!codec_data) {
142       GST_ERROR_OBJECT (self, "Raw AAC without codec_data not supported");
143       return FALSE;
144     }
145 
146     gst_buffer_map (codec_data, &map, GST_MAP_READ);
147     data = map.data;
148     size = map.size;
149 
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);
154       return FALSE;
155     }
156 
157     gst_buffer_unmap (codec_data, &map);
158     gst_buffer_unref (codec_data);
159   }
160 
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);
164     return FALSE;
165   }
166 
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);
173     return FALSE;
174   }
175 
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);
180   }
181 
182   return TRUE;
183 }
184 
185 static GstFlowReturn
gst_fdkaacdec_handle_frame(GstAudioDecoder * dec,GstBuffer * inbuf)186 gst_fdkaacdec_handle_frame (GstAudioDecoder * dec, GstBuffer * inbuf)
187 {
188   GstFdkAacDec *self = GST_FDKAACDEC (dec);
189   GstFlowReturn ret = GST_FLOW_OK;
190   GstBuffer *outbuf;
191   GstMapInfo imap;
192   AAC_DECODER_ERROR err;
193   guint size, valid;
194   CStreamInfo *stream_info;
195   GstAudioInfo info;
196   guint flags = 0, i;
197   GstAudioChannelPosition pos[64], gst_pos[64];
198   gboolean need_reorder;
199 
200   if (inbuf) {
201     gst_buffer_ref (inbuf);
202     gst_buffer_map (inbuf, &imap, GST_MAP_READ);
203     valid = size = imap.size;
204 
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);
209       goto out;
210     }
211 
212     if (GST_BUFFER_IS_DISCONT (inbuf)) {
213       flags |= AACDEC_INTR;
214     }
215   } else {
216     flags |= AACDEC_FLUSH;
217   }
218 
219   err = aacDecoder_DecodeFrame (self->dec, self->decode_buffer,
220       self->decode_buffer_size, flags);
221   if (err == AAC_DEC_TRANSPORT_SYNC_ERROR) {
222     ret = GST_FLOW_OK;
223     outbuf = NULL;
224     goto finish;
225   } else if (err != AAC_DEC_OK) {
226     GST_AUDIO_DECODER_ERROR (self, 1, STREAM, DECODE, (NULL),
227         ("decoding error: %d", err), ret);
228     goto out;
229   }
230 
231   stream_info = aacDecoder_GetStreamInfo (self->dec);
232   if (!stream_info) {
233     GST_AUDIO_DECODER_ERROR (self, 1, STREAM, DECODE, (NULL),
234         ("failed to get stream info"), ret);
235     goto out;
236   }
237 
238   /* FIXME: Don't recalculate this on every buffer */
239   if (stream_info->numChannels == 1) {
240     pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
241   } else {
242     gint n_front = 0, n_back = 0, n_lfe = 0;
243 
244     /* FIXME: Can this be simplified somehow? */
245     for (i = 0; i < stream_info->numChannels; i++) {
246       if (stream_info->pChannelType[i] == ACT_FRONT) {
247         n_front++;
248       } else if (stream_info->pChannelType[i] == ACT_BACK) {
249         n_back++;
250       } else if (stream_info->pChannelType[i] == ACT_LFE) {
251         n_lfe++;
252       } else {
253         GST_ERROR_OBJECT (self, "Channel type %d not supported",
254             stream_info->pChannelType[i]);
255         ret = GST_FLOW_NOT_NEGOTIATED;
256         goto out;
257       }
258     }
259 
260     for (i = 0; i < stream_info->numChannels; i++) {
261       if (stream_info->pChannelType[i] == ACT_FRONT) {
262         if (stream_info->pChannelIndices[i] == 0) {
263           if (n_front & 1)
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;
267           else
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;
276           else
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;
285           else
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;
294           else
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 ();
303           else
304             g_assert_not_reached ();
305         } else {
306           GST_ERROR_OBJECT (self, "Front channel index %d not supported",
307               stream_info->pChannelIndices[i]);
308           ret = GST_FLOW_NOT_NEGOTIATED;
309           goto out;
310         }
311       } else if (stream_info->pChannelType[i] == ACT_BACK) {
312         if (stream_info->pChannelIndices[i] == 0) {
313           if (n_back & 1)
314             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
315           else if (n_back > 2)
316             pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
317           else
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;
322           else if (n_back & 1)
323             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
324           else if (n_back > 2)
325             pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
326           else
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;
331           else if (n_back & 1)
332             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
333           else if (n_back > 2)
334             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
335           else
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;
340           else if (n_back & 1)
341             g_assert_not_reached ();
342           else if (n_back > 2)
343             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
344           else
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;
349           else if (n_back & 1)
350             g_assert_not_reached ();
351           else if (n_back > 2)
352             g_assert_not_reached ();
353           else
354             g_assert_not_reached ();
355         } else {
356           GST_ERROR_OBJECT (self, "Side channel index %d not supported",
357               stream_info->pChannelIndices[i]);
358           ret = GST_FLOW_NOT_NEGOTIATED;
359           goto out;
360         }
361       } else if (stream_info->pChannelType[i] == ACT_LFE) {
362         if (stream_info->pChannelIndices[i] == 0) {
363           pos[i] = GST_AUDIO_CHANNEL_POSITION_LFE1;
364         } else {
365           GST_ERROR_OBJECT (self, "LFE channel index %d not supported",
366               stream_info->pChannelIndices[i]);
367           ret = GST_FLOW_NOT_NEGOTIATED;
368           goto out;
369         }
370       } else {
371         GST_ERROR_OBJECT (self, "Channel type %d not supported",
372             stream_info->pChannelType[i]);
373         ret = GST_FLOW_NOT_NEGOTIATED;
374         goto out;
375       }
376     }
377   }
378 
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;
384     goto out;
385   }
386 
387   need_reorder =
388       memcmp (pos, gst_pos,
389       sizeof (GstAudioChannelPosition) * stream_info->numChannels) != 0;
390 
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;
396     goto out;
397   }
398 
399   outbuf =
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));
404 
405   if (need_reorder) {
406     gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_INFO_FORMAT (&info),
407         GST_AUDIO_INFO_CHANNELS (&info), pos, gst_pos);
408   }
409 
410 finish:
411   ret = gst_audio_decoder_finish_frame (dec, outbuf, 1);
412 
413 out:
414 
415   if (inbuf) {
416     gst_buffer_unmap (inbuf, &imap);
417     gst_buffer_unref (inbuf);
418   }
419 
420   return ret;
421 }
422 
423 static void
gst_fdkaacdec_flush(GstAudioDecoder * dec,gboolean hard)424 gst_fdkaacdec_flush (GstAudioDecoder * dec, gboolean hard)
425 {
426   GstFdkAacDec *self = GST_FDKAACDEC (dec);
427 
428   if (self->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);
434     }
435   }
436 }
437 
438 static void
gst_fdkaacdec_init(GstFdkAacDec * self)439 gst_fdkaacdec_init (GstFdkAacDec * self)
440 {
441   self->dec = NULL;
442 
443   gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (self), TRUE);
444   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (self), TRUE);
445 }
446 
447 static void
gst_fdkaacdec_class_init(GstFdkAacDecClass * klass)448 gst_fdkaacdec_class_init (GstFdkAacDecClass * klass)
449 {
450   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
451   GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
452 
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);
458 
459   gst_element_class_add_static_pad_template (element_class, &sink_template);
460   gst_element_class_add_static_pad_template (element_class, &src_template);
461 
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>");
465 
466   GST_DEBUG_CATEGORY_INIT (gst_fdkaacdec_debug, "fdkaacdec", 0,
467       "fdkaac decoder");
468 }
469