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 "codec_internal.h"
35 #include "internal.h"
36
37 #include <zlib.h>
38
39 /*
40 * Decoder context
41 */
42 typedef struct DxaDecContext {
43 AVFrame *prev;
44
45 int dsize;
46 #define DECOMP_BUF_PADDING 16
47 uint8_t *decomp_buf;
48 uint32_t pal[256];
49 } DxaDecContext;
50
51 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
52 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
53
decode_13(AVCodecContext * avctx,DxaDecContext * c,uint8_t * dst,int stride,uint8_t * src,int srcsize,uint8_t * ref)54 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
55 int stride, uint8_t *src, int srcsize, uint8_t *ref)
56 {
57 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
58 uint8_t *src_end = src + srcsize;
59 int i, j, k;
60 int type, x, y, d, d2;
61 uint32_t mask;
62
63 if (12ULL + ((avctx->width * avctx->height) >> 4) + AV_RB32(src + 0) + AV_RB32(src + 4) > srcsize)
64 return AVERROR_INVALIDDATA;
65
66 code = src + 12;
67 data = code + ((avctx->width * avctx->height) >> 4);
68 mv = data + AV_RB32(src + 0);
69 msk = mv + AV_RB32(src + 4);
70
71 for(j = 0; j < avctx->height; j += 4){
72 for(i = 0; i < avctx->width; i += 4){
73 if (data > src_end || mv > src_end || msk > src_end)
74 return AVERROR_INVALIDDATA;
75 tmp = dst + i;
76 tmp2 = ref + i;
77 type = *code++;
78 switch(type){
79 case 4: // motion compensation
80 x = (*mv) >> 4; if(x & 8) x = 8 - x;
81 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
82 if (i < -x || avctx->width - i - 4 < x ||
83 j < -y || avctx->height - j - 4 < y) {
84 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
85 return AVERROR_INVALIDDATA;
86 }
87 tmp2 += x + y*stride;
88 case 0: // skip
89 case 5: // skip in method 12
90 for(y = 0; y < 4; y++){
91 memcpy(tmp, tmp2, 4);
92 tmp += stride;
93 tmp2 += stride;
94 }
95 break;
96 case 1: // masked change
97 case 10: // masked change with only half of pixels changed
98 case 11: // cases 10-15 are for method 12 only
99 case 12:
100 case 13:
101 case 14:
102 case 15:
103 if(type == 1){
104 mask = AV_RB16(msk);
105 msk += 2;
106 }else{
107 type -= 10;
108 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
109 msk++;
110 }
111 for(y = 0; y < 4; y++){
112 for(x = 0; x < 4; x++){
113 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
114 mask <<= 1;
115 }
116 tmp += stride;
117 tmp2 += stride;
118 }
119 break;
120 case 2: // fill block
121 for(y = 0; y < 4; y++){
122 memset(tmp, data[0], 4);
123 tmp += stride;
124 }
125 data++;
126 break;
127 case 3: // raw block
128 for(y = 0; y < 4; y++){
129 memcpy(tmp, data, 4);
130 data += 4;
131 tmp += stride;
132 }
133 break;
134 case 8: // subblocks - method 13 only
135 mask = *msk++;
136 for(k = 0; k < 4; k++){
137 d = ((k & 1) << 1) + ((k & 2) * stride);
138 d2 = ((k & 1) << 1) + ((k & 2) * stride);
139 tmp2 = ref + i + d2;
140 switch(mask & 0xC0){
141 case 0x80: // motion compensation
142 x = (*mv) >> 4; if(x & 8) x = 8 - x;
143 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
144 if (i + 2*(k & 1) < -x || avctx->width - i - 2*(k & 1) - 2 < x ||
145 j + (k & 2) < -y || avctx->height - j - (k & 2) - 2 < y) {
146 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
147 return AVERROR_INVALIDDATA;
148 }
149 tmp2 += x + y*stride;
150 case 0x00: // skip
151 tmp[d + 0 ] = tmp2[0];
152 tmp[d + 1 ] = tmp2[1];
153 tmp[d + 0 + stride] = tmp2[0 + stride];
154 tmp[d + 1 + stride] = tmp2[1 + stride];
155 break;
156 case 0x40: // fill
157 tmp[d + 0 ] = data[0];
158 tmp[d + 1 ] = data[0];
159 tmp[d + 0 + stride] = data[0];
160 tmp[d + 1 + stride] = data[0];
161 data++;
162 break;
163 case 0xC0: // raw
164 tmp[d + 0 ] = *data++;
165 tmp[d + 1 ] = *data++;
166 tmp[d + 0 + stride] = *data++;
167 tmp[d + 1 + stride] = *data++;
168 break;
169 }
170 mask <<= 2;
171 }
172 break;
173 case 32: // vector quantization - 2 colors
174 mask = AV_RB16(msk);
175 msk += 2;
176 for(y = 0; y < 4; y++){
177 for(x = 0; x < 4; x++){
178 tmp[x] = data[mask & 1];
179 mask >>= 1;
180 }
181 tmp += stride;
182 tmp2 += stride;
183 }
184 data += 2;
185 break;
186 case 33: // vector quantization - 3 or 4 colors
187 case 34:
188 mask = AV_RB32(msk);
189 msk += 4;
190 for(y = 0; y < 4; y++){
191 for(x = 0; x < 4; x++){
192 tmp[x] = data[mask & 3];
193 mask >>= 2;
194 }
195 tmp += stride;
196 tmp2 += stride;
197 }
198 data += type - 30;
199 break;
200 default:
201 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
202 return AVERROR_INVALIDDATA;
203 }
204 }
205 dst += stride * 4;
206 ref += stride * 4;
207 }
208 return 0;
209 }
210
decode_frame(AVCodecContext * avctx,AVFrame * frame,int * got_frame,AVPacket * avpkt)211 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
212 int *got_frame, AVPacket *avpkt)
213 {
214 DxaDecContext * const c = avctx->priv_data;
215 uint8_t *outptr, *srcptr, *tmpptr;
216 unsigned long dsize;
217 int i, j, compr, ret;
218 int stride;
219 int pc = 0;
220 GetByteContext gb;
221
222 bytestream2_init(&gb, avpkt->data, avpkt->size);
223
224 /* make the palette available on the way out */
225 if (bytestream2_peek_le32(&gb) == MKTAG('C','M','A','P')) {
226 bytestream2_skip(&gb, 4);
227 for(i = 0; i < 256; i++){
228 c->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
229 }
230 pc = 1;
231 }
232
233 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
234 return ret;
235 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
236 frame->palette_has_changed = pc;
237
238 outptr = frame->data[0];
239 srcptr = c->decomp_buf;
240 tmpptr = c->prev->data[0];
241 stride = frame->linesize[0];
242
243 if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
244 compr = -1;
245 else
246 compr = bytestream2_get_byte(&gb);
247
248 dsize = c->dsize;
249 if (compr != 4 && compr != -1) {
250 bytestream2_skip(&gb, 4);
251 if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
252 bytestream2_get_bytes_left(&gb)) != Z_OK) {
253 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
254 return AVERROR_UNKNOWN;
255 }
256 memset(c->decomp_buf + dsize, 0, DECOMP_BUF_PADDING);
257 }
258
259 if (avctx->debug & FF_DEBUG_PICT_INFO)
260 av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
261
262 switch(compr){
263 case -1:
264 frame->key_frame = 0;
265 frame->pict_type = AV_PICTURE_TYPE_P;
266 if (c->prev->data[0])
267 memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
268 else{ // Should happen only when first frame is 'NULL'
269 memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
270 frame->key_frame = 1;
271 frame->pict_type = AV_PICTURE_TYPE_I;
272 }
273 break;
274 case 2:
275 case 4:
276 frame->key_frame = 1;
277 frame->pict_type = AV_PICTURE_TYPE_I;
278 for (j = 0; j < avctx->height; j++) {
279 memcpy(outptr, srcptr, avctx->width);
280 outptr += stride;
281 srcptr += avctx->width;
282 }
283 break;
284 case 3:
285 case 5:
286 if (!tmpptr) {
287 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
288 if (!(avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
289 return AVERROR_INVALIDDATA;
290 }
291 frame->key_frame = 0;
292 frame->pict_type = AV_PICTURE_TYPE_P;
293 for (j = 0; j < avctx->height; j++) {
294 if(tmpptr){
295 for(i = 0; i < avctx->width; i++)
296 outptr[i] = srcptr[i] ^ tmpptr[i];
297 tmpptr += stride;
298 }else
299 memcpy(outptr, srcptr, avctx->width);
300 outptr += stride;
301 srcptr += avctx->width;
302 }
303 break;
304 case 12: // ScummVM coding
305 case 13:
306 frame->key_frame = 0;
307 frame->pict_type = AV_PICTURE_TYPE_P;
308 if (!c->prev->data[0]) {
309 av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
310 return AVERROR_INVALIDDATA;
311 }
312 decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, dsize, c->prev->data[0]);
313 break;
314 default:
315 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
316 return AVERROR_INVALIDDATA;
317 }
318
319 av_frame_unref(c->prev);
320 if ((ret = av_frame_ref(c->prev, frame)) < 0)
321 return ret;
322
323 *got_frame = 1;
324
325 /* always report that the buffer was completely consumed */
326 return avpkt->size;
327 }
328
decode_init(AVCodecContext * avctx)329 static av_cold int decode_init(AVCodecContext *avctx)
330 {
331 DxaDecContext * const c = avctx->priv_data;
332
333 if (avctx->width%4 || avctx->height%4) {
334 avpriv_request_sample(avctx, "dimensions are not a multiple of 4");
335 return AVERROR_INVALIDDATA;
336 }
337
338 c->prev = av_frame_alloc();
339 if (!c->prev)
340 return AVERROR(ENOMEM);
341
342 avctx->pix_fmt = AV_PIX_FMT_PAL8;
343
344 c->dsize = avctx->width * avctx->height * 2;
345 c->decomp_buf = av_malloc(c->dsize + DECOMP_BUF_PADDING);
346 if (!c->decomp_buf) {
347 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
348 return AVERROR(ENOMEM);
349 }
350
351 return 0;
352 }
353
decode_end(AVCodecContext * avctx)354 static av_cold int decode_end(AVCodecContext *avctx)
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
364 const FFCodec ff_dxa_decoder = {
365 .p.name = "dxa",
366 .p.long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
367 .p.type = AVMEDIA_TYPE_VIDEO,
368 .p.id = AV_CODEC_ID_DXA,
369 .priv_data_size = sizeof(DxaDecContext),
370 .init = decode_init,
371 .close = decode_end,
372 FF_CODEC_DECODE_CB(decode_frame),
373 .p.capabilities = AV_CODEC_CAP_DR1,
374 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
375 };
376