qdrw.c 3.99 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 24 25
/**
 * @file qdrw.c
 * Apple QuickDraw codec.
 */
26

27 28 29 30 31 32 33
#include "avcodec.h"

typedef struct QdrawContext{
    AVCodecContext *avctx;
    AVFrame pic;
} QdrawContext;

34
static int decode_frame(AVCodecContext *avctx,
35
                        void *data, int *data_size,
Michael Niedermayer's avatar
Michael Niedermayer committed
36
                        const uint8_t *buf, int buf_size)
37 38 39 40 41 42
{
    QdrawContext * const a = avctx->priv_data;
    AVFrame * const p= (AVFrame*)&a->pic;
    uint8_t* outdata;
    int colors;
    int i;
43 44
    uint32_t *pal;
    int r, g, b;
45

46 47 48 49 50 51 52 53
    if(p->data[0])
        avctx->release_buffer(avctx, p);

    p->reference= 0;
    if(avctx->get_buffer(avctx, p) < 0){
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return -1;
    }
54
    p->pict_type= FF_I_TYPE;
55 56 57
    p->key_frame= 1;

    outdata = a->pic.data[0];
58

59
    buf += 0x68; /* jump to palette */
60
    colors = AV_RB32(buf);
61
    buf += 4;
62

63 64 65 66
    if(colors < 0 || colors > 256) {
        av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
        return -1;
    }
67

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

74 75 76 77 78
        if (idx > 255) {
            av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
            buf += 6;
            continue;
        }
79
        r = *buf++;
80
        buf++;
81
        g = *buf++;
82
        buf++;
83
        b = *buf++;
84
        buf++;
85
        pal[idx] = (r << 16) | (g << 8) | b;
86
    }
87
    p->palette_has_changed = 1;
88 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 97
        /* decode line */
        out = outdata;
98
        size = AV_RB16(buf); /* size of packed line */
99 100 101 102 103 104 105
        buf += 2;
        left = size;
        next = buf + size;
        while (left > 0) {
            code = *buf++;
            if (code & 0x80 ) { /* run */
                pix = *buf++;
106
                if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
107
                    break;
108 109
                memset(out, pix, 257 - code);
                out += 257 - code;
110 111 112
                tsize += 257 - code;
                left -= 2;
            } else { /* copy */
113
                if ((out + code) > (outdata +  a->pic.linesize[0]))
114
                    break;
115 116 117
                memcpy(out, buf, code + 1);
                out += code + 1;
                buf += code + 1;
118 119 120 121 122 123 124 125 126 127
                left -= 2 + code;
                tsize += code + 1;
            }
        }
        buf = next;
        outdata += a->pic.linesize[0];
    }

    *data_size = sizeof(AVFrame);
    *(AVFrame*)data = a->pic;
128

129 130 131
    return buf_size;
}

132
static av_cold int decode_init(AVCodecContext *avctx){
133 134
//    QdrawContext * const a = avctx->priv_data;

135
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
136 137 138
        return 1;
    }

139
    avctx->pix_fmt= PIX_FMT_PAL8;
140 141 142 143 144 145 146 147 148 149 150 151 152 153

    return 0;
}

AVCodec qdraw_decoder = {
    "qdraw",
    CODEC_TYPE_VIDEO,
    CODEC_ID_QDRAW,
    sizeof(QdrawContext),
    decode_init,
    NULL,
    NULL,
    decode_frame,
    CODEC_CAP_DR1,
154
    .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
155
};