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 const char *const ppsz_sout_options
[] = { "quality-mode", NULL
};
46 static int OpenDecoder(vlc_object_t
*);
47 static void CloseDecoder(vlc_object_t
*);
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"\
60 /*****************************************************************************
62 *****************************************************************************/
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
)
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
;
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
;
115 if (block
->i_flags
& (BLOCK_FLAG_CORRUPTED
)) {
116 block_Release(block
);
120 /* Associate packet PTS with decoded frame */
121 mtime_t
*pkt_pts
= malloc(sizeof(*pkt_pts
));
123 block_Release(block
);
128 *pkt_pts
= block
->i_pts
;
131 err
= vpx_codec_decode(ctx
, block
->p_buffer
, block
->i_buffer
, pkt_pts
, 0);
133 block_Release(block
);
136 if (err
!= VPX_CODEC_OK
) {
138 VPX_ERR(dec
, ctx
, "Failed to decode frame");
142 const void *iter
= NULL
;
143 struct vpx_image
*img
= vpx_codec_get_frame(ctx
, &iter
);
149 /* fetches back the PTS */
150 pkt_pts
= img
->user_priv
;
151 mtime_t pts
= *pkt_pts
;
154 if (img
->fmt
!= VPX_IMG_FMT_I420
) {
155 msg_Err(dec
, "Unsupported output colorspace %d", img
->fmt
);
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
);
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
);
184 pic
->b_progressive
= true; /* codec does not support interlacing */
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
;
199 switch (dec
->fmt_in
.i_codec
)
201 #ifdef ENABLE_VP8_DECODER
203 iface
= &vpx_codec_vp8_dx_algo
;
207 #ifdef ENABLE_VP9_DECODER
209 iface
= &vpx_codec_vp9_dx_algo
;
217 decoder_sys_t
*sys
= malloc(sizeof(*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");
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
;
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
;
254 const void *iter
= NULL
;
256 struct vpx_image
*img
= vpx_codec_get_frame(&sys
->ctx
, &iter
);
259 free(img
->user_priv
);
262 vpx_codec_destroy(&sys
->ctx
);
269 /*****************************************************************************
270 * encoder_sys_t: libvpx encoder descriptor
271 *****************************************************************************/
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
));
289 p_enc
->p_sys
= p_sys
;
291 const struct vpx_codec_iface
*iface
;
294 switch (p_enc
->fmt_out
.i_codec
)
296 #ifdef ENABLE_VP8_ENCODER
298 iface
= &vpx_codec_vp8_cx_algo
;
302 #ifdef ENABLE_VP9_DECODER
304 iface
= &vpx_codec_vp9_cx_algo
;
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");
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
);
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");
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
);
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")) {
374 quality
= VPX_DL_REALTIME
;
377 quality
= VPX_DL_BEST_QUALITY
;
383 vpx_codec_err_t res
= vpx_codec_encode(ctx
, &img
, p_pict
->date
, 1,
385 if (res
!= VPX_CODEC_OK
) {
386 VPX_ERR(p_enc
, ctx
, "Failed to encode frame");
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
;
403 p_block
->i_flags
|= BLOCK_FLAG_TYPE_I
;
404 block_ChainAppend(&p_out
, p_block
);
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");
423 #endif /* ENABLE_SOUT */