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"
32 static int decode_frame(AVCodecContext
*avctx
,
33 void *data
, int *got_frame
,
36 const uint8_t *buf
= avpkt
->data
;
37 const uint8_t *buf_end
= avpkt
->data
+ avpkt
->size
;
38 int buf_size
= avpkt
->size
;
39 AVFrame
* const p
= data
;
46 if ((ret
= ff_get_buffer(avctx
, p
, 0)) < 0)
48 p
->pict_type
= AV_PICTURE_TYPE_I
;
53 if (buf_end
- buf
< 0x68 + 4)
54 return AVERROR_INVALIDDATA
;
55 buf
+= 0x68; /* jump to palette */
56 colors
= AV_RB32(buf
);
59 if (colors
< 0 || colors
> 256) {
60 av_log(avctx
, AV_LOG_ERROR
, "Error color count - %i(0x%X)\n", colors
, colors
);
61 return AVERROR_INVALIDDATA
;
63 if (buf_end
- buf
< (colors
+ 1) * 8)
64 return AVERROR_INVALIDDATA
;
66 pal
= (uint32_t*)p
->data
[1];
67 for (i
= 0; i
<= colors
; i
++) {
69 idx
= AV_RB16(buf
); /* color index */
73 av_log(avctx
, AV_LOG_ERROR
, "Palette index out of range: %u\n", idx
);
83 pal
[idx
] = 0xFFU
<< 24 | r
<< 16 | g
<< 8 | b
;
85 p
->palette_has_changed
= 1;
87 if (buf_end
- buf
< 18)
88 return AVERROR_INVALIDDATA
;
89 buf
+= 18; /* skip unneeded data */
90 for (i
= 0; i
< avctx
->height
; i
++) {
91 int size
, left
, code
, pix
;
98 size
= AV_RB16(buf
); /* size of packed line */
100 if (buf_end
- buf
< size
)
101 return AVERROR_INVALIDDATA
;
107 if (code
& 0x80 ) { /* run */
109 if ((out
+ (257 - code
)) > (outdata
+ p
->linesize
[0]))
111 memset(out
, pix
, 257 - code
);
116 if ((out
+ code
) > (outdata
+ p
->linesize
[0]))
118 if (buf_end
- buf
< code
+ 1)
119 return AVERROR_INVALIDDATA
;
120 memcpy(out
, buf
, code
+ 1);
128 outdata
+= p
->linesize
[0];
136 static av_cold
int decode_init(AVCodecContext
*avctx
)
138 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
143 AVCodec ff_qdraw_decoder
= {
145 .long_name
= NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
146 .type
= AVMEDIA_TYPE_VIDEO
,
147 .id
= AV_CODEC_ID_QDRAW
,
149 .decode
= decode_frame
,
150 .capabilities
= CODEC_CAP_DR1
,