dfa.c 13.3 KB
Newer Older
1 2 3 4 5
/*
 * Chronomaster DFA Video Decoder
 * Copyright (c) 2011 Konstantin Shishkov
 * based on work by Vladimir "VAG" Gneushev
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

23 24
#include <inttypes.h>

25 26
#include "avcodec.h"
#include "bytestream.h"
27
#include "internal.h"
28

29
#include "libavutil/avassert.h"
30
#include "libavutil/imgutils.h"
31
#include "libavutil/mem.h"
32 33 34 35 36 37 38 39 40 41

typedef struct DfaContext {
    uint32_t pal[256];
    uint8_t *frame_buf;
} DfaContext;

static av_cold int dfa_decode_init(AVCodecContext *avctx)
{
    DfaContext *s = avctx->priv_data;

42
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
43

44
    if (!avctx->width || !avctx->height || FFMAX(avctx->width, avctx->height) >= (1<<16))
45 46 47
        return AVERROR_INVALIDDATA;

    av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
48

49
    s->frame_buf = av_mallocz(avctx->width * avctx->height);
50 51 52 53 54 55
    if (!s->frame_buf)
        return AVERROR(ENOMEM);

    return 0;
}

56
static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
57 58 59
{
    const int size = width * height;

60
    if (bytestream2_get_buffer(gb, frame, size) != size)
61
        return AVERROR_INVALIDDATA;
62 63 64
    return 0;
}

65
static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
66 67 68 69
{
    const uint8_t *frame_start = frame;
    const uint8_t *frame_end   = frame + width * height;
    int mask = 0x10000, bitbuf = 0;
70 71
    int v, count;
    unsigned segments;
72
    unsigned offset;
73

74 75
    segments = bytestream2_get_le32(gb);
    offset   = bytestream2_get_le32(gb);
76 77
    if (segments == 0 && offset == frame_end - frame)
        return 0; // skip frame
78
    if (frame_end - frame <= offset)
79
        return AVERROR_INVALIDDATA;
80
    frame += offset;
81
    while (segments--) {
82
        if (bytestream2_get_bytes_left(gb) < 2)
83
            return AVERROR_INVALIDDATA;
84
        if (mask == 0x10000) {
85
            bitbuf = bytestream2_get_le16u(gb);
86 87
            mask = 1;
        }
88
        if (frame_end - frame < 2)
89
            return AVERROR_INVALIDDATA;
90
        if (bitbuf & mask) {
91
            v = bytestream2_get_le16(gb);
92 93
            offset = (v & 0x1FFF) << 1;
            count = ((v >> 13) + 2) << 1;
94
            if (frame - frame_start < offset || frame_end - frame < count)
95
                return AVERROR_INVALIDDATA;
96 97 98
            av_memcpy_backptr(frame, offset, count);
            frame += count;
        } else {
99 100
            *frame++ = bytestream2_get_byte(gb);
            *frame++ = bytestream2_get_byte(gb);
101 102 103 104 105 106 107
        }
        mask <<= 1;
    }

    return 0;
}

108
static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
109 110 111 112 113 114
{
    const uint8_t *frame_start = frame;
    const uint8_t *frame_end   = frame + width * height;
    int mask = 0x10000, bitbuf = 0;
    int v, offset, count, segments;

115
    segments = bytestream2_get_le16(gb);
116
    while (segments--) {
117
        if (bytestream2_get_bytes_left(gb) < 2)
118
            return AVERROR_INVALIDDATA;
119
        if (mask == 0x10000) {
120
            bitbuf = bytestream2_get_le16u(gb);
121 122
            mask = 1;
        }
123
        if (frame_end - frame < 2)
124
            return AVERROR_INVALIDDATA;
125
        if (bitbuf & mask) {
126
            v = bytestream2_get_le16(gb);
127 128
            offset = (v & 0x1FFF) << 1;
            count = ((v >> 13) + 2) << 1;
129
            if (frame - frame_start < offset || frame_end - frame < count)
130
                return AVERROR_INVALIDDATA;
131
            av_memcpy_backptr(frame, offset, count);
132 133
            frame += count;
        } else if (bitbuf & (mask << 1)) {
134
            frame += bytestream2_get_le16(gb);
135
        } else {
136 137
            *frame++ = bytestream2_get_byte(gb);
            *frame++ = bytestream2_get_byte(gb);
138 139 140 141 142 143 144
        }
        mask <<= 2;
    }

    return 0;
}

145
static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
146 147 148 149 150 151
{
    const uint8_t *frame_start = frame;
    const uint8_t *frame_end   = frame + width * height;
    int mask = 0x10000, bitbuf = 0;
    int i, v, offset, count, segments;

152 153
    if ((width | height) & 1)
        return AVERROR_INVALIDDATA;
154
    segments = bytestream2_get_le16(gb);
155
    while (segments--) {
156
        if (bytestream2_get_bytes_left(gb) < 2)
157
            return AVERROR_INVALIDDATA;
158
        if (mask == 0x10000) {
159
            bitbuf = bytestream2_get_le16u(gb);
160 161
            mask = 1;
        }
162

163
        if (bitbuf & mask) {
164
            v = bytestream2_get_le16(gb);
165 166
            offset = (v & 0x1FFF) << 2;
            count = ((v >> 13) + 2) << 1;
167
            if (frame - frame_start < offset || frame_end - frame < count*2 + width)
168
                return AVERROR_INVALIDDATA;
169 170 171 172 173 174 175
            for (i = 0; i < count; i++) {
                frame[0] = frame[1] =
                frame[width] = frame[width + 1] = frame[-offset];

                frame += 2;
            }
        } else if (bitbuf & (mask << 1)) {
176 177 178 179
            v = bytestream2_get_le16(gb)*2;
            if (frame - frame_end < v)
                return AVERROR_INVALIDDATA;
            frame += v;
180
        } else {
181
            if (width < 4 || frame_end - frame < width + 4)
182
                return AVERROR_INVALIDDATA;
183
            frame[0] = frame[1] =
184
            frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
185 186
            frame += 2;
            frame[0] = frame[1] =
187
            frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
188 189 190 191 192 193 194 195
            frame += 2;
        }
        mask <<= 2;
    }

    return 0;
}

196
static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
197 198 199 200
{
    uint8_t *line_ptr;
    int count, lines, segments;

201
    count = bytestream2_get_le16(gb);
202
    if (count >= height)
203
        return AVERROR_INVALIDDATA;
204
    frame += width * count;
205 206
    lines = bytestream2_get_le16(gb);
    if (count + lines > height)
207
        return AVERROR_INVALIDDATA;
208 209

    while (lines--) {
210
        if (bytestream2_get_bytes_left(gb) < 1)
211
            return AVERROR_INVALIDDATA;
212 213
        line_ptr = frame;
        frame += width;
214
        segments = bytestream2_get_byteu(gb);
215
        while (segments--) {
216
            if (frame - line_ptr <= bytestream2_peek_byte(gb))
217
                return AVERROR_INVALIDDATA;
218 219
            line_ptr += bytestream2_get_byte(gb);
            count = (int8_t)bytestream2_get_byte(gb);
220
            if (count >= 0) {
221
                if (frame - line_ptr < count)
222
                    return AVERROR_INVALIDDATA;
223
                if (bytestream2_get_buffer(gb, line_ptr, count) != count)
224
                    return AVERROR_INVALIDDATA;
225 226
            } else {
                count = -count;
227
                if (frame - line_ptr < count)
228
                    return AVERROR_INVALIDDATA;
229
                memset(line_ptr, bytestream2_get_byte(gb), count);
230 231 232 233 234 235 236 237
            }
            line_ptr += count;
        }
    }

    return 0;
}

238
static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
239 240 241 242
{
    const uint8_t *frame_end   = frame + width * height;
    uint8_t *line_ptr;
    int count, i, v, lines, segments;
243
    int y = 0;
244

245 246
    lines = bytestream2_get_le16(gb);
    if (lines > height)
247
        return AVERROR_INVALIDDATA;
248 249

    while (lines--) {
250
        if (bytestream2_get_bytes_left(gb) < 2)
251
            return AVERROR_INVALIDDATA;
252
        segments = bytestream2_get_le16u(gb);
253
        while ((segments & 0xC000) == 0xC000) {
254
            unsigned skip_lines = -(int16_t)segments;
255
            int64_t delta = -((int16_t)segments * (int64_t)width);
256
            if (frame_end - frame <= delta || y + lines + skip_lines > height)
257
                return AVERROR_INVALIDDATA;
258
            frame    += delta;
259
            y        += skip_lines;
260
            segments = bytestream2_get_le16(gb);
261
        }
262 263 264

        if (frame_end <= frame)
            return AVERROR_INVALIDDATA;
265 266
        if (segments & 0x8000) {
            frame[width - 1] = segments & 0xFF;
267
            segments = bytestream2_get_le16(gb);
268 269
        }
        line_ptr = frame;
270 271
        if (frame_end - frame < width)
            return AVERROR_INVALIDDATA;
272
        frame += width;
273
        y++;
274
        while (segments--) {
275
            if (frame - line_ptr <= bytestream2_peek_byte(gb))
276
                return AVERROR_INVALIDDATA;
277 278
            line_ptr += bytestream2_get_byte(gb);
            count = (int8_t)bytestream2_get_byte(gb);
279
            if (count >= 0) {
280
                if (frame - line_ptr < count * 2)
281
                    return AVERROR_INVALIDDATA;
282
                if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
283
                    return AVERROR_INVALIDDATA;
284 285 286
                line_ptr += count * 2;
            } else {
                count = -count;
287
                if (frame - line_ptr < count * 2)
288
                    return AVERROR_INVALIDDATA;
289
                v = bytestream2_get_le16(gb);
290 291 292 293 294 295 296 297 298
                for (i = 0; i < count; i++)
                    bytestream_put_le16(&line_ptr, v);
            }
        }
    }

    return 0;
}

299
static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
300
{
301 302
    const uint8_t *frame_end = frame + width * height;
    uint32_t segments = bytestream2_get_le32(gb);
303
    int skip, copy;
304 305

    while (segments--) {
306
        if (bytestream2_get_bytes_left(gb) < 2)
307
            return AVERROR_INVALIDDATA;
308 309 310 311
        copy = bytestream2_get_byteu(gb) * 2;
        skip = bytestream2_get_byteu(gb) * 2;
        if (frame_end - frame < copy + skip ||
            bytestream2_get_bytes_left(gb) < copy)
312
            return AVERROR_INVALIDDATA;
313 314 315
        frame += skip;
        bytestream2_get_buffer(gb, frame, copy);
        frame += copy;
316 317 318
    }

    return 0;
319 320
}

321
static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
322 323 324 325 326 327
{
    memset(frame, 0, width * height);
    return 0;
}


328
typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
329 330 331

static const chunk_decoder decoder[8] = {
    decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
332
    decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
333 334
};

335
static const char * const chunk_name[8] = {
336
    "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
337 338 339
};

static int dfa_decode_frame(AVCodecContext *avctx,
340
                            void *data, int *got_frame,
341 342
                            AVPacket *avpkt)
{
343
    AVFrame *frame = data;
344
    DfaContext *s = avctx->priv_data;
345
    GetByteContext gb;
346 347 348 349 350
    const uint8_t *buf = avpkt->data;
    uint32_t chunk_type, chunk_size;
    uint8_t *dst;
    int ret;
    int i, pal_elems;
351
    int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
352

353
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
354 355
        return ret;

356 357
    bytestream2_init(&gb, avpkt->data, avpkt->size);
    while (bytestream2_get_bytes_left(&gb) > 0) {
358 359
        if (bytestream2_get_bytes_left(&gb) < 12)
            return AVERROR_INVALIDDATA;
360 361 362
        bytestream2_skip(&gb, 4);
        chunk_size = bytestream2_get_le32(&gb);
        chunk_type = bytestream2_get_le32(&gb);
363 364 365 366 367
        if (!chunk_type)
            break;
        if (chunk_type == 1) {
            pal_elems = FFMIN(chunk_size / 3, 256);
            for (i = 0; i < pal_elems; i++) {
368
                s->pal[i] = bytestream2_get_be24(&gb) << 2;
369
                s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
370
            }
371
            frame->palette_has_changed = 1;
372
        } else if (chunk_type <= 9) {
373
            if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
374 375
                av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
                       chunk_name[chunk_type - 2]);
376
                return AVERROR_INVALIDDATA;
377 378
            }
        } else {
379 380
            av_log(avctx, AV_LOG_WARNING,
                   "Ignoring unknown chunk type %"PRIu32"\n",
381 382 383 384 385 386
                   chunk_type);
        }
        buf += chunk_size;
    }

    buf = s->frame_buf;
387
    dst = frame->data[0];
388
    for (i = 0; i < avctx->height; i++) {
389 390 391 392 393 394 395 396 397 398
        if(version == 0x100) {
            int j;
            for(j = 0; j < avctx->width; j++) {
                dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
                             ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
            }
        } else {
            memcpy(dst, buf, avctx->width);
            buf += avctx->width;
        }
399
        dst += frame->linesize[0];
400
    }
401
    memcpy(frame->data[1], s->pal, sizeof(s->pal));
402

403
    *got_frame = 1;
404 405 406 407 408 409 410 411 412 413 414 415 416 417

    return avpkt->size;
}

static av_cold int dfa_decode_end(AVCodecContext *avctx)
{
    DfaContext *s = avctx->priv_data;

    av_freep(&s->frame_buf);

    return 0;
}

AVCodec ff_dfa_decoder = {
418
    .name           = "dfa",
419
    .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
420
    .type           = AVMEDIA_TYPE_VIDEO,
421
    .id             = AV_CODEC_ID_DFA,
422 423 424 425
    .priv_data_size = sizeof(DfaContext),
    .init           = dfa_decode_init,
    .close          = dfa_decode_end,
    .decode         = dfa_decode_frame,
426
    .capabilities   = AV_CODEC_CAP_DR1,
427
};