Libav
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 "bytestream.h"
25 #include "internal.h"
26 
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
29 
30 typedef struct DfaContext {
31  uint32_t pal[256];
33 } DfaContext;
34 
36 {
37  DfaContext *s = avctx->priv_data;
38  int ret;
39 
40  avctx->pix_fmt = AV_PIX_FMT_PAL8;
41 
42  if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
43  return ret;
44 
45  s->frame_buf = av_mallocz(avctx->width * avctx->height);
46  if (!s->frame_buf)
47  return AVERROR(ENOMEM);
48 
49  return 0;
50 }
51 
52 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
53 {
54  const int size = width * height;
55 
56  if (bytestream2_get_buffer(gb, frame, size) != size)
57  return AVERROR_INVALIDDATA;
58  return 0;
59 }
60 
61 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
62 {
63  const uint8_t *frame_start = frame;
64  const uint8_t *frame_end = frame + width * height;
65  int mask = 0x10000, bitbuf = 0;
66  int v, count, segments;
67  unsigned offset;
68 
69  segments = bytestream2_get_le32(gb);
70  offset = bytestream2_get_le32(gb);
71  if (frame_end - frame <= offset)
72  return AVERROR_INVALIDDATA;
73  frame += offset;
74  while (segments--) {
75  if (bytestream2_get_bytes_left(gb) < 2)
76  return AVERROR_INVALIDDATA;
77  if (mask == 0x10000) {
78  bitbuf = bytestream2_get_le16u(gb);
79  mask = 1;
80  }
81  if (frame_end - frame < 2)
82  return AVERROR_INVALIDDATA;
83  if (bitbuf & mask) {
84  v = bytestream2_get_le16(gb);
85  offset = (v & 0x1FFF) << 1;
86  count = ((v >> 13) + 2) << 1;
87  if (frame - frame_start < offset || frame_end - frame < count)
88  return AVERROR_INVALIDDATA;
89  av_memcpy_backptr(frame, offset, count);
90  frame += count;
91  } else {
92  *frame++ = bytestream2_get_byte(gb);
93  *frame++ = bytestream2_get_byte(gb);
94  }
95  mask <<= 1;
96  }
97 
98  return 0;
99 }
100 
101 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
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 = bytestream2_get_le16(gb);
109  while (segments--) {
110  if (bytestream2_get_bytes_left(gb) < 2)
111  return AVERROR_INVALIDDATA;
112  if (mask == 0x10000) {
113  bitbuf = bytestream2_get_le16u(gb);
114  mask = 1;
115  }
116  if (frame_end - frame < 2)
117  return AVERROR_INVALIDDATA;
118  if (bitbuf & mask) {
119  v = bytestream2_get_le16(gb);
120  offset = (v & 0x1FFF) << 1;
121  count = ((v >> 13) + 2) << 1;
122  if (frame - frame_start < offset || frame_end - frame < count)
123  return AVERROR_INVALIDDATA;
124  av_memcpy_backptr(frame, offset, count);
125  frame += count;
126  } else if (bitbuf & (mask << 1)) {
127  frame += bytestream2_get_le16(gb);
128  } else {
129  *frame++ = bytestream2_get_byte(gb);
130  *frame++ = bytestream2_get_byte(gb);
131  }
132  mask <<= 2;
133  }
134 
135  return 0;
136 }
137 
138 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
139 {
140  const uint8_t *frame_start = frame;
141  const uint8_t *frame_end = frame + width * height;
142  int mask = 0x10000, bitbuf = 0;
143  int i, v, offset, count, segments;
144 
145  segments = bytestream2_get_le16(gb);
146  while (segments--) {
147  if (bytestream2_get_bytes_left(gb) < 2)
148  return AVERROR_INVALIDDATA;
149  if (mask == 0x10000) {
150  bitbuf = bytestream2_get_le16u(gb);
151  mask = 1;
152  }
153 
154  if (bitbuf & mask) {
155  v = bytestream2_get_le16(gb);
156  offset = (v & 0x1FFF) << 2;
157  count = ((v >> 13) + 2) << 1;
158  if (frame - frame_start < offset || frame_end - frame < count*2 + width)
159  return AVERROR_INVALIDDATA;
160  for (i = 0; i < count; i++) {
161  frame[0] = frame[1] =
162  frame[width] = frame[width + 1] = frame[-offset];
163 
164  frame += 2;
165  }
166  } else if (bitbuf & (mask << 1)) {
167  v = bytestream2_get_le16(gb)*2;
168  if (frame - frame_end < v)
169  return AVERROR_INVALIDDATA;
170  frame += v;
171  } else {
172  if (frame_end - frame < width + 3)
173  return AVERROR_INVALIDDATA;
174  frame[0] = frame[1] =
175  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
176  frame += 2;
177  frame[0] = frame[1] =
178  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
179  frame += 2;
180  }
181  mask <<= 2;
182  }
183 
184  return 0;
185 }
186 
187 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
188 {
189  uint8_t *line_ptr;
190  int count, lines, segments;
191 
192  count = bytestream2_get_le16(gb);
193  if (count >= height)
194  return AVERROR_INVALIDDATA;
195  frame += width * count;
196  lines = bytestream2_get_le16(gb);
197  if (count + lines > height)
198  return AVERROR_INVALIDDATA;
199 
200  while (lines--) {
201  if (bytestream2_get_bytes_left(gb) < 1)
202  return AVERROR_INVALIDDATA;
203  line_ptr = frame;
204  frame += width;
205  segments = bytestream2_get_byteu(gb);
206  while (segments--) {
207  if (frame - line_ptr <= bytestream2_peek_byte(gb))
208  return AVERROR_INVALIDDATA;
209  line_ptr += bytestream2_get_byte(gb);
210  count = (int8_t)bytestream2_get_byte(gb);
211  if (count >= 0) {
212  if (frame - line_ptr < count)
213  return AVERROR_INVALIDDATA;
214  if (bytestream2_get_buffer(gb, line_ptr, count) != count)
215  return AVERROR_INVALIDDATA;
216  } else {
217  count = -count;
218  if (frame - line_ptr < count)
219  return AVERROR_INVALIDDATA;
220  memset(line_ptr, bytestream2_get_byte(gb), count);
221  }
222  line_ptr += count;
223  }
224  }
225 
226  return 0;
227 }
228 
229 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
230 {
231  const uint8_t *frame_end = frame + width * height;
232  uint8_t *line_ptr;
233  int count, i, v, lines, segments;
234  int y = 0;
235 
236  lines = bytestream2_get_le16(gb);
237  if (lines > height)
238  return AVERROR_INVALIDDATA;
239 
240  while (lines--) {
241  if (bytestream2_get_bytes_left(gb) < 2)
242  return AVERROR_INVALIDDATA;
243  segments = bytestream2_get_le16u(gb);
244  while ((segments & 0xC000) == 0xC000) {
245  unsigned skip_lines = -(int16_t)segments;
246  unsigned delta = -((int16_t)segments * width);
247  if (frame_end - frame <= delta || y + lines + skip_lines > height)
248  return AVERROR_INVALIDDATA;
249  frame += delta;
250  y += skip_lines;
251  segments = bytestream2_get_le16(gb);
252  }
253  if (segments & 0x8000) {
254  frame[width - 1] = segments & 0xFF;
255  segments = bytestream2_get_le16(gb);
256  }
257  line_ptr = frame;
258  if (frame_end - frame < width)
259  return AVERROR_INVALIDDATA;
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_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
288 {
289  const uint8_t *frame_end = frame + width * height;
290  int segments = bytestream2_get_le32(gb);
291  int skip, copy;
292 
293  while (segments--) {
294  if (bytestream2_get_bytes_left(gb) < 2)
295  return AVERROR_INVALIDDATA;
296  copy = bytestream2_get_byteu(gb) * 2;
297  skip = bytestream2_get_byteu(gb) * 2;
298  if (frame_end - frame < copy + skip ||
299  bytestream2_get_bytes_left(gb) < copy)
300  return AVERROR_INVALIDDATA;
301  frame += skip;
302  bytestream2_get_buffer(gb, frame, copy);
303  frame += copy;
304  }
305 
306  return 0;
307 }
308 
309 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
310 {
311  memset(frame, 0, width * height);
312  return 0;
313 }
314 
315 
316 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
317 
318 static const chunk_decoder decoder[8] = {
321 };
322 
323 static const char* chunk_name[8] = {
324  "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
325 };
326 
328  void *data, int *got_frame,
329  AVPacket *avpkt)
330 {
331  AVFrame *frame = data;
332  DfaContext *s = avctx->priv_data;
333  GetByteContext gb;
334  const uint8_t *buf = avpkt->data;
335  uint32_t chunk_type, chunk_size;
336  uint8_t *dst;
337  int ret;
338  int i, pal_elems;
339 
340  if ((ret = ff_get_buffer(avctx, frame, 0))) {
341  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
342  return ret;
343  }
344 
345  bytestream2_init(&gb, avpkt->data, avpkt->size);
346  while (bytestream2_get_bytes_left(&gb) > 0) {
347  bytestream2_skip(&gb, 4);
348  chunk_size = bytestream2_get_le32(&gb);
349  chunk_type = bytestream2_get_le32(&gb);
350  if (!chunk_type)
351  break;
352  if (chunk_type == 1) {
353  pal_elems = FFMIN(chunk_size / 3, 256);
354  for (i = 0; i < pal_elems; i++) {
355  s->pal[i] = bytestream2_get_be24(&gb) << 2;
356  s->pal[i] |= (s->pal[i] >> 6) & 0x333;
357  }
358  frame->palette_has_changed = 1;
359  } else if (chunk_type <= 9) {
360  if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
361  av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
362  chunk_name[chunk_type - 2]);
363  return AVERROR_INVALIDDATA;
364  }
365  } else {
366  av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
367  chunk_type);
368  }
369  buf += chunk_size;
370  }
371 
372  buf = s->frame_buf;
373  dst = frame->data[0];
374  for (i = 0; i < avctx->height; i++) {
375  memcpy(dst, buf, avctx->width);
376  dst += frame->linesize[0];
377  buf += avctx->width;
378  }
379  memcpy(frame->data[1], s->pal, sizeof(s->pal));
380 
381  *got_frame = 1;
382 
383  return avpkt->size;
384 }
385 
387 {
388  DfaContext *s = avctx->priv_data;
389 
390  av_freep(&s->frame_buf);
391 
392  return 0;
393 }
394 
396  .name = "dfa",
397  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
398  .type = AVMEDIA_TYPE_VIDEO,
399  .id = AV_CODEC_ID_DFA,
400  .priv_data_size = sizeof(DfaContext),
404  .capabilities = CODEC_CAP_DR1,
405 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static const char * chunk_name[8]
Definition: dfa.c:323
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
memory handling functions
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
AVCodec.
Definition: avcodec.h:2755
static av_cold int dfa_decode_end(AVCodecContext *avctx)
Definition: dfa.c:386
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
uint8_t
#define av_cold
Definition: attributes.h:66
float delta
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:309
static int dfa_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dfa.c:327
static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:287
static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:101
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:248
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:52
Definition: dfa.c:30
static void frame_end(MpegEncContext *s)
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
#define FFMIN(a, b)
Definition: common.h:57
static const chunk_decoder decoder[8]
Definition: dfa.c:318
int width
picture width / height.
Definition: avcodec.h:1217
uint32_t pal[256]
Definition: dfa.c:31
static av_cold int dfa_decode_init(AVCodecContext *avctx)
Definition: dfa.c:35
static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:187
static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:138
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
static int frame_start(MpegEncContext *s)
AVCodec ff_dfa_decoder
Definition: dfa.c:395
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:302
static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:229
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
int height
Definition: gxfenc.c:72
common internal api header.
static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
void * priv_data
Definition: avcodec.h:1090
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:299
int(* chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:316
uint8_t * frame_buf
Definition: dfa.c:32
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205