2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
4 * Copyright (c) 2015 Vittorio Giovara
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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++) {
99 return AVERROR_INVALIDDATA;
103 for (j = 0; j < code + 1; j++) {
104 out[pos] = bytestream2_get_byte(gbc);
111 return AVERROR_INVALIDDATA;
116 outdata += p->linesize[0];
121 static int check_header(const char *buf, int buf_size)
123 unsigned w, h, v0, v1;
130 v0 = AV_RB16(buf+10);
131 v1 = AV_RB16(buf+12);
138 if (v0 == 0x0011 && v1 == 0x02FF)
144 static int decode_frame(AVCodecContext *avctx,
145 void *data, int *got_frame,
148 AVFrame * const p = data;
154 bytestream2_init(&gbc, avpkt->data, avpkt->size);
155 if ( bytestream2_get_bytes_left(&gbc) >= 552
156 && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
158 bytestream2_skip(&gbc, 512);
160 ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
162 /* smallest PICT header */
163 if (bytestream2_get_bytes_left(&gbc) < 40) {
164 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
165 bytestream2_get_bytes_left(&gbc));
166 return AVERROR_INVALIDDATA;
169 bytestream2_skip(&gbc, 6);
170 h = bytestream2_get_be16(&gbc);
171 w = bytestream2_get_be16(&gbc);
173 ret = ff_set_dimensions(avctx, w, h);
177 /* version 1 is identified by 0x1101
178 * it uses byte-aligned opcodes rather than word-aligned */
180 avpriv_request_sample(avctx, "QuickDraw version 1");
181 return AVERROR_PATCHWELCOME;
182 } else if (ver != 2) {
183 avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
184 return AVERROR_PATCHWELCOME;
187 bytestream2_skip(&gbc, 4+26);
189 while (bytestream2_get_bytes_left(&gbc) >= 4) {
191 int rowbytes, pack_type;
192 int opcode = bytestream2_get_be16(&gbc);
197 av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
199 bytestream2_skip(&gbc, 30);
200 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
201 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
203 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
204 if (bppcnt == 1 && bpp == 8) {
205 avctx->pix_fmt = AV_PIX_FMT_PAL8;
207 av_log(avctx, AV_LOG_ERROR,
208 "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
210 return AVERROR_INVALIDDATA;
213 /* jump to palette */
214 bytestream2_skip(&gbc, 18);
215 colors = bytestream2_get_be16(&gbc);
217 if (colors < 0 || colors > 256) {
218 av_log(avctx, AV_LOG_ERROR,
219 "Error color count - %i(0x%X)\n", colors, colors);
220 return AVERROR_INVALIDDATA;
222 if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
223 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
224 bytestream2_get_bytes_left(&gbc));
225 return AVERROR_INVALIDDATA;
227 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
230 parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
231 p->palette_has_changed = 1;
233 /* jump to image data */
234 bytestream2_skip(&gbc, 18);
236 if (opcode == PACKBITSRGN) {
237 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
238 avpriv_report_missing_feature(avctx, "Packbit mask region");
241 ret = decode_rle(avctx, p, &gbc, bppcnt);
248 av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
250 bytestream2_skip(&gbc, 4);
251 rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
252 if (rowbytes <= 250) {
253 avpriv_report_missing_feature(avctx, "Short rowbytes");
254 return AVERROR_PATCHWELCOME;
257 bytestream2_skip(&gbc, 10);
258 pack_type = bytestream2_get_be16(&gbc);
260 bytestream2_skip(&gbc, 16);
261 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
262 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
264 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
265 if (bppcnt == 3 && bpp == 8) {
266 avctx->pix_fmt = AV_PIX_FMT_RGB24;
267 } else if (bppcnt == 4 && bpp == 8) {
268 avctx->pix_fmt = AV_PIX_FMT_ARGB;
270 av_log(avctx, AV_LOG_ERROR,
271 "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
273 return AVERROR_INVALIDDATA;
276 /* set packing when default is selected */
280 if (pack_type != 3 && pack_type != 4) {
281 avpriv_request_sample(avctx, "Pack type %d", pack_type);
282 return AVERROR_PATCHWELCOME;
284 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
285 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
290 bytestream2_skip(&gbc, 30);
292 if (opcode == DIRECTBITSRGN) {
293 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
294 avpriv_report_missing_feature(avctx, "DirectBit mask region");
297 ret = decode_rle(avctx, p, &gbc, bppcnt);
303 av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
306 /* exit the loop when a known pixel block has been found */
310 /* re-align to a word */
311 bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
313 eop = bytestream2_get_be16(&gbc);
314 trail = bytestream2_get_bytes_left(&gbc);
316 av_log(avctx, AV_LOG_WARNING,
317 "Missing end of picture opcode (found 0x%04X)\n", eop);
319 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
325 p->pict_type = AV_PICTURE_TYPE_I;
330 av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
332 return AVERROR_INVALIDDATA;
336 AVCodec ff_qdraw_decoder = {
338 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
339 .type = AVMEDIA_TYPE_VIDEO,
340 .id = AV_CODEC_ID_QDRAW,
341 .decode = decode_frame,
342 .capabilities = AV_CODEC_CAP_DR1,