From eaf57cc90b991a59aed548f4a4ad022500c5aa5f Mon Sep 17 00:00:00 2001 From: Sreerenj Balachandran Date: Mon, 25 May 2015 11:58:20 +0300 Subject: [PATCH] HEVC_Encode: Add HEVC(h265) Encoder plugin https://bugzilla.gnome.org/show_bug.cgi?id=748874 Signed-off-by: Sreerenj Balachandran --- gst/vaapi/Makefile.am | 2 + gst/vaapi/gstvaapi.c | 10 + gst/vaapi/gstvaapiencode_h265.c | 403 ++++++++++++++++++++++++++++++++ gst/vaapi/gstvaapiencode_h265.h | 71 ++++++ 4 files changed, 486 insertions(+) create mode 100644 gst/vaapi/gstvaapiencode_h265.c create mode 100644 gst/vaapi/gstvaapiencode_h265.h diff --git a/gst/vaapi/Makefile.am b/gst/vaapi/Makefile.am index a66db37cce..75cf214c4b 100644 --- a/gst/vaapi/Makefile.am +++ b/gst/vaapi/Makefile.am @@ -81,12 +81,14 @@ libgstvaapi_enc_source_c = \ gstvaapiencode.c \ gstvaapiencode_h264.c \ gstvaapiencode_mpeg2.c \ + gstvaapiencode_h265.c \ $(NULL) libgstvaapi_enc_source_h = \ gstvaapiencode.h \ gstvaapiencode_h264.h \ gstvaapiencode_mpeg2.h \ + gstvaapiencode_h265.h \ $(NULL) if USE_ENCODERS diff --git a/gst/vaapi/gstvaapi.c b/gst/vaapi/gstvaapi.c index 64ee44c477..e5bf227257 100644 --- a/gst/vaapi/gstvaapi.c +++ b/gst/vaapi/gstvaapi.c @@ -42,6 +42,10 @@ #include "gstvaapiencode_vp8.h" #endif +#if USE_H265_ENCODER +#include "gstvaapiencode_h265.h" +#endif + #define PLUGIN_NAME "vaapi" #define PLUGIN_DESC "VA-API based elements" #define PLUGIN_LICENSE "LGPL" @@ -77,6 +81,12 @@ plugin_init (GstPlugin *plugin) GST_TYPE_VAAPIENCODE_VP8); #endif +#if USE_H265_ENCODER + gst_element_register(plugin, "vaapiencode_h265", + GST_RANK_PRIMARY, + GST_TYPE_VAAPIENCODE_H265); +#endif + #if GST_CHECK_VERSION(1,4,0) gst_element_register(plugin, "vaapidecodebin", GST_RANK_PRIMARY + 2, diff --git a/gst/vaapi/gstvaapiencode_h265.c b/gst/vaapi/gstvaapiencode_h265.c new file mode 100644 index 0000000000..9bd38b6eb3 --- /dev/null +++ b/gst/vaapi/gstvaapiencode_h265.c @@ -0,0 +1,403 @@ +/* + * gstvaapiencode_h265.c - VA-API H.265 encoder + * + * Copyright (C) 2015 Intel Corporation + * Author: Sreerenj Balachandran + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "gstcompat.h" +#include +#include +#include +#include "gstvaapiencode_h265.h" +#include "gstvaapipluginutil.h" +#include "gstvaapivideomemory.h" + +#define GST_PLUGIN_NAME "vaapiencode_h265" +#define GST_PLUGIN_DESC "A VA-API based H.265 video encoder" + +GST_DEBUG_CATEGORY_STATIC (gst_vaapi_h265_encode_debug); +#define GST_CAT_DEFAULT gst_vaapi_h265_encode_debug + +#define GST_CODEC_CAPS \ + "video/x-h265, " \ + "stream-format = (string) { hvc1, byte-stream }, " \ + "alignment = (string) au" + +/* *INDENT-OFF* */ +static const char gst_vaapiencode_h265_sink_caps_str[] = + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE, + "{ ENCODED, NV12, I420, YV12 }") ", " + GST_CAPS_INTERLACED_FALSE "; " + GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ", " + GST_CAPS_INTERLACED_FALSE; +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +static const char gst_vaapiencode_h265_src_caps_str[] = + GST_CODEC_CAPS ", " + "profile = (string) { main }"; +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +static GstStaticPadTemplate gst_vaapiencode_h265_sink_factory = + GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (gst_vaapiencode_h265_sink_caps_str)); +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +static GstStaticPadTemplate gst_vaapiencode_h265_src_factory = + GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (gst_vaapiencode_h265_src_caps_str)); +/* *INDENT-ON* */ + +/* h265 encode */ +G_DEFINE_TYPE (GstVaapiEncodeH265, gst_vaapiencode_h265, GST_TYPE_VAAPIENCODE); + +static void +gst_vaapiencode_h265_init (GstVaapiEncodeH265 * encode) +{ + gst_vaapiencode_init_properties (GST_VAAPIENCODE_CAST (encode)); +} + +static void +gst_vaapiencode_h265_finalize (GObject * object) +{ + G_OBJECT_CLASS (gst_vaapiencode_h265_parent_class)->finalize (object); +} + +static void +gst_vaapiencode_h265_set_property (GObject * object, + guint prop_id, const GValue * value, GParamSpec * pspec) +{ + GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_GET_CLASS (object); + GstVaapiEncode *const base_encode = GST_VAAPIENCODE_CAST (object); + + switch (prop_id) { + default: + if (!encode_class->set_property (base_encode, prop_id, value)) + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_vaapiencode_h265_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_GET_CLASS (object); + GstVaapiEncode *const base_encode = GST_VAAPIENCODE_CAST (object); + + switch (prop_id) { + default: + if (!encode_class->get_property (base_encode, prop_id, value)) + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +typedef struct +{ + GstVaapiProfile best_profile; + guint best_score; +} FindBestProfileData; + +static void +find_best_profile_value (FindBestProfileData * data, const GValue * value) +{ + const gchar *str; + GstVaapiProfile profile; + guint score; + + if (!value || !G_VALUE_HOLDS_STRING (value)) + return; + + str = g_value_get_string (value); + if (!str) + return; + profile = gst_vaapi_utils_h265_get_profile_from_string (str); + if (!profile) + return; + score = gst_vaapi_utils_h265_get_profile_score (profile); + if (score < data->best_score) + return; + data->best_profile = profile; + data->best_score = score; +} + +static GstVaapiProfile +find_best_profile (GstCaps * caps) +{ + FindBestProfileData data; + guint i, j, num_structures, num_values; + + data.best_profile = GST_VAAPI_PROFILE_UNKNOWN; + data.best_score = 0; + + num_structures = gst_caps_get_size (caps); + for (i = 0; i < num_structures; i++) { + GstStructure *const structure = gst_caps_get_structure (caps, i); + const GValue *const value = gst_structure_get_value (structure, "profile"); + + if (!value) + continue; + if (G_VALUE_HOLDS_STRING (value)) + find_best_profile_value (&data, value); + else if (GST_VALUE_HOLDS_LIST (value)) { + num_values = gst_value_list_get_size (value); + for (j = 0; j < num_values; j++) + find_best_profile_value (&data, gst_value_list_get_value (value, j)); + } + } + return data.best_profile; +} + +static gboolean +gst_vaapiencode_h265_set_config (GstVaapiEncode * base_encode) +{ + GstVaapiEncoderH265 *const encoder = + GST_VAAPI_ENCODER_H265 (base_encode->encoder); + GstCaps *allowed_caps; + GstVaapiProfile profile; + + /* Check for the largest profile that is supported */ + allowed_caps = + gst_pad_get_allowed_caps (GST_VAAPI_PLUGIN_BASE_SRC_PAD (base_encode)); + if (!allowed_caps) + return TRUE; + + profile = find_best_profile (allowed_caps); + gst_caps_unref (allowed_caps); + if (profile) { + GST_INFO ("using %s profile as target decoder constraints", + gst_vaapi_utils_h265_get_profile_string (profile)); + if (!gst_vaapi_encoder_h265_set_max_profile (encoder, profile)) + return FALSE; + } + return TRUE; +} + +static GstCaps * +gst_vaapiencode_h265_get_caps (GstVaapiEncode * base_encode) +{ + GstVaapiEncodeH265 *const encode = GST_VAAPIENCODE_H265_CAST (base_encode); + GstCaps *caps, *allowed_caps; + + caps = gst_caps_from_string (GST_CODEC_CAPS); + + /* Check whether "stream-format" is hvcC mode */ + allowed_caps = + gst_pad_get_allowed_caps (GST_VAAPI_PLUGIN_BASE_SRC_PAD (encode)); + if (allowed_caps) { + const char *stream_format = NULL; + GstStructure *structure; + guint i, num_structures; + + num_structures = gst_caps_get_size (allowed_caps); + for (i = 0; !stream_format && i < num_structures; i++) { + structure = gst_caps_get_structure (allowed_caps, i); + if (!gst_structure_has_field_typed (structure, "stream-format", + G_TYPE_STRING)) + continue; + stream_format = gst_structure_get_string (structure, "stream-format"); + } + encode->is_hvc = stream_format && strcmp (stream_format, "hvc1") == 0; + gst_caps_unref (allowed_caps); + } + gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, + encode->is_hvc ? "hvc1" : "byte-stream", NULL); + + base_encode->need_codec_data = encode->is_hvc; + + /* XXX: update profile and level information */ + return caps; +} + +static GstVaapiEncoder * +gst_vaapiencode_h265_alloc_encoder (GstVaapiEncode * base, + GstVaapiDisplay * display) +{ + return gst_vaapi_encoder_h265_new (display); +} + +/* h265 NAL byte stream operations */ +static guint8 * +_h265_byte_stream_next_nal (guint8 * buffer, guint32 len, guint32 * nal_size) +{ + const guint8 *cur = buffer; + const guint8 *const end = buffer + len; + guint8 *nal_start = NULL; + guint32 flag = 0xFFFFFFFF; + guint32 nal_start_len = 0; + + g_assert (len >= 0 && buffer && nal_size); + if (len < 3) { + *nal_size = len; + nal_start = (len ? buffer : NULL); + return nal_start; + } + + /*locate head postion */ + if (!buffer[0] && !buffer[1]) { + if (buffer[2] == 1) { /* 0x000001 */ + nal_start_len = 3; + } else if (!buffer[2] && len >= 4 && buffer[3] == 1) { /* 0x00000001 */ + nal_start_len = 4; + } + } + nal_start = buffer + nal_start_len; + cur = nal_start; + + /*find next nal start position */ + while (cur < end) { + flag = ((flag << 8) | ((*cur++) & 0xFF)); + if ((flag & 0x00FFFFFF) == 0x00000001) { + if (flag == 0x00000001) + *nal_size = cur - 4 - nal_start; + else + *nal_size = cur - 3 - nal_start; + break; + } + } + if (cur >= end) { + *nal_size = end - nal_start; + if (nal_start >= end) { + nal_start = NULL; + } + } + return nal_start; +} + +static inline void +_start_code_to_size (guint8 nal_start_code[4], guint32 nal_size) +{ + nal_start_code[0] = ((nal_size >> 24) & 0xFF); + nal_start_code[1] = ((nal_size >> 16) & 0xFF); + nal_start_code[2] = ((nal_size >> 8) & 0xFF); + nal_start_code[3] = (nal_size & 0xFF); +} + +static gboolean +_h265_convert_byte_stream_to_hvc (GstBuffer * buf) +{ + GstMapInfo info; + guint32 nal_size; + guint8 *nal_start_code, *nal_body; + guint8 *frame_end; + + g_assert (buf); + + if (!gst_buffer_map (buf, &info, GST_MAP_READ | GST_MAP_WRITE)) + return FALSE; + + nal_start_code = info.data; + frame_end = info.data + info.size; + nal_size = 0; + + while ((frame_end > nal_start_code) && + (nal_body = _h265_byte_stream_next_nal (nal_start_code, + frame_end - nal_start_code, &nal_size)) != NULL) { + if (!nal_size) + goto error; + + g_assert (nal_body - nal_start_code == 4); + _start_code_to_size (nal_start_code, nal_size); + nal_start_code = nal_body + nal_size; + } + gst_buffer_unmap (buf, &info); + return TRUE; + +error: + gst_buffer_unmap (buf, &info); + return FALSE; +} + +static GstFlowReturn +gst_vaapiencode_h265_alloc_buffer (GstVaapiEncode * base_encode, + GstVaapiCodedBuffer * coded_buf, GstBuffer ** out_buffer_ptr) +{ + GstVaapiEncodeH265 *const encode = GST_VAAPIENCODE_H265_CAST (base_encode); + GstVaapiEncoderH265 *const encoder = + GST_VAAPI_ENCODER_H265 (base_encode->encoder); + GstFlowReturn ret; + + g_return_val_if_fail (encoder != NULL, GST_FLOW_ERROR); + + ret = + GST_VAAPIENCODE_CLASS (gst_vaapiencode_h265_parent_class)->alloc_buffer + (base_encode, coded_buf, out_buffer_ptr); + if (ret != GST_FLOW_OK) + return ret; + + if (!encode->is_hvc) + return GST_FLOW_OK; + + /* Convert to hvcC format */ + if (!_h265_convert_byte_stream_to_hvc (*out_buffer_ptr)) + goto error_convert_buffer; + return GST_FLOW_OK; + + /* ERRORS */ +error_convert_buffer: + { + GST_ERROR ("failed to convert from bytestream format to hvcC format"); + gst_buffer_replace (out_buffer_ptr, NULL); + return GST_FLOW_ERROR; + } +} + +static void +gst_vaapiencode_h265_class_init (GstVaapiEncodeH265Class * klass) +{ + GObjectClass *const object_class = G_OBJECT_CLASS (klass); + GstElementClass *const element_class = GST_ELEMENT_CLASS (klass); + GstVaapiEncodeClass *const encode_class = GST_VAAPIENCODE_CLASS (klass); + + GST_DEBUG_CATEGORY_INIT (gst_vaapi_h265_encode_debug, + GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC); + + object_class->finalize = gst_vaapiencode_h265_finalize; + object_class->set_property = gst_vaapiencode_h265_set_property; + object_class->get_property = gst_vaapiencode_h265_get_property; + + encode_class->get_properties = gst_vaapi_encoder_h265_get_default_properties; + encode_class->set_config = gst_vaapiencode_h265_set_config; + encode_class->get_caps = gst_vaapiencode_h265_get_caps; + encode_class->alloc_encoder = gst_vaapiencode_h265_alloc_encoder; + encode_class->alloc_buffer = gst_vaapiencode_h265_alloc_buffer; + + gst_element_class_set_static_metadata (element_class, + "VA-API H.265 encoder", + "Codec/Encoder/Video", + GST_PLUGIN_DESC, + "Sreerenj Balachandran "); + + /* sink pad */ + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&gst_vaapiencode_h265_sink_factory)); + + /* src pad */ + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&gst_vaapiencode_h265_src_factory)); + + gst_vaapiencode_class_init_properties (encode_class); +} diff --git a/gst/vaapi/gstvaapiencode_h265.h b/gst/vaapi/gstvaapiencode_h265.h new file mode 100644 index 0000000000..db567b3946 --- /dev/null +++ b/gst/vaapi/gstvaapiencode_h265.h @@ -0,0 +1,71 @@ +/* + * gstvaapiencode_h265.h - VA-API H.265 encoder + * + * Copyright (C) 2015 Intel Corporation + * Author: Sreerenj Balachandran + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#ifndef GST_VAAPIENCODE_H265_H +#define GST_VAAPIENCODE_H265_H + +#include +#include "gstvaapiencode.h" + +G_BEGIN_DECLS + +#define GST_TYPE_VAAPIENCODE_H265 \ + (gst_vaapiencode_h265_get_type ()) +#define GST_VAAPIENCODE_H265_CAST(obj) \ + ((GstVaapiEncodeH265 *)(obj)) +#define GST_VAAPIENCODE_H265(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_VAAPIENCODE_H265, \ + GstVaapiEncodeH265)) +#define GST_VAAPIENCODE_H265_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_VAAPIENCODE_H265, \ + GstVaapiEncodeH265Class)) +#define GST_VAAPIENCODE_H265_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_VAAPIENCODE_H265, \ + GstVaapiEncodeH265Class)) +#define GST_IS_VAAPIENCODE_H265(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_VAAPIENCODE_H265)) +#define GST_IS_VAAPIENCODE_H265_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_VAAPIENCODE_H265)) + +typedef struct _GstVaapiEncodeH265 GstVaapiEncodeH265; +typedef struct _GstVaapiEncodeH265Class GstVaapiEncodeH265Class; + +struct _GstVaapiEncodeH265 +{ + /*< private >*/ + GstVaapiEncode parent_instance; + + guint is_hvc:1; /* [FALSE]=byte-stream (default); [TRUE]=hvcC */ +}; + +struct _GstVaapiEncodeH265Class +{ + /*< private >*/ + GstVaapiEncodeClass parent_class; +}; + +GType +gst_vaapiencode_h265_get_type (void) G_GNUC_CONST; + +G_END_DECLS + +#endif /* GST_VAAPIENCODE_H265_H */ -- 2.34.1