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 * After calling this function the output buffer of the frame is to be
1332 * considered read-only. This function will also change the metadata
1335 * Returns: a #GstFlowReturn resulting from sending data downstream
1340 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1341 GstVideoCodecFrame * frame)
1343 GstVideoEncoderPrivate *priv = encoder->priv;
1344 GstFlowReturn ret = GST_FLOW_OK;
1345 GstVideoEncoderClass *encoder_class;
1347 gboolean send_headers = FALSE;
1348 gboolean discont = (frame->presentation_frame_number == 0);
1350 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1352 GST_LOG_OBJECT (encoder,
1353 "finish frame fpn %d", frame->presentation_frame_number);
1355 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1357 if (G_UNLIKELY (priv->output_state_changed))
1358 gst_video_encoder_set_src_caps (encoder);
1360 if (G_UNLIKELY (priv->output_state == NULL))
1361 goto no_output_state;
1363 /* Push all pending events that arrived before this frame */
1364 for (l = priv->frames; l; l = l->next) {
1365 GstVideoCodecFrame *tmp = l->data;
1370 for (k = g_list_last (tmp->events); k; k = k->prev)
1371 gst_video_encoder_push_event (encoder, k->data);
1372 g_list_free (tmp->events);
1380 /* no buffer data means this frame is skipped/dropped */
1381 if (!frame->output_buffer) {
1382 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1383 GST_TIME_ARGS (frame->pts));
1387 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1388 GstClockTime stream_time, running_time;
1390 ForcedKeyUnitEvent *fevt = NULL;
1394 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1397 GST_OBJECT_LOCK (encoder);
1398 for (l = priv->force_key_unit; l; l = l->next) {
1399 ForcedKeyUnitEvent *tmp = l->data;
1401 /* Skip non-pending keyunits */
1405 /* Simple case, keyunit ASAP */
1406 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1411 /* Event for before this frame */
1412 if (tmp->running_time <= running_time) {
1419 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1421 GST_OBJECT_UNLOCK (encoder);
1425 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1428 ev = gst_video_event_new_downstream_force_key_unit
1429 (frame->pts, stream_time, running_time,
1430 fevt->all_headers, fevt->count);
1432 gst_video_encoder_push_event (encoder, ev);
1434 if (fevt->all_headers)
1435 send_headers = TRUE;
1437 GST_DEBUG_OBJECT (encoder,
1438 "Forced key unit: running-time %" GST_TIME_FORMAT
1439 ", all_headers %d, count %u",
1440 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1441 forced_key_unit_event_free (fevt);
1445 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1446 priv->distance_from_sync = 0;
1447 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1448 /* For keyframes, DTS = PTS */
1449 frame->dts = frame->pts;
1451 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1454 frame->distance_from_sync = priv->distance_from_sync;
1455 priv->distance_from_sync++;
1457 GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
1458 GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
1459 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1461 /* update rate estimate */
1462 priv->bytes += gst_buffer_get_size (frame->output_buffer);
1463 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1464 priv->time += frame->duration;
1466 /* better none than nothing valid */
1467 priv->time = GST_CLOCK_TIME_NONE;
1470 if (G_UNLIKELY (send_headers || priv->new_headers)) {
1471 GList *tmp, *copy = NULL;
1473 GST_DEBUG_OBJECT (encoder, "Sending headers");
1475 /* First make all buffers metadata-writable */
1476 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1477 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1479 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1481 g_list_free (priv->headers);
1482 priv->headers = copy;
1484 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1485 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1487 gst_buffer_ref (tmpbuf);
1488 priv->bytes += gst_buffer_get_size (tmpbuf);
1489 if (G_UNLIKELY (discont)) {
1490 GST_LOG_OBJECT (encoder, "marking discont");
1491 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1495 gst_pad_push (encoder->srcpad, tmpbuf);
1497 priv->new_headers = FALSE;
1500 if (G_UNLIKELY (discont)) {
1501 GST_LOG_OBJECT (encoder, "marking discont");
1502 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1505 if (encoder_class->pre_push)
1506 ret = encoder_class->pre_push (encoder, frame);
1508 /* A reference always needs to be owned by the frame on the buffer.
1509 * For that reason, we use a complete sub-buffer (zero-cost) to push
1511 * The original buffer will be free-ed only when downstream AND the
1512 * current implementation are done with the frame. */
1513 if (ret == GST_FLOW_OK)
1514 ret = gst_pad_push (encoder->srcpad, gst_buffer_ref (frame->output_buffer));
1519 /* unref once from the list */
1520 l = g_list_find (priv->frames, frame);
1522 gst_video_codec_frame_unref (frame);
1523 priv->frames = g_list_delete_link (priv->frames, l);
1525 /* unref because this function takes ownership */
1526 gst_video_codec_frame_unref (frame);
1528 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1535 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1536 GST_ERROR_OBJECT (encoder, "Output state was not configured");
1537 return GST_FLOW_ERROR;
1542 * gst_video_encoder_get_output_state:
1543 * @encoder: a #GstVideoEncoder
1545 * Get the current #GstVideoCodecState
1547 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1551 GstVideoCodecState *
1552 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1554 GstVideoCodecState *state;
1556 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1557 state = gst_video_codec_state_ref (encoder->priv->output_state);
1558 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1564 * gst_video_encoder_set_output_state:
1565 * @encoder: a #GstVideoEncoder
1566 * @caps: (transfer full): the #GstCaps to use for the output
1567 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1569 * Creates a new #GstVideoCodecState with the specified caps as the output state
1571 * Any previously set output state on @decoder will be replaced by the newly
1574 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1575 * framerate, codec-data, .... Those should be specified instead in the returned
1576 * #GstVideoCodecState.
1578 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1579 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1582 * If the subclass wishes to override some fields from the output state (like
1583 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1585 * The new output state will only take effect (set on pads and buffers) starting
1586 * from the next call to #gst_video_encoder_finish_frame().
1588 * Returns: (transfer full): the newly configured output state.
1592 GstVideoCodecState *
1593 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1594 GstVideoCodecState * reference)
1596 GstVideoEncoderPrivate *priv = encoder->priv;
1597 GstVideoCodecState *state;
1599 g_return_val_if_fail (caps != NULL, NULL);
1601 state = _new_output_state (caps, reference);
1603 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1604 if (priv->output_state)
1605 gst_video_codec_state_unref (priv->output_state);
1606 priv->output_state = gst_video_codec_state_ref (state);
1608 priv->output_state_changed = TRUE;
1609 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1615 * gst_video_encoder_set_latency:
1616 * @encoder: a #GstVideoEncoder
1617 * @min_latency: minimum latency
1618 * @max_latency: maximum latency
1620 * Informs baseclass of encoding latency.
1625 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1626 GstClockTime min_latency, GstClockTime max_latency)
1628 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1629 g_return_if_fail (max_latency >= min_latency);
1631 GST_OBJECT_LOCK (encoder);
1632 encoder->priv->min_latency = min_latency;
1633 encoder->priv->max_latency = max_latency;
1634 GST_OBJECT_UNLOCK (encoder);
1636 gst_element_post_message (GST_ELEMENT_CAST (encoder),
1637 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1641 * gst_video_encoder_get_latency:
1642 * @encoder: a #GstVideoEncoder
1643 * @min_latency: (out) (allow-none): address of variable in which to store the
1644 * configured minimum latency, or %NULL
1645 * @max_latency: (out) (allow-none): address of variable in which to store the
1646 * configured maximum latency, or %NULL
1648 * Query the configured encoding latency. Results will be returned via
1649 * @min_latency and @max_latency.
1654 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1655 GstClockTime * min_latency, GstClockTime * max_latency)
1657 GST_OBJECT_LOCK (encoder);
1659 *min_latency = encoder->priv->min_latency;
1661 *max_latency = encoder->priv->max_latency;
1662 GST_OBJECT_UNLOCK (encoder);
1666 * gst_video_encoder_get_oldest_frame:
1667 * @encoder: a #GstVideoEncoder
1669 * Get the oldest unfinished pending #GstVideoCodecFrame
1671 * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
1675 GstVideoCodecFrame *
1676 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1678 GstVideoCodecFrame *frame = NULL;
1680 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1681 if (encoder->priv->frames)
1682 frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
1683 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1685 return (GstVideoCodecFrame *) frame;
1689 * gst_video_encoder_get_frame:
1690 * @encoder: a #GstVideoEnccoder
1691 * @frame_number: system_frame_number of a frame
1693 * Get a pending unfinished #GstVideoCodecFrame
1695 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1699 GstVideoCodecFrame *
1700 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1703 GstVideoCodecFrame *frame = NULL;
1705 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
1707 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1708 for (g = encoder->priv->frames; g; g = g->next) {
1709 GstVideoCodecFrame *tmp = g->data;
1711 if (tmp->system_frame_number == frame_number) {
1712 frame = gst_video_codec_frame_ref (tmp);
1716 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);