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,
108 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
114 gst_flxdec_base_init (GstFlxDecClass * klass)
116 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
118 gst_element_class_set_details (gstelement_class, &flxdec_details);
119 gst_element_class_add_pad_template (gstelement_class,
120 gst_static_pad_template_get (&sink_factory));
121 gst_element_class_add_pad_template (gstelement_class,
122 gst_static_pad_template_get (&src_video_factory));
126 gst_flxdec_class_init (GstFlxDecClass * klass)
128 GObjectClass *gobject_class;
129 GstElementClass *gstelement_class;
131 gobject_class = (GObjectClass *) klass;
132 gstelement_class = (GstElementClass *) klass;
134 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136 gobject_class->set_property = gst_flxdec_set_property;
137 gobject_class->get_property = gst_flxdec_get_property;
139 gstelement_class->change_state = gst_flxdec_change_state;
145 gst_flxdec_init (GstFlxDec * flxdec)
148 gst_pad_new_from_template (gst_static_pad_template_get (&sink_factory),
150 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
151 gst_element_set_loop_function (GST_ELEMENT (flxdec), gst_flxdec_loop);
154 gst_pad_new_from_template (gst_static_pad_template_get
155 (&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;
176 flx_decode_color (flxdec, data, dest, 2);
177 data += rndalign (hdr->size) - FlxFrameChunkSize;
181 flx_decode_color (flxdec, data, dest, 0);
182 data += rndalign (hdr->size) - FlxFrameChunkSize;
186 flx_decode_brun (flxdec, data, dest);
187 data += rndalign (hdr->size) - FlxFrameChunkSize;
191 flx_decode_delta_fli (flxdec, data, dest);
192 data += rndalign (hdr->size) - FlxFrameChunkSize;
196 flx_decode_delta_flc (flxdec, data, dest);
197 data += rndalign (hdr->size) - FlxFrameChunkSize;
201 memset (dest, 0, flxdec->size);
205 data += rndalign (hdr->size) - FlxFrameChunkSize;
209 g_print ("GstFlxDec: Unimplented chunk type: 0x%02x size: %d\n",
211 g_print ("GstFlxDec: Skipping...\n");
212 data += rndalign (hdr->size) - FlxFrameChunkSize;
220 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
222 guint packs, count, indx;
224 g_return_if_fail (flxdec != NULL);
226 packs = (data[0] + (data[1] << 8));
231 g_print ("GstFlxDec: cmap packs: %d\n", packs);
233 /* color map index + skip count */
236 /* number of rgb triplets */
237 count = *data++ & 0xff;
241 g_print ("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
242 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
249 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
251 gulong count, lines, row;
254 g_return_if_fail (flxdec != NULL);
256 lines = flxdec->hdr.height;
259 * should not be used anymore, since the flc format can
260 * contain more then 255 RLE packets. we use the frame
265 row = flxdec->hdr.width;
271 count = 0x100 - count;
290 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
292 gulong count, packets, lines, start_line, start_l;
295 g_return_if_fail (flxdec != NULL);
296 g_return_if_fail (flxdec->delta != NULL);
299 /* use last frame for delta */
300 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
301 GST_BUFFER_SIZE (flxdec->delta));
303 start_line = (data[0] + (data[1] << 8));
304 lines = (data[2] + (data[3] << 8));
307 /* start position of delta */
308 dest += (flxdec->hdr.width * start_line);
325 count = 0x100 - count;
337 start_p += flxdec->hdr.width;
343 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
345 gulong count, lines, start_l, opcode;
348 g_return_if_fail (flxdec != NULL);
349 g_return_if_fail (flxdec->delta != NULL);
352 /* use last frame for delta */
353 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
354 GST_BUFFER_SIZE (flxdec->delta));
356 lines = (data[0] + (data[1] << 8));
363 dest = start_p + (flxdec->hdr.width * (start_l - lines));
365 /* process opcode(s) */
366 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
368 if ((opcode & 0xc000) == 0xc000) {
370 start_l += (0x10000 - opcode);
371 dest += flxdec->hdr.width * (0x10000 - opcode);
374 dest += flxdec->hdr.width;
375 *dest++ = (opcode & 0xff);
380 /* last opcode is the packet count */
389 /* replicate word run */
390 count = 0x100 - count;
397 /* literal word run */
409 flx_get_data (GstFlxDec * flxdec, gulong size)
414 g_return_val_if_fail (flxdec != NULL, NULL);
416 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
417 if (got_bytes < size) {
421 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
422 gst_pad_event_default (flxdec->sinkpad, event);
430 gst_flxdec_loop (GstElement * element)
434 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 && flxh->type != FLX_MAGICHDR_FLX) {
467 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
468 ("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;
485 flxdec->frame_time = flxh->speed * GST_MSECOND;
488 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
489 gst_caps_set_simple (caps,
490 "width", G_TYPE_INT, flxh->width,
491 "height", G_TYPE_INT, flxh->height,
492 "framerate", G_TYPE_DOUBLE, GST_SECOND / flxdec->frame_time, NULL);
494 if (flxh->depth <= 8)
496 flx_colorspace_converter_new (flxh->width, flxh->height);
498 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
499 g_print ("GstFlxDec: (FLC) aspect_dx : %d\n", flxh->aspect_dx);
500 g_print ("GstFlxDec: (FLC) aspect_dy : %d\n", flxh->aspect_dy);
501 g_print ("GstFlxDec: (FLC) oframe1 : 0x%08x\n", flxh->oframe1);
502 g_print ("GstFlxDec: (FLC) oframe2 : 0x%08x\n", flxh->oframe2);
505 flxdec->size = (flxh->width * flxh->height);
507 /* create delta and output frame */
508 flxdec->frame = gst_buffer_new ();
509 flxdec->delta = gst_buffer_new ();
510 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
511 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
512 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
513 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
515 flxdec->state = GST_FLXDEC_PLAYING;
516 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
519 databuf = flx_get_data (flxdec, FlxFrameChunkSize);
523 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA (databuf);
527 buf = flx_get_data (flxdec, flxfh->size - FlxFrameChunkSize);
529 chunk = GST_BUFFER_DATA (buf);
531 if (((FlxFrameType *) chunk)->chunks == 0)
534 /* create 32 bits output frame */
535 out = gst_buffer_new ();
536 GST_BUFFER_DATA (out) = g_malloc (flxdec->size * 4);
537 GST_BUFFER_SIZE (out) = flxdec->size * 4;
540 flx_decode_chunks (flxdec,
541 ((FlxFrameType *) chunk)->chunks,
542 GST_BUFFER_DATA (buf) + FlxFrameTypeSize,
543 GST_BUFFER_DATA (flxdec->frame));
545 /* destroy input buffer */
546 gst_buffer_unref (buf);
548 /* save copy of the current frame for possible delta. */
549 memcpy (GST_BUFFER_DATA (flxdec->delta),
550 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
552 /* convert current frame. */
553 flx_colorspace_convert (flxdec->converter,
554 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
556 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
557 flxdec->next_time += flxdec->frame_time;
559 gst_pad_push (flxdec->srcpad, GST_DATA (out));
564 /* destroy header buffer */
565 gst_buffer_unref (databuf);
569 static GstElementStateReturn
570 gst_flxdec_change_state (GstElement * element)
574 flxdec = GST_FLXDEC (element);
576 switch (GST_STATE_TRANSITION (element)) {
577 case GST_STATE_NULL_TO_READY:
579 case GST_STATE_READY_TO_PAUSED:
580 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
581 flxdec->state = GST_FLXDEC_READ_HEADER;
583 case GST_STATE_PAUSED_TO_PLAYING:
585 case GST_STATE_PLAYING_TO_PAUSED:
587 case GST_STATE_PAUSED_TO_READY:
588 gst_buffer_unref (flxdec->frame);
589 flxdec->frame = NULL;
590 gst_buffer_unref (flxdec->delta);
591 flxdec->delta = NULL;
592 gst_bytestream_destroy (flxdec->bs);
594 case GST_STATE_READY_TO_NULL:
598 parent_class->change_state (element);
600 return GST_STATE_SUCCESS;
604 gst_flxdec_set_property (GObject * object, guint prop_id, const GValue * value,
609 /* it's not null if we got it, but it might not be ours */
610 g_return_if_fail (GST_IS_FLXDEC (object));
611 flxdec = GST_FLXDEC (object);
620 gst_flxdec_get_property (GObject * object, guint prop_id, GValue * value,
625 /* it's not null if we got it, but it might not be ours */
626 g_return_if_fail (GST_IS_FLXDEC (object));
627 flxdec = GST_FLXDEC (object);
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
637 plugin_init (GstPlugin * plugin)
639 if (!gst_library_load ("gstbytestream"))
642 return gst_element_register (plugin, "flxdec",
643 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
646 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
650 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)