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 #include "gstlibfame.h"
28 #define FAMEENC_BUFFER_SIZE (300 * 1024)
30 /* elementfactory information */
31 static GstElementDetails gst_fameenc_details = {
32 "MPEG1 and MPEG4 video encoder using the libfame library",
33 "Codec/Video/Encoder",
34 "Uses fameenc to encode MPEG video streams",
36 "fameenc: (C) 2000-2001, Vivien Chappelier\n"
37 "Thomas Vander Stichele <thomas@apestaart.org>",
41 static GQuark fame_object_name;
44 /* FameEnc signals and args */
59 ARG_FRAMES_PER_SEQUENCE,
60 /* dynamically generated properties start here */
65 GST_PAD_TEMPLATE_FACTORY (sink_template_factory,
72 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
73 "width", GST_PROPS_INT_RANGE (16, 4096),
74 "height", GST_PROPS_INT_RANGE (16, 4096)
78 GST_PAD_TEMPLATE_FACTORY (src_template_factory,
85 "mpegversion", GST_PROPS_LIST (
86 GST_PROPS_INT (1), GST_PROPS_INT (4)),
87 "systemstream", GST_PROPS_BOOLEAN (FALSE)
91 #define MAX_FRAME_RATES 16
98 static const frame_rate_entry frame_rates[] =
119 framerate_to_index (num, den)
123 for (i = 0; i < MAX_FRAME_RATES; i++) {
124 if (frame_rates[i].num == num && frame_rates[i].den == den)
130 #define GST_TYPE_FAMEENC_FRAMERATE (gst_fameenc_framerate_get_type())
132 gst_fameenc_framerate_get_type(void) {
133 static GType fameenc_framerate_type = 0;
134 static GEnumValue fameenc_framerate[] = {
135 {1, "1", "24000/1001 (23.97)"},
138 {4, "4", "30000/1001 (29.97)"},
141 {7, "7", "60000/1001 (59.94)"},
145 if (!fameenc_framerate_type) {
146 fameenc_framerate_type = g_enum_register_static("GstFameEncFrameRate", fameenc_framerate);
148 return fameenc_framerate_type;
151 static void gst_fameenc_class_init (GstFameEncClass *klass);
152 static void gst_fameenc_init (GstFameEnc *fameenc);
153 static void gst_fameenc_dispose (GObject *object);
155 static void gst_fameenc_set_property (GObject *object, guint prop_id,
156 const GValue *value, GParamSpec *pspec);
157 static void gst_fameenc_get_property (GObject *object, guint prop_id,
158 GValue *value, GParamSpec *pspec);
160 static void gst_fameenc_chain (GstPad *pad, GstBuffer *buf);
162 static GstElementClass *parent_class = NULL;
163 /*static guint gst_fameenc_signals[LAST_SIGNAL] = { 0 };*/
166 gst_fameenc_get_type (void)
168 static GType fameenc_type = 0;
171 static const GTypeInfo fameenc_info = {
172 sizeof (GstFameEncClass),
175 (GClassInitFunc) gst_fameenc_class_init,
180 (GInstanceInitFunc) gst_fameenc_init,
182 fameenc_type = g_type_register_static (GST_TYPE_ELEMENT,
183 "GstFameEnc", &fameenc_info, 0);
189 gst_fameenc_item_compare (fame_list_t *item1, fame_list_t *item2)
191 return strcmp (item1->type, item2->type);
195 gst_fameenc_class_init (GstFameEncClass *klass)
197 GObjectClass *gobject_class = NULL;
198 GstElementClass *gstelement_class = NULL;
199 fame_context_t *context;
201 GList *props = NULL, *props_walk;
202 gint current_prop = ARG_FAME_PROPS_START;
204 gobject_class = (GObjectClass*) klass;
205 gstelement_class = (GstElementClass*) klass;
207 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
209 gobject_class->set_property = gst_fameenc_set_property;
210 gobject_class->get_property = gst_fameenc_get_property;
211 gobject_class->dispose = gst_fameenc_dispose;
213 fame_object_name = g_quark_from_string ("GstFameObjectName");
215 context = fame_open ();
218 /* first sort the list */
219 walk = context->type_list;
221 props = g_list_insert_sorted (props, walk, (GCompareFunc)gst_fameenc_item_compare);
228 const gchar *current_type;
231 fame_object_t *current_default;
234 walk = (fame_list_t *)props_walk->data;
235 array = g_array_new (TRUE, FALSE, sizeof (GEnumValue));
237 current_type = walk->type;
239 current_len = strlen (walk->type);
240 current_default = fame_get_object (context, current_type);
244 if (strstr (walk->type, "/")) {
247 if (current_default == walk->item)
248 default_index = current_value;
250 value.value = current_value++;
251 value.value_name = g_strdup (walk->type);
252 value.value_nick = g_strdup (walk->item->name);
254 g_array_append_val (array, value);
257 props_walk = g_list_next (props_walk);
259 walk = (fame_list_t *)props_walk->data;
261 } while (props_walk && !strncmp (walk->type, current_type, current_len));
263 if (array->len > 0) {
267 type = g_enum_register_static (g_strdup_printf ("GstFameEnc_%s", current_type), (GEnumValue *)array->data);
269 pspec = g_param_spec_enum (current_type, current_type, g_strdup_printf ("The FAME \"%s\" object", current_type),
270 type, default_index, G_PARAM_READWRITE);
272 g_param_spec_set_qdata (pspec, fame_object_name, (gpointer) current_type);
274 g_object_class_install_property (G_OBJECT_CLASS (klass), current_prop++, pspec);
278 g_object_class_install_property (gobject_class, ARG_FRAMERATE,
279 g_param_spec_enum ("framerate", "Frame Rate", "Number of frames per second",
280 GST_TYPE_FAMEENC_FRAMERATE, 3, G_PARAM_READWRITE));
281 g_object_class_install_property (gobject_class, ARG_BITRATE,
282 g_param_spec_int ("bitrate", "Bitrate", "Target bitrate (0 = VBR)",
283 0, 5000000, 0, G_PARAM_READWRITE));
284 g_object_class_install_property (gobject_class, ARG_QUALITY,
285 g_param_spec_int ("quality", "Quality", "Percentage of quality of compression (versus size)",
286 0, 100, 75, G_PARAM_READWRITE));
287 g_object_class_install_property (gobject_class, ARG_PATTERN,
288 g_param_spec_string ("pattern", "Pattern", "Encoding pattern of I, P, and B frames",
289 "IPPPPPPPPPPP", G_PARAM_READWRITE));
290 g_object_class_install_property (gobject_class, ARG_FRAMES_PER_SEQUENCE,
291 g_param_spec_int ("frames_per_sequence", "Frames Per Sequence",
292 "The number of frames in one sequence",
293 1, G_MAXINT, 12, G_PARAM_READWRITE));
294 g_object_class_install_property (gobject_class, ARG_FAME_VERBOSE,
295 g_param_spec_boolean ("fame_verbose", "Fame Verbose", "Make FAME produce verbose output",
296 FALSE, G_PARAM_READWRITE));
297 g_object_class_install_property (gobject_class, ARG_BUFFER_SIZE,
298 g_param_spec_int ("buffer_size", "Buffer Size", "Set the decoding output buffer size",
299 0, 1024*1024, FAMEENC_BUFFER_SIZE, G_PARAM_READWRITE));
302 static GstPadLinkReturn
303 gst_fameenc_sinkconnect (GstPad *pad, GstCaps *caps)
308 fameenc = GST_FAMEENC (gst_pad_get_parent (pad));
310 if (!GST_CAPS_IS_FIXED (caps))
311 return GST_PAD_LINK_DELAYED;
313 if (fameenc->initialized) {
314 GST_DEBUG ("error: fameenc encoder already initialized !");
315 return GST_PAD_LINK_REFUSED;
318 gst_caps_get_int (caps, "width", &width);
319 gst_caps_get_int (caps, "height", &height);
321 /* fameenc requires width and height to be multiples of 16 */
322 if (width % 16 != 0 || height % 16 != 0)
323 return GST_PAD_LINK_REFUSED;
325 fameenc->fp.width = width;
326 fameenc->fp.height = height;
327 fameenc->fp.coding = (const char *) fameenc->pattern;
329 /* FIXME: choose good parameters */
330 fameenc->fp.slices_per_frame = 1;
332 /* FIXME: handle these properly */
333 fameenc->fp.shape_quality = 75;
334 fameenc->fp.search_range = 0;
335 fameenc->fp.total_frames = 0;
336 fameenc->fp.retrieve_cb = NULL;
338 fame_init (fameenc->fc, &fameenc->fp, fameenc->buffer, fameenc->buffer_size);
340 fameenc->initialized = TRUE;
341 fameenc->time_interval = 0;
343 return GST_PAD_LINK_OK;
347 gst_fameenc_init (GstFameEnc *fameenc)
349 g_assert (fameenc != NULL);
350 g_assert (GST_IS_FAMEENC (fameenc));
353 fameenc->fc = fame_open ();
354 g_assert (fameenc->fc != NULL);
356 /* create the sink and src pads */
357 fameenc->sinkpad = gst_pad_new_from_template (
358 GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
359 gst_element_add_pad (GST_ELEMENT (fameenc), fameenc->sinkpad);
360 gst_pad_set_chain_function (fameenc->sinkpad, gst_fameenc_chain);
361 gst_pad_set_link_function (fameenc->sinkpad, gst_fameenc_sinkconnect);
363 fameenc->srcpad = gst_pad_new_from_template (
364 GST_PAD_TEMPLATE_GET (src_template_factory), "src");
365 gst_element_add_pad (GST_ELEMENT (fameenc), fameenc->srcpad);
366 /* FIXME: set some more handler functions here */
368 fameenc->verbose = FALSE;
370 /* reset the initial video state */
371 fameenc->fp.width = -1;
372 fameenc->fp.height = -1;
373 fameenc->initialized = FALSE;
376 fameenc->fp.bitrate = 0;
377 fameenc->fp.quality = 75;
378 fameenc->fp.frame_rate_num = 25;
379 fameenc->fp.frame_rate_den = 1; /* avoid floating point exceptions */
380 fameenc->fp.frames_per_sequence = 12;
382 fameenc->pattern = g_strdup ("IPPPPPPPPPP");
384 /* allocate space for the buffer */
385 fameenc->buffer_size = FAMEENC_BUFFER_SIZE; /* FIXME */
386 fameenc->buffer = (unsigned char *) g_malloc (fameenc->buffer_size);
388 fameenc->next_time = 0;
389 fameenc->time_interval = 0;
393 gst_fameenc_dispose (GObject *object)
395 GstFameEnc *fameenc = GST_FAMEENC (object);
397 G_OBJECT_CLASS (parent_class)->dispose (object);
399 g_free (fameenc->buffer);
403 gst_fameenc_chain (GstPad *pad, GstBuffer *buf)
411 g_return_if_fail (pad != NULL);
412 g_return_if_fail (GST_IS_PAD (pad));
413 g_return_if_fail (buf != NULL);
414 g_return_if_fail (GST_IS_BUFFER (buf));
416 fameenc = GST_FAMEENC (gst_pad_get_parent (pad));
418 data = (guchar *) GST_BUFFER_DATA (buf);
419 size = GST_BUFFER_SIZE (buf);
421 GST_DEBUG ("gst_fameenc_chain: got buffer of %ld bytes in '%s'",
422 size, GST_OBJECT_NAME (fameenc));
424 /* the data contains the three planes side by side, with size w * h, w * h /4,
426 fameenc->fy.w = fameenc->fp.width;
427 fameenc->fy.h = fameenc->fp.height;
429 frame_size = fameenc->fp.width * fameenc->fp.height;
432 fameenc->fy.y = data;
433 fameenc->fy.u = data + frame_size;
434 fameenc->fy.v = fameenc->fy.u + (frame_size >> 2);
436 fame_start_frame (fameenc->fc, &fameenc->fy, NULL);
438 while ((length = fame_encode_slice (fameenc->fc)) != 0) {
441 outbuf = gst_buffer_new ();
443 /* FIXME: safeguard, remove me when a better way is found */
444 if (length > FAMEENC_BUFFER_SIZE)
445 g_warning ("FAMEENC_BUFFER_SIZE is defined too low, encoded slice has size %d !\n", length);
447 if (!fameenc->time_interval) {
448 fameenc->time_interval = GST_SECOND / fameenc->fp.frame_rate_num;
451 fameenc->next_time += fameenc->time_interval;
453 GST_BUFFER_SIZE (outbuf) = length;
454 GST_BUFFER_TIMESTAMP (outbuf) = fameenc->next_time;
455 GST_BUFFER_DATA (outbuf) = g_malloc (length);
456 memcpy (GST_BUFFER_DATA(outbuf), fameenc->buffer, length);
457 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
459 GST_DEBUG ("gst_fameenc_chain: pushing buffer of size %d",
460 GST_BUFFER_SIZE(outbuf));
462 gst_pad_push (fameenc->srcpad, outbuf);
465 fame_end_frame (fameenc->fc, NULL);
467 gst_buffer_unref(buf);
471 gst_fameenc_set_property (GObject *object, guint prop_id,
472 const GValue *value, GParamSpec *pspec)
476 g_return_if_fail (GST_IS_FAMEENC (object));
477 fameenc = GST_FAMEENC (object);
479 if (fameenc->initialized) {
480 GST_DEBUG ("error: fameenc encoder already initialized, cannot set properties !");
487 gint index = g_value_get_enum (value);
489 fameenc->fp.frame_rate_num = frame_rates[index].num;
490 fameenc->fp.frame_rate_den = frame_rates[index].den;
491 fameenc->time_interval = 0;
495 fameenc->fp.bitrate = g_value_get_int (value);
498 fameenc->fp.quality = CLAMP (g_value_get_int (value), 0, 100);
501 g_free (fameenc->pattern);
502 fameenc->pattern = g_strdup (g_value_get_string (value));
504 case ARG_FAME_VERBOSE:
505 fameenc->verbose = g_value_get_boolean (value);
507 case ARG_BUFFER_SIZE:
508 fameenc->buffer_size = g_value_get_int (value);
510 case ARG_FRAMES_PER_SEQUENCE:
511 fameenc->fp.frames_per_sequence = g_value_get_int (value);
514 if (prop_id >= ARG_FAME_PROPS_START) {
516 gint index = g_value_get_enum (value);
519 values = G_ENUM_CLASS (g_type_class_ref (pspec->value_type))->values;
520 name = (gchar *) g_param_spec_get_qdata (pspec, fame_object_name);
522 fame_register (fameenc->fc, name, fame_get_object (fameenc->fc, values[index].value_name));
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531 gst_fameenc_get_property (GObject *object, guint prop_id,
532 GValue *value, GParamSpec *pspec)
536 g_return_if_fail (GST_IS_FAMEENC (object));
537 fameenc = GST_FAMEENC (object);
542 gint index = framerate_to_index (fameenc->fp.frame_rate_num,
543 fameenc->fp.frame_rate_den);
544 g_value_set_enum (value, index);
548 g_value_set_int (value, fameenc->fp.bitrate);
551 g_value_set_int (value, fameenc->fp.quality);
554 g_value_set_string (value, g_strdup (fameenc->pattern));
556 case ARG_FAME_VERBOSE:
557 g_value_set_boolean (value, fameenc->verbose);
559 case ARG_BUFFER_SIZE:
560 g_value_set_int (value, fameenc->buffer_size);
562 case ARG_FRAMES_PER_SEQUENCE:
563 g_value_set_int (value, fameenc->fp.frames_per_sequence);
566 if (prop_id >= ARG_FAME_PROPS_START) {
570 fame_object_t *f_object;
572 values = G_ENUM_CLASS (g_type_class_ref (pspec->value_type))->values;
573 name = (gchar *) g_param_spec_get_qdata (pspec, fame_object_name);
575 f_object = fame_get_object (fameenc->fc, name);
577 while (values[index].value_name) {
578 if (!strcmp (values[index].value_nick, f_object->name)) {
579 g_value_set_enum (value, index);
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
591 plugin_init (GModule *module, GstPlugin *plugin)
593 GstElementFactory *factory;
595 /* create an elementfactory for the fameenc element */
596 factory = gst_element_factory_new ("fameenc", GST_TYPE_FAMEENC,
597 &gst_fameenc_details);
598 g_return_val_if_fail (factory != NULL, FALSE);
600 gst_element_factory_add_pad_template (factory,
601 GST_PAD_TEMPLATE_GET (sink_template_factory));
602 gst_element_factory_add_pad_template (factory,
603 GST_PAD_TEMPLATE_GET (src_template_factory));
605 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
610 GstPluginDesc plugin_desc = {