FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <stdio.h>
28 #include <stdlib.h>
29 
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "bytestream.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 
36 #include <zlib.h>
37 
38 /*
39  * Decoder context
40  */
41 typedef struct DxaDecContext {
43 
44  int dsize;
45 #define DECOMP_BUF_PADDING 16
47  uint32_t pal[256];
49 
50 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
51 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
52 
53 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
54  int stride, uint8_t *src, int srcsize, uint8_t *ref)
55 {
56  uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
57  uint8_t *src_end = src + srcsize;
58  int i, j, k;
59  int type, x, y, d, d2;
60  uint32_t mask;
61 
62  if (12ULL + ((avctx->width * avctx->height) >> 4) + AV_RB32(src + 0) + AV_RB32(src + 4) > srcsize)
63  return AVERROR_INVALIDDATA;
64 
65  code = src + 12;
66  data = code + ((avctx->width * avctx->height) >> 4);
67  mv = data + AV_RB32(src + 0);
68  msk = mv + AV_RB32(src + 4);
69 
70  for(j = 0; j < avctx->height; j += 4){
71  for(i = 0; i < avctx->width; i += 4){
72  if (data > src_end || mv > src_end || msk > src_end)
73  return AVERROR_INVALIDDATA;
74  tmp = dst + i;
75  tmp2 = ref + i;
76  type = *code++;
77  switch(type){
78  case 4: // motion compensation
79  x = (*mv) >> 4; if(x & 8) x = 8 - x;
80  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
81  if (i < -x || avctx->width - i - 4 < x ||
82  j < -y || avctx->height - j - 4 < y) {
83  av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
84  return AVERROR_INVALIDDATA;
85  }
86  tmp2 += x + y*stride;
87  case 0: // skip
88  case 5: // skip in method 12
89  for(y = 0; y < 4; y++){
90  memcpy(tmp, tmp2, 4);
91  tmp += stride;
92  tmp2 += stride;
93  }
94  break;
95  case 1: // masked change
96  case 10: // masked change with only half of pixels changed
97  case 11: // cases 10-15 are for method 12 only
98  case 12:
99  case 13:
100  case 14:
101  case 15:
102  if(type == 1){
103  mask = AV_RB16(msk);
104  msk += 2;
105  }else{
106  type -= 10;
107  mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
108  msk++;
109  }
110  for(y = 0; y < 4; y++){
111  for(x = 0; x < 4; x++){
112  tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
113  mask <<= 1;
114  }
115  tmp += stride;
116  tmp2 += stride;
117  }
118  break;
119  case 2: // fill block
120  for(y = 0; y < 4; y++){
121  memset(tmp, data[0], 4);
122  tmp += stride;
123  }
124  data++;
125  break;
126  case 3: // raw block
127  for(y = 0; y < 4; y++){
128  memcpy(tmp, data, 4);
129  data += 4;
130  tmp += stride;
131  }
132  break;
133  case 8: // subblocks - method 13 only
134  mask = *msk++;
135  for(k = 0; k < 4; k++){
136  d = ((k & 1) << 1) + ((k & 2) * stride);
137  d2 = ((k & 1) << 1) + ((k & 2) * stride);
138  tmp2 = ref + i + d2;
139  switch(mask & 0xC0){
140  case 0x80: // motion compensation
141  x = (*mv) >> 4; if(x & 8) x = 8 - x;
142  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
143  if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
144  j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
145  av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
146  return AVERROR_INVALIDDATA;
147  }
148  tmp2 += x + y*stride;
149  case 0x00: // skip
150  tmp[d + 0 ] = tmp2[0];
151  tmp[d + 1 ] = tmp2[1];
152  tmp[d + 0 + stride] = tmp2[0 + stride];
153  tmp[d + 1 + stride] = tmp2[1 + stride];
154  break;
155  case 0x40: // fill
156  tmp[d + 0 ] = data[0];
157  tmp[d + 1 ] = data[0];
158  tmp[d + 0 + stride] = data[0];
159  tmp[d + 1 + stride] = data[0];
160  data++;
161  break;
162  case 0xC0: // raw
163  tmp[d + 0 ] = *data++;
164  tmp[d + 1 ] = *data++;
165  tmp[d + 0 + stride] = *data++;
166  tmp[d + 1 + stride] = *data++;
167  break;
168  }
169  mask <<= 2;
170  }
171  break;
172  case 32: // vector quantization - 2 colors
173  mask = AV_RB16(msk);
174  msk += 2;
175  for(y = 0; y < 4; y++){
176  for(x = 0; x < 4; x++){
177  tmp[x] = data[mask & 1];
178  mask >>= 1;
179  }
180  tmp += stride;
181  tmp2 += stride;
182  }
183  data += 2;
184  break;
185  case 33: // vector quantization - 3 or 4 colors
186  case 34:
187  mask = AV_RB32(msk);
188  msk += 4;
189  for(y = 0; y < 4; y++){
190  for(x = 0; x < 4; x++){
191  tmp[x] = data[mask & 3];
192  mask >>= 2;
193  }
194  tmp += stride;
195  tmp2 += stride;
196  }
197  data += type - 30;
198  break;
199  default:
200  av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
201  return AVERROR_INVALIDDATA;
202  }
203  }
204  dst += stride * 4;
205  ref += stride * 4;
206  }
207  return 0;
208 }
209 
210 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
211 {
212  AVFrame *frame = data;
213  DxaDecContext * const c = avctx->priv_data;
214  uint8_t *outptr, *srcptr, *tmpptr;
215  unsigned long dsize;
216  int i, j, compr, ret;
217  int stride;
218  int pc = 0;
219  GetByteContext gb;
220 
221  bytestream2_init(&gb, avpkt->data, avpkt->size);
222 
223  /* make the palette available on the way out */
224  if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
225  bytestream2_skip(&gb, 4);
226  for(i = 0; i < 256; i++){
227  c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
228  }
229  pc = 1;
230  }
231 
232  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
233  return ret;
234  memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
235  frame->palette_has_changed = pc;
236 
237  outptr = frame->data[0];
238  srcptr = c->decomp_buf;
239  tmpptr = c->prev->data[0];
240  stride = frame->linesize[0];
241 
242  if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
243  compr = -1;
244  else
245  compr = bytestream2_get_byte(&gb);
246 
247  dsize = c->dsize;
248  if (compr != 4 && compr != -1) {
249  bytestream2_skip(&gb, 4);
250  if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
251  bytestream2_get_bytes_left(&gb)) != Z_OK) {
252  av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
253  return AVERROR_UNKNOWN;
254  }
255  memset(c->decomp_buf + dsize, 0, DECOMP_BUF_PADDING);
256  }
257 
258  if (avctx->debug & FF_DEBUG_PICT_INFO)
259  av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
260 
261  switch(compr){
262  case -1:
263  frame->key_frame = 0;
264  frame->pict_type = AV_PICTURE_TYPE_P;
265  if (c->prev->data[0])
266  memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
267  else{ // Should happen only when first frame is 'NULL'
268  memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
269  frame->key_frame = 1;
270  frame->pict_type = AV_PICTURE_TYPE_I;
271  }
272  break;
273  case 2:
274  case 4:
275  frame->key_frame = 1;
276  frame->pict_type = AV_PICTURE_TYPE_I;
277  for (j = 0; j < avctx->height; j++) {
278  memcpy(outptr, srcptr, avctx->width);
279  outptr += stride;
280  srcptr += avctx->width;
281  }
282  break;
283  case 3:
284  case 5:
285  if (!tmpptr) {
286  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
287  if (!(avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
288  return AVERROR_INVALIDDATA;
289  }
290  frame->key_frame = 0;
291  frame->pict_type = AV_PICTURE_TYPE_P;
292  for (j = 0; j < avctx->height; j++) {
293  if(tmpptr){
294  for(i = 0; i < avctx->width; i++)
295  outptr[i] = srcptr[i] ^ tmpptr[i];
296  tmpptr += stride;
297  }else
298  memcpy(outptr, srcptr, avctx->width);
299  outptr += stride;
300  srcptr += avctx->width;
301  }
302  break;
303  case 12: // ScummVM coding
304  case 13:
305  frame->key_frame = 0;
306  frame->pict_type = AV_PICTURE_TYPE_P;
307  if (!c->prev->data[0]) {
308  av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
309  return AVERROR_INVALIDDATA;
310  }
311  decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, dsize, c->prev->data[0]);
312  break;
313  default:
314  av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
315  return AVERROR_INVALIDDATA;
316  }
317 
318  av_frame_unref(c->prev);
319  if ((ret = av_frame_ref(c->prev, frame)) < 0)
320  return ret;
321 
322  *got_frame = 1;
323 
324  /* always report that the buffer was completely consumed */
325  return avpkt->size;
326 }
327 
329 {
330  DxaDecContext * const c = avctx->priv_data;
331 
332  if (avctx->width%4 || avctx->height%4) {
333  avpriv_request_sample(avctx, "dimensions are not a multiple of 4");
334  return AVERROR_INVALIDDATA;
335  }
336 
337  c->prev = av_frame_alloc();
338  if (!c->prev)
339  return AVERROR(ENOMEM);
340 
341  avctx->pix_fmt = AV_PIX_FMT_PAL8;
342 
343  c->dsize = avctx->width * avctx->height * 2;
345  if (!c->decomp_buf) {
346  av_frame_free(&c->prev);
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  .name = "dxa",
366  .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
367  .type = AVMEDIA_TYPE_VIDEO,
368  .id = AV_CODEC_ID_DXA,
369  .priv_data_size = sizeof(DxaDecContext),
370  .init = decode_init,
371  .close = decode_end,
372  .decode = decode_frame,
373  .capabilities = AV_CODEC_CAP_DR1,
374 };
#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:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dxa.c:210
int size
Definition: avcodec.h:1602
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
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:87
AVCodec.
Definition: avcodec.h:3600
static av_cold int decode_end(AVCodecContext *avctx)
Definition: dxa.c:354
int dsize
Definition: dxa.c:44
uint8_t * decomp_buf
Definition: dxa.c:46
static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t *dst, int stride, uint8_t *src, int srcsize, uint8_t *ref)
Definition: dxa.c:53
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:145
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2917
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:383
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:87
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1601
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const int shift1[6]
Definition: dxa.c:50
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:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
AVFrame * prev
Definition: dxa.c:42
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
#define width
int width
picture width / height.
Definition: avcodec.h:1863
#define DECOMP_BUF_PADDING
Definition: dxa.c:45
#define src
Definition: vp9dsp.c:530
static const int8_t mv[256][2]
Definition: 4xm.c:77
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
int debug
debug
Definition: avcodec.h:2916
main external API structure.
Definition: avcodec.h:1676
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
GLint GLenum type
Definition: opengl_enc.c:105
AVCodec ff_dxa_decoder
Definition: dxa.c:364
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:332
uint32_t pal[256]
Definition: dxa.c:47
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:493
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
common internal and external API header
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dxa.c:328
static const int shift2[6]
Definition: dxa.c:51
static double c[64]
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * priv_data
Definition: avcodec.h:1718
static uint8_t tmp[8]
Definition: des.c:38
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:253
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1778
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition: avcodec.h:929
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
#define stride
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1578
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
Predicted.
Definition: avutil.h:269