FFmpeg
Loading...
Searching...
No Matches
dxa.c
Go to the documentation of this file.
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
29#include "libavutil/mem.h"
30#include "bytestream.h"
31#include "avcodec.h"
32#include "codec_internal.h"
33#include "decode.h"
34
35#include <zlib.h>
36
37/*
38 * Decoder context
39 */
40typedef struct DxaDecContext {
42
43 int dsize;
44#define DECOMP_BUF_PADDING 16
45 uint8_t *decomp_buf;
46 uint32_t pal[256];
48
49static const uint8_t shift1[6] = { 0, 8, 8, 8, 4, 4 };
50static const uint8_t shift2[6] = { 0, 0, 8, 4, 0, 4 };
51
52static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
53 int stride, uint8_t *src, int srcsize, uint8_t *ref)
54{
55 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
56 uint8_t *src_end = src + srcsize;
57 int i, j, k;
58 int type, x, y, d, d2;
59 uint32_t mask;
60
61 if (12ULL + ((avctx->width * avctx->height) >> 4) + AV_RB32(src + 0) + AV_RB32(src + 4) > srcsize)
63
64 code = src + 12;
65 data = code + ((avctx->width * avctx->height) >> 4);
66 mv = data + AV_RB32(src + 0);
67 msk = mv + AV_RB32(src + 4);
68
69 for(j = 0; j < avctx->height; j += 4){
70 for(i = 0; i < avctx->width; i += 4){
71 if (data > src_end || mv > src_end || msk > src_end)
73 tmp = dst + i;
74 tmp2 = ref + i;
75 type = *code++;
76 switch(type){
77 case 4: // motion compensation
78 x = (*mv) >> 4; if(x & 8) x = 8 - x;
79 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
80 if (i < -x || avctx->width - i - 4 < x ||
81 j < -y || avctx->height - j - 4 < y) {
82 av_log(avctx, AV_LOG_ERROR, "MV %d %d out of bounds\n", x,y);
84 }
85 tmp2 += x + y*stride;
87 case 0: // skip
88 case 5: // skip in method 12
89 for(y = 0; y < 4; y++){
90 memcpy(tmp, tmp2, 4);
91 tmp += stride;
92 tmp2 += stride;
93 }
94 break;
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
98 case 12:
99 case 13:
100 case 14:
101 case 15:
102 if(type == 1){
103 mask = AV_RB16(msk);
104 msk += 2;
105 }else{
106 type -= 10;
107 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
108 msk++;
109 }
110 for(y = 0; y < 4; y++){
111 for(x = 0; x < 4; x++){
112 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
113 mask <<= 1;
114 }
115 tmp += stride;
116 tmp2 += stride;
117 }
118 break;
119 case 2: // fill block
120 for(y = 0; y < 4; y++){
121 memset(tmp, data[0], 4);
122 tmp += stride;
123 }
124 data++;
125 break;
126 case 3: // raw block
127 for(y = 0; y < 4; y++){
128 memcpy(tmp, data, 4);
129 data += 4;
130 tmp += stride;
131 }
132 break;
133 case 8: // subblocks - method 13 only
134 mask = *msk++;
135 for(k = 0; k < 4; k++){
136 d = ((k & 1) << 1) + ((k & 2) * stride);
137 d2 = ((k & 1) << 1) + ((k & 2) * stride);
138 tmp2 = ref + i + d2;
139 switch(mask & 0xC0){
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;
147 }
148 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
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;
220
221 bytestream2_init(&gb, avpkt->data, avpkt->size);
222
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);
228 }
229 }
230
231 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
232 return ret;
233 memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
234
235 outptr = frame->data[0];
236 srcptr = c->decomp_buf;
237 tmpptr = c->prev->data[0];
238 stride = frame->linesize[0];
239
240 if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
241 compr = -1;
242 else
243 compr = bytestream2_get_byte(&gb);
244
245 dsize = c->dsize;
246 if (compr != 4 && compr != -1) {
247 bytestream2_skip(&gb, 4);
248 if (uncompress(c->decomp_buf, &dsize, avpkt->data + bytestream2_tell(&gb),
249 bytestream2_get_bytes_left(&gb)) != Z_OK) {
250 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
251 return AVERROR_UNKNOWN;
252 }
253 memset(c->decomp_buf + dsize, 0, DECOMP_BUF_PADDING);
254 }
255
256 if (avctx->debug & FF_DEBUG_PICT_INFO)
257 av_log(avctx, AV_LOG_DEBUG, "compr:%2d, dsize:%d\n", compr, (int)dsize);
258
259 switch(compr){
260 case -1:
261 frame->flags &= ~AV_FRAME_FLAG_KEY;
262 frame->pict_type = AV_PICTURE_TYPE_P;
263 if (c->prev->data[0])
264 memcpy(frame->data[0], c->prev->data[0], frame->linesize[0] * avctx->height);
265 else{ // Should happen only when first frame is 'NULL'
266 memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
267 frame->flags |= AV_FRAME_FLAG_KEY;
268 frame->pict_type = AV_PICTURE_TYPE_I;
269 }
270 break;
271 case 2:
272 case 4:
273 frame->flags |= AV_FRAME_FLAG_KEY;
274 frame->pict_type = AV_PICTURE_TYPE_I;
275 for (j = 0; j < avctx->height; j++) {
276 memcpy(outptr, srcptr, avctx->width);
277 outptr += stride;
278 srcptr += avctx->width;
279 }
280 break;
281 case 3:
282 case 5:
283 if (!tmpptr) {
284 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
285 if (!(avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
286 return AVERROR_INVALIDDATA;
287 }
288 frame->flags &= ~AV_FRAME_FLAG_KEY;
289 frame->pict_type = AV_PICTURE_TYPE_P;
290 for (j = 0; j < avctx->height; j++) {
291 if(tmpptr){
292 for(i = 0; i < avctx->width; i++)
293 outptr[i] = srcptr[i] ^ tmpptr[i];
294 tmpptr += stride;
295 }else
296 memcpy(outptr, srcptr, avctx->width);
297 outptr += stride;
298 srcptr += avctx->width;
299 }
300 break;
301 case 12: // ScummVM coding
302 case 13:
303 frame->flags &= ~AV_FRAME_FLAG_KEY;
304 frame->pict_type = AV_PICTURE_TYPE_P;
305 if (!c->prev->data[0]) {
306 av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
307 return AVERROR_INVALIDDATA;
308 }
309 decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, dsize, c->prev->data[0]);
310 break;
311 default:
312 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
313 return AVERROR_INVALIDDATA;
314 }
315
316 if ((ret = av_frame_replace(c->prev, frame)) < 0)
317 return ret;
318
319 *got_frame = 1;
320
321 /* always report that the buffer was completely consumed */
322 return avpkt->size;
323}
324
326{
327 DxaDecContext * const c = avctx->priv_data;
328
329 if (avctx->width%4 || avctx->height%4) {
330 avpriv_request_sample(avctx, "dimensions are not a multiple of 4");
331 return AVERROR_INVALIDDATA;
332 }
333
334 c->prev = av_frame_alloc();
335 if (!c->prev)
336 return AVERROR(ENOMEM);
337
338 avctx->pix_fmt = AV_PIX_FMT_PAL8;
339
340 c->dsize = avctx->width * avctx->height * 2;
341 c->decomp_buf = av_malloc(c->dsize + DECOMP_BUF_PADDING);
342 if (!c->decomp_buf) {
343 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
344 return AVERROR(ENOMEM);
345 }
346
347 return 0;
348}
349
351{
352 DxaDecContext * const c = avctx->priv_data;
353
354 av_freep(&c->decomp_buf);
355 av_frame_free(&c->prev);
356
357 return 0;
358}
359
361 .p.name = "dxa",
362 CODEC_LONG_NAME("Feeble Files/ScummVM DXA"),
363 .p.type = AVMEDIA_TYPE_VIDEO,
364 .p.id = AV_CODEC_ID_DXA,
365 .priv_data_size = sizeof(DxaDecContext),
366 .init = decode_init,
367 .close = decode_end,
369 .p.capabilities = AV_CODEC_CAP_DR1,
370 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
371};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_dxa_decoder
Definition dxa.c:360
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1394
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_FLAG2_SHOW_ALL
Show all frames before the first keyframe.
Definition avcodec.h:364
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_DXA
Definition codec_id.h:148
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
cl_device_type type
#define AV_RB32(p)
#define AV_RB16(p)
static const int8_t mv[256][2]
Definition 4xm.c:81
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static const uint8_t shift1[6]
Definition dxa.c:49
static const uint8_t shift2[6]
Definition dxa.c:50
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition dxa.c:211
static av_cold int decode_init(AVCodecContext *avctx)
Definition dxa.c:325
static av_cold int decode_end(AVCodecContext *avctx)
Definition dxa.c:350
#define DECOMP_BUF_PADDING
Definition dxa.c:44
static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t *dst, int stride, uint8_t *src, int srcsize, uint8_t *ref)
Definition dxa.c:52
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
static const uint16_t mask[17]
Definition lzw.c:38
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_SIZE
Definition pixfmt.h:32
const uint8_t * code
Definition spdifenc.c:433
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
int debug
debug
Definition avcodec.h:1393
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
uint32_t pal[256]
Definition dxa.c:46
int dsize
Definition dxa.c:43
uint8_t * decomp_buf
Definition dxa.c:45
AVFrame * prev
Definition dxa.c:41
#define stride
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static double c[64]