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 -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
72 /* From vp9/vp9_cx_iface.c */
73 #define DEFAULT_PROFILE 0
75 #define DEFAULT_RC_END_USAGE VPX_VBR
76 #define DEFAULT_RC_TARGET_BITRATE 256000
77 #define DEFAULT_RC_MIN_QUANTIZER 4
78 #define DEFAULT_RC_MAX_QUANTIZER 63
80 #define DEFAULT_RC_DROPFRAME_THRESH 0
81 #define DEFAULT_RC_RESIZE_ALLOWED 0
82 #define DEFAULT_RC_RESIZE_UP_THRESH 30
83 #define DEFAULT_RC_RESIZE_DOWN_THRESH 60
84 #define DEFAULT_RC_UNDERSHOOT_PCT 100
85 #define DEFAULT_RC_OVERSHOOT_PCT 100
86 #define DEFAULT_RC_BUF_SZ 6000
87 #define DEFAULT_RC_BUF_INITIAL_SZ 4000
88 #define DEFAULT_RC_BUF_OPTIMAL_SZ 5000
89 #define DEFAULT_RC_2PASS_VBR_BIAS_PCT 50
90 #define DEFAULT_RC_2PASS_VBR_MINSECTION_PCT 0
91 #define DEFAULT_RC_2PASS_VBR_MAXSECTION_PCT 400
93 #define DEFAULT_KF_MODE VPX_KF_AUTO
94 #define DEFAULT_KF_MAX_DIST 128
96 #define DEFAULT_MULTIPASS_MODE VPX_RC_ONE_PASS
97 #define DEFAULT_MULTIPASS_CACHE_FILE "multipass.cache"
99 #define DEFAULT_TS_NUMBER_LAYERS 1
100 #define DEFAULT_TS_TARGET_BITRATE NULL
101 #define DEFAULT_TS_RATE_DECIMATOR NULL
102 #define DEFAULT_TS_PERIODICITY 0
103 #define DEFAULT_TS_LAYER_ID NULL
105 #define DEFAULT_ERROR_RESILIENT 0
106 #define DEFAULT_LAG_IN_FRAMES 0
108 #define DEFAULT_THREADS 0
110 #define DEFAULT_H_SCALING_MODE VP8E_NORMAL
111 #define DEFAULT_V_SCALING_MODE VP8E_NORMAL
112 #define DEFAULT_CPU_USED 0
113 #define DEFAULT_ENABLE_AUTO_ALT_REF FALSE
114 #define DEFAULT_DEADLINE VPX_DL_BEST_QUALITY
115 #define DEFAULT_NOISE_SENSITIVITY 0
116 #define DEFAULT_SHARPNESS 0
117 #define DEFAULT_STATIC_THRESHOLD 0
118 #define DEFAULT_TOKEN_PARTITIONS 0
119 #define DEFAULT_ARNR_MAXFRAMES 0
120 #define DEFAULT_ARNR_STRENGTH 3
121 #define DEFAULT_ARNR_TYPE 3
122 #define DEFAULT_TUNING VP8_TUNE_PSNR
123 #define DEFAULT_CQ_LEVEL 10
124 #define DEFAULT_MAX_INTRA_BITRATE_PCT 0
125 #define DEFAULT_TIMEBASE_N 0
126 #define DEFAULT_TIMEBASE_D 1
132 PROP_RC_TARGET_BITRATE,
133 PROP_RC_MIN_QUANTIZER,
134 PROP_RC_MAX_QUANTIZER,
135 PROP_RC_DROPFRAME_THRESH,
136 PROP_RC_RESIZE_ALLOWED,
137 PROP_RC_RESIZE_UP_THRESH,
138 PROP_RC_RESIZE_DOWN_THRESH,
139 PROP_RC_UNDERSHOOT_PCT,
140 PROP_RC_OVERSHOOT_PCT,
142 PROP_RC_BUF_INITIAL_SZ,
143 PROP_RC_BUF_OPTIMAL_SZ,
144 PROP_RC_2PASS_VBR_BIAS_PCT,
145 PROP_RC_2PASS_VBR_MINSECTION_PCT,
146 PROP_RC_2PASS_VBR_MAXSECTION_PCT,
149 PROP_TS_NUMBER_LAYERS,
150 PROP_TS_TARGET_BITRATE,
151 PROP_TS_RATE_DECIMATOR,
155 PROP_MULTIPASS_CACHE_FILE,
156 PROP_ERROR_RESILIENT,
163 PROP_ENABLE_AUTO_ALT_REF,
164 PROP_NOISE_SENSITIVITY,
166 PROP_STATIC_THRESHOLD,
167 PROP_TOKEN_PARTITIONS,
173 PROP_MAX_INTRA_BITRATE_PCT,
177 #define GST_VP9_ENC_END_USAGE_TYPE (gst_vp9_enc_end_usage_get_type())
179 gst_vp9_enc_end_usage_get_type (void)
181 static const GEnumValue values[] = {
182 {VPX_VBR, "Variable Bit Rate (VBR) mode", "vbr"},
183 {VPX_CBR, "Constant Bit Rate (CBR) mode", "cbr"},
184 {VPX_CQ, "Constant Quality Mode (CQ) mode", "cq"},
187 static volatile GType id = 0;
189 if (g_once_init_enter ((gsize *) & id)) {
192 _id = g_enum_register_static ("GstVP9EncEndUsage", values);
194 g_once_init_leave ((gsize *) & id, _id);
200 #define GST_VP9_ENC_MULTIPASS_MODE_TYPE (gst_vp9_enc_multipass_mode_get_type())
202 gst_vp9_enc_multipass_mode_get_type (void)
204 static const GEnumValue values[] = {
205 {VPX_RC_ONE_PASS, "One pass encoding (default)", "one-pass"},
206 {VPX_RC_FIRST_PASS, "First pass of multipass encoding", "first-pass"},
207 {VPX_RC_LAST_PASS, "Last pass of multipass encoding", "last-pass"},
210 static volatile GType id = 0;
212 if (g_once_init_enter ((gsize *) & id)) {
215 _id = g_enum_register_static ("GstVP9EncMultipassMode", values);
217 g_once_init_leave ((gsize *) & id, _id);
223 #define GST_VP9_ENC_KF_MODE_TYPE (gst_vp9_enc_kf_mode_get_type())
225 gst_vp9_enc_kf_mode_get_type (void)
227 static const GEnumValue values[] = {
228 {VPX_KF_AUTO, "Determine optimal placement automatically", "auto"},
229 {VPX_KF_DISABLED, "Don't automatically place keyframes", "disabled"},
232 static volatile GType id = 0;
234 if (g_once_init_enter ((gsize *) & id)) {
237 _id = g_enum_register_static ("GstVP9EncKfMode", values);
239 g_once_init_leave ((gsize *) & id, _id);
245 #define GST_VP9_ENC_TUNING_TYPE (gst_vp9_enc_tuning_get_type())
247 gst_vp9_enc_tuning_get_type (void)
249 static const GEnumValue values[] = {
250 {VP8_TUNE_PSNR, "Tune for PSNR", "psnr"},
251 {VP8_TUNE_SSIM, "Tune for SSIM", "ssim"},
254 static volatile GType id = 0;
256 if (g_once_init_enter ((gsize *) & id)) {
259 _id = g_enum_register_static ("GstVP9EncTuning", values);
261 g_once_init_leave ((gsize *) & id, _id);
267 #define GST_VP9_ENC_SCALING_MODE_TYPE (gst_vp9_enc_scaling_mode_get_type())
269 gst_vp9_enc_scaling_mode_get_type (void)
271 static const GEnumValue values[] = {
272 {VP8E_NORMAL, "Normal", "normal"},
273 {VP8E_FOURFIVE, "4:5", "4:5"},
274 {VP8E_THREEFIVE, "3:5", "3:5"},
275 {VP8E_ONETWO, "1:2", "1:2"},
278 static volatile GType id = 0;
280 if (g_once_init_enter ((gsize *) & id)) {
283 _id = g_enum_register_static ("GstVP9EncScalingMode", values);
285 g_once_init_leave ((gsize *) & id, _id);
291 #define GST_VP9_ENC_TOKEN_PARTITIONS_TYPE (gst_vp9_enc_token_partitions_get_type())
293 gst_vp9_enc_token_partitions_get_type (void)
295 static const GEnumValue values[] = {
296 {VP8_ONE_TOKENPARTITION, "One token partition", "1"},
297 {VP8_TWO_TOKENPARTITION, "Two token partitions", "2"},
298 {VP8_FOUR_TOKENPARTITION, "Four token partitions", "4"},
299 {VP8_EIGHT_TOKENPARTITION, "Eight token partitions", "8"},
302 static volatile GType id = 0;
304 if (g_once_init_enter ((gsize *) & id)) {
307 _id = g_enum_register_static ("GstVP9EncTokenPartitions", values);
309 g_once_init_leave ((gsize *) & id, _id);
315 #define GST_VP9_ENC_ER_FLAGS_TYPE (gst_vp9_enc_er_flags_get_type())
317 gst_vp9_enc_er_flags_get_type (void)
319 static const GFlagsValue values[] = {
320 {VPX_ERROR_RESILIENT_DEFAULT, "Default error resilience", "default"},
321 {VPX_ERROR_RESILIENT_PARTITIONS,
322 "Allow partitions to be decoded independently", "partitions"},
325 static volatile GType id = 0;
327 if (g_once_init_enter ((gsize *) & id)) {
330 _id = g_flags_register_static ("GstVP9EncErFlags", values);
332 g_once_init_leave ((gsize *) & id, _id);
338 static void gst_vp9_enc_finalize (GObject * object);
339 static void gst_vp9_enc_set_property (GObject * object, guint prop_id,
340 const GValue * value, GParamSpec * pspec);
341 static void gst_vp9_enc_get_property (GObject * object, guint prop_id,
342 GValue * value, GParamSpec * pspec);
344 static gboolean gst_vp9_enc_start (GstVideoEncoder * encoder);
345 static gboolean gst_vp9_enc_stop (GstVideoEncoder * encoder);
346 static gboolean gst_vp9_enc_set_format (GstVideoEncoder *
347 video_encoder, GstVideoCodecState * state);
348 static gboolean gst_vp9_enc_finish (GstVideoEncoder * video_encoder);
349 static GstFlowReturn gst_vp9_enc_handle_frame (GstVideoEncoder *
350 video_encoder, GstVideoCodecFrame * frame);
351 static gboolean gst_vp9_enc_sink_event (GstVideoEncoder *
352 video_encoder, GstEvent * event);
353 static gboolean gst_vp9_enc_propose_allocation (GstVideoEncoder * encoder,
356 /* FIXME: Y42B and Y444 do not work yet it seems */
357 static GstStaticPadTemplate gst_vp9_enc_sink_template =
358 GST_STATIC_PAD_TEMPLATE ("sink",
361 /*GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }")) */
362 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12 }"))
365 static GstStaticPadTemplate gst_vp9_enc_src_template =
366 GST_STATIC_PAD_TEMPLATE ("src",
369 GST_STATIC_CAPS ("video/x-vp9, " "profile = (string) {0, 1, 2, 3}")
372 #define parent_class gst_vp9_enc_parent_class
373 G_DEFINE_TYPE_WITH_CODE (GstVP9Enc, gst_vp9_enc, GST_TYPE_VIDEO_ENCODER,
374 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
375 G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL););
378 gst_vp9_enc_class_init (GstVP9EncClass * klass)
380 GObjectClass *gobject_class;
381 GstElementClass *element_class;
382 GstVideoEncoderClass *video_encoder_class;
384 gobject_class = G_OBJECT_CLASS (klass);
385 element_class = GST_ELEMENT_CLASS (klass);
386 video_encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
388 gobject_class->set_property = gst_vp9_enc_set_property;
389 gobject_class->get_property = gst_vp9_enc_get_property;
390 gobject_class->finalize = gst_vp9_enc_finalize;
392 gst_element_class_add_pad_template (element_class,
393 gst_static_pad_template_get (&gst_vp9_enc_src_template));
394 gst_element_class_add_pad_template (element_class,
395 gst_static_pad_template_get (&gst_vp9_enc_sink_template));
397 gst_element_class_set_static_metadata (element_class,
399 "Codec/Encoder/Video",
400 "Encode VP9 video streams", "David Schleef <ds@entropywave.com>, "
401 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
403 video_encoder_class->start = gst_vp9_enc_start;
404 video_encoder_class->stop = gst_vp9_enc_stop;
405 video_encoder_class->handle_frame = gst_vp9_enc_handle_frame;
406 video_encoder_class->set_format = gst_vp9_enc_set_format;
407 video_encoder_class->finish = gst_vp9_enc_finish;
408 video_encoder_class->sink_event = gst_vp9_enc_sink_event;
409 video_encoder_class->propose_allocation = gst_vp9_enc_propose_allocation;
411 g_object_class_install_property (gobject_class, PROP_RC_END_USAGE,
412 g_param_spec_enum ("end-usage", "Rate control mode",
414 GST_VP9_ENC_END_USAGE_TYPE, DEFAULT_RC_END_USAGE,
415 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
417 g_object_class_install_property (gobject_class, PROP_RC_TARGET_BITRATE,
418 g_param_spec_int ("target-bitrate", "Target bitrate",
419 "Target bitrate (in bits/sec)",
420 0, G_MAXINT, DEFAULT_RC_TARGET_BITRATE,
421 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
423 g_object_class_install_property (gobject_class, PROP_RC_MIN_QUANTIZER,
424 g_param_spec_int ("min-quantizer", "Minimum Quantizer",
425 "Minimum Quantizer (best)",
426 0, 63, DEFAULT_RC_MIN_QUANTIZER,
427 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
429 g_object_class_install_property (gobject_class, PROP_RC_MAX_QUANTIZER,
430 g_param_spec_int ("max-quantizer", "Maximum Quantizer",
431 "Maximum Quantizer (worst)",
432 0, 63, DEFAULT_RC_MAX_QUANTIZER,
433 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
435 g_object_class_install_property (gobject_class, PROP_RC_DROPFRAME_THRESH,
436 g_param_spec_int ("dropframe-threshold", "Drop Frame Threshold",
437 "Temporal resampling threshold (buf %)",
438 0, 100, DEFAULT_RC_DROPFRAME_THRESH,
439 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
441 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_ALLOWED,
442 g_param_spec_boolean ("resize-allowed", "Resize Allowed",
443 "Allow spatial resampling",
444 DEFAULT_RC_RESIZE_ALLOWED,
445 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
447 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_UP_THRESH,
448 g_param_spec_int ("resize-up-threshold", "Resize Up Threshold",
449 "Upscale threshold (buf %)",
450 0, 100, DEFAULT_RC_RESIZE_UP_THRESH,
451 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
453 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_DOWN_THRESH,
454 g_param_spec_int ("resize-down-threshold", "Resize Down Threshold",
455 "Downscale threshold (buf %)",
456 0, 100, DEFAULT_RC_RESIZE_DOWN_THRESH,
457 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
459 g_object_class_install_property (gobject_class, PROP_RC_UNDERSHOOT_PCT,
460 g_param_spec_int ("undershoot", "Undershoot PCT",
461 "Datarate undershoot (min) target (%)",
462 0, 1000, DEFAULT_RC_UNDERSHOOT_PCT,
463 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
465 g_object_class_install_property (gobject_class, PROP_RC_OVERSHOOT_PCT,
466 g_param_spec_int ("overshoot", "Overshoot PCT",
467 "Datarate overshoot (max) target (%)",
468 0, 1000, DEFAULT_RC_OVERSHOOT_PCT,
469 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
471 g_object_class_install_property (gobject_class, PROP_RC_BUF_SZ,
472 g_param_spec_int ("buffer-size", "Buffer size",
473 "Client buffer size (ms)",
474 0, G_MAXINT, DEFAULT_RC_BUF_SZ,
475 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
477 g_object_class_install_property (gobject_class, PROP_RC_BUF_INITIAL_SZ,
478 g_param_spec_int ("buffer-initial-size", "Buffer initial size",
479 "Initial client buffer size (ms)",
480 0, G_MAXINT, DEFAULT_RC_BUF_INITIAL_SZ,
481 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
483 g_object_class_install_property (gobject_class, PROP_RC_BUF_OPTIMAL_SZ,
484 g_param_spec_int ("buffer-optimal-size", "Buffer optimal size",
485 "Optimal client buffer size (ms)",
486 0, G_MAXINT, DEFAULT_RC_BUF_OPTIMAL_SZ,
487 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
489 g_object_class_install_property (gobject_class, PROP_RC_2PASS_VBR_BIAS_PCT,
490 g_param_spec_int ("twopass-vbr-bias", "2-pass VBR bias",
491 "CBR/VBR bias (0=CBR, 100=VBR)",
492 0, 100, DEFAULT_RC_2PASS_VBR_BIAS_PCT,
493 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
495 g_object_class_install_property (gobject_class,
496 PROP_RC_2PASS_VBR_MINSECTION_PCT,
497 g_param_spec_int ("twopass-vbr-minsection", "2-pass GOP min bitrate",
498 "GOP minimum bitrate (% target)", 0, G_MAXINT,
499 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT,
500 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
502 g_object_class_install_property (gobject_class,
503 PROP_RC_2PASS_VBR_MAXSECTION_PCT,
504 g_param_spec_int ("twopass-vbr-maxsection", "2-pass GOP max bitrate",
505 "GOP maximum bitrate (% target)", 0, G_MAXINT,
506 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT,
507 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
509 g_object_class_install_property (gobject_class, PROP_KF_MODE,
510 g_param_spec_enum ("keyframe-mode", "Keyframe Mode",
511 "Keyframe placement",
512 GST_VP9_ENC_KF_MODE_TYPE, DEFAULT_KF_MODE,
513 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
515 g_object_class_install_property (gobject_class, PROP_KF_MAX_DIST,
516 g_param_spec_int ("keyframe-max-dist", "Keyframe max distance",
517 "Maximum distance between keyframes (number of frames)",
518 0, G_MAXINT, DEFAULT_KF_MAX_DIST,
519 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
521 g_object_class_install_property (gobject_class, PROP_MULTIPASS_MODE,
522 g_param_spec_enum ("multipass-mode", "Multipass Mode",
523 "Multipass encode mode",
524 GST_VP9_ENC_MULTIPASS_MODE_TYPE, DEFAULT_MULTIPASS_MODE,
525 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
527 g_object_class_install_property (gobject_class, PROP_MULTIPASS_CACHE_FILE,
528 g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
529 "Multipass cache file",
530 DEFAULT_MULTIPASS_CACHE_FILE,
531 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
533 g_object_class_install_property (gobject_class, PROP_TS_NUMBER_LAYERS,
534 g_param_spec_int ("temporal-scalability-number-layers",
535 "Number of coding layers", "Number of coding layers to use", 1, 5,
536 DEFAULT_TS_NUMBER_LAYERS,
537 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
539 g_object_class_install_property (gobject_class, PROP_TS_TARGET_BITRATE,
540 g_param_spec_value_array ("temporal-scalability-target-bitrate",
541 "Coding layer target bitrates",
542 "Target bitrates for coding layers (one per layer, decreasing)",
543 g_param_spec_int ("target-bitrate", "Target bitrate",
544 "Target bitrate", 0, G_MAXINT, DEFAULT_RC_TARGET_BITRATE,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
546 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
548 g_object_class_install_property (gobject_class, PROP_TS_RATE_DECIMATOR,
549 g_param_spec_value_array ("temporal-scalability-rate-decimator",
550 "Coding layer rate decimator",
551 "Rate decimation factors for each layer",
552 g_param_spec_int ("rate-decimator", "Rate decimator",
553 "Rate decimator", 0, 1000000000, 0,
554 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
555 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_TS_PERIODICITY,
558 g_param_spec_int ("temporal-scalability-periodicity",
559 "Coding layer periodicity",
560 "Length of sequence that defines layer membership periodicity", 0, 16,
561 DEFAULT_TS_PERIODICITY,
562 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
564 g_object_class_install_property (gobject_class, PROP_TS_LAYER_ID,
565 g_param_spec_value_array ("temporal-scalability-layer-id",
566 "Coding layer identification",
567 "Sequence defining coding layer membership",
568 g_param_spec_int ("layer-id", "Layer ID", "Layer ID", 0, 4, 0,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 g_object_class_install_property (gobject_class, PROP_LAG_IN_FRAMES,
573 g_param_spec_int ("lag-in-frames", "Lag in frames",
574 "Maximum number of frames to lag",
575 0, 25, DEFAULT_LAG_IN_FRAMES,
576 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
578 g_object_class_install_property (gobject_class, PROP_ERROR_RESILIENT,
579 g_param_spec_flags ("error-resilient", "Error resilient",
580 "Error resilience flags",
581 GST_VP9_ENC_ER_FLAGS_TYPE, DEFAULT_ERROR_RESILIENT,
582 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
584 g_object_class_install_property (gobject_class, PROP_THREADS,
585 g_param_spec_int ("threads", "Threads",
586 "Number of threads to use",
587 0, 64, DEFAULT_THREADS,
588 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
590 g_object_class_install_property (gobject_class, PROP_DEADLINE,
591 g_param_spec_int64 ("deadline", "Deadline",
592 "Deadline per frame (usec, 0=disabled)",
593 0, G_MAXINT64, DEFAULT_DEADLINE,
594 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
596 g_object_class_install_property (gobject_class, PROP_H_SCALING_MODE,
597 g_param_spec_enum ("horizontal-scaling-mode", "Horizontal scaling mode",
598 "Horizontal scaling mode",
599 GST_VP9_ENC_SCALING_MODE_TYPE, DEFAULT_H_SCALING_MODE,
600 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
602 g_object_class_install_property (gobject_class, PROP_V_SCALING_MODE,
603 g_param_spec_enum ("vertical-scaling-mode", "Vertical scaling mode",
604 "Vertical scaling mode",
605 GST_VP9_ENC_SCALING_MODE_TYPE, DEFAULT_V_SCALING_MODE,
606 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
608 g_object_class_install_property (gobject_class, PROP_CPU_USED,
609 g_param_spec_int ("cpu-used", "CPU used",
611 -16, 16, DEFAULT_CPU_USED,
612 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
614 g_object_class_install_property (gobject_class, PROP_ENABLE_AUTO_ALT_REF,
615 g_param_spec_boolean ("auto-alt-ref", "Auto alt reference frames",
616 "Automatically generate AltRef frames",
617 DEFAULT_ENABLE_AUTO_ALT_REF,
618 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
620 g_object_class_install_property (gobject_class, PROP_NOISE_SENSITIVITY,
621 g_param_spec_int ("noise-sensitivity", "Noise sensitivity",
622 "Noise sensisivity (frames to blur)",
623 0, 6, DEFAULT_NOISE_SENSITIVITY,
624 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
626 g_object_class_install_property (gobject_class, PROP_SHARPNESS,
627 g_param_spec_int ("sharpness", "Sharpness",
629 0, 7, DEFAULT_SHARPNESS,
630 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
632 g_object_class_install_property (gobject_class, PROP_STATIC_THRESHOLD,
633 g_param_spec_int ("static-threshold", "Static Threshold",
634 "Motion detection threshold",
635 0, G_MAXINT, DEFAULT_STATIC_THRESHOLD,
636 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
638 g_object_class_install_property (gobject_class, PROP_TOKEN_PARTITIONS,
639 g_param_spec_enum ("token-partitions", "Token partitions",
640 "Number of token partitions",
641 GST_VP9_ENC_TOKEN_PARTITIONS_TYPE, DEFAULT_TOKEN_PARTITIONS,
642 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
644 g_object_class_install_property (gobject_class, PROP_ARNR_MAXFRAMES,
645 g_param_spec_int ("arnr-maxframes", "AltRef max frames",
646 "AltRef maximum number of frames",
647 0, 15, DEFAULT_ARNR_MAXFRAMES,
648 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
650 g_object_class_install_property (gobject_class, PROP_ARNR_STRENGTH,
651 g_param_spec_int ("arnr-strength", "AltRef strength",
653 0, 6, DEFAULT_ARNR_STRENGTH,
654 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
656 g_object_class_install_property (gobject_class, PROP_ARNR_TYPE,
657 g_param_spec_int ("arnr-type", "AltRef type",
659 1, 3, DEFAULT_ARNR_TYPE,
660 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
662 g_object_class_install_property (gobject_class, PROP_TUNING,
663 g_param_spec_enum ("tuning", "Tuning",
665 GST_VP9_ENC_TUNING_TYPE, DEFAULT_TUNING,
666 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
668 g_object_class_install_property (gobject_class, PROP_CQ_LEVEL,
669 g_param_spec_int ("cq-level", "Constrained quality level",
670 "Constrained quality level",
671 0, 63, DEFAULT_CQ_LEVEL,
672 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
674 g_object_class_install_property (gobject_class, PROP_MAX_INTRA_BITRATE_PCT,
675 g_param_spec_int ("max-intra-bitrate", "Max Intra bitrate",
676 "Maximum Intra frame bitrate",
677 0, G_MAXINT, DEFAULT_MAX_INTRA_BITRATE_PCT,
678 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
680 g_object_class_install_property (gobject_class, PROP_TIMEBASE,
681 gst_param_spec_fraction ("timebase", "Shortest interframe time",
682 "Fraction of one second that is the shortest interframe time - normally left as zero which will default to the framerate",
683 0, 1, G_MAXINT, 1, DEFAULT_TIMEBASE_N, DEFAULT_TIMEBASE_D,
684 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686 GST_DEBUG_CATEGORY_INIT (gst_vp9enc_debug, "vp9enc", 0, "VP9 Encoder");
690 gst_vp9_enc_init (GstVP9Enc * gst_vp9_enc)
692 vpx_codec_err_t status;
694 GST_DEBUG_OBJECT (gst_vp9_enc, "init");
697 vpx_codec_enc_config_default (&vpx_codec_vp9_cx_algo, &gst_vp9_enc->cfg,
699 if (status != VPX_CODEC_OK) {
700 GST_ERROR_OBJECT (gst_vp9_enc,
701 "Failed to get default encoder configuration: %s",
702 gst_vpx_error_name (status));
703 gst_vp9_enc->have_default_config = FALSE;
705 gst_vp9_enc->have_default_config = TRUE;
708 gst_vp9_enc->cfg.rc_end_usage = DEFAULT_RC_END_USAGE;
709 gst_vp9_enc->cfg.rc_target_bitrate = DEFAULT_RC_TARGET_BITRATE / 1000;
710 gst_vp9_enc->rc_target_bitrate_set = FALSE;
711 gst_vp9_enc->cfg.rc_min_quantizer = DEFAULT_RC_MIN_QUANTIZER;
712 gst_vp9_enc->cfg.rc_max_quantizer = DEFAULT_RC_MAX_QUANTIZER;
713 gst_vp9_enc->cfg.rc_dropframe_thresh = DEFAULT_RC_DROPFRAME_THRESH;
714 gst_vp9_enc->cfg.rc_resize_allowed = DEFAULT_RC_RESIZE_ALLOWED;
715 gst_vp9_enc->cfg.rc_resize_up_thresh = DEFAULT_RC_RESIZE_UP_THRESH;
716 gst_vp9_enc->cfg.rc_resize_down_thresh = DEFAULT_RC_RESIZE_DOWN_THRESH;
717 gst_vp9_enc->cfg.rc_undershoot_pct = DEFAULT_RC_UNDERSHOOT_PCT;
718 gst_vp9_enc->cfg.rc_overshoot_pct = DEFAULT_RC_OVERSHOOT_PCT;
719 gst_vp9_enc->cfg.rc_buf_sz = DEFAULT_RC_BUF_SZ;
720 gst_vp9_enc->cfg.rc_buf_initial_sz = DEFAULT_RC_BUF_INITIAL_SZ;
721 gst_vp9_enc->cfg.rc_buf_optimal_sz = DEFAULT_RC_BUF_OPTIMAL_SZ;
722 gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct = DEFAULT_RC_2PASS_VBR_BIAS_PCT;
723 gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct =
724 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT;
725 gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct =
726 DEFAULT_RC_2PASS_VBR_MAXSECTION_PCT;
727 gst_vp9_enc->cfg.kf_mode = DEFAULT_KF_MODE;
728 gst_vp9_enc->cfg.kf_max_dist = DEFAULT_KF_MAX_DIST;
729 gst_vp9_enc->cfg.g_pass = DEFAULT_MULTIPASS_MODE;
730 gst_vp9_enc->multipass_cache_file = g_strdup (DEFAULT_MULTIPASS_CACHE_FILE);
731 gst_vp9_enc->cfg.ts_number_layers = DEFAULT_TS_NUMBER_LAYERS;
732 gst_vp9_enc->n_ts_target_bitrate = 0;
733 gst_vp9_enc->n_ts_rate_decimator = 0;
734 gst_vp9_enc->cfg.ts_periodicity = DEFAULT_TS_PERIODICITY;
735 gst_vp9_enc->n_ts_layer_id = 0;
736 gst_vp9_enc->cfg.g_error_resilient = DEFAULT_ERROR_RESILIENT;
737 gst_vp9_enc->cfg.g_lag_in_frames = DEFAULT_LAG_IN_FRAMES;
738 gst_vp9_enc->cfg.g_threads = DEFAULT_THREADS;
739 gst_vp9_enc->deadline = DEFAULT_DEADLINE;
740 gst_vp9_enc->h_scaling_mode = DEFAULT_H_SCALING_MODE;
741 gst_vp9_enc->v_scaling_mode = DEFAULT_V_SCALING_MODE;
742 gst_vp9_enc->cpu_used = DEFAULT_CPU_USED;
743 gst_vp9_enc->enable_auto_alt_ref = DEFAULT_ENABLE_AUTO_ALT_REF;
744 gst_vp9_enc->noise_sensitivity = DEFAULT_NOISE_SENSITIVITY;
745 gst_vp9_enc->sharpness = DEFAULT_SHARPNESS;
746 gst_vp9_enc->static_threshold = DEFAULT_STATIC_THRESHOLD;
747 gst_vp9_enc->token_partitions = DEFAULT_TOKEN_PARTITIONS;
748 gst_vp9_enc->arnr_maxframes = DEFAULT_ARNR_MAXFRAMES;
749 gst_vp9_enc->arnr_strength = DEFAULT_ARNR_STRENGTH;
750 gst_vp9_enc->arnr_type = DEFAULT_ARNR_TYPE;
751 gst_vp9_enc->tuning = DEFAULT_TUNING;
752 gst_vp9_enc->cq_level = DEFAULT_CQ_LEVEL;
753 gst_vp9_enc->max_intra_bitrate_pct = DEFAULT_MAX_INTRA_BITRATE_PCT;
754 gst_vp9_enc->timebase_n = DEFAULT_TIMEBASE_N;
755 gst_vp9_enc->timebase_d = DEFAULT_TIMEBASE_D;
757 gst_vp9_enc->cfg.g_profile = DEFAULT_PROFILE;
759 g_mutex_init (&gst_vp9_enc->encoder_lock);
763 gst_vp9_enc_finalize (GObject * object)
765 GstVP9Enc *gst_vp9_enc;
767 GST_DEBUG_OBJECT (object, "finalize");
769 g_return_if_fail (GST_IS_VP9_ENC (object));
770 gst_vp9_enc = GST_VP9_ENC (object);
772 g_free (gst_vp9_enc->multipass_cache_file);
773 gst_vp9_enc->multipass_cache_file = NULL;
775 if (gst_vp9_enc->input_state)
776 gst_video_codec_state_unref (gst_vp9_enc->input_state);
778 g_mutex_clear (&gst_vp9_enc->encoder_lock);
780 G_OBJECT_CLASS (parent_class)->finalize (object);
785 gst_vp9_enc_set_property (GObject * object, guint prop_id,
786 const GValue * value, GParamSpec * pspec)
788 GstVP9Enc *gst_vp9_enc;
789 gboolean global = FALSE;
790 vpx_codec_err_t status;
792 g_return_if_fail (GST_IS_VP9_ENC (object));
793 gst_vp9_enc = GST_VP9_ENC (object);
795 GST_DEBUG_OBJECT (object, "gst_vp9_enc_set_property");
796 g_mutex_lock (&gst_vp9_enc->encoder_lock);
798 case PROP_RC_END_USAGE:
799 gst_vp9_enc->cfg.rc_end_usage = g_value_get_enum (value);
802 case PROP_RC_TARGET_BITRATE:
803 gst_vp9_enc->cfg.rc_target_bitrate = g_value_get_int (value) / 1000;
804 gst_vp9_enc->rc_target_bitrate_set = TRUE;
807 case PROP_RC_MIN_QUANTIZER:
808 gst_vp9_enc->cfg.rc_min_quantizer = g_value_get_int (value);
811 case PROP_RC_MAX_QUANTIZER:
812 gst_vp9_enc->cfg.rc_max_quantizer = g_value_get_int (value);
815 case PROP_RC_DROPFRAME_THRESH:
816 gst_vp9_enc->cfg.rc_dropframe_thresh = g_value_get_int (value);
819 case PROP_RC_RESIZE_ALLOWED:
820 gst_vp9_enc->cfg.rc_resize_allowed = g_value_get_boolean (value);
823 case PROP_RC_RESIZE_UP_THRESH:
824 gst_vp9_enc->cfg.rc_resize_up_thresh = g_value_get_int (value);
827 case PROP_RC_RESIZE_DOWN_THRESH:
828 gst_vp9_enc->cfg.rc_resize_down_thresh = g_value_get_int (value);
831 case PROP_RC_UNDERSHOOT_PCT:
832 gst_vp9_enc->cfg.rc_undershoot_pct = g_value_get_int (value);
835 case PROP_RC_OVERSHOOT_PCT:
836 gst_vp9_enc->cfg.rc_overshoot_pct = g_value_get_int (value);
840 gst_vp9_enc->cfg.rc_buf_sz = g_value_get_int (value);
843 case PROP_RC_BUF_INITIAL_SZ:
844 gst_vp9_enc->cfg.rc_buf_initial_sz = g_value_get_int (value);
847 case PROP_RC_BUF_OPTIMAL_SZ:
848 gst_vp9_enc->cfg.rc_buf_optimal_sz = g_value_get_int (value);
851 case PROP_RC_2PASS_VBR_BIAS_PCT:
852 gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct = g_value_get_int (value);
855 case PROP_RC_2PASS_VBR_MINSECTION_PCT:
856 gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct = g_value_get_int (value);
859 case PROP_RC_2PASS_VBR_MAXSECTION_PCT:
860 gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct = g_value_get_int (value);
864 gst_vp9_enc->cfg.kf_mode = g_value_get_enum (value);
867 case PROP_KF_MAX_DIST:
868 gst_vp9_enc->cfg.kf_max_dist = g_value_get_int (value);
871 case PROP_MULTIPASS_MODE:
872 gst_vp9_enc->cfg.g_pass = g_value_get_enum (value);
875 case PROP_MULTIPASS_CACHE_FILE:
876 if (gst_vp9_enc->multipass_cache_file)
877 g_free (gst_vp9_enc->multipass_cache_file);
878 gst_vp9_enc->multipass_cache_file = g_value_dup_string (value);
880 case PROP_TS_NUMBER_LAYERS:
881 gst_vp9_enc->cfg.ts_number_layers = g_value_get_int (value);
884 case PROP_TS_TARGET_BITRATE:{
885 GValueArray *va = g_value_get_boxed (value);
887 memset (&gst_vp9_enc->cfg.ts_target_bitrate, 0,
888 sizeof (gst_vp9_enc->cfg.ts_target_bitrate));
890 gst_vp9_enc->n_ts_target_bitrate = 0;
892 if (va->n_values > VPX_TS_MAX_LAYERS) {
893 g_warning ("%s: Only %d layers allowed at maximum",
894 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
898 for (i = 0; i < va->n_values; i++)
899 gst_vp9_enc->cfg.ts_target_bitrate[i] =
900 g_value_get_int (g_value_array_get_nth (va, i));
901 gst_vp9_enc->n_ts_target_bitrate = va->n_values;
907 case PROP_TS_RATE_DECIMATOR:{
908 GValueArray *va = g_value_get_boxed (value);
910 memset (&gst_vp9_enc->cfg.ts_rate_decimator, 0,
911 sizeof (gst_vp9_enc->cfg.ts_rate_decimator));
913 gst_vp9_enc->n_ts_rate_decimator = 0;
914 } else if (va->n_values > VPX_TS_MAX_LAYERS) {
915 g_warning ("%s: Only %d layers allowed at maximum",
916 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
920 for (i = 0; i < va->n_values; i++)
921 gst_vp9_enc->cfg.ts_rate_decimator[i] =
922 g_value_get_int (g_value_array_get_nth (va, i));
923 gst_vp9_enc->n_ts_rate_decimator = va->n_values;
928 case PROP_TS_PERIODICITY:
929 gst_vp9_enc->cfg.ts_periodicity = g_value_get_int (value);
932 case PROP_TS_LAYER_ID:{
933 GValueArray *va = g_value_get_boxed (value);
935 memset (&gst_vp9_enc->cfg.ts_layer_id, 0,
936 sizeof (gst_vp9_enc->cfg.ts_layer_id));
937 if (va && va->n_values > VPX_TS_MAX_PERIODICITY) {
938 g_warning ("%s: Only %d sized layer sequences allowed at maximum",
939 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_PERIODICITY);
943 for (i = 0; i < va->n_values; i++)
944 gst_vp9_enc->cfg.ts_layer_id[i] =
945 g_value_get_int (g_value_array_get_nth (va, i));
946 gst_vp9_enc->n_ts_layer_id = va->n_values;
948 gst_vp9_enc->n_ts_layer_id = 0;
953 case PROP_ERROR_RESILIENT:
954 gst_vp9_enc->cfg.g_error_resilient = g_value_get_flags (value);
957 case PROP_LAG_IN_FRAMES:
958 gst_vp9_enc->cfg.g_lag_in_frames = g_value_get_int (value);
962 gst_vp9_enc->cfg.g_threads = g_value_get_int (value);
966 gst_vp9_enc->deadline = g_value_get_int64 (value);
968 case PROP_H_SCALING_MODE:
969 gst_vp9_enc->h_scaling_mode = g_value_get_enum (value);
970 if (gst_vp9_enc->inited) {
971 vpx_scaling_mode_t sm;
973 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
974 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
977 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
978 if (status != VPX_CODEC_OK) {
979 GST_WARNING_OBJECT (gst_vp9_enc,
980 "Failed to set VP8E_SET_SCALEMODE: %s",
981 gst_vpx_error_name (status));
985 case PROP_V_SCALING_MODE:
986 gst_vp9_enc->v_scaling_mode = g_value_get_enum (value);
987 if (gst_vp9_enc->inited) {
988 vpx_scaling_mode_t sm;
990 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
991 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
994 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
995 if (status != VPX_CODEC_OK) {
996 GST_WARNING_OBJECT (gst_vp9_enc,
997 "Failed to set VP8E_SET_SCALEMODE: %s",
998 gst_vpx_error_name (status));
1003 gst_vp9_enc->cpu_used = g_value_get_int (value);
1004 if (gst_vp9_enc->inited) {
1006 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CPUUSED,
1007 gst_vp9_enc->cpu_used);
1008 if (status != VPX_CODEC_OK) {
1009 GST_WARNING_OBJECT (gst_vp9_enc, "Failed to set VP8E_SET_CPUUSED: %s",
1010 gst_vpx_error_name (status));
1014 case PROP_ENABLE_AUTO_ALT_REF:
1015 gst_vp9_enc->enable_auto_alt_ref = g_value_get_boolean (value);
1016 if (gst_vp9_enc->inited) {
1018 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ENABLEAUTOALTREF,
1019 (gst_vp9_enc->enable_auto_alt_ref ? 1 : 0));
1020 if (status != VPX_CODEC_OK) {
1021 GST_WARNING_OBJECT (gst_vp9_enc,
1022 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1023 gst_vpx_error_name (status));
1027 case PROP_NOISE_SENSITIVITY:
1028 gst_vp9_enc->noise_sensitivity = g_value_get_int (value);
1029 if (gst_vp9_enc->inited) {
1031 vpx_codec_control (&gst_vp9_enc->encoder,
1032 VP8E_SET_NOISE_SENSITIVITY, gst_vp9_enc->noise_sensitivity);
1033 if (status != VPX_CODEC_OK) {
1034 GST_WARNING_OBJECT (gst_vp9_enc,
1035 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1036 gst_vpx_error_name (status));
1040 case PROP_SHARPNESS:
1041 gst_vp9_enc->sharpness = g_value_get_int (value);
1042 if (gst_vp9_enc->inited) {
1043 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SHARPNESS,
1044 gst_vp9_enc->sharpness);
1045 if (status != VPX_CODEC_OK) {
1046 GST_WARNING_OBJECT (gst_vp9_enc,
1047 "Failed to set VP8E_SET_SHARPNESS: %s",
1048 gst_vpx_error_name (status));
1052 case PROP_STATIC_THRESHOLD:
1053 gst_vp9_enc->static_threshold = g_value_get_int (value);
1054 if (gst_vp9_enc->inited) {
1056 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_STATIC_THRESHOLD,
1057 gst_vp9_enc->static_threshold);
1058 if (status != VPX_CODEC_OK) {
1059 GST_WARNING_OBJECT (gst_vp9_enc,
1060 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1061 gst_vpx_error_name (status));
1065 case PROP_TOKEN_PARTITIONS:
1066 gst_vp9_enc->token_partitions = g_value_get_enum (value);
1067 if (gst_vp9_enc->inited) {
1069 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TOKEN_PARTITIONS,
1070 gst_vp9_enc->token_partitions);
1071 if (status != VPX_CODEC_OK) {
1072 GST_WARNING_OBJECT (gst_vp9_enc,
1073 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1074 gst_vpx_error_name (status));
1078 case PROP_ARNR_MAXFRAMES:
1079 gst_vp9_enc->arnr_maxframes = g_value_get_int (value);
1080 if (gst_vp9_enc->inited) {
1082 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_MAXFRAMES,
1083 gst_vp9_enc->arnr_maxframes);
1084 if (status != VPX_CODEC_OK) {
1085 GST_WARNING_OBJECT (gst_vp9_enc,
1086 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1087 gst_vpx_error_name (status));
1091 case PROP_ARNR_STRENGTH:
1092 gst_vp9_enc->arnr_strength = g_value_get_int (value);
1093 if (gst_vp9_enc->inited) {
1095 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_STRENGTH,
1096 gst_vp9_enc->arnr_strength);
1097 if (status != VPX_CODEC_OK) {
1098 GST_WARNING_OBJECT (gst_vp9_enc,
1099 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1100 gst_vpx_error_name (status));
1104 case PROP_ARNR_TYPE:
1105 gst_vp9_enc->arnr_type = g_value_get_int (value);
1106 if (gst_vp9_enc->inited) {
1107 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_TYPE,
1108 gst_vp9_enc->arnr_type);
1109 if (status != VPX_CODEC_OK) {
1110 GST_WARNING_OBJECT (gst_vp9_enc,
1111 "Failed to set VP8E_SET_ARNR_TYPE: %s",
1112 gst_vpx_error_name (status));
1117 gst_vp9_enc->tuning = g_value_get_enum (value);
1118 if (gst_vp9_enc->inited) {
1119 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TUNING,
1120 gst_vp9_enc->tuning);
1121 if (status != VPX_CODEC_OK) {
1122 GST_WARNING_OBJECT (gst_vp9_enc,
1123 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1128 gst_vp9_enc->cq_level = g_value_get_int (value);
1129 if (gst_vp9_enc->inited) {
1130 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CQ_LEVEL,
1131 gst_vp9_enc->cq_level);
1132 if (status != VPX_CODEC_OK) {
1133 GST_WARNING_OBJECT (gst_vp9_enc,
1134 "Failed to set VP8E_SET_CQ_LEVEL: %s",
1135 gst_vpx_error_name (status));
1139 case PROP_MAX_INTRA_BITRATE_PCT:
1140 gst_vp9_enc->max_intra_bitrate_pct = g_value_get_int (value);
1141 if (gst_vp9_enc->inited) {
1143 vpx_codec_control (&gst_vp9_enc->encoder,
1144 VP8E_SET_MAX_INTRA_BITRATE_PCT, gst_vp9_enc->max_intra_bitrate_pct);
1145 if (status != VPX_CODEC_OK) {
1146 GST_WARNING_OBJECT (gst_vp9_enc,
1147 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1148 gst_vpx_error_name (status));
1153 gst_vp9_enc->timebase_n = gst_value_get_fraction_numerator (value);
1154 gst_vp9_enc->timebase_d = gst_value_get_fraction_denominator (value);
1160 if (global &&gst_vp9_enc->inited) {
1162 vpx_codec_enc_config_set (&gst_vp9_enc->encoder, &gst_vp9_enc->cfg);
1163 if (status != VPX_CODEC_OK) {
1164 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1165 GST_ELEMENT_ERROR (gst_vp9_enc, LIBRARY, INIT,
1166 ("Failed to set encoder configuration"), ("%s",
1167 gst_vpx_error_name (status)));
1169 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1172 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1177 gst_vp9_enc_get_property (GObject * object, guint prop_id, GValue * value,
1180 GstVP9Enc *gst_vp9_enc;
1182 g_return_if_fail (GST_IS_VP9_ENC (object));
1183 gst_vp9_enc = GST_VP9_ENC (object);
1185 g_mutex_lock (&gst_vp9_enc->encoder_lock);
1187 case PROP_RC_END_USAGE:
1188 g_value_set_enum (value, gst_vp9_enc->cfg.rc_end_usage);
1190 case PROP_RC_TARGET_BITRATE:
1191 g_value_set_int (value, gst_vp9_enc->cfg.rc_target_bitrate * 1000);
1193 case PROP_RC_MIN_QUANTIZER:
1194 g_value_set_int (value, gst_vp9_enc->cfg.rc_min_quantizer);
1196 case PROP_RC_MAX_QUANTIZER:
1197 g_value_set_int (value, gst_vp9_enc->cfg.rc_max_quantizer);
1199 case PROP_RC_DROPFRAME_THRESH:
1200 g_value_set_int (value, gst_vp9_enc->cfg.rc_dropframe_thresh);
1202 case PROP_RC_RESIZE_ALLOWED:
1203 g_value_set_boolean (value, gst_vp9_enc->cfg.rc_resize_allowed);
1205 case PROP_RC_RESIZE_UP_THRESH:
1206 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_up_thresh);
1208 case PROP_RC_RESIZE_DOWN_THRESH:
1209 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_down_thresh);
1211 case PROP_RC_UNDERSHOOT_PCT:
1212 g_value_set_int (value, gst_vp9_enc->cfg.rc_undershoot_pct);
1214 case PROP_RC_OVERSHOOT_PCT:
1215 g_value_set_int (value, gst_vp9_enc->cfg.rc_overshoot_pct);
1217 case PROP_RC_BUF_SZ:
1218 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_sz);
1220 case PROP_RC_BUF_INITIAL_SZ:
1221 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_initial_sz);
1223 case PROP_RC_BUF_OPTIMAL_SZ:
1224 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_optimal_sz);
1226 case PROP_RC_2PASS_VBR_BIAS_PCT:
1227 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct);
1229 case PROP_RC_2PASS_VBR_MINSECTION_PCT:
1230 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct);
1232 case PROP_RC_2PASS_VBR_MAXSECTION_PCT:
1233 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct);
1236 g_value_set_enum (value, gst_vp9_enc->cfg.kf_mode);
1238 case PROP_KF_MAX_DIST:
1239 g_value_set_int (value, gst_vp9_enc->cfg.kf_max_dist);
1241 case PROP_MULTIPASS_MODE:
1242 g_value_set_enum (value, gst_vp9_enc->cfg.g_pass);
1244 case PROP_MULTIPASS_CACHE_FILE:
1245 g_value_set_string (value, gst_vp9_enc->multipass_cache_file);
1247 case PROP_TS_NUMBER_LAYERS:
1248 g_value_set_int (value, gst_vp9_enc->cfg.ts_number_layers);
1250 case PROP_TS_TARGET_BITRATE:{
1253 if (gst_vp9_enc->n_ts_target_bitrate == 0) {
1254 g_value_set_boxed (value, NULL);
1258 va = g_value_array_new (gst_vp9_enc->n_ts_target_bitrate);
1259 for (i = 0; i < gst_vp9_enc->n_ts_target_bitrate; i++) {
1262 g_value_init (&v, G_TYPE_INT);
1263 g_value_set_int (&v, gst_vp9_enc->cfg.ts_target_bitrate[i]);
1264 g_value_array_append (va, &v);
1267 g_value_set_boxed (value, va);
1268 g_value_array_free (va);
1272 case PROP_TS_RATE_DECIMATOR:{
1275 if (gst_vp9_enc->n_ts_rate_decimator == 0) {
1276 g_value_set_boxed (value, NULL);
1280 va = g_value_array_new (gst_vp9_enc->n_ts_rate_decimator);
1281 for (i = 0; i < gst_vp9_enc->n_ts_rate_decimator; i++) {
1284 g_value_init (&v, G_TYPE_INT);
1285 g_value_set_int (&v, gst_vp9_enc->cfg.ts_rate_decimator[i]);
1286 g_value_array_append (va, &v);
1289 g_value_set_boxed (value, va);
1290 g_value_array_free (va);
1294 case PROP_TS_PERIODICITY:
1295 g_value_set_int (value, gst_vp9_enc->cfg.ts_periodicity);
1297 case PROP_TS_LAYER_ID:{
1300 if (gst_vp9_enc->n_ts_layer_id == 0) {
1301 g_value_set_boxed (value, NULL);
1305 va = g_value_array_new (gst_vp9_enc->n_ts_layer_id);
1306 for (i = 0; i < gst_vp9_enc->n_ts_layer_id; i++) {
1309 g_value_init (&v, G_TYPE_INT);
1310 g_value_set_int (&v, gst_vp9_enc->cfg.ts_layer_id[i]);
1311 g_value_array_append (va, &v);
1314 g_value_set_boxed (value, va);
1315 g_value_array_free (va);
1319 case PROP_ERROR_RESILIENT:
1320 g_value_set_flags (value, gst_vp9_enc->cfg.g_error_resilient);
1322 case PROP_LAG_IN_FRAMES:
1323 g_value_set_int (value, gst_vp9_enc->cfg.g_lag_in_frames);
1326 g_value_set_int (value, gst_vp9_enc->cfg.g_threads);
1329 g_value_set_int64 (value, gst_vp9_enc->deadline);
1331 case PROP_H_SCALING_MODE:
1332 g_value_set_enum (value, gst_vp9_enc->h_scaling_mode);
1334 case PROP_V_SCALING_MODE:
1335 g_value_set_enum (value, gst_vp9_enc->v_scaling_mode);
1338 g_value_set_int (value, gst_vp9_enc->cpu_used);
1340 case PROP_ENABLE_AUTO_ALT_REF:
1341 g_value_set_boolean (value, gst_vp9_enc->enable_auto_alt_ref);
1343 case PROP_NOISE_SENSITIVITY:
1344 g_value_set_int (value, gst_vp9_enc->noise_sensitivity);
1346 case PROP_SHARPNESS:
1347 g_value_set_int (value, gst_vp9_enc->sharpness);
1349 case PROP_STATIC_THRESHOLD:
1350 g_value_set_int (value, gst_vp9_enc->static_threshold);
1352 case PROP_TOKEN_PARTITIONS:
1353 g_value_set_enum (value, gst_vp9_enc->token_partitions);
1355 case PROP_ARNR_MAXFRAMES:
1356 g_value_set_int (value, gst_vp9_enc->arnr_maxframes);
1358 case PROP_ARNR_STRENGTH:
1359 g_value_set_int (value, gst_vp9_enc->arnr_strength);
1361 case PROP_ARNR_TYPE:
1362 g_value_set_int (value, gst_vp9_enc->arnr_type);
1365 g_value_set_enum (value, gst_vp9_enc->tuning);
1368 g_value_set_int (value, gst_vp9_enc->cq_level);
1370 case PROP_MAX_INTRA_BITRATE_PCT:
1371 g_value_set_int (value, gst_vp9_enc->max_intra_bitrate_pct);
1374 gst_value_set_fraction (value, gst_vp9_enc->timebase_n,
1375 gst_vp9_enc->timebase_d);
1378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1382 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1386 gst_vp9_enc_start (GstVideoEncoder * video_encoder)
1388 GstVP9Enc *encoder = GST_VP9_ENC (video_encoder);
1390 GST_DEBUG_OBJECT (video_encoder, "start");
1392 if (!encoder->have_default_config) {
1393 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1394 ("Failed to get default encoder configuration"), (NULL));
1402 gst_vp9_enc_stop (GstVideoEncoder * video_encoder)
1406 GST_DEBUG_OBJECT (video_encoder, "stop");
1408 encoder = GST_VP9_ENC (video_encoder);
1410 g_mutex_lock (&encoder->encoder_lock);
1411 if (encoder->inited) {
1412 vpx_codec_destroy (&encoder->encoder);
1413 encoder->inited = FALSE;
1416 if (encoder->first_pass_cache_content) {
1417 g_byte_array_free (encoder->first_pass_cache_content, TRUE);
1418 encoder->first_pass_cache_content = NULL;
1421 if (encoder->cfg.rc_twopass_stats_in.buf) {
1422 g_free (encoder->cfg.rc_twopass_stats_in.buf);
1423 encoder->cfg.rc_twopass_stats_in.buf = NULL;
1424 encoder->cfg.rc_twopass_stats_in.sz = 0;
1426 g_mutex_unlock (&encoder->encoder_lock);
1428 gst_tag_setter_reset_tags (GST_TAG_SETTER (encoder));
1434 gst_vp9_enc_get_downstream_profile (GstVP9Enc * encoder)
1438 gint profile = DEFAULT_PROFILE;
1440 allowed = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1442 allowed = gst_caps_truncate (allowed);
1443 s = gst_caps_get_structure (allowed, 0);
1444 if (gst_structure_has_field (s, "profile")) {
1445 const GValue *v = gst_structure_get_value (s, "profile");
1446 const gchar *profile_str = NULL;
1448 if (GST_VALUE_HOLDS_LIST (v) && gst_value_list_get_size (v) > 0) {
1449 profile_str = g_value_get_string (gst_value_list_get_value (v, 0));
1450 } else if (G_VALUE_HOLDS_STRING (v)) {
1451 profile_str = g_value_get_string (v);
1455 gchar *endptr = NULL;
1457 profile = g_ascii_strtoull (profile_str, &endptr, 10);
1458 if (*endptr != '\0' || profile < 0 || profile > 3) {
1459 GST_ERROR_OBJECT (encoder, "Invalid profile '%s'", profile_str);
1460 profile = DEFAULT_PROFILE;
1464 gst_caps_unref (allowed);
1467 GST_DEBUG_OBJECT (encoder, "Using profile %d", profile);
1473 gst_vp9_enc_set_format (GstVideoEncoder * video_encoder,
1474 GstVideoCodecState * state)
1477 vpx_codec_err_t status;
1480 gboolean ret = TRUE;
1481 GstVideoInfo *info = &state->info;
1482 GstVideoCodecState *output_state;
1485 encoder = GST_VP9_ENC (video_encoder);
1486 GST_DEBUG_OBJECT (video_encoder, "set_format");
1488 if (encoder->inited) {
1489 GST_DEBUG_OBJECT (video_encoder, "refusing renegotiation");
1493 g_mutex_lock (&encoder->encoder_lock);
1494 encoder->cfg.g_profile = gst_vp9_enc_get_downstream_profile (encoder);
1496 /* Scale default bitrate to our size */
1497 if (!encoder->rc_target_bitrate_set)
1498 encoder->cfg.rc_target_bitrate =
1499 gst_util_uint64_scale (DEFAULT_RC_TARGET_BITRATE,
1500 GST_VIDEO_INFO_WIDTH (info) * GST_VIDEO_INFO_HEIGHT (info),
1503 encoder->cfg.g_w = GST_VIDEO_INFO_WIDTH (info);
1504 encoder->cfg.g_h = GST_VIDEO_INFO_HEIGHT (info);
1506 if (encoder->timebase_n != 0 && encoder->timebase_d != 0) {
1507 GST_DEBUG_OBJECT (video_encoder, "Using timebase configuration");
1508 encoder->cfg.g_timebase.num = encoder->timebase_n;
1509 encoder->cfg.g_timebase.den = encoder->timebase_d;
1510 } else if (GST_VIDEO_INFO_FPS_D (info) != 0
1511 && GST_VIDEO_INFO_FPS_N (info) != 0) {
1512 /* GstVideoInfo holds either the framerate or max-framerate (if framerate
1513 * is 0) in FPS so this will be used if max-framerate or framerate
1515 GST_DEBUG_OBJECT (video_encoder, "Setting timebase from framerate");
1516 encoder->cfg.g_timebase.num = GST_VIDEO_INFO_FPS_D (info);
1517 encoder->cfg.g_timebase.den = GST_VIDEO_INFO_FPS_N (info);
1519 /* Zero framerate and max-framerate but still need to setup the timebase to avoid
1520 * a divide by zero error. Presuming the lowest common denominator will be RTP -
1521 * VP9 payload draft states clock rate of 90000 which should work for anyone where
1522 * FPS < 90000 (shouldn't be too many cases where it's higher) though wouldn't be optimal. RTP specification
1523 * http://tools.ietf.org/html/draft-ietf-payload-vp9-01 section 6.3.1 */
1524 GST_WARNING_OBJECT (encoder,
1525 "No timebase and zero framerate setting timebase to 1/90000");
1526 encoder->cfg.g_timebase.num = 1;
1527 encoder->cfg.g_timebase.den = 90000;
1530 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1531 encoder->first_pass_cache_content = g_byte_array_sized_new (4096);
1532 } else if (encoder->cfg.g_pass == VPX_RC_LAST_PASS) {
1535 if (!encoder->multipass_cache_file) {
1536 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1537 ("No multipass cache file provided"), (NULL));
1538 g_mutex_unlock (&encoder->encoder_lock);
1542 if (!g_file_get_contents (encoder->multipass_cache_file,
1543 (gchar **) & encoder->cfg.rc_twopass_stats_in.buf,
1544 &encoder->cfg.rc_twopass_stats_in.sz, &err)) {
1545 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1546 ("Failed to read multipass cache file provided"), ("%s",
1549 g_mutex_unlock (&encoder->encoder_lock);
1554 status = vpx_codec_enc_init (&encoder->encoder, &vpx_codec_vp9_cx_algo,
1556 if (status != VPX_CODEC_OK) {
1557 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1558 ("Failed to initialize encoder"), ("%s", gst_vpx_error_name (status)));
1559 g_mutex_unlock (&encoder->encoder_lock);
1563 /* FIXME: Disabled for now, does not work with VP9 */
1566 vpx_scaling_mode_t sm;
1568 sm.h_scaling_mode = encoder->h_scaling_mode;
1569 sm.v_scaling_mode = encoder->v_scaling_mode;
1571 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SCALEMODE, &sm);
1572 if (status != VPX_CODEC_OK) {
1573 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_SCALEMODE: %s",
1574 gst_vpx_error_name (status));
1580 vpx_codec_control (&encoder->encoder, VP8E_SET_CPUUSED,
1582 if (status != VPX_CODEC_OK) {
1583 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_CPUUSED: %s",
1584 gst_vpx_error_name (status));
1588 vpx_codec_control (&encoder->encoder, VP8E_SET_ENABLEAUTOALTREF,
1589 (encoder->enable_auto_alt_ref ? 1 : 0));
1590 if (status != VPX_CODEC_OK) {
1591 GST_WARNING_OBJECT (encoder,
1592 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1593 gst_vpx_error_name (status));
1595 status = vpx_codec_control (&encoder->encoder, VP8E_SET_NOISE_SENSITIVITY,
1596 encoder->noise_sensitivity);
1597 if (status != VPX_CODEC_OK) {
1598 GST_WARNING_OBJECT (encoder,
1599 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1600 gst_vpx_error_name (status));
1602 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SHARPNESS,
1603 encoder->sharpness);
1604 if (status != VPX_CODEC_OK) {
1605 GST_WARNING_OBJECT (encoder,
1606 "Failed to set VP8E_SET_SHARPNESS: %s", gst_vpx_error_name (status));
1608 status = vpx_codec_control (&encoder->encoder, VP8E_SET_STATIC_THRESHOLD,
1609 encoder->static_threshold);
1610 if (status != VPX_CODEC_OK) {
1611 GST_WARNING_OBJECT (encoder,
1612 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1613 gst_vpx_error_name (status));
1615 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TOKEN_PARTITIONS,
1616 encoder->token_partitions);
1617 if (status != VPX_CODEC_OK) {
1618 GST_WARNING_OBJECT (encoder,
1619 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1620 gst_vpx_error_name (status));
1622 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_MAXFRAMES,
1623 encoder->arnr_maxframes);
1624 if (status != VPX_CODEC_OK) {
1625 GST_WARNING_OBJECT (encoder,
1626 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1627 gst_vpx_error_name (status));
1629 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_STRENGTH,
1630 encoder->arnr_strength);
1631 if (status != VPX_CODEC_OK) {
1632 GST_WARNING_OBJECT (encoder,
1633 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1634 gst_vpx_error_name (status));
1636 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_TYPE,
1637 encoder->arnr_type);
1638 if (status != VPX_CODEC_OK) {
1639 GST_WARNING_OBJECT (encoder,
1640 "Failed to set VP8E_SET_ARNR_TYPE: %s", gst_vpx_error_name (status));
1642 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TUNING,
1644 if (status != VPX_CODEC_OK) {
1645 GST_WARNING_OBJECT (encoder,
1646 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1648 status = vpx_codec_control (&encoder->encoder, VP8E_SET_CQ_LEVEL,
1650 if (status != VPX_CODEC_OK) {
1651 GST_WARNING_OBJECT (encoder,
1652 "Failed to set VP8E_SET_CQ_LEVEL: %s", gst_vpx_error_name (status));
1654 status = vpx_codec_control (&encoder->encoder, VP8E_SET_MAX_INTRA_BITRATE_PCT,
1655 encoder->max_intra_bitrate_pct);
1656 if (status != VPX_CODEC_OK) {
1657 GST_WARNING_OBJECT (encoder,
1658 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1659 gst_vpx_error_name (status));
1662 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
1663 gst_video_encoder_set_latency (video_encoder, GST_CLOCK_TIME_NONE,
1664 GST_CLOCK_TIME_NONE);
1666 gst_video_encoder_set_latency (video_encoder, 0,
1667 gst_util_uint64_scale (encoder->cfg.g_lag_in_frames,
1668 GST_VIDEO_INFO_FPS_D (info) * GST_SECOND,
1669 GST_VIDEO_INFO_FPS_N (info)));
1671 encoder->inited = TRUE;
1673 /* Store input state */
1674 if (encoder->input_state)
1675 gst_video_codec_state_unref (encoder->input_state);
1676 encoder->input_state = gst_video_codec_state_ref (state);
1678 /* prepare cached image buffer setup */
1679 image = &encoder->image;
1680 memset (image, 0, sizeof (*image));
1682 switch (encoder->input_state->info.finfo->format) {
1683 case GST_VIDEO_FORMAT_I420:
1684 image->fmt = VPX_IMG_FMT_I420;
1686 image->x_chroma_shift = image->y_chroma_shift = 1;
1688 case GST_VIDEO_FORMAT_YV12:
1689 image->fmt = VPX_IMG_FMT_YV12;
1691 image->x_chroma_shift = image->y_chroma_shift = 1;
1693 case GST_VIDEO_FORMAT_Y42B:
1694 image->fmt = VPX_IMG_FMT_I422;
1696 image->x_chroma_shift = 1;
1697 image->y_chroma_shift = 0;
1699 case GST_VIDEO_FORMAT_Y444:
1700 image->fmt = VPX_IMG_FMT_I444;
1702 image->x_chroma_shift = image->y_chroma_shift = 0;
1705 g_assert_not_reached ();
1708 image->w = image->d_w = GST_VIDEO_INFO_WIDTH (info);
1709 image->h = image->d_h = GST_VIDEO_INFO_HEIGHT (info);
1711 image->stride[VPX_PLANE_Y] = GST_VIDEO_INFO_COMP_STRIDE (info, 0);
1712 image->stride[VPX_PLANE_U] = GST_VIDEO_INFO_COMP_STRIDE (info, 1);
1713 image->stride[VPX_PLANE_V] = GST_VIDEO_INFO_COMP_STRIDE (info, 2);
1715 profile_str = g_strdup_printf ("%d", encoder->cfg.g_profile);
1716 caps = gst_caps_new_simple ("video/x-vp9",
1717 "profile", G_TYPE_STRING, profile_str, NULL);
1718 g_free (profile_str);
1720 g_mutex_unlock (&encoder->encoder_lock);
1723 gst_video_encoder_set_output_state (video_encoder, caps, state);
1724 gst_video_codec_state_unref (output_state);
1726 gst_video_encoder_negotiate (GST_VIDEO_ENCODER (encoder));
1731 static GstFlowReturn
1732 gst_vp9_enc_process (GstVP9Enc * encoder)
1734 vpx_codec_iter_t iter = NULL;
1735 const vpx_codec_cx_pkt_t *pkt;
1736 GstVideoEncoder *video_encoder;
1737 GstVideoCodecFrame *frame;
1738 GstFlowReturn ret = GST_FLOW_OK;
1740 video_encoder = GST_VIDEO_ENCODER (encoder);
1742 g_mutex_lock (&encoder->encoder_lock);
1743 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1744 while (pkt != NULL) {
1748 GST_DEBUG_OBJECT (encoder, "packet %u type %d", (guint) pkt->data.frame.sz,
1751 if (pkt->kind == VPX_CODEC_STATS_PKT
1752 && encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1753 GST_LOG_OBJECT (encoder, "handling STATS packet");
1755 g_byte_array_append (encoder->first_pass_cache_content,
1756 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1758 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1759 if (frame != NULL) {
1760 buffer = gst_buffer_new ();
1761 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_LIVE);
1762 frame->output_buffer = buffer;
1763 g_mutex_unlock (&encoder->encoder_lock);
1764 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1765 g_mutex_lock (&encoder->encoder_lock);
1768 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1770 } else if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) {
1771 GST_LOG_OBJECT (encoder, "non frame pkt: %d", pkt->kind);
1772 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1776 invisible = (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE) != 0;
1777 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1778 g_assert (frame != NULL);
1779 if ((pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0)
1780 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
1782 GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
1784 /* FIXME : It would be nice to avoid the memory copy ... */
1786 gst_buffer_new_wrapped (g_memdup (pkt->data.frame.buf,
1787 pkt->data.frame.sz), pkt->data.frame.sz);
1790 g_mutex_unlock (&encoder->encoder_lock);
1791 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (encoder), buffer);
1792 g_mutex_lock (&encoder->encoder_lock);
1793 gst_video_codec_frame_unref (frame);
1795 frame->output_buffer = buffer;
1796 g_mutex_unlock (&encoder->encoder_lock);
1797 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1798 g_mutex_lock (&encoder->encoder_lock);
1801 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1803 g_mutex_unlock (&encoder->encoder_lock);
1808 static GstFlowReturn
1809 gst_vp9_enc_finish (GstVideoEncoder * video_encoder)
1813 vpx_codec_err_t status;
1815 GST_DEBUG_OBJECT (video_encoder, "finish");
1817 encoder = GST_VP9_ENC (video_encoder);
1819 g_mutex_lock (&encoder->encoder_lock);
1821 vpx_codec_encode (&encoder->encoder, NULL, encoder->n_frames, 1, flags,
1823 g_mutex_unlock (&encoder->encoder_lock);
1825 GST_ERROR_OBJECT (encoder, "encode returned %d %s", status,
1826 gst_vpx_error_name (status));
1827 return GST_FLOW_ERROR;
1830 /* dispatch remaining frames */
1831 gst_vp9_enc_process (encoder);
1833 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS && encoder->multipass_cache_file) {
1836 if (!g_file_set_contents (encoder->multipass_cache_file,
1837 (const gchar *) encoder->first_pass_cache_content->data,
1838 encoder->first_pass_cache_content->len, &err)) {
1839 GST_ELEMENT_ERROR (encoder, RESOURCE, WRITE, (NULL),
1840 ("Failed to write multipass cache file: %s", err->message));
1848 static vpx_image_t *
1849 gst_vp9_enc_buffer_to_image (GstVP9Enc * enc, GstVideoFrame * frame)
1851 vpx_image_t *image = g_slice_new (vpx_image_t);
1853 memcpy (image, &enc->image, sizeof (*image));
1855 image->planes[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
1856 image->planes[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
1857 image->planes[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
1859 image->stride[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
1860 image->stride[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1);
1861 image->stride[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2);
1866 static GstFlowReturn
1867 gst_vp9_enc_handle_frame (GstVideoEncoder * video_encoder,
1868 GstVideoCodecFrame * frame)
1871 vpx_codec_err_t status;
1874 GstVideoFrame vframe;
1876 GST_DEBUG_OBJECT (video_encoder, "handle_frame");
1878 encoder = GST_VP9_ENC (video_encoder);
1880 encoder->n_frames++;
1882 GST_DEBUG_OBJECT (video_encoder, "size %d %d",
1883 GST_VIDEO_INFO_WIDTH (&encoder->input_state->info),
1884 GST_VIDEO_INFO_HEIGHT (&encoder->input_state->info));
1886 gst_video_frame_map (&vframe, &encoder->input_state->info,
1887 frame->input_buffer, GST_MAP_READ);
1888 image = gst_vp9_enc_buffer_to_image (encoder, &vframe);
1890 if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
1891 flags |= VPX_EFLAG_FORCE_KF;
1894 g_mutex_lock (&encoder->encoder_lock);
1895 status = vpx_codec_encode (&encoder->encoder, image,
1896 encoder->n_frames, 1, flags, encoder->deadline);
1897 g_mutex_unlock (&encoder->encoder_lock);
1898 gst_video_frame_unmap (&vframe);
1901 GST_ELEMENT_ERROR (encoder, LIBRARY, ENCODE,
1902 ("Failed to encode frame"), ("%s", gst_vpx_error_name (status)));
1905 gst_video_codec_frame_unref (frame);
1906 return gst_vp9_enc_process (encoder);
1910 gst_vp9_enc_sink_event (GstVideoEncoder * benc, GstEvent * event)
1912 GstVP9Enc *enc = GST_VP9_ENC (benc);
1914 /* FIXME : Move this to base encoder class */
1916 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
1918 GstTagSetter *setter = GST_TAG_SETTER (enc);
1919 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
1921 gst_event_parse_tag (event, &list);
1922 gst_tag_setter_merge_tags (setter, list, mode);
1925 /* just peeked, baseclass handles the rest */
1926 return GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
1930 gst_vp9_enc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
1932 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1934 return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
1938 #endif /* HAVE_VP9_ENCODER */