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
25 * @see_also: vp9dec, webmmux, oggmux
27 * This element encodes raw video into a VP9 stream.
28 * [VP9](http://www.webmproject.org) is a royalty-free video codec maintained by
29 * [Google](http://www.google.com/). It's the successor of On2 VP3, which was
30 * the base of the Theora video codec.
32 * To control the quality of the encoding, the #GstVPXEnc:target-bitrate,
33 * #GstVPXEnc:min-quantizer, #GstVPXEnc:max-quantizer or #GstVPXEnc:cq-level
34 * properties can be used. Which one is used depends on the mode selected by
35 * the #GstVPXEnc:end-usage property.
36 * See [Encoder Parameters](http://www.webmproject.org/docs/encoder-parameters/)
37 * for explanation, examples for useful encoding parameters and more details
38 * on the encoding parameters.
42 * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! vp9enc ! webmmux ! filesink location=videotestsrc.webm
43 * ]| This example pipeline will encode a test video source to VP9 muxed in an
52 #ifdef HAVE_VP9_ENCODER
54 /* glib decided in 2.32 it would be a great idea to deprecated GValueArray without
55 * providing an alternative
57 * See https://bugzilla.gnome.org/show_bug.cgi?id=667228
59 #define GLIB_DISABLE_DEPRECATION_WARNINGS
61 #include <gst/tag/tag.h>
62 #include <gst/video/video.h>
65 #include "gstvpxelements.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
72 #define DEFAULT_TILE_COLUMNS 6
73 #define DEFAULT_TILE_ROWS 0
74 #define DEFAULT_ROW_MT 0
75 #define DEFAULT_AQ_MODE 0
86 /* FIXME: Y42B do not work yet it seems */
87 static GstStaticPadTemplate gst_vp9_enc_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
91 /*GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }")) */
92 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
93 ("{ I420, YV12, Y444, I420_10LE, I422_10LE }"))
96 static GstStaticPadTemplate gst_vp9_enc_src_template =
97 GST_STATIC_PAD_TEMPLATE ("src",
100 GST_STATIC_CAPS ("video/x-vp9, " "profile = (string) {0, 1, 2, 3}")
103 #define parent_class gst_vp9_enc_parent_class
104 G_DEFINE_TYPE (GstVP9Enc, gst_vp9_enc, GST_TYPE_VPX_ENC);
105 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (vp9enc, "vp9enc", GST_RANK_PRIMARY,
106 gst_vp9_enc_get_type (), vpx_element_init (plugin));
108 static vpx_codec_iface_t *gst_vp9_enc_get_algo (GstVPXEnc * enc);
109 static gboolean gst_vp9_enc_enable_scaling (GstVPXEnc * enc);
110 static void gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image);
111 static GstCaps *gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc);
112 static void gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
113 GstVideoInfo * info);
114 static void *gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
115 GstVideoCodecFrame * frame);
116 static GstFlowReturn gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc,
117 void *user_data, GstBuffer * buffer);
118 static void gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc,
119 GstVideoCodecFrame * frame, vpx_image_t * image);
120 static void gst_vp9_enc_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_vp9_enc_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
124 static gboolean gst_vp9_enc_configure_encoder (GstVPXEnc * encoder,
125 GstVideoCodecState * state);
127 #define DEFAULT_BITS_PER_PIXEL 0.0289
130 gst_vp9_enc_class_init (GstVP9EncClass * klass)
132 GObjectClass *gobject_class;
133 GstElementClass *element_class;
134 GstVPXEncClass *vpx_encoder_class;
136 gobject_class = G_OBJECT_CLASS (klass);
137 element_class = GST_ELEMENT_CLASS (klass);
138 vpx_encoder_class = GST_VPX_ENC_CLASS (klass);
140 gobject_class->set_property = gst_vp9_enc_set_property;
141 gobject_class->get_property = gst_vp9_enc_get_property;
144 * GstVP9Enc:tile-columns:
146 * Number of tile columns, log2
150 g_object_class_install_property (gobject_class, PROP_TILE_COLUMNS,
151 g_param_spec_int ("tile-columns", "Tile Columns",
152 "Number of tile columns, log2",
153 0, 6, DEFAULT_TILE_COLUMNS,
154 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
157 * GstVP9Enc:tile-rows:
159 * Number of tile rows, log2
163 g_object_class_install_property (gobject_class, PROP_TILE_ROWS,
164 g_param_spec_int ("tile-rows", "Tile Rows",
165 "Number of tile rows, log2",
166 0, 2, DEFAULT_TILE_ROWS,
167 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
172 * Whether each row should be encoded using multiple threads
176 g_object_class_install_property (gobject_class, PROP_ROW_MT,
177 g_param_spec_boolean ("row-mt", "Row Multithreading",
178 "Whether each row should be encoded using multiple threads",
180 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
185 * Adaptive Quantization Mode
189 g_object_class_install_property (gobject_class, PROP_AQ_MODE,
190 g_param_spec_int ("aq-mode", "Adaptive Quantization Mode",
191 "0: off (default), 1: variance 2: complexity, 3: cyclic refresh, 4: equator360",
192 0, 4, DEFAULT_AQ_MODE,
193 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
195 gst_element_class_add_static_pad_template (element_class,
196 &gst_vp9_enc_src_template);
197 gst_element_class_add_static_pad_template (element_class,
198 &gst_vp9_enc_sink_template);
200 gst_element_class_set_static_metadata (element_class,
202 "Codec/Encoder/Video",
203 "Encode VP9 video streams", "David Schleef <ds@entropywave.com>, "
204 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
206 vpx_encoder_class->get_algo = gst_vp9_enc_get_algo;
207 vpx_encoder_class->enable_scaling = gst_vp9_enc_enable_scaling;
208 vpx_encoder_class->set_image_format = gst_vp9_enc_set_image_format;
209 vpx_encoder_class->get_new_vpx_caps = gst_vp9_enc_get_new_simple_caps;
210 vpx_encoder_class->set_stream_info = gst_vp9_enc_set_stream_info;
211 vpx_encoder_class->process_frame_user_data =
212 gst_vp9_enc_process_frame_user_data;
213 vpx_encoder_class->handle_invisible_frame_buffer =
214 gst_vp9_enc_handle_invisible_frame_buffer;
215 vpx_encoder_class->set_frame_user_data = gst_vp9_enc_set_frame_user_data;
216 vpx_encoder_class->configure_encoder = gst_vp9_enc_configure_encoder;
218 GST_DEBUG_CATEGORY_INIT (gst_vp9enc_debug, "vp9enc", 0, "VP9 Encoder");
222 gst_vp9_enc_init (GstVP9Enc * gst_vp9_enc)
224 vpx_codec_err_t status;
225 GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (gst_vp9_enc);
226 GST_DEBUG_OBJECT (gst_vp9_enc, "gst_vp9_enc_init");
228 vpx_codec_enc_config_default (gst_vp9_enc_get_algo (gst_vpx_enc),
229 &gst_vpx_enc->cfg, 0);
230 if (status != VPX_CODEC_OK) {
231 GST_ERROR_OBJECT (gst_vpx_enc,
232 "Failed to get default encoder configuration: %s",
233 gst_vpx_error_name (status));
234 gst_vpx_enc->have_default_config = FALSE;
236 gst_vpx_enc->have_default_config = TRUE;
238 gst_vpx_enc->bits_per_pixel = DEFAULT_BITS_PER_PIXEL;
240 gst_vp9_enc->tile_columns = DEFAULT_TILE_COLUMNS;
241 gst_vp9_enc->tile_rows = DEFAULT_TILE_ROWS;
242 gst_vp9_enc->row_mt = DEFAULT_ROW_MT;
243 gst_vp9_enc->aq_mode = DEFAULT_AQ_MODE;
247 gst_vp9_enc_set_property (GObject * object, guint prop_id,
248 const GValue * value, GParamSpec * pspec)
250 GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (object);
251 GstVP9Enc *gst_vp9_enc = GST_VP9_ENC (object);
252 vpx_codec_err_t status;
254 g_mutex_lock (&gst_vpx_enc->encoder_lock);
257 case PROP_TILE_COLUMNS:
258 gst_vp9_enc->tile_columns = g_value_get_int (value);
259 if (gst_vpx_enc->inited) {
261 vpx_codec_control (&gst_vpx_enc->encoder, VP9E_SET_TILE_COLUMNS,
262 gst_vp9_enc->tile_columns);
263 if (status != VPX_CODEC_OK) {
264 GST_WARNING_OBJECT (gst_vpx_enc,
265 "Failed to set VP9E_SET_TILE_COLUMNS: %s",
266 gst_vpx_error_name (status));
271 gst_vp9_enc->tile_rows = g_value_get_int (value);
272 if (gst_vpx_enc->inited) {
274 vpx_codec_control (&gst_vpx_enc->encoder, VP9E_SET_TILE_ROWS,
275 gst_vp9_enc->tile_rows);
276 if (status != VPX_CODEC_OK) {
277 GST_WARNING_OBJECT (gst_vpx_enc,
278 "Failed to set VP9E_SET_TILE_ROWS: %s",
279 gst_vpx_error_name (status));
284 gst_vp9_enc->row_mt = g_value_get_boolean (value);
285 if (gst_vpx_enc->inited) {
287 vpx_codec_control (&gst_vpx_enc->encoder, VP9E_SET_ROW_MT,
288 gst_vp9_enc->row_mt ? 1 : 0);
289 if (status != VPX_CODEC_OK) {
290 GST_WARNING_OBJECT (gst_vpx_enc,
291 "Failed to set VP9E_SET_ROW_MT: %s", gst_vpx_error_name (status));
296 gst_vp9_enc->aq_mode = g_value_get_int (value);
297 if (gst_vpx_enc->inited) {
298 status = vpx_codec_control (&gst_vpx_enc->encoder, VP9E_SET_AQ_MODE,
299 gst_vp9_enc->aq_mode);
300 if (status != VPX_CODEC_OK) {
301 GST_WARNING_OBJECT (gst_vpx_enc,
302 "Failed to set VP9E_SET_AQ_MODE: %s",
303 gst_vpx_error_name (status));
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 g_mutex_unlock (&gst_vpx_enc->encoder_lock);
316 gst_vp9_enc_get_property (GObject * object, guint prop_id, GValue * value,
319 GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (object);
320 GstVP9Enc *gst_vp9_enc = GST_VP9_ENC (object);
322 g_mutex_lock (&gst_vpx_enc->encoder_lock);
325 case PROP_TILE_COLUMNS:
326 g_value_set_int (value, gst_vp9_enc->tile_columns);
329 g_value_set_int (value, gst_vp9_enc->tile_rows);
332 g_value_set_boolean (value, gst_vp9_enc->row_mt);
335 g_value_set_int (value, gst_vp9_enc->aq_mode);
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 g_mutex_unlock (&gst_vpx_enc->encoder_lock);
345 static vpx_color_space_t
346 gst_vp9_get_vpx_colorspace (GstVPXEnc * encoder, GstVideoColorimetry * in_cinfo,
347 GstVideoFormat format)
349 vpx_color_space_t colorspace = VPX_CS_UNKNOWN;
350 GstVideoColorimetry cinfo = *in_cinfo;
351 gchar *colorimetry_str;
357 vpx_color_space_t vpx_color_space;
358 } colorimetry_map[] = {
360 GST_VIDEO_COLORIMETRY_BT601, VPX_CS_BT_601}, {
361 GST_VIDEO_COLORIMETRY_BT709, VPX_CS_BT_709}, {
362 GST_VIDEO_COLORIMETRY_SMPTE240M, VPX_CS_SMPTE_240}, {
363 GST_VIDEO_COLORIMETRY_BT2020, VPX_CS_BT_2020}
366 /* We support any range, all mapped CSC are by default reduced range. */
367 cinfo.range = GST_VIDEO_COLOR_RANGE_16_235;
368 colorimetry_str = gst_video_colorimetry_to_string (&cinfo);
370 if (colorimetry_str != NULL) {
371 for (i = 0; i < G_N_ELEMENTS (colorimetry_map); ++i) {
372 if (g_strcmp0 (colorimetry_map[i].str, colorimetry_str) == 0) {
373 colorspace = colorimetry_map[i].vpx_color_space;
379 if (colorspace == VPX_CS_UNKNOWN) {
380 if (format == GST_VIDEO_FORMAT_GBR
381 || format == GST_VIDEO_FORMAT_GBR_10BE
382 || format == GST_VIDEO_FORMAT_GBR_10LE
383 || format == GST_VIDEO_FORMAT_GBR_12BE
384 || format == GST_VIDEO_FORMAT_GBR_12LE) {
385 /* Currently has no effect because vp*enc elements only accept YUV video
388 * FIXME: Support encoding GST_VIDEO_FORMAT_GBR and its high bits variants.
390 colorspace = VPX_CS_SRGB;
392 GST_WARNING_OBJECT (encoder, "Unsupported colorspace \"%s\"",
393 GST_STR_NULL (colorimetry_str));
397 g_free (colorimetry_str);
403 gst_vp9_get_vpx_color_range (GstVideoColorimetry * colorimetry)
405 if (colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255)
406 /* Full range (0..255 or HBD equivalent) */
409 /* Limited range (16..235 or HBD equivalent) */
414 gst_vp9_enc_configure_encoder (GstVPXEnc * encoder, GstVideoCodecState * state)
416 GstVP9Enc *vp9enc = GST_VP9_ENC (encoder);
417 GstVideoInfo *info = &state->info;
418 vpx_codec_err_t status;
420 status = vpx_codec_control (&encoder->encoder, VP9E_SET_COLOR_SPACE,
421 gst_vp9_get_vpx_colorspace (encoder, &GST_VIDEO_INFO_COLORIMETRY (info),
422 GST_VIDEO_INFO_FORMAT (info)));
423 if (status != VPX_CODEC_OK) {
424 GST_WARNING_OBJECT (encoder,
425 "Failed to set VP9E_SET_COLOR_SPACE: %s", gst_vpx_error_name (status));
428 status = vpx_codec_control (&encoder->encoder, VP9E_SET_COLOR_RANGE,
429 gst_vp9_get_vpx_color_range (&GST_VIDEO_INFO_COLORIMETRY (info)));
430 if (status != VPX_CODEC_OK) {
431 GST_WARNING_OBJECT (encoder,
432 "Failed to set VP9E_SET_COLOR_RANGE: %s", gst_vpx_error_name (status));
436 vpx_codec_control (&encoder->encoder, VP9E_SET_TILE_COLUMNS,
437 vp9enc->tile_columns);
438 if (status != VPX_CODEC_OK) {
439 GST_DEBUG_OBJECT (encoder, "Failed to set VP9E_SET_TILE_COLUMNS: %s",
440 gst_vpx_error_name (status));
444 vpx_codec_control (&encoder->encoder, VP9E_SET_TILE_ROWS,
446 if (status != VPX_CODEC_OK) {
447 GST_DEBUG_OBJECT (encoder, "Failed to set VP9E_SET_TILE_ROWS: %s",
448 gst_vpx_error_name (status));
451 vpx_codec_control (&encoder->encoder, VP9E_SET_ROW_MT,
452 vp9enc->row_mt ? 1 : 0);
453 if (status != VPX_CODEC_OK) {
454 GST_DEBUG_OBJECT (encoder,
455 "Failed to set VP9E_SET_ROW_MT: %s", gst_vpx_error_name (status));
458 vpx_codec_control (&encoder->encoder, VP9E_SET_AQ_MODE, vp9enc->aq_mode);
459 if (status != VPX_CODEC_OK) {
460 GST_WARNING_OBJECT (encoder,
461 "Failed to set VP9E_SET_AQ_MODE: %s", gst_vpx_error_name (status));
467 static vpx_codec_iface_t *
468 gst_vp9_enc_get_algo (GstVPXEnc * enc)
470 return &vpx_codec_vp9_cx_algo;
474 gst_vp9_enc_enable_scaling (GstVPXEnc * enc)
480 gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
482 switch (enc->input_state->info.finfo->format) {
483 case GST_VIDEO_FORMAT_I420:
484 image->fmt = VPX_IMG_FMT_I420;
486 image->x_chroma_shift = image->y_chroma_shift = 1;
488 case GST_VIDEO_FORMAT_YV12:
489 image->fmt = VPX_IMG_FMT_YV12;
491 image->x_chroma_shift = image->y_chroma_shift = 1;
493 case GST_VIDEO_FORMAT_Y42B:
494 image->fmt = VPX_IMG_FMT_I422;
496 image->x_chroma_shift = 1;
497 image->y_chroma_shift = 0;
499 case GST_VIDEO_FORMAT_Y444:
500 image->fmt = VPX_IMG_FMT_I444;
502 image->x_chroma_shift = image->y_chroma_shift = 0;
504 case GST_VIDEO_FORMAT_I420_10LE:
505 image->fmt = VPX_IMG_FMT_I42016;
507 image->x_chroma_shift = image->y_chroma_shift = 1;
509 case GST_VIDEO_FORMAT_I422_10LE:
510 image->fmt = VPX_IMG_FMT_I42216;
512 image->x_chroma_shift = 1;
513 image->y_chroma_shift = 0;
516 g_assert_not_reached ();
522 gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc)
525 gchar *profile_str = g_strdup_printf ("%d", enc->cfg.g_profile);
526 caps = gst_caps_new_simple ("video/x-vp9",
527 "profile", G_TYPE_STRING, profile_str, NULL);
528 g_free (profile_str);
533 gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
540 gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
541 GstVideoCodecFrame * frame)
547 gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
551 g_mutex_unlock (&enc->encoder_lock);
552 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (enc), buffer);
553 g_mutex_lock (&enc->encoder_lock);
558 gst_vp9_enc_user_data_free (vpx_image_t * image)
560 g_slice_free (vpx_image_t, image);
564 gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
567 gst_video_codec_frame_set_user_data (frame, image,
568 (GDestroyNotify) gst_vp9_enc_user_data_free);
572 #endif /* HAVE_VP9_ENCODER */