vpx: add vp8 and vp9 encoder
[vlc.git] / modules / codec / vpx.c
blobd774271a7fa5984d9143d98d25bf407bb5c211b5
1 /*****************************************************************************
2 * vpx.c: libvpx decoder (VP8/VP9) module
3 *****************************************************************************
4 * Copyright (C) 2013 Rafaël Carré
6 * Authors: Rafaël Carré <funman@videolanorg>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
34 #include <vpx/vpx_decoder.h>
35 #include <vpx/vp8dx.h>
37 #ifdef ENABLE_SOUT
38 # include <vpx/vpx_encoder.h>
39 # include <vpx/vp8cx.h>
40 #endif
42 /****************************************************************************
43 * Local prototypes
44 ****************************************************************************/
45 static const char *const ppsz_sout_options[] = { "quality-mode", NULL };
46 static int OpenDecoder(vlc_object_t *);
47 static void CloseDecoder(vlc_object_t *);
48 #ifdef ENABLE_SOUT
49 static int OpenEncoder(vlc_object_t *);
50 static void CloseEncoder(vlc_object_t *);
51 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict);
53 #define QUALITY_MODE_TEXT N_("Quality mode")
54 #define QUALITY_MODE_LONGTEXT N_("Quality setting which will determine max encoding time, default 0\n" \
55 " - 0: Good quality\n"\
56 " - 1: Realtime\n"\
57 " - 2: Best quality")
58 #endif
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
64 vlc_module_begin ()
65 set_shortname("vpx")
66 set_description(N_("WebM video decoder"))
67 set_capability("decoder", 60)
68 set_callbacks(OpenDecoder, CloseDecoder)
69 set_category(CAT_INPUT)
70 set_subcategory(SUBCAT_INPUT_VCODEC)
71 #ifdef ENABLE_SOUT
72 add_submodule()
73 set_shortname("vpx")
74 set_capability("encoder", 60)
75 set_description(N_("WebM video encoder"))
76 set_callbacks(OpenEncoder, CloseEncoder)
77 # define ENC_CFG_PREFIX "sout-vpx-"
78 add_integer( ENC_CFG_PREFIX "quality-mode", VPX_DL_GOOD_QUALITY, QUALITY_MODE_TEXT,
79 QUALITY_MODE_LONGTEXT, true )
80 change_integer_range( 0, 2 )
81 #endif
82 vlc_module_end ()
84 static void vpx_err_msg(vlc_object_t *this, struct vpx_codec_ctx *ctx,
85 const char *msg)
87 const char *error = vpx_codec_error(ctx);
88 const char *detail = vpx_codec_error_detail(ctx);
89 if (!detail)
90 detail = "no specific information";
91 msg_Err(this, msg, error, detail);
94 #define VPX_ERR(this, ctx, msg) vpx_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
96 /*****************************************************************************
97 * decoder_sys_t: libvpx decoder descriptor
98 *****************************************************************************/
99 struct decoder_sys_t
101 struct vpx_codec_ctx ctx;
104 /****************************************************************************
105 * Decode: the whole thing
106 ****************************************************************************/
107 static picture_t *Decode(decoder_t *dec, block_t **pp_block)
109 struct vpx_codec_ctx *ctx = &dec->p_sys->ctx;
111 block_t *block = *pp_block;
112 if (!block)
113 return NULL;
115 if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
116 block_Release(block);
117 return NULL;
120 /* Associate packet PTS with decoded frame */
121 mtime_t *pkt_pts = malloc(sizeof(*pkt_pts));
122 if (!pkt_pts) {
123 block_Release(block);
124 *pp_block = NULL;
125 return NULL;
128 *pkt_pts = block->i_pts;
130 vpx_codec_err_t err;
131 err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);
133 block_Release(block);
134 *pp_block = NULL;
136 if (err != VPX_CODEC_OK) {
137 free(pkt_pts);
138 VPX_ERR(dec, ctx, "Failed to decode frame");
139 return NULL;
142 const void *iter = NULL;
143 struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
144 if (!img) {
145 free(pkt_pts);
146 return NULL;
149 /* fetches back the PTS */
150 pkt_pts = img->user_priv;
151 mtime_t pts = *pkt_pts;
152 free(pkt_pts);
154 if (img->fmt != VPX_IMG_FMT_I420) {
155 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
156 return NULL;
159 video_format_t *v = &dec->fmt_out.video;
161 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
162 v->i_visible_width = img->d_w;
163 v->i_visible_height = img->d_h;
166 picture_t *pic = decoder_NewPicture(dec);
167 if (!pic)
168 return NULL;
170 for (int plane = 0; plane < pic->i_planes; plane++ ) {
171 uint8_t *src = img->planes[plane];
172 uint8_t *dst = pic->p[plane].p_pixels;
173 int src_stride = img->stride[plane];
174 int dst_stride = pic->p[plane].i_pitch;
176 int size = __MIN( src_stride, dst_stride );
177 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
178 memcpy( dst, src, size );
179 src += src_stride;
180 dst += dst_stride;
184 pic->b_progressive = true; /* codec does not support interlacing */
185 pic->date = pts;
187 return pic;
190 /*****************************************************************************
191 * OpenDecoder: probe the decoder
192 *****************************************************************************/
193 static int OpenDecoder(vlc_object_t *p_this)
195 decoder_t *dec = (decoder_t *)p_this;
196 const struct vpx_codec_iface *iface;
197 int vp_version;
199 switch (dec->fmt_in.i_codec)
201 #ifdef ENABLE_VP8_DECODER
202 case VLC_CODEC_VP8:
203 iface = &vpx_codec_vp8_dx_algo;
204 vp_version = 8;
205 break;
206 #endif
207 #ifdef ENABLE_VP9_DECODER
208 case VLC_CODEC_VP9:
209 iface = &vpx_codec_vp9_dx_algo;
210 vp_version = 9;
211 break;
212 #endif
213 default:
214 return VLC_EGENERIC;
217 decoder_sys_t *sys = malloc(sizeof(*sys));
218 if (!sys)
219 return VLC_ENOMEM;
220 dec->p_sys = sys;
222 struct vpx_codec_dec_cfg deccfg = {
223 .threads = __MIN(vlc_GetCPUCount(), 16)
226 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
227 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
229 if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
230 VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
231 free(sys);
232 return VLC_EGENERIC;;
235 dec->pf_decode_video = Decode;
237 dec->fmt_out.i_cat = VIDEO_ES;
238 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
239 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
240 dec->fmt_out.i_codec = VLC_CODEC_I420;
242 return VLC_SUCCESS;
245 /*****************************************************************************
246 * CloseDecoder: decoder destruction
247 *****************************************************************************/
248 static void CloseDecoder(vlc_object_t *p_this)
250 decoder_t *dec = (decoder_t *)p_this;
251 decoder_sys_t *sys = dec->p_sys;
253 /* Free our PTS */
254 const void *iter = NULL;
255 for (;;) {
256 struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
257 if (!img)
258 break;
259 free(img->user_priv);
262 vpx_codec_destroy(&sys->ctx);
264 free(sys);
267 #ifdef ENABLE_SOUT
269 /*****************************************************************************
270 * encoder_sys_t: libvpx encoder descriptor
271 *****************************************************************************/
272 struct encoder_sys_t
274 struct vpx_codec_ctx ctx;
277 /*****************************************************************************
278 * OpenEncoder: probe the encoder
279 *****************************************************************************/
280 static int OpenEncoder(vlc_object_t *p_this)
282 encoder_t *p_enc = (encoder_t *)p_this;
283 encoder_sys_t *p_sys;
285 /* Allocate the memory needed to store the encoder's structure */
286 p_sys = malloc(sizeof(*p_sys));
287 if (p_sys == NULL)
288 return VLC_ENOMEM;
289 p_enc->p_sys = p_sys;
291 const struct vpx_codec_iface *iface;
292 int vp_version;
294 switch (p_enc->fmt_out.i_codec)
296 #ifdef ENABLE_VP8_ENCODER
297 case VLC_CODEC_VP8:
298 iface = &vpx_codec_vp8_cx_algo;
299 vp_version = 8;
300 break;
301 #endif
302 #ifdef ENABLE_VP9_DECODER
303 case VLC_CODEC_VP9:
304 iface = &vpx_codec_vp9_cx_algo;
305 vp_version = 9;
306 break;
307 #endif
308 default:
309 return VLC_EGENERIC;
312 struct vpx_codec_enc_cfg enccfg = {};
313 vpx_codec_enc_config_default(iface, &enccfg, 0);
314 enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
315 enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
316 enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
318 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
319 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
321 struct vpx_codec_ctx *ctx = &p_sys->ctx;
322 if (vpx_codec_enc_init(ctx, iface, &enccfg, 0) != VPX_CODEC_OK) {
323 VPX_ERR(p_this, ctx, "Failed to initialize encoder");
324 free(p_sys);
325 return VLC_EGENERIC;
328 p_enc->pf_encode_video = Encode;
329 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
330 config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg);
332 return VLC_SUCCESS;
335 /****************************************************************************
336 * Encode: the whole thing
337 ****************************************************************************/
338 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
340 encoder_sys_t *p_sys = p_enc->p_sys;
341 struct vpx_codec_ctx *ctx = &p_sys->ctx;
343 if (!p_pict) return NULL;
345 vpx_image_t img = {};
346 unsigned i_w = p_enc->fmt_in.video.i_visible_width;
347 unsigned i_h = p_enc->fmt_in.video.i_visible_height;
349 /* Create and initialize the vpx_image */
350 if (!vpx_img_alloc(&img, VPX_IMG_FMT_I420, i_w, i_h, 1)) {
351 VPX_ERR(p_enc, ctx, "Failed to allocate image");
352 return NULL;
354 for (int plane = 0; plane < p_pict->i_planes; plane++) {
355 uint8_t *src = p_pict->p[plane].p_pixels;
356 uint8_t *dst = img.planes[plane];
357 int src_stride = p_pict->p[plane].i_pitch;
358 int dst_stride = img.stride[plane];
360 int size = __MIN(src_stride, dst_stride);
361 for (int line = 0; line < p_pict->p[plane].i_visible_lines; line++)
363 memcpy(dst, src, size);
364 src += src_stride;
365 dst += dst_stride;
369 int flags = 0;
370 /* Deadline (in ms) to spend in encoder */
371 int quality = VPX_DL_GOOD_QUALITY;
372 switch (var_GetInteger(p_enc, ENC_CFG_PREFIX "quality-mode")) {
373 case 1:
374 quality = VPX_DL_REALTIME;
375 break;
376 case 2:
377 quality = VPX_DL_BEST_QUALITY;
378 break;
379 default:
380 break;
383 vpx_codec_err_t res = vpx_codec_encode(ctx, &img, p_pict->date, 1,
384 flags, quality);
385 if (res != VPX_CODEC_OK) {
386 VPX_ERR(p_enc, ctx, "Failed to encode frame");
387 return NULL;
390 const vpx_codec_cx_pkt_t *pkt = NULL;
391 vpx_codec_iter_t iter = NULL;
392 block_t *p_out = NULL;
393 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL)
395 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
397 int keyframe = pkt->data.frame.flags & VPX_FRAME_IS_KEY;
398 block_t *p_block = block_Alloc(pkt->data.frame.sz);
400 memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
401 p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
402 if (keyframe)
403 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
404 block_ChainAppend(&p_out, p_block);
407 vpx_img_free(&img);
408 return p_out;
411 /*****************************************************************************
412 * CloseEncoder: encoder destruction
413 *****************************************************************************/
414 static void CloseEncoder(vlc_object_t *p_this)
416 encoder_t *p_enc = (encoder_t *)p_this;
417 encoder_sys_t *p_sys = p_enc->p_sys;
418 if (vpx_codec_destroy(&p_sys->ctx))
419 VPX_ERR(p_this, &p_sys->ctx, "Failed to destroy codec");
420 free(p_sys);
423 #endif /* ENABLE_SOUT */