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 {
37 PACKBITSRECT = 0x0098,
41 SHORTCOMMENT = 0x00A0,
47 static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
48 uint32_t *pal, int colors)
52 for (i = 0; i <= colors; i++) {
54 unsigned int idx = bytestream2_get_be16(gbc); /* color index */
56 av_log(avctx, AV_LOG_WARNING,
57 "Palette index out of range: %u\n", idx);
58 bytestream2_skip(gbc, 6);
61 r = bytestream2_get_byte(gbc);
62 bytestream2_skip(gbc, 1);
63 g = bytestream2_get_byte(gbc);
64 bytestream2_skip(gbc, 1);
65 b = bytestream2_get_byte(gbc);
66 bytestream2_skip(gbc, 1);
67 pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
72 static int decode_rle_bpp2(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
74 int offset = avctx->width;
75 uint8_t *outdata = p->data[0];
78 for (i = 0; i < avctx->height; i++) {
79 int size, left, code, pix;
80 uint8_t *out = outdata;
83 /* size of packed line */
85 size = left = bytestream2_get_be16(gbc);
87 size = left = bytestream2_get_byte(gbc);
88 if (bytestream2_get_bytes_left(gbc) < size)
89 return AVERROR_INVALIDDATA;
93 code = bytestream2_get_byte(gbc);
94 if (code & 0x80 ) { /* run */
95 pix = bytestream2_get_byte(gbc);
96 for (j = 0; j < 257 - code; j++) {
98 out[pos++] = (pix & 0xC0) >> 6;
100 out[pos++] = (pix & 0x30) >> 4;
102 out[pos++] = (pix & 0x0C) >> 2;
104 out[pos++] = (pix & 0x03);
108 for (j = 0; j < code + 1; j++) {
109 pix = bytestream2_get_byte(gbc);
111 out[pos++] = (pix & 0xC0) >> 6;
113 out[pos++] = (pix & 0x30) >> 4;
115 out[pos++] = (pix & 0x0C) >> 2;
117 out[pos++] = (pix & 0x03);
119 left -= 1 + (code + 1);
122 outdata += p->linesize[0];
127 static int decode_rle_bpp4(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
129 int offset = avctx->width;
130 uint8_t *outdata = p->data[0];
133 for (i = 0; i < avctx->height; i++) {
134 int size, left, code, pix;
135 uint8_t *out = outdata;
138 /* size of packed line */
139 size = left = bytestream2_get_be16(gbc);
140 if (bytestream2_get_bytes_left(gbc) < size)
141 return AVERROR_INVALIDDATA;
145 code = bytestream2_get_byte(gbc);
146 if (code & 0x80 ) { /* run */
147 pix = bytestream2_get_byte(gbc);
148 for (j = 0; j < 257 - code; j++) {
150 out[pos++] = (pix & 0xF0) >> 4;
152 out[pos++] = pix & 0xF;
156 for (j = 0; j < code + 1; j++) {
157 pix = bytestream2_get_byte(gbc);
159 out[pos++] = (pix & 0xF0) >> 4;
161 out[pos++] = pix & 0xF;
163 left -= 1 + (code + 1);
166 outdata += p->linesize[0];
171 static int decode_rle16(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
173 int offset = avctx->width;
174 uint8_t *outdata = p->data[0];
177 for (i = 0; i < avctx->height; i++) {
178 int size, left, code, pix;
179 uint16_t *out = (uint16_t *)outdata;
182 /* size of packed line */
183 size = left = bytestream2_get_be16(gbc);
184 if (bytestream2_get_bytes_left(gbc) < size)
185 return AVERROR_INVALIDDATA;
189 code = bytestream2_get_byte(gbc);
190 if (code & 0x80 ) { /* run */
191 pix = bytestream2_get_be16(gbc);
192 for (j = 0; j < 257 - code; j++) {
199 for (j = 0; j < code + 1; j++) {
201 out[pos++] = bytestream2_get_be16(gbc);
203 bytestream2_skip(gbc, 2);
206 left -= 1 + (code + 1) * 2;
209 outdata += p->linesize[0];
214 static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
218 int offset = avctx->width * step;
219 uint8_t *outdata = p->data[0];
221 for (i = 0; i < avctx->height; i++) {
222 int size, left, code, pix;
223 uint8_t *out = outdata;
226 /* size of packed line */
227 size = left = bytestream2_get_be16(gbc);
228 if (bytestream2_get_bytes_left(gbc) < size)
229 return AVERROR_INVALIDDATA;
233 code = bytestream2_get_byte(gbc);
234 if (code & 0x80 ) { /* run */
235 pix = bytestream2_get_byte(gbc);
236 for (j = 0; j < 257 - code; j++) {
240 if (pos >= offset && step > 1) {
247 for (j = 0; j < code + 1; j++) {
248 pix = bytestream2_get_byte(gbc);
252 if (pos >= offset && step > 1) {
260 outdata += p->linesize[0];
265 static int check_header(const char *buf, int buf_size)
267 unsigned w, h, v0, v1;
274 v0 = AV_RB16(buf+10);
275 v1 = AV_RB16(buf+12);
282 if (v0 == 0x0011 && v1 == 0x02FF)
288 static int decode_frame(AVCodecContext *avctx,
289 void *data, int *got_frame,
292 AVFrame * const p = data;
298 bytestream2_init(&gbc, avpkt->data, avpkt->size);
299 if ( bytestream2_get_bytes_left(&gbc) >= 552
300 && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
302 bytestream2_skip(&gbc, 512);
304 ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
306 /* smallest PICT header */
307 if (bytestream2_get_bytes_left(&gbc) < 40) {
308 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
309 bytestream2_get_bytes_left(&gbc));
310 return AVERROR_INVALIDDATA;
313 bytestream2_skip(&gbc, 6);
314 h = bytestream2_get_be16(&gbc);
315 w = bytestream2_get_be16(&gbc);
317 ret = ff_set_dimensions(avctx, w, h);
321 /* version 1 is identified by 0x1101
322 * it uses byte-aligned opcodes rather than word-aligned */
324 avpriv_request_sample(avctx, "QuickDraw version 1");
325 return AVERROR_PATCHWELCOME;
326 } else if (ver != 2) {
327 avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
328 return AVERROR_PATCHWELCOME;
331 bytestream2_skip(&gbc, 4+26);
333 while (bytestream2_get_bytes_left(&gbc) >= 4) {
335 int rowbytes, pack_type;
336 int opcode = bytestream2_get_be16(&gbc);
340 bytestream2_skip(&gbc, 10);
344 av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
346 bytestream2_skip(&gbc, 30);
347 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
348 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
350 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
351 if (bppcnt == 1 && bpp == 8) {
352 avctx->pix_fmt = AV_PIX_FMT_PAL8;
353 } else if (bppcnt == 1 && (bpp == 4 || bpp == 2)) {
354 avctx->pix_fmt = AV_PIX_FMT_PAL8;
355 } else if (bppcnt == 3 && bpp == 5) {
356 avctx->pix_fmt = AV_PIX_FMT_RGB555;
358 av_log(avctx, AV_LOG_ERROR,
359 "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
361 return AVERROR_INVALIDDATA;
364 /* jump to palette */
365 bytestream2_skip(&gbc, 18);
366 colors = bytestream2_get_be16(&gbc);
368 if (colors < 0 || colors > 256) {
369 av_log(avctx, AV_LOG_ERROR,
370 "Error color count - %i(0x%X)\n", colors, colors);
371 return AVERROR_INVALIDDATA;
373 if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
374 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
375 bytestream2_get_bytes_left(&gbc));
376 return AVERROR_INVALIDDATA;
378 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
381 parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
382 p->palette_has_changed = 1;
384 /* jump to image data */
385 bytestream2_skip(&gbc, 18);
387 if (opcode == PACKBITSRGN) {
388 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
389 avpriv_report_missing_feature(avctx, "Packbit mask region");
392 if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
393 ret = decode_rle16(avctx, p, &gbc);
395 ret = decode_rle_bpp2(avctx, p, &gbc);
397 ret = decode_rle_bpp4(avctx, p, &gbc);
399 ret = decode_rle(avctx, p, &gbc, bppcnt);
406 av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
408 bytestream2_skip(&gbc, 4);
409 rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
410 if (rowbytes <= 250) {
411 avpriv_report_missing_feature(avctx, "Short rowbytes");
412 return AVERROR_PATCHWELCOME;
415 bytestream2_skip(&gbc, 4);
416 h = bytestream2_get_be16(&gbc);
417 w = bytestream2_get_be16(&gbc);
418 bytestream2_skip(&gbc, 2);
420 ret = ff_set_dimensions(avctx, w, h);
424 pack_type = bytestream2_get_be16(&gbc);
426 bytestream2_skip(&gbc, 16);
427 bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
428 bpp = bytestream2_get_be16(&gbc); /* cmpSize */
430 av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
431 if (bppcnt == 3 && bpp == 8) {
432 avctx->pix_fmt = AV_PIX_FMT_RGB24;
433 } else if (bppcnt == 3 && bpp == 5) {
434 avctx->pix_fmt = AV_PIX_FMT_RGB555;
435 } else if (bppcnt == 4 && bpp == 8) {
436 avctx->pix_fmt = AV_PIX_FMT_ARGB;
438 av_log(avctx, AV_LOG_ERROR,
439 "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
441 return AVERROR_INVALIDDATA;
444 /* set packing when default is selected */
448 if (pack_type != 3 && pack_type != 4) {
449 avpriv_request_sample(avctx, "Pack type %d", pack_type);
450 return AVERROR_PATCHWELCOME;
452 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
456 bytestream2_skip(&gbc, 30);
458 if (opcode == DIRECTBITSRGN) {
459 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
460 avpriv_report_missing_feature(avctx, "DirectBit mask region");
463 if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
464 ret = decode_rle16(avctx, p, &gbc);
466 ret = decode_rle(avctx, p, &gbc, bppcnt);
472 bytestream2_get_be16(&gbc);
473 bytestream2_skip(&gbc, bytestream2_get_be16(&gbc));
476 av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
479 /* exit the loop when a known pixel block has been found */
483 /* re-align to a word */
484 bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
486 eop = bytestream2_get_be16(&gbc);
487 trail = bytestream2_get_bytes_left(&gbc);
489 av_log(avctx, AV_LOG_WARNING,
490 "Missing end of picture opcode (found 0x%04X)\n", eop);
492 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
498 p->pict_type = AV_PICTURE_TYPE_I;
503 av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
505 return AVERROR_INVALIDDATA;
509 AVCodec ff_qdraw_decoder = {
511 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
512 .type = AVMEDIA_TYPE_VIDEO,
513 .id = AV_CODEC_ID_QDRAW,
514 .decode = decode_frame,
515 .capabilities = AV_CODEC_CAP_DR1,