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));
889 if (va->n_values > VPX_TS_MAX_LAYERS) {
890 g_warning ("%s: Only %d layers allowed at maximum",
891 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
895 for (i = 0; i < va->n_values; i++)
896 gst_vp9_enc->cfg.ts_target_bitrate[i] =
897 g_value_get_int (g_value_array_get_nth (va, i));
898 gst_vp9_enc->n_ts_target_bitrate = va->n_values;
900 gst_vp9_enc->n_ts_target_bitrate = 0;
905 case PROP_TS_RATE_DECIMATOR:{
906 GValueArray *va = g_value_get_boxed (value);
908 memset (&gst_vp9_enc->cfg.ts_rate_decimator, 0,
909 sizeof (gst_vp9_enc->cfg.ts_rate_decimator));
910 if (va->n_values > VPX_TS_MAX_LAYERS) {
911 g_warning ("%s: Only %d layers allowed at maximum",
912 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
916 for (i = 0; i < va->n_values; i++)
917 gst_vp9_enc->cfg.ts_rate_decimator[i] =
918 g_value_get_int (g_value_array_get_nth (va, i));
919 gst_vp9_enc->n_ts_rate_decimator = va->n_values;
921 gst_vp9_enc->n_ts_rate_decimator = 0;
926 case PROP_TS_PERIODICITY:
927 gst_vp9_enc->cfg.ts_periodicity = g_value_get_int (value);
930 case PROP_TS_LAYER_ID:{
931 GValueArray *va = g_value_get_boxed (value);
933 memset (&gst_vp9_enc->cfg.ts_layer_id, 0,
934 sizeof (gst_vp9_enc->cfg.ts_layer_id));
935 if (va->n_values > VPX_TS_MAX_PERIODICITY) {
936 g_warning ("%s: Only %d sized layer sequences allowed at maximum",
937 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_PERIODICITY);
941 for (i = 0; i < va->n_values; i++)
942 gst_vp9_enc->cfg.ts_layer_id[i] =
943 g_value_get_int (g_value_array_get_nth (va, i));
944 gst_vp9_enc->n_ts_layer_id = va->n_values;
946 gst_vp9_enc->n_ts_layer_id = 0;
951 case PROP_ERROR_RESILIENT:
952 gst_vp9_enc->cfg.g_error_resilient = g_value_get_flags (value);
955 case PROP_LAG_IN_FRAMES:
956 gst_vp9_enc->cfg.g_lag_in_frames = g_value_get_int (value);
960 gst_vp9_enc->cfg.g_threads = g_value_get_int (value);
964 gst_vp9_enc->deadline = g_value_get_int64 (value);
966 case PROP_H_SCALING_MODE:
967 gst_vp9_enc->h_scaling_mode = g_value_get_enum (value);
968 if (gst_vp9_enc->inited) {
969 vpx_scaling_mode_t sm;
971 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
972 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
975 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
976 if (status != VPX_CODEC_OK) {
977 GST_WARNING_OBJECT (gst_vp9_enc,
978 "Failed to set VP8E_SET_SCALEMODE: %s",
979 gst_vpx_error_name (status));
983 case PROP_V_SCALING_MODE:
984 gst_vp9_enc->v_scaling_mode = g_value_get_enum (value);
985 if (gst_vp9_enc->inited) {
986 vpx_scaling_mode_t sm;
988 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
989 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
992 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
993 if (status != VPX_CODEC_OK) {
994 GST_WARNING_OBJECT (gst_vp9_enc,
995 "Failed to set VP8E_SET_SCALEMODE: %s",
996 gst_vpx_error_name (status));
1001 gst_vp9_enc->cpu_used = g_value_get_int (value);
1002 if (gst_vp9_enc->inited) {
1004 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CPUUSED,
1005 gst_vp9_enc->cpu_used);
1006 if (status != VPX_CODEC_OK) {
1007 GST_WARNING_OBJECT (gst_vp9_enc, "Failed to set VP8E_SET_CPUUSED: %s",
1008 gst_vpx_error_name (status));
1012 case PROP_ENABLE_AUTO_ALT_REF:
1013 gst_vp9_enc->enable_auto_alt_ref = g_value_get_boolean (value);
1014 if (gst_vp9_enc->inited) {
1016 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ENABLEAUTOALTREF,
1017 (gst_vp9_enc->enable_auto_alt_ref ? 1 : 0));
1018 if (status != VPX_CODEC_OK) {
1019 GST_WARNING_OBJECT (gst_vp9_enc,
1020 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1021 gst_vpx_error_name (status));
1025 case PROP_NOISE_SENSITIVITY:
1026 gst_vp9_enc->noise_sensitivity = g_value_get_int (value);
1027 if (gst_vp9_enc->inited) {
1029 vpx_codec_control (&gst_vp9_enc->encoder,
1030 VP8E_SET_NOISE_SENSITIVITY, gst_vp9_enc->noise_sensitivity);
1031 if (status != VPX_CODEC_OK) {
1032 GST_WARNING_OBJECT (gst_vp9_enc,
1033 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1034 gst_vpx_error_name (status));
1038 case PROP_SHARPNESS:
1039 gst_vp9_enc->sharpness = g_value_get_int (value);
1040 if (gst_vp9_enc->inited) {
1041 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SHARPNESS,
1042 gst_vp9_enc->sharpness);
1043 if (status != VPX_CODEC_OK) {
1044 GST_WARNING_OBJECT (gst_vp9_enc,
1045 "Failed to set VP8E_SET_SHARPNESS: %s",
1046 gst_vpx_error_name (status));
1050 case PROP_STATIC_THRESHOLD:
1051 gst_vp9_enc->static_threshold = g_value_get_int (value);
1052 if (gst_vp9_enc->inited) {
1054 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_STATIC_THRESHOLD,
1055 gst_vp9_enc->static_threshold);
1056 if (status != VPX_CODEC_OK) {
1057 GST_WARNING_OBJECT (gst_vp9_enc,
1058 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1059 gst_vpx_error_name (status));
1063 case PROP_TOKEN_PARTITIONS:
1064 gst_vp9_enc->token_partitions = g_value_get_enum (value);
1065 if (gst_vp9_enc->inited) {
1067 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TOKEN_PARTITIONS,
1068 gst_vp9_enc->token_partitions);
1069 if (status != VPX_CODEC_OK) {
1070 GST_WARNING_OBJECT (gst_vp9_enc,
1071 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1072 gst_vpx_error_name (status));
1076 case PROP_ARNR_MAXFRAMES:
1077 gst_vp9_enc->arnr_maxframes = g_value_get_int (value);
1078 if (gst_vp9_enc->inited) {
1080 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_MAXFRAMES,
1081 gst_vp9_enc->arnr_maxframes);
1082 if (status != VPX_CODEC_OK) {
1083 GST_WARNING_OBJECT (gst_vp9_enc,
1084 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1085 gst_vpx_error_name (status));
1089 case PROP_ARNR_STRENGTH:
1090 gst_vp9_enc->arnr_strength = g_value_get_int (value);
1091 if (gst_vp9_enc->inited) {
1093 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_STRENGTH,
1094 gst_vp9_enc->arnr_strength);
1095 if (status != VPX_CODEC_OK) {
1096 GST_WARNING_OBJECT (gst_vp9_enc,
1097 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1098 gst_vpx_error_name (status));
1102 case PROP_ARNR_TYPE:
1103 gst_vp9_enc->arnr_type = g_value_get_int (value);
1104 if (gst_vp9_enc->inited) {
1105 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_TYPE,
1106 gst_vp9_enc->arnr_type);
1107 if (status != VPX_CODEC_OK) {
1108 GST_WARNING_OBJECT (gst_vp9_enc,
1109 "Failed to set VP8E_SET_ARNR_TYPE: %s",
1110 gst_vpx_error_name (status));
1115 gst_vp9_enc->tuning = g_value_get_enum (value);
1116 if (gst_vp9_enc->inited) {
1117 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TUNING,
1118 gst_vp9_enc->tuning);
1119 if (status != VPX_CODEC_OK) {
1120 GST_WARNING_OBJECT (gst_vp9_enc,
1121 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1126 gst_vp9_enc->cq_level = g_value_get_int (value);
1127 if (gst_vp9_enc->inited) {
1128 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CQ_LEVEL,
1129 gst_vp9_enc->cq_level);
1130 if (status != VPX_CODEC_OK) {
1131 GST_WARNING_OBJECT (gst_vp9_enc,
1132 "Failed to set VP8E_SET_CQ_LEVEL: %s",
1133 gst_vpx_error_name (status));
1137 case PROP_MAX_INTRA_BITRATE_PCT:
1138 gst_vp9_enc->max_intra_bitrate_pct = g_value_get_int (value);
1139 if (gst_vp9_enc->inited) {
1141 vpx_codec_control (&gst_vp9_enc->encoder,
1142 VP8E_SET_MAX_INTRA_BITRATE_PCT, gst_vp9_enc->max_intra_bitrate_pct);
1143 if (status != VPX_CODEC_OK) {
1144 GST_WARNING_OBJECT (gst_vp9_enc,
1145 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1146 gst_vpx_error_name (status));
1151 gst_vp9_enc->timebase_n = gst_value_get_fraction_numerator (value);
1152 gst_vp9_enc->timebase_d = gst_value_get_fraction_denominator (value);
1158 if (global &&gst_vp9_enc->inited) {
1160 vpx_codec_enc_config_set (&gst_vp9_enc->encoder, &gst_vp9_enc->cfg);
1161 if (status != VPX_CODEC_OK) {
1162 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1163 GST_ELEMENT_ERROR (gst_vp9_enc, LIBRARY, INIT,
1164 ("Failed to set encoder configuration"), ("%s",
1165 gst_vpx_error_name (status)));
1167 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1170 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1175 gst_vp9_enc_get_property (GObject * object, guint prop_id, GValue * value,
1178 GstVP9Enc *gst_vp9_enc;
1180 g_return_if_fail (GST_IS_VP9_ENC (object));
1181 gst_vp9_enc = GST_VP9_ENC (object);
1183 g_mutex_lock (&gst_vp9_enc->encoder_lock);
1185 case PROP_RC_END_USAGE:
1186 g_value_set_enum (value, gst_vp9_enc->cfg.rc_end_usage);
1188 case PROP_RC_TARGET_BITRATE:
1189 g_value_set_int (value, gst_vp9_enc->cfg.rc_target_bitrate * 1000);
1191 case PROP_RC_MIN_QUANTIZER:
1192 g_value_set_int (value, gst_vp9_enc->cfg.rc_min_quantizer);
1194 case PROP_RC_MAX_QUANTIZER:
1195 g_value_set_int (value, gst_vp9_enc->cfg.rc_max_quantizer);
1197 case PROP_RC_DROPFRAME_THRESH:
1198 g_value_set_int (value, gst_vp9_enc->cfg.rc_dropframe_thresh);
1200 case PROP_RC_RESIZE_ALLOWED:
1201 g_value_set_boolean (value, gst_vp9_enc->cfg.rc_resize_allowed);
1203 case PROP_RC_RESIZE_UP_THRESH:
1204 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_up_thresh);
1206 case PROP_RC_RESIZE_DOWN_THRESH:
1207 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_down_thresh);
1209 case PROP_RC_UNDERSHOOT_PCT:
1210 g_value_set_int (value, gst_vp9_enc->cfg.rc_undershoot_pct);
1212 case PROP_RC_OVERSHOOT_PCT:
1213 g_value_set_int (value, gst_vp9_enc->cfg.rc_overshoot_pct);
1215 case PROP_RC_BUF_SZ:
1216 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_sz);
1218 case PROP_RC_BUF_INITIAL_SZ:
1219 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_initial_sz);
1221 case PROP_RC_BUF_OPTIMAL_SZ:
1222 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_optimal_sz);
1224 case PROP_RC_2PASS_VBR_BIAS_PCT:
1225 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct);
1227 case PROP_RC_2PASS_VBR_MINSECTION_PCT:
1228 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct);
1230 case PROP_RC_2PASS_VBR_MAXSECTION_PCT:
1231 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct);
1234 g_value_set_enum (value, gst_vp9_enc->cfg.kf_mode);
1236 case PROP_KF_MAX_DIST:
1237 g_value_set_int (value, gst_vp9_enc->cfg.kf_max_dist);
1239 case PROP_MULTIPASS_MODE:
1240 g_value_set_enum (value, gst_vp9_enc->cfg.g_pass);
1242 case PROP_MULTIPASS_CACHE_FILE:
1243 g_value_set_string (value, gst_vp9_enc->multipass_cache_file);
1245 case PROP_TS_NUMBER_LAYERS:
1246 g_value_set_int (value, gst_vp9_enc->cfg.ts_number_layers);
1248 case PROP_TS_TARGET_BITRATE:{
1251 if (gst_vp9_enc->n_ts_target_bitrate == 0) {
1252 g_value_set_boxed (value, NULL);
1256 va = g_value_array_new (gst_vp9_enc->n_ts_target_bitrate);
1257 for (i = 0; i < gst_vp9_enc->n_ts_target_bitrate; i++) {
1260 g_value_init (&v, G_TYPE_INT);
1261 g_value_set_int (&v, gst_vp9_enc->cfg.ts_target_bitrate[i]);
1262 g_value_array_append (va, &v);
1265 g_value_set_boxed (value, va);
1266 g_value_array_free (va);
1270 case PROP_TS_RATE_DECIMATOR:{
1273 if (gst_vp9_enc->n_ts_rate_decimator == 0) {
1274 g_value_set_boxed (value, NULL);
1278 va = g_value_array_new (gst_vp9_enc->n_ts_rate_decimator);
1279 for (i = 0; i < gst_vp9_enc->n_ts_rate_decimator; i++) {
1282 g_value_init (&v, G_TYPE_INT);
1283 g_value_set_int (&v, gst_vp9_enc->cfg.ts_rate_decimator[i]);
1284 g_value_array_append (va, &v);
1287 g_value_set_boxed (value, va);
1288 g_value_array_free (va);
1292 case PROP_TS_PERIODICITY:
1293 g_value_set_int (value, gst_vp9_enc->cfg.ts_periodicity);
1295 case PROP_TS_LAYER_ID:{
1298 if (gst_vp9_enc->n_ts_layer_id == 0) {
1299 g_value_set_boxed (value, NULL);
1303 va = g_value_array_new (gst_vp9_enc->n_ts_layer_id);
1304 for (i = 0; i < gst_vp9_enc->n_ts_layer_id; i++) {
1307 g_value_init (&v, G_TYPE_INT);
1308 g_value_set_int (&v, gst_vp9_enc->cfg.ts_layer_id[i]);
1309 g_value_array_append (va, &v);
1312 g_value_set_boxed (value, va);
1313 g_value_array_free (va);
1317 case PROP_ERROR_RESILIENT:
1318 g_value_set_flags (value, gst_vp9_enc->cfg.g_error_resilient);
1320 case PROP_LAG_IN_FRAMES:
1321 g_value_set_int (value, gst_vp9_enc->cfg.g_lag_in_frames);
1324 g_value_set_int (value, gst_vp9_enc->cfg.g_threads);
1327 g_value_set_int64 (value, gst_vp9_enc->deadline);
1329 case PROP_H_SCALING_MODE:
1330 g_value_set_enum (value, gst_vp9_enc->h_scaling_mode);
1332 case PROP_V_SCALING_MODE:
1333 g_value_set_enum (value, gst_vp9_enc->v_scaling_mode);
1336 g_value_set_int (value, gst_vp9_enc->cpu_used);
1338 case PROP_ENABLE_AUTO_ALT_REF:
1339 g_value_set_boolean (value, gst_vp9_enc->enable_auto_alt_ref);
1341 case PROP_NOISE_SENSITIVITY:
1342 g_value_set_int (value, gst_vp9_enc->noise_sensitivity);
1344 case PROP_SHARPNESS:
1345 g_value_set_int (value, gst_vp9_enc->sharpness);
1347 case PROP_STATIC_THRESHOLD:
1348 g_value_set_int (value, gst_vp9_enc->static_threshold);
1350 case PROP_TOKEN_PARTITIONS:
1351 g_value_set_enum (value, gst_vp9_enc->token_partitions);
1353 case PROP_ARNR_MAXFRAMES:
1354 g_value_set_int (value, gst_vp9_enc->arnr_maxframes);
1356 case PROP_ARNR_STRENGTH:
1357 g_value_set_int (value, gst_vp9_enc->arnr_strength);
1359 case PROP_ARNR_TYPE:
1360 g_value_set_int (value, gst_vp9_enc->arnr_type);
1363 g_value_set_enum (value, gst_vp9_enc->tuning);
1366 g_value_set_int (value, gst_vp9_enc->cq_level);
1368 case PROP_MAX_INTRA_BITRATE_PCT:
1369 g_value_set_int (value, gst_vp9_enc->max_intra_bitrate_pct);
1372 gst_value_set_fraction (value, gst_vp9_enc->timebase_n,
1373 gst_vp9_enc->timebase_d);
1376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1380 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1384 gst_vp9_enc_start (GstVideoEncoder * video_encoder)
1386 GstVP9Enc *encoder = GST_VP9_ENC (video_encoder);
1388 GST_DEBUG_OBJECT (video_encoder, "start");
1390 if (!encoder->have_default_config) {
1391 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1392 ("Failed to get default encoder configuration"), (NULL));
1400 gst_vp9_enc_stop (GstVideoEncoder * video_encoder)
1404 GST_DEBUG_OBJECT (video_encoder, "stop");
1406 encoder = GST_VP9_ENC (video_encoder);
1408 g_mutex_lock (&encoder->encoder_lock);
1409 if (encoder->inited) {
1410 vpx_codec_destroy (&encoder->encoder);
1411 encoder->inited = FALSE;
1414 if (encoder->first_pass_cache_content) {
1415 g_byte_array_free (encoder->first_pass_cache_content, TRUE);
1416 encoder->first_pass_cache_content = NULL;
1419 if (encoder->cfg.rc_twopass_stats_in.buf) {
1420 g_free (encoder->cfg.rc_twopass_stats_in.buf);
1421 encoder->cfg.rc_twopass_stats_in.buf = NULL;
1422 encoder->cfg.rc_twopass_stats_in.sz = 0;
1424 g_mutex_unlock (&encoder->encoder_lock);
1426 gst_tag_setter_reset_tags (GST_TAG_SETTER (encoder));
1432 gst_vp9_enc_get_downstream_profile (GstVP9Enc * encoder)
1436 gint profile = DEFAULT_PROFILE;
1438 allowed = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1440 allowed = gst_caps_truncate (allowed);
1441 s = gst_caps_get_structure (allowed, 0);
1442 if (gst_structure_has_field (s, "profile")) {
1443 const GValue *v = gst_structure_get_value (s, "profile");
1444 const gchar *profile_str = NULL;
1446 if (GST_VALUE_HOLDS_LIST (v) && gst_value_list_get_size (v) > 0) {
1447 profile_str = g_value_get_string (gst_value_list_get_value (v, 0));
1448 } else if (G_VALUE_HOLDS_STRING (v)) {
1449 profile_str = g_value_get_string (v);
1453 gchar *endptr = NULL;
1455 profile = g_ascii_strtoull (profile_str, &endptr, 10);
1456 if (*endptr != '\0' || profile < 0 || profile > 3) {
1457 GST_ERROR_OBJECT (encoder, "Invalid profile '%s'", profile_str);
1458 profile = DEFAULT_PROFILE;
1462 gst_caps_unref (allowed);
1465 GST_DEBUG_OBJECT (encoder, "Using profile %d", profile);
1471 gst_vp9_enc_set_format (GstVideoEncoder * video_encoder,
1472 GstVideoCodecState * state)
1475 vpx_codec_err_t status;
1478 gboolean ret = TRUE;
1479 GstVideoInfo *info = &state->info;
1480 GstVideoCodecState *output_state;
1483 encoder = GST_VP9_ENC (video_encoder);
1484 GST_DEBUG_OBJECT (video_encoder, "set_format");
1486 if (encoder->inited) {
1487 GST_DEBUG_OBJECT (video_encoder, "refusing renegotiation");
1491 g_mutex_lock (&encoder->encoder_lock);
1492 encoder->cfg.g_profile = gst_vp9_enc_get_downstream_profile (encoder);
1494 /* Scale default bitrate to our size */
1495 if (!encoder->rc_target_bitrate_set)
1496 encoder->cfg.rc_target_bitrate =
1497 gst_util_uint64_scale (DEFAULT_RC_TARGET_BITRATE,
1498 GST_VIDEO_INFO_WIDTH (info) * GST_VIDEO_INFO_HEIGHT (info),
1501 encoder->cfg.g_w = GST_VIDEO_INFO_WIDTH (info);
1502 encoder->cfg.g_h = GST_VIDEO_INFO_HEIGHT (info);
1504 if (encoder->timebase_n != 0 && encoder->timebase_d != 0) {
1505 GST_DEBUG_OBJECT (video_encoder, "Using timebase configuration");
1506 encoder->cfg.g_timebase.num = encoder->timebase_n;
1507 encoder->cfg.g_timebase.den = encoder->timebase_d;
1508 } else if (GST_VIDEO_INFO_FPS_D (info) != 0
1509 && GST_VIDEO_INFO_FPS_N (info) != 0) {
1510 /* GstVideoInfo holds either the framerate or max-framerate (if framerate
1511 * is 0) in FPS so this will be used if max-framerate or framerate
1513 GST_DEBUG_OBJECT (video_encoder, "Setting timebase from framerate");
1514 encoder->cfg.g_timebase.num = GST_VIDEO_INFO_FPS_D (info);
1515 encoder->cfg.g_timebase.den = GST_VIDEO_INFO_FPS_N (info);
1517 /* Zero framerate and max-framerate but still need to setup the timebase to avoid
1518 * a divide by zero error. Presuming the lowest common denominator will be RTP -
1519 * VP9 payload draft states clock rate of 90000 which should work for anyone where
1520 * FPS < 90000 (shouldn't be too many cases where it's higher) though wouldn't be optimal. RTP specification
1521 * http://tools.ietf.org/html/draft-ietf-payload-vp9-01 section 6.3.1 */
1522 GST_WARNING_OBJECT (encoder,
1523 "No timebase and zero framerate setting timebase to 1/90000");
1524 encoder->cfg.g_timebase.num = 1;
1525 encoder->cfg.g_timebase.den = 90000;
1528 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1529 encoder->first_pass_cache_content = g_byte_array_sized_new (4096);
1530 } else if (encoder->cfg.g_pass == VPX_RC_LAST_PASS) {
1533 if (!encoder->multipass_cache_file) {
1534 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1535 ("No multipass cache file provided"), (NULL));
1536 g_mutex_unlock (&encoder->encoder_lock);
1540 if (!g_file_get_contents (encoder->multipass_cache_file,
1541 (gchar **) & encoder->cfg.rc_twopass_stats_in.buf,
1542 &encoder->cfg.rc_twopass_stats_in.sz, &err)) {
1543 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1544 ("Failed to read multipass cache file provided"), ("%s",
1547 g_mutex_unlock (&encoder->encoder_lock);
1552 status = vpx_codec_enc_init (&encoder->encoder, &vpx_codec_vp9_cx_algo,
1554 if (status != VPX_CODEC_OK) {
1555 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1556 ("Failed to initialize encoder"), ("%s", gst_vpx_error_name (status)));
1557 g_mutex_unlock (&encoder->encoder_lock);
1561 /* FIXME: Disabled for now, does not work with VP9 */
1564 vpx_scaling_mode_t sm;
1566 sm.h_scaling_mode = encoder->h_scaling_mode;
1567 sm.v_scaling_mode = encoder->v_scaling_mode;
1569 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SCALEMODE, &sm);
1570 if (status != VPX_CODEC_OK) {
1571 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_SCALEMODE: %s",
1572 gst_vpx_error_name (status));
1578 vpx_codec_control (&encoder->encoder, VP8E_SET_CPUUSED,
1580 if (status != VPX_CODEC_OK) {
1581 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_CPUUSED: %s",
1582 gst_vpx_error_name (status));
1586 vpx_codec_control (&encoder->encoder, VP8E_SET_ENABLEAUTOALTREF,
1587 (encoder->enable_auto_alt_ref ? 1 : 0));
1588 if (status != VPX_CODEC_OK) {
1589 GST_WARNING_OBJECT (encoder,
1590 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1591 gst_vpx_error_name (status));
1593 status = vpx_codec_control (&encoder->encoder, VP8E_SET_NOISE_SENSITIVITY,
1594 encoder->noise_sensitivity);
1595 if (status != VPX_CODEC_OK) {
1596 GST_WARNING_OBJECT (encoder,
1597 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1598 gst_vpx_error_name (status));
1600 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SHARPNESS,
1601 encoder->sharpness);
1602 if (status != VPX_CODEC_OK) {
1603 GST_WARNING_OBJECT (encoder,
1604 "Failed to set VP8E_SET_SHARPNESS: %s", gst_vpx_error_name (status));
1606 status = vpx_codec_control (&encoder->encoder, VP8E_SET_STATIC_THRESHOLD,
1607 encoder->static_threshold);
1608 if (status != VPX_CODEC_OK) {
1609 GST_WARNING_OBJECT (encoder,
1610 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1611 gst_vpx_error_name (status));
1613 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TOKEN_PARTITIONS,
1614 encoder->token_partitions);
1615 if (status != VPX_CODEC_OK) {
1616 GST_WARNING_OBJECT (encoder,
1617 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1618 gst_vpx_error_name (status));
1620 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_MAXFRAMES,
1621 encoder->arnr_maxframes);
1622 if (status != VPX_CODEC_OK) {
1623 GST_WARNING_OBJECT (encoder,
1624 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1625 gst_vpx_error_name (status));
1627 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_STRENGTH,
1628 encoder->arnr_strength);
1629 if (status != VPX_CODEC_OK) {
1630 GST_WARNING_OBJECT (encoder,
1631 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1632 gst_vpx_error_name (status));
1634 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_TYPE,
1635 encoder->arnr_type);
1636 if (status != VPX_CODEC_OK) {
1637 GST_WARNING_OBJECT (encoder,
1638 "Failed to set VP8E_SET_ARNR_TYPE: %s", gst_vpx_error_name (status));
1640 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TUNING,
1642 if (status != VPX_CODEC_OK) {
1643 GST_WARNING_OBJECT (encoder,
1644 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1646 status = vpx_codec_control (&encoder->encoder, VP8E_SET_CQ_LEVEL,
1648 if (status != VPX_CODEC_OK) {
1649 GST_WARNING_OBJECT (encoder,
1650 "Failed to set VP8E_SET_CQ_LEVEL: %s", gst_vpx_error_name (status));
1652 status = vpx_codec_control (&encoder->encoder, VP8E_SET_MAX_INTRA_BITRATE_PCT,
1653 encoder->max_intra_bitrate_pct);
1654 if (status != VPX_CODEC_OK) {
1655 GST_WARNING_OBJECT (encoder,
1656 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1657 gst_vpx_error_name (status));
1660 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
1661 gst_video_encoder_set_latency (video_encoder, GST_CLOCK_TIME_NONE,
1662 GST_CLOCK_TIME_NONE);
1664 gst_video_encoder_set_latency (video_encoder, 0,
1665 gst_util_uint64_scale (encoder->cfg.g_lag_in_frames,
1666 GST_VIDEO_INFO_FPS_D (info) * GST_SECOND,
1667 GST_VIDEO_INFO_FPS_N (info)));
1669 encoder->inited = TRUE;
1671 /* Store input state */
1672 if (encoder->input_state)
1673 gst_video_codec_state_unref (encoder->input_state);
1674 encoder->input_state = gst_video_codec_state_ref (state);
1676 /* prepare cached image buffer setup */
1677 image = &encoder->image;
1678 memset (image, 0, sizeof (*image));
1680 switch (encoder->input_state->info.finfo->format) {
1681 case GST_VIDEO_FORMAT_I420:
1682 image->fmt = VPX_IMG_FMT_I420;
1684 image->x_chroma_shift = image->y_chroma_shift = 1;
1686 case GST_VIDEO_FORMAT_YV12:
1687 image->fmt = VPX_IMG_FMT_YV12;
1689 image->x_chroma_shift = image->y_chroma_shift = 1;
1691 case GST_VIDEO_FORMAT_Y42B:
1692 image->fmt = VPX_IMG_FMT_I422;
1694 image->x_chroma_shift = 1;
1695 image->y_chroma_shift = 0;
1697 case GST_VIDEO_FORMAT_Y444:
1698 image->fmt = VPX_IMG_FMT_I444;
1700 image->x_chroma_shift = image->y_chroma_shift = 0;
1703 g_assert_not_reached ();
1706 image->w = image->d_w = GST_VIDEO_INFO_WIDTH (info);
1707 image->h = image->d_h = GST_VIDEO_INFO_HEIGHT (info);
1709 image->stride[VPX_PLANE_Y] = GST_VIDEO_INFO_COMP_STRIDE (info, 0);
1710 image->stride[VPX_PLANE_U] = GST_VIDEO_INFO_COMP_STRIDE (info, 1);
1711 image->stride[VPX_PLANE_V] = GST_VIDEO_INFO_COMP_STRIDE (info, 2);
1713 profile_str = g_strdup_printf ("%d", encoder->cfg.g_profile);
1714 caps = gst_caps_new_simple ("video/x-vp9",
1715 "profile", G_TYPE_STRING, profile_str, NULL);
1716 g_free (profile_str);
1718 g_mutex_unlock (&encoder->encoder_lock);
1721 gst_video_encoder_set_output_state (video_encoder, caps, state);
1722 gst_video_codec_state_unref (output_state);
1724 gst_video_encoder_negotiate (GST_VIDEO_ENCODER (encoder));
1729 static GstFlowReturn
1730 gst_vp9_enc_process (GstVP9Enc * encoder)
1732 vpx_codec_iter_t iter = NULL;
1733 const vpx_codec_cx_pkt_t *pkt;
1734 GstVideoEncoder *video_encoder;
1735 GstVideoCodecFrame *frame;
1736 GstFlowReturn ret = GST_FLOW_OK;
1738 video_encoder = GST_VIDEO_ENCODER (encoder);
1740 g_mutex_lock (&encoder->encoder_lock);
1741 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1742 while (pkt != NULL) {
1746 GST_DEBUG_OBJECT (encoder, "packet %u type %d", (guint) pkt->data.frame.sz,
1749 if (pkt->kind == VPX_CODEC_STATS_PKT
1750 && encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1751 GST_LOG_OBJECT (encoder, "handling STATS packet");
1753 g_byte_array_append (encoder->first_pass_cache_content,
1754 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1756 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1757 if (frame != NULL) {
1758 buffer = gst_buffer_new ();
1759 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_LIVE);
1760 frame->output_buffer = buffer;
1761 g_mutex_unlock (&encoder->encoder_lock);
1762 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1763 g_mutex_lock (&encoder->encoder_lock);
1766 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1768 } else if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) {
1769 GST_LOG_OBJECT (encoder, "non frame pkt: %d", pkt->kind);
1770 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1774 invisible = (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE) != 0;
1775 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1776 g_assert (frame != NULL);
1777 if ((pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0)
1778 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
1780 GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
1782 /* FIXME : It would be nice to avoid the memory copy ... */
1784 gst_buffer_new_wrapped (g_memdup (pkt->data.frame.buf,
1785 pkt->data.frame.sz), pkt->data.frame.sz);
1788 g_mutex_unlock (&encoder->encoder_lock);
1789 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (encoder), buffer);
1790 g_mutex_lock (&encoder->encoder_lock);
1791 gst_video_codec_frame_unref (frame);
1793 frame->output_buffer = buffer;
1794 g_mutex_unlock (&encoder->encoder_lock);
1795 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1796 g_mutex_lock (&encoder->encoder_lock);
1799 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1801 g_mutex_unlock (&encoder->encoder_lock);
1806 static GstFlowReturn
1807 gst_vp9_enc_finish (GstVideoEncoder * video_encoder)
1811 vpx_codec_err_t status;
1813 GST_DEBUG_OBJECT (video_encoder, "finish");
1815 encoder = GST_VP9_ENC (video_encoder);
1817 g_mutex_lock (&encoder->encoder_lock);
1819 vpx_codec_encode (&encoder->encoder, NULL, encoder->n_frames, 1, flags,
1821 g_mutex_unlock (&encoder->encoder_lock);
1823 GST_ERROR_OBJECT (encoder, "encode returned %d %s", status,
1824 gst_vpx_error_name (status));
1825 return GST_FLOW_ERROR;
1828 /* dispatch remaining frames */
1829 gst_vp9_enc_process (encoder);
1831 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS && encoder->multipass_cache_file) {
1834 if (!g_file_set_contents (encoder->multipass_cache_file,
1835 (const gchar *) encoder->first_pass_cache_content->data,
1836 encoder->first_pass_cache_content->len, &err)) {
1837 GST_ELEMENT_ERROR (encoder, RESOURCE, WRITE, (NULL),
1838 ("Failed to write multipass cache file: %s", err->message));
1846 static vpx_image_t *
1847 gst_vp9_enc_buffer_to_image (GstVP9Enc * enc, GstVideoFrame * frame)
1849 vpx_image_t *image = g_slice_new (vpx_image_t);
1851 memcpy (image, &enc->image, sizeof (*image));
1853 image->planes[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
1854 image->planes[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
1855 image->planes[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
1857 image->stride[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
1858 image->stride[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1);
1859 image->stride[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2);
1864 static GstFlowReturn
1865 gst_vp9_enc_handle_frame (GstVideoEncoder * video_encoder,
1866 GstVideoCodecFrame * frame)
1869 vpx_codec_err_t status;
1872 GstVideoFrame vframe;
1874 GST_DEBUG_OBJECT (video_encoder, "handle_frame");
1876 encoder = GST_VP9_ENC (video_encoder);
1878 encoder->n_frames++;
1880 GST_DEBUG_OBJECT (video_encoder, "size %d %d",
1881 GST_VIDEO_INFO_WIDTH (&encoder->input_state->info),
1882 GST_VIDEO_INFO_HEIGHT (&encoder->input_state->info));
1884 gst_video_frame_map (&vframe, &encoder->input_state->info,
1885 frame->input_buffer, GST_MAP_READ);
1886 image = gst_vp9_enc_buffer_to_image (encoder, &vframe);
1888 if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
1889 flags |= VPX_EFLAG_FORCE_KF;
1892 g_mutex_lock (&encoder->encoder_lock);
1893 status = vpx_codec_encode (&encoder->encoder, image,
1894 encoder->n_frames, 1, flags, encoder->deadline);
1895 g_mutex_unlock (&encoder->encoder_lock);
1896 gst_video_frame_unmap (&vframe);
1899 GST_ELEMENT_ERROR (encoder, LIBRARY, ENCODE,
1900 ("Failed to encode frame"), ("%s", gst_vpx_error_name (status)));
1903 gst_video_codec_frame_unref (frame);
1904 return gst_vp9_enc_process (encoder);
1908 gst_vp9_enc_sink_event (GstVideoEncoder * benc, GstEvent * event)
1910 GstVP9Enc *enc = GST_VP9_ENC (benc);
1912 /* FIXME : Move this to base encoder class */
1914 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
1916 GstTagSetter *setter = GST_TAG_SETTER (enc);
1917 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
1919 gst_event_parse_tag (event, &list);
1920 gst_tag_setter_merge_tags (setter, list, mode);
1923 /* just peeked, baseclass handles the rest */
1924 return GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
1928 gst_vp9_enc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
1930 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1932 return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
1936 #endif /* HAVE_VP9_ENCODER */