2010-05-13 08:04:04 +00:00
|
|
|
/* VP8
|
|
|
|
* Copyright (C) 2006 David Schleef <ds@schleef.org>
|
|
|
|
* Copyright (C) 2010 Entropy Wave Inc
|
2012-09-14 21:41:48 +00:00
|
|
|
* Copyright (C) 2010-2012 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
2010-05-13 08:04:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-05-13 08:04:04 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-07-03 15:47:29 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-vp8enc
|
2018-10-22 09:39:24 +00:00
|
|
|
* @title: vp8enc
|
2010-07-03 15:47:29 +00:00
|
|
|
* @see_also: vp8dec, webmmux, oggmux
|
|
|
|
*
|
|
|
|
* This element encodes raw video into a VP8 stream.
|
2019-08-23 16:56:01 +00:00
|
|
|
* [VP8](http://www.webmproject.org) is a royalty-free video codec maintained by
|
|
|
|
* [Google](http://www.google.com/). It's the successor of On2 VP3, which was
|
|
|
|
* the base of the Theora video codec.
|
2010-07-03 15:47:29 +00:00
|
|
|
*
|
2020-06-23 16:47:44 +00:00
|
|
|
* To control the quality of the encoding, the #GstVPXEnc:target-bitrate,
|
|
|
|
* #GstVPXEnc:min-quantizer, #GstVPXEnc:max-quantizer or #GstVPXEnc:cq-level
|
2012-09-16 14:03:06 +00:00
|
|
|
* properties can be used. Which one is used depends on the mode selected by
|
2020-06-23 16:47:44 +00:00
|
|
|
* the #GstVPXEnc:end-usage property.
|
2019-08-23 16:56:01 +00:00
|
|
|
* See [Encoder Parameters](http://www.webmproject.org/docs/encoder-parameters/)
|
2012-09-16 14:03:06 +00:00
|
|
|
* for explanation, examples for useful encoding parameters and more details
|
|
|
|
* on the encoding parameters.
|
2010-07-03 15:47:29 +00:00
|
|
|
*
|
2018-10-22 09:39:24 +00:00
|
|
|
* ## Example pipeline
|
2010-07-03 15:47:29 +00:00
|
|
|
* |[
|
2015-05-10 10:05:00 +00:00
|
|
|
* gst-launch-1.0 -v videotestsrc num-buffers=1000 ! vp8enc ! webmmux ! filesink location=videotestsrc.webm
|
2010-07-03 15:47:29 +00:00
|
|
|
* ]| This example pipeline will encode a test video source to VP8 muxed in an
|
|
|
|
* WebM container.
|
2018-10-22 09:39:24 +00:00
|
|
|
*
|
2010-07-03 15:47:29 +00:00
|
|
|
*/
|
2010-05-13 08:04:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-21 06:13:06 +00:00
|
|
|
#ifdef HAVE_VP8_ENCODER
|
|
|
|
|
2012-09-25 12:43:15 +00:00
|
|
|
/* glib decided in 2.32 it would be a great idea to deprecated GValueArray without
|
|
|
|
* providing an alternative
|
|
|
|
*
|
|
|
|
* See https://bugzilla.gnome.org/show_bug.cgi?id=667228
|
|
|
|
* */
|
|
|
|
#define GLIB_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
|
2010-05-14 08:30:18 +00:00
|
|
|
#include <gst/tag/tag.h>
|
2012-12-12 17:35:04 +00:00
|
|
|
#include <gst/video/video.h>
|
2010-05-13 08:04:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-02-15 10:00:46 +00:00
|
|
|
#include "gstvpxelements.h"
|
2010-05-19 15:13:17 +00:00
|
|
|
#include "gstvp8utils.h"
|
2010-07-03 15:34:58 +00:00
|
|
|
#include "gstvp8enc.h"
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2010-05-20 08:19:54 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_vp8enc_debug);
|
2010-05-13 08:04:04 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_vp8enc_debug
|
|
|
|
|
2010-05-14 08:30:18 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
vpx_image_t *image;
|
|
|
|
GList *invisible;
|
2017-09-08 07:19:20 +00:00
|
|
|
guint layer_id;
|
|
|
|
guint8 tl0picidx;
|
|
|
|
gboolean layer_sync;
|
2012-05-16 12:04:28 +00:00
|
|
|
} GstVP8EncUserData;
|
2010-05-14 08:30:18 +00:00
|
|
|
|
2011-07-09 09:31:02 +00:00
|
|
|
static void
|
|
|
|
_gst_mini_object_unref0 (GstMiniObject * obj)
|
|
|
|
{
|
|
|
|
if (obj)
|
|
|
|
gst_mini_object_unref (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-16 12:04:28 +00:00
|
|
|
gst_vp8_enc_user_data_free (GstVP8EncUserData * user_data)
|
2011-07-09 09:31:02 +00:00
|
|
|
{
|
2012-05-16 12:04:28 +00:00
|
|
|
if (user_data->image)
|
2023-01-08 17:26:17 +00:00
|
|
|
g_free (user_data->image);
|
2011-07-09 09:31:02 +00:00
|
|
|
|
2012-05-16 12:04:28 +00:00
|
|
|
g_list_foreach (user_data->invisible, (GFunc) _gst_mini_object_unref0, NULL);
|
|
|
|
g_list_free (user_data->invisible);
|
2023-01-08 17:26:17 +00:00
|
|
|
g_free (user_data);
|
2011-07-09 09:31:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static vpx_codec_iface_t *gst_vp8_enc_get_algo (GstVPXEnc * enc);
|
|
|
|
static gboolean gst_vp8_enc_enable_scaling (GstVPXEnc * enc);
|
|
|
|
static void gst_vp8_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image);
|
|
|
|
static GstCaps *gst_vp8_enc_get_new_simple_caps (GstVPXEnc * enc);
|
|
|
|
static void gst_vp8_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
|
|
|
|
GstVideoInfo * info);
|
|
|
|
static void *gst_vp8_enc_process_frame_user_data (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame);
|
|
|
|
static GstFlowReturn gst_vp8_enc_handle_invisible_frame_buffer (GstVPXEnc * enc,
|
|
|
|
void *user_data, GstBuffer * buffer);
|
|
|
|
static void gst_vp8_enc_set_frame_user_data (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, vpx_image_t * image);
|
2017-09-08 07:19:20 +00:00
|
|
|
static void gst_vp8_enc_apply_frame_temporal_settings (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, guint layer_id, guint8 tl0picidx,
|
|
|
|
gboolean layer_sync);
|
|
|
|
static void gst_vp8_enc_get_frame_temporal_settings (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, guint * layer_id, guint8 * tl0picidx,
|
|
|
|
gboolean * layer_sync);
|
|
|
|
static void gst_vp8_enc_preflight_buffer (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, GstBuffer * buffer,
|
|
|
|
gboolean layer_sync, guint layer_id, guint8 tl0picidx);
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
static GstFlowReturn gst_vp8_enc_pre_push (GstVideoEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
2010-05-13 08:04:04 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_vp8_enc_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2015-04-01 23:01:49 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-raw, "
|
|
|
|
"format = (string) \"I420\", "
|
|
|
|
"width = (int) [1, 16383], "
|
|
|
|
"height = (int) [1, 16383], framerate = (fraction) [ 0/1, MAX ]")
|
2010-05-13 08:04:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_vp8_enc_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-09-15 18:23:13 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-vp8, " "profile = (string) {0, 1, 2, 3}")
|
2010-05-13 08:04:04 +00:00
|
|
|
);
|
|
|
|
|
2012-05-28 14:05:21 +00:00
|
|
|
#define parent_class gst_vp8_enc_parent_class
|
2015-09-24 07:27:00 +00:00
|
|
|
G_DEFINE_TYPE (GstVP8Enc, gst_vp8_enc, GST_TYPE_VPX_ENC);
|
2021-02-15 10:00:46 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (vp8enc, "vp8enc", GST_RANK_PRIMARY,
|
|
|
|
gst_vp8_enc_get_type (), vpx_element_init (plugin));
|
2011-11-03 13:01:41 +00:00
|
|
|
|
2010-05-18 12:47:54 +00:00
|
|
|
static void
|
2011-11-03 13:01:41 +00:00
|
|
|
gst_vp8_enc_class_init (GstVP8EncClass * klass)
|
2010-05-18 12:47:54 +00:00
|
|
|
{
|
2012-05-28 14:05:21 +00:00
|
|
|
GstElementClass *element_class;
|
2012-04-24 09:08:41 +00:00
|
|
|
GstVideoEncoderClass *video_encoder_class;
|
2015-09-24 07:27:00 +00:00
|
|
|
GstVPXEncClass *vpx_encoder_class;
|
2010-05-18 12:47:54 +00:00
|
|
|
|
2012-05-28 14:05:21 +00:00
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
2012-04-24 09:08:41 +00:00
|
|
|
video_encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
|
2015-09-24 07:27:00 +00:00
|
|
|
vpx_encoder_class = GST_VPX_ENC_CLASS (klass);
|
2010-05-13 08:04:04 +00:00
|
|
|
|
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_vp8_enc_src_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_vp8_enc_sink_template);
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-10-17 16:03:39 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2010-05-13 08:04:04 +00:00
|
|
|
"On2 VP8 Encoder",
|
|
|
|
"Codec/Encoder/Video",
|
2012-09-14 21:41:48 +00:00
|
|
|
"Encode VP8 video streams", "David Schleef <ds@entropywave.com>, "
|
|
|
|
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
video_encoder_class->pre_push = gst_vp8_enc_pre_push;
|
2011-07-10 01:53:24 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
vpx_encoder_class->get_algo = gst_vp8_enc_get_algo;
|
|
|
|
vpx_encoder_class->enable_scaling = gst_vp8_enc_enable_scaling;
|
|
|
|
vpx_encoder_class->set_image_format = gst_vp8_enc_set_image_format;
|
|
|
|
vpx_encoder_class->get_new_vpx_caps = gst_vp8_enc_get_new_simple_caps;
|
|
|
|
vpx_encoder_class->set_stream_info = gst_vp8_enc_set_stream_info;
|
|
|
|
vpx_encoder_class->process_frame_user_data =
|
|
|
|
gst_vp8_enc_process_frame_user_data;
|
|
|
|
vpx_encoder_class->handle_invisible_frame_buffer =
|
|
|
|
gst_vp8_enc_handle_invisible_frame_buffer;
|
|
|
|
vpx_encoder_class->set_frame_user_data = gst_vp8_enc_set_frame_user_data;
|
2017-09-08 07:19:20 +00:00
|
|
|
vpx_encoder_class->apply_frame_temporal_settings =
|
|
|
|
gst_vp8_enc_apply_frame_temporal_settings;
|
|
|
|
vpx_encoder_class->get_frame_temporal_settings =
|
|
|
|
gst_vp8_enc_get_frame_temporal_settings;
|
|
|
|
vpx_encoder_class->preflight_buffer = gst_vp8_enc_preflight_buffer;
|
2013-03-06 13:17:54 +00:00
|
|
|
|
2010-05-20 08:19:54 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_vp8enc_debug, "vp8enc", 0, "VP8 Encoder");
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-28 14:05:21 +00:00
|
|
|
gst_vp8_enc_init (GstVP8Enc * gst_vp8_enc)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
2012-09-17 07:16:39 +00:00
|
|
|
vpx_codec_err_t status;
|
2015-09-24 07:27:00 +00:00
|
|
|
GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (gst_vp8_enc);
|
|
|
|
GST_DEBUG_OBJECT (gst_vp8_enc, "gst_vp8_enc_init");
|
2012-09-17 07:16:39 +00:00
|
|
|
status =
|
2015-09-24 07:27:00 +00:00
|
|
|
vpx_codec_enc_config_default (gst_vp8_enc_get_algo (gst_vpx_enc),
|
|
|
|
&gst_vpx_enc->cfg, 0);
|
2012-09-17 07:16:39 +00:00
|
|
|
if (status != VPX_CODEC_OK) {
|
2015-09-24 07:27:00 +00:00
|
|
|
GST_ERROR_OBJECT (gst_vpx_enc,
|
2012-09-17 07:16:39 +00:00
|
|
|
"Failed to get default encoder configuration: %s",
|
|
|
|
gst_vpx_error_name (status));
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_vpx_enc->have_default_config = FALSE;
|
2012-09-17 07:40:41 +00:00
|
|
|
} else {
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_vpx_enc->have_default_config = TRUE;
|
2012-09-17 07:40:41 +00:00
|
|
|
}
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static vpx_codec_iface_t *
|
|
|
|
gst_vp8_enc_get_algo (GstVPXEnc * enc)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
2015-09-24 07:27:00 +00:00
|
|
|
return &vpx_codec_vp8_cx_algo;
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_vp8_enc_enable_scaling (GstVPXEnc * enc)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-03-09 15:46:02 +00:00
|
|
|
static void
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_vp8_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
|
2015-03-09 15:46:02 +00:00
|
|
|
{
|
2011-03-24 12:59:35 +00:00
|
|
|
image->fmt = VPX_IMG_FMT_I420;
|
|
|
|
image->bps = 12;
|
|
|
|
image->x_chroma_shift = image->y_chroma_shift = 1;
|
2015-09-24 07:27:00 +00:00
|
|
|
}
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_vp8_enc_get_new_simple_caps (GstVPXEnc * enc)
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
gchar *profile_str = g_strdup_printf ("%d", enc->cfg.g_profile);
|
2012-09-14 22:04:07 +00:00
|
|
|
caps = gst_caps_new_simple ("video/x-vp8",
|
2012-09-15 18:23:13 +00:00
|
|
|
"profile", G_TYPE_STRING, profile_str, NULL);
|
|
|
|
g_free (profile_str);
|
2015-09-24 07:27:00 +00:00
|
|
|
return caps;
|
|
|
|
}
|
2012-09-15 18:23:13 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static void
|
|
|
|
gst_vp8_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
|
|
|
|
GstVideoInfo * info)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
GstVideoEncoder *video_encoder;
|
|
|
|
GstBuffer *stream_hdr, *vorbiscomment;
|
|
|
|
const GstTagList *iface_tags;
|
|
|
|
GValue array = { 0, };
|
|
|
|
GValue value = { 0, };
|
|
|
|
guint8 *data = NULL;
|
|
|
|
GstMapInfo map;
|
|
|
|
|
|
|
|
video_encoder = GST_VIDEO_ENCODER (enc);
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
/* put buffers in a fixed list */
|
|
|
|
g_value_init (&array, GST_TYPE_ARRAY);
|
|
|
|
g_value_init (&value, GST_TYPE_BUFFER);
|
|
|
|
|
|
|
|
/* Create Ogg stream-info */
|
|
|
|
stream_hdr = gst_buffer_new_and_alloc (26);
|
|
|
|
gst_buffer_map (stream_hdr, &map, GST_MAP_WRITE);
|
|
|
|
data = map.data;
|
|
|
|
|
|
|
|
GST_WRITE_UINT8 (data, 0x4F);
|
|
|
|
GST_WRITE_UINT32_BE (data + 1, 0x56503830); /* "VP80" */
|
|
|
|
GST_WRITE_UINT8 (data + 5, 0x01); /* stream info header */
|
|
|
|
GST_WRITE_UINT8 (data + 6, 1); /* Major version 1 */
|
|
|
|
GST_WRITE_UINT8 (data + 7, 0); /* Minor version 0 */
|
|
|
|
GST_WRITE_UINT16_BE (data + 8, GST_VIDEO_INFO_WIDTH (info));
|
|
|
|
GST_WRITE_UINT16_BE (data + 10, GST_VIDEO_INFO_HEIGHT (info));
|
|
|
|
GST_WRITE_UINT24_BE (data + 12, GST_VIDEO_INFO_PAR_N (info));
|
|
|
|
GST_WRITE_UINT24_BE (data + 15, GST_VIDEO_INFO_PAR_D (info));
|
|
|
|
GST_WRITE_UINT32_BE (data + 18, GST_VIDEO_INFO_FPS_N (info));
|
|
|
|
GST_WRITE_UINT32_BE (data + 22, GST_VIDEO_INFO_FPS_D (info));
|
|
|
|
|
|
|
|
gst_buffer_unmap (stream_hdr, &map);
|
|
|
|
|
|
|
|
GST_BUFFER_FLAG_SET (stream_hdr, GST_BUFFER_FLAG_HEADER);
|
|
|
|
gst_value_set_buffer (&value, stream_hdr);
|
|
|
|
gst_value_array_append_value (&array, &value);
|
|
|
|
g_value_unset (&value);
|
|
|
|
gst_buffer_unref (stream_hdr);
|
|
|
|
|
|
|
|
iface_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (video_encoder));
|
|
|
|
if (iface_tags) {
|
|
|
|
vorbiscomment =
|
|
|
|
gst_tag_list_to_vorbiscomment_buffer (iface_tags,
|
|
|
|
(const guint8 *) "OVP80\2 ", 7,
|
|
|
|
"Encoded with GStreamer vp8enc " PACKAGE_VERSION);
|
|
|
|
|
|
|
|
GST_BUFFER_FLAG_SET (vorbiscomment, GST_BUFFER_FLAG_HEADER);
|
2011-08-12 10:08:08 +00:00
|
|
|
|
2010-05-19 19:34:42 +00:00
|
|
|
g_value_init (&value, GST_TYPE_BUFFER);
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_value_set_buffer (&value, vorbiscomment);
|
2010-05-19 19:34:42 +00:00
|
|
|
gst_value_array_append_value (&array, &value);
|
|
|
|
g_value_unset (&value);
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_buffer_unref (vorbiscomment);
|
2010-05-19 19:34:42 +00:00
|
|
|
}
|
2010-05-14 08:30:18 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_structure_set_value (s, "streamheader", &array);
|
|
|
|
g_value_unset (&array);
|
2012-07-26 17:31:14 +00:00
|
|
|
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static void *
|
|
|
|
gst_vp8_enc_process_frame_user_data (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
2012-05-16 12:04:28 +00:00
|
|
|
GstVP8EncUserData *user_data;
|
2016-01-20 11:41:33 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
user_data = gst_video_codec_frame_get_user_data (frame);
|
2016-01-20 11:41:33 +00:00
|
|
|
|
|
|
|
if (!user_data) {
|
|
|
|
GST_ERROR_OBJECT (enc, "Have no frame user data");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
if (user_data->image)
|
2023-01-08 17:26:17 +00:00
|
|
|
g_free (user_data->image);
|
2015-09-24 07:27:00 +00:00
|
|
|
user_data->image = NULL;
|
|
|
|
return user_data;
|
2011-03-24 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 06:03:51 +00:00
|
|
|
static GstFlowReturn
|
2015-09-24 07:27:00 +00:00
|
|
|
gst_vp8_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
|
|
|
|
GstBuffer * buffer)
|
2011-03-24 11:50:23 +00:00
|
|
|
{
|
2015-09-24 07:27:00 +00:00
|
|
|
GstVP8EncUserData *vp8_user_data = (GstVP8EncUserData *) user_data;
|
2016-01-20 11:41:33 +00:00
|
|
|
|
|
|
|
if (!vp8_user_data) {
|
|
|
|
GST_ERROR_OBJECT (enc, "Have no frame user data");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
vp8_user_data->invisible = g_list_append (vp8_user_data->invisible, buffer);
|
2016-01-20 11:41:33 +00:00
|
|
|
|
2011-07-21 06:03:51 +00:00
|
|
|
return GST_FLOW_OK;
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
static void
|
|
|
|
gst_vp8_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
|
|
|
|
vpx_image_t * image)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
2012-05-16 12:04:28 +00:00
|
|
|
GstVP8EncUserData *user_data;
|
2023-01-08 17:26:17 +00:00
|
|
|
user_data = g_new0 (GstVP8EncUserData, 1);
|
2012-05-16 12:04:28 +00:00
|
|
|
user_data->image = image;
|
|
|
|
gst_video_codec_frame_set_user_data (frame, user_data,
|
|
|
|
(GDestroyNotify) gst_vp8_enc_user_data_free);
|
2015-09-24 07:27:00 +00:00
|
|
|
return;
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 07:19:20 +00:00
|
|
|
static void
|
|
|
|
gst_vp8_enc_apply_frame_temporal_settings (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, guint layer_id, guint8 tl0picidx,
|
|
|
|
gboolean layer_sync)
|
|
|
|
{
|
|
|
|
GstVP8EncUserData *user_data;
|
|
|
|
|
|
|
|
user_data = gst_video_codec_frame_get_user_data (frame);
|
|
|
|
|
|
|
|
if (!user_data) {
|
|
|
|
GST_ERROR_OBJECT (enc, "Have no frame user data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vpx_codec_control (&enc->encoder, VP8E_SET_TEMPORAL_LAYER_ID, layer_id);
|
|
|
|
user_data->layer_id = layer_id;
|
|
|
|
user_data->tl0picidx = tl0picidx;
|
|
|
|
user_data->layer_sync = layer_sync;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vp8_enc_get_frame_temporal_settings (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, guint * layer_id, guint8 * tl0picidx,
|
|
|
|
gboolean * layer_sync)
|
|
|
|
{
|
|
|
|
GstVP8EncUserData *user_data;
|
|
|
|
|
|
|
|
user_data = gst_video_codec_frame_get_user_data (frame);
|
|
|
|
|
|
|
|
if (!user_data) {
|
|
|
|
GST_ERROR_OBJECT (enc, "Have no frame user data");
|
|
|
|
*layer_id = 0;
|
|
|
|
*tl0picidx = 0;
|
|
|
|
*layer_sync = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*layer_id = user_data->layer_id;
|
|
|
|
*tl0picidx = user_data->tl0picidx;
|
|
|
|
*layer_sync = user_data->layer_sync;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vp8_enc_preflight_buffer (GstVPXEnc * enc,
|
|
|
|
GstVideoCodecFrame * frame, GstBuffer * buffer,
|
|
|
|
gboolean layer_sync, guint layer_id, guint8 tl0picidx)
|
|
|
|
{
|
|
|
|
GstCustomMeta *meta = gst_buffer_add_custom_meta (buffer, "GstVP8Meta");
|
|
|
|
|
2023-09-27 12:46:35 +00:00
|
|
|
gst_structure_set (meta->structure,
|
2017-09-08 07:19:20 +00:00
|
|
|
"use-temporal-scaling", G_TYPE_BOOLEAN, (enc->cfg.ts_periodicity != 0),
|
|
|
|
"layer-sync", G_TYPE_BOOLEAN, layer_sync,
|
|
|
|
"layer-id", G_TYPE_UINT, layer_id,
|
|
|
|
"tl0picidx", G_TYPE_UINT, tl0picidx, NULL);
|
|
|
|
}
|
|
|
|
|
2010-05-14 08:30:18 +00:00
|
|
|
static guint64
|
|
|
|
_to_granulepos (guint64 frame_end_number, guint inv_count, guint keyframe_dist)
|
|
|
|
{
|
|
|
|
guint64 granulepos;
|
|
|
|
guint inv;
|
|
|
|
|
|
|
|
inv = (inv_count == 0) ? 0x3 : inv_count - 1;
|
|
|
|
|
|
|
|
granulepos = (frame_end_number << 32) | (inv << 30) | (keyframe_dist << 3);
|
|
|
|
return granulepos;
|
|
|
|
}
|
|
|
|
|
2010-05-13 08:04:04 +00:00
|
|
|
static GstFlowReturn
|
2012-04-24 09:08:41 +00:00
|
|
|
gst_vp8_enc_pre_push (GstVideoEncoder * video_encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
2010-05-13 08:04:04 +00:00
|
|
|
{
|
2010-05-14 08:30:18 +00:00
|
|
|
GstVP8Enc *encoder;
|
2015-09-24 07:27:00 +00:00
|
|
|
GstVPXEnc *vpx_enc;
|
2010-05-14 08:30:18 +00:00
|
|
|
GstBuffer *buf;
|
2012-04-24 09:08:41 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-05-16 12:04:28 +00:00
|
|
|
GstVP8EncUserData *user_data = gst_video_codec_frame_get_user_data (frame);
|
2010-05-14 08:30:18 +00:00
|
|
|
GList *l;
|
|
|
|
gint inv_count;
|
2012-04-24 09:08:41 +00:00
|
|
|
GstVideoInfo *info;
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
GST_DEBUG_OBJECT (video_encoder, "pre_push");
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
encoder = GST_VP8_ENC (video_encoder);
|
2015-09-24 07:27:00 +00:00
|
|
|
vpx_enc = GST_VPX_ENC (encoder);
|
2010-05-14 08:30:18 +00:00
|
|
|
|
2015-09-24 07:27:00 +00:00
|
|
|
info = &vpx_enc->input_state->info;
|
2010-05-13 08:04:04 +00:00
|
|
|
|
2012-05-16 12:04:28 +00:00
|
|
|
g_assert (user_data != NULL);
|
2010-06-12 07:02:29 +00:00
|
|
|
|
2012-05-16 12:04:28 +00:00
|
|
|
for (inv_count = 0, l = user_data->invisible; l; inv_count++, l = l->next) {
|
2010-07-10 14:51:53 +00:00
|
|
|
buf = l->data;
|
|
|
|
l->data = NULL;
|
2010-05-14 08:30:18 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
/* FIXME : All of this should have already been handled by base classes, no ? */
|
2012-05-16 12:04:28 +00:00
|
|
|
if (l == user_data->invisible
|
|
|
|
&& GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
|
2010-05-14 08:30:18 +00:00
|
|
|
GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
encoder->keyframe_distance = 0;
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
encoder->keyframe_distance++;
|
|
|
|
}
|
2010-07-10 14:51:53 +00:00
|
|
|
|
2012-10-02 18:51:29 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DECODE_ONLY);
|
2012-04-24 09:08:41 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_TIMESTAMP (frame->output_buffer);
|
2011-06-26 13:15:54 +00:00
|
|
|
GST_BUFFER_DURATION (buf) = 0;
|
2013-03-06 13:17:54 +00:00
|
|
|
if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
|
|
|
|
GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_OFFSET_END (buf) =
|
|
|
|
_to_granulepos (frame->presentation_frame_number + 1,
|
|
|
|
inv_count, encoder->keyframe_distance);
|
|
|
|
GST_BUFFER_OFFSET (buf) =
|
|
|
|
gst_util_uint64_scale (frame->presentation_frame_number + 1,
|
|
|
|
GST_SECOND * GST_VIDEO_INFO_FPS_D (info),
|
|
|
|
GST_VIDEO_INFO_FPS_N (info));
|
|
|
|
}
|
2010-07-10 14:51:53 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (video_encoder), buf);
|
2010-07-10 14:51:53 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (encoder, "flow error %d", ret);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
buf = frame->output_buffer;
|
2010-07-10 14:51:53 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
/* FIXME : All of this should have already been handled by base classes, no ? */
|
2012-05-16 12:04:28 +00:00
|
|
|
if (!user_data->invisible && GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
|
2010-07-10 14:51:53 +00:00
|
|
|
GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
encoder->keyframe_distance = 0;
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
encoder->keyframe_distance++;
|
2010-05-13 08:04:04 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 13:17:54 +00:00
|
|
|
if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
|
|
|
|
GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_OFFSET_END (buf) =
|
|
|
|
_to_granulepos (frame->presentation_frame_number + 1, 0,
|
|
|
|
encoder->keyframe_distance);
|
|
|
|
GST_BUFFER_OFFSET (buf) =
|
|
|
|
gst_util_uint64_scale (frame->presentation_frame_number + 1,
|
|
|
|
GST_SECOND * GST_VIDEO_INFO_FPS_D (info), GST_VIDEO_INFO_FPS_N (info));
|
|
|
|
}
|
2010-12-01 02:43:24 +00:00
|
|
|
|
2012-04-24 09:08:41 +00:00
|
|
|
GST_LOG_OBJECT (video_encoder, "src ts: %" GST_TIME_FORMAT,
|
2012-01-24 10:22:46 +00:00
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
|
|
|
|
|
2010-05-14 08:30:18 +00:00
|
|
|
done:
|
2010-05-13 08:04:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-05-18 12:47:54 +00:00
|
|
|
|
2010-05-20 14:49:03 +00:00
|
|
|
#endif /* HAVE_VP8_ENCODER */
|