libavcodec/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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/avassert.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
30 
31 typedef struct DfaContext {
32  uint32_t pal[256];
34 } DfaContext;
35 
37 {
38  DfaContext *s = avctx->priv_data;
39 
40  avctx->pix_fmt = AV_PIX_FMT_PAL8;
41 
42  if (!avctx->width || !avctx->height)
43  return AVERROR_INVALIDDATA;
44 
45  av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
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 (segments == 0 && offset == frame_end - frame)
74  return 0; // skip frame
75  if (frame_end - frame <= offset)
76  return AVERROR_INVALIDDATA;
77  frame += offset;
78  while (segments--) {
79  if (bytestream2_get_bytes_left(gb) < 2)
80  return AVERROR_INVALIDDATA;
81  if (mask == 0x10000) {
82  bitbuf = bytestream2_get_le16u(gb);
83  mask = 1;
84  }
85  if (frame_end - frame < 2)
86  return AVERROR_INVALIDDATA;
87  if (bitbuf & mask) {
88  v = bytestream2_get_le16(gb);
89  offset = (v & 0x1FFF) << 1;
90  count = ((v >> 13) + 2) << 1;
91  if (frame - frame_start < offset || frame_end - frame < count)
92  return AVERROR_INVALIDDATA;
93  av_memcpy_backptr(frame, offset, count);
94  frame += count;
95  } else {
96  *frame++ = bytestream2_get_byte(gb);
97  *frame++ = bytestream2_get_byte(gb);
98  }
99  mask <<= 1;
100  }
101 
102  return 0;
103 }
104 
105 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
106 {
107  const uint8_t *frame_start = frame;
108  const uint8_t *frame_end = frame + width * height;
109  int mask = 0x10000, bitbuf = 0;
110  int v, offset, count, segments;
111 
112  segments = bytestream2_get_le16(gb);
113  while (segments--) {
114  if (bytestream2_get_bytes_left(gb) < 2)
115  return AVERROR_INVALIDDATA;
116  if (mask == 0x10000) {
117  bitbuf = bytestream2_get_le16u(gb);
118  mask = 1;
119  }
120  if (frame_end - frame < 2)
121  return AVERROR_INVALIDDATA;
122  if (bitbuf & mask) {
123  v = bytestream2_get_le16(gb);
124  offset = (v & 0x1FFF) << 1;
125  count = ((v >> 13) + 2) << 1;
126  if (frame - frame_start < offset || frame_end - frame < count)
127  return AVERROR_INVALIDDATA;
128  av_memcpy_backptr(frame, offset, count);
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_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
292 {
293  const uint8_t *frame_end = frame + width * height;
294  uint32_t segments = bytestream2_get_le32(gb);
295  int skip, copy;
296 
297  while (segments--) {
298  if (bytestream2_get_bytes_left(gb) < 2)
299  return AVERROR_INVALIDDATA;
300  copy = bytestream2_get_byteu(gb) * 2;
301  skip = bytestream2_get_byteu(gb) * 2;
302  if (frame_end - frame < copy + skip ||
303  bytestream2_get_bytes_left(gb) < copy)
304  return AVERROR_INVALIDDATA;
305  frame += skip;
306  bytestream2_get_buffer(gb, frame, copy);
307  frame += copy;
308  }
309 
310  return 0;
311 }
312 
313 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
314 {
315  memset(frame, 0, width * height);
316  return 0;
317 }
318 
319 
320 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
321 
322 static const chunk_decoder decoder[8] = {
325 };
326 
327 static const char* chunk_name[8] = {
328  "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
329 };
330 
332  void *data, int *got_frame,
333  AVPacket *avpkt)
334 {
335  AVFrame *frame = data;
336  DfaContext *s = avctx->priv_data;
337  GetByteContext gb;
338  const uint8_t *buf = avpkt->data;
339  uint32_t chunk_type, chunk_size;
340  uint8_t *dst;
341  int ret;
342  int i, pal_elems;
343  int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
344 
345  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
346  return ret;
347 
348  bytestream2_init(&gb, avpkt->data, avpkt->size);
349  while (bytestream2_get_bytes_left(&gb) > 0) {
350  bytestream2_skip(&gb, 4);
351  chunk_size = bytestream2_get_le32(&gb);
352  chunk_type = bytestream2_get_le32(&gb);
353  if (!chunk_type)
354  break;
355  if (chunk_type == 1) {
356  pal_elems = FFMIN(chunk_size / 3, 256);
357  for (i = 0; i < pal_elems; i++) {
358  s->pal[i] = bytestream2_get_be24(&gb) << 2;
359  s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
360  }
361  frame->palette_has_changed = 1;
362  } else if (chunk_type <= 9) {
363  if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
364  av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
365  chunk_name[chunk_type - 2]);
366  return AVERROR_INVALIDDATA;
367  }
368  } else {
369  av_log(avctx, AV_LOG_WARNING, "Ignoring unknown chunk type %d\n",
370  chunk_type);
371  }
372  buf += chunk_size;
373  }
374 
375  buf = s->frame_buf;
376  dst = frame->data[0];
377  for (i = 0; i < avctx->height; i++) {
378  if(version == 0x100) {
379  int j;
380  for(j = 0; j < avctx->width; j++) {
381  dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
382  ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
383  }
384  } else {
385  memcpy(dst, buf, avctx->width);
386  buf += avctx->width;
387  }
388  dst += frame->linesize[0];
389  }
390  memcpy(frame->data[1], s->pal, sizeof(s->pal));
391 
392  *got_frame = 1;
393 
394  return avpkt->size;
395 }
396 
398 {
399  DfaContext *s = avctx->priv_data;
400 
401  av_freep(&s->frame_buf);
402 
403  return 0;
404 }
405 
407  .name = "dfa",
408  .type = AVMEDIA_TYPE_VIDEO,
409  .id = AV_CODEC_ID_DFA,
410  .priv_data_size = sizeof(DfaContext),
414  .capabilities = CODEC_CAP_DR1,
415  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
416 };
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
float v
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const char * chunk_name[8]
misc image utilities
memory handling functions
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
#define AV_RL16
int version
Definition: avisynth_c.h:666
static av_cold int dfa_decode_end(AVCodecContext *avctx)
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
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
#define av_cold
Definition: attributes.h:78
float delta
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
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)
static int dfa_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
frame
Definition: stft.m:14
#define U(x)
static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
static const uint16_t mask[17]
Definition: lzw.c:37
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. ...
Spectrum Plot time data
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
external API header
static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
int size
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:231
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
static const chunk_decoder decoder[8]
int width
picture width / height.
uint32_t pal[256]
static av_cold int dfa_decode_init(AVCodecContext *avctx)
static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
static int width
Definition: tests/utils.c:158
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
int(* chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height)
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
synthesis window for stochastic i
AVCodec ff_dfa_decoder
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
function y
Definition: D.m:1
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
struct DfaContext DfaContext
void INT64 INT64 count
Definition: avisynth_c.h:594
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:327
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
uint8_t * frame_buf
This structure stores compressed data.