2 * Copyright (C) 2008 David Schleef <ds@schleef.org>
3 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
4 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * Copyright (C) 2012 Collabora Ltd.
7 * Author : Edward Hervey <edward@collabora.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:gstvideoencoder
27 * @short_description: Base class for video encoders
30 * This base class is for video encoders turning raw video into
33 * GstVideoEncoder and subclass should cooperate as follows.
36 * <itemizedlist><title>Configuration</title>
38 * Initially, GstVideoEncoder calls @start when the encoder element
39 * is activated, which allows subclass to perform any global setup.
42 * GstVideoEncoder calls @set_format to inform subclass of the format
43 * of input video data that it is about to receive. Subclass should
44 * setup for encoding and configure base class as appropriate
45 * (e.g. latency). While unlikely, it might be called more than once,
46 * if changing input parameters require reconfiguration. Baseclass
47 * will ensure that processing of current configuration is finished.
50 * GstVideoEncoder calls @stop at end of all processing.
56 * <title>Data processing</title>
58 * Base class collects input data and metadata into a frame and hands
59 * this to subclass' @handle_frame.
62 * If codec processing results in encoded data, subclass should call
63 * @gst_video_encoder_finish_frame to have encoded data pushed
67 * If implemented, baseclass calls subclass @pre_push just prior to
68 * pushing to allow subclasses to modify some metadata on the buffer.
69 * If it returns GST_FLOW_OK, the buffer is pushed downstream.
72 * GstVideoEncoderClass will handle both srcpad and sinkpad events.
73 * Sink events will be passed to subclass if @event callback has been
79 * <itemizedlist><title>Shutdown phase</title>
81 * GstVideoEncoder class calls @stop to inform the subclass that data
82 * parsing will be stopped.
88 * Subclass is responsible for providing pad template caps for
89 * source and sink pads. The pads need to be named "sink" and "src". It should
90 * also be able to provide fixed src pad caps in @getcaps by the time it calls
91 * @gst_video_encoder_finish_frame.
93 * Things that subclass need to take care of:
95 * <listitem><para>Provide pad templates</para></listitem>
97 * Provide source pad caps before pushing the first buffer
100 * Accept data in @handle_frame and provide encoded results to
101 * @gst_video_encoder_finish_frame.
113 * * Change _set_output_format() to steal the reference of the provided caps
114 * * Calculate actual latency based on input/output timestamp/frame_number
115 * and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
118 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
119 * with newer GLib versions (>= 2.31.0) */
120 #define GLIB_DISABLE_DEPRECATION_WARNINGS
122 #include "gstvideoencoder.h"
123 #include "gstvideoutils.h"
125 #include <gst/video/gstvideometa.h>
129 GST_DEBUG_CATEGORY (videoencoder_debug);
130 #define GST_CAT_DEFAULT videoencoder_debug
132 #define GST_VIDEO_ENCODER_GET_PRIVATE(obj) \
133 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
134 GstVideoEncoderPrivate))
136 struct _GstVideoEncoderPrivate
138 guint64 presentation_frame_number;
139 int distance_from_sync;
141 /* FIXME : (and introduce a context ?) */
148 GList *current_frame_events;
151 gboolean new_headers; /* Whether new headers were just set */
153 GList *force_key_unit; /* List of pending forced keyunits */
155 guint64 system_frame_number;
157 GList *frames; /* Protected with OBJECT_LOCK */
158 GstVideoCodecState *input_state;
159 GstVideoCodecState *output_state;
160 gboolean output_state_changed;
165 GstAllocator *allocator;
166 GstAllocationParams params;
169 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
170 struct _ForcedKeyUnitEvent
172 GstClockTime running_time;
173 gboolean pending; /* TRUE if this was requested already */
174 gboolean all_headers;
179 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
181 g_slice_free (ForcedKeyUnitEvent, evt);
184 static ForcedKeyUnitEvent *
185 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
188 ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
190 evt->running_time = running_time;
191 evt->all_headers = all_headers;
197 static GstElementClass *parent_class = NULL;
198 static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
199 static void gst_video_encoder_init (GstVideoEncoder * enc,
200 GstVideoEncoderClass * klass);
202 static void gst_video_encoder_finalize (GObject * object);
204 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
206 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
208 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
210 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
212 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
214 static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
215 element, GstStateChange transition);
216 static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
218 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
220 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
221 encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
222 GstClockTime duration);
224 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
226 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
228 static gboolean gst_video_encoder_decide_allocation_default (GstVideoEncoder *
229 encoder, GstQuery * query);
230 static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
231 encoder, GstQuery * query);
233 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
234 * method to get to the padtemplates */
236 gst_video_encoder_get_type (void)
238 static volatile gsize type = 0;
240 if (g_once_init_enter (&type)) {
242 static const GTypeInfo info = {
243 sizeof (GstVideoEncoderClass),
246 (GClassInitFunc) gst_video_encoder_class_init,
249 sizeof (GstVideoEncoder),
251 (GInstanceInitFunc) gst_video_encoder_init,
253 const GInterfaceInfo preset_interface_info = {
254 NULL, /* interface_init */
255 NULL, /* interface_finalize */
256 NULL /* interface_data */
259 _type = g_type_register_static (GST_TYPE_ELEMENT,
260 "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
261 g_type_add_interface_static (_type, GST_TYPE_PRESET,
262 &preset_interface_info);
263 g_once_init_leave (&type, _type);
269 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
271 GObjectClass *gobject_class;
272 GstElementClass *gstelement_class;
274 gobject_class = G_OBJECT_CLASS (klass);
275 gstelement_class = GST_ELEMENT_CLASS (klass);
277 GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
278 "Base Video Encoder");
280 parent_class = g_type_class_peek_parent (klass);
282 g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
284 gobject_class->finalize = gst_video_encoder_finalize;
286 gstelement_class->change_state =
287 GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
289 klass->sink_event = gst_video_encoder_sink_event_default;
290 klass->src_event = gst_video_encoder_src_event_default;
291 klass->propose_allocation = gst_video_encoder_propose_allocation_default;
292 klass->decide_allocation = gst_video_encoder_decide_allocation_default;
296 gst_video_encoder_reset (GstVideoEncoder * encoder)
298 GstVideoEncoderPrivate *priv = encoder->priv;
301 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
303 priv->presentation_frame_number = 0;
304 priv->distance_from_sync = 0;
306 g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
308 g_list_free (priv->force_key_unit);
309 priv->force_key_unit = NULL;
311 priv->drained = TRUE;
312 priv->min_latency = 0;
313 priv->max_latency = 0;
315 g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
316 g_list_free (priv->headers);
317 priv->headers = NULL;
318 priv->new_headers = FALSE;
320 g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
321 g_list_free (priv->current_frame_events);
322 priv->current_frame_events = NULL;
324 for (g = priv->frames; g; g = g->next) {
325 gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
327 g_list_free (priv->frames);
333 if (priv->input_state)
334 gst_video_codec_state_unref (priv->input_state);
335 priv->input_state = NULL;
336 if (priv->output_state)
337 gst_video_codec_state_unref (priv->output_state);
338 priv->output_state = NULL;
340 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
344 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
346 GstVideoEncoderPrivate *priv;
347 GstPadTemplate *pad_template;
350 GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
352 priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
355 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
356 g_return_if_fail (pad_template != NULL);
358 encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
360 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
361 gst_pad_set_event_function (pad,
362 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
363 gst_pad_set_query_function (pad,
364 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
365 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
368 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
369 g_return_if_fail (pad_template != NULL);
371 encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
373 gst_pad_set_query_function (pad,
374 GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
375 gst_pad_set_event_function (pad,
376 GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
377 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
379 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
380 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
382 g_rec_mutex_init (&encoder->stream_lock);
384 priv->at_eos = FALSE;
385 priv->headers = NULL;
386 priv->new_headers = FALSE;
388 gst_video_encoder_reset (encoder);
392 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
393 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
396 gboolean res = FALSE;
398 g_return_val_if_fail (dest_format != NULL, FALSE);
399 g_return_val_if_fail (dest_value != NULL, FALSE);
401 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
404 *dest_value = src_value;
408 if (bytes <= 0 || time <= 0) {
409 GST_DEBUG ("not enough metadata yet to convert");
413 switch (src_format) {
414 case GST_FORMAT_BYTES:
415 switch (*dest_format) {
416 case GST_FORMAT_TIME:
417 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
424 case GST_FORMAT_TIME:
425 switch (*dest_format) {
426 case GST_FORMAT_BYTES:
427 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
435 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
445 * gst_video_encoder_set_headers:
446 * @encoder: a #GstVideoEncoder
447 * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
449 * Set the codec headers to be sent downstream whenever requested.
452 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
454 GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
456 GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
457 if (video_encoder->priv->headers) {
458 g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
460 g_list_free (video_encoder->priv->headers);
462 video_encoder->priv->headers = headers;
463 video_encoder->priv->new_headers = TRUE;
465 GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
469 gst_video_encoder_drain (GstVideoEncoder * enc)
471 GstVideoEncoderPrivate *priv;
472 GstVideoEncoderClass *enc_class;
475 enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
478 GST_DEBUG_OBJECT (enc, "draining");
481 GST_DEBUG_OBJECT (enc, "already drained");
485 if (enc_class->reset) {
486 GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
487 ret = enc_class->reset (enc, TRUE);
489 /* everything should be away now */
491 /* not fatal/impossible though if subclass/enc eats stuff */
492 g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
493 g_list_free (priv->frames);
500 static GstVideoCodecState *
501 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
503 GstVideoCodecState *state;
505 state = g_slice_new0 (GstVideoCodecState);
506 state->ref_count = 1;
507 gst_video_info_init (&state->info);
508 gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
513 GstVideoInfo *tgt, *ref;
516 ref = &reference->info;
518 /* Copy over extra fields from reference state */
519 tgt->interlace_mode = ref->interlace_mode;
520 tgt->flags = ref->flags;
521 tgt->width = ref->width;
522 tgt->height = ref->height;
523 tgt->chroma_site = ref->chroma_site;
524 tgt->colorimetry = ref->colorimetry;
525 tgt->par_n = ref->par_n;
526 tgt->par_d = ref->par_d;
527 tgt->fps_n = ref->fps_n;
528 tgt->fps_d = ref->fps_d;
534 static GstVideoCodecState *
535 _new_input_state (GstCaps * caps)
537 GstVideoCodecState *state;
539 state = g_slice_new0 (GstVideoCodecState);
540 state->ref_count = 1;
541 gst_video_info_init (&state->info);
542 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
544 state->caps = gst_caps_ref (caps);
550 g_slice_free (GstVideoCodecState, state);
556 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
558 GstVideoEncoderClass *encoder_class;
559 GstVideoCodecState *state;
561 gboolean samecaps = FALSE;
563 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
565 /* subclass should do something here ... */
566 g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
568 GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
570 state = _new_input_state (caps);
571 if (G_UNLIKELY (!state))
574 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
576 if (encoder->priv->input_state)
578 gst_video_info_is_equal (&state->info,
579 &encoder->priv->input_state->info);
582 /* arrange draining pending frames */
583 gst_video_encoder_drain (encoder);
585 /* and subclass should be ready to configure format at any time around */
586 ret = encoder_class->set_format (encoder, state);
588 if (encoder->priv->input_state)
589 gst_video_codec_state_unref (encoder->priv->input_state);
590 encoder->priv->input_state = state;
592 gst_video_codec_state_unref (state);
594 /* no need to stir things up */
595 GST_DEBUG_OBJECT (encoder,
596 "new video format identical to configured format");
597 gst_video_codec_state_unref (state);
601 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
604 GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
610 GST_WARNING_OBJECT (encoder, "Failed to parse caps");
616 * gst_video_encoder_proxy_getcaps:
617 * @enc: a #GstVideoEncoder
618 * @caps: initial caps
619 * @filter: filter caps
621 * Returns caps that express @caps (or sink template caps if @caps == NULL)
622 * restricted to resolution/format/... combinations supported by downstream
623 * elements (e.g. muxers).
625 * Returns: a #GstCaps owned by caller
628 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
633 GstCaps *fcaps, *filter_caps;
636 /* Allow downstream to specify width/height/framerate/PAR constraints
637 * and forward them upstream for video converters to handle
640 caps ? gst_caps_ref (caps) :
641 gst_pad_get_pad_template_caps (encoder->sinkpad);
642 allowed = gst_pad_get_allowed_caps (encoder->srcpad);
644 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
649 GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
650 GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
652 filter_caps = gst_caps_new_empty ();
654 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
656 gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
658 for (j = 0; j < gst_caps_get_size (allowed); j++) {
659 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
663 s = gst_structure_new_id_empty (q_name);
664 if ((val = gst_structure_get_value (allowed_s, "width")))
665 gst_structure_set_value (s, "width", val);
666 if ((val = gst_structure_get_value (allowed_s, "height")))
667 gst_structure_set_value (s, "height", val);
668 if ((val = gst_structure_get_value (allowed_s, "framerate")))
669 gst_structure_set_value (s, "framerate", val);
670 if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
671 gst_structure_set_value (s, "pixel-aspect-ratio", val);
673 filter_caps = gst_caps_merge_structure (filter_caps, s);
677 fcaps = gst_caps_intersect (filter_caps, templ_caps);
678 gst_caps_unref (filter_caps);
679 gst_caps_unref (templ_caps);
682 GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
683 filter_caps = gst_caps_intersect (fcaps, filter);
684 gst_caps_unref (fcaps);
689 gst_caps_replace (&allowed, NULL);
691 GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
697 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
699 GstVideoEncoderClass *klass;
702 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
705 caps = klass->getcaps (encoder, filter);
707 caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
709 GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
715 gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
718 GstAllocator *allocator = NULL;
719 GstAllocationParams params;
720 gboolean update_allocator;
722 /* we got configuration from our peer or the decide_allocation method,
724 if (gst_query_get_n_allocation_params (query) > 0) {
725 /* try the allocator */
726 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
727 update_allocator = TRUE;
730 gst_allocation_params_init (¶ms);
731 update_allocator = FALSE;
734 if (update_allocator)
735 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
737 gst_query_add_allocation_param (query, allocator, ¶ms);
739 gst_object_unref (allocator);
745 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
752 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
755 GstVideoEncoder *encoder;
756 gboolean res = FALSE;
758 encoder = GST_VIDEO_ENCODER (parent);
760 switch (GST_QUERY_TYPE (query)) {
763 GstCaps *filter, *caps;
765 gst_query_parse_caps (query, &filter);
766 caps = gst_video_encoder_sink_getcaps (encoder, filter);
767 gst_query_set_caps_result (query, caps);
768 gst_caps_unref (caps);
772 case GST_QUERY_ALLOCATION:
774 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
776 if (klass->propose_allocation)
777 res = klass->propose_allocation (encoder, query);
781 res = gst_pad_query_default (pad, parent, query);
788 gst_video_encoder_finalize (GObject * object)
790 GstVideoEncoder *encoder;
792 GST_DEBUG_OBJECT (object, "finalize");
794 encoder = GST_VIDEO_ENCODER (object);
795 if (encoder->priv->headers) {
796 g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
797 g_list_free (encoder->priv->headers);
799 g_rec_mutex_clear (&encoder->stream_lock);
801 if (encoder->priv->allocator) {
802 gst_object_unref (encoder->priv->allocator);
803 encoder->priv->allocator = NULL;
806 G_OBJECT_CLASS (parent_class)->finalize (object);
810 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
812 switch (GST_EVENT_TYPE (event)) {
813 case GST_EVENT_SEGMENT:
817 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
819 gst_event_copy_segment (event, &segment);
821 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
823 if (segment.format != GST_FORMAT_TIME) {
824 GST_DEBUG_OBJECT (encoder, "received non TIME segment");
825 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
829 encoder->output_segment = segment;
830 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
837 return gst_pad_push_event (encoder->srcpad, event);
841 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
844 GstVideoEncoderClass *encoder_class;
845 gboolean ret = FALSE;
847 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
849 switch (GST_EVENT_TYPE (event)) {
854 gst_event_parse_caps (event, &caps);
855 ret = gst_video_encoder_setcaps (encoder, caps);
856 gst_event_unref (event);
862 GstFlowReturn flow_ret;
864 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
865 encoder->priv->at_eos = TRUE;
867 if (encoder_class->finish) {
868 flow_ret = encoder_class->finish (encoder);
870 flow_ret = GST_FLOW_OK;
873 ret = (flow_ret == GST_FLOW_OK);
874 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
877 case GST_EVENT_SEGMENT:
881 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
883 gst_event_copy_segment (event, &segment);
885 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
887 if (segment.format != GST_FORMAT_TIME) {
888 GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
889 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
893 encoder->priv->at_eos = FALSE;
895 encoder->input_segment = segment;
897 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
900 case GST_EVENT_CUSTOM_DOWNSTREAM:
902 if (gst_video_event_is_force_key_unit (event)) {
903 GstClockTime running_time;
904 gboolean all_headers;
907 if (gst_video_event_parse_downstream_force_key_unit (event,
908 NULL, NULL, &running_time, &all_headers, &count)) {
909 ForcedKeyUnitEvent *fevt;
911 GST_OBJECT_LOCK (encoder);
912 fevt = forced_key_unit_event_new (running_time, all_headers, count);
913 encoder->priv->force_key_unit =
914 g_list_append (encoder->priv->force_key_unit, fevt);
915 GST_OBJECT_UNLOCK (encoder);
917 GST_DEBUG_OBJECT (encoder,
918 "force-key-unit event: running-time %" GST_TIME_FORMAT
919 ", all_headers %d, count %u",
920 GST_TIME_ARGS (running_time), all_headers, count);
922 gst_event_unref (event);
932 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
933 * For EOS this is required because no buffer or serialized event
934 * will come after EOS and nothing could trigger another
935 * _finish_frame() call. *
936 * If the subclass handles sending of EOS manually it can simply
937 * not chain up to the parent class' event handler
939 * For FLUSH_STOP this is required because it is expected
940 * to be forwarded immediately and no buffers are queued anyway.
943 if (!GST_EVENT_IS_SERIALIZED (event)
944 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
945 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
946 ret = gst_video_encoder_push_event (encoder, event);
948 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
949 encoder->priv->current_frame_events =
950 g_list_prepend (encoder->priv->current_frame_events, event);
951 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
960 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
963 GstVideoEncoder *enc;
964 GstVideoEncoderClass *klass;
967 enc = GST_VIDEO_ENCODER (parent);
968 klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
970 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
971 GST_EVENT_TYPE_NAME (event));
973 if (klass->sink_event)
974 ret = klass->sink_event (enc, event);
980 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
983 gboolean ret = FALSE;
985 switch (GST_EVENT_TYPE (event)) {
986 case GST_EVENT_CUSTOM_UPSTREAM:
988 if (gst_video_event_is_force_key_unit (event)) {
989 GstClockTime running_time;
990 gboolean all_headers;
993 if (gst_video_event_parse_upstream_force_key_unit (event,
994 &running_time, &all_headers, &count)) {
995 ForcedKeyUnitEvent *fevt;
997 GST_OBJECT_LOCK (encoder);
998 fevt = forced_key_unit_event_new (running_time, all_headers, count);
999 encoder->priv->force_key_unit =
1000 g_list_append (encoder->priv->force_key_unit, fevt);
1001 GST_OBJECT_UNLOCK (encoder);
1003 GST_DEBUG_OBJECT (encoder,
1004 "force-key-unit event: running-time %" GST_TIME_FORMAT
1005 ", all_headers %d, count %u",
1006 GST_TIME_ARGS (running_time), all_headers, count);
1008 gst_event_unref (event);
1020 gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
1027 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1029 GstVideoEncoder *encoder;
1030 GstVideoEncoderClass *klass;
1031 gboolean ret = FALSE;
1033 encoder = GST_VIDEO_ENCODER (parent);
1034 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1036 GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1038 if (klass->src_event)
1039 ret = klass->src_event (encoder, event);
1045 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1047 GstVideoEncoderPrivate *priv;
1048 GstVideoEncoder *enc;
1051 enc = GST_VIDEO_ENCODER (parent);
1054 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1056 switch (GST_QUERY_TYPE (query)) {
1057 case GST_QUERY_CONVERT:
1059 GstFormat src_fmt, dest_fmt;
1060 gint64 src_val, dest_val;
1062 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1064 gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1065 src_val, &dest_fmt, &dest_val);
1068 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1071 case GST_QUERY_LATENCY:
1074 GstClockTime min_latency, max_latency;
1076 res = gst_pad_peer_query (enc->sinkpad, query);
1078 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1079 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1080 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1081 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1083 GST_OBJECT_LOCK (enc);
1084 min_latency += priv->min_latency;
1085 if (enc->priv->max_latency == GST_CLOCK_TIME_NONE) {
1086 max_latency = GST_CLOCK_TIME_NONE;
1087 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1088 max_latency += enc->priv->max_latency;
1090 GST_OBJECT_UNLOCK (enc);
1092 gst_query_set_latency (query, live, min_latency, max_latency);
1097 res = gst_pad_query_default (pad, parent, query);
1102 GST_DEBUG_OBJECT (enc, "query failed");
1106 static GstVideoCodecFrame *
1107 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1108 GstClockTime pts, GstClockTime dts, GstClockTime duration)
1110 GstVideoEncoderPrivate *priv = encoder->priv;
1111 GstVideoCodecFrame *frame;
1113 frame = g_slice_new0 (GstVideoCodecFrame);
1115 frame->ref_count = 1;
1117 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1118 frame->system_frame_number = priv->system_frame_number;
1119 priv->system_frame_number++;
1121 frame->presentation_frame_number = priv->presentation_frame_number;
1122 priv->presentation_frame_number++;
1123 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1125 frame->events = priv->current_frame_events;
1126 priv->current_frame_events = NULL;
1127 frame->input_buffer = buf;
1130 frame->duration = duration;
1136 static GstFlowReturn
1137 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1139 GstVideoEncoder *encoder;
1140 GstVideoEncoderPrivate *priv;
1141 GstVideoEncoderClass *klass;
1142 GstVideoCodecFrame *frame;
1143 GstClockTime pts, dts, duration;
1144 GstFlowReturn ret = GST_FLOW_OK;
1145 guint64 start, stop, cstart, cstop;
1147 encoder = GST_VIDEO_ENCODER (parent);
1148 priv = encoder->priv;
1149 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1151 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1153 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1155 pts = GST_BUFFER_PTS (buf);
1156 dts = GST_BUFFER_DTS (buf);
1157 duration = GST_BUFFER_DURATION (buf);
1159 GST_LOG_OBJECT (encoder,
1160 "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
1161 ", PTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1162 gst_buffer_get_size (buf), GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
1163 GST_TIME_ARGS (duration));
1171 if (GST_CLOCK_TIME_IS_VALID (duration))
1172 stop = start + duration;
1174 stop = GST_CLOCK_TIME_NONE;
1176 /* Drop buffers outside of segment */
1177 if (!gst_segment_clip (&encoder->output_segment,
1178 GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1179 GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1180 gst_buffer_unref (buf);
1185 gst_video_encoder_new_frame (encoder, buf, cstart, dts, cstop - cstart);
1187 GST_OBJECT_LOCK (encoder);
1188 if (priv->force_key_unit) {
1189 ForcedKeyUnitEvent *fevt = NULL;
1190 GstClockTime running_time;
1194 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1197 for (l = priv->force_key_unit; l; l = l->next) {
1198 ForcedKeyUnitEvent *tmp = l->data;
1200 /* Skip pending keyunits */
1204 /* Simple case, keyunit ASAP */
1205 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1210 /* Event for before this frame */
1211 if (tmp->running_time <= running_time) {
1218 GST_DEBUG_OBJECT (encoder,
1219 "Forcing a key unit at running time %" GST_TIME_FORMAT,
1220 GST_TIME_ARGS (running_time));
1221 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1222 if (fevt->all_headers)
1223 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1224 fevt->pending = TRUE;
1227 GST_OBJECT_UNLOCK (encoder);
1229 gst_video_codec_frame_ref (frame);
1230 priv->frames = g_list_append (priv->frames, frame);
1232 /* new data, more finish needed */
1233 priv->drained = FALSE;
1235 GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1236 frame->presentation_frame_number);
1238 ret = klass->handle_frame (encoder, frame);
1241 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1246 static GstStateChangeReturn
1247 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1249 GstVideoEncoder *encoder;
1250 GstVideoEncoderClass *encoder_class;
1251 GstStateChangeReturn ret;
1253 encoder = GST_VIDEO_ENCODER (element);
1254 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1256 switch (transition) {
1257 case GST_STATE_CHANGE_NULL_TO_READY:
1258 /* open device/library if needed */
1259 if (encoder_class->open && !encoder_class->open (encoder))
1262 case GST_STATE_CHANGE_READY_TO_PAUSED:
1263 /* Initialize device/library if needed */
1264 if (encoder_class->start && !encoder_class->start (encoder))
1271 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1273 switch (transition) {
1274 case GST_STATE_CHANGE_PAUSED_TO_READY:
1275 gst_video_encoder_reset (encoder);
1276 if (encoder_class->stop && !encoder_class->stop (encoder))
1279 case GST_STATE_CHANGE_READY_TO_NULL:
1280 /* close device/library if needed */
1281 if (encoder_class->close && !encoder_class->close (encoder))
1294 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1295 ("Failed to open encoder"));
1296 return GST_STATE_CHANGE_FAILURE;
1301 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1302 ("Failed to start encoder"));
1303 return GST_STATE_CHANGE_FAILURE;
1308 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1309 ("Failed to stop encoder"));
1310 return GST_STATE_CHANGE_FAILURE;
1315 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1316 ("Failed to close encoder"));
1317 return GST_STATE_CHANGE_FAILURE;
1322 * gst_video_encoder_negotiate:
1323 * @encoder: a #GstVideoEncoder
1325 * Negotiate with downstream elements to currently configured #GstVideoCodecState.
1327 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
1330 gst_video_encoder_negotiate (GstVideoEncoder * encoder)
1332 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1333 GstAllocator *allocator;
1334 GstAllocationParams params;
1336 GstVideoCodecState *state = encoder->priv->output_state;
1337 GstVideoInfo *info = &state->info;
1338 GstQuery *query = NULL;
1340 g_return_val_if_fail (state->caps != NULL, FALSE);
1342 if (encoder->priv->output_state_changed) {
1343 state->caps = gst_caps_make_writable (state->caps);
1346 gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1347 "height", G_TYPE_INT, info->height,
1348 "pixel-aspect-ratio", GST_TYPE_FRACTION,
1349 info->par_n, info->par_d, NULL);
1350 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1351 /* variable fps with a max-framerate */
1352 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1353 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1355 /* no variable fps or no max-framerate */
1356 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1357 info->fps_n, info->fps_d, NULL);
1359 if (state->codec_data)
1360 gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1361 state->codec_data, NULL);
1362 encoder->priv->output_state_changed = FALSE;
1365 ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1369 query = gst_query_new_allocation (state->caps, TRUE);
1370 if (!gst_pad_peer_query (encoder->srcpad, query)) {
1371 GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
1374 g_assert (klass->decide_allocation != NULL);
1375 ret = klass->decide_allocation (encoder, query);
1377 GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
1381 goto no_decide_allocation;
1383 /* we got configuration from our peer or the decide_allocation method,
1385 if (gst_query_get_n_allocation_params (query) > 0) {
1386 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1389 gst_allocation_params_init (¶ms);
1392 if (encoder->priv->allocator)
1393 gst_object_unref (encoder->priv->allocator);
1394 encoder->priv->allocator = allocator;
1395 encoder->priv->params = params;
1399 gst_query_unref (query);
1404 no_decide_allocation:
1406 GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
1412 * gst_video_encoder_allocate_output_buffer:
1413 * @encoder: a #GstVideoEncoder
1414 * @size: size of the buffer
1416 * Helper function that allocates a buffer to hold an encoded video frame
1417 * for @encoder's current #GstVideoCodecState.
1419 * Returns: (transfer full): allocated buffer
1422 gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
1426 g_return_val_if_fail (size > 0, NULL);
1428 GST_DEBUG ("alloc src buffer");
1430 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1431 if (G_UNLIKELY (encoder->priv->output_state_changed
1432 || (encoder->priv->output_state
1433 && gst_pad_check_reconfigure (encoder->srcpad))))
1434 gst_video_encoder_negotiate (encoder);
1437 gst_buffer_new_allocate (encoder->priv->allocator, size,
1438 &encoder->priv->params);
1440 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1446 * gst_video_encoder_allocate_output_frame:
1447 * @encoder: a #GstVideoEncoder
1448 * @frame: a #GstVideoCodecFrame
1449 * @size: size of the buffer
1451 * Helper function that allocates a buffer to hold an encoded video frame for @encoder's
1452 * current #GstVideoCodecState. Subclass should already have configured video
1453 * state and set src pad caps.
1455 * The buffer allocated here is owned by the frame and you should only
1456 * keep references to the frame, not the buffer.
1458 * Returns: %GST_FLOW_OK if an output buffer could be allocated
1461 gst_video_encoder_allocate_output_frame (GstVideoEncoder *
1462 encoder, GstVideoCodecFrame * frame, gsize size)
1464 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
1465 g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
1467 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1468 if (G_UNLIKELY (encoder->priv->output_state_changed
1469 || (encoder->priv->output_state
1470 && gst_pad_check_reconfigure (encoder->srcpad))))
1471 gst_video_encoder_negotiate (encoder);
1473 GST_LOG_OBJECT (encoder, "alloc buffer size %d", size);
1475 frame->output_buffer =
1476 gst_buffer_new_allocate (encoder->priv->allocator, size,
1477 &encoder->priv->params);
1479 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1481 return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
1485 * gst_video_encoder_finish_frame:
1486 * @encoder: a #GstVideoEncoder
1487 * @frame: (transfer full): an encoded #GstVideoCodecFrame
1489 * @frame must have a valid encoded data buffer, whose metadata fields
1490 * are then appropriately set according to frame data or no buffer at
1491 * all if the frame should be dropped.
1492 * It is subsequently pushed downstream or provided to @pre_push.
1493 * In any case, the frame is considered finished and released.
1495 * After calling this function the output buffer of the frame is to be
1496 * considered read-only. This function will also change the metadata
1499 * Returns: a #GstFlowReturn resulting from sending data downstream
1502 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1503 GstVideoCodecFrame * frame)
1505 GstVideoEncoderPrivate *priv = encoder->priv;
1506 GstFlowReturn ret = GST_FLOW_OK;
1507 GstVideoEncoderClass *encoder_class;
1509 gboolean send_headers = FALSE;
1510 gboolean discont = (frame->presentation_frame_number == 0);
1512 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1514 GST_LOG_OBJECT (encoder,
1515 "finish frame fpn %d", frame->presentation_frame_number);
1517 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1519 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
1520 && gst_pad_check_reconfigure (encoder->srcpad))))
1521 gst_video_encoder_negotiate (encoder);
1524 if (G_UNLIKELY (priv->output_state == NULL))
1525 goto no_output_state;
1527 /* Push all pending events that arrived before this frame */
1528 for (l = priv->frames; l; l = l->next) {
1529 GstVideoCodecFrame *tmp = l->data;
1534 for (k = g_list_last (tmp->events); k; k = k->prev)
1535 gst_video_encoder_push_event (encoder, k->data);
1536 g_list_free (tmp->events);
1544 /* no buffer data means this frame is skipped/dropped */
1545 if (!frame->output_buffer) {
1546 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1547 GST_TIME_ARGS (frame->pts));
1551 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1552 GstClockTime stream_time, running_time;
1554 ForcedKeyUnitEvent *fevt = NULL;
1558 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1561 GST_OBJECT_LOCK (encoder);
1562 for (l = priv->force_key_unit; l; l = l->next) {
1563 ForcedKeyUnitEvent *tmp = l->data;
1565 /* Skip non-pending keyunits */
1569 /* Simple case, keyunit ASAP */
1570 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1575 /* Event for before this frame */
1576 if (tmp->running_time <= running_time) {
1583 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1585 GST_OBJECT_UNLOCK (encoder);
1589 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1592 ev = gst_video_event_new_downstream_force_key_unit
1593 (frame->pts, stream_time, running_time,
1594 fevt->all_headers, fevt->count);
1596 gst_video_encoder_push_event (encoder, ev);
1598 if (fevt->all_headers)
1599 send_headers = TRUE;
1601 GST_DEBUG_OBJECT (encoder,
1602 "Forced key unit: running-time %" GST_TIME_FORMAT
1603 ", all_headers %d, count %u",
1604 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1605 forced_key_unit_event_free (fevt);
1609 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1610 priv->distance_from_sync = 0;
1611 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1612 /* For keyframes, DTS = PTS */
1613 frame->dts = frame->pts;
1615 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1618 frame->distance_from_sync = priv->distance_from_sync;
1619 priv->distance_from_sync++;
1621 GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
1622 GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
1623 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1625 /* update rate estimate */
1626 priv->bytes += gst_buffer_get_size (frame->output_buffer);
1627 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1628 priv->time += frame->duration;
1630 /* better none than nothing valid */
1631 priv->time = GST_CLOCK_TIME_NONE;
1634 if (G_UNLIKELY (send_headers || priv->new_headers)) {
1635 GList *tmp, *copy = NULL;
1637 GST_DEBUG_OBJECT (encoder, "Sending headers");
1639 /* First make all buffers metadata-writable */
1640 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1641 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1643 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1645 g_list_free (priv->headers);
1646 priv->headers = copy;
1648 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1649 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1651 gst_buffer_ref (tmpbuf);
1652 priv->bytes += gst_buffer_get_size (tmpbuf);
1653 if (G_UNLIKELY (discont)) {
1654 GST_LOG_OBJECT (encoder, "marking discont");
1655 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1659 gst_pad_push (encoder->srcpad, tmpbuf);
1661 priv->new_headers = FALSE;
1664 if (G_UNLIKELY (discont)) {
1665 GST_LOG_OBJECT (encoder, "marking discont");
1666 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1669 if (encoder_class->pre_push)
1670 ret = encoder_class->pre_push (encoder, frame);
1672 /* A reference always needs to be owned by the frame on the buffer.
1673 * For that reason, we use a complete sub-buffer (zero-cost) to push
1675 * The original buffer will be free-ed only when downstream AND the
1676 * current implementation are done with the frame. */
1677 if (ret == GST_FLOW_OK)
1678 ret = gst_pad_push (encoder->srcpad, gst_buffer_ref (frame->output_buffer));
1683 /* unref once from the list */
1684 l = g_list_find (priv->frames, frame);
1686 gst_video_codec_frame_unref (frame);
1687 priv->frames = g_list_delete_link (priv->frames, l);
1689 /* unref because this function takes ownership */
1690 gst_video_codec_frame_unref (frame);
1692 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1699 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1700 GST_ERROR_OBJECT (encoder, "Output state was not configured");
1701 return GST_FLOW_ERROR;
1706 * gst_video_encoder_get_output_state:
1707 * @encoder: a #GstVideoEncoder
1709 * Get the current #GstVideoCodecState
1711 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1713 GstVideoCodecState *
1714 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1716 GstVideoCodecState *state;
1718 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1719 state = gst_video_codec_state_ref (encoder->priv->output_state);
1720 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1726 * gst_video_encoder_set_output_state:
1727 * @encoder: a #GstVideoEncoder
1728 * @caps: (transfer full): the #GstCaps to use for the output
1729 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1731 * Creates a new #GstVideoCodecState with the specified caps as the output state
1733 * Any previously set output state on @decoder will be replaced by the newly
1736 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1737 * framerate, codec-data, .... Those should be specified instead in the returned
1738 * #GstVideoCodecState.
1740 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1741 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1744 * If the subclass wishes to override some fields from the output state (like
1745 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1747 * The new output state will only take effect (set on pads and buffers) starting
1748 * from the next call to #gst_video_encoder_finish_frame().
1750 * Returns: (transfer full): the newly configured output state.
1752 GstVideoCodecState *
1753 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1754 GstVideoCodecState * reference)
1756 GstVideoEncoderPrivate *priv = encoder->priv;
1757 GstVideoCodecState *state;
1759 g_return_val_if_fail (caps != NULL, NULL);
1761 state = _new_output_state (caps, reference);
1763 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1764 if (priv->output_state)
1765 gst_video_codec_state_unref (priv->output_state);
1766 priv->output_state = gst_video_codec_state_ref (state);
1768 priv->output_state_changed = TRUE;
1769 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1775 * gst_video_encoder_set_latency:
1776 * @encoder: a #GstVideoEncoder
1777 * @min_latency: minimum latency
1778 * @max_latency: maximum latency
1780 * Informs baseclass of encoding latency.
1783 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1784 GstClockTime min_latency, GstClockTime max_latency)
1786 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1787 g_return_if_fail (max_latency >= min_latency);
1789 GST_OBJECT_LOCK (encoder);
1790 encoder->priv->min_latency = min_latency;
1791 encoder->priv->max_latency = max_latency;
1792 GST_OBJECT_UNLOCK (encoder);
1794 gst_element_post_message (GST_ELEMENT_CAST (encoder),
1795 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1799 * gst_video_encoder_get_latency:
1800 * @encoder: a #GstVideoEncoder
1801 * @min_latency: (out) (allow-none): address of variable in which to store the
1802 * configured minimum latency, or %NULL
1803 * @max_latency: (out) (allow-none): address of variable in which to store the
1804 * configured maximum latency, or %NULL
1806 * Query the configured encoding latency. Results will be returned via
1807 * @min_latency and @max_latency.
1810 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1811 GstClockTime * min_latency, GstClockTime * max_latency)
1813 GST_OBJECT_LOCK (encoder);
1815 *min_latency = encoder->priv->min_latency;
1817 *max_latency = encoder->priv->max_latency;
1818 GST_OBJECT_UNLOCK (encoder);
1822 * gst_video_encoder_get_oldest_frame:
1823 * @encoder: a #GstVideoEncoder
1825 * Get the oldest unfinished pending #GstVideoCodecFrame
1827 * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
1829 GstVideoCodecFrame *
1830 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1832 GstVideoCodecFrame *frame = NULL;
1834 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1835 if (encoder->priv->frames)
1836 frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
1837 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1839 return (GstVideoCodecFrame *) frame;
1843 * gst_video_encoder_get_frame:
1844 * @encoder: a #GstVideoEnccoder
1845 * @frame_number: system_frame_number of a frame
1847 * Get a pending unfinished #GstVideoCodecFrame
1849 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1851 GstVideoCodecFrame *
1852 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1855 GstVideoCodecFrame *frame = NULL;
1857 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
1859 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1860 for (g = encoder->priv->frames; g; g = g->next) {
1861 GstVideoCodecFrame *tmp = g->data;
1863 if (tmp->system_frame_number == frame_number) {
1864 frame = gst_video_codec_frame_ref (tmp);
1868 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);