GstVideoEncoder

## GstVideoEncoder GstVideoEncoder — Base class for video encoders |

## Synopsis

    #define             GST_VIDEO_ENCODER_CAST              (enc)
    #define             GST_VIDEO_ENCODER_FLOW_DROPPED
    #define             GST_VIDEO_ENCODER_FLOW_NEED_DATA
    #define             GST_VIDEO_ENCODER_SINK_NAME
    #define             GST_VIDEO_ENCODER_SINK_PAD          (obj)
    #define             GST_VIDEO_ENCODER_SRC_NAME
    #define             GST_VIDEO_ENCODER_SRC_PAD           (obj)
    #define             GST_VIDEO_ENCODER_INPUT_SEGMENT     (obj)
    #define             GST_VIDEO_ENCODER_OUTPUT_SEGMENT    (obj)
    #define             GST_VIDEO_ENCODER_STREAM_LOCK       (encoder)
    #define             GST_VIDEO_ENCODER_STREAM_UNLOCK     (encoder)
    struct              GstVideoEncoder;
    struct              GstVideoEncoderClass;
    GstFlowReturn       gst_video_encoder_finish_frame      (GstVideoEncoder *encoder,
                                                             GstVideoCodecFrame *frame);
    GstVideoCodecFrame * gst_video_encoder_get_frame        (GstVideoEncoder *encoder,
                                                             int frame_number);
    GstVideoCodecFrame * gst_video_encoder_get_oldest_frame (GstVideoEncoder *encoder);
    void                gst_video_encoder_set_headers       (GstVideoEncoder *encoder,
                                                             GList *headers);
    void                gst_video_encoder_get_latency       (GstVideoEncoder *encoder,
                                                             GstClockTime *min_latency,
                                                             GstClockTime *max_latency);
    void                gst_video_encoder_set_latency       (GstVideoEncoder *encoder,
                                                             GstClockTime min_latency,
                                                             GstClockTime max_latency);
    gboolean            gst_video_encoder_get_discont       (GstVideoEncoder *encoder);
    void                gst_video_encoder_set_discont       (GstVideoEncoder *encoder);
    GstVideoCodecState * gst_video_encoder_set_output_state (GstVideoEncoder *encoder,
                                                             GstCaps *caps,
                                                             GstVideoCodecState *reference);
    GstVideoCodecState * gst_video_encoder_get_output_state (GstVideoEncoder *encoder);
    GstCaps *           gst_video_encoder_proxy_getcaps     (GstVideoEncoder *enc,
                                                             GstCaps *caps,
                                                             GstCaps *filter);

## Description

This base class is for video encoders turning raw video into encoded video data.

GstVideoEncoder and subclass should cooperate as follows.

1. Configuration

   - Initially, GstVideoEncoder calls `start` when the encoder element is activated, which allows subclass to perform any global setup.

   - GstVideoEncoder calls `set_format` to inform subclass of the format of input video data that it is about to receive. Subclass should setup for encoding and configure base class as appropriate (e.g. latency). While unlikely, it might be called more than once, if changing input parameters require reconfiguration. Baseclass will ensure that processing of current configuration is finished.

   - GstVideoEncoder calls `stop` at end of all processing.

2. Data processing

   - Base class collects input data and metadata into a frame and hands this to subclass' `handle_frame`.

   - If codec processing results in encoded data, subclass should call `gst_video_encoder_finish_frame` to have encoded data pushed downstream.

   - If implemented, baseclass calls subclass `pre_push` just prior to pushing to allow subclasses to modify some metadata on the buffer. If it returns GST_FLOW_OK, the buffer is pushed downstream.

   - GstVideoEncoderClass will handle both srcpad and sinkpad events. Sink events will be passed to subclass if `event` callback has been provided.

3. Shutdown phase

   - GstVideoEncoder 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 should also be able to provide fixed src pad caps in `getcaps` by the time it calls `gst_video_encoder_finish_frame`.

Things that subclass need to take care of:

- Provide pad templates

- Provide source pad caps before pushing the first buffer

- Accept data in `handle_frame` and provide encoded results to `gst_video_encoder_finish_frame`.

## Details

### GST_VIDEO_ENCODER_CAST()

    #define GST_VIDEO_ENCODER_CAST(enc) ((GstVideoEncoder*)enc)

### GST_VIDEO_ENCODER_FLOW_DROPPED

    #define GST_VIDEO_ENCODER_FLOW_DROPPED GST_FLOW_CUSTOM_SUCCESS_1

Returned when the event/buffer should be dropped.

Since 0.10.36

### GST_VIDEO_ENCODER_FLOW_NEED_DATA

    #define GST_VIDEO_ENCODER_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS

Returned while parsing to indicate more data is needed.

Since 0.10.36

### GST_VIDEO_ENCODER_SINK_NAME

    #define GST_VIDEO_ENCODER_SINK_NAME    "sink"

The name of the templates for the sink pad.

Since 0.10.36

### GST_VIDEO_ENCODER_SINK_PAD()

    #define GST_VIDEO_ENCODER_SINK_PAD(obj)        (((GstVideoEncoder *) (obj))->sinkpad)

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

`obj` : | a GstVideoEncoder

Since 0.10.36

### GST_VIDEO_ENCODER_SRC_NAME

    #define GST_VIDEO_ENCODER_SRC_NAME     "src"

The name of the templates for the source pad.

Since 0.10.36

### GST_VIDEO_ENCODER_SRC_PAD()

    #define GST_VIDEO_ENCODER_SRC_PAD(obj)         (((GstVideoEncoder *) (obj))->srcpad)

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

`obj` : | a GstVideoEncoder

Since 0.10.36

### GST_VIDEO_ENCODER_INPUT_SEGMENT()

    #define GST_VIDEO_ENCODER_INPUT_SEGMENT(obj)     (GST_VIDEO_ENCODER_CAST (obj)->input_segment)

Gives the segment of the element.

`obj` : | base parse instance

Since 0.10.36

### GST_VIDEO_ENCODER_OUTPUT_SEGMENT()

    #define GST_VIDEO_ENCODER_OUTPUT_SEGMENT(obj)     (GST_VIDEO_ENCODER_CAST (obj)->output_segment)

Gives the segment of the element.

`obj` : | base parse instance

Since 0.10.36

### GST_VIDEO_ENCODER_STREAM_LOCK()

    #define GST_VIDEO_ENCODER_STREAM_LOCK(encoder) g_rec_mutex_lock (&GST_VIDEO_ENCODER (encoder)->stream_lock)

Obtain a lock to protect the encoder function from concurrent access.

`encoder` : | video encoder instance

Since 0.10.36

### GST_VIDEO_ENCODER_STREAM_UNLOCK()

    #define GST_VIDEO_ENCODER_STREAM_UNLOCK(encoder) g_rec_mutex_unlock (&GST_VIDEO_ENCODER (encoder)->stream_lock)

Release the lock that protects the encoder function from concurrent access.

`encoder` : | video encoder instance

Since 0.10.36

### struct GstVideoEncoder

    struct GstVideoEncoder {
    };

The opaque GstVideoEncoder data structure.

Since 0.10.36

### struct GstVideoEncoderClass

    struct GstVideoEncoderClass {
      /* virtual methods for subclasses */
      gboolean      (*open)         (GstVideoEncoder *encoder);

      gboolean      (*close)        (GstVideoEncoder *encoder);

      gboolean      (*start)        (GstVideoEncoder *encoder);

      gboolean      (*stop)         (GstVideoEncoder *encoder);

      gboolean      (*set_format)   (GstVideoEncoder *encoder,
    				 GstVideoCodecState *state);

      GstFlowReturn (*handle_frame) (GstVideoEncoder *encoder,
    				 GstVideoCodecFrame *frame);

      gboolean      (*reset)        (GstVideoEncoder *encoder,
    				 gboolean hard);

      GstFlowReturn (*finish)       (GstVideoEncoder *encoder);

      GstFlowReturn (*pre_push)     (GstVideoEncoder *encoder,
    				 GstVideoCodecFrame *frame);

      GstCaps *     (*getcaps)      (GstVideoEncoder *enc,
                                     GstCaps *filter);

      gboolean      (*sink_event)   (GstVideoEncoder *encoder,
    				 GstEvent *event);

      gboolean      (*src_event)    (GstVideoEncoder *encoder,
    				 GstEvent *event);

      gboolean      (*propose_allocation) (GstVideoEncoder * encoder,
                                           GstQuery * query);
    };

Subclasses can override any of the available virtual methods or not, as needed. At minimum `handle_frame` needs to be overridden, and `set_format` and `get_caps` are likely needed as well.

`open` () | Optional. Called when the element changes to GST_STATE_READY. Allows opening external resources. Since: 0.10.37.
`close` () | Optional. Called when the element changes to GST_STATE_NULL. Allows closing external resources. Since: 0.10.37.
`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_format` () | Optional. Notifies subclass of incoming data format. GstVideoCodecState fields have already been set according to provided caps.
`handle_frame` () | Provides input frame to subclass.
`reset` () | Optional. Allows subclass (encoder) to perform post-seek semantics reset.
`finish` () | Optional. Called to request subclass to dispatch any pending remaining data (e.g. at EOS).
`pre_push` () | Optional. Allows subclass to push frame downstream in whatever shape or form it deems appropriate. If not provided, provided encoded frame data is simply pushed downstream.
`getcaps` () | Optional. Allows for a custom sink getcaps implementation (e.g. for multichannel input specification). If not implemented, default returns gst_video_encoder_proxy_getcaps applied to sink template caps.
`sink_event` () | Optional. Event handler on the sink pad. This function should return TRUE if the event was handled and should be discarded (i.e. not unref'ed).
`src_event` () | Optional. Event handler on the source pad. This function should return TRUE if the event was handled and should be discarded (i.e. not unref'ed).
`propose_allocation` () | Optional. Propose buffer allocation parameters for upstream elements.

Since 0.10.36

### gst_video_encoder_finish_frame ()

    GstFlowReturn       gst_video_encoder_finish_frame      (GstVideoEncoder *encoder,
                                                             GstVideoCodecFrame *frame);

`frame` must have a valid encoded data buffer, whose metadata fields are then appropriately set according to frame data or no buffer at all if the frame should be dropped. It is subsequently pushed downstream or provided to `pre_push`. In any case, the frame is considered finished and released.

`encoder` : | a GstVideoEncoder
`frame` : | an encoded GstVideoCodecFrame. [transfer full]
Returns : | a GstFlowReturn resulting from sending data downstream

Since 0.10.36

### gst_video_encoder_get_frame ()

    GstVideoCodecFrame * gst_video_encoder_get_frame        (GstVideoEncoder *encoder,
                                                             int frame_number);

Get a pending unfinished GstVideoCodecFrame

`encoder` : | a GstVideoEnccoder
`frame_number` : | system_frame_number of a frame
Returns : | pending unfinished GstVideoCodecFrame identified by `frame_number`. [transfer none]

Since 0.10.36

### gst_video_encoder_get_oldest_frame ()

    GstVideoCodecFrame * gst_video_encoder_get_oldest_frame (GstVideoEncoder *encoder);

Get the oldest unfinished pending GstVideoCodecFrame

`encoder` : | a GstVideoEncoder
Returns : | oldest unfinished pending GstVideoCodecFrame. [transfer full]

Since 0.10.36

### gst_video_encoder_set_headers ()

    void                gst_video_encoder_set_headers       (GstVideoEncoder *encoder,
                                                             GList *headers);

Set the codec headers to be sent downstream whenever requested.

`encoder` : | a GstVideoEncoder
`headers` : | a list of GstBuffer containing the codec header. [transfer full][element-type GstBuffer]

Since 0.10.36

### gst_video_encoder_get_latency ()

    void                gst_video_encoder_get_latency       (GstVideoEncoder *encoder,
                                                             GstClockTime *min_latency,
                                                             GstClockTime *max_latency);

`encoder` : | a GstVideoEncoder
`min_latency` : | the configured minimum latency. [out][allow-none]
`max_latency` : | the configured maximum latency. [out][allow-none]
Returns : | the configured encoding latency.

Since 0.10.36

### gst_video_encoder_set_latency ()

    void                gst_video_encoder_set_latency       (GstVideoEncoder *encoder,
                                                             GstClockTime min_latency,
                                                             GstClockTime max_latency);

Informs baseclass of encoding latency.

`encoder` : | a GstVideoEncoder
`min_latency` : | minimum latency
`max_latency` : | maximum latency

Since 0.10.36

### gst_video_encoder_get_discont ()

    gboolean            gst_video_encoder_get_discont       (GstVideoEncoder *encoder);

### gst_video_encoder_set_discont ()

    void                gst_video_encoder_set_discont       (GstVideoEncoder *encoder);

### gst_video_encoder_set_output_state ()

    GstVideoCodecState * gst_video_encoder_set_output_state (GstVideoEncoder *encoder,
                                                             GstCaps *caps,
                                                             GstVideoCodecState *reference);

Creates a new GstVideoCodecState with the specified caps as the output state for the encoder. Any previously set output state on `decoder` will be replaced by the newly created one.

The specified `caps` should not contain any resolution, pixel-aspect-ratio, framerate, codec-data, .... Those should be specified instead in the returned GstVideoCodecState.

If the subclass wishes to copy over existing fields (like pixel aspect ratio, or framerate) from an existing GstVideoCodecState, it can be provided as a `reference`.

If the subclass wishes to override some fields from the output state (like pixel-aspect-ratio or framerate) it can do so on the returned GstVideoCodecState.

The new output state will only take effect (set on pads and buffers) starting from the next call to #`gst_video_encoder_finish_frame()`.

`encoder` : | a GstVideoEncoder
`caps` : | the GstCaps to use for the output. [transfer full]
`reference` : | An optional reference `GstVideoCodecState`. [allow-none][transfer none]
Returns : | the newly configured output state. [transfer full]

Since 0.10.36

### gst_video_encoder_get_output_state ()

    GstVideoCodecState * gst_video_encoder_get_output_state (GstVideoEncoder *encoder);

Get the current GstVideoCodecState

`encoder` : | a GstVideoEncoder
Returns : | GstVideoCodecState describing format of video data. [transfer full]

Since 0.10.36

### gst_video_encoder_proxy_getcaps ()

    GstCaps *           gst_video_encoder_proxy_getcaps     (GstVideoEncoder *enc,
                                                             GstCaps *caps,
                                                             GstCaps *filter);

Returns caps that express `caps` (or sink template caps if `caps` == NULL) restricted to resolution/format/... combinations supported by downstream elements (e.g. muxers).

`enc` : | a GstVideoEncoder
`caps` : | initial caps
Returns : | a GstCaps owned by caller

Since 0.10.36