video: add gst_video_decoder_allocate_output_frame_with_params
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideodecoder.h
1 /* GStreamer
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>
8  *
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.
13  *
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.
18  *
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., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #ifndef _GST_VIDEO_DECODER_H_
26 #define _GST_VIDEO_DECODER_H_
27
28 #include <gst/base/gstadapter.h>
29 #include <gst/video/gstvideoutils.h>
30
31 G_BEGIN_DECLS
32
33 #define GST_TYPE_VIDEO_DECODER \
34   (gst_video_decoder_get_type())
35 #define GST_VIDEO_DECODER(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoder))
37 #define GST_VIDEO_DECODER_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
39 #define GST_VIDEO_DECODER_GET_CLASS(obj) \
40   (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
41 #define GST_IS_VIDEO_DECODER(obj) \
42   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_DECODER))
43 #define GST_IS_VIDEO_DECODER_CLASS(klass) \
44   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_DECODER))
45 #define GST_VIDEO_DECODER_CAST(obj) ((GstVideoDecoder *)(obj))
46
47 /**
48  * GST_VIDEO_DECODER_SINK_NAME:
49  *
50  * The name of the templates for the sink pad.
51  */
52 #define GST_VIDEO_DECODER_SINK_NAME    "sink"
53 /**
54  * GST_VIDEO_DECODER_SRC_NAME:
55  *
56  * The name of the templates for the source pad.
57  */
58 #define GST_VIDEO_DECODER_SRC_NAME     "src"
59
60 /**
61  * GST_VIDEO_DECODER_SRC_PAD:
62  * @obj: a #GstVideoDecoder
63  *
64  * Gives the pointer to the source #GstPad object of the element.
65  */
66 #define GST_VIDEO_DECODER_SRC_PAD(obj)         (((GstVideoDecoder *) (obj))->srcpad)
67
68 /**
69  * GST_VIDEO_DECODER_SINK_PAD:
70  * @obj: a #GstVideoDecoder
71  *
72  * Gives the pointer to the sink #GstPad object of the element.
73  */
74 #define GST_VIDEO_DECODER_SINK_PAD(obj)        (((GstVideoDecoder *) (obj))->sinkpad)
75 /**
76  * GST_VIDEO_DECODER_FLOW_NEED_DATA:
77  *
78  * Returned while parsing to indicate more data is needed.
79  **/
80 #define GST_VIDEO_DECODER_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS
81
82 /**
83  * GST_VIDEO_DECODER_INPUT_SEGMENT:
84  * @obj: base decoder instance
85  *
86  * Gives the segment of the element.
87  */
88 #define GST_VIDEO_DECODER_INPUT_SEGMENT(obj)     (GST_VIDEO_DECODER_CAST (obj)->input_segment)
89
90 /**
91  * GST_VIDEO_DECODER_OUTPUT_SEGMENT:
92  * @obj: base decoder instance
93  *
94  * Gives the segment of the element.
95  */
96 #define GST_VIDEO_DECODER_OUTPUT_SEGMENT(obj)     (GST_VIDEO_DECODER_CAST (obj)->output_segment)
97
98 /**
99  * GST_VIDEO_DECODER_STREAM_LOCK:
100  * @decoder: video decoder instance
101  *
102  * Obtain a lock to protect the decoder function from concurrent access.
103  */
104 #define GST_VIDEO_DECODER_STREAM_LOCK(decoder) g_rec_mutex_lock (&GST_VIDEO_DECODER (decoder)->stream_lock)
105
106 /**
107  * GST_VIDEO_DECODER_STREAM_UNLOCK:
108  * @decoder: video decoder instance
109  *
110  * Release the lock that protects the decoder function from concurrent access.
111  */
112 #define GST_VIDEO_DECODER_STREAM_UNLOCK(decoder) g_rec_mutex_unlock (&GST_VIDEO_DECODER (decoder)->stream_lock)
113
114 typedef struct _GstVideoDecoder GstVideoDecoder;
115 typedef struct _GstVideoDecoderClass GstVideoDecoderClass;
116 typedef struct _GstVideoDecoderPrivate GstVideoDecoderPrivate;
117
118
119 /* do not use this one, use macro below */
120 GstFlowReturn _gst_video_decoder_error (GstVideoDecoder *dec, gint weight,
121                                              GQuark domain, gint code,
122                                              gchar *txt, gchar *debug,
123                                              const gchar *file, const gchar *function,
124                                              gint line);
125
126 /**
127  * GST_VIDEO_DECODER_ERROR:
128  * @el:     the base video decoder element that generates the error
129  * @w:      element defined weight of the error, added to error count
130  * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
131  * @code:   error code defined for that domain (see #gstreamer-GstGError)
132  * @text:   the message to display (format string and args enclosed in
133  *          parentheses)
134  * @debug:  debugging information for the message (format string and args
135  *          enclosed in parentheses)
136  * @ret:    variable to receive return value
137  *
138  * Utility function that video decoder elements can use in case they encountered
139  * a data processing error that may be fatal for the current "data unit" but
140  * need not prevent subsequent decoding.  Such errors are counted and if there
141  * are too many, as configured in the context's max_errors, the pipeline will
142  * post an error message and the application will be requested to stop further
143  * media processing.  Otherwise, it is considered a "glitch" and only a warning
144  * is logged. In either case, @ret is set to the proper value to
145  * return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
146  */
147 #define GST_VIDEO_DECODER_ERROR(el, w, domain, code, text, debug, ret) \
148 G_STMT_START {                                                              \
149   gchar *__txt = _gst_element_error_printf text;                            \
150   gchar *__dbg = _gst_element_error_printf debug;                           \
151   GstVideoDecoder *__dec = GST_VIDEO_DECODER (el);                   \
152   ret = _gst_video_decoder_error (__dec, w, GST_ ## domain ## _ERROR,    \
153       GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__,            \
154       GST_FUNCTION, __LINE__);                                              \
155 } G_STMT_END
156
157 /**
158  * GST_VIDEO_DECODER_MAX_ERRORS:
159  *
160  * Default maximum number of errors tolerated before signaling error.
161  */
162 #define GST_VIDEO_DECODER_MAX_ERRORS     10
163
164
165 /**
166  * GstVideoDecoder:
167  *
168  * The opaque #GstVideoDecoder data structure.
169  */
170 struct _GstVideoDecoder
171 {
172   /*< private >*/
173   GstElement     element;
174
175   /*< protected >*/
176   GstPad         *sinkpad;
177   GstPad         *srcpad;
178
179   /* protects all data processing, i.e. is locked
180    * in the chain function, finish_frame and when
181    * processing serialized events */
182   GRecMutex stream_lock;
183
184   /* MT-protected (with STREAM_LOCK) */
185   GstSegment      input_segment;
186   GstSegment      output_segment;
187
188   GstVideoDecoderPrivate *priv;
189
190   /*< private >*/
191   void             *padding[GST_PADDING_LARGE];
192 };
193
194 /**
195  * GstVideoDecoderClass:
196  * @open:           Optional.
197  *                  Called when the element changes to GST_STATE_READY.
198  *                  Allows opening external resources.
199  * @close:          Optional.
200  *                  Called when the element changes to GST_STATE_NULL.
201  *                  Allows closing external resources.
202  * @start:          Optional.
203  *                  Called when the element starts processing.
204  *                  Allows opening external resources.
205  * @stop:           Optional.
206  *                  Called when the element stops processing.
207  *                  Allows closing external resources.
208  * @set_format:     Notifies subclass of incoming data format (caps).
209  * @parse:          Required for non-packetized input.
210  *                  Allows chopping incoming data into manageable units (frames)
211  *                  for subsequent decoding.
212  * @reset:          Optional.
213  *                  Allows subclass (decoder) to perform post-seek semantics reset.
214  *                  Deprecated.
215  * @handle_frame:   Provides input data frame to subclass.
216  * @finish:         Optional.
217  *                  Called to request subclass to dispatch any pending remaining
218  *                  data at EOS. Sub-classes can refuse to decode new data after.
219  * @drain:          Optional.
220  *                  Called to request subclass to decode any data it can at this
221  *                  point, but that more data may arrive after. (e.g. at segment end).
222  *                  Sub-classes should be prepared to handle new data afterward,
223  *                  or seamless segment processing will break. Since: 1.6
224  * @sink_event:     Optional.
225  *                  Event handler on the sink pad. This function should return
226  *                  TRUE if the event was handled and should be discarded
227  *                  (i.e. not unref'ed).
228  *                  Subclasses should chain up to the parent implementation to
229  *                  invoke the default handler.
230  * @src_event:      Optional.
231  *                  Event handler on the source pad. This function should return
232  *                  TRUE if the event was handled and should be discarded
233  *                  (i.e. not unref'ed).
234  *                  Subclasses should chain up to the parent implementation to
235  *                  invoke the default handler.
236  * @negotiate:      Optional.
237  *                  Negotiate with downstream and configure buffer pools, etc.
238  *                  Subclasses should chain up to the parent implementation to
239  *                  invoke the default handler.
240  * @decide_allocation: Optional.
241  *                     Setup the allocation parameters for allocating output
242  *                     buffers. The passed in query contains the result of the
243  *                     downstream allocation query.
244  *                     Subclasses should chain up to the parent implementation to
245  *                     invoke the default handler.
246  * @propose_allocation: Optional.
247  *                      Propose buffer allocation parameters for upstream elements.
248  *                      Subclasses should chain up to the parent implementation to
249  *                      invoke the default handler.
250  * @flush:              Optional.
251  *                      Flush all remaining data from the decoder without
252  *                      pushing it downstream. Since: 1.2
253  * @sink_query:     Optional.
254  *                  Query handler on the sink pad. This function should
255  *                  return TRUE if the query could be performed. Subclasses
256  *                  should chain up to the parent implementation to invoke the
257  *                  default handler. Since 1.4
258  * @src_query:      Optional.
259  *                  Query handler on the source pad. This function should
260  *                  return TRUE if the query could be performed. Subclasses
261  *                  should chain up to the parent implementation to invoke the
262  *                  default handler. Since 1.4
263  * @getcaps:        Optional.
264  *                  Allows for a custom sink getcaps implementation.
265  *                  If not implemented, default returns
266  *                  gst_video_decoder_proxy_getcaps
267  *                  applied to sink template caps.
268  * @transform_meta: Optional. Transform the metadata on the input buffer to the
269  *                  output buffer. By default this method is copies all meta without
270  *                  tags and meta with only the "video" tag. subclasses can
271  *                  implement this method and return %TRUE if the metadata is to be
272  *                  copied. Since 1.6
273  *
274  * Subclasses can override any of the available virtual methods or not, as
275  * needed. At minimum @handle_frame needs to be overridden, and @set_format
276  * and likely as well.  If non-packetized input is supported or expected,
277  * @parse needs to be overridden as well.
278  */
279 struct _GstVideoDecoderClass
280 {
281   /*< private >*/
282   GstElementClass element_class;
283
284   /*< public >*/
285   gboolean      (*open)           (GstVideoDecoder *decoder);
286
287   gboolean      (*close)          (GstVideoDecoder *decoder);
288
289   gboolean      (*start)          (GstVideoDecoder *decoder);
290
291   gboolean      (*stop)           (GstVideoDecoder *decoder);
292
293   GstFlowReturn (*parse)          (GstVideoDecoder *decoder,
294                                    GstVideoCodecFrame *frame,
295                                    GstAdapter *adapter,
296                                    gboolean at_eos);
297
298   gboolean      (*set_format)     (GstVideoDecoder *decoder,
299                                    GstVideoCodecState * state);
300
301   gboolean      (*reset)          (GstVideoDecoder *decoder,
302                                    gboolean hard);
303
304   GstFlowReturn (*finish)         (GstVideoDecoder *decoder);
305
306   GstFlowReturn (*handle_frame)   (GstVideoDecoder *decoder,
307                                    GstVideoCodecFrame *frame);
308
309   gboolean      (*sink_event)     (GstVideoDecoder *decoder,
310                                    GstEvent *event);
311
312   gboolean      (*src_event)      (GstVideoDecoder *decoder,
313                                    GstEvent *event);
314
315   gboolean      (*negotiate)      (GstVideoDecoder *decoder);
316
317   gboolean      (*decide_allocation)  (GstVideoDecoder *decoder, GstQuery *query);
318
319   gboolean      (*propose_allocation) (GstVideoDecoder *decoder, GstQuery * query);
320
321   gboolean      (*flush)              (GstVideoDecoder *decoder);
322
323   gboolean      (*sink_query)     (GstVideoDecoder *decoder,
324                                    GstQuery *query);
325
326   gboolean      (*src_query)      (GstVideoDecoder *decoder,
327                                    GstQuery *query);
328
329   GstCaps*      (*getcaps)        (GstVideoDecoder *decoder,
330                                    GstCaps *filter);
331
332   GstFlowReturn (*drain)          (GstVideoDecoder *decoder);
333
334   gboolean      (*transform_meta) (GstVideoDecoder *decoder,
335                                    GstVideoCodecFrame *frame,
336                                    GstMeta * meta);
337
338   /*< private >*/
339   void         *padding[GST_PADDING_LARGE-6];
340 };
341
342 GType    gst_video_decoder_get_type (void);
343
344 /* Context parameters */
345 void     gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
346                                            gboolean packetized);
347
348 gboolean gst_video_decoder_get_packetized (GstVideoDecoder * decoder);
349
350 void     gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec,
351                                               gboolean          enabled);
352
353 gint     gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec);
354
355 void     gst_video_decoder_set_max_errors (GstVideoDecoder * dec,
356                                            gint              num);
357
358 gint     gst_video_decoder_get_max_errors (GstVideoDecoder * dec);
359
360 void     gst_video_decoder_set_needs_format (GstVideoDecoder * dec,
361                                              gboolean enabled);
362
363 gboolean gst_video_decoder_get_needs_format (GstVideoDecoder * dec);
364
365 void     gst_video_decoder_set_latency (GstVideoDecoder *decoder,
366                                         GstClockTime min_latency,
367                                         GstClockTime max_latency);
368 void     gst_video_decoder_get_latency (GstVideoDecoder *decoder,
369                                         GstClockTime *min_latency,
370                                         GstClockTime *max_latency);
371
372 void     gst_video_decoder_get_allocator (GstVideoDecoder *decoder,
373                                           GstAllocator **allocator,
374                                           GstAllocationParams *params);
375 GstBufferPool *gst_video_decoder_get_buffer_pool (GstVideoDecoder *decoder);
376
377 /* Object methods */
378
379 GstVideoCodecFrame *gst_video_decoder_get_frame        (GstVideoDecoder *decoder,
380                                                         int frame_number);
381
382 GstVideoCodecFrame *gst_video_decoder_get_oldest_frame (GstVideoDecoder *decoder);
383
384 GList *             gst_video_decoder_get_frames       (GstVideoDecoder *decoder);
385
386 /* Parsing related methods */
387 void           gst_video_decoder_add_to_frame     (GstVideoDecoder *decoder,
388                                                    int n_bytes);
389 GstFlowReturn  gst_video_decoder_have_frame       (GstVideoDecoder *decoder);
390 gsize          gst_video_decoder_get_pending_frame_size (GstVideoDecoder *decoder);
391
392 GstBuffer     *gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder);
393
394 GstFlowReturn  gst_video_decoder_allocate_output_frame_with_params (GstVideoDecoder *decoder,
395                                                                     GstVideoCodecFrame * frame,
396                                                                     GstBufferPoolAcquireParams *params);
397 GstFlowReturn  gst_video_decoder_allocate_output_frame  (GstVideoDecoder *decoder,
398                                                          GstVideoCodecFrame *frame);
399
400 GstVideoCodecState *gst_video_decoder_set_output_state (GstVideoDecoder *decoder,
401                                                         GstVideoFormat fmt, guint width, guint height,
402                                                         GstVideoCodecState *reference);
403
404 GstVideoCodecState *gst_video_decoder_get_output_state (GstVideoDecoder *decoder);
405
406 gboolean         gst_video_decoder_negotiate           (GstVideoDecoder * decoder);
407
408 GstClockTimeDiff gst_video_decoder_get_max_decode_time (GstVideoDecoder *decoder,
409                                                         GstVideoCodecFrame *frame);
410
411 gdouble          gst_video_decoder_get_qos_proportion (GstVideoDecoder * decoder);
412
413 GstFlowReturn    gst_video_decoder_finish_frame (GstVideoDecoder *decoder,
414                                                  GstVideoCodecFrame *frame);
415
416 GstFlowReturn    gst_video_decoder_drop_frame (GstVideoDecoder *dec,
417                                                GstVideoCodecFrame *frame);
418
419 void             gst_video_decoder_release_frame (GstVideoDecoder * dec,
420                                                   GstVideoCodecFrame * frame);
421
422 void             gst_video_decoder_merge_tags (GstVideoDecoder *decoder,
423                                                const GstTagList *tags,
424                                                GstTagMergeMode mode);
425
426 GstCaps *        gst_video_decoder_proxy_getcaps (GstVideoDecoder * decoder,
427                                                   GstCaps         * caps,
428                                                   GstCaps         * filter);
429
430 void             gst_video_decoder_set_use_default_pad_acceptcaps (GstVideoDecoder * decoder,
431                                                                    gboolean use);
432
433 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
434 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstVideoDecoder, gst_object_unref)
435 #endif
436
437 G_END_DECLS
438
439 #endif
440