qdrw.c 4.21 KB
Newer Older
1 2 3 4
/*
 * QuickDraw (qdrw) codec
 * Copyright (c) 2004 Konstantin Shishkov
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * 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
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 25
 * Apple QuickDraw codec.
 */
26

27
#include "libavutil/common.h"
28
#include "libavutil/intreadwrite.h"
29
#include "avcodec.h"
30
#include "internal.h"
31

32
static int decode_frame(AVCodecContext *avctx,
33
                        void *data, int *got_frame,
34
                        AVPacket *avpkt)
35
{
36
    const uint8_t *buf     = avpkt->data;
37
    const uint8_t *buf_end = avpkt->data + avpkt->size;
38
    int buf_size           = avpkt->size;
39
    AVFrame * const p      = data;
40 41
    uint8_t* outdata;
    int colors;
42
    int i, ret;
43 44
    uint32_t *pal;
    int r, g, b;
45

46
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
47
        return ret;
48 49
    p->pict_type = AV_PICTURE_TYPE_I;
    p->key_frame = 1;
50

51
    outdata = p->data[0];
52

53 54
    if (buf_end - buf < 0x68 + 4)
        return AVERROR_INVALIDDATA;
55
    buf   += 0x68; /* jump to palette */
56
    colors = AV_RB32(buf);
57
    buf   += 4;
58

59
    if (colors < 0 || colors > 256) {
60
        av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
61
        return AVERROR_INVALIDDATA;
62
    }
63 64
    if (buf_end - buf < (colors + 1) * 8)
        return AVERROR_INVALIDDATA;
65

66
    pal = (uint32_t*)p->data[1];
67
    for (i = 0; i <= colors; i++) {
68
        unsigned int idx;
69
        idx = AV_RB16(buf); /* color index */
70
        buf += 2;
71

72 73 74 75 76
        if (idx > 255) {
            av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
            buf += 6;
            continue;
        }
77
        r = *buf++;
78
        buf++;
79
        g = *buf++;
80
        buf++;
81
        b = *buf++;
82
        buf++;
83
        pal[idx] = 0xFFU << 24 | r << 16 | g << 8 | b;
84
    }
85
    p->palette_has_changed = 1;
86

87 88
    if (buf_end - buf < 18)
        return AVERROR_INVALIDDATA;
89 90 91
    buf += 18; /* skip unneeded data */
    for (i = 0; i < avctx->height; i++) {
        int size, left, code, pix;
Michael Niedermayer's avatar
Michael Niedermayer committed
92
        const uint8_t *next;
93 94
        uint8_t *out;
        int tsize = 0;
95

96
        /* decode line */
97
        out  = outdata;
98
        size = AV_RB16(buf); /* size of packed line */
99
        buf += 2;
100 101 102
        if (buf_end - buf < size)
            return AVERROR_INVALIDDATA;

103 104 105 106 107 108
        left = size;
        next = buf + size;
        while (left > 0) {
            code = *buf++;
            if (code & 0x80 ) { /* run */
                pix = *buf++;
109
                if ((out + (257 - code)) > (outdata +  p->linesize[0]))
110
                    break;
111
                memset(out, pix, 257 - code);
112
                out   += 257 - code;
113
                tsize += 257 - code;
114
                left  -= 2;
115
            } else { /* copy */
116
                if ((out + code) > (outdata +  p->linesize[0]))
117
                    break;
118 119
                if (buf_end - buf < code + 1)
                    return AVERROR_INVALIDDATA;
120
                memcpy(out, buf, code + 1);
121 122 123
                out   += code + 1;
                buf   += code + 1;
                left  -= 2 + code;
124 125 126 127
                tsize += code + 1;
            }
        }
        buf = next;
128
        outdata += p->linesize[0];
129 130
    }

131
    *got_frame      = 1;
132

133 134 135
    return buf_size;
}

136 137
static av_cold int decode_init(AVCodecContext *avctx)
{
138
    avctx->pix_fmt= AV_PIX_FMT_PAL8;
139 140 141 142

    return 0;
}

143
AVCodec ff_qdraw_decoder = {
144
    .name           = "qdraw",
145
    .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
146
    .type           = AVMEDIA_TYPE_VIDEO,
147
    .id             = AV_CODEC_ID_QDRAW,
148 149 150
    .init           = decode_init,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
151
};