GstVideoEncoder: GStreamer Base Plugins 1.0 Library Reference Manual

## GstVideoEncoder GstVideoEncoder — Base class for video encoders |

## Functions

#define | GST_VIDEO_ENCODER_CAST()
#define | GST_VIDEO_ENCODER_SINK_PAD()
#define | GST_VIDEO_ENCODER_SRC_PAD()
#define | GST_VIDEO_ENCODER_INPUT_SEGMENT()
#define | GST_VIDEO_ENCODER_OUTPUT_SEGMENT()
#define | GST_VIDEO_ENCODER_STREAM_LOCK()
#define | GST_VIDEO_ENCODER_STREAM_UNLOCK()
GstBuffer * | gst_video_encoder_allocate_output_buffer ()
GstFlowReturn | gst_video_encoder_allocate_output_frame ()
void | gst_video_encoder_get_allocator ()
GstFlowReturn | gst_video_encoder_finish_frame ()
gboolean | gst_video_encoder_negotiate ()
GstVideoCodecFrame * | gst_video_encoder_get_frame ()
GList * | gst_video_encoder_get_frames ()
GstVideoCodecFrame * | gst_video_encoder_get_oldest_frame ()
void | gst_video_encoder_set_headers ()
void | gst_video_encoder_get_latency ()
void | gst_video_encoder_set_latency ()
GstVideoCodecState * | gst_video_encoder_set_output_state ()
GstVideoCodecState * | gst_video_encoder_get_output_state ()
void | gst_video_encoder_set_min_pts ()
GstCaps * | gst_video_encoder_proxy_getcaps ()
void | gst_video_encoder_merge_tags ()
GstClockTimeDiff | gst_video_encoder_get_max_encode_time ()
gboolean | gst_video_encoder_is_qos_enabled ()
void | gst_video_encoder_set_qos_enabled ()

## Properties

gboolean | qos | Read / Write

## Types and Values

#define | GST_VIDEO_ENCODER_FLOW_DROPPED
#define | GST_VIDEO_ENCODER_FLOW_NEED_DATA
#define | GST_VIDEO_ENCODER_SINK_NAME
#define | GST_VIDEO_ENCODER_SRC_NAME
struct | GstVideoEncoder
struct | GstVideoEncoderClass

## Object Hierarchy

        GObject
        ╰── GInitiallyUnowned
            ╰── GstObject
                ╰── GstElement
                    ╰── GstVideoEncoder

## Implemented Interfaces

GstVideoEncoder implements GstPreset.

## Includes

    #include <gst/video/video.h>

## Description

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

GstVideoEncoder and subclass should cooperate as follows.

#### 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.

#### 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.

#### 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` .

The “qos” property will enable the Quality-of-Service features of the encoder which gather statistics about the real-time performance of the downstream elements. If enabled, subclasses can use `gst_video_encoder_get_max_encode_time()` to check if input frames are already late and drop them right away to give a chance to the pipeline to catch up.

## Functions

### GST_VIDEO_ENCODER_CAST()

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

### 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.

#### Parameters

obj | a GstVideoEncoder |

### 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.

#### Parameters

obj | a GstVideoEncoder |

### 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.

#### Parameters

obj | base parse instance |

### 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.

#### Parameters

obj | base parse instance |

### 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.

#### Parameters

encoder | video encoder instance |

### 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.

#### Parameters

encoder | video encoder instance |

### gst_video_encoder_allocate_output_buffer ()

    GstBuffer *
    gst_video_encoder_allocate_output_buffer
                                   (GstVideoEncoder *encoder,
                                    gsize size);

Helper function that allocates a buffer to hold an encoded video frame for `encoder` 's current GstVideoCodecState.

#### Parameters

encoder | a GstVideoEncoder |
size | size of the buffer |

#### Returns

allocated buffer.

[transfer full]

### gst_video_encoder_allocate_output_frame ()

    GstFlowReturn
    gst_video_encoder_allocate_output_frame
                                   (GstVideoEncoder *encoder,
                                    GstVideoCodecFrame *frame,
                                    gsize size);

Helper function that allocates a buffer to hold an encoded video frame for `encoder` 's current GstVideoCodecState. Subclass should already have configured video state and set src pad caps.

The buffer allocated here is owned by the frame and you should only keep references to the frame, not the buffer.

#### Parameters

encoder | a GstVideoEncoder |
frame | a GstVideoCodecFrame |
size | size of the buffer |

#### Returns

`GST_FLOW_OK` if an output buffer could be allocated

### gst_video_encoder_get_allocator ()

    void
    gst_video_encoder_get_allocator (GstVideoEncoder *encoder,
                                     GstAllocator **allocator,
                                     GstAllocationParams *params);

Lets GstVideoEncoder sub-classes to know the memory `allocator` used by the base class and its `params` .

Unref the `allocator` after use it.

#### Parameters

encoder | a GstVideoEncoder |
allocator | the GstAllocator used. | [out][allow-none][transfer full]
params | the GstAllocationParams of `allocator` . | [out][allow-none][transfer full]

### 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.

After calling this function the output buffer of the frame is to be considered read-only. This function will also change the metadata of the buffer.

#### Parameters

encoder | a GstVideoEncoder |
frame | an encoded GstVideoCodecFrame. | [transfer full]

#### Returns

a GstFlowReturn resulting from sending data downstream

### gst_video_encoder_negotiate ()

    gboolean
    gst_video_encoder_negotiate (GstVideoEncoder *encoder);

Negotiate with downstream elements to currently configured GstVideoCodecState. Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if negotiate fails.

#### Parameters

encoder | a GstVideoEncoder |

#### Returns

`TRUE` if the negotiation succeeded, else `FALSE`.

### gst_video_encoder_get_frame ()

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

Get a pending unfinished GstVideoCodecFrame

#### Parameters

encoder | a GstVideoEncoder |
frame_number | system_frame_number of a frame |

#### Returns

pending unfinished GstVideoCodecFrame identified by `frame_number` .

[transfer full]

### gst_video_encoder_get_frames ()

    GList *
    gst_video_encoder_get_frames (GstVideoEncoder *encoder);

Get all pending unfinished GstVideoCodecFrame

#### Parameters

encoder | a GstVideoEncoder |

#### Returns

pending unfinished GstVideoCodecFrame.

[transfer full][element-type GstVideoCodecFrame]

### gst_video_encoder_get_oldest_frame ()

    GstVideoCodecFrame *
    gst_video_encoder_get_oldest_frame (GstVideoEncoder *encoder);

Get the oldest unfinished pending GstVideoCodecFrame

#### Parameters

encoder | a GstVideoEncoder |

#### Returns

oldest unfinished pending GstVideoCodecFrame.

[transfer full]

### 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.

#### Parameters

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

### gst_video_encoder_get_latency ()

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

Query the configured encoding latency. Results will be returned via `min_latency` and `max_latency` .

#### Parameters

encoder | a GstVideoEncoder |
min_latency | address of variable in which to store the configured minimum latency, or `NULL`. | [out][allow-none]
max_latency | address of variable in which to store the configured maximum latency, or `NULL`. | [out][allow-none]

### gst_video_encoder_set_latency ()

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

Informs baseclass of encoding latency.

#### Parameters

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

### 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 `encoder` 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()`.

#### Parameters

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]

### gst_video_encoder_get_output_state ()

    GstVideoCodecState *
    gst_video_encoder_get_output_state (GstVideoEncoder *encoder);

Get the current GstVideoCodecState

#### Parameters

encoder | a GstVideoEncoder |

#### Returns

GstVideoCodecState describing format of video data.

[transfer full]

### gst_video_encoder_set_min_pts ()

    void
    gst_video_encoder_set_min_pts (GstVideoEncoder *encoder,
                                   GstClockTime min_pts);

Request minimal value for PTS passed to handle_frame.

For streams with reordered frames this can be used to ensure that there is enough time to accomodate first DTS, which may be less than first PTS

Since 1.6

#### Parameters

encoder | a GstVideoEncoder |
min_pts | minimal PTS that will be passed to handle_frame |

### 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).

#### Parameters

enc | a GstVideoEncoder |
caps | initial caps. | [allow-none]
filter | filter caps. | [allow-none]

#### Returns

a GstCaps owned by caller.

[transfer full]

### gst_video_encoder_merge_tags ()

    void
    gst_video_encoder_merge_tags (GstVideoEncoder *encoder,
                                  const GstTagList *tags,
                                  GstTagMergeMode mode);

Sets the video encoder tags and how they should be merged with any upstream stream tags. This will override any tags previously-set with `gst_video_encoder_merge_tags()`.

Note that this is provided for convenience, and the subclass is not required to use this and can still do tag handling on its own.

MT safe.

#### Parameters

encoder | a GstVideoEncoder |
tags | a GstTagList to merge, or NULL to unset previously-set tags. | [allow-none]
mode | the GstTagMergeMode to use, usually GST_TAG_MERGE_REPLACE |

### gst_video_encoder_get_max_encode_time ()

    GstClockTimeDiff
    gst_video_encoder_get_max_encode_time (GstVideoEncoder *encoder,
                                           GstVideoCodecFrame *frame);

Determines maximum possible encoding time for `frame` that will allow it to encode and arrive in time (as determined by QoS events). In particular, a negative result means encoding in time is no longer possible and should therefore occur as soon/skippy as possible.

If no QoS events have been received from downstream, or if “qos” is disabled this function returns G_MAXINT64.

#### Parameters

encoder | a GstVideoEncoder |
frame | a GstVideoCodecFrame |

#### Returns

max decoding time.

Since: 1.14

### gst_video_encoder_is_qos_enabled ()

    gboolean
    gst_video_encoder_is_qos_enabled (GstVideoEncoder *encoder);

Checks if `encoder` is currently configured to handle Quality-of-Service events from downstream.

#### Parameters

encoder | the encoder |

#### Returns

`TRUE` if the encoder is configured to perform Quality-of-Service.

Since: 1.14

### gst_video_encoder_set_qos_enabled ()

    void
    gst_video_encoder_set_qos_enabled (GstVideoEncoder *encoder,
                                       gboolean enabled);

Configures `encoder` to handle Quality-of-Service events from downstream.

#### Parameters

encoder | the encoder |
enabled | the new qos value. |

Since: 1.14

## Types and Values

### GST_VIDEO_ENCODER_FLOW_DROPPED

    #define GST_VIDEO_ENCODER_FLOW_DROPPED GST_FLOW_CUSTOM_SUCCESS_1

`GST_VIDEO_ENCODER_FLOW_DROPPED` is deprecated and should not be used in newly-written code.

since 1.8. use gst_video_encoder_finish_frame with a `NULL` frame->output_buffer to drop the frame instead.

Returned when the event/buffer should be dropped.

### 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.

### GST_VIDEO_ENCODER_SINK_NAME

    #define GST_VIDEO_ENCODER_SINK_NAME    "sink"

The name of the templates for the sink pad.

### GST_VIDEO_ENCODER_SRC_NAME

    #define GST_VIDEO_ENCODER_SRC_NAME     "src"

The name of the templates for the source pad.

### struct GstVideoEncoder

    struct GstVideoEncoder;

The opaque GstVideoEncoder data structure.

### 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      (*negotiate)    (GstVideoEncoder *encoder);

      gboolean      (*decide_allocation)  (GstVideoEncoder *encoder, GstQuery *query);

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

      gboolean      (*sink_query)     (GstVideoEncoder *encoder,
    				   GstQuery *query);

      gboolean      (*src_query)      (GstVideoEncoder *encoder,
    				   GstQuery *query);

      gboolean      (*transform_meta) (GstVideoEncoder *encoder,
                                       GstVideoCodecFrame *frame,
                                       GstMeta * meta);
    };

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.

#### Members

`open` () | Optional. Called when the element changes to GST_STATE_READY. Allows opening external resources. |
`close` () | Optional. Called when the element changes to GST_STATE_NULL. Allows closing external resources. |
`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. Deprecated. |
`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). Subclasses should chain up to the parent implementation to invoke the default handler. |
`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). Subclasses should chain up to the parent implementation to invoke the default handler. |
`negotiate` () | Optional. Negotiate with downstream and configure buffer pools, etc. Subclasses should chain up to the parent implementation to invoke the default handler. |
`decide_allocation` () | Optional. Setup the allocation parameters for allocating output buffers. The passed in query contains the result of the downstream allocation query. Subclasses should chain up to the parent implementation to invoke the default handler. |
`propose_allocation` () | Optional. Propose buffer allocation parameters for upstream elements. Subclasses should chain up to the parent implementation to invoke the default handler. |
`flush` () | Optional. Flush all remaining data from the encoder without pushing it downstream. Since: 1.2 |
`sink_query` () | Optional. Query handler on the sink pad. This function should return TRUE if the query could be performed. Subclasses should chain up to the parent implementation to invoke the default handler. Since 1.4 |
`src_query` () | Optional. Query handler on the source pad. This function should return TRUE if the query could be performed. Subclasses should chain up to the parent implementation to invoke the default handler. Since 1.4 |
`transform_meta` () | Optional. Transform the metadata on the input buffer to the output buffer. By default this method is copies all meta without tags and meta with only the "video" tag. subclasses can implement this method and return `TRUE` if the metadata is to be copied. Since 1.6 |

## Property Details

### The `“qos”` property

      “qos”                      gboolean

Handle Quality-of-Service events from downstream.

Flags: Read / Write

Default value: FALSE