upload tizen1.0 source
[framework/multimedia/gstreamer0.10-ffmpeg.git] / ext / ffmpeg / gstffmpegenc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <assert.h>
25 #include <string.h>
26 /* for stats file handling */
27 #include <stdio.h>
28 #include <glib/gstdio.h>
29 #include <errno.h>
30
31 #ifdef HAVE_FFMPEG_UNINSTALLED
32 #include <avcodec.h>
33 #else
34 #include <libavcodec/avcodec.h>
35 #endif
36
37 #include <gst/gst.h>
38
39 #include "gstffmpeg.h"
40 #include "gstffmpegcodecmap.h"
41 #include "gstffmpegutils.h"
42 #include "gstffmpegenc.h"
43 #include "gstffmpegcfg.h"
44
45 #define DEFAULT_VIDEO_BITRATE 300000    /* in bps */
46 #define DEFAULT_VIDEO_GOP_SIZE 15
47 #define DEFAULT_AUDIO_BITRATE 128000
48
49 #define DEFAULT_WIDTH 352
50 #define DEFAULT_HEIGHT 288
51
52
53 #define VIDEO_BUFFER_SIZE (1024*1024)
54
55 enum
56 {
57   /* FILL ME */
58   LAST_SIGNAL
59 };
60
61 enum
62 {
63   ARG_0,
64   ARG_BIT_RATE,
65   ARG_GOP_SIZE,
66   ARG_ME_METHOD,
67   ARG_BUFSIZE,
68   ARG_RTP_PAYLOAD_SIZE,
69   ARG_CFG_BASE
70 };
71
72 #define GST_TYPE_ME_METHOD (gst_ffmpegenc_me_method_get_type())
73 static GType
74 gst_ffmpegenc_me_method_get_type (void)
75 {
76   static GType ffmpegenc_me_method_type = 0;
77   static GEnumValue ffmpegenc_me_methods[] = {
78     {ME_ZERO, "None (Very low quality)", "zero"},
79     {ME_FULL, "Full (Slow, unmaintained)", "full"},
80     {ME_LOG, "Logarithmic (Low quality, unmaintained)", "logarithmic"},
81     {ME_PHODS, "phods (Low quality, unmaintained)", "phods"},
82     {ME_EPZS, "EPZS (Best quality, Fast)", "epzs"},
83     {ME_X1, "X1 (Experimental)", "x1"},
84     {0, NULL, NULL},
85   };
86   if (!ffmpegenc_me_method_type) {
87     ffmpegenc_me_method_type =
88         g_enum_register_static ("GstFFMpegEncMeMethod", ffmpegenc_me_methods);
89   }
90   return ffmpegenc_me_method_type;
91 }
92
93 /* A number of function prototypes are given so we can refer to them later. */
94 static void gst_ffmpegenc_class_init (GstFFMpegEncClass * klass);
95 static void gst_ffmpegenc_base_init (GstFFMpegEncClass * klass);
96 static void gst_ffmpegenc_init (GstFFMpegEnc * ffmpegenc);
97 static void gst_ffmpegenc_finalize (GObject * object);
98
99 static gboolean gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps);
100 static GstCaps *gst_ffmpegenc_getcaps (GstPad * pad);
101 static GstFlowReturn gst_ffmpegenc_chain_video (GstPad * pad,
102     GstBuffer * buffer);
103 static GstFlowReturn gst_ffmpegenc_chain_audio (GstPad * pad,
104     GstBuffer * buffer);
105 static gboolean gst_ffmpegenc_event_video (GstPad * pad, GstEvent * event);
106 static gboolean gst_ffmpegenc_event_src (GstPad * pad, GstEvent * event);
107
108 static void gst_ffmpegenc_set_property (GObject * object,
109     guint prop_id, const GValue * value, GParamSpec * pspec);
110 static void gst_ffmpegenc_get_property (GObject * object,
111     guint prop_id, GValue * value, GParamSpec * pspec);
112
113 static GstStateChangeReturn gst_ffmpegenc_change_state (GstElement * element,
114     GstStateChange transition);
115
116 #define GST_FFENC_PARAMS_QDATA g_quark_from_static_string("ffenc-params")
117
118 static GstElementClass *parent_class = NULL;
119
120 /*static guint gst_ffmpegenc_signals[LAST_SIGNAL] = { 0 }; */
121
122 static void
123 gst_ffmpegenc_base_init (GstFFMpegEncClass * klass)
124 {
125   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
126   AVCodec *in_plugin;
127   GstPadTemplate *srctempl = NULL, *sinktempl = NULL;
128   GstCaps *srccaps = NULL, *sinkcaps = NULL;
129   gchar *longname, *classification, *description;
130
131   in_plugin =
132       (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
133       GST_FFENC_PARAMS_QDATA);
134   g_assert (in_plugin != NULL);
135
136   /* construct the element details struct */
137   longname = g_strdup_printf ("FFmpeg %s encoder", in_plugin->long_name);
138   classification = g_strdup_printf ("Codec/Encoder/%s",
139       (in_plugin->type == CODEC_TYPE_VIDEO) ? "Video" : "Audio");
140   description = g_strdup_printf ("FFmpeg %s encoder", in_plugin->name);
141   gst_element_class_set_details_simple (element_class, longname, classification,
142       description,
143       "Wim Taymans <wim.taymans@gmail.com>, "
144       "Ronald Bultje <rbultje@ronald.bitfreak.net>");
145   g_free (longname);
146   g_free (classification);
147   g_free (description);
148
149   if (!(srccaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, TRUE))) {
150     GST_DEBUG ("Couldn't get source caps for encoder '%s'", in_plugin->name);
151     srccaps = gst_caps_new_simple ("unknown/unknown", NULL);
152   }
153
154   if (in_plugin->type == CODEC_TYPE_VIDEO) {
155     sinkcaps = gst_caps_from_string
156         ("video/x-raw-rgb; video/x-raw-yuv; video/x-raw-gray");
157   } else {
158     sinkcaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
159         in_plugin->id, TRUE, in_plugin);
160   }
161   if (!sinkcaps) {
162     GST_DEBUG ("Couldn't get sink caps for encoder '%s'", in_plugin->name);
163     sinkcaps = gst_caps_new_simple ("unknown/unknown", NULL);
164   }
165
166   /* pad templates */
167   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
168       GST_PAD_ALWAYS, sinkcaps);
169   srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
170
171   gst_element_class_add_pad_template (element_class, srctempl);
172   gst_element_class_add_pad_template (element_class, sinktempl);
173
174   klass->in_plugin = in_plugin;
175   klass->srctempl = srctempl;
176   klass->sinktempl = sinktempl;
177   klass->sinkcaps = NULL;
178
179   return;
180 }
181
182 static void
183 gst_ffmpegenc_class_init (GstFFMpegEncClass * klass)
184 {
185   GObjectClass *gobject_class;
186   GstElementClass *gstelement_class;
187
188   gobject_class = (GObjectClass *) klass;
189   gstelement_class = (GstElementClass *) klass;
190
191   parent_class = g_type_class_peek_parent (klass);
192
193   gobject_class->set_property = gst_ffmpegenc_set_property;
194   gobject_class->get_property = gst_ffmpegenc_get_property;
195
196   if (klass->in_plugin->type == CODEC_TYPE_VIDEO) {
197     g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BIT_RATE,
198         g_param_spec_ulong ("bitrate", "Bit Rate",
199             "Target Video Bitrate", 0, G_MAXULONG, DEFAULT_VIDEO_BITRATE,
200             G_PARAM_READWRITE));
201     g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GOP_SIZE,
202         g_param_spec_int ("gop-size", "GOP Size",
203             "Number of frames within one GOP", 0, G_MAXINT,
204             DEFAULT_VIDEO_GOP_SIZE, G_PARAM_READWRITE));
205     g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ME_METHOD,
206         g_param_spec_enum ("me-method", "ME Method", "Motion Estimation Method",
207             GST_TYPE_ME_METHOD, ME_EPZS, G_PARAM_READWRITE));
208
209     /* FIXME 0.11: Make this property read-only */
210     g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFSIZE,
211         g_param_spec_ulong ("buffer-size", "Buffer Size",
212             "Size of the video buffers. "
213             "Note: Setting this property has no effect "
214             "and is deprecated!", 0, G_MAXULONG, 0, G_PARAM_READWRITE));
215     g_object_class_install_property (G_OBJECT_CLASS (klass),
216         ARG_RTP_PAYLOAD_SIZE, g_param_spec_ulong ("rtp-payload-size",
217             "RTP Payload Size", "Target GOB length", 0, G_MAXULONG, 0,
218             G_PARAM_READWRITE));
219
220     /* register additional properties, possibly dependent on the exact CODEC */
221     gst_ffmpeg_cfg_install_property (klass, ARG_CFG_BASE);
222   } else if (klass->in_plugin->type == CODEC_TYPE_AUDIO) {
223     g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BIT_RATE,
224         g_param_spec_ulong ("bitrate", "Bit Rate",
225             "Target Audio Bitrate", 0, G_MAXULONG, DEFAULT_AUDIO_BITRATE,
226             G_PARAM_READWRITE));
227   }
228
229   gstelement_class->change_state = gst_ffmpegenc_change_state;
230
231   gobject_class->finalize = gst_ffmpegenc_finalize;
232 }
233
234 static void
235 gst_ffmpegenc_init (GstFFMpegEnc * ffmpegenc)
236 {
237   GstFFMpegEncClass *oclass =
238       (GstFFMpegEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
239
240   /* setup pads */
241   ffmpegenc->sinkpad = gst_pad_new_from_template (oclass->sinktempl, "sink");
242   gst_pad_set_setcaps_function (ffmpegenc->sinkpad, gst_ffmpegenc_setcaps);
243   gst_pad_set_getcaps_function (ffmpegenc->sinkpad, gst_ffmpegenc_getcaps);
244   ffmpegenc->srcpad = gst_pad_new_from_template (oclass->srctempl, "src");
245   gst_pad_use_fixed_caps (ffmpegenc->srcpad);
246
247   /* ffmpeg objects */
248   ffmpegenc->context = avcodec_alloc_context ();
249   ffmpegenc->picture = avcodec_alloc_frame ();
250   ffmpegenc->opened = FALSE;
251
252   ffmpegenc->file = NULL;
253   ffmpegenc->delay = g_queue_new ();
254
255   if (oclass->in_plugin->type == CODEC_TYPE_VIDEO) {
256     gst_pad_set_chain_function (ffmpegenc->sinkpad, gst_ffmpegenc_chain_video);
257     /* so we know when to flush the buffers on EOS */
258     gst_pad_set_event_function (ffmpegenc->sinkpad, gst_ffmpegenc_event_video);
259     gst_pad_set_event_function (ffmpegenc->srcpad, gst_ffmpegenc_event_src);
260
261     ffmpegenc->bitrate = DEFAULT_VIDEO_BITRATE;
262     ffmpegenc->me_method = ME_EPZS;
263     ffmpegenc->buffer_size = 512 * 1024;
264     ffmpegenc->gop_size = DEFAULT_VIDEO_GOP_SIZE;
265     ffmpegenc->rtp_payload_size = 0;
266
267     ffmpegenc->lmin = 2;
268     ffmpegenc->lmax = 31;
269     ffmpegenc->max_key_interval = 0;
270
271     gst_ffmpeg_cfg_set_defaults (ffmpegenc);
272   } else if (oclass->in_plugin->type == CODEC_TYPE_AUDIO) {
273     gst_pad_set_chain_function (ffmpegenc->sinkpad, gst_ffmpegenc_chain_audio);
274
275     ffmpegenc->bitrate = DEFAULT_AUDIO_BITRATE;
276   }
277
278   gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->sinkpad);
279   gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->srcpad);
280
281   ffmpegenc->adapter = gst_adapter_new ();
282 }
283
284 static void
285 gst_ffmpegenc_finalize (GObject * object)
286 {
287   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) object;
288
289   gst_ffmpeg_cfg_finalize (ffmpegenc);
290
291   /* close old session */
292   if (ffmpegenc->opened) {
293     gst_ffmpeg_avcodec_close (ffmpegenc->context);
294     ffmpegenc->opened = FALSE;
295   }
296
297   /* clean up remaining allocated data */
298   av_free (ffmpegenc->context);
299   av_free (ffmpegenc->picture);
300
301   g_queue_free (ffmpegenc->delay);
302   g_free (ffmpegenc->filename);
303
304   g_object_unref (ffmpegenc->adapter);
305
306   G_OBJECT_CLASS (parent_class)->finalize (object);
307 }
308
309 static GstCaps *
310 gst_ffmpegenc_get_possible_sizes (GstFFMpegEnc * ffmpegenc, GstPad * pad,
311     const GstCaps * caps)
312 {
313   GstCaps *othercaps = NULL;
314   GstCaps *tmpcaps = NULL;
315   GstCaps *intersect = NULL;
316   guint i;
317
318   othercaps = gst_pad_peer_get_caps (ffmpegenc->srcpad);
319
320   if (!othercaps)
321     return gst_caps_copy (caps);
322
323   intersect = gst_caps_intersect (othercaps,
324       gst_pad_get_pad_template_caps (ffmpegenc->srcpad));
325   gst_caps_unref (othercaps);
326
327   if (gst_caps_is_empty (intersect))
328     return intersect;
329
330   if (gst_caps_is_any (intersect))
331     return gst_caps_copy (caps);
332
333   tmpcaps = gst_caps_new_empty ();
334
335   for (i = 0; i < gst_caps_get_size (intersect); i++) {
336     GstStructure *s = gst_caps_get_structure (intersect, i);
337     const GValue *height = NULL;
338     const GValue *width = NULL;
339     const GValue *framerate = NULL;
340     GstStructure *tmps;
341
342     height = gst_structure_get_value (s, "height");
343     width = gst_structure_get_value (s, "width");
344     framerate = gst_structure_get_value (s, "framerate");
345
346     tmps = gst_structure_new ("video/x-raw-rgb", NULL);
347     if (width)
348       gst_structure_set_value (tmps, "width", width);
349     if (height)
350       gst_structure_set_value (tmps, "height", height);
351     if (framerate)
352       gst_structure_set_value (tmps, "framerate", framerate);
353     gst_caps_merge_structure (tmpcaps, gst_structure_copy (tmps));
354
355     gst_structure_set_name (tmps, "video/x-raw-yuv");
356     gst_caps_merge_structure (tmpcaps, gst_structure_copy (tmps));
357
358     gst_structure_set_name (tmps, "video/x-raw-gray");
359     gst_caps_merge_structure (tmpcaps, tmps);
360   }
361   gst_caps_unref (intersect);
362
363   intersect = gst_caps_intersect (caps, tmpcaps);
364   gst_caps_unref (tmpcaps);
365
366   return intersect;
367 }
368
369
370 static GstCaps *
371 gst_ffmpegenc_getcaps (GstPad * pad)
372 {
373   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) GST_PAD_PARENT (pad);
374   GstFFMpegEncClass *oclass =
375       (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
376   AVCodecContext *ctx = NULL;
377   enum PixelFormat pixfmt;
378   GstCaps *caps = NULL;
379   GstCaps *finalcaps = NULL;
380   gint i;
381
382   GST_DEBUG_OBJECT (ffmpegenc, "getting caps");
383
384   /* audio needs no special care */
385   if (oclass->in_plugin->type == CODEC_TYPE_AUDIO) {
386     caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
387
388     GST_DEBUG_OBJECT (ffmpegenc, "audio caps, return template %" GST_PTR_FORMAT,
389         caps);
390
391     return caps;
392   }
393
394   /* cached */
395   if (oclass->sinkcaps) {
396     caps = gst_ffmpegenc_get_possible_sizes (ffmpegenc, pad, oclass->sinkcaps);
397     GST_DEBUG_OBJECT (ffmpegenc, "return cached caps %" GST_PTR_FORMAT, caps);
398     return caps;
399   }
400
401   /* create cache etc. */
402
403   /* shut up the logging while we autoprobe; we don't want warnings and
404    * errors about unsupported formats */
405   /* FIXME: if someone cares about this disabling the logging for other
406    * instances/threads/..., one could investigate if there is a way to
407    * set this as a struct member on the av context, and check it from the
408    * log handler */
409 #ifndef GST_DISABLE_GST_DEBUG
410   _shut_up_I_am_probing = TRUE;
411 #endif
412   GST_DEBUG_OBJECT (ffmpegenc, "probing caps");
413   i = pixfmt = 0;
414   /* check pixfmt until deemed finished */
415   for (pixfmt = 0;; pixfmt++) {
416     GstCaps *tmpcaps;
417
418     /* override looping all pixfmt if codec declares pixfmts;
419      * these may not properly check and report supported pixfmt during _init */
420     if (oclass->in_plugin->pix_fmts) {
421       if ((pixfmt = oclass->in_plugin->pix_fmts[i++]) == PIX_FMT_NONE) {
422         GST_DEBUG_OBJECT (ffmpegenc,
423             "At the end of official pixfmt for this codec, breaking out");
424         break;
425       }
426       GST_DEBUG_OBJECT (ffmpegenc,
427           "Got an official pixfmt [%d], attempting to get caps", pixfmt);
428       tmpcaps = gst_ffmpeg_pixfmt_to_caps (pixfmt, NULL, oclass->in_plugin->id);
429       if (tmpcaps) {
430         GST_DEBUG_OBJECT (ffmpegenc, "Got caps, breaking out");
431         if (!caps)
432           caps = gst_caps_new_empty ();
433         gst_caps_append (caps, tmpcaps);
434         continue;
435       }
436       GST_DEBUG_OBJECT (ffmpegenc,
437           "Couldn't figure out caps without context, trying again with a context");
438     }
439
440     GST_DEBUG_OBJECT (ffmpegenc, "pixfmt :%d", pixfmt);
441     if (pixfmt >= PIX_FMT_NB) {
442       GST_WARNING ("Invalid pixfmt, breaking out");
443       break;
444     }
445
446     /* need to start with a fresh codec_context each time around, since
447      * codec_close may have released stuff causing the next pass to segfault */
448     ctx = avcodec_alloc_context ();
449     if (!ctx) {
450       GST_DEBUG_OBJECT (ffmpegenc, "no context");
451       break;
452     }
453
454     /* set some default properties */
455     ctx->width = DEFAULT_WIDTH;
456     ctx->height = DEFAULT_HEIGHT;
457     ctx->time_base.num = 1;
458     ctx->time_base.den = 25;
459     ctx->ticks_per_frame = 1;
460     ctx->bit_rate = DEFAULT_VIDEO_BITRATE;
461     /* makes it silent */
462     ctx->strict_std_compliance = -1;
463
464     ctx->pix_fmt = pixfmt;
465
466     GST_DEBUG ("Attempting to open codec");
467     if (gst_ffmpeg_avcodec_open (ctx, oclass->in_plugin) >= 0 &&
468         ctx->pix_fmt == pixfmt) {
469       ctx->width = -1;
470       if (!caps)
471         caps = gst_caps_new_empty ();
472       tmpcaps = gst_ffmpeg_codectype_to_caps (oclass->in_plugin->type, ctx,
473           oclass->in_plugin->id, TRUE);
474       if (tmpcaps)
475         gst_caps_append (caps, tmpcaps);
476       else
477         GST_LOG_OBJECT (ffmpegenc,
478             "Couldn't get caps for oclass->in_plugin->name:%s",
479             oclass->in_plugin->name);
480       gst_ffmpeg_avcodec_close (ctx);
481     } else {
482       GST_DEBUG_OBJECT (ffmpegenc, "Opening codec failed with pixfmt : %d",
483           pixfmt);
484     }
485     if (ctx->priv_data)
486       gst_ffmpeg_avcodec_close (ctx);
487     av_free (ctx);
488   }
489 #ifndef GST_DISABLE_GST_DEBUG
490   _shut_up_I_am_probing = FALSE;
491 #endif
492
493   /* make sure we have something */
494   if (!caps) {
495     caps = gst_ffmpegenc_get_possible_sizes (ffmpegenc, pad,
496         gst_pad_get_pad_template_caps (pad));
497     GST_DEBUG_OBJECT (ffmpegenc, "probing gave nothing, "
498         "return template %" GST_PTR_FORMAT, caps);
499     return caps;
500   }
501
502   GST_DEBUG_OBJECT (ffmpegenc, "probed caps gave %" GST_PTR_FORMAT, caps);
503   oclass->sinkcaps = gst_caps_copy (caps);
504
505   finalcaps = gst_ffmpegenc_get_possible_sizes (ffmpegenc, pad, caps);
506   gst_caps_unref (caps);
507
508   return finalcaps;
509 }
510
511 static gboolean
512 gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps)
513 {
514   GstCaps *other_caps;
515   GstCaps *allowed_caps;
516   GstCaps *icaps;
517   enum PixelFormat pix_fmt;
518   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) GST_PAD_PARENT (pad);
519   GstFFMpegEncClass *oclass =
520       (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
521
522   /* close old session */
523   if (ffmpegenc->opened) {
524     gst_ffmpeg_avcodec_close (ffmpegenc->context);
525     ffmpegenc->opened = FALSE;
526   }
527
528   /* set defaults */
529   avcodec_get_context_defaults (ffmpegenc->context);
530
531   /* if we set it in _getcaps we should set it also in _link */
532   ffmpegenc->context->strict_std_compliance = -1;
533
534   /* user defined properties */
535   ffmpegenc->context->bit_rate = ffmpegenc->bitrate;
536   ffmpegenc->context->bit_rate_tolerance = ffmpegenc->bitrate;
537   ffmpegenc->context->gop_size = ffmpegenc->gop_size;
538   ffmpegenc->context->me_method = ffmpegenc->me_method;
539   GST_DEBUG_OBJECT (ffmpegenc, "Setting avcontext to bitrate %lu, gop_size %d",
540       ffmpegenc->bitrate, ffmpegenc->gop_size);
541
542   /* RTP payload used for GOB production (for Asterisk) */
543   if (ffmpegenc->rtp_payload_size) {
544     ffmpegenc->context->rtp_payload_size = ffmpegenc->rtp_payload_size;
545   }
546
547   /* additional avcodec settings */
548   /* first fill in the majority by copying over */
549   gst_ffmpeg_cfg_fill_context (ffmpegenc, ffmpegenc->context);
550
551   /* then handle some special cases */
552   ffmpegenc->context->lmin = (ffmpegenc->lmin * FF_QP2LAMBDA + 0.5);
553   ffmpegenc->context->lmax = (ffmpegenc->lmax * FF_QP2LAMBDA + 0.5);
554
555   if (ffmpegenc->interlaced) {
556     ffmpegenc->context->flags |=
557         CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME;
558     ffmpegenc->picture->interlaced_frame = TRUE;
559     /* if this is not the case, a filter element should be used to swap fields */
560     ffmpegenc->picture->top_field_first = TRUE;
561   }
562
563   /* some other defaults */
564   ffmpegenc->context->rc_strategy = 2;
565   ffmpegenc->context->b_frame_strategy = 0;
566   ffmpegenc->context->coder_type = 0;
567   ffmpegenc->context->context_model = 0;
568   ffmpegenc->context->scenechange_threshold = 0;
569   ffmpegenc->context->inter_threshold = 0;
570
571   /* and last but not least the pass; CBR, 2-pass, etc */
572   ffmpegenc->context->flags |= ffmpegenc->pass;
573   switch (ffmpegenc->pass) {
574       /* some additional action depends on type of pass */
575     case CODEC_FLAG_QSCALE:
576       ffmpegenc->context->global_quality
577           = ffmpegenc->picture->quality = FF_QP2LAMBDA * ffmpegenc->quantizer;
578       break;
579     case CODEC_FLAG_PASS1:     /* need to prepare a stats file */
580       /* we don't close when changing caps, fingers crossed */
581       if (!ffmpegenc->file)
582         ffmpegenc->file = g_fopen (ffmpegenc->filename, "w");
583       if (!ffmpegenc->file) {
584         GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, OPEN_WRITE,
585             (("Could not open file \"%s\" for writing."), ffmpegenc->filename),
586             GST_ERROR_SYSTEM);
587         return FALSE;
588       }
589       break;
590     case CODEC_FLAG_PASS2:
591     {                           /* need to read the whole stats file ! */
592       gsize size;
593
594       if (!g_file_get_contents (ffmpegenc->filename,
595               &ffmpegenc->context->stats_in, &size, NULL)) {
596         GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, READ,
597             (("Could not get contents of file \"%s\"."), ffmpegenc->filename),
598             GST_ERROR_SYSTEM);
599         return FALSE;
600       }
601
602       break;
603     }
604     default:
605       break;
606   }
607
608   /* fetch pix_fmt and so on */
609   gst_ffmpeg_caps_with_codectype (oclass->in_plugin->type,
610       caps, ffmpegenc->context);
611   if (!ffmpegenc->context->time_base.den) {
612     ffmpegenc->context->time_base.den = 25;
613     ffmpegenc->context->time_base.num = 1;
614     ffmpegenc->context->ticks_per_frame = 1;
615   } else if ((oclass->in_plugin->id == CODEC_ID_MPEG4)
616       && (ffmpegenc->context->time_base.den > 65535)) {
617     /* MPEG4 Standards do not support time_base denominator greater than
618      * (1<<16) - 1 . We therefore scale them down.
619      * Agreed, it will not be the exact framerate... but the difference
620      * shouldn't be that noticeable */
621     ffmpegenc->context->time_base.num =
622         (gint) gst_util_uint64_scale_int (ffmpegenc->context->time_base.num,
623         65535, ffmpegenc->context->time_base.den);
624     ffmpegenc->context->time_base.den = 65535;
625     GST_LOG_OBJECT (ffmpegenc, "MPEG4 : scaled down framerate to %d / %d",
626         ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num);
627   }
628
629   pix_fmt = ffmpegenc->context->pix_fmt;
630
631   /* max-key-interval may need the framerate set above */
632   if (ffmpegenc->max_key_interval) {
633     AVCodecContext *ctx;
634
635     /* override gop-size */
636     ctx = ffmpegenc->context;
637     ctx->gop_size = (ffmpegenc->max_key_interval < 0) ?
638         (-ffmpegenc->max_key_interval
639         * (ctx->time_base.den * ctx->ticks_per_frame / ctx->time_base.num))
640         : ffmpegenc->max_key_interval;
641   }
642
643   /* open codec */
644   if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) {
645     if (ffmpegenc->context->priv_data)
646       gst_ffmpeg_avcodec_close (ffmpegenc->context);
647     if (ffmpegenc->context->stats_in)
648       g_free (ffmpegenc->context->stats_in);
649     GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to open FFMPEG codec",
650         oclass->in_plugin->name);
651     return FALSE;
652   }
653
654   /* second pass stats buffer no longer needed */
655   if (ffmpegenc->context->stats_in)
656     g_free (ffmpegenc->context->stats_in);
657
658   /* is the colourspace correct? */
659   if (pix_fmt != ffmpegenc->context->pix_fmt) {
660     gst_ffmpeg_avcodec_close (ffmpegenc->context);
661     GST_DEBUG_OBJECT (ffmpegenc,
662         "ffenc_%s: AV wants different colourspace (%d given, %d wanted)",
663         oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt);
664     return FALSE;
665   }
666   /* we may have failed mapping caps to a pixfmt,
667    * and quite some codecs do not make up their own mind about that
668    * in any case, _NONE can never work out later on */
669   if (oclass->in_plugin->type == CODEC_TYPE_VIDEO && pix_fmt == PIX_FMT_NONE) {
670     GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to determine input format",
671         oclass->in_plugin->name);
672     return FALSE;
673   }
674
675   /* some codecs support more than one format, first auto-choose one */
676   GST_DEBUG_OBJECT (ffmpegenc, "picking an output format ...");
677   allowed_caps = gst_pad_get_allowed_caps (ffmpegenc->srcpad);
678   if (!allowed_caps) {
679     GST_DEBUG_OBJECT (ffmpegenc, "... but no peer, using template caps");
680     /* we need to copy because get_allowed_caps returns a ref, and
681      * get_pad_template_caps doesn't */
682     allowed_caps =
683         gst_caps_copy (gst_pad_get_pad_template_caps (ffmpegenc->srcpad));
684   }
685   GST_DEBUG_OBJECT (ffmpegenc, "chose caps %" GST_PTR_FORMAT, allowed_caps);
686   gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
687       oclass->in_plugin->type, allowed_caps, ffmpegenc->context);
688
689   /* try to set this caps on the other side */
690   other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id,
691       ffmpegenc->context, TRUE);
692
693   if (!other_caps) {
694     gst_ffmpeg_avcodec_close (ffmpegenc->context);
695     GST_DEBUG ("Unsupported codec - no caps found");
696     return FALSE;
697   }
698
699   icaps = gst_caps_intersect (allowed_caps, other_caps);
700   gst_caps_unref (allowed_caps);
701   gst_caps_unref (other_caps);
702   if (gst_caps_is_empty (icaps)) {
703     gst_caps_unref (icaps);
704     return FALSE;
705   }
706
707   if (gst_caps_get_size (icaps) > 1) {
708     GstCaps *newcaps;
709
710     newcaps =
711         gst_caps_new_full (gst_structure_copy (gst_caps_get_structure (icaps,
712                 0)), NULL);
713     gst_caps_unref (icaps);
714     icaps = newcaps;
715   }
716
717   if (!gst_pad_set_caps (ffmpegenc->srcpad, icaps)) {
718     gst_ffmpeg_avcodec_close (ffmpegenc->context);
719     gst_caps_unref (icaps);
720     return FALSE;
721   }
722   gst_caps_unref (icaps);
723
724   /* success! */
725   ffmpegenc->opened = TRUE;
726
727   return TRUE;
728 }
729
730 static void
731 ffmpegenc_setup_working_buf (GstFFMpegEnc * ffmpegenc)
732 {
733   guint wanted_size =
734       ffmpegenc->context->width * ffmpegenc->context->height * 6 +
735       FF_MIN_BUFFER_SIZE;
736
737   /* Above is the buffer size used by ffmpeg/ffmpeg.c */
738
739   if (ffmpegenc->working_buf == NULL ||
740       ffmpegenc->working_buf_size != wanted_size) {
741     if (ffmpegenc->working_buf)
742       g_free (ffmpegenc->working_buf);
743     ffmpegenc->working_buf_size = wanted_size;
744     ffmpegenc->working_buf = g_malloc (ffmpegenc->working_buf_size);
745   }
746   ffmpegenc->buffer_size = wanted_size;
747 }
748
749 static GstFlowReturn
750 gst_ffmpegenc_chain_video (GstPad * pad, GstBuffer * inbuf)
751 {
752   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
753   GstBuffer *outbuf;
754   gint ret_size = 0, frame_size;
755   gboolean force_keyframe;
756
757   GST_DEBUG_OBJECT (ffmpegenc,
758       "Received buffer of time %" GST_TIME_FORMAT,
759       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
760
761   GST_OBJECT_LOCK (ffmpegenc);
762   force_keyframe = ffmpegenc->force_keyframe;
763   ffmpegenc->force_keyframe = FALSE;
764   GST_OBJECT_UNLOCK (ffmpegenc);
765
766   if (force_keyframe)
767     ffmpegenc->picture->pict_type = FF_I_TYPE;
768
769   frame_size = gst_ffmpeg_avpicture_fill ((AVPicture *) ffmpegenc->picture,
770       GST_BUFFER_DATA (inbuf),
771       ffmpegenc->context->pix_fmt,
772       ffmpegenc->context->width, ffmpegenc->context->height);
773   g_return_val_if_fail (frame_size == GST_BUFFER_SIZE (inbuf), GST_FLOW_ERROR);
774
775   ffmpegenc->picture->pts =
776       gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (inbuf) /
777       ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base);
778
779   ffmpegenc_setup_working_buf (ffmpegenc);
780
781   ret_size = avcodec_encode_video (ffmpegenc->context,
782       ffmpegenc->working_buf, ffmpegenc->working_buf_size, ffmpegenc->picture);
783
784   if (ret_size < 0) {
785 #ifndef GST_DISABLE_GST_DEBUG
786     GstFFMpegEncClass *oclass =
787         (GstFFMpegEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
788     GST_ERROR_OBJECT (ffmpegenc,
789         "ffenc_%s: failed to encode buffer", oclass->in_plugin->name);
790 #endif /* GST_DISABLE_GST_DEBUG */
791     gst_buffer_unref (inbuf);
792     return GST_FLOW_OK;
793   }
794
795   /* handle b-frame delay when no output, so we don't output empty frames;
796    * timestamps and so can permute a bit between coding and display order
797    * but keyframes should still end up with the proper metadata */
798   g_queue_push_tail (ffmpegenc->delay, inbuf);
799   if (ret_size)
800     inbuf = g_queue_pop_head (ffmpegenc->delay);
801   else
802     return GST_FLOW_OK;
803
804   /* save stats info if there is some as well as a stats file */
805   if (ffmpegenc->file && ffmpegenc->context->stats_out)
806     if (fprintf (ffmpegenc->file, "%s", ffmpegenc->context->stats_out) < 0)
807       GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, WRITE,
808           (("Could not write to file \"%s\"."), ffmpegenc->filename),
809           GST_ERROR_SYSTEM);
810
811   outbuf = gst_buffer_new_and_alloc (ret_size);
812   memcpy (GST_BUFFER_DATA (outbuf), ffmpegenc->working_buf, ret_size);
813   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (inbuf);
814   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (inbuf);
815   /* buggy codec may not set coded_frame */
816   if (ffmpegenc->context->coded_frame) {
817     if (!ffmpegenc->context->coded_frame->key_frame)
818       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
819   } else
820     GST_WARNING_OBJECT (ffmpegenc, "codec did not provide keyframe info");
821   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (ffmpegenc->srcpad));
822
823   gst_buffer_unref (inbuf);
824
825   /* Reset frame type */
826   if (ffmpegenc->picture->pict_type)
827     ffmpegenc->picture->pict_type = 0;
828
829   if (force_keyframe) {
830     gst_pad_push_event (ffmpegenc->srcpad,
831         gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
832             gst_structure_new ("GstForceKeyUnit",
833                 "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (outbuf),
834                 NULL)));
835   }
836
837   return gst_pad_push (ffmpegenc->srcpad, outbuf);
838 }
839
840 static GstFlowReturn
841 gst_ffmpegenc_encode_audio (GstFFMpegEnc * ffmpegenc, guint8 * audio_in,
842     guint max_size, GstClockTime timestamp, GstClockTime duration,
843     gboolean discont)
844 {
845   GstBuffer *outbuf;
846   AVCodecContext *ctx;
847   guint8 *audio_out;
848   gint res;
849   GstFlowReturn ret;
850
851   ctx = ffmpegenc->context;
852
853   outbuf = gst_buffer_new_and_alloc (max_size);
854   audio_out = GST_BUFFER_DATA (outbuf);
855
856   GST_LOG_OBJECT (ffmpegenc, "encoding buffer of max size %d", max_size);
857   if (ffmpegenc->buffer_size != max_size)
858     ffmpegenc->buffer_size = max_size;
859
860   res = avcodec_encode_audio (ctx, audio_out, max_size, (short *) audio_in);
861
862   if (res < 0) {
863     GST_ERROR_OBJECT (ffmpegenc, "Failed to encode buffer: %d", res);
864     gst_buffer_unref (outbuf);
865     return GST_FLOW_OK;
866   }
867   GST_LOG_OBJECT (ffmpegenc, "got output size %d", res);
868
869   GST_BUFFER_SIZE (outbuf) = res;
870   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
871   GST_BUFFER_DURATION (outbuf) = duration;
872   if (discont)
873     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
874   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (ffmpegenc->srcpad));
875
876   GST_LOG_OBJECT (ffmpegenc, "pushing size %d, timestamp %" GST_TIME_FORMAT,
877       res, GST_TIME_ARGS (timestamp));
878
879   ret = gst_pad_push (ffmpegenc->srcpad, outbuf);
880
881   return ret;
882 }
883
884 static GstFlowReturn
885 gst_ffmpegenc_chain_audio (GstPad * pad, GstBuffer * inbuf)
886 {
887   GstFFMpegEnc *ffmpegenc;
888   GstFFMpegEncClass *oclass;
889   AVCodecContext *ctx;
890   GstClockTime timestamp, duration;
891   guint size, frame_size;
892   gint osize;
893   GstFlowReturn ret;
894   gint out_size;
895   gboolean discont;
896   guint8 *in_data;
897
898   ffmpegenc = (GstFFMpegEnc *) (GST_OBJECT_PARENT (pad));
899   oclass = (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
900
901   ctx = ffmpegenc->context;
902
903   size = GST_BUFFER_SIZE (inbuf);
904   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
905   duration = GST_BUFFER_DURATION (inbuf);
906   discont = GST_BUFFER_IS_DISCONT (inbuf);
907
908   GST_DEBUG_OBJECT (ffmpegenc,
909       "Received time %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
910       ", size %d", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration), size);
911
912   frame_size = ctx->frame_size;
913   osize = av_get_bits_per_sample_format (ctx->sample_fmt) / 8;
914
915   if (frame_size > 1) {
916     /* we have a frame_size, feed the encoder multiples of this frame size */
917     guint avail, frame_bytes;
918
919     if (discont) {
920       GST_LOG_OBJECT (ffmpegenc, "DISCONT, clear adapter");
921       gst_adapter_clear (ffmpegenc->adapter);
922       ffmpegenc->discont = TRUE;
923     }
924
925     if (gst_adapter_available (ffmpegenc->adapter) == 0) {
926       /* lock on to new timestamp */
927       GST_LOG_OBJECT (ffmpegenc, "taking buffer timestamp %" GST_TIME_FORMAT,
928           GST_TIME_ARGS (timestamp));
929       ffmpegenc->adapter_ts = timestamp;
930       ffmpegenc->adapter_consumed = 0;
931     } else {
932       GstClockTime upstream_time;
933       GstClockTime consumed_time;
934       guint64 bytes;
935
936       /* use timestamp at head of the adapter */
937       consumed_time =
938           gst_util_uint64_scale (ffmpegenc->adapter_consumed, GST_SECOND,
939           ctx->sample_rate);
940       timestamp = ffmpegenc->adapter_ts + consumed_time;
941       GST_LOG_OBJECT (ffmpegenc, "taking adapter timestamp %" GST_TIME_FORMAT
942           " and adding consumed time %" GST_TIME_FORMAT,
943           GST_TIME_ARGS (ffmpegenc->adapter_ts), GST_TIME_ARGS (consumed_time));
944
945       /* check with upstream timestamps, if too much deviation,
946        * forego some timestamp perfection in favour of upstream syncing
947        * (particularly in case these do not happen to come in multiple
948        * of frame size) */
949       upstream_time = gst_adapter_prev_timestamp (ffmpegenc->adapter, &bytes);
950       if (GST_CLOCK_TIME_IS_VALID (upstream_time)) {
951         GstClockTimeDiff diff;
952
953         upstream_time +=
954             gst_util_uint64_scale (bytes, GST_SECOND,
955             ctx->sample_rate * osize * ctx->channels);
956         diff = upstream_time - timestamp;
957         /* relaxed difference, rather than half a sample or so ... */
958         if (diff > GST_SECOND / 10 || diff < -GST_SECOND / 10) {
959           GST_DEBUG_OBJECT (ffmpegenc, "adapter timestamp drifting, "
960               "taking upstream timestamp %" GST_TIME_FORMAT,
961               GST_TIME_ARGS (upstream_time));
962           timestamp = upstream_time;
963           /* samples corresponding to bytes */
964           ffmpegenc->adapter_consumed = bytes / (osize * ctx->channels);
965           ffmpegenc->adapter_ts = upstream_time -
966               gst_util_uint64_scale (ffmpegenc->adapter_consumed, GST_SECOND,
967               ctx->sample_rate);
968           ffmpegenc->discont = TRUE;
969         }
970       }
971     }
972
973     GST_LOG_OBJECT (ffmpegenc, "pushing buffer in adapter");
974     gst_adapter_push (ffmpegenc->adapter, inbuf);
975
976     /* first see how many bytes we need to feed to the decoder. */
977     frame_bytes = frame_size * osize * ctx->channels;
978     avail = gst_adapter_available (ffmpegenc->adapter);
979
980     GST_LOG_OBJECT (ffmpegenc, "frame_bytes %u, avail %u", frame_bytes, avail);
981
982     /* while there is more than a frame size in the adapter, consume it */
983     while (avail >= frame_bytes) {
984       GST_LOG_OBJECT (ffmpegenc, "taking %u bytes from the adapter",
985           frame_bytes);
986
987       /* Note that we take frame_bytes and add frame_size.
988        * Makes sense when resyncing because you don't have to count channels
989        * or samplesize to divide by the samplerate */
990
991       /* take an audio buffer out of the adapter */
992       in_data = (guint8 *) gst_adapter_peek (ffmpegenc->adapter, frame_bytes);
993       ffmpegenc->adapter_consumed += frame_size;
994
995       /* calculate timestamp and duration relative to start of adapter and to
996        * the amount of samples we consumed */
997       duration =
998           gst_util_uint64_scale (ffmpegenc->adapter_consumed, GST_SECOND,
999           ctx->sample_rate);
1000       duration -= (timestamp - ffmpegenc->adapter_ts);
1001
1002       /* 4 times the input size plus the minimal buffer size
1003        * should be big enough... */
1004       out_size = frame_bytes * 4 + FF_MIN_BUFFER_SIZE;
1005
1006       ret = gst_ffmpegenc_encode_audio (ffmpegenc, in_data, out_size,
1007           timestamp, duration, ffmpegenc->discont);
1008
1009       gst_adapter_flush (ffmpegenc->adapter, frame_bytes);
1010
1011       if (ret != GST_FLOW_OK)
1012         goto push_failed;
1013
1014       /* advance the adapter timestamp with the duration */
1015       timestamp += duration;
1016
1017       ffmpegenc->discont = FALSE;
1018       avail = gst_adapter_available (ffmpegenc->adapter);
1019     }
1020     GST_LOG_OBJECT (ffmpegenc, "%u bytes left in the adapter", avail);
1021   } else {
1022     /* we have no frame_size, feed the encoder all the data and expect a fixed
1023      * output size */
1024     int coded_bps = av_get_bits_per_sample (oclass->in_plugin->id) / 8;
1025
1026     GST_LOG_OBJECT (ffmpegenc, "coded bps %d, osize %d", coded_bps, osize);
1027
1028     out_size = size / osize;
1029     if (coded_bps)
1030       out_size *= coded_bps;
1031
1032     /* We need to provide at least ffmpegs minimal buffer size */
1033     out_size += FF_MIN_BUFFER_SIZE;
1034
1035     in_data = (guint8 *) GST_BUFFER_DATA (inbuf);
1036     ret = gst_ffmpegenc_encode_audio (ffmpegenc, in_data, out_size,
1037         timestamp, duration, discont);
1038     gst_buffer_unref (inbuf);
1039
1040     if (ret != GST_FLOW_OK)
1041       goto push_failed;
1042   }
1043
1044   return GST_FLOW_OK;
1045
1046   /* ERRORS */
1047 push_failed:
1048   {
1049     GST_DEBUG_OBJECT (ffmpegenc, "Failed to push buffer %d (%s)", ret,
1050         gst_flow_get_name (ret));
1051     return ret;
1052   }
1053 }
1054
1055 static void
1056 gst_ffmpegenc_flush_buffers (GstFFMpegEnc * ffmpegenc, gboolean send)
1057 {
1058   GstBuffer *outbuf, *inbuf;
1059   gint ret_size;
1060
1061   GST_DEBUG_OBJECT (ffmpegenc, "flushing buffers with sending %d", send);
1062
1063   /* no need to empty codec if there is none */
1064   if (!ffmpegenc->opened)
1065     goto flush;
1066
1067   while (!g_queue_is_empty (ffmpegenc->delay)) {
1068
1069     ffmpegenc_setup_working_buf (ffmpegenc);
1070
1071     ret_size = avcodec_encode_video (ffmpegenc->context,
1072         ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL);
1073
1074     if (ret_size < 0) {         /* there should be something, notify and give up */
1075 #ifndef GST_DISABLE_GST_DEBUG
1076       GstFFMpegEncClass *oclass =
1077           (GstFFMpegEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
1078       GST_WARNING_OBJECT (ffmpegenc,
1079           "ffenc_%s: failed to flush buffer", oclass->in_plugin->name);
1080 #endif /* GST_DISABLE_GST_DEBUG */
1081       break;
1082     }
1083
1084     /* save stats info if there is some as well as a stats file */
1085     if (ffmpegenc->file && ffmpegenc->context->stats_out)
1086       if (fprintf (ffmpegenc->file, "%s", ffmpegenc->context->stats_out) < 0)
1087         GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, WRITE,
1088             (("Could not write to file \"%s\"."), ffmpegenc->filename),
1089             GST_ERROR_SYSTEM);
1090
1091     /* handle b-frame delay when no output, so we don't output empty frames */
1092     inbuf = g_queue_pop_head (ffmpegenc->delay);
1093
1094     outbuf = gst_buffer_new_and_alloc (ret_size);
1095     memcpy (GST_BUFFER_DATA (outbuf), ffmpegenc->working_buf, ret_size);
1096     GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (inbuf);
1097     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (inbuf);
1098
1099     if (!ffmpegenc->context->coded_frame->key_frame)
1100       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1101     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (ffmpegenc->srcpad));
1102
1103     gst_buffer_unref (inbuf);
1104
1105     if (send)
1106       gst_pad_push (ffmpegenc->srcpad, outbuf);
1107     else
1108       gst_buffer_unref (outbuf);
1109   }
1110
1111 flush:
1112   {
1113     /* make sure that we empty the queue, is still needed if we had to break */
1114     while (!g_queue_is_empty (ffmpegenc->delay))
1115       gst_buffer_unref (g_queue_pop_head (ffmpegenc->delay));
1116   }
1117 }
1118
1119 static gboolean
1120 gst_ffmpegenc_event_video (GstPad * pad, GstEvent * event)
1121 {
1122   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
1123
1124   switch (GST_EVENT_TYPE (event)) {
1125     case GST_EVENT_EOS:
1126       gst_ffmpegenc_flush_buffers (ffmpegenc, TRUE);
1127       break;
1128       /* no flushing if flush received,
1129        * buffers in encoder are considered (in the) past */
1130
1131     case GST_EVENT_CUSTOM_DOWNSTREAM:{
1132       const GstStructure *s;
1133       s = gst_event_get_structure (event);
1134       if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1135         ffmpegenc->picture->pict_type = FF_I_TYPE;
1136       }
1137       break;
1138     }
1139     default:
1140       break;
1141   }
1142
1143   return gst_pad_push_event (ffmpegenc->srcpad, event);
1144 }
1145
1146 static gboolean
1147 gst_ffmpegenc_event_src (GstPad * pad, GstEvent * event)
1148 {
1149   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
1150   gboolean forward = TRUE;
1151
1152   switch (GST_EVENT_TYPE (event)) {
1153     case GST_EVENT_CUSTOM_UPSTREAM:{
1154       const GstStructure *s;
1155       s = gst_event_get_structure (event);
1156       if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1157         GST_OBJECT_LOCK (ffmpegenc);
1158         ffmpegenc->force_keyframe = TRUE;
1159         GST_OBJECT_UNLOCK (ffmpegenc);
1160         forward = FALSE;
1161         gst_event_unref (event);
1162       }
1163       break;
1164     }
1165
1166     default:
1167       break;
1168   }
1169
1170   if (forward)
1171     return gst_pad_push_event (ffmpegenc->sinkpad, event);
1172   else
1173     return TRUE;
1174 }
1175
1176 static void
1177 gst_ffmpegenc_set_property (GObject * object,
1178     guint prop_id, const GValue * value, GParamSpec * pspec)
1179 {
1180   GstFFMpegEnc *ffmpegenc;
1181
1182   /* Get a pointer of the right type. */
1183   ffmpegenc = (GstFFMpegEnc *) (object);
1184
1185   if (ffmpegenc->opened) {
1186     GST_WARNING_OBJECT (ffmpegenc,
1187         "Can't change properties once decoder is setup !");
1188     return;
1189   }
1190
1191   /* Check the argument id to see which argument we're setting. */
1192   switch (prop_id) {
1193     case ARG_BIT_RATE:
1194       ffmpegenc->bitrate = g_value_get_ulong (value);
1195       break;
1196     case ARG_GOP_SIZE:
1197       ffmpegenc->gop_size = g_value_get_int (value);
1198       break;
1199     case ARG_ME_METHOD:
1200       ffmpegenc->me_method = g_value_get_enum (value);
1201       break;
1202     case ARG_BUFSIZE:
1203       break;
1204     case ARG_RTP_PAYLOAD_SIZE:
1205       ffmpegenc->rtp_payload_size = g_value_get_ulong (value);
1206       break;
1207     default:
1208       if (!gst_ffmpeg_cfg_set_property (object, value, pspec))
1209         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1210       break;
1211   }
1212 }
1213
1214 /* The set function is simply the inverse of the get fuction. */
1215 static void
1216 gst_ffmpegenc_get_property (GObject * object,
1217     guint prop_id, GValue * value, GParamSpec * pspec)
1218 {
1219   GstFFMpegEnc *ffmpegenc;
1220
1221   /* It's not null if we got it, but it might not be ours */
1222   ffmpegenc = (GstFFMpegEnc *) (object);
1223
1224   switch (prop_id) {
1225     case ARG_BIT_RATE:
1226       g_value_set_ulong (value, ffmpegenc->bitrate);
1227       break;
1228     case ARG_GOP_SIZE:
1229       g_value_set_int (value, ffmpegenc->gop_size);
1230       break;
1231     case ARG_ME_METHOD:
1232       g_value_set_enum (value, ffmpegenc->me_method);
1233       break;
1234     case ARG_BUFSIZE:
1235       g_value_set_ulong (value, ffmpegenc->buffer_size);
1236       break;
1237     case ARG_RTP_PAYLOAD_SIZE:
1238       g_value_set_ulong (value, ffmpegenc->rtp_payload_size);
1239       break;
1240     default:
1241       if (!gst_ffmpeg_cfg_get_property (object, value, pspec))
1242         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1243       break;
1244   }
1245 }
1246
1247 static GstStateChangeReturn
1248 gst_ffmpegenc_change_state (GstElement * element, GstStateChange transition)
1249 {
1250   GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) element;
1251   GstStateChangeReturn result;
1252
1253   switch (transition) {
1254     default:
1255       break;
1256   }
1257
1258   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1259
1260   switch (transition) {
1261     case GST_STATE_CHANGE_PAUSED_TO_READY:
1262       gst_ffmpegenc_flush_buffers (ffmpegenc, FALSE);
1263       if (ffmpegenc->opened) {
1264         gst_ffmpeg_avcodec_close (ffmpegenc->context);
1265         ffmpegenc->opened = FALSE;
1266       }
1267       gst_adapter_clear (ffmpegenc->adapter);
1268
1269       if (ffmpegenc->file) {
1270         fclose (ffmpegenc->file);
1271         ffmpegenc->file = NULL;
1272       }
1273       if (ffmpegenc->working_buf) {
1274         g_free (ffmpegenc->working_buf);
1275         ffmpegenc->working_buf = NULL;
1276       }
1277       break;
1278     default:
1279       break;
1280   }
1281   return result;
1282 }
1283
1284 gboolean
1285 gst_ffmpegenc_register (GstPlugin * plugin)
1286 {
1287   GTypeInfo typeinfo = {
1288     sizeof (GstFFMpegEncClass),
1289     (GBaseInitFunc) gst_ffmpegenc_base_init,
1290     NULL,
1291     (GClassInitFunc) gst_ffmpegenc_class_init,
1292     NULL,
1293     NULL,
1294     sizeof (GstFFMpegEnc),
1295     0,
1296     (GInstanceInitFunc) gst_ffmpegenc_init,
1297   };
1298   GType type;
1299   AVCodec *in_plugin;
1300
1301
1302   GST_LOG ("Registering encoders");
1303
1304   /* build global ffmpeg param/property info */
1305   gst_ffmpeg_cfg_init ();
1306
1307   in_plugin = av_codec_next (NULL);
1308   while (in_plugin) {
1309     gchar *type_name;
1310
1311     /* no quasi codecs, please */
1312     if (in_plugin->id == CODEC_ID_RAWVIDEO ||
1313         in_plugin->id == CODEC_ID_V210 ||
1314         in_plugin->id == CODEC_ID_V210X ||
1315         in_plugin->id == CODEC_ID_R210 ||
1316         in_plugin->id == CODEC_ID_ZLIB ||
1317         (in_plugin->id >= CODEC_ID_PCM_S16LE &&
1318             in_plugin->id <= CODEC_ID_PCM_BLURAY)) {
1319       goto next;
1320     }
1321
1322     /* No encoders depending on external libraries (we don't build them, but
1323      * people who build against an external ffmpeg might have them.
1324      * We have native gstreamer plugins for all of those libraries anyway. */
1325     if (!strncmp (in_plugin->name, "lib", 3)) {
1326       GST_DEBUG
1327           ("Not using external library encoder %s. Use the gstreamer-native ones instead.",
1328           in_plugin->name);
1329       goto next;
1330     }
1331
1332     /* only encoders */
1333     if (!in_plugin->encode) {
1334       goto next;
1335     }
1336
1337     /* FIXME : We should have a method to know cheaply whether we have a mapping
1338      * for the given plugin or not */
1339
1340     GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
1341
1342     /* no codecs for which we're GUARANTEED to have better alternatives */
1343     if (!strcmp (in_plugin->name, "vorbis") ||
1344         !strcmp (in_plugin->name, "gif") || !strcmp (in_plugin->name, "flac")) {
1345       GST_LOG ("Ignoring encoder %s", in_plugin->name);
1346       goto next;
1347     }
1348
1349     /* construct the type */
1350     type_name = g_strdup_printf ("ffenc_%s", in_plugin->name);
1351
1352     type = g_type_from_name (type_name);
1353
1354     if (!type) {
1355
1356       /* create the glib type now */
1357       type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
1358       g_type_set_qdata (type, GST_FFENC_PARAMS_QDATA, (gpointer) in_plugin);
1359
1360       {
1361         static const GInterfaceInfo preset_info = {
1362           NULL,
1363           NULL,
1364           NULL
1365         };
1366         g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_info);
1367       }
1368     }
1369
1370     if (!gst_element_register (plugin, type_name, GST_RANK_SECONDARY, type)) {
1371       g_free (type_name);
1372       return FALSE;
1373     }
1374
1375     g_free (type_name);
1376
1377   next:
1378     in_plugin = av_codec_next (in_plugin);
1379   }
1380
1381   GST_LOG ("Finished registering encoders");
1382
1383   return TRUE;
1384 }