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., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, 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 * * Calculate actual latency based on input/output timestamp/frame_number
114 * and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
117 #include <gst/video/video.h>
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 ?) */
145 GList *current_frame_events;
148 gboolean new_headers; /* Whether new headers were just set */
150 GList *force_key_unit; /* List of pending forced keyunits */
152 guint32 system_frame_number;
154 GList *frames; /* Protected with OBJECT_LOCK */
155 GstVideoCodecState *input_state;
156 GstVideoCodecState *output_state;
157 gboolean output_state_changed;
162 GstAllocator *allocator;
163 GstAllocationParams params;
166 gboolean tags_changed;
169 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
170 struct _ForcedKeyUnitEvent
172 GstClockTime running_time;
173 gboolean pending; /* TRUE if this was requested already */
174 gboolean all_headers;
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);
234 static gboolean gst_video_encoder_negotiate_unlocked (GstVideoEncoder *
237 static gboolean gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
239 static gboolean gst_video_encoder_src_query_default (GstVideoEncoder * encoder,
242 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
243 * method to get to the padtemplates */
245 gst_video_encoder_get_type (void)
247 static volatile gsize type = 0;
249 if (g_once_init_enter (&type)) {
251 static const GTypeInfo info = {
252 sizeof (GstVideoEncoderClass),
255 (GClassInitFunc) gst_video_encoder_class_init,
258 sizeof (GstVideoEncoder),
260 (GInstanceInitFunc) gst_video_encoder_init,
262 const GInterfaceInfo preset_interface_info = {
263 NULL, /* interface_init */
264 NULL, /* interface_finalize */
265 NULL /* interface_data */
268 _type = g_type_register_static (GST_TYPE_ELEMENT,
269 "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
270 g_type_add_interface_static (_type, GST_TYPE_PRESET,
271 &preset_interface_info);
272 g_once_init_leave (&type, _type);
278 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
280 GObjectClass *gobject_class;
281 GstElementClass *gstelement_class;
283 gobject_class = G_OBJECT_CLASS (klass);
284 gstelement_class = GST_ELEMENT_CLASS (klass);
286 GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
287 "Base Video Encoder");
289 parent_class = g_type_class_peek_parent (klass);
291 g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
293 gobject_class->finalize = gst_video_encoder_finalize;
295 gstelement_class->change_state =
296 GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
298 klass->sink_event = gst_video_encoder_sink_event_default;
299 klass->src_event = gst_video_encoder_src_event_default;
300 klass->propose_allocation = gst_video_encoder_propose_allocation_default;
301 klass->decide_allocation = gst_video_encoder_decide_allocation_default;
302 klass->negotiate = gst_video_encoder_negotiate_default;
303 klass->sink_query = gst_video_encoder_sink_query_default;
304 klass->src_query = gst_video_encoder_src_query_default;
308 _flush_events (GstPad * pad, GList * events)
312 for (tmp = events; tmp; tmp = tmp->next) {
313 if (GST_EVENT_TYPE (tmp->data) == GST_EVENT_EOS ||
314 GST_EVENT_TYPE (tmp->data) == GST_EVENT_SEGMENT ||
315 !GST_EVENT_IS_STICKY (tmp->data)) {
316 gst_event_unref (tmp->data);
318 gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
321 g_list_free (events);
327 gst_video_encoder_reset (GstVideoEncoder * encoder, gboolean hard)
329 GstVideoEncoderPrivate *priv = encoder->priv;
332 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
334 priv->presentation_frame_number = 0;
335 priv->distance_from_sync = 0;
337 g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
339 g_list_free (priv->force_key_unit);
340 priv->force_key_unit = NULL;
342 priv->drained = TRUE;
344 g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
345 g_list_free (priv->frames);
352 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
353 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
355 if (priv->input_state)
356 gst_video_codec_state_unref (priv->input_state);
357 priv->input_state = NULL;
358 if (priv->output_state)
359 gst_video_codec_state_unref (priv->output_state);
360 priv->output_state = NULL;
363 gst_tag_list_unref (priv->tags);
365 priv->tags_changed = FALSE;
367 priv->min_latency = 0;
368 priv->max_latency = 0;
370 g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
371 g_list_free (priv->headers);
372 priv->headers = NULL;
373 priv->new_headers = FALSE;
375 if (priv->allocator) {
376 gst_object_unref (priv->allocator);
377 priv->allocator = NULL;
380 g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
381 g_list_free (priv->current_frame_events);
382 priv->current_frame_events = NULL;
387 for (l = priv->frames; l; l = l->next) {
388 GstVideoCodecFrame *frame = l->data;
390 frame->events = _flush_events (encoder->srcpad, frame->events);
392 priv->current_frame_events = _flush_events (encoder->srcpad,
393 encoder->priv->current_frame_events);
396 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
401 /* Always call reset() in one way or another after this */
403 gst_video_encoder_flush (GstVideoEncoder * encoder)
405 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
408 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
410 ret = klass->flush (encoder);
412 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
417 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
419 GstVideoEncoderPrivate *priv;
420 GstPadTemplate *pad_template;
423 GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
425 priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
428 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
429 g_return_if_fail (pad_template != NULL);
431 encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
433 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
434 gst_pad_set_event_function (pad,
435 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
436 gst_pad_set_query_function (pad,
437 GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
438 gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
441 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
442 g_return_if_fail (pad_template != NULL);
444 encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
446 gst_pad_set_query_function (pad,
447 GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
448 gst_pad_set_event_function (pad,
449 GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
450 gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
452 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
453 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
455 g_rec_mutex_init (&encoder->stream_lock);
457 priv->at_eos = FALSE;
458 priv->headers = NULL;
459 priv->new_headers = FALSE;
461 gst_video_encoder_reset (encoder, TRUE);
465 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
466 GstFormat src_format, gint64 src_value, GstFormat * dest_format,
469 gboolean res = FALSE;
471 g_return_val_if_fail (dest_format != NULL, FALSE);
472 g_return_val_if_fail (dest_value != NULL, FALSE);
474 if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
477 *dest_value = src_value;
481 if (bytes <= 0 || time <= 0) {
482 GST_DEBUG ("not enough metadata yet to convert");
486 switch (src_format) {
487 case GST_FORMAT_BYTES:
488 switch (*dest_format) {
489 case GST_FORMAT_TIME:
490 *dest_value = gst_util_uint64_scale (src_value, time, bytes);
497 case GST_FORMAT_TIME:
498 switch (*dest_format) {
499 case GST_FORMAT_BYTES:
500 *dest_value = gst_util_uint64_scale (src_value, bytes, time);
508 GST_DEBUG ("unhandled conversion from %d to %d", src_format,
518 * gst_video_encoder_set_headers:
519 * @encoder: a #GstVideoEncoder
520 * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
522 * Set the codec headers to be sent downstream whenever requested.
525 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
527 GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
529 GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
530 if (video_encoder->priv->headers) {
531 g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
533 g_list_free (video_encoder->priv->headers);
535 video_encoder->priv->headers = headers;
536 video_encoder->priv->new_headers = TRUE;
538 GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
541 static GstVideoCodecState *
542 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
544 GstVideoCodecState *state;
546 state = g_slice_new0 (GstVideoCodecState);
547 state->ref_count = 1;
548 gst_video_info_init (&state->info);
549 gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
554 GstVideoInfo *tgt, *ref;
557 ref = &reference->info;
559 /* Copy over extra fields from reference state */
560 tgt->interlace_mode = ref->interlace_mode;
561 tgt->flags = ref->flags;
562 tgt->width = ref->width;
563 tgt->height = ref->height;
564 tgt->chroma_site = ref->chroma_site;
565 tgt->colorimetry = ref->colorimetry;
566 tgt->par_n = ref->par_n;
567 tgt->par_d = ref->par_d;
568 tgt->fps_n = ref->fps_n;
569 tgt->fps_d = ref->fps_d;
575 static GstVideoCodecState *
576 _new_input_state (GstCaps * caps)
578 GstVideoCodecState *state;
580 state = g_slice_new0 (GstVideoCodecState);
581 state->ref_count = 1;
582 gst_video_info_init (&state->info);
583 if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
585 state->caps = gst_caps_ref (caps);
591 g_slice_free (GstVideoCodecState, state);
597 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
599 GstVideoEncoderClass *encoder_class;
600 GstVideoCodecState *state;
603 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
605 /* subclass should do something here ... */
606 g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
608 GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
610 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
612 if (encoder->priv->input_state) {
613 GST_DEBUG_OBJECT (encoder,
614 "Checking if caps changed old %" GST_PTR_FORMAT " new %" GST_PTR_FORMAT,
615 encoder->priv->input_state->caps, caps);
616 if (gst_caps_is_equal (encoder->priv->input_state->caps, caps))
617 goto caps_not_changed;
620 state = _new_input_state (caps);
621 if (G_UNLIKELY (!state))
624 if (encoder->priv->input_state
625 && gst_video_info_is_equal (&state->info,
626 &encoder->priv->input_state->info)) {
627 gst_video_codec_state_unref (state);
628 goto caps_not_changed;
631 if (encoder_class->reset) {
632 GST_FIXME_OBJECT (encoder, "GstVideoEncoder::reset() is deprecated");
633 ret = encoder_class->reset (encoder, TRUE);
636 /* and subclass should be ready to configure format at any time around */
637 ret = encoder_class->set_format (encoder, state);
639 if (encoder->priv->input_state)
640 gst_video_codec_state_unref (encoder->priv->input_state);
641 encoder->priv->input_state = state;
643 gst_video_codec_state_unref (state);
646 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
649 GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
655 GST_DEBUG_OBJECT (encoder, "Caps did not change - ignore");
656 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
663 GST_WARNING_OBJECT (encoder, "Failed to parse caps");
664 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
670 * gst_video_encoder_proxy_getcaps:
671 * @enc: a #GstVideoEncoder
672 * @caps: initial caps
673 * @filter: filter caps
675 * Returns caps that express @caps (or sink template caps if @caps == NULL)
676 * restricted to resolution/format/... combinations supported by downstream
677 * elements (e.g. muxers).
679 * Returns: a #GstCaps owned by caller
682 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
687 GstCaps *fcaps, *filter_caps;
690 /* Allow downstream to specify width/height/framerate/PAR constraints
691 * and forward them upstream for video converters to handle
694 caps ? gst_caps_ref (caps) :
695 gst_pad_get_pad_template_caps (encoder->sinkpad);
696 allowed = gst_pad_get_allowed_caps (encoder->srcpad);
698 if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
703 GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
704 GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
706 filter_caps = gst_caps_new_empty ();
708 for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
710 gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
712 for (j = 0; j < gst_caps_get_size (allowed); j++) {
713 const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
717 s = gst_structure_new_id_empty (q_name);
718 if ((val = gst_structure_get_value (allowed_s, "width")))
719 gst_structure_set_value (s, "width", val);
720 if ((val = gst_structure_get_value (allowed_s, "height")))
721 gst_structure_set_value (s, "height", val);
722 if ((val = gst_structure_get_value (allowed_s, "framerate")))
723 gst_structure_set_value (s, "framerate", val);
724 if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
725 gst_structure_set_value (s, "pixel-aspect-ratio", val);
727 filter_caps = gst_caps_merge_structure (filter_caps, s);
731 fcaps = gst_caps_intersect (filter_caps, templ_caps);
732 gst_caps_unref (filter_caps);
733 gst_caps_unref (templ_caps);
736 GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
737 filter_caps = gst_caps_intersect (fcaps, filter);
738 gst_caps_unref (fcaps);
743 gst_caps_replace (&allowed, NULL);
745 GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
751 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
753 GstVideoEncoderClass *klass;
756 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
759 caps = klass->getcaps (encoder, filter);
761 caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
763 GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
769 gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
772 GstAllocator *allocator = NULL;
773 GstAllocationParams params;
774 gboolean update_allocator;
776 /* we got configuration from our peer or the decide_allocation method,
778 if (gst_query_get_n_allocation_params (query) > 0) {
779 /* try the allocator */
780 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
781 update_allocator = TRUE;
784 gst_allocation_params_init (¶ms);
785 update_allocator = FALSE;
788 if (update_allocator)
789 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
791 gst_query_add_allocation_param (query, allocator, ¶ms);
793 gst_object_unref (allocator);
799 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
807 gst_query_parse_allocation (query, &caps, NULL);
812 if (!gst_video_info_from_caps (&info, caps))
815 size = GST_VIDEO_INFO_SIZE (&info);
817 if (gst_query_get_n_allocation_pools (query) == 0) {
818 GstStructure *structure;
819 GstAllocator *allocator = NULL;
820 GstAllocationParams params = { 0, 15, 0, 0 };
822 if (gst_query_get_n_allocation_params (query) > 0)
823 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
825 gst_query_add_allocation_param (query, allocator, ¶ms);
827 pool = gst_video_buffer_pool_new ();
829 structure = gst_buffer_pool_get_config (pool);
830 gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
831 gst_buffer_pool_config_set_allocator (structure, allocator, ¶ms);
834 gst_object_unref (allocator);
836 if (!gst_buffer_pool_set_config (pool, structure))
839 gst_query_add_allocation_pool (query, pool, size, 0, 0);
840 gst_object_unref (pool);
841 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
849 GST_ERROR_OBJECT (encoder, "failed to set config");
850 gst_object_unref (pool);
856 gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
859 GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
860 gboolean res = FALSE;
862 switch (GST_QUERY_TYPE (query)) {
865 GstCaps *filter, *caps;
867 gst_query_parse_caps (query, &filter);
868 caps = gst_video_encoder_sink_getcaps (encoder, filter);
869 gst_query_set_caps_result (query, caps);
870 gst_caps_unref (caps);
874 case GST_QUERY_ALLOCATION:
876 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
878 if (klass->propose_allocation)
879 res = klass->propose_allocation (encoder, query);
883 res = gst_pad_query_default (pad, GST_OBJECT (encoder), query);
890 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
893 GstVideoEncoder *encoder;
894 GstVideoEncoderClass *encoder_class;
895 gboolean ret = FALSE;
897 encoder = GST_VIDEO_ENCODER (parent);
898 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
900 GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
901 GST_QUERY_TYPE_NAME (query));
903 if (encoder_class->sink_query)
904 ret = encoder_class->sink_query (encoder, query);
910 gst_video_encoder_finalize (GObject * object)
912 GstVideoEncoder *encoder;
914 GST_DEBUG_OBJECT (object, "finalize");
916 encoder = GST_VIDEO_ENCODER (object);
917 g_rec_mutex_clear (&encoder->stream_lock);
919 if (encoder->priv->allocator) {
920 gst_object_unref (encoder->priv->allocator);
921 encoder->priv->allocator = NULL;
924 G_OBJECT_CLASS (parent_class)->finalize (object);
928 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
930 switch (GST_EVENT_TYPE (event)) {
931 case GST_EVENT_SEGMENT:
935 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
937 gst_event_copy_segment (event, &segment);
939 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
941 if (segment.format != GST_FORMAT_TIME) {
942 GST_DEBUG_OBJECT (encoder, "received non TIME segment");
943 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
947 encoder->output_segment = segment;
948 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
955 return gst_pad_push_event (encoder->srcpad, event);
959 gst_video_encoder_check_and_push_tags (GstVideoEncoder * encoder)
961 if (encoder->priv->tags && encoder->priv->tags_changed) {
962 gst_video_encoder_push_event (encoder,
963 gst_event_new_tag (gst_tag_list_ref (encoder->priv->tags)));
964 encoder->priv->tags_changed = FALSE;
969 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
972 GstVideoEncoderClass *encoder_class;
973 gboolean ret = FALSE;
975 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
977 switch (GST_EVENT_TYPE (event)) {
982 gst_event_parse_caps (event, &caps);
983 ret = gst_video_encoder_setcaps (encoder, caps);
985 gst_event_unref (event);
991 GstFlowReturn flow_ret;
993 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
994 encoder->priv->at_eos = TRUE;
996 if (encoder_class->finish) {
997 flow_ret = encoder_class->finish (encoder);
999 flow_ret = GST_FLOW_OK;
1002 if (encoder->priv->current_frame_events) {
1005 for (l = g_list_last (encoder->priv->current_frame_events); l;
1006 l = g_list_previous (l)) {
1007 GstEvent *event = GST_EVENT (l->data);
1009 gst_video_encoder_push_event (encoder, event);
1012 g_list_free (encoder->priv->current_frame_events);
1013 encoder->priv->current_frame_events = NULL;
1015 gst_video_encoder_check_and_push_tags (encoder);
1017 ret = (flow_ret == GST_FLOW_OK);
1018 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1021 case GST_EVENT_SEGMENT:
1025 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1027 gst_event_copy_segment (event, &segment);
1029 GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
1031 if (segment.format != GST_FORMAT_TIME) {
1032 GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
1033 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1037 encoder->priv->at_eos = FALSE;
1039 encoder->input_segment = segment;
1041 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1044 case GST_EVENT_CUSTOM_DOWNSTREAM:
1046 if (gst_video_event_is_force_key_unit (event)) {
1047 GstClockTime running_time;
1048 gboolean all_headers;
1051 if (gst_video_event_parse_downstream_force_key_unit (event,
1052 NULL, NULL, &running_time, &all_headers, &count)) {
1053 ForcedKeyUnitEvent *fevt;
1055 GST_OBJECT_LOCK (encoder);
1056 fevt = forced_key_unit_event_new (running_time, all_headers, count);
1057 encoder->priv->force_key_unit =
1058 g_list_append (encoder->priv->force_key_unit, fevt);
1059 GST_OBJECT_UNLOCK (encoder);
1061 GST_DEBUG_OBJECT (encoder,
1062 "force-key-unit event: running-time %" GST_TIME_FORMAT
1063 ", all_headers %d, count %u",
1064 GST_TIME_ARGS (running_time), all_headers, count);
1066 gst_event_unref (event);
1076 gst_event_parse_tag (event, &tags);
1078 if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1079 tags = gst_tag_list_copy (tags);
1081 /* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
1082 gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1083 gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
1084 gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1085 gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
1086 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1087 gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
1088 gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
1089 gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
1090 gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
1091 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
1092 gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
1094 gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
1095 gst_tag_list_unref (tags);
1096 gst_event_unref (event);
1102 case GST_EVENT_FLUSH_STOP:{
1103 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1104 gst_video_encoder_flush (encoder);
1105 gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
1106 gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
1107 gst_video_encoder_reset (encoder, FALSE);
1108 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1115 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1116 * For EOS this is required because no buffer or serialized event
1117 * will come after EOS and nothing could trigger another
1118 * _finish_frame() call. *
1119 * If the subclass handles sending of EOS manually it can simply
1120 * not chain up to the parent class' event handler
1122 * For FLUSH_STOP this is required because it is expected
1123 * to be forwarded immediately and no buffers are queued anyway.
1126 if (!GST_EVENT_IS_SERIALIZED (event)
1127 || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1128 || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1129 ret = gst_video_encoder_push_event (encoder, event);
1131 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1132 encoder->priv->current_frame_events =
1133 g_list_prepend (encoder->priv->current_frame_events, event);
1134 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1143 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
1146 GstVideoEncoder *enc;
1147 GstVideoEncoderClass *klass;
1148 gboolean ret = TRUE;
1150 enc = GST_VIDEO_ENCODER (parent);
1151 klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
1153 GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1154 GST_EVENT_TYPE_NAME (event));
1156 if (klass->sink_event)
1157 ret = klass->sink_event (enc, event);
1163 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
1166 gboolean ret = FALSE;
1168 switch (GST_EVENT_TYPE (event)) {
1169 case GST_EVENT_CUSTOM_UPSTREAM:
1171 if (gst_video_event_is_force_key_unit (event)) {
1172 GstClockTime running_time;
1173 gboolean all_headers;
1176 if (gst_video_event_parse_upstream_force_key_unit (event,
1177 &running_time, &all_headers, &count)) {
1178 ForcedKeyUnitEvent *fevt;
1180 GST_OBJECT_LOCK (encoder);
1181 fevt = forced_key_unit_event_new (running_time, all_headers, count);
1182 encoder->priv->force_key_unit =
1183 g_list_append (encoder->priv->force_key_unit, fevt);
1184 GST_OBJECT_UNLOCK (encoder);
1186 GST_DEBUG_OBJECT (encoder,
1187 "force-key-unit event: running-time %" GST_TIME_FORMAT
1188 ", all_headers %d, count %u",
1189 GST_TIME_ARGS (running_time), all_headers, count);
1191 gst_event_unref (event);
1203 gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
1210 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1212 GstVideoEncoder *encoder;
1213 GstVideoEncoderClass *klass;
1214 gboolean ret = FALSE;
1216 encoder = GST_VIDEO_ENCODER (parent);
1217 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1219 GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1221 if (klass->src_event)
1222 ret = klass->src_event (encoder, event);
1228 gst_video_encoder_src_query_default (GstVideoEncoder * enc, GstQuery * query)
1230 GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (enc);
1231 GstVideoEncoderPrivate *priv;
1236 GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1238 switch (GST_QUERY_TYPE (query)) {
1239 case GST_QUERY_CONVERT:
1241 GstFormat src_fmt, dest_fmt;
1242 gint64 src_val, dest_val;
1244 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1246 gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1247 src_val, &dest_fmt, &dest_val);
1250 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1253 case GST_QUERY_LATENCY:
1256 GstClockTime min_latency, max_latency;
1258 res = gst_pad_peer_query (enc->sinkpad, query);
1260 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1261 GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1262 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1263 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1265 GST_OBJECT_LOCK (enc);
1266 min_latency += priv->min_latency;
1267 if (enc->priv->max_latency == GST_CLOCK_TIME_NONE) {
1268 max_latency = GST_CLOCK_TIME_NONE;
1269 } else if (max_latency != GST_CLOCK_TIME_NONE) {
1270 max_latency += enc->priv->max_latency;
1272 GST_OBJECT_UNLOCK (enc);
1274 gst_query_set_latency (query, live, min_latency, max_latency);
1279 res = gst_pad_query_default (pad, GST_OBJECT (enc), query);
1284 GST_DEBUG_OBJECT (enc, "query failed");
1289 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1291 GstVideoEncoder *encoder;
1292 GstVideoEncoderClass *encoder_class;
1293 gboolean ret = FALSE;
1295 encoder = GST_VIDEO_ENCODER (parent);
1296 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1298 GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
1299 GST_QUERY_TYPE_NAME (query));
1301 if (encoder_class->src_query)
1302 ret = encoder_class->src_query (encoder, query);
1307 static GstVideoCodecFrame *
1308 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1309 GstClockTime pts, GstClockTime dts, GstClockTime duration)
1311 GstVideoEncoderPrivate *priv = encoder->priv;
1312 GstVideoCodecFrame *frame;
1314 frame = g_slice_new0 (GstVideoCodecFrame);
1316 frame->ref_count = 1;
1318 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1319 frame->system_frame_number = priv->system_frame_number;
1320 priv->system_frame_number++;
1322 frame->presentation_frame_number = priv->presentation_frame_number;
1323 priv->presentation_frame_number++;
1324 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1326 frame->events = priv->current_frame_events;
1327 priv->current_frame_events = NULL;
1328 frame->input_buffer = buf;
1331 frame->duration = duration;
1332 frame->abidata.ABI.ts = pts;
1338 static GstFlowReturn
1339 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1341 GstVideoEncoder *encoder;
1342 GstVideoEncoderPrivate *priv;
1343 GstVideoEncoderClass *klass;
1344 GstVideoCodecFrame *frame;
1345 GstClockTime pts, duration;
1346 GstFlowReturn ret = GST_FLOW_OK;
1347 guint64 start, stop, cstart, cstop;
1349 encoder = GST_VIDEO_ENCODER (parent);
1350 priv = encoder->priv;
1351 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1353 g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1355 if (!encoder->priv->input_state)
1356 goto not_negotiated;
1358 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1360 pts = GST_BUFFER_PTS (buf);
1361 duration = GST_BUFFER_DURATION (buf);
1363 GST_LOG_OBJECT (encoder,
1364 "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
1365 ", DTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1366 gst_buffer_get_size (buf), GST_TIME_ARGS (pts),
1367 GST_TIME_ARGS (GST_BUFFER_DTS (buf)), GST_TIME_ARGS (duration));
1375 if (GST_CLOCK_TIME_IS_VALID (duration))
1376 stop = start + duration;
1378 stop = GST_CLOCK_TIME_NONE;
1380 /* Drop buffers outside of segment */
1381 if (!gst_segment_clip (&encoder->input_segment,
1382 GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1383 GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1384 gst_buffer_unref (buf);
1388 if (GST_CLOCK_TIME_IS_VALID (cstop))
1389 duration = cstop - cstart;
1391 duration = GST_CLOCK_TIME_NONE;
1393 /* incoming DTS is not really relevant and does not make sense anyway,
1394 * so pass along _NONE and maybe come up with something better later on */
1395 frame = gst_video_encoder_new_frame (encoder, buf, cstart,
1396 GST_CLOCK_TIME_NONE, duration);
1398 GST_OBJECT_LOCK (encoder);
1399 if (priv->force_key_unit) {
1400 ForcedKeyUnitEvent *fevt = NULL;
1401 GstClockTime running_time;
1405 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1408 for (l = priv->force_key_unit; l; l = l->next) {
1409 ForcedKeyUnitEvent *tmp = l->data;
1411 /* Skip pending keyunits */
1415 /* Simple case, keyunit ASAP */
1416 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1421 /* Event for before this frame */
1422 if (tmp->running_time <= running_time) {
1429 fevt->frame_id = frame->system_frame_number;
1430 GST_DEBUG_OBJECT (encoder,
1431 "Forcing a key unit at running time %" GST_TIME_FORMAT,
1432 GST_TIME_ARGS (running_time));
1433 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1434 if (fevt->all_headers)
1435 GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1436 fevt->pending = TRUE;
1439 GST_OBJECT_UNLOCK (encoder);
1441 gst_video_codec_frame_ref (frame);
1442 priv->frames = g_list_append (priv->frames, frame);
1444 /* new data, more finish needed */
1445 priv->drained = FALSE;
1447 GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1448 frame->presentation_frame_number);
1450 ret = klass->handle_frame (encoder, frame);
1453 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1460 GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
1461 ("encoder not initialized"));
1462 gst_buffer_unref (buf);
1463 return GST_FLOW_NOT_NEGOTIATED;
1467 static GstStateChangeReturn
1468 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1470 GstVideoEncoder *encoder;
1471 GstVideoEncoderClass *encoder_class;
1472 GstStateChangeReturn ret;
1474 encoder = GST_VIDEO_ENCODER (element);
1475 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1477 switch (transition) {
1478 case GST_STATE_CHANGE_NULL_TO_READY:
1479 /* open device/library if needed */
1480 if (encoder_class->open && !encoder_class->open (encoder))
1483 case GST_STATE_CHANGE_READY_TO_PAUSED:
1484 /* Initialize device/library if needed */
1485 if (encoder_class->start && !encoder_class->start (encoder))
1487 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1488 gst_video_encoder_reset (encoder, TRUE);
1489 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1495 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1497 switch (transition) {
1498 case GST_STATE_CHANGE_PAUSED_TO_READY:
1499 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1500 gst_video_encoder_reset (encoder, TRUE);
1501 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1502 if (encoder_class->stop && !encoder_class->stop (encoder))
1505 case GST_STATE_CHANGE_READY_TO_NULL:
1506 /* close device/library if needed */
1507 if (encoder_class->close && !encoder_class->close (encoder))
1520 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1521 ("Failed to open encoder"));
1522 return GST_STATE_CHANGE_FAILURE;
1527 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1528 ("Failed to start encoder"));
1529 return GST_STATE_CHANGE_FAILURE;
1534 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1535 ("Failed to stop encoder"));
1536 return GST_STATE_CHANGE_FAILURE;
1541 GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1542 ("Failed to close encoder"));
1543 return GST_STATE_CHANGE_FAILURE;
1548 gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
1550 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1551 GstAllocator *allocator;
1552 GstAllocationParams params;
1553 gboolean ret = TRUE;
1554 GstVideoCodecState *state = encoder->priv->output_state;
1555 GstVideoInfo *info = &state->info;
1556 GstQuery *query = NULL;
1557 GstVideoCodecFrame *frame;
1560 g_return_val_if_fail (state->caps != NULL, FALSE);
1562 if (encoder->priv->output_state_changed) {
1563 state->caps = gst_caps_make_writable (state->caps);
1566 gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1567 "height", G_TYPE_INT, info->height,
1568 "pixel-aspect-ratio", GST_TYPE_FRACTION,
1569 info->par_n, info->par_d, NULL);
1570 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1571 /* variable fps with a max-framerate */
1572 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1573 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1575 /* no variable fps or no max-framerate */
1576 gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1577 info->fps_n, info->fps_d, NULL);
1579 if (state->codec_data)
1580 gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1581 state->codec_data, NULL);
1582 encoder->priv->output_state_changed = FALSE;
1585 /* Push all pending pre-caps events of the oldest frame before
1587 frame = encoder->priv->frames ? encoder->priv->frames->data : NULL;
1588 if (frame || encoder->priv->current_frame_events) {
1592 events = &frame->events;
1594 events = &encoder->priv->current_frame_events;
1597 for (l = g_list_last (*events); l;) {
1598 GstEvent *event = GST_EVENT (l->data);
1601 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
1602 gst_video_encoder_push_event (encoder, event);
1605 *events = g_list_delete_link (*events, tmp);
1612 prevcaps = gst_pad_get_current_caps (encoder->srcpad);
1613 if (!prevcaps || !gst_caps_is_equal (prevcaps, state->caps))
1614 ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1618 gst_caps_unref (prevcaps);
1623 query = gst_query_new_allocation (state->caps, TRUE);
1624 if (!gst_pad_peer_query (encoder->srcpad, query)) {
1625 GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
1628 g_assert (klass->decide_allocation != NULL);
1629 ret = klass->decide_allocation (encoder, query);
1631 GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
1635 goto no_decide_allocation;
1637 /* we got configuration from our peer or the decide_allocation method,
1639 if (gst_query_get_n_allocation_params (query) > 0) {
1640 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1643 gst_allocation_params_init (¶ms);
1646 if (encoder->priv->allocator)
1647 gst_object_unref (encoder->priv->allocator);
1648 encoder->priv->allocator = allocator;
1649 encoder->priv->params = params;
1653 gst_query_unref (query);
1658 no_decide_allocation:
1660 GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
1666 gst_video_encoder_negotiate_unlocked (GstVideoEncoder * encoder)
1668 GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1669 gboolean ret = TRUE;
1671 if (G_LIKELY (klass->negotiate))
1672 ret = klass->negotiate (encoder);
1678 * gst_video_encoder_negotiate:
1679 * @encoder: a #GstVideoEncoder
1681 * Negotiate with downstream elements to currently configured #GstVideoCodecState.
1682 * Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
1685 * Returns: #TRUE if the negotiation succeeded, else #FALSE.
1688 gst_video_encoder_negotiate (GstVideoEncoder * encoder)
1690 GstVideoEncoderClass *klass;
1691 gboolean ret = TRUE;
1693 g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
1694 g_return_val_if_fail (encoder->priv->output_state, FALSE);
1696 klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1698 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1699 gst_pad_check_reconfigure (encoder->srcpad);
1700 if (klass->negotiate) {
1701 ret = klass->negotiate (encoder);
1703 gst_pad_mark_reconfigure (encoder->srcpad);
1705 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1711 * gst_video_encoder_allocate_output_buffer:
1712 * @encoder: a #GstVideoEncoder
1713 * @size: size of the buffer
1715 * Helper function that allocates a buffer to hold an encoded video frame
1716 * for @encoder's current #GstVideoCodecState.
1718 * Returns: (transfer full): allocated buffer
1721 gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
1724 gboolean needs_reconfigure = FALSE;
1726 g_return_val_if_fail (size > 0, NULL);
1728 GST_DEBUG ("alloc src buffer");
1730 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1731 needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
1732 if (G_UNLIKELY (encoder->priv->output_state_changed
1733 || (encoder->priv->output_state && needs_reconfigure))) {
1734 if (!gst_video_encoder_negotiate_unlocked (encoder)) {
1735 GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
1736 gst_pad_mark_reconfigure (encoder->srcpad);
1742 gst_buffer_new_allocate (encoder->priv->allocator, size,
1743 &encoder->priv->params);
1745 GST_INFO_OBJECT (encoder, "couldn't allocate output buffer");
1749 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1754 buffer = gst_buffer_new_allocate (NULL, size, NULL);
1756 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1762 * gst_video_encoder_allocate_output_frame:
1763 * @encoder: a #GstVideoEncoder
1764 * @frame: a #GstVideoCodecFrame
1765 * @size: size of the buffer
1767 * Helper function that allocates a buffer to hold an encoded video frame for @encoder's
1768 * current #GstVideoCodecState. Subclass should already have configured video
1769 * state and set src pad caps.
1771 * The buffer allocated here is owned by the frame and you should only
1772 * keep references to the frame, not the buffer.
1774 * Returns: %GST_FLOW_OK if an output buffer could be allocated
1777 gst_video_encoder_allocate_output_frame (GstVideoEncoder *
1778 encoder, GstVideoCodecFrame * frame, gsize size)
1780 gboolean needs_reconfigure = FALSE;
1782 g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
1784 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1785 needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
1786 if (G_UNLIKELY (encoder->priv->output_state_changed
1787 || (encoder->priv->output_state && needs_reconfigure))) {
1788 if (!gst_video_encoder_negotiate_unlocked (encoder)) {
1789 GST_DEBUG_OBJECT (encoder, "Failed to negotiate, fallback allocation");
1790 gst_pad_mark_reconfigure (encoder->srcpad);
1794 GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
1796 frame->output_buffer =
1797 gst_buffer_new_allocate (encoder->priv->allocator, size,
1798 &encoder->priv->params);
1800 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1802 return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
1806 gst_video_encoder_release_frame (GstVideoEncoder * enc,
1807 GstVideoCodecFrame * frame)
1811 /* unref once from the list */
1812 link = g_list_find (enc->priv->frames, frame);
1814 gst_video_codec_frame_unref (frame);
1815 enc->priv->frames = g_list_delete_link (enc->priv->frames, link);
1817 /* unref because this function takes ownership */
1818 gst_video_codec_frame_unref (frame);
1822 * gst_video_encoder_finish_frame:
1823 * @encoder: a #GstVideoEncoder
1824 * @frame: (transfer full): an encoded #GstVideoCodecFrame
1826 * @frame must have a valid encoded data buffer, whose metadata fields
1827 * are then appropriately set according to frame data or no buffer at
1828 * all if the frame should be dropped.
1829 * It is subsequently pushed downstream or provided to @pre_push.
1830 * In any case, the frame is considered finished and released.
1832 * After calling this function the output buffer of the frame is to be
1833 * considered read-only. This function will also change the metadata
1836 * Returns: a #GstFlowReturn resulting from sending data downstream
1839 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1840 GstVideoCodecFrame * frame)
1842 GstVideoEncoderPrivate *priv = encoder->priv;
1843 GstFlowReturn ret = GST_FLOW_OK;
1844 GstVideoEncoderClass *encoder_class;
1846 gboolean send_headers = FALSE;
1847 gboolean discont = (frame->presentation_frame_number == 0);
1849 gboolean needs_reconfigure = FALSE;
1851 encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1853 GST_LOG_OBJECT (encoder,
1854 "finish frame fpn %d", frame->presentation_frame_number);
1856 GST_LOG_OBJECT (encoder, "frame PTS %" GST_TIME_FORMAT
1857 ", DTS %" GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts),
1858 GST_TIME_ARGS (frame->dts));
1860 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1862 needs_reconfigure = gst_pad_check_reconfigure (encoder->srcpad);
1863 if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
1864 && needs_reconfigure))) {
1865 if (!gst_video_encoder_negotiate_unlocked (encoder)) {
1866 gst_pad_mark_reconfigure (encoder->srcpad);
1867 if (GST_PAD_IS_FLUSHING (encoder->srcpad))
1868 ret = GST_FLOW_FLUSHING;
1870 ret = GST_FLOW_NOT_NEGOTIATED;
1875 if (G_UNLIKELY (priv->output_state == NULL))
1876 goto no_output_state;
1878 /* Push all pending events that arrived before this frame */
1879 for (l = priv->frames; l; l = l->next) {
1880 GstVideoCodecFrame *tmp = l->data;
1885 for (k = g_list_last (tmp->events); k; k = k->prev)
1886 gst_video_encoder_push_event (encoder, k->data);
1887 g_list_free (tmp->events);
1895 gst_video_encoder_check_and_push_tags (encoder);
1897 /* no buffer data means this frame is skipped/dropped */
1898 if (!frame->output_buffer) {
1899 GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1900 GST_TIME_ARGS (frame->pts));
1904 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1905 GstClockTime stream_time, running_time;
1907 ForcedKeyUnitEvent *fevt = NULL;
1911 gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1914 GST_OBJECT_LOCK (encoder);
1915 for (l = priv->force_key_unit; l; l = l->next) {
1916 ForcedKeyUnitEvent *tmp = l->data;
1918 /* Skip non-pending keyunits */
1922 /* Exact match using the frame id */
1923 if (frame->system_frame_number == tmp->frame_id) {
1928 /* Simple case, keyunit ASAP */
1929 if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1934 /* Event for before this frame */
1935 if (tmp->running_time <= running_time) {
1942 priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1944 GST_OBJECT_UNLOCK (encoder);
1948 gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1951 ev = gst_video_event_new_downstream_force_key_unit
1952 (frame->pts, stream_time, running_time,
1953 fevt->all_headers, fevt->count);
1955 gst_video_encoder_push_event (encoder, ev);
1957 if (fevt->all_headers)
1958 send_headers = TRUE;
1960 GST_DEBUG_OBJECT (encoder,
1961 "Forced key unit: running-time %" GST_TIME_FORMAT
1962 ", all_headers %d, count %u",
1963 GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1964 forced_key_unit_event_free (fevt);
1968 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1969 priv->distance_from_sync = 0;
1970 GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1971 /* For keyframes, DTS = PTS */
1972 if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
1973 frame->dts = frame->pts;
1974 } else if (GST_CLOCK_TIME_IS_VALID (frame->pts) && frame->pts != frame->dts) {
1975 GST_WARNING_OBJECT (encoder, "keyframe PTS != DTS");
1978 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1981 /* DTS is expected monotone ascending,
1982 * so a good guess is the lowest unsent PTS (all being OK) */
1984 GstClockTime min_ts = GST_CLOCK_TIME_NONE;
1985 GstVideoCodecFrame *oframe = NULL;
1986 gboolean seen_none = FALSE;
1988 /* some maintenance regardless */
1989 for (l = priv->frames; l; l = l->next) {
1990 GstVideoCodecFrame *tmp = l->data;
1992 if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
1997 if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
1998 min_ts = tmp->abidata.ABI.ts;
2002 /* save a ts if needed */
2003 if (oframe && oframe != frame) {
2004 oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
2007 /* and set if needed */
2008 if (!GST_CLOCK_TIME_IS_VALID (frame->dts) && !seen_none) {
2009 frame->dts = min_ts;
2010 GST_DEBUG_OBJECT (encoder,
2011 "no valid DTS, using oldest PTS %" GST_TIME_FORMAT,
2012 GST_TIME_ARGS (frame->pts));
2016 frame->distance_from_sync = priv->distance_from_sync;
2017 priv->distance_from_sync++;
2019 GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
2020 GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
2021 GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
2023 /* update rate estimate */
2024 priv->bytes += gst_buffer_get_size (frame->output_buffer);
2025 if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
2026 priv->time += frame->duration;
2028 /* better none than nothing valid */
2029 priv->time = GST_CLOCK_TIME_NONE;
2032 if (G_UNLIKELY (send_headers || priv->new_headers)) {
2033 GList *tmp, *copy = NULL;
2035 GST_DEBUG_OBJECT (encoder, "Sending headers");
2037 /* First make all buffers metadata-writable */
2038 for (tmp = priv->headers; tmp; tmp = tmp->next) {
2039 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
2041 copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
2043 g_list_free (priv->headers);
2044 priv->headers = copy;
2046 for (tmp = priv->headers; tmp; tmp = tmp->next) {
2047 GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
2049 priv->bytes += gst_buffer_get_size (tmpbuf);
2050 if (G_UNLIKELY (discont)) {
2051 GST_LOG_OBJECT (encoder, "marking discont");
2052 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
2056 gst_pad_push (encoder->srcpad, gst_buffer_ref (tmpbuf));
2058 priv->new_headers = FALSE;
2061 if (G_UNLIKELY (discont)) {
2062 GST_LOG_OBJECT (encoder, "marking discont");
2063 GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
2066 if (encoder_class->pre_push)
2067 ret = encoder_class->pre_push (encoder, frame);
2069 /* Get an additional ref to the buffer, which is going to be pushed
2070 * downstream, the original ref is owned by the frame */
2071 buffer = gst_buffer_ref (frame->output_buffer);
2073 /* Release frame so the buffer is writable when we push it downstream
2074 * if possible, i.e. if the subclass does not hold additional references
2077 gst_video_encoder_release_frame (encoder, frame);
2080 if (ret == GST_FLOW_OK)
2081 ret = gst_pad_push (encoder->srcpad, buffer);
2086 gst_video_encoder_release_frame (encoder, frame);
2088 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2095 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2096 GST_ERROR_OBJECT (encoder, "Output state was not configured");
2097 return GST_FLOW_ERROR;
2102 * gst_video_encoder_get_output_state:
2103 * @encoder: a #GstVideoEncoder
2105 * Get the current #GstVideoCodecState
2107 * Returns: (transfer full): #GstVideoCodecState describing format of video data.
2109 GstVideoCodecState *
2110 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
2112 GstVideoCodecState *state;
2114 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2115 state = gst_video_codec_state_ref (encoder->priv->output_state);
2116 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2122 * gst_video_encoder_set_output_state:
2123 * @encoder: a #GstVideoEncoder
2124 * @caps: (transfer full): the #GstCaps to use for the output
2125 * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
2127 * Creates a new #GstVideoCodecState with the specified caps as the output state
2129 * Any previously set output state on @encoder will be replaced by the newly
2132 * The specified @caps should not contain any resolution, pixel-aspect-ratio,
2133 * framerate, codec-data, .... Those should be specified instead in the returned
2134 * #GstVideoCodecState.
2136 * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
2137 * or framerate) from an existing #GstVideoCodecState, it can be provided as a
2140 * If the subclass wishes to override some fields from the output state (like
2141 * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
2143 * The new output state will only take effect (set on pads and buffers) starting
2144 * from the next call to #gst_video_encoder_finish_frame().
2146 * Returns: (transfer full): the newly configured output state.
2148 GstVideoCodecState *
2149 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
2150 GstVideoCodecState * reference)
2152 GstVideoEncoderPrivate *priv = encoder->priv;
2153 GstVideoCodecState *state;
2155 g_return_val_if_fail (caps != NULL, NULL);
2157 state = _new_output_state (caps, reference);
2159 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2160 if (priv->output_state)
2161 gst_video_codec_state_unref (priv->output_state);
2162 priv->output_state = gst_video_codec_state_ref (state);
2164 priv->output_state_changed = TRUE;
2165 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2171 * gst_video_encoder_set_latency:
2172 * @encoder: a #GstVideoEncoder
2173 * @min_latency: minimum latency
2174 * @max_latency: maximum latency
2176 * Informs baseclass of encoding latency.
2179 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
2180 GstClockTime min_latency, GstClockTime max_latency)
2182 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2183 g_return_if_fail (max_latency >= min_latency);
2185 GST_OBJECT_LOCK (encoder);
2186 encoder->priv->min_latency = min_latency;
2187 encoder->priv->max_latency = max_latency;
2188 GST_OBJECT_UNLOCK (encoder);
2190 gst_element_post_message (GST_ELEMENT_CAST (encoder),
2191 gst_message_new_latency (GST_OBJECT_CAST (encoder)));
2195 * gst_video_encoder_get_latency:
2196 * @encoder: a #GstVideoEncoder
2197 * @min_latency: (out) (allow-none): address of variable in which to store the
2198 * configured minimum latency, or %NULL
2199 * @max_latency: (out) (allow-none): address of variable in which to store the
2200 * configured maximum latency, or %NULL
2202 * Query the configured encoding latency. Results will be returned via
2203 * @min_latency and @max_latency.
2206 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
2207 GstClockTime * min_latency, GstClockTime * max_latency)
2209 GST_OBJECT_LOCK (encoder);
2211 *min_latency = encoder->priv->min_latency;
2213 *max_latency = encoder->priv->max_latency;
2214 GST_OBJECT_UNLOCK (encoder);
2218 * gst_video_encoder_get_oldest_frame:
2219 * @encoder: a #GstVideoEncoder
2221 * Get the oldest unfinished pending #GstVideoCodecFrame
2223 * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
2225 GstVideoCodecFrame *
2226 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
2228 GstVideoCodecFrame *frame = NULL;
2230 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2231 if (encoder->priv->frames)
2232 frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
2233 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2235 return (GstVideoCodecFrame *) frame;
2239 * gst_video_encoder_get_frame:
2240 * @encoder: a #GstVideoEnccoder
2241 * @frame_number: system_frame_number of a frame
2243 * Get a pending unfinished #GstVideoCodecFrame
2245 * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2247 GstVideoCodecFrame *
2248 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
2251 GstVideoCodecFrame *frame = NULL;
2253 GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
2255 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2256 for (g = encoder->priv->frames; g; g = g->next) {
2257 GstVideoCodecFrame *tmp = g->data;
2259 if (tmp->system_frame_number == frame_number) {
2260 frame = gst_video_codec_frame_ref (tmp);
2264 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2270 * gst_video_encoder_get_frames:
2271 * @encoder: a #GstVideoEncoder
2273 * Get all pending unfinished #GstVideoCodecFrame
2275 * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
2278 gst_video_encoder_get_frames (GstVideoEncoder * encoder)
2282 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2283 frames = g_list_copy (encoder->priv->frames);
2284 g_list_foreach (frames, (GFunc) gst_video_codec_frame_ref, NULL);
2285 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2291 * gst_video_encoder_merge_tags:
2292 * @encoder: a #GstVideoEncoder
2293 * @tags: a #GstTagList to merge
2294 * @mode: the #GstTagMergeMode to use
2296 * Adds tags to so-called pending tags, which will be processed
2297 * before pushing out data downstream.
2299 * Note that this is provided for convenience, and the subclass is
2300 * not required to use this and can still do tag handling on its own.
2305 gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
2306 const GstTagList * tags, GstTagMergeMode mode)
2310 g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2311 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2313 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2315 GST_DEBUG_OBJECT (encoder, "merging tags %" GST_PTR_FORMAT, tags);
2316 otags = encoder->priv->tags;
2317 encoder->priv->tags = gst_tag_list_merge (encoder->priv->tags, tags, mode);
2319 gst_tag_list_unref (otags);
2320 encoder->priv->tags_changed = TRUE;
2321 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2325 * gst_video_encoder_get_allocator:
2326 * @encoder: a #GstVideoEncoder
2327 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2329 * @params: (out) (allow-none) (transfer full): the
2330 * #GstAllocatorParams of @allocator
2332 * Lets #GstVideoEncoder sub-classes to know the memory @allocator
2333 * used by the base class and its @params.
2335 * Unref the @allocator after use it.
2338 gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
2339 GstAllocator ** allocator, GstAllocationParams * params)
2341 g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2344 *allocator = encoder->priv->allocator ?
2345 gst_object_ref (encoder->priv->allocator) : NULL;
2348 *params = encoder->priv->params;