Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / ext / faac / gstfaac.c
1 /* GStreamer FAAC (Free AAC Encoder) plugin
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * Copyright (C) 2009 Mark Nauwelaerts <mnauw@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-faac
23  * @title: faac
24  * @see_also: faad
25  *
26  * faac encodes raw audio to AAC (MPEG-4 part 3) streams.
27  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 audiotestsrc wave=sine num-buffers=100 ! audioconvert ! faac ! matroskamux ! filesink location=sine.mkv
31  * ]| Encode a sine beep as aac and write to matroska container.
32  *
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <gst/audio/audio.h>
42 #include <gst/pbutils/codec-utils.h>
43
44 #include "gstfaac.h"
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 /* these don't seem to work? */
60 #if 0
61 "audio/x-raw-int, "
62     "endianness = (int) BYTE_ORDER, "
63     "signed = (boolean) true, "
64     "width = (int) 32, "
65     "depth = (int) { 24, 32 }, "
66     "rate = (int) [ 8000, 96000], "
67     "channels = (int) [ 1, 6]; "
68     "audio/x-raw-float, "
69     "endianness = (int) BYTE_ORDER, "
70     "width = (int) 32, "
71     "rate = (int) [ 8000, 96000], " "channels = (int) [ 1, 6]"
72 #endif
73 #define SRC_CAPS \
74     "audio/mpeg, "                     \
75     "mpegversion = (int) 4, "   \
76     "channels = (int) [ 1, 6 ], "      \
77     "rate = (int) {" SAMPLE_RATES "}, "   \
78     "stream-format = (string) { adts, raw }, " \
79     "base-profile = (string) { main, lc, ssr, ltp }, " \
80     "framed = (boolean) true; " \
81     "audio/mpeg, "                     \
82     "mpegversion = (int) 2, "   \
83     "channels = (int) [ 1, 6 ], "      \
84     "rate = (int) {" SAMPLE_RATES "}, "   \
85     "stream-format = (string) { adts, raw }, " \
86     "profile = (string) { main, lc }," \
87     "framed = (boolean) true; "
88 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
89     GST_PAD_SRC,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (SRC_CAPS));
92
93 enum
94 {
95   PROP_0,
96   PROP_QUALITY,
97   PROP_BITRATE,
98   PROP_RATE_CONTROL,
99   PROP_PROFILE,
100   PROP_TNS,
101   PROP_MIDSIDE,
102   PROP_SHORTCTL
103 };
104
105 enum
106 {
107   VBR = 1,
108   ABR
109 };
110
111 static void gst_faac_set_property (GObject * object,
112     guint prop_id, const GValue * value, GParamSpec * pspec);
113 static void gst_faac_get_property (GObject * object,
114     guint prop_id, GValue * value, GParamSpec * pspec);
115
116 static GstCaps *gst_faac_enc_generate_sink_caps (void);
117 static gboolean gst_faac_configure_source_pad (GstFaac * faac,
118     GstAudioInfo * info);
119
120 static gboolean gst_faac_stop (GstAudioEncoder * enc);
121 static gboolean gst_faac_set_format (GstAudioEncoder * enc,
122     GstAudioInfo * info);
123 static GstFlowReturn gst_faac_handle_frame (GstAudioEncoder * enc,
124     GstBuffer * in_buf);
125
126 GST_DEBUG_CATEGORY_STATIC (faac_debug);
127 #define GST_CAT_DEFAULT faac_debug
128
129 #define FAAC_DEFAULT_QUALITY      100
130 #define FAAC_DEFAULT_BITRATE      128 * 1000
131 #define FAAC_DEFAULT_RATE_CONTROL VBR
132 #define FAAC_DEFAULT_TNS          FALSE
133 #define FAAC_DEFAULT_MIDSIDE      TRUE
134 #define FAAC_DEFAULT_SHORTCTL     SHORTCTL_NORMAL
135
136 #define gst_faac_parent_class parent_class
137 G_DEFINE_TYPE (GstFaac, gst_faac, GST_TYPE_AUDIO_ENCODER);
138 GST_ELEMENT_REGISTER_DEFINE (faac, "faac", GST_RANK_SECONDARY, GST_TYPE_FAAC);
139
140 #define GST_TYPE_FAAC_RATE_CONTROL (gst_faac_brtype_get_type ())
141 static GType
142 gst_faac_brtype_get_type (void)
143 {
144   static GType gst_faac_brtype_type = 0;
145
146   if (!gst_faac_brtype_type) {
147     static const GEnumValue gst_faac_brtype[] = {
148       {VBR, "VBR", "VBR encoding"},
149       {ABR, "ABR", "ABR encoding"},
150       {0, NULL, NULL},
151     };
152
153     gst_faac_brtype_type = g_enum_register_static ("GstFaacBrtype",
154         gst_faac_brtype);
155   }
156
157   return gst_faac_brtype_type;
158 }
159
160 #define GST_TYPE_FAAC_SHORTCTL (gst_faac_shortctl_get_type ())
161 static GType
162 gst_faac_shortctl_get_type (void)
163 {
164   static GType gst_faac_shortctl_type = 0;
165
166   if (!gst_faac_shortctl_type) {
167     static const GEnumValue gst_faac_shortctl[] = {
168       {SHORTCTL_NORMAL, "SHORTCTL_NORMAL", "Normal block type"},
169       {SHORTCTL_NOSHORT, "SHORTCTL_NOSHORT", "No short blocks"},
170       {SHORTCTL_NOLONG, "SHORTCTL_NOLONG", "No long blocks"},
171       {0, NULL, NULL},
172     };
173
174     gst_faac_shortctl_type = g_enum_register_static ("GstFaacShortCtl",
175         gst_faac_shortctl);
176   }
177
178   return gst_faac_shortctl_type;
179 }
180
181 static void
182 gst_faac_class_init (GstFaacClass * klass)
183 {
184   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
186   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
187   GstCaps *sink_caps;
188   GstPadTemplate *sink_templ;
189
190   gobject_class->set_property = gst_faac_set_property;
191   gobject_class->get_property = gst_faac_get_property;
192
193   GST_DEBUG_CATEGORY_INIT (faac_debug, "faac", 0, "AAC encoding");
194
195   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
196
197   sink_caps = gst_faac_enc_generate_sink_caps ();
198   sink_templ = gst_pad_template_new ("sink",
199       GST_PAD_SINK, GST_PAD_ALWAYS, sink_caps);
200   gst_element_class_add_pad_template (gstelement_class, sink_templ);
201   gst_caps_unref (sink_caps);
202
203   gst_element_class_set_static_metadata (gstelement_class, "AAC audio encoder",
204       "Codec/Encoder/Audio",
205       "Free MPEG-2/4 AAC encoder",
206       "Ronald Bultje <rbultje@ronald.bitfreak.net>");
207
208   gst_type_mark_as_plugin_api (GST_TYPE_FAAC_RATE_CONTROL, 0);
209   gst_type_mark_as_plugin_api (GST_TYPE_FAAC_SHORTCTL, 0);
210
211   base_class->stop = GST_DEBUG_FUNCPTR (gst_faac_stop);
212   base_class->set_format = GST_DEBUG_FUNCPTR (gst_faac_set_format);
213   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_faac_handle_frame);
214
215   /* properties */
216   g_object_class_install_property (gobject_class, PROP_QUALITY,
217       g_param_spec_int ("quality", "Quality (%)",
218           "Variable bitrate (VBR) quantizer quality in %", 1, 1000,
219           FAAC_DEFAULT_QUALITY,
220           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221   g_object_class_install_property (gobject_class, PROP_BITRATE,
222       g_param_spec_int ("bitrate", "Bitrate (bps)",
223           "Average Bitrate (ABR) in bits/sec", 8 * 1000, 320 * 1000,
224           FAAC_DEFAULT_BITRATE,
225           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226   g_object_class_install_property (gobject_class, PROP_RATE_CONTROL,
227       g_param_spec_enum ("rate-control", "Rate Control (ABR/VBR)",
228           "Encoding bitrate type (VBR/ABR)", GST_TYPE_FAAC_RATE_CONTROL,
229           FAAC_DEFAULT_RATE_CONTROL,
230           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231   g_object_class_install_property (gobject_class, PROP_TNS,
232       g_param_spec_boolean ("tns", "TNS", "Use temporal noise shaping",
233           FAAC_DEFAULT_TNS,
234           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235   g_object_class_install_property (gobject_class, PROP_MIDSIDE,
236       g_param_spec_boolean ("midside", "Midside", "Allow mid/side encoding",
237           FAAC_DEFAULT_MIDSIDE,
238           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239   g_object_class_install_property (gobject_class, PROP_SHORTCTL,
240       g_param_spec_enum ("shortctl", "Block type",
241           "Block type encorcing",
242           GST_TYPE_FAAC_SHORTCTL, FAAC_DEFAULT_SHORTCTL,
243           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 }
245
246 static void
247 gst_faac_init (GstFaac * faac)
248 {
249   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (faac));
250 }
251
252 static void
253 gst_faac_close_encoder (GstFaac * faac)
254 {
255   if (faac->handle)
256     faacEncClose (faac->handle);
257   faac->handle = NULL;
258 }
259
260 static gboolean
261 gst_faac_stop (GstAudioEncoder * enc)
262 {
263   GstFaac *faac = GST_FAAC (enc);
264
265   GST_DEBUG_OBJECT (faac, "stop");
266   gst_faac_close_encoder (faac);
267   return TRUE;
268 }
269
270 static const GstAudioChannelPosition aac_channel_positions[][8] = {
271   {GST_AUDIO_CHANNEL_POSITION_MONO},
272   {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
273       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
274   {
275         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
276         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
277         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
278       },
279   {
280         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
281         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
282         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
283       GST_AUDIO_CHANNEL_POSITION_REAR_CENTER},
284   {
285         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
286         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
287         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
288         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
289       GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
290   {
291         GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
292         GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
293         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
294         GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
295         GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
296       GST_AUDIO_CHANNEL_POSITION_LFE1}
297 };
298
299 static GstCaps *
300 gst_faac_enc_generate_sink_caps (void)
301 {
302   GstCaps *caps = gst_caps_new_empty ();
303   GstStructure *s, *t;
304   gint i, c;
305   static const int rates[] = {
306     8000, 11025, 12000, 16000, 22050, 24000,
307     32000, 44100, 48000, 64000, 88200, 96000
308   };
309   GValue rates_arr = { 0, };
310   GValue tmp_v = { 0, };
311
312   g_value_init (&rates_arr, GST_TYPE_LIST);
313   g_value_init (&tmp_v, G_TYPE_INT);
314   for (i = 0; i < G_N_ELEMENTS (rates); i++) {
315     g_value_set_int (&tmp_v, rates[i]);
316     gst_value_list_append_value (&rates_arr, &tmp_v);
317   }
318   g_value_unset (&tmp_v);
319
320   s = gst_structure_new ("audio/x-raw",
321       "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
322       "layout", G_TYPE_STRING, "interleaved", NULL);
323   gst_structure_set_value (s, "rate", &rates_arr);
324
325   t = gst_structure_copy (s);
326   gst_structure_set (t, "channels", G_TYPE_INT, 1, NULL);
327   gst_caps_append_structure (caps, t);
328
329   for (i = 2; i <= 6; i++) {
330     guint64 channel_mask = 0;
331     t = gst_structure_copy (s);
332
333     gst_structure_set (t, "channels", G_TYPE_INT, i, NULL);
334     for (c = 0; c < i; c++)
335       channel_mask |= G_GUINT64_CONSTANT (1) << aac_channel_positions[i - 1][c];
336
337     gst_structure_set (t, "channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
338     gst_caps_append_structure (caps, t);
339   }
340   gst_structure_free (s);
341   g_value_unset (&rates_arr);
342
343   GST_DEBUG ("Generated sinkcaps: %" GST_PTR_FORMAT, caps);
344   return caps;
345 }
346
347 static void
348 gst_faac_set_tags (GstFaac * faac)
349 {
350   GstTagList *taglist;
351
352   /* create a taglist and add a bitrate tag to it */
353   taglist = gst_tag_list_new_empty ();
354   gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
355       GST_TAG_BITRATE, faac->bitrate, NULL);
356
357   gst_audio_encoder_merge_tags (GST_AUDIO_ENCODER (faac), taglist,
358       GST_TAG_MERGE_REPLACE);
359
360   gst_tag_list_unref (taglist);
361 }
362
363 static gboolean
364 gst_faac_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
365 {
366   GstFaac *faac = GST_FAAC (enc);
367   gint width;
368   gulong fmt = 0;
369   gboolean result = FALSE;
370
371   /* base class takes care */
372   width = GST_AUDIO_INFO_WIDTH (info);
373
374   if (GST_AUDIO_INFO_IS_INTEGER (info)) {
375     switch (width) {
376       case 16:
377         fmt = FAAC_INPUT_16BIT;
378         break;
379       case 24:
380       case 32:
381         fmt = FAAC_INPUT_32BIT;
382         break;
383       default:
384         g_return_val_if_reached (FALSE);
385     }
386   } else {
387     fmt = FAAC_INPUT_FLOAT;
388   }
389
390   faac->format = fmt;
391
392   /* finish up */
393   result = gst_faac_configure_source_pad (faac, info);
394   if (!result)
395     goto done;
396
397   gst_faac_set_tags (faac);
398
399   /* report needs to base class */
400   gst_audio_encoder_set_frame_samples_min (enc, faac->samples);
401   gst_audio_encoder_set_frame_samples_max (enc, faac->samples);
402   gst_audio_encoder_set_frame_max (enc, 1);
403
404 done:
405   return result;
406 }
407
408 /* check downstream caps to configure format */
409 static void
410 gst_faac_negotiate (GstFaac * faac)
411 {
412   GstCaps *caps;
413
414   /* default setup */
415   faac->profile = LOW;
416   faac->mpegversion = 4;
417   faac->outputformat = 0;
418
419   caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (faac));
420
421   GST_DEBUG_OBJECT (faac, "allowed caps: %" GST_PTR_FORMAT, caps);
422
423   if (caps && gst_caps_get_size (caps) > 0) {
424     GstStructure *s = gst_caps_get_structure (caps, 0);
425     const gchar *str = NULL;
426     gint i = 4;
427
428     if ((str = gst_structure_get_string (s, "stream-format"))) {
429       if (strcmp (str, "adts") == 0) {
430         GST_DEBUG_OBJECT (faac, "use ADTS format for output");
431         faac->outputformat = 1;
432       } else if (strcmp (str, "raw") == 0) {
433         GST_DEBUG_OBJECT (faac, "use RAW format for output");
434         faac->outputformat = 0;
435       } else {
436         GST_DEBUG_OBJECT (faac, "unknown stream-format: %s", str);
437         faac->outputformat = 0;
438       }
439     }
440
441     if ((str = gst_structure_get_string (s, "profile"))) {
442       if (strcmp (str, "main") == 0) {
443         faac->profile = MAIN;
444       } else if (strcmp (str, "lc") == 0) {
445         faac->profile = LOW;
446       } else if (strcmp (str, "ssr") == 0) {
447         faac->profile = SSR;
448       } else if (strcmp (str, "ltp") == 0) {
449         faac->profile = LTP;
450       } else {
451         faac->profile = LOW;
452       }
453     }
454
455     if (!gst_structure_get_int (s, "mpegversion", &i) || i == 4) {
456       faac->mpegversion = 4;
457     } else {
458       faac->mpegversion = 2;
459     }
460   }
461
462   if (caps)
463     gst_caps_unref (caps);
464 }
465
466 static gboolean
467 gst_faac_open_encoder (GstFaac * faac, GstAudioInfo * info)
468 {
469   faacEncHandle *handle;
470   faacEncConfiguration *conf;
471   guint maxbitrate;
472   gulong samples, bytes;
473
474   g_return_val_if_fail (info->rate != 0 && info->channels != 0, FALSE);
475
476   /* clean up in case of re-configure */
477   gst_faac_close_encoder (faac);
478
479   if (!(handle = faacEncOpen (info->rate, info->channels, &samples, &bytes)))
480     goto setup_failed;
481
482   /* mind channel count */
483   samples /= info->channels;
484
485   /* record */
486   faac->handle = handle;
487   faac->samples = samples;
488   faac->bytes = bytes;
489
490   GST_DEBUG_OBJECT (faac, "faac needs samples %d, output size %d",
491       faac->samples, faac->bytes);
492
493   /* we negotiated caps update current configuration */
494   conf = faacEncGetCurrentConfiguration (faac->handle);
495   conf->mpegVersion = (faac->mpegversion == 4) ? MPEG4 : MPEG2;
496   conf->aacObjectType = faac->profile;
497   conf->allowMidside = faac->midside;
498   conf->useLfe = 0;
499   conf->useTns = faac->tns;
500
501   if (faac->brtype == VBR) {
502     conf->quantqual = faac->quality;
503   } else if (faac->brtype == ABR) {
504     conf->bitRate = faac->bitrate / info->channels;
505   }
506
507   conf->inputFormat = faac->format;
508   conf->outputFormat = faac->outputformat;
509   conf->shortctl = faac->shortctl;
510
511   /* check, warn and correct if the max bitrate for the given samplerate is
512    * exceeded. Maximum of 6144 bit for a channel */
513   maxbitrate =
514       (unsigned int) (6144.0 * (double) info->rate / (double) 1024.0 + .5);
515   if (conf->bitRate > maxbitrate) {
516     GST_ELEMENT_WARNING (faac, RESOURCE, SETTINGS, (NULL),
517         ("bitrate %lu exceeds maximum allowed bitrate of %u for samplerate %d. "
518             "Setting bitrate to %u", conf->bitRate, maxbitrate,
519             info->rate, maxbitrate));
520     conf->bitRate = maxbitrate;
521   }
522
523   /* default 0 to start with, libfaac chooses based on bitrate */
524   conf->bandWidth = 0;
525
526   if (!faacEncSetConfiguration (faac->handle, conf))
527     goto setup_failed;
528
529   /* let's see what really happened,
530    * note that this may not really match desired rate */
531   GST_DEBUG_OBJECT (faac, "average bitrate: %lu kbps",
532       (conf->bitRate + 500) / 1000 * info->channels);
533   GST_DEBUG_OBJECT (faac, "quantization quality: %ld", conf->quantqual);
534   GST_DEBUG_OBJECT (faac, "bandwidth: %d Hz", conf->bandWidth);
535
536   return TRUE;
537
538   /* ERRORS */
539 setup_failed:
540   {
541     GST_ELEMENT_ERROR (faac, LIBRARY, SETTINGS, (NULL), (NULL));
542     return FALSE;
543   }
544 }
545
546 static gboolean
547 gst_faac_configure_source_pad (GstFaac * faac, GstAudioInfo * info)
548 {
549   GstCaps *srccaps;
550   gboolean ret;
551
552   /* negotiate stream format */
553   gst_faac_negotiate (faac);
554
555   if (!gst_faac_open_encoder (faac, info))
556     goto set_failed;
557
558   /* now create a caps for it all */
559   srccaps = gst_caps_new_simple ("audio/mpeg",
560       "mpegversion", G_TYPE_INT, faac->mpegversion,
561       "channels", G_TYPE_INT, info->channels,
562       "rate", G_TYPE_INT, info->rate,
563       "stream-format", G_TYPE_STRING, (faac->outputformat ? "adts" : "raw"),
564       "framed", G_TYPE_BOOLEAN, TRUE, NULL);
565
566   /* DecoderSpecificInfo is only available for mpegversion=4 */
567   if (faac->mpegversion == 4) {
568     guint8 *config = NULL;
569     gulong config_len = 0;
570
571     /* get the config string */
572     GST_DEBUG_OBJECT (faac, "retrieving decoder info");
573     faacEncGetDecoderSpecificInfo (faac->handle, &config, &config_len);
574
575     if (!gst_codec_utils_aac_caps_set_level_and_profile (srccaps, config,
576             config_len)) {
577       free (config);
578       gst_caps_unref (srccaps);
579       goto invalid_codec_data;
580     }
581
582     if (!faac->outputformat) {
583       GstBuffer *codec_data;
584
585       /* copy it into a buffer */
586       codec_data = gst_buffer_new_and_alloc (config_len);
587       gst_buffer_fill (codec_data, 0, config, config_len);
588
589       /* add to caps */
590       gst_caps_set_simple (srccaps,
591           "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
592
593       gst_buffer_unref (codec_data);
594     }
595
596     free (config);
597   } else {
598     const gchar *profile;
599
600     /* Add least add the profile to the caps */
601     switch (faac->profile) {
602       case MAIN:
603         profile = "main";
604         break;
605       case LTP:
606         profile = "ltp";
607         break;
608       case SSR:
609         profile = "ssr";
610         break;
611       case LOW:
612       default:
613         profile = "lc";
614         break;
615     }
616     gst_caps_set_simple (srccaps, "profile", G_TYPE_STRING, profile, NULL);
617     /* FIXME: How to get the profile for mpegversion==2? */
618   }
619
620   GST_DEBUG_OBJECT (faac, "src pad caps: %" GST_PTR_FORMAT, srccaps);
621
622   ret = gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (faac), srccaps);
623   gst_caps_unref (srccaps);
624
625   return ret;
626
627   /* ERROR */
628 set_failed:
629   {
630     GST_WARNING_OBJECT (faac, "Faac doesn't support the current configuration");
631     return FALSE;
632   }
633 invalid_codec_data:
634   {
635     GST_ERROR_OBJECT (faac, "Invalid codec data");
636     return FALSE;
637   }
638 }
639
640 static GstFlowReturn
641 gst_faac_handle_frame (GstAudioEncoder * enc, GstBuffer * in_buf)
642 {
643   GstFaac *faac = GST_FAAC (enc);
644   GstFlowReturn ret = GST_FLOW_OK;
645   GstBuffer *out_buf;
646   gsize size, ret_size;
647   int enc_ret;
648   GstMapInfo map, omap;
649   guint8 *data;
650   GstAudioInfo *info =
651       gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (faac));
652
653   out_buf = gst_buffer_new_and_alloc (faac->bytes);
654   gst_buffer_map (out_buf, &omap, GST_MAP_WRITE);
655
656   if (G_LIKELY (in_buf)) {
657     if (memcmp (info->position, aac_channel_positions[info->channels - 1],
658             sizeof (GstAudioChannelPosition) * info->channels) != 0) {
659       in_buf = gst_buffer_make_writable (in_buf);
660       gst_audio_buffer_reorder_channels (in_buf, info->finfo->format,
661           info->channels, info->position,
662           aac_channel_positions[info->channels - 1]);
663     }
664     gst_buffer_map (in_buf, &map, GST_MAP_READ);
665     data = map.data;
666     size = map.size;
667   } else {
668     data = NULL;
669     size = 0;
670   }
671
672   if (G_UNLIKELY ((enc_ret = faacEncEncode (faac->handle, (gint32 *) data,
673                   size / (info->finfo->width / 8), omap.data, omap.size)) < 0))
674     goto encode_failed;
675   ret_size = enc_ret;
676
677   if (in_buf)
678     gst_buffer_unmap (in_buf, &map);
679
680   GST_LOG_OBJECT (faac, "encoder return: %" G_GSIZE_FORMAT, ret_size);
681
682   if (ret_size > 0) {
683     gst_buffer_unmap (out_buf, &omap);
684     gst_buffer_resize (out_buf, 0, ret_size);
685     ret = gst_audio_encoder_finish_frame (enc, out_buf, faac->samples);
686   } else {
687     gst_buffer_unmap (out_buf, &omap);
688     gst_buffer_unref (out_buf);
689     /* re-create encoder after final flush */
690     if (!in_buf) {
691       GST_DEBUG_OBJECT (faac, "flushed; recreating encoder");
692       gst_faac_close_encoder (faac);
693       if (!gst_faac_open_encoder (faac, gst_audio_encoder_get_audio_info (enc)))
694         ret = GST_FLOW_ERROR;
695     }
696   }
697
698   return ret;
699
700   /* ERRORS */
701 encode_failed:
702   {
703     GST_ELEMENT_ERROR (faac, LIBRARY, ENCODE, (NULL), (NULL));
704     if (in_buf)
705       gst_buffer_unmap (in_buf, &map);
706     gst_buffer_unmap (out_buf, &omap);
707     gst_buffer_unref (out_buf);
708     return GST_FLOW_ERROR;
709   }
710 }
711
712 static void
713 gst_faac_set_property (GObject * object,
714     guint prop_id, const GValue * value, GParamSpec * pspec)
715 {
716   GstFaac *faac = GST_FAAC (object);
717
718   GST_OBJECT_LOCK (faac);
719
720   switch (prop_id) {
721     case PROP_QUALITY:
722       faac->quality = g_value_get_int (value);
723       break;
724     case PROP_BITRATE:
725       faac->bitrate = g_value_get_int (value);
726       break;
727     case PROP_RATE_CONTROL:
728       faac->brtype = g_value_get_enum (value);
729       break;
730     case PROP_TNS:
731       faac->tns = g_value_get_boolean (value);
732       break;
733     case PROP_MIDSIDE:
734       faac->midside = g_value_get_boolean (value);
735       break;
736     case PROP_SHORTCTL:
737       faac->shortctl = g_value_get_enum (value);
738       break;
739     default:
740       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
741       break;
742   }
743
744   GST_OBJECT_UNLOCK (faac);
745 }
746
747 static void
748 gst_faac_get_property (GObject * object,
749     guint prop_id, GValue * value, GParamSpec * pspec)
750 {
751   GstFaac *faac = GST_FAAC (object);
752
753   GST_OBJECT_LOCK (faac);
754
755   switch (prop_id) {
756     case PROP_QUALITY:
757       g_value_set_int (value, faac->quality);
758       break;
759     case PROP_BITRATE:
760       g_value_set_int (value, faac->bitrate);
761       break;
762     case PROP_RATE_CONTROL:
763       g_value_set_enum (value, faac->brtype);
764       break;
765     case PROP_TNS:
766       g_value_set_boolean (value, faac->tns);
767       break;
768     case PROP_MIDSIDE:
769       g_value_set_boolean (value, faac->midside);
770       break;
771     case PROP_SHORTCTL:
772       g_value_set_enum (value, faac->shortctl);
773       break;
774     default:
775       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
776       break;
777   }
778
779   GST_OBJECT_UNLOCK (faac);
780 }
781
782 static gboolean
783 plugin_init (GstPlugin * plugin)
784 {
785   return GST_ELEMENT_REGISTER (faac, plugin);
786 }
787
788 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
789     GST_VERSION_MINOR,
790     faac,
791     "Free AAC Encoder (FAAC)",
792     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)