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 GST_DEBUG_CATEGORY_STATIC (flxdec_debug);
32 #define GST_CAT_DEFAULT flxdec_debug
34 /* flx element information */
35 static GstElementDetails flxdec_details = {
37 "Codec/Decoder/Audio",
39 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>"
42 /* Flx signals and args */
55 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("video/x-fli")
62 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
69 static void gst_flxdec_class_init (GstFlxDecClass * klass);
70 static void gst_flxdec_base_init (GstFlxDecClass * klass);
71 static void gst_flxdec_init (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_event_handler (GstPad * pad, GstEvent * event);
79 static gboolean gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event);
81 static void gst_flxdec_set_property (GObject * object, guint prop_id,
82 const GValue * value, GParamSpec * pspec);
83 static void gst_flxdec_get_property (GObject * object, guint prop_id,
84 GValue * value, GParamSpec * pspec);
87 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
88 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
89 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
90 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
92 #define rndalign(off) ((off) + ((off) % 2))
94 static GstElementClass *parent_class = NULL;
97 gst_flxdec_get_type (void)
99 static GType flxdec_type = 0;
102 static const GTypeInfo flxdec_info = {
103 sizeof (GstFlxDecClass),
104 (GBaseInitFunc) gst_flxdec_base_init,
106 (GClassInitFunc) gst_flxdec_class_init,
111 (GInstanceInitFunc) gst_flxdec_init,
115 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
121 gst_flxdec_base_init (GstFlxDecClass * klass)
123 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
125 gst_element_class_set_details (gstelement_class, &flxdec_details);
126 gst_element_class_add_pad_template (gstelement_class,
127 gst_static_pad_template_get (&sink_factory));
128 gst_element_class_add_pad_template (gstelement_class,
129 gst_static_pad_template_get (&src_video_factory));
133 gst_flxdec_class_init (GstFlxDecClass * klass)
135 GObjectClass *gobject_class;
136 GstElementClass *gstelement_class;
138 gobject_class = (GObjectClass *) klass;
139 gstelement_class = (GstElementClass *) klass;
141 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
143 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
145 gobject_class->set_property = gst_flxdec_set_property;
146 gobject_class->get_property = gst_flxdec_get_property;
148 gstelement_class->change_state = gst_flxdec_change_state;
152 gst_flxdec_init (GstFlxDec * flxdec)
155 gst_pad_new_from_template (gst_static_pad_template_get (&sink_factory),
157 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
158 gst_pad_set_chain_function (flxdec->sinkpad, gst_flxdec_chain);
159 gst_pad_set_event_function (flxdec->sinkpad, gst_flxdec_sink_event_handler);
162 gst_pad_new_from_template (gst_static_pad_template_get
163 (&src_video_factory), "src");
164 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
165 gst_pad_set_event_function (flxdec->srcpad, gst_flxdec_src_event_handler);
167 gst_pad_use_fixed_caps (flxdec->srcpad);
169 flxdec->frame = NULL;
170 flxdec->delta = NULL;
172 flxdec->adapter = gst_adapter_new ();
176 gst_flxdec_src_event_handler (GstPad * pad, GstEvent * event)
178 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
180 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
182 /* TODO: implement the seek and other event handling */
184 return gst_pad_push_event (flxdec->sinkpad, event);
188 gst_flxdec_sink_event_handler (GstPad * pad, GstEvent * event)
190 GstFlxDec *flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
192 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
194 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS ||
195 GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
196 GST_STREAM_LOCK (flxdec->srcpad);
198 gst_pad_push_event (flxdec->srcpad, gst_event_ref (event));
200 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS ||
201 GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT)
202 GST_STREAM_UNLOCK (flxdec->srcpad);
208 flx_decode_chunks (GstFlxDec * flxdec, gulong count, guchar * data,
213 g_return_if_fail (data != NULL);
216 hdr = (FlxFrameChunk *) data;
217 data += FlxFrameChunkSize;
221 flx_decode_color (flxdec, data, dest, 2);
222 data += rndalign (hdr->size) - FlxFrameChunkSize;
226 flx_decode_color (flxdec, data, dest, 0);
227 data += rndalign (hdr->size) - FlxFrameChunkSize;
231 flx_decode_brun (flxdec, data, dest);
232 data += rndalign (hdr->size) - FlxFrameChunkSize;
236 flx_decode_delta_fli (flxdec, data, dest);
237 data += rndalign (hdr->size) - FlxFrameChunkSize;
241 flx_decode_delta_flc (flxdec, data, dest);
242 data += rndalign (hdr->size) - FlxFrameChunkSize;
246 memset (dest, 0, flxdec->size);
250 data += rndalign (hdr->size) - FlxFrameChunkSize;
254 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
256 data += rndalign (hdr->size) - FlxFrameChunkSize;
264 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
266 guint packs, count, indx;
268 g_return_if_fail (flxdec != NULL);
270 packs = (data[0] + (data[1] << 8));
275 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
277 /* color map index + skip count */
280 /* number of rgb triplets */
281 count = *data++ & 0xff;
285 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
286 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
293 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
295 gulong count, lines, row;
298 g_return_if_fail (flxdec != NULL);
300 lines = flxdec->hdr.height;
303 * should not be used anymore, since the flc format can
304 * contain more then 255 RLE packets. we use the frame
309 row = flxdec->hdr.width;
315 count = 0x100 - count;
334 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
336 gulong count, packets, lines, start_line, start_l;
339 g_return_if_fail (flxdec != NULL);
340 g_return_if_fail (flxdec->delta != NULL);
342 /* use last frame for delta */
343 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
344 GST_BUFFER_SIZE (flxdec->delta));
346 start_line = (data[0] + (data[1] << 8));
347 lines = (data[2] + (data[3] << 8));
350 /* start position of delta */
351 dest += (flxdec->hdr.width * start_line);
368 count = 0x100 - count;
380 start_p += flxdec->hdr.width;
386 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
388 gulong count, lines, start_l, opcode;
391 g_return_if_fail (flxdec != NULL);
392 g_return_if_fail (flxdec->delta != NULL);
394 /* use last frame for delta */
395 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
396 GST_BUFFER_SIZE (flxdec->delta));
398 lines = (data[0] + (data[1] << 8));
405 dest = start_p + (flxdec->hdr.width * (start_l - lines));
407 /* process opcode(s) */
408 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
410 if ((opcode & 0xc000) == 0xc000) {
412 start_l += (0x10000 - opcode);
413 dest += flxdec->hdr.width * (0x10000 - opcode);
416 dest += flxdec->hdr.width;
417 *dest++ = (opcode & 0xff);
422 /* last opcode is the packet count */
431 /* replicate word run */
432 count = 0x100 - count;
439 /* literal word run */
451 gst_flxdec_chain (GstPad * pad, GstBuffer * buf)
455 GstFlowReturn res = GST_FLOW_OK;
460 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
461 flxdec = (GstFlxDec *) gst_pad_get_parent (pad);
462 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
464 gst_adapter_push (flxdec->adapter, buf);
465 avail = gst_adapter_available (flxdec->adapter);
467 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
468 if (avail >= FlxHeaderSize) {
469 const guint8 *data = gst_adapter_peek (flxdec->adapter, FlxHeaderSize);
471 memcpy ((gchar *) & flxdec->hdr, data, FlxHeaderSize);
472 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
477 if (flxh->type != FLX_MAGICHDR_FLI &&
478 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) {
479 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
480 ("not a flx file (type %d)\n", flxh->type));
481 return GST_FLOW_ERROR;
485 GST_LOG ("size : %d\n", flxh->size);
486 GST_LOG ("frames : %d\n", flxh->frames);
487 GST_LOG ("width : %d\n", flxh->width);
488 GST_LOG ("height : %d\n", flxh->height);
489 GST_LOG ("depth : %d\n", flxh->depth);
490 GST_LOG ("speed : %d\n", flxh->speed);
492 flxdec->next_time = 0;
494 if (flxh->type == FLX_MAGICHDR_FLI) {
495 flxdec->frame_time = JIFFIE * flxh->speed;
497 flxdec->frame_time = flxh->speed * GST_MSECOND;
500 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
501 gst_caps_set_simple (caps,
502 "width", G_TYPE_INT, flxh->width,
503 "height", G_TYPE_INT, flxh->height,
504 "framerate", G_TYPE_DOUBLE,
505 (gdouble) (GST_SECOND / flxdec->frame_time), NULL);
507 gst_pad_set_caps (flxdec->srcpad, caps);
508 gst_caps_unref (caps);
510 if (flxh->depth <= 8)
512 flx_colorspace_converter_new (flxh->width, flxh->height);
514 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
515 GST_LOG ("(FLC) aspect_dx : %d\n", flxh->aspect_dx);
516 GST_LOG ("(FLC) aspect_dy : %d\n", flxh->aspect_dy);
517 GST_LOG ("(FLC) oframe1 : 0x%08x\n", flxh->oframe1);
518 GST_LOG ("(FLC) oframe2 : 0x%08x\n", flxh->oframe2);
521 flxdec->size = (flxh->width * flxh->height);
523 /* create delta and output frame */
524 flxdec->frame = gst_buffer_new ();
525 flxdec->delta = gst_buffer_new ();
526 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
527 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
528 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
529 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
531 flxdec->state = GST_FLXDEC_PLAYING;
533 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
536 if (avail >= FlxFrameChunkSize) {
538 guchar *chunk = NULL;
541 gst_adapter_peek (flxdec->adapter, FlxFrameChunkSize);
542 memcpy (&flxfh, data, FlxFrameChunkSize);
546 if (avail < flxfh.size) {
550 gst_adapter_flush (flxdec->adapter, FlxFrameChunkSize);
551 data = gst_adapter_peek (flxdec->adapter,
552 flxfh.size - FlxFrameChunkSize);
553 chunk = g_memdup (data, flxfh.size - FlxFrameChunkSize);
554 to_flush = flxfh.size - FlxFrameChunkSize;
556 if (((FlxFrameType *) chunk)->chunks == 0)
559 /* create 32 bits output frame */
560 res = gst_pad_alloc_buffer (flxdec->srcpad,
561 GST_BUFFER_OFFSET_NONE,
562 flxdec->size * 4, GST_PAD_CAPS (flxdec->srcpad), &out);
564 if (res != GST_FLOW_OK)
568 flx_decode_chunks (flxdec,
569 ((FlxFrameType *) chunk)->chunks,
570 chunk + FlxFrameTypeSize, GST_BUFFER_DATA (flxdec->frame));
572 /* save copy of the current frame for possible delta. */
573 memcpy (GST_BUFFER_DATA (flxdec->delta),
574 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
576 /* convert current frame. */
577 flx_colorspace_convert (flxdec->converter,
578 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
580 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
581 flxdec->next_time += flxdec->frame_time;
583 gst_pad_push (flxdec->srcpad, out);
587 gst_adapter_flush (flxdec->adapter, to_flush);
597 static GstStateChangeReturn
598 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
602 flxdec = GST_FLXDEC (element);
604 switch (transition) {
605 case GST_STATE_CHANGE_NULL_TO_READY:
607 case GST_STATE_CHANGE_READY_TO_PAUSED:
608 gst_adapter_clear (flxdec->adapter);
609 flxdec->state = GST_FLXDEC_READ_HEADER;
611 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
613 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
615 case GST_STATE_CHANGE_PAUSED_TO_READY:
616 gst_buffer_unref (flxdec->frame);
617 flxdec->frame = NULL;
618 gst_buffer_unref (flxdec->delta);
619 flxdec->delta = NULL;
621 case GST_STATE_CHANGE_READY_TO_NULL:
625 return parent_class->change_state (element, transition);
627 //return GST_STATE_CHANGE_SUCCESS;
631 gst_flxdec_set_property (GObject * object, guint prop_id, const GValue * value,
636 g_return_if_fail (GST_IS_FLXDEC (object));
637 flxdec = GST_FLXDEC (object);
646 gst_flxdec_get_property (GObject * object, guint prop_id, GValue * value,
651 g_return_if_fail (GST_IS_FLXDEC (object));
652 flxdec = GST_FLXDEC (object);
656 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
662 plugin_init (GstPlugin * plugin)
664 return gst_element_register (plugin, "flxdec",
665 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
668 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
672 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)