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 (1000000/70)
27 static GstCaps* flxdec_typefind(GstBuffer *buf, gpointer private);
29 /* flx element information */
30 static GstElementDetails flxdec_details = {
35 "Sepp Wijnands <mrrazz@garbage-coderz.net>"
39 static GstTypeDefinition flxdec_definition = {
46 /* Flx signals and args */
57 GST_PADTEMPLATE_FACTORY (sink_factory,
69 GST_PADTEMPLATE_FACTORY (src_video_factory,
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)
89 static void gst_flxdec_class_init (GstFlxDecClass *klass);
90 static void gst_flxdec_init (GstFlxDec *flxdec);
92 static void gst_flxdec_loop (GstElement *element);
94 static GstElementStateReturn
95 gst_flxdec_change_state (GstElement *element);
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);
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 *);
106 #define rndalign(off) ((off) + ((off) % 2))
108 static GstElementClass *parent_class = NULL;
111 flxdec_typefind (GstBuffer *buf, gpointer private)
113 guchar *data = GST_BUFFER_DATA(buf);
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);
132 gst_flxdec_get_type(void)
134 static GType flxdec_type = 0;
137 static const GTypeInfo flxdec_info = {
138 sizeof(GstFlxDecClass), NULL,
140 (GClassInitFunc)gst_flxdec_class_init,
145 (GInstanceInitFunc)gst_flxdec_init,
147 flxdec_type = g_type_register_static(GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
153 gst_flxdec_class_init (GstFlxDecClass *klass)
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
158 gobject_class = (GObjectClass*)klass;
159 gstelement_class = (GstElementClass*)klass;
161 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
163 gobject_class->set_property = NULL;
164 gobject_class->get_property = NULL;
166 gstelement_class->change_state = gst_flxdec_change_state;
172 gst_flxdec_init(GstFlxDec *flxdec)
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);
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);
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)
440 g_return_val_if_fail (flxdec != NULL, NULL);
442 retbuf = gst_bytestream_read (flxdec->bs, size);
447 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
448 gst_pad_event_default (flxdec->sinkpad, event);
456 gst_flxdec_loop (GstElement *element)
460 guchar *data, *chunk;
464 FlxFrameChunk *flxfh;
466 g_return_if_fail (element != NULL);
467 g_return_if_fail (GST_IS_FLXDEC(element));
469 GST_DEBUG (0, "entering loop function\n");
471 flxdec = GST_FLXDEC(element);
473 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
474 databuf = flx_get_data(flxdec, FlxHeaderSize);
476 data = GST_BUFFER_DATA(databuf);
478 memcpy((char *) &flxdec->hdr, data, sizeof(FlxHeader));
480 gst_buffer_unref (databuf);
485 if (flxh->type != FLX_MAGICHDR_FLI &&
486 flxh->type != FLX_MAGICHDR_FLC &&
487 flxh->type != FLX_MAGICHDR_FLX)
491 g_print("GstFlxDec: size : %d\n", flxh->size);
492 g_print("GstFlxDec: frames : %d\n", flxh->frames);
493 g_print("GstFlxDec: width : %d\n", flxh->width);
494 g_print("GstFlxDec: height : %d\n", flxh->height);
495 g_print("GstFlxDec: depth : %d\n", flxh->depth);
496 g_print("GstFlxDec: speed : %d\n", flxh->speed);
498 flxdec->next_time = 0;
500 if (flxh->type == FLX_MAGICHDR_FLI) {
501 flxdec->frame_time = JIFFIE * flxh->speed;
504 flxdec->frame_time = flxh->speed * 1000;
507 gst_pad_try_set_caps (flxdec->srcpad,
512 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('R', 'G', 'B', ' ')),
513 "bpp", GST_PROPS_INT (32),
514 "depth", GST_PROPS_INT (32),
515 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
516 "red_mask", GST_PROPS_INT (0x00ff0000),
517 "green_mask", GST_PROPS_INT (0x0000ff00),
518 "blue_mask", GST_PROPS_INT (0x000000ff),
519 "width", GST_PROPS_INT (flxh->width),
520 "height", GST_PROPS_INT (flxh->height),
523 if (flxh->depth <= 8)
524 flxdec->converter = flx_colorspace_converter_new(flxh->width, flxh->height);
526 if (flxh->type == FLX_MAGICHDR_FLC ||
527 flxh->type == FLX_MAGICHDR_FLX) {
528 g_print("GstFlxDec: (FLC) aspect_dx : %d\n", flxh->aspect_dx);
529 g_print("GstFlxDec: (FLC) aspect_dy : %d\n", flxh->aspect_dy);
530 g_print("GstFlxDec: (FLC) oframe1 : 0x%08x\n", flxh->oframe1);
531 g_print("GstFlxDec: (FLC) oframe2 : 0x%08x\n", flxh->oframe2);
535 flxdec->size = (flxh->width * flxh->height);
537 /* create delta and output frame */
538 flxdec->frame = gst_buffer_new();
539 flxdec->delta = gst_buffer_new();
540 GST_BUFFER_DATA(flxdec->frame) = g_malloc(flxdec->size);
541 GST_BUFFER_SIZE(flxdec->frame) = flxdec->size;
542 GST_BUFFER_DATA(flxdec->delta) = g_malloc(flxdec->size);
543 GST_BUFFER_SIZE(flxdec->delta) = flxdec->size;
545 flxdec->state = GST_FLXDEC_PLAYING;
547 else if (flxdec->state == GST_FLXDEC_PLAYING) {
549 databuf = flx_get_data(flxdec, FlxFrameChunkSize);
551 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA(databuf);
555 buf = flx_get_data(flxdec, flxfh->size-FlxFrameChunkSize);
557 chunk = GST_BUFFER_DATA(buf);
559 if (((FlxFrameType *)chunk)->chunks == 0)
562 /* create 32 bits output frame */
563 flxdec->out = gst_buffer_new();
564 GST_BUFFER_DATA(flxdec->out) = g_malloc(flxdec->size * 4);
565 GST_BUFFER_SIZE(flxdec->out) = flxdec->size * 4;
569 flx_decode_chunks(flxdec,
570 ((FlxFrameType *)chunk)->chunks,
571 GST_BUFFER_DATA(buf) + FlxFrameTypeSize,
572 GST_BUFFER_DATA(flxdec->frame));
574 /* destroy input buffer*/
575 gst_buffer_unref(buf);
577 /* save copy of the current frame for possible delta. */
578 memcpy(GST_BUFFER_DATA(flxdec->delta),
579 GST_BUFFER_DATA(flxdec->frame),
580 GST_BUFFER_SIZE(flxdec->delta));
582 /* convert current frame. */
583 flx_colorspace_convert(flxdec->converter,
584 GST_BUFFER_DATA(flxdec->frame),
585 GST_BUFFER_DATA(flxdec->out));
587 GST_BUFFER_TIMESTAMP (flxdec->out) = flxdec->next_time;
588 flxdec->next_time += flxdec->frame_time;
590 gst_pad_push(flxdec->srcpad, flxdec->out);
595 /* destroy header buffer */
596 gst_buffer_unref(databuf);
600 static GstElementStateReturn
601 gst_flxdec_change_state (GstElement *element)
605 flxdec = GST_FLXDEC(element);
607 switch (GST_STATE_TRANSITION (element)) {
608 case GST_STATE_NULL_TO_READY:
609 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
611 case GST_STATE_READY_TO_PAUSED:
612 flxdec->state = GST_FLXDEC_READ_HEADER;
614 case GST_STATE_PAUSED_TO_PLAYING:
616 case GST_STATE_PLAYING_TO_PAUSED:
618 case GST_STATE_PAUSED_TO_READY:
619 gst_buffer_unref (flxdec->frame);
620 gst_buffer_unref (flxdec->delta);
622 case GST_STATE_READY_TO_NULL:
623 gst_bytestream_destroy (flxdec->bs);
627 parent_class->change_state (element);
629 return GST_STATE_SUCCESS;
633 gst_flxdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
637 /* it's not null if we got it, but it might not be ours */
638 g_return_if_fail(GST_IS_FLXDEC(object));
639 flxdec = GST_FLXDEC(object);
648 gst_flxdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
652 /* it's not null if we got it, but it might not be ours */
653 g_return_if_fail(GST_IS_FLXDEC(object));
654 flxdec = GST_FLXDEC(object);
658 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
664 plugin_init (GModule *module, GstPlugin *plugin)
666 GstElementFactory *factory;
667 GstTypeFactory *type;
669 /* this filter needs the bytestream package */
670 if (!gst_library_load("gstbytestream")) {
671 gst_info("flxdec:: could not load support library: 'gstbytestream'\n");
675 factory = gst_elementfactory_new("flxdec", GST_TYPE_FLXDEC, &flxdec_details);
676 g_return_val_if_fail(factory != NULL, FALSE);
678 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_factory));
679 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_video_factory));
681 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
683 type = gst_typefactory_new (&flxdec_definition);
684 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
689 GstPluginDesc plugin_desc = {