summaryrefslogtreecommitdiff
path: root/ext/ffmpeg/gstffmpegenc.c
blob: deab9cb78dd62e3e5060ce9ff2a0a2e479258354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* Gnome-Streamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gstffmpegenc.h"

#include <string.h>

enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_WIDTH,
  ARG_HEIGHT,
  ARG_BIT_RATE,
  ARG_FRAME_RATE,
  ARG_SAMPLE_RATE,
  ARG_GOP_SIZE,
  ARG_HQ,
  ARG_ME_METHOD,
  /* FILL ME */
};

/* This factory is much simpler, and defines the source pad. */
GST_PADTEMPLATE_FACTORY (gst_ffmpegenc_src_factory,
  "src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "ffmpegenc_src",
    "unknown/unknown",
    NULL
  )
)

/* This factory is much simpler, and defines the source pad. */
GST_PADTEMPLATE_FACTORY (gst_ffmpegenc_audio_sink_factory,
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "ffmpegenc_sink",
    "audio/raw",
      "format",       GST_PROPS_STRING ("int"),
        "law",        GST_PROPS_INT (0),
        "endianness", GST_PROPS_INT (G_BYTE_ORDER),
        "signed",     GST_PROPS_BOOLEAN (TRUE),
        "width",      GST_PROPS_INT (16),
	"depth",      GST_PROPS_INT (16),
        "rate",       GST_PROPS_INT_RANGE (8000, 96000),
        "channels",   GST_PROPS_INT_RANGE (1, 2)
  )
)

/* This factory is much simpler, and defines the source pad. */
GST_PADTEMPLATE_FACTORY (gst_ffmpegenc_video_sink_factory,
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "ffmpegenc_sink",
    "video/raw",
      "format",       GST_PROPS_LIST (
	                GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
	                GST_PROPS_FOURCC (GST_STR_FOURCC ("YUY2"))
		      ),
        "width",      GST_PROPS_INT_RANGE (16, 4096),
        "height",     GST_PROPS_INT_RANGE (16, 4096)
  )
)

#define GST_TYPE_ME_METHOD (gst_ffmpegenc_me_method_get_type())
static GType
gst_ffmpegenc_me_method_get_type (void)
{
  static GType ffmpegenc_me_method_type = 0;
  static GEnumValue ffmpegenc_me_methods[] = {
    { ME_ZERO,  "0", "zero" },
    { ME_FULL,  "1", "full" },
    { ME_LOG,   "2", "logarithmic" },
    { ME_PHODS, "3", "phods" },
    { 0, NULL, NULL },
  };
  if (!ffmpegenc_me_method_type) {
    ffmpegenc_me_method_type = g_enum_register_static ("GstFFMpegEncMeMethod", ffmpegenc_me_methods);
  }
  return ffmpegenc_me_method_type;
}

static GHashTable *enc_global_plugins;

/* A number of functon prototypes are given so we can refer to them later. */
static void	gst_ffmpegenc_class_init	(GstFFMpegEncClass *klass);
static void	gst_ffmpegenc_init		(GstFFMpegEnc *ffmpegenc);

static void	gst_ffmpegenc_chain_audio	(GstPad *pad, GstBuffer *buffer);
static void	gst_ffmpegenc_chain_video	(GstPad *pad, GstBuffer *buffer);

static void	gst_ffmpegenc_set_property	(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void	gst_ffmpegenc_get_property	(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

static GstElementClass *parent_class = NULL;

//static guint gst_ffmpegenc_signals[LAST_SIGNAL] = { 0 };

static void
gst_ffmpegenc_class_init (GstFFMpegEncClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_ELEMENT);

  klass->in_plugin = g_hash_table_lookup (enc_global_plugins,
		  GINT_TO_POINTER (G_OBJECT_CLASS_TYPE (gobject_class)));

  if (klass->in_plugin->type == CODEC_TYPE_VIDEO) {
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_WIDTH,
      g_param_spec_int ("width","width","width",
                      0, G_MAXINT, -1, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_HEIGHT,
      g_param_spec_int ("height","height","height",
                      0, G_MAXINT, -1, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_BIT_RATE,
      g_param_spec_int ("bit_rate","bit_rate","bit_rate",
                      0, G_MAXINT, 300000, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_FRAME_RATE,
      g_param_spec_int ("frame_rate","frame_rate","frame_rate",
                      0, G_MAXINT, 25, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_GOP_SIZE,
      g_param_spec_int ("gop_size","gop_size","gop_size",
                      0, G_MAXINT, 15, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_HQ,
      g_param_spec_boolean ("hq","hq","hq",
                      FALSE, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_ME_METHOD,
      g_param_spec_enum ("me_method","me_method","me_method",
                      GST_TYPE_ME_METHOD, ME_LOG, G_PARAM_READWRITE)); 
  }
  else if (klass->in_plugin->type == CODEC_TYPE_AUDIO) {
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_BIT_RATE,
      g_param_spec_int ("bit_rate","bit_rate","bit_rate",
                      0, G_MAXINT, 128000, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SAMPLE_RATE,
      g_param_spec_int ("sample_rate","rate","rate",
                      0, G_MAXINT, -1, G_PARAM_READWRITE)); 
    g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_HQ,
      g_param_spec_boolean ("hq","hq","hq",
                      FALSE, G_PARAM_READWRITE)); 
  }

  gobject_class->set_property = gst_ffmpegenc_set_property;
  gobject_class->get_property = gst_ffmpegenc_get_property;
}

static void
gst_ffmpegenc_newcaps (GstPad *pad, GstCaps *caps)
{
  GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) gst_pad_get_parent (pad);
  GstFFMpegEncClass *oclass = (GstFFMpegEncClass*)(G_OBJECT_GET_CLASS(ffmpegenc));

  if (strstr (gst_caps_get_mime (caps), "audio/raw")) {
    ffmpegenc->context->sample_rate = gst_caps_get_int (caps, "rate");
    ffmpegenc->context->channels = gst_caps_get_int (caps, "channels");
  }
  else if (strstr (gst_caps_get_mime (caps), "video/raw")) {
    ffmpegenc->in_width = gst_caps_get_int (caps, "width");
    ffmpegenc->in_height = gst_caps_get_int (caps, "height");
    if (ffmpegenc->need_resample) {
      ffmpegenc->context->width = ffmpegenc->out_width;
      ffmpegenc->context->height = ffmpegenc->out_height;
    }
    else {
      ffmpegenc->context->width = ffmpegenc->in_width;
      ffmpegenc->context->height = ffmpegenc->in_height;
    }
    if (gst_caps_get_fourcc_int (caps, "format") == GST_STR_FOURCC ("I420")) {
      ffmpegenc->context->pix_fmt = PIX_FMT_YUV420P;
    }
    else {
      ffmpegenc->context->pix_fmt = PIX_FMT_YUV422;
    }

    ffmpegenc->resample = img_resample_init (ffmpegenc->context->width, ffmpegenc->context->height, 
		    	ffmpegenc->in_width, ffmpegenc->in_height);
  }
  else {
    g_warning ("ffmpegenc: invalid caps %s\n", gst_caps_get_mime (caps));
  }

  if (avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) {
    g_warning ("ffmpegenc: could not open codec\n");
  }

  if (oclass->in_plugin->type == CODEC_TYPE_AUDIO) {
    ffmpegenc->buffer = g_malloc (ffmpegenc->context->frame_size * 2 * 
		  ffmpegenc->context->channels);
    ffmpegenc->buffer_pos = 0;
  }
}

static void
gst_ffmpegenc_init(GstFFMpegEnc *ffmpegenc)
{
  GstFFMpegEncClass *oclass = (GstFFMpegEncClass*)(G_OBJECT_GET_CLASS (ffmpegenc));

  ffmpegenc->context = g_malloc0 (sizeof (AVCodecContext));

  if (oclass->in_plugin->type == CODEC_TYPE_VIDEO) {
    ffmpegenc->sinkpad = gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (gst_ffmpegenc_video_sink_factory), "sink");
    gst_pad_set_chain_function (ffmpegenc->sinkpad, gst_ffmpegenc_chain_video);
    ffmpegenc->context->bit_rate = 300000;
    ffmpegenc->context->gop_size = 15;
    ffmpegenc->context->frame_rate = 25;
    ffmpegenc->out_width = -1;
    ffmpegenc->out_height = -1;
  }
  else if (oclass->in_plugin->type == CODEC_TYPE_AUDIO) {
    ffmpegenc->sinkpad = gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (gst_ffmpegenc_audio_sink_factory), "sink");
    gst_pad_set_chain_function (ffmpegenc->sinkpad, gst_ffmpegenc_chain_audio);
    ffmpegenc->context->bit_rate = 128000;
    ffmpegenc->context->sample_rate = -1;
  }

  gst_pad_set_newcaps_function (ffmpegenc->sinkpad, gst_ffmpegenc_newcaps);
  gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->sinkpad);

  ffmpegenc->srcpad = gst_pad_new_from_template (
		  GST_PADTEMPLATE_GET (gst_ffmpegenc_src_factory), "src");
  gst_element_add_pad (GST_ELEMENT (ffmpegenc), ffmpegenc->srcpad);

  /* Initialization of element's private variables. */
  ffmpegenc->need_resample = FALSE;
}

static void
gst_ffmpegenc_chain_audio (GstPad *pad, GstBuffer *inbuf)
{
  GstBuffer *outbuf;
  GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *)(gst_pad_get_parent (pad));
  gpointer data;
  gint size;
  gint frame_size = ffmpegenc->context->frame_size * ffmpegenc->context->channels * 2;

  data = GST_BUFFER_DATA (inbuf);
  size = GST_BUFFER_SIZE (inbuf);

  GST_DEBUG (0, "got buffer %p %d\n", data, size);

  if (ffmpegenc->buffer_pos && (ffmpegenc->buffer_pos + size >= frame_size)) {

    memcpy (ffmpegenc->buffer + ffmpegenc->buffer_pos, data, frame_size - ffmpegenc->buffer_pos);

    outbuf = gst_buffer_new ();
    GST_BUFFER_SIZE (outbuf) = frame_size;
    GST_BUFFER_DATA (outbuf) = g_malloc (frame_size);

    GST_BUFFER_SIZE (outbuf) = avcodec_encode_audio (ffmpegenc->context, GST_BUFFER_DATA (outbuf),
  				GST_BUFFER_SIZE (outbuf), (const short *)ffmpegenc->buffer);

    gst_pad_push (ffmpegenc->srcpad, outbuf);

    size -= (frame_size - ffmpegenc->buffer_pos);
    data += (frame_size - ffmpegenc->buffer_pos);

    ffmpegenc->buffer_pos = 0;
  }
  while (size >= frame_size) {

    outbuf = gst_buffer_new ();
    GST_BUFFER_SIZE (outbuf) = frame_size;
    GST_BUFFER_DATA (outbuf) = g_malloc (frame_size);

    GST_BUFFER_SIZE (outbuf) = avcodec_encode_audio (ffmpegenc->context, GST_BUFFER_DATA (outbuf),
  				GST_BUFFER_SIZE (outbuf), (const short *)data);

    gst_pad_push (ffmpegenc->srcpad, outbuf);

    size -= frame_size;
    data += frame_size;
  }
    
  // save leftover
  if (size) {
     memcpy (ffmpegenc->buffer + ffmpegenc->buffer_pos, data, size);
     ffmpegenc->buffer_pos += size;
  }

  gst_buffer_unref (inbuf);
}

static void
gst_ffmpegenc_chain_video (GstPad *pad, GstBuffer *inbuf)
{
  GstBuffer *outbuf;
  GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *)(gst_pad_get_parent (pad));
  gpointer data;
  gint size, frame_size;
  AVPicture picture, rpicture, *toencode;
  gboolean free_data = FALSE, free_res = FALSE;

  data = GST_BUFFER_DATA (inbuf);
  size = GST_BUFFER_SIZE (inbuf);

  frame_size = ffmpegenc->in_width * ffmpegenc->in_height;

  switch (ffmpegenc->context->pix_fmt) {
    case PIX_FMT_YUV422: 
    {
      guchar *temp;

      temp = g_malloc ((frame_size * 3) /2 );
      size = (frame_size * 3)/2;

      img_convert_to_yuv420 (temp, data, PIX_FMT_YUV422, 
		      ffmpegenc->in_width, ffmpegenc->in_height);
      data = temp;
      free_data = TRUE;
      break;
    }
    default:
      break;
  }

  picture.data[0] = data;
  picture.data[1] = picture.data[0] + frame_size;
  picture.data[2] = picture.data[1] + (frame_size >> 2);
  picture.linesize[0] = ffmpegenc->in_width;
  picture.linesize[1] = ffmpegenc->in_width >> 1;
  picture.linesize[2] = ffmpegenc->in_width >> 1;
  toencode = &picture;

  if (ffmpegenc->need_resample) {
    gint rframe_size = ffmpegenc->context->width * ffmpegenc->context->height;

    rpicture.data[0] = g_malloc ((rframe_size * 3)/2);
    rpicture.data[1] = rpicture.data[0] + rframe_size;
    rpicture.data[2] = rpicture.data[1] + (rframe_size >> 2);
    rpicture.linesize[0] = ffmpegenc->context->width;
    rpicture.linesize[1] = ffmpegenc->context->width >> 1;
    rpicture.linesize[2] = ffmpegenc->context->width >> 1;
    free_res = TRUE;
    toencode = &rpicture;
    
    img_resample (ffmpegenc->resample, &rpicture, &picture);
  }
  
  outbuf = gst_buffer_new ();
  GST_BUFFER_SIZE (outbuf) = size;
  GST_BUFFER_DATA (outbuf) = g_malloc (size);

  GST_BUFFER_SIZE (outbuf) = avcodec_encode_video (ffmpegenc->context, GST_BUFFER_DATA (outbuf),
  				GST_BUFFER_SIZE (outbuf), toencode);

  gst_pad_push (ffmpegenc->srcpad, outbuf);

  if (free_data)
    g_free (data);
  if (free_res)
    g_free (rpicture.data[0]);

  gst_buffer_unref (inbuf);
}

static void
gst_ffmpegenc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  GstFFMpegEnc *ffmpegenc;

  /* Get a pointer of the right type. */
  ffmpegenc = (GstFFMpegEnc *)(object);

  /* Check the argument id to see which argument we're setting. */
  switch (prop_id) {
    case ARG_WIDTH:
      ffmpegenc->out_width = g_value_get_int (value);
      ffmpegenc->need_resample = (ffmpegenc->out_width != -1);
      break;
    case ARG_HEIGHT:
      ffmpegenc->out_height = g_value_get_int (value);
      ffmpegenc->need_resample = (ffmpegenc->out_height != -1);
      break;
    case ARG_BIT_RATE:
      ffmpegenc->context->bit_rate = g_value_get_int (value);
      break;
    case ARG_FRAME_RATE:
      ffmpegenc->context->frame_rate = g_value_get_int (value);
      break;
    case ARG_SAMPLE_RATE:
      ffmpegenc->context->sample_rate = g_value_get_int (value);
      break;
    case ARG_GOP_SIZE:
      ffmpegenc->context->gop_size = g_value_get_int (value);
      break;
    case ARG_HQ:
      ffmpegenc->context->flags &= ~CODEC_FLAG_HQ;
      ffmpegenc->context->flags |= (g_value_get_boolean (value)?CODEC_FLAG_HQ:0);
      break;
    case ARG_ME_METHOD:
      motion_estimation_method = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

/* The set function is simply the inverse of the get fuction. */
static void
gst_ffmpegenc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  GstFFMpegEnc *ffmpegenc;

  /* It's not null if we got it, but it might not be ours */
  ffmpegenc = (GstFFMpegEnc *)(object);

  switch (prop_id) {
    case ARG_WIDTH:
      g_value_set_int (value, ffmpegenc->out_width);
      break;
    case ARG_HEIGHT:
      g_value_set_int (value, ffmpegenc->out_height);
      break;
    case ARG_BIT_RATE:
      g_value_set_int (value, ffmpegenc->context->bit_rate);
      break;
    case ARG_SAMPLE_RATE:
      g_value_set_int (value, ffmpegenc->context->sample_rate);
      break;
    case ARG_FRAME_RATE:
      g_value_set_int (value, ffmpegenc->context->frame_rate);
      break;
    case ARG_GOP_SIZE:
      g_value_set_int (value, ffmpegenc->context->gop_size);
      break;
    case ARG_HQ:
      g_value_set_boolean (value, ffmpegenc->context->flags & CODEC_FLAG_HQ);
      break;
    case ARG_ME_METHOD:
      g_value_set_enum (value, motion_estimation_method);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

gboolean
gst_ffmpegenc_register (GstPlugin *plugin)
{
  GstElementFactory *factory;
  GTypeInfo typeinfo = {
    sizeof(GstFFMpegEncClass),      
    NULL,
    NULL,
    (GClassInitFunc)gst_ffmpegenc_class_init,
    NULL,
    NULL,
    sizeof(GstFFMpegEnc),
    0,
    (GInstanceInitFunc)gst_ffmpegenc_init,
  };
  GType type;
  GstElementDetails *details;
  AVCodec *in_plugin;
  
  in_plugin = first_avcodec;

  enc_global_plugins = g_hash_table_new (NULL, NULL);

  while (in_plugin) {
    gchar *type_name;
    gchar *codec_type;

    if (in_plugin->encode) {
      codec_type = "enc";
    }
    else {
      goto next;
    }
    // construct the type
    type_name = g_strdup_printf("ffmpeg%s_%s", codec_type, in_plugin->name);

    // if it's already registered, drop it
    if (g_type_from_name(type_name)) {
      g_free(type_name);
      goto next;
    }

    // create the gtk type now
    type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);

    // construct the element details struct
    details = g_new0 (GstElementDetails,1);
    details->longname = g_strdup (in_plugin->name);
    details->klass = "Codec/FFMpeg";
    details->description = g_strdup (in_plugin->name);
    details->version = g_strdup("1.0.0");
    details->author = g_strdup("The FFMPEG crew, GStreamer plugin by Wim Taymans <wim.taymans@chello.be>");
    details->copyright = g_strdup("(c) 2001");

    g_hash_table_insert (enc_global_plugins, 
		         GINT_TO_POINTER (type), 
			 (gpointer) in_plugin);

    // register the plugin with gstreamer
    factory = gst_elementfactory_new(type_name,type,details);
    g_return_val_if_fail(factory != NULL, FALSE);

    gst_elementfactory_add_padtemplate (factory, 
		    GST_PADTEMPLATE_GET (gst_ffmpegenc_src_factory));
    if (in_plugin->type == CODEC_TYPE_VIDEO) {
      gst_elementfactory_add_padtemplate (factory, 
		    GST_PADTEMPLATE_GET (gst_ffmpegenc_video_sink_factory));
    }
    else if (in_plugin->type == CODEC_TYPE_AUDIO) {
      gst_elementfactory_add_padtemplate (factory, 
		    GST_PADTEMPLATE_GET (gst_ffmpegenc_audio_sink_factory));
    }

    /* The very last thing is to register the elementfactory with the plugin. */
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

next:
    in_plugin = in_plugin->next;
  }

  return TRUE;
}