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.
20 * SECTION:element-flxdec
22 * This element decodes fli/flc/flx-video into raw video
25 * http://www.coolutils.com/Formats/FLI
26 * http://woodshole.er.usgs.gov/operations/modeling/flc.html
35 #include "gstflxdec.h"
36 #include <gst/video/video.h>
38 #define JIFFIE (GST_SECOND/70)
40 GST_DEBUG_CATEGORY_STATIC (flxdec_debug);
41 #define GST_CAT_DEFAULT flxdec_debug
44 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_STATIC_CAPS ("video/x-fli")
51 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
58 static void gst_flxdec_class_init (GstFlxDecClass * klass);
59 static void gst_flxdec_base_init (GstFlxDecClass * klass);
60 static void gst_flxdec_init (GstFlxDec * flxdec);
61 static void gst_flxdec_dispose (GstFlxDec * flxdec);
63 static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstBuffer * buf);
65 static GstStateChangeReturn gst_flxdec_change_state (GstElement * element,
66 GstStateChange transition);
68 static gboolean gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query);
69 static gboolean gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event);
70 static gboolean gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event);
72 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
73 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
74 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
75 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
77 #define rndalign(off) ((off) + ((off) & 1))
79 static GstElementClass *parent_class = NULL;
82 gst_flxdec_get_type (void)
84 static GType flxdec_type = 0;
87 static const GTypeInfo flxdec_info = {
88 sizeof (GstFlxDecClass),
89 (GBaseInitFunc) gst_flxdec_base_init,
91 (GClassInitFunc) gst_flxdec_class_init,
96 (GInstanceInitFunc) gst_flxdec_init,
100 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
106 gst_flxdec_base_init (GstFlxDecClass * klass)
108 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_set_details_simple (gstelement_class, "FLX video decoder",
111 "Codec/Decoder/Video",
112 "FLC/FLI/FLX video decoder",
113 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>");
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&sink_factory));
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&src_video_factory));
121 gst_flxdec_class_init (GstFlxDecClass * klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
129 parent_class = g_type_class_peek_parent (klass);
131 gobject_class->dispose = (GObjectFinalizeFunc) gst_flxdec_dispose;
133 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
135 gstelement_class->change_state = gst_flxdec_change_state;
139 gst_flxdec_init (GstFlxDec * flxdec)
141 flxdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
142 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
143 gst_pad_set_chain_function (flxdec->sinkpad, gst_flxdec_chain);
144 gst_pad_set_event_function (flxdec->sinkpad, gst_flxdec_sink_event_handler);
146 flxdec->srcpad = gst_pad_new_from_static_template (&src_video_factory, "src");
147 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
148 gst_pad_set_query_function (flxdec->srcpad, gst_flxdec_src_query_handler);
149 gst_pad_set_event_function (flxdec->srcpad, gst_flxdec_src_event_handler);
151 gst_pad_use_fixed_caps (flxdec->srcpad);
153 flxdec->frame = NULL;
154 flxdec->delta = NULL;
156 flxdec->adapter = gst_adapter_new ();
160 gst_flxdec_dispose (GstFlxDec * flxdec)
162 if (flxdec->adapter) {
163 g_object_unref (flxdec->adapter);
164 flxdec->adapter = NULL;
167 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) flxdec);
171 gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query)
173 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
174 gboolean ret = FALSE;
176 switch (GST_QUERY_TYPE (query)) {
177 case GST_QUERY_DURATION:
181 gst_query_parse_duration (query, &format, NULL);
183 if (format != GST_FORMAT_TIME)
186 gst_query_set_duration (query, format, flxdec->duration);
194 gst_object_unref (flxdec);
200 gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event)
202 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
205 /* TODO: implement the seek and other event handling */
207 ret = gst_pad_push_event (flxdec->sinkpad, event);
209 gst_object_unref (flxdec);
215 gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event)
220 flxdec = GST_FLXDEC (gst_pad_get_parent (pad));
222 ret = gst_pad_push_event (flxdec->srcpad, event);
224 gst_object_unref (flxdec);
229 flx_decode_chunks (GstFlxDec * flxdec, gulong count, guchar * data,
234 g_return_if_fail (data != NULL);
237 hdr = (FlxFrameChunk *) data;
238 FLX_FRAME_CHUNK_FIX_ENDIANNESS (hdr);
239 data += FlxFrameChunkSize;
243 flx_decode_color (flxdec, data, dest, 2);
244 data += rndalign (hdr->size) - FlxFrameChunkSize;
248 flx_decode_color (flxdec, data, dest, 0);
249 data += rndalign (hdr->size) - FlxFrameChunkSize;
253 flx_decode_brun (flxdec, data, dest);
254 data += rndalign (hdr->size) - FlxFrameChunkSize;
258 flx_decode_delta_fli (flxdec, data, dest);
259 data += rndalign (hdr->size) - FlxFrameChunkSize;
263 flx_decode_delta_flc (flxdec, data, dest);
264 data += rndalign (hdr->size) - FlxFrameChunkSize;
268 memset (dest, 0, flxdec->size);
272 data += rndalign (hdr->size) - FlxFrameChunkSize;
276 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
278 data += rndalign (hdr->size) - FlxFrameChunkSize;
286 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
288 guint packs, count, indx;
290 g_return_if_fail (flxdec != NULL);
292 packs = (data[0] + (data[1] << 8));
297 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
299 /* color map index + skip count */
302 /* number of rgb triplets */
303 count = *data++ & 0xff;
307 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)", count, indx);
308 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
315 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
317 gulong count, lines, row;
320 g_return_if_fail (flxdec != NULL);
322 lines = flxdec->hdr.height;
325 * should not be used anymore, since the flc format can
326 * contain more then 255 RLE packets. we use the frame
331 row = flxdec->hdr.width;
337 count = 0x100 - count;
356 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
358 gulong count, packets, lines, start_line;
361 g_return_if_fail (flxdec != NULL);
362 g_return_if_fail (flxdec->delta != NULL);
364 /* use last frame for delta */
365 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
366 GST_BUFFER_SIZE (flxdec->delta));
368 start_line = (data[0] + (data[1] << 8));
369 lines = (data[2] + (data[3] << 8));
372 /* start position of delta */
373 dest += (flxdec->hdr.width * start_line);
389 count = 0x100 - count;
401 start_p += flxdec->hdr.width;
407 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
409 gulong count, lines, start_l, opcode;
412 g_return_if_fail (flxdec != NULL);
413 g_return_if_fail (flxdec->delta != NULL);
415 /* use last frame for delta */
416 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
417 GST_BUFFER_SIZE (flxdec->delta));
419 lines = (data[0] + (data[1] << 8));
426 dest = start_p + (flxdec->hdr.width * (start_l - lines));
428 /* process opcode(s) */
429 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
431 if ((opcode & 0xc000) == 0xc000) {
433 start_l += (0x10000 - opcode);
434 dest += flxdec->hdr.width * (0x10000 - opcode);
437 dest += flxdec->hdr.width;
438 *dest++ = (opcode & 0xff);
443 /* last opcode is the packet count */
452 /* replicate word run */
453 count = 0x100 - count;
460 /* literal word run */
472 gst_flxdec_chain (GstPad * pad, GstBuffer * buf)
476 GstFlowReturn res = GST_FLOW_OK;
481 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
482 flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
483 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
485 gst_adapter_push (flxdec->adapter, buf);
486 avail = gst_adapter_available (flxdec->adapter);
488 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
489 if (avail >= FlxHeaderSize) {
490 const guint8 *data = gst_adapter_peek (flxdec->adapter, FlxHeaderSize);
492 memcpy ((gchar *) & flxdec->hdr, data, FlxHeaderSize);
493 FLX_HDR_FIX_ENDIANNESS (&(flxdec->hdr));
494 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
499 if (flxh->type != FLX_MAGICHDR_FLI &&
500 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX)
503 GST_LOG ("size : %d", flxh->size);
504 GST_LOG ("frames : %d", flxh->frames);
505 GST_LOG ("width : %d", flxh->width);
506 GST_LOG ("height : %d", flxh->height);
507 GST_LOG ("depth : %d", flxh->depth);
508 GST_LOG ("speed : %d", flxh->speed);
510 flxdec->next_time = 0;
512 if (flxh->type == FLX_MAGICHDR_FLI) {
513 flxdec->frame_time = JIFFIE * flxh->speed;
514 } else if (flxh->speed == 0) {
515 flxdec->frame_time = GST_SECOND / 70;
517 flxdec->frame_time = flxh->speed * GST_MSECOND;
520 flxdec->duration = flxh->frames * flxdec->frame_time;
521 GST_LOG ("duration : %" GST_TIME_FORMAT,
522 GST_TIME_ARGS (flxdec->duration));
524 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
525 gst_caps_set_simple (caps,
526 "width", G_TYPE_INT, flxh->width,
527 "height", G_TYPE_INT, flxh->height,
528 "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND,
529 (gint) flxdec->frame_time / 1000, NULL);
531 gst_pad_set_caps (flxdec->srcpad, caps);
532 gst_caps_unref (caps);
534 if (flxh->depth <= 8)
536 flx_colorspace_converter_new (flxh->width, flxh->height);
538 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
539 GST_LOG ("(FLC) aspect_dx : %d", flxh->aspect_dx);
540 GST_LOG ("(FLC) aspect_dy : %d", flxh->aspect_dy);
541 GST_LOG ("(FLC) oframe1 : 0x%08x", flxh->oframe1);
542 GST_LOG ("(FLC) oframe2 : 0x%08x", flxh->oframe2);
545 flxdec->size = (flxh->width * flxh->height);
547 /* create delta and output frame */
548 flxdec->frame = gst_buffer_new ();
549 flxdec->delta = gst_buffer_new ();
550 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
551 GST_BUFFER_MALLOCDATA (flxdec->frame) = GST_BUFFER_DATA (flxdec->frame);
552 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
553 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
554 GST_BUFFER_MALLOCDATA (flxdec->delta) = GST_BUFFER_DATA (flxdec->delta);
555 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
557 flxdec->state = GST_FLXDEC_PLAYING;
559 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
562 /* while we have enough data in the adapter */
563 while (avail >= FlxFrameChunkSize) {
569 data = gst_adapter_peek (flxdec->adapter, FlxFrameChunkSize);
570 memcpy (&flxfh, data, FlxFrameChunkSize);
571 FLX_FRAME_CHUNK_FIX_ENDIANNESS (&flxfh);
575 /* check if we have the complete frame */
576 if (avail < flxfh.size)
580 gst_adapter_flush (flxdec->adapter, FlxFrameChunkSize);
582 chunk = gst_adapter_take (flxdec->adapter,
583 flxfh.size - FlxFrameChunkSize);
584 FLX_FRAME_TYPE_FIX_ENDIANNESS ((FlxFrameType *) chunk);
585 if (((FlxFrameType *) chunk)->chunks == 0)
588 /* create 32 bits output frame */
589 res = gst_pad_alloc_buffer_and_set_caps (flxdec->srcpad,
590 GST_BUFFER_OFFSET_NONE,
591 flxdec->size * 4, GST_PAD_CAPS (flxdec->srcpad), &out);
592 if (res != GST_FLOW_OK)
596 flx_decode_chunks (flxdec,
597 ((FlxFrameType *) chunk)->chunks,
598 chunk + FlxFrameTypeSize, GST_BUFFER_DATA (flxdec->frame));
600 /* save copy of the current frame for possible delta. */
601 memcpy (GST_BUFFER_DATA (flxdec->delta),
602 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
604 /* convert current frame. */
605 flx_colorspace_convert (flxdec->converter,
606 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
608 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
609 flxdec->next_time += flxdec->frame_time;
611 gst_pad_push (flxdec->srcpad, out);
618 avail = gst_adapter_available (flxdec->adapter);
622 gst_object_unref (flxdec);
628 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
629 ("not a flx file (type %x)", flxh->type));
630 gst_object_unref (flxdec);
631 return GST_FLOW_ERROR;
635 static GstStateChangeReturn
636 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
639 GstStateChangeReturn ret;
641 flxdec = GST_FLXDEC (element);
643 switch (transition) {
644 case GST_STATE_CHANGE_NULL_TO_READY:
646 case GST_STATE_CHANGE_READY_TO_PAUSED:
647 gst_adapter_clear (flxdec->adapter);
648 flxdec->state = GST_FLXDEC_READ_HEADER;
650 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
656 ret = parent_class->change_state (element, transition);
658 switch (transition) {
659 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
661 case GST_STATE_CHANGE_PAUSED_TO_READY:
663 gst_buffer_unref (flxdec->frame);
664 flxdec->frame = NULL;
667 gst_buffer_unref (flxdec->delta);
668 flxdec->delta = NULL;
671 case GST_STATE_CHANGE_READY_TO_NULL:
680 plugin_init (GstPlugin * plugin)
682 return gst_element_register (plugin, "flxdec",
683 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
686 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
689 "FLC/FLI/FLX video decoder",
690 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)