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("decoder", 100)
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 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
));
177 block_Release(block
);
178 return VLCDEC_SUCCESS
;
181 *pkt_pts
= block
->i_pts
? block
->i_pts
: block
->i_dts
;
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
) {
190 VPX_ERR(dec
, ctx
, "Failed to decode frame");
191 if (err
== VPX_CODEC_UNSUP_BITSTREAM
)
192 return VLCDEC_ECRITICAL
;
194 return VLCDEC_SUCCESS
;
197 const void *iter
= NULL
;
198 struct vpx_image
*img
= vpx_codec_get_frame(ctx
, &iter
);
201 return VLCDEC_SUCCESS
;
204 /* fetches back the PTS */
205 pkt_pts
= img
->user_priv
;
206 mtime_t pts
= *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
;
235 v
->space
= COLOR_SPACE_BT709
;
238 case VPX_CS_SMPTE_170
:
239 case VPX_CS_SMPTE_240
:
240 v
->space
= COLOR_SPACE_BT601
;
243 v
->space
= COLOR_SPACE_BT2020
;
249 dec
->fmt_out
.video
.projection_mode
= dec
->fmt_in
.video
.projection_mode
;
250 dec
->fmt_out
.video
.pose
= dec
->fmt_in
.video
.pose
;
252 if (decoder_UpdateVideoFormat(dec
))
253 return VLCDEC_SUCCESS
;
254 picture_t
*pic
= decoder_NewPicture(dec
);
256 return VLCDEC_SUCCESS
;
258 for (int plane
= 0; plane
< pic
->i_planes
; plane
++ ) {
259 uint8_t *src
= img
->planes
[plane
];
260 uint8_t *dst
= pic
->p
[plane
].p_pixels
;
261 int src_stride
= img
->stride
[plane
];
262 int dst_stride
= pic
->p
[plane
].i_pitch
;
264 int size
= __MIN( src_stride
, dst_stride
);
265 for( int line
= 0; line
< pic
->p
[plane
].i_visible_lines
; line
++ ) {
266 memcpy( dst
, src
, size
);
272 pic
->b_progressive
= true; /* codec does not support interlacing */
275 decoder_QueueVideo(dec
, pic
);
276 return VLCDEC_SUCCESS
;
279 /*****************************************************************************
280 * OpenDecoder: probe the decoder
281 *****************************************************************************/
282 static int OpenDecoder(vlc_object_t
*p_this
)
284 decoder_t
*dec
= (decoder_t
*)p_this
;
285 const struct vpx_codec_iface
*iface
;
288 switch (dec
->fmt_in
.i_codec
)
290 #ifdef ENABLE_VP8_DECODER
292 iface
= &vpx_codec_vp8_dx_algo
;
296 #ifdef ENABLE_VP9_DECODER
298 iface
= &vpx_codec_vp9_dx_algo
;
306 decoder_sys_t
*sys
= malloc(sizeof(*sys
));
311 struct vpx_codec_dec_cfg deccfg
= {
312 .threads
= __MIN(vlc_GetCPUCount(), 16)
315 msg_Dbg(p_this
, "VP%d: using libvpx version %s (build options %s)",
316 vp_version
, vpx_codec_version_str(), vpx_codec_build_config());
318 if (vpx_codec_dec_init(&sys
->ctx
, iface
, &deccfg
, 0) != VPX_CODEC_OK
) {
319 VPX_ERR(p_this
, &sys
->ctx
, "Failed to initialize decoder");
321 return VLC_EGENERIC
;;
324 dec
->pf_decode
= Decode
;
326 dec
->fmt_out
.i_cat
= VIDEO_ES
;
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
;
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
;
347 const void *iter
= NULL
;
349 struct vpx_image
*img
= vpx_codec_get_frame(&sys
->ctx
, &iter
);
352 free(img
->user_priv
);
355 vpx_codec_destroy(&sys
->ctx
);
362 /*****************************************************************************
363 * encoder_sys_t: libvpx encoder descriptor
364 *****************************************************************************/
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
));
382 p_enc
->p_sys
= p_sys
;
384 const struct vpx_codec_iface
*iface
;
387 switch (p_enc
->fmt_out
.i_codec
)
389 #ifdef ENABLE_VP8_ENCODER
391 iface
= &vpx_codec_vp8_cx_algo
;
395 #ifdef ENABLE_VP9_ENCODER
397 iface
= &vpx_codec_vp9_cx_algo
;
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");
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
);
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");
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
);
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")) {
468 quality
= VPX_DL_REALTIME
;
471 quality
= VPX_DL_BEST_QUALITY
;
477 vpx_codec_err_t res
= vpx_codec_encode(ctx
, &img
, p_pict
->date
, 1,
479 if (res
!= VPX_CODEC_OK
) {
480 VPX_ERR(p_enc
, ctx
, "Failed to encode frame");
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
;
497 p_block
->i_flags
|= BLOCK_FLAG_TYPE_I
;
498 block_ChainAppend(&p_out
, p_block
);
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");
517 #endif /* ENABLE_SOUT */