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"
127 GST_DEBUG_CATEGORY (videoencoder_debug);
128 #define GST_CAT_DEFAULT videoencoder_debug
130 #define GST_VIDEO_ENCODER_GET_PRIVATE(obj) \
131 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
132 GstVideoEncoderPrivate))
134 struct _GstVideoEncoderPrivate
136 guint64 presentation_frame_number;
137 int distance_from_sync;
139 /* FIXME : (and introduce a context ?) */
146 GList *current_frame_events;
149 gboolean new_headers; /* Whether new headers were just set */
151 GList *force_key_unit; /* List of pending forced keyunits */
153 guint64 system_frame_number;
155 GList *frames; /* Protected with OBJECT_LOCK */
156 GstVideoCodecState *input_state;
157 GstVideoCodecState *output_state;
158 gboolean output_state_changed;
164 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
165 struct _ForcedKeyUnitEvent
167 GstClockTime running_time;
168 gboolean pending; /* TRUE if this was requested already */
169 gboolean all_headers;
174 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
176 g_slice_free (ForcedKeyUnitEvent, evt);
179 static ForcedKeyUnitEvent *
180 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
183 ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
185 evt->running_time = running_time;
186 evt->all_headers = all_headers;
192 static GstElementClass *parent_class = NULL;
193 static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
194 static void gst_video_encoder_init (GstVideoEncoder * enc,
195 GstVideoEncoderClass * klass);
197 static void gst_video_encoder_finalize (GObject * object);
199 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
201 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
203 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
205 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
207 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
209 static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
210 element, GstStateChange transition);
211 static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
213 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
215 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
216 encoder, GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
218 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
220 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
223 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
224 * method to get to the padtemplates */
226 gst_video_encoder_get_type (void)
228 static volatile gsize type = 0;
230 if (g_once_init_enter (&type)) {
232 static const GTypeInfo info = {
233 sizeof (GstVideoEncoderClass),
236 (GClassInitFunc) gst_video_encoder_class_init,
239 sizeof (GstVideoEncoder),
241 (GInstanceInitFunc) gst_video_encoder_init,
243 const GInterfaceInfo preset_interface_info = {
244 NULL, /* interface_init */
245 NULL, /* interface_finalize */
246 NULL /* interface_data */
249 _type = g_type_register_static (GST_TYPE_ELEMENT,
250 "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
251 g_type_add_interface_static (_type, GST_TYPE_PRESET,
252 &preset_interface_info);
253 g_once_init_leave (&type, _type);
259 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
261 GObjectClass *gobject_class;
262 GstElementClass *gstelement_class;
264 gobject_class = G_OBJECT_CLASS (klass);
265 gstelement_class = GST_ELEMENT_CLASS (klass);
267 GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
268 "Base Video Encoder");
270 parent_class = g_type_class_peek_parent (klass);
272 g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
274 gobject_class->finalize = gst_video_encoder_finalize;
276 gstelement_class->change_state =
277 GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
279 klass->sink_event = gst_video_encoder_sink_event_default;
280 klass->src_event = gst_video_encoder_src_event_default;
284 gst_video_encoder_reset (GstVideoEncoder * encoder)
286 GstVideoEncoderPrivate *priv = encoder->priv;
289 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
291 priv->presentation_frame_number = 0;
292 priv->distance_from_sync = 0;
294 g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
296 g_list_free (priv->force_key_unit);
297 priv->force_key_unit = NULL;
299 priv->drained = TRUE;
300 priv->min_latency = 0;
301 priv->max_latency = 0;
303 g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
304 g_list_free (priv->headers);
305 priv->headers = NULL;
306 priv->new_headers = FALSE;
308 g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
309 g_list_free (priv->current_frame_events);
310 priv->current_frame_events = NULL;
312 for (g = priv->frames; g; g = g->next) {
313 gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
315 g_list_free (priv->frames);
321 if (priv->input_state)
322 gst_video_codec_state_unref (priv->input_state);
323 priv->input_state = NULL;
324 if (priv->output_state)
325 gst_video_codec_state_unref (priv->output_state);
326 priv->output_state = NULL;
328 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
332 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
334 GstVideoEncoderPrivate *priv;
335 GstPadTemplate *pad_template;
338 GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
340 priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
343 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
344 g_return_if_fail (pad_template != NULL);
346 encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
348 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
349 gst_pad_set_event_function (pad,
350 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
351 gst_pad_set_query_function (pad,
352 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
353 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
356 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
357 g_return_if_fail (pad_template != NULL);
359 encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
361 gst_pad_set_query_function (pad,
362 GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
363 gst_pad_set_event_function (pad,
364 GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
365 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
367 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
368 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
370 g_rec_mutex_init (&encoder->stream_lock);
372 priv->at_eos = FALSE;
373 priv->headers = NULL;
374 priv->new_headers = FALSE;
376 gst_video_encoder_reset (encoder);
380 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
381 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
384 gboolean res = FALSE;
386 g_return_val_if_fail (dest_format != NULL, FALSE);
387 g_return_val_if_fail (dest_value != NULL, FALSE);
389 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
392 *dest_value = src_value;
396 if (bytes <= 0 || time <= 0) {
397 GST_DEBUG ("not enough metadata yet to convert");
401 switch (src_format) {
402 case GST_FORMAT_BYTES:
403 switch (*dest_format) {
404 case GST_FORMAT_TIME:
405 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
412 case GST_FORMAT_TIME:
413 switch (*dest_format) {
414 case GST_FORMAT_BYTES:
415 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
423 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
433 * gst_video_encoder_set_headers:
434 * @encoder: a #GstVideoEncoder
435 * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
437 * Set the codec headers to be sent downstream whenever requested.
442 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
444 GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
446 GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
447 if (video_encoder->priv->headers) {
448 g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
450 g_list_free (video_encoder->priv->headers);
452 video_encoder->priv->headers = headers;
453 video_encoder->priv->new_headers = TRUE;
455 GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
459 gst_video_encoder_drain (GstVideoEncoder * enc)
461 GstVideoEncoderPrivate *priv;
462 GstVideoEncoderClass *enc_class;
465 enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
468 GST_DEBUG_OBJECT (enc, "draining");
471 GST_DEBUG_OBJECT (enc, "already drained");
475 if (enc_class->reset) {
476 GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
477 ret = enc_class->reset (enc, TRUE);
479 /* everything should be away now */
481 /* not fatal/impossible though if subclass/enc eats stuff */
482 g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
483 g_list_free (priv->frames);
490 static GstVideoCodecState *
491 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
493 GstVideoCodecState *state;
495 state = g_slice_new0 (GstVideoCodecState);
496 state->ref_count = 1;
497 gst_video_info_init (&state->info);
498 gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
503 GstVideoInfo *tgt, *ref;
506 ref = &reference->info;
508 /* Copy over extra fields from reference state */
509 tgt->interlace_mode = ref->interlace_mode;
510 tgt->flags = ref->flags;
511 tgt->width = ref->width;
512 tgt->height = ref->height;
513 tgt->chroma_site = ref->chroma_site;
514 tgt->colorimetry = ref->colorimetry;
515 tgt->par_n = ref->par_n;
516 tgt->par_d = ref->par_d;
517 tgt->fps_n = ref->fps_n;
518 tgt->fps_d = ref->fps_d;
524 static GstVideoCodecState *
525 _new_input_state (GstCaps * caps)
527 GstVideoCodecState *state;
529 state = g_slice_new0 (GstVideoCodecState);
530 state->ref_count = 1;
531 gst_video_info_init (&state->info);
532 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
534 state->caps = gst_caps_ref (caps);
540 g_slice_free (GstVideoCodecState, state);
546 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
548 GstVideoEncoderClass *encoder_class;
549 GstVideoCodecState *state;
551 gboolean samecaps = FALSE;
553 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
555 /* subclass should do something here ... */
556 g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
558 GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
560 state = _new_input_state (caps);
561 if (G_UNLIKELY (!state))
564 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
566 if (encoder->priv->input_state)
568 gst_video_info_is_equal (&state->info,
569 &encoder->priv->input_state->info);
572 /* arrange draining pending frames */
573 gst_video_encoder_drain (encoder);
575 /* and subclass should be ready to configure format at any time around */
576 ret = encoder_class->set_format (encoder, state);
578 if (encoder->priv->input_state)
579 gst_video_codec_state_unref (encoder->priv->input_state);
580 encoder->priv->input_state = state;
582 gst_video_codec_state_unref (state);
584 /* no need to stir things up */
585 GST_DEBUG_OBJECT (encoder,
586 "new video format identical to configured format");
587 gst_video_codec_state_unref (state);
591 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
594 GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
600 GST_WARNING_OBJECT (encoder, "Failed to parse caps");
606 * gst_video_encoder_proxy_getcaps:
607 * @enc: a #GstVideoEncoder
608 * @caps: initial caps
610 * Returns caps that express @caps (or sink template caps if @caps == NULL)
611 * restricted to resolution/format/... combinations supported by downstream
612 * elements (e.g. muxers).
614 * Returns: a #GstCaps owned by caller
619 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
624 GstCaps *fcaps, *filter_caps;
627 /* Allow downstream to specify width/height/framerate/PAR constraints
628 * and forward them upstream for video converters to handle
631 caps ? gst_caps_ref (caps) :
632 gst_pad_get_pad_template_caps (encoder->sinkpad);
633 allowed = gst_pad_get_allowed_caps (encoder->srcpad);
635 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
640 GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
641 GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
643 filter_caps = gst_caps_new_empty ();
645 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
647 gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
649 for (j = 0; j < gst_caps_get_size (allowed); j++) {
650 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
654 s = gst_structure_new_id_empty (q_name);
655 if ((val = gst_structure_get_value (allowed_s, "width")))
656 gst_structure_set_value (s, "width", val);
657 if ((val = gst_structure_get_value (allowed_s, "height")))
658 gst_structure_set_value (s, "height", val);
659 if ((val = gst_structure_get_value (allowed_s, "framerate")))
660 gst_structure_set_value (s, "framerate", val);
661 if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
662 gst_structure_set_value (s, "pixel-aspect-ratio", val);
664 filter_caps = gst_caps_merge_structure (filter_caps, s);
668 fcaps = gst_caps_intersect (filter_caps, templ_caps);
669 gst_caps_unref (filter_caps);
670 gst_caps_unref (templ_caps);
673 GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
674 filter_caps = gst_caps_intersect (fcaps, filter);
675 gst_caps_unref (fcaps);
680 gst_caps_replace (&allowed, NULL);
682 GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
688 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
690 GstVideoEncoderClass *klass;
693 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
696 caps = klass->getcaps (encoder, filter);
698 caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
700 GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
707 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
710 GstVideoEncoder *encoder;
711 gboolean res = FALSE;
713 encoder = GST_VIDEO_ENCODER (parent);
715 switch (GST_QUERY_TYPE (query)) {
718 GstCaps *filter, *caps;
720 gst_query_parse_caps (query, &filter);
721 caps = gst_video_encoder_sink_getcaps (encoder, filter);
722 gst_query_set_caps_result (query, caps);
723 gst_caps_unref (caps);
728 res = gst_pad_query_default (pad, parent, query);
735 gst_video_encoder_finalize (GObject * object)
737 GstVideoEncoder *encoder;
739 GST_DEBUG_OBJECT (object, "finalize");
741 encoder = GST_VIDEO_ENCODER (object);
742 if (encoder->priv->headers) {
743 g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
744 g_list_free (encoder->priv->headers);
746 g_rec_mutex_clear (&encoder->stream_lock);
748 G_OBJECT_CLASS (parent_class)->finalize (object);
752 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
754 switch (GST_EVENT_TYPE (event)) {
755 case GST_EVENT_SEGMENT:
759 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
761 gst_event_copy_segment (event, &segment);
763 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
765 if (segment.format != GST_FORMAT_TIME) {
766 GST_DEBUG_OBJECT (encoder, "received non TIME segment");
767 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
771 encoder->output_segment = segment;
772 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
779 return gst_pad_push_event (encoder->srcpad, event);
783 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
786 GstVideoEncoderClass *encoder_class;
787 gboolean ret = FALSE;
789 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
791 switch (GST_EVENT_TYPE (event)) {
796 gst_event_parse_caps (event, &caps);
797 ret = gst_video_encoder_setcaps (encoder, caps);
798 gst_event_unref (event);
804 GstFlowReturn flow_ret;
806 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
807 encoder->priv->at_eos = TRUE;
809 if (encoder_class->finish) {
810 flow_ret = encoder_class->finish (encoder);
812 flow_ret = GST_FLOW_OK;
815 ret = (flow_ret == GST_FLOW_OK);
816 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
819 case GST_EVENT_SEGMENT:
823 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
825 gst_event_copy_segment (event, &segment);
827 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
829 if (segment.format != GST_FORMAT_TIME) {
830 GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
831 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
835 encoder->priv->at_eos = FALSE;
837 encoder->input_segment = segment;
839 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
842 case GST_EVENT_CUSTOM_DOWNSTREAM:
844 if (gst_video_event_is_force_key_unit (event)) {
845 GstClockTime running_time;
846 gboolean all_headers;
849 if (gst_video_event_parse_downstream_force_key_unit (event,
850 NULL, NULL, &running_time, &all_headers, &count)) {
851 ForcedKeyUnitEvent *fevt;
853 GST_OBJECT_LOCK (encoder);
854 fevt = forced_key_unit_event_new (running_time, all_headers, count);
855 encoder->priv->force_key_unit =
856 g_list_append (encoder->priv->force_key_unit, fevt);
857 GST_OBJECT_UNLOCK (encoder);
859 GST_DEBUG_OBJECT (encoder,
860 "force-key-unit event: running-time %" GST_TIME_FORMAT
861 ", all_headers %d, count %u",
862 GST_TIME_ARGS (running_time), all_headers, count);
864 gst_event_unref (event);
874 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
875 * For EOS this is required because no buffer or serialized event
876 * will come after EOS and nothing could trigger another
877 * _finish_frame() call. *
878 * If the subclass handles sending of EOS manually it can simply
879 * not chain up to the parent class' event handler
881 * For FLUSH_STOP this is required because it is expected
882 * to be forwarded immediately and no buffers are queued anyway.
885 if (!GST_EVENT_IS_SERIALIZED (event)
886 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
887 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
888 ret = gst_video_encoder_push_event (encoder, event);
890 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
891 encoder->priv->current_frame_events =
892 g_list_prepend (encoder->priv->current_frame_events, event);
893 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
902 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
905 GstVideoEncoder *enc;
906 GstVideoEncoderClass *klass;
909 enc = GST_VIDEO_ENCODER (parent);
910 klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
912 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
913 GST_EVENT_TYPE_NAME (event));
915 if (klass->sink_event)
916 ret = klass->sink_event (enc, event);
922 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
925 gboolean ret = FALSE;
927 switch (GST_EVENT_TYPE (event)) {
928 case GST_EVENT_CUSTOM_UPSTREAM:
930 if (gst_video_event_is_force_key_unit (event)) {
931 GstClockTime running_time;
932 gboolean all_headers;
935 if (gst_video_event_parse_upstream_force_key_unit (event,
936 &running_time, &all_headers, &count)) {
937 ForcedKeyUnitEvent *fevt;
939 GST_OBJECT_LOCK (encoder);
940 fevt = forced_key_unit_event_new (running_time, all_headers, count);
941 encoder->priv->force_key_unit =
942 g_list_append (encoder->priv->force_key_unit, fevt);
943 GST_OBJECT_UNLOCK (encoder);
945 GST_DEBUG_OBJECT (encoder,
946 "force-key-unit event: running-time %" GST_TIME_FORMAT
947 ", all_headers %d, count %u",
948 GST_TIME_ARGS (running_time), all_headers, count);
950 gst_event_unref (event);
962 gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
969 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
971 GstVideoEncoder *encoder;
972 GstVideoEncoderClass *klass;
973 gboolean ret = FALSE;
975 encoder = GST_VIDEO_ENCODER (parent);
976 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
978 GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
980 if (klass->src_event)
981 ret = klass->src_event (encoder, event);
987 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
989 GstVideoEncoderPrivate *priv;
990 GstVideoEncoder *enc;
993 enc = GST_VIDEO_ENCODER (parent);
996 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
998 switch (GST_QUERY_TYPE (query)) {
999 case GST_QUERY_CONVERT:
1001 GstFormat src_fmt, dest_fmt;
1002 gint64 src_val, dest_val;
1004 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1006 gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1007 src_val, &dest_fmt, &dest_val);
1010 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1013 case GST_QUERY_LATENCY:
1016 GstClockTime min_latency, max_latency;
1018 res = gst_pad_peer_query (enc->sinkpad, query);
1020 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1021 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1022 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1023 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1025 GST_OBJECT_LOCK (enc);
1026 min_latency += priv->min_latency;
1027 if (max_latency != GST_CLOCK_TIME_NONE) {
1028 max_latency += priv->max_latency;
1030 GST_OBJECT_UNLOCK (enc);
1032 gst_query_set_latency (query, live, min_latency, max_latency);
1037 res = gst_pad_query_default (pad, parent, query);
1042 GST_DEBUG_OBJECT (enc, "query failed");
1046 static GstVideoCodecFrame *
1047 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1048 GstClockTime timestamp, GstClockTime duration)
1050 GstVideoEncoderPrivate *priv = encoder->priv;
1051 GstVideoCodecFrame *frame;
1053 frame = g_slice_new0 (GstVideoCodecFrame);
1055 frame->ref_count = 1;
1057 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1058 frame->system_frame_number = priv->system_frame_number;
1059 priv->system_frame_number++;
1061 frame->presentation_frame_number = priv->presentation_frame_number;
1062 priv->presentation_frame_number++;
1063 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1065 frame->events = priv->current_frame_events;
1066 priv->current_frame_events = NULL;
1067 frame->input_buffer = buf;
1068 frame->pts = timestamp;
1069 frame->duration = duration;
1075 static GstFlowReturn
1076 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1078 GstVideoEncoder *encoder;
1079 GstVideoEncoderPrivate *priv;
1080 GstVideoEncoderClass *klass;
1081 GstVideoCodecFrame *frame;
1082 GstFlowReturn ret = GST_FLOW_OK;
1083 guint64 start, stop = GST_CLOCK_TIME_NONE, cstart, cstop;
1085 encoder = GST_VIDEO_ENCODER (parent);
1086 priv = encoder->priv;
1087 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1089 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1091 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1093 start = GST_BUFFER_TIMESTAMP (buf);
1094 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buf)))
1095 stop = start + GST_BUFFER_DURATION (buf);
1097 GST_LOG_OBJECT (encoder,
1098 "received buffer of size %d with ts %" GST_TIME_FORMAT
1099 ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
1100 GST_TIME_ARGS (start), GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1107 /* Drop buffers outside of segment */
1108 if (!gst_segment_clip (&encoder->output_segment,
1109 GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1110 GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1111 gst_buffer_unref (buf);
1115 frame = gst_video_encoder_new_frame (encoder, buf, cstart, cstop - cstart);
1117 GST_OBJECT_LOCK (encoder);
1118 if (priv->force_key_unit) {
1119 ForcedKeyUnitEvent *fevt = NULL;
1120 GstClockTime running_time;
1124 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1125 GST_BUFFER_TIMESTAMP (buf));
1127 for (l = priv->force_key_unit; l; l = l->next) {
1128 ForcedKeyUnitEvent *tmp = l->data;
1130 /* Skip pending keyunits */
1134 /* Simple case, keyunit ASAP */
1135 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1140 /* Event for before this frame */
1141 if (tmp->running_time <= running_time) {
1148 GST_DEBUG_OBJECT (encoder,
1149 "Forcing a key unit at running time %" GST_TIME_FORMAT,
1150 GST_TIME_ARGS (running_time));
1151 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1152 if (fevt->all_headers)
1153 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1154 fevt->pending = TRUE;
1157 GST_OBJECT_UNLOCK (encoder);
1159 priv->frames = g_list_append (priv->frames, frame);
1161 /* new data, more finish needed */
1162 priv->drained = FALSE;
1164 GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1165 frame->presentation_frame_number);
1167 ret = klass->handle_frame (encoder, frame);
1170 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1175 static GstStateChangeReturn
1176 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1178 GstVideoEncoder *encoder;
1179 GstVideoEncoderClass *encoder_class;
1180 GstStateChangeReturn ret;
1182 encoder = GST_VIDEO_ENCODER (element);
1183 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1185 switch (transition) {
1186 case GST_STATE_CHANGE_NULL_TO_READY:
1187 /* open device/library if needed */
1188 if (encoder_class->open && !encoder_class->open (encoder))
1191 case GST_STATE_CHANGE_READY_TO_PAUSED:
1192 /* Initialize device/library if needed */
1193 if (encoder_class->start && !encoder_class->start (encoder))
1200 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1202 switch (transition) {
1203 case GST_STATE_CHANGE_PAUSED_TO_READY:
1204 gst_video_encoder_reset (encoder);
1205 if (encoder_class->stop && !encoder_class->stop (encoder))
1208 case GST_STATE_CHANGE_READY_TO_NULL:
1209 /* close device/library if needed */
1210 if (encoder_class->close && !encoder_class->close (encoder))
1223 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1224 ("Failed to open encoder"));
1225 return GST_STATE_CHANGE_FAILURE;
1230 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1231 ("Failed to start encoder"));
1232 return GST_STATE_CHANGE_FAILURE;
1237 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1238 ("Failed to stop encoder"));
1239 return GST_STATE_CHANGE_FAILURE;
1244 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1245 ("Failed to close encoder"));
1246 return GST_STATE_CHANGE_FAILURE;
1251 gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
1254 GstVideoCodecState *state = encoder->priv->output_state;
1255 GstVideoInfo *info = &state->info;
1257 g_return_val_if_fail (state->caps != NULL, FALSE);
1259 if (encoder->priv->output_state_changed) {
1260 state->caps = gst_caps_make_writable (state->caps);
1263 gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1264 "height", G_TYPE_INT, info->height,
1265 "pixel-aspect-ratio", GST_TYPE_FRACTION,
1266 info->par_n, info->par_d, NULL);
1267 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1268 /* variable fps with a max-framerate */
1269 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1270 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1272 /* no variable fps or no max-framerate */
1273 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1274 info->fps_n, info->fps_d, NULL);
1276 if (state->codec_data)
1277 gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1278 state->codec_data, NULL);
1279 encoder->priv->output_state_changed = FALSE;
1282 ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1288 * gst_video_encoder_finish_frame:
1289 * @encoder: a #GstVideoEncoder
1290 * @frame: (transfer full): an encoded #GstVideoCodecFrame
1292 * @frame must have a valid encoded data buffer, whose metadata fields
1293 * are then appropriately set according to frame data or no buffer at
1294 * all if the frame should be dropped.
1295 * It is subsequently pushed downstream or provided to @pre_push.
1296 * In any case, the frame is considered finished and released.
1298 * Returns: a #GstFlowReturn resulting from sending data downstream
1303 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1304 GstVideoCodecFrame * frame)
1306 GstVideoEncoderPrivate *priv = encoder->priv;
1307 GstFlowReturn ret = GST_FLOW_OK;
1308 GstVideoEncoderClass *encoder_class;
1310 gboolean send_headers = FALSE;
1311 gboolean discont = (frame->presentation_frame_number == 0);
1313 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1315 GST_LOG_OBJECT (encoder,
1316 "finish frame fpn %d", frame->presentation_frame_number);
1318 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1320 if (G_UNLIKELY (priv->output_state_changed))
1321 gst_video_encoder_set_src_caps (encoder);
1323 if (G_UNLIKELY (priv->output_state == NULL))
1324 goto no_output_state;
1326 /* Push all pending events that arrived before this frame */
1327 for (l = priv->frames; l; l = l->next) {
1328 GstVideoCodecFrame *tmp = l->data;
1333 for (k = g_list_last (tmp->events); k; k = k->prev)
1334 gst_video_encoder_push_event (encoder, k->data);
1335 g_list_free (tmp->events);
1343 /* no buffer data means this frame is skipped/dropped */
1344 if (!frame->output_buffer) {
1345 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1346 GST_TIME_ARGS (frame->pts));
1350 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1351 GstClockTime stream_time, running_time;
1353 ForcedKeyUnitEvent *fevt = NULL;
1357 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1360 GST_OBJECT_LOCK (encoder);
1361 for (l = priv->force_key_unit; l; l = l->next) {
1362 ForcedKeyUnitEvent *tmp = l->data;
1364 /* Skip non-pending keyunits */
1368 /* Simple case, keyunit ASAP */
1369 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1374 /* Event for before this frame */
1375 if (tmp->running_time <= running_time) {
1382 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1384 GST_OBJECT_UNLOCK (encoder);
1388 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1391 ev = gst_video_event_new_downstream_force_key_unit
1392 (frame->pts, stream_time, running_time,
1393 fevt->all_headers, fevt->count);
1395 gst_video_encoder_push_event (encoder, ev);
1397 if (fevt->all_headers)
1398 send_headers = TRUE;
1400 GST_DEBUG_OBJECT (encoder,
1401 "Forced key unit: running-time %" GST_TIME_FORMAT
1402 ", all_headers %d, count %u",
1403 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1404 forced_key_unit_event_free (fevt);
1408 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1409 priv->distance_from_sync = 0;
1410 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1411 /* For keyframes, DTS = PTS */
1412 frame->dts = frame->pts;
1414 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1417 frame->distance_from_sync = priv->distance_from_sync;
1418 priv->distance_from_sync++;
1420 GST_BUFFER_TIMESTAMP (frame->output_buffer) = frame->pts;
1421 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1423 /* update rate estimate */
1424 priv->bytes += gst_buffer_get_size (frame->output_buffer);
1425 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1426 priv->time += frame->duration;
1428 /* better none than nothing valid */
1429 priv->time = GST_CLOCK_TIME_NONE;
1432 if (G_UNLIKELY (send_headers || priv->new_headers)) {
1433 GList *tmp, *copy = NULL;
1435 GST_DEBUG_OBJECT (encoder, "Sending headers");
1437 /* First make all buffers metadata-writable */
1438 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1439 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1441 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1443 g_list_free (priv->headers);
1444 priv->headers = copy;
1446 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1447 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1449 gst_buffer_ref (tmpbuf);
1450 priv->bytes += gst_buffer_get_size (tmpbuf);
1451 if (G_UNLIKELY (discont)) {
1452 GST_LOG_OBJECT (encoder, "marking discont");
1453 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1457 gst_pad_push (encoder->srcpad, tmpbuf);
1459 priv->new_headers = FALSE;
1462 if (G_UNLIKELY (discont)) {
1463 GST_LOG_OBJECT (encoder, "marking discont");
1464 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1467 if (encoder_class->pre_push)
1468 ret = encoder_class->pre_push (encoder, frame);
1470 if (ret == GST_FLOW_OK)
1471 ret = gst_pad_push (encoder->srcpad, frame->output_buffer);
1473 frame->output_buffer = NULL;
1477 priv->frames = g_list_remove (priv->frames, frame);
1479 gst_video_codec_frame_unref (frame);
1481 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1488 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1489 GST_ERROR_OBJECT (encoder, "Output state was not configured");
1490 return GST_FLOW_ERROR;
1495 * gst_video_encoder_get_output_state:
1496 * @encoder: a #GstVideoEncoder
1498 * Get the current #GstVideoCodecState
1500 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1504 GstVideoCodecState *
1505 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1507 GstVideoCodecState *state;
1509 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1510 state = gst_video_codec_state_ref (encoder->priv->output_state);
1511 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1517 * gst_video_encoder_set_output_state:
1518 * @encoder: a #GstVideoEncoder
1519 * @caps: (transfer full): the #GstCaps to use for the output
1520 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1522 * Creates a new #GstVideoCodecState with the specified caps as the output state
1524 * Any previously set output state on @decoder will be replaced by the newly
1527 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1528 * framerate, codec-data, .... Those should be specified instead in the returned
1529 * #GstVideoCodecState.
1531 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1532 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1535 * If the subclass wishes to override some fields from the output state (like
1536 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1538 * The new output state will only take effect (set on pads and buffers) starting
1539 * from the next call to #gst_video_encoder_finish_frame().
1541 * Returns: (transfer full): the newly configured output state.
1545 GstVideoCodecState *
1546 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1547 GstVideoCodecState * reference)
1549 GstVideoEncoderPrivate *priv = encoder->priv;
1550 GstVideoCodecState *state;
1552 g_return_val_if_fail (caps != NULL, NULL);
1554 state = _new_output_state (caps, reference);
1556 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1557 if (priv->output_state)
1558 gst_video_codec_state_unref (priv->output_state);
1559 priv->output_state = gst_video_codec_state_ref (state);
1561 priv->output_state_changed = TRUE;
1562 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1568 * gst_video_encoder_set_latency:
1569 * @encoder: a #GstVideoEncoder
1570 * @min_latency: minimum latency
1571 * @max_latency: maximum latency
1573 * Informs baseclass of encoding latency.
1578 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1579 GstClockTime min_latency, GstClockTime max_latency)
1581 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1582 g_return_if_fail (max_latency >= min_latency);
1584 GST_OBJECT_LOCK (encoder);
1585 encoder->priv->min_latency = min_latency;
1586 encoder->priv->max_latency = max_latency;
1587 GST_OBJECT_UNLOCK (encoder);
1589 gst_element_post_message (GST_ELEMENT_CAST (encoder),
1590 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1594 * gst_video_encoder_get_latency:
1595 * @encoder: a #GstVideoEncoder
1596 * @min_latency: (out) (allow-none): the configured minimum latency
1597 * @max_latency: (out) (allow-none): the configured maximum latency
1599 * Returns the configured encoding latency.
1604 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1605 GstClockTime * min_latency, GstClockTime * max_latency)
1607 GST_OBJECT_LOCK (encoder);
1609 *min_latency = encoder->priv->min_latency;
1611 *max_latency = encoder->priv->max_latency;
1612 GST_OBJECT_UNLOCK (encoder);
1616 * gst_video_encoder_get_oldest_frame:
1617 * @encoder: a #GstVideoEncoder
1619 * Get the oldest unfinished pending #GstVideoCodecFrame
1621 * Returns: oldest unfinished pending #GstVideoCodecFrame
1625 GstVideoCodecFrame *
1626 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1630 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1631 g = encoder->priv->frames;
1632 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1636 return (GstVideoCodecFrame *) (g->data);
1640 * gst_video_encoder_get_frame:
1641 * @encoder: a #GstVideoEnccoder
1642 * @frame_number: system_frame_number of a frame
1644 * Get a pending unfinished #GstVideoCodecFrame
1646 * Returns: (transfer none): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1650 GstVideoCodecFrame *
1651 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1654 GstVideoCodecFrame *frame = NULL;
1656 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
1658 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1659 for (g = encoder->priv->frames; g; g = g->next) {
1660 GstVideoCodecFrame *tmp = g->data;
1662 if (tmp->system_frame_number == frame_number) {
1667 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);