2 * Copyright (C) 2006 David Schleef <ds@schleef.org>
3 * Copyright (C) 2010 Entropy Wave Inc
4 * Copyright (C) 2010-2013 Sebastian Dröge <slomo@circular-chaos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-vp9enc
24 * @see_also: vp9dec, webmmux, oggmux
26 * This element encodes raw video into a VP9 stream.
27 * <ulink url="http://www.webmproject.org">VP9</ulink> is a royalty-free
28 * video codec maintained by <ulink url="http://www.google.com/">Google
29 * </ulink>. It's the successor of On2 VP3, which was the base of the
32 * To control the quality of the encoding, the #GstVP9Enc::target-bitrate,
33 * #GstVP9Enc::min-quantizer, #GstVP9Enc::max-quantizer or #GstVP9Enc::cq-level
34 * properties can be used. Which one is used depends on the mode selected by
35 * the #GstVP9Enc::end-usage property.
36 * See <ulink url="http://www.webmproject.org/docs/encoder-parameters/">Encoder Parameters</ulink>
37 * for explanation, examples for useful encoding parameters and more details
38 * on the encoding parameters.
41 * <title>Example pipeline</title>
43 * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! vp9enc ! webmmux ! filesink location=videotestsrc.webm
44 * ]| This example pipeline will encode a test video source to VP9 muxed in an
53 #ifdef HAVE_VP9_ENCODER
55 /* glib decided in 2.32 it would be a great idea to deprecated GValueArray without
56 * providing an alternative
58 * See https://bugzilla.gnome.org/show_bug.cgi?id=667228
60 #define GLIB_DISABLE_DEPRECATION_WARNINGS
62 #include <gst/tag/tag.h>
63 #include <gst/video/video.h>
66 #include "gstvp8utils.h"
67 #include "gstvp9enc.h"
69 GST_DEBUG_CATEGORY_STATIC (gst_vp9enc_debug);
70 #define GST_CAT_DEFAULT gst_vp9enc_debug
73 /* FIXME: Y42B and Y444 do not work yet it seems */
74 static GstStaticPadTemplate gst_vp9_enc_sink_template =
75 GST_STATIC_PAD_TEMPLATE ("sink",
78 /*GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }")) */
79 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12 }"))
82 static GstStaticPadTemplate gst_vp9_enc_src_template =
83 GST_STATIC_PAD_TEMPLATE ("src",
86 GST_STATIC_CAPS ("video/x-vp9, " "profile = (string) {0, 1, 2, 3}")
89 #define parent_class gst_vp9_enc_parent_class
90 G_DEFINE_TYPE (GstVP9Enc, gst_vp9_enc, GST_TYPE_VPX_ENC);
92 static vpx_codec_iface_t *gst_vp9_enc_get_algo (GstVPXEnc * enc);
93 static gboolean gst_vp9_enc_enable_scaling (GstVPXEnc * enc);
94 static void gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image);
95 static GstCaps *gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc);
96 static void gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
98 static void *gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
99 GstVideoCodecFrame * frame);
100 static GstFlowReturn gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc,
101 void *user_data, GstBuffer * buffer);
102 static void gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc,
103 GstVideoCodecFrame * frame, vpx_image_t * image);
106 gst_vp9_enc_class_init (GstVP9EncClass * klass)
108 GstElementClass *element_class;
109 GstVPXEncClass *vpx_encoder_class;
111 element_class = GST_ELEMENT_CLASS (klass);
112 vpx_encoder_class = GST_VPX_ENC_CLASS (klass);
114 gst_element_class_add_static_pad_template (element_class,
115 &gst_vp9_enc_src_template);
116 gst_element_class_add_static_pad_template (element_class,
117 &gst_vp9_enc_sink_template);
119 gst_element_class_set_static_metadata (element_class,
121 "Codec/Encoder/Video",
122 "Encode VP9 video streams", "David Schleef <ds@entropywave.com>, "
123 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
125 vpx_encoder_class->get_algo = gst_vp9_enc_get_algo;
126 vpx_encoder_class->enable_scaling = gst_vp9_enc_enable_scaling;
127 vpx_encoder_class->set_image_format = gst_vp9_enc_set_image_format;
128 vpx_encoder_class->get_new_vpx_caps = gst_vp9_enc_get_new_simple_caps;
129 vpx_encoder_class->set_stream_info = gst_vp9_enc_set_stream_info;
130 vpx_encoder_class->process_frame_user_data =
131 gst_vp9_enc_process_frame_user_data;
132 vpx_encoder_class->handle_invisible_frame_buffer =
133 gst_vp9_enc_handle_invisible_frame_buffer;
134 vpx_encoder_class->set_frame_user_data = gst_vp9_enc_set_frame_user_data;
136 GST_DEBUG_CATEGORY_INIT (gst_vp9enc_debug, "vp9enc", 0, "VP9 Encoder");
140 gst_vp9_enc_init (GstVP9Enc * gst_vp9_enc)
142 vpx_codec_err_t status;
143 GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (gst_vp9_enc);
144 GST_DEBUG_OBJECT (gst_vp9_enc, "gst_vp9_enc_init");
146 vpx_codec_enc_config_default (gst_vp9_enc_get_algo (gst_vpx_enc),
147 &gst_vpx_enc->cfg, 0);
148 if (status != VPX_CODEC_OK) {
149 GST_ERROR_OBJECT (gst_vpx_enc,
150 "Failed to get default encoder configuration: %s",
151 gst_vpx_error_name (status));
152 gst_vpx_enc->have_default_config = FALSE;
154 gst_vpx_enc->have_default_config = TRUE;
158 static vpx_codec_iface_t *
159 gst_vp9_enc_get_algo (GstVPXEnc * enc)
161 return &vpx_codec_vp9_cx_algo;
165 gst_vp9_enc_enable_scaling (GstVPXEnc * enc)
171 gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
173 switch (enc->input_state->info.finfo->format) {
174 case GST_VIDEO_FORMAT_I420:
175 image->fmt = VPX_IMG_FMT_I420;
177 image->x_chroma_shift = image->y_chroma_shift = 1;
179 case GST_VIDEO_FORMAT_YV12:
180 image->fmt = VPX_IMG_FMT_YV12;
182 image->x_chroma_shift = image->y_chroma_shift = 1;
184 case GST_VIDEO_FORMAT_Y42B:
185 image->fmt = VPX_IMG_FMT_I422;
187 image->x_chroma_shift = 1;
188 image->y_chroma_shift = 0;
190 case GST_VIDEO_FORMAT_Y444:
191 image->fmt = VPX_IMG_FMT_I444;
193 image->x_chroma_shift = image->y_chroma_shift = 0;
196 g_assert_not_reached ();
202 gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc)
205 gchar *profile_str = g_strdup_printf ("%d", enc->cfg.g_profile);
206 caps = gst_caps_new_simple ("video/x-vp9",
207 "profile", G_TYPE_STRING, profile_str, NULL);
208 g_free (profile_str);
213 gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
220 gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
221 GstVideoCodecFrame * frame)
227 gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
231 g_mutex_unlock (&enc->encoder_lock);
232 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (enc), buffer);
233 g_mutex_lock (&enc->encoder_lock);
238 gst_vp9_enc_user_data_free (vpx_image_t * image)
240 g_slice_free (vpx_image_t, image);
244 gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
247 gst_video_codec_frame_set_user_data (frame, image,
248 (GDestroyNotify) gst_vp9_enc_user_data_free);
252 #endif /* HAVE_VP9_ENCODER */