1/*
2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * 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 * 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 * 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 * Apple QuickDraw codec.
25 */
26
27#include "libavutil/intreadwrite.h"
28#include "avcodec.h"
29
30typedef struct QdrawContext{
31    AVCodecContext *avctx;
32    AVFrame pic;
33} QdrawContext;
34
35static int decode_frame(AVCodecContext *avctx,
36                        void *data, int *data_size,
37                        const uint8_t *buf, int buf_size)
38{
39    QdrawContext * const a = avctx->priv_data;
40    AVFrame * const p= (AVFrame*)&a->pic;
41    uint8_t* outdata;
42    int colors;
43    int i;
44    uint32_t *pal;
45    int r, g, b;
46
47    if(p->data[0])
48        avctx->release_buffer(avctx, p);
49
50    p->reference= 0;
51    if(avctx->get_buffer(avctx, p) < 0){
52        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
53        return -1;
54    }
55    p->pict_type= FF_I_TYPE;
56    p->key_frame= 1;
57
58    outdata = a->pic.data[0];
59
60    buf += 0x68; /* jump to palette */
61    colors = AV_RB32(buf);
62    buf += 4;
63
64    if(colors < 0 || colors > 256) {
65        av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
66        return -1;
67    }
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        if (idx > 255) {
76            av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
77            buf += 6;
78            continue;
79        }
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    buf += 18; /* skip unneeded data */
91    for (i = 0; i < avctx->height; i++) {
92        int size, left, code, pix;
93        const uint8_t *next;
94        uint8_t *out;
95        int tsize = 0;
96
97        /* decode line */
98        out = outdata;
99        size = AV_RB16(buf); /* size of packed line */
100        buf += 2;
101        left = size;
102        next = buf + size;
103        while (left > 0) {
104            code = *buf++;
105            if (code & 0x80 ) { /* run */
106                pix = *buf++;
107                if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
108                    break;
109                memset(out, pix, 257 - code);
110                out += 257 - code;
111                tsize += 257 - code;
112                left -= 2;
113            } else { /* copy */
114                if ((out + code) > (outdata +  a->pic.linesize[0]))
115                    break;
116                memcpy(out, buf, code + 1);
117                out += code + 1;
118                buf += code + 1;
119                left -= 2 + code;
120                tsize += code + 1;
121            }
122        }
123        buf = next;
124        outdata += a->pic.linesize[0];
125    }
126
127    *data_size = sizeof(AVFrame);
128    *(AVFrame*)data = a->pic;
129
130    return buf_size;
131}
132
133static av_cold int decode_init(AVCodecContext *avctx){
134//    QdrawContext * const a = avctx->priv_data;
135
136    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
137        return 1;
138    }
139
140    avctx->pix_fmt= PIX_FMT_PAL8;
141
142    return 0;
143}
144
145AVCodec qdraw_decoder = {
146    "qdraw",
147    CODEC_TYPE_VIDEO,
148    CODEC_ID_QDRAW,
149    sizeof(QdrawContext),
150    decode_init,
151    NULL,
152    NULL,
153    decode_frame,
154    CODEC_CAP_DR1,
155    .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
156};
157