1 /* VP8
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>
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-vp8enc
24  * @see_also: vp8dec, webmmux, oggmux
25  *
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
30  * Theora video codec.
31  *
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.
39  *
40  * <refsect2>
41  * <title>Example pipeline</title>
42  * |[
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
45  * WebM container.
46  * </refsect2>
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #ifdef HAVE_VP8_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 "gstvp8enc.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_vp8enc_debug);
70 #define GST_CAT_DEFAULT gst_vp8enc_debug
71 
72 typedef struct
73 {
74   vpx_image_t *image;
75   GList *invisible;
76 } GstVP8EncUserData;
77 
78 static void
_gst_mini_object_unref0(GstMiniObject * obj)79 _gst_mini_object_unref0 (GstMiniObject * obj)
80 {
81   if (obj)
82     gst_mini_object_unref (obj);
83 }
84 
85 static void
gst_vp8_enc_user_data_free(GstVP8EncUserData * user_data)86 gst_vp8_enc_user_data_free (GstVP8EncUserData * user_data)
87 {
88   if (user_data->image)
89     g_slice_free (vpx_image_t, user_data->image);
90 
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);
94 }
95 
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);
108 
109 static GstFlowReturn gst_vp8_enc_pre_push (GstVideoEncoder * encoder,
110     GstVideoCodecFrame * frame);
111 
112 static GstStaticPadTemplate gst_vp8_enc_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink",
114     GST_PAD_SINK,
115     GST_PAD_ALWAYS,
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 ]")
120     );
121 
122 static GstStaticPadTemplate gst_vp8_enc_src_template =
123 GST_STATIC_PAD_TEMPLATE ("src",
124     GST_PAD_SRC,
125     GST_PAD_ALWAYS,
126     GST_STATIC_CAPS ("video/x-vp8, " "profile = (string) {0, 1, 2, 3}")
127     );
128 
129 #define parent_class gst_vp8_enc_parent_class
130 G_DEFINE_TYPE (GstVP8Enc, gst_vp8_enc, GST_TYPE_VPX_ENC);
131 
132 static void
gst_vp8_enc_class_init(GstVP8EncClass * klass)133 gst_vp8_enc_class_init (GstVP8EncClass * klass)
134 {
135   GstElementClass *element_class;
136   GstVideoEncoderClass *video_encoder_class;
137   GstVPXEncClass *vpx_encoder_class;
138 
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);
142 
143 
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);
148 
149   gst_element_class_set_static_metadata (element_class,
150       "On2 VP8 Encoder",
151       "Codec/Encoder/Video",
152       "Encode VP8 video streams", "David Schleef <ds@entropywave.com>, "
153       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
154 
155   video_encoder_class->pre_push = gst_vp8_enc_pre_push;
156 
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;
167 
168   GST_DEBUG_CATEGORY_INIT (gst_vp8enc_debug, "vp8enc", 0, "VP8 Encoder");
169 }
170 
171 static void
gst_vp8_enc_init(GstVP8Enc * gst_vp8_enc)172 gst_vp8_enc_init (GstVP8Enc * gst_vp8_enc)
173 {
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");
177   status =
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;
185   } else {
186     gst_vpx_enc->have_default_config = TRUE;
187   }
188 }
189 
190 static vpx_codec_iface_t *
gst_vp8_enc_get_algo(GstVPXEnc * enc)191 gst_vp8_enc_get_algo (GstVPXEnc * enc)
192 {
193   return &vpx_codec_vp8_cx_algo;
194 }
195 
196 static gboolean
gst_vp8_enc_enable_scaling(GstVPXEnc * enc)197 gst_vp8_enc_enable_scaling (GstVPXEnc * enc)
198 {
199   return TRUE;
200 }
201 
202 static void
gst_vp8_enc_set_image_format(GstVPXEnc * enc,vpx_image_t * image)203 gst_vp8_enc_set_image_format (GstVPXEnc * enc, vpx_image_t * image)
204 {
205   image->fmt = VPX_IMG_FMT_I420;
206   image->bps = 12;
207   image->x_chroma_shift = image->y_chroma_shift = 1;
208 }
209 
210 static GstCaps *
gst_vp8_enc_get_new_simple_caps(GstVPXEnc * enc)211 gst_vp8_enc_get_new_simple_caps (GstVPXEnc * enc)
212 {
213   GstCaps *caps;
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);
218   return caps;
219 }
220 
221 static void
gst_vp8_enc_set_stream_info(GstVPXEnc * enc,GstCaps * caps,GstVideoInfo * info)222 gst_vp8_enc_set_stream_info (GstVPXEnc * enc, GstCaps * caps,
223     GstVideoInfo * info)
224 {
225   GstStructure *s;
226   GstVideoEncoder *video_encoder;
227   GstBuffer *stream_hdr, *vorbiscomment;
228   const GstTagList *iface_tags;
229   GValue array = { 0, };
230   GValue value = { 0, };
231   guint8 *data = NULL;
232   GstMapInfo map;
233 
234   video_encoder = GST_VIDEO_ENCODER (enc);
235   s = gst_caps_get_structure (caps, 0);
236 
237   /* put buffers in a fixed list */
238   g_value_init (&array, GST_TYPE_ARRAY);
239   g_value_init (&value, GST_TYPE_BUFFER);
240 
241   /* Create Ogg stream-info */
242   stream_hdr = gst_buffer_new_and_alloc (26);
243   gst_buffer_map (stream_hdr, &map, GST_MAP_WRITE);
244   data = map.data;
245 
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));
257 
258   gst_buffer_unmap (stream_hdr, &map);
259 
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);
265 
266   iface_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (video_encoder));
267   if (iface_tags) {
268     vorbiscomment =
269         gst_tag_list_to_vorbiscomment_buffer (iface_tags,
270         (const guint8 *) "OVP80\2 ", 7,
271         "Encoded with GStreamer vp8enc " PACKAGE_VERSION);
272 
273     GST_BUFFER_FLAG_SET (vorbiscomment, GST_BUFFER_FLAG_HEADER);
274 
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);
280   }
281 
282   gst_structure_set_value (s, "streamheader", &array);
283   g_value_unset (&array);
284 
285 }
286 
287 static void *
gst_vp8_enc_process_frame_user_data(GstVPXEnc * enc,GstVideoCodecFrame * frame)288 gst_vp8_enc_process_frame_user_data (GstVPXEnc * enc,
289     GstVideoCodecFrame * frame)
290 {
291   GstVP8EncUserData *user_data;
292 
293   user_data = gst_video_codec_frame_get_user_data (frame);
294 
295   if (!user_data) {
296     GST_ERROR_OBJECT (enc, "Have no frame user data");
297     return NULL;
298   }
299 
300   if (user_data->image)
301     g_slice_free (vpx_image_t, user_data->image);
302   user_data->image = NULL;
303   return user_data;
304 }
305 
306 static GstFlowReturn
gst_vp8_enc_handle_invisible_frame_buffer(GstVPXEnc * enc,void * user_data,GstBuffer * buffer)307 gst_vp8_enc_handle_invisible_frame_buffer (GstVPXEnc * enc, void *user_data,
308     GstBuffer * buffer)
309 {
310   GstVP8EncUserData *vp8_user_data = (GstVP8EncUserData *) user_data;
311 
312   if (!vp8_user_data) {
313     GST_ERROR_OBJECT (enc, "Have no frame user data");
314     return GST_FLOW_ERROR;
315   }
316 
317   vp8_user_data->invisible = g_list_append (vp8_user_data->invisible, buffer);
318 
319   return GST_FLOW_OK;
320 }
321 
322 static void
gst_vp8_enc_set_frame_user_data(GstVPXEnc * enc,GstVideoCodecFrame * frame,vpx_image_t * image)323 gst_vp8_enc_set_frame_user_data (GstVPXEnc * enc, GstVideoCodecFrame * frame,
324     vpx_image_t * image)
325 {
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);
331   return;
332 }
333 
334 static guint64
_to_granulepos(guint64 frame_end_number,guint inv_count,guint keyframe_dist)335 _to_granulepos (guint64 frame_end_number, guint inv_count, guint keyframe_dist)
336 {
337   guint64 granulepos;
338   guint inv;
339 
340   inv = (inv_count == 0) ? 0x3 : inv_count - 1;
341 
342   granulepos = (frame_end_number << 32) | (inv << 30) | (keyframe_dist << 3);
343   return granulepos;
344 }
345 
346 static GstFlowReturn
gst_vp8_enc_pre_push(GstVideoEncoder * video_encoder,GstVideoCodecFrame * frame)347 gst_vp8_enc_pre_push (GstVideoEncoder * video_encoder,
348     GstVideoCodecFrame * frame)
349 {
350   GstVP8Enc *encoder;
351   GstVPXEnc *vpx_enc;
352   GstBuffer *buf;
353   GstFlowReturn ret = GST_FLOW_OK;
354   GstVP8EncUserData *user_data = gst_video_codec_frame_get_user_data (frame);
355   GList *l;
356   gint inv_count;
357   GstVideoInfo *info;
358 
359   GST_DEBUG_OBJECT (video_encoder, "pre_push");
360 
361   encoder = GST_VP8_ENC (video_encoder);
362   vpx_enc = GST_VPX_ENC (encoder);
363 
364   info = &vpx_enc->input_state->info;
365 
366   g_assert (user_data != NULL);
367 
368   for (inv_count = 0, l = user_data->invisible; l; inv_count++, l = l->next) {
369     buf = l->data;
370     l->data = NULL;
371 
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;
377     } else {
378       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
379       encoder->keyframe_distance++;
380     }
381 
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;
388     } else {
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));
396     }
397 
398     ret = gst_pad_push (GST_VIDEO_ENCODER_SRC_PAD (video_encoder), buf);
399 
400     if (ret != GST_FLOW_OK) {
401       GST_WARNING_OBJECT (encoder, "flow error %d", ret);
402       goto done;
403     }
404   }
405 
406   buf = frame->output_buffer;
407 
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;
412   } else {
413     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
414     encoder->keyframe_distance++;
415   }
416 
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;
420   } else {
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));
427   }
428 
429   GST_LOG_OBJECT (video_encoder, "src ts: %" GST_TIME_FORMAT,
430       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
431 
432 done:
433   return ret;
434 }
435 
436 #endif /* HAVE_VP8_ENCODER */
437