4971ea29b143f190b0f6a3830502249bc7ef7c9e
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / gstaudiodecoder.h
1 /* GStreamer
2  * Copyright (C) 2009 Igalia S.L.
3  * Author: Iago Toral Quiroga <itoral@igalia.com>
4  * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
5  * Copyright (C) 2011 Nokia Corporation. All rights reserved.
6  *   Contact: Stefan Kost <stefan.kost@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 #ifndef _GST_AUDIO_DECODER_H_
24 #define _GST_AUDIO_DECODER_H_
25
26 #include <gst/gst.h>
27 #include <gst/audio/audio.h>
28 #include <gst/base/gstadapter.h>
29
30 G_BEGIN_DECLS
31
32 #define GST_TYPE_AUDIO_DECODER \
33   (gst_audio_decoder_get_type())
34 #define GST_AUDIO_DECODER(obj) \
35   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_DECODER,GstAudioDecoder))
36 #define GST_AUDIO_DECODER_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_DECODER,GstAudioDecoderClass))
38 #define GST_AUDIO_DECODER_GET_CLASS(obj) \
39   (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_AUDIO_DECODER,GstAudioDecoderClass))
40 #define GST_IS_AUDIO_DECODER(obj) \
41   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_DECODER))
42 #define GST_IS_AUDIO_DECODER_CLASS(obj) \
43   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_DECODER))
44 #define GST_AUDIO_DECODER_CAST(obj) \
45   ((GstAudioDecoder *)(obj))
46
47 /**
48  * GST_AUDIO_DECODER_SINK_NAME:
49  *
50  * The name of the templates for the sink pad.
51  *
52  * Since: 0.10.36
53  */
54 #define GST_AUDIO_DECODER_SINK_NAME    "sink"
55 /**
56  * GST_AUDIO_DECODER_SRC_NAME:
57  *
58  * The name of the templates for the source pad.
59  *
60  * Since: 0.10.36
61  */
62 #define GST_AUDIO_DECODER_SRC_NAME     "src"
63
64 /**
65  * GST_AUDIO_DECODER_SRC_PAD:
66  * @obj: base audio codec instance
67  *
68  * Gives the pointer to the source #GstPad object of the element.
69  *
70  * Since: 0.10.36
71  */
72 #define GST_AUDIO_DECODER_SRC_PAD(obj)         (((GstAudioDecoder *) (obj))->srcpad)
73
74 /**
75  * GST_AUDIO_DECODER_SINK_PAD:
76  * @obj: base audio codec instance
77  *
78  * Gives the pointer to the sink #GstPad object of the element.
79  *
80  * Since: 0.10.36
81  */
82 #define GST_AUDIO_DECODER_SINK_PAD(obj)        (((GstAudioDecoder *) (obj))->sinkpad)
83
84 #define GST_AUDIO_DECODER_STREAM_LOCK(dec)   g_rec_mutex_lock (&GST_AUDIO_DECODER (dec)->stream_lock)
85 #define GST_AUDIO_DECODER_STREAM_UNLOCK(dec) g_rec_mutex_unlock (&GST_AUDIO_DECODER (dec)->stream_lock)
86
87 typedef struct _GstAudioDecoder GstAudioDecoder;
88 typedef struct _GstAudioDecoderClass GstAudioDecoderClass;
89
90 typedef struct _GstAudioDecoderPrivate GstAudioDecoderPrivate;
91
92 /* do not use this one, use macro below */
93 GstFlowReturn _gst_audio_decoder_error (GstAudioDecoder *dec, gint weight,
94                                         GQuark domain, gint code,
95                                         gchar *txt, gchar *debug,
96                                         const gchar *file, const gchar *function,
97                                         gint line);
98
99 /**
100  * GST_AUDIO_DECODER_ERROR:
101  * @el:     the base audio decoder element that generates the error
102  * @weight: element defined weight of the error, added to error count
103  * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
104  * @code:   error code defined for that domain (see #gstreamer-GstGError)
105  * @text:   the message to display (format string and args enclosed in
106  *          parentheses)
107  * @debug:  debugging information for the message (format string and args
108  *          enclosed in parentheses)
109  * @ret:    variable to receive return value
110  *
111  * Utility function that audio decoder elements can use in case they encountered
112  * a data processing error that may be fatal for the current "data unit" but
113  * need not prevent subsequent decoding.  Such errors are counted and if there
114  * are too many, as configured in the context's max_errors, the pipeline will
115  * post an error message and the application will be requested to stop further
116  * media processing.  Otherwise, it is considered a "glitch" and only a warning
117  * is logged. In either case, @ret is set to the proper value to
118  * return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
119  *
120  * Since: 0.10.36
121  */
122 #define GST_AUDIO_DECODER_ERROR(el, weight, domain, code, text, debug, ret) \
123 G_STMT_START {                                                              \
124   gchar *__txt = _gst_element_error_printf text;                            \
125   gchar *__dbg = _gst_element_error_printf debug;                           \
126   GstAudioDecoder *dec = GST_AUDIO_DECODER (el);                   \
127   ret = _gst_audio_decoder_error (dec, weight, GST_ ## domain ## _ERROR,    \
128       GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__,            \
129       GST_FUNCTION, __LINE__);                                              \
130 } G_STMT_END
131
132
133 /**
134  * GST_AUDIO_DECODER_MAX_ERRORS:
135  *
136  * Default maximum number of errors tolerated before signaling error.
137  *
138  * Since: 0.10.36
139  */
140 #define GST_AUDIO_DECODER_MAX_ERRORS     10
141
142 /**
143  * GstAudioDecoder:
144  *
145  * The opaque #GstAudioDecoder data structure.
146  *
147  * Since: 0.10.36
148  */
149 struct _GstAudioDecoder
150 {
151   GstElement element;
152
153   /*< protected >*/
154   /* source and sink pads */
155   GstPad         *sinkpad;
156   GstPad         *srcpad;
157
158   /* protects all data processing, i.e. is locked
159    * in the chain function, finish_frame and when
160    * processing serialized events */
161   GRecMutex       stream_lock;
162
163   /* MT-protected (with STREAM_LOCK) */
164   GstSegment      segment;
165
166   /*< private >*/
167   GstAudioDecoderPrivate *priv;
168
169   gpointer       _gst_reserved[GST_PADDING_LARGE];
170 };
171
172 /**
173  * GstAudioDecoderClass:
174  * @element_class:  The parent class structure
175  * @start:          Optional.
176  *                  Called when the element starts processing.
177  *                  Allows opening external resources.
178  * @stop:           Optional.
179  *                  Called when the element stops processing.
180  *                  Allows closing external resources.
181  * @set_format:     Notifies subclass of incoming data format (caps).
182  * @parse:          Optional.
183  *                  Allows chopping incoming data into manageable units (frames)
184  *                  for subsequent decoding.  This division is at subclass
185  *                  discretion and may or may not correspond to 1 (or more)
186  *                  frames as defined by audio format.
187  * @handle_frame:   Provides input data (or NULL to clear any remaining data)
188  *                  to subclass.  Input data ref management is performed by
189  *                  base class, subclass should not care or intervene,
190  *                  and input data is only valid until next call to base class,
191  *                  most notably a call to gst_audio_decoder_finish_frame().
192  * @flush:          Optional.
193  *                  Instructs subclass to clear any codec caches and discard
194  *                  any pending samples and not yet returned decoded data.
195  *                  @hard indicates whether a FLUSH is being processed,
196  *                  or otherwise a DISCONT (or conceptually similar).
197  * @event:          Optional.
198  *                  Event handler on the sink pad. Subclasses should chain up to
199  *                  the parent implementation to invoke the default handler.
200  * @pre_push:       Optional.
201  *                  Called just prior to pushing (encoded data) buffer downstream.
202  *                  Subclass has full discretionary access to buffer,
203  *                  and a not OK flow return will abort downstream pushing.
204  * @open:           Optional.
205  *                  Called when the element changes to GST_STATE_READY.
206  *                  Allows opening external resources. Since: 0.10.37.
207  * @close:          Optional.
208  *                  Called when the element changes to GST_STATE_NULL.
209  *                  Allows closing external resources. Since: 0.10.37.
210  *
211  * Subclasses can override any of the available virtual methods or not, as
212  * needed. At minimum @handle_frame (and likely @set_format) needs to be
213  * overridden.
214  *
215  * Since: 0.10.36
216  */
217 struct _GstAudioDecoderClass
218 {
219   GstElementClass element_class;
220
221   /*< public >*/
222   /* virtual methods for subclasses */
223
224   gboolean      (*start)              (GstAudioDecoder *dec);
225
226   gboolean      (*stop)               (GstAudioDecoder *dec);
227
228   gboolean      (*set_format)         (GstAudioDecoder *dec,
229                                        GstCaps *caps);
230
231   GstFlowReturn (*parse)              (GstAudioDecoder *dec,
232                                        GstAdapter *adapter,
233                                        gint *offset, gint *length);
234
235   GstFlowReturn (*handle_frame)       (GstAudioDecoder *dec,
236                                        GstBuffer *buffer);
237
238   void          (*flush)              (GstAudioDecoder *dec, gboolean hard);
239
240   GstFlowReturn (*pre_push)           (GstAudioDecoder *dec,
241                                        GstBuffer **buffer);
242
243   gboolean      (*event)              (GstAudioDecoder *dec,
244                                        GstEvent *event);
245
246   gboolean      (*open)               (GstAudioDecoder *dec);
247   
248   gboolean      (*close)              (GstAudioDecoder *dec);
249
250   /*< private >*/
251   gpointer       _gst_reserved[GST_PADDING_LARGE-2];
252 };
253
254 GType             gst_audio_decoder_get_type (void);
255
256 gboolean          gst_audio_decoder_set_output_format  (GstAudioDecoder    * dec,
257                                                         const GstAudioInfo * info);
258
259 GstFlowReturn     gst_audio_decoder_finish_frame (GstAudioDecoder * dec,
260                                                   GstBuffer * buf, gint frames);
261
262 /* context parameters */
263 GstAudioInfo    * gst_audio_decoder_get_audio_info (GstAudioDecoder * dec);
264
265 void              gst_audio_decoder_set_plc_aware  (GstAudioDecoder * dec,
266                                                     gboolean          plc);
267
268 gint              gst_audio_decoder_get_plc_aware  (GstAudioDecoder * dec);
269
270 void              gst_audio_decoder_set_byte_time  (GstAudioDecoder * dec,
271                                                     gboolean          enabled);
272
273 gint              gst_audio_decoder_get_byte_time  (GstAudioDecoder * dec);
274
275 gint              gst_audio_decoder_get_delay      (GstAudioDecoder * dec);
276
277 void              gst_audio_decoder_set_max_errors (GstAudioDecoder * dec,
278                                                    gint               num);
279
280 gint              gst_audio_decoder_get_max_errors (GstAudioDecoder * dec);
281
282 void              gst_audio_decoder_set_latency (GstAudioDecoder * dec,
283                                                  GstClockTime      min,
284                                                  GstClockTime      max);
285
286 void              gst_audio_decoder_get_latency (GstAudioDecoder * dec,
287                                                  GstClockTime    * min,
288                                                  GstClockTime    * max);
289
290 void              gst_audio_decoder_get_parse_state (GstAudioDecoder * dec,
291                                                      gboolean        * sync,
292                                                      gboolean        * eos);
293
294
295 /* object properties */
296 void              gst_audio_decoder_set_plc (GstAudioDecoder * dec,
297                                              gboolean          enabled);
298
299 gboolean          gst_audio_decoder_get_plc (GstAudioDecoder * dec);
300
301 void              gst_audio_decoder_set_min_latency (GstAudioDecoder * dec,
302                                                      gint64            num);
303
304 gint64            gst_audio_decoder_get_min_latency (GstAudioDecoder * dec);
305
306 void              gst_audio_decoder_set_tolerance   (GstAudioDecoder * dec,
307                                                      gint64            tolerance);
308
309 gint64            gst_audio_decoder_get_tolerance   (GstAudioDecoder * dec);
310
311 void              gst_audio_decoder_set_drainable (GstAudioDecoder * dec,
312                                                    gboolean enabled);
313
314 gboolean          gst_audio_decoder_get_drainable (GstAudioDecoder * dec);
315
316 void              gst_audio_decoder_set_needs_format (GstAudioDecoder * dec,
317                                                       gboolean enabled);
318
319 gboolean          gst_audio_decoder_get_needs_format (GstAudioDecoder * dec);
320
321 void              gst_audio_decoder_merge_tags (GstAudioDecoder * dec,
322                                                 const GstTagList * tags, GstTagMergeMode mode);
323
324 G_END_DECLS
325
326 #endif /* _GST_AUDIO_DECODER_H_ */