119be7066ce0bcf28158d020916bb65b1652845e
[libav.git] / libavcodec / dfa.c
1 /*
2 * Chronomaster DFA Video Decoder
3 * Copyright (c) 2011 Konstantin Shishkov
4 * based on work by Vladimir "VAG" Gneushev
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "avcodec.h"
24 #include "bytestream.h"
25 #include "internal.h"
26
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
29
30 typedef struct DfaContext {
31 AVFrame pic;
32
33 uint32_t pal[256];
34 uint8_t *frame_buf;
35 } DfaContext;
36
37 static av_cold int dfa_decode_init(AVCodecContext *avctx)
38 {
39 DfaContext *s = avctx->priv_data;
40 int ret;
41
42 avctx->pix_fmt = AV_PIX_FMT_PAL8;
43
44 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
45 return ret;
46
47 s->frame_buf = av_mallocz(avctx->width * avctx->height);
48 if (!s->frame_buf)
49 return AVERROR(ENOMEM);
50
51 return 0;
52 }
53
54 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
55 {
56 const int size = width * height;
57
58 if (bytestream2_get_buffer(gb, frame, size) != size)
59 return AVERROR_INVALIDDATA;
60 return 0;
61 }
62
63 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
64 {
65 const uint8_t *frame_start = frame;
66 const uint8_t *frame_end = frame + width * height;
67 int mask = 0x10000, bitbuf = 0;
68 int v, count, segments;
69 unsigned offset;
70
71 segments = bytestream2_get_le32(gb);
72 offset = bytestream2_get_le32(gb);
73 if (frame_end - frame <= offset)
74 return AVERROR_INVALIDDATA;
75 frame += offset;
76 while (segments--) {
77 if (bytestream2_get_bytes_left(gb) < 2)
78 return AVERROR_INVALIDDATA;
79 if (mask == 0x10000) {
80 bitbuf = bytestream2_get_le16u(gb);
81 mask = 1;
82 }
83 if (frame_end - frame < 2)
84 return AVERROR_INVALIDDATA;
85 if (bitbuf & mask) {
86 v = bytestream2_get_le16(gb);
87 offset = (v & 0x1FFF) << 1;
88 count = ((v >> 13) + 2) << 1;
89 if (frame - frame_start < offset || frame_end - frame < count)
90 return AVERROR_INVALIDDATA;
91 av_memcpy_backptr(frame, offset, count);
92 frame += count;
93 } else {
94 *frame++ = bytestream2_get_byte(gb);
95 *frame++ = bytestream2_get_byte(gb);
96 }
97 mask <<= 1;
98 }
99
100 return 0;
101 }
102
103 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
104 {
105 const uint8_t *frame_start = frame;
106 const uint8_t *frame_end = frame + width * height;
107 int mask = 0x10000, bitbuf = 0;
108 int v, offset, count, segments;
109
110 segments = bytestream2_get_le16(gb);
111 while (segments--) {
112 if (bytestream2_get_bytes_left(gb) < 2)
113 return AVERROR_INVALIDDATA;
114 if (mask == 0x10000) {
115 bitbuf = bytestream2_get_le16u(gb);
116 mask = 1;
117 }
118 if (frame_end - frame < 2)
119 return AVERROR_INVALIDDATA;
120 if (bitbuf & mask) {
121 v = bytestream2_get_le16(gb);
122 offset = (v & 0x1FFF) << 1;
123 count = ((v >> 13) + 2) << 1;
124 if (frame - frame_start < offset || frame_end - frame < count)
125 return AVERROR_INVALIDDATA;
126 av_memcpy_backptr(frame, offset, count);
127 frame += count;
128 } else if (bitbuf & (mask << 1)) {
129 frame += bytestream2_get_le16(gb);
130 } else {
131 *frame++ = bytestream2_get_byte(gb);
132 *frame++ = bytestream2_get_byte(gb);
133 }
134 mask <<= 2;
135 }
136
137 return 0;
138 }
139
140 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
141 {
142 const uint8_t *frame_start = frame;
143 const uint8_t *frame_end = frame + width * height;
144 int mask = 0x10000, bitbuf = 0;
145 int i, v, offset, count, segments;
146
147 segments = bytestream2_get_le16(gb);
148 while (segments--) {
149 if (bytestream2_get_bytes_left(gb) < 2)
150 return AVERROR_INVALIDDATA;
151 if (mask == 0x10000) {
152 bitbuf = bytestream2_get_le16u(gb);
153 mask = 1;
154 }
155
156 if (bitbuf & mask) {
157 v = bytestream2_get_le16(gb);
158 offset = (v & 0x1FFF) << 2;
159 count = ((v >> 13) + 2) << 1;
160 if (frame - frame_start < offset || frame_end - frame < count*2 + width)
161 return AVERROR_INVALIDDATA;
162 for (i = 0; i < count; i++) {
163 frame[0] = frame[1] =
164 frame[width] = frame[width + 1] = frame[-offset];
165
166 frame += 2;
167 }
168 } else if (bitbuf & (mask << 1)) {
169 v = bytestream2_get_le16(gb)*2;
170 if (frame - frame_end < v)
171 return AVERROR_INVALIDDATA;
172 frame += v;
173 } else {
174 if (frame_end - frame < width + 3)
175 return AVERROR_INVALIDDATA;
176 frame[0] = frame[1] =
177 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
178 frame += 2;
179 frame[0] = frame[1] =
180 frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
181 frame += 2;
182 }
183 mask <<= 2;
184 }
185
186 return 0;
187 }
188
189 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
190 {
191 uint8_t *line_ptr;
192 int count, lines, segments;
193
194 count = bytestream2_get_le16(gb);
195 if (count >= height)
196 return AVERROR_INVALIDDATA;
197 frame += width * count;
198 lines = bytestream2_get_le16(gb);
199 if (count + lines > height)
200 return AVERROR_INVALIDDATA;
201
202 while (lines--) {
203 if (bytestream2_get_bytes_left(gb) < 1)
204 return AVERROR_INVALIDDATA;
205 line_ptr = frame;
206 frame += width;
207 segments = bytestream2_get_byteu(gb);
208 while (segments--) {
209 if (frame - line_ptr <= bytestream2_peek_byte(gb))
210 return AVERROR_INVALIDDATA;
211 line_ptr += bytestream2_get_byte(gb);
212 count = (int8_t)bytestream2_get_byte(gb);
213 if (count >= 0) {
214 if (frame - line_ptr < count)
215 return AVERROR_INVALIDDATA;
216 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
217 return AVERROR_INVALIDDATA;
218 } else {
219 count = -count;
220 if (frame - line_ptr < count)
221 return AVERROR_INVALIDDATA;
222 memset(line_ptr, bytestream2_get_byte(gb), count);
223 }
224 line_ptr += count;
225 }
226 }
227
228 return 0;
229 }
230
231 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
232 {
233 const uint8_t *frame_end = frame + width * height;
234 uint8_t *line_ptr;
235 int count, i, v, lines, segments;
236 int y = 0;
237
238 lines = bytestream2_get_le16(gb);
239 if (lines > height)
240 return AVERROR_INVALIDDATA;
241
242 while (lines--) {
243 if (bytestream2_get_bytes_left(gb) < 2)
244 return AVERROR_INVALIDDATA;
245 segments = bytestream2_get_le16u(gb);
246 while ((segments & 0xC000) == 0xC000) {
247 unsigned skip_lines = -(int16_t)segments;
248 unsigned delta = -((int16_t)segments * width);
249 if (frame_end - frame <= delta || y + lines + skip_lines > height)
250 return AVERROR_INVALIDDATA;
251 frame += delta;
252 y += skip_lines;
253 segments = bytestream2_get_le16(gb);
254 }
255 if (segments & 0x8000) {
256 frame[width - 1] = segments & 0xFF;
257 segments = bytestream2_get_le16(gb);
258 }
259 line_ptr = frame;
260 frame += width;
261 y++;
262 while (segments--) {
263 if (frame - line_ptr <= bytestream2_peek_byte(gb))
264 return AVERROR_INVALIDDATA;
265 line_ptr += bytestream2_get_byte(gb);
266 count = (int8_t)bytestream2_get_byte(gb);
267 if (count >= 0) {
268 if (frame - line_ptr < count * 2)
269 return AVERROR_INVALIDDATA;
270 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
271 return AVERROR_INVALIDDATA;
272 line_ptr += count * 2;
273 } else {
274 count = -count;
275 if (frame - line_ptr < count * 2)
276 return AVERROR_INVALIDDATA;
277 v = bytestream2_get_le16(gb);
278 for (i = 0; i < count; i++)
279 bytestream_put_le16(&line_ptr, v);
280 }
281 }
282 }
283
284 return 0;
285 }
286
287 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
288 {
289 return AVERROR_PATCHWELCOME;
290 }
291
292 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
293 {
294 memset(frame, 0, width * height);
295 return 0;
296 }
297
298
299 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
300
301 static const chunk_decoder decoder[8] = {
302 decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
303 decode_unk6, decode_dsw1, decode_blck, decode_dds1,
304 };
305
306 static const char* chunk_name[8] = {
307 "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
308 };
309
310 static int dfa_decode_frame(AVCodecContext *avctx,
311 void *data, int *got_frame,
312 AVPacket *avpkt)
313 {
314 DfaContext *s = avctx->priv_data;
315 GetByteContext gb;
316 const uint8_t *buf = avpkt->data;
317 uint32_t chunk_type, chunk_size;
318 uint8_t *dst;
319 int ret;
320 int i, pal_elems;
321
322 if (s->pic.data[0])
323 avctx->release_buffer(avctx, &s->pic);
324
325 if ((ret = ff_get_buffer(avctx, &s->pic))) {
326 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
327 return ret;
328 }
329
330 bytestream2_init(&gb, avpkt->data, avpkt->size);
331 while (bytestream2_get_bytes_left(&gb) > 0) {
332 bytestream2_skip(&gb, 4);
333 chunk_size = bytestream2_get_le32(&gb);
334 chunk_type = bytestream2_get_le32(&gb);
335 if (!chunk_type)
336 break;
337 if (chunk_type == 1) {
338 pal_elems = FFMIN(chunk_size / 3, 256);
339 for (i = 0; i < pal_elems; i++) {
340 s->pal[i] = bytestream2_get_be24(&gb) << 2;
341 s->pal[i] |= (s->pal[i] >> 6) & 0x333;
342 }
343 s->pic.palette_has_changed = 1;
344 } else if (chunk_type <= 9) {
345 if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
346 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
347 chunk_name[chunk_type - 2]);
348 return AVERROR_INVALIDDATA;
349 }
350 } else {
351 av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
352 chunk_type);
353 }
354 buf += chunk_size;
355 }
356
357 buf = s->frame_buf;
358 dst = s->pic.data[0];
359 for (i = 0; i < avctx->height; i++) {
360 memcpy(dst, buf, avctx->width);
361 dst += s->pic.linesize[0];
362 buf += avctx->width;
363 }
364 memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
365
366 *got_frame = 1;
367 *(AVFrame*)data = s->pic;
368
369 return avpkt->size;
370 }
371
372 static av_cold int dfa_decode_end(AVCodecContext *avctx)
373 {
374 DfaContext *s = avctx->priv_data;
375
376 if (s->pic.data[0])
377 avctx->release_buffer(avctx, &s->pic);
378
379 av_freep(&s->frame_buf);
380
381 return 0;
382 }
383
384 AVCodec ff_dfa_decoder = {
385 .name = "dfa",
386 .type = AVMEDIA_TYPE_VIDEO,
387 .id = AV_CODEC_ID_DFA,
388 .priv_data_size = sizeof(DfaContext),
389 .init = dfa_decode_init,
390 .close = dfa_decode_end,
391 .decode = dfa_decode_frame,
392 .capabilities = CODEC_CAP_DR1,
393 .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
394 };