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/common.h"
28 #include "libavutil/intreadwrite.h"
31 typedef struct QdrawContext{
32 AVCodecContext *avctx;
36 static int decode_frame(AVCodecContext *avctx,
37 void *data, int *data_size,
40 const uint8_t *buf = avpkt->data;
41 const uint8_t *buf_end = avpkt->data + avpkt->size;
42 int buf_size = avpkt->size;
43 QdrawContext * const a = avctx->priv_data;
44 AVFrame * const p = &a->pic;
52 avctx->release_buffer(avctx, p);
55 if(avctx->get_buffer(avctx, p) < 0){
56 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
59 p->pict_type= AV_PICTURE_TYPE_I;
62 outdata = a->pic.data[0];
64 if (buf_end - buf < 0x68 + 4)
65 return AVERROR_INVALIDDATA;
66 buf += 0x68; /* jump to palette */
67 colors = AV_RB32(buf);
70 if(colors < 0 || colors > 256) {
71 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
74 if (buf_end - buf < (colors + 1) * 8)
75 return AVERROR_INVALIDDATA;
77 pal = (uint32_t*)p->data[1];
78 for (i = 0; i <= colors; i++) {
80 idx = AV_RB16(buf); /* color index */
84 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
94 pal[idx] = 0xFF << 24 | r << 16 | g << 8 | b;
96 p->palette_has_changed = 1;
98 if (buf_end - buf < 18)
99 return AVERROR_INVALIDDATA;
100 buf += 18; /* skip unneeded data */
101 for (i = 0; i < avctx->height; i++) {
102 int size, left, code, pix;
109 size = AV_RB16(buf); /* size of packed line */
111 if (buf_end - buf < size)
112 return AVERROR_INVALIDDATA;
118 if (code & 0x80 ) { /* run */
120 if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
122 memset(out, pix, 257 - code);
127 if ((out + code) > (outdata + a->pic.linesize[0]))
129 if (buf_end - buf < code + 1)
130 return AVERROR_INVALIDDATA;
131 memcpy(out, buf, code + 1);
139 outdata += a->pic.linesize[0];
142 *data_size = sizeof(AVFrame);
143 *(AVFrame*)data = a->pic;
148 static av_cold int decode_init(AVCodecContext *avctx){
149 QdrawContext * const a = avctx->priv_data;
151 avcodec_get_frame_defaults(&a->pic);
152 avctx->pix_fmt= PIX_FMT_PAL8;
157 static av_cold int decode_end(AVCodecContext *avctx){
158 QdrawContext * const a = avctx->priv_data;
159 AVFrame *pic = &a->pic;
162 avctx->release_buffer(avctx, pic);
167 AVCodec ff_qdraw_decoder = {
169 .type = AVMEDIA_TYPE_VIDEO,
170 .id = AV_CODEC_ID_QDRAW,
171 .priv_data_size = sizeof(QdrawContext),
174 .decode = decode_frame,
175 .capabilities = CODEC_CAP_DR1,
176 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),