videoencoder: Don't delay set_format
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideoencoder.c
1 /* GStreamer
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>
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:gstvideoencoder
27  * @short_description: Base class for video encoders
28  * @see_also:
29  *
30  * This base class is for video encoders turning raw video into
31  * encoded video data.
32  *
33  * GstVideoEncoder and subclass should cooperate as follows.
34  * <orderedlist>
35  * <listitem>
36  *   <itemizedlist><title>Configuration</title>
37  *   <listitem><para>
38  *     Initially, GstVideoEncoder calls @start when the encoder element
39  *     is activated, which allows subclass to perform any global setup.
40  *   </para></listitem>
41  *   <listitem><para>
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.
48  *   </para></listitem>
49  *   <listitem><para>
50  *     GstVideoEncoder calls @stop at end of all processing.
51  *   </para></listitem>
52  *   </itemizedlist>
53  * </listitem>
54  * <listitem>
55  *   <itemizedlist>
56  *   <title>Data processing</title>
57  *     <listitem><para>
58  *       Base class collects input data and metadata into a frame and hands
59  *       this to subclass' @handle_frame.
60  *     </para></listitem>
61  *     <listitem><para>
62  *       If codec processing results in encoded data, subclass should call
63  *       @gst_video_encoder_finish_frame to have encoded data pushed
64  *       downstream.
65  *     </para></listitem>
66  *     <listitem><para>
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.
70  *     </para></listitem>
71  *     <listitem><para>
72  *       GstVideoEncoderClass will handle both srcpad and sinkpad events.
73  *       Sink events will be passed to subclass if @event callback has been
74  *       provided.
75  *     </para></listitem>
76  *   </itemizedlist>
77  * </listitem>
78  * <listitem>
79  *   <itemizedlist><title>Shutdown phase</title>
80  *   <listitem><para>
81  *     GstVideoEncoder class calls @stop to inform the subclass that data
82  *     parsing will be stopped.
83  *   </para></listitem>
84  *   </itemizedlist>
85  * </listitem>
86  * </orderedlist>
87  *
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.
92  *
93  * Things that subclass need to take care of:
94  * <itemizedlist>
95  *   <listitem><para>Provide pad templates</para></listitem>
96  *   <listitem><para>
97  *      Provide source pad caps before pushing the first buffer
98  *   </para></listitem>
99  *   <listitem><para>
100  *      Accept data in @handle_frame and provide encoded results to
101  *      @gst_video_encoder_finish_frame.
102  *   </para></listitem>
103  * </itemizedlist>
104  *
105  */
106
107 #ifdef HAVE_CONFIG_H
108 #include "config.h"
109 #endif
110
111 /* TODO
112  *
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
115  */
116
117 #include <gst/video/video.h>
118 #include "gstvideoencoder.h"
119 #include "gstvideoutils.h"
120
121 #include <gst/video/gstvideometa.h>
122 #include <gst/video/gstvideopool.h>
123
124 #include <string.h>
125
126 GST_DEBUG_CATEGORY (videoencoder_debug);
127 #define GST_CAT_DEFAULT videoencoder_debug
128
129 #define GST_VIDEO_ENCODER_GET_PRIVATE(obj)  \
130     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
131         GstVideoEncoderPrivate))
132
133 struct _GstVideoEncoderPrivate
134 {
135   guint64 presentation_frame_number;
136   int distance_from_sync;
137
138   /* FIXME : (and introduce a context ?) */
139   gboolean drained;
140   gboolean at_eos;
141
142   gint64 min_latency;
143   gint64 max_latency;
144
145   GList *current_frame_events;
146
147   GList *headers;
148   gboolean new_headers;         /* Whether new headers were just set */
149
150   GList *force_key_unit;        /* List of pending forced keyunits */
151
152   guint32 system_frame_number;
153
154   GList *frames;                /* Protected with OBJECT_LOCK */
155   GstVideoCodecState *input_state;
156   GstVideoCodecState *output_state;
157   gboolean output_state_changed;
158
159   gint64 bytes;
160   gint64 time;
161
162   GstAllocator *allocator;
163   GstAllocationParams params;
164
165   GstTagList *tags;
166   gboolean tags_changed;
167 };
168
169 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
170 struct _ForcedKeyUnitEvent
171 {
172   GstClockTime running_time;
173   gboolean pending;             /* TRUE if this was requested already */
174   gboolean all_headers;
175   guint count;
176   guint32 frame_id;
177 };
178
179 static void
180 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
181 {
182   g_slice_free (ForcedKeyUnitEvent, evt);
183 }
184
185 static ForcedKeyUnitEvent *
186 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
187     guint count)
188 {
189   ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
190
191   evt->running_time = running_time;
192   evt->all_headers = all_headers;
193   evt->count = count;
194
195   return evt;
196 }
197
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);
202
203 static void gst_video_encoder_finalize (GObject * object);
204
205 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
206     GstCaps * caps);
207 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
208     GstCaps * filter);
209 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
210     GstEvent * event);
211 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
212     GstEvent * event);
213 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
214     GstBuffer * buf);
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,
218     GstQuery * query);
219 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
220     GstQuery * query);
221 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
222     encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
223     GstClockTime duration);
224
225 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
226     GstEvent * event);
227 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
228     GstEvent * event);
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 *
235     encoder);
236
237 static gboolean gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
238     GstQuery * query);
239 static gboolean gst_video_encoder_src_query_default (GstVideoEncoder * encoder,
240     GstQuery * query);
241
242 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
243  * method to get to the padtemplates */
244 GType
245 gst_video_encoder_get_type (void)
246 {
247   static volatile gsize type = 0;
248
249   if (g_once_init_enter (&type)) {
250     GType _type;
251     static const GTypeInfo info = {
252       sizeof (GstVideoEncoderClass),
253       NULL,
254       NULL,
255       (GClassInitFunc) gst_video_encoder_class_init,
256       NULL,
257       NULL,
258       sizeof (GstVideoEncoder),
259       0,
260       (GInstanceInitFunc) gst_video_encoder_init,
261     };
262     const GInterfaceInfo preset_interface_info = {
263       NULL,                     /* interface_init */
264       NULL,                     /* interface_finalize */
265       NULL                      /* interface_data */
266     };
267
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);
273   }
274   return type;
275 }
276
277 static void
278 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
279 {
280   GObjectClass *gobject_class;
281   GstElementClass *gstelement_class;
282
283   gobject_class = G_OBJECT_CLASS (klass);
284   gstelement_class = GST_ELEMENT_CLASS (klass);
285
286   GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
287       "Base Video Encoder");
288
289   parent_class = g_type_class_peek_parent (klass);
290
291   g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
292
293   gobject_class->finalize = gst_video_encoder_finalize;
294
295   gstelement_class->change_state =
296       GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
297
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;
305 }
306
307 static GList *
308 _flush_events (GstPad * pad, GList * events)
309 {
310   GList *tmp;
311
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);
317     } else {
318       gst_pad_store_sticky_event (pad, GST_EVENT_CAST (tmp->data));
319     }
320   }
321   g_list_free (events);
322
323   return NULL;
324 }
325
326 static gboolean
327 gst_video_encoder_reset (GstVideoEncoder * encoder, gboolean hard)
328 {
329   GstVideoEncoderPrivate *priv = encoder->priv;
330   gboolean ret = TRUE;
331
332   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
333
334   priv->presentation_frame_number = 0;
335   priv->distance_from_sync = 0;
336
337   g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
338       NULL);
339   g_list_free (priv->force_key_unit);
340   priv->force_key_unit = NULL;
341
342   priv->drained = TRUE;
343
344   g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
345   g_list_free (priv->frames);
346   priv->frames = NULL;
347
348   priv->bytes = 0;
349   priv->time = 0;
350
351   if (hard) {
352     gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
353     gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
354
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;
361
362     if (priv->tags)
363       gst_tag_list_unref (priv->tags);
364     priv->tags = NULL;
365     priv->tags_changed = FALSE;
366
367     priv->min_latency = 0;
368     priv->max_latency = 0;
369
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;
374
375     if (priv->allocator) {
376       gst_object_unref (priv->allocator);
377       priv->allocator = NULL;
378     }
379
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;
383
384   } else {
385     GList *l;
386
387     for (l = priv->frames; l; l = l->next) {
388       GstVideoCodecFrame *frame = l->data;
389
390       frame->events = _flush_events (encoder->srcpad, frame->events);
391     }
392     priv->current_frame_events = _flush_events (encoder->srcpad,
393         encoder->priv->current_frame_events);
394   }
395
396   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
397
398   return ret;
399 }
400
401 /* Always call reset() in one way or another after this */
402 static gboolean
403 gst_video_encoder_flush (GstVideoEncoder * encoder)
404 {
405   GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
406   gboolean ret = TRUE;
407
408   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
409   if (klass->flush)
410     ret = klass->flush (encoder);
411
412   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
413   return ret;
414 }
415
416 static void
417 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
418 {
419   GstVideoEncoderPrivate *priv;
420   GstPadTemplate *pad_template;
421   GstPad *pad;
422
423   GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
424
425   priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
426
427   pad_template =
428       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
429   g_return_if_fail (pad_template != NULL);
430
431   encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
432
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);
439
440   pad_template =
441       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
442   g_return_if_fail (pad_template != NULL);
443
444   encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
445
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);
451
452   gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
453   gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
454
455   g_rec_mutex_init (&encoder->stream_lock);
456
457   priv->at_eos = FALSE;
458   priv->headers = NULL;
459   priv->new_headers = FALSE;
460
461   gst_video_encoder_reset (encoder, TRUE);
462 }
463
464 static gboolean
465 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
466     GstFormat src_format, gint64 src_value, GstFormat * dest_format,
467     gint64 * dest_value)
468 {
469   gboolean res = FALSE;
470
471   g_return_val_if_fail (dest_format != NULL, FALSE);
472   g_return_val_if_fail (dest_value != NULL, FALSE);
473
474   if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
475           src_value == -1)) {
476     if (dest_value)
477       *dest_value = src_value;
478     return TRUE;
479   }
480
481   if (bytes <= 0 || time <= 0) {
482     GST_DEBUG ("not enough metadata yet to convert");
483     goto exit;
484   }
485
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);
491           res = TRUE;
492           break;
493         default:
494           res = FALSE;
495       }
496       break;
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);
501           res = TRUE;
502           break;
503         default:
504           res = FALSE;
505       }
506       break;
507     default:
508       GST_DEBUG ("unhandled conversion from %d to %d", src_format,
509           *dest_format);
510       res = FALSE;
511   }
512
513 exit:
514   return res;
515 }
516
517 /**
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
521  *
522  * Set the codec headers to be sent downstream whenever requested.
523  */
524 void
525 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
526 {
527   GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
528
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,
532         NULL);
533     g_list_free (video_encoder->priv->headers);
534   }
535   video_encoder->priv->headers = headers;
536   video_encoder->priv->new_headers = TRUE;
537
538   GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
539 }
540
541 static GstVideoCodecState *
542 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
543 {
544   GstVideoCodecState *state;
545
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);
550
551   state->caps = caps;
552
553   if (reference) {
554     GstVideoInfo *tgt, *ref;
555
556     tgt = &state->info;
557     ref = &reference->info;
558
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;
570   }
571
572   return state;
573 }
574
575 static GstVideoCodecState *
576 _new_input_state (GstCaps * caps)
577 {
578   GstVideoCodecState *state;
579
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)))
584     goto parse_fail;
585   state->caps = gst_caps_ref (caps);
586
587   return state;
588
589 parse_fail:
590   {
591     g_slice_free (GstVideoCodecState, state);
592     return NULL;
593   }
594 }
595
596 static gboolean
597 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
598 {
599   GstVideoEncoderClass *encoder_class;
600   GstVideoCodecState *state;
601   gboolean ret;
602
603   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
604
605   /* subclass should do something here ... */
606   g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
607
608   GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
609
610   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
611
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;
618   }
619
620   state = _new_input_state (caps);
621   if (G_UNLIKELY (!state))
622     goto parse_fail;
623
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;
629   }
630
631   if (encoder_class->reset) {
632     GST_FIXME_OBJECT (encoder, "GstVideoEncoder::reset() is deprecated");
633     ret = encoder_class->reset (encoder, TRUE);
634   }
635
636   /* and subclass should be ready to configure format at any time around */
637   ret = encoder_class->set_format (encoder, state);
638   if (ret) {
639     if (encoder->priv->input_state)
640       gst_video_codec_state_unref (encoder->priv->input_state);
641     encoder->priv->input_state = state;
642   } else {
643     gst_video_codec_state_unref (state);
644   }
645
646   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
647
648   if (!ret)
649     GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
650
651   return ret;
652
653 caps_not_changed:
654   {
655     GST_DEBUG_OBJECT (encoder, "Caps did not change - ignore");
656     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
657     return TRUE;
658   }
659
660   /* ERRORS */
661 parse_fail:
662   {
663     GST_WARNING_OBJECT (encoder, "Failed to parse caps");
664     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
665     return FALSE;
666   }
667 }
668
669 /**
670  * gst_video_encoder_proxy_getcaps:
671  * @enc: a #GstVideoEncoder
672  * @caps: initial caps
673  * @filter: filter caps
674  *
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).
678  *
679  * Returns: a #GstCaps owned by caller
680  */
681 GstCaps *
682 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
683     GstCaps * filter)
684 {
685   GstCaps *templ_caps;
686   GstCaps *allowed;
687   GstCaps *fcaps, *filter_caps;
688   gint i, j;
689
690   /* Allow downstream to specify width/height/framerate/PAR constraints
691    * and forward them upstream for video converters to handle
692    */
693   templ_caps =
694       caps ? gst_caps_ref (caps) :
695       gst_pad_get_pad_template_caps (encoder->sinkpad);
696   allowed = gst_pad_get_allowed_caps (encoder->srcpad);
697
698   if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
699     fcaps = templ_caps;
700     goto done;
701   }
702
703   GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
704   GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
705
706   filter_caps = gst_caps_new_empty ();
707
708   for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
709     GQuark q_name =
710         gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
711
712     for (j = 0; j < gst_caps_get_size (allowed); j++) {
713       const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
714       const GValue *val;
715       GstStructure *s;
716
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);
726
727       filter_caps = gst_caps_merge_structure (filter_caps, s);
728     }
729   }
730
731   fcaps = gst_caps_intersect (filter_caps, templ_caps);
732   gst_caps_unref (filter_caps);
733   gst_caps_unref (templ_caps);
734
735   if (filter) {
736     GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
737     filter_caps = gst_caps_intersect (fcaps, filter);
738     gst_caps_unref (fcaps);
739     fcaps = filter_caps;
740   }
741
742 done:
743   gst_caps_replace (&allowed, NULL);
744
745   GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
746
747   return fcaps;
748 }
749
750 static GstCaps *
751 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
752 {
753   GstVideoEncoderClass *klass;
754   GstCaps *caps;
755
756   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
757
758   if (klass->getcaps)
759     caps = klass->getcaps (encoder, filter);
760   else
761     caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
762
763   GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
764
765   return caps;
766 }
767
768 static gboolean
769 gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
770     GstQuery * query)
771 {
772   GstAllocator *allocator = NULL;
773   GstAllocationParams params;
774   gboolean update_allocator;
775
776   /* we got configuration from our peer or the decide_allocation method,
777    * parse them */
778   if (gst_query_get_n_allocation_params (query) > 0) {
779     /* try the allocator */
780     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
781     update_allocator = TRUE;
782   } else {
783     allocator = NULL;
784     gst_allocation_params_init (&params);
785     update_allocator = FALSE;
786   }
787
788   if (update_allocator)
789     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
790   else
791     gst_query_add_allocation_param (query, allocator, &params);
792   if (allocator)
793     gst_object_unref (allocator);
794
795   return TRUE;
796 }
797
798 static gboolean
799 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
800     GstQuery * query)
801 {
802   GstCaps *caps;
803   GstVideoInfo info;
804   GstBufferPool *pool;
805   guint size;
806
807   gst_query_parse_allocation (query, &caps, NULL);
808
809   if (caps == NULL)
810     return FALSE;
811
812   if (!gst_video_info_from_caps (&info, caps))
813     return FALSE;
814
815   size = GST_VIDEO_INFO_SIZE (&info);
816
817   if (gst_query_get_n_allocation_pools (query) == 0) {
818     GstStructure *structure;
819     GstAllocator *allocator = NULL;
820     GstAllocationParams params = { 0, 15, 0, 0 };
821
822     if (gst_query_get_n_allocation_params (query) > 0)
823       gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
824     else
825       gst_query_add_allocation_param (query, allocator, &params);
826
827     pool = gst_video_buffer_pool_new ();
828
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, &params);
832
833     if (allocator)
834       gst_object_unref (allocator);
835
836     if (!gst_buffer_pool_set_config (pool, structure))
837       goto config_failed;
838
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);
842   }
843
844   return TRUE;
845
846   /* ERRORS */
847 config_failed:
848   {
849     GST_ERROR_OBJECT (encoder, "failed to set config");
850     gst_object_unref (pool);
851     return FALSE;
852   }
853 }
854
855 static gboolean
856 gst_video_encoder_sink_query_default (GstVideoEncoder * encoder,
857     GstQuery * query)
858 {
859   GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
860   gboolean res = FALSE;
861
862   switch (GST_QUERY_TYPE (query)) {
863     case GST_QUERY_CAPS:
864     {
865       GstCaps *filter, *caps;
866
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);
871       res = TRUE;
872       break;
873     }
874     case GST_QUERY_ALLOCATION:
875     {
876       GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
877
878       if (klass->propose_allocation)
879         res = klass->propose_allocation (encoder, query);
880       break;
881     }
882     default:
883       res = gst_pad_query_default (pad, GST_OBJECT (encoder), query);
884       break;
885   }
886   return res;
887 }
888
889 static gboolean
890 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
891     GstQuery * query)
892 {
893   GstVideoEncoder *encoder;
894   GstVideoEncoderClass *encoder_class;
895   gboolean ret = FALSE;
896
897   encoder = GST_VIDEO_ENCODER (parent);
898   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
899
900   GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
901       GST_QUERY_TYPE_NAME (query));
902
903   if (encoder_class->sink_query)
904     ret = encoder_class->sink_query (encoder, query);
905
906   return ret;
907 }
908
909 static void
910 gst_video_encoder_finalize (GObject * object)
911 {
912   GstVideoEncoder *encoder;
913
914   GST_DEBUG_OBJECT (object, "finalize");
915
916   encoder = GST_VIDEO_ENCODER (object);
917   g_rec_mutex_clear (&encoder->stream_lock);
918
919   if (encoder->priv->allocator) {
920     gst_object_unref (encoder->priv->allocator);
921     encoder->priv->allocator = NULL;
922   }
923
924   G_OBJECT_CLASS (parent_class)->finalize (object);
925 }
926
927 static gboolean
928 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
929 {
930   switch (GST_EVENT_TYPE (event)) {
931     case GST_EVENT_SEGMENT:
932     {
933       GstSegment segment;
934
935       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
936
937       gst_event_copy_segment (event, &segment);
938
939       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
940
941       if (segment.format != GST_FORMAT_TIME) {
942         GST_DEBUG_OBJECT (encoder, "received non TIME segment");
943         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
944         break;
945       }
946
947       encoder->output_segment = segment;
948       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
949       break;
950     }
951     default:
952       break;
953   }
954
955   return gst_pad_push_event (encoder->srcpad, event);
956 }
957
958 static inline void
959 gst_video_encoder_check_and_push_tags (GstVideoEncoder * encoder)
960 {
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;
965   }
966 }
967
968 static gboolean
969 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
970     GstEvent * event)
971 {
972   GstVideoEncoderClass *encoder_class;
973   gboolean ret = FALSE;
974
975   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
976
977   switch (GST_EVENT_TYPE (event)) {
978     case GST_EVENT_CAPS:
979     {
980       GstCaps *caps;
981
982       gst_event_parse_caps (event, &caps);
983       ret = gst_video_encoder_setcaps (encoder, caps);
984
985       gst_event_unref (event);
986       event = NULL;
987       break;
988     }
989     case GST_EVENT_EOS:
990     {
991       GstFlowReturn flow_ret;
992
993       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
994       encoder->priv->at_eos = TRUE;
995
996       if (encoder_class->finish) {
997         flow_ret = encoder_class->finish (encoder);
998       } else {
999         flow_ret = GST_FLOW_OK;
1000       }
1001
1002       if (encoder->priv->current_frame_events) {
1003         GList *l;
1004
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);
1008
1009           gst_video_encoder_push_event (encoder, event);
1010         }
1011       }
1012       g_list_free (encoder->priv->current_frame_events);
1013       encoder->priv->current_frame_events = NULL;
1014
1015       gst_video_encoder_check_and_push_tags (encoder);
1016
1017       ret = (flow_ret == GST_FLOW_OK);
1018       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1019       break;
1020     }
1021     case GST_EVENT_SEGMENT:
1022     {
1023       GstSegment segment;
1024
1025       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1026
1027       gst_event_copy_segment (event, &segment);
1028
1029       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
1030
1031       if (segment.format != GST_FORMAT_TIME) {
1032         GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
1033         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1034         break;
1035       }
1036
1037       encoder->priv->at_eos = FALSE;
1038
1039       encoder->input_segment = segment;
1040       ret = TRUE;
1041       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1042       break;
1043     }
1044     case GST_EVENT_CUSTOM_DOWNSTREAM:
1045     {
1046       if (gst_video_event_is_force_key_unit (event)) {
1047         GstClockTime running_time;
1048         gboolean all_headers;
1049         guint count;
1050
1051         if (gst_video_event_parse_downstream_force_key_unit (event,
1052                 NULL, NULL, &running_time, &all_headers, &count)) {
1053           ForcedKeyUnitEvent *fevt;
1054
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);
1060
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);
1065         }
1066         gst_event_unref (event);
1067         event = NULL;
1068         ret = TRUE;
1069       }
1070       break;
1071     }
1072     case GST_EVENT_TAG:
1073     {
1074       GstTagList *tags;
1075
1076       gst_event_parse_tag (event, &tags);
1077
1078       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1079         tags = gst_tag_list_copy (tags);
1080
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);
1093
1094         gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
1095         gst_tag_list_unref (tags);
1096         gst_event_unref (event);
1097         event = NULL;
1098         ret = TRUE;
1099       }
1100       break;
1101     }
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);
1109       break;
1110     }
1111     default:
1112       break;
1113   }
1114
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
1121    *
1122    * For FLUSH_STOP this is required because it is expected
1123    * to be forwarded immediately and no buffers are queued anyway.
1124    */
1125   if (event) {
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);
1130     } else {
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);
1135       ret = TRUE;
1136     }
1137   }
1138
1139   return ret;
1140 }
1141
1142 static gboolean
1143 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
1144     GstEvent * event)
1145 {
1146   GstVideoEncoder *enc;
1147   GstVideoEncoderClass *klass;
1148   gboolean ret = TRUE;
1149
1150   enc = GST_VIDEO_ENCODER (parent);
1151   klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
1152
1153   GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1154       GST_EVENT_TYPE_NAME (event));
1155
1156   if (klass->sink_event)
1157     ret = klass->sink_event (enc, event);
1158
1159   return ret;
1160 }
1161
1162 static gboolean
1163 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
1164     GstEvent * event)
1165 {
1166   gboolean ret = FALSE;
1167
1168   switch (GST_EVENT_TYPE (event)) {
1169     case GST_EVENT_CUSTOM_UPSTREAM:
1170     {
1171       if (gst_video_event_is_force_key_unit (event)) {
1172         GstClockTime running_time;
1173         gboolean all_headers;
1174         guint count;
1175
1176         if (gst_video_event_parse_upstream_force_key_unit (event,
1177                 &running_time, &all_headers, &count)) {
1178           ForcedKeyUnitEvent *fevt;
1179
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);
1185
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);
1190         }
1191         gst_event_unref (event);
1192         event = NULL;
1193         ret = TRUE;
1194       }
1195       break;
1196     }
1197     default:
1198       break;
1199   }
1200
1201   if (event)
1202     ret =
1203         gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
1204         event);
1205
1206   return ret;
1207 }
1208
1209 static gboolean
1210 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1211 {
1212   GstVideoEncoder *encoder;
1213   GstVideoEncoderClass *klass;
1214   gboolean ret = FALSE;
1215
1216   encoder = GST_VIDEO_ENCODER (parent);
1217   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1218
1219   GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1220
1221   if (klass->src_event)
1222     ret = klass->src_event (encoder, event);
1223
1224   return ret;
1225 }
1226
1227 static gboolean
1228 gst_video_encoder_src_query_default (GstVideoEncoder * enc, GstQuery * query)
1229 {
1230   GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (enc);
1231   GstVideoEncoderPrivate *priv;
1232   gboolean res;
1233
1234   priv = enc->priv;
1235
1236   GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1237
1238   switch (GST_QUERY_TYPE (query)) {
1239     case GST_QUERY_CONVERT:
1240     {
1241       GstFormat src_fmt, dest_fmt;
1242       gint64 src_val, dest_val;
1243
1244       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1245       res =
1246           gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1247           src_val, &dest_fmt, &dest_val);
1248       if (!res)
1249         goto error;
1250       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1251       break;
1252     }
1253     case GST_QUERY_LATENCY:
1254     {
1255       gboolean live;
1256       GstClockTime min_latency, max_latency;
1257
1258       res = gst_pad_peer_query (enc->sinkpad, query);
1259       if (res) {
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));
1264
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;
1271         }
1272         GST_OBJECT_UNLOCK (enc);
1273
1274         gst_query_set_latency (query, live, min_latency, max_latency);
1275       }
1276     }
1277       break;
1278     default:
1279       res = gst_pad_query_default (pad, GST_OBJECT (enc), query);
1280   }
1281   return res;
1282
1283 error:
1284   GST_DEBUG_OBJECT (enc, "query failed");
1285   return res;
1286 }
1287
1288 static gboolean
1289 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1290 {
1291   GstVideoEncoder *encoder;
1292   GstVideoEncoderClass *encoder_class;
1293   gboolean ret = FALSE;
1294
1295   encoder = GST_VIDEO_ENCODER (parent);
1296   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1297
1298   GST_DEBUG_OBJECT (encoder, "received query %d, %s", GST_QUERY_TYPE (query),
1299       GST_QUERY_TYPE_NAME (query));
1300
1301   if (encoder_class->src_query)
1302     ret = encoder_class->src_query (encoder, query);
1303
1304   return ret;
1305 }
1306
1307 static GstVideoCodecFrame *
1308 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1309     GstClockTime pts, GstClockTime dts, GstClockTime duration)
1310 {
1311   GstVideoEncoderPrivate *priv = encoder->priv;
1312   GstVideoCodecFrame *frame;
1313
1314   frame = g_slice_new0 (GstVideoCodecFrame);
1315
1316   frame->ref_count = 1;
1317
1318   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1319   frame->system_frame_number = priv->system_frame_number;
1320   priv->system_frame_number++;
1321
1322   frame->presentation_frame_number = priv->presentation_frame_number;
1323   priv->presentation_frame_number++;
1324   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1325
1326   frame->events = priv->current_frame_events;
1327   priv->current_frame_events = NULL;
1328   frame->input_buffer = buf;
1329   frame->pts = pts;
1330   frame->dts = dts;
1331   frame->duration = duration;
1332   frame->abidata.ABI.ts = pts;
1333
1334   return frame;
1335 }
1336
1337
1338 static GstFlowReturn
1339 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1340 {
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;
1348
1349   encoder = GST_VIDEO_ENCODER (parent);
1350   priv = encoder->priv;
1351   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1352
1353   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1354
1355   if (!encoder->priv->input_state)
1356     goto not_negotiated;
1357
1358   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1359
1360   pts = GST_BUFFER_PTS (buf);
1361   duration = GST_BUFFER_DURATION (buf);
1362
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));
1368
1369   if (priv->at_eos) {
1370     ret = GST_FLOW_EOS;
1371     goto done;
1372   }
1373
1374   start = pts;
1375   if (GST_CLOCK_TIME_IS_VALID (duration))
1376     stop = start + duration;
1377   else
1378     stop = GST_CLOCK_TIME_NONE;
1379
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);
1385     goto done;
1386   }
1387
1388   if (GST_CLOCK_TIME_IS_VALID (cstop))
1389     duration = cstop - cstart;
1390   else
1391     duration = GST_CLOCK_TIME_NONE;
1392
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);
1397
1398   GST_OBJECT_LOCK (encoder);
1399   if (priv->force_key_unit) {
1400     ForcedKeyUnitEvent *fevt = NULL;
1401     GstClockTime running_time;
1402     GList *l;
1403
1404     running_time =
1405         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1406         cstart);
1407
1408     for (l = priv->force_key_unit; l; l = l->next) {
1409       ForcedKeyUnitEvent *tmp = l->data;
1410
1411       /* Skip pending keyunits */
1412       if (tmp->pending)
1413         continue;
1414
1415       /* Simple case, keyunit ASAP */
1416       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1417         fevt = tmp;
1418         break;
1419       }
1420
1421       /* Event for before this frame */
1422       if (tmp->running_time <= running_time) {
1423         fevt = tmp;
1424         break;
1425       }
1426     }
1427
1428     if (fevt) {
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;
1437     }
1438   }
1439   GST_OBJECT_UNLOCK (encoder);
1440
1441   gst_video_codec_frame_ref (frame);
1442   priv->frames = g_list_append (priv->frames, frame);
1443
1444   /* new data, more finish needed */
1445   priv->drained = FALSE;
1446
1447   GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1448       frame->presentation_frame_number);
1449
1450   ret = klass->handle_frame (encoder, frame);
1451
1452 done:
1453   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1454
1455   return ret;
1456
1457   /* ERRORS */
1458 not_negotiated:
1459   {
1460     GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
1461         ("encoder not initialized"));
1462     gst_buffer_unref (buf);
1463     return GST_FLOW_NOT_NEGOTIATED;
1464   }
1465 }
1466
1467 static GstStateChangeReturn
1468 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1469 {
1470   GstVideoEncoder *encoder;
1471   GstVideoEncoderClass *encoder_class;
1472   GstStateChangeReturn ret;
1473
1474   encoder = GST_VIDEO_ENCODER (element);
1475   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1476
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))
1481         goto open_failed;
1482       break;
1483     case GST_STATE_CHANGE_READY_TO_PAUSED:
1484       /* Initialize device/library if needed */
1485       if (encoder_class->start && !encoder_class->start (encoder))
1486         goto start_failed;
1487       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1488       gst_video_encoder_reset (encoder, TRUE);
1489       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1490       break;
1491     default:
1492       break;
1493   }
1494
1495   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1496
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))
1503         goto stop_failed;
1504       break;
1505     case GST_STATE_CHANGE_READY_TO_NULL:
1506       /* close device/library if needed */
1507       if (encoder_class->close && !encoder_class->close (encoder))
1508         goto close_failed;
1509       break;
1510     default:
1511       break;
1512   }
1513
1514   return ret;
1515
1516   /* Errors */
1517
1518 open_failed:
1519   {
1520     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1521         ("Failed to open encoder"));
1522     return GST_STATE_CHANGE_FAILURE;
1523   }
1524
1525 start_failed:
1526   {
1527     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1528         ("Failed to start encoder"));
1529     return GST_STATE_CHANGE_FAILURE;
1530   }
1531
1532 stop_failed:
1533   {
1534     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1535         ("Failed to stop encoder"));
1536     return GST_STATE_CHANGE_FAILURE;
1537   }
1538
1539 close_failed:
1540   {
1541     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1542         ("Failed to close encoder"));
1543     return GST_STATE_CHANGE_FAILURE;
1544   }
1545 }
1546
1547 static gboolean
1548 gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
1549 {
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;
1558   GstCaps *prevcaps;
1559
1560   g_return_val_if_fail (state->caps != NULL, FALSE);
1561
1562   if (encoder->priv->output_state_changed) {
1563     state->caps = gst_caps_make_writable (state->caps);
1564
1565     /* Fill 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);
1574     } else {
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);
1578     }
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;
1583   }
1584
1585   /* Push all pending pre-caps events of the oldest frame before
1586    * setting caps */
1587   frame = encoder->priv->frames ? encoder->priv->frames->data : NULL;
1588   if (frame || encoder->priv->current_frame_events) {
1589     GList **events, *l;
1590
1591     if (frame) {
1592       events = &frame->events;
1593     } else {
1594       events = &encoder->priv->current_frame_events;
1595     }
1596
1597     for (l = g_list_last (*events); l;) {
1598       GstEvent *event = GST_EVENT (l->data);
1599       GList *tmp;
1600
1601       if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
1602         gst_video_encoder_push_event (encoder, event);
1603         tmp = l;
1604         l = l->prev;
1605         *events = g_list_delete_link (*events, tmp);
1606       } else {
1607         l = l->prev;
1608       }
1609     }
1610   }
1611
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);
1615   else
1616     ret = TRUE;
1617   if (prevcaps)
1618     gst_caps_unref (prevcaps);
1619
1620   if (!ret)
1621     goto done;
1622
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");
1626   }
1627
1628   g_assert (klass->decide_allocation != NULL);
1629   ret = klass->decide_allocation (encoder, query);
1630
1631   GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
1632       query);
1633
1634   if (!ret)
1635     goto no_decide_allocation;
1636
1637   /* we got configuration from our peer or the decide_allocation method,
1638    * parse them */
1639   if (gst_query_get_n_allocation_params (query) > 0) {
1640     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
1641   } else {
1642     allocator = NULL;
1643     gst_allocation_params_init (&params);
1644   }
1645
1646   if (encoder->priv->allocator)
1647     gst_object_unref (encoder->priv->allocator);
1648   encoder->priv->allocator = allocator;
1649   encoder->priv->params = params;
1650
1651 done:
1652   if (query)
1653     gst_query_unref (query);
1654
1655   return ret;
1656
1657   /* Errors */
1658 no_decide_allocation:
1659   {
1660     GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
1661     goto done;
1662   }
1663 }
1664
1665 static gboolean
1666 gst_video_encoder_negotiate_unlocked (GstVideoEncoder * encoder)
1667 {
1668   GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1669   gboolean ret = TRUE;
1670
1671   if (G_LIKELY (klass->negotiate))
1672     ret = klass->negotiate (encoder);
1673
1674   return ret;
1675 }
1676
1677 /**
1678  * gst_video_encoder_negotiate:
1679  * @encoder: a #GstVideoEncoder
1680  *
1681  * Negotiate with downstream elements to currently configured #GstVideoCodecState.
1682  * Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
1683  * negotiate fails.
1684  *
1685  * Returns: #TRUE if the negotiation succeeded, else #FALSE.
1686  */
1687 gboolean
1688 gst_video_encoder_negotiate (GstVideoEncoder * encoder)
1689 {
1690   GstVideoEncoderClass *klass;
1691   gboolean ret = TRUE;
1692
1693   g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
1694   g_return_val_if_fail (encoder->priv->output_state, FALSE);
1695
1696   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1697
1698   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1699   gst_pad_check_reconfigure (encoder->srcpad);
1700   if (klass->negotiate) {
1701     ret = klass->negotiate (encoder);
1702     if (!ret)
1703       gst_pad_mark_reconfigure (encoder->srcpad);
1704   }
1705   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1706
1707   return ret;
1708 }
1709
1710 /**
1711  * gst_video_encoder_allocate_output_buffer:
1712  * @encoder: a #GstVideoEncoder
1713  * @size: size of the buffer
1714  *
1715  * Helper function that allocates a buffer to hold an encoded video frame
1716  * for @encoder's current #GstVideoCodecState.
1717  *
1718  * Returns: (transfer full): allocated buffer
1719  */
1720 GstBuffer *
1721 gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
1722 {
1723   GstBuffer *buffer;
1724   gboolean needs_reconfigure = FALSE;
1725
1726   g_return_val_if_fail (size > 0, NULL);
1727
1728   GST_DEBUG ("alloc src buffer");
1729
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);
1737       goto fallback;
1738     }
1739   }
1740
1741   buffer =
1742       gst_buffer_new_allocate (encoder->priv->allocator, size,
1743       &encoder->priv->params);
1744   if (!buffer) {
1745     GST_INFO_OBJECT (encoder, "couldn't allocate output buffer");
1746     goto fallback;
1747   }
1748
1749   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1750
1751   return buffer;
1752
1753 fallback:
1754   buffer = gst_buffer_new_allocate (NULL, size, NULL);
1755
1756   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1757
1758   return buffer;
1759 }
1760
1761 /**
1762  * gst_video_encoder_allocate_output_frame:
1763  * @encoder: a #GstVideoEncoder
1764  * @frame: a #GstVideoCodecFrame
1765  * @size: size of the buffer
1766  *
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.
1770  *
1771  * The buffer allocated here is owned by the frame and you should only
1772  * keep references to the frame, not the buffer.
1773  *
1774  * Returns: %GST_FLOW_OK if an output buffer could be allocated
1775  */
1776 GstFlowReturn
1777 gst_video_encoder_allocate_output_frame (GstVideoEncoder *
1778     encoder, GstVideoCodecFrame * frame, gsize size)
1779 {
1780   gboolean needs_reconfigure = FALSE;
1781
1782   g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
1783
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);
1791     }
1792   }
1793
1794   GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
1795
1796   frame->output_buffer =
1797       gst_buffer_new_allocate (encoder->priv->allocator, size,
1798       &encoder->priv->params);
1799
1800   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1801
1802   return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
1803 }
1804
1805 static void
1806 gst_video_encoder_release_frame (GstVideoEncoder * enc,
1807     GstVideoCodecFrame * frame)
1808 {
1809   GList *link;
1810
1811   /* unref once from the list */
1812   link = g_list_find (enc->priv->frames, frame);
1813   if (link) {
1814     gst_video_codec_frame_unref (frame);
1815     enc->priv->frames = g_list_delete_link (enc->priv->frames, link);
1816   }
1817   /* unref because this function takes ownership */
1818   gst_video_codec_frame_unref (frame);
1819 }
1820
1821 /**
1822  * gst_video_encoder_finish_frame:
1823  * @encoder: a #GstVideoEncoder
1824  * @frame: (transfer full): an encoded #GstVideoCodecFrame 
1825  *
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.
1831  *
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
1834  * of the buffer.
1835  *
1836  * Returns: a #GstFlowReturn resulting from sending data downstream
1837  */
1838 GstFlowReturn
1839 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1840     GstVideoCodecFrame * frame)
1841 {
1842   GstVideoEncoderPrivate *priv = encoder->priv;
1843   GstFlowReturn ret = GST_FLOW_OK;
1844   GstVideoEncoderClass *encoder_class;
1845   GList *l;
1846   gboolean send_headers = FALSE;
1847   gboolean discont = (frame->presentation_frame_number == 0);
1848   GstBuffer *buffer;
1849   gboolean needs_reconfigure = FALSE;
1850
1851   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1852
1853   GST_LOG_OBJECT (encoder,
1854       "finish frame fpn %d", frame->presentation_frame_number);
1855
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));
1859
1860   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1861
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;
1869       else
1870         ret = GST_FLOW_NOT_NEGOTIATED;
1871       goto done;
1872     }
1873   }
1874
1875   if (G_UNLIKELY (priv->output_state == NULL))
1876     goto no_output_state;
1877
1878   /* Push all pending events that arrived before this frame */
1879   for (l = priv->frames; l; l = l->next) {
1880     GstVideoCodecFrame *tmp = l->data;
1881
1882     if (tmp->events) {
1883       GList *k;
1884
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);
1888       tmp->events = NULL;
1889     }
1890
1891     if (tmp == frame)
1892       break;
1893   }
1894
1895   gst_video_encoder_check_and_push_tags (encoder);
1896
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));
1901     goto done;
1902   }
1903
1904   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1905     GstClockTime stream_time, running_time;
1906     GstEvent *ev;
1907     ForcedKeyUnitEvent *fevt = NULL;
1908     GList *l;
1909
1910     running_time =
1911         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1912         frame->pts);
1913
1914     GST_OBJECT_LOCK (encoder);
1915     for (l = priv->force_key_unit; l; l = l->next) {
1916       ForcedKeyUnitEvent *tmp = l->data;
1917
1918       /* Skip non-pending keyunits */
1919       if (!tmp->pending)
1920         continue;
1921
1922       /* Exact match using the frame id */
1923       if (frame->system_frame_number == tmp->frame_id) {
1924         fevt = tmp;
1925         break;
1926       }
1927
1928       /* Simple case, keyunit ASAP */
1929       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1930         fevt = tmp;
1931         break;
1932       }
1933
1934       /* Event for before this frame */
1935       if (tmp->running_time <= running_time) {
1936         fevt = tmp;
1937         break;
1938       }
1939     }
1940
1941     if (fevt) {
1942       priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1943     }
1944     GST_OBJECT_UNLOCK (encoder);
1945
1946     if (fevt) {
1947       stream_time =
1948           gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1949           frame->pts);
1950
1951       ev = gst_video_event_new_downstream_force_key_unit
1952           (frame->pts, stream_time, running_time,
1953           fevt->all_headers, fevt->count);
1954
1955       gst_video_encoder_push_event (encoder, ev);
1956
1957       if (fevt->all_headers)
1958         send_headers = TRUE;
1959
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);
1965     }
1966   }
1967
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");
1976     }
1977   } else {
1978     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1979   }
1980
1981   /* DTS is expected monotone ascending,
1982    * so a good guess is the lowest unsent PTS (all being OK) */
1983   {
1984     GstClockTime min_ts = GST_CLOCK_TIME_NONE;
1985     GstVideoCodecFrame *oframe = NULL;
1986     gboolean seen_none = FALSE;
1987
1988     /* some maintenance regardless */
1989     for (l = priv->frames; l; l = l->next) {
1990       GstVideoCodecFrame *tmp = l->data;
1991
1992       if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
1993         seen_none = TRUE;
1994         continue;
1995       }
1996
1997       if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
1998         min_ts = tmp->abidata.ABI.ts;
1999         oframe = tmp;
2000       }
2001     }
2002     /* save a ts if needed */
2003     if (oframe && oframe != frame) {
2004       oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
2005     }
2006
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));
2013     }
2014   }
2015
2016   frame->distance_from_sync = priv->distance_from_sync;
2017   priv->distance_from_sync++;
2018
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;
2022
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;
2027   } else {
2028     /* better none than nothing valid */
2029     priv->time = GST_CLOCK_TIME_NONE;
2030   }
2031
2032   if (G_UNLIKELY (send_headers || priv->new_headers)) {
2033     GList *tmp, *copy = NULL;
2034
2035     GST_DEBUG_OBJECT (encoder, "Sending headers");
2036
2037     /* First make all buffers metadata-writable */
2038     for (tmp = priv->headers; tmp; tmp = tmp->next) {
2039       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
2040
2041       copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
2042     }
2043     g_list_free (priv->headers);
2044     priv->headers = copy;
2045
2046     for (tmp = priv->headers; tmp; tmp = tmp->next) {
2047       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
2048
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);
2053         discont = FALSE;
2054       }
2055
2056       gst_pad_push (encoder->srcpad, gst_buffer_ref (tmpbuf));
2057     }
2058     priv->new_headers = FALSE;
2059   }
2060
2061   if (G_UNLIKELY (discont)) {
2062     GST_LOG_OBJECT (encoder, "marking discont");
2063     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
2064   }
2065
2066   if (encoder_class->pre_push)
2067     ret = encoder_class->pre_push (encoder, frame);
2068
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);
2072
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
2075    * to the frame
2076    */
2077   gst_video_encoder_release_frame (encoder, frame);
2078   frame = NULL;
2079
2080   if (ret == GST_FLOW_OK)
2081     ret = gst_pad_push (encoder->srcpad, buffer);
2082
2083 done:
2084   /* handed out */
2085   if (frame)
2086     gst_video_encoder_release_frame (encoder, frame);
2087
2088   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2089
2090   return ret;
2091
2092   /* ERRORS */
2093 no_output_state:
2094   {
2095     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2096     GST_ERROR_OBJECT (encoder, "Output state was not configured");
2097     return GST_FLOW_ERROR;
2098   }
2099 }
2100
2101 /**
2102  * gst_video_encoder_get_output_state:
2103  * @encoder: a #GstVideoEncoder
2104  *
2105  * Get the current #GstVideoCodecState
2106  *
2107  * Returns: (transfer full): #GstVideoCodecState describing format of video data.
2108  */
2109 GstVideoCodecState *
2110 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
2111 {
2112   GstVideoCodecState *state;
2113
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);
2117
2118   return state;
2119 }
2120
2121 /**
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
2126  *
2127  * Creates a new #GstVideoCodecState with the specified caps as the output state
2128  * for the encoder.
2129  * Any previously set output state on @encoder will be replaced by the newly
2130  * created one.
2131  *
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.
2135  *
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
2138  * @reference.
2139  *
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.
2142  *
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().
2145  *
2146  * Returns: (transfer full): the newly configured output state.
2147  */
2148 GstVideoCodecState *
2149 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
2150     GstVideoCodecState * reference)
2151 {
2152   GstVideoEncoderPrivate *priv = encoder->priv;
2153   GstVideoCodecState *state;
2154
2155   g_return_val_if_fail (caps != NULL, NULL);
2156
2157   state = _new_output_state (caps, reference);
2158
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);
2163
2164   priv->output_state_changed = TRUE;
2165   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2166
2167   return state;
2168 }
2169
2170 /**
2171  * gst_video_encoder_set_latency:
2172  * @encoder: a #GstVideoEncoder
2173  * @min_latency: minimum latency
2174  * @max_latency: maximum latency
2175  *
2176  * Informs baseclass of encoding latency.
2177  */
2178 void
2179 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
2180     GstClockTime min_latency, GstClockTime max_latency)
2181 {
2182   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2183   g_return_if_fail (max_latency >= min_latency);
2184
2185   GST_OBJECT_LOCK (encoder);
2186   encoder->priv->min_latency = min_latency;
2187   encoder->priv->max_latency = max_latency;
2188   GST_OBJECT_UNLOCK (encoder);
2189
2190   gst_element_post_message (GST_ELEMENT_CAST (encoder),
2191       gst_message_new_latency (GST_OBJECT_CAST (encoder)));
2192 }
2193
2194 /**
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
2201  *
2202  * Query the configured encoding latency. Results will be returned via
2203  * @min_latency and @max_latency.
2204  */
2205 void
2206 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
2207     GstClockTime * min_latency, GstClockTime * max_latency)
2208 {
2209   GST_OBJECT_LOCK (encoder);
2210   if (min_latency)
2211     *min_latency = encoder->priv->min_latency;
2212   if (max_latency)
2213     *max_latency = encoder->priv->max_latency;
2214   GST_OBJECT_UNLOCK (encoder);
2215 }
2216
2217 /**
2218  * gst_video_encoder_get_oldest_frame:
2219  * @encoder: a #GstVideoEncoder
2220  *
2221  * Get the oldest unfinished pending #GstVideoCodecFrame
2222  *
2223  * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
2224  */
2225 GstVideoCodecFrame *
2226 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
2227 {
2228   GstVideoCodecFrame *frame = NULL;
2229
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);
2234
2235   return (GstVideoCodecFrame *) frame;
2236 }
2237
2238 /**
2239  * gst_video_encoder_get_frame:
2240  * @encoder: a #GstVideoEnccoder
2241  * @frame_number: system_frame_number of a frame
2242  *
2243  * Get a pending unfinished #GstVideoCodecFrame
2244  * 
2245  * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2246  */
2247 GstVideoCodecFrame *
2248 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
2249 {
2250   GList *g;
2251   GstVideoCodecFrame *frame = NULL;
2252
2253   GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
2254
2255   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2256   for (g = encoder->priv->frames; g; g = g->next) {
2257     GstVideoCodecFrame *tmp = g->data;
2258
2259     if (tmp->system_frame_number == frame_number) {
2260       frame = gst_video_codec_frame_ref (tmp);
2261       break;
2262     }
2263   }
2264   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2265
2266   return frame;
2267 }
2268
2269 /**
2270  * gst_video_encoder_get_frames:
2271  * @encoder: a #GstVideoEncoder
2272  *
2273  * Get all pending unfinished #GstVideoCodecFrame
2274  * 
2275  * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
2276  */
2277 GList *
2278 gst_video_encoder_get_frames (GstVideoEncoder * encoder)
2279 {
2280   GList *frames;
2281
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);
2286
2287   return frames;
2288 }
2289
2290 /**
2291  * gst_video_encoder_merge_tags:
2292  * @encoder: a #GstVideoEncoder
2293  * @tags: a #GstTagList to merge
2294  * @mode: the #GstTagMergeMode to use
2295  *
2296  * Adds tags to so-called pending tags, which will be processed
2297  * before pushing out data downstream.
2298  *
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.
2301  *
2302  * MT safe.
2303  */
2304 void
2305 gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
2306     const GstTagList * tags, GstTagMergeMode mode)
2307 {
2308   GstTagList *otags;
2309
2310   g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2311   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2312
2313   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2314   if (tags)
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);
2318   if (otags)
2319     gst_tag_list_unref (otags);
2320   encoder->priv->tags_changed = TRUE;
2321   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2322 }
2323
2324 /**
2325  * gst_video_encoder_get_allocator:
2326  * @encoder: a #GstVideoEncoder
2327  * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2328  * used
2329  * @params: (out) (allow-none) (transfer full): the
2330  * #GstAllocatorParams of @allocator
2331  *
2332  * Lets #GstVideoEncoder sub-classes to know the memory @allocator
2333  * used by the base class and its @params.
2334  *
2335  * Unref the @allocator after use it.
2336  */
2337 void
2338 gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
2339     GstAllocator ** allocator, GstAllocationParams * params)
2340 {
2341   g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2342
2343   if (allocator)
2344     *allocator = encoder->priv->allocator ?
2345         gst_object_ref (encoder->priv->allocator) : NULL;
2346
2347   if (params)
2348     *params = encoder->priv->params;
2349 }