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
43 /* flx element information */
44 static const GstElementDetails flxdec_details =
45 GST_ELEMENT_DETAILS ("FLX audio decoder",
46 "Codec/Decoder/Video",
47 "FLC/FLI/FLX video decoder",
48 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>");
51 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("video/x-fli")
58 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
65 static void gst_flxdec_class_init (GstFlxDecClass * klass);
66 static void gst_flxdec_base_init (GstFlxDecClass * klass);
67 static void gst_flxdec_init (GstFlxDec * flxdec);
68 static void gst_flxdec_dispose (GstFlxDec * flxdec);
70 static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstBuffer * buf);
72 static GstStateChangeReturn gst_flxdec_change_state (GstElement * element,
73 GstStateChange transition);
75 static gboolean gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query);
76 static gboolean gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event);
77 static gboolean gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event);
79 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
80 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
81 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
82 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
84 #define rndalign(off) ((off) + ((off) & 1))
86 static GstElementClass *parent_class = NULL;
89 gst_flxdec_get_type (void)
91 static GType flxdec_type = 0;
94 static const GTypeInfo flxdec_info = {
95 sizeof (GstFlxDecClass),
96 (GBaseInitFunc) gst_flxdec_base_init,
98 (GClassInitFunc) gst_flxdec_class_init,
103 (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_peek_parent (klass);
135 gobject_class->dispose = (GObjectFinalizeFunc) gst_flxdec_dispose;
137 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
139 gstelement_class->change_state = gst_flxdec_change_state;
143 gst_flxdec_init (GstFlxDec * flxdec)
145 flxdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
146 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
147 gst_pad_set_chain_function (flxdec->sinkpad, gst_flxdec_chain);
148 gst_pad_set_event_function (flxdec->sinkpad, gst_flxdec_sink_event_handler);
150 flxdec->srcpad = gst_pad_new_from_static_template (&src_video_factory, "src");
151 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
152 gst_pad_set_query_function (flxdec->srcpad, gst_flxdec_src_query_handler);
153 gst_pad_set_event_function (flxdec->srcpad, gst_flxdec_src_event_handler);
155 gst_pad_use_fixed_caps (flxdec->srcpad);
157 flxdec->frame = NULL;
158 flxdec->delta = NULL;
160 flxdec->adapter = gst_adapter_new ();
164 gst_flxdec_dispose (GstFlxDec * flxdec)
166 if (flxdec->adapter) {
167 g_object_unref (flxdec->adapter);
168 flxdec->adapter = NULL;
171 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) flxdec);
175 gst_flxdec_src_query_handler (GstPad * pad, GstQuery * query)
177 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
178 gboolean ret = FALSE;
180 switch (GST_QUERY_TYPE (query)) {
181 case GST_QUERY_DURATION:
185 gst_query_parse_duration (query, &format, NULL);
187 if (format != GST_FORMAT_TIME)
190 gst_query_set_duration (query, format, flxdec->duration);
198 gst_object_unref (flxdec);
204 gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event)
206 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
209 /* TODO: implement the seek and other event handling */
211 ret = gst_pad_push_event (flxdec->sinkpad, event);
213 gst_object_unref (flxdec);
219 gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event)
224 flxdec = GST_FLXDEC (gst_pad_get_parent (pad));
226 ret = gst_pad_push_event (flxdec->srcpad, event);
228 gst_object_unref (flxdec);
233 flx_decode_chunks (GstFlxDec * flxdec, gulong count, guchar * data,
238 g_return_if_fail (data != NULL);
241 hdr = (FlxFrameChunk *) data;
242 FLX_FRAME_CHUNK_FIX_ENDIANNESS (hdr);
243 data += FlxFrameChunkSize;
247 flx_decode_color (flxdec, data, dest, 2);
248 data += rndalign (hdr->size) - FlxFrameChunkSize;
252 flx_decode_color (flxdec, data, dest, 0);
253 data += rndalign (hdr->size) - FlxFrameChunkSize;
257 flx_decode_brun (flxdec, data, dest);
258 data += rndalign (hdr->size) - FlxFrameChunkSize;
262 flx_decode_delta_fli (flxdec, data, dest);
263 data += rndalign (hdr->size) - FlxFrameChunkSize;
267 flx_decode_delta_flc (flxdec, data, dest);
268 data += rndalign (hdr->size) - FlxFrameChunkSize;
272 memset (dest, 0, flxdec->size);
276 data += rndalign (hdr->size) - FlxFrameChunkSize;
280 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
282 data += rndalign (hdr->size) - FlxFrameChunkSize;
290 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
292 guint packs, count, indx;
294 g_return_if_fail (flxdec != NULL);
296 packs = (data[0] + (data[1] << 8));
301 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
303 /* color map index + skip count */
306 /* number of rgb triplets */
307 count = *data++ & 0xff;
311 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)", count, indx);
312 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
319 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
321 gulong count, lines, row;
324 g_return_if_fail (flxdec != NULL);
326 lines = flxdec->hdr.height;
329 * should not be used anymore, since the flc format can
330 * contain more then 255 RLE packets. we use the frame
335 row = flxdec->hdr.width;
341 count = 0x100 - count;
360 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
362 gulong count, packets, lines, start_line;
365 g_return_if_fail (flxdec != NULL);
366 g_return_if_fail (flxdec->delta != NULL);
368 /* use last frame for delta */
369 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
370 GST_BUFFER_SIZE (flxdec->delta));
372 start_line = (data[0] + (data[1] << 8));
373 lines = (data[2] + (data[3] << 8));
376 /* start position of delta */
377 dest += (flxdec->hdr.width * start_line);
393 count = 0x100 - count;
405 start_p += flxdec->hdr.width;
411 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
413 gulong count, lines, start_l, opcode;
416 g_return_if_fail (flxdec != NULL);
417 g_return_if_fail (flxdec->delta != NULL);
419 /* use last frame for delta */
420 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
421 GST_BUFFER_SIZE (flxdec->delta));
423 lines = (data[0] + (data[1] << 8));
430 dest = start_p + (flxdec->hdr.width * (start_l - lines));
432 /* process opcode(s) */
433 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
435 if ((opcode & 0xc000) == 0xc000) {
437 start_l += (0x10000 - opcode);
438 dest += flxdec->hdr.width * (0x10000 - opcode);
441 dest += flxdec->hdr.width;
442 *dest++ = (opcode & 0xff);
447 /* last opcode is the packet count */
456 /* replicate word run */
457 count = 0x100 - count;
464 /* literal word run */
476 gst_flxdec_chain (GstPad * pad, GstBuffer * buf)
480 GstFlowReturn res = GST_FLOW_OK;
485 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
486 flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
487 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
489 gst_adapter_push (flxdec->adapter, buf);
490 avail = gst_adapter_available (flxdec->adapter);
492 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
493 if (avail >= FlxHeaderSize) {
494 const guint8 *data = gst_adapter_peek (flxdec->adapter, FlxHeaderSize);
496 memcpy ((gchar *) & flxdec->hdr, data, FlxHeaderSize);
497 FLX_HDR_FIX_ENDIANNESS (&(flxdec->hdr));
498 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
503 if (flxh->type != FLX_MAGICHDR_FLI &&
504 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX)
507 GST_LOG ("size : %d", flxh->size);
508 GST_LOG ("frames : %d", flxh->frames);
509 GST_LOG ("width : %d", flxh->width);
510 GST_LOG ("height : %d", flxh->height);
511 GST_LOG ("depth : %d", flxh->depth);
512 GST_LOG ("speed : %d", flxh->speed);
514 flxdec->next_time = 0;
516 if (flxh->type == FLX_MAGICHDR_FLI) {
517 flxdec->frame_time = JIFFIE * flxh->speed;
518 } else if (flxh->speed == 0) {
519 flxdec->frame_time = GST_SECOND / 70;
521 flxdec->frame_time = flxh->speed * GST_MSECOND;
524 flxdec->duration = flxh->frames * flxdec->frame_time;
525 GST_LOG ("duration : %" GST_TIME_FORMAT,
526 GST_TIME_ARGS (flxdec->duration));
528 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
529 gst_caps_set_simple (caps,
530 "width", G_TYPE_INT, flxh->width,
531 "height", G_TYPE_INT, flxh->height,
532 "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND,
533 (gint) flxdec->frame_time / 1000, NULL);
535 gst_pad_set_caps (flxdec->srcpad, caps);
536 gst_caps_unref (caps);
538 if (flxh->depth <= 8)
540 flx_colorspace_converter_new (flxh->width, flxh->height);
542 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
543 GST_LOG ("(FLC) aspect_dx : %d", flxh->aspect_dx);
544 GST_LOG ("(FLC) aspect_dy : %d", flxh->aspect_dy);
545 GST_LOG ("(FLC) oframe1 : 0x%08x", flxh->oframe1);
546 GST_LOG ("(FLC) oframe2 : 0x%08x", flxh->oframe2);
549 flxdec->size = (flxh->width * flxh->height);
551 /* create delta and output frame */
552 flxdec->frame = gst_buffer_new ();
553 flxdec->delta = gst_buffer_new ();
554 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
555 GST_BUFFER_MALLOCDATA (flxdec->frame) = GST_BUFFER_DATA (flxdec->frame);
556 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
557 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
558 GST_BUFFER_MALLOCDATA (flxdec->delta) = GST_BUFFER_DATA (flxdec->delta);
559 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
561 flxdec->state = GST_FLXDEC_PLAYING;
563 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
566 /* while we have enough data in the adapter */
567 while (avail >= FlxFrameChunkSize) {
573 data = gst_adapter_peek (flxdec->adapter, FlxFrameChunkSize);
574 memcpy (&flxfh, data, FlxFrameChunkSize);
575 FLX_FRAME_CHUNK_FIX_ENDIANNESS (&flxfh);
579 /* check if we have the complete frame */
580 if (avail < flxfh.size)
584 gst_adapter_flush (flxdec->adapter, FlxFrameChunkSize);
586 chunk = gst_adapter_take (flxdec->adapter,
587 flxfh.size - FlxFrameChunkSize);
588 FLX_FRAME_TYPE_FIX_ENDIANNESS ((FlxFrameType *) chunk);
589 if (((FlxFrameType *) chunk)->chunks == 0)
592 /* create 32 bits output frame */
593 res = gst_pad_alloc_buffer_and_set_caps (flxdec->srcpad,
594 GST_BUFFER_OFFSET_NONE,
595 flxdec->size * 4, GST_PAD_CAPS (flxdec->srcpad), &out);
596 if (res != GST_FLOW_OK)
600 flx_decode_chunks (flxdec,
601 ((FlxFrameType *) chunk)->chunks,
602 chunk + FlxFrameTypeSize, GST_BUFFER_DATA (flxdec->frame));
604 /* save copy of the current frame for possible delta. */
605 memcpy (GST_BUFFER_DATA (flxdec->delta),
606 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
608 /* convert current frame. */
609 flx_colorspace_convert (flxdec->converter,
610 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
612 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
613 flxdec->next_time += flxdec->frame_time;
615 gst_pad_push (flxdec->srcpad, out);
622 avail = gst_adapter_available (flxdec->adapter);
626 gst_object_unref (flxdec);
632 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
633 ("not a flx file (type %x)", flxh->type));
634 gst_object_unref (flxdec);
635 return GST_FLOW_ERROR;
639 static GstStateChangeReturn
640 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
643 GstStateChangeReturn ret;
645 flxdec = GST_FLXDEC (element);
647 switch (transition) {
648 case GST_STATE_CHANGE_NULL_TO_READY:
650 case GST_STATE_CHANGE_READY_TO_PAUSED:
651 gst_adapter_clear (flxdec->adapter);
652 flxdec->state = GST_FLXDEC_READ_HEADER;
654 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
660 ret = parent_class->change_state (element, transition);
662 switch (transition) {
663 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
665 case GST_STATE_CHANGE_PAUSED_TO_READY:
667 gst_buffer_unref (flxdec->frame);
668 flxdec->frame = NULL;
671 gst_buffer_unref (flxdec->delta);
672 flxdec->delta = NULL;
675 case GST_STATE_CHANGE_READY_TO_NULL:
684 plugin_init (GstPlugin * plugin)
686 return gst_element_register (plugin, "flxdec",
687 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
690 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
693 "FLC/FLI/FLX video decoder",
694 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)