FFmpeg
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 <inttypes.h>
24 
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "internal.h"
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 
33 typedef struct DfaContext {
34  uint32_t pal[256];
35  uint8_t *frame_buf;
36 } DfaContext;
37 
39 {
40  DfaContext *s = avctx->priv_data;
41 
42  avctx->pix_fmt = AV_PIX_FMT_PAL8;
43 
44  if (!avctx->width || !avctx->height || FFMAX(avctx->width, avctx->height) >= (1<<16))
45  return AVERROR_INVALIDDATA;
46 
47  av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
48 
49  s->frame_buf = av_mallocz(avctx->width * avctx->height);
50  if (!s->frame_buf)
51  return AVERROR(ENOMEM);
52 
53  return 0;
54 }
55 
56 static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
57 {
58  const int size = width * height;
59 
61  return AVERROR_INVALIDDATA;
62  return 0;
63 }
64 
65 static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
66 {
67  const uint8_t *frame_start = frame;
68  const uint8_t *frame_end = frame + width * height;
69  int mask = 0x10000, bitbuf = 0;
70  int v, count;
71  unsigned segments;
72  unsigned offset;
73 
74  segments = bytestream2_get_le32(gb);
75  offset = bytestream2_get_le32(gb);
76  if (segments == 0 && offset == frame_end - frame)
77  return 0; // skip frame
78  if (frame_end - frame <= offset)
79  return AVERROR_INVALIDDATA;
80  frame += offset;
81  while (segments--) {
82  if (bytestream2_get_bytes_left(gb) < 2)
83  return AVERROR_INVALIDDATA;
84  if (mask == 0x10000) {
85  bitbuf = bytestream2_get_le16u(gb);
86  mask = 1;
87  }
88  if (frame_end - frame < 2)
89  return AVERROR_INVALIDDATA;
90  if (bitbuf & mask) {
91  v = bytestream2_get_le16(gb);
92  offset = (v & 0x1FFF) << 1;
93  count = ((v >> 13) + 2) << 1;
94  if (frame - frame_start < offset || frame_end - frame < count)
95  return AVERROR_INVALIDDATA;
97  frame += count;
98  } else {
99  *frame++ = bytestream2_get_byte(gb);
100  *frame++ = bytestream2_get_byte(gb);
101  }
102  mask <<= 1;
103  }
104 
105  return 0;
106 }
107 
108 static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
109 {
110  const uint8_t *frame_start = frame;
111  const uint8_t *frame_end = frame + width * height;
112  int mask = 0x10000, bitbuf = 0;
113  int v, offset, count, segments;
114 
115  segments = bytestream2_get_le16(gb);
116  while (segments--) {
117  if (bytestream2_get_bytes_left(gb) < 2)
118  return AVERROR_INVALIDDATA;
119  if (mask == 0x10000) {
120  bitbuf = bytestream2_get_le16u(gb);
121  mask = 1;
122  }
123  if (frame_end - frame < 2)
124  return AVERROR_INVALIDDATA;
125  if (bitbuf & mask) {
126  v = bytestream2_get_le16(gb);
127  offset = (v & 0x1FFF) << 1;
128  count = ((v >> 13) + 2) << 1;
129  if (frame - frame_start < offset || frame_end - frame < count)
130  return AVERROR_INVALIDDATA;
131  av_memcpy_backptr(frame, offset, count);
132  frame += count;
133  } else if (bitbuf & (mask << 1)) {
134  frame += bytestream2_get_le16(gb);
135  } else {
136  *frame++ = bytestream2_get_byte(gb);
137  *frame++ = bytestream2_get_byte(gb);
138  }
139  mask <<= 2;
140  }
141 
142  return 0;
143 }
144 
145 static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
146 {
147  const uint8_t *frame_start = frame;
148  const uint8_t *frame_end = frame + width * height;
149  int mask = 0x10000, bitbuf = 0;
150  int i, v, offset, count, segments;
151 
152  if ((width | height) & 1)
153  return AVERROR_INVALIDDATA;
154  segments = bytestream2_get_le16(gb);
155  while (segments--) {
156  if (bytestream2_get_bytes_left(gb) < 2)
157  return AVERROR_INVALIDDATA;
158  if (mask == 0x10000) {
159  bitbuf = bytestream2_get_le16u(gb);
160  mask = 1;
161  }
162 
163  if (bitbuf & mask) {
164  v = bytestream2_get_le16(gb);
165  offset = (v & 0x1FFF) << 2;
166  count = ((v >> 13) + 2) << 1;
167  if (frame - frame_start < offset || frame_end - frame < count*2 + width)
168  return AVERROR_INVALIDDATA;
169  for (i = 0; i < count; i++) {
170  frame[0] = frame[1] =
171  frame[width] = frame[width + 1] = frame[-offset];
172 
173  frame += 2;
174  }
175  } else if (bitbuf & (mask << 1)) {
176  v = bytestream2_get_le16(gb)*2;
177  if (frame - frame_end < v)
178  return AVERROR_INVALIDDATA;
179  frame += v;
180  } else {
181  if (width < 4 || frame_end - frame < width + 4)
182  return AVERROR_INVALIDDATA;
183  frame[0] = frame[1] =
184  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
185  frame += 2;
186  frame[0] = frame[1] =
187  frame[width] = frame[width + 1] = bytestream2_get_byte(gb);
188  frame += 2;
189  }
190  mask <<= 2;
191  }
192 
193  return 0;
194 }
195 
196 static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
197 {
198  uint8_t *line_ptr;
199  int count, lines, segments;
200 
201  count = bytestream2_get_le16(gb);
202  if (count >= height)
203  return AVERROR_INVALIDDATA;
204  frame += width * count;
205  lines = bytestream2_get_le16(gb);
206  if (count + lines > height)
207  return AVERROR_INVALIDDATA;
208 
209  while (lines--) {
210  if (bytestream2_get_bytes_left(gb) < 1)
211  return AVERROR_INVALIDDATA;
212  line_ptr = frame;
213  frame += width;
214  segments = bytestream2_get_byteu(gb);
215  while (segments--) {
216  if (frame - line_ptr <= bytestream2_peek_byte(gb))
217  return AVERROR_INVALIDDATA;
218  line_ptr += bytestream2_get_byte(gb);
219  count = (int8_t)bytestream2_get_byte(gb);
220  if (count >= 0) {
221  if (frame - line_ptr < count)
222  return AVERROR_INVALIDDATA;
223  if (bytestream2_get_buffer(gb, line_ptr, count) != count)
224  return AVERROR_INVALIDDATA;
225  } else {
226  count = -count;
227  if (frame - line_ptr < count)
228  return AVERROR_INVALIDDATA;
229  memset(line_ptr, bytestream2_get_byte(gb), count);
230  }
231  line_ptr += count;
232  }
233  }
234 
235  return 0;
236 }
237 
238 static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
239 {
240  const uint8_t *frame_end = frame + width * height;
241  uint8_t *line_ptr;
242  int count, i, v, lines, segments;
243  int y = 0;
244 
245  lines = bytestream2_get_le16(gb);
246  if (lines > height)
247  return AVERROR_INVALIDDATA;
248 
249  while (lines--) {
250  if (bytestream2_get_bytes_left(gb) < 2)
251  return AVERROR_INVALIDDATA;
252  segments = bytestream2_get_le16u(gb);
253  while ((segments & 0xC000) == 0xC000) {
254  unsigned skip_lines = -(int16_t)segments;
255  int64_t delta = -((int16_t)segments * (int64_t)width);
256  if (frame_end - frame <= delta || y + lines + skip_lines > height)
257  return AVERROR_INVALIDDATA;
258  frame += delta;
259  y += skip_lines;
260  segments = bytestream2_get_le16(gb);
261  }
262 
263  if (frame_end <= frame)
264  return AVERROR_INVALIDDATA;
265  if (segments & 0x8000) {
266  frame[width - 1] = segments & 0xFF;
267  segments = bytestream2_get_le16(gb);
268  }
269  line_ptr = frame;
270  if (frame_end - frame < width)
271  return AVERROR_INVALIDDATA;
272  frame += width;
273  y++;
274  while (segments--) {
275  if (frame - line_ptr <= bytestream2_peek_byte(gb))
276  return AVERROR_INVALIDDATA;
277  line_ptr += bytestream2_get_byte(gb);
278  count = (int8_t)bytestream2_get_byte(gb);
279  if (count >= 0) {
280  if (frame - line_ptr < count * 2)
281  return AVERROR_INVALIDDATA;
282  if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
283  return AVERROR_INVALIDDATA;
284  line_ptr += count * 2;
285  } else {
286  count = -count;
287  if (frame - line_ptr < count * 2)
288  return AVERROR_INVALIDDATA;
289  v = bytestream2_get_le16(gb);
290  for (i = 0; i < count; i++)
291  bytestream_put_le16(&line_ptr, v);
292  }
293  }
294  }
295 
296  return 0;
297 }
298 
299 static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
300 {
301  const uint8_t *frame_end = frame + width * height;
302  uint32_t segments = bytestream2_get_le32(gb);
303  int skip, copy;
304 
305  while (segments--) {
306  if (bytestream2_get_bytes_left(gb) < 2)
307  return AVERROR_INVALIDDATA;
308  copy = bytestream2_get_byteu(gb) * 2;
309  skip = bytestream2_get_byteu(gb) * 2;
310  if (frame_end - frame < copy + skip ||
312  return AVERROR_INVALIDDATA;
313  frame += skip;
315  frame += copy;
316  }
317 
318  return 0;
319 }
320 
321 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
322 {
323  memset(frame, 0, width * height);
324  return 0;
325 }
326 
327 
328 typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
329 
330 static const chunk_decoder decoder[8] = {
333 };
334 
335 static const char chunk_name[8][5] = {
336  "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
337 };
338 
340  void *data, int *got_frame,
341  AVPacket *avpkt)
342 {
343  AVFrame *frame = data;
344  DfaContext *s = avctx->priv_data;
345  GetByteContext gb;
346  const uint8_t *buf = avpkt->data;
347  uint32_t chunk_type, chunk_size;
348  uint8_t *dst;
349  int ret;
350  int i, pal_elems;
351  int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
352 
353  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
354  return ret;
355 
356  bytestream2_init(&gb, avpkt->data, avpkt->size);
357  while (bytestream2_get_bytes_left(&gb) > 0) {
358  if (bytestream2_get_bytes_left(&gb) < 12)
359  return AVERROR_INVALIDDATA;
360  bytestream2_skip(&gb, 4);
361  chunk_size = bytestream2_get_le32(&gb);
362  chunk_type = bytestream2_get_le32(&gb);
363  if (!chunk_type)
364  break;
365  if (chunk_type == 1) {
366  pal_elems = FFMIN(chunk_size / 3, 256);
367  for (i = 0; i < pal_elems; i++) {
368  s->pal[i] = bytestream2_get_be24(&gb) << 2;
369  s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
370  }
371  frame->palette_has_changed = 1;
372  } else if (chunk_type <= 9) {
373  if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
374  av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
375  chunk_name[chunk_type - 2]);
376  return AVERROR_INVALIDDATA;
377  }
378  } else {
379  av_log(avctx, AV_LOG_WARNING,
380  "Ignoring unknown chunk type %"PRIu32"\n",
381  chunk_type);
382  }
383  buf += chunk_size;
384  }
385 
386  buf = s->frame_buf;
387  dst = frame->data[0];
388  for (i = 0; i < avctx->height; i++) {
389  if(version == 0x100) {
390  int j;
391  for(j = 0; j < avctx->width; j++) {
392  dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
393  ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
394  }
395  } else {
396  memcpy(dst, buf, avctx->width);
397  buf += avctx->width;
398  }
399  dst += frame->linesize[0];
400  }
401  memcpy(frame->data[1], s->pal, sizeof(s->pal));
402 
403  *got_frame = 1;
404 
405  return avpkt->size;
406 }
407 
409 {
410  DfaContext *s = avctx->priv_data;
411 
412  av_freep(&s->frame_buf);
413 
414  return 0;
415 }
416 
418  .name = "dfa",
419  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
420  .type = AVMEDIA_TYPE_VIDEO,
421  .id = AV_CODEC_ID_DFA,
422  .priv_data_size = sizeof(DfaContext),
424  .close = dfa_decode_end,
426  .capabilities = AV_CODEC_CAP_DR1,
427  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
428 };
AVCodec
AVCodec.
Definition: codec.h:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
dfa_decode_init
static av_cold int dfa_decode_init(AVCodecContext *avctx)
Definition: dfa.c:38
GetByteContext
Definition: bytestream.h:33
decode_wdlt
static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:238
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
init
static int init
Definition: av_tx.c:47
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
decoder
static const chunk_decoder decoder[8]
Definition: dfa.c:330
U
#define U(x)
Definition: vp56_arith.h:37
decode_dsw1
static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:108
avassert.h
frame_start
static int frame_start(MpegEncContext *s)
Definition: mpegvideo_enc.c:1641
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
mask
static const uint16_t mask[17]
Definition: lzw.c:38
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:454
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
decode_tsw1
static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:65
dfa_decode_frame
static int dfa_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dfa.c:339
decode_tdlt
static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:299
DfaContext::frame_buf
uint8_t * frame_buf
Definition: dfa.c:35
ff_dfa_decoder
const AVCodec ff_dfa_decoder
Definition: dfa.c:417
DfaContext::pal
uint32_t pal[256]
Definition: dfa.c:34
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
decode_bdlt
static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:196
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:187
size
int size
Definition: twinvq_data.h:10344
height
#define height
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
DfaContext
Definition: dfa.c:33
version
version
Definition: libkvazaar.c:313
chunk_name
static const char chunk_name[8][5]
Definition: dfa.c:335
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:383
dfa_decode_end
static av_cold int dfa_decode_end(AVCodecContext *avctx)
Definition: dfa.c:408
frame_end
static void frame_end(MpegEncContext *s)
Definition: mpegvideo_enc.c:1583
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
bytestream.h
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
chunk_decoder
int(* chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:328
av_image_check_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:318
AV_CODEC_ID_DFA
@ AV_CODEC_ID_DFA
Definition: codec_id.h:200
int
int
Definition: ffmpeg_filter.c:153
decode_blck
static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:321
decode_dds1
static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:145
decode_copy
static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
Definition: dfa.c:56