Fix the "64 colors flx too dark" bug.
[platform/upstream/gst-plugins-good.git] / gst / flx / gstflxdec.c
1 /* Gnome-Streamer
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 #include <string.h>
21
22 #include "flx_fmt.h"
23 #include "gstflxdec.h"
24
25 #define JIFFIE  (1000000/70)
26
27 static GstCaps* flxdec_typefind(GstBuffer *buf, gpointer private);
28
29 /* flx element information */
30 static GstElementDetails flxdec_details = {
31   "FLX Decoder",
32   "flxdec",
33   "FLX decoder",
34   VERSION,
35   "Sepp Wijnands <mrrazz@garbage-coderz.net>"
36   "(C) 2001",
37 };
38
39 static GstTypeDefinition flxdec_definition = {
40   "flxdec_video/fli",
41   "video/fli",
42   ".flc .fli",
43   flxdec_typefind,
44 };
45
46 /* Flx signals and args */
47 enum {
48   /* FILL ME */
49   LAST_SIGNAL
50 };
51
52 enum {
53   ARG_0
54 };
55
56 /* input */
57 GST_PADTEMPLATE_FACTORY (sink_factory,
58   "sink",          
59   GST_PAD_SINK, 
60   GST_PAD_ALWAYS,
61   GST_CAPS_NEW (
62     "flxdec_sink",         
63     "video/fli",
64      NULL
65   )
66 )
67
68 /* output */
69 GST_PADTEMPLATE_FACTORY (src_video_factory,
70   "src",
71   GST_PAD_SRC,
72   GST_PAD_ALWAYS,
73   GST_CAPS_NEW (
74     "src_video",
75     "video/raw",
76       "format",       GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
77         "bpp",        GST_PROPS_INT (32),
78         "depth",      GST_PROPS_INT (32),
79         "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
80         "red_mask",   GST_PROPS_INT (0x00ff0000),
81         "green_mask", GST_PROPS_INT (0x0000ff00),
82         "blue_mask",  GST_PROPS_INT (0x000000ff),
83         "width",      GST_PROPS_INT_RANGE(320, 1280), 
84         "height",     GST_PROPS_INT_RANGE(200, 1024)
85   )
86 )
87
88
89 static void     gst_flxdec_class_init   (GstFlxDecClass *klass);
90 static void     gst_flxdec_init         (GstFlxDec *flxdec);
91
92 static void     gst_flxdec_loop         (GstElement *element);
93
94 static GstElementStateReturn 
95                 gst_flxdec_change_state (GstElement *element);
96
97 static void     gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
98 static void     gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
99
100
101 static void     flx_decode_color        (GstFlxDec *, guchar *, guchar *, gint);
102 static void     flx_decode_brun         (GstFlxDec *, guchar *, guchar *);
103 static void     flx_decode_delta_fli    (GstFlxDec *, guchar *, guchar *);
104 static void     flx_decode_delta_flc    (GstFlxDec *, guchar *, guchar *);
105
106 #define rndalign(off) ((off) + ((off) % 2))
107
108 static GstElementClass *parent_class = NULL;
109
110 static GstCaps* 
111 flxdec_typefind (GstBuffer *buf, gpointer private)
112 {
113   guchar *data = GST_BUFFER_DATA(buf);
114   GstCaps *new;
115
116   /* check magic */
117   if ((data[4] == 0x11 || data[4] == 0x12
118        || data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
119       /* check the frame type of the first frame */
120       if ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1) {
121         g_print("GstFlxDec: found supported flx format\n");
122         new = gst_caps_new("flxdec_typefind","video/fli", NULL);
123         return new;
124       }
125   }
126   
127   return NULL; 
128 }
129
130
131 GType
132 gst_flxdec_get_type(void) 
133 {
134   static GType flxdec_type = 0;
135
136   if (!flxdec_type) {
137     static const GTypeInfo flxdec_info = {
138       sizeof(GstFlxDecClass),      NULL,
139       NULL,
140       (GClassInitFunc)gst_flxdec_class_init,
141       NULL,
142       NULL,
143       sizeof(GstFlxDec),
144       0,
145       (GInstanceInitFunc)gst_flxdec_init,
146     };
147     flxdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
148   }
149   return flxdec_type;
150 }
151
152 static void 
153 gst_flxdec_class_init (GstFlxDecClass *klass) 
154 {
155   GObjectClass *gobject_class;
156   GstElementClass *gstelement_class;
157
158   gobject_class = (GObjectClass*)klass;
159   gstelement_class = (GstElementClass*)klass;
160
161   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
162
163   gobject_class->set_property = NULL;  
164   gobject_class->get_property = NULL;
165
166   gstelement_class->change_state = gst_flxdec_change_state;
167 }
168
169
170   
171 static void 
172 gst_flxdec_init(GstFlxDec *flxdec) 
173 {
174   flxdec->sinkpad = gst_pad_new_from_template (
175                   GST_PADTEMPLATE_GET (sink_factory), "sink");
176   gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->sinkpad);
177   gst_element_set_loop_function(GST_ELEMENT(flxdec),gst_flxdec_loop);
178
179   flxdec->srcpad = gst_pad_new_from_template (
180                   GST_PADTEMPLATE_GET (src_video_factory), "src");
181   gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->srcpad);
182
183   flxdec->buf = NULL;
184   flxdec->bs = NULL;
185   flxdec->frame = NULL;
186   flxdec->delta = NULL;
187 }
188
189 static void
190 flx_decode_chunks (GstFlxDec *flxdec , gulong count, gchar *data, gchar *dest)
191 {
192   FlxFrameChunk  *hdr;
193
194   g_return_if_fail(data != NULL);
195
196   while (count--) {
197     hdr  = (FlxFrameChunk *) data;
198     data += FlxFrameChunkSize;
199
200     switch(hdr->id) 
201     {
202       case FLX_COLOR64:
203         flx_decode_color(flxdec, data, dest, 2);
204         data += rndalign(hdr->size) - FlxFrameChunkSize;
205         break;
206
207       case FLX_COLOR256:
208         flx_decode_color(flxdec, data, dest, 0);
209         data += rndalign(hdr->size) - FlxFrameChunkSize;
210         break;
211
212       case FLX_BRUN:
213         flx_decode_brun(flxdec, data, dest);
214         data += rndalign(hdr->size) - FlxFrameChunkSize;
215         break;
216
217       case FLX_LC:
218         flx_decode_delta_fli(flxdec, data, dest);
219         data += rndalign(hdr->size) - FlxFrameChunkSize;
220         break;
221
222       case FLX_SS2:
223         flx_decode_delta_flc(flxdec, data, dest);
224         data += rndalign(hdr->size) - FlxFrameChunkSize;
225         break;
226
227       case FLX_BLACK:
228         memset(dest, 0, flxdec->size);
229         break;
230
231       case FLX_MINI:
232         data += rndalign(hdr->size) - FlxFrameChunkSize;
233         break;
234
235       default:
236         g_print("GstFlxDec: Unimplented chunk type: 0x%02x size: %d\n",
237                  hdr->id, hdr->size);
238         g_print("GstFlxDec: Skipping...\n");
239         data += rndalign(hdr->size) - FlxFrameChunkSize;
240         break;
241     }
242   }
243 }
244
245
246 static void
247 flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest, gint scale)
248 {
249   guint packs, count, indx;
250
251   g_return_if_fail(flxdec != NULL);
252
253   packs = (data[0] + (data[1] << 8));
254
255   data += 2;
256   indx  = 0;
257
258   g_print("GstFlxDec: cmap packs: %d\n", packs);
259   while (packs--) {
260     /* color map index + skip count */
261     indx += *data++;
262
263     /* number of rgb triplets */
264     count = *data++ & 0xff;
265     if (count == 0)
266       count = 256;
267
268     g_print("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
269     flx_set_palette_vector(flxdec->converter, indx, count, data, scale);
270
271     data += (count * 3);
272   }
273 }
274
275 static void 
276 flx_decode_brun(GstFlxDec *flxdec, guchar *data, guchar *dest)
277 {
278   gulong  count, lines, row;
279   guchar  x;
280   
281   g_return_if_fail(flxdec != NULL);
282
283   lines = flxdec->hdr.height;
284   while(lines--) {
285     /* packet count.  
286      * should not be used anymore, since the flc format can
287      * contain more then 255 RLE packets. we use the frame 
288      * width instead. 
289      */
290     data++;
291
292     row = flxdec->hdr.width;
293     while(row) {
294       count = *data++;
295
296       if (count > 0x7f) { 
297         /* literal run */
298         count = 0x100 - count;
299         row -= count;
300
301         while(count--) 
302           *dest++ = *data++;
303         
304       } else {
305         /* replicate run */
306         row -= count;
307         x = *data++;
308
309         while(count--) 
310           *dest++ = x;
311       }
312     }
313   }
314 }
315
316 static void 
317 flx_decode_delta_fli(GstFlxDec *flxdec, guchar *data, guchar *dest)
318 {
319   gulong  count, packets, lines, start_line, start_l;
320   guchar  *start_p,  x;
321   
322   g_return_if_fail(flxdec != NULL);
323   g_return_if_fail(flxdec->delta != NULL);
324
325
326   /* use last frame for delta */
327   memcpy(dest, GST_BUFFER_DATA(flxdec->delta), 
328          GST_BUFFER_SIZE(flxdec->delta));
329
330   start_line = (data[0] + (data[1] << 8));
331   lines      = (data[2] + (data[3] << 8));
332   data    += 4;
333
334   /* start position of delta */
335   dest += (flxdec->hdr.width * start_line);
336   start_p = dest;
337   start_l = lines;
338
339   while(lines--) {
340     /* packet count */
341     packets = *data++;
342
343     dest = start_p + (flxdec->hdr.width * (start_l - lines));
344
345     while(packets--) {
346       /* skip count */
347       dest += *data++;
348
349       /* RLE count */
350       count = *data++;
351
352       if (count > 0x7f) { 
353         /* literal run */
354         count = 0x100 - count;
355         x = *data++;
356
357         while (count--)
358           *dest++ = x;
359
360       } else {
361         /* replicate run */
362         while (count--) 
363           *dest++ = *data++;
364       }
365     }
366   }                  
367 }
368
369 static void 
370 flx_decode_delta_flc(GstFlxDec *flxdec, guchar *data, guchar *dest)
371 {
372   gulong  count, lines, start_l, opcode;
373   guchar  *start_p;
374   
375   g_return_if_fail(flxdec != NULL);
376   g_return_if_fail(flxdec->delta != NULL);
377
378
379   /* use last frame for delta */
380   memcpy(dest, GST_BUFFER_DATA(flxdec->delta), 
381          GST_BUFFER_SIZE(flxdec->delta));
382
383   lines      = (data[0] + (data[1] << 8));
384   data    += 2;
385
386   start_p    = dest;
387   start_l    = lines;
388
389   while(lines--) {
390     dest = start_p + (flxdec->hdr.width * (start_l - lines));
391
392     /* process opcode(s) */
393     while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
394       data += 2;
395       if ((opcode & 0xc000) == 0xc000) {
396         /* skip count */
397         start_l += (0x10000 - opcode);  
398         dest    += flxdec->hdr.width * (0x10000 - opcode);
399       } else {
400         /* last pixel */
401         dest    += flxdec->hdr.width;
402         *dest++  = (opcode & 0xff);
403       }        
404     }
405     data += 2;
406
407     /* last opcode is the packet count */
408     while(opcode--) {
409       /* skip count */
410       dest += *data++;
411   
412       /* RLE count */
413       count = *data++;
414       
415       if (count > 0x7f) {
416         /* replicate word run */
417         count = 0x100 - count;
418         while (count--) {
419           *dest++ = data[0];
420           *dest++ = data[1];
421         }
422         data += 2;
423       } else {
424         /* literal word run */
425         while (count--) {
426           *dest++ = *data++;
427           *dest++ = *data++;
428         }
429       }
430     }
431   }
432 }
433           
434 static GstBuffer*
435 flx_get_data(GstFlxDec *flxdec, gulong size)
436 {
437   GstBuffer *retbuf;
438
439   g_return_val_if_fail (flxdec != NULL, NULL);
440
441   retbuf = gst_bytestream_read (flxdec->bs, size);
442   if (!retbuf) {
443     GstEvent *event;
444     guint32 remaining;
445
446     gst_bytestream_get_status (flxdec->bs, &remaining, &event);
447     gst_pad_event_default (flxdec->sinkpad, event);
448   }
449
450   return retbuf;
451 }
452
453
454 static void 
455 gst_flxdec_loop (GstElement *element) 
456 {
457   GstBuffer  *buf;  
458   GstBuffer  *databuf;
459   guchar     *data, *chunk;
460
461   GstFlxDec         *flxdec;  
462   FlxHeader      *flxh;
463   FlxFrameChunk  *flxfh;
464   
465   g_return_if_fail (element != NULL);
466   g_return_if_fail (GST_IS_FLXDEC(element));
467
468   GST_DEBUG (0, "entering loop function\n");
469   
470   flxdec = GST_FLXDEC(element);
471
472   if (flxdec->state == GST_FLXDEC_READ_HEADER) {
473     databuf = flx_get_data(flxdec, FlxHeaderSize);
474
475     data = GST_BUFFER_DATA(databuf);
476
477     memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
478
479     gst_buffer_unref (databuf);
480
481     flxh = &flxdec->hdr;
482
483     /* check header */
484     if (flxh->type != FLX_MAGICHDR_FLI &&
485       flxh->type != FLX_MAGICHDR_FLC &&
486       flxh->type != FLX_MAGICHDR_FLX) 
487       return;
488   
489   
490     g_print("GstFlxDec:       size      :  %d\n", flxh->size);
491     g_print("GstFlxDec:       frames    :  %d\n", flxh->frames);
492     g_print("GstFlxDec:       width     :  %d\n", flxh->width);
493     g_print("GstFlxDec:       height    :  %d\n", flxh->height);
494     g_print("GstFlxDec:       depth     :  %d\n", flxh->depth);
495     g_print("GstFlxDec:       speed     :  %d\n", flxh->speed);
496
497     flxdec->next_time = 0;
498
499     if (flxh->type == FLX_MAGICHDR_FLI) {
500       flxdec->frame_time = JIFFIE * flxh->speed;
501     }
502     else {
503       flxdec->frame_time = flxh->speed * 1000;
504     }
505     
506     gst_pad_try_set_caps (flxdec->srcpad,
507                 gst_caps_new (
508                   "src_video",
509                   "video/raw",
510                   gst_props_new (
511                     "format",       GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
512                       "bpp",        GST_PROPS_INT (32),
513                       "depth",      GST_PROPS_INT (32),
514                       "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
515                       "red_mask",   GST_PROPS_INT (0x00ff0000),
516                       "green_mask", GST_PROPS_INT (0x0000ff00),
517                       "blue_mask",  GST_PROPS_INT (0x000000ff),
518                       "width",      GST_PROPS_INT (flxh->width), 
519                       "height",     GST_PROPS_INT (flxh->height),
520                     NULL)));
521
522     if (flxh->depth <= 8) 
523       flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
524
525     if (flxh->type == FLX_MAGICHDR_FLC || 
526         flxh->type == FLX_MAGICHDR_FLX) {
527       g_print("GstFlxDec: (FLC) aspect_dx :  %d\n", flxh->aspect_dx);
528       g_print("GstFlxDec: (FLC) aspect_dy :  %d\n", flxh->aspect_dy);
529       g_print("GstFlxDec: (FLC) oframe1   :  0x%08x\n", flxh->oframe1);
530       g_print("GstFlxDec: (FLC) oframe2   :  0x%08x\n", flxh->oframe2);
531     }
532
533   
534     flxdec->size = (flxh->width * flxh->height);
535   
536     /* create delta and output frame */
537     flxdec->frame = gst_buffer_new();
538     flxdec->delta = gst_buffer_new();
539     GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
540     GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
541     GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
542     GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
543
544     flxdec->state = GST_FLXDEC_PLAYING;
545   }
546   else if (flxdec->state == GST_FLXDEC_PLAYING) {
547
548     databuf = flx_get_data(flxdec, FlxFrameChunkSize);
549
550     flxfh = (FlxFrameChunk *) GST_BUFFER_DATA(databuf);
551     
552     switch(flxfh->id) {
553       case FLX_FRAME_TYPE:
554         buf = flx_get_data(flxdec, flxfh->size-FlxFrameChunkSize);
555  
556         chunk = GST_BUFFER_DATA(buf);
557  
558         if (((FlxFrameType *)chunk)->chunks == 0)
559           break;
560
561         /* create 32 bits output frame */
562         flxdec->out = gst_buffer_new();
563         GST_BUFFER_DATA(flxdec->out) = g_malloc(flxdec->size * 4);
564         GST_BUFFER_SIZE(flxdec->out) = flxdec->size * 4;
565
566
567         /* decode chunks */
568         flx_decode_chunks(flxdec, 
569                          ((FlxFrameType *)chunk)->chunks, 
570                          GST_BUFFER_DATA(buf) + FlxFrameTypeSize,
571                          GST_BUFFER_DATA(flxdec->frame));
572  
573         /* destroy input buffer*/
574         gst_buffer_unref(buf);
575     
576         /* save copy of the current frame for possible delta. */
577         memcpy(GST_BUFFER_DATA(flxdec->delta), 
578                GST_BUFFER_DATA(flxdec->frame), 
579                GST_BUFFER_SIZE(flxdec->delta));
580
581         /* convert current frame. */
582         flx_colorspace_convert(flxdec->converter,
583              GST_BUFFER_DATA(flxdec->frame),
584              GST_BUFFER_DATA(flxdec->out));
585
586         GST_BUFFER_TIMESTAMP (flxdec->out) = flxdec->next_time;
587         flxdec->next_time += flxdec->frame_time;
588
589         gst_pad_push(flxdec->srcpad, flxdec->out);
590         
591         break;
592     }
593
594     /* destroy header buffer */
595     gst_buffer_unref(databuf);
596   }
597 }
598
599 static GstElementStateReturn 
600 gst_flxdec_change_state (GstElement *element)
601 {
602   GstFlxDec *flxdec;
603
604   flxdec = GST_FLXDEC(element);
605
606   switch (GST_STATE_TRANSITION (element)) {
607     case GST_STATE_NULL_TO_READY:
608       flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
609       break;
610     case GST_STATE_READY_TO_PAUSED:
611       flxdec->state = GST_FLXDEC_READ_HEADER;
612       break;
613     case GST_STATE_PAUSED_TO_PLAYING:
614       break;
615     case GST_STATE_PLAYING_TO_PAUSED:
616       break;
617     case GST_STATE_PAUSED_TO_READY:
618       gst_buffer_unref (flxdec->frame);
619       gst_buffer_unref (flxdec->delta);
620       break;
621     case GST_STATE_READY_TO_NULL:
622       gst_bytestream_destroy (flxdec->bs);
623       break;
624   }
625   
626   parent_class->change_state (element);
627
628   return GST_STATE_SUCCESS;
629 }
630
631 static void 
632 gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
633 {
634   GstFlxDec *flxdec;
635
636   /* it's not null if we got it, but it might not be ours */
637   g_return_if_fail(GST_IS_FLXDEC(object));
638   flxdec = GST_FLXDEC(object);
639
640   switch (prop_id) {
641     default:
642       break;
643   }
644 }
645
646 static void 
647 gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
648 {
649   GstFlxDec *flxdec;
650
651   /* it's not null if we got it, but it might not be ours */
652   g_return_if_fail(GST_IS_FLXDEC(object));
653   flxdec = GST_FLXDEC(object);
654
655   switch (prop_id) {
656     default:
657       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
658       break;
659   }
660 }
661
662 static gboolean
663 plugin_init (GModule *module, GstPlugin *plugin)
664 {
665   GstElementFactory *factory;
666   GstTypeFactory *type;
667
668   /* this filter needs the bytestream package */
669   if (!gst_library_load("gstbytestream")) {
670     gst_info("flxdec:: could not load support library: 'gstbytestream'\n");
671     return FALSE;
672   }
673
674   factory = gst_elementfactory_new("flxdec", GST_TYPE_FLXDEC, &flxdec_details);
675   g_return_val_if_fail(factory != NULL, FALSE);
676
677   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_factory));
678   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_video_factory));
679
680   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
681
682   type = gst_typefactory_new (&flxdec_definition);
683   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
684
685   return TRUE;
686 }
687
688 GstPluginDesc plugin_desc = {
689   GST_VERSION_MAJOR,
690   GST_VERSION_MINOR,
691   "flxdec",
692   plugin_init
693 };