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
24 * This element decodes fli/flc/flx-video into raw video
28 * http://www.coolutils.com/Formats/FLI
29 * http://woodshole.er.usgs.gov/operations/modeling/flc.html
38 #include "gstflxdec.h"
39 #include <gst/video/video.h>
41 #define JIFFIE (GST_SECOND/70)
43 GST_DEBUG_CATEGORY_STATIC (flxdec_debug);
44 #define GST_CAT_DEFAULT flxdec_debug
46 /* flx element information */
47 static const GstElementDetails flxdec_details =
48 GST_ELEMENT_DETAILS ("FLX audio decoder",
49 "Codec/Decoder/Video",
50 "FLC/FLI/FLX video decoder",
51 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>");
54 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS ("video/x-fli")
61 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
68 static void gst_flxdec_class_init (GstFlxDecClass * klass);
69 static void gst_flxdec_base_init (GstFlxDecClass * klass);
70 static void gst_flxdec_init (GstFlxDec * flxdec);
71 static void gst_flxdec_dispose (GstFlxDec * flxdec);
73 static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstBuffer * buf);
75 static GstStateChangeReturn gst_flxdec_change_state (GstElement * element,
76 GstStateChange transition);
78 static gboolean gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query);
79 static gboolean gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event);
80 static gboolean gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event);
82 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
83 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
84 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
85 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
87 #define rndalign(off) ((off) + ((off) & 1))
89 static GstElementClass *parent_class = NULL;
92 gst_flxdec_get_type (void)
94 static GType flxdec_type = 0;
97 static const GTypeInfo flxdec_info = {
98 sizeof (GstFlxDecClass),
99 (GBaseInitFunc) gst_flxdec_base_init,
101 (GClassInitFunc) gst_flxdec_class_init,
106 (GInstanceInitFunc) gst_flxdec_init,
110 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
116 gst_flxdec_base_init (GstFlxDecClass * klass)
118 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
120 gst_element_class_set_details (gstelement_class, &flxdec_details);
121 gst_element_class_add_pad_template (gstelement_class,
122 gst_static_pad_template_get (&sink_factory));
123 gst_element_class_add_pad_template (gstelement_class,
124 gst_static_pad_template_get (&src_video_factory));
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_peek_parent (klass);
138 gobject_class->dispose = (GObjectFinalizeFunc) gst_flxdec_dispose;
140 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
142 gstelement_class->change_state = gst_flxdec_change_state;
146 gst_flxdec_init (GstFlxDec * flxdec)
148 flxdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
149 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
150 gst_pad_set_chain_function (flxdec->sinkpad, gst_flxdec_chain);
151 gst_pad_set_event_function (flxdec->sinkpad, gst_flxdec_sink_event_handler);
153 flxdec->srcpad = gst_pad_new_from_static_template (&src_video_factory, "src");
154 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
155 gst_pad_set_query_function (flxdec->srcpad, gst_flxdec_src_query_handler);
156 gst_pad_set_event_function (flxdec->srcpad, gst_flxdec_src_event_handler);
158 gst_pad_use_fixed_caps (flxdec->srcpad);
160 flxdec->frame = NULL;
161 flxdec->delta = NULL;
163 flxdec->adapter = gst_adapter_new ();
167 gst_flxdec_dispose (GstFlxDec * flxdec)
169 if (flxdec->adapter) {
170 g_object_unref (flxdec->adapter);
171 flxdec->adapter = NULL;
174 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) flxdec);
178 gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query)
180 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
181 gboolean ret = FALSE;
183 switch (GST_QUERY_TYPE (query)) {
184 case GST_QUERY_DURATION:
188 gst_query_parse_duration (query, &format, NULL);
190 if (format != GST_FORMAT_TIME)
193 gst_query_set_duration (query, format, flxdec->duration);
201 gst_object_unref (flxdec);
207 gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event)
209 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
212 /* TODO: implement the seek and other event handling */
214 ret = gst_pad_push_event (flxdec->sinkpad, event);
216 gst_object_unref (flxdec);
222 gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event)
227 flxdec = GST_FLXDEC (gst_pad_get_parent (pad));
229 ret = gst_pad_push_event (flxdec->srcpad, event);
231 gst_object_unref (flxdec);
236 flx_decode_chunks (GstFlxDec * flxdec, gulong count, guchar * data,
241 g_return_if_fail (data != NULL);
244 hdr = (FlxFrameChunk *) data;
245 FLX_FRAME_CHUNK_FIX_ENDIANNESS (hdr);
246 data += FlxFrameChunkSize;
250 flx_decode_color (flxdec, data, dest, 2);
251 data += rndalign (hdr->size) - FlxFrameChunkSize;
255 flx_decode_color (flxdec, data, dest, 0);
256 data += rndalign (hdr->size) - FlxFrameChunkSize;
260 flx_decode_brun (flxdec, data, dest);
261 data += rndalign (hdr->size) - FlxFrameChunkSize;
265 flx_decode_delta_fli (flxdec, data, dest);
266 data += rndalign (hdr->size) - FlxFrameChunkSize;
270 flx_decode_delta_flc (flxdec, data, dest);
271 data += rndalign (hdr->size) - FlxFrameChunkSize;
275 memset (dest, 0, flxdec->size);
279 data += rndalign (hdr->size) - FlxFrameChunkSize;
283 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
285 data += rndalign (hdr->size) - FlxFrameChunkSize;
293 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
295 guint packs, count, indx;
297 g_return_if_fail (flxdec != NULL);
299 packs = (data[0] + (data[1] << 8));
304 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
306 /* color map index + skip count */
309 /* number of rgb triplets */
310 count = *data++ & 0xff;
314 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)", count, indx);
315 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
322 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
324 gulong count, lines, row;
327 g_return_if_fail (flxdec != NULL);
329 lines = flxdec->hdr.height;
332 * should not be used anymore, since the flc format can
333 * contain more then 255 RLE packets. we use the frame
338 row = flxdec->hdr.width;
344 count = 0x100 - count;
363 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
365 gulong count, packets, lines, start_line, start_l;
368 g_return_if_fail (flxdec != NULL);
369 g_return_if_fail (flxdec->delta != NULL);
371 /* use last frame for delta */
372 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
373 GST_BUFFER_SIZE (flxdec->delta));
375 start_line = (data[0] + (data[1] << 8));
376 lines = (data[2] + (data[3] << 8));
379 /* start position of delta */
380 dest += (flxdec->hdr.width * start_line);
397 count = 0x100 - count;
409 start_p += flxdec->hdr.width;
415 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
417 gulong count, lines, start_l, opcode;
420 g_return_if_fail (flxdec != NULL);
421 g_return_if_fail (flxdec->delta != NULL);
423 /* use last frame for delta */
424 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
425 GST_BUFFER_SIZE (flxdec->delta));
427 lines = (data[0] + (data[1] << 8));
434 dest = start_p + (flxdec->hdr.width * (start_l - lines));
436 /* process opcode(s) */
437 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
439 if ((opcode & 0xc000) == 0xc000) {
441 start_l += (0x10000 - opcode);
442 dest += flxdec->hdr.width * (0x10000 - opcode);
445 dest += flxdec->hdr.width;
446 *dest++ = (opcode & 0xff);
451 /* last opcode is the packet count */
460 /* replicate word run */
461 count = 0x100 - count;
468 /* literal word run */
480 gst_flxdec_chain (GstPad * pad, GstBuffer * buf)
484 GstFlowReturn res = GST_FLOW_OK;
489 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
490 flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
491 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
493 gst_adapter_push (flxdec->adapter, buf);
494 avail = gst_adapter_available (flxdec->adapter);
496 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
497 if (avail >= FlxHeaderSize) {
498 const guint8 *data = gst_adapter_peek (flxdec->adapter, FlxHeaderSize);
500 memcpy ((gchar *) & flxdec->hdr, data, FlxHeaderSize);
501 FLX_HDR_FIX_ENDIANNESS (&(flxdec->hdr));
502 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
507 if (flxh->type != FLX_MAGICHDR_FLI &&
508 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX)
511 GST_LOG ("size : %d", flxh->size);
512 GST_LOG ("frames : %d", flxh->frames);
513 GST_LOG ("width : %d", flxh->width);
514 GST_LOG ("height : %d", flxh->height);
515 GST_LOG ("depth : %d", flxh->depth);
516 GST_LOG ("speed : %d", flxh->speed);
518 flxdec->next_time = 0;
520 if (flxh->type == FLX_MAGICHDR_FLI) {
521 flxdec->frame_time = JIFFIE * flxh->speed;
522 } else if (flxh->speed == 0) {
523 flxdec->frame_time = GST_SECOND / 70;
525 flxdec->frame_time = flxh->speed * GST_MSECOND;
528 flxdec->duration = flxh->frames * flxdec->frame_time;
529 GST_LOG ("duration : %" GST_TIME_FORMAT,
530 GST_TIME_ARGS (flxdec->duration));
532 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
533 gst_caps_set_simple (caps,
534 "width", G_TYPE_INT, flxh->width,
535 "height", G_TYPE_INT, flxh->height,
536 "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND,
537 (gint) flxdec->frame_time / 1000, NULL);
539 gst_pad_set_caps (flxdec->srcpad, caps);
540 gst_caps_unref (caps);
542 if (flxh->depth <= 8)
544 flx_colorspace_converter_new (flxh->width, flxh->height);
546 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
547 GST_LOG ("(FLC) aspect_dx : %d", flxh->aspect_dx);
548 GST_LOG ("(FLC) aspect_dy : %d", flxh->aspect_dy);
549 GST_LOG ("(FLC) oframe1 : 0x%08x", flxh->oframe1);
550 GST_LOG ("(FLC) oframe2 : 0x%08x", flxh->oframe2);
553 flxdec->size = (flxh->width * flxh->height);
555 /* create delta and output frame */
556 flxdec->frame = gst_buffer_new ();
557 flxdec->delta = gst_buffer_new ();
558 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
559 GST_BUFFER_MALLOCDATA (flxdec->frame) = GST_BUFFER_DATA (flxdec->frame);
560 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
561 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
562 GST_BUFFER_MALLOCDATA (flxdec->delta) = GST_BUFFER_DATA (flxdec->delta);
563 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
565 flxdec->state = GST_FLXDEC_PLAYING;
567 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
570 /* while we have enough data in the adapter */
571 while (avail >= FlxFrameChunkSize) {
577 data = gst_adapter_peek (flxdec->adapter, FlxFrameChunkSize);
578 memcpy (&flxfh, data, FlxFrameChunkSize);
579 FLX_FRAME_CHUNK_FIX_ENDIANNESS (&flxfh);
583 /* check if we have the complete frame */
584 if (avail < flxfh.size)
588 gst_adapter_flush (flxdec->adapter, FlxFrameChunkSize);
590 chunk = gst_adapter_take (flxdec->adapter,
591 flxfh.size - FlxFrameChunkSize);
592 FLX_FRAME_TYPE_FIX_ENDIANNESS ((FlxFrameType *) chunk);
593 if (((FlxFrameType *) chunk)->chunks == 0)
596 /* create 32 bits output frame */
597 res = gst_pad_alloc_buffer_and_set_caps (flxdec->srcpad,
598 GST_BUFFER_OFFSET_NONE,
599 flxdec->size * 4, GST_PAD_CAPS (flxdec->srcpad), &out);
600 if (res != GST_FLOW_OK)
604 flx_decode_chunks (flxdec,
605 ((FlxFrameType *) chunk)->chunks,
606 chunk + FlxFrameTypeSize, GST_BUFFER_DATA (flxdec->frame));
608 /* save copy of the current frame for possible delta. */
609 memcpy (GST_BUFFER_DATA (flxdec->delta),
610 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
612 /* convert current frame. */
613 flx_colorspace_convert (flxdec->converter,
614 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
616 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
617 flxdec->next_time += flxdec->frame_time;
619 gst_pad_push (flxdec->srcpad, out);
626 avail = gst_adapter_available (flxdec->adapter);
630 gst_object_unref (flxdec);
636 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
637 ("not a flx file (type %x)", flxh->type));
638 gst_object_unref (flxdec);
639 return GST_FLOW_ERROR;
643 static GstStateChangeReturn
644 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
647 GstStateChangeReturn ret;
649 flxdec = GST_FLXDEC (element);
651 switch (transition) {
652 case GST_STATE_CHANGE_NULL_TO_READY:
654 case GST_STATE_CHANGE_READY_TO_PAUSED:
655 gst_adapter_clear (flxdec->adapter);
656 flxdec->state = GST_FLXDEC_READ_HEADER;
658 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
664 ret = parent_class->change_state (element, transition);
666 switch (transition) {
667 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
669 case GST_STATE_CHANGE_PAUSED_TO_READY:
671 gst_buffer_unref (flxdec->frame);
672 flxdec->frame = NULL;
675 gst_buffer_unref (flxdec->delta);
676 flxdec->delta = NULL;
679 case GST_STATE_CHANGE_READY_TO_NULL:
688 plugin_init (GstPlugin * plugin)
690 return gst_element_register (plugin, "flxdec",
691 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
694 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
697 "FLC/FLI/FLX video decoder",
698 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)