qdrw.c 10.6 KB
Newer Older
1 2 3
/*
 * QuickDraw (qdrw) codec
 * Copyright (c) 2004 Konstantin Shishkov
4
 * Copyright (c) 2015 Vittorio Giovara
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

23
/**
24
 * @file
25
 * Apple QuickDraw codec.
26
 * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
27
 */
28

29
#include "libavutil/common.h"
30
#include "libavutil/intreadwrite.h"
31
#include "avcodec.h"
32
#include "bytestream.h"
33
#include "internal.h"
34

35 36 37
enum QuickdrawOpcodes {
    PACKBITSRECT = 0x0098,
    PACKBITSRGN,
38 39
    DIRECTBITSRECT,
    DIRECTBITSRGN,
40

41 42
    EOP = 0x00FF,
};
43

44 45 46 47
static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
                         uint32_t *pal, int colors)
{
    int i;
48

49
    for (i = 0; i <= colors; i++) {
50
        uint8_t r, g, b;
51
        unsigned int idx = bytestream2_get_be16(gbc); /* color index */
52
        if (idx > 255) {
53 54 55
            av_log(avctx, AV_LOG_WARNING,
                   "Palette index out of range: %u\n", idx);
            bytestream2_skip(gbc, 6);
56 57
            continue;
        }
58 59 60 61 62 63
        r = bytestream2_get_byte(gbc);
        bytestream2_skip(gbc, 1);
        g = bytestream2_get_byte(gbc);
        bytestream2_skip(gbc, 1);
        b = bytestream2_get_byte(gbc);
        bytestream2_skip(gbc, 1);
64
        pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
65
    }
66 67
    return 0;
}
68

69 70
static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
                      int step)
71
{
72 73
    int i, j;
    int offset = avctx->width * step;
74
    uint8_t *outdata = p->data[0];
75

76 77
    for (i = 0; i < avctx->height; i++) {
        int size, left, code, pix;
78
        uint8_t *out = outdata;
79
        int pos = 0;
80

81
        /* size of packed line */
82 83
        size = left = bytestream2_get_be16(gbc);
        if (bytestream2_get_bytes_left(gbc) < size)
84 85
            return AVERROR_INVALIDDATA;

86
        /* decode line */
87
        while (left > 0) {
88
            code = bytestream2_get_byte(gbc);
89
            if (code & 0x80 ) { /* run */
90
                pix = bytestream2_get_byte(gbc);
91 92 93 94 95 96 97
                for (j = 0; j < 257 - code; j++) {
                    out[pos] = pix;
                    pos += step;
                    if (pos >= offset) {
                        pos -= offset;
                        pos++;
                    }
98 99
                    if (pos >= offset)
                        return AVERROR_INVALIDDATA;
100
                }
101
                left  -= 2;
102
            } else { /* copy */
103 104 105 106 107 108 109
                for (j = 0; j < code + 1; j++) {
                    out[pos] = bytestream2_get_byte(gbc);
                    pos += step;
                    if (pos >= offset) {
                        pos -= offset;
                        pos++;
                    }
110 111
                    if (pos >= offset)
                        return AVERROR_INVALIDDATA;
112
                }
113
                left  -= 2 + code;
114 115
            }
        }
116
        outdata += p->linesize[0];
117
    }
118
    return 0;
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static int check_header(const char *buf, int buf_size)
{
    unsigned w, h, v0, v1;

    if (buf_size < 40)
        return 0;

    w = AV_RB16(buf+6);
    h = AV_RB16(buf+8);
    v0 = AV_RB16(buf+10);
    v1 = AV_RB16(buf+12);

    if (!w || !h)
        return 0;

    if (v0 == 0x1101)
        return 1;
    if (v0 == 0x0011 && v1 == 0x02FF)
        return 2;
    return 0;
}


144 145 146
static int decode_frame(AVCodecContext *avctx,
                        void *data, int *got_frame,
                        AVPacket *avpkt)
147
{
148 149 150 151
    AVFrame * const p      = data;
    GetByteContext gbc;
    int colors;
    int w, h, ret;
152
    int ver;
153

154
    bytestream2_init(&gbc, avpkt->data, avpkt->size);
155 156 157
    if (   bytestream2_get_bytes_left(&gbc) >= 552
           &&  check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
       )
158 159
        bytestream2_skip(&gbc, 512);

160 161
    ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    /* smallest PICT header */
    if (bytestream2_get_bytes_left(&gbc) < 40) {
        av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
               bytestream2_get_bytes_left(&gbc));
        return AVERROR_INVALIDDATA;
    }

    bytestream2_skip(&gbc, 6);
    h = bytestream2_get_be16(&gbc);
    w = bytestream2_get_be16(&gbc);

    ret = ff_set_dimensions(avctx, w, h);
    if (ret < 0)
        return ret;

    /* version 1 is identified by 0x1101
     * it uses byte-aligned opcodes rather than word-aligned */
179
    if (ver == 1) {
180 181
        avpriv_request_sample(avctx, "QuickDraw version 1");
        return AVERROR_PATCHWELCOME;
182 183 184
    } else if (ver != 2) {
        avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
        return AVERROR_PATCHWELCOME;
185 186
    }

187
    bytestream2_skip(&gbc, 4+26);
188 189 190

    while (bytestream2_get_bytes_left(&gbc) >= 4) {
        int bppcnt, bpp;
191
        int rowbytes, pack_type;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        int opcode = bytestream2_get_be16(&gbc);

        switch(opcode) {
        case PACKBITSRECT:
        case PACKBITSRGN:
            av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");

            bytestream2_skip(&gbc, 30);
            bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
            bpp    = bytestream2_get_be16(&gbc); /* cmpSize */

            av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
            if (bppcnt == 1 && bpp == 8) {
                avctx->pix_fmt = AV_PIX_FMT_PAL8;
            } else {
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
                       bppcnt, bpp);
                return AVERROR_INVALIDDATA;
            }

            /* jump to palette */
            bytestream2_skip(&gbc, 18);
            colors = bytestream2_get_be16(&gbc);

            if (colors < 0 || colors > 256) {
                av_log(avctx, AV_LOG_ERROR,
                       "Error color count - %i(0x%X)\n", colors, colors);
                return AVERROR_INVALIDDATA;
            }
            if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
                av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
                       bytestream2_get_bytes_left(&gbc));
                return AVERROR_INVALIDDATA;
            }
227
            if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
228 229 230 231 232 233 234 235 236 237 238 239 240
                return ret;

            parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
            p->palette_has_changed = 1;

            /* jump to image data */
            bytestream2_skip(&gbc, 18);

            if (opcode == PACKBITSRGN) {
                bytestream2_skip(&gbc, 2 + 8); /* size + rect */
                avpriv_report_missing_feature(avctx, "Packbit mask region");
            }

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
            ret = decode_rle(avctx, p, &gbc, bppcnt);
            if (ret < 0)
                return ret;
            *got_frame = 1;
            break;
        case DIRECTBITSRECT:
        case DIRECTBITSRGN:
            av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");

            bytestream2_skip(&gbc, 4);
            rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
            if (rowbytes <= 250) {
                avpriv_report_missing_feature(avctx, "Short rowbytes");
                return AVERROR_PATCHWELCOME;
            }

            bytestream2_skip(&gbc, 10);
            pack_type = bytestream2_get_be16(&gbc);

            bytestream2_skip(&gbc, 16);
            bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
            bpp    = bytestream2_get_be16(&gbc); /* cmpSize */

            av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
            if (bppcnt == 3 && bpp == 8) {
                avctx->pix_fmt = AV_PIX_FMT_RGB24;
            } else if (bppcnt == 4 && bpp == 8) {
                avctx->pix_fmt = AV_PIX_FMT_ARGB;
            } else {
                av_log(avctx, AV_LOG_ERROR,
                       "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
                       bppcnt, bpp);
                return AVERROR_INVALIDDATA;
            }

            /* set packing when default is selected */
            if (pack_type == 0)
                pack_type = bppcnt;

            if (pack_type != 3 && pack_type != 4) {
                avpriv_request_sample(avctx, "Pack type %d", pack_type);
                return AVERROR_PATCHWELCOME;
            }
284
            if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
285 286 287 288 289 290 291 292 293 294 295
                return ret;

            /* jump to data */
            bytestream2_skip(&gbc, 30);

            if (opcode == DIRECTBITSRGN) {
                bytestream2_skip(&gbc, 2 + 8); /* size + rect */
                avpriv_report_missing_feature(avctx, "DirectBit mask region");
            }

            ret = decode_rle(avctx, p, &gbc, bppcnt);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            if (ret < 0)
                return ret;
            *got_frame = 1;
            break;
        default:
            av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
            break;
        }
        /* exit the loop when a known pixel block has been found */
        if (*got_frame) {
            int eop, trail;

            /* re-align to a word */
            bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);

            eop = bytestream2_get_be16(&gbc);
            trail = bytestream2_get_bytes_left(&gbc);
            if (eop != EOP)
                av_log(avctx, AV_LOG_WARNING,
                       "Missing end of picture opcode (found 0x%04X)\n", eop);
            if (trail)
                av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
            break;
        }
    }

    if (*got_frame) {
        p->pict_type = AV_PICTURE_TYPE_I;
        p->key_frame = 1;

        return avpkt->size;
    } else {
        av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");

        return AVERROR_INVALIDDATA;
    }
332 333
}

334
AVCodec ff_qdraw_decoder = {
335
    .name           = "qdraw",
336
    .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
337
    .type           = AVMEDIA_TYPE_VIDEO,
338
    .id             = AV_CODEC_ID_QDRAW,
339
    .decode         = decode_frame,
340
    .capabilities   = AV_CODEC_CAP_DR1,
341
};