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>"
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 void gst_flxdec_loop (GstElement * element);
75 static GstElementStateReturn gst_flxdec_change_state (GstElement * element);
77 static void gst_flxdec_set_property (GObject * object, guint prop_id,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_flxdec_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
83 static void flx_decode_color (GstFlxDec *, guchar *, guchar *, gint);
84 static void flx_decode_brun (GstFlxDec *, guchar *, guchar *);
85 static void flx_decode_delta_fli (GstFlxDec *, guchar *, guchar *);
86 static void flx_decode_delta_flc (GstFlxDec *, guchar *, guchar *);
88 #define rndalign(off) ((off) + ((off) % 2))
90 static GstElementClass *parent_class = NULL;
93 gst_flxdec_get_type (void)
95 static GType flxdec_type = 0;
98 static const GTypeInfo flxdec_info = {
99 sizeof (GstFlxDecClass),
100 (GBaseInitFunc) gst_flxdec_base_init,
102 (GClassInitFunc) gst_flxdec_class_init,
107 (GInstanceInitFunc) gst_flxdec_init,
111 g_type_register_static (GST_TYPE_ELEMENT, "GstFlxDec", &flxdec_info, 0);
117 gst_flxdec_base_init (GstFlxDecClass * klass)
119 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
121 gst_element_class_set_details (gstelement_class, &flxdec_details);
122 gst_element_class_add_pad_template (gstelement_class,
123 gst_static_pad_template_get (&sink_factory));
124 gst_element_class_add_pad_template (gstelement_class,
125 gst_static_pad_template_get (&src_video_factory));
129 gst_flxdec_class_init (GstFlxDecClass * klass)
131 GObjectClass *gobject_class;
132 GstElementClass *gstelement_class;
134 gobject_class = (GObjectClass *) klass;
135 gstelement_class = (GstElementClass *) klass;
137 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
139 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
141 gobject_class->set_property = gst_flxdec_set_property;
142 gobject_class->get_property = gst_flxdec_get_property;
144 gstelement_class->change_state = gst_flxdec_change_state;
150 gst_flxdec_init (GstFlxDec * flxdec)
153 gst_pad_new_from_template (gst_static_pad_template_get (&sink_factory),
155 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
156 gst_element_set_loop_function (GST_ELEMENT (flxdec), gst_flxdec_loop);
159 gst_pad_new_from_template (gst_static_pad_template_get
160 (&src_video_factory), "src");
161 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
162 gst_pad_use_explicit_caps (flxdec->srcpad);
165 flxdec->frame = NULL;
166 flxdec->delta = NULL;
170 flx_decode_chunks (GstFlxDec * flxdec, gulong count, gchar * data, gchar * dest)
174 g_return_if_fail (data != NULL);
177 hdr = (FlxFrameChunk *) data;
178 data += FlxFrameChunkSize;
182 flx_decode_color (flxdec, data, dest, 2);
183 data += rndalign (hdr->size) - FlxFrameChunkSize;
187 flx_decode_color (flxdec, data, dest, 0);
188 data += rndalign (hdr->size) - FlxFrameChunkSize;
192 flx_decode_brun (flxdec, data, dest);
193 data += rndalign (hdr->size) - FlxFrameChunkSize;
197 flx_decode_delta_fli (flxdec, data, dest);
198 data += rndalign (hdr->size) - FlxFrameChunkSize;
202 flx_decode_delta_flc (flxdec, data, dest);
203 data += rndalign (hdr->size) - FlxFrameChunkSize;
207 memset (dest, 0, flxdec->size);
211 data += rndalign (hdr->size) - FlxFrameChunkSize;
215 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
217 data += rndalign (hdr->size) - FlxFrameChunkSize;
225 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
227 guint packs, count, indx;
229 g_return_if_fail (flxdec != NULL);
231 packs = (data[0] + (data[1] << 8));
236 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
238 /* color map index + skip count */
241 /* number of rgb triplets */
242 count = *data++ & 0xff;
246 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
247 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
254 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
256 gulong count, lines, row;
259 g_return_if_fail (flxdec != NULL);
261 lines = flxdec->hdr.height;
264 * should not be used anymore, since the flc format can
265 * contain more then 255 RLE packets. we use the frame
270 row = flxdec->hdr.width;
276 count = 0x100 - count;
295 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
297 gulong count, packets, lines, start_line, start_l;
300 g_return_if_fail (flxdec != NULL);
301 g_return_if_fail (flxdec->delta != NULL);
304 /* use last frame for delta */
305 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
306 GST_BUFFER_SIZE (flxdec->delta));
308 start_line = (data[0] + (data[1] << 8));
309 lines = (data[2] + (data[3] << 8));
312 /* start position of delta */
313 dest += (flxdec->hdr.width * start_line);
330 count = 0x100 - count;
342 start_p += flxdec->hdr.width;
348 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
350 gulong count, lines, start_l, opcode;
353 g_return_if_fail (flxdec != NULL);
354 g_return_if_fail (flxdec->delta != NULL);
357 /* use last frame for delta */
358 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
359 GST_BUFFER_SIZE (flxdec->delta));
361 lines = (data[0] + (data[1] << 8));
368 dest = start_p + (flxdec->hdr.width * (start_l - lines));
370 /* process opcode(s) */
371 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
373 if ((opcode & 0xc000) == 0xc000) {
375 start_l += (0x10000 - opcode);
376 dest += flxdec->hdr.width * (0x10000 - opcode);
379 dest += flxdec->hdr.width;
380 *dest++ = (opcode & 0xff);
385 /* last opcode is the packet count */
394 /* replicate word run */
395 count = 0x100 - count;
402 /* literal word run */
414 flx_get_data (GstFlxDec * flxdec, gulong size)
419 g_return_val_if_fail (flxdec != NULL, NULL);
421 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
422 if (got_bytes < size) {
426 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
427 gst_pad_event_default (flxdec->sinkpad, event);
435 gst_flxdec_loop (GstElement * element)
439 guchar *data, *chunk;
444 FlxFrameChunk *flxfh;
446 g_return_if_fail (element != NULL);
447 g_return_if_fail (GST_IS_FLXDEC (element));
449 GST_DEBUG ("entering loop function");
451 flxdec = GST_FLXDEC (element);
453 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
454 databuf = flx_get_data (flxdec, FlxHeaderSize);
457 GST_LOG ("empty buffer");
461 data = GST_BUFFER_DATA (databuf);
463 memcpy ((char *) &flxdec->hdr, data, sizeof (FlxHeader));
465 gst_buffer_unref (databuf);
470 if (flxh->type != FLX_MAGICHDR_FLI &&
471 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) {
472 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
473 ("not a flx file (type %d)\n", flxh->type));
478 GST_LOG ("size : %d\n", flxh->size);
479 GST_LOG ("frames : %d\n", flxh->frames);
480 GST_LOG ("width : %d\n", flxh->width);
481 GST_LOG ("height : %d\n", flxh->height);
482 GST_LOG ("depth : %d\n", flxh->depth);
483 GST_LOG ("speed : %d\n", flxh->speed);
485 flxdec->next_time = 0;
487 if (flxh->type == FLX_MAGICHDR_FLI) {
488 flxdec->frame_time = JIFFIE * flxh->speed;
490 flxdec->frame_time = flxh->speed * GST_MSECOND;
493 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
494 gst_caps_set_simple (caps,
495 "width", G_TYPE_INT, flxh->width,
496 "height", G_TYPE_INT, flxh->height,
497 "framerate", G_TYPE_DOUBLE, (gdouble) (GST_SECOND / flxdec->frame_time),
500 gst_pad_set_explicit_caps (flxdec->srcpad, caps);
502 if (flxh->depth <= 8)
504 flx_colorspace_converter_new (flxh->width, flxh->height);
506 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
507 GST_LOG ("(FLC) aspect_dx : %d\n", flxh->aspect_dx);
508 GST_LOG ("(FLC) aspect_dy : %d\n", flxh->aspect_dy);
509 GST_LOG ("(FLC) oframe1 : 0x%08x\n", flxh->oframe1);
510 GST_LOG ("(FLC) oframe2 : 0x%08x\n", flxh->oframe2);
513 flxdec->size = (flxh->width * flxh->height);
515 /* create delta and output frame */
516 flxdec->frame = gst_buffer_new ();
517 flxdec->delta = gst_buffer_new ();
518 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
519 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
520 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
521 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
523 flxdec->state = GST_FLXDEC_PLAYING;
524 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
527 databuf = flx_get_data (flxdec, FlxFrameChunkSize);
531 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA (databuf);
535 buf = flx_get_data (flxdec, flxfh->size - FlxFrameChunkSize);
537 chunk = GST_BUFFER_DATA (buf);
539 if (((FlxFrameType *) chunk)->chunks == 0)
542 /* create 32 bits output frame */
543 out = gst_buffer_new ();
544 GST_BUFFER_DATA (out) = g_malloc (flxdec->size * 4);
545 GST_BUFFER_SIZE (out) = flxdec->size * 4;
548 flx_decode_chunks (flxdec,
549 ((FlxFrameType *) chunk)->chunks,
550 GST_BUFFER_DATA (buf) + FlxFrameTypeSize,
551 GST_BUFFER_DATA (flxdec->frame));
553 /* destroy input buffer */
554 gst_buffer_unref (buf);
556 /* save copy of the current frame for possible delta. */
557 memcpy (GST_BUFFER_DATA (flxdec->delta),
558 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
560 /* convert current frame. */
561 flx_colorspace_convert (flxdec->converter,
562 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
564 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
565 flxdec->next_time += flxdec->frame_time;
567 gst_pad_push (flxdec->srcpad, GST_DATA (out));
572 /* destroy header buffer */
573 gst_buffer_unref (databuf);
577 static GstElementStateReturn
578 gst_flxdec_change_state (GstElement * element)
582 flxdec = GST_FLXDEC (element);
584 switch (GST_STATE_TRANSITION (element)) {
585 case GST_STATE_NULL_TO_READY:
587 case GST_STATE_READY_TO_PAUSED:
588 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
589 flxdec->state = GST_FLXDEC_READ_HEADER;
591 case GST_STATE_PAUSED_TO_PLAYING:
593 case GST_STATE_PLAYING_TO_PAUSED:
595 case GST_STATE_PAUSED_TO_READY:
596 gst_buffer_unref (flxdec->frame);
597 flxdec->frame = NULL;
598 gst_buffer_unref (flxdec->delta);
599 flxdec->delta = NULL;
600 gst_bytestream_destroy (flxdec->bs);
602 case GST_STATE_READY_TO_NULL:
606 parent_class->change_state (element);
608 return GST_STATE_SUCCESS;
612 gst_flxdec_set_property (GObject * object, guint prop_id, const GValue * value,
617 /* it's not null if we got it, but it might not be ours */
618 g_return_if_fail (GST_IS_FLXDEC (object));
619 flxdec = GST_FLXDEC (object);
628 gst_flxdec_get_property (GObject * object, guint prop_id, GValue * value,
633 /* it's not null if we got it, but it might not be ours */
634 g_return_if_fail (GST_IS_FLXDEC (object));
635 flxdec = GST_FLXDEC (object);
639 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
645 plugin_init (GstPlugin * plugin)
647 if (!gst_library_load ("gstbytestream"))
650 return gst_element_register (plugin, "flxdec",
651 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
654 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
658 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)