1 /* VP9
2  * Copyright (C) 2006 David Schleef <ds@schleef.org>
3  * Copyright (C) 2008,2009,2010 Entropy Wave Inc
4  * Copyright (C) 2010-2013 Sebastian Dröge <slomo@circular-chaos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 /**
23  * SECTION:element-vp9dec
24  * @see_also: vp9enc, matroskademux
25  *
26  * This element decodes VP9 streams into raw video.
27  * <ulink url="http://www.webmproject.org">VP9</ulink> is a royalty-free
28  * video codec maintained by <ulink url="http://www.google.com/">Google
29  * </ulink>. It's the successor of On2 VP3, which was the base of the
30  * Theora video codec.
31  *
32  * <refsect2>
33  * <title>Example pipeline</title>
34  * |[
35  * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! vp9dec ! videoconvert ! videoscale ! autovideosink
36  * ]| This example pipeline will decode a WebM stream and decodes the VP9 video.
37  * </refsect2>
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 
44 #ifdef HAVE_VP9_DECODER
45 
46 #include <string.h>
47 
48 #include "gstvp8utils.h"
49 #include "gstvp9dec.h"
50 
51 #include <gst/video/gstvideometa.h>
52 #include <gst/video/gstvideopool.h>
53 
54 GST_DEBUG_CATEGORY_STATIC (gst_vp9dec_debug);
55 #define GST_CAT_DEFAULT gst_vp9dec_debug
56 
57 #define VP9_DECODER_VIDEO_TAG "VP9 video"
58 
59 static void gst_vp9_dec_set_stream_info (GstVPXDec * dec,
60     vpx_codec_stream_info_t * stream_info);
61 static gboolean gst_vp9_dec_get_valid_format (GstVPXDec * dec,
62     vpx_image_t * img, GstVideoFormat * fmt);
63 static void gst_vp9_dec_handle_resolution_change (GstVPXDec * dec,
64     vpx_image_t * img, GstVideoFormat fmt);
65 
66 static GstStaticPadTemplate gst_vp9_dec_sink_template =
67 GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("video/x-vp9")
71     );
72 
73 static GstStaticPadTemplate gst_vp9_dec_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444, GBR }"))
78     );
79 
80 #define parent_class gst_vp9_dec_parent_class
81 G_DEFINE_TYPE (GstVP9Dec, gst_vp9_dec, GST_TYPE_VPX_DEC);
82 
83 static void
gst_vp9_dec_class_init(GstVP9DecClass * klass)84 gst_vp9_dec_class_init (GstVP9DecClass * klass)
85 {
86   GstElementClass *element_class;
87   GstVPXDecClass *vpx_class;
88 
89   element_class = GST_ELEMENT_CLASS (klass);
90   vpx_class = GST_VPX_DEC_CLASS (klass);
91 
92   gst_element_class_add_static_pad_template (element_class,
93       &gst_vp9_dec_src_template);
94   gst_element_class_add_static_pad_template (element_class,
95       &gst_vp9_dec_sink_template);
96 
97   gst_element_class_set_static_metadata (element_class,
98       "On2 VP9 Decoder",
99       "Codec/Decoder/Video",
100       "Decode VP9 video streams", "David Schleef <ds@entropywave.com>, "
101       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
102 
103   vpx_class->video_codec_tag = VP9_DECODER_VIDEO_TAG;
104   vpx_class->codec_algo = &vpx_codec_vp9_dx_algo;
105   vpx_class->set_stream_info = GST_DEBUG_FUNCPTR (gst_vp9_dec_set_stream_info);
106   vpx_class->get_frame_format =
107       GST_DEBUG_FUNCPTR (gst_vp9_dec_get_valid_format);
108   vpx_class->handle_resolution_change =
109       GST_DEBUG_FUNCPTR (gst_vp9_dec_handle_resolution_change);
110 
111   GST_DEBUG_CATEGORY_INIT (gst_vp9dec_debug, "vp9dec", 0, "VP9 Decoder");
112 }
113 
114 static void
gst_vp9_dec_init(GstVP9Dec * gst_vp9_dec)115 gst_vp9_dec_init (GstVP9Dec * gst_vp9_dec)
116 {
117   GST_DEBUG_OBJECT (gst_vp9_dec, "gst_vp9_dec_init");
118 }
119 
120 static void
gst_vp9_dec_set_stream_info(GstVPXDec * dec,vpx_codec_stream_info_t * stream_info)121 gst_vp9_dec_set_stream_info (GstVPXDec * dec,
122     vpx_codec_stream_info_t * stream_info)
123 {
124   /* FIXME: peek_stream_info() does not return valid values, take input caps */
125   stream_info->w = dec->input_state->info.width;
126   stream_info->h = dec->input_state->info.height;
127   return;
128 }
129 
130 static gboolean
gst_vp9_dec_get_valid_format(GstVPXDec * dec,vpx_image_t * img,GstVideoFormat * fmt)131 gst_vp9_dec_get_valid_format (GstVPXDec * dec, vpx_image_t * img,
132     GstVideoFormat * fmt)
133 {
134   switch (img->fmt) {
135     case VPX_IMG_FMT_I420:
136       *fmt = GST_VIDEO_FORMAT_I420;
137       return TRUE;
138 
139     case VPX_IMG_FMT_YV12:
140       *fmt = GST_VIDEO_FORMAT_YV12;
141       return TRUE;
142 
143     case VPX_IMG_FMT_I422:
144       *fmt = GST_VIDEO_FORMAT_Y42B;
145       return TRUE;
146 
147     case VPX_IMG_FMT_I444:
148       if (img->cs == VPX_CS_SRGB)
149         *fmt = GST_VIDEO_FORMAT_GBR;
150       else
151         *fmt = GST_VIDEO_FORMAT_Y444;
152       return TRUE;
153 #ifdef VPX_IMG_FMT_I440
154     case VPX_IMG_FMT_I440:
155       /* Planar, half height, full width U/V */
156       GST_FIXME_OBJECT (dec, "Please add a 4:4:0 planar frame format");
157       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
158           (NULL), ("Unsupported frame format - 4:4:0 planar"));
159       return FALSE;
160 #endif
161 #ifdef VPX_IMG_FMT_I42016
162     case VPX_IMG_FMT_I42016:
163       /* VPX_IMG_FMT_I420 | VPX_IMG_FMT_HIGHBITDEPTH */
164       GST_FIXME_OBJECT (dec, "Please add 16-bit I420 format");
165       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
166           (NULL), ("Unsupported frame format - 16-bit 4:2:0 planar"));
167       return FALSE;
168 #endif
169 #ifdef VPX_IMG_FMT_I42216
170     case VPX_IMG_FMT_I42216:
171       /* VPX_IMG_FMT_I422 | VPX_IMG_FMT_HIGHBITDEPTH */
172       GST_FIXME_OBJECT (dec, "Please add 16-bit Y42B format");
173       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
174           (NULL), ("Unsupported frame format - 16-bit 4:2:2 planar"));
175       return FALSE;
176 #endif
177 #ifdef VPX_IMG_FMT_I44416
178     case VPX_IMG_FMT_I44416:
179       /* VPX_IMG_FMT_I444 | VPX_IMG_FMT_HIGHBITDEPTH */
180       GST_FIXME_OBJECT (dec, "Please add 16-bit Y444 format");
181       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
182           (NULL), ("Unsupported frame format - 16-bit 4:4:4 planar"));
183       return FALSE;
184 #endif
185 #ifdef VPX_IMG_FMT_I44016
186     case VPX_IMG_FMT_I44016:
187       /* VPX_IMG_FMT_I440 | VPX_IMG_FMT_HIGHBITDEPTH */
188       GST_FIXME_OBJECT (dec, "Please add 16-bit 4:4:0 planar frame format");
189       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
190           (NULL), ("Unsupported frame format - 16-bit 4:4:0 planar"));
191       return FALSE;
192 #endif
193     default:
194       return FALSE;
195   }
196 }
197 
198 static void
gst_vp9_dec_handle_resolution_change(GstVPXDec * dec,vpx_image_t * img,GstVideoFormat fmt)199 gst_vp9_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
200     GstVideoFormat fmt)
201 {
202   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
203 
204   if (!dec->output_state || dec->output_state->info.finfo->format != fmt ||
205       dec->output_state->info.width != img->d_w ||
206       dec->output_state->info.height != img->d_h) {
207     gboolean send_tags = !dec->output_state;
208 
209     if (dec->output_state)
210       gst_video_codec_state_unref (dec->output_state);
211 
212     dec->output_state =
213         gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
214         fmt, img->d_w, img->d_h, dec->input_state);
215     gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
216 
217     if (send_tags)
218       vpxclass->send_tags (dec);
219   }
220 }
221 
222 #endif /* HAVE_VP9_DECODER */
223