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;
166 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
167 struct _ForcedKeyUnitEvent
169 GstClockTime running_time;
170 gboolean pending; /* TRUE if this was requested already */
171 gboolean all_headers;
176 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
178 g_slice_free (ForcedKeyUnitEvent, evt);
181 static ForcedKeyUnitEvent *
182 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
185 ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
187 evt->running_time = running_time;
188 evt->all_headers = all_headers;
194 static GstElementClass *parent_class = NULL;
195 static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
196 static void gst_video_encoder_init (GstVideoEncoder * enc,
197 GstVideoEncoderClass * klass);
199 static void gst_video_encoder_finalize (GObject * object);
201 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
203 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
205 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
207 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
209 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
211 static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
212 element, GstStateChange transition);
213 static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
215 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
217 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
218 encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
219 GstClockTime duration);
221 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
223 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
225 static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
226 encoder, GstQuery * query);
228 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
229 * method to get to the padtemplates */
231 gst_video_encoder_get_type (void)
233 static volatile gsize type = 0;
235 if (g_once_init_enter (&type)) {
237 static const GTypeInfo info = {
238 sizeof (GstVideoEncoderClass),
241 (GClassInitFunc) gst_video_encoder_class_init,
244 sizeof (GstVideoEncoder),
246 (GInstanceInitFunc) gst_video_encoder_init,
248 const GInterfaceInfo preset_interface_info = {
249 NULL, /* interface_init */
250 NULL, /* interface_finalize */
251 NULL /* interface_data */
254 _type = g_type_register_static (GST_TYPE_ELEMENT,
255 "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
256 g_type_add_interface_static (_type, GST_TYPE_PRESET,
257 &preset_interface_info);
258 g_once_init_leave (&type, _type);
264 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
266 GObjectClass *gobject_class;
267 GstElementClass *gstelement_class;
269 gobject_class = G_OBJECT_CLASS (klass);
270 gstelement_class = GST_ELEMENT_CLASS (klass);
272 GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
273 "Base Video Encoder");
275 parent_class = g_type_class_peek_parent (klass);
277 g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
279 gobject_class->finalize = gst_video_encoder_finalize;
281 gstelement_class->change_state =
282 GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
284 klass->sink_event = gst_video_encoder_sink_event_default;
285 klass->src_event = gst_video_encoder_src_event_default;
286 klass->propose_allocation = gst_video_encoder_propose_allocation_default;
290 gst_video_encoder_reset (GstVideoEncoder * encoder)
292 GstVideoEncoderPrivate *priv = encoder->priv;
295 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
297 priv->presentation_frame_number = 0;
298 priv->distance_from_sync = 0;
300 g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
302 g_list_free (priv->force_key_unit);
303 priv->force_key_unit = NULL;
305 priv->drained = TRUE;
306 priv->min_latency = 0;
307 priv->max_latency = 0;
309 g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
310 g_list_free (priv->headers);
311 priv->headers = NULL;
312 priv->new_headers = FALSE;
314 g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
315 g_list_free (priv->current_frame_events);
316 priv->current_frame_events = NULL;
318 for (g = priv->frames; g; g = g->next) {
319 gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
321 g_list_free (priv->frames);
327 if (priv->input_state)
328 gst_video_codec_state_unref (priv->input_state);
329 priv->input_state = NULL;
330 if (priv->output_state)
331 gst_video_codec_state_unref (priv->output_state);
332 priv->output_state = NULL;
334 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
338 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
340 GstVideoEncoderPrivate *priv;
341 GstPadTemplate *pad_template;
344 GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
346 priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
349 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
350 g_return_if_fail (pad_template != NULL);
352 encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
354 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
355 gst_pad_set_event_function (pad,
356 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
357 gst_pad_set_query_function (pad,
358 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
359 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
362 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
363 g_return_if_fail (pad_template != NULL);
365 encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
367 gst_pad_set_query_function (pad,
368 GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
369 gst_pad_set_event_function (pad,
370 GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
371 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
373 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
374 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
376 g_rec_mutex_init (&encoder->stream_lock);
378 priv->at_eos = FALSE;
379 priv->headers = NULL;
380 priv->new_headers = FALSE;
382 gst_video_encoder_reset (encoder);
386 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
387 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
390 gboolean res = FALSE;
392 g_return_val_if_fail (dest_format != NULL, FALSE);
393 g_return_val_if_fail (dest_value != NULL, FALSE);
395 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
398 *dest_value = src_value;
402 if (bytes <= 0 || time <= 0) {
403 GST_DEBUG ("not enough metadata yet to convert");
407 switch (src_format) {
408 case GST_FORMAT_BYTES:
409 switch (*dest_format) {
410 case GST_FORMAT_TIME:
411 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
418 case GST_FORMAT_TIME:
419 switch (*dest_format) {
420 case GST_FORMAT_BYTES:
421 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
429 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
439 * gst_video_encoder_set_headers:
440 * @encoder: a #GstVideoEncoder
441 * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
443 * Set the codec headers to be sent downstream whenever requested.
448 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
450 GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
452 GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
453 if (video_encoder->priv->headers) {
454 g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
456 g_list_free (video_encoder->priv->headers);
458 video_encoder->priv->headers = headers;
459 video_encoder->priv->new_headers = TRUE;
461 GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
465 gst_video_encoder_drain (GstVideoEncoder * enc)
467 GstVideoEncoderPrivate *priv;
468 GstVideoEncoderClass *enc_class;
471 enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
474 GST_DEBUG_OBJECT (enc, "draining");
477 GST_DEBUG_OBJECT (enc, "already drained");
481 if (enc_class->reset) {
482 GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
483 ret = enc_class->reset (enc, TRUE);
485 /* everything should be away now */
487 /* not fatal/impossible though if subclass/enc eats stuff */
488 g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
489 g_list_free (priv->frames);
496 static GstVideoCodecState *
497 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
499 GstVideoCodecState *state;
501 state = g_slice_new0 (GstVideoCodecState);
502 state->ref_count = 1;
503 gst_video_info_init (&state->info);
504 gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
509 GstVideoInfo *tgt, *ref;
512 ref = &reference->info;
514 /* Copy over extra fields from reference state */
515 tgt->interlace_mode = ref->interlace_mode;
516 tgt->flags = ref->flags;
517 tgt->width = ref->width;
518 tgt->height = ref->height;
519 tgt->chroma_site = ref->chroma_site;
520 tgt->colorimetry = ref->colorimetry;
521 tgt->par_n = ref->par_n;
522 tgt->par_d = ref->par_d;
523 tgt->fps_n = ref->fps_n;
524 tgt->fps_d = ref->fps_d;
530 static GstVideoCodecState *
531 _new_input_state (GstCaps * caps)
533 GstVideoCodecState *state;
535 state = g_slice_new0 (GstVideoCodecState);
536 state->ref_count = 1;
537 gst_video_info_init (&state->info);
538 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
540 state->caps = gst_caps_ref (caps);
546 g_slice_free (GstVideoCodecState, state);
552 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
554 GstVideoEncoderClass *encoder_class;
555 GstVideoCodecState *state;
557 gboolean samecaps = FALSE;
559 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
561 /* subclass should do something here ... */
562 g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
564 GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
566 state = _new_input_state (caps);
567 if (G_UNLIKELY (!state))
570 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
572 if (encoder->priv->input_state)
574 gst_video_info_is_equal (&state->info,
575 &encoder->priv->input_state->info);
578 /* arrange draining pending frames */
579 gst_video_encoder_drain (encoder);
581 /* and subclass should be ready to configure format at any time around */
582 ret = encoder_class->set_format (encoder, state);
584 if (encoder->priv->input_state)
585 gst_video_codec_state_unref (encoder->priv->input_state);
586 encoder->priv->input_state = state;
588 gst_video_codec_state_unref (state);
590 /* no need to stir things up */
591 GST_DEBUG_OBJECT (encoder,
592 "new video format identical to configured format");
593 gst_video_codec_state_unref (state);
597 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
600 GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
606 GST_WARNING_OBJECT (encoder, "Failed to parse caps");
612 * gst_video_encoder_proxy_getcaps:
613 * @enc: a #GstVideoEncoder
614 * @caps: initial caps
616 * Returns caps that express @caps (or sink template caps if @caps == NULL)
617 * restricted to resolution/format/... combinations supported by downstream
618 * elements (e.g. muxers).
620 * Returns: a #GstCaps owned by caller
625 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
630 GstCaps *fcaps, *filter_caps;
633 /* Allow downstream to specify width/height/framerate/PAR constraints
634 * and forward them upstream for video converters to handle
637 caps ? gst_caps_ref (caps) :
638 gst_pad_get_pad_template_caps (encoder->sinkpad);
639 allowed = gst_pad_get_allowed_caps (encoder->srcpad);
641 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
646 GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
647 GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
649 filter_caps = gst_caps_new_empty ();
651 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
653 gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
655 for (j = 0; j < gst_caps_get_size (allowed); j++) {
656 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
660 s = gst_structure_new_id_empty (q_name);
661 if ((val = gst_structure_get_value (allowed_s, "width")))
662 gst_structure_set_value (s, "width", val);
663 if ((val = gst_structure_get_value (allowed_s, "height")))
664 gst_structure_set_value (s, "height", val);
665 if ((val = gst_structure_get_value (allowed_s, "framerate")))
666 gst_structure_set_value (s, "framerate", val);
667 if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
668 gst_structure_set_value (s, "pixel-aspect-ratio", val);
670 filter_caps = gst_caps_merge_structure (filter_caps, s);
674 fcaps = gst_caps_intersect (filter_caps, templ_caps);
675 gst_caps_unref (filter_caps);
676 gst_caps_unref (templ_caps);
679 GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
680 filter_caps = gst_caps_intersect (fcaps, filter);
681 gst_caps_unref (fcaps);
686 gst_caps_replace (&allowed, NULL);
688 GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
694 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
696 GstVideoEncoderClass *klass;
699 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
702 caps = klass->getcaps (encoder, filter);
704 caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
706 GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
712 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
719 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
722 GstVideoEncoder *encoder;
723 gboolean res = FALSE;
725 encoder = GST_VIDEO_ENCODER (parent);
727 switch (GST_QUERY_TYPE (query)) {
730 GstCaps *filter, *caps;
732 gst_query_parse_caps (query, &filter);
733 caps = gst_video_encoder_sink_getcaps (encoder, filter);
734 gst_query_set_caps_result (query, caps);
735 gst_caps_unref (caps);
739 case GST_QUERY_ALLOCATION:
741 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
743 if (klass->propose_allocation)
744 res = klass->propose_allocation (encoder, query);
748 res = gst_pad_query_default (pad, parent, query);
755 gst_video_encoder_finalize (GObject * object)
757 GstVideoEncoder *encoder;
759 GST_DEBUG_OBJECT (object, "finalize");
761 encoder = GST_VIDEO_ENCODER (object);
762 if (encoder->priv->headers) {
763 g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
764 g_list_free (encoder->priv->headers);
766 g_rec_mutex_clear (&encoder->stream_lock);
768 G_OBJECT_CLASS (parent_class)->finalize (object);
772 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
774 switch (GST_EVENT_TYPE (event)) {
775 case GST_EVENT_SEGMENT:
779 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
781 gst_event_copy_segment (event, &segment);
783 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
785 if (segment.format != GST_FORMAT_TIME) {
786 GST_DEBUG_OBJECT (encoder, "received non TIME segment");
787 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
791 encoder->output_segment = segment;
792 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
799 return gst_pad_push_event (encoder->srcpad, event);
803 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
806 GstVideoEncoderClass *encoder_class;
807 gboolean ret = FALSE;
809 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
811 switch (GST_EVENT_TYPE (event)) {
816 gst_event_parse_caps (event, &caps);
817 ret = gst_video_encoder_setcaps (encoder, caps);
818 gst_event_unref (event);
824 GstFlowReturn flow_ret;
826 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
827 encoder->priv->at_eos = TRUE;
829 if (encoder_class->finish) {
830 flow_ret = encoder_class->finish (encoder);
832 flow_ret = GST_FLOW_OK;
835 ret = (flow_ret == GST_FLOW_OK);
836 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
839 case GST_EVENT_SEGMENT:
843 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
845 gst_event_copy_segment (event, &segment);
847 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
849 if (segment.format != GST_FORMAT_TIME) {
850 GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
851 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
855 encoder->priv->at_eos = FALSE;
857 encoder->input_segment = segment;
859 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
862 case GST_EVENT_CUSTOM_DOWNSTREAM:
864 if (gst_video_event_is_force_key_unit (event)) {
865 GstClockTime running_time;
866 gboolean all_headers;
869 if (gst_video_event_parse_downstream_force_key_unit (event,
870 NULL, NULL, &running_time, &all_headers, &count)) {
871 ForcedKeyUnitEvent *fevt;
873 GST_OBJECT_LOCK (encoder);
874 fevt = forced_key_unit_event_new (running_time, all_headers, count);
875 encoder->priv->force_key_unit =
876 g_list_append (encoder->priv->force_key_unit, fevt);
877 GST_OBJECT_UNLOCK (encoder);
879 GST_DEBUG_OBJECT (encoder,
880 "force-key-unit event: running-time %" GST_TIME_FORMAT
881 ", all_headers %d, count %u",
882 GST_TIME_ARGS (running_time), all_headers, count);
884 gst_event_unref (event);
894 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
895 * For EOS this is required because no buffer or serialized event
896 * will come after EOS and nothing could trigger another
897 * _finish_frame() call. *
898 * If the subclass handles sending of EOS manually it can simply
899 * not chain up to the parent class' event handler
901 * For FLUSH_STOP this is required because it is expected
902 * to be forwarded immediately and no buffers are queued anyway.
905 if (!GST_EVENT_IS_SERIALIZED (event)
906 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
907 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
908 ret = gst_video_encoder_push_event (encoder, event);
910 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
911 encoder->priv->current_frame_events =
912 g_list_prepend (encoder->priv->current_frame_events, event);
913 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
922 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
925 GstVideoEncoder *enc;
926 GstVideoEncoderClass *klass;
929 enc = GST_VIDEO_ENCODER (parent);
930 klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
932 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
933 GST_EVENT_TYPE_NAME (event));
935 if (klass->sink_event)
936 ret = klass->sink_event (enc, event);
942 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
945 gboolean ret = FALSE;
947 switch (GST_EVENT_TYPE (event)) {
948 case GST_EVENT_CUSTOM_UPSTREAM:
950 if (gst_video_event_is_force_key_unit (event)) {
951 GstClockTime running_time;
952 gboolean all_headers;
955 if (gst_video_event_parse_upstream_force_key_unit (event,
956 &running_time, &all_headers, &count)) {
957 ForcedKeyUnitEvent *fevt;
959 GST_OBJECT_LOCK (encoder);
960 fevt = forced_key_unit_event_new (running_time, all_headers, count);
961 encoder->priv->force_key_unit =
962 g_list_append (encoder->priv->force_key_unit, fevt);
963 GST_OBJECT_UNLOCK (encoder);
965 GST_DEBUG_OBJECT (encoder,
966 "force-key-unit event: running-time %" GST_TIME_FORMAT
967 ", all_headers %d, count %u",
968 GST_TIME_ARGS (running_time), all_headers, count);
970 gst_event_unref (event);
982 gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
989 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
991 GstVideoEncoder *encoder;
992 GstVideoEncoderClass *klass;
993 gboolean ret = FALSE;
995 encoder = GST_VIDEO_ENCODER (parent);
996 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
998 GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1000 if (klass->src_event)
1001 ret = klass->src_event (encoder, event);
1007 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1009 GstVideoEncoderPrivate *priv;
1010 GstVideoEncoder *enc;
1013 enc = GST_VIDEO_ENCODER (parent);
1016 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1018 switch (GST_QUERY_TYPE (query)) {
1019 case GST_QUERY_CONVERT:
1021 GstFormat src_fmt, dest_fmt;
1022 gint64 src_val, dest_val;
1024 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1026 gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1027 src_val, &dest_fmt, &dest_val);
1030 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1033 case GST_QUERY_LATENCY:
1036 GstClockTime min_latency, max_latency;
1038 res = gst_pad_peer_query (enc->sinkpad, query);
1040 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1041 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1042 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1043 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1045 GST_OBJECT_LOCK (enc);
1046 min_latency += priv->min_latency;
1047 if (enc->priv->max_latency == GST_CLOCK_TIME_NONE) {
1048 max_latency = GST_CLOCK_TIME_NONE;
1049 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1050 max_latency += enc->priv->max_latency;
1052 GST_OBJECT_UNLOCK (enc);
1054 gst_query_set_latency (query, live, min_latency, max_latency);
1059 res = gst_pad_query_default (pad, parent, query);
1064 GST_DEBUG_OBJECT (enc, "query failed");
1068 static GstVideoCodecFrame *
1069 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1070 GstClockTime pts, GstClockTime dts, GstClockTime duration)
1072 GstVideoEncoderPrivate *priv = encoder->priv;
1073 GstVideoCodecFrame *frame;
1075 frame = g_slice_new0 (GstVideoCodecFrame);
1077 frame->ref_count = 1;
1079 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1080 frame->system_frame_number = priv->system_frame_number;
1081 priv->system_frame_number++;
1083 frame->presentation_frame_number = priv->presentation_frame_number;
1084 priv->presentation_frame_number++;
1085 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1087 frame->events = priv->current_frame_events;
1088 priv->current_frame_events = NULL;
1089 frame->input_buffer = buf;
1092 frame->duration = duration;
1098 static GstFlowReturn
1099 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1101 GstVideoEncoder *encoder;
1102 GstVideoEncoderPrivate *priv;
1103 GstVideoEncoderClass *klass;
1104 GstVideoCodecFrame *frame;
1105 GstClockTime pts, dts, duration;
1106 GstFlowReturn ret = GST_FLOW_OK;
1107 guint64 start, stop, cstart, cstop;
1109 encoder = GST_VIDEO_ENCODER (parent);
1110 priv = encoder->priv;
1111 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1113 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1115 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1117 pts = GST_BUFFER_PTS (buf);
1118 dts = GST_BUFFER_DTS (buf);
1119 duration = GST_BUFFER_DURATION (buf);
1121 GST_LOG_OBJECT (encoder,
1122 "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
1123 ", PTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1124 gst_buffer_get_size (buf), GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
1125 GST_TIME_ARGS (duration));
1133 if (GST_CLOCK_TIME_IS_VALID (duration))
1134 stop = start + duration;
1136 stop = GST_CLOCK_TIME_NONE;
1138 /* Drop buffers outside of segment */
1139 if (!gst_segment_clip (&encoder->output_segment,
1140 GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1141 GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1142 gst_buffer_unref (buf);
1147 gst_video_encoder_new_frame (encoder, buf, cstart, dts, cstop - cstart);
1149 GST_OBJECT_LOCK (encoder);
1150 if (priv->force_key_unit) {
1151 ForcedKeyUnitEvent *fevt = NULL;
1152 GstClockTime running_time;
1156 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1159 for (l = priv->force_key_unit; l; l = l->next) {
1160 ForcedKeyUnitEvent *tmp = l->data;
1162 /* Skip pending keyunits */
1166 /* Simple case, keyunit ASAP */
1167 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1172 /* Event for before this frame */
1173 if (tmp->running_time <= running_time) {
1180 GST_DEBUG_OBJECT (encoder,
1181 "Forcing a key unit at running time %" GST_TIME_FORMAT,
1182 GST_TIME_ARGS (running_time));
1183 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1184 if (fevt->all_headers)
1185 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1186 fevt->pending = TRUE;
1189 GST_OBJECT_UNLOCK (encoder);
1191 gst_video_codec_frame_ref (frame);
1192 priv->frames = g_list_append (priv->frames, frame);
1194 /* new data, more finish needed */
1195 priv->drained = FALSE;
1197 GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1198 frame->presentation_frame_number);
1200 ret = klass->handle_frame (encoder, frame);
1203 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1208 static GstStateChangeReturn
1209 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1211 GstVideoEncoder *encoder;
1212 GstVideoEncoderClass *encoder_class;
1213 GstStateChangeReturn ret;
1215 encoder = GST_VIDEO_ENCODER (element);
1216 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1218 switch (transition) {
1219 case GST_STATE_CHANGE_NULL_TO_READY:
1220 /* open device/library if needed */
1221 if (encoder_class->open && !encoder_class->open (encoder))
1224 case GST_STATE_CHANGE_READY_TO_PAUSED:
1225 /* Initialize device/library if needed */
1226 if (encoder_class->start && !encoder_class->start (encoder))
1233 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1235 switch (transition) {
1236 case GST_STATE_CHANGE_PAUSED_TO_READY:
1237 gst_video_encoder_reset (encoder);
1238 if (encoder_class->stop && !encoder_class->stop (encoder))
1241 case GST_STATE_CHANGE_READY_TO_NULL:
1242 /* close device/library if needed */
1243 if (encoder_class->close && !encoder_class->close (encoder))
1256 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1257 ("Failed to open encoder"));
1258 return GST_STATE_CHANGE_FAILURE;
1263 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1264 ("Failed to start encoder"));
1265 return GST_STATE_CHANGE_FAILURE;
1270 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1271 ("Failed to stop encoder"));
1272 return GST_STATE_CHANGE_FAILURE;
1277 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1278 ("Failed to close encoder"));
1279 return GST_STATE_CHANGE_FAILURE;
1284 gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
1287 GstVideoCodecState *state = encoder->priv->output_state;
1288 GstVideoInfo *info = &state->info;
1290 g_return_val_if_fail (state->caps != NULL, FALSE);
1292 if (encoder->priv->output_state_changed) {
1293 state->caps = gst_caps_make_writable (state->caps);
1296 gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1297 "height", G_TYPE_INT, info->height,
1298 "pixel-aspect-ratio", GST_TYPE_FRACTION,
1299 info->par_n, info->par_d, NULL);
1300 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1301 /* variable fps with a max-framerate */
1302 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1303 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1305 /* no variable fps or no max-framerate */
1306 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1307 info->fps_n, info->fps_d, NULL);
1309 if (state->codec_data)
1310 gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1311 state->codec_data, NULL);
1312 encoder->priv->output_state_changed = FALSE;
1315 ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1321 * gst_video_encoder_finish_frame:
1322 * @encoder: a #GstVideoEncoder
1323 * @frame: (transfer full): an encoded #GstVideoCodecFrame
1325 * @frame must have a valid encoded data buffer, whose metadata fields
1326 * are then appropriately set according to frame data or no buffer at
1327 * all if the frame should be dropped.
1328 * It is subsequently pushed downstream or provided to @pre_push.
1329 * In any case, the frame is considered finished and released.
1331 * Returns: a #GstFlowReturn resulting from sending data downstream
1336 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1337 GstVideoCodecFrame * frame)
1339 GstVideoEncoderPrivate *priv = encoder->priv;
1340 GstFlowReturn ret = GST_FLOW_OK;
1341 GstVideoEncoderClass *encoder_class;
1343 gboolean send_headers = FALSE;
1344 gboolean discont = (frame->presentation_frame_number == 0);
1346 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1348 GST_LOG_OBJECT (encoder,
1349 "finish frame fpn %d", frame->presentation_frame_number);
1351 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1353 if (G_UNLIKELY (priv->output_state_changed))
1354 gst_video_encoder_set_src_caps (encoder);
1356 if (G_UNLIKELY (priv->output_state == NULL))
1357 goto no_output_state;
1359 /* Push all pending events that arrived before this frame */
1360 for (l = priv->frames; l; l = l->next) {
1361 GstVideoCodecFrame *tmp = l->data;
1366 for (k = g_list_last (tmp->events); k; k = k->prev)
1367 gst_video_encoder_push_event (encoder, k->data);
1368 g_list_free (tmp->events);
1376 /* no buffer data means this frame is skipped/dropped */
1377 if (!frame->output_buffer) {
1378 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1379 GST_TIME_ARGS (frame->pts));
1383 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1384 GstClockTime stream_time, running_time;
1386 ForcedKeyUnitEvent *fevt = NULL;
1390 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1393 GST_OBJECT_LOCK (encoder);
1394 for (l = priv->force_key_unit; l; l = l->next) {
1395 ForcedKeyUnitEvent *tmp = l->data;
1397 /* Skip non-pending keyunits */
1401 /* Simple case, keyunit ASAP */
1402 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1407 /* Event for before this frame */
1408 if (tmp->running_time <= running_time) {
1415 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1417 GST_OBJECT_UNLOCK (encoder);
1421 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1424 ev = gst_video_event_new_downstream_force_key_unit
1425 (frame->pts, stream_time, running_time,
1426 fevt->all_headers, fevt->count);
1428 gst_video_encoder_push_event (encoder, ev);
1430 if (fevt->all_headers)
1431 send_headers = TRUE;
1433 GST_DEBUG_OBJECT (encoder,
1434 "Forced key unit: running-time %" GST_TIME_FORMAT
1435 ", all_headers %d, count %u",
1436 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1437 forced_key_unit_event_free (fevt);
1441 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1442 priv->distance_from_sync = 0;
1443 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1444 /* For keyframes, DTS = PTS */
1445 frame->dts = frame->pts;
1447 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1450 frame->distance_from_sync = priv->distance_from_sync;
1451 priv->distance_from_sync++;
1453 GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
1454 GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
1455 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1457 /* update rate estimate */
1458 priv->bytes += gst_buffer_get_size (frame->output_buffer);
1459 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1460 priv->time += frame->duration;
1462 /* better none than nothing valid */
1463 priv->time = GST_CLOCK_TIME_NONE;
1466 if (G_UNLIKELY (send_headers || priv->new_headers)) {
1467 GList *tmp, *copy = NULL;
1469 GST_DEBUG_OBJECT (encoder, "Sending headers");
1471 /* First make all buffers metadata-writable */
1472 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1473 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1475 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1477 g_list_free (priv->headers);
1478 priv->headers = copy;
1480 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1481 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1483 gst_buffer_ref (tmpbuf);
1484 priv->bytes += gst_buffer_get_size (tmpbuf);
1485 if (G_UNLIKELY (discont)) {
1486 GST_LOG_OBJECT (encoder, "marking discont");
1487 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1491 gst_pad_push (encoder->srcpad, tmpbuf);
1493 priv->new_headers = FALSE;
1496 if (G_UNLIKELY (discont)) {
1497 GST_LOG_OBJECT (encoder, "marking discont");
1498 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1501 if (encoder_class->pre_push)
1502 ret = encoder_class->pre_push (encoder, frame);
1504 /* A reference always needs to be owned by the frame on the buffer.
1505 * For that reason, we use a complete sub-buffer (zero-cost) to push
1507 * The original buffer will be free-ed only when downstream AND the
1508 * current implementation are done with the frame. */
1509 if (ret == GST_FLOW_OK)
1510 ret = gst_pad_push (encoder->srcpad, gst_buffer_ref (frame->output_buffer));
1515 /* unref once from the list */
1516 l = g_list_find (priv->frames, frame);
1518 gst_video_codec_frame_unref (frame);
1519 priv->frames = g_list_delete_link (priv->frames, l);
1521 /* unref because this function takes ownership */
1522 gst_video_codec_frame_unref (frame);
1524 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1531 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1532 GST_ERROR_OBJECT (encoder, "Output state was not configured");
1533 return GST_FLOW_ERROR;
1538 * gst_video_encoder_get_output_state:
1539 * @encoder: a #GstVideoEncoder
1541 * Get the current #GstVideoCodecState
1543 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1547 GstVideoCodecState *
1548 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1550 GstVideoCodecState *state;
1552 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1553 state = gst_video_codec_state_ref (encoder->priv->output_state);
1554 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1560 * gst_video_encoder_set_output_state:
1561 * @encoder: a #GstVideoEncoder
1562 * @caps: (transfer full): the #GstCaps to use for the output
1563 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1565 * Creates a new #GstVideoCodecState with the specified caps as the output state
1567 * Any previously set output state on @decoder will be replaced by the newly
1570 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1571 * framerate, codec-data, .... Those should be specified instead in the returned
1572 * #GstVideoCodecState.
1574 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1575 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1578 * If the subclass wishes to override some fields from the output state (like
1579 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1581 * The new output state will only take effect (set on pads and buffers) starting
1582 * from the next call to #gst_video_encoder_finish_frame().
1584 * Returns: (transfer full): the newly configured output state.
1588 GstVideoCodecState *
1589 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1590 GstVideoCodecState * reference)
1592 GstVideoEncoderPrivate *priv = encoder->priv;
1593 GstVideoCodecState *state;
1595 g_return_val_if_fail (caps != NULL, NULL);
1597 state = _new_output_state (caps, reference);
1599 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1600 if (priv->output_state)
1601 gst_video_codec_state_unref (priv->output_state);
1602 priv->output_state = gst_video_codec_state_ref (state);
1604 priv->output_state_changed = TRUE;
1605 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1611 * gst_video_encoder_set_latency:
1612 * @encoder: a #GstVideoEncoder
1613 * @min_latency: minimum latency
1614 * @max_latency: maximum latency
1616 * Informs baseclass of encoding latency.
1621 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1622 GstClockTime min_latency, GstClockTime max_latency)
1624 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1625 g_return_if_fail (max_latency >= min_latency);
1627 GST_OBJECT_LOCK (encoder);
1628 encoder->priv->min_latency = min_latency;
1629 encoder->priv->max_latency = max_latency;
1630 GST_OBJECT_UNLOCK (encoder);
1632 gst_element_post_message (GST_ELEMENT_CAST (encoder),
1633 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1637 * gst_video_encoder_get_latency:
1638 * @encoder: a #GstVideoEncoder
1639 * @min_latency: (out) (allow-none): address of variable in which to store the
1640 * configured minimum latency, or %NULL
1641 * @max_latency: (out) (allow-none): address of variable in which to store the
1642 * configured maximum latency, or %NULL
1644 * Query the configured encoding latency. Results will be returned via
1645 * @min_latency and @max_latency.
1650 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1651 GstClockTime * min_latency, GstClockTime * max_latency)
1653 GST_OBJECT_LOCK (encoder);
1655 *min_latency = encoder->priv->min_latency;
1657 *max_latency = encoder->priv->max_latency;
1658 GST_OBJECT_UNLOCK (encoder);
1662 * gst_video_encoder_get_oldest_frame:
1663 * @encoder: a #GstVideoEncoder
1665 * Get the oldest unfinished pending #GstVideoCodecFrame
1667 * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
1671 GstVideoCodecFrame *
1672 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1674 GstVideoCodecFrame *frame = NULL;
1676 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1677 if (encoder->priv->frames)
1678 frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
1679 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1681 return (GstVideoCodecFrame *) frame;
1685 * gst_video_encoder_get_frame:
1686 * @encoder: a #GstVideoEnccoder
1687 * @frame_number: system_frame_number of a frame
1689 * Get a pending unfinished #GstVideoCodecFrame
1691 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1695 GstVideoCodecFrame *
1696 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1699 GstVideoCodecFrame *frame = NULL;
1701 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
1703 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1704 for (g = encoder->priv->frames; g; g = g->next) {
1705 GstVideoCodecFrame *tmp = g->data;
1707 if (tmp->system_frame_number == frame_number) {
1708 frame = gst_video_codec_frame_ref (tmp);
1712 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);