annotate libmpcodecs/native/fli.c @ 7141:8a46f9572b8b

sync
author nexus
date Thu, 29 Aug 2002 19:12:36 +0000
parents e6c8285a4bd8
children efb1ac9d44d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
1 /*
9200edf69d1d initial commit
melanson
parents:
diff changeset
2 FLI Decoder for MPlayer
9200edf69d1d initial commit
melanson
parents:
diff changeset
3
9200edf69d1d initial commit
melanson
parents:
diff changeset
4 (C) 2001 Mike Melanson
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
5
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
6 32bpp support (c) alex
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
7
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
8 Additional code and bug fixes by Roberto Togni
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
9
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
10 For information on the FLI format, as well as various traps to
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
11 avoid while programming one, visit:
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
12 http://www.pcisys.net/~melanson/codecs/
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
13 */
9200edf69d1d initial commit
melanson
parents:
diff changeset
14
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
15 #include <stdio.h>
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
16 #include <stdlib.h>
6131
e6c8285a4bd8 memset and memcpy were used without #include <string.h> in a few places.
arpi
parents: 5602
diff changeset
17 #include <string.h>
3509
21c7b77b3e83 fixed endian-ness for FLI and MS Video 1 decoders; fixed padding bug in
melanson
parents: 3185
diff changeset
18 #include "config.h"
21c7b77b3e83 fixed endian-ness for FLI and MS Video 1 decoders; fixed padding bug in
melanson
parents: 3185
diff changeset
19 #include "bswap.h"
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
20 #include "mp_msg.h"
3509
21c7b77b3e83 fixed endian-ness for FLI and MS Video 1 decoders; fixed padding bug in
melanson
parents: 3185
diff changeset
21
21c7b77b3e83 fixed endian-ness for FLI and MS Video 1 decoders; fixed padding bug in
melanson
parents: 3185
diff changeset
22 #define LE_16(x) (le2me_16(*(unsigned short *)(x)))
21c7b77b3e83 fixed endian-ness for FLI and MS Video 1 decoders; fixed padding bug in
melanson
parents: 3185
diff changeset
23 #define LE_32(x) (le2me_32(*(unsigned int *)(x)))
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
24
9200edf69d1d initial commit
melanson
parents:
diff changeset
25 #define FLI_256_COLOR 4
9200edf69d1d initial commit
melanson
parents:
diff changeset
26 #define FLI_DELTA 7
9200edf69d1d initial commit
melanson
parents:
diff changeset
27 #define FLI_COLOR 11
9200edf69d1d initial commit
melanson
parents:
diff changeset
28 #define FLI_LC 12
9200edf69d1d initial commit
melanson
parents:
diff changeset
29 #define FLI_BLACK 13
9200edf69d1d initial commit
melanson
parents:
diff changeset
30 #define FLI_BRUN 15
9200edf69d1d initial commit
melanson
parents:
diff changeset
31 #define FLI_COPY 16
9200edf69d1d initial commit
melanson
parents:
diff changeset
32 #define FLI_MINI 18
9200edf69d1d initial commit
melanson
parents:
diff changeset
33
9200edf69d1d initial commit
melanson
parents:
diff changeset
34 // 256 RGB entries; 25% of these bytes will be unused, but it's faster
9200edf69d1d initial commit
melanson
parents:
diff changeset
35 // to index 4-byte entries
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
36 #define PALETTE_SIZE 1024
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
37 static unsigned char palette[PALETTE_SIZE];
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
38
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
39 void *init_fli_decoder(int width, int height)
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
40 {
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
41 memset(palette, 0, PALETTE_SIZE);
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
42
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
43 return malloc(width * height * sizeof (unsigned char));
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
44 }
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
45
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
46 void decode_fli_frame(
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
47 unsigned char *encoded,
9200edf69d1d initial commit
melanson
parents:
diff changeset
48 int encoded_size,
9200edf69d1d initial commit
melanson
parents:
diff changeset
49 unsigned char *decoded,
9200edf69d1d initial commit
melanson
parents:
diff changeset
50 int width,
9200edf69d1d initial commit
melanson
parents:
diff changeset
51 int height,
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
52 int bytes_per_pixel,
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
53 void *context)
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
54 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
55 int stream_ptr = 0;
4887
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
56 int stream_ptr_after_color_chunk;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
57 int pixel_ptr;
9200edf69d1d initial commit
melanson
parents:
diff changeset
58 int palette_ptr1;
9200edf69d1d initial commit
melanson
parents:
diff changeset
59 int palette_ptr2;
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
60 unsigned char palette_idx1;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
61 unsigned char palette_idx2;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
62
9200edf69d1d initial commit
melanson
parents:
diff changeset
63 unsigned int frame_size;
9200edf69d1d initial commit
melanson
parents:
diff changeset
64 int num_chunks;
9200edf69d1d initial commit
melanson
parents:
diff changeset
65
9200edf69d1d initial commit
melanson
parents:
diff changeset
66 unsigned int chunk_size;
9200edf69d1d initial commit
melanson
parents:
diff changeset
67 int chunk_type;
9200edf69d1d initial commit
melanson
parents:
diff changeset
68
9200edf69d1d initial commit
melanson
parents:
diff changeset
69 int i, j;
9200edf69d1d initial commit
melanson
parents:
diff changeset
70
9200edf69d1d initial commit
melanson
parents:
diff changeset
71 int color_packets;
9200edf69d1d initial commit
melanson
parents:
diff changeset
72 int color_changes;
9200edf69d1d initial commit
melanson
parents:
diff changeset
73 int color_scale;
9200edf69d1d initial commit
melanson
parents:
diff changeset
74
9200edf69d1d initial commit
melanson
parents:
diff changeset
75 int lines;
9200edf69d1d initial commit
melanson
parents:
diff changeset
76 int compressed_lines;
9200edf69d1d initial commit
melanson
parents:
diff changeset
77 int starting_line;
9200edf69d1d initial commit
melanson
parents:
diff changeset
78 signed short line_packets;
9200edf69d1d initial commit
melanson
parents:
diff changeset
79 int y_ptr;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
80 int line_inc = width * bytes_per_pixel;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
81 signed char byte_run;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
82 int pixel_skip;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
83 int update_whole_frame = 0; // Palette change flag
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
84 unsigned char *fli_ghost_image = (unsigned char *)context;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
85 int ghost_pixel_ptr;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
86 int ghost_y_ptr;
4887
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
87
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
88 frame_size = LE_32(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
89 stream_ptr += 6; // skip the magic number
9200edf69d1d initial commit
melanson
parents:
diff changeset
90 num_chunks = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
91 stream_ptr += 10; // skip padding
9200edf69d1d initial commit
melanson
parents:
diff changeset
92
9200edf69d1d initial commit
melanson
parents:
diff changeset
93 // iterate through the chunks
9200edf69d1d initial commit
melanson
parents:
diff changeset
94 frame_size -= 16;
9200edf69d1d initial commit
melanson
parents:
diff changeset
95 while ((frame_size > 0) && (num_chunks > 0))
9200edf69d1d initial commit
melanson
parents:
diff changeset
96 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
97 chunk_size = LE_32(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
98 stream_ptr += 4;
9200edf69d1d initial commit
melanson
parents:
diff changeset
99 chunk_type = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
100 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
101
9200edf69d1d initial commit
melanson
parents:
diff changeset
102 switch (chunk_type)
9200edf69d1d initial commit
melanson
parents:
diff changeset
103 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
104 case FLI_256_COLOR:
9200edf69d1d initial commit
melanson
parents:
diff changeset
105 case FLI_COLOR:
4887
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
106 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
107 if (chunk_type == FLI_COLOR)
9200edf69d1d initial commit
melanson
parents:
diff changeset
108 color_scale = 4;
9200edf69d1d initial commit
melanson
parents:
diff changeset
109 else
9200edf69d1d initial commit
melanson
parents:
diff changeset
110 color_scale = 1;
9200edf69d1d initial commit
melanson
parents:
diff changeset
111 // set up the palette
9200edf69d1d initial commit
melanson
parents:
diff changeset
112 color_packets = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
113 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
114 palette_ptr1 = 0;
9200edf69d1d initial commit
melanson
parents:
diff changeset
115 for (i = 0; i < color_packets; i++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
116 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
117 // first byte is how many colors to skip
9200edf69d1d initial commit
melanson
parents:
diff changeset
118 palette_ptr1 += (encoded[stream_ptr++] * 4);
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
119 // wrap around, for good measure
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
120 if (palette_ptr1 >= PALETTE_SIZE)
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
121 palette_ptr1 = 0;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
122 // next byte indicates how many entries to change
9200edf69d1d initial commit
melanson
parents:
diff changeset
123 color_changes = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
124 // if there are 0 color changes, there are actually 256
9200edf69d1d initial commit
melanson
parents:
diff changeset
125 if (color_changes == 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
126 color_changes = 256;
9200edf69d1d initial commit
melanson
parents:
diff changeset
127 for (j = 0; j < color_changes; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
128 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
129 palette[palette_ptr1++] = encoded[stream_ptr + 2] * color_scale;
9200edf69d1d initial commit
melanson
parents:
diff changeset
130 palette[palette_ptr1++] = encoded[stream_ptr + 1] * color_scale;
9200edf69d1d initial commit
melanson
parents:
diff changeset
131 palette[palette_ptr1++] = encoded[stream_ptr + 0] * color_scale;
9200edf69d1d initial commit
melanson
parents:
diff changeset
132 palette_ptr1++;
9200edf69d1d initial commit
melanson
parents:
diff changeset
133 stream_ptr += 3;
9200edf69d1d initial commit
melanson
parents:
diff changeset
134 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
135 }
4887
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
136
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
137 // color chunks sometimes have weird 16-bit alignment issues;
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
138 // therefore, take the hardline approach and set the stream_ptr
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
139 // to the value calculate w.r.t. the size specified by the color
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
140 // chunk header
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
141 stream_ptr = stream_ptr_after_color_chunk;
6d753f5dde1e fixed another color chunk issue
melanson
parents: 4719
diff changeset
142
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
143 /* Palette has changed, must update frame */
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
144 update_whole_frame = 1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
145 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
146
9200edf69d1d initial commit
melanson
parents:
diff changeset
147 case FLI_DELTA:
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
148 y_ptr = ghost_y_ptr = 0;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
149 compressed_lines = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
150 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
151 while (compressed_lines > 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
152 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
153 line_packets = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
154 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
155 if (line_packets < 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
156 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
157 line_packets = -line_packets;
9200edf69d1d initial commit
melanson
parents:
diff changeset
158 y_ptr += (line_packets * line_inc);
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
159 ghost_y_ptr += (line_packets * width);
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
160 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
161 else
9200edf69d1d initial commit
melanson
parents:
diff changeset
162 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
163 pixel_ptr = y_ptr;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
164 ghost_pixel_ptr = ghost_y_ptr;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
165 for (i = 0; i < line_packets; i++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
166 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
167 // account for the skip bytes
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
168 pixel_skip = encoded[stream_ptr++];
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
169 pixel_ptr += pixel_skip * bytes_per_pixel;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
170 ghost_pixel_ptr += pixel_skip;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
171 byte_run = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
172 if (byte_run < 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
173 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
174 byte_run = -byte_run;
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
175 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
176 palette_ptr2 = (palette_idx2 = encoded[stream_ptr++]) * 4;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
177 for (j = 0; j < byte_run; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
178 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
179 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
180 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
181 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
182 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
183 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
184 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
185
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
186 fli_ghost_image[ghost_pixel_ptr++] = palette_idx2;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
187 decoded[pixel_ptr++] = palette[palette_ptr2 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
188 decoded[pixel_ptr++] = palette[palette_ptr2 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
189 decoded[pixel_ptr++] = palette[palette_ptr2 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
190 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
191 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
192 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
193 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
194 else
9200edf69d1d initial commit
melanson
parents:
diff changeset
195 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
196 for (j = 0; j < byte_run * 2; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
197 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
198 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
199 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
200 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
201 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
202 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
203 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
204 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
205 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
206 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
207 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
208 y_ptr += line_inc;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
209 ghost_y_ptr += width;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
210 compressed_lines--;
9200edf69d1d initial commit
melanson
parents:
diff changeset
211 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
212 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
213 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
214
9200edf69d1d initial commit
melanson
parents:
diff changeset
215 case FLI_LC:
9200edf69d1d initial commit
melanson
parents:
diff changeset
216 // line compressed
9200edf69d1d initial commit
melanson
parents:
diff changeset
217 starting_line = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
218 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
219 y_ptr = starting_line * line_inc;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
220 ghost_y_ptr = starting_line * width;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
221
9200edf69d1d initial commit
melanson
parents:
diff changeset
222 compressed_lines = LE_16(&encoded[stream_ptr]);
9200edf69d1d initial commit
melanson
parents:
diff changeset
223 stream_ptr += 2;
9200edf69d1d initial commit
melanson
parents:
diff changeset
224 while (compressed_lines > 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
225 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
226 pixel_ptr = y_ptr;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
227 ghost_pixel_ptr = ghost_y_ptr;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
228 line_packets = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
229 if (line_packets > 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
230 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
231 for (i = 0; i < line_packets; i++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
232 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
233 // account for the skip bytes
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
234 pixel_skip = encoded[stream_ptr++];
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
235 pixel_ptr += pixel_skip * bytes_per_pixel;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
236 ghost_pixel_ptr += pixel_skip;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
237 byte_run = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
238 if (byte_run > 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
239 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
240 for (j = 0; j < byte_run; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
241 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
242 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
243 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
244 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
245 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
246 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
247 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
248 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
249 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
250 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
251 else
9200edf69d1d initial commit
melanson
parents:
diff changeset
252 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
253 byte_run = -byte_run;
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
254 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
255 for (j = 0; j < byte_run; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
256 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
257 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
258 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
259 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
260 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
261 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
262 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
263 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
264 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
265 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
266 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
267
9200edf69d1d initial commit
melanson
parents:
diff changeset
268 y_ptr += line_inc;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
269 ghost_y_ptr += width;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
270 compressed_lines--;
9200edf69d1d initial commit
melanson
parents:
diff changeset
271 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
272 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
273
9200edf69d1d initial commit
melanson
parents:
diff changeset
274 case FLI_BLACK:
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
275 // set the whole frame to color 0 (which is usually black) by
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
276 // clearing the ghost image and trigger a full frame update
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
277 memset(fli_ghost_image, 0, width * height * sizeof(unsigned char));
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
278 update_whole_frame = 1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
279 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
280
9200edf69d1d initial commit
melanson
parents:
diff changeset
281 case FLI_BRUN:
9200edf69d1d initial commit
melanson
parents:
diff changeset
282 // byte run compression
9200edf69d1d initial commit
melanson
parents:
diff changeset
283 y_ptr = 0;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
284 ghost_y_ptr = 0;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
285 for (lines = 0; lines < height; lines++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
286 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
287 pixel_ptr = y_ptr;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
288 ghost_pixel_ptr = ghost_y_ptr;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
289 line_packets = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
290 for (i = 0; i < line_packets; i++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
291 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
292 byte_run = encoded[stream_ptr++];
9200edf69d1d initial commit
melanson
parents:
diff changeset
293 if (byte_run > 0)
9200edf69d1d initial commit
melanson
parents:
diff changeset
294 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
295 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
296 for (j = 0; j < byte_run; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
297 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
298 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
299 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
300 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
301 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
302 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
303 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
304 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
305 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
306 else // copy bytes if byte_run < 0
9200edf69d1d initial commit
melanson
parents:
diff changeset
307 {
9200edf69d1d initial commit
melanson
parents:
diff changeset
308 byte_run = -byte_run;
9200edf69d1d initial commit
melanson
parents:
diff changeset
309 for (j = 0; j < byte_run; j++)
9200edf69d1d initial commit
melanson
parents:
diff changeset
310 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
311 palette_ptr1 = (palette_idx1 = encoded[stream_ptr++]) * 4;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
312 fli_ghost_image[ghost_pixel_ptr++] = palette_idx1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
313 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
9200edf69d1d initial commit
melanson
parents:
diff changeset
314 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
9200edf69d1d initial commit
melanson
parents:
diff changeset
315 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
3185
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
316 if (bytes_per_pixel == 4) /* 32bpp */
a1205b22a5f4 32bpp support added
alex
parents: 3155
diff changeset
317 pixel_ptr++;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
318 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
319 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
320 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
321
9200edf69d1d initial commit
melanson
parents:
diff changeset
322 y_ptr += line_inc;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
323 ghost_y_ptr += width;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
324 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
325 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
326
9200edf69d1d initial commit
melanson
parents:
diff changeset
327 case FLI_COPY:
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
328 // copy the chunk (uncompressed frame) to the ghost image and
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
329 // schedule the whole frame to be updated
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
330 if (chunk_size - 6 > width * height)
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
331 {
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
332 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
333 "FLI: in chunk FLI_COPY : source data (%d bytes) bigger than image," \
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
334 " skipping chunk\n",
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
335 chunk_size - 6);
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
336 break;
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
337 }
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
338 else
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
339 memcpy(fli_ghost_image, &encoded[stream_ptr], chunk_size - 6);
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
340 stream_ptr += chunk_size - 6;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
341 update_whole_frame = 1;
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
342 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
343
9200edf69d1d initial commit
melanson
parents:
diff changeset
344 case FLI_MINI:
9200edf69d1d initial commit
melanson
parents:
diff changeset
345 // sort of a thumbnail? disregard this chunk...
9200edf69d1d initial commit
melanson
parents:
diff changeset
346 stream_ptr += chunk_size - 6;
9200edf69d1d initial commit
melanson
parents:
diff changeset
347 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
348
9200edf69d1d initial commit
melanson
parents:
diff changeset
349 default:
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
350 mp_msg (MSGT_DECVIDEO, MSGL_WARN,
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
351 "FLI: Unrecognized chunk type: %d\n", chunk_type);
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
352 break;
9200edf69d1d initial commit
melanson
parents:
diff changeset
353 }
9200edf69d1d initial commit
melanson
parents:
diff changeset
354
9200edf69d1d initial commit
melanson
parents:
diff changeset
355 frame_size -= chunk_size;
9200edf69d1d initial commit
melanson
parents:
diff changeset
356 num_chunks--;
9200edf69d1d initial commit
melanson
parents:
diff changeset
357 }
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
358
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
359 if (update_whole_frame)
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
360 {
4692
f78d2b53aa48 a few quick fixes to the FLI decoder
melanson
parents: 4688
diff changeset
361 pixel_ptr = ghost_pixel_ptr = 0;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
362 while (pixel_ptr < (width * height * bytes_per_pixel))
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
363 {
4719
62eb233b12ed shrink the size of the ghost image, courtesy of Roberto Togni
melanson
parents: 4692
diff changeset
364 palette_ptr1 = fli_ghost_image[ghost_pixel_ptr++] * 4;
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
365 decoded[pixel_ptr++] = palette[palette_ptr1 + 0];
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
366 decoded[pixel_ptr++] = palette[palette_ptr1 + 1];
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
367 decoded[pixel_ptr++] = palette[palette_ptr1 + 2];
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
368 if (bytes_per_pixel == 4) /* 32bpp */
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
369 pixel_ptr++;
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
370 }
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
371 }
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
372
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
373 // by the end of the chunk, the stream ptr should equal the frame
4688
68582b4ce881 cosmetic fix
melanson
parents: 4687
diff changeset
374 // size (minus 1, possibly); if it doesn't, issue a warning
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
375 if ((stream_ptr != encoded_size) && (stream_ptr != encoded_size - 1))
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
376 mp_msg(MSGT_DECVIDEO, MSGL_WARN,
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
377 " warning: processed FLI chunk where encoded size = %d\n" \
4688
68582b4ce881 cosmetic fix
melanson
parents: 4687
diff changeset
378 " and final chunk ptr = %d\n",
4687
2973e997c4a5 some obscure bug fixes to the FLI decoder, with many thanks to Roberto
melanson
parents: 3737
diff changeset
379 encoded_size, stream_ptr);
3155
9200edf69d1d initial commit
melanson
parents:
diff changeset
380 }