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 {
49 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
50 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
52 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
53 int stride, uint8_t *src, uint8_t *ref)
55 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
57 int type, x, y, d, d2;
61 data = code + ((avctx->width * avctx->height) >> 4);
62 mv = data + AV_RB32(src + 0);
63 msk = mv + AV_RB32(src + 4);
65 for(j = 0; j < avctx->height; j += 4){
66 for(i = 0; i < avctx->width; i += 4){
71 case 4: // motion compensation
72 x = (*mv) >> 4; if(x & 8) x = 8 - x;
73 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
74 if (i < -x || avctx->width - i - 4 < x ||
75 j < -y || avctx->height - j - 4 < y) {
76 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
77 return AVERROR_INVALIDDATA;
81 case 5: // skip in method 12
82 for(y = 0; y < 4; y++){
88 case 1: // masked change
89 case 10: // masked change with only half of pixels changed
90 case 11: // cases 10-15 are for method 12 only
100 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
103 for(y = 0; y < 4; y++){
104 for(x = 0; x < 4; x++){
105 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
112 case 2: // fill block
113 for(y = 0; y < 4; y++){
114 memset(tmp, data[0], 4);
120 for(y = 0; y < 4; y++){
121 memcpy(tmp, data, 4);
126 case 8: // subblocks - method 13 only
128 for(k = 0; k < 4; k++){
129 d = ((k & 1) << 1) + ((k & 2) * stride);
130 d2 = ((k & 1) << 1) + ((k & 2) * stride);
133 case 0x80: // motion compensation
134 x = (*mv) >> 4; if(x & 8) x = 8 - x;
135 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
136 if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
137 j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
138 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
139 return AVERROR_INVALIDDATA;
141 tmp2 += x + y*stride;
143 tmp[d + 0 ] = tmp2[0];
144 tmp[d + 1 ] = tmp2[1];
145 tmp[d + 0 + stride] = tmp2[0 + stride];
146 tmp[d + 1 + stride] = tmp2[1 + stride];
149 tmp[d + 0 ] = data[0];
150 tmp[d + 1 ] = data[0];
151 tmp[d + 0 + stride] = data[0];
152 tmp[d + 1 + stride] = data[0];
156 tmp[d + 0 ] = *data++;
157 tmp[d + 1 ] = *data++;
158 tmp[d + 0 + stride] = *data++;
159 tmp[d + 1 + stride] = *data++;
165 case 32: // vector quantization - 2 colors
168 for(y = 0; y < 4; y++){
169 for(x = 0; x < 4; x++){
170 tmp[x] = data[mask & 1];
178 case 33: // vector quantization - 3 or 4 colors
182 for(y = 0; y < 4; y++){
183 for(x = 0; x < 4; x++){
184 tmp[x] = data[mask & 3];
193 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
194 return AVERROR_INVALIDDATA;
203 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
205 AVFrame *frame = data;
206 DxaDecContext * const c = avctx->priv_data;
207 uint8_t *outptr, *srcptr, *tmpptr;
209 int i, j, compr, ret;
214 bytestream2_init(&gb, avpkt->data, avpkt->size);
216 /* make the palette available on the way out */
217 if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
218 bytestream2_skip(&gb, 4);
219 for(i = 0; i < 256; i++){
220 c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
225 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
227 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
228 frame->palette_has_changed = pc;
230 outptr = frame->data[0];
231 srcptr = c->decomp_buf;
232 tmpptr = c->prev.data[0];
233 stride = frame->linesize[0];
235 if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
238 compr = bytestream2_get_byte(&gb);
241 if (compr != 4 && compr != -1) {
242 bytestream2_skip(&gb, 4);
243 if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
244 bytestream2_get_bytes_left(&gb)) != Z_OK) {
245 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
246 return AVERROR_UNKNOWN;
251 frame->key_frame = 0;
252 frame->pict_type = AV_PICTURE_TYPE_P;
254 memcpy(frame->data[0], c->prev.data[0], frame->linesize[0] * avctx->height);
255 else{ // Should happen only when first frame is 'NULL'
256 memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
257 frame->key_frame = 1;
258 frame->pict_type = AV_PICTURE_TYPE_I;
265 frame->key_frame = !(compr & 1);
266 frame->pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
267 for(j = 0; j < avctx->height; j++){
268 if((compr & 1) && tmpptr){
269 for(i = 0; i < avctx->width; i++)
270 outptr[i] = srcptr[i] ^ tmpptr[i];
273 memcpy(outptr, srcptr, avctx->width);
275 srcptr += avctx->width;
278 case 12: // ScummVM coding
280 frame->key_frame = 0;
281 frame->pict_type = AV_PICTURE_TYPE_P;
282 if (!c->prev.data[0]) {
283 av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
284 return AVERROR_INVALIDDATA;
286 decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, c->prev.data[0]);
289 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
290 return AVERROR_INVALIDDATA;
293 av_frame_unref(&c->prev);
294 if ((ret = av_frame_ref(&c->prev, frame)) < 0)
299 /* always report that the buffer was completely consumed */
303 static av_cold int decode_init(AVCodecContext *avctx)
305 DxaDecContext * const c = avctx->priv_data;
307 avctx->pix_fmt = AV_PIX_FMT_PAL8;
309 avcodec_get_frame_defaults(&c->prev);
311 c->dsize = avctx->width * avctx->height * 2;
312 c->decomp_buf = av_malloc(c->dsize);
313 if (!c->decomp_buf) {
314 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
315 return AVERROR(ENOMEM);
321 static av_cold int decode_end(AVCodecContext *avctx)
323 DxaDecContext * const c = avctx->priv_data;
325 av_freep(&c->decomp_buf);
326 av_frame_unref(&c->prev);
331 AVCodec ff_dxa_decoder = {
333 .type = AVMEDIA_TYPE_VIDEO,
334 .id = AV_CODEC_ID_DXA,
335 .priv_data_size = sizeof(DxaDecContext),
338 .decode = decode_frame,
339 .capabilities = CODEC_CAP_DR1,
340 .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),