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 "libavutil/intreadwrite.h"
25 #include "bytestream.h"
26 #include "libavutil/lzo.h" // for av_memcpy_backptr
27 
28 typedef struct DfaContext {
30 
31  uint32_t pal[256];
32  uint8_t *frame_buf;
33 } DfaContext;
34 
36 {
37  DfaContext *s = avctx->priv_data;
38 
39  avctx->pix_fmt = PIX_FMT_PAL8;
40 
41  s->frame_buf = av_mallocz(avctx->width * avctx->height + AV_LZO_OUTPUT_PADDING);
42  if (!s->frame_buf)
43  return AVERROR(ENOMEM);
44 
45  return 0;
46 }
47 
48 static int decode_copy(uint8_t *frame, int width, int height,
49  const uint8_t *src, const uint8_t *src_end)
50 {
51  const int size = width * height;
52 
53  if (src_end - src < size)
54  return -1;
55  bytestream_get_buffer(&src, frame, size);
56  return 0;
57 }
58 
59 static int decode_tsw1(uint8_t *frame, int width, int height,
60  const uint8_t *src, const uint8_t *src_end)
61 {
62  const uint8_t *frame_start = frame;
63  const uint8_t *frame_end = frame + width * height;
64  int mask = 0x10000, bitbuf = 0;
65  int v, count, segments;
66  unsigned offset;
67 
68  segments = bytestream_get_le32(&src);
69  offset = bytestream_get_le32(&src);
70  if (frame_end - frame <= offset)
71  return -1;
72  frame += offset;
73  while (segments--) {
74  if (mask == 0x10000) {
75  if (src >= src_end)
76  return -1;
77  bitbuf = bytestream_get_le16(&src);
78  mask = 1;
79  }
80  if (src_end - src < 2 || frame_end - frame < 2)
81  return -1;
82  if (bitbuf & mask) {
83  v = bytestream_get_le16(&src);
84  offset = (v & 0x1FFF) << 1;
85  count = ((v >> 13) + 2) << 1;
86  if (frame - frame_start < offset || frame_end - frame < count)
87  return -1;
88  av_memcpy_backptr(frame, offset, count);
89  frame += count;
90  } else {
91  *frame++ = *src++;
92  *frame++ = *src++;
93  }
94  mask <<= 1;
95  }
96 
97  return 0;
98 }
99 
100 static int decode_dsw1(uint8_t *frame, int width, int height,
101  const uint8_t *src, const uint8_t *src_end)
102 {
103  const uint8_t *frame_start = frame;
104  const uint8_t *frame_end = frame + width * height;
105  int mask = 0x10000, bitbuf = 0;
106  int v, offset, count, segments;
107 
108  segments = bytestream_get_le16(&src);
109  while (segments--) {
110  if (mask == 0x10000) {
111  if (src >= src_end)
112  return -1;
113  bitbuf = bytestream_get_le16(&src);
114  mask = 1;
115  }
116  if (src_end - src < 2 || frame_end - frame < 2)
117  return -1;
118  if (bitbuf & mask) {
119  v = bytestream_get_le16(&src);
120  offset = (v & 0x1FFF) << 1;
121  count = ((v >> 13) + 2) << 1;
122  if (frame - frame_start < offset || frame_end - frame < count)
123  return -1;
124  // can't use av_memcpy_backptr() since it can overwrite following pixels
125  for (v = 0; v < count; v++)
126  frame[v] = frame[v - offset];
127  frame += count;
128  } else if (bitbuf & (mask << 1)) {
129  frame += bytestream_get_le16(&src);
130  } else {
131  *frame++ = *src++;
132  *frame++ = *src++;
133  }
134  mask <<= 2;
135  }
136 
137  return 0;
138 }
139 
140 static int decode_dds1(uint8_t *frame, int width, int height,
141  const uint8_t *src, const uint8_t *src_end)
142 {
143  const uint8_t *frame_start = frame;
144  const uint8_t *frame_end = frame + width * height;
145  int mask = 0x10000, bitbuf = 0;
146  int i, v, offset, count, segments;
147 
148  segments = bytestream_get_le16(&src);
149  while (segments--) {
150  if (mask == 0x10000) {
151  if (src >= src_end)
152  return -1;
153  bitbuf = bytestream_get_le16(&src);
154  mask = 1;
155  }
156  if (src_end - src < 2 || frame_end - frame < 2)
157  return -1;
158  if (bitbuf & mask) {
159  v = bytestream_get_le16(&src);
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 -1;
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  frame += bytestream_get_le16(&src) * 2;
172  } else {
173  frame[0] = frame[1] =
174  frame[width] = frame[width + 1] = *src++;
175  frame += 2;
176  frame[0] = frame[1] =
177  frame[width] = frame[width + 1] = *src++;
178  frame += 2;
179  }
180  mask <<= 2;
181  }
182 
183  return 0;
184 }
185 
186 static int decode_bdlt(uint8_t *frame, int width, int height,
187  const uint8_t *src, const uint8_t *src_end)
188 {
189  uint8_t *line_ptr;
190  int count, lines, segments;
191 
192  count = bytestream_get_le16(&src);
193  if (count >= height)
194  return -1;
195  frame += width * count;
196  lines = bytestream_get_le16(&src);
197  if (count + lines > height || src >= src_end)
198  return -1;
199 
200  while (lines--) {
201  line_ptr = frame;
202  frame += width;
203  segments = *src++;
204  while (segments--) {
205  if (src_end - src < 3)
206  return -1;
207  if (frame - line_ptr <= *src)
208  return -1;
209  line_ptr += *src++;
210  count = (int8_t)*src++;
211  if (count >= 0) {
212  if (frame - line_ptr < count || src_end - src < count)
213  return -1;
214  bytestream_get_buffer(&src, line_ptr, count);
215  } else {
216  count = -count;
217  if (frame - line_ptr < count || src >= src_end)
218  return -1;
219  memset(line_ptr, *src++, count);
220  }
221  line_ptr += count;
222  }
223  }
224 
225  return 0;
226 }
227 
228 static int decode_wdlt(uint8_t *frame, int width, int height,
229  const uint8_t *src, const uint8_t *src_end)
230 {
231  const uint8_t *frame_end = frame + width * height;
232  uint8_t *line_ptr;
233  int count, i, v, lines, segments;
234 
235  lines = bytestream_get_le16(&src);
236  if (lines > height || src >= src_end)
237  return -1;
238 
239  while (lines--) {
240  segments = bytestream_get_le16(&src);
241  while ((segments & 0xC000) == 0xC000) {
242  unsigned delta = -((int16_t)segments * width);
243  if (frame_end - frame <= delta)
244  return -1;
245  frame += delta;
246  segments = bytestream_get_le16(&src);
247  }
248  if (segments & 0x8000) {
249  frame[width - 1] = segments & 0xFF;
250  segments = bytestream_get_le16(&src);
251  }
252  line_ptr = frame;
253  frame += width;
254  while (segments--) {
255  if (src_end - src < 2)
256  return -1;
257  if (frame - line_ptr <= *src)
258  return -1;
259  line_ptr += *src++;
260  count = (int8_t)*src++;
261  if (count >= 0) {
262  if (frame - line_ptr < count*2 || src_end - src < count*2)
263  return -1;
264  bytestream_get_buffer(&src, line_ptr, count*2);
265  line_ptr += count * 2;
266  } else {
267  count = -count;
268  if (frame - line_ptr < count*2 || src_end - src < 2)
269  return -1;
270  v = bytestream_get_le16(&src);
271  for (i = 0; i < count; i++)
272  bytestream_put_le16(&line_ptr, v);
273  }
274  }
275  }
276 
277  return 0;
278 }
279 
280 static int decode_unk6(uint8_t *frame, int width, int height,
281  const uint8_t *src, const uint8_t *src_end)
282 {
283  return -1;
284 }
285 
286 static int decode_blck(uint8_t *frame, int width, int height,
287  const uint8_t *src, const uint8_t *src_end)
288 {
289  memset(frame, 0, width * height);
290  return 0;
291 }
292 
293 
294 typedef int (*chunk_decoder)(uint8_t *frame, int width, int height,
295  const uint8_t *src, const uint8_t *src_end);
296 
297 static const chunk_decoder decoder[8] = {
300 };
301 
302 static const char* chunk_name[8] = {
303  "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
304 };
305 
307  void *data, int *data_size,
308  AVPacket *avpkt)
309 {
310  DfaContext *s = avctx->priv_data;
311  const uint8_t *buf = avpkt->data;
312  const uint8_t *buf_end = avpkt->data + avpkt->size;
313  const uint8_t *tmp_buf;
314  uint32_t chunk_type, chunk_size;
315  uint8_t *dst;
316  int ret;
317  int i, pal_elems;
318 
319  if (s->pic.data[0])
320  avctx->release_buffer(avctx, &s->pic);
321 
322  if ((ret = avctx->get_buffer(avctx, &s->pic))) {
323  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
324  return ret;
325  }
326 
327  while (buf < buf_end) {
328  chunk_size = AV_RL32(buf + 4);
329  chunk_type = AV_RL32(buf + 8);
330  buf += 12;
331  if (buf_end - buf < chunk_size) {
332  av_log(avctx, AV_LOG_ERROR, "Chunk size is too big (%d bytes)\n", chunk_size);
333  return -1;
334  }
335  if (!chunk_type)
336  break;
337  if (chunk_type == 1) {
338  pal_elems = FFMIN(chunk_size / 3, 256);
339  tmp_buf = buf;
340  for (i = 0; i < pal_elems; i++) {
341  s->pal[i] = bytestream_get_be24(&tmp_buf) << 2;
342  s->pal[i] |= (s->pal[i] >> 6) & 0x333;
343  }
344  s->pic.palette_has_changed = 1;
345  } else if (chunk_type <= 9) {
346  if (decoder[chunk_type - 2](s->frame_buf, avctx->width, avctx->height,
347  buf, buf + chunk_size)) {
348  av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
349  chunk_name[chunk_type - 2]);
350  return -1;
351  }
352  } else {
353  av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
354  chunk_type);
355  }
356  buf += chunk_size;
357  }
358 
359  buf = s->frame_buf;
360  dst = s->pic.data[0];
361  for (i = 0; i < avctx->height; i++) {
362  memcpy(dst, buf, avctx->width);
363  dst += s->pic.linesize[0];
364  buf += avctx->width;
365  }
366  memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
367 
368  *data_size = sizeof(AVFrame);
369  *(AVFrame*)data = s->pic;
370 
371  return avpkt->size;
372 }
373 
375 {
376  DfaContext *s = avctx->priv_data;
377 
378  if (s->pic.data[0])
379  avctx->release_buffer(avctx, &s->pic);
380 
381  av_freep(&s->frame_buf);
382 
383  return 0;
384 }
385 
387  .name = "dfa",
388  .type = AVMEDIA_TYPE_VIDEO,
389  .id = CODEC_ID_DFA,
390  .priv_data_size = sizeof(DfaContext),
394  .capabilities = CODEC_CAP_DR1,
395  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
396 };