2 * Copyright (C) 2006 David Schleef <ds@schleef.org>
3 * Copyright (C) 2010 Entropy Wave Inc
4 * Copyright (C) 2010-2013 Sebastian Dröge <slomo@circular-chaos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-vp9enc
24 * @see_also: vp9dec, webmmux, oggmux
26 * This element encodes raw video into a VP9 stream.
27 * <ulink url="http://www.webmproject.org">VP9</ulink> is a royalty-free
28 * video codec maintained by <ulink url="http://www.google.com/">Google
29 * </ulink>. It's the successor of On2 VP3, which was the base of the
32 * To control the quality of the encoding, the #GstVP9Enc::target-bitrate,
33 * #GstVP9Enc::min-quantizer, #GstVP9Enc::max-quantizer or #GstVP9Enc::cq-level
34 * properties can be used. Which one is used depends on the mode selected by
35 * the #GstVP9Enc::end-usage property.
36 * See <ulink url="http://www.webmproject.org/docs/encoder-parameters/">Encoder Parameters</ulink>
37 * for explanation, examples for useful encoding parameters and more details
38 * on the encoding parameters.
41 * <title>Example pipeline</title>
43 * gst-launch-1.0 -v videotestsrc num-buffers=1000 ! vp9enc ! webmmux ! filesink location=videotestsrc.webm
44 * ]| This example pipeline will encode a test video source to VP9 muxed in an
53 #ifdef HAVE_VP9_ENCODER
55 /* glib decided in 2.32 it would be a great idea to deprecated GValueArray without
56 * providing an alternative
58 * See https://bugzilla.gnome.org/show_bug.cgi?id=667228
60 #define GLIB_DISABLE_DEPRECATION_WARNINGS
62 #include <gst/tag/tag.h>
63 #include <gst/video/video.h>
66 #include "gstvp8utils.h"
67 #include "gstvp9enc.h"
69 GST_DEBUG_CATEGORY_STATIC (gst_vp9enc_debug);
70 #define GST_CAT_DEFAULT gst_vp9enc_debug
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 GstFlowReturn gst_vp9_enc_finish (GstVideoEncoder * video_encoder);
349 static gboolean gst_vp9_enc_flush (GstVideoEncoder * video_encoder);
350 static GstFlowReturn gst_vp9_enc_drain (GstVideoEncoder * video_encoder);
351 static GstFlowReturn gst_vp9_enc_handle_frame (GstVideoEncoder *
352 video_encoder, GstVideoCodecFrame * frame);
353 static gboolean gst_vp9_enc_sink_event (GstVideoEncoder *
354 video_encoder, GstEvent * event);
355 static gboolean gst_vp9_enc_propose_allocation (GstVideoEncoder * encoder,
358 /* FIXME: Y42B and Y444 do not work yet it seems */
359 static GstStaticPadTemplate gst_vp9_enc_sink_template =
360 GST_STATIC_PAD_TEMPLATE ("sink",
363 /*GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }")) */
364 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12 }"))
367 static GstStaticPadTemplate gst_vp9_enc_src_template =
368 GST_STATIC_PAD_TEMPLATE ("src",
371 GST_STATIC_CAPS ("video/x-vp9, " "profile = (string) {0, 1, 2, 3}")
374 #define parent_class gst_vp9_enc_parent_class
375 G_DEFINE_TYPE_WITH_CODE (GstVP9Enc, gst_vp9_enc, GST_TYPE_VIDEO_ENCODER,
376 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
377 G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL););
380 gst_vp9_enc_class_init (GstVP9EncClass * klass)
382 GObjectClass *gobject_class;
383 GstElementClass *element_class;
384 GstVideoEncoderClass *video_encoder_class;
386 gobject_class = G_OBJECT_CLASS (klass);
387 element_class = GST_ELEMENT_CLASS (klass);
388 video_encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
390 gobject_class->set_property = gst_vp9_enc_set_property;
391 gobject_class->get_property = gst_vp9_enc_get_property;
392 gobject_class->finalize = gst_vp9_enc_finalize;
394 gst_element_class_add_pad_template (element_class,
395 gst_static_pad_template_get (&gst_vp9_enc_src_template));
396 gst_element_class_add_pad_template (element_class,
397 gst_static_pad_template_get (&gst_vp9_enc_sink_template));
399 gst_element_class_set_static_metadata (element_class,
401 "Codec/Encoder/Video",
402 "Encode VP9 video streams", "David Schleef <ds@entropywave.com>, "
403 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
405 video_encoder_class->start = gst_vp9_enc_start;
406 video_encoder_class->stop = gst_vp9_enc_stop;
407 video_encoder_class->handle_frame = gst_vp9_enc_handle_frame;
408 video_encoder_class->set_format = gst_vp9_enc_set_format;
409 video_encoder_class->flush = gst_vp9_enc_flush;
410 video_encoder_class->finish = gst_vp9_enc_finish;
411 video_encoder_class->sink_event = gst_vp9_enc_sink_event;
412 video_encoder_class->propose_allocation = gst_vp9_enc_propose_allocation;
414 g_object_class_install_property (gobject_class, PROP_RC_END_USAGE,
415 g_param_spec_enum ("end-usage", "Rate control mode",
417 GST_VP9_ENC_END_USAGE_TYPE, DEFAULT_RC_END_USAGE,
418 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
420 g_object_class_install_property (gobject_class, PROP_RC_TARGET_BITRATE,
421 g_param_spec_int ("target-bitrate", "Target bitrate",
422 "Target bitrate (in bits/sec)",
423 0, G_MAXINT, DEFAULT_RC_TARGET_BITRATE,
424 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
426 g_object_class_install_property (gobject_class, PROP_RC_MIN_QUANTIZER,
427 g_param_spec_int ("min-quantizer", "Minimum Quantizer",
428 "Minimum Quantizer (best)",
429 0, 63, DEFAULT_RC_MIN_QUANTIZER,
430 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
432 g_object_class_install_property (gobject_class, PROP_RC_MAX_QUANTIZER,
433 g_param_spec_int ("max-quantizer", "Maximum Quantizer",
434 "Maximum Quantizer (worst)",
435 0, 63, DEFAULT_RC_MAX_QUANTIZER,
436 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
438 g_object_class_install_property (gobject_class, PROP_RC_DROPFRAME_THRESH,
439 g_param_spec_int ("dropframe-threshold", "Drop Frame Threshold",
440 "Temporal resampling threshold (buf %)",
441 0, 100, DEFAULT_RC_DROPFRAME_THRESH,
442 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
444 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_ALLOWED,
445 g_param_spec_boolean ("resize-allowed", "Resize Allowed",
446 "Allow spatial resampling",
447 DEFAULT_RC_RESIZE_ALLOWED,
448 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
450 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_UP_THRESH,
451 g_param_spec_int ("resize-up-threshold", "Resize Up Threshold",
452 "Upscale threshold (buf %)",
453 0, 100, DEFAULT_RC_RESIZE_UP_THRESH,
454 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
456 g_object_class_install_property (gobject_class, PROP_RC_RESIZE_DOWN_THRESH,
457 g_param_spec_int ("resize-down-threshold", "Resize Down Threshold",
458 "Downscale threshold (buf %)",
459 0, 100, DEFAULT_RC_RESIZE_DOWN_THRESH,
460 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
462 g_object_class_install_property (gobject_class, PROP_RC_UNDERSHOOT_PCT,
463 g_param_spec_int ("undershoot", "Undershoot PCT",
464 "Datarate undershoot (min) target (%)",
465 0, 1000, DEFAULT_RC_UNDERSHOOT_PCT,
466 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
468 g_object_class_install_property (gobject_class, PROP_RC_OVERSHOOT_PCT,
469 g_param_spec_int ("overshoot", "Overshoot PCT",
470 "Datarate overshoot (max) target (%)",
471 0, 1000, DEFAULT_RC_OVERSHOOT_PCT,
472 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
474 g_object_class_install_property (gobject_class, PROP_RC_BUF_SZ,
475 g_param_spec_int ("buffer-size", "Buffer size",
476 "Client buffer size (ms)",
477 0, G_MAXINT, DEFAULT_RC_BUF_SZ,
478 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
480 g_object_class_install_property (gobject_class, PROP_RC_BUF_INITIAL_SZ,
481 g_param_spec_int ("buffer-initial-size", "Buffer initial size",
482 "Initial client buffer size (ms)",
483 0, G_MAXINT, DEFAULT_RC_BUF_INITIAL_SZ,
484 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
486 g_object_class_install_property (gobject_class, PROP_RC_BUF_OPTIMAL_SZ,
487 g_param_spec_int ("buffer-optimal-size", "Buffer optimal size",
488 "Optimal client buffer size (ms)",
489 0, G_MAXINT, DEFAULT_RC_BUF_OPTIMAL_SZ,
490 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
492 g_object_class_install_property (gobject_class, PROP_RC_2PASS_VBR_BIAS_PCT,
493 g_param_spec_int ("twopass-vbr-bias", "2-pass VBR bias",
494 "CBR/VBR bias (0=CBR, 100=VBR)",
495 0, 100, DEFAULT_RC_2PASS_VBR_BIAS_PCT,
496 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
498 g_object_class_install_property (gobject_class,
499 PROP_RC_2PASS_VBR_MINSECTION_PCT,
500 g_param_spec_int ("twopass-vbr-minsection", "2-pass GOP min bitrate",
501 "GOP minimum bitrate (% target)", 0, G_MAXINT,
502 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT,
503 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
505 g_object_class_install_property (gobject_class,
506 PROP_RC_2PASS_VBR_MAXSECTION_PCT,
507 g_param_spec_int ("twopass-vbr-maxsection", "2-pass GOP max bitrate",
508 "GOP maximum bitrate (% target)", 0, G_MAXINT,
509 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT,
510 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
512 g_object_class_install_property (gobject_class, PROP_KF_MODE,
513 g_param_spec_enum ("keyframe-mode", "Keyframe Mode",
514 "Keyframe placement",
515 GST_VP9_ENC_KF_MODE_TYPE, DEFAULT_KF_MODE,
516 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
518 g_object_class_install_property (gobject_class, PROP_KF_MAX_DIST,
519 g_param_spec_int ("keyframe-max-dist", "Keyframe max distance",
520 "Maximum distance between keyframes (number of frames)",
521 0, G_MAXINT, DEFAULT_KF_MAX_DIST,
522 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
524 g_object_class_install_property (gobject_class, PROP_MULTIPASS_MODE,
525 g_param_spec_enum ("multipass-mode", "Multipass Mode",
526 "Multipass encode mode",
527 GST_VP9_ENC_MULTIPASS_MODE_TYPE, DEFAULT_MULTIPASS_MODE,
528 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
530 g_object_class_install_property (gobject_class, PROP_MULTIPASS_CACHE_FILE,
531 g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
532 "Multipass cache file. "
533 "If stream caps reinited, multiple files will be created: "
534 "file, file.1, file.2, ... and so on.",
535 DEFAULT_MULTIPASS_CACHE_FILE,
536 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
538 g_object_class_install_property (gobject_class, PROP_TS_NUMBER_LAYERS,
539 g_param_spec_int ("temporal-scalability-number-layers",
540 "Number of coding layers", "Number of coding layers to use", 1, 5,
541 DEFAULT_TS_NUMBER_LAYERS,
542 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
544 g_object_class_install_property (gobject_class, PROP_TS_TARGET_BITRATE,
545 g_param_spec_value_array ("temporal-scalability-target-bitrate",
546 "Coding layer target bitrates",
547 "Target bitrates for coding layers (one per layer, decreasing)",
548 g_param_spec_int ("target-bitrate", "Target bitrate",
549 "Target bitrate", 0, G_MAXINT, DEFAULT_RC_TARGET_BITRATE,
550 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
551 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
553 g_object_class_install_property (gobject_class, PROP_TS_RATE_DECIMATOR,
554 g_param_spec_value_array ("temporal-scalability-rate-decimator",
555 "Coding layer rate decimator",
556 "Rate decimation factors for each layer",
557 g_param_spec_int ("rate-decimator", "Rate decimator",
558 "Rate decimator", 0, 1000000000, 0,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
560 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
562 g_object_class_install_property (gobject_class, PROP_TS_PERIODICITY,
563 g_param_spec_int ("temporal-scalability-periodicity",
564 "Coding layer periodicity",
565 "Length of sequence that defines layer membership periodicity", 0, 16,
566 DEFAULT_TS_PERIODICITY,
567 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
569 g_object_class_install_property (gobject_class, PROP_TS_LAYER_ID,
570 g_param_spec_value_array ("temporal-scalability-layer-id",
571 "Coding layer identification",
572 "Sequence defining coding layer membership",
573 g_param_spec_int ("layer-id", "Layer ID", "Layer ID", 0, 4, 0,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
575 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 g_object_class_install_property (gobject_class, PROP_LAG_IN_FRAMES,
578 g_param_spec_int ("lag-in-frames", "Lag in frames",
579 "Maximum number of frames to lag",
580 0, 25, DEFAULT_LAG_IN_FRAMES,
581 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
583 g_object_class_install_property (gobject_class, PROP_ERROR_RESILIENT,
584 g_param_spec_flags ("error-resilient", "Error resilient",
585 "Error resilience flags",
586 GST_VP9_ENC_ER_FLAGS_TYPE, DEFAULT_ERROR_RESILIENT,
587 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
589 g_object_class_install_property (gobject_class, PROP_THREADS,
590 g_param_spec_int ("threads", "Threads",
591 "Number of threads to use",
592 0, 64, DEFAULT_THREADS,
593 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
595 g_object_class_install_property (gobject_class, PROP_DEADLINE,
596 g_param_spec_int64 ("deadline", "Deadline",
597 "Deadline per frame (usec, 0=disabled)",
598 0, G_MAXINT64, DEFAULT_DEADLINE,
599 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
601 g_object_class_install_property (gobject_class, PROP_H_SCALING_MODE,
602 g_param_spec_enum ("horizontal-scaling-mode", "Horizontal scaling mode",
603 "Horizontal scaling mode",
604 GST_VP9_ENC_SCALING_MODE_TYPE, DEFAULT_H_SCALING_MODE,
605 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
607 g_object_class_install_property (gobject_class, PROP_V_SCALING_MODE,
608 g_param_spec_enum ("vertical-scaling-mode", "Vertical scaling mode",
609 "Vertical scaling mode",
610 GST_VP9_ENC_SCALING_MODE_TYPE, DEFAULT_V_SCALING_MODE,
611 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
613 g_object_class_install_property (gobject_class, PROP_CPU_USED,
614 g_param_spec_int ("cpu-used", "CPU used",
616 -16, 16, DEFAULT_CPU_USED,
617 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
619 g_object_class_install_property (gobject_class, PROP_ENABLE_AUTO_ALT_REF,
620 g_param_spec_boolean ("auto-alt-ref", "Auto alt reference frames",
621 "Automatically generate AltRef frames",
622 DEFAULT_ENABLE_AUTO_ALT_REF,
623 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
625 g_object_class_install_property (gobject_class, PROP_NOISE_SENSITIVITY,
626 g_param_spec_int ("noise-sensitivity", "Noise sensitivity",
627 "Noise sensisivity (frames to blur)",
628 0, 6, DEFAULT_NOISE_SENSITIVITY,
629 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
631 g_object_class_install_property (gobject_class, PROP_SHARPNESS,
632 g_param_spec_int ("sharpness", "Sharpness",
634 0, 7, DEFAULT_SHARPNESS,
635 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
637 g_object_class_install_property (gobject_class, PROP_STATIC_THRESHOLD,
638 g_param_spec_int ("static-threshold", "Static Threshold",
639 "Motion detection threshold",
640 0, G_MAXINT, DEFAULT_STATIC_THRESHOLD,
641 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
643 g_object_class_install_property (gobject_class, PROP_TOKEN_PARTITIONS,
644 g_param_spec_enum ("token-partitions", "Token partitions",
645 "Number of token partitions",
646 GST_VP9_ENC_TOKEN_PARTITIONS_TYPE, DEFAULT_TOKEN_PARTITIONS,
647 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
649 g_object_class_install_property (gobject_class, PROP_ARNR_MAXFRAMES,
650 g_param_spec_int ("arnr-maxframes", "AltRef max frames",
651 "AltRef maximum number of frames",
652 0, 15, DEFAULT_ARNR_MAXFRAMES,
653 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
655 g_object_class_install_property (gobject_class, PROP_ARNR_STRENGTH,
656 g_param_spec_int ("arnr-strength", "AltRef strength",
658 0, 6, DEFAULT_ARNR_STRENGTH,
659 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
661 g_object_class_install_property (gobject_class, PROP_ARNR_TYPE,
662 g_param_spec_int ("arnr-type", "AltRef type",
664 1, 3, DEFAULT_ARNR_TYPE,
665 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
666 G_PARAM_DEPRECATED)));
668 g_object_class_install_property (gobject_class, PROP_TUNING,
669 g_param_spec_enum ("tuning", "Tuning",
671 GST_VP9_ENC_TUNING_TYPE, DEFAULT_TUNING,
672 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
674 g_object_class_install_property (gobject_class, PROP_CQ_LEVEL,
675 g_param_spec_int ("cq-level", "Constrained quality level",
676 "Constrained quality level",
677 0, 63, DEFAULT_CQ_LEVEL,
678 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
680 g_object_class_install_property (gobject_class, PROP_MAX_INTRA_BITRATE_PCT,
681 g_param_spec_int ("max-intra-bitrate", "Max Intra bitrate",
682 "Maximum Intra frame bitrate",
683 0, G_MAXINT, DEFAULT_MAX_INTRA_BITRATE_PCT,
684 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
686 g_object_class_install_property (gobject_class, PROP_TIMEBASE,
687 gst_param_spec_fraction ("timebase", "Shortest interframe time",
688 "Fraction of one second that is the shortest interframe time - normally left as zero which will default to the framerate",
689 0, 1, G_MAXINT, 1, DEFAULT_TIMEBASE_N, DEFAULT_TIMEBASE_D,
690 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
692 GST_DEBUG_CATEGORY_INIT (gst_vp9enc_debug, "vp9enc", 0, "VP9 Encoder");
696 gst_vp9_enc_init (GstVP9Enc * gst_vp9_enc)
698 vpx_codec_err_t status;
700 GST_DEBUG_OBJECT (gst_vp9_enc, "init");
703 vpx_codec_enc_config_default (&vpx_codec_vp9_cx_algo, &gst_vp9_enc->cfg,
705 if (status != VPX_CODEC_OK) {
706 GST_ERROR_OBJECT (gst_vp9_enc,
707 "Failed to get default encoder configuration: %s",
708 gst_vpx_error_name (status));
709 gst_vp9_enc->have_default_config = FALSE;
711 gst_vp9_enc->have_default_config = TRUE;
714 gst_vp9_enc->cfg.rc_end_usage = DEFAULT_RC_END_USAGE;
715 gst_vp9_enc->cfg.rc_target_bitrate = DEFAULT_RC_TARGET_BITRATE / 1000;
716 gst_vp9_enc->rc_target_bitrate_set = FALSE;
717 gst_vp9_enc->cfg.rc_min_quantizer = DEFAULT_RC_MIN_QUANTIZER;
718 gst_vp9_enc->cfg.rc_max_quantizer = DEFAULT_RC_MAX_QUANTIZER;
719 gst_vp9_enc->cfg.rc_dropframe_thresh = DEFAULT_RC_DROPFRAME_THRESH;
720 gst_vp9_enc->cfg.rc_resize_allowed = DEFAULT_RC_RESIZE_ALLOWED;
721 gst_vp9_enc->cfg.rc_resize_up_thresh = DEFAULT_RC_RESIZE_UP_THRESH;
722 gst_vp9_enc->cfg.rc_resize_down_thresh = DEFAULT_RC_RESIZE_DOWN_THRESH;
723 gst_vp9_enc->cfg.rc_undershoot_pct = DEFAULT_RC_UNDERSHOOT_PCT;
724 gst_vp9_enc->cfg.rc_overshoot_pct = DEFAULT_RC_OVERSHOOT_PCT;
725 gst_vp9_enc->cfg.rc_buf_sz = DEFAULT_RC_BUF_SZ;
726 gst_vp9_enc->cfg.rc_buf_initial_sz = DEFAULT_RC_BUF_INITIAL_SZ;
727 gst_vp9_enc->cfg.rc_buf_optimal_sz = DEFAULT_RC_BUF_OPTIMAL_SZ;
728 gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct = DEFAULT_RC_2PASS_VBR_BIAS_PCT;
729 gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct =
730 DEFAULT_RC_2PASS_VBR_MINSECTION_PCT;
731 gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct =
732 DEFAULT_RC_2PASS_VBR_MAXSECTION_PCT;
733 gst_vp9_enc->cfg.kf_mode = DEFAULT_KF_MODE;
734 gst_vp9_enc->cfg.kf_max_dist = DEFAULT_KF_MAX_DIST;
735 gst_vp9_enc->cfg.g_pass = DEFAULT_MULTIPASS_MODE;
736 gst_vp9_enc->multipass_cache_prefix = g_strdup (DEFAULT_MULTIPASS_CACHE_FILE);
737 gst_vp9_enc->multipass_cache_file = NULL;
738 gst_vp9_enc->multipass_cache_idx = 0;
739 gst_vp9_enc->cfg.ts_number_layers = DEFAULT_TS_NUMBER_LAYERS;
740 gst_vp9_enc->n_ts_target_bitrate = 0;
741 gst_vp9_enc->n_ts_rate_decimator = 0;
742 gst_vp9_enc->cfg.ts_periodicity = DEFAULT_TS_PERIODICITY;
743 gst_vp9_enc->n_ts_layer_id = 0;
744 gst_vp9_enc->cfg.g_error_resilient = DEFAULT_ERROR_RESILIENT;
745 gst_vp9_enc->cfg.g_lag_in_frames = DEFAULT_LAG_IN_FRAMES;
746 gst_vp9_enc->cfg.g_threads = DEFAULT_THREADS;
747 gst_vp9_enc->deadline = DEFAULT_DEADLINE;
748 gst_vp9_enc->h_scaling_mode = DEFAULT_H_SCALING_MODE;
749 gst_vp9_enc->v_scaling_mode = DEFAULT_V_SCALING_MODE;
750 gst_vp9_enc->cpu_used = DEFAULT_CPU_USED;
751 gst_vp9_enc->enable_auto_alt_ref = DEFAULT_ENABLE_AUTO_ALT_REF;
752 gst_vp9_enc->noise_sensitivity = DEFAULT_NOISE_SENSITIVITY;
753 gst_vp9_enc->sharpness = DEFAULT_SHARPNESS;
754 gst_vp9_enc->static_threshold = DEFAULT_STATIC_THRESHOLD;
755 gst_vp9_enc->token_partitions = DEFAULT_TOKEN_PARTITIONS;
756 gst_vp9_enc->arnr_maxframes = DEFAULT_ARNR_MAXFRAMES;
757 gst_vp9_enc->arnr_strength = DEFAULT_ARNR_STRENGTH;
758 gst_vp9_enc->arnr_type = DEFAULT_ARNR_TYPE;
759 gst_vp9_enc->tuning = DEFAULT_TUNING;
760 gst_vp9_enc->cq_level = DEFAULT_CQ_LEVEL;
761 gst_vp9_enc->max_intra_bitrate_pct = DEFAULT_MAX_INTRA_BITRATE_PCT;
762 gst_vp9_enc->timebase_n = DEFAULT_TIMEBASE_N;
763 gst_vp9_enc->timebase_d = DEFAULT_TIMEBASE_D;
765 gst_vp9_enc->cfg.g_profile = DEFAULT_PROFILE;
767 g_mutex_init (&gst_vp9_enc->encoder_lock);
771 gst_vp9_enc_finalize (GObject * object)
773 GstVP9Enc *gst_vp9_enc;
775 GST_DEBUG_OBJECT (object, "finalize");
777 g_return_if_fail (GST_IS_VP9_ENC (object));
778 gst_vp9_enc = GST_VP9_ENC (object);
780 g_free (gst_vp9_enc->multipass_cache_prefix);
781 g_free (gst_vp9_enc->multipass_cache_file);
782 gst_vp9_enc->multipass_cache_idx = 0;
784 if (gst_vp9_enc->input_state)
785 gst_video_codec_state_unref (gst_vp9_enc->input_state);
787 g_mutex_clear (&gst_vp9_enc->encoder_lock);
789 G_OBJECT_CLASS (parent_class)->finalize (object);
793 gst_vp9_enc_set_property (GObject * object, guint prop_id,
794 const GValue * value, GParamSpec * pspec)
796 GstVP9Enc *gst_vp9_enc;
797 gboolean global = FALSE;
798 vpx_codec_err_t status;
800 g_return_if_fail (GST_IS_VP9_ENC (object));
801 gst_vp9_enc = GST_VP9_ENC (object);
803 GST_DEBUG_OBJECT (object, "gst_vp9_enc_set_property");
804 g_mutex_lock (&gst_vp9_enc->encoder_lock);
806 case PROP_RC_END_USAGE:
807 gst_vp9_enc->cfg.rc_end_usage = g_value_get_enum (value);
810 case PROP_RC_TARGET_BITRATE:
811 gst_vp9_enc->cfg.rc_target_bitrate = g_value_get_int (value) / 1000;
812 gst_vp9_enc->rc_target_bitrate_set = TRUE;
815 case PROP_RC_MIN_QUANTIZER:
816 gst_vp9_enc->cfg.rc_min_quantizer = g_value_get_int (value);
819 case PROP_RC_MAX_QUANTIZER:
820 gst_vp9_enc->cfg.rc_max_quantizer = g_value_get_int (value);
823 case PROP_RC_DROPFRAME_THRESH:
824 gst_vp9_enc->cfg.rc_dropframe_thresh = g_value_get_int (value);
827 case PROP_RC_RESIZE_ALLOWED:
828 gst_vp9_enc->cfg.rc_resize_allowed = g_value_get_boolean (value);
831 case PROP_RC_RESIZE_UP_THRESH:
832 gst_vp9_enc->cfg.rc_resize_up_thresh = g_value_get_int (value);
835 case PROP_RC_RESIZE_DOWN_THRESH:
836 gst_vp9_enc->cfg.rc_resize_down_thresh = g_value_get_int (value);
839 case PROP_RC_UNDERSHOOT_PCT:
840 gst_vp9_enc->cfg.rc_undershoot_pct = g_value_get_int (value);
843 case PROP_RC_OVERSHOOT_PCT:
844 gst_vp9_enc->cfg.rc_overshoot_pct = g_value_get_int (value);
848 gst_vp9_enc->cfg.rc_buf_sz = g_value_get_int (value);
851 case PROP_RC_BUF_INITIAL_SZ:
852 gst_vp9_enc->cfg.rc_buf_initial_sz = g_value_get_int (value);
855 case PROP_RC_BUF_OPTIMAL_SZ:
856 gst_vp9_enc->cfg.rc_buf_optimal_sz = g_value_get_int (value);
859 case PROP_RC_2PASS_VBR_BIAS_PCT:
860 gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct = g_value_get_int (value);
863 case PROP_RC_2PASS_VBR_MINSECTION_PCT:
864 gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct = g_value_get_int (value);
867 case PROP_RC_2PASS_VBR_MAXSECTION_PCT:
868 gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct = g_value_get_int (value);
872 gst_vp9_enc->cfg.kf_mode = g_value_get_enum (value);
875 case PROP_KF_MAX_DIST:
876 gst_vp9_enc->cfg.kf_max_dist = g_value_get_int (value);
879 case PROP_MULTIPASS_MODE:
880 gst_vp9_enc->cfg.g_pass = g_value_get_enum (value);
883 case PROP_MULTIPASS_CACHE_FILE:
884 if (gst_vp9_enc->multipass_cache_prefix)
885 g_free (gst_vp9_enc->multipass_cache_prefix);
886 gst_vp9_enc->multipass_cache_prefix = g_value_dup_string (value);
888 case PROP_TS_NUMBER_LAYERS:
889 gst_vp9_enc->cfg.ts_number_layers = g_value_get_int (value);
892 case PROP_TS_TARGET_BITRATE:{
893 GValueArray *va = g_value_get_boxed (value);
895 memset (&gst_vp9_enc->cfg.ts_target_bitrate, 0,
896 sizeof (gst_vp9_enc->cfg.ts_target_bitrate));
898 gst_vp9_enc->n_ts_target_bitrate = 0;
900 if (va->n_values > VPX_TS_MAX_LAYERS) {
901 g_warning ("%s: Only %d layers allowed at maximum",
902 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
906 for (i = 0; i < va->n_values; i++)
907 gst_vp9_enc->cfg.ts_target_bitrate[i] =
908 g_value_get_int (g_value_array_get_nth (va, i));
909 gst_vp9_enc->n_ts_target_bitrate = va->n_values;
915 case PROP_TS_RATE_DECIMATOR:{
916 GValueArray *va = g_value_get_boxed (value);
918 memset (&gst_vp9_enc->cfg.ts_rate_decimator, 0,
919 sizeof (gst_vp9_enc->cfg.ts_rate_decimator));
921 gst_vp9_enc->n_ts_rate_decimator = 0;
922 } else if (va->n_values > VPX_TS_MAX_LAYERS) {
923 g_warning ("%s: Only %d layers allowed at maximum",
924 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_LAYERS);
928 for (i = 0; i < va->n_values; i++)
929 gst_vp9_enc->cfg.ts_rate_decimator[i] =
930 g_value_get_int (g_value_array_get_nth (va, i));
931 gst_vp9_enc->n_ts_rate_decimator = va->n_values;
936 case PROP_TS_PERIODICITY:
937 gst_vp9_enc->cfg.ts_periodicity = g_value_get_int (value);
940 case PROP_TS_LAYER_ID:{
941 GValueArray *va = g_value_get_boxed (value);
943 memset (&gst_vp9_enc->cfg.ts_layer_id, 0,
944 sizeof (gst_vp9_enc->cfg.ts_layer_id));
945 if (va && va->n_values > VPX_TS_MAX_PERIODICITY) {
946 g_warning ("%s: Only %d sized layer sequences allowed at maximum",
947 GST_ELEMENT_NAME (gst_vp9_enc), VPX_TS_MAX_PERIODICITY);
951 for (i = 0; i < va->n_values; i++)
952 gst_vp9_enc->cfg.ts_layer_id[i] =
953 g_value_get_int (g_value_array_get_nth (va, i));
954 gst_vp9_enc->n_ts_layer_id = va->n_values;
956 gst_vp9_enc->n_ts_layer_id = 0;
961 case PROP_ERROR_RESILIENT:
962 gst_vp9_enc->cfg.g_error_resilient = g_value_get_flags (value);
965 case PROP_LAG_IN_FRAMES:
966 gst_vp9_enc->cfg.g_lag_in_frames = g_value_get_int (value);
970 gst_vp9_enc->cfg.g_threads = g_value_get_int (value);
974 gst_vp9_enc->deadline = g_value_get_int64 (value);
976 case PROP_H_SCALING_MODE:
977 gst_vp9_enc->h_scaling_mode = g_value_get_enum (value);
978 if (gst_vp9_enc->inited) {
979 vpx_scaling_mode_t sm;
981 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
982 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
985 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
986 if (status != VPX_CODEC_OK) {
987 GST_WARNING_OBJECT (gst_vp9_enc,
988 "Failed to set VP8E_SET_SCALEMODE: %s",
989 gst_vpx_error_name (status));
993 case PROP_V_SCALING_MODE:
994 gst_vp9_enc->v_scaling_mode = g_value_get_enum (value);
995 if (gst_vp9_enc->inited) {
996 vpx_scaling_mode_t sm;
998 sm.h_scaling_mode = gst_vp9_enc->h_scaling_mode;
999 sm.v_scaling_mode = gst_vp9_enc->v_scaling_mode;
1002 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SCALEMODE, &sm);
1003 if (status != VPX_CODEC_OK) {
1004 GST_WARNING_OBJECT (gst_vp9_enc,
1005 "Failed to set VP8E_SET_SCALEMODE: %s",
1006 gst_vpx_error_name (status));
1011 gst_vp9_enc->cpu_used = g_value_get_int (value);
1012 if (gst_vp9_enc->inited) {
1014 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CPUUSED,
1015 gst_vp9_enc->cpu_used);
1016 if (status != VPX_CODEC_OK) {
1017 GST_WARNING_OBJECT (gst_vp9_enc, "Failed to set VP8E_SET_CPUUSED: %s",
1018 gst_vpx_error_name (status));
1022 case PROP_ENABLE_AUTO_ALT_REF:
1023 gst_vp9_enc->enable_auto_alt_ref = g_value_get_boolean (value);
1024 if (gst_vp9_enc->inited) {
1026 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ENABLEAUTOALTREF,
1027 (gst_vp9_enc->enable_auto_alt_ref ? 1 : 0));
1028 if (status != VPX_CODEC_OK) {
1029 GST_WARNING_OBJECT (gst_vp9_enc,
1030 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1031 gst_vpx_error_name (status));
1035 case PROP_NOISE_SENSITIVITY:
1036 gst_vp9_enc->noise_sensitivity = g_value_get_int (value);
1037 if (gst_vp9_enc->inited) {
1039 vpx_codec_control (&gst_vp9_enc->encoder,
1040 VP8E_SET_NOISE_SENSITIVITY, gst_vp9_enc->noise_sensitivity);
1041 if (status != VPX_CODEC_OK) {
1042 GST_WARNING_OBJECT (gst_vp9_enc,
1043 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1044 gst_vpx_error_name (status));
1048 case PROP_SHARPNESS:
1049 gst_vp9_enc->sharpness = g_value_get_int (value);
1050 if (gst_vp9_enc->inited) {
1051 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_SHARPNESS,
1052 gst_vp9_enc->sharpness);
1053 if (status != VPX_CODEC_OK) {
1054 GST_WARNING_OBJECT (gst_vp9_enc,
1055 "Failed to set VP8E_SET_SHARPNESS: %s",
1056 gst_vpx_error_name (status));
1060 case PROP_STATIC_THRESHOLD:
1061 gst_vp9_enc->static_threshold = g_value_get_int (value);
1062 if (gst_vp9_enc->inited) {
1064 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_STATIC_THRESHOLD,
1065 gst_vp9_enc->static_threshold);
1066 if (status != VPX_CODEC_OK) {
1067 GST_WARNING_OBJECT (gst_vp9_enc,
1068 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1069 gst_vpx_error_name (status));
1073 case PROP_TOKEN_PARTITIONS:
1074 gst_vp9_enc->token_partitions = g_value_get_enum (value);
1075 if (gst_vp9_enc->inited) {
1077 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TOKEN_PARTITIONS,
1078 gst_vp9_enc->token_partitions);
1079 if (status != VPX_CODEC_OK) {
1080 GST_WARNING_OBJECT (gst_vp9_enc,
1081 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1082 gst_vpx_error_name (status));
1086 case PROP_ARNR_MAXFRAMES:
1087 gst_vp9_enc->arnr_maxframes = g_value_get_int (value);
1088 if (gst_vp9_enc->inited) {
1090 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_MAXFRAMES,
1091 gst_vp9_enc->arnr_maxframes);
1092 if (status != VPX_CODEC_OK) {
1093 GST_WARNING_OBJECT (gst_vp9_enc,
1094 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1095 gst_vpx_error_name (status));
1099 case PROP_ARNR_STRENGTH:
1100 gst_vp9_enc->arnr_strength = g_value_get_int (value);
1101 if (gst_vp9_enc->inited) {
1103 vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_ARNR_STRENGTH,
1104 gst_vp9_enc->arnr_strength);
1105 if (status != VPX_CODEC_OK) {
1106 GST_WARNING_OBJECT (gst_vp9_enc,
1107 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1108 gst_vpx_error_name (status));
1112 case PROP_ARNR_TYPE:
1113 gst_vp9_enc->arnr_type = g_value_get_int (value);
1114 g_warning ("arnr-type is a no-op since control has been deprecated "
1118 gst_vp9_enc->tuning = g_value_get_enum (value);
1119 if (gst_vp9_enc->inited) {
1120 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_TUNING,
1121 gst_vp9_enc->tuning);
1122 if (status != VPX_CODEC_OK) {
1123 GST_WARNING_OBJECT (gst_vp9_enc,
1124 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1129 gst_vp9_enc->cq_level = g_value_get_int (value);
1130 if (gst_vp9_enc->inited) {
1131 status = vpx_codec_control (&gst_vp9_enc->encoder, VP8E_SET_CQ_LEVEL,
1132 gst_vp9_enc->cq_level);
1133 if (status != VPX_CODEC_OK) {
1134 GST_WARNING_OBJECT (gst_vp9_enc,
1135 "Failed to set VP8E_SET_CQ_LEVEL: %s",
1136 gst_vpx_error_name (status));
1140 case PROP_MAX_INTRA_BITRATE_PCT:
1141 gst_vp9_enc->max_intra_bitrate_pct = g_value_get_int (value);
1142 if (gst_vp9_enc->inited) {
1144 vpx_codec_control (&gst_vp9_enc->encoder,
1145 VP8E_SET_MAX_INTRA_BITRATE_PCT, gst_vp9_enc->max_intra_bitrate_pct);
1146 if (status != VPX_CODEC_OK) {
1147 GST_WARNING_OBJECT (gst_vp9_enc,
1148 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1149 gst_vpx_error_name (status));
1154 gst_vp9_enc->timebase_n = gst_value_get_fraction_numerator (value);
1155 gst_vp9_enc->timebase_d = gst_value_get_fraction_denominator (value);
1161 if (global &&gst_vp9_enc->inited) {
1163 vpx_codec_enc_config_set (&gst_vp9_enc->encoder, &gst_vp9_enc->cfg);
1164 if (status != VPX_CODEC_OK) {
1165 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1166 GST_ELEMENT_ERROR (gst_vp9_enc, LIBRARY, INIT,
1167 ("Failed to set encoder configuration"), ("%s",
1168 gst_vpx_error_name (status)));
1170 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1173 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1178 gst_vp9_enc_get_property (GObject * object, guint prop_id, GValue * value,
1181 GstVP9Enc *gst_vp9_enc;
1183 g_return_if_fail (GST_IS_VP9_ENC (object));
1184 gst_vp9_enc = GST_VP9_ENC (object);
1186 g_mutex_lock (&gst_vp9_enc->encoder_lock);
1188 case PROP_RC_END_USAGE:
1189 g_value_set_enum (value, gst_vp9_enc->cfg.rc_end_usage);
1191 case PROP_RC_TARGET_BITRATE:
1192 g_value_set_int (value, gst_vp9_enc->cfg.rc_target_bitrate * 1000);
1194 case PROP_RC_MIN_QUANTIZER:
1195 g_value_set_int (value, gst_vp9_enc->cfg.rc_min_quantizer);
1197 case PROP_RC_MAX_QUANTIZER:
1198 g_value_set_int (value, gst_vp9_enc->cfg.rc_max_quantizer);
1200 case PROP_RC_DROPFRAME_THRESH:
1201 g_value_set_int (value, gst_vp9_enc->cfg.rc_dropframe_thresh);
1203 case PROP_RC_RESIZE_ALLOWED:
1204 g_value_set_boolean (value, gst_vp9_enc->cfg.rc_resize_allowed);
1206 case PROP_RC_RESIZE_UP_THRESH:
1207 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_up_thresh);
1209 case PROP_RC_RESIZE_DOWN_THRESH:
1210 g_value_set_int (value, gst_vp9_enc->cfg.rc_resize_down_thresh);
1212 case PROP_RC_UNDERSHOOT_PCT:
1213 g_value_set_int (value, gst_vp9_enc->cfg.rc_undershoot_pct);
1215 case PROP_RC_OVERSHOOT_PCT:
1216 g_value_set_int (value, gst_vp9_enc->cfg.rc_overshoot_pct);
1218 case PROP_RC_BUF_SZ:
1219 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_sz);
1221 case PROP_RC_BUF_INITIAL_SZ:
1222 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_initial_sz);
1224 case PROP_RC_BUF_OPTIMAL_SZ:
1225 g_value_set_int (value, gst_vp9_enc->cfg.rc_buf_optimal_sz);
1227 case PROP_RC_2PASS_VBR_BIAS_PCT:
1228 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_bias_pct);
1230 case PROP_RC_2PASS_VBR_MINSECTION_PCT:
1231 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_minsection_pct);
1233 case PROP_RC_2PASS_VBR_MAXSECTION_PCT:
1234 g_value_set_int (value, gst_vp9_enc->cfg.rc_2pass_vbr_maxsection_pct);
1237 g_value_set_enum (value, gst_vp9_enc->cfg.kf_mode);
1239 case PROP_KF_MAX_DIST:
1240 g_value_set_int (value, gst_vp9_enc->cfg.kf_max_dist);
1242 case PROP_MULTIPASS_MODE:
1243 g_value_set_enum (value, gst_vp9_enc->cfg.g_pass);
1245 case PROP_MULTIPASS_CACHE_FILE:
1246 g_value_set_string (value, gst_vp9_enc->multipass_cache_prefix);
1248 case PROP_TS_NUMBER_LAYERS:
1249 g_value_set_int (value, gst_vp9_enc->cfg.ts_number_layers);
1251 case PROP_TS_TARGET_BITRATE:{
1254 if (gst_vp9_enc->n_ts_target_bitrate == 0) {
1255 g_value_set_boxed (value, NULL);
1259 va = g_value_array_new (gst_vp9_enc->n_ts_target_bitrate);
1260 for (i = 0; i < gst_vp9_enc->n_ts_target_bitrate; i++) {
1263 g_value_init (&v, G_TYPE_INT);
1264 g_value_set_int (&v, gst_vp9_enc->cfg.ts_target_bitrate[i]);
1265 g_value_array_append (va, &v);
1268 g_value_set_boxed (value, va);
1269 g_value_array_free (va);
1273 case PROP_TS_RATE_DECIMATOR:{
1276 if (gst_vp9_enc->n_ts_rate_decimator == 0) {
1277 g_value_set_boxed (value, NULL);
1281 va = g_value_array_new (gst_vp9_enc->n_ts_rate_decimator);
1282 for (i = 0; i < gst_vp9_enc->n_ts_rate_decimator; i++) {
1285 g_value_init (&v, G_TYPE_INT);
1286 g_value_set_int (&v, gst_vp9_enc->cfg.ts_rate_decimator[i]);
1287 g_value_array_append (va, &v);
1290 g_value_set_boxed (value, va);
1291 g_value_array_free (va);
1295 case PROP_TS_PERIODICITY:
1296 g_value_set_int (value, gst_vp9_enc->cfg.ts_periodicity);
1298 case PROP_TS_LAYER_ID:{
1301 if (gst_vp9_enc->n_ts_layer_id == 0) {
1302 g_value_set_boxed (value, NULL);
1306 va = g_value_array_new (gst_vp9_enc->n_ts_layer_id);
1307 for (i = 0; i < gst_vp9_enc->n_ts_layer_id; i++) {
1310 g_value_init (&v, G_TYPE_INT);
1311 g_value_set_int (&v, gst_vp9_enc->cfg.ts_layer_id[i]);
1312 g_value_array_append (va, &v);
1315 g_value_set_boxed (value, va);
1316 g_value_array_free (va);
1320 case PROP_ERROR_RESILIENT:
1321 g_value_set_flags (value, gst_vp9_enc->cfg.g_error_resilient);
1323 case PROP_LAG_IN_FRAMES:
1324 g_value_set_int (value, gst_vp9_enc->cfg.g_lag_in_frames);
1327 g_value_set_int (value, gst_vp9_enc->cfg.g_threads);
1330 g_value_set_int64 (value, gst_vp9_enc->deadline);
1332 case PROP_H_SCALING_MODE:
1333 g_value_set_enum (value, gst_vp9_enc->h_scaling_mode);
1335 case PROP_V_SCALING_MODE:
1336 g_value_set_enum (value, gst_vp9_enc->v_scaling_mode);
1339 g_value_set_int (value, gst_vp9_enc->cpu_used);
1341 case PROP_ENABLE_AUTO_ALT_REF:
1342 g_value_set_boolean (value, gst_vp9_enc->enable_auto_alt_ref);
1344 case PROP_NOISE_SENSITIVITY:
1345 g_value_set_int (value, gst_vp9_enc->noise_sensitivity);
1347 case PROP_SHARPNESS:
1348 g_value_set_int (value, gst_vp9_enc->sharpness);
1350 case PROP_STATIC_THRESHOLD:
1351 g_value_set_int (value, gst_vp9_enc->static_threshold);
1353 case PROP_TOKEN_PARTITIONS:
1354 g_value_set_enum (value, gst_vp9_enc->token_partitions);
1356 case PROP_ARNR_MAXFRAMES:
1357 g_value_set_int (value, gst_vp9_enc->arnr_maxframes);
1359 case PROP_ARNR_STRENGTH:
1360 g_value_set_int (value, gst_vp9_enc->arnr_strength);
1362 case PROP_ARNR_TYPE:
1363 g_value_set_int (value, gst_vp9_enc->arnr_type);
1366 g_value_set_enum (value, gst_vp9_enc->tuning);
1369 g_value_set_int (value, gst_vp9_enc->cq_level);
1371 case PROP_MAX_INTRA_BITRATE_PCT:
1372 g_value_set_int (value, gst_vp9_enc->max_intra_bitrate_pct);
1375 gst_value_set_fraction (value, gst_vp9_enc->timebase_n,
1376 gst_vp9_enc->timebase_d);
1379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1383 g_mutex_unlock (&gst_vp9_enc->encoder_lock);
1387 gst_vp9_enc_start (GstVideoEncoder * video_encoder)
1389 GstVP9Enc *encoder = GST_VP9_ENC (video_encoder);
1391 GST_DEBUG_OBJECT (video_encoder, "start");
1393 if (!encoder->have_default_config) {
1394 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1395 ("Failed to get default encoder configuration"), (NULL));
1403 gst_vp9_enc_destroy_encoder (GstVP9Enc * encoder)
1405 g_mutex_lock (&encoder->encoder_lock);
1406 if (encoder->inited) {
1407 vpx_codec_destroy (&encoder->encoder);
1408 encoder->inited = FALSE;
1411 if (encoder->first_pass_cache_content) {
1412 g_byte_array_free (encoder->first_pass_cache_content, TRUE);
1413 encoder->first_pass_cache_content = NULL;
1416 if (encoder->cfg.rc_twopass_stats_in.buf) {
1417 g_free (encoder->cfg.rc_twopass_stats_in.buf);
1418 encoder->cfg.rc_twopass_stats_in.buf = NULL;
1419 encoder->cfg.rc_twopass_stats_in.sz = 0;
1421 g_mutex_unlock (&encoder->encoder_lock);
1425 gst_vp9_enc_stop (GstVideoEncoder * video_encoder)
1429 GST_DEBUG_OBJECT (video_encoder, "stop");
1431 encoder = GST_VP9_ENC (video_encoder);
1433 gst_vp9_enc_destroy_encoder (encoder);
1435 gst_tag_setter_reset_tags (GST_TAG_SETTER (encoder));
1437 g_free (encoder->multipass_cache_file);
1438 encoder->multipass_cache_file = NULL;
1439 encoder->multipass_cache_idx = 0;
1445 gst_vp9_enc_get_downstream_profile (GstVP9Enc * encoder)
1449 gint profile = DEFAULT_PROFILE;
1451 allowed = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1453 allowed = gst_caps_truncate (allowed);
1454 s = gst_caps_get_structure (allowed, 0);
1455 if (gst_structure_has_field (s, "profile")) {
1456 const GValue *v = gst_structure_get_value (s, "profile");
1457 const gchar *profile_str = NULL;
1459 if (GST_VALUE_HOLDS_LIST (v) && gst_value_list_get_size (v) > 0) {
1460 profile_str = g_value_get_string (gst_value_list_get_value (v, 0));
1461 } else if (G_VALUE_HOLDS_STRING (v)) {
1462 profile_str = g_value_get_string (v);
1466 gchar *endptr = NULL;
1468 profile = g_ascii_strtoull (profile_str, &endptr, 10);
1469 if (*endptr != '\0' || profile < 0 || profile > 3) {
1470 GST_ERROR_OBJECT (encoder, "Invalid profile '%s'", profile_str);
1471 profile = DEFAULT_PROFILE;
1475 gst_caps_unref (allowed);
1478 GST_DEBUG_OBJECT (encoder, "Using profile %d", profile);
1484 gst_vp9_enc_set_format (GstVideoEncoder * video_encoder,
1485 GstVideoCodecState * state)
1488 vpx_codec_err_t status;
1491 gboolean ret = TRUE;
1492 GstVideoInfo *info = &state->info;
1493 GstVideoCodecState *output_state;
1495 GstClockTime latency;
1497 encoder = GST_VP9_ENC (video_encoder);
1498 GST_DEBUG_OBJECT (video_encoder, "set_format");
1500 if (encoder->inited) {
1501 gst_vp9_enc_drain (video_encoder);
1502 g_mutex_lock (&encoder->encoder_lock);
1503 vpx_codec_destroy (&encoder->encoder);
1504 encoder->inited = FALSE;
1505 encoder->multipass_cache_idx++;
1507 g_mutex_lock (&encoder->encoder_lock);
1510 encoder->cfg.g_profile = gst_vp9_enc_get_downstream_profile (encoder);
1512 /* Scale default bitrate to our size */
1513 if (!encoder->rc_target_bitrate_set)
1514 encoder->cfg.rc_target_bitrate =
1515 gst_util_uint64_scale (DEFAULT_RC_TARGET_BITRATE,
1516 GST_VIDEO_INFO_WIDTH (info) * GST_VIDEO_INFO_HEIGHT (info),
1519 encoder->cfg.g_w = GST_VIDEO_INFO_WIDTH (info);
1520 encoder->cfg.g_h = GST_VIDEO_INFO_HEIGHT (info);
1522 if (encoder->timebase_n != 0 && encoder->timebase_d != 0) {
1523 GST_DEBUG_OBJECT (video_encoder, "Using timebase configuration");
1524 encoder->cfg.g_timebase.num = encoder->timebase_n;
1525 encoder->cfg.g_timebase.den = encoder->timebase_d;
1527 /* Zero framerate and max-framerate but still need to setup the timebase to avoid
1528 * a divide by zero error. Presuming the lowest common denominator will be RTP -
1529 * VP9 payload draft states clock rate of 90000 which should work for anyone where
1530 * FPS < 90000 (shouldn't be too many cases where it's higher) though wouldn't be optimal. RTP specification
1531 * http://tools.ietf.org/html/draft-ietf-payload-vp9-01 section 6.3.1 */
1532 encoder->cfg.g_timebase.num = 1;
1533 encoder->cfg.g_timebase.den = 90000;
1536 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS ||
1537 encoder->cfg.g_pass == VPX_RC_LAST_PASS) {
1538 if (!encoder->multipass_cache_prefix) {
1539 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1540 ("No multipass cache file provided"), (NULL));
1541 g_mutex_unlock (&encoder->encoder_lock);
1545 g_free (encoder->multipass_cache_file);
1547 if (encoder->multipass_cache_idx > 0)
1548 encoder->multipass_cache_file = g_strdup_printf ("%s.%u",
1549 encoder->multipass_cache_prefix, encoder->multipass_cache_idx);
1551 encoder->multipass_cache_file =
1552 g_strdup (encoder->multipass_cache_prefix);
1555 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1556 if (encoder->first_pass_cache_content != NULL)
1557 g_byte_array_free (encoder->first_pass_cache_content, TRUE);
1559 encoder->first_pass_cache_content = g_byte_array_sized_new (4096);
1561 } else if (encoder->cfg.g_pass == VPX_RC_LAST_PASS) {
1564 if (encoder->cfg.rc_twopass_stats_in.buf != NULL) {
1565 g_free (encoder->cfg.rc_twopass_stats_in.buf);
1566 encoder->cfg.rc_twopass_stats_in.buf = NULL;
1567 encoder->cfg.rc_twopass_stats_in.sz = 0;
1570 if (!g_file_get_contents (encoder->multipass_cache_file,
1571 (gchar **) & encoder->cfg.rc_twopass_stats_in.buf,
1572 &encoder->cfg.rc_twopass_stats_in.sz, &err)) {
1573 GST_ELEMENT_ERROR (encoder, RESOURCE, OPEN_READ,
1574 ("Failed to read multipass cache file provided"), ("%s",
1577 g_mutex_unlock (&encoder->encoder_lock);
1582 status = vpx_codec_enc_init (&encoder->encoder, &vpx_codec_vp9_cx_algo,
1584 if (status != VPX_CODEC_OK) {
1585 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT,
1586 ("Failed to initialize encoder"), ("%s", gst_vpx_error_name (status)));
1587 g_mutex_unlock (&encoder->encoder_lock);
1591 /* FIXME: Disabled for now, does not work with VP9 */
1594 vpx_scaling_mode_t sm;
1596 sm.h_scaling_mode = encoder->h_scaling_mode;
1597 sm.v_scaling_mode = encoder->v_scaling_mode;
1599 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SCALEMODE, &sm);
1600 if (status != VPX_CODEC_OK) {
1601 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_SCALEMODE: %s",
1602 gst_vpx_error_name (status));
1608 vpx_codec_control (&encoder->encoder, VP8E_SET_CPUUSED,
1610 if (status != VPX_CODEC_OK) {
1611 GST_WARNING_OBJECT (encoder, "Failed to set VP8E_SET_CPUUSED: %s",
1612 gst_vpx_error_name (status));
1616 vpx_codec_control (&encoder->encoder, VP8E_SET_ENABLEAUTOALTREF,
1617 (encoder->enable_auto_alt_ref ? 1 : 0));
1618 if (status != VPX_CODEC_OK) {
1619 GST_WARNING_OBJECT (encoder,
1620 "Failed to set VP8E_SET_ENABLEAUTOALTREF: %s",
1621 gst_vpx_error_name (status));
1623 status = vpx_codec_control (&encoder->encoder, VP8E_SET_NOISE_SENSITIVITY,
1624 encoder->noise_sensitivity);
1625 if (status != VPX_CODEC_OK) {
1626 GST_WARNING_OBJECT (encoder,
1627 "Failed to set VP8E_SET_NOISE_SENSITIVITY: %s",
1628 gst_vpx_error_name (status));
1630 status = vpx_codec_control (&encoder->encoder, VP8E_SET_SHARPNESS,
1631 encoder->sharpness);
1632 if (status != VPX_CODEC_OK) {
1633 GST_WARNING_OBJECT (encoder,
1634 "Failed to set VP8E_SET_SHARPNESS: %s", gst_vpx_error_name (status));
1636 status = vpx_codec_control (&encoder->encoder, VP8E_SET_STATIC_THRESHOLD,
1637 encoder->static_threshold);
1638 if (status != VPX_CODEC_OK) {
1639 GST_WARNING_OBJECT (encoder,
1640 "Failed to set VP8E_SET_STATIC_THRESHOLD: %s",
1641 gst_vpx_error_name (status));
1643 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TOKEN_PARTITIONS,
1644 encoder->token_partitions);
1645 if (status != VPX_CODEC_OK) {
1646 GST_WARNING_OBJECT (encoder,
1647 "Failed to set VP8E_SET_TOKEN_PARTIONS: %s",
1648 gst_vpx_error_name (status));
1650 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_MAXFRAMES,
1651 encoder->arnr_maxframes);
1652 if (status != VPX_CODEC_OK) {
1653 GST_WARNING_OBJECT (encoder,
1654 "Failed to set VP8E_SET_ARNR_MAXFRAMES: %s",
1655 gst_vpx_error_name (status));
1657 status = vpx_codec_control (&encoder->encoder, VP8E_SET_ARNR_STRENGTH,
1658 encoder->arnr_strength);
1659 if (status != VPX_CODEC_OK) {
1660 GST_WARNING_OBJECT (encoder,
1661 "Failed to set VP8E_SET_ARNR_STRENGTH: %s",
1662 gst_vpx_error_name (status));
1664 status = vpx_codec_control (&encoder->encoder, VP8E_SET_TUNING,
1666 if (status != VPX_CODEC_OK) {
1667 GST_WARNING_OBJECT (encoder,
1668 "Failed to set VP8E_SET_TUNING: %s", gst_vpx_error_name (status));
1670 status = vpx_codec_control (&encoder->encoder, VP8E_SET_CQ_LEVEL,
1672 if (status != VPX_CODEC_OK) {
1673 GST_WARNING_OBJECT (encoder,
1674 "Failed to set VP8E_SET_CQ_LEVEL: %s", gst_vpx_error_name (status));
1676 status = vpx_codec_control (&encoder->encoder, VP8E_SET_MAX_INTRA_BITRATE_PCT,
1677 encoder->max_intra_bitrate_pct);
1678 if (status != VPX_CODEC_OK) {
1679 GST_WARNING_OBJECT (encoder,
1680 "Failed to set VP8E_SET_MAX_INTRA_BITRATE_PCT: %s",
1681 gst_vpx_error_name (status));
1684 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
1685 /* FIXME: Assume 25fps for unknown framerates. Better than reporting
1686 * that we introduce no latency while we actually do
1688 latency = gst_util_uint64_scale (encoder->cfg.g_lag_in_frames,
1689 1 * GST_SECOND, 25);
1691 latency = gst_util_uint64_scale (encoder->cfg.g_lag_in_frames,
1692 GST_VIDEO_INFO_FPS_D (info) * GST_SECOND, GST_VIDEO_INFO_FPS_N (info));
1694 gst_video_encoder_set_latency (video_encoder, latency, latency);
1695 encoder->inited = TRUE;
1697 /* Store input state */
1698 if (encoder->input_state)
1699 gst_video_codec_state_unref (encoder->input_state);
1700 encoder->input_state = gst_video_codec_state_ref (state);
1702 /* prepare cached image buffer setup */
1703 image = &encoder->image;
1704 memset (image, 0, sizeof (*image));
1706 switch (encoder->input_state->info.finfo->format) {
1707 case GST_VIDEO_FORMAT_I420:
1708 image->fmt = VPX_IMG_FMT_I420;
1710 image->x_chroma_shift = image->y_chroma_shift = 1;
1712 case GST_VIDEO_FORMAT_YV12:
1713 image->fmt = VPX_IMG_FMT_YV12;
1715 image->x_chroma_shift = image->y_chroma_shift = 1;
1717 case GST_VIDEO_FORMAT_Y42B:
1718 image->fmt = VPX_IMG_FMT_I422;
1720 image->x_chroma_shift = 1;
1721 image->y_chroma_shift = 0;
1723 case GST_VIDEO_FORMAT_Y444:
1724 image->fmt = VPX_IMG_FMT_I444;
1726 image->x_chroma_shift = image->y_chroma_shift = 0;
1729 g_assert_not_reached ();
1732 image->w = image->d_w = GST_VIDEO_INFO_WIDTH (info);
1733 image->h = image->d_h = GST_VIDEO_INFO_HEIGHT (info);
1735 image->stride[VPX_PLANE_Y] = GST_VIDEO_INFO_COMP_STRIDE (info, 0);
1736 image->stride[VPX_PLANE_U] = GST_VIDEO_INFO_COMP_STRIDE (info, 1);
1737 image->stride[VPX_PLANE_V] = GST_VIDEO_INFO_COMP_STRIDE (info, 2);
1739 profile_str = g_strdup_printf ("%d", encoder->cfg.g_profile);
1740 caps = gst_caps_new_simple ("video/x-vp9",
1741 "profile", G_TYPE_STRING, profile_str, NULL);
1742 g_free (profile_str);
1744 g_mutex_unlock (&encoder->encoder_lock);
1747 gst_video_encoder_set_output_state (video_encoder, caps, state);
1748 gst_video_codec_state_unref (output_state);
1750 gst_video_encoder_negotiate (GST_VIDEO_ENCODER (encoder));
1755 static GstFlowReturn
1756 gst_vp9_enc_process (GstVP9Enc * encoder)
1758 vpx_codec_iter_t iter = NULL;
1759 const vpx_codec_cx_pkt_t *pkt;
1760 GstVideoEncoder *video_encoder;
1761 GstVideoCodecFrame *frame;
1762 GstFlowReturn ret = GST_FLOW_OK;
1764 video_encoder = GST_VIDEO_ENCODER (encoder);
1766 g_mutex_lock (&encoder->encoder_lock);
1767 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1768 while (pkt != NULL) {
1772 GST_DEBUG_OBJECT (encoder, "packet %u type %d", (guint) pkt->data.frame.sz,
1775 if (pkt->kind == VPX_CODEC_STATS_PKT
1776 && encoder->cfg.g_pass == VPX_RC_FIRST_PASS) {
1777 GST_LOG_OBJECT (encoder, "handling STATS packet");
1779 g_byte_array_append (encoder->first_pass_cache_content,
1780 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1782 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1783 if (frame != NULL) {
1784 buffer = gst_buffer_new ();
1785 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_LIVE);
1786 frame->output_buffer = buffer;
1787 g_mutex_unlock (&encoder->encoder_lock);
1788 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1789 g_mutex_lock (&encoder->encoder_lock);
1792 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1794 } else if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) {
1795 GST_LOG_OBJECT (encoder, "non frame pkt: %d", pkt->kind);
1796 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1800 invisible = (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE) != 0;
1801 frame = gst_video_encoder_get_oldest_frame (video_encoder);
1802 g_assert (frame != NULL);
1803 if ((pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0)
1804 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
1806 GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
1808 /* FIXME : It would be nice to avoid the memory copy ... */
1810 gst_buffer_new_wrapped (g_memdup (pkt->data.frame.buf,
1811 pkt->data.frame.sz), pkt->data.frame.sz);
1814 g_mutex_unlock (&encoder->encoder_lock);
1815 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (encoder), buffer);
1816 g_mutex_lock (&encoder->encoder_lock);
1817 gst_video_codec_frame_unref (frame);
1819 frame->output_buffer = buffer;
1820 g_mutex_unlock (&encoder->encoder_lock);
1821 ret = gst_video_encoder_finish_frame (video_encoder, frame);
1822 g_mutex_lock (&encoder->encoder_lock);
1825 pkt = vpx_codec_get_cx_data (&encoder->encoder, &iter);
1827 g_mutex_unlock (&encoder->encoder_lock);
1832 /* This function should be called holding then stream lock*/
1833 static GstFlowReturn
1834 gst_vp9_enc_drain (GstVideoEncoder * video_encoder)
1838 vpx_codec_err_t status;
1840 vpx_codec_pts_t pts;
1842 encoder = GST_VP9_ENC (video_encoder);
1844 g_mutex_lock (&encoder->encoder_lock);
1845 deadline = encoder->deadline;
1848 gst_util_uint64_scale (encoder->last_pts,
1849 encoder->cfg.g_timebase.den,
1850 encoder->cfg.g_timebase.num * (GstClockTime) GST_SECOND);
1852 status = vpx_codec_encode (&encoder->encoder, NULL, pts, 0, flags, deadline);
1853 g_mutex_unlock (&encoder->encoder_lock);
1856 GST_ERROR_OBJECT (encoder, "encode returned %d %s", status,
1857 gst_vpx_error_name (status));
1858 return GST_FLOW_ERROR;
1861 /* dispatch remaining frames */
1862 gst_vp9_enc_process (encoder);
1864 g_mutex_lock (&encoder->encoder_lock);
1865 if (encoder->cfg.g_pass == VPX_RC_FIRST_PASS && encoder->multipass_cache_file) {
1868 if (!g_file_set_contents (encoder->multipass_cache_file,
1869 (const gchar *) encoder->first_pass_cache_content->data,
1870 encoder->first_pass_cache_content->len, &err)) {
1871 GST_ELEMENT_ERROR (encoder, RESOURCE, WRITE, (NULL),
1872 ("Failed to write multipass cache file: %s", err->message));
1876 g_mutex_unlock (&encoder->encoder_lock);
1882 gst_vp9_enc_flush (GstVideoEncoder * video_encoder)
1886 GST_DEBUG_OBJECT (video_encoder, "flush");
1888 encoder = GST_VP9_ENC (video_encoder);
1890 gst_vp9_enc_destroy_encoder (encoder);
1891 if (encoder->input_state) {
1892 gst_video_codec_state_ref (encoder->input_state);
1893 gst_vp9_enc_set_format (video_encoder, encoder->input_state);
1894 gst_video_codec_state_unref (encoder->input_state);
1900 static GstFlowReturn
1901 gst_vp9_enc_finish (GstVideoEncoder * video_encoder)
1906 GST_DEBUG_OBJECT (video_encoder, "finish");
1908 encoder = GST_VP9_ENC (video_encoder);
1910 if (encoder->inited) {
1911 ret = gst_vp9_enc_drain (video_encoder);
1919 static vpx_image_t *
1920 gst_vp9_enc_buffer_to_image (GstVP9Enc * enc, GstVideoFrame * frame)
1922 vpx_image_t *image = g_slice_new (vpx_image_t);
1924 memcpy (image, &enc->image, sizeof (*image));
1926 image->planes[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
1927 image->planes[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
1928 image->planes[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
1930 image->stride[VPX_PLANE_Y] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
1931 image->stride[VPX_PLANE_U] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1);
1932 image->stride[VPX_PLANE_V] = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2);
1937 static GstFlowReturn
1938 gst_vp9_enc_handle_frame (GstVideoEncoder * video_encoder,
1939 GstVideoCodecFrame * frame)
1942 vpx_codec_err_t status;
1945 GstVideoFrame vframe;
1946 vpx_codec_pts_t pts;
1947 unsigned long duration;
1949 GST_DEBUG_OBJECT (video_encoder, "handle_frame");
1951 encoder = GST_VP9_ENC (video_encoder);
1953 GST_DEBUG_OBJECT (video_encoder, "size %d %d",
1954 GST_VIDEO_INFO_WIDTH (&encoder->input_state->info),
1955 GST_VIDEO_INFO_HEIGHT (&encoder->input_state->info));
1957 gst_video_frame_map (&vframe, &encoder->input_state->info,
1958 frame->input_buffer, GST_MAP_READ);
1959 image = gst_vp9_enc_buffer_to_image (encoder, &vframe);
1961 if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame)) {
1962 flags |= VPX_EFLAG_FORCE_KF;
1965 g_mutex_lock (&encoder->encoder_lock);
1967 gst_util_uint64_scale (frame->pts,
1968 encoder->cfg.g_timebase.den,
1969 encoder->cfg.g_timebase.num * (GstClockTime) GST_SECOND);
1970 encoder->last_pts = frame->pts;
1972 if (frame->duration != GST_CLOCK_TIME_NONE) {
1974 gst_util_uint64_scale (frame->duration, encoder->cfg.g_timebase.den,
1975 encoder->cfg.g_timebase.num * (GstClockTime) GST_SECOND);
1976 encoder->last_pts += frame->duration;
1981 status = vpx_codec_encode (&encoder->encoder, image,
1982 pts, duration, flags, encoder->deadline);
1984 g_mutex_unlock (&encoder->encoder_lock);
1985 gst_video_frame_unmap (&vframe);
1988 GST_ELEMENT_ERROR (encoder, LIBRARY, ENCODE,
1989 ("Failed to encode frame"), ("%s", gst_vpx_error_name (status)));
1992 gst_video_codec_frame_unref (frame);
1993 return gst_vp9_enc_process (encoder);
1997 gst_vp9_enc_sink_event (GstVideoEncoder * benc, GstEvent * event)
1999 GstVP9Enc *enc = GST_VP9_ENC (benc);
2001 /* FIXME : Move this to base encoder class */
2003 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
2005 GstTagSetter *setter = GST_TAG_SETTER (enc);
2006 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
2008 gst_event_parse_tag (event, &list);
2009 gst_tag_setter_merge_tags (setter, list, mode);
2012 /* just peeked, baseclass handles the rest */
2013 return GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
2017 gst_vp9_enc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
2019 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2021 return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
2025 #endif /* HAVE_VP9_ENCODER */