baseparse: refactor passthrough into format flags
[platform/upstream/gstreamer.git] / gst / audioparsers / gstbaseparse.h
1 /* GStreamer
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *
4  * Contact: Stefan Kost <stefan.kost@nokia.com>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef __GST_BASE_PARSE_H__
23 #define __GST_BASE_PARSE_H__
24
25 #include <gst/gst.h>
26 #include <gst/base/gstadapter.h>
27
28 G_BEGIN_DECLS
29
30 #define GST_TYPE_BASE_PARSE                (gst_base_parse_get_type())
31 #define GST_BASE_PARSE(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_PARSE,GstBaseParse))
32 #define GST_BASE_PARSE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_PARSE,GstBaseParseClass))
33 #define GST_BASE_PARSE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_PARSE,GstBaseParseClass))
34 #define GST_IS_BASE_PARSE(obj)     (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_PARSE))
35 #define GST_IS_BASE_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_PARSE))
36 #define GST_BASE_PARSE_CAST(obj)        ((GstBaseParse *)(obj))
37
38 /**
39  * GST_BASE_PARSE_SINK_NAME:
40  *
41  * the name of the templates for the sink pad
42  */
43 #define GST_BASE_PARSE_SINK_NAME        "sink"
44 /**
45  * GST_BASE_PARSE_SRC_NAME:
46  *
47  * the name of the templates for the source pad
48  */
49 #define GST_BASE_PARSE_SRC_NAME "src"
50
51 /**
52  * GST_BASE_PARSE_SRC_PAD:
53  * @obj: base parse instance
54  *
55  * Gives the pointer to the source #GstPad object of the element.
56  *
57  * Since: 0.10.x
58  */
59 #define GST_BASE_PARSE_SRC_PAD(obj)             (GST_BASE_PARSE_CAST (obj)->srcpad)
60
61 /**
62  * GST_BASE_PARSE_SINK_PAD:
63  * @obj: base parse instance
64  *
65  * Gives the pointer to the sink #GstPad object of the element.
66  *
67  * Since: 0.10.x
68  */
69 #define GST_BASE_PARSE_SINK_PAD(obj)    (GST_BASE_PARSE_CAST (obj)->sinkpad)
70
71 /**
72  * GST_BASE_PARSE_SEGMENT:
73  * @obj: base parse instance
74  *
75  * Gives the segment of the element.
76  *
77  * Since: 0.10.x
78  */
79 #define GST_BASE_PARSE_SEGMENT(obj)     (GST_BASE_PARSE_CAST (obj)->segment)
80
81 /**
82  * GST_BASE_PARSE_FLOW_DROPPED:
83  *
84  * A #GstFlowReturn that can be returned from parse_frame to
85  * indicate that no output buffer was generated, or from pre_push_buffer to
86  * to forego pushing buffer.
87  *
88  * Since: 0.10.x
89  */
90 #define GST_BASE_PARSE_FLOW_DROPPED   GST_FLOW_CUSTOM_SUCCESS
91
92 /**
93  * GstBaseParseFrameFlags:
94  * @GST_BASE_PARSE_FRAME_FLAG_NONE: no flag
95  * @GST_BASE_PARSE_FRAME_FLAG_SYNC: indicates if parsing is 'in sync'
96  * @GST_BASE_PARSE_FRAME_FLAG_DRAIN: indicates if parser is 'draining'.
97  *   That is, leftover data (e.g. in FLUSH or EOS situation) is being parsed.
98  * @GST_BASE_PARSE_FRAME_FLAG_NO_FRAME: set to indicate this buffer should not be
99  *   counted as frame, e.g. if this frame is dependent on a previous one.
100  *   As it is not counted as a frame, bitrate increases but frame to time
101  *   conversions are maintained.
102  * @GST_BASE_PARSE_FRAME_FLAG_CLIP: @pre_push_buffer can set this to indicate
103  *    that regular segment clipping can still be performed (as opposed to
104  *    any custom one having been done).
105  *
106  * Flags to be used in a #GstBaseParseFrame.
107  *
108  * Since: 0.10.x
109  */
110 typedef enum {
111   GST_BASE_PARSE_FRAME_FLAG_NONE         = 0,
112   GST_BASE_PARSE_FRAME_FLAG_SYNC         = (1 << 0),
113   GST_BASE_PARSE_FRAME_FLAG_DRAIN        = (1 << 1),
114   GST_BASE_PARSE_FRAME_FLAG_NO_FRAME     = (1 << 2),
115   GST_BASE_PARSE_FRAME_FLAG_CLIP         = (1 << 3)
116 } GstBaseParseFrameFlags;
117
118 /**
119  * GstBaseParseFrame:
120  * @buffer: data to check for valid frame or parsed frame.
121  *   Subclass is allowed to replace this buffer.
122  * @overhead: subclass can set this to indicates the metadata overhead
123  *   for the given frame, which is then used to enable more accurate bitrate
124  *   computations. If this is -1, it is assumed that this frame should be
125  *   skipped in bitrate calculation.
126  * @flags: a combination of input and output #GstBaseParseFrameFlags that
127  *  convey additional context to subclass or allow subclass to tune
128  *  subsequent #GstBaseParse actions.
129  *
130  * Frame (context) data passed to each frame parsing virtual methods.  In
131  * addition to providing the data to be checked for a valid frame or an already
132  * identified frame, it conveys additional metadata or control information
133  * from and to the subclass w.r.t. the particular frame in question (rather
134  * than global parameters).  Some of these may apply to each parsing stage, others
135  * only to some a particular one.  These parameters are effectively zeroed at start
136  * of each frame's processing, i.e. parsing virtual method invocation sequence.
137  *
138  * Since: 0.10.x
139  */
140 typedef struct {
141   GstBuffer       *buffer;
142   guint           flags;
143   gint            overhead;
144 } GstBaseParseFrame;
145
146 /**
147  * GST_BASE_PARSE_FRAME_SYNC:
148  * @frame: base parse frame instance
149  *
150  * Obtains current sync status indicated in frame.
151  *
152  * Since: 0.10.x
153  */
154 #define GST_BASE_PARSE_FRAME_SYNC(frame)     (!!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_SYNC))
155
156 /**
157  * GST_BASE_PARSE_FRAME_DRAIN:
158  * @frame: base parse frame instance
159  *
160  * Obtains current drain status indicated in frame.
161  *
162  * Since: 0.10.x
163  */
164 #define GST_BASE_PARSE_FRAME_DRAIN(frame)    (!!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_DRAIN))
165
166 /**
167  * GstBaseParseFormat:
168  * @GST_BASE_PARSE_FORMAT_NONE: default setting
169  * @GST_BASE_PARSE_FORMAT_PASSTHROUGH: nature of format or configuration
170  *   does not allow (much) parsing, so parser should operate in passthrough mode
171  *   (which only applies operating in pull mode).  That is, incoming buffers
172  *   are pushed through unmodified, i.e. no @check_valid_frame or @parse_frame
173  *   callbacks will be invoked.  On the other hand, @pre_push_buffer is still invoked,
174  *   where subclass can perform as much or as little is appropriate for
175  *   "passthrough" semantics.
176  * @GST_BASE_PARSE_FORMAT_HAS_TIME: frames carry timing info which subclass
177  *   can (generally) parse and provide.  In particular, intrinsic time
178  *   (rather than estimated) can be obtained following seek.
179  *
180  * Since: 0.10.x
181  */
182 typedef enum _GstBaseParseFormat {
183   GST_BASE_PARSE_FORMAT_NONE               = 0,
184   GST_BASE_PARSE_FORMAT_PASSTHROUGH        = (1 << 0),
185   GST_BASE_PARSE_FORMAT_HAS_TIME           = (1 << 1),
186 } GstBaseParseFormat;
187
188 /**
189  * GstBaseParseSeekable:
190  * @GST_BASE_PARSE_SEEK_NONE: No seeking possible.
191  * @GST_BASE_PARSE_SEEK_DEFAULT: Default seeking possible using estimated bitrate.
192  * @GST_BASE_PARSE_SEEK_TABLE: Additional metadata provides more accurate seeking.
193  *
194  * Indicates what level (of quality) of seeking is possible.
195  *
196  * Since: 0.10.x
197  */
198 typedef enum _GstBaseParseSeekable {
199   GST_BASE_PARSE_SEEK_NONE,
200   GST_BASE_PARSE_SEEK_DEFAULT,
201   GST_BASE_PARSE_SEEK_TABLE
202 } GstBaseParseSeekable;
203
204 typedef struct _GstBaseParse GstBaseParse;
205 typedef struct _GstBaseParseClass GstBaseParseClass;
206 typedef struct _GstBaseParsePrivate GstBaseParsePrivate;
207
208 /**
209  * GstBaseParse:
210  * @element: the parent element.
211  *
212  * The opaque #GstBaseParse data structure.
213  */
214 struct _GstBaseParse {
215   GstElement     element;
216   GstAdapter    *adapter;
217
218   /*< protected >*/
219   /* source and sink pads */
220   GstPad         *sinkpad;
221   GstPad         *srcpad;
222
223   /* MT-protected (with STREAM_LOCK) */
224   GstSegment      segment;
225
226   /* Newsegment event to be sent after SEEK */
227   GstEvent       *pending_segment;
228
229   /* Segment event that closes the running segment prior to SEEK */
230   GstEvent       *close_segment;
231
232   /*< private >*/
233   gpointer       _gst_reserved[GST_PADDING_LARGE];
234   GstBaseParsePrivate *priv;
235 };
236
237 /**
238  * GstBaseParseClass:
239  * @start:          Optional.
240  *                  Called when the element starts processing.
241  *                  Allows opening external resources.
242  * @stop:           Optional.
243  *                  Called when the element stops processing.
244  *                  Allows closing external resources.
245  * @set_sink_caps:  allows the subclass to be notified of the actual caps set.
246  * @check_valid_frame:  Check if the given piece of data contains a valid
247  *                      frame.
248  * @parse_frame:    Parse the already checked frame. Subclass need to
249  *                  set the buffer timestamp, duration, caps and possibly
250  *                  other necessary metadata. This is called with srcpad's
251  *                  STREAM_LOCK held.
252  * @convert:        Optional.
253  *                  Convert between formats.
254  * @find_frame:     Optional.
255  *                  Finds a frame. Gets a position passed and should return
256  *                  TRUE and the offset in bytes where this position is.
257  *                  Will only be called in pull mode and the subclass can pull
258  *                  whatever it wants from upstream. If not implemented,
259  *                  the base class will implement it by calling
260  *                  @check_valid_frame and @parse_frame to find the wanted
261  *                  frame and build a seek table.
262  * @event:          Optional.
263  *                  Event handler on the sink pad. This function should return
264  *                  TRUE if the event was handled and can be dropped.
265  * @src_event:      Optional.
266  *                  Event handler on the source pad. Should return TRUE
267  *                  if the event was handled and can be dropped.
268  *
269  * @get_frame_overhead: Finds the metadata overhead for the given frame. This
270  *                      is used to enable more accurate bitrate computations.
271  *                      If NULL, the per-frame overhead is assumed to be 0. If
272  *                      this returns -1, it is assumed that this frame should
273  *                      be skipped in bitrate calculation.
274  *
275  * @pre_push_buffer: Optional.
276  *                   Called just prior to pushing a frame (after any pending
277  *                   events have been sent) to give subclass a chance to perform
278  *                   additional actions at this time (e.g. tag sending) or to
279  *                   decide whether this buffer should be dropped or not
280  *                   (e.g. custom segment clipping).
281  *
282  * Subclasses can override any of the available virtual methods or not, as
283  * needed. At minimum @check_valid_frame and @parse_frame needs to be
284  * overridden.
285  */
286 struct _GstBaseParseClass {
287   GstElementClass parent_class;
288
289   /*< public >*/
290   /* virtual methods for subclasses */
291
292   gboolean      (*start)              (GstBaseParse *parse);
293
294   gboolean      (*stop)               (GstBaseParse *parse);
295
296   gboolean      (*set_sink_caps)      (GstBaseParse *parse,
297                                        GstCaps *caps);
298
299   gboolean      (*check_valid_frame)  (GstBaseParse *parse,
300                                        GstBaseParseFrame *frame,
301                                        guint *framesize,
302                                        gint *skipsize);
303
304   GstFlowReturn (*parse_frame)        (GstBaseParse *parse,
305                                        GstBaseParseFrame *frame);
306
307   GstFlowReturn (*pre_push_frame)     (GstBaseParse *parse,
308                                        GstBaseParseFrame *frame);
309
310   gboolean      (*convert)            (GstBaseParse * parse,
311                                        GstFormat src_format,
312                                        gint64 src_value,
313                                        GstFormat dest_format,
314                                        gint64 * dest_value);
315
316   gboolean      (*event)              (GstBaseParse *parse,
317                                        GstEvent *event);
318
319   gboolean      (*src_event)          (GstBaseParse *parse,
320                                        GstEvent *event);
321
322   /*< private >*/
323   gpointer       _gst_reserved[GST_PADDING_LARGE];
324 };
325
326 GType           gst_base_parse_get_type         (void);
327
328 void gst_base_parse_frame_init (GstBaseParse * parse, GstBaseParseFrame * frame);
329
330 GstFlowReturn gst_base_parse_push_frame (GstBaseParse *parse,
331                                           GstBaseParseFrame *frame);
332
333 void gst_base_parse_set_duration (GstBaseParse *parse,
334                                   GstFormat fmt, gint64 duration, gint interval);
335
336 void gst_base_parse_set_seek (GstBaseParse * parse,
337                               GstBaseParseSeekable seek, guint bitrate);
338
339 void gst_base_parse_set_min_frame_size (GstBaseParse *parse, guint min_size);
340
341 void gst_base_parse_set_format (GstBaseParse * parse, GstBaseParseFormat flag,
342                                 gboolean on);
343
344 void gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
345                                      guint fps_den, guint lead_in, guint lead_out);
346
347 gboolean gst_base_parse_convert_default (GstBaseParse * parse,
348                                          GstFormat src_format, gint64 src_value,
349                                          GstFormat dest_format, gint64 * dest_value);
350
351 gboolean gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
352                                          GstClockTime ts, gboolean key, gboolean force);
353
354 G_END_DECLS
355
356 #endif /* __GST_BASE_PARSE_H__ */