videoencoder: Make sure to push any pre-caps events before the caps are set
[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   gboolean do_caps;
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   guint32 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   GstAllocator *allocator;
164   GstAllocationParams params;
165
166   GstTagList *tags;
167   gboolean tags_changed;
168 };
169
170 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
171 struct _ForcedKeyUnitEvent
172 {
173   GstClockTime running_time;
174   gboolean pending;             /* TRUE if this was requested already */
175   gboolean all_headers;
176   guint count;
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
235 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
236  * method to get to the padtemplates */
237 GType
238 gst_video_encoder_get_type (void)
239 {
240   static volatile gsize type = 0;
241
242   if (g_once_init_enter (&type)) {
243     GType _type;
244     static const GTypeInfo info = {
245       sizeof (GstVideoEncoderClass),
246       NULL,
247       NULL,
248       (GClassInitFunc) gst_video_encoder_class_init,
249       NULL,
250       NULL,
251       sizeof (GstVideoEncoder),
252       0,
253       (GInstanceInitFunc) gst_video_encoder_init,
254     };
255     const GInterfaceInfo preset_interface_info = {
256       NULL,                     /* interface_init */
257       NULL,                     /* interface_finalize */
258       NULL                      /* interface_data */
259     };
260
261     _type = g_type_register_static (GST_TYPE_ELEMENT,
262         "GstVideoEncoder", &info, G_TYPE_FLAG_ABSTRACT);
263     g_type_add_interface_static (_type, GST_TYPE_PRESET,
264         &preset_interface_info);
265     g_once_init_leave (&type, _type);
266   }
267   return type;
268 }
269
270 static void
271 gst_video_encoder_class_init (GstVideoEncoderClass * klass)
272 {
273   GObjectClass *gobject_class;
274   GstElementClass *gstelement_class;
275
276   gobject_class = G_OBJECT_CLASS (klass);
277   gstelement_class = GST_ELEMENT_CLASS (klass);
278
279   GST_DEBUG_CATEGORY_INIT (videoencoder_debug, "videoencoder", 0,
280       "Base Video Encoder");
281
282   parent_class = g_type_class_peek_parent (klass);
283
284   g_type_class_add_private (klass, sizeof (GstVideoEncoderPrivate));
285
286   gobject_class->finalize = gst_video_encoder_finalize;
287
288   gstelement_class->change_state =
289       GST_DEBUG_FUNCPTR (gst_video_encoder_change_state);
290
291   klass->sink_event = gst_video_encoder_sink_event_default;
292   klass->src_event = gst_video_encoder_src_event_default;
293   klass->propose_allocation = gst_video_encoder_propose_allocation_default;
294   klass->decide_allocation = gst_video_encoder_decide_allocation_default;
295   klass->negotiate = gst_video_encoder_negotiate_default;
296 }
297
298 static void
299 gst_video_encoder_reset (GstVideoEncoder * encoder)
300 {
301   GstVideoEncoderPrivate *priv = encoder->priv;
302   GList *g;
303
304   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
305
306   priv->presentation_frame_number = 0;
307   priv->distance_from_sync = 0;
308
309   g_list_foreach (priv->force_key_unit, (GFunc) forced_key_unit_event_free,
310       NULL);
311   g_list_free (priv->force_key_unit);
312   priv->force_key_unit = NULL;
313
314   priv->drained = TRUE;
315   priv->min_latency = 0;
316   priv->max_latency = 0;
317
318   g_list_foreach (priv->headers, (GFunc) gst_event_unref, NULL);
319   g_list_free (priv->headers);
320   priv->headers = NULL;
321   priv->new_headers = FALSE;
322
323   g_list_foreach (priv->current_frame_events, (GFunc) gst_event_unref, NULL);
324   g_list_free (priv->current_frame_events);
325   priv->current_frame_events = NULL;
326
327   for (g = priv->frames; g; g = g->next) {
328     gst_video_codec_frame_unref ((GstVideoCodecFrame *) g->data);
329   }
330   g_list_free (priv->frames);
331   priv->frames = NULL;
332
333   priv->bytes = 0;
334   priv->time = 0;
335
336   if (priv->input_state)
337     gst_video_codec_state_unref (priv->input_state);
338   priv->input_state = NULL;
339   if (priv->output_state)
340     gst_video_codec_state_unref (priv->output_state);
341   priv->output_state = NULL;
342
343   if (priv->tags)
344     gst_tag_list_unref (priv->tags);
345   priv->tags = NULL;
346   priv->tags_changed = FALSE;
347
348   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
349 }
350
351 static void
352 gst_video_encoder_init (GstVideoEncoder * encoder, GstVideoEncoderClass * klass)
353 {
354   GstVideoEncoderPrivate *priv;
355   GstPadTemplate *pad_template;
356   GstPad *pad;
357
358   GST_DEBUG_OBJECT (encoder, "gst_video_encoder_init");
359
360   priv = encoder->priv = GST_VIDEO_ENCODER_GET_PRIVATE (encoder);
361
362   pad_template =
363       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
364   g_return_if_fail (pad_template != NULL);
365
366   encoder->sinkpad = pad = gst_pad_new_from_template (pad_template, "sink");
367
368   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_video_encoder_chain));
369   gst_pad_set_event_function (pad,
370       GST_DEBUG_FUNCPTR (gst_video_encoder_sink_event));
371   gst_pad_set_query_function (pad,
372       GST_DEBUG_FUNCPTR (gst_video_encoder_sink_query));
373   gst_element_add_pad (GST_ELEMENT (encoder), encoder->sinkpad);
374
375   pad_template =
376       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
377   g_return_if_fail (pad_template != NULL);
378
379   encoder->srcpad = pad = gst_pad_new_from_template (pad_template, "src");
380
381   gst_pad_set_query_function (pad,
382       GST_DEBUG_FUNCPTR (gst_video_encoder_src_query));
383   gst_pad_set_event_function (pad,
384       GST_DEBUG_FUNCPTR (gst_video_encoder_src_event));
385   gst_element_add_pad (GST_ELEMENT (encoder), encoder->srcpad);
386
387   gst_segment_init (&encoder->input_segment, GST_FORMAT_TIME);
388   gst_segment_init (&encoder->output_segment, GST_FORMAT_TIME);
389
390   g_rec_mutex_init (&encoder->stream_lock);
391
392   priv->at_eos = FALSE;
393   priv->headers = NULL;
394   priv->new_headers = FALSE;
395
396   gst_video_encoder_reset (encoder);
397 }
398
399 static gboolean
400 gst_video_encoded_video_convert (gint64 bytes, gint64 time,
401     GstFormat src_format, gint64 src_value, GstFormat * dest_format,
402     gint64 * dest_value)
403 {
404   gboolean res = FALSE;
405
406   g_return_val_if_fail (dest_format != NULL, FALSE);
407   g_return_val_if_fail (dest_value != NULL, FALSE);
408
409   if (G_UNLIKELY (src_format == *dest_format || src_value == 0 ||
410           src_value == -1)) {
411     if (dest_value)
412       *dest_value = src_value;
413     return TRUE;
414   }
415
416   if (bytes <= 0 || time <= 0) {
417     GST_DEBUG ("not enough metadata yet to convert");
418     goto exit;
419   }
420
421   switch (src_format) {
422     case GST_FORMAT_BYTES:
423       switch (*dest_format) {
424         case GST_FORMAT_TIME:
425           *dest_value = gst_util_uint64_scale (src_value, time, bytes);
426           res = TRUE;
427           break;
428         default:
429           res = FALSE;
430       }
431       break;
432     case GST_FORMAT_TIME:
433       switch (*dest_format) {
434         case GST_FORMAT_BYTES:
435           *dest_value = gst_util_uint64_scale (src_value, bytes, time);
436           res = TRUE;
437           break;
438         default:
439           res = FALSE;
440       }
441       break;
442     default:
443       GST_DEBUG ("unhandled conversion from %d to %d", src_format,
444           *dest_format);
445       res = FALSE;
446   }
447
448 exit:
449   return res;
450 }
451
452 /**
453  * gst_video_encoder_set_headers:
454  * @encoder: a #GstVideoEncoder
455  * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
456  *
457  * Set the codec headers to be sent downstream whenever requested.
458  */
459 void
460 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
461 {
462   GST_VIDEO_ENCODER_STREAM_LOCK (video_encoder);
463
464   GST_DEBUG_OBJECT (video_encoder, "new headers %p", headers);
465   if (video_encoder->priv->headers) {
466     g_list_foreach (video_encoder->priv->headers, (GFunc) gst_buffer_unref,
467         NULL);
468     g_list_free (video_encoder->priv->headers);
469   }
470   video_encoder->priv->headers = headers;
471   video_encoder->priv->new_headers = TRUE;
472
473   GST_VIDEO_ENCODER_STREAM_UNLOCK (video_encoder);
474 }
475
476 static gboolean
477 gst_video_encoder_drain (GstVideoEncoder * enc)
478 {
479   GstVideoEncoderPrivate *priv;
480   GstVideoEncoderClass *enc_class;
481   gboolean ret = TRUE;
482
483   enc_class = GST_VIDEO_ENCODER_GET_CLASS (enc);
484   priv = enc->priv;
485
486   GST_DEBUG_OBJECT (enc, "draining");
487
488   if (priv->drained) {
489     GST_DEBUG_OBJECT (enc, "already drained");
490     return TRUE;
491   }
492
493   if (enc_class->reset) {
494     GST_DEBUG_OBJECT (enc, "requesting subclass to finish");
495     ret = enc_class->reset (enc, TRUE);
496   }
497   /* everything should be away now */
498   if (priv->frames) {
499     /* not fatal/impossible though if subclass/enc eats stuff */
500     g_list_foreach (priv->frames, (GFunc) gst_video_codec_frame_unref, NULL);
501     g_list_free (priv->frames);
502     priv->frames = NULL;
503   }
504
505   return ret;
506 }
507
508 static GstVideoCodecState *
509 _new_output_state (GstCaps * caps, GstVideoCodecState * reference)
510 {
511   GstVideoCodecState *state;
512
513   state = g_slice_new0 (GstVideoCodecState);
514   state->ref_count = 1;
515   gst_video_info_init (&state->info);
516   gst_video_info_set_format (&state->info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
517
518   state->caps = caps;
519
520   if (reference) {
521     GstVideoInfo *tgt, *ref;
522
523     tgt = &state->info;
524     ref = &reference->info;
525
526     /* Copy over extra fields from reference state */
527     tgt->interlace_mode = ref->interlace_mode;
528     tgt->flags = ref->flags;
529     tgt->width = ref->width;
530     tgt->height = ref->height;
531     tgt->chroma_site = ref->chroma_site;
532     tgt->colorimetry = ref->colorimetry;
533     tgt->par_n = ref->par_n;
534     tgt->par_d = ref->par_d;
535     tgt->fps_n = ref->fps_n;
536     tgt->fps_d = ref->fps_d;
537   }
538
539   return state;
540 }
541
542 static GstVideoCodecState *
543 _new_input_state (GstCaps * caps)
544 {
545   GstVideoCodecState *state;
546
547   state = g_slice_new0 (GstVideoCodecState);
548   state->ref_count = 1;
549   gst_video_info_init (&state->info);
550   if (G_UNLIKELY (!gst_video_info_from_caps (&state->info, caps)))
551     goto parse_fail;
552   state->caps = gst_caps_ref (caps);
553
554   return state;
555
556 parse_fail:
557   {
558     g_slice_free (GstVideoCodecState, state);
559     return NULL;
560   }
561 }
562
563 static gboolean
564 gst_video_encoder_setcaps (GstVideoEncoder * encoder, GstCaps * caps)
565 {
566   GstVideoEncoderClass *encoder_class;
567   GstVideoCodecState *state;
568   gboolean ret;
569
570   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
571
572   /* subclass should do something here ... */
573   g_return_val_if_fail (encoder_class->set_format != NULL, FALSE);
574
575   GST_DEBUG_OBJECT (encoder, "setcaps %" GST_PTR_FORMAT, caps);
576
577   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
578
579   if (encoder->priv->input_state) {
580     GST_DEBUG_OBJECT (encoder,
581         "Checking if caps changed old %" GST_PTR_FORMAT " new %" GST_PTR_FORMAT,
582         encoder->priv->input_state->caps, caps);
583     if (gst_caps_is_equal (encoder->priv->input_state->caps, caps))
584       goto caps_not_changed;
585   }
586
587   state = _new_input_state (caps);
588   if (G_UNLIKELY (!state))
589     goto parse_fail;
590
591   if (encoder->priv->input_state
592       && gst_video_info_is_equal (&state->info,
593           &encoder->priv->input_state->info)) {
594     gst_video_codec_state_unref (state);
595     goto caps_not_changed;
596   }
597
598   /* arrange draining pending frames */
599   gst_video_encoder_drain (encoder);
600
601   /* and subclass should be ready to configure format at any time around */
602   ret = encoder_class->set_format (encoder, state);
603   if (ret) {
604     if (encoder->priv->input_state)
605       gst_video_codec_state_unref (encoder->priv->input_state);
606     encoder->priv->input_state = state;
607   } else {
608     gst_video_codec_state_unref (state);
609   }
610
611   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
612
613   if (!ret)
614     GST_WARNING_OBJECT (encoder, "rejected caps %" GST_PTR_FORMAT, caps);
615
616   return ret;
617
618 caps_not_changed:
619   {
620     GST_DEBUG_OBJECT (encoder, "Caps did not change - ignore");
621     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
622     return TRUE;
623   }
624
625   /* ERRORS */
626 parse_fail:
627   {
628     GST_WARNING_OBJECT (encoder, "Failed to parse caps");
629     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
630     return FALSE;
631   }
632 }
633
634 /**
635  * gst_video_encoder_proxy_getcaps:
636  * @enc: a #GstVideoEncoder
637  * @caps: initial caps
638  * @filter: filter caps
639  *
640  * Returns caps that express @caps (or sink template caps if @caps == NULL)
641  * restricted to resolution/format/... combinations supported by downstream
642  * elements (e.g. muxers).
643  *
644  * Returns: a #GstCaps owned by caller
645  */
646 GstCaps *
647 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
648     GstCaps * filter)
649 {
650   GstCaps *templ_caps;
651   GstCaps *allowed;
652   GstCaps *fcaps, *filter_caps;
653   gint i, j;
654
655   /* Allow downstream to specify width/height/framerate/PAR constraints
656    * and forward them upstream for video converters to handle
657    */
658   templ_caps =
659       caps ? gst_caps_ref (caps) :
660       gst_pad_get_pad_template_caps (encoder->sinkpad);
661   allowed = gst_pad_get_allowed_caps (encoder->srcpad);
662
663   if (!allowed || gst_caps_is_empty (allowed) || gst_caps_is_any (allowed)) {
664     fcaps = templ_caps;
665     goto done;
666   }
667
668   GST_LOG_OBJECT (encoder, "template caps %" GST_PTR_FORMAT, templ_caps);
669   GST_LOG_OBJECT (encoder, "allowed caps %" GST_PTR_FORMAT, allowed);
670
671   filter_caps = gst_caps_new_empty ();
672
673   for (i = 0; i < gst_caps_get_size (templ_caps); i++) {
674     GQuark q_name =
675         gst_structure_get_name_id (gst_caps_get_structure (templ_caps, i));
676
677     for (j = 0; j < gst_caps_get_size (allowed); j++) {
678       const GstStructure *allowed_s = gst_caps_get_structure (allowed, j);
679       const GValue *val;
680       GstStructure *s;
681
682       s = gst_structure_new_id_empty (q_name);
683       if ((val = gst_structure_get_value (allowed_s, "width")))
684         gst_structure_set_value (s, "width", val);
685       if ((val = gst_structure_get_value (allowed_s, "height")))
686         gst_structure_set_value (s, "height", val);
687       if ((val = gst_structure_get_value (allowed_s, "framerate")))
688         gst_structure_set_value (s, "framerate", val);
689       if ((val = gst_structure_get_value (allowed_s, "pixel-aspect-ratio")))
690         gst_structure_set_value (s, "pixel-aspect-ratio", val);
691
692       filter_caps = gst_caps_merge_structure (filter_caps, s);
693     }
694   }
695
696   fcaps = gst_caps_intersect (filter_caps, templ_caps);
697   gst_caps_unref (filter_caps);
698   gst_caps_unref (templ_caps);
699
700   if (filter) {
701     GST_LOG_OBJECT (encoder, "intersecting with %" GST_PTR_FORMAT, filter);
702     filter_caps = gst_caps_intersect (fcaps, filter);
703     gst_caps_unref (fcaps);
704     fcaps = filter_caps;
705   }
706
707 done:
708   gst_caps_replace (&allowed, NULL);
709
710   GST_LOG_OBJECT (encoder, "proxy caps %" GST_PTR_FORMAT, fcaps);
711
712   return fcaps;
713 }
714
715 static GstCaps *
716 gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
717 {
718   GstVideoEncoderClass *klass;
719   GstCaps *caps;
720
721   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
722
723   if (klass->getcaps)
724     caps = klass->getcaps (encoder, filter);
725   else
726     caps = gst_video_encoder_proxy_getcaps (encoder, NULL, filter);
727
728   GST_LOG_OBJECT (encoder, "Returning caps %" GST_PTR_FORMAT, caps);
729
730   return caps;
731 }
732
733 static gboolean
734 gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
735     GstQuery * query)
736 {
737   GstAllocator *allocator = NULL;
738   GstAllocationParams params;
739   gboolean update_allocator;
740
741   /* we got configuration from our peer or the decide_allocation method,
742    * parse them */
743   if (gst_query_get_n_allocation_params (query) > 0) {
744     /* try the allocator */
745     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
746     update_allocator = TRUE;
747   } else {
748     allocator = NULL;
749     gst_allocation_params_init (&params);
750     update_allocator = FALSE;
751   }
752
753   if (update_allocator)
754     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
755   else
756     gst_query_add_allocation_param (query, allocator, &params);
757   if (allocator)
758     gst_object_unref (allocator);
759
760   return TRUE;
761 }
762
763 static gboolean
764 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
765     GstQuery * query)
766 {
767   GstCaps *caps;
768   GstVideoInfo info;
769   GstBufferPool *pool;
770   guint size;
771
772   gst_query_parse_allocation (query, &caps, NULL);
773
774   if (caps == NULL)
775     return FALSE;
776
777   if (!gst_video_info_from_caps (&info, caps))
778     return FALSE;
779
780   size = GST_VIDEO_INFO_SIZE (&info);
781
782   if (gst_query_get_n_allocation_pools (query) == 0) {
783     GstStructure *structure;
784     GstAllocator *allocator = NULL;
785     GstAllocationParams params = { 0, 15, 0, 0 };
786
787     if (gst_query_get_n_allocation_params (query) > 0)
788       gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
789     else
790       gst_query_add_allocation_param (query, allocator, &params);
791
792     pool = gst_video_buffer_pool_new ();
793
794     structure = gst_buffer_pool_get_config (pool);
795     gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
796     gst_buffer_pool_config_set_allocator (structure, allocator, &params);
797
798     if (allocator)
799       gst_object_unref (allocator);
800
801     if (!gst_buffer_pool_set_config (pool, structure))
802       goto config_failed;
803
804     gst_query_add_allocation_pool (query, pool, size, 0, 0);
805     gst_object_unref (pool);
806     gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
807   }
808
809   return TRUE;
810
811   /* ERRORS */
812 config_failed:
813   {
814     GST_ERROR_OBJECT (encoder, "failed to set config");
815     gst_object_unref (pool);
816     return FALSE;
817   }
818 }
819
820 static gboolean
821 gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
822     GstQuery * query)
823 {
824   GstVideoEncoder *encoder;
825   gboolean res = FALSE;
826
827   encoder = GST_VIDEO_ENCODER (parent);
828
829   switch (GST_QUERY_TYPE (query)) {
830     case GST_QUERY_CAPS:
831     {
832       GstCaps *filter, *caps;
833
834       gst_query_parse_caps (query, &filter);
835       caps = gst_video_encoder_sink_getcaps (encoder, filter);
836       gst_query_set_caps_result (query, caps);
837       gst_caps_unref (caps);
838       res = TRUE;
839       break;
840     }
841     case GST_QUERY_ALLOCATION:
842     {
843       GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
844
845       if (klass->propose_allocation)
846         res = klass->propose_allocation (encoder, query);
847       break;
848     }
849     default:
850       res = gst_pad_query_default (pad, parent, query);
851       break;
852   }
853   return res;
854 }
855
856 static void
857 gst_video_encoder_finalize (GObject * object)
858 {
859   GstVideoEncoder *encoder;
860
861   GST_DEBUG_OBJECT (object, "finalize");
862
863   encoder = GST_VIDEO_ENCODER (object);
864   if (encoder->priv->headers) {
865     g_list_foreach (encoder->priv->headers, (GFunc) gst_buffer_unref, NULL);
866     g_list_free (encoder->priv->headers);
867   }
868   g_rec_mutex_clear (&encoder->stream_lock);
869
870   if (encoder->priv->allocator) {
871     gst_object_unref (encoder->priv->allocator);
872     encoder->priv->allocator = NULL;
873   }
874
875   G_OBJECT_CLASS (parent_class)->finalize (object);
876 }
877
878 static gboolean
879 gst_video_encoder_push_event (GstVideoEncoder * encoder, GstEvent * event)
880 {
881   switch (GST_EVENT_TYPE (event)) {
882     case GST_EVENT_SEGMENT:
883     {
884       GstSegment segment;
885
886       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
887
888       gst_event_copy_segment (event, &segment);
889
890       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
891
892       if (segment.format != GST_FORMAT_TIME) {
893         GST_DEBUG_OBJECT (encoder, "received non TIME segment");
894         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
895         break;
896       }
897
898       encoder->output_segment = segment;
899       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
900       break;
901     }
902     default:
903       break;
904   }
905
906   return gst_pad_push_event (encoder->srcpad, event);
907 }
908
909 static gboolean
910 gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
911     GstEvent * event)
912 {
913   GstVideoEncoderClass *encoder_class;
914   gboolean ret = FALSE;
915
916   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
917
918   switch (GST_EVENT_TYPE (event)) {
919     case GST_EVENT_CAPS:
920     {
921       GstCaps *caps;
922
923       gst_event_parse_caps (event, &caps);
924       ret = TRUE;
925       encoder->priv->do_caps = TRUE;
926       gst_event_unref (event);
927       event = NULL;
928       break;
929     }
930     case GST_EVENT_EOS:
931     {
932       GstFlowReturn flow_ret;
933
934       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
935       encoder->priv->at_eos = TRUE;
936
937       if (encoder_class->finish) {
938         flow_ret = encoder_class->finish (encoder);
939       } else {
940         flow_ret = GST_FLOW_OK;
941       }
942
943       ret = (flow_ret == GST_FLOW_OK);
944       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
945       break;
946     }
947     case GST_EVENT_SEGMENT:
948     {
949       GstSegment segment;
950
951       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
952
953       gst_event_copy_segment (event, &segment);
954
955       GST_DEBUG_OBJECT (encoder, "segment %" GST_SEGMENT_FORMAT, &segment);
956
957       if (segment.format != GST_FORMAT_TIME) {
958         GST_DEBUG_OBJECT (encoder, "received non TIME newsegment");
959         GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
960         break;
961       }
962
963       encoder->priv->at_eos = FALSE;
964
965       encoder->input_segment = segment;
966       ret = TRUE;
967       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
968       break;
969     }
970     case GST_EVENT_CUSTOM_DOWNSTREAM:
971     {
972       if (gst_video_event_is_force_key_unit (event)) {
973         GstClockTime running_time;
974         gboolean all_headers;
975         guint count;
976
977         if (gst_video_event_parse_downstream_force_key_unit (event,
978                 NULL, NULL, &running_time, &all_headers, &count)) {
979           ForcedKeyUnitEvent *fevt;
980
981           GST_OBJECT_LOCK (encoder);
982           fevt = forced_key_unit_event_new (running_time, all_headers, count);
983           encoder->priv->force_key_unit =
984               g_list_append (encoder->priv->force_key_unit, fevt);
985           GST_OBJECT_UNLOCK (encoder);
986
987           GST_DEBUG_OBJECT (encoder,
988               "force-key-unit event: running-time %" GST_TIME_FORMAT
989               ", all_headers %d, count %u",
990               GST_TIME_ARGS (running_time), all_headers, count);
991         }
992         gst_event_unref (event);
993         event = NULL;
994         ret = TRUE;
995       }
996       break;
997     }
998     case GST_EVENT_TAG:
999     {
1000       GstTagList *tags;
1001
1002       gst_event_parse_tag (event, &tags);
1003
1004       if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
1005         tags = gst_tag_list_copy (tags);
1006
1007         /* FIXME: make generic based on GST_TAG_FLAG_ENCODED */
1008         gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
1009         gst_tag_list_remove_tag (tags, GST_TAG_AUDIO_CODEC);
1010         gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
1011         gst_tag_list_remove_tag (tags, GST_TAG_SUBTITLE_CODEC);
1012         gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1013         gst_tag_list_remove_tag (tags, GST_TAG_BITRATE);
1014         gst_tag_list_remove_tag (tags, GST_TAG_NOMINAL_BITRATE);
1015         gst_tag_list_remove_tag (tags, GST_TAG_MAXIMUM_BITRATE);
1016         gst_tag_list_remove_tag (tags, GST_TAG_MINIMUM_BITRATE);
1017         gst_tag_list_remove_tag (tags, GST_TAG_ENCODER);
1018         gst_tag_list_remove_tag (tags, GST_TAG_ENCODER_VERSION);
1019
1020         gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
1021         gst_tag_list_unref (tags);
1022         gst_event_unref (event);
1023         event = NULL;
1024         ret = TRUE;
1025       }
1026       break;
1027     }
1028     default:
1029       break;
1030   }
1031
1032   /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1033    * For EOS this is required because no buffer or serialized event
1034    * will come after EOS and nothing could trigger another
1035    * _finish_frame() call.   *
1036    * If the subclass handles sending of EOS manually it can simply
1037    * not chain up to the parent class' event handler
1038    *
1039    * For FLUSH_STOP this is required because it is expected
1040    * to be forwarded immediately and no buffers are queued anyway.
1041    */
1042   if (event) {
1043     if (!GST_EVENT_IS_SERIALIZED (event)
1044         || GST_EVENT_TYPE (event) == GST_EVENT_EOS
1045         || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
1046       ret = gst_video_encoder_push_event (encoder, event);
1047     } else {
1048       GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1049       encoder->priv->current_frame_events =
1050           g_list_prepend (encoder->priv->current_frame_events, event);
1051       GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1052       ret = TRUE;
1053     }
1054   }
1055
1056   return ret;
1057 }
1058
1059 static gboolean
1060 gst_video_encoder_sink_event (GstPad * pad, GstObject * parent,
1061     GstEvent * event)
1062 {
1063   GstVideoEncoder *enc;
1064   GstVideoEncoderClass *klass;
1065   gboolean ret = TRUE;
1066
1067   enc = GST_VIDEO_ENCODER (parent);
1068   klass = GST_VIDEO_ENCODER_GET_CLASS (enc);
1069
1070   GST_DEBUG_OBJECT (enc, "received event %d, %s", GST_EVENT_TYPE (event),
1071       GST_EVENT_TYPE_NAME (event));
1072
1073   if (klass->sink_event)
1074     ret = klass->sink_event (enc, event);
1075
1076   return ret;
1077 }
1078
1079 static gboolean
1080 gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
1081     GstEvent * event)
1082 {
1083   gboolean ret = FALSE;
1084
1085   switch (GST_EVENT_TYPE (event)) {
1086     case GST_EVENT_CUSTOM_UPSTREAM:
1087     {
1088       if (gst_video_event_is_force_key_unit (event)) {
1089         GstClockTime running_time;
1090         gboolean all_headers;
1091         guint count;
1092
1093         if (gst_video_event_parse_upstream_force_key_unit (event,
1094                 &running_time, &all_headers, &count)) {
1095           ForcedKeyUnitEvent *fevt;
1096
1097           GST_OBJECT_LOCK (encoder);
1098           fevt = forced_key_unit_event_new (running_time, all_headers, count);
1099           encoder->priv->force_key_unit =
1100               g_list_append (encoder->priv->force_key_unit, fevt);
1101           GST_OBJECT_UNLOCK (encoder);
1102
1103           GST_DEBUG_OBJECT (encoder,
1104               "force-key-unit event: running-time %" GST_TIME_FORMAT
1105               ", all_headers %d, count %u",
1106               GST_TIME_ARGS (running_time), all_headers, count);
1107         }
1108         gst_event_unref (event);
1109         event = NULL;
1110         ret = TRUE;
1111       }
1112       break;
1113     }
1114     default:
1115       break;
1116   }
1117
1118   if (event)
1119     ret =
1120         gst_pad_event_default (encoder->srcpad, GST_OBJECT_CAST (encoder),
1121         event);
1122
1123   return ret;
1124 }
1125
1126 static gboolean
1127 gst_video_encoder_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1128 {
1129   GstVideoEncoder *encoder;
1130   GstVideoEncoderClass *klass;
1131   gboolean ret = FALSE;
1132
1133   encoder = GST_VIDEO_ENCODER (parent);
1134   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1135
1136   GST_LOG_OBJECT (encoder, "handling event: %" GST_PTR_FORMAT, event);
1137
1138   if (klass->src_event)
1139     ret = klass->src_event (encoder, event);
1140
1141   return ret;
1142 }
1143
1144 static gboolean
1145 gst_video_encoder_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1146 {
1147   GstVideoEncoderPrivate *priv;
1148   GstVideoEncoder *enc;
1149   gboolean res;
1150
1151   enc = GST_VIDEO_ENCODER (parent);
1152   priv = enc->priv;
1153
1154   GST_LOG_OBJECT (enc, "handling query: %" GST_PTR_FORMAT, query);
1155
1156   switch (GST_QUERY_TYPE (query)) {
1157     case GST_QUERY_CONVERT:
1158     {
1159       GstFormat src_fmt, dest_fmt;
1160       gint64 src_val, dest_val;
1161
1162       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1163       res =
1164           gst_video_encoded_video_convert (priv->bytes, priv->time, src_fmt,
1165           src_val, &dest_fmt, &dest_val);
1166       if (!res)
1167         goto error;
1168       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1169       break;
1170     }
1171     case GST_QUERY_LATENCY:
1172     {
1173       gboolean live;
1174       GstClockTime min_latency, max_latency;
1175
1176       res = gst_pad_peer_query (enc->sinkpad, query);
1177       if (res) {
1178         gst_query_parse_latency (query, &live, &min_latency, &max_latency);
1179         GST_DEBUG_OBJECT (enc, "Peer latency: live %d, min %"
1180             GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
1181             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1182
1183         GST_OBJECT_LOCK (enc);
1184         min_latency += priv->min_latency;
1185         if (enc->priv->max_latency == GST_CLOCK_TIME_NONE) {
1186           max_latency = GST_CLOCK_TIME_NONE;
1187         } else if (max_latency != GST_CLOCK_TIME_NONE) {
1188           max_latency += enc->priv->max_latency;
1189         }
1190         GST_OBJECT_UNLOCK (enc);
1191
1192         gst_query_set_latency (query, live, min_latency, max_latency);
1193       }
1194     }
1195       break;
1196     default:
1197       res = gst_pad_query_default (pad, parent, query);
1198   }
1199   return res;
1200
1201 error:
1202   GST_DEBUG_OBJECT (enc, "query failed");
1203   return res;
1204 }
1205
1206 static GstVideoCodecFrame *
1207 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
1208     GstClockTime pts, GstClockTime dts, GstClockTime duration)
1209 {
1210   GstVideoEncoderPrivate *priv = encoder->priv;
1211   GstVideoCodecFrame *frame;
1212
1213   frame = g_slice_new0 (GstVideoCodecFrame);
1214
1215   frame->ref_count = 1;
1216
1217   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1218   frame->system_frame_number = priv->system_frame_number;
1219   priv->system_frame_number++;
1220
1221   frame->presentation_frame_number = priv->presentation_frame_number;
1222   priv->presentation_frame_number++;
1223   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1224
1225   frame->events = priv->current_frame_events;
1226   priv->current_frame_events = NULL;
1227   frame->input_buffer = buf;
1228   frame->pts = pts;
1229   frame->dts = dts;
1230   frame->duration = duration;
1231   frame->abidata.ABI.ts = pts;
1232
1233   return frame;
1234 }
1235
1236
1237 static GstFlowReturn
1238 gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1239 {
1240   GstVideoEncoder *encoder;
1241   GstVideoEncoderPrivate *priv;
1242   GstVideoEncoderClass *klass;
1243   GstVideoCodecFrame *frame;
1244   GstClockTime pts, duration;
1245   GstFlowReturn ret = GST_FLOW_OK;
1246   guint64 start, stop, cstart, cstop;
1247
1248   encoder = GST_VIDEO_ENCODER (parent);
1249   priv = encoder->priv;
1250   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1251
1252   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
1253
1254   if (G_UNLIKELY (encoder->priv->do_caps)) {
1255     GstCaps *caps = gst_pad_get_current_caps (encoder->sinkpad);
1256     if (!caps)
1257       goto not_negotiated;
1258     if (!gst_video_encoder_setcaps (encoder, caps)) {
1259       gst_caps_unref (caps);
1260       goto not_negotiated;
1261     }
1262     gst_caps_unref (caps);
1263     encoder->priv->do_caps = FALSE;
1264   }
1265
1266   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1267
1268   pts = GST_BUFFER_PTS (buf);
1269   duration = GST_BUFFER_DURATION (buf);
1270
1271   GST_LOG_OBJECT (encoder,
1272       "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
1273       ", DTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1274       gst_buffer_get_size (buf), GST_TIME_ARGS (pts),
1275       GST_TIME_ARGS (GST_BUFFER_DTS (buf)), GST_TIME_ARGS (duration));
1276
1277   if (priv->at_eos) {
1278     ret = GST_FLOW_EOS;
1279     goto done;
1280   }
1281
1282   start = pts;
1283   if (GST_CLOCK_TIME_IS_VALID (duration))
1284     stop = start + duration;
1285   else
1286     stop = GST_CLOCK_TIME_NONE;
1287
1288   /* Drop buffers outside of segment */
1289   if (!gst_segment_clip (&encoder->input_segment,
1290           GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
1291     GST_DEBUG_OBJECT (encoder, "clipping to segment dropped frame");
1292     gst_buffer_unref (buf);
1293     goto done;
1294   }
1295
1296   /* incoming DTS is not really relevant and does not make sense anyway,
1297    * so pass along _NONE and maybe come up with something better later on */
1298   frame = gst_video_encoder_new_frame (encoder, buf, cstart,
1299       GST_CLOCK_TIME_NONE, cstop - cstart);
1300
1301   GST_OBJECT_LOCK (encoder);
1302   if (priv->force_key_unit) {
1303     ForcedKeyUnitEvent *fevt = NULL;
1304     GstClockTime running_time;
1305     GList *l;
1306
1307     running_time =
1308         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1309         cstart);
1310
1311     for (l = priv->force_key_unit; l; l = l->next) {
1312       ForcedKeyUnitEvent *tmp = l->data;
1313
1314       /* Skip pending keyunits */
1315       if (tmp->pending)
1316         continue;
1317
1318       /* Simple case, keyunit ASAP */
1319       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1320         fevt = tmp;
1321         break;
1322       }
1323
1324       /* Event for before this frame */
1325       if (tmp->running_time <= running_time) {
1326         fevt = tmp;
1327         break;
1328       }
1329     }
1330
1331     if (fevt) {
1332       GST_DEBUG_OBJECT (encoder,
1333           "Forcing a key unit at running time %" GST_TIME_FORMAT,
1334           GST_TIME_ARGS (running_time));
1335       GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME (frame);
1336       if (fevt->all_headers)
1337         GST_VIDEO_CODEC_FRAME_SET_FORCE_KEYFRAME_HEADERS (frame);
1338       fevt->pending = TRUE;
1339     }
1340   }
1341   GST_OBJECT_UNLOCK (encoder);
1342
1343   gst_video_codec_frame_ref (frame);
1344   priv->frames = g_list_append (priv->frames, frame);
1345
1346   /* new data, more finish needed */
1347   priv->drained = FALSE;
1348
1349   GST_LOG_OBJECT (encoder, "passing frame pfn %d to subclass",
1350       frame->presentation_frame_number);
1351
1352   ret = klass->handle_frame (encoder, frame);
1353
1354 done:
1355   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1356
1357   return ret;
1358
1359   /* ERRORS */
1360 not_negotiated:
1361   {
1362     GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
1363         ("encoder not initialized"));
1364     gst_buffer_unref (buf);
1365     return GST_FLOW_NOT_NEGOTIATED;
1366   }
1367 }
1368
1369 static GstStateChangeReturn
1370 gst_video_encoder_change_state (GstElement * element, GstStateChange transition)
1371 {
1372   GstVideoEncoder *encoder;
1373   GstVideoEncoderClass *encoder_class;
1374   GstStateChangeReturn ret;
1375
1376   encoder = GST_VIDEO_ENCODER (element);
1377   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (element);
1378
1379   switch (transition) {
1380     case GST_STATE_CHANGE_NULL_TO_READY:
1381       /* open device/library if needed */
1382       if (encoder_class->open && !encoder_class->open (encoder))
1383         goto open_failed;
1384       break;
1385     case GST_STATE_CHANGE_READY_TO_PAUSED:
1386       /* Initialize device/library if needed */
1387       if (encoder_class->start && !encoder_class->start (encoder))
1388         goto start_failed;
1389       break;
1390     default:
1391       break;
1392   }
1393
1394   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1395
1396   switch (transition) {
1397     case GST_STATE_CHANGE_PAUSED_TO_READY:
1398       gst_video_encoder_reset (encoder);
1399       if (encoder_class->stop && !encoder_class->stop (encoder))
1400         goto stop_failed;
1401       break;
1402     case GST_STATE_CHANGE_READY_TO_NULL:
1403       /* close device/library if needed */
1404       if (encoder_class->close && !encoder_class->close (encoder))
1405         goto close_failed;
1406       break;
1407     default:
1408       break;
1409   }
1410
1411   return ret;
1412
1413   /* Errors */
1414
1415 open_failed:
1416   {
1417     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1418         ("Failed to open encoder"));
1419     return GST_STATE_CHANGE_FAILURE;
1420   }
1421
1422 start_failed:
1423   {
1424     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1425         ("Failed to start encoder"));
1426     return GST_STATE_CHANGE_FAILURE;
1427   }
1428
1429 stop_failed:
1430   {
1431     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1432         ("Failed to stop encoder"));
1433     return GST_STATE_CHANGE_FAILURE;
1434   }
1435
1436 close_failed:
1437   {
1438     GST_ELEMENT_ERROR (encoder, LIBRARY, INIT, (NULL),
1439         ("Failed to close encoder"));
1440     return GST_STATE_CHANGE_FAILURE;
1441   }
1442 }
1443
1444 static gboolean
1445 gst_video_encoder_negotiate_default (GstVideoEncoder * encoder)
1446 {
1447   GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1448   GstAllocator *allocator;
1449   GstAllocationParams params;
1450   gboolean ret;
1451   GstVideoCodecState *state = encoder->priv->output_state;
1452   GstVideoInfo *info = &state->info;
1453   GstQuery *query = NULL;
1454   GstVideoCodecFrame *frame;
1455
1456   g_return_val_if_fail (state->caps != NULL, FALSE);
1457
1458   if (encoder->priv->output_state_changed) {
1459     state->caps = gst_caps_make_writable (state->caps);
1460
1461     /* Fill caps */
1462     gst_caps_set_simple (state->caps, "width", G_TYPE_INT, info->width,
1463         "height", G_TYPE_INT, info->height,
1464         "pixel-aspect-ratio", GST_TYPE_FRACTION,
1465         info->par_n, info->par_d, NULL);
1466     if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
1467       /* variable fps with a max-framerate */
1468       gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION, 0, 1,
1469           "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
1470     } else {
1471       /* no variable fps or no max-framerate */
1472       gst_caps_set_simple (state->caps, "framerate", GST_TYPE_FRACTION,
1473           info->fps_n, info->fps_d, NULL);
1474     }
1475     if (state->codec_data)
1476       gst_caps_set_simple (state->caps, "codec_data", GST_TYPE_BUFFER,
1477           state->codec_data, NULL);
1478     encoder->priv->output_state_changed = FALSE;
1479   }
1480
1481   /* Push all pending pre-caps events of the oldest frame before
1482    * setting caps */
1483   frame = encoder->priv->frames ? encoder->priv->frames->data : NULL;
1484   if (frame || encoder->priv->current_frame_events) {
1485     GList **events, *l;
1486     gboolean set_caps = FALSE;
1487
1488     if (frame) {
1489       events = &frame->events;
1490     } else {
1491       events = &encoder->priv->current_frame_events;
1492     }
1493
1494     ret = FALSE;
1495     for (l = g_list_last (*events); l;) {
1496       GstEvent *event = GST_EVENT (l->data);
1497       GList *tmp;
1498
1499       if (GST_EVENT_TYPE (event) > GST_EVENT_CAPS && !set_caps) {
1500         ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1501         set_caps = TRUE;
1502         break;
1503       }
1504       gst_video_encoder_push_event (encoder, event);
1505       tmp = l;
1506       l = l->prev;
1507       *events = g_list_delete_link (*events, tmp);
1508     }
1509     if (!set_caps) {
1510       ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1511     }
1512   } else {
1513     ret = gst_pad_set_caps (encoder->srcpad, state->caps);
1514   }
1515
1516   if (!ret)
1517     goto done;
1518
1519   query = gst_query_new_allocation (state->caps, TRUE);
1520   if (!gst_pad_peer_query (encoder->srcpad, query)) {
1521     GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
1522   }
1523
1524   g_assert (klass->decide_allocation != NULL);
1525   ret = klass->decide_allocation (encoder, query);
1526
1527   GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
1528       query);
1529
1530   if (!ret)
1531     goto no_decide_allocation;
1532
1533   /* we got configuration from our peer or the decide_allocation method,
1534    * parse them */
1535   if (gst_query_get_n_allocation_params (query) > 0) {
1536     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
1537   } else {
1538     allocator = NULL;
1539     gst_allocation_params_init (&params);
1540   }
1541
1542   if (encoder->priv->allocator)
1543     gst_object_unref (encoder->priv->allocator);
1544   encoder->priv->allocator = allocator;
1545   encoder->priv->params = params;
1546
1547 done:
1548   if (query)
1549     gst_query_unref (query);
1550
1551   return ret;
1552
1553   /* Errors */
1554 no_decide_allocation:
1555   {
1556     GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
1557     goto done;
1558   }
1559 }
1560
1561 /**
1562  * gst_video_encoder_negotiate:
1563  * @encoder: a #GstVideoEncoder
1564  *
1565  * Negotiate with downstream elements to currently configured #GstVideoCodecState.
1566  *
1567  * Returns: #TRUE if the negotiation succeeded, else #FALSE.
1568  */
1569 gboolean
1570 gst_video_encoder_negotiate (GstVideoEncoder * encoder)
1571 {
1572   GstVideoEncoderClass *klass;
1573   gboolean ret = TRUE;
1574
1575   g_return_val_if_fail (GST_IS_VIDEO_ENCODER (encoder), FALSE);
1576
1577   klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1578
1579   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1580   if (klass->negotiate)
1581     ret = klass->negotiate (encoder);
1582   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1583
1584   return ret;
1585 }
1586
1587 /**
1588  * gst_video_encoder_allocate_output_buffer:
1589  * @encoder: a #GstVideoEncoder
1590  * @size: size of the buffer
1591  *
1592  * Helper function that allocates a buffer to hold an encoded video frame
1593  * for @encoder's current #GstVideoCodecState.
1594  *
1595  * Returns: (transfer full): allocated buffer
1596  */
1597 GstBuffer *
1598 gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
1599 {
1600   GstBuffer *buffer;
1601
1602   g_return_val_if_fail (size > 0, NULL);
1603
1604   GST_DEBUG ("alloc src buffer");
1605
1606   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1607   if (G_UNLIKELY (encoder->priv->output_state_changed
1608           || (encoder->priv->output_state
1609               && gst_pad_check_reconfigure (encoder->srcpad))))
1610     gst_video_encoder_negotiate (encoder);
1611
1612   buffer =
1613       gst_buffer_new_allocate (encoder->priv->allocator, size,
1614       &encoder->priv->params);
1615
1616   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1617
1618   return buffer;
1619 }
1620
1621 /**
1622  * gst_video_encoder_allocate_output_frame:
1623  * @encoder: a #GstVideoEncoder
1624  * @frame: a #GstVideoCodecFrame
1625  * @size: size of the buffer
1626  *
1627  * Helper function that allocates a buffer to hold an encoded video frame for @encoder's
1628  * current #GstVideoCodecState.  Subclass should already have configured video
1629  * state and set src pad caps.
1630  *
1631  * The buffer allocated here is owned by the frame and you should only
1632  * keep references to the frame, not the buffer.
1633  *
1634  * Returns: %GST_FLOW_OK if an output buffer could be allocated
1635  */
1636 GstFlowReturn
1637 gst_video_encoder_allocate_output_frame (GstVideoEncoder *
1638     encoder, GstVideoCodecFrame * frame, gsize size)
1639 {
1640   g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
1641
1642   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1643   if (G_UNLIKELY (encoder->priv->output_state_changed
1644           || (encoder->priv->output_state
1645               && gst_pad_check_reconfigure (encoder->srcpad))))
1646     gst_video_encoder_negotiate (encoder);
1647
1648   GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
1649
1650   frame->output_buffer =
1651       gst_buffer_new_allocate (encoder->priv->allocator, size,
1652       &encoder->priv->params);
1653
1654   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1655
1656   return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
1657 }
1658
1659 static void
1660 gst_video_encoder_release_frame (GstVideoEncoder * enc,
1661     GstVideoCodecFrame * frame)
1662 {
1663   GList *link;
1664
1665   /* unref once from the list */
1666   link = g_list_find (enc->priv->frames, frame);
1667   if (link) {
1668     gst_video_codec_frame_unref (frame);
1669     enc->priv->frames = g_list_delete_link (enc->priv->frames, link);
1670   }
1671   /* unref because this function takes ownership */
1672   gst_video_codec_frame_unref (frame);
1673 }
1674
1675 /**
1676  * gst_video_encoder_finish_frame:
1677  * @encoder: a #GstVideoEncoder
1678  * @frame: (transfer full): an encoded #GstVideoCodecFrame 
1679  *
1680  * @frame must have a valid encoded data buffer, whose metadata fields
1681  * are then appropriately set according to frame data or no buffer at
1682  * all if the frame should be dropped.
1683  * It is subsequently pushed downstream or provided to @pre_push.
1684  * In any case, the frame is considered finished and released.
1685  *
1686  * After calling this function the output buffer of the frame is to be
1687  * considered read-only. This function will also change the metadata
1688  * of the buffer.
1689  *
1690  * Returns: a #GstFlowReturn resulting from sending data downstream
1691  */
1692 GstFlowReturn
1693 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
1694     GstVideoCodecFrame * frame)
1695 {
1696   GstVideoEncoderPrivate *priv = encoder->priv;
1697   GstFlowReturn ret = GST_FLOW_OK;
1698   GstVideoEncoderClass *encoder_class;
1699   GList *l;
1700   gboolean send_headers = FALSE;
1701   gboolean discont = (frame->presentation_frame_number == 0);
1702   GstBuffer *buffer;
1703
1704   encoder_class = GST_VIDEO_ENCODER_GET_CLASS (encoder);
1705
1706   GST_LOG_OBJECT (encoder,
1707       "finish frame fpn %d", frame->presentation_frame_number);
1708
1709   GST_LOG_OBJECT (encoder, "frame PTS %" GST_TIME_FORMAT
1710       ", DTS %" GST_TIME_FORMAT, GST_TIME_ARGS (frame->pts),
1711       GST_TIME_ARGS (frame->dts));
1712
1713   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1714
1715   if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
1716               && gst_pad_check_reconfigure (encoder->srcpad))))
1717     gst_video_encoder_negotiate (encoder);
1718
1719   if (G_UNLIKELY (priv->output_state == NULL))
1720     goto no_output_state;
1721
1722   /* Push all pending events that arrived before this frame */
1723   for (l = priv->frames; l; l = l->next) {
1724     GstVideoCodecFrame *tmp = l->data;
1725
1726     if (tmp->events) {
1727       GList *k;
1728
1729       for (k = g_list_last (tmp->events); k; k = k->prev)
1730         gst_video_encoder_push_event (encoder, k->data);
1731       g_list_free (tmp->events);
1732       tmp->events = NULL;
1733     }
1734
1735     if (tmp == frame)
1736       break;
1737   }
1738
1739   if (priv->tags && priv->tags_changed) {
1740     gst_video_encoder_push_event (encoder,
1741         gst_event_new_tag (gst_tag_list_ref (priv->tags)));
1742     priv->tags_changed = FALSE;
1743   }
1744
1745   /* no buffer data means this frame is skipped/dropped */
1746   if (!frame->output_buffer) {
1747     GST_DEBUG_OBJECT (encoder, "skipping frame %" GST_TIME_FORMAT,
1748         GST_TIME_ARGS (frame->pts));
1749     goto done;
1750   }
1751
1752   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) && priv->force_key_unit) {
1753     GstClockTime stream_time, running_time;
1754     GstEvent *ev;
1755     ForcedKeyUnitEvent *fevt = NULL;
1756     GList *l;
1757
1758     running_time =
1759         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
1760         frame->pts);
1761
1762     GST_OBJECT_LOCK (encoder);
1763     for (l = priv->force_key_unit; l; l = l->next) {
1764       ForcedKeyUnitEvent *tmp = l->data;
1765
1766       /* Skip non-pending keyunits */
1767       if (!tmp->pending)
1768         continue;
1769
1770       /* Simple case, keyunit ASAP */
1771       if (tmp->running_time == GST_CLOCK_TIME_NONE) {
1772         fevt = tmp;
1773         break;
1774       }
1775
1776       /* Event for before this frame */
1777       if (tmp->running_time <= running_time) {
1778         fevt = tmp;
1779         break;
1780       }
1781     }
1782
1783     if (fevt) {
1784       priv->force_key_unit = g_list_remove (priv->force_key_unit, fevt);
1785     }
1786     GST_OBJECT_UNLOCK (encoder);
1787
1788     if (fevt) {
1789       stream_time =
1790           gst_segment_to_stream_time (&encoder->output_segment, GST_FORMAT_TIME,
1791           frame->pts);
1792
1793       ev = gst_video_event_new_downstream_force_key_unit
1794           (frame->pts, stream_time, running_time,
1795           fevt->all_headers, fevt->count);
1796
1797       gst_video_encoder_push_event (encoder, ev);
1798
1799       if (fevt->all_headers)
1800         send_headers = TRUE;
1801
1802       GST_DEBUG_OBJECT (encoder,
1803           "Forced key unit: running-time %" GST_TIME_FORMAT
1804           ", all_headers %d, count %u",
1805           GST_TIME_ARGS (running_time), fevt->all_headers, fevt->count);
1806       forced_key_unit_event_free (fevt);
1807     }
1808   }
1809
1810   if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame)) {
1811     priv->distance_from_sync = 0;
1812     GST_BUFFER_FLAG_UNSET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1813     /* For keyframes, DTS = PTS */
1814     if (!GST_CLOCK_TIME_IS_VALID (frame->dts)) {
1815       frame->dts = frame->pts;
1816     } else if (GST_CLOCK_TIME_IS_VALID (frame->pts) && frame->pts != frame->dts) {
1817       GST_WARNING_OBJECT (encoder, "keyframe PTS != DTS");
1818     }
1819   } else {
1820     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1821   }
1822
1823   /* DTS is expected monotone ascending,
1824    * so a good guess is the lowest unsent PTS (all being OK) */
1825   {
1826     GstClockTime min_ts = GST_CLOCK_TIME_NONE;
1827     GstVideoCodecFrame *oframe = NULL;
1828     gboolean seen_none = FALSE;
1829
1830     /* some maintenance regardless */
1831     for (l = priv->frames; l; l = l->next) {
1832       GstVideoCodecFrame *tmp = l->data;
1833
1834       if (!GST_CLOCK_TIME_IS_VALID (tmp->abidata.ABI.ts)) {
1835         seen_none = TRUE;
1836         continue;
1837       }
1838
1839       if (!GST_CLOCK_TIME_IS_VALID (min_ts) || tmp->abidata.ABI.ts < min_ts) {
1840         min_ts = tmp->abidata.ABI.ts;
1841         oframe = tmp;
1842       }
1843     }
1844     /* save a ts if needed */
1845     if (oframe && oframe != frame) {
1846       oframe->abidata.ABI.ts = frame->abidata.ABI.ts;
1847     }
1848
1849     /* and set if needed */
1850     if (!GST_CLOCK_TIME_IS_VALID (frame->dts) && !seen_none) {
1851       frame->dts = min_ts;
1852       GST_DEBUG_OBJECT (encoder,
1853           "no valid DTS, using oldest PTS %" GST_TIME_FORMAT,
1854           GST_TIME_ARGS (frame->pts));
1855     }
1856   }
1857
1858   frame->distance_from_sync = priv->distance_from_sync;
1859   priv->distance_from_sync++;
1860
1861   GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
1862   GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
1863   GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
1864
1865   /* update rate estimate */
1866   priv->bytes += gst_buffer_get_size (frame->output_buffer);
1867   if (GST_CLOCK_TIME_IS_VALID (frame->duration)) {
1868     priv->time += frame->duration;
1869   } else {
1870     /* better none than nothing valid */
1871     priv->time = GST_CLOCK_TIME_NONE;
1872   }
1873
1874   if (G_UNLIKELY (send_headers || priv->new_headers)) {
1875     GList *tmp, *copy = NULL;
1876
1877     GST_DEBUG_OBJECT (encoder, "Sending headers");
1878
1879     /* First make all buffers metadata-writable */
1880     for (tmp = priv->headers; tmp; tmp = tmp->next) {
1881       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1882
1883       copy = g_list_append (copy, gst_buffer_make_writable (tmpbuf));
1884     }
1885     g_list_free (priv->headers);
1886     priv->headers = copy;
1887
1888     for (tmp = priv->headers; tmp; tmp = tmp->next) {
1889       GstBuffer *tmpbuf = GST_BUFFER (tmp->data);
1890
1891       priv->bytes += gst_buffer_get_size (tmpbuf);
1892       if (G_UNLIKELY (discont)) {
1893         GST_LOG_OBJECT (encoder, "marking discont");
1894         GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1895         discont = FALSE;
1896       }
1897
1898       gst_pad_push (encoder->srcpad, gst_buffer_ref (tmpbuf));
1899     }
1900     priv->new_headers = FALSE;
1901   }
1902
1903   if (G_UNLIKELY (discont)) {
1904     GST_LOG_OBJECT (encoder, "marking discont");
1905     GST_BUFFER_FLAG_SET (frame->output_buffer, GST_BUFFER_FLAG_DISCONT);
1906   }
1907
1908   if (encoder_class->pre_push)
1909     ret = encoder_class->pre_push (encoder, frame);
1910
1911   /* Get an additional ref to the buffer, which is going to be pushed
1912    * downstream, the original ref is owned by the frame */
1913   buffer = gst_buffer_ref (frame->output_buffer);
1914
1915   /* Release frame so the buffer is writable when we push it downstream
1916    * if possible, i.e. if the subclass does not hold additional references
1917    * to the frame
1918    */
1919   gst_video_encoder_release_frame (encoder, frame);
1920   frame = NULL;
1921
1922   if (ret == GST_FLOW_OK)
1923     ret = gst_pad_push (encoder->srcpad, buffer);
1924
1925 done:
1926   /* handed out */
1927   if (frame)
1928     gst_video_encoder_release_frame (encoder, frame);
1929
1930   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1931
1932   return ret;
1933
1934   /* ERRORS */
1935 no_output_state:
1936   {
1937     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1938     GST_ERROR_OBJECT (encoder, "Output state was not configured");
1939     return GST_FLOW_ERROR;
1940   }
1941 }
1942
1943 /**
1944  * gst_video_encoder_get_output_state:
1945  * @encoder: a #GstVideoEncoder
1946  *
1947  * Get the current #GstVideoCodecState
1948  *
1949  * Returns: (transfer full): #GstVideoCodecState describing format of video data.
1950  */
1951 GstVideoCodecState *
1952 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
1953 {
1954   GstVideoCodecState *state;
1955
1956   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
1957   state = gst_video_codec_state_ref (encoder->priv->output_state);
1958   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
1959
1960   return state;
1961 }
1962
1963 /**
1964  * gst_video_encoder_set_output_state:
1965  * @encoder: a #GstVideoEncoder
1966  * @caps: (transfer full): the #GstCaps to use for the output
1967  * @reference: (allow-none) (transfer none): An optional reference @GstVideoCodecState
1968  *
1969  * Creates a new #GstVideoCodecState with the specified caps as the output state
1970  * for the encoder.
1971  * Any previously set output state on @encoder will be replaced by the newly
1972  * created one.
1973  *
1974  * The specified @caps should not contain any resolution, pixel-aspect-ratio,
1975  * framerate, codec-data, .... Those should be specified instead in the returned
1976  * #GstVideoCodecState.
1977  *
1978  * If the subclass wishes to copy over existing fields (like pixel aspect ratio,
1979  * or framerate) from an existing #GstVideoCodecState, it can be provided as a
1980  * @reference.
1981  *
1982  * If the subclass wishes to override some fields from the output state (like
1983  * pixel-aspect-ratio or framerate) it can do so on the returned #GstVideoCodecState.
1984  *
1985  * The new output state will only take effect (set on pads and buffers) starting
1986  * from the next call to #gst_video_encoder_finish_frame().
1987  *
1988  * Returns: (transfer full): the newly configured output state.
1989  */
1990 GstVideoCodecState *
1991 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
1992     GstVideoCodecState * reference)
1993 {
1994   GstVideoEncoderPrivate *priv = encoder->priv;
1995   GstVideoCodecState *state;
1996
1997   g_return_val_if_fail (caps != NULL, NULL);
1998
1999   state = _new_output_state (caps, reference);
2000
2001   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2002   if (priv->output_state)
2003     gst_video_codec_state_unref (priv->output_state);
2004   priv->output_state = gst_video_codec_state_ref (state);
2005
2006   priv->output_state_changed = TRUE;
2007   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2008
2009   return state;
2010 }
2011
2012 /**
2013  * gst_video_encoder_set_latency:
2014  * @encoder: a #GstVideoEncoder
2015  * @min_latency: minimum latency
2016  * @max_latency: maximum latency
2017  *
2018  * Informs baseclass of encoding latency.
2019  */
2020 void
2021 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
2022     GstClockTime min_latency, GstClockTime max_latency)
2023 {
2024   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
2025   g_return_if_fail (max_latency >= min_latency);
2026
2027   GST_OBJECT_LOCK (encoder);
2028   encoder->priv->min_latency = min_latency;
2029   encoder->priv->max_latency = max_latency;
2030   GST_OBJECT_UNLOCK (encoder);
2031
2032   gst_element_post_message (GST_ELEMENT_CAST (encoder),
2033       gst_message_new_latency (GST_OBJECT_CAST (encoder)));
2034 }
2035
2036 /**
2037  * gst_video_encoder_get_latency:
2038  * @encoder: a #GstVideoEncoder
2039  * @min_latency: (out) (allow-none): address of variable in which to store the
2040  *     configured minimum latency, or %NULL
2041  * @max_latency: (out) (allow-none): address of variable in which to store the
2042  *     configured maximum latency, or %NULL
2043  *
2044  * Query the configured encoding latency. Results will be returned via
2045  * @min_latency and @max_latency.
2046  */
2047 void
2048 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
2049     GstClockTime * min_latency, GstClockTime * max_latency)
2050 {
2051   GST_OBJECT_LOCK (encoder);
2052   if (min_latency)
2053     *min_latency = encoder->priv->min_latency;
2054   if (max_latency)
2055     *max_latency = encoder->priv->max_latency;
2056   GST_OBJECT_UNLOCK (encoder);
2057 }
2058
2059 /**
2060  * gst_video_encoder_get_oldest_frame:
2061  * @encoder: a #GstVideoEncoder
2062  *
2063  * Get the oldest unfinished pending #GstVideoCodecFrame
2064  *
2065  * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
2066  */
2067 GstVideoCodecFrame *
2068 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
2069 {
2070   GstVideoCodecFrame *frame = NULL;
2071
2072   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2073   if (encoder->priv->frames)
2074     frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
2075   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2076
2077   return (GstVideoCodecFrame *) frame;
2078 }
2079
2080 /**
2081  * gst_video_encoder_get_frame:
2082  * @encoder: a #GstVideoEnccoder
2083  * @frame_number: system_frame_number of a frame
2084  *
2085  * Get a pending unfinished #GstVideoCodecFrame
2086  * 
2087  * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
2088  */
2089 GstVideoCodecFrame *
2090 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
2091 {
2092   GList *g;
2093   GstVideoCodecFrame *frame = NULL;
2094
2095   GST_DEBUG_OBJECT (encoder, "frame_number : %d", frame_number);
2096
2097   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2098   for (g = encoder->priv->frames; g; g = g->next) {
2099     GstVideoCodecFrame *tmp = g->data;
2100
2101     if (tmp->system_frame_number == frame_number) {
2102       frame = gst_video_codec_frame_ref (tmp);
2103       break;
2104     }
2105   }
2106   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2107
2108   return frame;
2109 }
2110
2111 /**
2112  * gst_video_encoder_get_frames:
2113  * @encoder: a #GstVideoEncoder
2114  *
2115  * Get all pending unfinished #GstVideoCodecFrame
2116  * 
2117  * Returns: (transfer full) (element-type GstVideoCodecFrame): pending unfinished #GstVideoCodecFrame.
2118  */
2119 GList *
2120 gst_video_encoder_get_frames (GstVideoEncoder * encoder)
2121 {
2122   GList *frames;
2123
2124   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2125   frames = g_list_copy (encoder->priv->frames);
2126   g_list_foreach (frames, (GFunc) gst_video_codec_frame_ref, NULL);
2127   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2128
2129   return frames;
2130 }
2131
2132 /**
2133  * gst_video_encoder_merge_tags:
2134  * @encoder: a #GstVideoEncoder
2135  * @tags: a #GstTagList to merge
2136  * @mode: the #GstTagMergeMode to use
2137  *
2138  * Adds tags to so-called pending tags, which will be processed
2139  * before pushing out data downstream.
2140  *
2141  * Note that this is provided for convenience, and the subclass is
2142  * not required to use this and can still do tag handling on its own.
2143  *
2144  * MT safe.
2145  */
2146 void
2147 gst_video_encoder_merge_tags (GstVideoEncoder * encoder,
2148     const GstTagList * tags, GstTagMergeMode mode)
2149 {
2150   GstTagList *otags;
2151
2152   g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2153   g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
2154
2155   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
2156   if (tags)
2157     GST_DEBUG_OBJECT (encoder, "merging tags %" GST_PTR_FORMAT, tags);
2158   otags = encoder->priv->tags;
2159   encoder->priv->tags = gst_tag_list_merge (encoder->priv->tags, tags, mode);
2160   if (otags)
2161     gst_tag_list_unref (otags);
2162   encoder->priv->tags_changed = TRUE;
2163   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
2164 }
2165
2166 /**
2167  * gst_video_encoder_get_allocator:
2168  * @encoder: a #GstVideoEncoder
2169  * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
2170  * used
2171  * @params: (out) (allow-none) (transfer full): the
2172  * #GstAllocatorParams of @allocator
2173  *
2174  * Lets #GstVideoEncoder sub-classes to know the memory @allocator
2175  * used by the base class and its @params.
2176  *
2177  * Unref the @allocator after use it.
2178  */
2179 void
2180 gst_video_encoder_get_allocator (GstVideoEncoder * encoder,
2181     GstAllocator ** allocator, GstAllocationParams * params)
2182 {
2183   g_return_if_fail (GST_IS_VIDEO_ENCODER (encoder));
2184
2185   if (allocator)
2186     *allocator = encoder->priv->allocator ?
2187         gst_object_ref (encoder->priv->allocator) : NULL;
2188
2189   if (params)
2190     *params = encoder->priv->params;
2191 }