From patchwork Tue Apr 19 08:26:25 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 1085 Return-Path: Delivered-To: unknown Received: from imap.gmail.com (74.125.159.109) by localhost6.localdomain6 with IMAP4-SSL; 08 Jun 2011 14:48:58 -0000 Delivered-To: patches@linaro.org Received: by 10.224.67.148 with SMTP id r20cs61732qai; Tue, 19 Apr 2011 01:26:28 -0700 (PDT) Received: by 10.68.7.74 with SMTP id h10mr8402403pba.350.1303201587349; Tue, 19 Apr 2011 01:26:27 -0700 (PDT) Received: from mail-pz0-f44.google.com (mail-pz0-f44.google.com [209.85.210.44]) by mx.google.com with ESMTPS id q4si12895161pbf.23.2011.04.19.01.26.25 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 19 Apr 2011 01:26:26 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.210.44 is neither permitted nor denied by best guess record for domain of benjamin.gaignard@linaro.org) client-ip=209.85.210.44; Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.210.44 is neither permitted nor denied by best guess record for domain of benjamin.gaignard@linaro.org) smtp.mail=benjamin.gaignard@linaro.org Received: by pzk30 with SMTP id 30so4315050pzk.17 for ; Tue, 19 Apr 2011 01:26:25 -0700 (PDT) MIME-Version: 1.0 Received: by 10.142.204.20 with SMTP id b20mr3261132wfg.217.1303201585778; Tue, 19 Apr 2011 01:26:25 -0700 (PDT) Received: by 10.142.132.3 with HTTP; Tue, 19 Apr 2011 01:26:25 -0700 (PDT) Date: Tue, 19 Apr 2011 10:26:25 +0200 Message-ID: Subject: [Gstreamer] new vo-aacenc plugin From: Benjamin Gaignard To: Patch Tracking Hi, This patch add the new vo-aacenc plugin developped by Kan Hu. AAC encoder is based on vo-aacenc library and will be in released in gst-plugin-bad-0.10.23 https://bugzilla.gnome.org/show_bug.cgi?id=647748 Benjamin >From 95343e3ad9af7eecb2be61b1eb84c0fcb54a48db Mon Sep 17 00:00:00 2001 From: benjamin gaignard Date: Mon, 18 Apr 2011 17:19:00 +0200 Subject: [PATCH] [voaacenc] add new plugin for audio AAC encoder based on vo-aacenc lib add plugin and unit --- configure.ac | 8 + ext/Makefile.am | 7 + ext/voaacenc/Makefile.am | 18 + ext/voaacenc/gstvoaac.c | 38 +++ ext/voaacenc/gstvoaacenc.c | 665 +++++++++++++++++++++++++++++++++++++++ ext/voaacenc/gstvoaacenc.h | 81 +++++ tests/check/Makefile.am | 11 + tests/check/elements/voaacenc.c | 287 +++++++++++++++++ 8 files changed, 1115 insertions(+), 0 deletions(-) create mode 100644 ext/voaacenc/Makefile.am create mode 100644 ext/voaacenc/gstvoaac.c create mode 100644 ext/voaacenc/gstvoaacenc.c create mode 100644 ext/voaacenc/gstvoaacenc.h create mode 100644 tests/check/elements/voaacenc.c diff --git a/configure.ac b/configure.ac index 6b4bf57..7742eaa 100644 --- a/configure.ac +++ b/configure.ac @@ -569,6 +569,12 @@ AG_GST_CHECK_FEATURE(AMRWB, [amrwb library], amrwbenc, [ AC_SUBST(AMRWB_LIBS)) ]) +dnl *** aac-enc *** +translit(dnm, m, l) AM_CONDITIONAL(USE_VOAACENC, true) +AG_GST_CHECK_FEATURE(VOAACENC, [vo-aacenc library], vo-aacenc, [ + AG_GST_PKG_CHECK_MODULES(VOAACENC, vo-aacenc >= 0.1.0) +]) + dnl *** apexsink *** translit(dnm, m, l) AM_CONDITIONAL(USE_APEXSINK, true) AG_GST_CHECK_FEATURE(APEXSINK, [AirPort Express Wireless sink], apexsink, [ @@ -1585,6 +1591,7 @@ dnl but we still need to set the conditionals AM_CONDITIONAL(USE_ASSRENDER, false) AM_CONDITIONAL(USE_AMRWB, false) +AM_CONDITIONAL(USE_VOAACENC, false) AM_CONDITIONAL(USE_APEXSINK, false) AM_CONDITIONAL(USE_BZ2, false) AM_CONDITIONAL(USE_CDAUDIO, false) @@ -1820,6 +1827,7 @@ tests/examples/mxf/Makefile tests/examples/scaletempo/Makefile tests/icles/Makefile ext/amrwbenc/Makefile +ext/voaacenc/Makefile ext/assrender/Makefile ext/apexsink/Makefile ext/bz2/Makefile diff --git a/ext/Makefile.am b/ext/Makefile.am index 9b670d4..3e4beb2 100644 --- a/ext/Makefile.am +++ b/ext/Makefile.am @@ -112,6 +112,12 @@ else FAAD_DIR= endif +if USE_VOAACENC + VOAACENC_DIR=voaacenc +else + VOAACENC_DIR= +endif + if USE_FLITE FLITE_DIR=flite else @@ -368,6 +374,7 @@ endif SUBDIRS=\ + $(VOAACENC_DIR) \ $(ASSRENDER_DIR) \ $(AMRWB_DIR) \ $(APEXSINK_DIR) \ diff --git a/ext/voaacenc/Makefile.am b/ext/voaacenc/Makefile.am new file mode 100644 index 0000000..7506490 --- /dev/null +++ b/ext/voaacenc/Makefile.am @@ -0,0 +1,18 @@ +plugin_LTLIBRARIES = libgstvoaacenc.la + +libgstvoaacenc_la_SOURCES = \ + gstvoaac.c \ + gstvoaacenc.c + +libgstvoaacenc_la_CFLAGS = $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(VOAACENC_CFLAGS) +libgstvoaacenc_la_LIBADD = -lgstaudio-$(GST_MAJORMINOR) \ + $(GST_BASE_LIBS) $(GST_LIBS) $(VOAACENC_LIBS) +libgstvoaacenc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) +libgstvoaacenc_la_LIBTOOLFLAGS = --tag=disable-static + +noinst_HEADERS = \ + gstvoaacenc.h + +presetdir = $(datadir)/gstreamer-$(GST_MAJORMINOR)/presets + +EXTRA_DIST = $(preset_DATA) diff --git a/ext/voaacenc/gstvoaac.c b/ext/voaacenc/gstvoaac.c new file mode 100644 index 0000000..30b5df9 --- /dev/null +++ b/ext/voaacenc/gstvoaac.c @@ -0,0 +1,38 @@ +/* GStreamer AAC encoder plugin + * Copyright (C) 2011 Kan Hu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstvoaacenc.h" + +static gboolean +plugin_init (GstPlugin * plugin) +{ + return gst_element_register (plugin, "voaacenc", + GST_RANK_SECONDARY, GST_TYPE_VOAACENC); +} + + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "voaacenc", + "AAC audio encoder", + plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN); diff --git a/ext/voaacenc/gstvoaacenc.c b/ext/voaacenc/gstvoaacenc.c new file mode 100644 index 0000000..ffad419 --- /dev/null +++ b/ext/voaacenc/gstvoaacenc.c @@ -0,0 +1,665 @@ +/* GStreamer AAC encoder plugin + * Copyright (C) 2011 Kan Hu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/** + * SECTION:element-voaacenc + * + * AAC audio encoder based on vo-aacenc library + * vo-aacenc library source file. + * + * + * Example launch line + * |[ + * gst-launch filesrc location=abc.wav ! wavparse ! audioresample ! audioconvert ! voaacenc ! filesink location=abc.aac + * ]| + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include + +#include "gstvoaacenc.h" + +#define VOAAC_ENC_DEFAULT_BITRATE (128000) +#define VOAAC_ENC_DEFAULT_CHANNELS (2) +#define VOAAC_ENC_DEFAULT_RATE (44100) +#define VOAAC_ENC_DEFAULT_OUTPUTFORMAT (0) /* RAW */ +#define VOAAC_ENC_MPEGVERSION (4) +#define VOAAC_ENC_CODECDATA_LEN (2) +#define VOAAC_ENC_BITS_PER_SAMPLE (16) + +enum +{ + PROP_0, + PROP_BITRATE +}; + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "width = (int) 16, " + "depth = (int) 16, " + "signed = (boolean) TRUE, " + "endianness = (int) BYTE_ORDER, " + "rate = (int) [8000, 96000], " "channels = (int) [1, 6]") + ); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/mpeg, " + "mpegversion = (int) 4, " + "rate = (int) [8000, 96000], " + "channels = (int) [1, 6], " "stream-format = (string) { adts, raw } ") + ); + +GST_DEBUG_CATEGORY_STATIC (gst_voaacenc_debug); +#define GST_CAT_DEFAULT gst_voaacenc_debug + +static void gst_voaacenc_finalize (GObject * object); + +static GstFlowReturn gst_voaacenc_chain (GstPad * pad, GstBuffer * buffer); +static gboolean gst_voaacenc_setcaps (GstPad * pad, GstCaps * caps); +static GstStateChangeReturn gst_voaacenc_state_change (GstElement * element, + GstStateChange transition); +static gboolean voaacenc_core_init (GstVoAacEnc * voaacenc); +static gboolean voaacenc_core_set_parameter (GstVoAacEnc * voaacenc); +static void voaacenc_core_uninit (GstVoAacEnc * voaacenc); +static GstCaps *gst_voaacenc_getcaps (GstPad * pad); +static GstCaps *gst_voaacenc_create_source_pad_caps (GstVoAacEnc * voaacenc); +static gint voaacenc_get_rate_index (gint rate); + +#define VOAAC_ENC_MAX_CHANNELS 6 + +/* describe the channels position */ +const GstAudioChannelPosition + gst_voaacenc_channel_position[][VOAAC_ENC_MAX_CHANNELS] = { + { /* 1 ch: Mono */ + GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}, + { /* 2 ch: front left + front right (front stereo) */ + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, + { /* 3 ch: front center + front stereo */ + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, + { /* 4 ch: front center + front stereo + back center */ + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, + GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, + { /* 5 ch: front center + front stereo + back stereo */ + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, + GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, + GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, + { /* 6ch: front center + front stereo + back stereo + LFE */ + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, + GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, + GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, + GST_AUDIO_CHANNEL_POSITION_LFE} +}; + +static void +_do_init (GType object_type) +{ + const GInterfaceInfo preset_interface_info = { + NULL, /* interface init */ + NULL, /* interface finalize */ + NULL /* interface_data */ + }; + + g_type_add_interface_static (object_type, GST_TYPE_PRESET, + &preset_interface_info); + + GST_DEBUG_CATEGORY_INIT (gst_voaacenc_debug, "voaacenc", 0, + "AAC audio encoder"); +} + +GST_BOILERPLATE_FULL (GstVoAacEnc, gst_voaacenc, GstElement, GST_TYPE_ELEMENT, + _do_init); + +static void +gst_voaacenc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstVoAacEnc *self = GST_VOAACENC (object); + + switch (prop_id) { + case PROP_BITRATE: + self->bitrate = g_value_get_int (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + return; +} + +static void +gst_voaacenc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstVoAacEnc *self = GST_VOAACENC (object); + + switch (prop_id) { + case PROP_BITRATE: + g_value_set_int (value, self->bitrate); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + return; +} + +static void +gst_voaacenc_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details_simple (element_class, "AAC audio encoder", + "Codec/Encoder/Audio", "AAC audio encoder", "Kan Hu "); +} + +static void +gst_voaacenc_class_init (GstVoAacEncClass * klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + object_class->set_property = GST_DEBUG_FUNCPTR (gst_voaacenc_set_property); + object_class->get_property = GST_DEBUG_FUNCPTR (gst_voaacenc_get_property); + object_class->finalize = GST_DEBUG_FUNCPTR (gst_voaacenc_finalize); + + g_object_class_install_property (object_class, PROP_BITRATE, + g_param_spec_int ("bitrate", + "Bitrate", + "Target Audio Bitrate", + 0, G_MAXINT, VOAAC_ENC_DEFAULT_BITRATE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + element_class->change_state = GST_DEBUG_FUNCPTR (gst_voaacenc_state_change); +} + +static void +gst_voaacenc_init (GstVoAacEnc * voaacenc, GstVoAacEncClass * klass) +{ + /* create the sink pad */ + voaacenc->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); + gst_pad_set_setcaps_function (voaacenc->sinkpad, + GST_DEBUG_FUNCPTR (gst_voaacenc_setcaps)); + gst_pad_set_getcaps_function (voaacenc->sinkpad, + GST_DEBUG_FUNCPTR (gst_voaacenc_getcaps)); + gst_pad_set_chain_function (voaacenc->sinkpad, + GST_DEBUG_FUNCPTR (gst_voaacenc_chain)); + gst_element_add_pad (GST_ELEMENT (voaacenc), voaacenc->sinkpad); + + /* create the src pad */ + voaacenc->srcpad = gst_pad_new_from_static_template (&src_template, "src"); + gst_pad_use_fixed_caps (voaacenc->srcpad); + gst_element_add_pad (GST_ELEMENT (voaacenc), voaacenc->srcpad); + + voaacenc->adapter = gst_adapter_new (); + + voaacenc->bitrate = VOAAC_ENC_DEFAULT_BITRATE; + voaacenc->rate = VOAAC_ENC_DEFAULT_RATE; + voaacenc->channels = VOAAC_ENC_DEFAULT_CHANNELS; + voaacenc->output_format = VOAAC_ENC_DEFAULT_OUTPUTFORMAT; + + /* init rest */ + voaacenc->handle = NULL; + voaacenc->sinkcaps = NULL; +} + +static void +gst_voaacenc_finalize (GObject * object) +{ + GstVoAacEnc *voaacenc; + + voaacenc = GST_VOAACENC (object); + + if (voaacenc->sinkcaps) { + gst_caps_unref (voaacenc->sinkcaps); + voaacenc->sinkcaps = NULL; + } + + g_object_unref (G_OBJECT (voaacenc->adapter)); + voaacenc->adapter = NULL; + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +/* check downstream caps to configure format */ +static void +gst_voaacenc_negotiate (GstVoAacEnc * voaacenc) +{ + GstCaps *caps; + + caps = gst_pad_get_allowed_caps (voaacenc->srcpad); + + GST_DEBUG_OBJECT (voaacenc, "allowed caps: %" GST_PTR_FORMAT, caps); + + if (caps && gst_caps_get_size (caps) > 0) { + GstStructure *s = gst_caps_get_structure (caps, 0); + const gchar *str = NULL; + + if ((str = gst_structure_get_string (s, "stream-format"))) { + if (strcmp (str, "adts") == 0) { + GST_DEBUG_OBJECT (voaacenc, "use ADTS format for output"); + voaacenc->output_format = 1; + } else if (strcmp (str, "raw") == 0) { + GST_DEBUG_OBJECT (voaacenc, "use RAW format for output"); + voaacenc->output_format = 0; + } else { + GST_DEBUG_OBJECT (voaacenc, "unknown stream-format: %s", str); + voaacenc->output_format = 0; + } + } + } + + if (caps) + gst_caps_unref (caps); + +} + + +static GstCaps * +gst_voaacenc_generate_sink_caps (void) +{ + GstCaps *caps = gst_caps_new_empty (); + gint i, c; + + for (i = 0; i < VOAAC_ENC_MAX_CHANNELS; i++) { + GValue chanpos = { 0 }; + GValue pos = { 0 }; + GstStructure *structure; + + g_value_init (&chanpos, GST_TYPE_ARRAY); + g_value_init (&pos, GST_TYPE_AUDIO_CHANNEL_POSITION); + + for (c = 0; c <= i; c++) { + g_value_set_enum (&pos, gst_voaacenc_channel_position[i][c]); + gst_value_array_append_value (&chanpos, &pos); + } + + g_value_unset (&pos); + + structure = gst_structure_new ("audio/x-raw-int", + "width", G_TYPE_INT, 16, + "depth", G_TYPE_INT, 16, + "signed", G_TYPE_BOOLEAN, TRUE, + "endianness", G_TYPE_INT, G_BYTE_ORDER, + "rate", GST_TYPE_INT_RANGE, 8000, 96000, "channels", G_TYPE_INT, i + 1); + + gst_structure_set_value (structure, "channel-positions", &chanpos); + g_value_unset (&chanpos); + + gst_caps_append_structure (caps, structure); + } + + return caps; +} + + +static GstCaps * +gst_voaacenc_getcaps (GstPad * pad) +{ + GstVoAacEnc *voaacenc = GST_VOAACENC (GST_PAD_PARENT (pad)); + + if (voaacenc->sinkcaps == NULL) { + voaacenc->sinkcaps = gst_voaacenc_generate_sink_caps (); + } + + GST_DEBUG_OBJECT (voaacenc, "generated sink caps: %" GST_PTR_FORMAT, + voaacenc->sinkcaps); + + return gst_caps_ref (voaacenc->sinkcaps); +} + + +static gboolean +gst_voaacenc_setcaps (GstPad * pad, GstCaps * caps) +{ + gboolean ret = FALSE; + GstStructure *structure; + GstVoAacEnc *voaacenc; + GstCaps *src_caps; + + voaacenc = GST_VOAACENC (GST_PAD_PARENT (pad)); + + structure = gst_caps_get_structure (caps, 0); + + /* get channel count */ + gst_structure_get_int (structure, "channels", &voaacenc->channels); + gst_structure_get_int (structure, "rate", &voaacenc->rate); + + /* precalc duration as it's constant now */ + voaacenc->duration = + gst_util_uint64_scale_int (1024, GST_SECOND, voaacenc->rate); + voaacenc->inbuf_size = voaacenc->channels * 2 * 1024; + + gst_voaacenc_negotiate (voaacenc); + + /* create reverse caps */ + src_caps = gst_voaacenc_create_source_pad_caps (voaacenc); + + if (src_caps) { + gst_pad_set_caps (voaacenc->srcpad, src_caps); + gst_caps_unref (src_caps); + ret = voaacenc_core_set_parameter (voaacenc); + } + return ret; +} + +static GstFlowReturn +gst_voaacenc_chain (GstPad * pad, GstBuffer * buffer) +{ + GstVoAacEnc *voaacenc; + GstFlowReturn ret; + guint64 timestamp, distance = 0; + + voaacenc = GST_VOAACENC (GST_PAD_PARENT (pad)); + + g_return_val_if_fail (voaacenc->handle, GST_FLOW_WRONG_STATE); + + if (voaacenc->rate == 0 || voaacenc->channels == 0) + goto not_negotiated; + + /* discontinuity clears adapter, FIXME, maybe we can set some + * encoder flag to mask the discont. */ + if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) { + gst_adapter_clear (voaacenc->adapter); + voaacenc->ts = 0; + voaacenc->discont = TRUE; + } + + ret = GST_FLOW_OK; + gst_adapter_push (voaacenc->adapter, buffer); + + /* Collect samples until we have enough for an output frame */ + while (gst_adapter_available (voaacenc->adapter) >= voaacenc->inbuf_size) { + GstBuffer *out; + guint8 *data; + VO_CODECBUFFER input = { 0 } + , output = { + 0}; + VO_AUDIO_OUTPUTINFO output_info = { {0} + }; + + + /* max size */ + if ((ret = + gst_pad_alloc_buffer_and_set_caps (voaacenc->srcpad, 0, + voaacenc->inbuf_size, GST_PAD_CAPS (voaacenc->srcpad), + &out)) != GST_FLOW_OK) { + return ret; + } + + output.Buffer = GST_BUFFER_DATA (out); + output.Length = voaacenc->inbuf_size; + + if (voaacenc->discont) { + GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT); + voaacenc->discont = FALSE; + } + + data = + (guint8 *) gst_adapter_peek (voaacenc->adapter, voaacenc->inbuf_size); + input.Buffer = data; + input.Length = voaacenc->inbuf_size; + voaacenc->codec_api.SetInputData (voaacenc->handle, &input); + + /* encode */ + if (voaacenc->codec_api.GetOutputData (voaacenc->handle, &output, + &output_info) != VO_ERR_NONE) { + gst_buffer_unref (out); + return GST_FLOW_ERROR; + } + + /* get timestamp from adapter */ + timestamp = gst_adapter_prev_timestamp (voaacenc->adapter, &distance); + + if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp))) { + GST_BUFFER_TIMESTAMP (out) = + timestamp + + GST_FRAMES_TO_CLOCK_TIME (distance / voaacenc->channels / + VOAAC_ENC_BITS_PER_SAMPLE, voaacenc->rate); + } + + GST_BUFFER_DURATION (out) = + GST_FRAMES_TO_CLOCK_TIME (voaacenc->inbuf_size / voaacenc->channels / + VOAAC_ENC_BITS_PER_SAMPLE, voaacenc->rate); + + voaacenc->ts = GST_BUFFER_TIMESTAMP (out) + GST_BUFFER_DURATION (out); + + GST_LOG_OBJECT (voaacenc, "Pushing out buffer time: %" GST_TIME_FORMAT + " duration: %" GST_TIME_FORMAT, + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (out)), + GST_TIME_ARGS (GST_BUFFER_DURATION (out))); + + GST_BUFFER_SIZE (out) = output.Length; + + /* flush the among of data we have peek */ + gst_adapter_flush (voaacenc->adapter, voaacenc->inbuf_size); + + /* play */ + if ((ret = gst_pad_push (voaacenc->srcpad, out)) != GST_FLOW_OK) + break; + } + return ret; + + /* ERRORS */ +not_negotiated: + { + GST_ELEMENT_ERROR (voaacenc, STREAM, TYPE_NOT_FOUND, + (NULL), ("unknown type")); + return GST_FLOW_NOT_NEGOTIATED; + } +} + +static GstStateChangeReturn +gst_voaacenc_state_change (GstElement * element, GstStateChange transition) +{ + GstVoAacEnc *voaacenc; + GstStateChangeReturn ret; + + voaacenc = GST_VOAACENC (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + if (voaacenc_core_init (voaacenc) == FALSE) + return GST_STATE_CHANGE_FAILURE; + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + voaacenc->rate = 0; + voaacenc->channels = 0; + voaacenc->ts = 0; + voaacenc->discont = FALSE; + gst_adapter_clear (voaacenc->adapter); + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + + switch (transition) { + case GST_STATE_CHANGE_READY_TO_NULL: + voaacenc_core_uninit (voaacenc); + gst_adapter_clear (voaacenc->adapter); + break; + default: + break; + } + return ret; +} + +static GstCaps * +gst_voaacenc_create_source_pad_caps (GstVoAacEnc * voaacenc) +{ + GstCaps *caps = NULL; + GstBuffer *codec_data; + gint index; + + if ((index = voaacenc_get_rate_index (voaacenc->rate)) >= 0) { + + caps = gst_caps_new_simple ("audio/mpeg", + "mpegversion", G_TYPE_INT, VOAAC_ENC_MPEGVERSION, + "channels", G_TYPE_INT, voaacenc->channels, + "rate", G_TYPE_INT, voaacenc->rate, + "stream-format", G_TYPE_STRING, + (voaacenc->output_format ? "adts" : "raw") + , NULL); + + if (!voaacenc->output_format) { + codec_data = gst_buffer_new_and_alloc (VOAAC_ENC_CODECDATA_LEN); + + GST_BUFFER_DATA (codec_data)[0] = ((0x02 << 3) | (index >> 1)); + GST_BUFFER_DATA (codec_data)[1] = + ((index & 0x01) << 7) | (voaacenc->channels << 3); + + gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data, + NULL); + + gst_buffer_unref (codec_data); + } + } + + return caps; +} + + + +static VO_U32 +voaacenc_core_mem_alloc (VO_S32 uID, VO_MEM_INFO * pMemInfo) +{ + if (!pMemInfo) + return VO_ERR_INVALID_ARG; + + pMemInfo->VBuffer = g_malloc (pMemInfo->Size); + return 0; +} + +static VO_U32 +voaacenc_core_mem_free (VO_S32 uID, VO_PTR pMem) +{ + g_free (pMem); + return 0; +} + +static VO_U32 +voaacenc_core_mem_set (VO_S32 uID, VO_PTR pBuff, VO_U8 uValue, VO_U32 uSize) +{ + memset (pBuff, uValue, uSize); + return 0; +} + +static VO_U32 +voaacenc_core_mem_copy (VO_S32 uID, VO_PTR pDest, VO_PTR pSource, VO_U32 uSize) +{ + memcpy (pDest, pSource, uSize); + return 0; +} + +static VO_U32 +voaacenc_core_mem_check (VO_S32 uID, VO_PTR pBuffer, VO_U32 uSize) +{ + return 0; +} + +static gboolean +voaacenc_core_init (GstVoAacEnc * voaacenc) +{ + VO_CODEC_INIT_USERDATA user_data = { 0 }; + voGetAACEncAPI (&voaacenc->codec_api); + + voaacenc->mem_operator.Alloc = voaacenc_core_mem_alloc; + voaacenc->mem_operator.Copy = voaacenc_core_mem_copy; + voaacenc->mem_operator.Free = voaacenc_core_mem_free; + voaacenc->mem_operator.Set = voaacenc_core_mem_set; + voaacenc->mem_operator.Check = voaacenc_core_mem_check; + user_data.memflag = VO_IMF_USERMEMOPERATOR; + user_data.memData = &voaacenc->mem_operator; + voaacenc->codec_api.Init (&voaacenc->handle, VO_AUDIO_CodingAAC, &user_data); + + if (voaacenc->handle == NULL) { + return FALSE; + } + return TRUE; + +} + +static gboolean +voaacenc_core_set_parameter (GstVoAacEnc * voaacenc) +{ + AACENC_PARAM params = { 0 }; + params.sampleRate = voaacenc->rate; + params.bitRate = voaacenc->bitrate; + params.nChannels = voaacenc->channels; + if (voaacenc->output_format) { + params.adtsUsed = 1; + } else { + params.adtsUsed = 0; + } + if (voaacenc->codec_api.SetParam (voaacenc->handle, VO_PID_AAC_ENCPARAM, + ¶ms) != VO_ERR_NONE) { + return FALSE; + } + return TRUE; +} + +static void +voaacenc_core_uninit (GstVoAacEnc * voaacenc) +{ + if (voaacenc->handle) { + voaacenc->codec_api.Uninit (voaacenc->handle); + voaacenc->handle = NULL; + } +} + +static gint +voaacenc_get_rate_index (gint rate) +{ + static const gint rate_table[] = { + 96000, 88200, 64000, 48000, 44100, 32000, + 24000, 22050, 16000, 12000, 11025, 8000 + }; + gint i; + for (i = 0; i < G_N_ELEMENTS (rate_table); ++i) { + if (rate == rate_table[i]) { + return i; + } + } + return -1; +} diff --git a/ext/voaacenc/gstvoaacenc.h b/ext/voaacenc/gstvoaacenc.h new file mode 100644 index 0000000..0a336cd --- /dev/null +++ b/ext/voaacenc/gstvoaacenc.h @@ -0,0 +1,81 @@ +/* GStreamer AAC encoder plugin + * Copyright (C) 2011 Kan Hu + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_VOAACENC_H__ +#define __GST_VOAACENC_H__ + +#include +#include +#include + +#include + +G_BEGIN_DECLS + +#define GST_TYPE_VOAACENC \ + (gst_voaacenc_get_type()) +#define GST_VOAACENC(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_VOAACENC, GstVoAacEnc)) +#define GST_VOAACENC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_VOAACENC, GstVoAacEncClass)) +#define GST_IS_VOAACENC(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_VOAACENC)) +#define GST_IS_VOAACENC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_VOAACENC)) + +typedef struct _GstVoAacEnc GstVoAacEnc; +typedef struct _GstVoAacEncClass GstVoAacEncClass; + +struct _GstVoAacEnc { + GstElement element; + + /* pads */ + GstPad *sinkpad, *srcpad; + GstCaps *sinkcaps; + guint64 ts; + gboolean discont; + + GstAdapter *adapter; + + + /* desired bitrate */ + gint bitrate; + gint channels; + gint rate; + gint output_format; + gint duration; + + gint inbuf_size; + + /* library handle */ + VO_AUDIO_CODECAPI codec_api; + VO_HANDLE handle; + VO_MEM_OPERATOR mem_operator; + +}; + +struct _GstVoAacEncClass { + GstElementClass parent_class; +}; + +GType gst_voaacenc_get_type (void); + +G_END_DECLS + +#endif /* __GST_VOAACENC_H__ */ diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 9de5de2..89b0c1c 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -46,6 +46,12 @@ else check_faad = endif +if USE_VOAACENC +check_voaacenc = elements/voaacenc +else +check_voaacenc = +endif + if USE_EXIF check_jifmux = elements/jifmux else @@ -143,6 +149,7 @@ check_PROGRAMS = \ $(check_assrender) \ $(check_faac) \ $(check_faad) \ + $(check_voaacenc) \ $(check_mpeg2enc) \ $(check_mplex) \ $(check_ofa) \ @@ -184,6 +191,10 @@ AM_CFLAGS = $(GST_CHECK_CFLAGS) $(GST_OPTION_CFLAGS) \ -UG_DISABLE_ASSERT -UG_DISABLE_CAST_CHECKS LDADD = $(GST_CHECK_LIBS) +elements_voaacenc_LDADD = \ + $(GST_BASE_LIBS) $(GST_LIBS) $(LDADD) \ + -lgstaudio-@GST_MAJORMINOR@ + elements_camerabin_CFLAGS = \ $(GST_PLUGINS_BAD_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS) \ $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(AM_CFLAGS) -DGST_USE_UNSTABLE_API diff --git a/tests/check/elements/voaacenc.c b/tests/check/elements/voaacenc.c new file mode 100644 index 0000000..22b42fb --- /dev/null +++ b/tests/check/elements/voaacenc.c @@ -0,0 +1,287 @@ +/* GStreamer + * + * unit test for voaacenc + * + * Copyright (C) <2009> Mark Nauwelaerts + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +#include +#include + +/* For ease of programming we use globals to keep refs for our floating + * src and sink pads we create; otherwise we always have to do get_pad, + * get_peer, and then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + +#define AUDIO_CAPS_STRING "audio/x-raw-int, " \ + "rate = (int) 48000, " \ + "channels = (int) 2, " \ + "width = (int) 16, " \ + "depth = (int) 16, " \ + "signed = (boolean) true, " \ + "endianness = (int) BYTE_ORDER " + +#define AAC_RAW_CAPS_STRING "audio/mpeg, " \ + "mpegversion = (int) 4, " \ + "rate = (int) 48000, " \ + "channels = (int) 2, " \ + "stream-format = \"raw\"" + +#define AAC_ADTS_CAPS_STRING "audio/mpeg, " \ + "mpegversion = (int) 4, " \ + "rate = (int) 48000, " \ + "channels = (int) 2, " \ + "stream-format = \"adts\"" + + +static GstStaticPadTemplate sinktemplate_adts = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (AAC_ADTS_CAPS_STRING)); + +static GstStaticPadTemplate sinktemplate_raw = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (AAC_RAW_CAPS_STRING)); + + +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (AUDIO_CAPS_STRING)); + + +static GstElement * +setup_voaacenc (gboolean adts) +{ + GstElement *voaacenc; + + GST_DEBUG ("setup_voaacenc"); + voaacenc = gst_check_setup_element ("voaacenc"); + mysrcpad = gst_check_setup_src_pad (voaacenc, &srctemplate, NULL); + + if (adts) + mysinkpad = gst_check_setup_sink_pad (voaacenc, &sinktemplate_adts, NULL); + else + mysinkpad = gst_check_setup_sink_pad (voaacenc, &sinktemplate_raw, NULL); + + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + return voaacenc; +} + +static void +cleanup_voaacenc (GstElement * voaacenc) +{ + GST_DEBUG ("cleanup_aacenc"); + gst_element_set_state (voaacenc, GST_STATE_NULL); + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (voaacenc); + gst_check_teardown_sink_pad (voaacenc); + gst_check_teardown_element (voaacenc); +} + +static void +set_channel_positions (GstCaps * caps, int channels, + GstAudioChannelPosition * channelpositions) +{ + GValue chanpos = { 0 }; + GValue pos = { 0 }; + GstStructure *structure = gst_caps_get_structure (caps, 0); + int c; + + g_value_init (&chanpos, GST_TYPE_ARRAY); + g_value_init (&pos, GST_TYPE_AUDIO_CHANNEL_POSITION); + + for (c = 0; c < channels; c++) { + g_value_set_enum (&pos, channelpositions[c]); + gst_value_array_append_value (&chanpos, &pos); + } + g_value_unset (&pos); + + gst_structure_set_value (structure, "channel-positions", &chanpos); + g_value_unset (&chanpos); +} + +static void +do_test (gboolean adts) +{ + GstElement *voaacenc; + GstBuffer *inbuffer, *outbuffer; + GstCaps *caps; + gint i, num_buffers; + const gint nbuffers = 10; + GstAudioChannelPosition channel_position_layout[2] = + { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT + }; + + voaacenc = setup_voaacenc (adts); + fail_unless (gst_element_set_state (voaacenc, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + + /* corresponds to audio buffer mentioned in the caps */ + inbuffer = gst_buffer_new_and_alloc (1024 * nbuffers * 2 * 2); + /* makes valgrind's memcheck happier */ + memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer)); + caps = gst_caps_from_string (AUDIO_CAPS_STRING); + + set_channel_positions (caps, 2, channel_position_layout); + + gst_buffer_set_caps (inbuffer, caps); + gst_caps_unref (caps); + GST_BUFFER_TIMESTAMP (inbuffer) = 0; + ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); + fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); + + /* send eos to have all flushed if needed */ + fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE); + + num_buffers = g_list_length (buffers); + fail_unless_equals_int (num_buffers, nbuffers); + + /* clean up buffers */ + for (i = 0; i < num_buffers; ++i) { + gint size, header = 0, id; + guint8 *data; + + outbuffer = GST_BUFFER (buffers->data); + fail_if (outbuffer == NULL); + + data = GST_BUFFER_DATA (outbuffer); + size = GST_BUFFER_SIZE (outbuffer); + + if (adts) { + gboolean protection; + gint k; + + fail_if (size < 7); + protection = !(data[1] & 0x1); + /* expect only 1 raw data block */ + k = (data[6] & 0x3) + 1; + fail_if (k != 1); + + header = 7; + if (protection) + header += (k - 1) * 2 + 2; + + /* check header */ + k = GST_READ_UINT16_BE (data) & 0xFFF6; + /* sync */ + fail_unless (k == 0xFFF0); + k = data[2]; + /* profile */ + fail_unless ((k >> 6) == 0x1); + /* rate */ + fail_unless (((k >> 2) & 0xF) == 0x3); + /* channels */ + fail_unless ((k & 0x1) == 0); + k = data[3]; + fail_unless ((k >> 6) == 0x2); + + } else { + GstCaps *caps; + GstStructure *s; + const GValue *value; + GstBuffer *buf; + gint k; + + caps = gst_buffer_get_caps (outbuffer); + fail_if (caps == NULL); + s = gst_caps_get_structure (caps, 0); + fail_if (s == NULL); + value = gst_structure_get_value (s, "codec_data"); + fail_if (value == NULL); + buf = gst_value_get_buffer (value); + fail_if (buf == NULL); + data = GST_BUFFER_DATA (buf); + size = GST_BUFFER_SIZE (buf); + fail_if (size < 2); + k = GST_READ_UINT16_BE (data); + /* profile, rate, channels */ + fail_unless ((k & 0xFFF8) == ((0x02 << 11) | (0x3 << 7) | (0x02 << 3))); + gst_caps_unref (caps); + + } + + fail_if (size <= header); + id = data[header] & (0x7 << 5); + /* allow all but ID_END or ID_LFE */ + fail_if (id == 7 || id == 3); + + buffers = g_list_remove (buffers, outbuffer); + + ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1); + gst_buffer_unref (outbuffer); + outbuffer = NULL; + } + + cleanup_voaacenc (voaacenc); + g_list_free (buffers); + buffers = NULL; +} + +GST_START_TEST (test_adts) +{ + do_test (TRUE); +} + +GST_END_TEST; + +GST_START_TEST (test_raw) +{ + do_test (FALSE); +} + +GST_END_TEST; + +static Suite * +voaacenc_suite (void) +{ + Suite *s = suite_create ("voaacenc"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_adts); + tcase_add_test (tc_chain, test_raw); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = voaacenc_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} -- 1.7.0.4