Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "internal.h"
34 
35 #include <zlib.h>
36 
37 /*
38  * Decoder context
39  */
40 typedef struct DxaDecContext {
42 
43  int dsize;
45  uint32_t pal[256];
47 
48 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
49 static const int 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, uint8_t *ref)
53 {
54  uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
55  int i, j, k;
56  int type, x, y, d, d2;
57  uint32_t mask;
58 
59  code = src + 12;
60  data = code + ((avctx->width * avctx->height) >> 4);
61  mv = data + AV_RB32(src + 0);
62  msk = mv + AV_RB32(src + 4);
63 
64  for(j = 0; j < avctx->height; j += 4){
65  for(i = 0; i < avctx->width; i += 4){
66  tmp = dst + i;
67  tmp2 = ref + i;
68  type = *code++;
69  switch(type){
70  case 4: // motion compensation
71  x = (*mv) >> 4; if(x & 8) x = 8 - x;
72  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
73  tmp2 += x + y*stride;
74  case 0: // skip
75  case 5: // skip in method 12
76  for(y = 0; y < 4; y++){
77  memcpy(tmp, tmp2, 4);
78  tmp += stride;
79  tmp2 += stride;
80  }
81  break;
82  case 1: // masked change
83  case 10: // masked change with only half of pixels changed
84  case 11: // cases 10-15 are for method 12 only
85  case 12:
86  case 13:
87  case 14:
88  case 15:
89  if(type == 1){
90  mask = AV_RB16(msk);
91  msk += 2;
92  }else{
93  type -= 10;
94  mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
95  msk++;
96  }
97  for(y = 0; y < 4; y++){
98  for(x = 0; x < 4; x++){
99  tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
100  mask <<= 1;
101  }
102  tmp += stride;
103  tmp2 += stride;
104  }
105  break;
106  case 2: // fill block
107  for(y = 0; y < 4; y++){
108  memset(tmp, data[0], 4);
109  tmp += stride;
110  }
111  data++;
112  break;
113  case 3: // raw block
114  for(y = 0; y < 4; y++){
115  memcpy(tmp, data, 4);
116  data += 4;
117  tmp += stride;
118  }
119  break;
120  case 8: // subblocks - method 13 only
121  mask = *msk++;
122  for(k = 0; k < 4; k++){
123  d = ((k & 1) << 1) + ((k & 2) * stride);
124  d2 = ((k & 1) << 1) + ((k & 2) * stride);
125  tmp2 = ref + i + d2;
126  switch(mask & 0xC0){
127  case 0x80: // motion compensation
128  x = (*mv) >> 4; if(x & 8) x = 8 - x;
129  y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
130  tmp2 += x + y*stride;
131  case 0x00: // skip
132  tmp[d + 0 ] = tmp2[0];
133  tmp[d + 1 ] = tmp2[1];
134  tmp[d + 0 + stride] = tmp2[0 + stride];
135  tmp[d + 1 + stride] = tmp2[1 + stride];
136  break;
137  case 0x40: // fill
138  tmp[d + 0 ] = data[0];
139  tmp[d + 1 ] = data[0];
140  tmp[d + 0 + stride] = data[0];
141  tmp[d + 1 + stride] = data[0];
142  data++;
143  break;
144  case 0xC0: // raw
145  tmp[d + 0 ] = *data++;
146  tmp[d + 1 ] = *data++;
147  tmp[d + 0 + stride] = *data++;
148  tmp[d + 1 + stride] = *data++;
149  break;
150  }
151  mask <<= 2;
152  }
153  break;
154  case 32: // vector quantization - 2 colors
155  mask = AV_RB16(msk);
156  msk += 2;
157  for(y = 0; y < 4; y++){
158  for(x = 0; x < 4; x++){
159  tmp[x] = data[mask & 1];
160  mask >>= 1;
161  }
162  tmp += stride;
163  tmp2 += stride;
164  }
165  data += 2;
166  break;
167  case 33: // vector quantization - 3 or 4 colors
168  case 34:
169  mask = AV_RB32(msk);
170  msk += 4;
171  for(y = 0; y < 4; y++){
172  for(x = 0; x < 4; x++){
173  tmp[x] = data[mask & 3];
174  mask >>= 2;
175  }
176  tmp += stride;
177  tmp2 += stride;
178  }
179  data += type - 30;
180  break;
181  default:
182  av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
183  return AVERROR_INVALIDDATA;
184  }
185  }
186  dst += stride * 4;
187  ref += stride * 4;
188  }
189  return 0;
190 }
191 
192 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
193 {
194  AVFrame *frame = data;
195  const uint8_t *buf = avpkt->data;
196  int buf_size = avpkt->size;
197  DxaDecContext * const c = avctx->priv_data;
198  uint8_t *outptr, *srcptr, *tmpptr;
199  unsigned long dsize;
200  int i, j, compr, ret;
201  int stride;
202  int orig_buf_size = buf_size;
203  int pc = 0;
204 
205  /* make the palette available on the way out */
206  if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
207  int r, g, b;
208 
209  buf += 4;
210  for(i = 0; i < 256; i++){
211  r = *buf++;
212  g = *buf++;
213  b = *buf++;
214  c->pal[i] = (r << 16) | (g << 8) | b;
215  }
216  pc = 1;
217  buf_size -= 768+4;
218  }
219 
220  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
221  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
222  return ret;
223  }
224  memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
225  frame->palette_has_changed = pc;
226 
227  outptr = frame->data[0];
228  srcptr = c->decomp_buf;
229  tmpptr = c->prev->data[0];
230  stride = frame->linesize[0];
231 
232  if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
233  compr = -1;
234  else
235  compr = buf[4];
236 
237  dsize = c->dsize;
238  if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
239  av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
240  return AVERROR_UNKNOWN;
241  }
242  switch(compr){
243  case -1:
244  frame->key_frame = 0;
245  frame->pict_type = AV_PICTURE_TYPE_P;
246  if (c->prev->data[0])
247  memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
248  else{ // Should happen only when first frame is 'NULL'
249  memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
250  frame->key_frame = 1;
251  frame->pict_type = AV_PICTURE_TYPE_I;
252  }
253  break;
254  case 2:
255  case 4:
256  frame->key_frame = 1;
257  frame->pict_type = AV_PICTURE_TYPE_I;
258  for (j = 0; j < avctx->height; j++) {
259  memcpy(outptr, srcptr, avctx->width);
260  outptr += stride;
261  srcptr += avctx->width;
262  }
263  break;
264  case 3:
265  case 5:
266  if (!tmpptr) {
267  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
268  return AVERROR_INVALIDDATA;
269  }
270  frame->key_frame = 0;
271  frame->pict_type = AV_PICTURE_TYPE_P;
272  for (j = 0; j < avctx->height; j++) {
273  for (i = 0; i < avctx->width; i++)
274  outptr[i] = srcptr[i] ^ tmpptr[i];
275  tmpptr += stride;
276  outptr += stride;
277  srcptr += avctx->width;
278  }
279  break;
280  case 12: // ScummVM coding
281  case 13:
282  frame->key_frame = 0;
283  frame->pict_type = AV_PICTURE_TYPE_P;
284  decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, c->prev->data[0]);
285  break;
286  default:
287  av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
288  return AVERROR_INVALIDDATA;
289  }
290 
291  av_frame_unref(c->prev);
292  if ((ret = av_frame_ref(c->prev, frame)) < 0)
293  return ret;
294 
295  *got_frame = 1;
296 
297  /* always report that the buffer was completely consumed */
298  return orig_buf_size;
299 }
300 
302 {
303  DxaDecContext * const c = avctx->priv_data;
304 
305  c->prev = av_frame_alloc();
306  if (!c->prev)
307  return AVERROR(ENOMEM);
308 
309  avctx->pix_fmt = AV_PIX_FMT_PAL8;
310 
311  c->dsize = avctx->width * avctx->height * 2;
312  if (!(c->decomp_buf = av_malloc(c->dsize))) {
313  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
314  return AVERROR(ENOMEM);
315  }
316 
317  return 0;
318 }
319 
321 {
322  DxaDecContext * const c = avctx->priv_data;
323 
324  av_freep(&c->decomp_buf);
325  av_frame_free(&c->prev);
326 
327  return 0;
328 }
329 
331  .name = "dxa",
332  .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
333  .type = AVMEDIA_TYPE_VIDEO,
334  .id = AV_CODEC_ID_DXA,
335  .priv_data_size = sizeof(DxaDecContext),
336  .init = decode_init,
337  .close = decode_end,
338  .decode = decode_frame,
339  .capabilities = CODEC_CAP_DR1,
340 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3051
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dxa.c:192
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
static av_cold int decode_end(AVCodecContext *avctx)
Definition: dxa.c:320
int dsize
Definition: dxa.c:43
uint8_t * decomp_buf
Definition: dxa.c:44
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:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define AV_RB32
Definition: intreadwrite.h:130
#define b
Definition: input.c:52
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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define r
Definition: input.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const int shift1[6]
Definition: dxa.c:48
static const uint16_t mask[17]
Definition: lzw.c:38
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
g
Definition: yuv2rgb.c:535
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t *dst, int stride, uint8_t *src, uint8_t *ref)
Definition: dxa.c:51
AVFrame * prev
Definition: dxa.c:41
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int width
picture width / height.
Definition: avcodec.h:1229
static const int8_t mv[256][2]
Definition: 4xm.c:75
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
AVCodec ff_dxa_decoder
Definition: dxa.c:330
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
uint32_t pal[256]
Definition: dxa.c:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
common internal and external API header
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dxa.c:301
static const int shift2[6]
Definition: dxa.c:49
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
This structure stores compressed data.
Definition: avcodec.h:950
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
Predicted.
Definition: avutil.h:254