2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
4 * Copyright (c) 2015 Vittorio Giovara
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Apple QuickDraw codec.
26 * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
32 #include "bytestream.h"
35 enum QuickdrawOpcodes
{
36 PACKBITSRECT
= 0x0098,
44 static int parse_palette(AVCodecContext
*avctx
, GetByteContext
*gbc
,
45 uint32_t *pal
, int colors
)
49 for (i
= 0; i
<= colors
; i
++) {
51 unsigned int idx
= bytestream2_get_be16(gbc
); /* color index */
53 av_log(avctx
, AV_LOG_WARNING
,
54 "Palette index out of range: %u\n", idx
);
55 bytestream2_skip(gbc
, 6);
58 r
= bytestream2_get_byte(gbc
);
59 bytestream2_skip(gbc
, 1);
60 g
= bytestream2_get_byte(gbc
);
61 bytestream2_skip(gbc
, 1);
62 b
= bytestream2_get_byte(gbc
);
63 bytestream2_skip(gbc
, 1);
64 pal
[idx
] = (0xFFU
<< 24) | (r
<< 16) | (g
<< 8) | b
;
69 static int decode_rle(AVCodecContext
*avctx
, AVFrame
*p
, GetByteContext
*gbc
,
73 int offset
= avctx
->width
* step
;
74 uint8_t *outdata
= p
->data
[0];
76 for (i
= 0; i
< avctx
->height
; i
++) {
77 int size
, left
, code
, pix
;
78 uint8_t *out
= outdata
;
81 /* size of packed line */
82 size
= left
= bytestream2_get_be16(gbc
);
83 if (bytestream2_get_bytes_left(gbc
) < size
)
84 return AVERROR_INVALIDDATA
;
88 code
= bytestream2_get_byte(gbc
);
89 if (code
& 0x80 ) { /* run */
90 pix
= bytestream2_get_byte(gbc
);
91 for (j
= 0; j
< 257 - code
; j
++) {
101 for (j
= 0; j
< code
+ 1; j
++) {
102 out
[pos
] = bytestream2_get_byte(gbc
);
112 outdata
+= p
->linesize
[0];
117 static int decode_frame(AVCodecContext
*avctx
,
118 void *data
, int *got_frame
,
121 AVFrame
* const p
= data
;
126 bytestream2_init(&gbc
, avpkt
->data
, avpkt
->size
);
128 /* PICT images start with a 512 bytes empty header */
129 if (bytestream2_peek_be32(&gbc
) == 0)
130 bytestream2_skip(&gbc
, 512);
132 /* smallest PICT header */
133 if (bytestream2_get_bytes_left(&gbc
) < 40) {
134 av_log(avctx
, AV_LOG_ERROR
, "Frame is too small %d\n",
135 bytestream2_get_bytes_left(&gbc
));
136 return AVERROR_INVALIDDATA
;
139 bytestream2_skip(&gbc
, 6);
140 h
= bytestream2_get_be16(&gbc
);
141 w
= bytestream2_get_be16(&gbc
);
143 ret
= ff_set_dimensions(avctx
, w
, h
);
147 /* version 1 is identified by 0x1101
148 * it uses byte-aligned opcodes rather than word-aligned */
149 if (bytestream2_get_be32(&gbc
) != 0x001102FF) {
150 avpriv_request_sample(avctx
, "QuickDraw version 1");
151 return AVERROR_PATCHWELCOME
;
154 bytestream2_skip(&gbc
, 26);
156 while (bytestream2_get_bytes_left(&gbc
) >= 4) {
158 int rowbytes
, pack_type
;
159 int opcode
= bytestream2_get_be16(&gbc
);
164 av_log(avctx
, AV_LOG_DEBUG
, "Parsing Packbit opcode\n");
166 bytestream2_skip(&gbc
, 30);
167 bppcnt
= bytestream2_get_be16(&gbc
); /* cmpCount */
168 bpp
= bytestream2_get_be16(&gbc
); /* cmpSize */
170 av_log(avctx
, AV_LOG_DEBUG
, "bppcount %d bpp %d\n", bppcnt
, bpp
);
171 if (bppcnt
== 1 && bpp
== 8) {
172 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
174 av_log(avctx
, AV_LOG_ERROR
,
175 "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
177 return AVERROR_INVALIDDATA
;
180 /* jump to palette */
181 bytestream2_skip(&gbc
, 18);
182 colors
= bytestream2_get_be16(&gbc
);
184 if (colors
< 0 || colors
> 256) {
185 av_log(avctx
, AV_LOG_ERROR
,
186 "Error color count - %i(0x%X)\n", colors
, colors
);
187 return AVERROR_INVALIDDATA
;
189 if (bytestream2_get_bytes_left(&gbc
) < (colors
+ 1) * 8) {
190 av_log(avctx
, AV_LOG_ERROR
, "Palette is too small %d\n",
191 bytestream2_get_bytes_left(&gbc
));
192 return AVERROR_INVALIDDATA
;
194 if ((ret
= ff_get_buffer(avctx
, p
, 0)) < 0) {
195 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
199 parse_palette(avctx
, &gbc
, (uint32_t *)p
->data
[1], colors
);
200 p
->palette_has_changed
= 1;
202 /* jump to image data */
203 bytestream2_skip(&gbc
, 18);
205 if (opcode
== PACKBITSRGN
) {
206 bytestream2_skip(&gbc
, 2 + 8); /* size + rect */
207 avpriv_report_missing_feature(avctx
, "Packbit mask region");
210 ret
= decode_rle(avctx
, p
, &gbc
, bppcnt
);
217 av_log(avctx
, AV_LOG_DEBUG
, "Parsing Directbit opcode\n");
219 bytestream2_skip(&gbc
, 4);
220 rowbytes
= bytestream2_get_be16(&gbc
) & 0x3FFF;
221 if (rowbytes
<= 250) {
222 avpriv_report_missing_feature(avctx
, "Short rowbytes");
223 return AVERROR_PATCHWELCOME
;
226 bytestream2_skip(&gbc
, 10);
227 pack_type
= bytestream2_get_be16(&gbc
);
229 bytestream2_skip(&gbc
, 16);
230 bppcnt
= bytestream2_get_be16(&gbc
); /* cmpCount */
231 bpp
= bytestream2_get_be16(&gbc
); /* cmpSize */
233 av_log(avctx
, AV_LOG_DEBUG
, "bppcount %d bpp %d\n", bppcnt
, bpp
);
234 if (bppcnt
== 3 && bpp
== 8) {
235 avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
236 } else if (bppcnt
== 4 && bpp
== 8) {
237 avctx
->pix_fmt
= AV_PIX_FMT_ARGB
;
239 av_log(avctx
, AV_LOG_ERROR
,
240 "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
242 return AVERROR_INVALIDDATA
;
245 /* set packing when default is selected */
249 if (pack_type
!= 3 && pack_type
!= 4) {
250 avpriv_request_sample(avctx
, "Pack type %d", pack_type
);
251 return AVERROR_PATCHWELCOME
;
253 if ((ret
= ff_get_buffer(avctx
, p
, 0)) < 0) {
254 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
259 bytestream2_skip(&gbc
, 30);
261 if (opcode
== DIRECTBITSRGN
) {
262 bytestream2_skip(&gbc
, 2 + 8); /* size + rect */
263 avpriv_report_missing_feature(avctx
, "DirectBit mask region");
266 ret
= decode_rle(avctx
, p
, &gbc
, bppcnt
);
272 av_log(avctx
, AV_LOG_TRACE
, "Unknown 0x%04X opcode\n", opcode
);
275 /* exit the loop when a known pixel block has been found */
279 /* re-align to a word */
280 bytestream2_skip(&gbc
, bytestream2_get_bytes_left(&gbc
) % 2);
282 eop
= bytestream2_get_be16(&gbc
);
283 trail
= bytestream2_get_bytes_left(&gbc
);
285 av_log(avctx
, AV_LOG_WARNING
,
286 "Missing end of picture opcode (found 0x%04X)\n", eop
);
288 av_log(avctx
, AV_LOG_WARNING
, "Got %d trailing bytes\n", trail
);
294 p
->pict_type
= AV_PICTURE_TYPE_I
;
299 av_log(avctx
, AV_LOG_ERROR
, "Frame contained no usable data\n");
301 return AVERROR_INVALIDDATA
;
305 AVCodec ff_qdraw_decoder
= {
307 .long_name
= NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
308 .type
= AVMEDIA_TYPE_VIDEO
,
309 .id
= AV_CODEC_ID_QDRAW
,
310 .decode
= decode_frame
,
311 .capabilities
= AV_CODEC_CAP_DR1
,