video: Initial port of video base classes and related things to 0.11
[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., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, 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  * * Change _set_output_format() to steal the reference of the provided caps
114  * * Calculate actual latency based on input/output timestamp/frame_number
115  *   and if it exceeds the recorded one, save it and emit a GST_MESSAGE_LATENCY
116  */
117
118 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
119  * with newer GLib versions (>= 2.31.0) */
120 #define GLIB_DISABLE_DEPRECATION_WARNINGS
121
122 #include "gstvideoencoder.h"
123 #include "gstvideoutils.h"
124
125 #include <string.h>
126
127 GST_DEBUG_CATEGORY (videoencoder_debug);
128 #define GST_CAT_DEFAULT videoencoder_debug
129
130 #define GST_VIDEO_ENCODER_GET_PRIVATE(obj)  \
131     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VIDEO_ENCODER, \
132         GstVideoEncoderPrivate))
133
134 struct _GstVideoEncoderPrivate
135 {
136   guint64 presentation_frame_number;
137   int distance_from_sync;
138
139   /* FIXME : (and introduce a context ?) */
140   gboolean drained;
141   gboolean at_eos;
142
143   gint64 min_latency;
144   gint64 max_latency;
145
146   GList *current_frame_events;
147
148   GList *headers;
149   gboolean new_headers;         /* Whether new headers were just set */
150
151   GList *force_key_unit;        /* List of pending forced keyunits */
152
153   guint64 system_frame_number;
154
155   GList *frames;                /* Protected with OBJECT_LOCK */
156   GstVideoCodecState *input_state;
157   GstVideoCodecState *output_state;
158   gboolean output_state_changed;
159
160   gint64 bytes;
161   gint64 time;
162 };
163
164 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
165 struct _ForcedKeyUnitEvent
166 {
167   GstClockTime running_time;
168   gboolean pending;             /* TRUE if this was requested already */
169   gboolean all_headers;
170   guint count;
171 };
172
173 static void
174 forced_key_unit_event_free (ForcedKeyUnitEvent * evt)
175 {
176   g_slice_free (ForcedKeyUnitEvent, evt);
177 }
178
179 static ForcedKeyUnitEvent *
180 forced_key_unit_event_new (GstClockTime running_time, gboolean all_headers,
181     guint count)
182 {
183   ForcedKeyUnitEvent *evt = g_slice_new0 (ForcedKeyUnitEvent);
184
185   evt->running_time = running_time;
186   evt->all_headers = all_headers;
187   evt->count = count;
188
189   return evt;
190 }
191
192 static GstElementClass *parent_class = NULL;
193 static void gst_video_encoder_class_init (GstVideoEncoderClass * klass);
194 static void gst_video_encoder_init (GstVideoEncoder * enc,
195     GstVideoEncoderClass * klass);
196
197 static void gst_video_encoder_finalize (GObject * object);
198
199 static gboolean gst_video_encoder_setcaps (GstVideoEncoder * enc,
200     GstCaps * caps);
201 static GstCaps *gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder,
202     GstCaps * filter);
203 static gboolean gst_video_encoder_src_event (GstPad * pad, GstObject * parent,
204     GstEvent * event);
205 static gboolean gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
206     GstEvent * event);
207 static GstFlowReturn gst_video_encoder_chain (GstPad * pad, GstObject * parent,
208     GstBuffer * buf);
209 static GstStateChangeReturn gst_video_encoder_change_state (GstElement *
210     element, GstStateChange transition);
211 static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
212     GstQuery * query);
213 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
214     GstQuery * query);
215 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
216     encoder, GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
217
218 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
219  * method to get to the padtemplates */
220 GType
221 gst_video_encoder_get_type (void)
222 {
223   static volatile gsize type = 0;
224
225   if (g_once_init_enter (&type)) {
226     GType _type;
227     static const GTypeInfo info = {
228       sizeof (GstVideoEncoderClass),
229       NULL,
230       NULL,
231       (GClassInitFunc) gst_video_encoder_class_init,
232       NULL,
233       NULL,
234       sizeof (GstVideoEncoder),
235       0,
236       (GInstanceInitFunc) gst_video_encoder_init,
237     };
238     const GInterfaceInfo preset_interface_info = {
239       NULL,                     /* interface_init */
240       NULL,                     /* interface_finalize */
241       NULL                      /* interface_data */
242     };
243
244     _type = g_type_register_static (GST_TYPE_ELEMENT,
245         "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
246     g_type_add_interface_static (_type, GST_TYPE_PRESET,
247         &preset_interface_info);
248     g_once_init_leave (&type, _type);
249   }
250   return type;
251 }
252
253 static void
254 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
255 {
256   GObjectClass *gobject_class;
257   GstElementClass *gstelement_class;
258
259   gobject_class = G_OBJECT_CLASS (klass);
260   gstelement_class = GST_ELEMENT_CLASS (klass);
261
262   GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
263       "Base Video Encoder");
264
265   parent_class = g_type_class_peek_parent (klass);
266
267   g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
268
269   gobject_class->finalize = gst_video_encoder_finalize;
270
271   gstelement_class->change_state =
272       GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
273 }
274
275 static void
276 gst_video_encoder_reset (GstVideoEncoder * encoder)
277 {
278   GstVideoEncoderPrivate *priv = encoder->priv;
279   GList *g;
280
281   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
282
283   priv->presentation_frame_number = 0;
284   priv->distance_from_sync = 0;
285
286   g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
287       NULL);
288   g_list_free (priv->force_key_unit);
289   priv->force_key_unit = NULL;
290
291   priv->drained = TRUE;
292   priv->min_latency = 0;
293   priv->max_latency = 0;
294
295   g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
296   g_list_free (priv->headers);
297   priv->headers = NULL;
298   priv->new_headers = FALSE;
299
300   g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
301   g_list_free (priv->current_frame_events);
302   priv->current_frame_events = NULL;
303
304   for (g = priv->frames; g; g = g->next) {
305     gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
306   }
307   g_list_free (priv->frames);
308   priv->frames = NULL;
309
310   priv->bytes = 0;
311   priv->time = 0;
312
313   if (priv->input_state)
314     gst_video_codec_state_unref (priv->input_state);
315   priv->input_state = NULL;
316   if (priv->output_state)
317     gst_video_codec_state_unref (priv->output_state);
318   priv->output_state = NULL;
319
320   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
321 }
322
323 static void
324 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
325 {
326   GstVideoEncoderPrivate *priv;
327   GstPadTemplate *pad_template;
328   GstPad *pad;
329
330   GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
331
332   priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
333
334   pad_template =
335       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
336   g_return_if_fail (pad_template != NULL);
337
338   encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
339
340   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
341   gst_pad_set_event_function (pad,
342       GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
343   gst_pad_set_query_function (pad,
344       GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
345   gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
346
347   pad_template =
348       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
349   g_return_if_fail (pad_template != NULL);
350
351   encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
352
353   gst_pad_set_query_function (pad,
354       GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
355   gst_pad_set_event_function (pad,
356       GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
357   gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
358
359   gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
360   gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
361
362   g_rec_mutex_init (&encoder->stream_lock);
363
364   priv->at_eos = FALSE;
365   priv->headers = NULL;
366   priv->new_headers = FALSE;
367
368   gst_video_encoder_reset (encoder);
369 }
370
371 static gboolean
372 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
373     GstFormat src_format, gint64 src_value, GstFormat * dest_format,
374     gint64 * dest_value)
375 {
376   gboolean res = FALSE;
377
378   g_return_val_if_fail (dest_format != NULL, FALSE);
379   g_return_val_if_fail (dest_value != NULL, FALSE);
380
381   if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
382           src_value == -1)) {
383     if (dest_value)
384       *dest_value = src_value;
385     return TRUE;
386   }
387
388   if (bytes <= 0 || time <= 0) {
389     GST_DEBUG ("not enough metadata yet to convert");
390     goto exit;
391   }
392
393   switch (src_format) {
394     case GST_FORMAT_BYTES:
395       switch (*dest_format) {
396         case GST_FORMAT_TIME:
397           *dest_value = gst_util_uint64_scale (src_value, time, bytes);
398           res = TRUE;
399           break;
400         default:
401           res = FALSE;
402       }
403       break;
404     case GST_FORMAT_TIME:
405       switch (*dest_format) {
406         case GST_FORMAT_BYTES:
407           *dest_value = gst_util_uint64_scale (src_value, bytes, time);
408           res = TRUE;
409           break;
410         default:
411           res = FALSE;
412       }
413       break;
414     default:
415       GST_DEBUG ("unhandled conversion from %d to %d", src_format,
416           *dest_format);
417       res = FALSE;
418   }
419
420 exit:
421   return res;
422 }
423
424 /**
425  * gst_video_encoder_set_headers:
426  * @encoder: a #GstVideoEncoder
427  * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
428  *
429  * Set the codec headers to be sent downstream whenever requested.
430  *
431  * Since: 0.10.36
432  */
433 void
434 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
435 {
436   GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
437
438   GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
439   if (video_encoder->priv->headers) {
440     g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
441         NULL);
442     g_list_free (video_encoder->priv->headers);
443   }
444   video_encoder->priv->headers = headers;
445   video_encoder->priv->new_headers = TRUE;
446
447   GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
448 }
449
450 static gboolean
451 gst_video_encoder_drain (GstVideoEncoder * enc)
452 {
453   GstVideoEncoderPrivate *priv;
454   GstVideoEncoderClass *enc_class;
455   gboolean ret = TRUE;
456
457   enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
458   priv = enc->priv;
459
460   GST_DEBUG_OBJECT (enc, "draining");
461
462   if (priv->drained) {
463     GST_DEBUG_OBJECT (enc, "already drained");
464     return TRUE;
465   }
466
467   if (enc_class->reset) {
468     GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
469     ret = enc_class->reset (enc, TRUE);
470   }
471   /* everything should be away now */
472   if (priv->frames) {
473     /* not fatal/impossible though if subclass/enc eats stuff */
474     g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
475     g_list_free (priv->frames);
476     priv->frames = NULL;
477   }
478
479   return ret;
480 }
481
482 static GstVideoCodecState *
483 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
484 {
485   GstVideoCodecState *state;
486
487   state = g_slice_new0 (GstVideoCodecState);
488   state->ref_count = 1;
489   gst_video_info_init (&state->info);
490   gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
491
492   state->caps = caps;
493
494   if (reference) {
495     GstVideoInfo *tgt, *ref;
496
497     tgt = &state->info;
498     ref = &reference->info;
499
500     /* Copy over extra fields from reference state */
501     tgt->interlace_mode = ref->interlace_mode;
502     tgt->flags = ref->flags;
503     tgt->width = ref->width;
504     tgt->height = ref->height;
505     tgt->chroma_site = ref->chroma_site;
506     tgt->colorimetry = ref->colorimetry;
507     tgt->par_n = ref->par_n;
508     tgt->par_d = ref->par_d;
509     tgt->fps_n = ref->fps_n;
510     tgt->fps_d = ref->fps_d;
511   }
512
513   return state;
514 }
515
516 static GstVideoCodecState *
517 _new_input_state (GstCaps * caps)
518 {
519   GstVideoCodecState *state;
520
521   state = g_slice_new0 (GstVideoCodecState);
522   state->ref_count = 1;
523   gst_video_info_init (&state->info);
524   if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
525     goto parse_fail;
526   state->caps = gst_caps_ref (caps);
527
528   return state;
529
530 parse_fail:
531   {
532     g_slice_free (GstVideoCodecState, state);
533     return NULL;
534   }
535 }
536
537 static gboolean
538 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
539 {
540   GstVideoEncoderClass *encoder_class;
541   GstVideoCodecState *state;
542   gboolean ret;
543   gboolean samecaps = FALSE;
544
545   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
546
547   /* subclass should do something here ... */
548   g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
549
550   GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
551
552   state = _new_input_state (caps);
553   if (G_UNLIKELY (!state))
554     goto parse_fail;
555
556   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
557
558   if (encoder->priv->input_state)
559     samecaps =
560         gst_video_info_is_equal (&state->info,
561         &encoder->priv->input_state->info);
562
563   if (!samecaps) {
564     /* arrange draining pending frames */
565     gst_video_encoder_drain (encoder);
566
567     /* and subclass should be ready to configure format at any time around */
568     ret = encoder_class->set_format (encoder, state);
569     if (ret) {
570       if (encoder->priv->input_state)
571         gst_video_codec_state_unref (encoder->priv->input_state);
572       encoder->priv->input_state = state;
573     } else
574       gst_video_codec_state_unref (state);
575   } else {
576     /* no need to stir things up */
577     GST_DEBUG_OBJECT (encoder,
578         "new video format identical to configured format");
579     gst_video_codec_state_unref (state);
580     ret = TRUE;
581   }
582
583   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
584
585   if (!ret)
586     GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
587
588   gst_object_unref (encoder);
589
590   return ret;
591
592 parse_fail:
593   {
594     GST_WARNING_OBJECT (encoder, "Failed to parse caps");
595     gst_object_unref (encoder);
596     return FALSE;
597   }
598 }
599
600 /**
601  * gst_video_encoder_proxy_getcaps:
602  * @enc: a #GstVideoEncoder
603  * @caps: initial caps
604  *
605  * Returns caps that express @caps (or sink template caps if @caps == NULL)
606  * restricted to resolution/format/... combinations supported by downstream
607  * elements (e.g. muxers).
608  *
609  * Returns: a #GstCaps owned by caller
610  *
611  * Since: 0.10.36
612  */
613 GstCaps *
614 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
615     GstCaps * filter)
616 {
617   GstCaps *templ_caps;
618   GstCaps *allowed;
619   GstCaps *fcaps, *filter_caps;
620   gint i, j;
621
622   /* Allow downstream to specify width/height/framerate/PAR constraints
623    * and forward them upstream for video converters to handle
624    */
625   templ_caps = caps ? caps : gst_pad_get_pad_template_caps (encoder->sinkpad);
626   allowed = gst_pad_get_allowed_caps (encoder->srcpad);
627
628   if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
629     fcaps = templ_caps;
630     goto done;
631   }
632
633   GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
634   GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
635
636   filter_caps = gst_caps_new_empty ();
637
638   for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
639     GQuark q_name =
640         gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
641
642     for (j = 0; j < gst_caps_get_size (allowed); j++) {
643       const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
644       const GValue *val;
645       GstStructure *s;
646
647       s = gst_structure_new_id_empty (q_name);
648       if ((val = gst_structure_get_value (allowed_s, "width")))
649         gst_structure_set_value (s, "width", val);
650       if ((val = gst_structure_get_value (allowed_s, "height")))
651         gst_structure_set_value (s, "height", val);
652       if ((val = gst_structure_get_value (allowed_s, "framerate")))
653         gst_structure_set_value (s, "framerate", val);
654       if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
655         gst_structure_set_value (s, "pixel-aspect-ratio", val);
656
657       filter_caps = gst_caps_merge_structure (filter_caps, s);
658     }
659   }
660
661   fcaps = gst_caps_intersect (filter_caps, templ_caps);
662   gst_caps_unref (filter_caps);
663   gst_caps_unref (templ_caps);
664
665   if (filter) {
666     GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
667     filter_caps = gst_caps_intersect (fcaps, filter);
668     gst_caps_unref (fcaps);
669     fcaps = filter_caps;
670   }
671
672 done:
673   gst_caps_replace (&allowed, NULL);
674
675   GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
676
677   return fcaps;
678 }
679
680 static GstCaps *
681 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
682 {
683   GstVideoEncoderClass *klass;
684   GstCaps *caps;
685
686   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
687
688   if (klass->getcaps)
689     caps = klass->getcaps (encoder, filter);
690   else
691     caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
692
693   GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
694
695   return caps;
696 }
697
698
699 static gboolean
700 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
701     GstQuery * query)
702 {
703   GstVideoEncoder *encoder;
704   gboolean res = FALSE;
705
706   encoder = GST_VIDEO_ENCODER (parent);
707
708   switch (GST_QUERY_TYPE (query)) {
709     case GST_QUERY_CAPS:
710     {
711       GstCaps *filter, *caps;
712
713       gst_query_parse_caps (query, &filter);
714       caps = gst_video_encoder_sink_getcaps (encoder, filter);
715       gst_query_set_caps_result (query, caps);
716       gst_caps_unref (caps);
717       res = TRUE;
718       break;
719     }
720     default:
721       res = gst_pad_query_default (pad, parent, query);
722       break;
723   }
724   return res;
725 }
726
727 static void
728 gst_video_encoder_finalize (GObject * object)
729 {
730   GstVideoEncoder *encoder;
731
732   GST_DEBUG_OBJECT (object, "finalize");
733
734   encoder = GST_VIDEO_ENCODER (object);
735   if (encoder->priv->headers) {
736     g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
737     g_list_free (encoder->priv->headers);
738   }
739   g_rec_mutex_clear (&encoder->stream_lock);
740
741   G_OBJECT_CLASS (parent_class)->finalize (object);
742 }
743
744 static gboolean
745 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
746 {
747   switch (GST_EVENT_TYPE (event)) {
748     case GST_EVENT_SEGMENT:
749     {
750       GstSegment segment;
751
752       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
753
754       gst_event_copy_segment (event, &segment);
755
756       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
757
758       if (segment.format != GST_FORMAT_TIME) {
759         GST_DEBUG_OBJECT (encoder, "received non TIME segment");
760         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
761         break;
762       }
763
764       encoder->output_segment = segment;
765       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
766       break;
767     }
768     default:
769       break;
770   }
771
772   return gst_pad_push_event (encoder->srcpad, event);
773 }
774
775 static gboolean
776 gst_video_encoder_sink_eventfunc (GstVideoEncoder * encoder, GstEvent * event)
777 {
778   GstVideoEncoderClass *encoder_class;
779   gboolean ret = FALSE;
780
781   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
782
783   switch (GST_EVENT_TYPE (event)) {
784     case GST_EVENT_CAPS:
785     {
786       GstCaps *caps;
787
788       gst_event_parse_caps (event, &caps);
789       ret = gst_video_encoder_setcaps (encoder, caps);
790       gst_event_unref (event);
791       break;
792     }
793     case GST_EVENT_EOS:
794     {
795       GstFlowReturn flow_ret;
796
797       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
798       encoder->priv->at_eos = TRUE;
799
800       if (encoder_class->finish) {
801         flow_ret = encoder_class->finish (encoder);
802       } else {
803         flow_ret = GST_FLOW_OK;
804       }
805
806       ret = (flow_ret == GST_VIDEO_ENCODER_FLOW_DROPPED);
807       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
808       break;
809     }
810     case GST_EVENT_SEGMENT:
811     {
812       GstSegment segment;
813
814       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
815
816       gst_event_copy_segment (event, &segment);
817
818       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
819
820       if (segment.format != GST_FORMAT_TIME) {
821         GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
822         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
823         break;
824       }
825
826       encoder->priv->at_eos = FALSE;
827
828       encoder->input_segment = segment;
829       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
830       break;
831     }
832     case GST_EVENT_CUSTOM_DOWNSTREAM:
833     {
834       if (gst_video_event_is_force_key_unit (event)) {
835         GstClockTime running_time;
836         gboolean all_headers;
837         guint count;
838
839         if (gst_video_event_parse_downstream_force_key_unit (event,
840                 NULL, NULL, &running_time, &all_headers, &count)) {
841           ForcedKeyUnitEvent *fevt;
842
843           GST_OBJECT_LOCK (encoder);
844           fevt = forced_key_unit_event_new (running_time, all_headers, count);
845           encoder->priv->force_key_unit =
846               g_list_append (encoder->priv->force_key_unit, fevt);
847           GST_OBJECT_UNLOCK (encoder);
848
849           GST_DEBUG_OBJECT (encoder,
850               "force-key-unit event: running-time %" GST_TIME_FORMAT
851               ", all_headers %d, count %u",
852               GST_TIME_ARGS (running_time), all_headers, count);
853         }
854         gst_event_unref (event);
855         ret = TRUE;
856       }
857       break;
858     }
859     default:
860       break;
861   }
862
863   return ret;
864 }
865
866 static gboolean
867 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
868     GstEvent * event)
869 {
870   GstVideoEncoder *enc;
871   GstVideoEncoderClass *klass;
872   gboolean handled = FALSE;
873   gboolean ret = TRUE;
874
875   enc = GST_VIDEO_ENCODER (parent);
876   klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
877
878   GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
879       GST_EVENT_TYPE_NAME (event));
880
881   if (klass->sink_event)
882     handled = klass->sink_event (enc, event);
883
884   if (!handled)
885     handled = gst_video_encoder_sink_eventfunc (enc, event);
886
887   if (!handled) {
888     /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
889      * For EOS this is required because no buffer or serialized event
890      * will come after EOS and nothing could trigger another
891      * _finish_frame() call.   *
892      * If the subclass handles sending of EOS manually it can return
893      * _DROPPED from ::finish() and all other subclasses should have
894      * decoded/flushed all remaining data before this
895      *
896      * For FLUSH_STOP this is required because it is expected
897      * to be forwarded immediately and no buffers are queued anyway.
898      */
899     if (!GST_EVENT_IS_SERIALIZED (event)
900         || GST_EVENT_TYPE (event) == GST_EVENT_EOS
901         || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
902       ret = gst_video_encoder_push_event (enc, event);
903     } else {
904       GST_VIDEO_ENCODER_STREAM_LOCK (enc);
905       enc->priv->current_frame_events =
906           g_list_prepend (enc->priv->current_frame_events, event);
907       GST_VIDEO_ENCODER_STREAM_UNLOCK (enc);
908     }
909   }
910
911   GST_DEBUG_OBJECT (enc, "event handled");
912
913   return ret;
914 }
915
916 static gboolean
917 gst_video_encoder_src_eventfunc (GstVideoEncoder * encoder, GstEvent * event)
918 {
919   gboolean handled = FALSE;
920
921   switch (GST_EVENT_TYPE (event)) {
922     case GST_EVENT_CUSTOM_UPSTREAM:
923     {
924       if (gst_video_event_is_force_key_unit (event)) {
925         GstClockTime running_time;
926         gboolean all_headers;
927         guint count;
928
929         if (gst_video_event_parse_upstream_force_key_unit (event,
930                 &running_time, &all_headers, &count)) {
931           ForcedKeyUnitEvent *fevt;
932
933           GST_OBJECT_LOCK (encoder);
934           fevt = forced_key_unit_event_new (running_time, all_headers, count);
935           encoder->priv->force_key_unit =
936               g_list_append (encoder->priv->force_key_unit, fevt);
937           GST_OBJECT_UNLOCK (encoder);
938
939           GST_DEBUG_OBJECT (encoder,
940               "force-key-unit event: running-time %" GST_TIME_FORMAT
941               ", all_headers %d, count %u",
942               GST_TIME_ARGS (running_time), all_headers, count);
943         }
944         gst_event_unref (event);
945         handled = TRUE;
946       }
947       break;
948     }
949     default:
950       break;
951   }
952
953   return handled;
954 }
955
956 static gboolean
957 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
958 {
959   GstVideoEncoder *encoder;
960   GstVideoEncoderClass *klass;
961   gboolean ret = FALSE;
962   gboolean handled = FALSE;
963
964   encoder = GST_VIDEO_ENCODER (parent);
965   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
966
967   GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
968
969   if (klass->src_event)
970     handled = klass->src_event (encoder, event);
971
972   if (!handled)
973     handled = gst_video_encoder_src_eventfunc (encoder, event);
974
975   if (!handled)
976     ret = gst_pad_event_default (pad, parent, event);
977
978   return ret;
979 }
980
981 static gboolean
982 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
983 {
984   GstVideoEncoderPrivate *priv;
985   GstVideoEncoder *enc;
986   gboolean res;
987
988   enc = GST_VIDEO_ENCODER (parent);
989   priv = enc->priv;
990
991   GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
992
993   switch (GST_QUERY_TYPE (query)) {
994     case GST_QUERY_CONVERT:
995     {
996       GstFormat src_fmt, dest_fmt;
997       gint64 src_val, dest_val;
998
999       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1000       res =
1001           gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1002           src_val, &dest_fmt, &dest_val);
1003       if (!res)
1004         goto error;
1005       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1006       break;
1007     }
1008     case GST_QUERY_LATENCY:
1009     {
1010       gboolean live;
1011       GstClockTime min_latency, max_latency;
1012
1013       res = gst_pad_peer_query (enc->sinkpad, query);
1014       if (res) {
1015         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1016         GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1017             GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1018             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1019
1020         GST_OBJECT_LOCK (enc);
1021         min_latency += priv->min_latency;
1022         if (max_latency != GST_CLOCK_TIME_NONE) {
1023           max_latency += priv->max_latency;
1024         }
1025         GST_OBJECT_UNLOCK (enc);
1026
1027         gst_query_set_latency (query, live, min_latency, max_latency);
1028       }
1029     }
1030       break;
1031     default:
1032       res = gst_pad_query_default (pad, parent, query);
1033   }
1034   return res;
1035
1036 error:
1037   GST_DEBUG_OBJECT (enc, "query failed");
1038   return res;
1039 }
1040
1041 static GstVideoCodecFrame *
1042 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1043     GstClockTime timestamp, GstClockTime duration)
1044 {
1045   GstVideoEncoderPrivate *priv = encoder->priv;
1046   GstVideoCodecFrame *frame;
1047
1048   frame = g_slice_new0 (GstVideoCodecFrame);
1049
1050   frame->ref_count = 1;
1051
1052   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1053   frame->system_frame_number = priv->system_frame_number;
1054   priv->system_frame_number++;
1055
1056   frame->presentation_frame_number = priv->presentation_frame_number;
1057   priv->presentation_frame_number++;
1058   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1059
1060   frame->events = priv->current_frame_events;
1061   priv->current_frame_events = NULL;
1062   frame->input_buffer = buf;
1063   frame->pts = timestamp;
1064   frame->duration = duration;
1065
1066   return frame;
1067 }
1068
1069
1070 static GstFlowReturn
1071 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1072 {
1073   GstVideoEncoder *encoder;
1074   GstVideoEncoderPrivate *priv;
1075   GstVideoEncoderClass *klass;
1076   GstVideoCodecFrame *frame;
1077   GstFlowReturn ret = GST_FLOW_OK;
1078   guint64 start, stop = GST_CLOCK_TIME_NONE, cstart, cstop;
1079
1080   encoder = GST_VIDEO_ENCODER (parent);
1081   priv = encoder->priv;
1082   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1083
1084   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1085
1086   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1087
1088   start = GST_BUFFER_TIMESTAMP (buf);
1089   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buf)))
1090     stop = start + GST_BUFFER_DURATION (buf);
1091
1092   GST_LOG_OBJECT (encoder,
1093       "received buffer of size %d with ts %" GST_TIME_FORMAT
1094       ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
1095       GST_TIME_ARGS (start), GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1096
1097   if (priv->at_eos) {
1098     ret = GST_FLOW_EOS;
1099     goto done;
1100   }
1101
1102   /* Drop buffers outside of segment */
1103   if (!gst_segment_clip (&encoder->output_segment,
1104           GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1105     GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1106     gst_buffer_unref (buf);
1107     goto done;
1108   }
1109
1110   frame = gst_video_encoder_new_frame (encoder, buf, cstart, cstop - cstart);
1111
1112   GST_OBJECT_LOCK (encoder);
1113   if (priv->force_key_unit) {
1114     ForcedKeyUnitEvent *fevt = NULL;
1115     GstClockTime running_time;
1116     GList *l;
1117
1118     running_time =
1119         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1120         GST_BUFFER_TIMESTAMP (buf));
1121
1122     for (l = priv->force_key_unit; l; l = l->next) {
1123       ForcedKeyUnitEvent *tmp = l->data;
1124
1125       /* Skip pending keyunits */
1126       if (tmp->pending)
1127         continue;
1128
1129       /* Simple case, keyunit ASAP */
1130       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1131         fevt = tmp;
1132         break;
1133       }
1134
1135       /* Event for before this frame */
1136       if (tmp->running_time <= running_time) {
1137         fevt = tmp;
1138         break;
1139       }
1140     }
1141
1142     if (fevt) {
1143       GST_DEBUG_OBJECT (encoder,
1144           "Forcing a key unit at running time %" GST_TIME_FORMAT,
1145           GST_TIME_ARGS (running_time));
1146       GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1147       if (fevt->all_headers)
1148         GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1149       fevt->pending = TRUE;
1150     }
1151   }
1152   GST_OBJECT_UNLOCK (encoder);
1153
1154   priv->frames = g_list_append (priv->frames, frame);
1155
1156   /* new data, more finish needed */
1157   priv->drained = FALSE;
1158
1159   GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1160       frame->presentation_frame_number);
1161
1162   ret = klass->handle_frame (encoder, frame);
1163
1164 done:
1165   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1166
1167   return ret;
1168 }
1169
1170 static GstStateChangeReturn
1171 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1172 {
1173   GstVideoEncoder *encoder;
1174   GstVideoEncoderClass *encoder_class;
1175   GstStateChangeReturn ret;
1176
1177   encoder = GST_VIDEO_ENCODER (element);
1178   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1179
1180   switch (transition) {
1181     case GST_STATE_CHANGE_NULL_TO_READY:
1182       /* open device/library if needed */
1183       if (encoder_class->open && !encoder_class->open (encoder))
1184         goto open_failed;
1185       break;
1186     case GST_STATE_CHANGE_READY_TO_PAUSED:
1187       /* Initialize device/library if needed */
1188       if (encoder_class->start && !encoder_class->start (encoder))
1189         goto start_failed;
1190       break;
1191     default:
1192       break;
1193   }
1194
1195   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1196
1197   switch (transition) {
1198     case GST_STATE_CHANGE_PAUSED_TO_READY:
1199       gst_video_encoder_reset (encoder);
1200       if (encoder_class->stop && !encoder_class->stop (encoder))
1201         goto stop_failed;
1202       break;
1203     case GST_STATE_CHANGE_READY_TO_NULL:
1204       /* close device/library if needed */
1205       if (encoder_class->close && !encoder_class->close (encoder))
1206         goto close_failed;
1207       break;
1208     default:
1209       break;
1210   }
1211
1212   return ret;
1213
1214   /* Errors */
1215
1216 open_failed:
1217   {
1218     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1219         ("Failed to open encoder"));
1220     return GST_STATE_CHANGE_FAILURE;
1221   }
1222
1223 start_failed:
1224   {
1225     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1226         ("Failed to start encoder"));
1227     return GST_STATE_CHANGE_FAILURE;
1228   }
1229
1230 stop_failed:
1231   {
1232     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1233         ("Failed to stop encoder"));
1234     return GST_STATE_CHANGE_FAILURE;
1235   }
1236
1237 close_failed:
1238   {
1239     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1240         ("Failed to close encoder"));
1241     return GST_STATE_CHANGE_FAILURE;
1242   }
1243 }
1244
1245 static gboolean
1246 gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
1247 {
1248   gboolean ret;
1249   GstVideoCodecState *state = encoder->priv->output_state;
1250   GstVideoInfo *info = &state->info;
1251
1252   g_return_val_if_fail (state->caps != NULL, FALSE);
1253
1254   if (encoder->priv->output_state_changed) {
1255     state->caps = gst_caps_make_writable (state->caps);
1256
1257     /* Fill caps */
1258     gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1259         "height", G_TYPE_INT, info->height,
1260         "pixel-aspect-ratio", GST_TYPE_FRACTION,
1261         info->par_n, info->par_d, NULL);
1262     if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1263       /* variable fps with a max-framerate */
1264       gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1265           "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1266     } else {
1267       /* no variable fps or no max-framerate */
1268       gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1269           info->fps_n, info->fps_d, NULL);
1270     }
1271     if (state->codec_data)
1272       gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1273           state->codec_data, NULL);
1274     encoder->priv->output_state_changed = FALSE;
1275   }
1276
1277   ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1278
1279   return ret;
1280 }
1281
1282 /**
1283  * gst_video_encoder_finish_frame:
1284  * @encoder: a #GstVideoEncoder
1285  * @frame: (transfer full): an encoded #GstVideoCodecFrame 
1286  *
1287  * @frame must have a valid encoded data buffer, whose metadata fields
1288  * are then appropriately set according to frame data or no buffer at
1289  * all if the frame should be dropped.
1290  * It is subsequently pushed downstream or provided to @pre_push.
1291  * In any case, the frame is considered finished and released.
1292  *
1293  * Returns: a #GstFlowReturn resulting from sending data downstream
1294  *
1295  * Since: 0.10.36
1296  */
1297 GstFlowReturn
1298 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1299     GstVideoCodecFrame * frame)
1300 {
1301   GstVideoEncoderPrivate *priv = encoder->priv;
1302   GstFlowReturn ret = GST_FLOW_OK;
1303   GstVideoEncoderClass *encoder_class;
1304   GList *l;
1305   gboolean send_headers = FALSE;
1306   gboolean discont = (frame->presentation_frame_number == 0);
1307
1308   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1309
1310   GST_LOG_OBJECT (encoder,
1311       "finish frame fpn %d", frame->presentation_frame_number);
1312
1313   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1314
1315   if (G_UNLIKELY (priv->output_state_changed))
1316     gst_video_encoder_set_src_caps (encoder);
1317
1318   if (G_UNLIKELY (priv->output_state == NULL))
1319     goto no_output_state;
1320
1321   /* Push all pending events that arrived before this frame */
1322   for (l = priv->frames; l; l = l->next) {
1323     GstVideoCodecFrame *tmp = l->data;
1324
1325     if (tmp->events) {
1326       GList *k;
1327
1328       for (k = g_list_last (tmp->events); k; k = k->prev)
1329         gst_video_encoder_push_event (encoder, k->data);
1330       g_list_free (tmp->events);
1331       tmp->events = NULL;
1332     }
1333
1334     if (tmp == frame)
1335       break;
1336   }
1337
1338   /* no buffer data means this frame is skipped/dropped */
1339   if (!frame->output_buffer) {
1340     GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1341         GST_TIME_ARGS (frame->pts));
1342     goto done;
1343   }
1344
1345   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1346     GstClockTime stream_time, running_time;
1347     GstEvent *ev;
1348     ForcedKeyUnitEvent *fevt = NULL;
1349     GList *l;
1350
1351     running_time =
1352         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1353         frame->pts);
1354
1355     GST_OBJECT_LOCK (encoder);
1356     for (l = priv->force_key_unit; l; l = l->next) {
1357       ForcedKeyUnitEvent *tmp = l->data;
1358
1359       /* Skip non-pending keyunits */
1360       if (!tmp->pending)
1361         continue;
1362
1363       /* Simple case, keyunit ASAP */
1364       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1365         fevt = tmp;
1366         break;
1367       }
1368
1369       /* Event for before this frame */
1370       if (tmp->running_time <= running_time) {
1371         fevt = tmp;
1372         break;
1373       }
1374     }
1375
1376     if (fevt) {
1377       priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1378     }
1379     GST_OBJECT_UNLOCK (encoder);
1380
1381     if (fevt) {
1382       stream_time =
1383           gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1384           frame->pts);
1385
1386       ev = gst_video_event_new_downstream_force_key_unit
1387           (frame->pts, stream_time, running_time,
1388           fevt->all_headers, fevt->count);
1389
1390       gst_video_encoder_push_event (encoder, ev);
1391
1392       if (fevt->all_headers)
1393         send_headers = TRUE;
1394
1395       GST_DEBUG_OBJECT (encoder,
1396           "Forced key unit: running-time %" GST_TIME_FORMAT
1397           ", all_headers %d, count %u",
1398           GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1399       forced_key_unit_event_free (fevt);
1400     }
1401   }
1402
1403   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1404     priv->distance_from_sync = 0;
1405     GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1406     /* For keyframes, DTS = PTS */
1407     frame->dts = frame->pts;
1408   } else {
1409     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1410   }
1411
1412   frame->distance_from_sync = priv->distance_from_sync;
1413   priv->distance_from_sync++;
1414
1415   GST_BUFFER_TIMESTAMP (frame->output_buffer) = frame->pts;
1416   GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1417
1418   /* update rate estimate */
1419   priv->bytes += gst_buffer_get_size (frame->output_buffer);
1420   if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1421     priv->time += frame->duration;
1422   } else {
1423     /* better none than nothing valid */
1424     priv->time = GST_CLOCK_TIME_NONE;
1425   }
1426
1427   if (G_UNLIKELY (send_headers || priv->new_headers)) {
1428     GList *tmp, *copy = NULL;
1429
1430     GST_DEBUG_OBJECT (encoder, "Sending headers");
1431
1432     /* First make all buffers metadata-writable */
1433     for (tmp = priv->headers; tmp; tmp = tmp->next) {
1434       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1435
1436       copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1437     }
1438     g_list_free (priv->headers);
1439     priv->headers = copy;
1440
1441     for (tmp = priv->headers; tmp; tmp = tmp->next) {
1442       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1443
1444       gst_buffer_ref (tmpbuf);
1445       priv->bytes += gst_buffer_get_size (tmpbuf);
1446       if (G_UNLIKELY (discont)) {
1447         GST_LOG_OBJECT (encoder, "marking discont");
1448         GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1449         discont = FALSE;
1450       }
1451
1452       gst_pad_push (encoder->srcpad, tmpbuf);
1453     }
1454     priv->new_headers = FALSE;
1455   }
1456
1457   if (G_UNLIKELY (discont)) {
1458     GST_LOG_OBJECT (encoder, "marking discont");
1459     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1460   }
1461
1462   if (encoder_class->pre_push)
1463     ret = encoder_class->pre_push (encoder, frame);
1464
1465   if (ret == GST_FLOW_OK)
1466     ret = gst_pad_push (encoder->srcpad, frame->output_buffer);
1467
1468   frame->output_buffer = NULL;
1469
1470 done:
1471   /* handed out */
1472   priv->frames = g_list_remove (priv->frames, frame);
1473
1474   gst_video_codec_frame_unref (frame);
1475
1476   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1477
1478   return ret;
1479
1480   /* ERRORS */
1481 no_output_state:
1482   {
1483     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1484     GST_ERROR_OBJECT (encoder, "Output state was not configured");
1485     return GST_FLOW_ERROR;
1486   }
1487 }
1488
1489 /**
1490  * gst_video_encoder_get_output_state:
1491  * @encoder: a #GstVideoEncoder
1492  *
1493  * Get the current #GstVideoCodecState
1494  *
1495  * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1496  *
1497  * Since: 0.10.36
1498  */
1499 GstVideoCodecState *
1500 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1501 {
1502   GstVideoCodecState *state;
1503
1504   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1505   state = gst_video_codec_state_ref (encoder->priv->output_state);
1506   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1507
1508   return state;
1509 }
1510
1511 /**
1512  * gst_video_encoder_set_output_state:
1513  * @encoder: a #GstVideoEncoder
1514  * @caps: (transfer full): the #GstCaps to use for the output
1515  * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1516  *
1517  * Creates a new #GstVideoCodecState with the specified caps as the output state
1518  * for the encoder.
1519  * Any previously set output state on @decoder will be replaced by the newly
1520  * created one.
1521  *
1522  * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1523  * framerate, codec-data, .... Those should be specified instead in the returned
1524  * #GstVideoCodecState.
1525  *
1526  * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1527  * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1528  * @reference.
1529  *
1530  * If the subclass wishes to override some fields from the output state (like
1531  * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1532  *
1533  * The new output state will only take effect (set on pads and buffers) starting
1534  * from the next call to #gst_video_encoder_finish_frame().
1535  *
1536  * Returns: (transfer full): the newly configured output state.
1537  *
1538  * Since: 0.10.36
1539  */
1540 GstVideoCodecState *
1541 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1542     GstVideoCodecState * reference)
1543 {
1544   GstVideoEncoderPrivate *priv = encoder->priv;
1545   GstVideoCodecState *state;
1546
1547   g_return_val_if_fail (caps != NULL, NULL);
1548
1549   state = _new_output_state (caps, reference);
1550
1551   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1552   if (priv->output_state)
1553     gst_video_codec_state_unref (priv->output_state);
1554   priv->output_state = gst_video_codec_state_ref (state);
1555
1556   priv->output_state_changed = TRUE;
1557   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1558
1559   return state;
1560 }
1561
1562 /**
1563  * gst_video_encoder_set_latency:
1564  * @encoder: a #GstVideoEncoder
1565  * @min_latency: minimum latency
1566  * @max_latency: maximum latency
1567  *
1568  * Informs baseclass of encoding latency.
1569  *
1570  * Since: 0.10.36
1571  */
1572 void
1573 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
1574     GstClockTime min_latency, GstClockTime max_latency)
1575 {
1576   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
1577   g_return_if_fail (max_latency >= min_latency);
1578
1579   GST_OBJECT_LOCK (encoder);
1580   encoder->priv->min_latency = min_latency;
1581   encoder->priv->max_latency = max_latency;
1582   GST_OBJECT_UNLOCK (encoder);
1583
1584   gst_element_post_message (GST_ELEMENT_CAST (encoder),
1585       gst_message_new_latency (GST_OBJECT_CAST (encoder)));
1586 }
1587
1588 /**
1589  * gst_video_encoder_get_latency:
1590  * @encoder: a #GstVideoEncoder
1591  * @min_latency: (out) (allow-none): the configured minimum latency
1592  * @max_latency: (out) (allow-none): the configured maximum latency
1593  *
1594  * Returns the configured encoding latency.
1595  *
1596  * Since: 0.10.36
1597  */
1598 void
1599 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
1600     GstClockTime * min_latency, GstClockTime * max_latency)
1601 {
1602   GST_OBJECT_LOCK (encoder);
1603   if (min_latency)
1604     *min_latency = encoder->priv->min_latency;
1605   if (max_latency)
1606     *max_latency = encoder->priv->max_latency;
1607   GST_OBJECT_UNLOCK (encoder);
1608 }
1609
1610 /**
1611  * gst_video_encoder_get_oldest_frame:
1612  * @encoder: a #GstVideoEncoder
1613  *
1614  * Get the oldest unfinished pending #GstVideoCodecFrame
1615  *
1616  * Returns: oldest unfinished pending #GstVideoCodecFrame
1617  *
1618  * Since: 0.10.36
1619  */
1620 GstVideoCodecFrame *
1621 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
1622 {
1623   GList *g;
1624
1625   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1626   g = encoder->priv->frames;
1627   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1628
1629   if (g == NULL)
1630     return NULL;
1631   return (GstVideoCodecFrame *) (g->data);
1632 }
1633
1634 /**
1635  * gst_video_encoder_get_frame:
1636  * @encoder: a #GstVideoEnccoder
1637  * @frame_number: system_frame_number of a frame
1638  *
1639  * Get a pending unfinished #GstVideoCodecFrame
1640  * 
1641  * Returns: (transfer none): pending unfinished #GstVideoCodecFrame identified by @frame_number.
1642  *
1643  * Since: 0.10.36
1644  */
1645 GstVideoCodecFrame *
1646 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
1647 {
1648   GList *g;
1649   GstVideoCodecFrame *frame = NULL;
1650
1651   GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
1652
1653   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1654   for (g = encoder->priv->frames; g; g = g->next) {
1655     GstVideoCodecFrame *tmp = g->data;
1656
1657     if (tmp->system_frame_number == frame_number) {
1658       frame = tmp;
1659       break;
1660     }
1661   }
1662   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1663
1664   return frame;
1665 }