2 * Copyright (C) 2008 David Schleef <ds@schleef.org>
3 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
4 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * Copyright (C) 2012 Collabora Ltd.
7 * Author : Edward Hervey <edward@collabora.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 #ifndef _GST_VIDEO_DECODER_H_
26 #define _GST_VIDEO_DECODER_H_
28 #include <gst/base/gstadapter.h>
29 #include <gst/video/video.h>
30 #include <gst/video/gstvideoutils.h>
34 #define GST_TYPE_VIDEO_DECODER \
35 (gst_video_decoder_get_type())
36 #define GST_VIDEO_DECODER(obj) \
37 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoder))
38 #define GST_VIDEO_DECODER_CLASS(klass) \
39 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
40 #define GST_VIDEO_DECODER_GET_CLASS(obj) \
41 (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
42 #define GST_IS_VIDEO_DECODER(obj) \
43 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_DECODER))
44 #define GST_IS_VIDEO_DECODER_CLASS(obj) \
45 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_DECODER))
48 * GST_VIDEO_DECODER_SINK_NAME:
50 * The name of the templates for the sink pad.
54 #define GST_VIDEO_DECODER_SINK_NAME "sink"
56 * GST_VIDEO_DECODER_SRC_NAME:
58 * The name of the templates for the source pad.
62 #define GST_VIDEO_DECODER_SRC_NAME "src"
65 * GST_VIDEO_DECODER_SRC_PAD:
66 * @obj: a #GstVideoDecoder
68 * Gives the pointer to the source #GstPad object of the element.
72 #define GST_VIDEO_DECODER_SRC_PAD(obj) (((GstVideoDecoder *) (obj))->srcpad)
75 * GST_VIDEO_DECODER_SINK_PAD:
76 * @obj: a #GstVideoDecoder
78 * Gives the pointer to the sink #GstPad object of the element.
82 #define GST_VIDEO_DECODER_SINK_PAD(obj) (((GstVideoDecoder *) (obj))->sinkpad)
84 * GST_VIDEO_DECODER_FLOW_NEED_DATA:
86 * Returned while parsing to indicate more data is needed.
90 #define GST_VIDEO_DECODER_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS
93 * GST_VIDEO_DECODER_INPUT_SEGMENT:
94 * @obj: base decoder instance
96 * Gives the segment of the element.
100 #define GST_VIDEO_DECODER_INPUT_SEGMENT(obj) (GST_VIDEO_DECODER_CAST (obj)->input_segment)
103 * GST_VIDEO_DECODER_OUTPUT_SEGMENT:
104 * @obj: base decoder instance
106 * Gives the segment of the element.
110 #define GST_VIDEO_DECODER_OUTPUT_SEGMENT(obj) (GST_VIDEO_DECODER_CAST (obj)->output_segment)
113 * GST_VIDEO_DECODER_STREAM_LOCK:
114 * @decoder: video decoder instance
116 * Obtain a lock to protect the decoder function from concurrent access.
120 #define GST_VIDEO_DECODER_STREAM_LOCK(decoder) g_rec_mutex_lock (&GST_VIDEO_DECODER (decoder)->stream_lock)
123 * GST_VIDEO_DECODER_STREAM_UNLOCK:
124 * @decoder: video decoder instance
126 * Release the lock that protects the decoder function from concurrent access.
130 #define GST_VIDEO_DECODER_STREAM_UNLOCK(decoder) g_rec_mutex_unlock (&GST_VIDEO_DECODER (decoder)->stream_lock)
132 typedef struct _GstVideoDecoder GstVideoDecoder;
133 typedef struct _GstVideoDecoderClass GstVideoDecoderClass;
134 typedef struct _GstVideoDecoderPrivate GstVideoDecoderPrivate;
137 /* do not use this one, use macro below */
138 GstFlowReturn _gst_video_decoder_error (GstVideoDecoder *dec, gint weight,
139 GQuark domain, gint code,
140 gchar *txt, gchar *debug,
141 const gchar *file, const gchar *function,
145 * GST_VIDEO_DECODER_ERROR:
146 * @el: the base video decoder element that generates the error
147 * @weight: element defined weight of the error, added to error count
148 * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
149 * @code: error code defined for that domain (see #gstreamer-GstGError)
150 * @text: the message to display (format string and args enclosed in
152 * @debug: debugging information for the message (format string and args
153 * enclosed in parentheses)
154 * @ret: variable to receive return value
156 * Utility function that video decoder elements can use in case they encountered
157 * a data processing error that may be fatal for the current "data unit" but
158 * need not prevent subsequent decoding. Such errors are counted and if there
159 * are too many, as configured in the context's max_errors, the pipeline will
160 * post an error message and the application will be requested to stop further
161 * media processing. Otherwise, it is considered a "glitch" and only a warning
162 * is logged. In either case, @ret is set to the proper value to
163 * return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
167 #define GST_VIDEO_DECODER_ERROR(el, w, domain, code, text, debug, ret) \
169 gchar *__txt = _gst_element_error_printf text; \
170 gchar *__dbg = _gst_element_error_printf debug; \
171 GstVideoDecoder *dec = GST_VIDEO_DECODER (el); \
172 ret = _gst_video_decoder_error (dec, w, GST_ ## domain ## _ERROR, \
173 GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__, \
174 GST_FUNCTION, __LINE__); \
178 * GST_VIDEO_DECODER_MAX_ERRORS:
180 * Default maximum number of errors tolerated before signaling error.
184 #define GST_VIDEO_DECODER_MAX_ERRORS 10
190 * The opaque #GstVideoDecoder data structure.
194 struct _GstVideoDecoder
203 /* protects all data processing, i.e. is locked
204 * in the chain function, finish_frame and when
205 * processing serialized events */
206 GRecMutex stream_lock;
208 /* MT-protected (with STREAM_LOCK) */
209 GstSegment input_segment;
210 GstSegment output_segment;
212 GstVideoDecoderPrivate *priv;
214 /* FIXME before moving to base */
215 void *padding[GST_PADDING_LARGE];
219 * GstVideoDecoderClass:
221 * Called when the element changes to GST_STATE_READY.
222 * Allows opening external resources.
224 * Called when the element changes to GST_STATE_NULL.
225 * Allows closing external resources.
227 * Called when the element starts processing.
228 * Allows opening external resources.
230 * Called when the element stops processing.
231 * Allows closing external resources.
232 * @set_format: Notifies subclass of incoming data format (caps).
233 * @parse: Required for non-packetized input.
234 * Allows chopping incoming data into manageable units (frames)
235 * for subsequent decoding.
237 * Allows subclass (decoder) to perform post-seek semantics reset.
238 * @handle_frame: Provides input data frame to subclass.
240 * Called to request subclass to dispatch any pending remaining
241 * data (e.g. at EOS).
242 * @sink_event: Optional.
243 * Event handler on the sink pad. This function should return
244 * TRUE if the event was handled and should be discarded
245 * (i.e. not unref'ed).
246 * @src_event: Optional.
247 * Event handler on the source pad. This function should return
248 * TRUE if the event was handled and should be discarded
249 * (i.e. not unref'ed).
250 * @configure_buffer_pool: Optional.
251 * Configure the buffer that is used for allocation of output
252 * buffers. The passed query contains the result of the allocation
253 * query. The default implementation will add the VIDEO_META if
254 * supported by the buffer pool.
255 * Subclasses can override any of the available virtual methods or not, as
256 * needed. At minimum @handle_frame needs to be overridden, and @set_format
257 * and likely as well. If non-packetized input is supported or expected,
258 * @parse needs to be overridden as well.
262 struct _GstVideoDecoderClass
265 GstElementClass element_class;
268 gboolean (*open) (GstVideoDecoder *decoder);
270 gboolean (*close) (GstVideoDecoder *decoder);
272 gboolean (*start) (GstVideoDecoder *decoder);
274 gboolean (*stop) (GstVideoDecoder *decoder);
276 GstFlowReturn (*parse) (GstVideoDecoder *decoder,
277 GstVideoCodecFrame *frame,
281 gboolean (*set_format) (GstVideoDecoder *decoder,
282 GstVideoCodecState * state);
284 gboolean (*reset) (GstVideoDecoder *decoder,
287 GstFlowReturn (*finish) (GstVideoDecoder *decoder);
289 GstFlowReturn (*handle_frame) (GstVideoDecoder *decoder,
290 GstVideoCodecFrame *frame);
292 gboolean (*sink_event) (GstVideoDecoder *decoder,
295 gboolean (*src_event) (GstVideoDecoder *decoder,
298 gboolean (*configure_buffer_pool) (GstVideoDecoder *decoder, GstQuery *query, GstBufferPool *pool);
302 /* FIXME before moving to base */
303 void *padding[GST_PADDING_LARGE];
306 GType gst_video_decoder_get_type (void);
308 /* Context parameters */
309 void gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
310 gboolean packetized);
312 gboolean gst_video_decoder_get_packetized (GstVideoDecoder * decoder);
314 void gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec,
317 gint gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec);
319 void gst_video_decoder_set_max_errors (GstVideoDecoder * dec,
322 gint gst_video_decoder_get_max_errors (GstVideoDecoder * dec);
324 void gst_video_decoder_set_latency (GstVideoDecoder *decoder,
325 GstClockTime min_latency,
326 GstClockTime max_latency);
327 void gst_video_decoder_get_latency (GstVideoDecoder *decoder,
328 GstClockTime *min_latency,
329 GstClockTime *max_latency);
334 GstVideoCodecFrame *gst_video_decoder_get_frame (GstVideoDecoder *decoder,
337 GstVideoCodecFrame *gst_video_decoder_get_oldest_frame (GstVideoDecoder *decoder);
339 /* Parsing related methods */
340 void gst_video_decoder_add_to_frame (GstVideoDecoder *decoder,
342 GstFlowReturn gst_video_decoder_have_frame (GstVideoDecoder *decoder);
344 GstBuffer *gst_video_decoder_alloc_output_buffer (GstVideoDecoder * decoder);
346 GstFlowReturn gst_video_decoder_alloc_output_frame (GstVideoDecoder *decoder,
347 GstVideoCodecFrame *frame);
349 GstVideoCodecState *gst_video_decoder_set_output_state (GstVideoDecoder *decoder,
350 GstVideoFormat fmt, guint width, guint height,
351 GstVideoCodecState *reference);
353 GstVideoCodecState *gst_video_decoder_get_output_state (GstVideoDecoder *decoder);
355 GstClockTimeDiff gst_video_decoder_get_max_decode_time (GstVideoDecoder *decoder,
356 GstVideoCodecFrame *frame);
358 GstFlowReturn gst_video_decoder_finish_frame (GstVideoDecoder *decoder,
359 GstVideoCodecFrame *frame);
361 GstFlowReturn gst_video_decoder_drop_frame (GstVideoDecoder *dec,
362 GstVideoCodecFrame *frame);