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);
164 flxdec->frame = NULL;
165 flxdec->delta = NULL;
169 flx_decode_chunks (GstFlxDec * flxdec, gulong count, gchar * data, gchar * dest)
173 g_return_if_fail (data != NULL);
176 hdr = (FlxFrameChunk *) data;
177 data += FlxFrameChunkSize;
181 flx_decode_color (flxdec, data, dest, 2);
182 data += rndalign (hdr->size) - FlxFrameChunkSize;
186 flx_decode_color (flxdec, data, dest, 0);
187 data += rndalign (hdr->size) - FlxFrameChunkSize;
191 flx_decode_brun (flxdec, data, dest);
192 data += rndalign (hdr->size) - FlxFrameChunkSize;
196 flx_decode_delta_fli (flxdec, data, dest);
197 data += rndalign (hdr->size) - FlxFrameChunkSize;
201 flx_decode_delta_flc (flxdec, data, dest);
202 data += rndalign (hdr->size) - FlxFrameChunkSize;
206 memset (dest, 0, flxdec->size);
210 data += rndalign (hdr->size) - FlxFrameChunkSize;
214 GST_WARNING ("Unimplented chunk type: 0x%02x size: %d - skipping",
216 data += rndalign (hdr->size) - FlxFrameChunkSize;
224 flx_decode_color (GstFlxDec * flxdec, guchar * data, guchar * dest, gint scale)
226 guint packs, count, indx;
228 g_return_if_fail (flxdec != NULL);
230 packs = (data[0] + (data[1] << 8));
235 GST_LOG ("GstFlxDec: cmap packs: %d", packs);
237 /* color map index + skip count */
240 /* number of rgb triplets */
241 count = *data++ & 0xff;
245 GST_LOG ("GstFlxDec: cmap count: %d (indx: %d)\n", count, indx);
246 flx_set_palette_vector (flxdec->converter, indx, count, data, scale);
253 flx_decode_brun (GstFlxDec * flxdec, guchar * data, guchar * dest)
255 gulong count, lines, row;
258 g_return_if_fail (flxdec != NULL);
260 lines = flxdec->hdr.height;
263 * should not be used anymore, since the flc format can
264 * contain more then 255 RLE packets. we use the frame
269 row = flxdec->hdr.width;
275 count = 0x100 - count;
294 flx_decode_delta_fli (GstFlxDec * flxdec, guchar * data, guchar * dest)
296 gulong count, packets, lines, start_line, start_l;
299 g_return_if_fail (flxdec != NULL);
300 g_return_if_fail (flxdec->delta != NULL);
303 /* use last frame for delta */
304 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
305 GST_BUFFER_SIZE (flxdec->delta));
307 start_line = (data[0] + (data[1] << 8));
308 lines = (data[2] + (data[3] << 8));
311 /* start position of delta */
312 dest += (flxdec->hdr.width * start_line);
329 count = 0x100 - count;
341 start_p += flxdec->hdr.width;
347 flx_decode_delta_flc (GstFlxDec * flxdec, guchar * data, guchar * dest)
349 gulong count, lines, start_l, opcode;
352 g_return_if_fail (flxdec != NULL);
353 g_return_if_fail (flxdec->delta != NULL);
356 /* use last frame for delta */
357 memcpy (dest, GST_BUFFER_DATA (flxdec->delta),
358 GST_BUFFER_SIZE (flxdec->delta));
360 lines = (data[0] + (data[1] << 8));
367 dest = start_p + (flxdec->hdr.width * (start_l - lines));
369 /* process opcode(s) */
370 while ((opcode = (data[0] + (data[1] << 8))) & 0xc000) {
372 if ((opcode & 0xc000) == 0xc000) {
374 start_l += (0x10000 - opcode);
375 dest += flxdec->hdr.width * (0x10000 - opcode);
378 dest += flxdec->hdr.width;
379 *dest++ = (opcode & 0xff);
384 /* last opcode is the packet count */
393 /* replicate word run */
394 count = 0x100 - count;
401 /* literal word run */
413 flx_get_data (GstFlxDec * flxdec, gulong size)
418 g_return_val_if_fail (flxdec != NULL, NULL);
420 got_bytes = gst_bytestream_read (flxdec->bs, &retbuf, size);
421 if (got_bytes < size) {
425 gst_bytestream_get_status (flxdec->bs, &remaining, &event);
426 gst_pad_event_default (flxdec->sinkpad, event);
434 gst_flxdec_loop (GstElement * element)
438 guchar *data, *chunk;
443 FlxFrameChunk *flxfh;
445 g_return_if_fail (element != NULL);
446 g_return_if_fail (GST_IS_FLXDEC (element));
448 GST_DEBUG ("entering loop function");
450 flxdec = GST_FLXDEC (element);
452 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
453 databuf = flx_get_data (flxdec, FlxHeaderSize);
456 GST_LOG ("empty buffer");
460 data = GST_BUFFER_DATA (databuf);
462 memcpy ((char *) &flxdec->hdr, data, sizeof (FlxHeader));
464 gst_buffer_unref (databuf);
469 if (flxh->type != FLX_MAGICHDR_FLI &&
470 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) {
471 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
472 ("not a flx file (type %d)\n", flxh->type));
477 GST_LOG ("size : %d\n", flxh->size);
478 GST_LOG ("frames : %d\n", flxh->frames);
479 GST_LOG ("width : %d\n", flxh->width);
480 GST_LOG ("height : %d\n", flxh->height);
481 GST_LOG ("depth : %d\n", flxh->depth);
482 GST_LOG ("speed : %d\n", flxh->speed);
484 flxdec->next_time = 0;
486 if (flxh->type == FLX_MAGICHDR_FLI) {
487 flxdec->frame_time = JIFFIE * flxh->speed;
489 flxdec->frame_time = flxh->speed * GST_MSECOND;
492 caps = gst_caps_from_string (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN);
493 gst_caps_set_simple (caps,
494 "width", G_TYPE_INT, flxh->width,
495 "height", G_TYPE_INT, flxh->height,
496 "framerate", G_TYPE_DOUBLE, GST_SECOND / flxdec->frame_time, NULL);
498 if (flxh->depth <= 8)
500 flx_colorspace_converter_new (flxh->width, flxh->height);
502 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
503 GST_LOG ("(FLC) aspect_dx : %d\n", flxh->aspect_dx);
504 GST_LOG ("(FLC) aspect_dy : %d\n", flxh->aspect_dy);
505 GST_LOG ("(FLC) oframe1 : 0x%08x\n", flxh->oframe1);
506 GST_LOG ("(FLC) oframe2 : 0x%08x\n", flxh->oframe2);
509 flxdec->size = (flxh->width * flxh->height);
511 /* create delta and output frame */
512 flxdec->frame = gst_buffer_new ();
513 flxdec->delta = gst_buffer_new ();
514 GST_BUFFER_DATA (flxdec->frame) = g_malloc (flxdec->size);
515 GST_BUFFER_SIZE (flxdec->frame) = flxdec->size;
516 GST_BUFFER_DATA (flxdec->delta) = g_malloc (flxdec->size);
517 GST_BUFFER_SIZE (flxdec->delta) = flxdec->size;
519 flxdec->state = GST_FLXDEC_PLAYING;
520 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
523 databuf = flx_get_data (flxdec, FlxFrameChunkSize);
527 flxfh = (FlxFrameChunk *) GST_BUFFER_DATA (databuf);
531 buf = flx_get_data (flxdec, flxfh->size - FlxFrameChunkSize);
533 chunk = GST_BUFFER_DATA (buf);
535 if (((FlxFrameType *) chunk)->chunks == 0)
538 /* create 32 bits output frame */
539 out = gst_buffer_new ();
540 GST_BUFFER_DATA (out) = g_malloc (flxdec->size * 4);
541 GST_BUFFER_SIZE (out) = flxdec->size * 4;
544 flx_decode_chunks (flxdec,
545 ((FlxFrameType *) chunk)->chunks,
546 GST_BUFFER_DATA (buf) + FlxFrameTypeSize,
547 GST_BUFFER_DATA (flxdec->frame));
549 /* destroy input buffer */
550 gst_buffer_unref (buf);
552 /* save copy of the current frame for possible delta. */
553 memcpy (GST_BUFFER_DATA (flxdec->delta),
554 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_SIZE (flxdec->delta));
556 /* convert current frame. */
557 flx_colorspace_convert (flxdec->converter,
558 GST_BUFFER_DATA (flxdec->frame), GST_BUFFER_DATA (out));
560 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
561 flxdec->next_time += flxdec->frame_time;
563 gst_pad_push (flxdec->srcpad, GST_DATA (out));
568 /* destroy header buffer */
569 gst_buffer_unref (databuf);
573 static GstElementStateReturn
574 gst_flxdec_change_state (GstElement * element)
578 flxdec = GST_FLXDEC (element);
580 switch (GST_STATE_TRANSITION (element)) {
581 case GST_STATE_NULL_TO_READY:
583 case GST_STATE_READY_TO_PAUSED:
584 flxdec->bs = gst_bytestream_new (flxdec->sinkpad);
585 flxdec->state = GST_FLXDEC_READ_HEADER;
587 case GST_STATE_PAUSED_TO_PLAYING:
589 case GST_STATE_PLAYING_TO_PAUSED:
591 case GST_STATE_PAUSED_TO_READY:
592 gst_buffer_unref (flxdec->frame);
593 flxdec->frame = NULL;
594 gst_buffer_unref (flxdec->delta);
595 flxdec->delta = NULL;
596 gst_bytestream_destroy (flxdec->bs);
598 case GST_STATE_READY_TO_NULL:
602 parent_class->change_state (element);
604 return GST_STATE_SUCCESS;
608 gst_flxdec_set_property (GObject * object, guint prop_id, const GValue * value,
613 /* it's not null if we got it, but it might not be ours */
614 g_return_if_fail (GST_IS_FLXDEC (object));
615 flxdec = GST_FLXDEC (object);
624 gst_flxdec_get_property (GObject * object, guint prop_id, GValue * value,
629 /* it's not null if we got it, but it might not be ours */
630 g_return_if_fail (GST_IS_FLXDEC (object));
631 flxdec = GST_FLXDEC (object);
635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
641 plugin_init (GstPlugin * plugin)
643 if (!gst_library_load ("gstbytestream"))
646 return gst_element_register (plugin, "flxdec",
647 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
650 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
654 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)