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.
26 #include "gstflxdec.h"
27 #include <gst/video/video.h>
29 #define JIFFIE (GST_SECOND/70)
31 /* flx element information */
32 static GstElementDetails flxdec_details = {
34 "Codec/Audio/Decoder",
38 "Sepp Wijnands <mrrazz@garbage-coderz.net>",
42 /* Flx signals and args */
53 GST_PAD_TEMPLATE_FACTORY (sink_factory,
65 GST_PAD_TEMPLATE_FACTORY (src_video_factory,
72 "bpp", GST_PROPS_INT (32),
73 "depth", GST_PROPS_INT (32),
74 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
75 "red_mask", GST_PROPS_INT (R_MASK_32),
76 "green_mask", GST_PROPS_INT (G_MASK_32),
77 "blue_mask", GST_PROPS_INT (B_MASK_32),
78 "width", GST_PROPS_INT_RANGE(320, 1280),
79 "height", GST_PROPS_INT_RANGE(200, 1024),
80 "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT)
85 static void gst_flxdec_class_init (GstFlxDecClass *klass);
86 static void gst_flxdec_init (GstFlxDec *flxdec);
88 static void gst_flxdec_loop (GstElement *element);
90 static GstElementStateReturn
91 gst_flxdec_change_state (GstElement *element);
93 static void gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
94 static void gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
97 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
98 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
99 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
100 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
102 #define rndalign(off) ((off) + ((off) % 2))
104 static GstElementClass *parent_class = NULL;
107 gst_flxdec_get_type(void)
109 static GType flxdec_type = 0;
112 static const GTypeInfo flxdec_info = {
113 sizeof(GstFlxDecClass), NULL,
115 (GClassInitFunc)gst_flxdec_class_init,
120 (GInstanceInitFunc)gst_flxdec_init,
122 flxdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
128 gst_flxdec_class_init (GstFlxDecClass *klass)
130 GObjectClass *gobject_class;
131 GstElementClass *gstelement_class;
133 gobject_class = (GObjectClass*)klass;
134 gstelement_class = (GstElementClass*)klass;
136 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
138 gobject_class->set_property = gst_flxdec_set_property;
139 gobject_class->get_property = gst_flxdec_get_property;
141 gstelement_class->change_state = gst_flxdec_change_state;
147 gst_flxdec_init(GstFlxDec *flxdec)
149 flxdec->sinkpad = gst_pad_new_from_template (
150 GST_PAD_TEMPLATE_GET (sink_factory), "sink");
151 gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->sinkpad);
152 gst_element_set_loop_function(GST_ELEMENT(flxdec),gst_flxdec_loop);
154 flxdec->srcpad = gst_pad_new_from_template (
155 GST_PAD_TEMPLATE_GET (src_video_factory), "src");
156 gst_element_add_pad(GST_ELEMENT(flxdec),flxdec->srcpad);
159 flxdec->frame = NULL;
160 flxdec->delta = NULL;
164 flx_decode_chunks (GstFlxDec *flxdec , gulong count, gchar *data, gchar *dest)
168 g_return_if_fail(data != NULL);
171 hdr = (FlxFrameChunk *) data;
172 data += FlxFrameChunkSize;
177 flx_decode_color(flxdec, data, dest, 2);
178 data += rndalign(hdr->size) - FlxFrameChunkSize;
182 flx_decode_color(flxdec, data, dest, 0);
183 data += rndalign(hdr->size) - FlxFrameChunkSize;
187 flx_decode_brun(flxdec, data, dest);
188 data += rndalign(hdr->size) - FlxFrameChunkSize;
192 flx_decode_delta_fli(flxdec, data, dest);
193 data += rndalign(hdr->size) - FlxFrameChunkSize;
197 flx_decode_delta_flc(flxdec, data, dest);
198 data += rndalign(hdr->size) - FlxFrameChunkSize;
202 memset(dest, 0, flxdec->size);
206 data += rndalign(hdr->size) - FlxFrameChunkSize;
210 g_print("GstFlxDec: Unimplented chunk type: 0x%02x size: %d\n",
212 g_print("GstFlxDec: Skipping...\n");
213 data += rndalign(hdr->size) - FlxFrameChunkSize;
221 flx_decode_color(GstFlxDec *flxdec, guchar *data, guchar *dest, gint scale)
223 guint packs, count, indx;
225 g_return_if_fail(flxdec != NULL);
227 packs = (data[0] + (data[1] << 8));
232 g_print("GstFlxDec: cmap packs: %d\n", packs);
234 /* color map index + skip count */
237 /* number of rgb triplets */
238 count = *data++ & 0xff;
242 g_print("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
243 flx_set_palette_vector(flxdec->converter, indx, count, data, scale);
250 flx_decode_brun(GstFlxDec *flxdec, guchar *data, guchar *dest)
252 gulong count, lines, row;
255 g_return_if_fail(flxdec != NULL);
257 lines = flxdec->hdr.height;
260 * should not be used anymore, since the flc format can
261 * contain more then 255 RLE packets. we use the frame
266 row = flxdec->hdr.width;
272 count = 0x100 - count;
291 flx_decode_delta_fli(GstFlxDec *flxdec, guchar *data, guchar *dest)
293 gulong count, packets, lines, start_line, start_l;
296 g_return_if_fail(flxdec != NULL);
297 g_return_if_fail(flxdec->delta != NULL);
300 /* use last frame for delta */
301 memcpy(dest, GST_BUFFER_DATA(flxdec->delta),
302 GST_BUFFER_SIZE(flxdec->delta));
304 start_line = (data[0] + (data[1] << 8));
305 lines = (data[2] + (data[3] << 8));
308 /* start position of delta */
309 dest += (flxdec->hdr.width * start_line);
326 count = 0x100 - count;
338 start_p += flxdec->hdr.width;
344 flx_decode_delta_flc(GstFlxDec *flxdec, guchar *data, guchar *dest)
346 gulong count, lines, start_l, opcode;
349 g_return_if_fail(flxdec != NULL);
350 g_return_if_fail(flxdec->delta != NULL);
353 /* use last frame for delta */
354 memcpy(dest, GST_BUFFER_DATA(flxdec->delta),
355 GST_BUFFER_SIZE(flxdec->delta));
357 lines = (data[0] + (data[1] << 8));
364 dest = start_p + (flxdec->hdr.width * (start_l - lines));
366 /* process opcode(s) */
367 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
369 if ((opcode & 0xc000) == 0xc000) {
371 start_l += (0x10000 - opcode);
372 dest += flxdec->hdr.width * (0x10000 - opcode);
375 dest += flxdec->hdr.width;
376 *dest++ = (opcode & 0xff);
381 /* last opcode is the packet count */
390 /* replicate word run */
391 count = 0x100 - count;
398 /* literal word run */
410 flx_get_data(GstFlxDec *flxdec, gulong size)
415 g_return_val_if_fail (flxdec != NULL, NULL);
417 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
418 if (got_bytes < size) {
422 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
423 gst_pad_event_default (flxdec->sinkpad, event);
431 gst_flxdec_loop (GstElement *element)
435 guchar *data, *chunk;
439 FlxFrameChunk *flxfh;
441 g_return_if_fail (element != NULL);
442 g_return_if_fail (GST_IS_FLXDEC(element));
444 GST_DEBUG ("entering loop function");
446 flxdec = GST_FLXDEC(element);
448 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
449 databuf = flx_get_data(flxdec, FlxHeaderSize);
452 g_print ("empty buffer\n");
456 data = GST_BUFFER_DATA(databuf);
458 memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
460 gst_buffer_unref (databuf);
465 if (flxh->type != FLX_MAGICHDR_FLI &&
466 flxh->type != FLX_MAGICHDR_FLC &&
467 flxh->type != FLX_MAGICHDR_FLX) {
468 gst_element_error (element, "not a flx file (type %d)\n", flxh->type);
473 g_print("GstFlxDec: size : %d\n", flxh->size);
474 g_print("GstFlxDec: frames : %d\n", flxh->frames);
475 g_print("GstFlxDec: width : %d\n", flxh->width);
476 g_print("GstFlxDec: height : %d\n", flxh->height);
477 g_print("GstFlxDec: depth : %d\n", flxh->depth);
478 g_print("GstFlxDec: speed : %d\n", flxh->speed);
480 flxdec->next_time = 0;
482 if (flxh->type == FLX_MAGICHDR_FLI) {
483 flxdec->frame_time = JIFFIE * flxh->speed;
486 flxdec->frame_time = flxh->speed * GST_MSECOND;
489 gst_pad_try_set_caps (flxdec->srcpad,
493 "bpp", GST_PROPS_INT (32),
494 "depth", GST_PROPS_INT (32),
495 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
496 "red_mask", GST_PROPS_INT (R_MASK_32),
497 "green_mask", GST_PROPS_INT (G_MASK_32),
498 "blue_mask", GST_PROPS_INT (B_MASK_32),
499 "width", GST_PROPS_INT (flxh->width),
500 "height", GST_PROPS_INT (flxh->height),
501 "framerate", GST_PROPS_FLOAT (GST_SECOND/flxdec->frame_time)));
503 if (flxh->depth <= 8)
504 flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
506 if (flxh->type == FLX_MAGICHDR_FLC ||
507 flxh->type == FLX_MAGICHDR_FLX) {
508 g_print("GstFlxDec: (FLC) aspect_dx : %d\n", flxh->aspect_dx);
509 g_print("GstFlxDec: (FLC) aspect_dy : %d\n", flxh->aspect_dy);
510 g_print("GstFlxDec: (FLC) oframe1 : 0x%08x\n", flxh->oframe1);
511 g_print("GstFlxDec: (FLC) oframe2 : 0x%08x\n", flxh->oframe2);
515 flxdec->size = (flxh->width * flxh->height);
517 /* create delta and output frame */
518 flxdec->frame = gst_buffer_new();
519 flxdec->delta = gst_buffer_new();
520 GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
521 GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
522 GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
523 GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
525 flxdec->state = GST_FLXDEC_PLAYING;
527 else if (flxdec->state == GST_FLXDEC_PLAYING) {
530 databuf = flx_get_data(flxdec, FlxFrameChunkSize);
534 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA(databuf);
538 buf = flx_get_data(flxdec, flxfh->size-FlxFrameChunkSize);
540 chunk = GST_BUFFER_DATA(buf);
542 if (((FlxFrameType *)chunk)->chunks == 0)
545 /* create 32 bits output frame */
546 out = gst_buffer_new();
547 GST_BUFFER_DATA(out) = g_malloc(flxdec->size * 4);
548 GST_BUFFER_SIZE(out) = flxdec->size * 4;
551 flx_decode_chunks(flxdec,
552 ((FlxFrameType *)chunk)->chunks,
553 GST_BUFFER_DATA(buf) + FlxFrameTypeSize,
554 GST_BUFFER_DATA(flxdec->frame));
556 /* destroy input buffer*/
557 gst_buffer_unref(buf);
559 /* save copy of the current frame for possible delta. */
560 memcpy(GST_BUFFER_DATA(flxdec->delta),
561 GST_BUFFER_DATA(flxdec->frame),
562 GST_BUFFER_SIZE(flxdec->delta));
564 /* convert current frame. */
565 flx_colorspace_convert(flxdec->converter,
566 GST_BUFFER_DATA(flxdec->frame),
567 GST_BUFFER_DATA(out));
569 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
570 flxdec->next_time += flxdec->frame_time;
572 gst_pad_push(flxdec->srcpad, GST_DATA (out));
577 /* destroy header buffer */
578 gst_buffer_unref(databuf);
582 static GstElementStateReturn
583 gst_flxdec_change_state (GstElement *element)
587 flxdec = GST_FLXDEC(element);
589 switch (GST_STATE_TRANSITION (element)) {
590 case GST_STATE_NULL_TO_READY:
592 case GST_STATE_READY_TO_PAUSED:
593 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
594 flxdec->state = GST_FLXDEC_READ_HEADER;
596 case GST_STATE_PAUSED_TO_PLAYING:
598 case GST_STATE_PLAYING_TO_PAUSED:
600 case GST_STATE_PAUSED_TO_READY:
601 gst_buffer_unref (flxdec->frame);
602 flxdec->frame = NULL;
603 gst_buffer_unref (flxdec->delta);
604 flxdec->delta = NULL;
605 gst_bytestream_destroy (flxdec->bs);
607 case GST_STATE_READY_TO_NULL:
611 parent_class->change_state (element);
613 return GST_STATE_SUCCESS;
617 gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
621 /* it's not null if we got it, but it might not be ours */
622 g_return_if_fail(GST_IS_FLXDEC(object));
623 flxdec = GST_FLXDEC(object);
632 gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
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);
642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648 plugin_init (GModule *module, GstPlugin *plugin)
650 GstElementFactory *factory;
652 if (!gst_library_load ("gstbytestream"))
655 factory = gst_element_factory_new("flxdec", GST_TYPE_FLXDEC, &flxdec_details);
656 g_return_val_if_fail(factory != NULL, FALSE);
657 gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_PRIMARY);
659 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_factory));
660 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_video_factory));
662 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
667 GstPluginDesc plugin_desc = {