Merge CAPS branch
[platform/upstream/gst-plugins-good.git] / gst / flx / gstflxdec.c
index 5fd5eb2..ca89339 100644 (file)
@@ -1,5 +1,5 @@
-/* Gnome-Streamer
- * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
+/* GStreamer
+ * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA 02111-1307, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <string.h>
 
 #include "flx_fmt.h"
 #include "gstflxdec.h"
+#include <gst/video/video.h>
 
-static GstCaps* flxdec_typefind(GstBuffer *buf, gpointer private);
+#define JIFFIE  (GST_SECOND/70)
 
 /* flx element information */
 static GstElementDetails flxdec_details = {
   "FLX Decoder",
-  "flxdec",
+  "Codec/Decoder/Audio",
   "FLX decoder",
-  VERSION,
   "Sepp Wijnands <mrrazz@garbage-coderz.net>"
-  "(C) 2001",
-};
-
-static GstTypeDefinition flxdec_definition = {
-  "flxdec_video/fli",
-  "video/fli",
-  ".flc .fli",
-  flxdec_typefind,
 };
 
 /* Flx signals and args */
@@ -52,77 +47,46 @@ enum {
 };
 
 /* input */
-GST_PADTEMPLATE_FACTORY (sink_factory,
+static GstStaticPadTemplate sink_factory =
+GST_STATIC_PAD_TEMPLATE (
   "sink",          
   GST_PAD_SINK, 
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "flxdec_sink",         
-    "video/fli",
-     NULL
-  )
-)
+  GST_STATIC_CAPS ( "video/x-fli" )
+);
 
 /* output */
-GST_PADTEMPLATE_FACTORY (src_video_factory,
+static GstStaticPadTemplate src_video_factory =
+GST_STATIC_PAD_TEMPLATE (
   "src",
   GST_PAD_SRC,
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "src_video",
-    "video/raw",
-      "format",       GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
-        "bpp",        GST_PROPS_INT (32),
-        "depth",      GST_PROPS_INT (32),
-        "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
-        "red_mask",   GST_PROPS_INT (0x00ff0000),
-        "green_mask", GST_PROPS_INT (0x0000ff00),
-        "blue_mask",  GST_PROPS_INT (0x000000ff),
-        "width",      GST_PROPS_INT_RANGE(320, 1280), 
-        "height",     GST_PROPS_INT_RANGE(200, 1024)
-  )
-)
+  GST_STATIC_CAPS ( GST_VIDEO_RGB_PAD_TEMPLATE_CAPS_32 )
+);
 
 
 static void    gst_flxdec_class_init   (GstFlxDecClass *klass);
+static void    gst_flxdec_base_init    (GstFlxDecClass *klass);
 static void    gst_flxdec_init         (GstFlxDec *flxdec);
 
 static void    gst_flxdec_loop         (GstElement *element);
 
+static GstElementStateReturn 
+               gst_flxdec_change_state (GstElement *element);
+
 static void    gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
 static void    gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
 
 
-static void flx_decode_color(GstFlxDec *, guchar *, guchar *);
-static void flx_decode_brun(GstFlxDec *, guchar *, guchar *);
-static void flx_decode_delta_fli(GstFlxDec *, guchar *, guchar *);
-static void flx_decode_delta_flc(GstFlxDec *, guchar *, guchar *);
+static void    flx_decode_color        (GstFlxDec *, guchar *, guchar *, gint);
+static void    flx_decode_brun         (GstFlxDec *, guchar *, guchar *);
+static void    flx_decode_delta_fli    (GstFlxDec *, guchar *, guchar *);
+static void    flx_decode_delta_flc    (GstFlxDec *, guchar *, guchar *);
 
 #define rndalign(off) ((off) + ((off) % 2))
 
 static GstElementClass *parent_class = NULL;
 
-static GstCaps* 
-flxdec_typefind (GstBuffer *buf, gpointer private)
-{
-  guchar *data = GST_BUFFER_DATA(buf);
-  GstCaps *new;
-
-  // check magic 
-  if ((data[4] == 0x11 || data[4] == 0x12
-       || data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
-      // check the frame type of the first frame
-      if ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1) {
-        g_print("GstFlxDec: found supported flx format\n");
-        new = gst_caps_new("flxdec_typefind","video/fli", NULL);
-        return new;
-      }
-  }
-  
-  return NULL; 
-}
-
-
 GType
 gst_flxdec_get_type(void) 
 {
@@ -130,7 +94,8 @@ gst_flxdec_get_type(void)
 
   if (!flxdec_type) {
     static const GTypeInfo flxdec_info = {
-      sizeof(GstFlxDecClass),      NULL,
+      sizeof(GstFlxDecClass),
+      (GBaseInitFunc)gst_flxdec_base_init,
       NULL,
       (GClassInitFunc)gst_flxdec_class_init,
       NULL,
@@ -144,6 +109,18 @@ gst_flxdec_get_type(void)
   return flxdec_type;
 }
 
+static void
+gst_flxdec_base_init (GstFlxDecClass *klass)
+{
+  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+  
+  gst_element_class_set_details (gstelement_class, &flxdec_details);
+  gst_element_class_add_pad_template (gstelement_class,
+       gst_static_pad_template_get (&sink_factory));
+  gst_element_class_add_pad_template (gstelement_class,
+       gst_static_pad_template_get (&src_video_factory));
+}
+
 static void 
 gst_flxdec_class_init (GstFlxDecClass *klass) 
 {
@@ -155,9 +132,10 @@ gst_flxdec_class_init (GstFlxDecClass *klass)
 
   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
 
-  gobject_class->set_property = NULL;  
-  gobject_class->get_property = NULL;
+  gobject_class->set_property = gst_flxdec_set_property;  
+  gobject_class->get_property = gst_flxdec_get_property;
 
+  gstelement_class->change_state = gst_flxdec_change_state;
 }
 
 
@@ -166,18 +144,17 @@ static void
 gst_flxdec_init(GstFlxDec *flxdec) 
 {
   flxdec->sinkpad = gst_pad_new_from_template (
-                 GST_PADTEMPLATE_GET (sink_factory), "sink");
+                 gst_static_pad_template_get (&sink_factory), "sink");
   gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->sinkpad);
   gst_element_set_loop_function(GST_ELEMENT(flxdec),gst_flxdec_loop);
 
   flxdec->srcpad = gst_pad_new_from_template (
-                 GST_PADTEMPLATE_GET (src_video_factory), "src");
+                 gst_static_pad_template_get (&src_video_factory), "src");
   gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->srcpad);
 
-  flxdec->buf = NULL;
-  flxdec->offset = 0;
-  flxdec->new_buf = TRUE;
-
+  flxdec->bs = NULL;
+  flxdec->frame = NULL;
+  flxdec->delta = NULL;
 }
 
 static void
@@ -194,8 +171,12 @@ flx_decode_chunks (GstFlxDec *flxdec , gulong count, gchar *data, gchar *dest)
     switch(hdr->id) 
     {
       case FLX_COLOR64:
+        flx_decode_color(flxdec, data, dest, 2);
+        data += rndalign(hdr->size) - FlxFrameChunkSize;
+        break;
+
       case FLX_COLOR256:
-        flx_decode_color(flxdec, data, dest);
+        flx_decode_color(flxdec, data, dest, 0);
         data += rndalign(hdr->size) - FlxFrameChunkSize;
         break;
 
@@ -234,7 +215,7 @@ flx_decode_chunks (GstFlxDec *flxdec , gulong count, gchar *data, gchar *dest)
 
 
 static void
-flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest)
+flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest, gint scale)
 {
   guint packs, count, indx;
 
@@ -256,7 +237,7 @@ flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest)
       count = 256;
 
     g_print("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
-    flx_set_palette_vector(flxdec->converter, indx, count, data);
+    flx_set_palette_vector(flxdec->converter, indx, count, data, scale);
 
     data += (count * 3);
   }
@@ -330,8 +311,6 @@ flx_decode_delta_fli(GstFlxDec *flxdec, guchar *data, guchar *dest)
     /* packet count */
     packets = *data++;
 
-    dest = start_p + (flxdec->hdr.width * (start_l - lines));
-
     while(packets--) {
       /* skip count */
       dest += *data++;
@@ -353,6 +332,8 @@ flx_decode_delta_fli(GstFlxDec *flxdec, guchar *data, guchar *dest)
           *dest++ = *data++;
       }
     }
+    start_p += flxdec->hdr.width;
+    dest = start_p;
   }                  
 }
 
@@ -376,7 +357,7 @@ flx_decode_delta_flc(GstFlxDec *flxdec, guchar *data, guchar *dest)
   start_p    = dest;
   start_l    = lines;
 
-  while(lines--) {
+  while (lines) {
     dest = start_p + (flxdec->hdr.width * (start_l - lines));
 
     /* process opcode(s) */
@@ -418,6 +399,7 @@ flx_decode_delta_flc(GstFlxDec *flxdec, guchar *data, guchar *dest)
         }
       }
     }
+    lines--;
   }
 }
           
@@ -425,18 +407,17 @@ static GstBuffer*
 flx_get_data(GstFlxDec *flxdec, gulong size)
 {
   GstBuffer *retbuf;
+  guint32 got_bytes;
 
   g_return_val_if_fail (flxdec != NULL, NULL);
 
-  if (flxdec->new_buf) {
-    retbuf = gst_pad_pullregion(flxdec->sinkpad, 
-        GST_REGION_OFFSET_LEN, 0, size);
-    flxdec->new_buf = FALSE;
-    flxdec->offset = size;
-  } else {
-    retbuf = gst_pad_pullregion(flxdec->sinkpad, GST_REGION_OFFSET_LEN, 
-        flxdec->offset, size);
-    flxdec->offset += size;
+  got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
+  if (got_bytes < size) {
+    GstEvent *event;
+    guint32 remaining;
+
+    gst_bytestream_get_status (flxdec->bs, &remaining, &event);
+    gst_pad_event_default (flxdec->sinkpad, event);
   }
 
   return retbuf;
@@ -449,6 +430,7 @@ gst_flxdec_loop (GstElement *element)
   GstBuffer  *buf;  
   GstBuffer  *databuf;
   guchar     *data, *chunk;
+  GstCaps   *caps;
 
   GstFlxDec         *flxdec;  
   FlxHeader      *flxh;
@@ -457,87 +439,90 @@ gst_flxdec_loop (GstElement *element)
   g_return_if_fail (element != NULL);
   g_return_if_fail (GST_IS_FLXDEC(element));
 
-  GST_DEBUG (0, "entering loop function\n");
+  GST_DEBUG ("entering loop function");
   
   flxdec = GST_FLXDEC(element);
 
-  databuf = flx_get_data(flxdec, FlxHeaderSize);
+  if (flxdec->state == GST_FLXDEC_READ_HEADER) {
+    databuf = flx_get_data(flxdec, FlxHeaderSize);
 
-  g_return_if_fail (databuf != NULL);
+    if (!databuf) {
+      g_print ("empty buffer\n");
+      return;
+    }
 
-  data = GST_BUFFER_DATA(databuf);
+    data = GST_BUFFER_DATA(databuf);
 
-  memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
+    memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
 
-  gst_buffer_unref (databuf);
+    gst_buffer_unref (databuf);
 
-  flxh = &flxdec->hdr;
+    flxh = &flxdec->hdr;
 
-  // check header
-  if (flxh->type != FLX_MAGICHDR_FLI &&
+    /* check header */
+    if (flxh->type != FLX_MAGICHDR_FLI &&
       flxh->type != FLX_MAGICHDR_FLC &&
-      flxh->type != FLX_MAGICHDR_FLX) 
+      flxh->type != FLX_MAGICHDR_FLX) {
+      gst_element_error (element, "not a flx file (type %d)\n", flxh->type);
       return;
+    }
   
   
-  g_print("GstFlxDec:       size      :  %d\n", flxh->size);
-  g_print("GstFlxDec:       frames    :  %d\n", flxh->frames);
-  g_print("GstFlxDec:       width     :  %d\n", flxh->width);
-  g_print("GstFlxDec:       height    :  %d\n", flxh->height);
-  g_print("GstFlxDec:       depth     :  %d\n", flxh->depth);
-  g_print("GstFlxDec:       speed     :  %d\n", flxh->speed);
-
-  gst_pad_set_caps (flxdec->srcpad,
-               gst_caps_new (
-                 "src_video",
-                 "video/raw",
-                 gst_props_new (
-                   "format",       GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
-                     "bpp",        GST_PROPS_INT (32),
-                     "depth",      GST_PROPS_INT (32),
-                     "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
-                     "red_mask",   GST_PROPS_INT (0x00ff0000),
-                     "green_mask", GST_PROPS_INT (0x0000ff00),
-                     "blue_mask",  GST_PROPS_INT (0x000000ff),
-                     "width",      GST_PROPS_INT (flxh->width), 
-                     "height",     GST_PROPS_INT (flxh->height),
-                   NULL)));
-
-  if (flxh->depth <= 8) 
-    flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
-
-  if (flxh->type == FLX_MAGICHDR_FLC || 
-      flxh->type == FLX_MAGICHDR_FLX) {
-    g_print("GstFlxDec: (FLC) aspect_dx :  %d\n", 
-      flxh->aspect_dx);
-    g_print("GstFlxDec: (FLC) aspect_dy :  %d\n", 
-      flxh->aspect_dy);
-    g_print("GstFlxDec: (FLC) oframe1   :  0x%08x\n", 
-      flxh->oframe1);
-    g_print("GstFlxDec: (FLC) oframe2   :  0x%08x\n", 
-      flxh->oframe2);
-  }
+    g_print("GstFlxDec:       size      :  %d\n", flxh->size);
+    g_print("GstFlxDec:       frames    :  %d\n", flxh->frames);
+    g_print("GstFlxDec:       width     :  %d\n", flxh->width);
+    g_print("GstFlxDec:       height    :  %d\n", flxh->height);
+    g_print("GstFlxDec:       depth     :  %d\n", flxh->depth);
+    g_print("GstFlxDec:       speed     :  %d\n", flxh->speed);
+
+    flxdec->next_time = 0;
 
+    if (flxh->type == FLX_MAGICHDR_FLI) {
+      flxdec->frame_time = JIFFIE * flxh->speed;
+    }
+    else {
+      flxdec->frame_time = flxh->speed * GST_MSECOND;
+    }
+    
+    caps = gst_caps_from_string (GST_VIDEO_RGB_PAD_TEMPLATE_CAPS_32);
+    gst_caps_set_simple (caps,
+       "width", G_TYPE_INT, flxh->width, 
+       "height", G_TYPE_INT, flxh->height,
+       "framerate",  G_TYPE_DOUBLE, GST_SECOND/flxdec->frame_time, NULL);
+
+    if (flxh->depth <= 8) 
+      flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
+
+    if (flxh->type == FLX_MAGICHDR_FLC || 
+        flxh->type == FLX_MAGICHDR_FLX) {
+      g_print("GstFlxDec: (FLC) aspect_dx :  %d\n", flxh->aspect_dx);
+      g_print("GstFlxDec: (FLC) aspect_dy :  %d\n", flxh->aspect_dy);
+      g_print("GstFlxDec: (FLC) oframe1   :  0x%08x\n", flxh->oframe1);
+      g_print("GstFlxDec: (FLC) oframe2   :  0x%08x\n", flxh->oframe2);
+    }
   
-  flxdec->size = (flxh->width * flxh->height);
+    flxdec->size = (flxh->width * flxh->height);
   
-  // create delta and output frame 
-  flxdec->frame = gst_buffer_new();
-  flxdec->delta = gst_buffer_new();
-  GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
-  GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
-  GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
-  GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
-
-  do
-  {
+    /* create delta and output frame */
+    flxdec->frame = gst_buffer_new();
+    flxdec->delta = gst_buffer_new();
+    GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
+    GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
+    GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
+    GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
+
+    flxdec->state = GST_FLXDEC_PLAYING;
+  }
+  else if (flxdec->state == GST_FLXDEC_PLAYING) {
+    GstBuffer *out;
 
     databuf = flx_get_data(flxdec, FlxFrameChunkSize);
+    if (!databuf)
+      return;
 
     flxfh = (FlxFrameChunk *) GST_BUFFER_DATA(databuf);
     
-    switch(flxfh->id)
-    {
+    switch(flxfh->id) {
       case FLX_FRAME_TYPE:
         buf = flx_get_data(flxdec, flxfh->size-FlxFrameChunkSize);
  
@@ -546,43 +531,75 @@ gst_flxdec_loop (GstElement *element)
         if (((FlxFrameType *)chunk)->chunks == 0)
           break;
 
-        // create 32 bits output frame
-        flxdec->out = gst_buffer_new();
-        GST_BUFFER_DATA(flxdec->out) = g_malloc(flxdec->size * 4);
-        GST_BUFFER_SIZE(flxdec->out) = flxdec->size * 4;
+        /* create 32 bits output frame */
+        out = gst_buffer_new();
+        GST_BUFFER_DATA(out) = g_malloc(flxdec->size * 4);
+        GST_BUFFER_SIZE(out) = flxdec->size * 4;
 
-
-        // decode chunks 
+        /* decode chunks */
         flx_decode_chunks(flxdec, 
                          ((FlxFrameType *)chunk)->chunks, 
                          GST_BUFFER_DATA(buf) + FlxFrameTypeSize,
                          GST_BUFFER_DATA(flxdec->frame));
  
-        // destroy input buffer
+        /* destroy input buffer*/
         gst_buffer_unref(buf);
     
-        // save copy of the current frame for possible delta.
+        /* save copy of the current frame for possible delta. */
         memcpy(GST_BUFFER_DATA(flxdec->delta), 
                GST_BUFFER_DATA(flxdec->frame), 
                GST_BUFFER_SIZE(flxdec->delta));
 
-        // convert current frame.
+        /* convert current frame. */
         flx_colorspace_convert(flxdec->converter,
              GST_BUFFER_DATA(flxdec->frame),
-             GST_BUFFER_DATA(flxdec->out));
+             GST_BUFFER_DATA(out));
+
+       GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
+       flxdec->next_time += flxdec->frame_time;
 
-        gst_pad_push(flxdec->srcpad, flxdec->out);
+        gst_pad_push(flxdec->srcpad, GST_DATA (out));
         
         break;
     }
 
-    // destroy header buffer 
+    /* destroy header buffer */
     gst_buffer_unref(databuf);
+  }
+}
 
-    gst_element_yield (element);
+static GstElementStateReturn 
+gst_flxdec_change_state (GstElement *element)
+{
+  GstFlxDec *flxdec;
+
+  flxdec = GST_FLXDEC(element);
+
+  switch (GST_STATE_TRANSITION (element)) {
+    case GST_STATE_NULL_TO_READY:
+      break;
+    case GST_STATE_READY_TO_PAUSED:
+      flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
+      flxdec->state = GST_FLXDEC_READ_HEADER;
+      break;
+    case GST_STATE_PAUSED_TO_PLAYING:
+      break;
+    case GST_STATE_PLAYING_TO_PAUSED:
+      break;
+    case GST_STATE_PAUSED_TO_READY:
+      gst_buffer_unref (flxdec->frame);
+      flxdec->frame = NULL;
+      gst_buffer_unref (flxdec->delta);
+      flxdec->delta = NULL;
+      gst_bytestream_destroy (flxdec->bs);
+      break;
+    case GST_STATE_READY_TO_NULL:
+      break;
   }
-  while (TRUE);
+  
+  parent_class->change_state (element);
 
+  return GST_STATE_SUCCESS;
 }
 
 static void 
@@ -617,28 +634,23 @@ gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSp
 }
 
 static gboolean
-plugin_init (GModule *module, GstPlugin *plugin)
+plugin_init (GstPlugin *plugin)
 {
-  GstElementFactory *factory;
-  GstTypeFactory *type;
+  if (!gst_library_load ("gstbytestream"))
+    return FALSE;
 
-  factory = gst_elementfactory_new("flxdec", GST_TYPE_FLXDEC, &flxdec_details);
-  g_return_val_if_fail(factory != NULL, FALSE);
-
-  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_factory));
-  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_video_factory));
-
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
-
-  type = gst_typefactory_new (&flxdec_definition);
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
-
-  return TRUE;
+  return gst_element_register (plugin, "flxdec",
+                              GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
 }
 
-GstPluginDesc plugin_desc = {
+GST_PLUGIN_DEFINE (
   GST_VERSION_MAJOR,
   GST_VERSION_MINOR,
   "flxdec",
-  plugin_init
-};
+  "FLX video decoder",
+  plugin_init,
+  VERSION,
+  GST_LICENSE,
+  GST_PACKAGE,
+  GST_ORIGIN
+)