qdrw.c 4.04 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 libavcodec/qdrw.c
24 25
 * Apple QuickDraw codec.
 */
26

27
#include "libavutil/intreadwrite.h"
28 29 30 31 32 33 34
#include "avcodec.h"

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

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

47 48 49 50 51 52 53 54
    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;
    }
55
    p->pict_type= FF_I_TYPE;
56 57 58
    p->key_frame= 1;

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

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

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

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

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

    buf += 18; /* skip unneeded data */
    for (i = 0; i < avctx->height; i++) {
        int size, left, code, pix;
Michael Niedermayer's avatar
Michael Niedermayer committed
93
        const uint8_t *next;
94 95
        uint8_t *out;
        int tsize = 0;
96

97 98
        /* decode line */
        out = outdata;
99
        size = AV_RB16(buf); /* size of packed line */
100 101 102 103 104 105 106
        buf += 2;
        left = size;
        next = buf + size;
        while (left > 0) {
            code = *buf++;
            if (code & 0x80 ) { /* run */
                pix = *buf++;
107
                if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
108
                    break;
109 110
                memset(out, pix, 257 - code);
                out += 257 - code;
111 112 113
                tsize += 257 - code;
                left -= 2;
            } else { /* copy */
114
                if ((out + code) > (outdata +  a->pic.linesize[0]))
115
                    break;
116 117 118
                memcpy(out, buf, code + 1);
                out += code + 1;
                buf += code + 1;
119 120 121 122 123 124 125 126 127 128
                left -= 2 + code;
                tsize += code + 1;
            }
        }
        buf = next;
        outdata += a->pic.linesize[0];
    }

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

130 131 132
    return buf_size;
}

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

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

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

    return 0;
}

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