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/Decoder/Audio",
36 "Sepp Wijnands <mrrazz@garbage-coderz.net>"
39 /* Flx signals and args */
52 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("video/x-fli")
59 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
66 static void gst_flxdec_class_init (GstFlxDecClass * klass);
67 static void gst_flxdec_base_init (GstFlxDecClass * klass);
68 static void gst_flxdec_init (GstFlxDec * flxdec);
70 static void gst_flxdec_loop (GstElement * element);
72 static GstElementStateReturn gst_flxdec_change_state (GstElement * element);
74 static void gst_flxdec_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void gst_flxdec_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
80 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
81 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
82 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
83 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
85 #define rndalign(off) ((off) + ((off) % 2))
87 static GstElementClass *parent_class = NULL;
90 gst_flxdec_get_type (void)
92 static GType flxdec_type = 0;
95 static const GTypeInfo flxdec_info = {
96 sizeof (GstFlxDecClass),
97 (GBaseInitFunc) gst_flxdec_base_init,
99 (GClassInitFunc) gst_flxdec_class_init,
104 (GInstanceInitFunc) gst_flxdec_init,
107 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
113 gst_flxdec_base_init (GstFlxDecClass * klass)
115 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
117 gst_element_class_set_details (gstelement_class, &flxdec_details);
118 gst_element_class_add_pad_template (gstelement_class,
119 gst_static_pad_template_get (&sink_factory));
120 gst_element_class_add_pad_template (gstelement_class,
121 gst_static_pad_template_get (&src_video_factory));
125 gst_flxdec_class_init (GstFlxDecClass * klass)
127 GObjectClass *gobject_class;
128 GstElementClass *gstelement_class;
130 gobject_class = (GObjectClass *) klass;
131 gstelement_class = (GstElementClass *) klass;
133 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
135 gobject_class->set_property = gst_flxdec_set_property;
136 gobject_class->get_property = gst_flxdec_get_property;
138 gstelement_class->change_state = gst_flxdec_change_state;
144 gst_flxdec_init (GstFlxDec * flxdec)
147 gst_pad_new_from_template (gst_static_pad_template_get (&sink_factory),
149 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
150 gst_element_set_loop_function (GST_ELEMENT (flxdec), gst_flxdec_loop);
153 gst_pad_new_from_template (gst_static_pad_template_get
154 (&src_video_factory), "src");
155 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
158 flxdec->frame = NULL;
159 flxdec->delta = NULL;
163 flx_decode_chunks (GstFlxDec * flxdec, gulong count, gchar * data, gchar * dest)
167 g_return_if_fail (data != NULL);
170 hdr = (FlxFrameChunk *) data;
171 data += FlxFrameChunkSize;
175 flx_decode_color (flxdec, data, dest, 2);
176 data += rndalign (hdr->size) - FlxFrameChunkSize;
180 flx_decode_color (flxdec, data, dest, 0);
181 data += rndalign (hdr->size) - FlxFrameChunkSize;
185 flx_decode_brun (flxdec, data, dest);
186 data += rndalign (hdr->size) - FlxFrameChunkSize;
190 flx_decode_delta_fli (flxdec, data, dest);
191 data += rndalign (hdr->size) - FlxFrameChunkSize;
195 flx_decode_delta_flc (flxdec, data, dest);
196 data += rndalign (hdr->size) - FlxFrameChunkSize;
200 memset (dest, 0, flxdec->size);
204 data += rndalign (hdr->size) - FlxFrameChunkSize;
208 g_print ("GstFlxDec: Unimplented chunk type: 0x%02x size: %d\n",
210 g_print ("GstFlxDec: Skipping...\n");
211 data += rndalign (hdr->size) - FlxFrameChunkSize;
219 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
221 guint packs, count, indx;
223 g_return_if_fail (flxdec != NULL);
225 packs = (data[0] + (data[1] << 8));
230 g_print ("GstFlxDec: cmap packs: %d\n", packs);
232 /* color map index + skip count */
235 /* number of rgb triplets */
236 count = *data++ & 0xff;
240 g_print ("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
241 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
248 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
250 gulong count, lines, row;
253 g_return_if_fail (flxdec != NULL);
255 lines = flxdec->hdr.height;
258 * should not be used anymore, since the flc format can
259 * contain more then 255 RLE packets. we use the frame
264 row = flxdec->hdr.width;
270 count = 0x100 - count;
289 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
291 gulong count, packets, lines, start_line, start_l;
294 g_return_if_fail (flxdec != NULL);
295 g_return_if_fail (flxdec->delta != NULL);
298 /* use last frame for delta */
299 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
300 GST_BUFFER_SIZE (flxdec->delta));
302 start_line = (data[0] + (data[1] << 8));
303 lines = (data[2] + (data[3] << 8));
306 /* start position of delta */
307 dest += (flxdec->hdr.width * start_line);
324 count = 0x100 - count;
336 start_p += flxdec->hdr.width;
342 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
344 gulong count, lines, start_l, opcode;
347 g_return_if_fail (flxdec != NULL);
348 g_return_if_fail (flxdec->delta != NULL);
351 /* use last frame for delta */
352 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
353 GST_BUFFER_SIZE (flxdec->delta));
355 lines = (data[0] + (data[1] << 8));
362 dest = start_p + (flxdec->hdr.width * (start_l - lines));
364 /* process opcode(s) */
365 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
367 if ((opcode & 0xc000) == 0xc000) {
369 start_l += (0x10000 - opcode);
370 dest += flxdec->hdr.width * (0x10000 - opcode);
373 dest += flxdec->hdr.width;
374 *dest++ = (opcode & 0xff);
379 /* last opcode is the packet count */
388 /* replicate word run */
389 count = 0x100 - count;
396 /* literal word run */
408 flx_get_data (GstFlxDec * flxdec, gulong size)
413 g_return_val_if_fail (flxdec != NULL, NULL);
415 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
416 if (got_bytes < size) {
420 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
421 gst_pad_event_default (flxdec->sinkpad, event);
429 gst_flxdec_loop (GstElement * element)
433 guchar *data, *chunk;
438 FlxFrameChunk *flxfh;
440 g_return_if_fail (element != NULL);
441 g_return_if_fail (GST_IS_FLXDEC (element));
443 GST_DEBUG ("entering loop function");
445 flxdec = GST_FLXDEC (element);
447 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
448 databuf = flx_get_data (flxdec, FlxHeaderSize);
451 g_print ("empty buffer\n");
455 data = GST_BUFFER_DATA (databuf);
457 memcpy ((char *) &flxdec->hdr, data, sizeof (FlxHeader));
459 gst_buffer_unref (databuf);
464 if (flxh->type != FLX_MAGICHDR_FLI &&
465 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) {
466 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
467 ("not a flx file (type %d)\n", flxh->type));
472 g_print ("GstFlxDec: size : %d\n", flxh->size);
473 g_print ("GstFlxDec: frames : %d\n", flxh->frames);
474 g_print ("GstFlxDec: width : %d\n", flxh->width);
475 g_print ("GstFlxDec: height : %d\n", flxh->height);
476 g_print ("GstFlxDec: depth : %d\n", flxh->depth);
477 g_print ("GstFlxDec: speed : %d\n", flxh->speed);
479 flxdec->next_time = 0;
481 if (flxh->type == FLX_MAGICHDR_FLI) {
482 flxdec->frame_time = JIFFIE * flxh->speed;
484 flxdec->frame_time = flxh->speed * GST_MSECOND;
487 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
488 gst_caps_set_simple (caps,
489 "width", G_TYPE_INT, flxh->width,
490 "height", G_TYPE_INT, flxh->height,
491 "framerate", G_TYPE_DOUBLE, GST_SECOND / flxdec->frame_time, NULL);
493 if (flxh->depth <= 8)
495 flx_colorspace_converter_new (flxh->width, flxh->height);
497 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
498 g_print ("GstFlxDec: (FLC) aspect_dx : %d\n", flxh->aspect_dx);
499 g_print ("GstFlxDec: (FLC) aspect_dy : %d\n", flxh->aspect_dy);
500 g_print ("GstFlxDec: (FLC) oframe1 : 0x%08x\n", flxh->oframe1);
501 g_print ("GstFlxDec: (FLC) oframe2 : 0x%08x\n", flxh->oframe2);
504 flxdec->size = (flxh->width * flxh->height);
506 /* create delta and output frame */
507 flxdec->frame = gst_buffer_new ();
508 flxdec->delta = gst_buffer_new ();
509 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
510 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
511 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
512 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
514 flxdec->state = GST_FLXDEC_PLAYING;
515 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
518 databuf = flx_get_data (flxdec, FlxFrameChunkSize);
522 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA (databuf);
526 buf = flx_get_data (flxdec, flxfh->size - FlxFrameChunkSize);
528 chunk = GST_BUFFER_DATA (buf);
530 if (((FlxFrameType *) chunk)->chunks == 0)
533 /* create 32 bits output frame */
534 out = gst_buffer_new ();
535 GST_BUFFER_DATA (out) = g_malloc (flxdec->size * 4);
536 GST_BUFFER_SIZE (out) = flxdec->size * 4;
539 flx_decode_chunks (flxdec,
540 ((FlxFrameType *) chunk)->chunks,
541 GST_BUFFER_DATA (buf) + FlxFrameTypeSize,
542 GST_BUFFER_DATA (flxdec->frame));
544 /* destroy input buffer */
545 gst_buffer_unref (buf);
547 /* save copy of the current frame for possible delta. */
548 memcpy (GST_BUFFER_DATA (flxdec->delta),
549 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
551 /* convert current frame. */
552 flx_colorspace_convert (flxdec->converter,
553 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
555 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
556 flxdec->next_time += flxdec->frame_time;
558 gst_pad_push (flxdec->srcpad, GST_DATA (out));
563 /* destroy header buffer */
564 gst_buffer_unref (databuf);
568 static GstElementStateReturn
569 gst_flxdec_change_state (GstElement * element)
573 flxdec = GST_FLXDEC (element);
575 switch (GST_STATE_TRANSITION (element)) {
576 case GST_STATE_NULL_TO_READY:
578 case GST_STATE_READY_TO_PAUSED:
579 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
580 flxdec->state = GST_FLXDEC_READ_HEADER;
582 case GST_STATE_PAUSED_TO_PLAYING:
584 case GST_STATE_PLAYING_TO_PAUSED:
586 case GST_STATE_PAUSED_TO_READY:
587 gst_buffer_unref (flxdec->frame);
588 flxdec->frame = NULL;
589 gst_buffer_unref (flxdec->delta);
590 flxdec->delta = NULL;
591 gst_bytestream_destroy (flxdec->bs);
593 case GST_STATE_READY_TO_NULL:
597 parent_class->change_state (element);
599 return GST_STATE_SUCCESS;
603 gst_flxdec_set_property (GObject * object, guint prop_id, const GValue * value,
608 /* it's not null if we got it, but it might not be ours */
609 g_return_if_fail (GST_IS_FLXDEC (object));
610 flxdec = GST_FLXDEC (object);
619 gst_flxdec_get_property (GObject * object, guint prop_id, GValue * value,
624 /* it's not null if we got it, but it might not be ours */
625 g_return_if_fail (GST_IS_FLXDEC (object));
626 flxdec = GST_FLXDEC (object);
630 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
636 plugin_init (GstPlugin * plugin)
638 if (!gst_library_load ("gstbytestream"))
641 return gst_element_register (plugin, "flxdec",
642 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
645 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
649 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)