2 * Feeble Files/ScummVM DXA decoder
3 * Copyright (c) 2007 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "bytestream.h"
41 typedef struct DxaDecContext
{
45 #define DECOMP_BUF_PADDING 16
50 static const int shift1
[6] = { 0, 8, 8, 8, 4, 4 };
51 static const int shift2
[6] = { 0, 0, 8, 4, 0, 4 };
53 static int decode_13(AVCodecContext
*avctx
, DxaDecContext
*c
, uint8_t* dst
,
54 int stride
, uint8_t *src
, int srcsize
, uint8_t *ref
)
56 uint8_t *code
, *data
, *mv
, *msk
, *tmp
, *tmp2
;
57 uint8_t *src_end
= src
+ srcsize
;
59 int type
, x
, y
, d
, d2
;
62 if (12ULL + ((avctx
->width
* avctx
->height
) >> 4) + AV_RB32(src
+ 0) + AV_RB32(src
+ 4) > srcsize
)
63 return AVERROR_INVALIDDATA
;
66 data
= code
+ ((avctx
->width
* avctx
->height
) >> 4);
67 mv
= data
+ AV_RB32(src
+ 0);
68 msk
= mv
+ AV_RB32(src
+ 4);
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
;
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
;
88 case 5: // skip in method 12
89 for(y
= 0; y
< 4; y
++){
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
107 mask
= ((msk
[0] & 0xF0) << shift1
[type
]) | ((msk
[0] & 0xF) << shift2
[type
]);
110 for(y
= 0; y
< 4; y
++){
111 for(x
= 0; x
< 4; x
++){
112 tmp
[x
] = (mask
& 0x8000) ? *data
++ : tmp2
[x
];
119 case 2: // fill block
120 for(y
= 0; y
< 4; y
++){
121 memset(tmp
, data
[0], 4);
127 for(y
= 0; y
< 4; y
++){
128 memcpy(tmp
, data
, 4);
133 case 8: // subblocks - method 13 only
135 for(k
= 0; k
< 4; k
++){
136 d
= ((k
& 1) << 1) + ((k
& 2) * stride
);
137 d2
= ((k
& 1) << 1) + ((k
& 2) * stride
);
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
;
148 tmp2
+= x
+ y
*stride
;
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
];
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];
163 tmp
[d
+ 0 ] = *data
++;
164 tmp
[d
+ 1 ] = *data
++;
165 tmp
[d
+ 0 + stride
] = *data
++;
166 tmp
[d
+ 1 + stride
] = *data
++;
172 case 32: // vector quantization - 2 colors
175 for(y
= 0; y
< 4; y
++){
176 for(x
= 0; x
< 4; x
++){
177 tmp
[x
] = data
[mask
& 1];
185 case 33: // vector quantization - 3 or 4 colors
189 for(y
= 0; y
< 4; y
++){
190 for(x
= 0; x
< 4; x
++){
191 tmp
[x
] = data
[mask
& 3];
200 av_log(avctx
, AV_LOG_ERROR
, "Unknown opcode %d\n", type
);
201 return AVERROR_INVALIDDATA
;
210 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame
, AVPacket
*avpkt
)
212 AVFrame
*frame
= data
;
213 DxaDecContext
* const c
= avctx
->priv_data
;
214 uint8_t *outptr
, *srcptr
, *tmpptr
;
216 int i
, j
, compr
, ret
;
221 bytestream2_init(&gb
, avpkt
->data
, avpkt
->size
);
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
);
232 if ((ret
= ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
)) < 0)
234 memcpy(frame
->data
[1], c
->pal
, AVPALETTE_SIZE
);
235 frame
->palette_has_changed
= pc
;
237 outptr
= frame
->data
[0];
238 srcptr
= c
->decomp_buf
;
239 tmpptr
= c
->prev
->data
[0];
240 stride
= frame
->linesize
[0];
242 if (bytestream2_get_le32(&gb
) == MKTAG('N','U','L','L'))
245 compr
= bytestream2_get_byte(&gb
);
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
;
255 memset(c
->decomp_buf
+ dsize
, 0, DECOMP_BUF_PADDING
);
258 if (avctx
->debug
& FF_DEBUG_PICT_INFO
)
259 av_log(avctx
, AV_LOG_DEBUG
, "compr:%2d, dsize:%d\n", compr
, (int)dsize
);
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
;
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
);
280 srcptr
+= avctx
->width
;
286 av_log(avctx
, AV_LOG_ERROR
, "Missing reference frame.\n");
287 if (!(avctx
->flags2
& CODEC_FLAG2_SHOW_ALL
))
288 return AVERROR_INVALIDDATA
;
290 frame
->key_frame
= 0;
291 frame
->pict_type
= AV_PICTURE_TYPE_P
;
292 for (j
= 0; j
< avctx
->height
; j
++) {
294 for(i
= 0; i
< avctx
->width
; i
++)
295 outptr
[i
] = srcptr
[i
] ^ tmpptr
[i
];
298 memcpy(outptr
, srcptr
, avctx
->width
);
300 srcptr
+= avctx
->width
;
303 case 12: // ScummVM coding
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
;
311 decode_13(avctx
, c
, frame
->data
[0], frame
->linesize
[0], srcptr
, dsize
, c
->prev
->data
[0]);
314 av_log(avctx
, AV_LOG_ERROR
, "Unknown/unsupported compression type %d\n", compr
);
315 return AVERROR_INVALIDDATA
;
318 av_frame_unref(c
->prev
);
319 if ((ret
= av_frame_ref(c
->prev
, frame
)) < 0)
324 /* always report that the buffer was completely consumed */
328 static av_cold
int decode_init(AVCodecContext
*avctx
)
330 DxaDecContext
* const c
= avctx
->priv_data
;
332 if (avctx
->width
%4 || avctx
->height
%4) {
333 avpriv_request_sample(avctx
, "dimensions are not a multiple of 4");
334 return AVERROR_INVALIDDATA
;
337 c
->prev
= av_frame_alloc();
339 return AVERROR(ENOMEM
);
341 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
343 c
->dsize
= avctx
->width
* avctx
->height
* 2;
344 c
->decomp_buf
= av_malloc(c
->dsize
+ DECOMP_BUF_PADDING
);
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
);
354 static av_cold
int decode_end(AVCodecContext
*avctx
)
356 DxaDecContext
* const c
= avctx
->priv_data
;
358 av_freep(&c
->decomp_buf
);
359 av_frame_free(&c
->prev
);
364 AVCodec ff_dxa_decoder
= {
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
),
372 .decode
= decode_frame
,
373 .capabilities
= CODEC_CAP_DR1
,