Statistics
| Branch: | Revision:

ffmpeg / libavcodec / qdrw.c @ ce5e49b0

History | View | Annotate | Download (4.2 KB)

1 d08d7142 Mike Melanson
/*
2
 * QuickDraw (qdrw) codec
3
 * Copyright (c) 2004 Konstantin Shishkov
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 d08d7142 Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 d08d7142 Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 d08d7142 Mike Melanson
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 d08d7142 Mike Melanson
 */
21 115329f1 Diego Biurrun
22 d08d7142 Mike Melanson
/**
23 ba87f080 Diego Biurrun
 * @file
24 d08d7142 Mike Melanson
 * Apple QuickDraw codec.
25
 */
26 115329f1 Diego Biurrun
27 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
28 d08d7142 Mike Melanson
#include "avcodec.h"
29
30
typedef struct QdrawContext{
31
    AVCodecContext *avctx;
32
    AVFrame pic;
33
} QdrawContext;
34
35 115329f1 Diego Biurrun
static int decode_frame(AVCodecContext *avctx,
36 d08d7142 Mike Melanson
                        void *data, int *data_size,
37 7a00bbad Thilo Borgmann
                        AVPacket *avpkt)
38 d08d7142 Mike Melanson
{
39 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
40
    int buf_size = avpkt->size;
41 d08d7142 Mike Melanson
    QdrawContext * const a = avctx->priv_data;
42
    AVFrame * const p= (AVFrame*)&a->pic;
43
    uint8_t* outdata;
44
    int colors;
45
    int i;
46 bcae1fd0 Kostya Shishkov
    uint32_t *pal;
47
    int r, g, b;
48 115329f1 Diego Biurrun
49 d08d7142 Mike Melanson
    if(p->data[0])
50
        avctx->release_buffer(avctx, p);
51
52
    p->reference= 0;
53
    if(avctx->get_buffer(avctx, p) < 0){
54
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
55
        return -1;
56
    }
57 ce5e49b0 Stefano Sabatini
    p->pict_type= AV_PICTURE_TYPE_I;
58 d08d7142 Mike Melanson
    p->key_frame= 1;
59
60
    outdata = a->pic.data[0];
61 115329f1 Diego Biurrun
62 d08d7142 Mike Melanson
    buf += 0x68; /* jump to palette */
63 fead30d4 Alex Beregszaszi
    colors = AV_RB32(buf);
64 d08d7142 Mike Melanson
    buf += 4;
65 115329f1 Diego Biurrun
66 d08d7142 Mike Melanson
    if(colors < 0 || colors > 256) {
67
        av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
68
        return -1;
69
    }
70 115329f1 Diego Biurrun
71 bcae1fd0 Kostya Shishkov
    pal = (uint32_t*)p->data[1];
72 d08d7142 Mike Melanson
    for (i = 0; i <= colors; i++) {
73 cca1a426 Roberto Togni
        unsigned int idx;
74 fead30d4 Alex Beregszaszi
        idx = AV_RB16(buf); /* color index */
75 d08d7142 Mike Melanson
        buf += 2;
76 115329f1 Diego Biurrun
77 cca1a426 Roberto Togni
        if (idx > 255) {
78
            av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
79
            buf += 6;
80
            continue;
81
        }
82 bcae1fd0 Kostya Shishkov
        r = *buf++;
83 d08d7142 Mike Melanson
        buf++;
84 bcae1fd0 Kostya Shishkov
        g = *buf++;
85 d08d7142 Mike Melanson
        buf++;
86 bcae1fd0 Kostya Shishkov
        b = *buf++;
87 d08d7142 Mike Melanson
        buf++;
88 bcae1fd0 Kostya Shishkov
        pal[idx] = (r << 16) | (g << 8) | b;
89 d08d7142 Mike Melanson
    }
90 bcae1fd0 Kostya Shishkov
    p->palette_has_changed = 1;
91 d08d7142 Mike Melanson
92
    buf += 18; /* skip unneeded data */
93
    for (i = 0; i < avctx->height; i++) {
94
        int size, left, code, pix;
95 13c2619e Michael Niedermayer
        const uint8_t *next;
96 d08d7142 Mike Melanson
        uint8_t *out;
97
        int tsize = 0;
98 115329f1 Diego Biurrun
99 d08d7142 Mike Melanson
        /* decode line */
100
        out = outdata;
101 fead30d4 Alex Beregszaszi
        size = AV_RB16(buf); /* size of packed line */
102 d08d7142 Mike Melanson
        buf += 2;
103
        left = size;
104
        next = buf + size;
105
        while (left > 0) {
106
            code = *buf++;
107
            if (code & 0x80 ) { /* run */
108
                pix = *buf++;
109 bcae1fd0 Kostya Shishkov
                if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
110 cca1a426 Roberto Togni
                    break;
111 bcae1fd0 Kostya Shishkov
                memset(out, pix, 257 - code);
112
                out += 257 - code;
113 d08d7142 Mike Melanson
                tsize += 257 - code;
114
                left -= 2;
115
            } else { /* copy */
116 bcae1fd0 Kostya Shishkov
                if ((out + code) > (outdata +  a->pic.linesize[0]))
117 cca1a426 Roberto Togni
                    break;
118 bcae1fd0 Kostya Shishkov
                memcpy(out, buf, code + 1);
119
                out += code + 1;
120
                buf += code + 1;
121 d08d7142 Mike Melanson
                left -= 2 + code;
122
                tsize += code + 1;
123
            }
124
        }
125
        buf = next;
126
        outdata += a->pic.linesize[0];
127
    }
128
129
    *data_size = sizeof(AVFrame);
130
    *(AVFrame*)data = a->pic;
131 115329f1 Diego Biurrun
132 d08d7142 Mike Melanson
    return buf_size;
133
}
134
135 98a6fff9 Zuxy Meng
static av_cold int decode_init(AVCodecContext *avctx){
136 d08d7142 Mike Melanson
//    QdrawContext * const a = avctx->priv_data;
137
138 bcae1fd0 Kostya Shishkov
    avctx->pix_fmt= PIX_FMT_PAL8;
139 d08d7142 Mike Melanson
140
    return 0;
141
}
142
143 6d924b5a Kostya Shishkov
static av_cold int decode_end(AVCodecContext *avctx){
144
    QdrawContext * const a = avctx->priv_data;
145
    AVFrame *pic = &a->pic;
146
147
    if (pic->data[0])
148
        avctx->release_buffer(avctx, pic);
149
150
    return 0;
151
}
152
153 e7e2df27 Diego Elio Pettenò
AVCodec ff_qdraw_decoder = {
154 d08d7142 Mike Melanson
    "qdraw",
155 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_VIDEO,
156 d08d7142 Mike Melanson
    CODEC_ID_QDRAW,
157
    sizeof(QdrawContext),
158
    decode_init,
159
    NULL,
160 6d924b5a Kostya Shishkov
    decode_end,
161 d08d7142 Mike Melanson
    decode_frame,
162
    CODEC_CAP_DR1,
163 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
164 d08d7142 Mike Melanson
};