rtspsrc: do not try to send EOS with invalid seqnum
[platform/upstream/gst-plugins-good.git] / ext / vpx / gstvp9dec.c
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 }"))
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
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
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
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
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       *fmt = GST_VIDEO_FORMAT_Y444;
149       return TRUE;
150 #ifdef VPX_IMG_FMT_I440
151     case VPX_IMG_FMT_I440:
152       /* Planar, half height, full width U/V */
153       GST_FIXME_OBJECT (dec, "Please add a 4:4:0 planar frame format");
154       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
155           (NULL), ("Unsupported frame format - 4:4:0 planar"));
156       return FALSE;
157 #endif
158 #ifdef VPX_IMG_FMT_I42016
159     case VPX_IMG_FMT_I42016:
160       /* VPX_IMG_FMT_I420 | VPX_IMG_FMT_HIGHBITDEPTH */
161       GST_FIXME_OBJECT (dec, "Please add 16-bit I420 format");
162       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
163           (NULL), ("Unsupported frame format - 16-bit 4:2:0 planar"));
164       return FALSE;
165 #endif
166 #ifdef VPX_IMG_FMT_I42216
167     case VPX_IMG_FMT_I42216:
168       /* VPX_IMG_FMT_I422 | VPX_IMG_FMT_HIGHBITDEPTH */
169       GST_FIXME_OBJECT (dec, "Please add 16-bit Y42B format");
170       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
171           (NULL), ("Unsupported frame format - 16-bit 4:2:2 planar"));
172       return FALSE;
173 #endif
174 #ifdef VPX_IMG_FMT_I44416
175     case VPX_IMG_FMT_I44416:
176       /* VPX_IMG_FMT_I444 | VPX_IMG_FMT_HIGHBITDEPTH */
177       GST_FIXME_OBJECT (dec, "Please add 16-bit Y444 format");
178       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
179           (NULL), ("Unsupported frame format - 16-bit 4:4:4 planar"));
180       return FALSE;
181 #endif
182 #ifdef VPX_IMG_FMT_I44016
183     case VPX_IMG_FMT_I44016:
184       /* VPX_IMG_FMT_I440 | VPX_IMG_FMT_HIGHBITDEPTH */
185       GST_FIXME_OBJECT (dec, "Please add 16-bit 4:4:0 planar frame format");
186       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED,
187           (NULL), ("Unsupported frame format - 16-bit 4:4:0 planar"));
188       return FALSE;
189 #endif
190     default:
191       return FALSE;
192   }
193 }
194
195 static void
196 gst_vp9_dec_handle_resolution_change (GstVPXDec * dec, vpx_image_t * img,
197     GstVideoFormat fmt)
198 {
199   GstVPXDecClass *vpxclass = GST_VPX_DEC_GET_CLASS (dec);
200
201   if (!dec->output_state || dec->output_state->info.finfo->format != fmt ||
202       dec->output_state->info.width != img->d_w ||
203       dec->output_state->info.height != img->d_h) {
204     gboolean send_tags = !dec->output_state;
205
206     if (dec->output_state)
207       gst_video_codec_state_unref (dec->output_state);
208
209     dec->output_state =
210         gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec),
211         fmt, img->d_w, img->d_h, dec->input_state);
212     gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec));
213
214     if (send_tags)
215       vpxclass->send_tags (dec);
216   }
217 }
218
219 #endif /* HAVE_VP9_DECODER */