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 #include "gstvideoencoder.h"
119 #include "gstvideoutils.h"
121 #include <gst/video/gstvideometa.h>
122 #include <gst/video/gstvideopool.h>
126 GST_DEBUG_CATEGORY (videoencoder_debug);
127 #define GST_CAT_DEFAULT videoencoder_debug
129 #define GST_VIDEO_ENCODER_GET_PRIVATE(obj) \
130 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
131 GstVideoEncoderPrivate))
133 struct _GstVideoEncoderPrivate
135 guint64 presentation_frame_number;
136 int distance_from_sync;
138 /* 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 guint32 system_frame_number;
155 GList *frames; /* Protected with OBJECT_LOCK */
156 GstVideoCodecState *input_state;
157 GstVideoCodecState *output_state;
158 gboolean output_state_changed;
163 GstAllocator *allocator;
164 GstAllocationParams params;
167 gboolean tags_changed;
170 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
171 struct _ForcedKeyUnitEvent
173 GstClockTime running_time;
174 gboolean pending; /* TRUE if this was requested already */
175 gboolean all_headers;
180 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
182 g_slice_free (ForcedKeyUnitEvent, evt);
185 static ForcedKeyUnitEvent *
186 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
189 ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
191 evt->running_time = running_time;
192 evt->all_headers = all_headers;
198 static GstElementClass *parent_class = NULL;
199 static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
200 static void gst_video_encoder_init (GstVideoEncoder * enc,
201 GstVideoEncoderClass * klass);
203 static void gst_video_encoder_finalize (GObject * object);
205 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
207 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
209 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
211 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
213 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
215 static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
216 element, GstStateChange transition);
217 static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
219 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
221 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
222 encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
223 GstClockTime duration);
225 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
227 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
229 static gboolean gst_video_encoder_decide_allocation_default (GstVideoEncoder *
230 encoder, GstQuery * query);
231 static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
232 encoder, GstQuery * query);
233 static gboolean gst_video_encoder_negotiate_default (GstVideoEncoder * encoder);
235 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
236 * method to get to the padtemplates */
238 gst_video_encoder_get_type (void)
240 static volatile gsize type = 0;
242 if (g_once_init_enter (&type)) {
244 static const GTypeInfo info = {
245 sizeof (GstVideoEncoderClass),
248 (GClassInitFunc) gst_video_encoder_class_init,
251 sizeof (GstVideoEncoder),
253 (GInstanceInitFunc) gst_video_encoder_init,
255 const GInterfaceInfo preset_interface_info = {
256 NULL, /* interface_init */
257 NULL, /* interface_finalize */
258 NULL /* interface_data */
261 _type = g_type_register_static (GST_TYPE_ELEMENT,
262 "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
263 g_type_add_interface_static (_type, GST_TYPE_PRESET,
264 &preset_interface_info);
265 g_once_init_leave (&type, _type);
271 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
273 GObjectClass *gobject_class;
274 GstElementClass *gstelement_class;
276 gobject_class = G_OBJECT_CLASS (klass);
277 gstelement_class = GST_ELEMENT_CLASS (klass);
279 GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
280 "Base Video Encoder");
282 parent_class = g_type_class_peek_parent (klass);
284 g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
286 gobject_class->finalize = gst_video_encoder_finalize;
288 gstelement_class->change_state =
289 GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
291 klass->sink_event = gst_video_encoder_sink_event_default;
292 klass->src_event = gst_video_encoder_src_event_default;
293 klass->propose_allocation = gst_video_encoder_propose_allocation_default;
294 klass->decide_allocation = gst_video_encoder_decide_allocation_default;
295 klass->negotiate = gst_video_encoder_negotiate_default;
299 gst_video_encoder_reset (GstVideoEncoder * encoder)
301 GstVideoEncoderPrivate *priv = encoder->priv;
304 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
306 priv->presentation_frame_number = 0;
307 priv->distance_from_sync = 0;
309 g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
311 g_list_free (priv->force_key_unit);
312 priv->force_key_unit = NULL;
314 priv->drained = TRUE;
315 priv->min_latency = 0;
316 priv->max_latency = 0;
318 g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
319 g_list_free (priv->headers);
320 priv->headers = NULL;
321 priv->new_headers = FALSE;
323 g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
324 g_list_free (priv->current_frame_events);
325 priv->current_frame_events = NULL;
327 for (g = priv->frames; g; g = g->next) {
328 gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
330 g_list_free (priv->frames);
336 if (priv->input_state)
337 gst_video_codec_state_unref (priv->input_state);
338 priv->input_state = NULL;
339 if (priv->output_state)
340 gst_video_codec_state_unref (priv->output_state);
341 priv->output_state = NULL;
344 gst_tag_list_unref (priv->tags);
346 priv->tags_changed = FALSE;
348 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
352 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
354 GstVideoEncoderPrivate *priv;
355 GstPadTemplate *pad_template;
358 GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
360 priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
363 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
364 g_return_if_fail (pad_template != NULL);
366 encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
368 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
369 gst_pad_set_event_function (pad,
370 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
371 gst_pad_set_query_function (pad,
372 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
373 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
376 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
377 g_return_if_fail (pad_template != NULL);
379 encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
381 gst_pad_set_query_function (pad,
382 GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
383 gst_pad_set_event_function (pad,
384 GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
385 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
387 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
388 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
390 g_rec_mutex_init (&encoder->stream_lock);
392 priv->at_eos = FALSE;
393 priv->headers = NULL;
394 priv->new_headers = FALSE;
396 gst_video_encoder_reset (encoder);
400 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
401 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
404 gboolean res = FALSE;
406 g_return_val_if_fail (dest_format != NULL, FALSE);
407 g_return_val_if_fail (dest_value != NULL, FALSE);
409 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
412 *dest_value = src_value;
416 if (bytes <= 0 || time <= 0) {
417 GST_DEBUG ("not enough metadata yet to convert");
421 switch (src_format) {
422 case GST_FORMAT_BYTES:
423 switch (*dest_format) {
424 case GST_FORMAT_TIME:
425 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
432 case GST_FORMAT_TIME:
433 switch (*dest_format) {
434 case GST_FORMAT_BYTES:
435 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
443 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
453 * gst_video_encoder_set_headers:
454 * @encoder: a #GstVideoEncoder
455 * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
457 * Set the codec headers to be sent downstream whenever requested.
460 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
462 GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
464 GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
465 if (video_encoder->priv->headers) {
466 g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
468 g_list_free (video_encoder->priv->headers);
470 video_encoder->priv->headers = headers;
471 video_encoder->priv->new_headers = TRUE;
473 GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
477 gst_video_encoder_drain (GstVideoEncoder * enc)
479 GstVideoEncoderPrivate *priv;
480 GstVideoEncoderClass *enc_class;
483 enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
486 GST_DEBUG_OBJECT (enc, "draining");
489 GST_DEBUG_OBJECT (enc, "already drained");
493 if (enc_class->reset) {
494 GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
495 ret = enc_class->reset (enc, TRUE);
497 /* everything should be away now */
499 /* not fatal/impossible though if subclass/enc eats stuff */
500 g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
501 g_list_free (priv->frames);
508 static GstVideoCodecState *
509 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
511 GstVideoCodecState *state;
513 state = g_slice_new0 (GstVideoCodecState);
514 state->ref_count = 1;
515 gst_video_info_init (&state->info);
516 gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
521 GstVideoInfo *tgt, *ref;
524 ref = &reference->info;
526 /* Copy over extra fields from reference state */
527 tgt->interlace_mode = ref->interlace_mode;
528 tgt->flags = ref->flags;
529 tgt->width = ref->width;
530 tgt->height = ref->height;
531 tgt->chroma_site = ref->chroma_site;
532 tgt->colorimetry = ref->colorimetry;
533 tgt->par_n = ref->par_n;
534 tgt->par_d = ref->par_d;
535 tgt->fps_n = ref->fps_n;
536 tgt->fps_d = ref->fps_d;
542 static GstVideoCodecState *
543 _new_input_state (GstCaps * caps)
545 GstVideoCodecState *state;
547 state = g_slice_new0 (GstVideoCodecState);
548 state->ref_count = 1;
549 gst_video_info_init (&state->info);
550 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
552 state->caps = gst_caps_ref (caps);
558 g_slice_free (GstVideoCodecState, state);
564 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
566 GstVideoEncoderClass *encoder_class;
567 GstVideoCodecState *state;
569 gboolean samecaps = FALSE;
571 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
573 /* subclass should do something here ... */
574 g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
576 GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
578 state = _new_input_state (caps);
579 if (G_UNLIKELY (!state))
582 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
584 if (encoder->priv->input_state)
586 gst_video_info_is_equal (&state->info,
587 &encoder->priv->input_state->info);
590 /* arrange draining pending frames */
591 gst_video_encoder_drain (encoder);
593 /* and subclass should be ready to configure format at any time around */
594 ret = encoder_class->set_format (encoder, state);
596 if (encoder->priv->input_state)
597 gst_video_codec_state_unref (encoder->priv->input_state);
598 encoder->priv->input_state = state;
600 gst_video_codec_state_unref (state);
602 /* no need to stir things up */
603 GST_DEBUG_OBJECT (encoder,
604 "new video format identical to configured format");
605 gst_video_codec_state_unref (state);
609 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
612 GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
618 GST_WARNING_OBJECT (encoder, "Failed to parse caps");
624 * gst_video_encoder_proxy_getcaps:
625 * @enc: a #GstVideoEncoder
626 * @caps: initial caps
627 * @filter: filter caps
629 * Returns caps that express @caps (or sink template caps if @caps == NULL)
630 * restricted to resolution/format/... combinations supported by downstream
631 * elements (e.g. muxers).
633 * Returns: a #GstCaps owned by caller
636 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
641 GstCaps *fcaps, *filter_caps;
644 /* Allow downstream to specify width/height/framerate/PAR constraints
645 * and forward them upstream for video converters to handle
648 caps ? gst_caps_ref (caps) :
649 gst_pad_get_pad_template_caps (encoder->sinkpad);
650 allowed = gst_pad_get_allowed_caps (encoder->srcpad);
652 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
657 GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
658 GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
660 filter_caps = gst_caps_new_empty ();
662 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
664 gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
666 for (j = 0; j < gst_caps_get_size (allowed); j++) {
667 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
671 s = gst_structure_new_id_empty (q_name);
672 if ((val = gst_structure_get_value (allowed_s, "width")))
673 gst_structure_set_value (s, "width", val);
674 if ((val = gst_structure_get_value (allowed_s, "height")))
675 gst_structure_set_value (s, "height", val);
676 if ((val = gst_structure_get_value (allowed_s, "framerate")))
677 gst_structure_set_value (s, "framerate", val);
678 if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
679 gst_structure_set_value (s, "pixel-aspect-ratio", val);
681 filter_caps = gst_caps_merge_structure (filter_caps, s);
685 fcaps = gst_caps_intersect (filter_caps, templ_caps);
686 gst_caps_unref (filter_caps);
687 gst_caps_unref (templ_caps);
690 GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
691 filter_caps = gst_caps_intersect (fcaps, filter);
692 gst_caps_unref (fcaps);
697 gst_caps_replace (&allowed, NULL);
699 GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
705 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
707 GstVideoEncoderClass *klass;
710 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
713 caps = klass->getcaps (encoder, filter);
715 caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
717 GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
723 gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
726 GstAllocator *allocator = NULL;
727 GstAllocationParams params;
728 gboolean update_allocator;
730 /* we got configuration from our peer or the decide_allocation method,
732 if (gst_query_get_n_allocation_params (query) > 0) {
733 /* try the allocator */
734 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
735 update_allocator = TRUE;
738 gst_allocation_params_init (¶ms);
739 update_allocator = FALSE;
742 if (update_allocator)
743 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
745 gst_query_add_allocation_param (query, allocator, ¶ms);
747 gst_object_unref (allocator);
753 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
761 gst_query_parse_allocation (query, &caps, NULL);
766 if (!gst_video_info_from_caps (&info, caps))
769 size = GST_VIDEO_INFO_SIZE (&info);
771 if (gst_query_get_n_allocation_pools (query) == 0) {
772 GstStructure *structure;
773 GstAllocator *allocator = NULL;
774 GstAllocationParams params = { 0, 15, 0, 0 };
776 if (gst_query_get_n_allocation_params (query) > 0)
777 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
779 gst_query_add_allocation_param (query, allocator, ¶ms);
781 pool = gst_video_buffer_pool_new ();
783 structure = gst_buffer_pool_get_config (pool);
784 gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
785 gst_buffer_pool_config_set_allocator (structure, allocator, ¶ms);
788 gst_object_unref (allocator);
790 if (!gst_buffer_pool_set_config (pool, structure))
793 gst_query_add_allocation_pool (query, pool, size, 0, 0);
794 gst_object_unref (pool);
795 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
803 GST_ERROR_OBJECT (encoder, "failed to set config");
804 gst_object_unref (pool);
810 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
813 GstVideoEncoder *encoder;
814 gboolean res = FALSE;
816 encoder = GST_VIDEO_ENCODER (parent);
818 switch (GST_QUERY_TYPE (query)) {
821 GstCaps *filter, *caps;
823 gst_query_parse_caps (query, &filter);
824 caps = gst_video_encoder_sink_getcaps (encoder, filter);
825 gst_query_set_caps_result (query, caps);
826 gst_caps_unref (caps);
830 case GST_QUERY_ALLOCATION:
832 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
834 if (klass->propose_allocation)
835 res = klass->propose_allocation (encoder, query);
839 res = gst_pad_query_default (pad, parent, query);
846 gst_video_encoder_finalize (GObject * object)
848 GstVideoEncoder *encoder;
850 GST_DEBUG_OBJECT (object, "finalize");
852 encoder = GST_VIDEO_ENCODER (object);
853 if (encoder->priv->headers) {
854 g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
855 g_list_free (encoder->priv->headers);
857 g_rec_mutex_clear (&encoder->stream_lock);
859 if (encoder->priv->allocator) {
860 gst_object_unref (encoder->priv->allocator);
861 encoder->priv->allocator = NULL;
864 G_OBJECT_CLASS (parent_class)->finalize (object);
868 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
870 switch (GST_EVENT_TYPE (event)) {
871 case GST_EVENT_SEGMENT:
875 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
877 gst_event_copy_segment (event, &segment);
879 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
881 if (segment.format != GST_FORMAT_TIME) {
882 GST_DEBUG_OBJECT (encoder, "received non TIME segment");
883 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
887 encoder->output_segment = segment;
888 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
895 return gst_pad_push_event (encoder->srcpad, event);
899 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
902 GstVideoEncoderClass *encoder_class;
903 gboolean ret = FALSE;
905 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
907 switch (GST_EVENT_TYPE (event)) {
912 gst_event_parse_caps (event, &caps);
914 encoder->priv->do_caps = TRUE;
915 gst_event_unref (event);
921 GstFlowReturn flow_ret;
923 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
924 encoder->priv->at_eos = TRUE;
926 if (encoder_class->finish) {
927 flow_ret = encoder_class->finish (encoder);
929 flow_ret = GST_FLOW_OK;
932 ret = (flow_ret == GST_FLOW_OK);
933 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
936 case GST_EVENT_SEGMENT:
940 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
942 gst_event_copy_segment (event, &segment);
944 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
946 if (segment.format != GST_FORMAT_TIME) {
947 GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
948 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
952 encoder->priv->at_eos = FALSE;
954 encoder->input_segment = segment;
956 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
959 case GST_EVENT_CUSTOM_DOWNSTREAM:
961 if (gst_video_event_is_force_key_unit (event)) {
962 GstClockTime running_time;
963 gboolean all_headers;
966 if (gst_video_event_parse_downstream_force_key_unit (event,
967 NULL, NULL, &running_time, &all_headers, &count)) {
968 ForcedKeyUnitEvent *fevt;
970 GST_OBJECT_LOCK (encoder);
971 fevt = forced_key_unit_event_new (running_time, all_headers, count);
972 encoder->priv->force_key_unit =
973 g_list_append (encoder->priv->force_key_unit, fevt);
974 GST_OBJECT_UNLOCK (encoder);
976 GST_DEBUG_OBJECT (encoder,
977 "force-key-unit event: running-time %" GST_TIME_FORMAT
978 ", all_headers %d, count %u",
979 GST_TIME_ARGS (running_time), all_headers, count);
981 gst_event_unref (event);
991 gst_event_parse_tag (event, &tags);
993 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
994 tags = gst_tag_list_copy (tags);
996 /* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
997 gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
998 gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
999 gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1000 gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
1001 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1002 gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
1003 gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
1004 gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
1005 gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
1006 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
1007 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
1009 gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
1010 gst_tag_list_unref (tags);
1011 gst_event_unref (event);
1021 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1022 * For EOS this is required because no buffer or serialized event
1023 * will come after EOS and nothing could trigger another
1024 * _finish_frame() call. *
1025 * If the subclass handles sending of EOS manually it can simply
1026 * not chain up to the parent class' event handler
1028 * For FLUSH_STOP this is required because it is expected
1029 * to be forwarded immediately and no buffers are queued anyway.
1032 if (!GST_EVENT_IS_SERIALIZED (event)
1033 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1034 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1035 ret = gst_video_encoder_push_event (encoder, event);
1037 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1038 encoder->priv->current_frame_events =
1039 g_list_prepend (encoder->priv->current_frame_events, event);
1040 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1049 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
1052 GstVideoEncoder *enc;
1053 GstVideoEncoderClass *klass;
1054 gboolean ret = TRUE;
1056 enc = GST_VIDEO_ENCODER (parent);
1057 klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
1059 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1060 GST_EVENT_TYPE_NAME (event));
1062 if (klass->sink_event)
1063 ret = klass->sink_event (enc, event);
1069 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
1072 gboolean ret = FALSE;
1074 switch (GST_EVENT_TYPE (event)) {
1075 case GST_EVENT_CUSTOM_UPSTREAM:
1077 if (gst_video_event_is_force_key_unit (event)) {
1078 GstClockTime running_time;
1079 gboolean all_headers;
1082 if (gst_video_event_parse_upstream_force_key_unit (event,
1083 &running_time, &all_headers, &count)) {
1084 ForcedKeyUnitEvent *fevt;
1086 GST_OBJECT_LOCK (encoder);
1087 fevt = forced_key_unit_event_new (running_time, all_headers, count);
1088 encoder->priv->force_key_unit =
1089 g_list_append (encoder->priv->force_key_unit, fevt);
1090 GST_OBJECT_UNLOCK (encoder);
1092 GST_DEBUG_OBJECT (encoder,
1093 "force-key-unit event: running-time %" GST_TIME_FORMAT
1094 ", all_headers %d, count %u",
1095 GST_TIME_ARGS (running_time), all_headers, count);
1097 gst_event_unref (event);
1109 gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
1116 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1118 GstVideoEncoder *encoder;
1119 GstVideoEncoderClass *klass;
1120 gboolean ret = FALSE;
1122 encoder = GST_VIDEO_ENCODER (parent);
1123 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1125 GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1127 if (klass->src_event)
1128 ret = klass->src_event (encoder, event);
1134 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1136 GstVideoEncoderPrivate *priv;
1137 GstVideoEncoder *enc;
1140 enc = GST_VIDEO_ENCODER (parent);
1143 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1145 switch (GST_QUERY_TYPE (query)) {
1146 case GST_QUERY_CONVERT:
1148 GstFormat src_fmt, dest_fmt;
1149 gint64 src_val, dest_val;
1151 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1153 gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1154 src_val, &dest_fmt, &dest_val);
1157 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1160 case GST_QUERY_LATENCY:
1163 GstClockTime min_latency, max_latency;
1165 res = gst_pad_peer_query (enc->sinkpad, query);
1167 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1168 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1169 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1170 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1172 GST_OBJECT_LOCK (enc);
1173 min_latency += priv->min_latency;
1174 if (enc->priv->max_latency == GST_CLOCK_TIME_NONE) {
1175 max_latency = GST_CLOCK_TIME_NONE;
1176 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1177 max_latency += enc->priv->max_latency;
1179 GST_OBJECT_UNLOCK (enc);
1181 gst_query_set_latency (query, live, min_latency, max_latency);
1186 res = gst_pad_query_default (pad, parent, query);
1191 GST_DEBUG_OBJECT (enc, "query failed");
1195 static GstVideoCodecFrame *
1196 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1197 GstClockTime pts, GstClockTime dts, GstClockTime duration)
1199 GstVideoEncoderPrivate *priv = encoder->priv;
1200 GstVideoCodecFrame *frame;
1202 frame = g_slice_new0 (GstVideoCodecFrame);
1204 frame->ref_count = 1;
1206 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1207 frame->system_frame_number = priv->system_frame_number;
1208 priv->system_frame_number++;
1210 frame->presentation_frame_number = priv->presentation_frame_number;
1211 priv->presentation_frame_number++;
1212 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1214 frame->events = priv->current_frame_events;
1215 priv->current_frame_events = NULL;
1216 frame->input_buffer = buf;
1219 frame->duration = duration;
1225 static GstFlowReturn
1226 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1228 GstVideoEncoder *encoder;
1229 GstVideoEncoderPrivate *priv;
1230 GstVideoEncoderClass *klass;
1231 GstVideoCodecFrame *frame;
1232 GstClockTime pts, dts, duration;
1233 GstFlowReturn ret = GST_FLOW_OK;
1234 guint64 start, stop, cstart, cstop;
1236 encoder = GST_VIDEO_ENCODER (parent);
1237 priv = encoder->priv;
1238 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1240 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1242 if (G_UNLIKELY (encoder->priv->do_caps)) {
1243 GstCaps *caps = gst_pad_get_current_caps (encoder->sinkpad);
1245 goto not_negotiated;
1246 if (!gst_video_encoder_setcaps (encoder, caps)) {
1247 gst_caps_unref (caps);
1248 goto not_negotiated;
1250 gst_caps_unref (caps);
1251 encoder->priv->do_caps = FALSE;
1254 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1256 pts = GST_BUFFER_PTS (buf);
1257 dts = GST_BUFFER_DTS (buf);
1258 duration = GST_BUFFER_DURATION (buf);
1260 GST_LOG_OBJECT (encoder,
1261 "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
1262 ", DTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1263 gst_buffer_get_size (buf), GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
1264 GST_TIME_ARGS (duration));
1272 if (GST_CLOCK_TIME_IS_VALID (duration))
1273 stop = start + duration;
1275 stop = GST_CLOCK_TIME_NONE;
1277 /* Drop buffers outside of segment */
1278 if (!gst_segment_clip (&encoder->input_segment,
1279 GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1280 GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1281 gst_buffer_unref (buf);
1286 gst_video_encoder_new_frame (encoder, buf, cstart, dts, cstop - cstart);
1288 GST_OBJECT_LOCK (encoder);
1289 if (priv->force_key_unit) {
1290 ForcedKeyUnitEvent *fevt = NULL;
1291 GstClockTime running_time;
1295 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1298 for (l = priv->force_key_unit; l; l = l->next) {
1299 ForcedKeyUnitEvent *tmp = l->data;
1301 /* Skip pending keyunits */
1305 /* Simple case, keyunit ASAP */
1306 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1311 /* Event for before this frame */
1312 if (tmp->running_time <= running_time) {
1319 GST_DEBUG_OBJECT (encoder,
1320 "Forcing a key unit at running time %" GST_TIME_FORMAT,
1321 GST_TIME_ARGS (running_time));
1322 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1323 if (fevt->all_headers)
1324 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1325 fevt->pending = TRUE;
1328 GST_OBJECT_UNLOCK (encoder);
1330 gst_video_codec_frame_ref (frame);
1331 priv->frames = g_list_append (priv->frames, frame);
1333 /* new data, more finish needed */
1334 priv->drained = FALSE;
1336 GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1337 frame->presentation_frame_number);
1339 ret = klass->handle_frame (encoder, frame);
1342 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1349 GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
1350 ("encoder not initialized"));
1351 gst_buffer_unref (buf);
1352 return GST_FLOW_NOT_NEGOTIATED;
1356 static GstStateChangeReturn
1357 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1359 GstVideoEncoder *encoder;
1360 GstVideoEncoderClass *encoder_class;
1361 GstStateChangeReturn ret;
1363 encoder = GST_VIDEO_ENCODER (element);
1364 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1366 switch (transition) {
1367 case GST_STATE_CHANGE_NULL_TO_READY:
1368 /* open device/library if needed */
1369 if (encoder_class->open && !encoder_class->open (encoder))
1372 case GST_STATE_CHANGE_READY_TO_PAUSED:
1373 /* Initialize device/library if needed */
1374 if (encoder_class->start && !encoder_class->start (encoder))
1381 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1383 switch (transition) {
1384 case GST_STATE_CHANGE_PAUSED_TO_READY:
1385 gst_video_encoder_reset (encoder);
1386 if (encoder_class->stop && !encoder_class->stop (encoder))
1389 case GST_STATE_CHANGE_READY_TO_NULL:
1390 /* close device/library if needed */
1391 if (encoder_class->close && !encoder_class->close (encoder))
1404 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1405 ("Failed to open encoder"));
1406 return GST_STATE_CHANGE_FAILURE;
1411 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1412 ("Failed to start encoder"));
1413 return GST_STATE_CHANGE_FAILURE;
1418 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1419 ("Failed to stop encoder"));
1420 return GST_STATE_CHANGE_FAILURE;
1425 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1426 ("Failed to close encoder"));
1427 return GST_STATE_CHANGE_FAILURE;
1432 gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
1434 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1435 GstAllocator *allocator;
1436 GstAllocationParams params;
1438 GstVideoCodecState *state = encoder->priv->output_state;
1439 GstVideoInfo *info = &state->info;
1440 GstQuery *query = NULL;
1442 g_return_val_if_fail (state->caps != NULL, FALSE);
1444 if (encoder->priv->output_state_changed) {
1445 state->caps = gst_caps_make_writable (state->caps);
1448 gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1449 "height", G_TYPE_INT, info->height,
1450 "pixel-aspect-ratio", GST_TYPE_FRACTION,
1451 info->par_n, info->par_d, NULL);
1452 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1453 /* variable fps with a max-framerate */
1454 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1455 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1457 /* no variable fps or no max-framerate */
1458 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1459 info->fps_n, info->fps_d, NULL);
1461 if (state->codec_data)
1462 gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1463 state->codec_data, NULL);
1464 encoder->priv->output_state_changed = FALSE;
1467 ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1471 query = gst_query_new_allocation (state->caps, TRUE);
1472 if (!gst_pad_peer_query (encoder->srcpad, query)) {
1473 GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
1476 g_assert (klass->decide_allocation != NULL);
1477 ret = klass->decide_allocation (encoder, query);
1479 GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
1483 goto no_decide_allocation;
1485 /* we got configuration from our peer or the decide_allocation method,
1487 if (gst_query_get_n_allocation_params (query) > 0) {
1488 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1491 gst_allocation_params_init (¶ms);
1494 if (encoder->priv->allocator)
1495 gst_object_unref (encoder->priv->allocator);
1496 encoder->priv->allocator = allocator;
1497 encoder->priv->params = params;
1501 gst_query_unref (query);
1506 no_decide_allocation:
1508 GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
1514 * gst_video_encoder_negotiate:
1515 * @encoder: a #GstVideoEncoder
1517 * Negotiate with downstream elements to currently configured #GstVideoCodecState.
1519 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
1522 gst_video_encoder_negotiate (GstVideoEncoder * encoder)
1524 GstVideoEncoderClass *klass;
1525 gboolean ret = TRUE;
1527 g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
1529 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1531 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1532 if (klass->negotiate)
1533 ret = klass->negotiate (encoder);
1534 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1540 * gst_video_encoder_allocate_output_buffer:
1541 * @encoder: a #GstVideoEncoder
1542 * @size: size of the buffer
1544 * Helper function that allocates a buffer to hold an encoded video frame
1545 * for @encoder's current #GstVideoCodecState.
1547 * Returns: (transfer full): allocated buffer
1550 gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
1554 g_return_val_if_fail (size > 0, NULL);
1556 GST_DEBUG ("alloc src buffer");
1558 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1559 if (G_UNLIKELY (encoder->priv->output_state_changed
1560 || (encoder->priv->output_state
1561 && gst_pad_check_reconfigure (encoder->srcpad))))
1562 gst_video_encoder_negotiate (encoder);
1565 gst_buffer_new_allocate (encoder->priv->allocator, size,
1566 &encoder->priv->params);
1568 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1574 * gst_video_encoder_allocate_output_frame:
1575 * @encoder: a #GstVideoEncoder
1576 * @frame: a #GstVideoCodecFrame
1577 * @size: size of the buffer
1579 * Helper function that allocates a buffer to hold an encoded video frame for @encoder's
1580 * current #GstVideoCodecState. Subclass should already have configured video
1581 * state and set src pad caps.
1583 * The buffer allocated here is owned by the frame and you should only
1584 * keep references to the frame, not the buffer.
1586 * Returns: %GST_FLOW_OK if an output buffer could be allocated
1589 gst_video_encoder_allocate_output_frame (GstVideoEncoder *
1590 encoder, GstVideoCodecFrame * frame, gsize size)
1592 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
1594 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1595 if (G_UNLIKELY (encoder->priv->output_state_changed
1596 || (encoder->priv->output_state
1597 && gst_pad_check_reconfigure (encoder->srcpad))))
1598 gst_video_encoder_negotiate (encoder);
1600 GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
1602 frame->output_buffer =
1603 gst_buffer_new_allocate (encoder->priv->allocator, size,
1604 &encoder->priv->params);
1606 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1608 return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
1612 * gst_video_encoder_finish_frame:
1613 * @encoder: a #GstVideoEncoder
1614 * @frame: (transfer full): an encoded #GstVideoCodecFrame
1616 * @frame must have a valid encoded data buffer, whose metadata fields
1617 * are then appropriately set according to frame data or no buffer at
1618 * all if the frame should be dropped.
1619 * It is subsequently pushed downstream or provided to @pre_push.
1620 * In any case, the frame is considered finished and released.
1622 * After calling this function the output buffer of the frame is to be
1623 * considered read-only. This function will also change the metadata
1626 * Returns: a #GstFlowReturn resulting from sending data downstream
1629 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1630 GstVideoCodecFrame * frame)
1632 GstVideoEncoderPrivate *priv = encoder->priv;
1633 GstFlowReturn ret = GST_FLOW_OK;
1634 GstVideoEncoderClass *encoder_class;
1636 gboolean send_headers = FALSE;
1637 gboolean discont = (frame->presentation_frame_number == 0);
1639 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1641 GST_LOG_OBJECT (encoder,
1642 "finish frame fpn %d", frame->presentation_frame_number);
1644 GST_LOG_OBJECT (encoder, "frame PTS %" GST_TIME_FORMAT
1645 ", DTS %" GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts),
1646 GST_TIME_ARGS (frame->dts));
1648 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1650 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
1651 && gst_pad_check_reconfigure (encoder->srcpad))))
1652 gst_video_encoder_negotiate (encoder);
1655 if (G_UNLIKELY (priv->output_state == NULL))
1656 goto no_output_state;
1658 /* Push all pending events that arrived before this frame */
1659 for (l = priv->frames; l; l = l->next) {
1660 GstVideoCodecFrame *tmp = l->data;
1665 for (k = g_list_last (tmp->events); k; k = k->prev)
1666 gst_video_encoder_push_event (encoder, k->data);
1667 g_list_free (tmp->events);
1675 if (priv->tags && priv->tags_changed) {
1676 gst_video_encoder_push_event (encoder,
1677 gst_event_new_tag (gst_tag_list_ref (priv->tags)));
1678 priv->tags_changed = FALSE;
1681 /* no buffer data means this frame is skipped/dropped */
1682 if (!frame->output_buffer) {
1683 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1684 GST_TIME_ARGS (frame->pts));
1688 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1689 GstClockTime stream_time, running_time;
1691 ForcedKeyUnitEvent *fevt = NULL;
1695 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1698 GST_OBJECT_LOCK (encoder);
1699 for (l = priv->force_key_unit; l; l = l->next) {
1700 ForcedKeyUnitEvent *tmp = l->data;
1702 /* Skip non-pending keyunits */
1706 /* Simple case, keyunit ASAP */
1707 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1712 /* Event for before this frame */
1713 if (tmp->running_time <= running_time) {
1720 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1722 GST_OBJECT_UNLOCK (encoder);
1726 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1729 ev = gst_video_event_new_downstream_force_key_unit
1730 (frame->pts, stream_time, running_time,
1731 fevt->all_headers, fevt->count);
1733 gst_video_encoder_push_event (encoder, ev);
1735 if (fevt->all_headers)
1736 send_headers = TRUE;
1738 GST_DEBUG_OBJECT (encoder,
1739 "Forced key unit: running-time %" GST_TIME_FORMAT
1740 ", all_headers %d, count %u",
1741 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1742 forced_key_unit_event_free (fevt);
1746 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1747 priv->distance_from_sync = 0;
1748 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1749 /* For keyframes, DTS = PTS */
1750 if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
1751 frame->dts = frame->pts;
1752 } else if (GST_CLOCK_TIME_IS_VALID (frame->pts) && frame->pts != frame->dts) {
1753 GST_WARNING_OBJECT (encoder, "keyframe PTS != DTS");
1756 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1759 frame->distance_from_sync = priv->distance_from_sync;
1760 priv->distance_from_sync++;
1762 GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
1763 GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
1764 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1766 /* update rate estimate */
1767 priv->bytes += gst_buffer_get_size (frame->output_buffer);
1768 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1769 priv->time += frame->duration;
1771 /* better none than nothing valid */
1772 priv->time = GST_CLOCK_TIME_NONE;
1775 if (G_UNLIKELY (send_headers || priv->new_headers)) {
1776 GList *tmp, *copy = NULL;
1778 GST_DEBUG_OBJECT (encoder, "Sending headers");
1780 /* First make all buffers metadata-writable */
1781 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1782 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1784 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1786 g_list_free (priv->headers);
1787 priv->headers = copy;
1789 for (tmp = priv->headers; tmp; tmp = tmp->next) {
1790 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1792 gst_buffer_ref (tmpbuf);
1793 priv->bytes += gst_buffer_get_size (tmpbuf);
1794 if (G_UNLIKELY (discont)) {
1795 GST_LOG_OBJECT (encoder, "marking discont");
1796 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1800 gst_pad_push (encoder->srcpad, tmpbuf);
1802 priv->new_headers = FALSE;
1805 if (G_UNLIKELY (discont)) {
1806 GST_LOG_OBJECT (encoder, "marking discont");
1807 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1810 if (encoder_class->pre_push)
1811 ret = encoder_class->pre_push (encoder, frame);
1813 /* A reference always needs to be owned by the frame on the buffer.
1814 * For that reason, we use a complete sub-buffer (zero-cost) to push
1816 * The original buffer will be free-ed only when downstream AND the
1817 * current implementation are done with the frame. */
1818 if (ret == GST_FLOW_OK)
1819 ret = gst_pad_push (encoder->srcpad, gst_buffer_ref (frame->output_buffer));
1824 /* unref once from the list */
1825 l = g_list_find (priv->frames, frame);
1827 gst_video_codec_frame_unref (frame);
1828 priv->frames = g_list_delete_link (priv->frames, l);
1830 /* unref because this function takes ownership */
1831 gst_video_codec_frame_unref (frame);
1833 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1840 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1841 GST_ERROR_OBJECT (encoder, "Output state was not configured");
1842 return GST_FLOW_ERROR;
1847 * gst_video_encoder_get_output_state:
1848 * @encoder: a #GstVideoEncoder
1850 * Get the current #GstVideoCodecState
1852 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1854 GstVideoCodecState *
1855 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1857 GstVideoCodecState *state;
1859 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1860 state = gst_video_codec_state_ref (encoder->priv->output_state);
1861 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1867 * gst_video_encoder_set_output_state:
1868 * @encoder: a #GstVideoEncoder
1869 * @caps: (transfer full): the #GstCaps to use for the output
1870 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1872 * Creates a new #GstVideoCodecState with the specified caps as the output state
1874 * Any previously set output state on @decoder will be replaced by the newly
1877 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1878 * framerate, codec-data, .... Those should be specified instead in the returned
1879 * #GstVideoCodecState.
1881 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1882 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1885 * If the subclass wishes to override some fields from the output state (like
1886 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1888 * The new output state will only take effect (set on pads and buffers) starting
1889 * from the next call to #gst_video_encoder_finish_frame().
1891 * Returns: (transfer full): the newly configured output state.
1893 GstVideoCodecState *
1894 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1895 GstVideoCodecState * reference)
1897 GstVideoEncoderPrivate *priv = encoder->priv;
1898 GstVideoCodecState *state;
1900 g_return_val_if_fail (caps != NULL, NULL);
1902 state = _new_output_state (caps, reference);
1904 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1905 if (priv->output_state)
1906 gst_video_codec_state_unref (priv->output_state);
1907 priv->output_state = gst_video_codec_state_ref (state);
1909 priv->output_state_changed = TRUE;
1910 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1916 * gst_video_encoder_set_latency:
1917 * @encoder: a #GstVideoEncoder
1918 * @min_latency: minimum latency
1919 * @max_latency: maximum latency
1921 * Informs baseclass of encoding latency.
1924 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1925 GstClockTime min_latency, GstClockTime max_latency)
1927 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1928 g_return_if_fail (max_latency >= min_latency);
1930 GST_OBJECT_LOCK (encoder);
1931 encoder->priv->min_latency = min_latency;
1932 encoder->priv->max_latency = max_latency;
1933 GST_OBJECT_UNLOCK (encoder);
1935 gst_element_post_message (GST_ELEMENT_CAST (encoder),
1936 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1940 * gst_video_encoder_get_latency:
1941 * @encoder: a #GstVideoEncoder
1942 * @min_latency: (out) (allow-none): address of variable in which to store the
1943 * configured minimum latency, or %NULL
1944 * @max_latency: (out) (allow-none): address of variable in which to store the
1945 * configured maximum latency, or %NULL
1947 * Query the configured encoding latency. Results will be returned via
1948 * @min_latency and @max_latency.
1951 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1952 GstClockTime * min_latency, GstClockTime * max_latency)
1954 GST_OBJECT_LOCK (encoder);
1956 *min_latency = encoder->priv->min_latency;
1958 *max_latency = encoder->priv->max_latency;
1959 GST_OBJECT_UNLOCK (encoder);
1963 * gst_video_encoder_get_oldest_frame:
1964 * @encoder: a #GstVideoEncoder
1966 * Get the oldest unfinished pending #GstVideoCodecFrame
1968 * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
1970 GstVideoCodecFrame *
1971 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1973 GstVideoCodecFrame *frame = NULL;
1975 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1976 if (encoder->priv->frames)
1977 frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
1978 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1980 return (GstVideoCodecFrame *) frame;
1984 * gst_video_encoder_get_frame:
1985 * @encoder: a #GstVideoEnccoder
1986 * @frame_number: system_frame_number of a frame
1988 * Get a pending unfinished #GstVideoCodecFrame
1990 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1992 GstVideoCodecFrame *
1993 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1996 GstVideoCodecFrame *frame = NULL;
1998 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
2000 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2001 for (g = encoder->priv->frames; g; g = g->next) {
2002 GstVideoCodecFrame *tmp = g->data;
2004 if (tmp->system_frame_number == frame_number) {
2005 frame = gst_video_codec_frame_ref (tmp);
2009 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2015 * gst_video_encoder_get_frames:
2016 * @encoder: a #GstVideoEncoder
2018 * Get all pending unfinished #GstVideoCodecFrame
2020 * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
2023 gst_video_encoder_get_frames (GstVideoEncoder * encoder)
2027 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2028 frames = g_list_copy (encoder->priv->frames);
2029 g_list_foreach (frames, (GFunc) gst_video_codec_frame_ref, NULL);
2030 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2036 * gst_video_encoder_merge_tags:
2037 * @encoder: a #GstVideoEncoder
2038 * @tags: a #GstTagList to merge
2039 * @mode: the #GstTagMergeMode to use
2041 * Adds tags to so-called pending tags, which will be processed
2042 * before pushing out data downstream.
2044 * Note that this is provided for convenience, and the subclass is
2045 * not required to use this and can still do tag handling on its own.
2050 gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
2051 const GstTagList * tags, GstTagMergeMode mode)
2055 g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2056 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2058 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2060 GST_DEBUG_OBJECT (encoder, "merging tags %" GST_PTR_FORMAT, tags);
2061 otags = encoder->priv->tags;
2062 encoder->priv->tags = gst_tag_list_merge (encoder->priv->tags, tags, mode);
2064 gst_tag_list_unref (otags);
2065 encoder->priv->tags_changed = TRUE;
2066 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2070 * gst_video_encoder_get_allocator:
2071 * @encoder: a #GstVideoEncoder
2072 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2074 * @params: (out) (allow-none) (transfer full): the
2075 * #GstAllocatorParams of @allocator
2077 * Lets #GstVideoEncoder sub-classes to know the memory @allocator
2078 * used by the base class and its @params.
2080 * Unref the @allocator after use it.
2083 gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
2084 GstAllocator ** allocator, GstAllocationParams * params)
2086 g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2089 *allocator = encoder->priv->allocator ?
2090 gst_object_ref (encoder->priv->allocator) : NULL;
2093 *params = encoder->priv->params;