compatibility fix for new GST_DEBUG stuff.
[platform/upstream/gstreamer.git] / ext / libfame / gstlibfame.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 #include <fame.h>
24 #include <string.h>
25
26 #include "gstlibfame.h"
27
28 #define FAMEENC_BUFFER_SIZE (300 * 1024) 
29
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",
35   VERSION,
36   "fameenc: (C) 2000-2001, Vivien Chappelier\n"
37   "Thomas Vander Stichele <thomas@apestaart.org>",
38   "(C) 2002",
39 };
40
41 static GQuark fame_object_name;
42
43
44 /* FameEnc signals and args */
45 enum {
46   /* FILL ME */
47   LAST_SIGNAL
48 };
49
50 enum {
51   ARG_0,
52   ARG_VERSION,
53   ARG_FRAMERATE,
54   ARG_BITRATE,
55   ARG_QUALITY,
56   ARG_PATTERN,
57   ARG_FAME_VERBOSE,
58   ARG_BUFFER_SIZE,
59   ARG_FRAMES_PER_SEQUENCE,
60   /* dynamically generated properties start here */
61   ARG_FAME_PROPS_START
62   /* FILL ME */
63 };
64
65 GST_PAD_TEMPLATE_FACTORY (sink_template_factory,
66   "sink",
67   GST_PAD_SINK,
68   GST_PAD_ALWAYS,
69   GST_CAPS_NEW (
70     "fameenc_sink_caps",
71     "video/raw",
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)
75   )
76 )
77
78 GST_PAD_TEMPLATE_FACTORY (src_template_factory,
79   "src",
80   GST_PAD_SRC,
81   GST_PAD_ALWAYS,
82   GST_CAPS_NEW (
83     "fameenc_src_caps",
84     "video/mpeg",
85       "mpegversion", GST_PROPS_LIST (
86           GST_PROPS_INT (1), GST_PROPS_INT (4)),
87       "systemstream", GST_PROPS_BOOLEAN (FALSE)
88   )
89 );
90
91 #define MAX_FRAME_RATES  16
92 typedef struct
93 {
94   gint num;
95   gint den;
96 } frame_rate_entry;
97
98 static const frame_rate_entry frame_rates[] =
99 {
100   { 0, 0 },
101   { 24000, 1001 },
102   { 24, 1 },
103   { 25, 1 },
104   { 30000, 1001 },
105   { 30, 1 },
106   { 50, 1 },
107   { 60000, 1001 },
108   { 60, 1 },
109   { 0, 0 },
110   { 0, 0 },
111   { 0, 0 },
112   { 0, 0 },
113   { 0, 0 },
114   { 0, 0 },
115   { 0, 0 },
116 };
117
118 static gint
119 framerate_to_index (num, den)
120 {
121   gint i;
122   
123   for (i = 0; i < MAX_FRAME_RATES; i++) {
124     if (frame_rates[i].num == num && frame_rates[i].den == den)
125       return i;
126   }
127   return 0;
128 }
129
130 #define GST_TYPE_FAMEENC_FRAMERATE (gst_fameenc_framerate_get_type())
131 static GType
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)"},
136     {2, "2", "24"},
137     {3, "3", "25"},
138     {4, "4", "30000/1001 (29.97)"},
139     {5, "5", "30"},
140     {6, "6", "50"},
141     {7, "7", "60000/1001 (59.94)"},
142     {8, "8", "60"},
143     {0, NULL, NULL},
144   };
145   if (!fameenc_framerate_type) {
146     fameenc_framerate_type = g_enum_register_static("GstFameEncFrameRate", fameenc_framerate);
147   }
148   return fameenc_framerate_type;
149 }
150
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);
154
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);
159
160 static void     gst_fameenc_chain               (GstPad *pad, GstBuffer *buf);
161
162 static GstElementClass *parent_class = NULL;
163 /*static guint gst_fameenc_signals[LAST_SIGNAL] = { 0 };*/
164
165 GType
166 gst_fameenc_get_type (void)
167 {
168   static GType fameenc_type = 0;
169
170   if (!fameenc_type) {
171     static const GTypeInfo fameenc_info = {
172       sizeof (GstFameEncClass),      
173       NULL,
174       NULL,
175       (GClassInitFunc) gst_fameenc_class_init,
176       NULL,
177       NULL,
178       sizeof (GstFameEnc),
179       0,
180       (GInstanceInitFunc) gst_fameenc_init,
181     };
182     fameenc_type = g_type_register_static (GST_TYPE_ELEMENT, 
183                                            "GstFameEnc", &fameenc_info, 0);
184   }
185   return fameenc_type;
186 }
187
188 static int
189 gst_fameenc_item_compare (fame_list_t *item1, fame_list_t *item2)
190 {
191   return strcmp (item1->type, item2->type);
192 }
193
194 static void
195 gst_fameenc_class_init (GstFameEncClass *klass)
196 {
197   GObjectClass *gobject_class = NULL;
198   GstElementClass *gstelement_class = NULL;
199   fame_context_t *context;
200   fame_list_t *walk;
201   GList *props = NULL, *props_walk;
202   gint current_prop = ARG_FAME_PROPS_START;
203
204   gobject_class = (GObjectClass*) klass;
205   gstelement_class = (GstElementClass*) klass;
206
207   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
208
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;
212
213   fame_object_name = g_quark_from_string ("GstFameObjectName");
214
215   context = fame_open ();
216   g_assert (context);
217
218   /* first sort the list */
219   walk = context->type_list;
220   while (walk) {
221     props = g_list_insert_sorted (props, walk, (GCompareFunc)gst_fameenc_item_compare);
222     walk = walk->next;
223   }
224
225   props_walk = props;
226   while (props_walk) {
227     GArray *array;
228     const gchar *current_type;
229     gint current_len;
230     gint current_value;
231     fame_object_t *current_default;
232     gint default_index;
233
234     walk = (fame_list_t *)props_walk->data;
235     array = g_array_new (TRUE, FALSE, sizeof (GEnumValue));
236
237     current_type = walk->type;
238     current_value = 0;
239     current_len = strlen (walk->type);
240     current_default = fame_get_object (context, current_type);
241     default_index = 1;
242
243     do {
244       if (strstr (walk->type, "/")) {
245         GEnumValue value;
246
247         if (current_default == walk->item) 
248           default_index = current_value;
249
250         value.value = current_value++;
251         value.value_name = g_strdup (walk->type);
252         value.value_nick = g_strdup (walk->item->name);
253         
254         g_array_append_val (array, value);
255       }
256
257       props_walk = g_list_next (props_walk);
258       if (props_walk)
259         walk = (fame_list_t *)props_walk->data;
260
261     } while (props_walk && !strncmp (walk->type, current_type, current_len));
262
263     if (array->len > 0) {
264       GType type;
265       GParamSpec *pspec;
266       
267       type = g_enum_register_static (g_strdup_printf ("GstFameEnc_%s", current_type), (GEnumValue *)array->data);
268
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);
271
272       g_param_spec_set_qdata (pspec, fame_object_name, (gpointer) current_type);
273       
274       g_object_class_install_property (G_OBJECT_CLASS (klass), current_prop++, pspec);
275     }
276   }
277
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)); 
300 }
301
302 static GstPadLinkReturn
303 gst_fameenc_sinkconnect (GstPad *pad, GstCaps *caps)
304 {
305   gint width, height;
306   GstFameEnc *fameenc;
307
308   fameenc = GST_FAMEENC (gst_pad_get_parent (pad));
309
310   if (!GST_CAPS_IS_FIXED (caps)) 
311     return GST_PAD_LINK_DELAYED;
312
313   if (fameenc->initialized) {
314     GST_DEBUG ("error: fameenc encoder already initialized !");
315     return GST_PAD_LINK_REFUSED;
316   }
317
318   gst_caps_get_int (caps, "width", &width);
319   gst_caps_get_int (caps, "height", &height);
320   
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;
324
325   fameenc->fp.width = width;
326   fameenc->fp.height = height;
327   fameenc->fp.coding = (const char *) fameenc->pattern;
328
329   /* FIXME: choose good parameters */
330   fameenc->fp.slices_per_frame = 1;
331
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;
337
338   fame_init (fameenc->fc, &fameenc->fp, fameenc->buffer, fameenc->buffer_size);
339
340   fameenc->initialized = TRUE;
341   fameenc->time_interval = 0;
342   
343   return GST_PAD_LINK_OK;
344 }
345
346 static void
347 gst_fameenc_init (GstFameEnc *fameenc)
348 {
349   g_assert (fameenc != NULL);
350   g_assert (GST_IS_FAMEENC (fameenc));
351
352   /* open fameenc */
353   fameenc->fc = fame_open ();
354   g_assert (fameenc->fc != NULL);
355
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);
362
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 */
367
368   fameenc->verbose = FALSE;
369
370   /* reset the initial video state */
371   fameenc->fp.width = -1;
372   fameenc->fp.height = -1;
373   fameenc->initialized = FALSE;
374
375   /* defaults */
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; 
381
382   fameenc->pattern = g_strdup ("IPPPPPPPPPP");
383
384   /* allocate space for the buffer */
385   fameenc->buffer_size = FAMEENC_BUFFER_SIZE; /* FIXME */
386   fameenc->buffer = (unsigned char *) g_malloc (fameenc->buffer_size);
387   
388   fameenc->next_time = 0; 
389   fameenc->time_interval = 0;
390 }
391
392 static void
393 gst_fameenc_dispose (GObject *object)
394 {
395   GstFameEnc *fameenc = GST_FAMEENC (object);
396
397   G_OBJECT_CLASS (parent_class)->dispose (object);
398
399   g_free (fameenc->buffer);
400 }
401
402 static void
403 gst_fameenc_chain (GstPad *pad, GstBuffer *buf)
404 {
405   GstFameEnc *fameenc;
406   guchar *data;
407   gulong size;
408   gint frame_size;
409   gint length;
410
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));
415
416   fameenc = GST_FAMEENC (gst_pad_get_parent (pad));
417
418   data = (guchar *) GST_BUFFER_DATA (buf);
419   size = GST_BUFFER_SIZE (buf);
420
421   GST_DEBUG ("gst_fameenc_chain: got buffer of %ld bytes in '%s'", 
422              size, GST_OBJECT_NAME (fameenc));
423
424   /* the data contains the three planes side by side, with size w * h, w * h /4,
425    * w * h / 4 */
426   fameenc->fy.w = fameenc->fp.width;
427   fameenc->fy.h = fameenc->fp.height;
428
429   frame_size = fameenc->fp.width * fameenc->fp.height;
430
431   fameenc->fy.p = 0; 
432   fameenc->fy.y = data;
433   fameenc->fy.u = data + frame_size;
434   fameenc->fy.v = fameenc->fy.u + (frame_size >> 2);
435
436   fame_start_frame (fameenc->fc, &fameenc->fy, NULL);
437
438   while ((length = fame_encode_slice (fameenc->fc)) != 0) {
439     GstBuffer *outbuf;
440
441     outbuf = gst_buffer_new ();
442
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);
446
447     if (!fameenc->time_interval) {
448         fameenc->time_interval = GST_SECOND / fameenc->fp.frame_rate_num;
449     }
450
451     fameenc->next_time += fameenc->time_interval;
452
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);
458
459     GST_DEBUG ("gst_fameenc_chain: pushing buffer of size %d",
460                GST_BUFFER_SIZE(outbuf));
461
462     gst_pad_push (fameenc->srcpad, outbuf);
463   }
464
465   fame_end_frame (fameenc->fc, NULL); 
466
467   gst_buffer_unref(buf);
468 }
469
470 static void
471 gst_fameenc_set_property (GObject *object, guint prop_id, 
472                           const GValue *value, GParamSpec *pspec)
473 {
474   GstFameEnc *fameenc;
475
476   g_return_if_fail (GST_IS_FAMEENC (object));
477   fameenc = GST_FAMEENC (object);
478
479   if (fameenc->initialized) {
480     GST_DEBUG ("error: fameenc encoder already initialized, cannot set properties !");
481     return;
482   }
483
484   switch (prop_id) {
485     case ARG_FRAMERATE:
486     {
487       gint index = g_value_get_enum (value);
488
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;
492       break;
493     }
494     case ARG_BITRATE:
495       fameenc->fp.bitrate = g_value_get_int (value);
496       break;
497     case ARG_QUALITY:
498       fameenc->fp.quality = CLAMP (g_value_get_int (value), 0, 100);
499       break;
500     case ARG_PATTERN:
501       g_free (fameenc->pattern);
502       fameenc->pattern = g_strdup (g_value_get_string (value));
503       break;
504     case ARG_FAME_VERBOSE:
505       fameenc->verbose = g_value_get_boolean (value);
506       break;
507     case ARG_BUFFER_SIZE:
508       fameenc->buffer_size = g_value_get_int (value);
509       break;
510     case ARG_FRAMES_PER_SEQUENCE:
511       fameenc->fp.frames_per_sequence = g_value_get_int (value);
512       break;
513     default:
514       if (prop_id >= ARG_FAME_PROPS_START) {
515         gchar *name;
516         gint index = g_value_get_enum (value);
517         GEnumValue *values;
518
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);
521         
522         fame_register (fameenc->fc, name, fame_get_object (fameenc->fc, values[index].value_name));
523       }
524       else
525         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
526       break;
527   }
528 }
529
530 static void
531 gst_fameenc_get_property (GObject *object, guint prop_id, 
532                           GValue *value, GParamSpec *pspec)
533 {
534   GstFameEnc *fameenc;
535
536   g_return_if_fail (GST_IS_FAMEENC (object));
537   fameenc = GST_FAMEENC (object);
538
539   switch (prop_id) {
540     case ARG_FRAMERATE:
541     {
542       gint index = framerate_to_index (fameenc->fp.frame_rate_num, 
543                                        fameenc->fp.frame_rate_den);
544       g_value_set_enum (value, index);
545       break;
546     }
547     case ARG_BITRATE:
548       g_value_set_int (value, fameenc->fp.bitrate);
549       break;
550     case ARG_QUALITY:
551       g_value_set_int (value, fameenc->fp.quality);
552       break;
553     case ARG_PATTERN:
554       g_value_set_string (value, g_strdup (fameenc->pattern));
555       break;
556     case ARG_FAME_VERBOSE:
557       g_value_set_boolean (value, fameenc->verbose);
558       break;
559     case ARG_BUFFER_SIZE:
560       g_value_set_int (value, fameenc->buffer_size);
561       break;
562     case ARG_FRAMES_PER_SEQUENCE:
563       g_value_set_int (value, fameenc->fp.frames_per_sequence);
564       break;
565     default:
566       if (prop_id >= ARG_FAME_PROPS_START) {
567         gchar *name;
568         gint index = 0;
569         GEnumValue *values;
570         fame_object_t *f_object;
571
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);
574         
575         f_object = fame_get_object (fameenc->fc, name);
576
577         while (values[index].value_name) {
578           if (!strcmp (values[index].value_nick, f_object->name)) {
579             g_value_set_enum (value, index);
580             return;
581           }
582           index++;
583         }
584       }
585       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586       break;
587   }
588 }
589
590 static gboolean
591 plugin_init (GModule *module, GstPlugin *plugin)
592 {
593   GstElementFactory *factory;
594
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);
599
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));
604
605   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
606
607   return TRUE;
608 }
609
610 GstPluginDesc plugin_desc = {
611   GST_VERSION_MAJOR,
612   GST_VERSION_MINOR,
613   "fameenc",
614   plugin_init
615 };