2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
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.
23 #include "gstflxdec.h"
25 #define JIFFIE (GST_SECOND/70)
27 static GstCaps* flxdec_type_find(GstBuffer *buf, gpointer private);
29 /* flx element information */
30 static GstElementDetails flxdec_details = {
32 "Codec/Audio/Decoder",
36 "Sepp Wijnands <mrrazz@garbage-coderz.net>"
40 static GstTypeDefinition flxdec_definition = {
47 /* Flx signals and args */
58 GST_PAD_TEMPLATE_FACTORY (sink_factory,
70 GST_PAD_TEMPLATE_FACTORY (src_video_factory,
77 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
78 "bpp", GST_PROPS_INT (32),
79 "depth", GST_PROPS_INT (32),
80 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
81 "red_mask", GST_PROPS_INT (0x00ff0000),
82 "green_mask", GST_PROPS_INT (0x0000ff00),
83 "blue_mask", GST_PROPS_INT (0x000000ff),
84 "width", GST_PROPS_INT_RANGE(320, 1280),
85 "height", GST_PROPS_INT_RANGE(200, 1024)
90 static void gst_flxdec_class_init (GstFlxDecClass *klass);
91 static void gst_flxdec_init (GstFlxDec *flxdec);
93 static void gst_flxdec_loop (GstElement *element);
95 static GstElementStateReturn
96 gst_flxdec_change_state (GstElement *element);
98 static void gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
99 static void gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
102 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
103 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
104 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
105 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
107 #define rndalign(off) ((off) + ((off) % 2))
109 static GstElementClass *parent_class = NULL;
112 flxdec_type_find (GstBuffer *buf, gpointer private)
114 guchar *data = GST_BUFFER_DATA(buf);
118 if ((data[4] == 0x11 || data[4] == 0x12
119 || data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
120 /* check the frame type of the first frame */
121 if ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1) {
122 g_print("GstFlxDec: found supported flx format\n");
123 new = gst_caps_new("flxdec_type_find","video/fli", NULL);
133 gst_flxdec_get_type(void)
135 static GType flxdec_type = 0;
138 static const GTypeInfo flxdec_info = {
139 sizeof(GstFlxDecClass), NULL,
141 (GClassInitFunc)gst_flxdec_class_init,
146 (GInstanceInitFunc)gst_flxdec_init,
148 flxdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
154 gst_flxdec_class_init (GstFlxDecClass *klass)
156 GObjectClass *gobject_class;
157 GstElementClass *gstelement_class;
159 gobject_class = (GObjectClass*)klass;
160 gstelement_class = (GstElementClass*)klass;
162 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
164 gobject_class->set_property = gst_flxdec_set_property;
165 gobject_class->get_property = gst_flxdec_get_property;
167 gstelement_class->change_state = gst_flxdec_change_state;
173 gst_flxdec_init(GstFlxDec *flxdec)
175 flxdec->sinkpad = gst_pad_new_from_template (
176 GST_PAD_TEMPLATE_GET (sink_factory), "sink");
177 gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->sinkpad);
178 gst_element_set_loop_function(GST_ELEMENT(flxdec),gst_flxdec_loop);
180 flxdec->srcpad = gst_pad_new_from_template (
181 GST_PAD_TEMPLATE_GET (src_video_factory), "src");
182 gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->srcpad);
185 flxdec->frame = NULL;
186 flxdec->delta = NULL;
190 flx_decode_chunks (GstFlxDec *flxdec , gulong count, gchar *data, gchar *dest)
194 g_return_if_fail(data != NULL);
197 hdr = (FlxFrameChunk *) data;
198 data += FlxFrameChunkSize;
203 flx_decode_color(flxdec, data, dest, 2);
204 data += rndalign(hdr->size) - FlxFrameChunkSize;
208 flx_decode_color(flxdec, data, dest, 0);
209 data += rndalign(hdr->size) - FlxFrameChunkSize;
213 flx_decode_brun(flxdec, data, dest);
214 data += rndalign(hdr->size) - FlxFrameChunkSize;
218 flx_decode_delta_fli(flxdec, data, dest);
219 data += rndalign(hdr->size) - FlxFrameChunkSize;
223 flx_decode_delta_flc(flxdec, data, dest);
224 data += rndalign(hdr->size) - FlxFrameChunkSize;
228 memset(dest, 0, flxdec->size);
232 data += rndalign(hdr->size) - FlxFrameChunkSize;
236 g_print("GstFlxDec: Unimplented chunk type: 0x%02x size: %d\n",
238 g_print("GstFlxDec: Skipping...\n");
239 data += rndalign(hdr->size) - FlxFrameChunkSize;
247 flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest, gint scale)
249 guint packs, count, indx;
251 g_return_if_fail(flxdec != NULL);
253 packs = (data[0] + (data[1] << 8));
258 g_print("GstFlxDec: cmap packs: %d\n", packs);
260 /* color map index + skip count */
263 /* number of rgb triplets */
264 count = *data++ & 0xff;
268 g_print("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
269 flx_set_palette_vector(flxdec->converter, indx, count, data, scale);
276 flx_decode_brun(GstFlxDec *flxdec, guchar *data, guchar *dest)
278 gulong count, lines, row;
281 g_return_if_fail(flxdec != NULL);
283 lines = flxdec->hdr.height;
286 * should not be used anymore, since the flc format can
287 * contain more then 255 RLE packets. we use the frame
292 row = flxdec->hdr.width;
298 count = 0x100 - count;
317 flx_decode_delta_fli(GstFlxDec *flxdec, guchar *data, guchar *dest)
319 gulong count, packets, lines, start_line, start_l;
322 g_return_if_fail(flxdec != NULL);
323 g_return_if_fail(flxdec->delta != NULL);
326 /* use last frame for delta */
327 memcpy(dest, GST_BUFFER_DATA(flxdec->delta),
328 GST_BUFFER_SIZE(flxdec->delta));
330 start_line = (data[0] + (data[1] << 8));
331 lines = (data[2] + (data[3] << 8));
334 /* start position of delta */
335 dest += (flxdec->hdr.width * start_line);
352 count = 0x100 - count;
364 start_p += flxdec->hdr.width;
370 flx_decode_delta_flc(GstFlxDec *flxdec, guchar *data, guchar *dest)
372 gulong count, lines, start_l, opcode;
375 g_return_if_fail(flxdec != NULL);
376 g_return_if_fail(flxdec->delta != NULL);
379 /* use last frame for delta */
380 memcpy(dest, GST_BUFFER_DATA(flxdec->delta),
381 GST_BUFFER_SIZE(flxdec->delta));
383 lines = (data[0] + (data[1] << 8));
390 dest = start_p + (flxdec->hdr.width * (start_l - lines));
392 /* process opcode(s) */
393 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
395 if ((opcode & 0xc000) == 0xc000) {
397 start_l += (0x10000 - opcode);
398 dest += flxdec->hdr.width * (0x10000 - opcode);
401 dest += flxdec->hdr.width;
402 *dest++ = (opcode & 0xff);
407 /* last opcode is the packet count */
416 /* replicate word run */
417 count = 0x100 - count;
424 /* literal word run */
436 flx_get_data(GstFlxDec *flxdec, gulong size)
441 g_return_val_if_fail (flxdec != NULL, NULL);
443 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
444 if (got_bytes < size) {
448 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
449 gst_pad_event_default (flxdec->sinkpad, event);
457 gst_flxdec_loop (GstElement *element)
461 guchar *data, *chunk;
465 FlxFrameChunk *flxfh;
467 g_return_if_fail (element != NULL);
468 g_return_if_fail (GST_IS_FLXDEC(element));
470 GST_DEBUG (0, "entering loop function");
472 flxdec = GST_FLXDEC(element);
474 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
475 databuf = flx_get_data(flxdec, FlxHeaderSize);
478 g_print ("empty buffer\n");
482 data = GST_BUFFER_DATA(databuf);
484 memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
486 gst_buffer_unref (databuf);
491 if (flxh->type != FLX_MAGICHDR_FLI &&
492 flxh->type != FLX_MAGICHDR_FLC &&
493 flxh->type != FLX_MAGICHDR_FLX) {
494 gst_element_error (element, "not a flx file (type %d)\n", flxh->type);
499 g_print("GstFlxDec: size : %d\n", flxh->size);
500 g_print("GstFlxDec: frames : %d\n", flxh->frames);
501 g_print("GstFlxDec: width : %d\n", flxh->width);
502 g_print("GstFlxDec: height : %d\n", flxh->height);
503 g_print("GstFlxDec: depth : %d\n", flxh->depth);
504 g_print("GstFlxDec: speed : %d\n", flxh->speed);
506 flxdec->next_time = 0;
508 if (flxh->type == FLX_MAGICHDR_FLI) {
509 flxdec->frame_time = JIFFIE * flxh->speed;
512 flxdec->frame_time = flxh->speed * GST_USECOND;
515 gst_pad_try_set_caps (flxdec->srcpad,
520 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
521 "bpp", GST_PROPS_INT (32),
522 "depth", GST_PROPS_INT (32),
523 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
524 "red_mask", GST_PROPS_INT (0x00ff0000),
525 "green_mask", GST_PROPS_INT (0x0000ff00),
526 "blue_mask", GST_PROPS_INT (0x000000ff),
527 "width", GST_PROPS_INT (flxh->width),
528 "height", GST_PROPS_INT (flxh->height),
531 if (flxh->depth <= 8)
532 flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
534 if (flxh->type == FLX_MAGICHDR_FLC ||
535 flxh->type == FLX_MAGICHDR_FLX) {
536 g_print("GstFlxDec: (FLC) aspect_dx : %d\n", flxh->aspect_dx);
537 g_print("GstFlxDec: (FLC) aspect_dy : %d\n", flxh->aspect_dy);
538 g_print("GstFlxDec: (FLC) oframe1 : 0x%08x\n", flxh->oframe1);
539 g_print("GstFlxDec: (FLC) oframe2 : 0x%08x\n", flxh->oframe2);
543 flxdec->size = (flxh->width * flxh->height);
545 /* create delta and output frame */
546 flxdec->frame = gst_buffer_new();
547 flxdec->delta = gst_buffer_new();
548 GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
549 GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
550 GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
551 GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
553 flxdec->state = GST_FLXDEC_PLAYING;
555 else if (flxdec->state == GST_FLXDEC_PLAYING) {
558 databuf = flx_get_data(flxdec, FlxFrameChunkSize);
562 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA(databuf);
566 buf = flx_get_data(flxdec, flxfh->size-FlxFrameChunkSize);
568 chunk = GST_BUFFER_DATA(buf);
570 if (((FlxFrameType *)chunk)->chunks == 0)
573 /* create 32 bits output frame */
574 out = gst_buffer_new();
575 GST_BUFFER_DATA(out) = g_malloc(flxdec->size * 4);
576 GST_BUFFER_SIZE(out) = flxdec->size * 4;
579 flx_decode_chunks(flxdec,
580 ((FlxFrameType *)chunk)->chunks,
581 GST_BUFFER_DATA(buf) + FlxFrameTypeSize,
582 GST_BUFFER_DATA(flxdec->frame));
584 /* destroy input buffer*/
585 gst_buffer_unref(buf);
587 /* save copy of the current frame for possible delta. */
588 memcpy(GST_BUFFER_DATA(flxdec->delta),
589 GST_BUFFER_DATA(flxdec->frame),
590 GST_BUFFER_SIZE(flxdec->delta));
592 /* convert current frame. */
593 flx_colorspace_convert(flxdec->converter,
594 GST_BUFFER_DATA(flxdec->frame),
595 GST_BUFFER_DATA(out));
597 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
598 flxdec->next_time += flxdec->frame_time;
600 gst_pad_push(flxdec->srcpad, out);
605 /* destroy header buffer */
606 gst_buffer_unref(databuf);
610 static GstElementStateReturn
611 gst_flxdec_change_state (GstElement *element)
615 flxdec = GST_FLXDEC(element);
617 switch (GST_STATE_TRANSITION (element)) {
618 case GST_STATE_NULL_TO_READY:
620 case GST_STATE_READY_TO_PAUSED:
621 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
622 flxdec->state = GST_FLXDEC_READ_HEADER;
624 case GST_STATE_PAUSED_TO_PLAYING:
626 case GST_STATE_PLAYING_TO_PAUSED:
628 case GST_STATE_PAUSED_TO_READY:
629 gst_buffer_unref (flxdec->frame);
630 flxdec->frame = NULL;
631 gst_buffer_unref (flxdec->delta);
632 flxdec->delta = NULL;
633 gst_bytestream_destroy (flxdec->bs);
635 case GST_STATE_READY_TO_NULL:
639 parent_class->change_state (element);
641 return GST_STATE_SUCCESS;
645 gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
649 /* it's not null if we got it, but it might not be ours */
650 g_return_if_fail(GST_IS_FLXDEC(object));
651 flxdec = GST_FLXDEC(object);
660 gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
664 /* it's not null if we got it, but it might not be ours */
665 g_return_if_fail(GST_IS_FLXDEC(object));
666 flxdec = GST_FLXDEC(object);
670 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
676 plugin_init (GModule *module, GstPlugin *plugin)
678 GstElementFactory *factory;
679 GstTypeFactory *type;
681 /* this filter needs the bytestream package */
682 if (!gst_library_load ("gstbytestream"))
685 factory = gst_element_factory_new("flxdec", GST_TYPE_FLXDEC, &flxdec_details);
686 g_return_val_if_fail(factory != NULL, FALSE);
687 gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_PRIMARY);
689 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_factory));
690 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_video_factory));
692 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
694 type = gst_type_factory_new (&flxdec_definition);
695 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
700 GstPluginDesc plugin_desc = {