annotate flicvideo.c @ 1731:c3579468d42c libavcodec

memleak fix
author michael
date Thu, 08 Jan 2004 18:24:35 +0000
parents 46dee16488ae
children e73a3b86565f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1624
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
1 /*
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
2 * FLI/FLC Animation Video Decoder
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
3 * Copyright (C) 2003 the ffmpeg project
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
4 *
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
9 *
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
13 * Lesser General Public License for more details.
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
14 *
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
18 *
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
19 */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
20
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
21 /**
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
22 * @file flic.c
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
23 * Autodesk Animator FLI/FLC Video Decoder
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
24 * by Mike Melanson (melanson@pcisys.net)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
25 * for more information on the .fli/.flc file format and all of its many
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
26 * variations, visit:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
27 * http://www.compuphase.com/flic.htm
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
28 *
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
29 * This decoder outputs PAL8 colorspace data. To use this decoder, be
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
30 * sure that your demuxer sends the FLI file header to the decoder via
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
31 * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
32 * large. The only exception is for FLI files from the game "Magic Carpet",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
33 * in which the header is only 12 bytes.
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
34 */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
35
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
36 #include <stdio.h>
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
37 #include <stdlib.h>
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
38 #include <string.h>
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
39 #include <unistd.h>
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
40
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
41 #include "common.h"
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
42 #include "avcodec.h"
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
43 #include "bswap.h"
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
44
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
45 #define FLI_256_COLOR 4
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
46 #define FLI_DELTA 7
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
47 #define FLI_COLOR 11
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
48 #define FLI_LC 12
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
49 #define FLI_BLACK 13
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
50 #define FLI_BRUN 15
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
51 #define FLI_COPY 16
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
52 #define FLI_MINI 18
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
53
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
54 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
55 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
56 (((uint8_t*)(x))[2] << 16) | \
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
57 (((uint8_t*)(x))[1] << 8) | \
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
58 ((uint8_t*)(x))[0])
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
59
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
60 typedef struct FlicDecodeContext {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
61 AVCodecContext *avctx;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
62 AVFrame frame;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
63 AVFrame prev_frame;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
64
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
65 unsigned int palette[256];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
66 int new_palette;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
67 int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
68 } FlicDecodeContext;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
69
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
70 static int flic_decode_init(AVCodecContext *avctx)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
71 {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
72 FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
73 unsigned char *fli_header = (unsigned char *)avctx->extradata;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
74
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
75 s->avctx = avctx;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
76 avctx->pix_fmt = PIX_FMT_PAL8;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
77 avctx->has_b_frames = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
78
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
79 if (s->avctx->extradata_size == 12) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
80 /* special case for magic carpet FLIs */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
81 s->fli_type = 0xAF13;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
82 } else if (s->avctx->extradata_size == 128) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
83 s->fli_type = LE_16(&fli_header[4]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
84 } else {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
85 printf (" FLI video: expected extradata of 12 or 128 bytes\n");
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
86 return -1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
87 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
88
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
89 s->frame.data[0] = s->prev_frame.data[0] = NULL;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
90 s->new_palette = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
91
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
92 return 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
93 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
94
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
95 static int flic_decode_frame(AVCodecContext *avctx,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
96 void *data, int *data_size,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
97 uint8_t *buf, int buf_size)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
98 {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
99 FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
100
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
101 int stream_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
102 int stream_ptr_after_color_chunk;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
103 int pixel_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
104 int palette_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
105 unsigned char palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
106 unsigned char palette_idx2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
107
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
108 unsigned int frame_size;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
109 int num_chunks;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
110
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
111 unsigned int chunk_size;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
112 int chunk_type;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
113
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
114 int i, j;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
115
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
116 int color_packets;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
117 int color_changes;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
118 int color_shift;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
119 unsigned char r, g, b;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
120
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
121 int lines;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
122 int compressed_lines;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
123 int starting_line;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
124 signed short line_packets;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
125 int y_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
126 signed char byte_run;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
127 int pixel_skip;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
128 int pixel_countdown;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
129 int height_countdown;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
130 unsigned char *pixels;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
131 unsigned char *prev_pixels;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
132
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
133 s->frame.reference = 1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
134 if (avctx->get_buffer(avctx, &s->frame) < 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
135 fprintf(stderr, " FLI: get_buffer() failed\n");
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
136 return -1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
137 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
138
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
139 pixels = s->frame.data[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
140 prev_pixels = s->prev_frame.data[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
141
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
142 frame_size = LE_32(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
143 stream_ptr += 6; /* skip the magic number */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
144 num_chunks = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
145 stream_ptr += 10; /* skip padding */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
146
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
147 frame_size -= 16;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
148
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
149 /* if there is no data, copy previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
150 if (frame_size == 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
151 memcpy(pixels, prev_pixels, s->frame.linesize[0] * s->avctx->height);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
152 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
153
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
154 /* iterate through the chunks */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
155 while ((frame_size > 0) && (num_chunks > 0)) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
156 chunk_size = LE_32(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
157 stream_ptr += 4;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
158 chunk_type = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
159 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
160
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
161 switch (chunk_type) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
162 case FLI_256_COLOR:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
163 case FLI_COLOR:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
164 stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
165 s->new_palette = 1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
166
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
167 /* check special case: If this file is from the Magic Carpet
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
168 * game and uses 6-bit colors even though it reports 256-color
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
169 * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
170 * initialization) */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
171 if ((chunk_type == FLI_256_COLOR) && (s->fli_type != 0xAF13))
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
172 color_shift = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
173 else
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
174 color_shift = 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
175 /* set up the palette */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
176 color_packets = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
177 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
178 palette_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
179 for (i = 0; i < color_packets; i++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
180 /* first byte is how many colors to skip */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
181 palette_ptr += buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
182
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
183 /* next byte indicates how many entries to change */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
184 color_changes = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
185
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
186 /* if there are 0 color changes, there are actually 256 */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
187 if (color_changes == 0)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
188 color_changes = 256;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
189
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
190 for (j = 0; j < color_changes; j++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
191
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
192 /* wrap around, for good measure */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
193 if (palette_ptr >= 256)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
194 palette_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
195
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
196 r = buf[stream_ptr++] << color_shift;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
197 g = buf[stream_ptr++] << color_shift;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
198 b = buf[stream_ptr++] << color_shift;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
199 s->palette[palette_ptr++] = (r << 16) | (g << 8) | b;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
200 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
201 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
202
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
203 /* color chunks sometimes have weird 16-bit alignment issues;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
204 * therefore, take the hardline approach and set the stream_ptr
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
205 * to the value calculated w.r.t. the size specified by the color
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
206 * chunk header */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
207 stream_ptr = stream_ptr_after_color_chunk;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
208
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
209 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
210
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
211 case FLI_DELTA:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
212 y_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
213 height_countdown = s->avctx->height;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
214 compressed_lines = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
215 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
216 while (compressed_lines > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
217 line_packets = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
218 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
219 if (line_packets < 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
220 line_packets = -line_packets;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
221 /* height_countdown was already decremented once on
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
222 * this iteration */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
223 height_countdown -= line_packets;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
224 /* copy the skipped lines from the previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
225 while (line_packets--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
226 memcpy(&pixels[y_ptr], &prev_pixels[y_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
227 s->avctx->width);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
228 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
229 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
230 } else {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
231 height_countdown--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
232 compressed_lines--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
233 pixel_ptr = y_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
234 pixel_countdown = s->avctx->width;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
235 for (i = 0; i < line_packets; i++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
236 /* account for the skip bytes */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
237 pixel_skip = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
238 memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
239 pixel_skip);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
240 pixel_ptr += pixel_skip;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
241 pixel_countdown -= pixel_skip;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
242 byte_run = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
243 if (byte_run < 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
244 byte_run = -byte_run;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
245 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
246 palette_idx2 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
247 for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
248 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
249 pixels[pixel_ptr++] = palette_idx2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
250 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
251 } else {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
252 for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
253 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
254 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
255 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
256 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
257 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
258
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
259 /* copy the remaining pixels in the line from the
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
260 * previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
261 memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
262 pixel_countdown);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
263
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
264 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
265 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
266 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
267
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
268 /* copy the remainder of the lines from the previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
269 while (height_countdown--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
270 memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
271 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
272 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
273 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
274
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
275 case FLI_LC:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
276 /* line compressed */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
277 height_countdown = s->avctx->height;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
278 starting_line = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
279 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
280
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
281 /* copy from the previous frame all of the lines prior to the
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
282 * starting line */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
283 y_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
284 height_countdown -= starting_line;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
285 while (starting_line--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
286 memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
287 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
288 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
289
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
290 compressed_lines = LE_16(&buf[stream_ptr]);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
291 stream_ptr += 2;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
292 while (compressed_lines > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
293 pixel_ptr = y_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
294 pixel_countdown = s->avctx->width;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
295 line_packets = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
296 if (line_packets > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
297 for (i = 0; i < line_packets; i++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
298 /* account for the skip bytes */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
299 pixel_skip = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
300 memcpy(&pixels[pixel_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
301 &prev_pixels[pixel_ptr], pixel_skip);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
302 pixel_ptr += pixel_skip;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
303 pixel_countdown -= pixel_skip;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
304 byte_run = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
305 if (byte_run > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
306 for (j = 0; j < byte_run; j++, pixel_countdown--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
307 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
308 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
309 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
310 } else {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
311 byte_run = -byte_run;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
312 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
313 for (j = 0; j < byte_run; j++, pixel_countdown--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
314 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
315 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
316 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
317 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
318 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
319
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
320 /* copy the remainder of the line from the previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
321 memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
322 pixel_countdown);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
323
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
324 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
325 compressed_lines--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
326 height_countdown--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
327 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
328
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
329 /* copy the remainder of the lines from the previous frame */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
330 while (height_countdown--) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
331 memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
332 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
333 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
334 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
335
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
336 case FLI_BLACK:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
337 /* set the whole frame to color 0 (which is usually black) */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
338 memset(pixels, 0,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
339 s->frame.linesize[0] * s->avctx->height);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
340 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
341
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
342 case FLI_BRUN:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
343 /* Byte run compression: This chunk type only occurs in the first
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
344 * FLI frame and it will update the entire frame. */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
345 y_ptr = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
346 for (lines = 0; lines < s->avctx->height; lines++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
347 pixel_ptr = y_ptr;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
348 /* disregard the line packets; instead, iterate through all
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
349 * pixels on a row */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
350 stream_ptr++;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
351 pixel_countdown = s->avctx->width;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
352 while (pixel_countdown > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
353 byte_run = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
354 if (byte_run > 0) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
355 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
356 for (j = 0; j < byte_run; j++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
357 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
358 pixel_countdown--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
359 if (pixel_countdown < 0)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
360 printf ("fli warning: pixel_countdown < 0 (%d)\n",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
361 pixel_countdown);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
362 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
363 } else { /* copy bytes if byte_run < 0 */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
364 byte_run = -byte_run;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
365 for (j = 0; j < byte_run; j++) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
366 palette_idx1 = buf[stream_ptr++];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
367 pixels[pixel_ptr++] = palette_idx1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
368 pixel_countdown--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
369 if (pixel_countdown < 0)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
370 printf ("fli warning: pixel_countdown < 0 (%d)\n",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
371 pixel_countdown);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
372 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
373 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
374 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
375
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
376 y_ptr += s->frame.linesize[0];
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
377 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
378 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
379
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
380 case FLI_COPY:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
381 /* copy the chunk (uncompressed frame) to the ghost image and
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
382 * schedule the whole frame to be updated */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
383 if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
384 printf(
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
385 "FLI: in chunk FLI_COPY : source data (%d bytes) bigger than" \
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
386 " image, skipping chunk\n",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
387 chunk_size - 6);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
388 stream_ptr += chunk_size - 6;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
389 } else {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
390 for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
391 y_ptr += s->frame.linesize[0]) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
392 memcpy(&pixels[y_ptr], &buf[stream_ptr],
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
393 s->avctx->width);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
394 stream_ptr += s->avctx->width;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
395 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
396 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
397 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
398
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
399 case FLI_MINI:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
400 /* some sort of a thumbnail? disregard this chunk... */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
401 stream_ptr += chunk_size - 6;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
402 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
403
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
404 default:
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
405 printf ("FLI: Unrecognized chunk type: %d\n", chunk_type);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
406 break;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
407 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
408
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
409 frame_size -= chunk_size;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
410 num_chunks--;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
411 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
412
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
413 /* by the end of the chunk, the stream ptr should equal the frame
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
414 * size (minus 1, possibly); if it doesn't, issue a warning */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
415 if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
416 printf (" warning: processed FLI chunk where chunk size = %d\n" \
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
417 " and final chunk ptr = %d\n",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
418 buf_size, stream_ptr);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
419
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
420 /* make the palette available on the way out */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
421 // if (s->new_palette) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
422 if (1) {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
423 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
424 s->frame.palette_has_changed = 1;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
425 s->new_palette = 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
426 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
427
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
428 if (s->prev_frame.data[0])
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
429 avctx->release_buffer(avctx, &s->prev_frame);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
430
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
431 /* shuffle frames */
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
432 s->prev_frame = s->frame;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
433
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
434 *data_size=sizeof(AVFrame);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
435 *(AVFrame*)data = s->frame;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
436
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
437 return buf_size;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
438 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
439
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
440 static int flic_decode_end(AVCodecContext *avctx)
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
441 {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
442 FlicDecodeContext *s = avctx->priv_data;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
443
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
444 if (s->prev_frame.data[0])
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
445 avctx->release_buffer(avctx, &s->prev_frame);
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
446
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
447 return 0;
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
448 }
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
449
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
450 AVCodec flic_decoder = {
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
451 "flic",
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
452 CODEC_TYPE_VIDEO,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
453 CODEC_ID_FLIC,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
454 sizeof(FlicDecodeContext),
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
455 flic_decode_init,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
456 NULL,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
457 flic_decode_end,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
458 flic_decode_frame,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
459 CODEC_CAP_DR1,
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
460 NULL
46dee16488ae yep, FLI support
melanson
parents:
diff changeset
461 };