packetizer: hevc: add poc debug
[vlc.git] / modules / codec / vpx.c
blobd626038ccc0c302b6752795d818a49da851cac52
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 struct decoder_sys_t
101 struct vpx_codec_ctx ctx;
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 struct vpx_codec_ctx *ctx = &dec->p_sys->ctx;
166 if (block == NULL) /* No Drain */
167 return VLCDEC_SUCCESS;
169 if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
170 block_Release(block);
171 return VLCDEC_SUCCESS;
174 /* Associate packet PTS with decoded frame */
175 mtime_t *pkt_pts = malloc(sizeof(*pkt_pts));
176 if (!pkt_pts) {
177 block_Release(block);
178 return VLCDEC_SUCCESS;
181 *pkt_pts = block->i_pts ? block->i_pts : block->i_dts;
183 vpx_codec_err_t err;
184 err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);
186 block_Release(block);
188 if (err != VPX_CODEC_OK) {
189 free(pkt_pts);
190 VPX_ERR(dec, ctx, "Failed to decode frame");
191 if (err == VPX_CODEC_UNSUP_BITSTREAM)
192 return VLCDEC_ECRITICAL;
193 else
194 return VLCDEC_SUCCESS;
197 const void *iter = NULL;
198 struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
199 if (!img) {
200 free(pkt_pts);
201 return VLCDEC_SUCCESS;
204 /* fetches back the PTS */
205 pkt_pts = img->user_priv;
206 mtime_t pts = *pkt_pts;
207 free(pkt_pts);
209 dec->fmt_out.i_codec = FindVlcChroma(img);
211 if( dec->fmt_out.i_codec == 0 ) {
212 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
213 return VLCDEC_SUCCESS;
216 video_format_t *v = &dec->fmt_out.video;
218 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
219 v->i_visible_width = dec->fmt_out.video.i_width = img->d_w;
220 v->i_visible_height = dec->fmt_out.video.i_height = img->d_h;
223 if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
225 dec->fmt_out.video.i_sar_num = 1;
226 dec->fmt_out.video.i_sar_den = 1;
229 v->b_color_range_full = img->range == VPX_CR_FULL_RANGE;
231 switch( img->cs )
233 case VPX_CS_SRGB:
234 case VPX_CS_BT_709:
235 v->space = COLOR_SPACE_BT709;
236 break;
237 case VPX_CS_BT_601:
238 case VPX_CS_SMPTE_170:
239 case VPX_CS_SMPTE_240:
240 v->space = COLOR_SPACE_BT601;
241 break;
242 case VPX_CS_BT_2020:
243 v->space = COLOR_SPACE_BT2020;
244 break;
245 default:
246 break;
249 dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
250 dec->fmt_out.video.multiview_mode = dec->fmt_in.video.multiview_mode;
251 dec->fmt_out.video.pose = dec->fmt_in.video.pose;
253 if (decoder_UpdateVideoFormat(dec))
254 return VLCDEC_SUCCESS;
255 picture_t *pic = decoder_NewPicture(dec);
256 if (!pic)
257 return VLCDEC_SUCCESS;
259 for (int plane = 0; plane < pic->i_planes; plane++ ) {
260 uint8_t *src = img->planes[plane];
261 uint8_t *dst = pic->p[plane].p_pixels;
262 int src_stride = img->stride[plane];
263 int dst_stride = pic->p[plane].i_pitch;
265 int size = __MIN( src_stride, dst_stride );
266 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
267 memcpy( dst, src, size );
268 src += src_stride;
269 dst += dst_stride;
273 pic->b_progressive = true; /* codec does not support interlacing */
274 pic->date = pts;
276 decoder_QueueVideo(dec, pic);
277 return VLCDEC_SUCCESS;
280 /*****************************************************************************
281 * OpenDecoder: probe the decoder
282 *****************************************************************************/
283 static int OpenDecoder(vlc_object_t *p_this)
285 decoder_t *dec = (decoder_t *)p_this;
286 const struct vpx_codec_iface *iface;
287 int vp_version;
289 switch (dec->fmt_in.i_codec)
291 #ifdef ENABLE_VP8_DECODER
292 case VLC_CODEC_VP8:
293 iface = &vpx_codec_vp8_dx_algo;
294 vp_version = 8;
295 break;
296 #endif
297 #ifdef ENABLE_VP9_DECODER
298 case VLC_CODEC_VP9:
299 iface = &vpx_codec_vp9_dx_algo;
300 vp_version = 9;
301 break;
302 #endif
303 default:
304 return VLC_EGENERIC;
307 decoder_sys_t *sys = malloc(sizeof(*sys));
308 if (!sys)
309 return VLC_ENOMEM;
310 dec->p_sys = sys;
312 struct vpx_codec_dec_cfg deccfg = {
313 .threads = __MIN(vlc_GetCPUCount(), 16)
316 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
317 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
319 if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
320 VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
321 free(sys);
322 return VLC_EGENERIC;;
325 dec->pf_decode = Decode;
327 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
328 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
330 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
331 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
332 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
335 return VLC_SUCCESS;
338 /*****************************************************************************
339 * CloseDecoder: decoder destruction
340 *****************************************************************************/
341 static void CloseDecoder(vlc_object_t *p_this)
343 decoder_t *dec = (decoder_t *)p_this;
344 decoder_sys_t *sys = dec->p_sys;
346 /* Free our PTS */
347 const void *iter = NULL;
348 for (;;) {
349 struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
350 if (!img)
351 break;
352 free(img->user_priv);
355 vpx_codec_destroy(&sys->ctx);
357 free(sys);
360 #ifdef ENABLE_SOUT
362 /*****************************************************************************
363 * encoder_sys_t: libvpx encoder descriptor
364 *****************************************************************************/
365 struct encoder_sys_t
367 struct vpx_codec_ctx ctx;
370 /*****************************************************************************
371 * OpenEncoder: probe the encoder
372 *****************************************************************************/
373 static int OpenEncoder(vlc_object_t *p_this)
375 encoder_t *p_enc = (encoder_t *)p_this;
376 encoder_sys_t *p_sys;
378 /* Allocate the memory needed to store the encoder's structure */
379 p_sys = malloc(sizeof(*p_sys));
380 if (p_sys == NULL)
381 return VLC_ENOMEM;
382 p_enc->p_sys = p_sys;
384 const struct vpx_codec_iface *iface;
385 int vp_version;
387 switch (p_enc->fmt_out.i_codec)
389 #ifdef ENABLE_VP8_ENCODER
390 case VLC_CODEC_VP8:
391 iface = &vpx_codec_vp8_cx_algo;
392 vp_version = 8;
393 break;
394 #endif
395 #ifdef ENABLE_VP9_ENCODER
396 case VLC_CODEC_VP9:
397 iface = &vpx_codec_vp9_cx_algo;
398 vp_version = 9;
399 break;
400 #endif
401 default:
402 free(p_sys);
403 return VLC_EGENERIC;
406 struct vpx_codec_enc_cfg enccfg = {};
407 vpx_codec_enc_config_default(iface, &enccfg, 0);
408 enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
409 enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
410 enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
412 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
413 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
415 struct vpx_codec_ctx *ctx = &p_sys->ctx;
416 if (vpx_codec_enc_init(ctx, iface, &enccfg, 0) != VPX_CODEC_OK) {
417 VPX_ERR(p_this, ctx, "Failed to initialize encoder");
418 free(p_sys);
419 return VLC_EGENERIC;
422 p_enc->pf_encode_video = Encode;
423 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
424 config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg);
426 return VLC_SUCCESS;
429 /****************************************************************************
430 * Encode: the whole thing
431 ****************************************************************************/
432 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
434 encoder_sys_t *p_sys = p_enc->p_sys;
435 struct vpx_codec_ctx *ctx = &p_sys->ctx;
437 if (!p_pict) return NULL;
439 vpx_image_t img = {};
440 unsigned i_w = p_enc->fmt_in.video.i_visible_width;
441 unsigned i_h = p_enc->fmt_in.video.i_visible_height;
443 /* Create and initialize the vpx_image */
444 if (!vpx_img_alloc(&img, VPX_IMG_FMT_I420, i_w, i_h, 1)) {
445 VPX_ERR(p_enc, ctx, "Failed to allocate image");
446 return NULL;
448 for (int plane = 0; plane < p_pict->i_planes; plane++) {
449 uint8_t *src = p_pict->p[plane].p_pixels;
450 uint8_t *dst = img.planes[plane];
451 int src_stride = p_pict->p[plane].i_pitch;
452 int dst_stride = img.stride[plane];
454 int size = __MIN(src_stride, dst_stride);
455 for (int line = 0; line < p_pict->p[plane].i_visible_lines; line++)
457 memcpy(dst, src, size);
458 src += src_stride;
459 dst += dst_stride;
463 int flags = 0;
464 /* Deadline (in ms) to spend in encoder */
465 int quality = VPX_DL_GOOD_QUALITY;
466 switch (var_GetInteger(p_enc, ENC_CFG_PREFIX "quality-mode")) {
467 case 1:
468 quality = VPX_DL_REALTIME;
469 break;
470 case 2:
471 quality = VPX_DL_BEST_QUALITY;
472 break;
473 default:
474 break;
477 vpx_codec_err_t res = vpx_codec_encode(ctx, &img, p_pict->date, 1,
478 flags, quality);
479 if (res != VPX_CODEC_OK) {
480 VPX_ERR(p_enc, ctx, "Failed to encode frame");
481 return NULL;
484 const vpx_codec_cx_pkt_t *pkt = NULL;
485 vpx_codec_iter_t iter = NULL;
486 block_t *p_out = NULL;
487 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL)
489 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
491 int keyframe = pkt->data.frame.flags & VPX_FRAME_IS_KEY;
492 block_t *p_block = block_Alloc(pkt->data.frame.sz);
494 memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
495 p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
496 if (keyframe)
497 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
498 block_ChainAppend(&p_out, p_block);
501 vpx_img_free(&img);
502 return p_out;
505 /*****************************************************************************
506 * CloseEncoder: encoder destruction
507 *****************************************************************************/
508 static void CloseEncoder(vlc_object_t *p_this)
510 encoder_t *p_enc = (encoder_t *)p_this;
511 encoder_sys_t *p_sys = p_enc->p_sys;
512 if (vpx_codec_destroy(&p_sys->ctx))
513 VPX_ERR(p_this, &p_sys->ctx, "Failed to destroy codec");
514 free(p_sys);
517 #endif /* ENABLE_SOUT */