2 * Copyright (C) 2010 Ole André Vadla Ravnås <oravnas@cisco.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "vth264decbin.h"
27 #include <gst/video/video.h>
29 #define VT_H264_DEC_BIN_ERROR_STATE_DEFAULT FALSE
31 GST_DEBUG_CATEGORY_STATIC (gst_vt_h264_dec_bin_debug);
32 #define GST_CAT_DEFAULT gst_vt_h264_dec_bin_debug
43 H264PARSE_OUTPUT_FORMAT_AVC_SAMPLE = 0,
44 H264PARSE_OUTPUT_FORMAT_BYTE_STREAM = 1,
45 H264PARSE_OUTPUT_FORMAT_INPUT = 2
48 static GstStaticPadTemplate vth264decbin_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS ("video/x-h264, "
53 "stream-format = (string) { byte-stream, avc }")
56 static GstStaticPadTemplate vth264decbin_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("NV12"))
63 #define TAA_VT_H264_DEC_BIN_GET_PRIVATE(obj) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VT_H264_DEC_BIN, \
65 GstVTH264DecBinPrivate))
67 struct _GstVTH264DecBinPrivate
70 GstPad *parser_sinkpad;
73 GstPad *decoder_srcpad;
78 GstClockTime prev_input_ts;
83 GST_BOILERPLATE (GstVTH264DecBin, gst_vt_h264_dec_bin, GstBin, GST_TYPE_BIN);
85 static gboolean gst_vt_h264_dec_bin_on_output (GstPad * pad,
86 GstMiniObject * mini_obj, gpointer user_data);
89 gst_vt_h264_dec_bin_update_error_state (GstVTH264DecBin * self,
92 GstVTH264DecBinPrivate *priv = self->priv;
93 GObject *obj = (GObject *) self;
95 GST_OBJECT_LOCK (self);
96 priv->error_state = error_state;
97 GST_OBJECT_UNLOCK (self);
99 if (priv->output_probe == 0 && (error_state || !priv->seen_output)) {
100 GST_DEBUG_OBJECT (self, "attaching buffer probe");
101 priv->output_probe = gst_pad_add_buffer_probe (priv->decoder_srcpad,
102 G_CALLBACK (gst_vt_h264_dec_bin_on_output), self);
103 } else if (priv->output_probe != 0 && (!error_state && priv->seen_output)) {
104 GST_DEBUG_OBJECT (self, "detaching buffer probe");
105 gst_pad_remove_buffer_probe (priv->decoder_srcpad, priv->output_probe);
106 priv->output_probe = 0;
109 g_object_notify (obj, "error-state");
110 g_object_notify (obj, "happy");
114 gst_vt_h264_dec_bin_sink_setcaps (GstPad * pad, GstCaps * caps)
116 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (GST_PAD_PARENT (pad));
119 gboolean access_unit;
121 format = gst_structure_get_string (gst_caps_get_structure (caps, 0),
124 goto no_stream_format;
126 if (strcmp (format, "byte-stream") == 0) {
127 output_format = H264PARSE_OUTPUT_FORMAT_AVC_SAMPLE;
130 output_format = H264PARSE_OUTPUT_FORMAT_INPUT;
134 g_object_set (self->priv->parser, "output-format", output_format,
135 "access-unit", access_unit, NULL);
137 return gst_pad_set_caps (GST_PAD_PEER (self->priv->parser_sinkpad), caps);
144 gst_vt_h264_dec_bin_sink_event (GstPad * pad, GstEvent * event)
146 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (GST_PAD_PARENT (pad));
147 GstVTH264DecBinPrivate *priv = self->priv;
149 switch (GST_EVENT_TYPE (event)) {
150 case GST_EVENT_NEWSEGMENT:
151 if (priv->seen_output) {
152 GST_DEBUG_OBJECT (self, "error state ON because of packetloss");
153 gst_vt_h264_dec_bin_update_error_state (self, TRUE);
156 case GST_EVENT_FLUSH_STOP:
157 priv->seen_output = FALSE;
158 priv->prev_input_ts = GST_CLOCK_TIME_NONE;
159 GST_DEBUG_OBJECT (self, "error state OFF because of FLUSH_STOP");
160 gst_vt_h264_dec_bin_update_error_state (self, FALSE);
166 return gst_pad_push_event (GST_PAD_PEER (priv->parser_sinkpad), event);
170 gst_vt_h264_dec_bin_sink_chain (GstPad * pad, GstBuffer * buffer)
172 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (GST_PAD_PARENT (pad));
173 GstVTH264DecBinPrivate *priv = self->priv;
175 GstFlowReturn flow_ret;
177 cur_ts = GST_BUFFER_TIMESTAMP (buffer);
179 gst_vt_h264_dec_bin_update_error_state (self, priv->error_state);
181 flow_ret = gst_pad_push (GST_PAD_PEER (priv->parser_sinkpad), buffer);
183 if (!priv->seen_output && !priv->error_state &&
184 GST_CLOCK_TIME_IS_VALID (priv->prev_input_ts)) {
185 if (cur_ts != priv->prev_input_ts) {
186 GST_DEBUG_OBJECT (self,
187 "error state ON because of no output and detected timestamp gap");
188 gst_vt_h264_dec_bin_update_error_state (self, TRUE);
191 priv->prev_input_ts = cur_ts;
197 gst_vt_h264_dec_bin_on_output (GstPad * pad, GstMiniObject * mini_obj,
200 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (user_data);
202 self->priv->seen_output = TRUE;
204 GST_DEBUG_OBJECT (self, "error state OFF because we saw output");
205 gst_vt_h264_dec_bin_update_error_state (self, FALSE);
211 gst_vt_h264_dec_bin_base_init (gpointer gclass)
213 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
215 gst_element_class_set_details_simple (element_class,
218 "VideoToolbox H.264 decoder bin",
219 "Ole André Vadla Ravnås <oravnas@cisco.com>");
221 gst_element_class_add_static_pad_template (element_class,
222 &vth264decbin_sink_template);
223 gst_element_class_add_static_pad_template (element_class,
224 &vth264decbin_src_template);
228 gst_vt_h264_dec_bin_init (GstVTH264DecBin * self, GstVTH264DecBinClass * gclass)
230 GstVTH264DecBinPrivate *priv;
233 self->priv = priv = TAA_VT_H264_DEC_BIN_GET_PRIVATE (self);
235 priv->parser = gst_element_factory_make ("h264parse", "parser");
236 priv->decoder = gst_element_factory_make ("vtdec_h264", "decoder");
237 gst_bin_add_many (GST_BIN_CAST (self), priv->parser, priv->decoder, NULL);
238 gst_element_link (priv->parser, priv->decoder);
240 priv->parser_sinkpad = gst_element_get_static_pad (priv->parser, "sink");
241 ghost_pad = gst_ghost_pad_new_from_template ("sink", priv->parser_sinkpad,
242 gst_static_pad_template_get (&vth264decbin_sink_template));
243 gst_pad_set_setcaps_function (ghost_pad, gst_vt_h264_dec_bin_sink_setcaps);
244 gst_pad_set_event_function (ghost_pad, gst_vt_h264_dec_bin_sink_event);
245 gst_pad_set_chain_function (ghost_pad, gst_vt_h264_dec_bin_sink_chain);
246 gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
248 priv->decoder_srcpad = gst_element_get_static_pad (priv->decoder, "src");
249 ghost_pad = gst_ghost_pad_new_from_template ("src", priv->decoder_srcpad,
250 gst_static_pad_template_get (&vth264decbin_src_template));
251 gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
253 priv->seen_output = FALSE;
254 priv->prev_input_ts = GST_CLOCK_TIME_NONE;
258 gst_vt_h264_dec_bin_dispose (GObject * obj)
260 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (obj);
261 GstVTH264DecBinPrivate *priv = self->priv;
263 if (priv->parser_sinkpad != NULL) {
264 gst_object_unref (priv->parser_sinkpad);
265 priv->parser_sinkpad = NULL;
268 if (priv->decoder_srcpad != NULL) {
269 gst_object_unref (priv->decoder_srcpad);
270 priv->decoder_srcpad = NULL;
273 G_OBJECT_CLASS (parent_class)->dispose (obj);
277 gst_vt_h264_dec_bin_get_property (GObject * obj, guint prop_id,
278 GValue * value, GParamSpec * pspec)
280 GstVTH264DecBin *self = GST_VT_H264_DEC_BIN_CAST (obj);
283 case PROP_ERROR_STATE:
284 GST_OBJECT_LOCK (self);
285 g_value_set_boolean (value, self->priv->error_state);
286 GST_OBJECT_UNLOCK (self);
289 GST_OBJECT_LOCK (self);
290 g_value_set_boolean (value, !self->priv->error_state);
291 GST_OBJECT_UNLOCK (self);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
300 gst_vt_h264_dec_bin_class_init (GstVTH264DecBinClass * klass)
302 GObjectClass *gobject_class = (GObjectClass *) klass;
304 gobject_class->dispose = gst_vt_h264_dec_bin_dispose;
305 gobject_class->get_property = gst_vt_h264_dec_bin_get_property;
307 g_type_class_add_private (klass, sizeof (GstVTH264DecBinPrivate));
309 g_object_class_install_property (gobject_class, PROP_ERROR_STATE,
310 g_param_spec_boolean ("error-state", "Error State",
311 "Whether the decoder is currently in an error state",
312 VT_H264_DEC_BIN_ERROR_STATE_DEFAULT,
313 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_HAPPY,
315 g_param_spec_boolean ("happy", "Happy",
316 "Whether the decoder is currently not in an error state",
317 !VT_H264_DEC_BIN_ERROR_STATE_DEFAULT,
318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 GST_DEBUG_CATEGORY_INIT (gst_vt_h264_dec_bin_debug,
321 "vth264decbin", 0, "VideoToolbox H.264 decoder bin");