2 * Copyright (C) 2006 David Schleef <ds@schleef.org>
3 * Copyright (C) 2010 Entropy Wave Inc
4 * Copyright (C) 2010-2012 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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-vp8enc
24 * @see_also: vp8dec, webmmux, oggmux
26 * This element encodes raw video into a VP8 stream.
27 * <ulink url="http://www.webmproject.org">VP8</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 #GstVP8Enc::target-bitrate,
33 * #GstVP8Enc::min-quantizer, #GstVP8Enc::max-quantizer or #GstVP8Enc::cq-level
34 * properties can be used. Which one is used depends on the mode selected by
35 * the #GstVP8Enc::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 ! vp8enc ! webmmux ! filesink location=videotestsrc.webm
44 * ]| This example pipeline will encode a test video source to VP8 muxed in an
53 #ifdef HAVE_VP8_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 "gstvp8enc.h"
69 GST_DEBUG_CATEGORY_STATIC (gst_vp8enc_debug);
70 #define GST_CAT_DEFAULT gst_vp8enc_debug
79 _gst_mini_object_unref0 (GstMiniObject * obj)
82 gst_mini_object_unref (obj);
86 gst_vp8_enc_user_data_free (GstVP8EncUserData * user_data)
89 g_slice_free (vpx_image_t, user_data->image);
91 g_list_foreach (user_data->invisible, (GFunc) _gst_mini_object_unref0, NULL);
92 g_list_free (user_data->invisible);
93 g_slice_free (GstVP8EncUserData, user_data);
96 static vpx_codec_iface_t *gst_vp8_enc_get_algo (GstVPXEnc * enc);
97 static gboolean gst_vp8_enc_enable_scaling (GstVPXEnc * enc);
98 static void gst_vp8_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image);
99 static GstCaps *gst_vp8_enc_get_new_simple_caps (GstVPXEnc * enc);
100 static void gst_vp8_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
101 GstVideoInfo * info);
102 static void *gst_vp8_enc_process_frame_user_data (GstVPXEnc * enc,
103 GstVideoCodecFrame * frame);
104 static GstFlowReturn gst_vp8_enc_handle_invisible_frame_buffer (GstVPXEnc * enc,
105 void *user_data, GstBuffer * buffer);
106 static void gst_vp8_enc_set_frame_user_data (GstVPXEnc * enc,
107 GstVideoCodecFrame * frame, vpx_image_t * image);
109 static GstFlowReturn gst_vp8_enc_pre_push (GstVideoEncoder * encoder,
110 GstVideoCodecFrame * frame);
112 static GstStaticPadTemplate gst_vp8_enc_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink",
116 GST_STATIC_CAPS ("video/x-raw, "
117 "format = (string) \"I420\", "
118 "width = (int) [1, 16383], "
119 "height = (int) [1, 16383], framerate = (fraction) [ 0/1, MAX ]")
122 static GstStaticPadTemplate gst_vp8_enc_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
126 GST_STATIC_CAPS ("video/x-vp8, " "profile = (string) {0, 1, 2, 3}")
129 #define parent_class gst_vp8_enc_parent_class
130 G_DEFINE_TYPE (GstVP8Enc, gst_vp8_enc, GST_TYPE_VPX_ENC);
133 gst_vp8_enc_class_init (GstVP8EncClass * klass)
135 GstElementClass *element_class;
136 GstVideoEncoderClass *video_encoder_class;
137 GstVPXEncClass *vpx_encoder_class;
139 element_class = GST_ELEMENT_CLASS (klass);
140 video_encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
141 vpx_encoder_class = GST_VPX_ENC_CLASS (klass);
144 gst_element_class_add_static_pad_template (element_class,
145 &gst_vp8_enc_src_template);
146 gst_element_class_add_static_pad_template (element_class,
147 &gst_vp8_enc_sink_template);
149 gst_element_class_set_static_metadata (element_class,
151 "Codec/Encoder/Video",
152 "Encode VP8 video streams", "David Schleef <ds@entropywave.com>, "
153 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
155 video_encoder_class->pre_push = gst_vp8_enc_pre_push;
157 vpx_encoder_class->get_algo = gst_vp8_enc_get_algo;
158 vpx_encoder_class->enable_scaling = gst_vp8_enc_enable_scaling;
159 vpx_encoder_class->set_image_format = gst_vp8_enc_set_image_format;
160 vpx_encoder_class->get_new_vpx_caps = gst_vp8_enc_get_new_simple_caps;
161 vpx_encoder_class->set_stream_info = gst_vp8_enc_set_stream_info;
162 vpx_encoder_class->process_frame_user_data =
163 gst_vp8_enc_process_frame_user_data;
164 vpx_encoder_class->handle_invisible_frame_buffer =
165 gst_vp8_enc_handle_invisible_frame_buffer;
166 vpx_encoder_class->set_frame_user_data = gst_vp8_enc_set_frame_user_data;
168 GST_DEBUG_CATEGORY_INIT (gst_vp8enc_debug, "vp8enc", 0, "VP8 Encoder");
172 gst_vp8_enc_init (GstVP8Enc * gst_vp8_enc)
174 vpx_codec_err_t status;
175 GstVPXEnc *gst_vpx_enc = GST_VPX_ENC (gst_vp8_enc);
176 GST_DEBUG_OBJECT (gst_vp8_enc, "gst_vp8_enc_init");
178 vpx_codec_enc_config_default (gst_vp8_enc_get_algo (gst_vpx_enc),
179 &gst_vpx_enc->cfg, 0);
180 if (status != VPX_CODEC_OK) {
181 GST_ERROR_OBJECT (gst_vpx_enc,
182 "Failed to get default encoder configuration: %s",
183 gst_vpx_error_name (status));
184 gst_vpx_enc->have_default_config = FALSE;
186 gst_vpx_enc->have_default_config = TRUE;
190 static vpx_codec_iface_t *
191 gst_vp8_enc_get_algo (GstVPXEnc * enc)
193 return &vpx_codec_vp8_cx_algo;
197 gst_vp8_enc_enable_scaling (GstVPXEnc * enc)
203 gst_vp8_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
205 image->fmt = VPX_IMG_FMT_I420;
207 image->x_chroma_shift = image->y_chroma_shift = 1;
211 gst_vp8_enc_get_new_simple_caps (GstVPXEnc * enc)
214 gchar *profile_str = g_strdup_printf ("%d", enc->cfg.g_profile);
215 caps = gst_caps_new_simple ("video/x-vp8",
216 "profile", G_TYPE_STRING, profile_str, NULL);
217 g_free (profile_str);
222 gst_vp8_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
226 GstVideoEncoder *video_encoder;
227 GstBuffer *stream_hdr, *vorbiscomment;
228 const GstTagList *iface_tags;
229 GValue array = { 0, };
230 GValue value = { 0, };
234 video_encoder = GST_VIDEO_ENCODER (enc);
235 s = gst_caps_get_structure (caps, 0);
237 /* put buffers in a fixed list */
238 g_value_init (&array, GST_TYPE_ARRAY);
239 g_value_init (&value, GST_TYPE_BUFFER);
241 /* Create Ogg stream-info */
242 stream_hdr = gst_buffer_new_and_alloc (26);
243 gst_buffer_map (stream_hdr, &map, GST_MAP_WRITE);
246 GST_WRITE_UINT8 (data, 0x4F);
247 GST_WRITE_UINT32_BE (data + 1, 0x56503830); /* "VP80" */
248 GST_WRITE_UINT8 (data + 5, 0x01); /* stream info header */
249 GST_WRITE_UINT8 (data + 6, 1); /* Major version 1 */
250 GST_WRITE_UINT8 (data + 7, 0); /* Minor version 0 */
251 GST_WRITE_UINT16_BE (data + 8, GST_VIDEO_INFO_WIDTH (info));
252 GST_WRITE_UINT16_BE (data + 10, GST_VIDEO_INFO_HEIGHT (info));
253 GST_WRITE_UINT24_BE (data + 12, GST_VIDEO_INFO_PAR_N (info));
254 GST_WRITE_UINT24_BE (data + 15, GST_VIDEO_INFO_PAR_D (info));
255 GST_WRITE_UINT32_BE (data + 18, GST_VIDEO_INFO_FPS_N (info));
256 GST_WRITE_UINT32_BE (data + 22, GST_VIDEO_INFO_FPS_D (info));
258 gst_buffer_unmap (stream_hdr, &map);
260 GST_BUFFER_FLAG_SET (stream_hdr, GST_BUFFER_FLAG_HEADER);
261 gst_value_set_buffer (&value, stream_hdr);
262 gst_value_array_append_value (&array, &value);
263 g_value_unset (&value);
264 gst_buffer_unref (stream_hdr);
266 iface_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (video_encoder));
269 gst_tag_list_to_vorbiscomment_buffer (iface_tags,
270 (const guint8 *) "OVP80\2 ", 7,
271 "Encoded with GStreamer vp8enc " PACKAGE_VERSION);
273 GST_BUFFER_FLAG_SET (vorbiscomment, GST_BUFFER_FLAG_HEADER);
275 g_value_init (&value, GST_TYPE_BUFFER);
276 gst_value_set_buffer (&value, vorbiscomment);
277 gst_value_array_append_value (&array, &value);
278 g_value_unset (&value);
279 gst_buffer_unref (vorbiscomment);
282 gst_structure_set_value (s, "streamheader", &array);
283 g_value_unset (&array);
288 gst_vp8_enc_process_frame_user_data (GstVPXEnc * enc,
289 GstVideoCodecFrame * frame)
291 GstVP8EncUserData *user_data;
293 user_data = gst_video_codec_frame_get_user_data (frame);
296 GST_ERROR_OBJECT (enc, "Have no frame user data");
300 if (user_data->image)
301 g_slice_free (vpx_image_t, user_data->image);
302 user_data->image = NULL;
307 gst_vp8_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
310 GstVP8EncUserData *vp8_user_data = (GstVP8EncUserData *) user_data;
312 if (!vp8_user_data) {
313 GST_ERROR_OBJECT (enc, "Have no frame user data");
314 return GST_FLOW_ERROR;
317 vp8_user_data->invisible = g_list_append (vp8_user_data->invisible, buffer);
323 gst_vp8_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
326 GstVP8EncUserData *user_data;
327 user_data = g_slice_new0 (GstVP8EncUserData);
328 user_data->image = image;
329 gst_video_codec_frame_set_user_data (frame, user_data,
330 (GDestroyNotify) gst_vp8_enc_user_data_free);
335 _to_granulepos (guint64 frame_end_number, guint inv_count, guint keyframe_dist)
340 inv = (inv_count == 0) ? 0x3 : inv_count - 1;
342 granulepos = (frame_end_number << 32) | (inv << 30) | (keyframe_dist << 3);
347 gst_vp8_enc_pre_push (GstVideoEncoder * video_encoder,
348 GstVideoCodecFrame * frame)
353 GstFlowReturn ret = GST_FLOW_OK;
354 GstVP8EncUserData *user_data = gst_video_codec_frame_get_user_data (frame);
359 GST_DEBUG_OBJECT (video_encoder, "pre_push");
361 encoder = GST_VP8_ENC (video_encoder);
362 vpx_enc = GST_VPX_ENC (encoder);
364 info = &vpx_enc->input_state->info;
366 g_assert (user_data != NULL);
368 for (inv_count = 0, l = user_data->invisible; l; inv_count++, l = l->next) {
372 /* FIXME : All of this should have already been handled by base classes, no ? */
373 if (l == user_data->invisible
374 && GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
375 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
376 encoder->keyframe_distance = 0;
378 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
379 encoder->keyframe_distance++;
382 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DECODE_ONLY);
383 GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_TIMESTAMP (frame->output_buffer);
384 GST_BUFFER_DURATION (buf) = 0;
385 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
386 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
387 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
389 GST_BUFFER_OFFSET_END (buf) =
390 _to_granulepos (frame->presentation_frame_number + 1,
391 inv_count, encoder->keyframe_distance);
392 GST_BUFFER_OFFSET (buf) =
393 gst_util_uint64_scale (frame->presentation_frame_number + 1,
394 GST_SECOND * GST_VIDEO_INFO_FPS_D (info),
395 GST_VIDEO_INFO_FPS_N (info));
398 ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (video_encoder), buf);
400 if (ret != GST_FLOW_OK) {
401 GST_WARNING_OBJECT (encoder, "flow error %d", ret);
406 buf = frame->output_buffer;
408 /* FIXME : All of this should have already been handled by base classes, no ? */
409 if (!user_data->invisible && GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
410 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
411 encoder->keyframe_distance = 0;
413 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
414 encoder->keyframe_distance++;
417 if (GST_VIDEO_INFO_FPS_D (info) == 0 || GST_VIDEO_INFO_FPS_N (info) == 0) {
418 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
419 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
421 GST_BUFFER_OFFSET_END (buf) =
422 _to_granulepos (frame->presentation_frame_number + 1, 0,
423 encoder->keyframe_distance);
424 GST_BUFFER_OFFSET (buf) =
425 gst_util_uint64_scale (frame->presentation_frame_number + 1,
426 GST_SECOND * GST_VIDEO_INFO_FPS_D (info), GST_VIDEO_INFO_FPS_N (info));
429 GST_LOG_OBJECT (video_encoder, "src ts: %" GST_TIME_FORMAT,
430 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
436 #endif /* HAVE_VP8_ENCODER */