2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
26 /* for stats file handling */
28 #include <glib/gstdio.h>
31 #ifdef HAVE_FFMPEG_UNINSTALLED
34 #include <libavcodec/avcodec.h>
39 #include "gstffmpeg.h"
40 #include "gstffmpegcodecmap.h"
41 #include "gstffmpegutils.h"
42 #include "gstffmpegenc.h"
43 #include "gstffmpegcfg.h"
45 #define DEFAULT_VIDEO_BITRATE 300000 /* in bps */
46 #define DEFAULT_VIDEO_GOP_SIZE 15
47 #define DEFAULT_AUDIO_BITRATE 128000
49 #define DEFAULT_WIDTH 352
50 #define DEFAULT_HEIGHT 288
53 #define VIDEO_BUFFER_SIZE (1024*1024)
72 #define GST_TYPE_ME_METHOD (gst_ffmpegenc_me_method_get_type())
74 gst_ffmpegenc_me_method_get_type (void)
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"},
86 if (!ffmpegenc_me_method_type) {
87 ffmpegenc_me_method_type =
88 g_enum_register_static ("GstFFMpegEncMeMethod", ffmpegenc_me_methods);
90 return ffmpegenc_me_method_type;
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);
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,
103 static GstFlowReturn gst_ffmpegenc_chain_audio (GstPad * pad,
105 static gboolean gst_ffmpegenc_event_video (GstPad * pad, GstEvent * event);
106 static gboolean gst_ffmpegenc_event_src (GstPad * pad, GstEvent * event);
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);
113 static GstStateChangeReturn gst_ffmpegenc_change_state (GstElement * element,
114 GstStateChange transition);
116 #define GST_FFENC_PARAMS_QDATA g_quark_from_static_string("ffenc-params")
118 static GstElementClass *parent_class = NULL;
120 /*static guint gst_ffmpegenc_signals[LAST_SIGNAL] = { 0 }; */
123 gst_ffmpegenc_base_init (GstFFMpegEncClass * klass)
125 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
127 GstPadTemplate *srctempl = NULL, *sinktempl = NULL;
128 GstCaps *srccaps = NULL, *sinkcaps = NULL;
129 gchar *longname, *classification, *description;
132 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
133 GST_FFENC_PARAMS_QDATA);
134 g_assert (in_plugin != NULL);
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,
143 "Wim Taymans <wim.taymans@gmail.com>, "
144 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
146 g_free (classification);
147 g_free (description);
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);
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");
158 sinkcaps = gst_ffmpeg_codectype_to_audio_caps (NULL,
159 in_plugin->id, TRUE, in_plugin);
162 GST_DEBUG ("Couldn't get sink caps for encoder '%s'", in_plugin->name);
163 sinkcaps = gst_caps_new_simple ("unknown/unknown", NULL);
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);
171 gst_element_class_add_pad_template (element_class, srctempl);
172 gst_element_class_add_pad_template (element_class, sinktempl);
174 klass->in_plugin = in_plugin;
175 klass->srctempl = srctempl;
176 klass->sinktempl = sinktempl;
177 klass->sinkcaps = NULL;
183 gst_ffmpegenc_class_init (GstFFMpegEncClass * klass)
185 GObjectClass *gobject_class;
186 GstElementClass *gstelement_class;
188 gobject_class = (GObjectClass *) klass;
189 gstelement_class = (GstElementClass *) klass;
191 parent_class = g_type_class_peek_parent (klass);
193 gobject_class->set_property = gst_ffmpegenc_set_property;
194 gobject_class->get_property = gst_ffmpegenc_get_property;
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,
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));
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,
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,
229 gstelement_class->change_state = gst_ffmpegenc_change_state;
231 gobject_class->finalize = gst_ffmpegenc_finalize;
235 gst_ffmpegenc_init (GstFFMpegEnc * ffmpegenc)
237 GstFFMpegEncClass *oclass =
238 (GstFFMpegEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
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);
248 ffmpegenc->context = avcodec_alloc_context ();
249 ffmpegenc->picture = avcodec_alloc_frame ();
250 ffmpegenc->opened = FALSE;
252 ffmpegenc->file = NULL;
253 ffmpegenc->delay = g_queue_new ();
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);
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;
268 ffmpegenc->lmax = 31;
269 ffmpegenc->max_key_interval = 0;
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);
275 ffmpegenc->bitrate = DEFAULT_AUDIO_BITRATE;
278 gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->sinkpad);
279 gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->srcpad);
281 ffmpegenc->adapter = gst_adapter_new ();
285 gst_ffmpegenc_finalize (GObject * object)
287 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) object;
289 gst_ffmpeg_cfg_finalize (ffmpegenc);
291 /* close old session */
292 if (ffmpegenc->opened) {
293 gst_ffmpeg_avcodec_close (ffmpegenc->context);
294 ffmpegenc->opened = FALSE;
297 /* clean up remaining allocated data */
298 av_free (ffmpegenc->context);
299 av_free (ffmpegenc->picture);
301 g_queue_free (ffmpegenc->delay);
302 g_free (ffmpegenc->filename);
304 g_object_unref (ffmpegenc->adapter);
306 G_OBJECT_CLASS (parent_class)->finalize (object);
310 gst_ffmpegenc_get_possible_sizes (GstFFMpegEnc * ffmpegenc, GstPad * pad,
311 const GstCaps * caps)
313 GstCaps *othercaps = NULL;
314 GstCaps *tmpcaps = NULL;
315 GstCaps *intersect = NULL;
318 othercaps = gst_pad_peer_get_caps (ffmpegenc->srcpad);
321 return gst_caps_copy (caps);
323 intersect = gst_caps_intersect (othercaps,
324 gst_pad_get_pad_template_caps (ffmpegenc->srcpad));
325 gst_caps_unref (othercaps);
327 if (gst_caps_is_empty (intersect))
330 if (gst_caps_is_any (intersect))
331 return gst_caps_copy (caps);
333 tmpcaps = gst_caps_new_empty ();
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;
342 height = gst_structure_get_value (s, "height");
343 width = gst_structure_get_value (s, "width");
344 framerate = gst_structure_get_value (s, "framerate");
346 tmps = gst_structure_new ("video/x-raw-rgb", NULL);
348 gst_structure_set_value (tmps, "width", width);
350 gst_structure_set_value (tmps, "height", height);
352 gst_structure_set_value (tmps, "framerate", framerate);
353 gst_caps_merge_structure (tmpcaps, gst_structure_copy (tmps));
355 gst_structure_set_name (tmps, "video/x-raw-yuv");
356 gst_caps_merge_structure (tmpcaps, gst_structure_copy (tmps));
358 gst_structure_set_name (tmps, "video/x-raw-gray");
359 gst_caps_merge_structure (tmpcaps, tmps);
361 gst_caps_unref (intersect);
363 intersect = gst_caps_intersect (caps, tmpcaps);
364 gst_caps_unref (tmpcaps);
371 gst_ffmpegenc_getcaps (GstPad * pad)
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;
382 GST_DEBUG_OBJECT (ffmpegenc, "getting caps");
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));
388 GST_DEBUG_OBJECT (ffmpegenc, "audio caps, return template %" GST_PTR_FORMAT,
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);
401 /* create cache etc. */
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
409 #ifndef GST_DISABLE_GST_DEBUG
410 _shut_up_I_am_probing = TRUE;
412 GST_DEBUG_OBJECT (ffmpegenc, "probing caps");
414 /* check pixfmt until deemed finished */
415 for (pixfmt = 0;; pixfmt++) {
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");
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);
430 GST_DEBUG_OBJECT (ffmpegenc, "Got caps, breaking out");
432 caps = gst_caps_new_empty ();
433 gst_caps_append (caps, tmpcaps);
436 GST_DEBUG_OBJECT (ffmpegenc,
437 "Couldn't figure out caps without context, trying again with a context");
440 GST_DEBUG_OBJECT (ffmpegenc, "pixfmt :%d", pixfmt);
441 if (pixfmt >= PIX_FMT_NB) {
442 GST_WARNING ("Invalid pixfmt, breaking out");
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 ();
450 GST_DEBUG_OBJECT (ffmpegenc, "no context");
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;
464 ctx->pix_fmt = pixfmt;
466 GST_DEBUG ("Attempting to open codec");
467 if (gst_ffmpeg_avcodec_open (ctx, oclass->in_plugin) >= 0 &&
468 ctx->pix_fmt == pixfmt) {
471 caps = gst_caps_new_empty ();
472 tmpcaps = gst_ffmpeg_codectype_to_caps (oclass->in_plugin->type, ctx,
473 oclass->in_plugin->id, TRUE);
475 gst_caps_append (caps, tmpcaps);
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);
482 GST_DEBUG_OBJECT (ffmpegenc, "Opening codec failed with pixfmt : %d",
486 gst_ffmpeg_avcodec_close (ctx);
489 #ifndef GST_DISABLE_GST_DEBUG
490 _shut_up_I_am_probing = FALSE;
493 /* make sure we have something */
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);
502 GST_DEBUG_OBJECT (ffmpegenc, "probed caps gave %" GST_PTR_FORMAT, caps);
503 oclass->sinkcaps = gst_caps_copy (caps);
505 finalcaps = gst_ffmpegenc_get_possible_sizes (ffmpegenc, pad, caps);
506 gst_caps_unref (caps);
512 gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps)
515 GstCaps *allowed_caps;
517 enum PixelFormat pix_fmt;
518 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) GST_PAD_PARENT (pad);
519 GstFFMpegEncClass *oclass =
520 (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
522 /* close old session */
523 if (ffmpegenc->opened) {
524 gst_ffmpeg_avcodec_close (ffmpegenc->context);
525 ffmpegenc->opened = FALSE;
529 avcodec_get_context_defaults (ffmpegenc->context);
531 /* if we set it in _getcaps we should set it also in _link */
532 ffmpegenc->context->strict_std_compliance = -1;
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);
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;
547 /* additional avcodec settings */
548 /* first fill in the majority by copying over */
549 gst_ffmpeg_cfg_fill_context (ffmpegenc, ffmpegenc->context);
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);
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;
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;
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;
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),
590 case CODEC_FLAG_PASS2:
591 { /* need to read the whole stats file ! */
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),
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);
629 pix_fmt = ffmpegenc->context->pix_fmt;
631 /* max-key-interval may need the framerate set above */
632 if (ffmpegenc->max_key_interval) {
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;
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);
654 /* second pass stats buffer no longer needed */
655 if (ffmpegenc->context->stats_in)
656 g_free (ffmpegenc->context->stats_in);
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);
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);
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);
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 */
683 gst_caps_copy (gst_pad_get_pad_template_caps (ffmpegenc->srcpad));
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);
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);
694 gst_ffmpeg_avcodec_close (ffmpegenc->context);
695 GST_DEBUG ("Unsupported codec - no caps found");
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);
707 if (gst_caps_get_size (icaps) > 1) {
711 gst_caps_new_full (gst_structure_copy (gst_caps_get_structure (icaps,
713 gst_caps_unref (icaps);
717 if (!gst_pad_set_caps (ffmpegenc->srcpad, icaps)) {
718 gst_ffmpeg_avcodec_close (ffmpegenc->context);
719 gst_caps_unref (icaps);
722 gst_caps_unref (icaps);
725 ffmpegenc->opened = TRUE;
731 ffmpegenc_setup_working_buf (GstFFMpegEnc * ffmpegenc)
734 ffmpegenc->context->width * ffmpegenc->context->height * 6 +
737 /* Above is the buffer size used by ffmpeg/ffmpeg.c */
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);
746 ffmpegenc->buffer_size = wanted_size;
750 gst_ffmpegenc_chain_video (GstPad * pad, GstBuffer * inbuf)
752 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
754 gint ret_size = 0, frame_size;
755 gboolean force_keyframe;
757 GST_DEBUG_OBJECT (ffmpegenc,
758 "Received buffer of time %" GST_TIME_FORMAT,
759 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
761 GST_OBJECT_LOCK (ffmpegenc);
762 force_keyframe = ffmpegenc->force_keyframe;
763 ffmpegenc->force_keyframe = FALSE;
764 GST_OBJECT_UNLOCK (ffmpegenc);
767 ffmpegenc->picture->pict_type = FF_I_TYPE;
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);
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);
779 ffmpegenc_setup_working_buf (ffmpegenc);
781 ret_size = avcodec_encode_video (ffmpegenc->context,
782 ffmpegenc->working_buf, ffmpegenc->working_buf_size, ffmpegenc->picture);
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);
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);
800 inbuf = g_queue_pop_head (ffmpegenc->delay);
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),
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);
820 GST_WARNING_OBJECT (ffmpegenc, "codec did not provide keyframe info");
821 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (ffmpegenc->srcpad));
823 gst_buffer_unref (inbuf);
825 /* Reset frame type */
826 if (ffmpegenc->picture->pict_type)
827 ffmpegenc->picture->pict_type = 0;
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),
837 return gst_pad_push (ffmpegenc->srcpad, outbuf);
841 gst_ffmpegenc_encode_audio (GstFFMpegEnc * ffmpegenc, guint8 * audio_in,
842 guint max_size, GstClockTime timestamp, GstClockTime duration,
851 ctx = ffmpegenc->context;
853 outbuf = gst_buffer_new_and_alloc (max_size);
854 audio_out = GST_BUFFER_DATA (outbuf);
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;
860 res = avcodec_encode_audio (ctx, audio_out, max_size, (short *) audio_in);
863 GST_ERROR_OBJECT (ffmpegenc, "Failed to encode buffer: %d", res);
864 gst_buffer_unref (outbuf);
867 GST_LOG_OBJECT (ffmpegenc, "got output size %d", res);
869 GST_BUFFER_SIZE (outbuf) = res;
870 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
871 GST_BUFFER_DURATION (outbuf) = duration;
873 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
874 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (ffmpegenc->srcpad));
876 GST_LOG_OBJECT (ffmpegenc, "pushing size %d, timestamp %" GST_TIME_FORMAT,
877 res, GST_TIME_ARGS (timestamp));
879 ret = gst_pad_push (ffmpegenc->srcpad, outbuf);
885 gst_ffmpegenc_chain_audio (GstPad * pad, GstBuffer * inbuf)
887 GstFFMpegEnc *ffmpegenc;
888 GstFFMpegEncClass *oclass;
890 GstClockTime timestamp, duration;
891 guint size, frame_size;
898 ffmpegenc = (GstFFMpegEnc *) (GST_OBJECT_PARENT (pad));
899 oclass = (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
901 ctx = ffmpegenc->context;
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);
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);
912 frame_size = ctx->frame_size;
913 osize = av_get_bits_per_sample_format (ctx->sample_fmt) / 8;
915 if (frame_size > 1) {
916 /* we have a frame_size, feed the encoder multiples of this frame size */
917 guint avail, frame_bytes;
920 GST_LOG_OBJECT (ffmpegenc, "DISCONT, clear adapter");
921 gst_adapter_clear (ffmpegenc->adapter);
922 ffmpegenc->discont = TRUE;
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;
932 GstClockTime upstream_time;
933 GstClockTime consumed_time;
936 /* use timestamp at head of the adapter */
938 gst_util_uint64_scale (ffmpegenc->adapter_consumed, GST_SECOND,
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));
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
949 upstream_time = gst_adapter_prev_timestamp (ffmpegenc->adapter, &bytes);
950 if (GST_CLOCK_TIME_IS_VALID (upstream_time)) {
951 GstClockTimeDiff diff;
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,
968 ffmpegenc->discont = TRUE;
973 GST_LOG_OBJECT (ffmpegenc, "pushing buffer in adapter");
974 gst_adapter_push (ffmpegenc->adapter, inbuf);
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);
980 GST_LOG_OBJECT (ffmpegenc, "frame_bytes %u, avail %u", frame_bytes, avail);
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",
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 */
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;
995 /* calculate timestamp and duration relative to start of adapter and to
996 * the amount of samples we consumed */
998 gst_util_uint64_scale (ffmpegenc->adapter_consumed, GST_SECOND,
1000 duration -= (timestamp - ffmpegenc->adapter_ts);
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;
1006 ret = gst_ffmpegenc_encode_audio (ffmpegenc, in_data, out_size,
1007 timestamp, duration, ffmpegenc->discont);
1009 gst_adapter_flush (ffmpegenc->adapter, frame_bytes);
1011 if (ret != GST_FLOW_OK)
1014 /* advance the adapter timestamp with the duration */
1015 timestamp += duration;
1017 ffmpegenc->discont = FALSE;
1018 avail = gst_adapter_available (ffmpegenc->adapter);
1020 GST_LOG_OBJECT (ffmpegenc, "%u bytes left in the adapter", avail);
1022 /* we have no frame_size, feed the encoder all the data and expect a fixed
1024 int coded_bps = av_get_bits_per_sample (oclass->in_plugin->id) / 8;
1026 GST_LOG_OBJECT (ffmpegenc, "coded bps %d, osize %d", coded_bps, osize);
1028 out_size = size / osize;
1030 out_size *= coded_bps;
1032 /* We need to provide at least ffmpegs minimal buffer size */
1033 out_size += FF_MIN_BUFFER_SIZE;
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);
1040 if (ret != GST_FLOW_OK)
1049 GST_DEBUG_OBJECT (ffmpegenc, "Failed to push buffer %d (%s)", ret,
1050 gst_flow_get_name (ret));
1056 gst_ffmpegenc_flush_buffers (GstFFMpegEnc * ffmpegenc, gboolean send)
1058 GstBuffer *outbuf, *inbuf;
1061 GST_DEBUG_OBJECT (ffmpegenc, "flushing buffers with sending %d", send);
1063 /* no need to empty codec if there is none */
1064 if (!ffmpegenc->opened)
1067 while (!g_queue_is_empty (ffmpegenc->delay)) {
1069 ffmpegenc_setup_working_buf (ffmpegenc);
1071 ret_size = avcodec_encode_video (ffmpegenc->context,
1072 ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL);
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 */
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),
1091 /* handle b-frame delay when no output, so we don't output empty frames */
1092 inbuf = g_queue_pop_head (ffmpegenc->delay);
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);
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));
1103 gst_buffer_unref (inbuf);
1106 gst_pad_push (ffmpegenc->srcpad, outbuf);
1108 gst_buffer_unref (outbuf);
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));
1120 gst_ffmpegenc_event_video (GstPad * pad, GstEvent * event)
1122 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
1124 switch (GST_EVENT_TYPE (event)) {
1126 gst_ffmpegenc_flush_buffers (ffmpegenc, TRUE);
1128 /* no flushing if flush received,
1129 * buffers in encoder are considered (in the) past */
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;
1143 return gst_pad_push_event (ffmpegenc->srcpad, event);
1147 gst_ffmpegenc_event_src (GstPad * pad, GstEvent * event)
1149 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) (GST_PAD_PARENT (pad));
1150 gboolean forward = TRUE;
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);
1161 gst_event_unref (event);
1171 return gst_pad_push_event (ffmpegenc->sinkpad, event);
1177 gst_ffmpegenc_set_property (GObject * object,
1178 guint prop_id, const GValue * value, GParamSpec * pspec)
1180 GstFFMpegEnc *ffmpegenc;
1182 /* Get a pointer of the right type. */
1183 ffmpegenc = (GstFFMpegEnc *) (object);
1185 if (ffmpegenc->opened) {
1186 GST_WARNING_OBJECT (ffmpegenc,
1187 "Can't change properties once decoder is setup !");
1191 /* Check the argument id to see which argument we're setting. */
1194 ffmpegenc->bitrate = g_value_get_ulong (value);
1197 ffmpegenc->gop_size = g_value_get_int (value);
1200 ffmpegenc->me_method = g_value_get_enum (value);
1204 case ARG_RTP_PAYLOAD_SIZE:
1205 ffmpegenc->rtp_payload_size = g_value_get_ulong (value);
1208 if (!gst_ffmpeg_cfg_set_property (object, value, pspec))
1209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1214 /* The set function is simply the inverse of the get fuction. */
1216 gst_ffmpegenc_get_property (GObject * object,
1217 guint prop_id, GValue * value, GParamSpec * pspec)
1219 GstFFMpegEnc *ffmpegenc;
1221 /* It's not null if we got it, but it might not be ours */
1222 ffmpegenc = (GstFFMpegEnc *) (object);
1226 g_value_set_ulong (value, ffmpegenc->bitrate);
1229 g_value_set_int (value, ffmpegenc->gop_size);
1232 g_value_set_enum (value, ffmpegenc->me_method);
1235 g_value_set_ulong (value, ffmpegenc->buffer_size);
1237 case ARG_RTP_PAYLOAD_SIZE:
1238 g_value_set_ulong (value, ffmpegenc->rtp_payload_size);
1241 if (!gst_ffmpeg_cfg_get_property (object, value, pspec))
1242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1247 static GstStateChangeReturn
1248 gst_ffmpegenc_change_state (GstElement * element, GstStateChange transition)
1250 GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) element;
1251 GstStateChangeReturn result;
1253 switch (transition) {
1258 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
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;
1267 gst_adapter_clear (ffmpegenc->adapter);
1269 if (ffmpegenc->file) {
1270 fclose (ffmpegenc->file);
1271 ffmpegenc->file = NULL;
1273 if (ffmpegenc->working_buf) {
1274 g_free (ffmpegenc->working_buf);
1275 ffmpegenc->working_buf = NULL;
1285 gst_ffmpegenc_register (GstPlugin * plugin)
1287 GTypeInfo typeinfo = {
1288 sizeof (GstFFMpegEncClass),
1289 (GBaseInitFunc) gst_ffmpegenc_base_init,
1291 (GClassInitFunc) gst_ffmpegenc_class_init,
1294 sizeof (GstFFMpegEnc),
1296 (GInstanceInitFunc) gst_ffmpegenc_init,
1302 GST_LOG ("Registering encoders");
1304 /* build global ffmpeg param/property info */
1305 gst_ffmpeg_cfg_init ();
1307 in_plugin = av_codec_next (NULL);
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)) {
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)) {
1327 ("Not using external library encoder %s. Use the gstreamer-native ones instead.",
1333 if (!in_plugin->encode) {
1337 /* FIXME : We should have a method to know cheaply whether we have a mapping
1338 * for the given plugin or not */
1340 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
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);
1349 /* construct the type */
1350 type_name = g_strdup_printf ("ffenc_%s", in_plugin->name);
1352 type = g_type_from_name (type_name);
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);
1361 static const GInterfaceInfo preset_info = {
1366 g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_info);
1370 if (!gst_element_register (plugin, type_name, GST_RANK_SECONDARY, type)) {
1378 in_plugin = av_codec_next (in_plugin);
1381 GST_LOG ("Finished registering encoders");