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