diff options
author | Sean DuBois <sean@siobud.com> | 2017-06-27 22:50:41 +0000 |
---|---|---|
committer | Edward Hervey <bilboed@bilboed.com> | 2017-12-15 15:44:17 +0100 |
commit | 1c583fd27b8625482d71f6af2acd60a9e8ac53bb (patch) | |
tree | 5069e60d9ee8d8c86d1588fc809471a247fb054d | |
parent | f2dac588dcd7d566252f2651912c1db75d873660 (diff) |
Add aom plugin with av1enc and av1dec element
https://bugzilla.gnome.org/show_bug.cgi?id=784160
-rw-r--r-- | configure.ac | 8 | ||||
-rw-r--r-- | ext/Makefile.am | 8 | ||||
-rw-r--r-- | ext/aom/Makefile.am | 15 | ||||
-rw-r--r-- | ext/aom/gstaom.c | 50 | ||||
-rw-r--r-- | ext/aom/gstaom.h | 24 | ||||
-rw-r--r-- | ext/aom/gstav1dec.c | 348 | ||||
-rw-r--r-- | ext/aom/gstav1dec.h | 70 | ||||
-rw-r--r-- | ext/aom/gstav1enc.c | 350 | ||||
-rw-r--r-- | ext/aom/gstav1enc.h | 69 | ||||
-rw-r--r-- | ext/aom/meson.build | 11 | ||||
-rw-r--r-- | ext/meson.build | 1 |
11 files changed, 954 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index f60701c13..9a401e801 100644 --- a/configure.ac +++ b/configure.ac @@ -2131,6 +2131,12 @@ AG_GST_CHECK_FEATURE(ASSRENDER, [ASS/SSA renderer], assrender, [ AC_SUBST(ASSRENDER_CFLAGS) AC_SUBST(ASSRENDER_LIBS) +dnl *** AOM *** +translit(dnm, m, l) AM_CONDITIONAL(USE_AOM, true) +AG_GST_CHECK_FEATURE(AOM, [AV1 encoder/decoder], aom, [ + AG_GST_PKG_CHECK_MODULES(AOM, aom) +]) + dnl *** vo-amrwbenc *** translit(dnm, m, l) AM_CONDITIONAL(USE_VOAMRWBENC, true) AG_GST_CHECK_FEATURE(VOAMRWBENC, [vo-amrwbenc library], vo-amrwbenc, [ @@ -3435,6 +3441,7 @@ dnl not building plugins with external dependencies, dnl but we still need to set the conditionals AM_CONDITIONAL(USE_ASSRENDER, false) +AM_CONDITIONAL(USE_AOM, false) AM_CONDITIONAL(USE_VOAMRWBENC, false) AM_CONDITIONAL(USE_VOAACENC, false) AM_CONDITIONAL(USE_BS2B, false) @@ -3760,6 +3767,7 @@ tests/icles/Makefile ext/voamrwbenc/Makefile ext/voaacenc/Makefile ext/assrender/Makefile +ext/aom/Makefile ext/bs2b/Makefile ext/bz2/Makefile ext/chromaprint/Makefile diff --git a/ext/Makefile.am b/ext/Makefile.am index 0c35ff53e..e4e0db7d6 100644 --- a/ext/Makefile.am +++ b/ext/Makefile.am @@ -4,6 +4,12 @@ else ASSRENDER_DIR = endif +if USE_AOM +AOM_DIR= aom +else +AOM_DIR= +endif + if USE_VOAMRWBENC VOAMRWBENC_DIR = voamrwbenc else @@ -409,6 +415,7 @@ endif SUBDIRS=\ $(VOAACENC_DIR) \ $(ASSRENDER_DIR) \ + $(AOM_DIR) \ $(VOAMRWBENC_DIR) \ $(AUDIOFILE_DIR) \ $(BS2B_DIR) \ @@ -479,6 +486,7 @@ SUBDIRS=\ DIST_SUBDIRS = \ assrender \ + aom \ bs2b \ bz2 \ colormanagement \ diff --git a/ext/aom/Makefile.am b/ext/aom/Makefile.am new file mode 100644 index 000000000..c28decd37 --- /dev/null +++ b/ext/aom/Makefile.am @@ -0,0 +1,15 @@ +plugin_LTLIBRARIES = libgstaom.la + +libgstaom_la_SOURCES = \ + gstaom.c \ + gstav1enc.c \ + gstav1dec.c + +libgstaom_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) $(AOM_CFLAGS) +libgstaom_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(GST_LIBS) -lgstvideo-$(GST_API_VERSION) $(AOM_LIBS) +libgstaom_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + +noinst_HEADERS = \ + gstaom.h \ + gstav1dec.h \ + gstav1enc.h diff --git a/ext/aom/gstaom.c b/ext/aom/gstaom.c new file mode 100644 index 000000000..506610575 --- /dev/null +++ b/ext/aom/gstaom.c @@ -0,0 +1,50 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <gst/gst.h> + +#include "gstaom.h" +#include "gstav1enc.h" +#include "gstav1dec.h" + +static gboolean +plugin_init (GstPlugin * plugin) +{ + + if (!gst_element_register (plugin, "av1enc", GST_RANK_PRIMARY, + GST_TYPE_AV1_ENC)) { + return FALSE; + } + + if (!gst_element_register (plugin, "av1dec", GST_RANK_PRIMARY, + GST_TYPE_AV1_DEC)) { + return FALSE; + } + + return TRUE; +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + aom, + "AOM plugin library", + plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) diff --git a/ext/aom/gstaom.h b/ext/aom/gstaom.h new file mode 100644 index 000000000..e1147d46b --- /dev/null +++ b/ext/aom/gstaom.h @@ -0,0 +1,24 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef __GST_AOM_H__ +#define __GST_AOM_H__ + +#endif /* __GST_AOM_H__ */ diff --git a/ext/aom/gstav1dec.c b/ext/aom/gstav1dec.c new file mode 100644 index 000000000..d5b93548b --- /dev/null +++ b/ext/aom/gstav1dec.c @@ -0,0 +1,348 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +/** + * SECTION:element-av1dec + * + * AV1 Decoder. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! av1dec ! videoconvert ! videoscale ! autovideosink + * ]| + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <string.h> +#include "gstav1dec.h" + +enum +{ + PROP_0, +}; + +static GstStaticPadTemplate gst_av1_dec_sink_pad_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-av1") + ); + +static GstStaticPadTemplate gst_av1_dec_src_pad_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-raw, " + "format = (string) \"I420\", " + "framerate = (fraction) [0, MAX], " + "width = (int) [ 4, MAX ], " "height = (int) [ 4, MAX ]") + ); + +GST_DEBUG_CATEGORY_STATIC (av1_dec_debug); +#define GST_CAT_DEFAULT av1_dec_debug + +static void gst_av1_dec_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_av1_dec_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static gboolean gst_av1_dec_start (GstVideoDecoder * dec); +static gboolean gst_av1_dec_stop (GstVideoDecoder * dec); +static gboolean gst_av1_dec_set_format (GstVideoDecoder * dec, + GstVideoCodecState * state); +static GstFlowReturn +gst_av1_dec_handle_frame (GstVideoDecoder * decoder, + GstVideoCodecFrame * frame); + +static void gst_av1_dec_image_to_buffer (GstAV1Dec * dec, + const aom_image_t * img, GstBuffer * buffer); +static GstFlowReturn gst_av1_dec_open_codec (GstAV1Dec * av1dec, + GstVideoCodecFrame * frame); + +#define gst_av1_dec_parent_class parent_class +G_DEFINE_TYPE (GstAV1Dec, gst_av1_dec, GST_TYPE_VIDEO_DECODER); + +static void +gst_av1_dec_class_init (GstAV1DecClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *element_class; + GstVideoDecoderClass *vdec_class; + + gobject_class = (GObjectClass *) klass; + element_class = (GstElementClass *) klass; + vdec_class = (GstVideoDecoderClass *) klass; + + + parent_class = g_type_class_peek_parent (klass); + + gobject_class->set_property = gst_av1_dec_set_property; + gobject_class->get_property = gst_av1_dec_get_property; + + gst_element_class_add_static_pad_template (element_class, + &gst_av1_dec_src_pad_template); + gst_element_class_add_static_pad_template (element_class, + &gst_av1_dec_sink_pad_template); + gst_element_class_set_static_metadata (element_class, "AV1 Decoder", + "Codec/Decoder/Video", "Decode AV1 video streams", + "Sean DuBois <sean@siobud.com>"); + + vdec_class->start = GST_DEBUG_FUNCPTR (gst_av1_dec_start); + vdec_class->stop = GST_DEBUG_FUNCPTR (gst_av1_dec_stop); + vdec_class->set_format = GST_DEBUG_FUNCPTR (gst_av1_dec_set_format); + vdec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_av1_dec_handle_frame); + + klass->codec_algo = &aom_codec_av1_dx_algo; + GST_DEBUG_CATEGORY_INIT (av1_dec_debug, "av1dec", 0, "AV1 decoding element"); +} + +static void +gst_av1_dec_init (GstAV1Dec * av1dec) +{ + GstVideoDecoder *dec = (GstVideoDecoder *) av1dec; + + GST_DEBUG_OBJECT (dec, "gst_av1_dec_init"); + gst_video_decoder_set_packetized (dec, TRUE); + gst_video_decoder_set_needs_format (dec, TRUE); + gst_video_decoder_set_use_default_pad_acceptcaps (dec, TRUE); + GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec)); +} + +static void +gst_av1_dec_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_av1_dec_get_property (GObject * object, guint prop_id, GValue * value, + GParamSpec * pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static gboolean +gst_av1_dec_start (GstVideoDecoder * dec) +{ + GstAV1Dec *av1dec = (GstAV1Dec *) dec; + + av1dec->decoder_inited = FALSE; + av1dec->output_state = NULL; + av1dec->input_state = NULL; + + return TRUE; +} + +static gboolean +gst_av1_dec_stop (GstVideoDecoder * dec) +{ + GstAV1Dec *av1dec = (GstAV1Dec *) dec; + + if (av1dec->output_state) { + gst_video_codec_state_unref (av1dec->output_state); + av1dec->output_state = NULL; + } + + if (av1dec->input_state) { + gst_video_codec_state_unref (av1dec->input_state); + av1dec->input_state = NULL; + } + + return TRUE; +} + +static gboolean +gst_av1_dec_set_format (GstVideoDecoder * dec, GstVideoCodecState * state) +{ + GstAV1Dec *av1dec = (GstAV1Dec *) dec; + + if (av1dec->decoder_inited) { + aom_codec_destroy (&av1dec->decoder); + } + av1dec->decoder_inited = FALSE; + + if (av1dec->output_state) { + gst_video_codec_state_unref (av1dec->output_state); + av1dec->output_state = NULL; + } + + if (av1dec->input_state) { + gst_video_codec_state_unref (av1dec->input_state); + } + + av1dec->input_state = gst_video_codec_state_ref (state); + + return TRUE; +} + +static GstFlowReturn +gst_av1_dec_open_codec (GstAV1Dec * av1dec, GstVideoCodecFrame * frame) +{ + aom_codec_err_t status; + GstAV1DecClass *av1class = GST_AV1_DEC_GET_CLASS (av1dec); + + status = aom_codec_dec_init (&av1dec->decoder, av1class->codec_algo, NULL, 0); + if (status != AOM_CODEC_OK) { + GST_ELEMENT_ERROR (av1dec, LIBRARY, INIT, + ("Failed to initialize AOM decoder"), ("%s", "")); + return GST_FLOW_ERROR; + } + + av1dec->decoder_inited = TRUE; + return GST_FLOW_OK; +} + +static void +gst_av1_dec_handle_resolution_change (GstAV1Dec * av1dec, aom_image_t * img, + GstVideoFormat fmt) +{ + if (!av1dec->output_state || + av1dec->output_state->info.finfo->format != fmt || + av1dec->output_state->info.width != img->d_w || + av1dec->output_state->info.height != img->d_h) { + + if (av1dec->output_state) + gst_video_codec_state_unref (av1dec->output_state); + + av1dec->output_state = + gst_video_decoder_set_output_state (GST_VIDEO_DECODER (av1dec), + fmt, img->d_w, img->d_h, av1dec->input_state); + gst_video_decoder_negotiate (GST_VIDEO_DECODER (av1dec)); + } + + +} + +static void +gst_av1_dec_image_to_buffer (GstAV1Dec * dec, const aom_image_t * img, + GstBuffer * buffer) +{ + int deststride, srcstride, height, width, line, comp; + guint8 *dest, *src; + GstVideoFrame frame; + GstVideoInfo *info = &dec->output_state->info; + + if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) { + GST_ERROR_OBJECT (dec, "Could not map video buffer"); + return; + } + + for (comp = 0; comp < 3; comp++) { + dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp); + src = img->planes[comp]; + width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, comp) + * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp); + height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp); + deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp); + srcstride = img->stride[comp]; + + if (srcstride == deststride) { + GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane", + comp, srcstride); + memcpy (dest, src, srcstride * height); + } else { + GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying " + "line by line.", comp, srcstride, deststride); + for (line = 0; line < height; line++) { + memcpy (dest, src, width); + dest += deststride; + src += srcstride; + } + } + } + + gst_video_frame_unmap (&frame); +} + +static GstFlowReturn +gst_av1_dec_handle_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame) +{ + GstAV1Dec *av1dec = (GstAV1Dec *) dec; + GstFlowReturn ret; + GstMapInfo minfo; + aom_codec_err_t status; + aom_image_t *img; + aom_codec_iter_t iter = NULL; + + if (!av1dec->decoder_inited) { + ret = gst_av1_dec_open_codec (av1dec, frame); + if (ret == GST_FLOW_CUSTOM_SUCCESS_1) { + gst_video_decoder_drop_frame (dec, frame); + return GST_FLOW_OK; + } else if (ret != GST_FLOW_OK) { + gst_video_codec_frame_unref (frame); + return ret; + } + } + + if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) { + GST_ERROR_OBJECT (dec, "Failed to map input buffer"); + gst_video_codec_frame_unref (frame); + return GST_FLOW_ERROR; + } + + status = aom_codec_decode (&av1dec->decoder, minfo.data, minfo.size, NULL, 0); + + gst_buffer_unmap (frame->input_buffer, &minfo); + + if (status) { + GST_ELEMENT_ERROR (av1dec, LIBRARY, INIT, + ("Failed to decode frame"), ("%s", "")); + gst_video_codec_frame_unref (frame); + return ret; + } + + img = aom_codec_get_frame (&av1dec->decoder, &iter); + if (img) { + gst_av1_dec_handle_resolution_change (av1dec, img, GST_VIDEO_FORMAT_I420); + + ret = gst_video_decoder_allocate_output_frame (dec, frame); + if (ret == GST_FLOW_OK) { + gst_av1_dec_image_to_buffer (av1dec, img, frame->output_buffer); + ret = gst_video_decoder_finish_frame (dec, frame); + } else { + gst_video_decoder_drop_frame (dec, frame); + } + + aom_img_free (img); + while ((img = aom_codec_get_frame (&av1dec->decoder, &iter))) { + GST_WARNING_OBJECT (dec, "Multiple decoded frames... dropping"); + aom_img_free (img); + } + } else { + GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame); + gst_video_decoder_finish_frame (dec, frame); + } + + + return ret; +} diff --git a/ext/aom/gstav1dec.h b/ext/aom/gstav1dec.h new file mode 100644 index 000000000..b793fdedb --- /dev/null +++ b/ext/aom/gstav1dec.h @@ -0,0 +1,70 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef __GST_AV1_DEC_H__ +#define __GST_AV1_DEC_H__ + + +#include <gst/gst.h> +#include <gst/video/gstvideodecoder.h> + +#include <aom/aomdx.h> +#include <aom/aom_decoder.h> + +G_BEGIN_DECLS +#define GST_TYPE_AV1_DEC \ + (gst_av1_dec_get_type()) +#define GST_AV1_DEC(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AV1_DEC,GstAV1Dec)) +#define GST_AV1_DEC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AV1_DEC,GstAV1DecClass)) +#define GST_IS_AV1_DEC(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AV1_DEC)) +#define GST_IS_AV1_DEC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AV1_DEC)) +#define GST_AV1_DEC_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AV1_DEC, GstAV1DecClass)) + +typedef struct _GstAV1Dec GstAV1Dec; +typedef struct _GstAV1DecClass GstAV1DecClass; + +struct _GstAV1Dec +{ + GstVideoDecoder base_video_decoder; + + gboolean decoder_inited; + + aom_codec_ctx_t decoder; + + GstVideoCodecState *input_state; + GstVideoCodecState *output_state; +}; + +struct _GstAV1DecClass +{ + GstVideoDecoderClass parent_class; + /*supported aom algo*/ + aom_codec_iface_t* codec_algo; +}; + +GType gst_av1_dec_get_type (void); + +G_END_DECLS +#endif /* __GST_AV1_DEC_H__ */ diff --git a/ext/aom/gstav1enc.c b/ext/aom/gstav1enc.c new file mode 100644 index 000000000..547023048 --- /dev/null +++ b/ext/aom/gstav1enc.c @@ -0,0 +1,350 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +/** + * SECTION:element-av1enc + * + * AV1 Encoder. + * + * <refsect2> + * <title>Example launch line</title> + * |[ + * gst-launch-1.0 videotestsrc num-buffers=50 ! av1enc ! webmmux ! filesink location=av1.webm + * ]| + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstav1enc.h" +#include <gst/video/video.h> +#include <gst/video/gstvideometa.h> +#include <gst/base/base.h> + +GST_DEBUG_CATEGORY_STATIC (av1_enc_debug); +#define GST_CAT_DEFAULT av1_enc_debug + +enum +{ + LAST_SIGNAL +}; + +enum +{ + PROP_0 +}; + +static void gst_av1_enc_finalize (GObject * object); + +static void gst_av1_enc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_av1_enc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static gboolean gst_av1_enc_start (GstVideoEncoder * benc); +static gboolean gst_av1_enc_stop (GstVideoEncoder * benc); +static gboolean gst_av1_enc_set_format (GstVideoEncoder * encoder, + GstVideoCodecState * state); +static GstFlowReturn gst_av1_enc_handle_frame (GstVideoEncoder * encoder, + GstVideoCodecFrame * frame); +static gboolean gst_av1_enc_propose_allocation (GstVideoEncoder * encoder, + GstQuery * query); + +#define gst_av1_enc_parent_class parent_class +G_DEFINE_TYPE (GstAV1Enc, gst_av1_enc, GST_TYPE_VIDEO_ENCODER); + +/* *INDENT-OFF* */ +static GstStaticPadTemplate gst_av1_enc_sink_pad_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-raw, " + "format = (string) \"I420\", " + "framerate = (fraction) [0, MAX], " + "width = (int) [ 4, MAX ], " + "height = (int) [ 4, MAX ]") + ); +/* *INDENT-ON* */ + +static GstStaticPadTemplate gst_av1_enc_src_pad_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-av1") + ); + +static void +gst_av1_enc_class_init (GstAV1EncClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *element_class; + GstVideoEncoderClass *venc_class; + + gobject_class = (GObjectClass *) klass; + element_class = (GstElementClass *) klass; + venc_class = (GstVideoEncoderClass *) klass; + + parent_class = g_type_class_peek_parent (klass); + + gobject_class->finalize = gst_av1_enc_finalize; + gobject_class->set_property = gst_av1_enc_set_property; + gobject_class->get_property = gst_av1_enc_get_property; + + gst_element_class_add_static_pad_template (element_class, + &gst_av1_enc_sink_pad_template); + gst_element_class_add_static_pad_template (element_class, + &gst_av1_enc_src_pad_template); + gst_element_class_set_static_metadata (element_class, "AV1 Encoder", + "Codec/Encoder/Video", "Encode AV1 video streams", + "Sean DuBois <sean@siobud.com>"); + + venc_class->start = gst_av1_enc_start; + venc_class->stop = gst_av1_enc_stop; + venc_class->set_format = gst_av1_enc_set_format; + venc_class->handle_frame = gst_av1_enc_handle_frame; + venc_class->propose_allocation = gst_av1_enc_propose_allocation; + + GST_DEBUG_CATEGORY_INIT (av1_enc_debug, "av1enc", 0, "AV1 encoding element"); +} + +static void +gst_av1_codec_error (aom_codec_ctx_t * ctx, const char *s) +{ + const char *detail = aom_codec_error_detail (ctx); + + g_print ("%s: %s\n", s, aom_codec_error (ctx)); + if (detail) { + g_print (" %s\n", detail); + } +} + +static void +gst_av1_enc_init (GstAV1Enc * av1enc) +{ + GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (av1enc)); +} + +static void +gst_av1_enc_finalize (GObject * object) +{ + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +gst_av1_enc_set_latency (GstAV1Enc * encoder) +{ + GstAV1Enc *av1enc = GST_AV1_ENC (encoder); + GstClockTime latency = + gst_util_uint64_scale (av1enc->aom_cfg.g_lag_in_frames, 1 * GST_SECOND, + 30); + gst_video_encoder_set_latency (GST_VIDEO_ENCODER (encoder), latency, latency); + + GST_WARNING_OBJECT (encoder, "Latency unimplemented"); +} + +static gboolean +gst_av1_enc_init_aom (GstAV1Enc * av1enc) +{ + av1enc->codec_interface = &aom_codec_av1_cx_algo; + + if (aom_codec_enc_config_default (av1enc->codec_interface, &av1enc->aom_cfg, + 0)) { + gst_av1_codec_error (&av1enc->codec, "Failed to get default codec config."); + return FALSE; + } + + av1enc->aom_cfg.g_w = av1enc->input_state->info.width; + av1enc->aom_cfg.g_h = av1enc->input_state->info.height; + av1enc->aom_cfg.g_timebase.num = av1enc->input_state->info.fps_d; + av1enc->aom_cfg.g_timebase.den = av1enc->input_state->info.fps_n; + av1enc->aom_cfg.rc_target_bitrate = 3000; + av1enc->aom_cfg.g_error_resilient = 0; + + if (aom_codec_enc_init (&av1enc->codec, av1enc->codec_interface, + &av1enc->aom_cfg, 0)) { + gst_av1_codec_error (&av1enc->codec, "Failed to initialize encoder"); + return FALSE; + } + + return TRUE; +} + + +static gboolean +gst_av1_enc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state) +{ + GstVideoCodecState *output_state; + GstAV1Enc *av1enc = GST_AV1_ENC (encoder); + + av1enc->keyframe_dist = 30; + + output_state = + gst_video_encoder_set_output_state (encoder, + gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder)), + state); + gst_video_codec_state_unref (output_state); + + av1enc->input_state = gst_video_codec_state_ref (state); + + gst_av1_enc_set_latency (av1enc); + return gst_av1_enc_init_aom (av1enc); +} + +static GstFlowReturn +gst_av1_enc_process (GstAV1Enc * encoder) +{ + aom_codec_iter_t iter = NULL; + const aom_codec_cx_pkt_t *pkt; + GstVideoCodecFrame *frame; + GstVideoEncoder *video_encoder; + + video_encoder = GST_VIDEO_ENCODER (encoder); + + while ((pkt = aom_codec_get_cx_data (&encoder->codec, &iter)) != NULL) { + if (pkt->kind == AOM_CODEC_STATS_PKT) { + GST_WARNING_OBJECT (encoder, "Unhandled stats packet"); + } else if (pkt->kind == AOM_CODEC_FPMB_STATS_PKT) { + GST_WARNING_OBJECT (encoder, "Unhandled FPMB pkt"); + } else if (pkt->kind == AOM_CODEC_PSNR_PKT) { + GST_WARNING_OBJECT (encoder, "Unhandled PSNR packet"); + } else if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { + frame = gst_video_encoder_get_oldest_frame (video_encoder); + g_assert (frame != NULL); + if ((pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0) { + GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame); + } else { + GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame); + } + + frame->output_buffer = + gst_buffer_new_wrapped (g_memdup (pkt->data.frame.buf, + pkt->data.frame.sz), pkt->data.frame.sz); + gst_video_encoder_finish_frame (video_encoder, frame); + } + } + + return GST_FLOW_OK; +} + +static void +gst_av1_enc_fill_image (GstAV1Enc * enc, GstVideoFrame * frame, + aom_image_t * image) +{ + image->planes[AOM_PLANE_Y] = GST_VIDEO_FRAME_COMP_DATA (frame, 0); + image->planes[AOM_PLANE_U] = GST_VIDEO_FRAME_COMP_DATA (frame, 1); + image->planes[AOM_PLANE_V] = GST_VIDEO_FRAME_COMP_DATA (frame, 2); + + image->stride[AOM_PLANE_Y] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0); + image->stride[AOM_PLANE_U] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1); + image->stride[AOM_PLANE_V] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2); +} + +static GstFlowReturn +gst_av1_enc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame) +{ + GstAV1Enc *av1enc = GST_AV1_ENC (encoder); + aom_image_t raw; + int flags = 0; + GstFlowReturn ret; + GstVideoFrame vframe; + + if (!aom_img_alloc (&raw, AOM_IMG_FMT_I420, av1enc->aom_cfg.g_w, + av1enc->aom_cfg.g_h, 1)) { + GST_ERROR_OBJECT (encoder, "Failed to initialize encoder"); + return FALSE; + } + + gst_video_frame_map (&vframe, &av1enc->input_state->info, + frame->input_buffer, GST_MAP_READ); + gst_av1_enc_fill_image (av1enc, &vframe, &raw); + gst_video_frame_unmap (&vframe); + + if (av1enc->keyframe_dist >= 30) { + av1enc->keyframe_dist = 0; + flags |= AOM_EFLAG_FORCE_KF; + } + av1enc->keyframe_dist++; + + if (aom_codec_encode (&av1enc->codec, &raw, frame->pts, 1, flags, + AOM_DL_GOOD_QUALITY) != AOM_CODEC_OK) { + gst_av1_codec_error (&av1enc->codec, "Failed to encode frame"); + } + + ret = gst_av1_enc_process (av1enc); + aom_img_free (&raw); + return ret; +} + +static gboolean +gst_av1_enc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query) +{ + gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL); + + return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder, + query); +} + +static void +gst_av1_enc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstAV1Enc *av1enc = GST_AV1_ENC (object); + + GST_OBJECT_LOCK (av1enc); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + + GST_OBJECT_UNLOCK (av1enc); +} + +static void +gst_av1_enc_get_property (GObject * object, guint prop_id, GValue * value, + GParamSpec * pspec) +{ + GstAV1Enc *av1enc = GST_AV1_ENC (object); + + GST_OBJECT_LOCK (av1enc); + + switch (prop_id) { + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + + GST_OBJECT_UNLOCK (av1enc); +} + +static gboolean +gst_av1_enc_start (GstVideoEncoder * encoder) +{ + return TRUE; +} + +static gboolean +gst_av1_enc_stop (GstVideoEncoder * benc) +{ + g_print ("AV1Enc Stop \n"); + return TRUE; +} diff --git a/ext/aom/gstav1enc.h b/ext/aom/gstav1enc.h new file mode 100644 index 000000000..2493d8c44 --- /dev/null +++ b/ext/aom/gstav1enc.h @@ -0,0 +1,69 @@ +/* GStreamer + * Copyright (C) <2017> Sean DuBois <sean@siobud.com> + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef __GST_AV1_ENC_H__ +#define __GST_AV1_ENC_H__ + + +#include <gst/gst.h> +#include <gst/video/video.h> +#include <gst/video/gstvideoencoder.h> + +#include <aom/aom_codec.h> +#include <aom/aom_encoder.h> +#include <aom/aomcx.h> + +G_BEGIN_DECLS +#define GST_TYPE_AV1_ENC \ + (gst_av1_enc_get_type()) +#define GST_AV1_ENC(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AV1_ENC,GstAV1Enc)) +#define GST_AV1_ENC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AV1_ENC,GstAV1EncClass)) +#define GST_IS_AV1_ENC(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AV1_ENC)) +#define GST_IS_AV1_ENC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AV1_ENC)) + +typedef struct _GstAV1Enc GstAV1Enc; +typedef struct _GstAV1EncClass GstAV1EncClass; + +struct _GstAV1Enc +{ + GstVideoEncoder encoder; + + guint keyframe_dist; + + GstVideoCodecState *input_state; + + aom_codec_iface_t *codec_interface; + aom_codec_enc_cfg_t aom_cfg; + aom_codec_ctx_t codec; +}; + +struct _GstAV1EncClass +{ + GstVideoEncoderClass parent_class; +}; + +GType gst_av1_enc_get_type (void); + +G_END_DECLS +#endif /* __GST_AV1_ENC_H__ */ diff --git a/ext/aom/meson.build b/ext/aom/meson.build new file mode 100644 index 000000000..96b086ebd --- /dev/null +++ b/ext/aom/meson.build @@ -0,0 +1,11 @@ +aom_dep = dependency('aom', required: false) +if aom_dep.found() + gstaom = library('gstaom', + ['gstaom.c', 'gstav1enc.c', 'gstav1dec.c'], + c_args : gst_plugins_bad_args, + include_directories : [configinc], + dependencies : [gstpbutils_dep, gstvideo_dep, aom_dep], + install : true, + install_dir : plugins_install_dir, + ) +endif diff --git a/ext/meson.build b/ext/meson.build index fbe3513d2..dce4e7480 100644 --- a/ext/meson.build +++ b/ext/meson.build @@ -1,4 +1,5 @@ subdir('assrender') +subdir('aom') #subdir('bs2b') subdir('bz2') subdir('chromaprint') |