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);
109 /* fetches back the PTS */
110 pkt_pts = img->user_priv;
111 mtime_t pts = *pkt_pts;
114 if (img->fmt != VPX_IMG_FMT_I420) {
115 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
119 video_format_t *v = &dec->fmt_out.video;
121 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
122 v->i_visible_width = img->d_w;
123 v->i_visible_height = img->d_h;
126 picture_t *pic = decoder_NewPicture(dec);
130 for (int plane = 0; plane < pic->i_planes; plane++ ) {
131 uint8_t *src = img->planes[plane];
132 uint8_t *dst = pic->p[plane].p_pixels;
133 int src_stride = img->stride[plane];
134 int dst_stride = pic->p[plane].i_pitch;
136 int size = __MIN( src_stride, dst_stride );
137 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
138 memcpy( dst, src, size );
144 pic->b_progressive = true; /* codec does not support interlacing */
150 /*****************************************************************************
151 * Open: probe the decoder
152 *****************************************************************************/
153 static int Open(vlc_object_t *p_this)
155 decoder_t *dec = (decoder_t *)p_this;
156 const struct vpx_codec_iface *iface;
159 switch (dec->fmt_in.i_codec)
161 #ifdef ENABLE_VP8_DECODER
163 iface = &vpx_codec_vp8_dx_algo;
167 #ifdef ENABLE_VP9_DECODER
169 iface = &vpx_codec_vp9_dx_algo;
177 decoder_sys_t *sys = malloc(sizeof(*sys));
182 struct vpx_codec_dec_cfg deccfg = {
183 .threads = __MIN(vlc_GetCPUCount(), 16)
186 msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
187 vp_version, vpx_codec_version_str(), vpx_codec_build_config());
189 if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
190 const char *error = vpx_codec_error(&sys->ctx);
191 msg_Err(p_this, "Failed to initialize decoder: %s\n", error);
193 return VLC_EGENERIC;;
196 dec->pf_decode_video = Decode;
198 dec->fmt_out.i_cat = VIDEO_ES;
199 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
200 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
201 dec->fmt_out.i_codec = VLC_CODEC_I420;
202 dec->b_need_packetized = true;
207 /*****************************************************************************
208 * Close: decoder destruction
209 *****************************************************************************/
210 static void Close(vlc_object_t *p_this)
212 decoder_t *dec = (decoder_t *)p_this;
213 decoder_sys_t *sys = dec->p_sys;
216 const void *iter = NULL;
218 struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
221 free(img->user_priv);
224 vpx_codec_destroy(&sys->ctx);