fdkaacenc: Implement flush function
[platform/upstream/gstreamer.git] / ext / fdkaac / gstfdkaacenc.c
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 "gstfdkaac.h"
25 #include "gstfdkaacenc.h"
26
27 #include <gst/pbutils/pbutils.h>
28
29 #include <string.h>
30
31 /* TODO:
32  * - Add support for other AOT / profiles
33  * - Expose more properties, e.g. afterburner and vbr
34  * - Signal encoder delay
35  * - LOAS / LATM support
36  */
37
38 enum
39 {
40   PROP_0,
41   PROP_BITRATE
42 };
43
44 #define DEFAULT_BITRATE (0)
45
46 #define SAMPLE_RATES " 8000, " \
47                     "11025, " \
48                     "12000, " \
49                     "16000, " \
50                     "22050, " \
51                     "24000, " \
52                     "32000, " \
53                     "44100, " \
54                     "48000, " \
55                     "64000, " \
56                     "88200, " \
57                     "96000"
58
59 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("audio/x-raw, "
63         "format = (string) " GST_AUDIO_NE (S16) ", "
64         "layout = (string) interleaved, "
65         "rate = (int) { " SAMPLE_RATES " }, "
66         "channels = (int) {1, 2, 3, 4, 5, 6, 8}")
67     );
68
69 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS ("audio/mpeg, "
73         "mpegversion = (int) 4, "
74         "rate = (int) { " SAMPLE_RATES " }, "
75         "channels = (int) {1, 2, 3, 4, 5, 6, 8}, "
76         "stream-format = (string) { adts, adif, raw }, "
77         "base-profile = (string) lc, " "framed = (boolean) true")
78     );
79
80 GST_DEBUG_CATEGORY_STATIC (gst_fdkaacenc_debug);
81 #define GST_CAT_DEFAULT gst_fdkaacenc_debug
82
83 static void gst_fdkaacenc_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_fdkaacenc_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87 static gboolean gst_fdkaacenc_start (GstAudioEncoder * enc);
88 static gboolean gst_fdkaacenc_stop (GstAudioEncoder * enc);
89 static gboolean gst_fdkaacenc_set_format (GstAudioEncoder * enc,
90     GstAudioInfo * info);
91 static GstFlowReturn gst_fdkaacenc_handle_frame (GstAudioEncoder * enc,
92     GstBuffer * in_buf);
93 static GstCaps *gst_fdkaacenc_get_caps (GstAudioEncoder * enc,
94     GstCaps * filter);
95 static void gst_fdkaacenc_flush (GstAudioEncoder * enc);
96
97 G_DEFINE_TYPE (GstFdkAacEnc, gst_fdkaacenc, GST_TYPE_AUDIO_ENCODER);
98
99 static void
100 gst_fdkaacenc_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec)
102 {
103   GstFdkAacEnc *self = GST_FDKAACENC (object);
104
105   switch (prop_id) {
106     case PROP_BITRATE:
107       self->bitrate = g_value_get_int (value);
108       break;
109     default:
110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111       break;
112   }
113   return;
114 }
115
116 static void
117 gst_fdkaacenc_get_property (GObject * object, guint prop_id,
118     GValue * value, GParamSpec * pspec)
119 {
120   GstFdkAacEnc *self = GST_FDKAACENC (object);
121
122   switch (prop_id) {
123     case PROP_BITRATE:
124       g_value_set_int (value, self->bitrate);
125       break;
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128       break;
129   }
130   return;
131 }
132
133 static gboolean
134 gst_fdkaacenc_start (GstAudioEncoder * enc)
135 {
136   GstFdkAacEnc *self = GST_FDKAACENC (enc);
137
138   GST_DEBUG_OBJECT (self, "start");
139
140   return TRUE;
141 }
142
143 static gboolean
144 gst_fdkaacenc_stop (GstAudioEncoder * enc)
145 {
146   GstFdkAacEnc *self = GST_FDKAACENC (enc);
147
148   GST_DEBUG_OBJECT (self, "stop");
149
150   if (self->enc) {
151     aacEncClose (&self->enc);
152     self->enc = NULL;
153   }
154
155   self->is_drained = TRUE;
156   return TRUE;
157 }
158
159 static GstCaps *
160 gst_fdkaacenc_get_caps (GstAudioEncoder * enc, GstCaps * filter)
161 {
162   const GstFdkAacChannelLayout *layout;
163   GstCaps *res, *caps;
164
165   caps = gst_caps_new_empty ();
166
167   for (layout = channel_layouts; layout->channels; layout++) {
168     gint channels = layout->channels;
169     GstCaps *tmp =
170         gst_caps_make_writable (gst_pad_get_pad_template_caps
171         (GST_AUDIO_ENCODER_SINK_PAD (enc)));
172
173     if (channels == 1) {
174       gst_caps_set_simple (tmp, "channels", G_TYPE_INT, channels, NULL);
175     } else {
176       guint64 channel_mask;
177       gst_audio_channel_positions_to_mask (layout->positions, channels, FALSE,
178           &channel_mask);
179       gst_caps_set_simple (tmp, "channels", G_TYPE_INT, channels,
180           "channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
181     }
182
183     gst_caps_append (caps, tmp);
184   }
185
186   res = gst_audio_encoder_proxy_getcaps (enc, caps, filter);
187   gst_caps_unref (caps);
188
189   return res;
190 }
191
192 static gboolean
193 gst_fdkaacenc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
194 {
195   GstFdkAacEnc *self = GST_FDKAACENC (enc);
196   gboolean ret = FALSE;
197   GstCaps *allowed_caps;
198   GstCaps *src_caps;
199   AACENC_ERROR err;
200   gint transmux = 0, aot = AOT_AAC_LC;
201   gint mpegversion = 4;
202   CHANNEL_MODE channel_mode;
203   AACENC_InfoStruct enc_info = { 0 };
204   gint bitrate;
205
206   if (self->enc && !self->is_drained) {
207     /* drain */
208     gst_fdkaacenc_handle_frame (enc, NULL);
209     aacEncClose (&self->enc);
210     self->is_drained = TRUE;
211   }
212
213   allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (self));
214
215   GST_DEBUG_OBJECT (self, "allowed caps: %" GST_PTR_FORMAT, allowed_caps);
216
217   if (allowed_caps && gst_caps_get_size (allowed_caps) > 0) {
218     GstStructure *s = gst_caps_get_structure (allowed_caps, 0);
219     const gchar *str = NULL;
220
221     if ((str = gst_structure_get_string (s, "stream-format"))) {
222       if (strcmp (str, "adts") == 0) {
223         GST_DEBUG_OBJECT (self, "use ADTS format for output");
224         transmux = 2;
225       } else if (strcmp (str, "adif") == 0) {
226         GST_DEBUG_OBJECT (self, "use ADIF format for output");
227         transmux = 1;
228       } else if (strcmp (str, "raw") == 0) {
229         GST_DEBUG_OBJECT (self, "use RAW format for output");
230         transmux = 0;
231       }
232     }
233
234     gst_structure_get_int (s, "mpegversion", &mpegversion);
235   }
236   if (allowed_caps)
237     gst_caps_unref (allowed_caps);
238
239   err = aacEncOpen (&self->enc, 0, GST_AUDIO_INFO_CHANNELS (info));
240   if (err != AACENC_OK) {
241     GST_ERROR_OBJECT (self, "Unable to open encoder: %d", err);
242     return FALSE;
243   }
244
245   aot = AOT_AAC_LC;
246
247   if ((err = aacEncoder_SetParam (self->enc, AACENC_AOT, aot)) != AACENC_OK) {
248     GST_ERROR_OBJECT (self, "Unable to set AOT %d: %d", aot, err);
249     return FALSE;
250   }
251
252   if ((err = aacEncoder_SetParam (self->enc, AACENC_SAMPLERATE,
253               GST_AUDIO_INFO_RATE (info))) != AACENC_OK) {
254     GST_ERROR_OBJECT (self, "Unable to set sample rate %d: %d",
255         GST_AUDIO_INFO_RATE (info), err);
256     return FALSE;
257   }
258
259   if (GST_AUDIO_INFO_CHANNELS (info) == 1) {
260     channel_mode = MODE_1;
261     self->need_reorder = FALSE;
262     self->aac_positions = NULL;
263   } else {
264     gint in_channels = GST_AUDIO_INFO_CHANNELS (info);
265     const GstAudioChannelPosition *in_positions =
266         &GST_AUDIO_INFO_POSITION (info, 0);
267     guint64 in_channel_mask;
268     const GstFdkAacChannelLayout *layout;
269
270     gst_audio_channel_positions_to_mask (in_positions, in_channels, FALSE,
271         &in_channel_mask);
272
273     for (layout = channel_layouts; layout->channels; layout++) {
274       gint channels = layout->channels;
275       const GstAudioChannelPosition *positions = layout->positions;
276       guint64 channel_mask;
277
278       if (channels != in_channels)
279         continue;
280
281       gst_audio_channel_positions_to_mask (positions, channels, FALSE,
282           &channel_mask);
283       if (channel_mask != in_channel_mask)
284         continue;
285
286       channel_mode = layout->mode;
287       self->need_reorder = memcmp (positions, in_positions,
288           channels * sizeof *positions) != 0;
289       self->aac_positions = positions;
290       break;
291     }
292
293     if (!layout->channels) {
294       GST_ERROR_OBJECT (self, "Couldn't find a valid channel layout");
295       return FALSE;
296     }
297   }
298
299   if ((err = aacEncoder_SetParam (self->enc, AACENC_CHANNELMODE,
300               channel_mode)) != AACENC_OK) {
301     GST_ERROR_OBJECT (self, "Unable to set channel mode %d: %d", channel_mode,
302         err);
303     return FALSE;
304   }
305
306   /* MPEG channel order */
307   if ((err = aacEncoder_SetParam (self->enc, AACENC_CHANNELORDER,
308               0)) != AACENC_OK) {
309     GST_ERROR_OBJECT (self, "Unable to set channel order %d: %d", channel_mode,
310         err);
311     return FALSE;
312   }
313
314   bitrate = self->bitrate;
315   /* See
316    * http://wiki.hydrogenaud.io/index.php?title=Fraunhofer_FDK_AAC#Recommended_Sampling_Rate_and_Bitrate_Combinations
317    */
318   if (bitrate == 0) {
319     if (GST_AUDIO_INFO_CHANNELS (info) == 1) {
320       if (GST_AUDIO_INFO_RATE (info) < 16000) {
321         bitrate = 8000;
322       } else if (GST_AUDIO_INFO_RATE (info) == 16000) {
323         bitrate = 16000;
324       } else if (GST_AUDIO_INFO_RATE (info) < 32000) {
325         bitrate = 24000;
326       } else if (GST_AUDIO_INFO_RATE (info) == 32000) {
327         bitrate = 32000;
328       } else if (GST_AUDIO_INFO_RATE (info) <= 44100) {
329         bitrate = 56000;
330       } else {
331         bitrate = 160000;
332       }
333     } else if (GST_AUDIO_INFO_CHANNELS (info) == 2) {
334       if (GST_AUDIO_INFO_RATE (info) < 16000) {
335         bitrate = 16000;
336       } else if (GST_AUDIO_INFO_RATE (info) == 16000) {
337         bitrate = 24000;
338       } else if (GST_AUDIO_INFO_RATE (info) < 22050) {
339         bitrate = 32000;
340       } else if (GST_AUDIO_INFO_RATE (info) < 32000) {
341         bitrate = 40000;
342       } else if (GST_AUDIO_INFO_RATE (info) == 32000) {
343         bitrate = 96000;
344       } else if (GST_AUDIO_INFO_RATE (info) <= 44100) {
345         bitrate = 112000;
346       } else {
347         bitrate = 320000;
348       }
349     } else {
350       /* 5, 5.1 */
351       if (GST_AUDIO_INFO_RATE (info) < 32000) {
352         bitrate = 160000;
353       } else if (GST_AUDIO_INFO_RATE (info) <= 44100) {
354         bitrate = 240000;
355       } else {
356         bitrate = 320000;
357       }
358     }
359   }
360
361   if ((err = aacEncoder_SetParam (self->enc, AACENC_TRANSMUX,
362               transmux)) != AACENC_OK) {
363     GST_ERROR_OBJECT (self, "Unable to set transmux %d: %d", transmux, err);
364     return FALSE;
365   }
366
367   if ((err = aacEncoder_SetParam (self->enc, AACENC_BITRATE,
368               bitrate)) != AACENC_OK) {
369     GST_ERROR_OBJECT (self, "Unable to set bitrate %d: %d", bitrate, err);
370     return FALSE;
371   }
372
373   if ((err = aacEncEncode (self->enc, NULL, NULL, NULL, NULL)) != AACENC_OK) {
374     GST_ERROR_OBJECT (self, "Unable to initialize encoder: %d", err);
375     return FALSE;
376   }
377
378   if ((err = aacEncInfo (self->enc, &enc_info)) != AACENC_OK) {
379     GST_ERROR_OBJECT (self, "Unable to get encoder info: %d", err);
380     return FALSE;
381   }
382
383   gst_audio_encoder_set_frame_max (enc, 1);
384   gst_audio_encoder_set_frame_samples_min (enc, enc_info.frameLength);
385   gst_audio_encoder_set_frame_samples_max (enc, enc_info.frameLength);
386   gst_audio_encoder_set_hard_min (enc, FALSE);
387   self->outbuf_size = enc_info.maxOutBufBytes;
388   self->samples_per_frame = enc_info.frameLength;
389
390   src_caps = gst_caps_new_simple ("audio/mpeg",
391       "mpegversion", G_TYPE_INT, mpegversion,
392       "channels", G_TYPE_INT, GST_AUDIO_INFO_CHANNELS (info),
393       "framed", G_TYPE_BOOLEAN, TRUE,
394       "rate", G_TYPE_INT, GST_AUDIO_INFO_RATE (info), NULL);
395
396   /* raw */
397   if (transmux == 0) {
398     GstBuffer *codec_data =
399         gst_buffer_new_wrapped (g_memdup (enc_info.confBuf, enc_info.confSize),
400         enc_info.confSize);
401     gst_caps_set_simple (src_caps, "codec_data", GST_TYPE_BUFFER, codec_data,
402         "stream-format", G_TYPE_STRING, "raw", NULL);
403     gst_buffer_unref (codec_data);
404   } else if (transmux == 1) {
405     gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adif",
406         NULL);
407   } else if (transmux == 2) {
408     gst_caps_set_simple (src_caps, "stream-format", G_TYPE_STRING, "adts",
409         NULL);
410   } else {
411     g_assert_not_reached ();
412   }
413
414   gst_codec_utils_aac_caps_set_level_and_profile (src_caps, enc_info.confBuf,
415       enc_info.confSize);
416
417   ret = gst_audio_encoder_set_output_format (enc, src_caps);
418   gst_caps_unref (src_caps);
419
420   return ret;
421 }
422
423 static GstFlowReturn
424 gst_fdkaacenc_handle_frame (GstAudioEncoder * enc, GstBuffer * inbuf)
425 {
426   GstFdkAacEnc *self = GST_FDKAACENC (enc);
427   GstFlowReturn ret = GST_FLOW_OK;
428   GstAudioInfo *info;
429   GstMapInfo imap, omap;
430   GstBuffer *outbuf;
431   AACENC_BufDesc in_desc = { 0 };
432   AACENC_BufDesc out_desc = { 0 };
433   AACENC_InArgs in_args = { 0 };
434   AACENC_OutArgs out_args = { 0 };
435   gint in_id = IN_AUDIO_DATA, out_id = OUT_BITSTREAM_DATA;
436   gint in_sizes, out_sizes;
437   gint in_el_sizes, out_el_sizes;
438   AACENC_ERROR err;
439
440   info = gst_audio_encoder_get_audio_info (enc);
441
442   if (inbuf) {
443     if (self->need_reorder) {
444       inbuf = gst_buffer_copy (inbuf);
445       gst_buffer_map (inbuf, &imap, GST_MAP_READWRITE);
446       gst_audio_reorder_channels (imap.data, imap.size,
447           GST_AUDIO_INFO_FORMAT (info), GST_AUDIO_INFO_CHANNELS (info),
448           &GST_AUDIO_INFO_POSITION (info, 0), self->aac_positions);
449     } else {
450       gst_buffer_map (inbuf, &imap, GST_MAP_READ);
451     }
452
453     in_args.numInSamples = imap.size / GST_AUDIO_INFO_BPS (info);
454
455     in_sizes = imap.size;
456     in_el_sizes = GST_AUDIO_INFO_BPS (info);
457     in_desc.numBufs = 1;
458   } else {
459     in_args.numInSamples = -1;
460
461     in_sizes = 0;
462     in_el_sizes = 0;
463     in_desc.numBufs = 0;
464   }
465   /* We unset is_drained even if there's no inbuf. Basically this is a
466    * workaround for aacEncEncode always producing 1024 bytes even without any
467    * input, thus messing up with the base class counting */
468   self->is_drained = FALSE;
469
470   in_desc.bufferIdentifiers = &in_id;
471   in_desc.bufs = (void *) &imap.data;
472   in_desc.bufSizes = &in_sizes;
473   in_desc.bufElSizes = &in_el_sizes;
474
475   outbuf = gst_audio_encoder_allocate_output_buffer (enc, self->outbuf_size);
476   if (!outbuf) {
477     ret = GST_FLOW_ERROR;
478     goto out;
479   }
480
481   gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
482   out_sizes = omap.size;
483   out_el_sizes = 1;
484   out_desc.bufferIdentifiers = &out_id;
485   out_desc.numBufs = 1;
486   out_desc.bufs = (void *) &omap.data;
487   out_desc.bufSizes = &out_sizes;
488   out_desc.bufElSizes = &out_el_sizes;
489
490   err = aacEncEncode (self->enc, &in_desc, &out_desc, &in_args, &out_args);
491   if (err == AACENC_ENCODE_EOF && !inbuf)
492     goto out;
493   else if (err != AACENC_OK) {
494     GST_ERROR_OBJECT (self, "Failed to encode data: %d", err);
495     ret = GST_FLOW_ERROR;
496     goto out;
497   }
498
499   if (inbuf) {
500     gst_buffer_unmap (inbuf, &imap);
501     if (self->need_reorder)
502       gst_buffer_unref (inbuf);
503     inbuf = NULL;
504   }
505
506   if (!out_args.numOutBytes)
507     goto out;
508
509   gst_buffer_unmap (outbuf, &omap);
510   gst_buffer_set_size (outbuf, out_args.numOutBytes);
511
512   ret = gst_audio_encoder_finish_frame (enc, outbuf, self->samples_per_frame);
513   outbuf = NULL;
514
515 out:
516   if (outbuf) {
517     gst_buffer_unmap (outbuf, &omap);
518     gst_buffer_unref (outbuf);
519   }
520   if (inbuf) {
521     gst_buffer_unmap (inbuf, &imap);
522     if (self->need_reorder)
523       gst_buffer_unref (inbuf);
524   }
525
526   return ret;
527 }
528
529 static void
530 gst_fdkaacenc_flush (GstAudioEncoder * enc)
531 {
532   GstFdkAacEnc *self = GST_FDKAACENC (enc);
533   GstAudioInfo *info = gst_audio_encoder_get_audio_info (enc);
534
535   aacEncClose (&self->enc);
536   self->enc = NULL;
537   self->is_drained = TRUE;
538
539   if (GST_AUDIO_INFO_IS_VALID (info))
540     gst_fdkaacenc_set_format (enc, info);
541 }
542
543 static void
544 gst_fdkaacenc_init (GstFdkAacEnc * self)
545 {
546   self->bitrate = DEFAULT_BITRATE;
547   self->enc = NULL;
548   self->is_drained = TRUE;
549
550   gst_audio_encoder_set_drainable (GST_AUDIO_ENCODER (self), TRUE);
551 }
552
553 static void
554 gst_fdkaacenc_class_init (GstFdkAacEncClass * klass)
555 {
556   GObjectClass *object_class = G_OBJECT_CLASS (klass);
557   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
558   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
559
560   object_class->set_property = GST_DEBUG_FUNCPTR (gst_fdkaacenc_set_property);
561   object_class->get_property = GST_DEBUG_FUNCPTR (gst_fdkaacenc_get_property);
562
563   base_class->start = GST_DEBUG_FUNCPTR (gst_fdkaacenc_start);
564   base_class->stop = GST_DEBUG_FUNCPTR (gst_fdkaacenc_stop);
565   base_class->set_format = GST_DEBUG_FUNCPTR (gst_fdkaacenc_set_format);
566   base_class->getcaps = GST_DEBUG_FUNCPTR (gst_fdkaacenc_get_caps);
567   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_fdkaacenc_handle_frame);
568   base_class->flush = GST_DEBUG_FUNCPTR (gst_fdkaacenc_flush);
569
570   g_object_class_install_property (object_class, PROP_BITRATE,
571       g_param_spec_int ("bitrate",
572           "Bitrate",
573           "Target Audio Bitrate (0 = fixed value based on "
574           " sample rate and channel count)",
575           0, G_MAXINT, DEFAULT_BITRATE,
576           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577
578   gst_element_class_add_static_pad_template (element_class, &sink_template);
579   gst_element_class_add_static_pad_template (element_class, &src_template);
580
581   gst_element_class_set_static_metadata (element_class, "FDK AAC audio encoder",
582       "Codec/Encoder/Audio", "FDK AAC audio encoder",
583       "Sebastian Dröge <sebastian@centricular.com>");
584
585   GST_DEBUG_CATEGORY_INIT (gst_fdkaacenc_debug, "fdkaacenc", 0,
586       "fdkaac encoder");
587 }