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>
37 /****************************************************************************
39 ****************************************************************************/
40 static int Open(vlc_object_t *);
41 static void Close(vlc_object_t *);
43 /*****************************************************************************
45 *****************************************************************************/
49 set_description(N_("WebM video decoder"))
50 set_capability("decoder", 60)
51 set_callbacks(Open, Close)
52 set_category(CAT_INPUT)
53 set_subcategory(SUBCAT_INPUT_VCODEC)
56 /*****************************************************************************
57 * decoder_sys_t: libvpx decoder descriptor
58 *****************************************************************************/
61 struct vpx_codec_ctx ctx;
64 /****************************************************************************
65 * Decode: the whole thing
66 ****************************************************************************/
67 static picture_t *Decode(decoder_t *dec, block_t **pp_block)
69 struct vpx_codec_ctx *ctx = &dec->p_sys->ctx;
71 block_t *block = *pp_block;
75 if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
78 /* Associate packet PTS with decoded frame */
79 mtime_t *pkt_pts = malloc(sizeof(*pkt_pts));
86 *pkt_pts = block->i_pts;
89 err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);
94 if (err != VPX_CODEC_OK) {
96 const char *error = vpx_codec_error(ctx);
97 const char *detail = vpx_codec_error_detail(ctx);
99 detail = "no specific information";
100 msg_Err(dec, "Failed to decode frame: %s (%s)", error, detail);
104 const void *iter = NULL;
105 struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
111 /* fetches back the PTS */
112 pkt_pts = img->user_priv;
113 mtime_t pts = *pkt_pts;
116 if (img->fmt != VPX_IMG_FMT_I420) {
117 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
121 video_format_t *v = &dec->fmt_out.video;
123 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
124 v->i_visible_width = img->d_w;
125 v->i_visible_height = img->d_h;
128 picture_t *pic = decoder_NewPicture(dec);
132 for (int plane = 0; plane < pic->i_planes; plane++ ) {
133 uint8_t *src = img->planes[plane];
134 uint8_t *dst = pic->p[plane].p_pixels;
135 int src_stride = img->stride[plane];
136 int dst_stride = pic->p[plane].i_pitch;
138 int size = __MIN( src_stride, dst_stride );
139 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
140 memcpy( dst, src, size );
146 pic->b_progressive = true; /* codec does not support interlacing */
152 /*****************************************************************************
153 * Open: probe the decoder
154 *****************************************************************************/
155 static int Open(vlc_object_t *p_this)
157 decoder_t *dec = (decoder_t *)p_this;
158 const struct vpx_codec_iface *iface;
161 switch (dec->fmt_in.i_codec)
163 #ifdef ENABLE_VP8_DECODER
165 iface = &vpx_codec_vp8_dx_algo;
169 #ifdef ENABLE_VP9_DECODER
171 iface = &vpx_codec_vp9_dx_algo;
179 decoder_sys_t *sys = malloc(sizeof(*sys));
184 struct vpx_codec_dec_cfg deccfg = {
185 .threads = __MIN(vlc_GetCPUCount(), 16)
188 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
189 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
191 if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
192 const char *error = vpx_codec_error(&sys->ctx);
193 msg_Err(p_this, "Failed to initialize decoder: %s\n", error);
195 return VLC_EGENERIC;;
198 dec->pf_decode_video = Decode;
200 dec->fmt_out.i_cat = VIDEO_ES;
201 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
202 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
203 dec->fmt_out.i_codec = VLC_CODEC_I420;
204 dec->b_need_packetized = true;
209 /*****************************************************************************
210 * Close: decoder destruction
211 *****************************************************************************/
212 static void Close(vlc_object_t *p_this)
214 decoder_t *dec = (decoder_t *)p_this;
215 decoder_sys_t *sys = dec->p_sys;
218 const void *iter = NULL;
220 struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
223 free(img->user_priv);
226 vpx_codec_destroy(&sys->ctx);