dfa.c
Go to the documentation of this file.
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 "internal.h"
25 #include "bytestream.h"
26 
27 #include "libavutil/imgutils.h"
28 #include "libavutil/lzo.h" // for av_memcpy_backptr
29 
30 typedef struct DfaContext {
32 
33  uint32_t pal[256];
34  uint8_t *frame_buf;
35 } DfaContext;
36 
38 {
39  DfaContext *s = avctx->priv_data;
40  int ret;
41 
42  avctx->pix_fmt = 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 + AV_LZO_OUTPUT_PADDING);
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  // can't use av_memcpy_backptr() since it can overwrite following pixels
127  for (v = 0; v < count; v++)
128  frame[v] = frame[v - offset];
129  frame += count;
130  } else if (bitbuf & (mask << 1)) {
131  frame += bytestream2_get_le16(gb);
132  } else {
133  *frame++ = bytestream2_get_byte(gb);
134  *frame++ = bytestream2_get_byte(gb);
135  }
136  mask <<= 2;
137  }
138 
139  return 0;
140 }
141 
142 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
143 {
144  const uint8_t *frame_start = frame;
145  const uint8_t *frame_end = frame + width * height;
146  int mask = 0x10000, bitbuf = 0;
147  int i, v, offset, count, segments;
148 
149  segments = bytestream2_get_le16(gb);
150  while (segments--) {
151  if (bytestream2_get_bytes_left(gb) < 2)
152  return AVERROR_INVALIDDATA;
153  if (mask == 0x10000) {
154  bitbuf = bytestream2_get_le16u(gb);
155  mask = 1;
156  }
157 
158  if (bitbuf & mask) {
159  v = bytestream2_get_le16(gb);
160  offset = (v & 0x1FFF) << 2;
161  count = ((v >> 13) + 2) << 1;
162  if (frame - frame_start < offset || frame_end - frame < count*2 + width)
163  return AVERROR_INVALIDDATA;
164  for (i = 0; i < count; i++) {
165  frame[0] = frame[1] =
166  frame[width] = frame[width + 1] = frame[-offset];
167 
168  frame += 2;
169  }
170  } else if (bitbuf & (mask << 1)) {
171  v = bytestream2_get_le16(gb)*2;
172  if (frame - frame_end < v)
173  return AVERROR_INVALIDDATA;
174  frame += v;
175  } else {
176  if (frame_end - frame < width + 3)
177  return AVERROR_INVALIDDATA;
178  frame[0] = frame[1] =
179  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
180  frame += 2;
181  frame[0] = frame[1] =
182  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
183  frame += 2;
184  }
185  mask <<= 2;
186  }
187 
188  return 0;
189 }
190 
191 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
192 {
193  uint8_t *line_ptr;
194  int count, lines, segments;
195 
196  count = bytestream2_get_le16(gb);
197  if (count >= height)
198  return AVERROR_INVALIDDATA;
199  frame += width * count;
200  lines = bytestream2_get_le16(gb);
201  if (count + lines > height)
202  return AVERROR_INVALIDDATA;
203 
204  while (lines--) {
205  if (bytestream2_get_bytes_left(gb) < 1)
206  return AVERROR_INVALIDDATA;
207  line_ptr = frame;
208  frame += width;
209  segments = bytestream2_get_byteu(gb);
210  while (segments--) {
211  if (frame - line_ptr <= bytestream2_peek_byte(gb))
212  return AVERROR_INVALIDDATA;
213  line_ptr += bytestream2_get_byte(gb);
214  count = (int8_t)bytestream2_get_byte(gb);
215  if (count >= 0) {
216  if (frame - line_ptr < count)
217  return AVERROR_INVALIDDATA;
218  if (bytestream2_get_buffer(gb, line_ptr, count) != count)
219  return AVERROR_INVALIDDATA;
220  } else {
221  count = -count;
222  if (frame - line_ptr < count)
223  return AVERROR_INVALIDDATA;
224  memset(line_ptr, bytestream2_get_byte(gb), count);
225  }
226  line_ptr += count;
227  }
228  }
229 
230  return 0;
231 }
232 
233 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
234 {
235  const uint8_t *frame_end = frame + width * height;
236  uint8_t *line_ptr;
237  int count, i, v, lines, segments;
238  int y = 0;
239 
240  lines = bytestream2_get_le16(gb);
241  if (lines > height)
242  return AVERROR_INVALIDDATA;
243 
244  while (lines--) {
245  if (bytestream2_get_bytes_left(gb) < 2)
246  return AVERROR_INVALIDDATA;
247  segments = bytestream2_get_le16u(gb);
248  while ((segments & 0xC000) == 0xC000) {
249  unsigned skip_lines = -(int16_t)segments;
250  unsigned delta = -((int16_t)segments * width);
251  if (frame_end - frame <= delta || y + lines + skip_lines > height)
252  return AVERROR_INVALIDDATA;
253  frame += delta;
254  y += skip_lines;
255  segments = bytestream2_get_le16(gb);
256  }
257  if (segments & 0x8000) {
258  frame[width - 1] = segments & 0xFF;
259  segments = bytestream2_get_le16(gb);
260  }
261  line_ptr = frame;
262  if (frame_end - frame < width)
263  return AVERROR_INVALIDDATA;
264  frame += width;
265  y++;
266  while (segments--) {
267  if (frame - line_ptr <= bytestream2_peek_byte(gb))
268  return AVERROR_INVALIDDATA;
269  line_ptr += bytestream2_get_byte(gb);
270  count = (int8_t)bytestream2_get_byte(gb);
271  if (count >= 0) {
272  if (frame - line_ptr < count * 2)
273  return AVERROR_INVALIDDATA;
274  if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
275  return AVERROR_INVALIDDATA;
276  line_ptr += count * 2;
277  } else {
278  count = -count;
279  if (frame - line_ptr < count * 2)
280  return AVERROR_INVALIDDATA;
281  v = bytestream2_get_le16(gb);
282  for (i = 0; i < count; i++)
283  bytestream_put_le16(&line_ptr, v);
284  }
285  }
286  }
287 
288  return 0;
289 }
290 
291 static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
292 {
293  return AVERROR_PATCHWELCOME;
294 }
295 
296 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
297 {
298  memset(frame, 0, width * height);
299  return 0;
300 }
301 
302 
303 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
304 
305 static const chunk_decoder decoder[8] = {
308 };
309 
310 static const char* chunk_name[8] = {
311  "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
312 };
313 
315  void *data, int *data_size,
316  AVPacket *avpkt)
317 {
318  DfaContext *s = avctx->priv_data;
319  GetByteContext gb;
320  const uint8_t *buf = avpkt->data;
321  uint32_t chunk_type, chunk_size;
322  uint8_t *dst;
323  int ret;
324  int i, pal_elems;
325 
326  if (s->pic.data[0])
327  avctx->release_buffer(avctx, &s->pic);
328 
329  if ((ret = ff_get_buffer(avctx, &s->pic))) {
330  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
331  return ret;
332  }
333 
334  bytestream2_init(&gb, avpkt->data, avpkt->size);
335  while (bytestream2_get_bytes_left(&gb) > 0) {
336  bytestream2_skip(&gb, 4);
337  chunk_size = bytestream2_get_le32(&gb);
338  chunk_type = bytestream2_get_le32(&gb);
339  if (!chunk_type)
340  break;
341  if (chunk_type == 1) {
342  pal_elems = FFMIN(chunk_size / 3, 256);
343  for (i = 0; i < pal_elems; i++) {
344  s->pal[i] = bytestream2_get_be24(&gb) << 2;
345  s->pal[i] |= (s->pal[i] >> 6) & 0x333;
346  }
347  s->pic.palette_has_changed = 1;
348  } else if (chunk_type <= 9) {
349  if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
350  av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
351  chunk_name[chunk_type - 2]);
352  return AVERROR_INVALIDDATA;
353  }
354  } else {
355  av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
356  chunk_type);
357  }
358  buf += chunk_size;
359  }
360 
361  buf = s->frame_buf;
362  dst = s->pic.data[0];
363  for (i = 0; i < avctx->height; i++) {
364  memcpy(dst, buf, avctx->width);
365  dst += s->pic.linesize[0];
366  buf += avctx->width;
367  }
368  memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
369 
370  *data_size = sizeof(AVFrame);
371  *(AVFrame*)data = s->pic;
372 
373  return avpkt->size;
374 }
375 
377 {
378  DfaContext *s = avctx->priv_data;
379 
380  if (s->pic.data[0])
381  avctx->release_buffer(avctx, &s->pic);
382 
383  av_freep(&s->frame_buf);
384 
385  return 0;
386 }
387 
389  .name = "dfa",
390  .type = AVMEDIA_TYPE_VIDEO,
391  .id = CODEC_ID_DFA,
392  .priv_data_size = sizeof(DfaContext),
396  .capabilities = CODEC_CAP_DR1,
397  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
398 };