1 /* VP9
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>
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22 /**
23  * SECTION:element-vp9enc
24  * @see_also: vp9dec, webmmux, oggmux
25  *
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
30  * Theora video codec.
31  *
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.
39  *
40  * <refsect2>
41  * <title>Example pipeline</title>
42  * |[
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
45  * WebM container.
46  * </refsect2>
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #ifdef HAVE_VP9_ENCODER
54 
55 /* glib decided in 2.32 it would be a great idea to deprecated GValueArray without
56  * providing an alternative
57  *
58  * See https://bugzilla.gnome.org/show_bug.cgi?id=667228
59  * */
60 #define GLIB_DISABLE_DEPRECATION_WARNINGS
61 
62 #include <gst/tag/tag.h>
63 #include <gst/video/video.h>
64 #include <string.h>
65 
66 #include "gstvp8utils.h"
67 #include "gstvp9enc.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_vp9enc_debug);
70 #define GST_CAT_DEFAULT gst_vp9enc_debug
71 
72 
73 /* FIXME: Y42B and Y444 do not work yet it seems */
74 static GstStaticPadTemplate gst_vp9_enc_sink_template =
75 GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     /*GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444 }")) */
79     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12 }"))
80     );
81 
82 static GstStaticPadTemplate gst_vp9_enc_src_template =
83 GST_STATIC_PAD_TEMPLATE ("src",
84     GST_PAD_SRC,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS ("video/x-vp9, " "profile = (string) {0, 1, 2, 3}")
87     );
88 
89 #define parent_class gst_vp9_enc_parent_class
90 G_DEFINE_TYPE (GstVP9Enc, gst_vp9_enc, GST_TYPE_VPX_ENC);
91 
92 static vpx_codec_iface_t *gst_vp9_enc_get_algo (GstVPXEnc * enc);
93 static gboolean gst_vp9_enc_enable_scaling (GstVPXEnc * enc);
94 static void gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image);
95 static GstCaps *gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc);
96 static void gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
97     GstVideoInfo * info);
98 static void *gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
99     GstVideoCodecFrame * frame);
100 static GstFlowReturn gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc,
101     void *user_data, GstBuffer * buffer);
102 static void gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc,
103     GstVideoCodecFrame * frame, vpx_image_t * image);
104 
105 static void
gst_vp9_enc_class_init(GstVP9EncClass * klass)106 gst_vp9_enc_class_init (GstVP9EncClass * klass)
107 {
108   GstElementClass *element_class;
109   GstVPXEncClass *vpx_encoder_class;
110 
111   element_class = GST_ELEMENT_CLASS (klass);
112   vpx_encoder_class = GST_VPX_ENC_CLASS (klass);
113 
114   gst_element_class_add_static_pad_template (element_class,
115       &gst_vp9_enc_src_template);
116   gst_element_class_add_static_pad_template (element_class,
117       &gst_vp9_enc_sink_template);
118 
119   gst_element_class_set_static_metadata (element_class,
120       "On2 VP9 Encoder",
121       "Codec/Encoder/Video",
122       "Encode VP9 video streams", "David Schleef <ds@entropywave.com>, "
123       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
124 
125   vpx_encoder_class->get_algo = gst_vp9_enc_get_algo;
126   vpx_encoder_class->enable_scaling = gst_vp9_enc_enable_scaling;
127   vpx_encoder_class->set_image_format = gst_vp9_enc_set_image_format;
128   vpx_encoder_class->get_new_vpx_caps = gst_vp9_enc_get_new_simple_caps;
129   vpx_encoder_class->set_stream_info = gst_vp9_enc_set_stream_info;
130   vpx_encoder_class->process_frame_user_data =
131       gst_vp9_enc_process_frame_user_data;
132   vpx_encoder_class->handle_invisible_frame_buffer =
133       gst_vp9_enc_handle_invisible_frame_buffer;
134   vpx_encoder_class->set_frame_user_data = gst_vp9_enc_set_frame_user_data;
135 
136   GST_DEBUG_CATEGORY_INIT (gst_vp9enc_debug, "vp9enc", 0, "VP9 Encoder");
137 }
138 
139 static void
gst_vp9_enc_init(GstVP9Enc * gst_vp9_enc)140 gst_vp9_enc_init (GstVP9Enc * gst_vp9_enc)
141 {
142   vpx_codec_err_t status;
143   GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (gst_vp9_enc);
144   GST_DEBUG_OBJECT (gst_vp9_enc, "gst_vp9_enc_init");
145   status =
146       vpx_codec_enc_config_default (gst_vp9_enc_get_algo (gst_vpx_enc),
147       &gst_vpx_enc->cfg, 0);
148   if (status != VPX_CODEC_OK) {
149     GST_ERROR_OBJECT (gst_vpx_enc,
150         "Failed to get default encoder configuration: %s",
151         gst_vpx_error_name (status));
152     gst_vpx_enc->have_default_config = FALSE;
153   } else {
154     gst_vpx_enc->have_default_config = TRUE;
155   }
156 }
157 
158 static vpx_codec_iface_t *
gst_vp9_enc_get_algo(GstVPXEnc * enc)159 gst_vp9_enc_get_algo (GstVPXEnc * enc)
160 {
161   return &vpx_codec_vp9_cx_algo;
162 }
163 
164 static gboolean
gst_vp9_enc_enable_scaling(GstVPXEnc * enc)165 gst_vp9_enc_enable_scaling (GstVPXEnc * enc)
166 {
167   return FALSE;
168 }
169 
170 static void
gst_vp9_enc_set_image_format(GstVPXEnc * enc,vpx_image_t * image)171 gst_vp9_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
172 {
173   switch (enc->input_state->info.finfo->format) {
174     case GST_VIDEO_FORMAT_I420:
175       image->fmt = VPX_IMG_FMT_I420;
176       image->bps = 12;
177       image->x_chroma_shift = image->y_chroma_shift = 1;
178       break;
179     case GST_VIDEO_FORMAT_YV12:
180       image->fmt = VPX_IMG_FMT_YV12;
181       image->bps = 12;
182       image->x_chroma_shift = image->y_chroma_shift = 1;
183       break;
184     case GST_VIDEO_FORMAT_Y42B:
185       image->fmt = VPX_IMG_FMT_I422;
186       image->bps = 16;
187       image->x_chroma_shift = 1;
188       image->y_chroma_shift = 0;
189       break;
190     case GST_VIDEO_FORMAT_Y444:
191       image->fmt = VPX_IMG_FMT_I444;
192       image->bps = 24;
193       image->x_chroma_shift = image->y_chroma_shift = 0;
194       break;
195     default:
196       g_assert_not_reached ();
197       break;
198   }
199 }
200 
201 static GstCaps *
gst_vp9_enc_get_new_simple_caps(GstVPXEnc * enc)202 gst_vp9_enc_get_new_simple_caps (GstVPXEnc * enc)
203 {
204   GstCaps *caps;
205   gchar *profile_str = g_strdup_printf ("%d", enc->cfg.g_profile);
206   caps = gst_caps_new_simple ("video/x-vp9",
207       "profile", G_TYPE_STRING, profile_str, NULL);
208   g_free (profile_str);
209   return caps;
210 }
211 
212 static void
gst_vp9_enc_set_stream_info(GstVPXEnc * enc,GstCaps * caps,GstVideoInfo * info)213 gst_vp9_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
214     GstVideoInfo * info)
215 {
216   return;
217 }
218 
219 static void *
gst_vp9_enc_process_frame_user_data(GstVPXEnc * enc,GstVideoCodecFrame * frame)220 gst_vp9_enc_process_frame_user_data (GstVPXEnc * enc,
221     GstVideoCodecFrame * frame)
222 {
223   return NULL;
224 }
225 
226 static GstFlowReturn
gst_vp9_enc_handle_invisible_frame_buffer(GstVPXEnc * enc,void * user_data,GstBuffer * buffer)227 gst_vp9_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
228     GstBuffer * buffer)
229 {
230   GstFlowReturn ret;
231   g_mutex_unlock (&enc->encoder_lock);
232   ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (enc), buffer);
233   g_mutex_lock (&enc->encoder_lock);
234   return ret;
235 }
236 
237 static void
gst_vp9_enc_user_data_free(vpx_image_t * image)238 gst_vp9_enc_user_data_free (vpx_image_t * image)
239 {
240   g_slice_free (vpx_image_t, image);
241 }
242 
243 static void
gst_vp9_enc_set_frame_user_data(GstVPXEnc * enc,GstVideoCodecFrame * frame,vpx_image_t * image)244 gst_vp9_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
245     vpx_image_t * image)
246 {
247   gst_video_codec_frame_set_user_data (frame, image,
248       (GDestroyNotify) gst_vp9_enc_user_data_free);
249   return;
250 }
251 
252 #endif /* HAVE_VP9_ENCODER */
253