input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / vpx.c
blob0d0eae0538bc7b1a9ac24213ba3cacffd0557c75
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 int OpenDecoder(vlc_object_t *);
46 static void CloseDecoder(vlc_object_t *);
47 #ifdef ENABLE_SOUT
48 static const char *const ppsz_sout_options[] = { "quality-mode", NULL };
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\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("video 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 typedef struct
101 struct vpx_codec_ctx ctx;
102 } decoder_sys_t;
104 static const struct
106 vlc_fourcc_t i_chroma;
107 enum vpx_img_fmt i_chroma_id;
108 uint8_t i_bitdepth;
109 uint8_t i_needs_hack;
111 } chroma_table[] =
113 { VLC_CODEC_I420, VPX_IMG_FMT_I420, 8, 0 },
114 { VLC_CODEC_I422, VPX_IMG_FMT_I422, 8, 0 },
115 { VLC_CODEC_I444, VPX_IMG_FMT_I444, 8, 0 },
116 { VLC_CODEC_I440, VPX_IMG_FMT_I440, 8, 0 },
118 { VLC_CODEC_YV12, VPX_IMG_FMT_YV12, 8, 0 },
119 { VLC_CODEC_YUVA, VPX_IMG_FMT_444A, 8, 0 },
120 { VLC_CODEC_YUYV, VPX_IMG_FMT_YUY2, 8, 0 },
121 { VLC_CODEC_UYVY, VPX_IMG_FMT_UYVY, 8, 0 },
122 { VLC_CODEC_YVYU, VPX_IMG_FMT_YVYU, 8, 0 },
124 { VLC_CODEC_RGB15, VPX_IMG_FMT_RGB555, 8, 0 },
125 { VLC_CODEC_RGB16, VPX_IMG_FMT_RGB565, 8, 0 },
126 { VLC_CODEC_RGB24, VPX_IMG_FMT_RGB24, 8, 0 },
127 { VLC_CODEC_RGB32, VPX_IMG_FMT_RGB32, 8, 0 },
129 { VLC_CODEC_ARGB, VPX_IMG_FMT_ARGB, 8, 0 },
130 { VLC_CODEC_BGRA, VPX_IMG_FMT_ARGB_LE, 8, 0 },
132 { VLC_CODEC_GBR_PLANAR, VPX_IMG_FMT_I444, 8, 1 },
133 { VLC_CODEC_GBR_PLANAR_10L, VPX_IMG_FMT_I44416, 10, 1 },
135 { VLC_CODEC_I420_10L, VPX_IMG_FMT_I42016, 10, 0 },
136 { VLC_CODEC_I422_10L, VPX_IMG_FMT_I42216, 10, 0 },
137 { VLC_CODEC_I444_10L, VPX_IMG_FMT_I44416, 10, 0 },
139 { VLC_CODEC_I420_12L, VPX_IMG_FMT_I42016, 12, 0 },
140 { VLC_CODEC_I422_12L, VPX_IMG_FMT_I42216, 12, 0 },
141 { VLC_CODEC_I444_12L, VPX_IMG_FMT_I44416, 12, 0 },
143 { VLC_CODEC_I444_16L, VPX_IMG_FMT_I44416, 16, 0 },
146 static vlc_fourcc_t FindVlcChroma( struct vpx_image *img )
148 uint8_t hack = (img->fmt & VPX_IMG_FMT_I444) && (img->cs == VPX_CS_SRGB);
150 for( unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++ )
151 if( chroma_table[i].i_chroma_id == img->fmt &&
152 chroma_table[i].i_bitdepth == img->bit_depth &&
153 chroma_table[i].i_needs_hack == hack )
154 return chroma_table[i].i_chroma;
156 return 0;
159 /****************************************************************************
160 * Decode: the whole thing
161 ****************************************************************************/
162 static int Decode(decoder_t *dec, block_t *block)
164 decoder_sys_t *p_sys = dec->p_sys;
165 struct vpx_codec_ctx *ctx = &p_sys->ctx;
167 if (block == NULL) /* No Drain */
168 return VLCDEC_SUCCESS;
170 if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
171 block_Release(block);
172 return VLCDEC_SUCCESS;
175 /* Associate packet PTS with decoded frame */
176 vlc_tick_t *pkt_pts = malloc(sizeof(*pkt_pts));
177 if (!pkt_pts) {
178 block_Release(block);
179 return VLCDEC_SUCCESS;
182 *pkt_pts = (block->i_pts != VLC_TICK_INVALID) ? block->i_pts : block->i_dts;
184 vpx_codec_err_t err;
185 err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);
187 block_Release(block);
189 if (err != VPX_CODEC_OK) {
190 free(pkt_pts);
191 VPX_ERR(dec, ctx, "Failed to decode frame");
192 if (err == VPX_CODEC_UNSUP_BITSTREAM)
193 return VLCDEC_ECRITICAL;
194 else
195 return VLCDEC_SUCCESS;
198 const void *iter = NULL;
199 struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
200 if (!img) {
201 free(pkt_pts);
202 return VLCDEC_SUCCESS;
205 /* fetches back the PTS */
206 pkt_pts = img->user_priv;
207 vlc_tick_t pts = *pkt_pts;
208 free(pkt_pts);
210 dec->fmt_out.i_codec = FindVlcChroma(img);
212 if( dec->fmt_out.i_codec == 0 ) {
213 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
214 return VLCDEC_SUCCESS;
217 video_format_t *v = &dec->fmt_out.video;
219 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
220 v->i_visible_width = dec->fmt_out.video.i_width = img->d_w;
221 v->i_visible_height = dec->fmt_out.video.i_height = img->d_h;
224 if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
226 dec->fmt_out.video.i_sar_num = 1;
227 dec->fmt_out.video.i_sar_den = 1;
230 v->b_color_range_full = img->range == VPX_CR_FULL_RANGE;
232 switch( img->cs )
234 case VPX_CS_SRGB:
235 case VPX_CS_BT_709:
236 v->space = COLOR_SPACE_BT709;
237 break;
238 case VPX_CS_BT_601:
239 case VPX_CS_SMPTE_170:
240 case VPX_CS_SMPTE_240:
241 v->space = COLOR_SPACE_BT601;
242 break;
243 case VPX_CS_BT_2020:
244 v->space = COLOR_SPACE_BT2020;
245 break;
246 default:
247 break;
250 dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
251 dec->fmt_out.video.multiview_mode = dec->fmt_in.video.multiview_mode;
252 dec->fmt_out.video.pose = dec->fmt_in.video.pose;
254 if (decoder_UpdateVideoFormat(dec))
255 return VLCDEC_SUCCESS;
256 picture_t *pic = decoder_NewPicture(dec);
257 if (!pic)
258 return VLCDEC_SUCCESS;
260 for (int plane = 0; plane < pic->i_planes; plane++ ) {
261 uint8_t *src = img->planes[plane];
262 uint8_t *dst = pic->p[plane].p_pixels;
263 int src_stride = img->stride[plane];
264 int dst_stride = pic->p[plane].i_pitch;
266 int size = __MIN( src_stride, dst_stride );
267 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
268 memcpy( dst, src, size );
269 src += src_stride;
270 dst += dst_stride;
274 pic->b_progressive = true; /* codec does not support interlacing */
275 pic->date = pts;
277 decoder_QueueVideo(dec, pic);
278 return VLCDEC_SUCCESS;
281 /*****************************************************************************
282 * OpenDecoder: probe the decoder
283 *****************************************************************************/
284 static int OpenDecoder(vlc_object_t *p_this)
286 decoder_t *dec = (decoder_t *)p_this;
287 const struct vpx_codec_iface *iface;
288 int vp_version;
290 switch (dec->fmt_in.i_codec)
292 #ifdef ENABLE_VP8_DECODER
293 case VLC_CODEC_WEBP:
294 case VLC_CODEC_VP8:
295 iface = &vpx_codec_vp8_dx_algo;
296 vp_version = 8;
297 break;
298 #endif
299 #ifdef ENABLE_VP9_DECODER
300 case VLC_CODEC_VP9:
301 iface = &vpx_codec_vp9_dx_algo;
302 vp_version = 9;
303 break;
304 #endif
305 default:
306 return VLC_EGENERIC;
309 decoder_sys_t *sys = malloc(sizeof(*sys));
310 if (!sys)
311 return VLC_ENOMEM;
312 dec->p_sys = sys;
314 struct vpx_codec_dec_cfg deccfg = {
315 .threads = __MIN(vlc_GetCPUCount(), 16)
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 if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
322 VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
323 free(sys);
324 return VLC_EGENERIC;;
327 dec->pf_decode = Decode;
329 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
330 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
332 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
333 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
334 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
337 return VLC_SUCCESS;
340 /*****************************************************************************
341 * CloseDecoder: decoder destruction
342 *****************************************************************************/
343 static void CloseDecoder(vlc_object_t *p_this)
345 decoder_t *dec = (decoder_t *)p_this;
346 decoder_sys_t *sys = dec->p_sys;
348 /* Free our PTS */
349 const void *iter = NULL;
350 for (;;) {
351 struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
352 if (!img)
353 break;
354 free(img->user_priv);
357 vpx_codec_destroy(&sys->ctx);
359 free(sys);
362 #ifdef ENABLE_SOUT
364 /*****************************************************************************
365 * encoder_sys_t: libvpx encoder descriptor
366 *****************************************************************************/
367 typedef struct
369 struct vpx_codec_ctx ctx;
370 unsigned long quality;
371 } encoder_sys_t;
373 /*****************************************************************************
374 * OpenEncoder: probe the encoder
375 *****************************************************************************/
376 static int OpenEncoder(vlc_object_t *p_this)
378 encoder_t *p_enc = (encoder_t *)p_this;
379 encoder_sys_t *p_sys;
381 /* Allocate the memory needed to store the encoder's structure */
382 p_sys = malloc(sizeof(*p_sys));
383 if (p_sys == NULL)
384 return VLC_ENOMEM;
385 p_enc->p_sys = p_sys;
387 const struct vpx_codec_iface *iface;
388 int vp_version;
390 switch (p_enc->fmt_out.i_codec)
392 #ifdef ENABLE_VP8_ENCODER
393 case VLC_CODEC_VP8:
394 iface = &vpx_codec_vp8_cx_algo;
395 vp_version = 8;
396 break;
397 #endif
398 #ifdef ENABLE_VP9_ENCODER
399 case VLC_CODEC_VP9:
400 iface = &vpx_codec_vp9_cx_algo;
401 vp_version = 9;
402 break;
403 #endif
404 default:
405 free(p_sys);
406 return VLC_EGENERIC;
409 struct vpx_codec_enc_cfg enccfg = {0};
410 vpx_codec_enc_config_default(iface, &enccfg, 0);
411 enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
412 enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
413 enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
415 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
416 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
418 struct vpx_codec_ctx *ctx = &p_sys->ctx;
419 if (vpx_codec_enc_init(ctx, iface, &enccfg, 0) != VPX_CODEC_OK) {
420 VPX_ERR(p_this, ctx, "Failed to initialize encoder");
421 free(p_sys);
422 return VLC_EGENERIC;
425 p_enc->pf_encode_video = Encode;
426 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
427 config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg);
429 /* Deadline (in ms) to spend in encoder */
430 switch (var_GetInteger(p_enc, ENC_CFG_PREFIX "quality-mode")) {
431 case 1:
432 p_sys->quality = VPX_DL_REALTIME;
433 break;
434 case 2:
435 p_sys->quality = VPX_DL_BEST_QUALITY;
436 break;
437 default:
438 p_sys->quality = VPX_DL_GOOD_QUALITY;
439 break;
442 return VLC_SUCCESS;
445 /****************************************************************************
446 * Encode: the whole thing
447 ****************************************************************************/
448 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
450 encoder_sys_t *p_sys = p_enc->p_sys;
451 struct vpx_codec_ctx *ctx = &p_sys->ctx;
453 if (!p_pict) return NULL;
455 vpx_image_t img = {0};
456 unsigned i_w = p_enc->fmt_in.video.i_visible_width;
457 unsigned i_h = p_enc->fmt_in.video.i_visible_height;
459 /* Create and initialize the vpx_image */
460 if (!vpx_img_wrap(&img, VPX_IMG_FMT_I420, i_w, i_h, 32, p_pict->p[0].p_pixels)) {
461 VPX_ERR(p_enc, ctx, "Failed to wrap image");
462 return NULL;
465 /* Correct chroma plane offsets. */
466 for (int plane = 1; plane < p_pict->i_planes; plane++) {
467 img.planes[plane] = p_pict->p[plane].p_pixels;
468 img.stride[plane] = p_pict->p[plane].i_pitch;
471 int flags = 0;
473 vpx_codec_err_t res = vpx_codec_encode(ctx, &img, p_pict->date, 1,
474 flags, p_sys->quality);
475 if (res != VPX_CODEC_OK) {
476 VPX_ERR(p_enc, ctx, "Failed to encode frame");
477 vpx_img_free(&img);
478 return NULL;
481 const vpx_codec_cx_pkt_t *pkt = NULL;
482 vpx_codec_iter_t iter = NULL;
483 block_t *p_out = NULL;
484 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL)
486 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
488 int keyframe = pkt->data.frame.flags & VPX_FRAME_IS_KEY;
489 block_t *p_block = block_Alloc(pkt->data.frame.sz);
490 if (unlikely(p_block == NULL))
492 block_ChainRelease(p_out);
493 p_out = NULL;
494 break;
497 memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
498 p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
499 if (keyframe)
500 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
501 block_ChainAppend(&p_out, p_block);
504 vpx_img_free(&img);
505 return p_out;
508 /*****************************************************************************
509 * CloseEncoder: encoder destruction
510 *****************************************************************************/
511 static void CloseEncoder(vlc_object_t *p_this)
513 encoder_t *p_enc = (encoder_t *)p_this;
514 encoder_sys_t *p_sys = p_enc->p_sys;
515 if (vpx_codec_destroy(&p_sys->ctx))
516 VPX_ERR(p_this, &p_sys->ctx, "Failed to destroy codec");
517 free(p_sys);
520 #endif /* ENABLE_SOUT */