• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: vp9dec
25  * @see_also: vp9enc, matroskademux
26  *
27  * This element decodes VP9 streams into raw video.
28  * [VP9](http://www.webmproject.org) is a royalty-free video codec maintained by
29  * [Google](http://www.google.com/) It's the successor of On2 VP3, which was the
30  * base of the Theora video codec.
31  *
32  * ## Example pipeline
33  * |[
34  * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! vp9dec ! videoconvert ! videoscale ! autovideosink
35  * ]| This example pipeline will decode a WebM stream and decodes the VP9 video.
36  *
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 
43 #ifdef HAVE_VP9_DECODER
44 
45 #include <string.h>
46 
47 #include "gstvpxelements.h"
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 static gboolean gst_vp9_dec_get_needs_sync_point (GstVPXDec * dec);
66 
67 static GstStaticPadTemplate gst_vp9_dec_sink_template =
68 GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("video/x-vp9")
72     );
73 
74 static GstStaticPadTemplate gst_vp9_dec_src_template =
75 GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
79         ("{ I420, YV12, Y42B, Y444, GBR, I420_10LE, I422_10LE }"))
80     );
81 
82 #define parent_class gst_vp9_dec_parent_class
83 G_DEFINE_TYPE (GstVP9Dec, gst_vp9_dec, GST_TYPE_VPX_DEC);
84 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (vp9dec, "vp9dec", GST_RANK_PRIMARY,
85     gst_vp9_dec_get_type (), vpx_element_init (plugin));
86 
87 static void
gst_vp9_dec_class_init(GstVP9DecClass * klass)88 gst_vp9_dec_class_init (GstVP9DecClass * klass)
89 {
90   GstElementClass *element_class;
91   GstVPXDecClass *vpx_class;
92 
93   element_class = GST_ELEMENT_CLASS (klass);
94   vpx_class = GST_VPX_DEC_CLASS (klass);
95 
96   gst_element_class_add_static_pad_template (element_class,
97       &gst_vp9_dec_src_template);
98   gst_element_class_add_static_pad_template (element_class,
99       &gst_vp9_dec_sink_template);
100 
101   gst_element_class_set_static_metadata (element_class,
102       "On2 VP9 Decoder",
103       "Codec/Decoder/Video",
104       "Decode VP9 video streams", "David Schleef <ds@entropywave.com>, "
105       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
106 
107   vpx_class->video_codec_tag = VP9_DECODER_VIDEO_TAG;
108   vpx_class->codec_algo = &vpx_codec_vp9_dx_algo;
109   vpx_class->set_stream_info = GST_DEBUG_FUNCPTR (gst_vp9_dec_set_stream_info);
110   vpx_class->get_frame_format =
111       GST_DEBUG_FUNCPTR (gst_vp9_dec_get_valid_format);
112   vpx_class->handle_resolution_change =
113       GST_DEBUG_FUNCPTR (gst_vp9_dec_handle_resolution_change);
114   vpx_class->get_needs_sync_point =
115       GST_DEBUG_FUNCPTR (gst_vp9_dec_get_needs_sync_point);
116 
117   GST_DEBUG_CATEGORY_INIT (gst_vp9dec_debug, "vp9dec", 0, "VP9 Decoder");
118 }
119 
120 static void
gst_vp9_dec_init(GstVP9Dec * gst_vp9_dec)121 gst_vp9_dec_init (GstVP9Dec * gst_vp9_dec)
122 {
123   GST_DEBUG_OBJECT (gst_vp9_dec, "gst_vp9_dec_init");
124 }
125 
126 static void
gst_vp9_dec_set_stream_info(GstVPXDec * dec,vpx_codec_stream_info_t * stream_info)127 gst_vp9_dec_set_stream_info (GstVPXDec * dec,
128     vpx_codec_stream_info_t * stream_info)
129 {
130   /* FIXME: peek_stream_info() does not return valid values, take input caps */
131   stream_info->w = dec->input_state->info.width;
132   stream_info->h = dec->input_state->info.height;
133   return;
134 }
135 
136 static gboolean
gst_vp9_dec_get_valid_format(GstVPXDec * dec,vpx_image_t * img,GstVideoFormat * fmt)137 gst_vp9_dec_get_valid_format (GstVPXDec * dec, vpx_image_t * img,
138     GstVideoFormat * fmt)
139 {
140   switch (img->fmt) {
141     case VPX_IMG_FMT_I420:
142       *fmt = GST_VIDEO_FORMAT_I420;
143       return TRUE;
144 
145     case VPX_IMG_FMT_YV12:
146       *fmt = GST_VIDEO_FORMAT_YV12;
147       return TRUE;
148 
149     case VPX_IMG_FMT_I422:
150       *fmt = GST_VIDEO_FORMAT_Y42B;
151       return TRUE;
152 
153     case VPX_IMG_FMT_I444:
154       if (img->cs == VPX_CS_SRGB)
155         *fmt = GST_VIDEO_FORMAT_GBR;
156       else
157         *fmt = GST_VIDEO_FORMAT_Y444;
158       return TRUE;
159 #ifdef VPX_IMG_FMT_I440
160     case VPX_IMG_FMT_I440:
161       /* Planar, half height, full width U/V */
162       GST_FIXME_OBJECT (dec, "Please add a 4:4:0 planar frame format");
163       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
164           (NULL), ("Unsupported frame format - 4:4:0 planar"));
165       return FALSE;
166 #endif
167     case VPX_IMG_FMT_I42016:
168       /* VPX_IMG_FMT_I420 | VPX_IMG_FMT_HIGHBITDEPTH */
169       if (img->bit_depth == 10) {
170         *fmt = GST_VIDEO_FORMAT_I420_10LE;
171         return TRUE;
172       }
173       GST_FIXME_OBJECT (dec, "Please add 16-bit I420 format");
174       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
175           (NULL), ("Unsupported frame format - 16-bit 4:2:0 planar"));
176       return FALSE;
177     case VPX_IMG_FMT_I42216:
178       /* VPX_IMG_FMT_I422 | VPX_IMG_FMT_HIGHBITDEPTH */
179       if (img->bit_depth == 10) {
180         *fmt = GST_VIDEO_FORMAT_I422_10LE;
181         return TRUE;
182       }
183       GST_FIXME_OBJECT (dec, "Please add 16-bit Y42B format");
184       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
185           (NULL), ("Unsupported frame format - 16-bit 4:2:2 planar"));
186       return FALSE;
187 #ifdef VPX_IMG_FMT_I44416
188     case VPX_IMG_FMT_I44416:
189       /* VPX_IMG_FMT_I444 | VPX_IMG_FMT_HIGHBITDEPTH */
190       GST_FIXME_OBJECT (dec, "Please add 16-bit Y444 format");
191       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
192           (NULL), ("Unsupported frame format - 16-bit 4:4:4 planar"));
193       return FALSE;
194 #endif
195 #ifdef VPX_IMG_FMT_I44016
196     case VPX_IMG_FMT_I44016:
197       /* VPX_IMG_FMT_I440 | VPX_IMG_FMT_HIGHBITDEPTH */
198       GST_FIXME_OBJECT (dec, "Please add 16-bit 4:4:0 planar frame format");
199       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
200           (NULL), ("Unsupported frame format - 16-bit 4:4:0 planar"));
201       return FALSE;
202 #endif
203     default:
204       return FALSE;
205   }
206 }
207 
208 static void
gst_vp9_dec_handle_resolution_change(GstVPXDec * dec,vpx_image_t * img,GstVideoFormat fmt)209 gst_vp9_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
210     GstVideoFormat fmt)
211 {
212   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
213 
214   if (!dec->output_state || dec->output_state->info.finfo->format != fmt ||
215       dec->output_state->info.width != img->d_w ||
216       dec->output_state->info.height != img->d_h) {
217     gboolean send_tags = !dec->output_state;
218 
219     if (dec->output_state)
220       gst_video_codec_state_unref (dec->output_state);
221 
222     dec->output_state =
223         gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
224         fmt, img->d_w, img->d_h, dec->input_state);
225     gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
226 
227     if (send_tags)
228       vpxclass->send_tags (dec);
229   }
230 }
231 
232 static gboolean
gst_vp9_dec_get_needs_sync_point(GstVPXDec * dec)233 gst_vp9_dec_get_needs_sync_point (GstVPXDec * dec)
234 {
235   return TRUE;
236 }
237 
238 #endif /* HAVE_VP9_DECODER */
239