2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * Apple QuickDraw codec.
27 #include "libavutil/intreadwrite.h"
30 typedef struct QdrawContext{
31 AVCodecContext *avctx;
35 static int decode_frame(AVCodecContext *avctx,
36 void *data, int *data_size,
39 const uint8_t *buf = avpkt->data;
40 const uint8_t *buf_end = avpkt->data + avpkt->size;
41 int buf_size = avpkt->size;
42 QdrawContext * const a = avctx->priv_data;
43 AVFrame * const p = &a->pic;
51 avctx->release_buffer(avctx, p);
54 if(avctx->get_buffer(avctx, p) < 0){
55 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
58 p->pict_type= AV_PICTURE_TYPE_I;
61 outdata = a->pic.data[0];
63 if (buf_end - buf < 0x68 + 4)
64 return AVERROR_INVALIDDATA;
65 buf += 0x68; /* jump to palette */
66 colors = AV_RB32(buf);
69 if(colors < 0 || colors > 256) {
70 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
73 if (buf_end - buf < (colors + 1) * 8)
74 return AVERROR_INVALIDDATA;
76 pal = (uint32_t*)p->data[1];
77 for (i = 0; i <= colors; i++) {
79 idx = AV_RB16(buf); /* color index */
83 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
93 pal[idx] = 0xFF << 24 | r << 16 | g << 8 | b;
95 p->palette_has_changed = 1;
97 if (buf_end - buf < 18)
98 return AVERROR_INVALIDDATA;
99 buf += 18; /* skip unneeded data */
100 for (i = 0; i < avctx->height; i++) {
101 int size, left, code, pix;
108 size = AV_RB16(buf); /* size of packed line */
110 if (buf_end - buf < size)
111 return AVERROR_INVALIDDATA;
117 if (code & 0x80 ) { /* run */
119 if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
121 memset(out, pix, 257 - code);
126 if ((out + code) > (outdata + a->pic.linesize[0]))
128 if (buf_end - buf < code + 1)
129 return AVERROR_INVALIDDATA;
130 memcpy(out, buf, code + 1);
138 outdata += a->pic.linesize[0];
141 *data_size = sizeof(AVFrame);
142 *(AVFrame*)data = a->pic;
147 static av_cold int decode_init(AVCodecContext *avctx){
148 QdrawContext * const a = avctx->priv_data;
150 avcodec_get_frame_defaults(&a->pic);
151 avctx->pix_fmt= PIX_FMT_PAL8;
156 static av_cold int decode_end(AVCodecContext *avctx){
157 QdrawContext * const a = avctx->priv_data;
158 AVFrame *pic = &a->pic;
161 avctx->release_buffer(avctx, pic);
166 AVCodec ff_qdraw_decoder = {
168 .type = AVMEDIA_TYPE_VIDEO,
169 .id = CODEC_ID_QDRAW,
170 .priv_data_size = sizeof(QdrawContext),
173 .decode = decode_frame,
174 .capabilities = CODEC_CAP_DR1,
175 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),