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,
40 SHORTCOMMENT = 0x00A0,
46 static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
47 uint32_t *pal, int colors)
51 for (i = 0; i <= colors; i++) {
53 unsigned int idx = bytestream2_get_be16(gbc); /* color index */
55 av_log(avctx, AV_LOG_WARNING,
56 "Palette index out of range: %u\n", idx);
57 bytestream2_skip(gbc, 6);
60 r = bytestream2_get_byte(gbc);
61 bytestream2_skip(gbc, 1);
62 g = bytestream2_get_byte(gbc);
63 bytestream2_skip(gbc, 1);
64 b = bytestream2_get_byte(gbc);
65 bytestream2_skip(gbc, 1);
66 pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
71 static int decode_rle16(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
73 int offset = avctx->width * 2;
74 uint8_t *outdata = p->data[0];
77 for (i = 0; i < avctx->height; i++) {
78 int size, left, code, pix;
79 uint16_t *out = (uint16_t *)outdata;
82 /* size of packed line */
83 size = left = bytestream2_get_be16(gbc);
84 if (bytestream2_get_bytes_left(gbc) < size)
85 return AVERROR_INVALIDDATA;
89 code = bytestream2_get_byte(gbc);
90 if (code & 0x80 ) { /* run */
91 pix = bytestream2_get_be16(gbc);
92 for (j = 0; j < 257 - code; j++) {
100 return AVERROR_INVALIDDATA;
104 for (j = 0; j < code + 1; j++) {
105 out[pos] = bytestream2_get_be16(gbc);
112 return AVERROR_INVALIDDATA;
114 left -= 1 + (code + 1) * 2;
117 outdata += p->linesize[0];
122 static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
126 int offset = avctx->width * step;
127 uint8_t *outdata = p->data[0];
129 for (i = 0; i < avctx->height; i++) {
130 int size, left, code, pix;
131 uint8_t *out = outdata;
134 /* size of packed line */
135 size = left = bytestream2_get_be16(gbc);
136 if (bytestream2_get_bytes_left(gbc) < size)
137 return AVERROR_INVALIDDATA;
141 code = bytestream2_get_byte(gbc);
142 if (code & 0x80 ) { /* run */
143 pix = bytestream2_get_byte(gbc);
144 for (j = 0; j < 257 - code; j++) {
152 return AVERROR_INVALIDDATA;
156 for (j = 0; j < code + 1; j++) {
157 out[pos] = bytestream2_get_byte(gbc);
164 return AVERROR_INVALIDDATA;
169 outdata += p->linesize[0];
174 static int check_header(const char *buf, int buf_size)
176 unsigned w, h, v0, v1;
183 v0 = AV_RB16(buf+10);
184 v1 = AV_RB16(buf+12);
191 if (v0 == 0x0011 && v1 == 0x02FF)
197 static int decode_frame(AVCodecContext *avctx,
198 void *data, int *got_frame,
201 AVFrame * const p = data;
207 bytestream2_init(&gbc, avpkt->data, avpkt->size);
208 if ( bytestream2_get_bytes_left(&gbc) >= 552
209 && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
211 bytestream2_skip(&gbc, 512);
213 ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
215 /* smallest PICT header */
216 if (bytestream2_get_bytes_left(&gbc) < 40) {
217 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
218 bytestream2_get_bytes_left(&gbc));
219 return AVERROR_INVALIDDATA;
222 bytestream2_skip(&gbc, 6);
223 h = bytestream2_get_be16(&gbc);
224 w = bytestream2_get_be16(&gbc);
226 ret = ff_set_dimensions(avctx, w, h);
230 /* version 1 is identified by 0x1101
231 * it uses byte-aligned opcodes rather than word-aligned */
233 avpriv_request_sample(avctx, "QuickDraw version 1");
234 return AVERROR_PATCHWELCOME;
235 } else if (ver != 2) {
236 avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
237 return AVERROR_PATCHWELCOME;
240 bytestream2_skip(&gbc, 4+26);
242 while (bytestream2_get_bytes_left(&gbc) >= 4) {
244 int rowbytes, pack_type;
245 int opcode = bytestream2_get_be16(&gbc);
250 av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
252 bytestream2_skip(&gbc, 30);
253 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
254 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
256 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
257 if (bppcnt == 1 && bpp == 8) {
258 avctx->pix_fmt = AV_PIX_FMT_PAL8;
259 } else if (bppcnt == 3 && bpp == 5) {
260 avctx->pix_fmt = AV_PIX_FMT_RGB555;
262 av_log(avctx, AV_LOG_ERROR,
263 "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
265 return AVERROR_INVALIDDATA;
268 /* jump to palette */
269 bytestream2_skip(&gbc, 18);
270 colors = bytestream2_get_be16(&gbc);
272 if (colors < 0 || colors > 256) {
273 av_log(avctx, AV_LOG_ERROR,
274 "Error color count - %i(0x%X)\n", colors, colors);
275 return AVERROR_INVALIDDATA;
277 if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
278 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
279 bytestream2_get_bytes_left(&gbc));
280 return AVERROR_INVALIDDATA;
282 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
285 parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
286 p->palette_has_changed = 1;
288 /* jump to image data */
289 bytestream2_skip(&gbc, 18);
291 if (opcode == PACKBITSRGN) {
292 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
293 avpriv_report_missing_feature(avctx, "Packbit mask region");
296 if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
297 ret = decode_rle16(avctx, p, &gbc);
299 ret = decode_rle(avctx, p, &gbc, bppcnt);
306 av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
308 bytestream2_skip(&gbc, 4);
309 rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
310 if (rowbytes <= 250) {
311 avpriv_report_missing_feature(avctx, "Short rowbytes");
312 return AVERROR_PATCHWELCOME;
315 bytestream2_skip(&gbc, 10);
316 pack_type = bytestream2_get_be16(&gbc);
318 bytestream2_skip(&gbc, 16);
319 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
320 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
322 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
323 if (bppcnt == 3 && bpp == 8) {
324 avctx->pix_fmt = AV_PIX_FMT_RGB24;
325 } else if (bppcnt == 3 && bpp == 5) {
326 avctx->pix_fmt = AV_PIX_FMT_RGB555;
327 } else if (bppcnt == 4 && bpp == 8) {
328 avctx->pix_fmt = AV_PIX_FMT_ARGB;
330 av_log(avctx, AV_LOG_ERROR,
331 "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
333 return AVERROR_INVALIDDATA;
336 /* set packing when default is selected */
340 if (pack_type != 3 && pack_type != 4) {
341 avpriv_request_sample(avctx, "Pack type %d", pack_type);
342 return AVERROR_PATCHWELCOME;
344 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
348 bytestream2_skip(&gbc, 30);
350 if (opcode == DIRECTBITSRGN) {
351 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
352 avpriv_report_missing_feature(avctx, "DirectBit mask region");
355 if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
356 ret = decode_rle16(avctx, p, &gbc);
358 ret = decode_rle(avctx, p, &gbc, bppcnt);
364 bytestream2_get_be16(&gbc);
365 bytestream2_skip(&gbc, bytestream2_get_be16(&gbc));
368 av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
371 /* exit the loop when a known pixel block has been found */
375 /* re-align to a word */
376 bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
378 eop = bytestream2_get_be16(&gbc);
379 trail = bytestream2_get_bytes_left(&gbc);
381 av_log(avctx, AV_LOG_WARNING,
382 "Missing end of picture opcode (found 0x%04X)\n", eop);
384 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
390 p->pict_type = AV_PICTURE_TYPE_I;
395 av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
397 return AVERROR_INVALIDDATA;
401 AVCodec ff_qdraw_decoder = {
403 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
404 .type = AVMEDIA_TYPE_VIDEO,
405 .id = AV_CODEC_ID_QDRAW,
406 .decode = decode_frame,
407 .capabilities = AV_CODEC_CAP_DR1,