2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
3 * Copyright (C) <2016> Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * SECTION:element-flxdec
23 * This element decodes fli/flc/flx-video into raw video
26 * http://www.coolutils.com/Formats/FLI
27 * http://woodshole.er.usgs.gov/operations/modeling/flc.html
28 * http://www.compuphase.com/flic.htm
37 #include "gstflxdec.h"
38 #include <gst/video/video.h>
40 #define JIFFIE (GST_SECOND/70)
42 GST_DEBUG_CATEGORY_STATIC (flxdec_debug);
43 #define GST_CAT_DEFAULT flxdec_debug
46 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_STATIC_CAPS ("video/x-fli")
52 #if G_BYTE_ORDER == G_BIG_ENDIAN
53 #define RGB_ORDER "xRGB"
55 #define RGB_ORDER "BGRx"
59 static GstStaticPadTemplate src_video_factory = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (RGB_ORDER))
65 static void gst_flxdec_dispose (GstFlxDec * flxdec);
67 static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstObject * parent,
69 static gboolean gst_flxdec_sink_event_handler (GstPad * pad,
70 GstObject * parent, GstEvent * event);
72 static GstStateChangeReturn gst_flxdec_change_state (GstElement * element,
73 GstStateChange transition);
75 static gboolean gst_flxdec_src_query_handler (GstPad * pad, GstObject * parent,
78 static gboolean flx_decode_color (GstFlxDec * flxdec, GstByteReader * reader,
79 GstByteWriter * writer, gint scale);
80 static gboolean flx_decode_brun (GstFlxDec * flxdec,
81 GstByteReader * reader, GstByteWriter * writer);
82 static gboolean flx_decode_delta_fli (GstFlxDec * flxdec,
83 GstByteReader * reader, GstByteWriter * writer);
84 static gboolean flx_decode_delta_flc (GstFlxDec * flxdec,
85 GstByteReader * reader, GstByteWriter * writer);
87 #define rndalign(off) ((off) + ((off) & 1))
89 #define gst_flxdec_parent_class parent_class
90 G_DEFINE_TYPE (GstFlxDec, gst_flxdec, GST_TYPE_ELEMENT);
93 gst_flxdec_class_init (GstFlxDecClass * klass)
95 GObjectClass *gobject_class;
96 GstElementClass *gstelement_class;
98 gobject_class = (GObjectClass *) klass;
99 gstelement_class = (GstElementClass *) klass;
101 parent_class = g_type_class_peek_parent (klass);
103 gobject_class->dispose = (GObjectFinalizeFunc) gst_flxdec_dispose;
105 GST_DEBUG_CATEGORY_INIT (flxdec_debug, "flxdec", 0, "FLX video decoder");
107 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flxdec_change_state);
109 gst_element_class_set_static_metadata (gstelement_class, "FLX video decoder",
110 "Codec/Decoder/Video",
111 "FLC/FLI/FLX video decoder",
112 "Sepp Wijnands <mrrazz@garbage-coderz.net>, Zeeshan Ali <zeenix@gmail.com>");
113 gst_element_class_add_pad_template (gstelement_class,
114 gst_static_pad_template_get (&sink_factory));
115 gst_element_class_add_pad_template (gstelement_class,
116 gst_static_pad_template_get (&src_video_factory));
120 gst_flxdec_init (GstFlxDec * flxdec)
122 flxdec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
123 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->sinkpad);
124 gst_pad_set_chain_function (flxdec->sinkpad,
125 GST_DEBUG_FUNCPTR (gst_flxdec_chain));
126 gst_pad_set_event_function (flxdec->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_flxdec_sink_event_handler));
129 flxdec->srcpad = gst_pad_new_from_static_template (&src_video_factory, "src");
130 gst_element_add_pad (GST_ELEMENT (flxdec), flxdec->srcpad);
131 gst_pad_set_query_function (flxdec->srcpad,
132 GST_DEBUG_FUNCPTR (gst_flxdec_src_query_handler));
134 gst_pad_use_fixed_caps (flxdec->srcpad);
136 flxdec->adapter = gst_adapter_new ();
140 gst_flxdec_dispose (GstFlxDec * flxdec)
142 if (flxdec->adapter) {
143 g_object_unref (flxdec->adapter);
144 flxdec->adapter = NULL;
147 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) flxdec);
151 gst_flxdec_src_query_handler (GstPad * pad, GstObject * parent,
154 GstFlxDec *flxdec = (GstFlxDec *) parent;
155 gboolean ret = FALSE;
157 switch (GST_QUERY_TYPE (query)) {
158 case GST_QUERY_DURATION:
162 gst_query_parse_duration (query, &format, NULL);
164 if (format != GST_FORMAT_TIME)
167 gst_query_set_duration (query, format, flxdec->duration);
176 ret = gst_pad_query_default (pad, parent, query);
182 gst_flxdec_sink_event_handler (GstPad * pad, GstObject * parent,
188 flxdec = GST_FLXDEC (parent);
190 switch (GST_EVENT_TYPE (event)) {
191 case GST_EVENT_SEGMENT:
193 gst_event_copy_segment (event, &flxdec->segment);
194 if (flxdec->segment.format != GST_FORMAT_TIME) {
195 GST_DEBUG_OBJECT (flxdec, "generating TIME segment");
196 gst_segment_init (&flxdec->segment, GST_FORMAT_TIME);
197 gst_event_unref (event);
198 event = gst_event_new_segment (&flxdec->segment);
201 if (gst_pad_has_current_caps (flxdec->srcpad)) {
202 ret = gst_pad_event_default (pad, parent, event);
204 flxdec->need_segment = TRUE;
205 gst_event_unref (event);
210 case GST_EVENT_FLUSH_STOP:
211 gst_segment_init (&flxdec->segment, GST_FORMAT_UNDEFINED);
212 ret = gst_pad_event_default (pad, parent, event);
215 ret = gst_pad_event_default (pad, parent, event);
223 flx_decode_chunks (GstFlxDec * flxdec, gulong n_chunks, GstByteReader * reader,
224 GstByteWriter * writer)
233 if (!gst_byte_reader_get_uint32_le (reader, &size))
235 if (!gst_byte_reader_get_uint16_le (reader, &type))
237 GST_LOG_OBJECT (flxdec, "chunk has type 0x%02x size %d", type, size);
239 if (!gst_byte_reader_get_sub_reader (reader, &chunk,
240 size - FlxFrameChunkSize)) {
241 GST_ERROR_OBJECT (flxdec, "Incorrect size in the chunk header");
247 ret = flx_decode_color (flxdec, &chunk, writer, 2);
251 ret = flx_decode_color (flxdec, &chunk, writer, 0);
255 ret = flx_decode_brun (flxdec, &chunk, writer);
259 ret = flx_decode_delta_fli (flxdec, &chunk, writer);
263 ret = flx_decode_delta_flc (flxdec, &chunk, writer);
267 ret = gst_byte_writer_fill (writer, 0, flxdec->size);
274 GST_WARNING ("Unimplemented chunk type: 0x%02x size: %d - skipping",
286 GST_ERROR_OBJECT (flxdec, "Failed to decode chunk");
293 flx_decode_color (GstFlxDec * flxdec, GstByteReader * reader,
294 GstByteWriter * writer, gint scale)
299 if (!gst_byte_reader_get_uint16_le (reader, &packs))
303 GST_LOG ("GstFlxDec: cmap packs: %d", (guint) packs);
306 guint16 actual_count;
308 /* color map index + skip count */
309 if (!gst_byte_reader_get_uint8 (reader, &indx))
312 /* number of rgb triplets */
313 if (!gst_byte_reader_get_uint8 (reader, &count))
316 actual_count = count == 0 ? 256 : count;
318 if (!gst_byte_reader_get_data (reader, count * 3, &data))
321 GST_LOG_OBJECT (flxdec, "cmap count: %d (indx: %d)", actual_count, indx);
322 flx_set_palette_vector (flxdec->converter, indx, actual_count,
323 (guchar *) data, scale);
329 GST_ERROR_OBJECT (flxdec, "Error decoding color palette");
334 flx_decode_brun (GstFlxDec * flxdec, GstByteReader * reader,
335 GstByteWriter * writer)
339 g_return_val_if_fail (flxdec != NULL, FALSE);
341 lines = flxdec->hdr.height;
344 * should not be used anymore, since the flc format can
345 * contain more then 255 RLE packets. we use the frame
348 if (!gst_byte_reader_skip (reader, 1))
351 row = flxdec->hdr.width;
355 if (!gst_byte_reader_get_int8 (reader, &count))
364 GST_LOG_OBJECT (flxdec, "have literal run of size %d", count);
367 GST_ERROR_OBJECT (flxdec, "Invalid BRUN line detected. "
368 "bytes to write exceeds the end of the row");
373 if (!gst_byte_reader_get_data (reader, count, &data))
375 if (!gst_byte_writer_put_data (writer, data, count))
380 GST_LOG_OBJECT (flxdec, "have replicate run of size %d", count);
383 GST_ERROR_OBJECT (flxdec, "Invalid BRUN packet detected."
384 "bytes to write exceeds the end of the row");
391 if (!gst_byte_reader_get_uint8 (reader, &x))
393 if (!gst_byte_writer_fill (writer, x, count))
402 GST_ERROR_OBJECT (flxdec, "Failed to decode BRUN packet");
407 flx_decode_delta_fli (GstFlxDec * flxdec, GstByteReader * reader,
408 GstByteWriter * writer)
410 guint16 start_line, lines;
413 g_return_val_if_fail (flxdec != NULL, FALSE);
414 g_return_val_if_fail (flxdec->delta_data != NULL, FALSE);
416 /* use last frame for delta */
417 if (!gst_byte_writer_put_data (writer, flxdec->delta_data, flxdec->size))
420 if (!gst_byte_reader_get_uint16_le (reader, &start_line))
422 if (!gst_byte_reader_get_uint16_le (reader, &lines))
424 GST_LOG_OBJECT (flxdec, "height %d start line %d line count %d",
425 flxdec->hdr.height, start_line, lines);
427 if (start_line + lines > flxdec->hdr.height) {
428 GST_ERROR_OBJECT (flxdec, "Invalid FLI packet detected. too many lines.");
432 line_start_i = flxdec->hdr.width * start_line;
433 if (!gst_byte_writer_set_pos (writer, line_start_i))
440 if (!gst_byte_reader_get_uint8 (reader, &packets))
442 GST_LOG_OBJECT (flxdec, "have %d packets", packets);
448 if (!gst_byte_reader_get_uint8 (reader, &skip))
452 if (!gst_byte_writer_set_pos (writer,
453 gst_byte_writer_get_pos (writer) + skip))
457 if (!gst_byte_reader_get_int8 (reader, &count))
465 GST_LOG_OBJECT (flxdec, "have literal run of size %d at offset %d",
468 if (skip + count > flxdec->hdr.width) {
469 GST_ERROR_OBJECT (flxdec, "Invalid FLI packet detected. "
474 if (!gst_byte_reader_get_uint8 (reader, &x))
476 if (!gst_byte_writer_fill (writer, x, count))
481 GST_LOG_OBJECT (flxdec, "have replicate run of size %d at offset %d",
484 if (skip + count > flxdec->hdr.width) {
485 GST_ERROR_OBJECT (flxdec, "Invalid FLI packet detected. "
491 if (!gst_byte_reader_get_data (reader, count, &data))
493 if (!gst_byte_writer_put_data (writer, data, count))
497 line_start_i += flxdec->hdr.width;
498 if (!gst_byte_writer_set_pos (writer, line_start_i))
505 GST_ERROR_OBJECT (flxdec, "Failed to decode FLI packet");
510 flx_decode_delta_flc (GstFlxDec * flxdec, GstByteReader * reader,
511 GstByteWriter * writer)
513 guint16 lines, start_l;
515 g_return_val_if_fail (flxdec != NULL, FALSE);
516 g_return_val_if_fail (flxdec->delta_data != NULL, FALSE);
518 /* use last frame for delta */
519 if (!gst_byte_writer_put_data (writer, flxdec->delta_data, flxdec->size))
521 if (!gst_byte_reader_get_uint16_le (reader, &lines))
524 if (lines > flxdec->hdr.height) {
525 GST_ERROR_OBJECT (flxdec, "Invalid FLC packet detected. too many lines.");
534 if (!gst_byte_writer_set_pos (writer,
535 flxdec->hdr.width * (start_l - lines)))
538 /* process opcode(s) */
540 if (!gst_byte_reader_get_uint16_le (reader, &opcode))
542 if ((opcode & 0xc000) == 0)
545 if ((opcode & 0xc000) == 0xc000) {
546 /* line skip count */
547 gulong skip = (0x10000 - opcode);
548 if (skip > flxdec->hdr.height) {
549 GST_ERROR_OBJECT (flxdec, "Invalid FLC packet detected. "
550 "skip line count too big.");
554 if (!gst_byte_writer_set_pos (writer,
555 gst_byte_writer_get_pos (writer) + flxdec->hdr.width * skip))
559 if (!gst_byte_writer_set_pos (writer,
560 gst_byte_writer_get_pos (writer) + flxdec->hdr.width))
562 if (!gst_byte_writer_put_uint8 (writer, opcode & 0xff))
567 /* last opcode is the packet count */
568 GST_LOG_OBJECT (flxdec, "have %d packets", opcode);
574 if (!gst_byte_reader_get_uint8 (reader, &skip))
576 if (!gst_byte_writer_set_pos (writer,
577 gst_byte_writer_get_pos (writer) + skip))
581 if (!gst_byte_reader_get_int8 (reader, &count))
587 /* replicate word run */
590 GST_LOG_OBJECT (flxdec, "have replicate run of size %d at offset %d",
593 if (skip + count > flxdec->hdr.width) {
594 GST_ERROR_OBJECT (flxdec, "Invalid FLC packet detected. "
599 if (!gst_byte_reader_get_uint16_le (reader, &x))
603 if (!gst_byte_writer_put_uint16_le (writer, x)) {
608 GST_LOG_OBJECT (flxdec, "have literal run of size %d at offset %d",
611 if (skip + count > flxdec->hdr.width) {
612 GST_ERROR_OBJECT (flxdec, "Invalid FLC packet detected. "
620 if (!gst_byte_reader_get_uint16_le (reader, &x))
622 if (!gst_byte_writer_put_uint16_le (writer, x))
633 GST_ERROR_OBJECT (flxdec, "Failed to decode FLI packet");
638 _read_flx_header (GstFlxDec * flxdec, GstByteReader * reader, FlxHeader * flxh)
640 memset (flxh, 0, sizeof (*flxh));
642 if (!gst_byte_reader_get_uint32_le (reader, &flxh->size))
644 if (flxh->size < FlxHeaderSize) {
645 GST_ERROR_OBJECT (flxdec, "Invalid file size in the header");
649 if (!gst_byte_reader_get_uint16_le (reader, &flxh->type))
651 if (!gst_byte_reader_get_uint16_le (reader, &flxh->frames))
653 if (!gst_byte_reader_get_uint16_le (reader, &flxh->width))
655 if (!gst_byte_reader_get_uint16_le (reader, &flxh->height))
657 if (!gst_byte_reader_get_uint16_le (reader, &flxh->depth))
659 if (!gst_byte_reader_get_uint16_le (reader, &flxh->flags))
661 if (!gst_byte_reader_get_uint32_le (reader, &flxh->speed))
663 if (!gst_byte_reader_skip (reader, 2)) /* reserved */
666 if (!gst_byte_reader_get_uint32_le (reader, &flxh->created))
668 if (!gst_byte_reader_get_uint32_le (reader, &flxh->creator))
670 if (!gst_byte_reader_get_uint32_le (reader, &flxh->updated))
672 if (!gst_byte_reader_get_uint32_le (reader, &flxh->updater))
674 if (!gst_byte_reader_get_uint16_le (reader, &flxh->aspect_dx))
676 if (!gst_byte_reader_get_uint16_le (reader, &flxh->aspect_dy))
679 if (!gst_byte_reader_get_uint16_le (reader, &flxh->ext_flags))
681 if (!gst_byte_reader_get_uint16_le (reader, &flxh->keyframes))
683 if (!gst_byte_reader_get_uint16_le (reader, &flxh->totalframes))
685 if (!gst_byte_reader_get_uint32_le (reader, &flxh->req_memory))
687 if (!gst_byte_reader_get_uint16_le (reader, &flxh->max_regions))
689 if (!gst_byte_reader_get_uint16_le (reader, &flxh->transp_num))
691 if (!gst_byte_reader_skip (reader, 24)) /* reserved */
694 if (!gst_byte_reader_get_uint32_le (reader, &flxh->oframe1))
696 if (!gst_byte_reader_get_uint32_le (reader, &flxh->oframe2))
698 if (!gst_byte_reader_skip (reader, 40)) /* reserved */
704 GST_ERROR_OBJECT (flxdec, "Error reading file header");
709 gst_flxdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
711 GstByteReader reader;
716 GstFlowReturn res = GST_FLOW_OK;
721 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
722 flxdec = (GstFlxDec *) parent;
723 g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR);
725 gst_adapter_push (flxdec->adapter, buf);
726 available = gst_adapter_available (flxdec->adapter);
727 input = gst_adapter_get_buffer (flxdec->adapter, available);
728 if (!gst_buffer_map (input, &map_info, GST_MAP_READ)) {
729 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
730 ("%s", "Failed to map buffer"), (NULL));
733 gst_byte_reader_init (&reader, map_info.data, map_info.size);
735 if (flxdec->state == GST_FLXDEC_READ_HEADER) {
736 if (available >= FlxHeaderSize) {
737 GstByteReader header;
740 if (!gst_byte_reader_get_sub_reader (&reader, &header, FlxHeaderSize)) {
741 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
742 ("%s", "Could not read header"), (NULL));
743 goto unmap_input_error;
745 gst_adapter_flush (flxdec->adapter, FlxHeaderSize);
746 available -= FlxHeaderSize;
748 if (!_read_flx_header (flxdec, &header, &flxdec->hdr)) {
749 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
750 ("%s", "Failed to parse header"), (NULL));
751 goto unmap_input_error;
757 if (flxh->type != FLX_MAGICHDR_FLI &&
758 flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) {
759 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL),
760 ("not a flx file (type %x)", flxh->type));
761 goto unmap_input_error;
764 GST_INFO_OBJECT (flxdec, "size : %d", flxh->size);
765 GST_INFO_OBJECT (flxdec, "frames : %d", flxh->frames);
766 GST_INFO_OBJECT (flxdec, "width : %d", flxh->width);
767 GST_INFO_OBJECT (flxdec, "height : %d", flxh->height);
768 GST_INFO_OBJECT (flxdec, "depth : %d", flxh->depth);
769 GST_INFO_OBJECT (flxdec, "speed : %d", flxh->speed);
771 flxdec->next_time = 0;
773 if (flxh->type == FLX_MAGICHDR_FLI) {
774 flxdec->frame_time = JIFFIE * flxh->speed;
775 } else if (flxh->speed == 0) {
776 flxdec->frame_time = GST_SECOND / 70;
778 flxdec->frame_time = flxh->speed * GST_MSECOND;
781 flxdec->duration = flxh->frames * flxdec->frame_time;
782 GST_LOG ("duration : %" GST_TIME_FORMAT,
783 GST_TIME_ARGS (flxdec->duration));
785 templ = gst_pad_get_pad_template_caps (flxdec->srcpad);
786 caps = gst_caps_copy (templ);
787 gst_caps_unref (templ);
788 gst_caps_set_simple (caps,
789 "width", G_TYPE_INT, flxh->width,
790 "height", G_TYPE_INT, flxh->height,
791 "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND,
792 (gint) flxdec->frame_time / 1000, NULL);
794 gst_pad_set_caps (flxdec->srcpad, caps);
795 gst_caps_unref (caps);
797 if (flxdec->need_segment) {
798 gst_pad_push_event (flxdec->srcpad,
799 gst_event_new_segment (&flxdec->segment));
800 flxdec->need_segment = FALSE;
804 if (flxh->depth == 0)
807 if (flxh->depth != 8) {
808 GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE,
809 ("%s", "Don't know how to decode non 8 bit depth streams"), (NULL));
810 goto unmap_input_error;
814 flx_colorspace_converter_new (flxh->width, flxh->height);
816 if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) {
817 GST_INFO_OBJECT (flxdec, "(FLC) aspect_dx : %d", flxh->aspect_dx);
818 GST_INFO_OBJECT (flxdec, "(FLC) aspect_dy : %d", flxh->aspect_dy);
819 GST_INFO_OBJECT (flxdec, "(FLC) oframe1 : 0x%08x", flxh->oframe1);
820 GST_INFO_OBJECT (flxdec, "(FLC) oframe2 : 0x%08x", flxh->oframe2);
823 flxdec->size = ((guint) flxh->width * (guint) flxh->height);
824 if (flxdec->size >= G_MAXSIZE / 4) {
825 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
826 ("%s", "Cannot allocate required memory"), (NULL));
827 goto unmap_input_error;
830 /* create delta and output frame */
831 flxdec->frame_data = g_malloc0 (flxdec->size);
832 flxdec->delta_data = g_malloc0 (flxdec->size);
834 flxdec->state = GST_FLXDEC_PLAYING;
836 } else if (flxdec->state == GST_FLXDEC_PLAYING) {
839 /* while we have enough data in the adapter */
840 while (available >= FlxFrameChunkSize && res == GST_FLOW_OK) {
844 if (!gst_byte_reader_get_uint32_le (&reader, &size))
846 if (available < size)
850 gst_adapter_flush (flxdec->adapter, size);
852 if (!gst_byte_reader_get_uint16_le (&reader, &type))
856 case FLX_FRAME_TYPE:{
857 GstByteReader chunks;
858 GstByteWriter writer;
862 GST_LOG_OBJECT (flxdec, "Have frame type 0x%02x of size %d", type,
865 if (!gst_byte_reader_get_sub_reader (&reader, &chunks,
866 size - FlxFrameChunkSize))
869 if (!gst_byte_reader_get_uint16_le (&chunks, &n_chunks))
871 GST_LOG_OBJECT (flxdec, "Have %d chunks", n_chunks);
875 if (!gst_byte_reader_skip (&chunks, 8)) /* reserved */
878 gst_byte_writer_init_with_data (&writer, flxdec->frame_data,
882 if (!flx_decode_chunks (flxdec, n_chunks, &chunks, &writer)) {
883 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
884 ("%s", "Could not decode chunk"), NULL);
885 goto unmap_input_error;
887 gst_byte_writer_reset (&writer);
889 /* save copy of the current frame for possible delta. */
890 memcpy (flxdec->delta_data, flxdec->frame_data, flxdec->size);
892 out = gst_buffer_new_and_alloc (flxdec->size * 4);
893 if (!gst_buffer_map (out, &map, GST_MAP_WRITE)) {
894 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
895 ("%s", "Could not map output buffer"), NULL);
896 gst_buffer_unref (out);
897 goto unmap_input_error;
900 /* convert current frame. */
901 flx_colorspace_convert (flxdec->converter, flxdec->frame_data,
903 gst_buffer_unmap (out, &map);
905 GST_BUFFER_TIMESTAMP (out) = flxdec->next_time;
906 flxdec->next_time += flxdec->frame_time;
908 res = gst_pad_push (flxdec->srcpad, out);
912 GST_DEBUG_OBJECT (flxdec, "Unknown frame type 0x%02x, skipping %d",
914 if (!gst_byte_reader_skip (&reader, size - FlxFrameChunkSize))
922 gst_buffer_unmap (input, &map_info);
923 gst_buffer_unref (input);
928 GST_ELEMENT_ERROR (flxdec, STREAM, DECODE,
929 ("%s", "Failed to parse stream"), (NULL));
931 gst_buffer_unmap (input, &map_info);
933 gst_buffer_unref (input);
934 return GST_FLOW_ERROR;
937 static GstStateChangeReturn
938 gst_flxdec_change_state (GstElement * element, GstStateChange transition)
941 GstStateChangeReturn ret;
943 flxdec = GST_FLXDEC (element);
945 switch (transition) {
946 case GST_STATE_CHANGE_NULL_TO_READY:
948 case GST_STATE_CHANGE_READY_TO_PAUSED:
949 gst_adapter_clear (flxdec->adapter);
950 flxdec->state = GST_FLXDEC_READ_HEADER;
951 gst_segment_init (&flxdec->segment, GST_FORMAT_UNDEFINED);
952 flxdec->need_segment = TRUE;
954 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
960 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
962 switch (transition) {
963 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
965 case GST_STATE_CHANGE_PAUSED_TO_READY:
966 if (flxdec->frame_data) {
967 g_free (flxdec->frame_data);
968 flxdec->frame_data = NULL;
970 if (flxdec->delta_data) {
971 g_free (flxdec->delta_data);
972 flxdec->delta_data = NULL;
974 if (flxdec->converter) {
975 flx_colorspace_converter_destroy (flxdec->converter);
976 flxdec->converter = NULL;
979 case GST_STATE_CHANGE_READY_TO_NULL:
988 plugin_init (GstPlugin * plugin)
990 return gst_element_register (plugin, "flxdec",
991 GST_RANK_PRIMARY, GST_TYPE_FLXDEC);
994 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
997 "FLC/FLI/FLX video decoder",
998 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)