GstBaseParse

## GstBaseParse GstBaseParse — Base class for stream parsers |

## Synopsis

    #include <gst/base/gstbaseparse.h>

    struct              GstBaseParse;
    struct              GstBaseParseClass;
    void                gst_base_parse_set_duration         (GstBaseParse *parse,
                                                             GstFormat fmt,
                                                             gint64 duration,
                                                             gint interval);
    void                gst_base_parse_set_average_bitrate  (GstBaseParse *parse,
                                                             guint bitrate);
    void                gst_base_parse_set_min_frame_size   (GstBaseParse *parse,
                                                             guint min_size);
    void                gst_base_parse_set_passthrough      (GstBaseParse *parse,
                                                             gboolean passthrough);
    void                gst_base_parse_set_syncable         (GstBaseParse *parse,
                                                             gboolean syncable);
    void                gst_base_parse_set_has_timing_info  (GstBaseParse *parse,
                                                             gboolean has_timing);
    void                gst_base_parse_set_frame_rate       (GstBaseParse *parse,
                                                             guint fps_num,
                                                             guint fps_den,
                                                             guint lead_in,
                                                             guint lead_out);
    gboolean            gst_base_parse_convert_default      (GstBaseParse *parse,
                                                             GstFormat src_format,
                                                             gint64 src_value,
                                                             GstFormat dest_format,
                                                             gint64 *dest_value);
    gboolean            gst_base_parse_add_index_entry      (GstBaseParse *parse,
                                                             guint64 offset,
                                                             GstClockTime ts,
                                                             gboolean key,
                                                             gboolean force);
                        GstBaseParseFrame;
    enum                GstBaseParseFrameFlags;
    GstBaseParseFrame * gst_base_parse_frame_new            (GstBuffer *buffer,
                                                             GstBaseParseFrameFlags flags,
                                                             gint overhead);
    void                gst_base_parse_frame_init           (GstBaseParseFrame *frame);
    void                gst_base_parse_frame_free           (GstBaseParseFrame *frame);
    GstFlowReturn       gst_base_parse_push_frame           (GstBaseParse *parse,
                                                             GstBaseParseFrame *frame);
    #define             GST_BASE_PARSE_DRAINING             (parse)
    #define             GST_BASE_PARSE_FLAG_DRAINING
    #define             GST_BASE_PARSE_FLAG_LOST_SYNC
    #define             GST_BASE_PARSE_FLOW_DROPPED
    #define             GST_BASE_PARSE_FLOW_QUEUED
    #define             GST_BASE_PARSE_LOST_SYNC            (parse)
    #define             GST_BASE_PARSE_SINK_PAD             (obj)
    #define             GST_BASE_PARSE_SRC_PAD              (obj)

## Description

This base class is for parser elements that process data and splits it into separate audio/video/whatever frames.

It provides for:

- provides one sink pad and one source pad

- handles state changes

- can operate in pull mode or push mode

- handles seeking in both modes

- handles events (NEWSEGMENT/EOS/FLUSH)

- handles queries (POSITION/DURATION/SEEKING/FORMAT/CONVERT)

- handles flushing

The purpose of this base class is to provide the basic functionality of a parser and share a lot of rather complex code.

Description of the parsing mechanism:

1. Set-up phase

   - GstBaseParse class calls `set_sink_caps` to inform the subclass about incoming sinkpad caps. Subclass should set the srcpad caps accordingly.

   - GstBaseParse calls `start` to inform subclass that data processing is about to start now.

   - At least at this point subclass needs to tell the GstBaseParse class how big data chunks it wants to receive (min_frame_size). It can do this with `gst_base_parse_set_min_frame_size()`.

   - GstBaseParse class sets up appropriate data passing mode (pull/push) and starts to process the data.

2. Parsing phase

   - GstBaseParse gathers at least min_frame_size bytes of data either by pulling it from upstream or collecting buffers in an internal GstAdapter.

   - A buffer of (at least) min_frame_size bytes is passed to subclass with `check_valid_frame`. Subclass checks the contents and returns TRUE if the buffer contains a valid frame. It also needs to set the `framesize` according to the detected frame size. If buffer didn't contain a valid frame, this call must return FALSE and optionally set the `skipsize` value to inform base class that how many bytes it needs to skip in order to find a valid frame. `framesize` can always indicate a new minimum for current frame parsing. Indicating G_MAXUINT for requested amount means subclass simply needs best available subsequent data. In push mode this amounts to an additional input buffer (thus minimal additional latency), in pull mode this amounts to some arbitrary reasonable buffer size increase. The passed buffer is read-only. Note that `check_valid_frame` might receive any small amount of input data when leftover data is being drained (e.g. at EOS).

   - After valid frame is found, it will be passed again to subclass with `parse_frame` call. Now subclass is responsible for parsing the frame contents and setting the caps, and buffer metadata (e.g. buffer timestamp and duration, or keyframe if applicable). (although the latter can also be done by GstBaseParse if it is appropriately configured, see below). Frame is provided with timestamp derived from upstream (as much as generally possible), duration obtained from configuration (see below), and offset if meaningful (in pull mode).

   - Finally the buffer can be pushed downstream and the parsing loop starts over again. Just prior to actually pushing the buffer in question, it is passed to `pre_push_frame` which gives subclass yet one last chance to examine buffer metadata, or to send some custom (tag) events, or to perform custom (segment) filtering.

   - During the parsing process GstBaseParseClass will handle both srcpad and sinkpad events. They will be passed to subclass if `event` or `src_event` callbacks have been provided.

3. Shutdown phase

   - GstBaseParse class calls `stop` to inform the subclass that data parsing will be stopped.

Subclass is responsible for providing pad template caps for source and sink pads. The pads need to be named "sink" and "src". It also needs to set the fixed caps on srcpad, when the format is ensured (e.g. when base class calls subclass' `set_sink_caps` function).

This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So, subclass conversion routine needs to know that conversion from GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the frame number that can be found from the given byte position.

GstBaseParse uses subclasses conversion methods also for seeking (or otherwise uses its own default one, see also below).

Subclass `start` and `stop` functions will be called to inform the beginning and end of data processing.

Things that subclass need to take care of:

- Provide pad templates

- Fixate the source pad caps when appropriate

- Inform base class how big data chunks should be retrieved. This is done with `gst_base_parse_set_min_frame_size()` function.

- Examine data chunks passed to subclass with `check_valid_frame` and tell if they contain a valid frame

- Set the caps and timestamp to frame that is passed to subclass with `parse_frame` function.

- Provide conversion functions

- Update the duration information with `gst_base_parse_set_duration()`

- Optionally passthrough using `gst_base_parse_set_passthrough()`

- Configure various baseparse parameters using `gst_base_parse_set_average_bitrate()`, `gst_base_parse_set_syncable()` and `gst_base_parse_set_frame_rate()`.

- In particular, if subclass is unable to determine a duration, but parsing (or specs) yields a frames per seconds rate, then this can be provided to GstBaseParse to enable it to cater for buffer time metadata (which will be taken from upstream as much as possible). Internally keeping track of frame durations and respective sizes that have been pushed provides GstBaseParse with an estimated bitrate. A default `convert` (used if not overriden) will then use these rates to perform obvious conversions. These rates are also used to update (estimated) duration at regular frame intervals.

## Details

### struct GstBaseParse

    struct GstBaseParse {
      GstElement     element;
    };

The opaque GstBaseParse data structure.

GstElement `element`; | the parent element.

### struct GstBaseParseClass

    struct GstBaseParseClass {
      GstElementClass parent_class;

      /* virtual methods for subclasses */

      gboolean      (*start)              (GstBaseParse * parse);

      gboolean      (*stop)               (GstBaseParse * parse);

      gboolean      (*set_sink_caps)      (GstBaseParse * parse,
                                           GstCaps      * caps);

      gboolean      (*check_valid_frame)  (GstBaseParse      * parse,
                                           GstBaseParseFrame * frame,
                                           guint             * framesize,
                                           gint              * skipsize);

      GstFlowReturn (*parse_frame)        (GstBaseParse      * parse,
                                           GstBaseParseFrame * frame);

      GstFlowReturn (*pre_push_frame)     (GstBaseParse      * parse,
                                           GstBaseParseFrame * frame);

      gboolean      (*convert)            (GstBaseParse * parse,
                                           GstFormat      src_format,
                                           gint64         src_value,
                                           GstFormat      dest_format,
                                           gint64       * dest_value);

      gboolean      (*event)              (GstBaseParse * parse,
                                           GstEvent     * event);

      gboolean      (*src_event)          (GstBaseParse * parse,
                                           GstEvent     * event);

      GstCaps *     (*get_sink_caps)      (GstBaseParse * parse);

      GstFlowReturn (*detect)             (GstBaseParse * parse,
                                           GstBuffer    * buffer);
    };

Subclasses can override any of the available virtual methods or not, as needed. At minimum `check_valid_frame` and `parse_frame` needs to be overridden.

GstElementClass `parent_class`; | the parent class
`start` () | Optional. Called when the element starts processing. Allows opening external resources.
`stop` () | Optional. Called when the element stops processing. Allows closing external resources.
`set_sink_caps` () | allows the subclass to be notified of the actual caps set.
`check_valid_frame` () | Check if the given piece of data contains a valid frame.
`parse_frame` () | Parse the already checked frame. Subclass need to set the buffer timestamp, duration, caps and possibly other necessary metadata. This is called with srcpad's STREAM_LOCK held.
`pre_push_frame` () | Optional. Called just prior to pushing a frame (after any pending events have been sent) to give subclass a chance to perform additional actions at this time (e.g. tag sending) or to decide whether this buffer should be dropped or not (e.g. custom segment clipping).
`convert` () | Optional. Convert between formats.
`event` () | Optional. Event handler on the sink pad. This function should return TRUE if the event was handled and can be dropped.
`src_event` () | Optional. Event handler on the source pad. Should return TRUE if the event was handled and can be dropped.
`get_sink_caps` () | allows the subclass to do its own sink get caps if needed. Since: 0.10.36
`detect` () | Optional. Called until it doesn't return GST_FLOW_OK anymore for the first buffers. Can be used by the subclass to detect the stream format. Since: 0.10.36

### gst_base_parse_set_duration ()

    void                gst_base_parse_set_duration         (GstBaseParse *parse,
                                                             GstFormat fmt,
                                                             gint64 duration,
                                                             gint interval);

Sets the duration of the currently playing media. Subclass can use this when it is able to determine duration and/or notices a change in the media duration. Alternatively, if `interval` is non-zero (default), then stream duration is determined based on estimated bitrate, and updated every `interval` frames.

`parse` : | GstBaseParse.
`fmt` : | GstFormat.
`duration` : | duration value.
`interval` : | how often to update the duration estimate based on bitrate, or 0.

Since 0.10.33

### gst_base_parse_set_average_bitrate ()

    void                gst_base_parse_set_average_bitrate  (GstBaseParse *parse,
                                                             guint bitrate);

Optionally sets the average bitrate detected in media (if non-zero), e.g. based on metadata, as it will be posted to the application.

By default, announced average bitrate is estimated. The average bitrate is used to estimate the total duration of the stream and to estimate a seek position, if there's no index and the format is syncable (see `gst_base_parse_set_syncable()`).

`parse` : | GstBaseParse.
`bitrate` : | average bitrate in bits/second

Since 0.10.33

### gst_base_parse_set_min_frame_size ()

    void                gst_base_parse_set_min_frame_size   (GstBaseParse *parse,
                                                             guint min_size);

Subclass can use this function to tell the base class that it needs to give at least min_size buffers.

`parse` : | GstBaseParse.
`min_size` : | Minimum size of the data that this base class should give to subclass.

Since 0.10.33

### gst_base_parse_set_passthrough ()

    void                gst_base_parse_set_passthrough      (GstBaseParse *parse,
                                                             gboolean passthrough);

Set if the nature of the format or configuration does not allow (much) parsing, and the parser should operate in passthrough mode (which only applies when operating in push mode). That is, incoming buffers are pushed through unmodified, i.e. no `check_valid_frame` or `parse_frame` callbacks will be invoked, but `pre_push_frame` will still be invoked, so subclass can perform as much or as little is appropriate for passthrough semantics in `pre_push_frame`.

`parse` : | a GstBaseParse
`passthrough` : | `TRUE` if parser should run in passthrough mode

Since 0.10.33

### gst_base_parse_set_syncable ()

    void                gst_base_parse_set_syncable         (GstBaseParse *parse,
                                                             gboolean syncable);

Set if frame starts can be identified. This is set by default and determines whether seeking based on bitrate averages is possible for a format/stream.

`parse` : | a GstBaseParse
`syncable` : | set if frame starts can be identified

Since 0.10.33

### gst_base_parse_set_has_timing_info ()

    void                gst_base_parse_set_has_timing_info  (GstBaseParse *parse,
                                                             gboolean has_timing);

Set if frames carry timing information which the subclass can (generally) parse and provide. In particular, intrinsic (rather than estimated) time can be obtained following a seek.

`parse` : | a GstBaseParse
`has_timing` : | whether frames carry timing information

Since 0.10.33

### gst_base_parse_set_frame_rate ()

    void                gst_base_parse_set_frame_rate       (GstBaseParse *parse,
                                                             guint fps_num,
                                                             guint fps_den,
                                                             guint lead_in,
                                                             guint lead_out);

If frames per second is configured, parser can take care of buffer duration and timestamping. When performing segment clipping, or seeking to a specific location, a corresponding decoder might need an initial `lead_in` and a following `lead_out` number of frames to ensure the desired segment is entirely filled upon decoding.

`parse` : | the GstBaseParse to set
`fps_num` : | frames per second (numerator).
`fps_den` : | frames per second (denominator).
`lead_in` : | frames needed before a segment for subsequent decode
`lead_out` : | frames needed after a segment

Since 0.10.33

### gst_base_parse_convert_default ()

    gboolean            gst_base_parse_convert_default      (GstBaseParse *parse,
                                                             GstFormat src_format,
                                                             gint64 src_value,
                                                             GstFormat dest_format,
                                                             gint64 *dest_value);

Default implementation of "convert" vmethod in GstBaseParse class.

`parse` : | GstBaseParse.
`src_format` : | GstFormat describing the source format.
`src_value` : | Source value to be converted.
`dest_format` : | GstFormat defining the converted format.
`dest_value` : | Pointer where the conversion result will be put.
Returns : | TRUE if conversion was successful.

Since 0.10.33

### gst_base_parse_add_index_entry ()

    gboolean            gst_base_parse_add_index_entry      (GstBaseParse *parse,
                                                             guint64 offset,
                                                             GstClockTime ts,
                                                             gboolean key,
                                                             gboolean force);

Adds an entry to the index associating `offset` to `ts`. It is recommended to only add keyframe entries. `force` allows to bypass checks, such as whether the stream is (upstream) seekable, another entry is already "close" to the new entry, etc.

`parse` : | GstBaseParse.
`offset` : | offset of entry
`ts` : | timestamp associated with offset
`key` : | whether entry refers to keyframe
`force` : | add entry disregarding sanity checks
Returns : | gboolean indicating whether entry was added

Since 0.10.33

### GstBaseParseFrame

    typedef struct {
      GstBuffer * buffer;
      guint       flags;
      gint        overhead;
    } GstBaseParseFrame;

Frame (context) data passed to each frame parsing virtual methods. In addition to providing the data to be checked for a valid frame or an already identified frame, it conveys additional metadata or control information from and to the subclass w.r.t. the particular frame in question (rather than global parameters). Some of these may apply to each parsing stage, others only to some a particular one. These parameters are effectively zeroed at start of each frame's processing, i.e. parsing virtual method invocation sequence.

GstBuffer *`buffer`; | data to check for valid frame or parsed frame. Subclass is allowed to replace this buffer.
guint `flags`; | a combination of input and output GstBaseParseFrameFlags that convey additional context to subclass or allow subclass to tune subsequent GstBaseParse actions.
gint `overhead`; | subclass can set this to indicates the metadata overhead for the given frame, which is then used to enable more accurate bitrate computations. If this is -1, it is assumed that this frame should be skipped in bitrate calculation.

Since 0.10.33

### enum GstBaseParseFrameFlags

    typedef enum {
      GST_BASE_PARSE_FRAME_FLAG_NONE         = 0,
      GST_BASE_PARSE_FRAME_FLAG_NO_FRAME     = (1 << 0),
      GST_BASE_PARSE_FRAME_FLAG_CLIP         = (1 << 1)
    } GstBaseParseFrameFlags;

Flags to be used in a GstBaseParseFrame.

`GST_BASE_PARSE_FRAME_FLAG_NONE` | no flag
`GST_BASE_PARSE_FRAME_FLAG_NO_FRAME` | set to indicate this buffer should not be counted as frame, e.g. if this frame is dependent on a previous one. As it is not counted as a frame, bitrate increases but frame to time conversions are maintained.
`GST_BASE_PARSE_FRAME_FLAG_CLIP` | `pre_push_frame` can set this to indicate that regular segment clipping can still be performed (as opposed to any custom one having been done).

Since 0.10.33

### gst_base_parse_frame_new ()

    GstBaseParseFrame * gst_base_parse_frame_new            (GstBuffer *buffer,
                                                             GstBaseParseFrameFlags flags,
                                                             gint overhead);

Allocates a new GstBaseParseFrame. This function is mainly for bindings, elements written in C should usually allocate the frame on the stack and then use `gst_base_parse_frame_init()` to initialise it.

`buffer` : | a GstBuffer. [transfer none]
`flags` : | the flags
`overhead` : | number of bytes in this frame which should be counted as metadata overhead, ie. not used to calculate the average bitrate. Set to -1 to mark the entire frame as metadata. If in doubt, set to 0.
Returns : | a newly-allocated GstBaseParseFrame. Free with `gst_base_parse_frame_free()` when no longer needed, unless you gave away ownership to `gst_base_parse_push_frame()`.

Since 0.10.33

### gst_base_parse_frame_init ()

    void                gst_base_parse_frame_init           (GstBaseParseFrame *frame);

Sets a GstBaseParseFrame to initial state. Currently this means all public fields are zero-ed and a private flag is set to make sure `gst_base_parse_frame_free()` only frees the contents but not the actual frame. Use this function to initialise a GstBaseParseFrame allocated on the stack.

`frame` : | GstBaseParseFrame.

Since 0.10.33

### gst_base_parse_frame_free ()

    void                gst_base_parse_frame_free           (GstBaseParseFrame *frame);

### gst_base_parse_push_frame ()

    GstFlowReturn       gst_base_parse_push_frame           (GstBaseParse *parse,
                                                             GstBaseParseFrame *frame);

Pushes the frame downstream, sends any pending events and does some timestamp and segment handling. Takes ownership of `frame` and will clear it (if it was initialised with `gst_base_parse_frame_init()`) or free it.

This must be called with sinkpad STREAM_LOCK held.

`parse` : | GstBaseParse.
`frame` : | a GstBaseParseFrame. [transfer full]
Returns : | GstFlowReturn

Since 0.10.33

### GST_BASE_PARSE_DRAINING()

    #define GST_BASE_PARSE_DRAINING(parse)  (!!(GST_BASE_PARSE_CAST(parse)->flags & GST_BASE_PARSE_FLAG_DRAINING))

Obtains current drain status (ie. whether EOS has been received and the parser is now processing the frames at the end of the stream)

`parse` : | base parse instance

Since 0.10.33

### GST_BASE_PARSE_FLAG_DRAINING

    #define GST_BASE_PARSE_FLAG_DRAINING  (1 << 1)

### GST_BASE_PARSE_FLAG_LOST_SYNC

    #define GST_BASE_PARSE_FLAG_LOST_SYNC (1 << 0)

### GST_BASE_PARSE_FLOW_DROPPED

    #define GST_BASE_PARSE_FLOW_DROPPED     GST_FLOW_CUSTOM_SUCCESS

A GstFlowReturn that can be returned from parse_frame to indicate that no output buffer was generated, or from pre_push_frame to to forego pushing buffer.

Since 0.10.33

### GST_BASE_PARSE_FLOW_QUEUED

    #define GST_BASE_PARSE_FLOW_QUEUED      GST_FLOW_CUSTOM_SUCCESS_1

A GstFlowReturn that can be returned from parse frame to indicate that the buffer will be queued to be pushed when the next OK

Since 0.10.33

### GST_BASE_PARSE_LOST_SYNC()

    #define GST_BASE_PARSE_LOST_SYNC(parse) (!!(GST_BASE_PARSE_CAST(parse)->flags & GST_BASE_PARSE_FLAG_LOST_SYNC))

Obtains current sync status.

`parse` : | base parse instance

Since 0.10.33

### GST_BASE_PARSE_SINK_PAD()

    #define GST_BASE_PARSE_SINK_PAD(obj)    (GST_BASE_PARSE_CAST (obj)->sinkpad)

Gives the pointer to the sink GstPad object of the element.

`obj` : | base parse instance

Since 0.10.33

### GST_BASE_PARSE_SRC_PAD()

    #define GST_BASE_PARSE_SRC_PAD(obj)    (GST_BASE_PARSE_CAST (obj)->srcpad)

Gives the pointer to the source GstPad object of the element.

`obj` : | base parse instance

Since 0.10.33

## See Also

GstBaseTransform