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 /*****************************************************************************
25 *****************************************************************************/
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>
38 # include <vpx/vpx_encoder.h>
39 # include <vpx/vp8cx.h>
42 /****************************************************************************
44 ****************************************************************************/
45 static int OpenDecoder(vlc_object_t
*);
46 static void CloseDecoder(vlc_object_t
*);
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"\
60 /*****************************************************************************
62 *****************************************************************************/
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
)
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 )
84 static void vpx_err_msg(vlc_object_t
*this, struct vpx_codec_ctx
*ctx
,
87 const char *error
= vpx_codec_error(ctx
);
88 const char *detail
= vpx_codec_error_detail(ctx
);
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 *****************************************************************************/
101 struct vpx_codec_ctx ctx
;
106 vlc_fourcc_t i_chroma
;
107 enum vpx_img_fmt i_chroma_id
;
109 uint8_t i_needs_hack
;
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
;
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
));
178 block_Release(block
);
179 return VLCDEC_SUCCESS
;
182 *pkt_pts
= (block
->i_pts
!= VLC_TICK_INVALID
) ? block
->i_pts
: block
->i_dts
;
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
) {
191 VPX_ERR(dec
, ctx
, "Failed to decode frame");
192 if (err
== VPX_CODEC_UNSUP_BITSTREAM
)
193 return VLCDEC_ECRITICAL
;
195 return VLCDEC_SUCCESS
;
198 const void *iter
= NULL
;
199 struct vpx_image
*img
= vpx_codec_get_frame(ctx
, &iter
);
202 return VLCDEC_SUCCESS
;
205 /* fetches back the PTS */
206 pkt_pts
= img
->user_priv
;
207 vlc_tick_t pts
= *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
;
236 v
->space
= COLOR_SPACE_BT709
;
239 case VPX_CS_SMPTE_170
:
240 case VPX_CS_SMPTE_240
:
241 v
->space
= COLOR_SPACE_BT601
;
244 v
->space
= COLOR_SPACE_BT2020
;
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
);
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
);
274 pic
->b_progressive
= true; /* codec does not support interlacing */
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
;
290 switch (dec
->fmt_in
.i_codec
)
292 #ifdef ENABLE_VP8_DECODER
295 iface
= &vpx_codec_vp8_dx_algo
;
299 #ifdef ENABLE_VP9_DECODER
301 iface
= &vpx_codec_vp9_dx_algo
;
309 decoder_sys_t
*sys
= malloc(sizeof(*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");
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
;
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
;
349 const void *iter
= NULL
;
351 struct vpx_image
*img
= vpx_codec_get_frame(&sys
->ctx
, &iter
);
354 free(img
->user_priv
);
357 vpx_codec_destroy(&sys
->ctx
);
364 /*****************************************************************************
365 * encoder_sys_t: libvpx encoder descriptor
366 *****************************************************************************/
369 struct vpx_codec_ctx ctx
;
370 unsigned long quality
;
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
));
385 p_enc
->p_sys
= p_sys
;
387 const struct vpx_codec_iface
*iface
;
390 switch (p_enc
->fmt_out
.i_codec
)
392 #ifdef ENABLE_VP8_ENCODER
394 iface
= &vpx_codec_vp8_cx_algo
;
398 #ifdef ENABLE_VP9_ENCODER
400 iface
= &vpx_codec_vp9_cx_algo
;
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");
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")) {
432 p_sys
->quality
= VPX_DL_REALTIME
;
435 p_sys
->quality
= VPX_DL_BEST_QUALITY
;
438 p_sys
->quality
= VPX_DL_GOOD_QUALITY
;
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");
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
;
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");
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
);
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
;
500 p_block
->i_flags
|= BLOCK_FLAG_TYPE_I
;
501 block_ChainAppend(&p_out
, p_block
);
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");
520 #endif /* ENABLE_SOUT */