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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 #if G_BYTE_ORDER == G_BIG_ENDIAN
55 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("xRGB"))
57 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRx"))
61 static void gst_flxdec_dispose (GstFlxDec * flxdec);
63 static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstObject * parent,
65 static gboolean gst_flxdec_sink_event_handler (GstPad * pad,
66 GstObject * parent, GstEvent * event);
68 static GstStateChangeReturn gst_flxdec_change_state (GstElement * element,
69 GstStateChange transition);
71 static gboolean gst_flxdec_src_query_handler (GstPad * pad, GstObject * parent,
74 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
75 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
76 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
77 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
79 #define rndalign(off) ((off) + ((off) & 1))
81 #define gst_flxdec_parent_class parent_class
82 G_DEFINE_TYPE (GstFlxDec, gst_flxdec, GST_TYPE_ELEMENT);
85 gst_flxdec_class_init (GstFlxDecClass * klass)
87 GObjectClass *gobject_class;
88 GstElementClass *gstelement_class;
90 gobject_class = (GObjectClass *) klass;
91 gstelement_class = (GstElementClass *) klass;
93 parent_class = g_type_class_peek_parent (klass);
95 gobject_class->dispose = (GObjectFinalizeFunc) gst_flxdec_dispose;
97 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
99 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flxdec_change_state);
101 gst_element_class_set_static_metadata (gstelement_class, "FLX video decoder",
102 "Codec/Decoder/Video",
103 "FLC/FLI/FLX video decoder",
104 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>");
105 gst_element_class_add_pad_template (gstelement_class,
106 gst_static_pad_template_get (&sink_factory));
107 gst_element_class_add_pad_template (gstelement_class,
108 gst_static_pad_template_get (&src_video_factory));
112 gst_flxdec_init (GstFlxDec * flxdec)
114 flxdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
115 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
116 gst_pad_set_chain_function (flxdec->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_flxdec_chain));
118 gst_pad_set_event_function (flxdec->sinkpad,
119 GST_DEBUG_FUNCPTR (gst_flxdec_sink_event_handler));
121 flxdec->srcpad = gst_pad_new_from_static_template (&src_video_factory, "src");
122 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
123 gst_pad_set_query_function (flxdec->srcpad,
124 GST_DEBUG_FUNCPTR (gst_flxdec_src_query_handler));
126 gst_pad_use_fixed_caps (flxdec->srcpad);
128 flxdec->adapter = gst_adapter_new ();
132 gst_flxdec_dispose (GstFlxDec * flxdec)
134 if (flxdec->adapter) {
135 g_object_unref (flxdec->adapter);
136 flxdec->adapter = NULL;
139 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) flxdec);
143 gst_flxdec_src_query_handler (GstPad * pad, GstObject * parent,
146 GstFlxDec *flxdec = (GstFlxDec *) parent;
147 gboolean ret = FALSE;
149 switch (GST_QUERY_TYPE (query)) {
150 case GST_QUERY_DURATION:
154 gst_query_parse_duration (query, &format, NULL);
156 if (format != GST_FORMAT_TIME)
159 gst_query_set_duration (query, format, flxdec->duration);
168 ret = gst_pad_query_default (pad, parent, query);
174 gst_flxdec_sink_event_handler (GstPad * pad, GstObject * parent,
180 flxdec = GST_FLXDEC (parent);
182 switch (GST_EVENT_TYPE (event)) {
183 case GST_EVENT_SEGMENT:
187 gst_event_copy_segment (event, &segment);
188 if (segment.format != GST_FORMAT_TIME) {
189 GST_DEBUG_OBJECT (flxdec, "generating TIME segment");
190 gst_segment_init (&segment, GST_FORMAT_TIME);
191 gst_event_unref (event);
192 event = gst_event_new_segment (&segment);
197 ret = gst_pad_event_default (pad, parent, event);
205 flx_decode_chunks (GstFlxDec * flxdec, gulong count, guchar * data,
210 g_return_if_fail (data != NULL);
213 hdr = (FlxFrameChunk *) data;
214 FLX_FRAME_CHUNK_FIX_ENDIANNESS (hdr);
215 data += FlxFrameChunkSize;
219 flx_decode_color (flxdec, data, dest, 2);
220 data += rndalign (hdr->size) - FlxFrameChunkSize;
224 flx_decode_color (flxdec, data, dest, 0);
225 data += rndalign (hdr->size) - FlxFrameChunkSize;
229 flx_decode_brun (flxdec, data, dest);
230 data += rndalign (hdr->size) - FlxFrameChunkSize;
234 flx_decode_delta_fli (flxdec, data, dest);
235 data += rndalign (hdr->size) - FlxFrameChunkSize;
239 flx_decode_delta_flc (flxdec, data, dest);
240 data += rndalign (hdr->size) - FlxFrameChunkSize;
244 memset (dest, 0, flxdec->size);
248 data += rndalign (hdr->size) - FlxFrameChunkSize;
252 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
254 data += rndalign (hdr->size) - FlxFrameChunkSize;
262 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
264 guint packs, count, indx;
266 g_return_if_fail (flxdec != NULL);
268 packs = (data[0] + (data[1] << 8));
273 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
275 /* color map index + skip count */
278 /* number of rgb triplets */
279 count = *data++ & 0xff;
283 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)", count, indx);
284 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
291 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
293 gulong count, lines, row;
296 g_return_if_fail (flxdec != NULL);
298 lines = flxdec->hdr.height;
301 * should not be used anymore, since the flc format can
302 * contain more then 255 RLE packets. we use the frame
307 row = flxdec->hdr.width;
313 count = 0x100 - count;
332 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
334 gulong count, packets, lines, start_line;
337 g_return_if_fail (flxdec != NULL);
338 g_return_if_fail (flxdec->delta_data != NULL);
340 /* use last frame for delta */
341 memcpy (dest, flxdec->delta_data, flxdec->size);
343 start_line = (data[0] + (data[1] << 8));
344 lines = (data[2] + (data[3] << 8));
347 /* start position of delta */
348 dest += (flxdec->hdr.width * start_line);
364 count = 0x100 - count;
376 start_p += flxdec->hdr.width;
382 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
384 gulong count, lines, start_l, opcode;
387 g_return_if_fail (flxdec != NULL);
388 g_return_if_fail (flxdec->delta_data != NULL);
390 /* use last frame for delta */
391 memcpy (dest, flxdec->delta_data, flxdec->size);
393 lines = (data[0] + (data[1] << 8));
400 dest = start_p + (flxdec->hdr.width * (start_l - lines));
402 /* process opcode(s) */
403 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
405 if ((opcode & 0xc000) == 0xc000) {
407 start_l += (0x10000 - opcode);
408 dest += flxdec->hdr.width * (0x10000 - opcode);
411 dest += flxdec->hdr.width;
412 *dest++ = (opcode & 0xff);
417 /* last opcode is the packet count */
426 /* replicate word run */
427 count = 0x100 - count;
434 /* literal word run */
446 gst_flxdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
450 GstFlowReturn res = GST_FLOW_OK;
455 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
456 flxdec = (GstFlxDec *) parent;
457 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
459 gst_adapter_push (flxdec->adapter, buf);
460 avail = gst_adapter_available (flxdec->adapter);
462 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
463 if (avail >= FlxHeaderSize) {
464 const guint8 *data = gst_adapter_map (flxdec->adapter, FlxHeaderSize);
467 memcpy ((gchar *) & flxdec->hdr, data, FlxHeaderSize);
468 FLX_HDR_FIX_ENDIANNESS (&(flxdec->hdr));
469 gst_adapter_unmap (flxdec->adapter);
470 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
475 if (flxh->type != FLX_MAGICHDR_FLI &&
476 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX)
479 GST_LOG ("size : %d", flxh->size);
480 GST_LOG ("frames : %d", flxh->frames);
481 GST_LOG ("width : %d", flxh->width);
482 GST_LOG ("height : %d", flxh->height);
483 GST_LOG ("depth : %d", flxh->depth);
484 GST_LOG ("speed : %d", flxh->speed);
486 flxdec->next_time = 0;
488 if (flxh->type == FLX_MAGICHDR_FLI) {
489 flxdec->frame_time = JIFFIE * flxh->speed;
490 } else if (flxh->speed == 0) {
491 flxdec->frame_time = GST_SECOND / 70;
493 flxdec->frame_time = flxh->speed * GST_MSECOND;
496 flxdec->duration = flxh->frames * flxdec->frame_time;
497 GST_LOG ("duration : %" GST_TIME_FORMAT,
498 GST_TIME_ARGS (flxdec->duration));
500 templ = gst_pad_get_pad_template_caps (flxdec->srcpad);
501 caps = gst_caps_copy (templ);
502 gst_caps_unref (templ);
503 gst_caps_set_simple (caps,
504 "width", G_TYPE_INT, flxh->width,
505 "height", G_TYPE_INT, flxh->height,
506 "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND,
507 (gint) flxdec->frame_time / 1000, NULL);
509 gst_pad_set_caps (flxdec->srcpad, caps);
510 gst_caps_unref (caps);
512 if (flxh->depth <= 8)
514 flx_colorspace_converter_new (flxh->width, flxh->height);
516 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
517 GST_LOG ("(FLC) aspect_dx : %d", flxh->aspect_dx);
518 GST_LOG ("(FLC) aspect_dy : %d", flxh->aspect_dy);
519 GST_LOG ("(FLC) oframe1 : 0x%08x", flxh->oframe1);
520 GST_LOG ("(FLC) oframe2 : 0x%08x", flxh->oframe2);
523 flxdec->size = ((guint) flxh->width * (guint) flxh->height);
525 /* create delta and output frame */
526 flxdec->frame_data = g_malloc (flxdec->size);
527 flxdec->delta_data = g_malloc (flxdec->size);
529 flxdec->state = GST_FLXDEC_PLAYING;
531 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
534 /* while we have enough data in the adapter */
535 while (avail >= FlxFrameChunkSize && res == GST_FLOW_OK) {
542 data = gst_adapter_map (flxdec->adapter, FlxFrameChunkSize);
543 memcpy (&flxfh, data, FlxFrameChunkSize);
544 FLX_FRAME_CHUNK_FIX_ENDIANNESS (&flxfh);
545 gst_adapter_unmap (flxdec->adapter);
549 /* check if we have the complete frame */
550 if (avail < flxfh.size)
554 gst_adapter_flush (flxdec->adapter, FlxFrameChunkSize);
556 chunk = gst_adapter_take (flxdec->adapter,
557 flxfh.size - FlxFrameChunkSize);
558 FLX_FRAME_TYPE_FIX_ENDIANNESS ((FlxFrameType *) chunk);
559 if (((FlxFrameType *) chunk)->chunks == 0)
562 /* create 32 bits output frame */
563 // res = gst_pad_alloc_buffer_and_set_caps (flxdec->srcpad,
564 // GST_BUFFER_OFFSET_NONE,
565 // flxdec->size * 4, GST_PAD_CAPS (flxdec->srcpad), &out);
566 // if (res != GST_FLOW_OK)
569 out = gst_buffer_new_and_alloc (flxdec->size * 4);
572 flx_decode_chunks (flxdec,
573 ((FlxFrameType *) chunk)->chunks,
574 chunk + FlxFrameTypeSize, flxdec->frame_data);
576 /* save copy of the current frame for possible delta. */
577 memcpy (flxdec->delta_data, flxdec->frame_data, flxdec->size);
579 gst_buffer_map (out, &map, GST_MAP_WRITE);
580 /* convert current frame. */
581 flx_colorspace_convert (flxdec->converter, flxdec->frame_data,
583 gst_buffer_unmap (out, &map);
585 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
586 flxdec->next_time += flxdec->frame_time;
588 res = gst_pad_push (flxdec->srcpad, out);
591 /* check if we have the complete frame */
592 if (avail < flxfh.size)
595 gst_adapter_flush (flxdec->adapter, flxfh.size);
602 avail = gst_adapter_available (flxdec->adapter);
611 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
612 ("not a flx file (type %x)", flxh->type));
613 gst_object_unref (flxdec);
614 return GST_FLOW_ERROR;
618 static GstStateChangeReturn
619 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
622 GstStateChangeReturn ret;
624 flxdec = GST_FLXDEC (element);
626 switch (transition) {
627 case GST_STATE_CHANGE_NULL_TO_READY:
629 case GST_STATE_CHANGE_READY_TO_PAUSED:
630 gst_adapter_clear (flxdec->adapter);
631 flxdec->state = GST_FLXDEC_READ_HEADER;
633 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
639 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
641 switch (transition) {
642 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
644 case GST_STATE_CHANGE_PAUSED_TO_READY:
645 if (flxdec->frame_data) {
646 g_free (flxdec->frame_data);
647 flxdec->frame_data = NULL;
649 if (flxdec->delta_data) {
650 g_free (flxdec->delta_data);
651 flxdec->delta_data = NULL;
653 if (flxdec->converter) {
654 flx_colorspace_converter_destroy (flxdec->converter);
655 flxdec->converter = NULL;
658 case GST_STATE_CHANGE_READY_TO_NULL:
667 plugin_init (GstPlugin * plugin)
669 return gst_element_register (plugin, "flxdec",
670 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
673 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
676 "FLC/FLI/FLX video decoder",
677 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)