FFmpeg
dxa.c
Go to the documentation of this file.
1 /*
2  * Feeble Files/ScummVM DXA decoder
3  * Copyright (c) 2007 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * DXA Video decoder
25  */
26 
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
29 #include "bytestream.h"
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "decode.h"
33 
34 #include <zlib.h>
35 
36 /*
37  * Decoder context
38  */
39 typedef struct DxaDecContext {
41 
42  int dsize;
43 #define DECOMP_BUF_PADDING 16
44  uint8_t *decomp_buf;
45  uint32_t pal[256];
47 
48 static const uint8_t shift1[6] = { 0, 8, 8, 8, 4, 4 };
49 static const uint8_t shift2[6] = { 0, 0, 8, 4, 0, 4 };
50 
51 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
52  int stride, uint8_t *src, int srcsize, uint8_t *ref)
53 {
54  uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
55  uint8_t *src_end = src + srcsize;
56  int i, j, k;
57  int type, x, y, d, d2;
58  uint32_t mask;
59 
60  if (12ULL + ((avctx->width * avctx->height) >> 4) + AV_RB32(src + 0) + AV_RB32(src + 4) > srcsize)
61  return AVERROR_INVALIDDATA;
62 
63  code = src + 12;
64  data = code + ((avctx->width * avctx->height) >> 4);
65  mv = data + AV_RB32(src + 0);
66  msk = mv + AV_RB32(src + 4);
67 
68  for(j = 0; j < avctx->height; j += 4){
69  for(i = 0; i < avctx->width; i += 4){
70  if (data > src_end || mv > src_end || msk > src_end)
71  return AVERROR_INVALIDDATA;
72  tmp = dst + i;
73  tmp2 = ref + i;
74  type = *code++;
75  switch(type){
76  case 4: // motion compensation
77  x = (*mv) >> 4; if(x & 8) x = 8 - x;
78  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
79  if (i < -x || avctx->width - i - 4 < x ||
80  j < -y || avctx->height - j - 4 < y) {
81  av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
82  return AVERROR_INVALIDDATA;
83  }
84  tmp2 += x + y*stride;
85  case 0: // skip
86  case 5: // skip in method 12
87  for(y = 0; y < 4; y++){
88  memcpy(tmp, tmp2, 4);
89  tmp += stride;
90  tmp2 += stride;
91  }
92  break;
93  case 1: // masked change
94  case 10: // masked change with only half of pixels changed
95  case 11: // cases 10-15 are for method 12 only
96  case 12:
97  case 13:
98  case 14:
99  case 15:
100  if(type == 1){
101  mask = AV_RB16(msk);
102  msk += 2;
103  }else{
104  type -= 10;
105  mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
106  msk++;
107  }
108  for(y = 0; y < 4; y++){
109  for(x = 0; x < 4; x++){
110  tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
111  mask <<= 1;
112  }
113  tmp += stride;
114  tmp2 += stride;
115  }
116  break;
117  case 2: // fill block
118  for(y = 0; y < 4; y++){
119  memset(tmp, data[0], 4);
120  tmp += stride;
121  }
122  data++;
123  break;
124  case 3: // raw block
125  for(y = 0; y < 4; y++){
126  memcpy(tmp, data, 4);
127  data += 4;
128  tmp += stride;
129  }
130  break;
131  case 8: // subblocks - method 13 only
132  mask = *msk++;
133  for(k = 0; k < 4; k++){
134  d = ((k & 1) << 1) + ((k & 2) * stride);
135  d2 = ((k & 1) << 1) + ((k & 2) * stride);
136  tmp2 = ref + i + d2;
137  switch(mask & 0xC0){
138  case 0x80: // motion compensation
139  x = (*mv) >> 4; if(x & 8) x = 8 - x;
140  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
141  if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
142  j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
143  av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
144  return AVERROR_INVALIDDATA;
145  }
146  tmp2 += x + y*stride;
147  case 0x00: // skip
148  tmp[d + 0 ] = tmp2[0];
149  tmp[d + 1 ] = tmp2[1];
150  tmp[d + 0 + stride] = tmp2[0 + stride];
151  tmp[d + 1 + stride] = tmp2[1 + stride];
152  break;
153  case 0x40: // fill
154  tmp[d + 0 ] = data[0];
155  tmp[d + 1 ] = data[0];
156  tmp[d + 0 + stride] = data[0];
157  tmp[d + 1 + stride] = data[0];
158  data++;
159  break;
160  case 0xC0: // raw
161  tmp[d + 0 ] = *data++;
162  tmp[d + 1 ] = *data++;
163  tmp[d + 0 + stride] = *data++;
164  tmp[d + 1 + stride] = *data++;
165  break;
166  }
167  mask <<= 2;
168  }
169  break;
170  case 32: // vector quantization - 2 colors
171  mask = AV_RB16(msk);
172  msk += 2;
173  for(y = 0; y < 4; y++){
174  for(x = 0; x < 4; x++){
175  tmp[x] = data[mask & 1];
176  mask >>= 1;
177  }
178  tmp += stride;
179  tmp2 += stride;
180  }
181  data += 2;
182  break;
183  case 33: // vector quantization - 3 or 4 colors
184  case 34:
185  mask = AV_RB32(msk);
186  msk += 4;
187  for(y = 0; y < 4; y++){
188  for(x = 0; x < 4; x++){
189  tmp[x] = data[mask & 3];
190  mask >>= 2;
191  }
192  tmp += stride;
193  tmp2 += stride;
194  }
195  data += type - 30;
196  break;
197  default:
198  av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
199  return AVERROR_INVALIDDATA;
200  }
201  }
202  dst += stride * 4;
203  ref += stride * 4;
204  }
205  return 0;
206 }
207 
209  int *got_frame, AVPacket *avpkt)
210 {
211  DxaDecContext * const c = avctx->priv_data;
212  uint8_t *outptr, *srcptr, *tmpptr;
213  unsigned long dsize;
214  int i, j, compr, ret;
215  int stride;
216  int pc = 0;
217  GetByteContext gb;
218 
219  bytestream2_init(&gb, avpkt->data, avpkt->size);
220 
221  /* make the palette available on the way out */
222  if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
223  bytestream2_skip(&gb, 4);
224  for(i = 0; i < 256; i++){
225  c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
226  }
227  pc = 1;
228  }
229 
230  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
231  return ret;
232  memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
233 #if FF_API_PALETTE_HAS_CHANGED
235  frame->palette_has_changed = pc;
237 #endif
238 
239  outptr = frame->data[0];
240  srcptr = c->decomp_buf;
241  tmpptr = c->prev->data[0];
242  stride = frame->linesize[0];
243 
244  if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
245  compr = -1;
246  else
247  compr = bytestream2_get_byte(&gb);
248 
249  dsize = c->dsize;
250  if (compr != 4 && compr != -1) {
251  bytestream2_skip(&gb, 4);
252  if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
253  bytestream2_get_bytes_left(&gb)) != Z_OK) {
254  av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
255  return AVERROR_UNKNOWN;
256  }
257  memset(c->decomp_buf + dsize, 0, DECOMP_BUF_PADDING);
258  }
259 
260  if (avctx->debug & FF_DEBUG_PICT_INFO)
261  av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
262 
263  switch(compr){
264  case -1:
265  frame->flags &= ~AV_FRAME_FLAG_KEY;
266  frame->pict_type = AV_PICTURE_TYPE_P;
267  if (c->prev->data[0])
268  memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
269  else{ // Should happen only when first frame is 'NULL'
270  memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
271  frame->flags |= AV_FRAME_FLAG_KEY;
272  frame->pict_type = AV_PICTURE_TYPE_I;
273  }
274  break;
275  case 2:
276  case 4:
277  frame->flags |= AV_FRAME_FLAG_KEY;
278  frame->pict_type = AV_PICTURE_TYPE_I;
279  for (j = 0; j < avctx->height; j++) {
280  memcpy(outptr, srcptr, avctx->width);
281  outptr += stride;
282  srcptr += avctx->width;
283  }
284  break;
285  case 3:
286  case 5:
287  if (!tmpptr) {
288  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
289  if (!(avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
290  return AVERROR_INVALIDDATA;
291  }
292  frame->flags &= ~AV_FRAME_FLAG_KEY;
293  frame->pict_type = AV_PICTURE_TYPE_P;
294  for (j = 0; j < avctx->height; j++) {
295  if(tmpptr){
296  for(i = 0; i < avctx->width; i++)
297  outptr[i] = srcptr[i] ^ tmpptr[i];
298  tmpptr += stride;
299  }else
300  memcpy(outptr, srcptr, avctx->width);
301  outptr += stride;
302  srcptr += avctx->width;
303  }
304  break;
305  case 12: // ScummVM coding
306  case 13:
307  frame->flags &= ~AV_FRAME_FLAG_KEY;
308  frame->pict_type = AV_PICTURE_TYPE_P;
309  if (!c->prev->data[0]) {
310  av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
311  return AVERROR_INVALIDDATA;
312  }
313  decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, dsize, c->prev->data[0]);
314  break;
315  default:
316  av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
317  return AVERROR_INVALIDDATA;
318  }
319 
320  if ((ret = av_frame_replace(c->prev, frame)) < 0)
321  return ret;
322 
323  *got_frame = 1;
324 
325  /* always report that the buffer was completely consumed */
326  return avpkt->size;
327 }
328 
330 {
331  DxaDecContext * const c = avctx->priv_data;
332 
333  if (avctx->width%4 || avctx->height%4) {
334  avpriv_request_sample(avctx, "dimensions are not a multiple of 4");
335  return AVERROR_INVALIDDATA;
336  }
337 
338  c->prev = av_frame_alloc();
339  if (!c->prev)
340  return AVERROR(ENOMEM);
341 
342  avctx->pix_fmt = AV_PIX_FMT_PAL8;
343 
344  c->dsize = avctx->width * avctx->height * 2;
345  c->decomp_buf = av_malloc(c->dsize + DECOMP_BUF_PADDING);
346  if (!c->decomp_buf) {
347  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
348  return AVERROR(ENOMEM);
349  }
350 
351  return 0;
352 }
353 
355 {
356  DxaDecContext * const c = avctx->priv_data;
357 
358  av_freep(&c->decomp_buf);
359  av_frame_free(&c->prev);
360 
361  return 0;
362 }
363 
365  .p.name = "dxa",
366  CODEC_LONG_NAME("Feeble Files/ScummVM DXA"),
367  .p.type = AVMEDIA_TYPE_VIDEO,
368  .p.id = AV_CODEC_ID_DXA,
369  .priv_data_size = sizeof(DxaDecContext),
370  .init = decode_init,
371  .close = decode_end,
373  .p.capabilities = AV_CODEC_CAP_DR1,
374  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
375 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_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
GetByteContext
Definition: bytestream.h:33
mv
static const int8_t mv[256][2]
Definition: 4xm.c:81
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:524
data
const char data[16]
Definition: mxf.c:148
FFCodec
Definition: codec_internal.h:126
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ff_dxa_decoder
const FFCodec ff_dxa_decoder
Definition: dxa.c:364
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1397
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: dxa.c:354
type
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 type
Definition: writing_filters.txt:86
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
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
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
mask
static const uint16_t mask[17]
Definition: lzw.c:38
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
intreadwrite.h
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:425
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
DECOMP_BUF_PADDING
#define DECOMP_BUF_PADDING
Definition: dxa.c:43
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:509
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:525
codec_internal.h
DxaDecContext::dsize
int dsize
Definition: dxa.c:42
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
height
#define height
decode_13
static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t *dst, int stride, uint8_t *src, int srcsize, uint8_t *ref)
Definition: dxa.c:51
AV_CODEC_ID_DXA
@ AV_CODEC_ID_DXA
Definition: codec_id.h:150
DxaDecContext::pal
uint32_t pal[256]
Definition: dxa.c:45
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AV_CODEC_FLAG2_SHOW_ALL
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:380
shift2
static const uint8_t shift2[6]
Definition: dxa.c:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
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
DxaDecContext::prev
AVFrame * prev
Definition: dxa.c:40
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: dxa.c:208
U
#define U(x)
Definition: vpx_arith.h:37
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:483
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1396
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
DxaDecContext
Definition: dxa.c:39
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:501
DxaDecContext::decomp_buf
uint8_t * decomp_buf
Definition: dxa.c:44
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
d
d
Definition: ffmpeg_filter.c:424
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.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:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
shift1
static const uint8_t shift1[6]
Definition: dxa.c:48
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dxa.c:329
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98