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, uint8_t *src, uint8_t *ref)
54 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
56 int type, x, y, d, d2;
57 int stride = c->pic.linesize[0];
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;
76 case 5: // skip in method 12
77 for(y = 0; y < 4; y++){
83 case 1: // masked change
84 case 10: // masked change with only half of pixels changed
85 case 11: // cases 10-15 are for method 12 only
95 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
98 for(y = 0; y < 4; y++){
99 for(x = 0; x < 4; x++){
100 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
107 case 2: // fill block
108 for(y = 0; y < 4; y++){
109 memset(tmp, data[0], 4);
115 for(y = 0; y < 4; y++){
116 memcpy(tmp, data, 4);
121 case 8: // subblocks - method 13 only
123 for(k = 0; k < 4; k++){
124 d = ((k & 1) << 1) + ((k & 2) * stride);
125 d2 = ((k & 1) << 1) + ((k & 2) * stride);
128 case 0x80: // motion compensation
129 x = (*mv) >> 4; if(x & 8) x = 8 - x;
130 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
131 tmp2 += x + y*stride;
133 tmp[d + 0 ] = tmp2[0];
134 tmp[d + 1 ] = tmp2[1];
135 tmp[d + 0 + stride] = tmp2[0 + stride];
136 tmp[d + 1 + stride] = tmp2[1 + stride];
139 tmp[d + 0 ] = data[0];
140 tmp[d + 1 ] = data[0];
141 tmp[d + 0 + stride] = data[0];
142 tmp[d + 1 + stride] = data[0];
146 tmp[d + 0 ] = *data++;
147 tmp[d + 1 ] = *data++;
148 tmp[d + 0 + stride] = *data++;
149 tmp[d + 1 + stride] = *data++;
155 case 32: // vector quantization - 2 colors
158 for(y = 0; y < 4; y++){
159 for(x = 0; x < 4; x++){
160 tmp[x] = data[mask & 1];
168 case 33: // vector quantization - 3 or 4 colors
172 for(y = 0; y < 4; y++){
173 for(x = 0; x < 4; x++){
174 tmp[x] = data[mask & 3];
183 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
184 return AVERROR_INVALIDDATA;
193 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
195 DxaDecContext * const c = avctx->priv_data;
196 uint8_t *outptr, *srcptr, *tmpptr;
198 int i, j, compr, ret;
203 bytestream2_init(&gb, avpkt->data, avpkt->size);
205 /* make the palette available on the way out */
206 if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
207 bytestream2_skip(&gb, 4);
208 for(i = 0; i < 256; i++){
209 c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
214 if ((ret = ff_get_buffer(avctx, &c->pic)) < 0){
215 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
218 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
219 c->pic.palette_has_changed = pc;
221 outptr = c->pic.data[0];
222 srcptr = c->decomp_buf;
223 tmpptr = c->prev.data[0];
224 stride = c->pic.linesize[0];
226 if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
229 compr = bytestream2_get_byte(&gb);
232 if (compr != 4 && compr != -1) {
233 bytestream2_skip(&gb, 4);
234 if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
235 bytestream2_get_bytes_left(&gb)) != Z_OK) {
236 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
237 return AVERROR_INVALIDDATA;
242 c->pic.key_frame = 0;
243 c->pic.pict_type = AV_PICTURE_TYPE_P;
245 memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
246 else{ // Should happen only when first frame is 'NULL'
247 memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
248 c->pic.key_frame = 1;
249 c->pic.pict_type = AV_PICTURE_TYPE_I;
256 c->pic.key_frame = !(compr & 1);
257 c->pic.pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
258 for(j = 0; j < avctx->height; j++){
259 if((compr & 1) && tmpptr){
260 for(i = 0; i < avctx->width; i++)
261 outptr[i] = srcptr[i] ^ tmpptr[i];
264 memcpy(outptr, srcptr, avctx->width);
266 srcptr += avctx->width;
269 case 12: // ScummVM coding
271 c->pic.key_frame = 0;
272 c->pic.pict_type = AV_PICTURE_TYPE_P;
273 if (!c->prev.data[0]) {
274 av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
275 return AVERROR_INVALIDDATA;
277 decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
280 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
281 return AVERROR_INVALIDDATA;
284 FFSWAP(AVFrame, c->pic, c->prev);
286 avctx->release_buffer(avctx, &c->pic);
289 *(AVFrame*)data = c->prev;
291 /* always report that the buffer was completely consumed */
295 static av_cold int decode_init(AVCodecContext *avctx)
297 DxaDecContext * const c = avctx->priv_data;
299 avctx->pix_fmt = AV_PIX_FMT_PAL8;
301 avcodec_get_frame_defaults(&c->pic);
302 avcodec_get_frame_defaults(&c->prev);
304 c->dsize = avctx->width * avctx->height * 2;
305 c->decomp_buf = av_malloc(c->dsize);
306 if (!c->decomp_buf) {
307 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
308 return AVERROR(ENOMEM);
314 static av_cold int decode_end(AVCodecContext *avctx)
316 DxaDecContext * const c = avctx->priv_data;
318 av_freep(&c->decomp_buf);
320 avctx->release_buffer(avctx, &c->prev);
322 avctx->release_buffer(avctx, &c->pic);
327 AVCodec ff_dxa_decoder = {
329 .type = AVMEDIA_TYPE_VIDEO,
330 .id = AV_CODEC_ID_DXA,
331 .priv_data_size = sizeof(DxaDecContext),
334 .decode = decode_frame,
335 .capabilities = CODEC_CAP_DR1,
336 .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),