libavcodec/qdrw.c
d08d7142
 /*
  * QuickDraw (qdrw) codec
  * Copyright (c) 2004 Konstantin Shishkov
bb2cb0a6
  * Copyright (c) 2015 Vittorio Giovara
d08d7142
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
d08d7142
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
d08d7142
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
d08d7142
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d08d7142
  */
115329f1
 
d08d7142
 /**
ba87f080
  * @file
d08d7142
  * Apple QuickDraw codec.
bb2cb0a6
  * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
d08d7142
  */
115329f1
 
1d9c2dc8
 #include "libavutil/common.h"
6a5d31ac
 #include "libavutil/intreadwrite.h"
d08d7142
 #include "avcodec.h"
d00f1e0f
 #include "bytestream.h"
594d4d5d
 #include "internal.h"
d08d7142
 
bb2cb0a6
 enum QuickdrawOpcodes {
     PACKBITSRECT = 0x0098,
     PACKBITSRGN,
34efb8a1
     DIRECTBITSRECT,
     DIRECTBITSRGN,
d00f1e0f
 
bb2cb0a6
     EOP = 0x00FF,
 };
115329f1
 
bb2cb0a6
 static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
                          uint32_t *pal, int colors)
 {
     int i;
115329f1
 
d08d7142
     for (i = 0; i <= colors; i++) {
d00f1e0f
         uint8_t r, g, b;
bb2cb0a6
         unsigned int idx = bytestream2_get_be16(gbc); /* color index */
cca1a426
         if (idx > 255) {
bb2cb0a6
             av_log(avctx, AV_LOG_WARNING,
                    "Palette index out of range: %u\n", idx);
             bytestream2_skip(gbc, 6);
cca1a426
             continue;
         }
bb2cb0a6
         r = bytestream2_get_byte(gbc);
         bytestream2_skip(gbc, 1);
         g = bytestream2_get_byte(gbc);
         bytestream2_skip(gbc, 1);
         b = bytestream2_get_byte(gbc);
         bytestream2_skip(gbc, 1);
d0dce15d
         pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
d08d7142
     }
bb2cb0a6
     return 0;
 }
d08d7142
 
34efb8a1
 static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
                       int step)
bb2cb0a6
 {
34efb8a1
     int i, j;
     int offset = avctx->width * step;
bb2cb0a6
     uint8_t *outdata = p->data[0];
d00f1e0f
 
d08d7142
     for (i = 0; i < avctx->height; i++) {
         int size, left, code, pix;
d00f1e0f
         uint8_t *out = outdata;
34efb8a1
         int pos = 0;
115329f1
 
d00f1e0f
         /* size of packed line */
bb2cb0a6
         size = left = bytestream2_get_be16(gbc);
         if (bytestream2_get_bytes_left(gbc) < size)
44e2f0c3
             return AVERROR_INVALIDDATA;
 
d00f1e0f
         /* decode line */
d08d7142
         while (left > 0) {
bb2cb0a6
             code = bytestream2_get_byte(gbc);
d08d7142
             if (code & 0x80 ) { /* run */
bb2cb0a6
                 pix = bytestream2_get_byte(gbc);
34efb8a1
                 for (j = 0; j < 257 - code; j++) {
                     out[pos] = pix;
                     pos += step;
                     if (pos >= offset) {
                         pos -= offset;
                         pos++;
                     }
1eda5551
                     if (pos >= offset)
                         return AVERROR_INVALIDDATA;
34efb8a1
                 }
80cf2ebc
                 left  -= 2;
d08d7142
             } else { /* copy */
34efb8a1
                 for (j = 0; j < code + 1; j++) {
                     out[pos] = bytestream2_get_byte(gbc);
                     pos += step;
                     if (pos >= offset) {
                         pos -= offset;
                         pos++;
                     }
1eda5551
                     if (pos >= offset)
                         return AVERROR_INVALIDDATA;
34efb8a1
                 }
80cf2ebc
                 left  -= 2 + code;
d08d7142
             }
         }
759001c5
         outdata += p->linesize[0];
d08d7142
     }
bb2cb0a6
     return 0;
d08d7142
 }
 
5c8e4bf7
 static int check_header(const char *buf, int buf_size)
 {
     unsigned w, h, v0, v1;
 
     if (buf_size < 40)
         return 0;
 
     w = AV_RB16(buf+6);
     h = AV_RB16(buf+8);
     v0 = AV_RB16(buf+10);
     v1 = AV_RB16(buf+12);
 
     if (!w || !h)
         return 0;
 
     if (v0 == 0x1101)
         return 1;
     if (v0 == 0x0011 && v1 == 0x02FF)
         return 2;
     return 0;
 }
 
 
bb2cb0a6
 static int decode_frame(AVCodecContext *avctx,
                         void *data, int *got_frame,
                         AVPacket *avpkt)
80cf2ebc
 {
bb2cb0a6
     AVFrame * const p      = data;
     GetByteContext gbc;
     int colors;
     int w, h, ret;
019daa07
     int ver;
d08d7142
 
bb2cb0a6
     bytestream2_init(&gbc, avpkt->data, avpkt->size);
5c8e4bf7
     if (   bytestream2_get_bytes_left(&gbc) >= 552
            &&  check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
        )
0348e74c
         bytestream2_skip(&gbc, 512);
 
019daa07
     ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
 
bb2cb0a6
     /* smallest PICT header */
     if (bytestream2_get_bytes_left(&gbc) < 40) {
         av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
                bytestream2_get_bytes_left(&gbc));
         return AVERROR_INVALIDDATA;
     }
 
     bytestream2_skip(&gbc, 6);
     h = bytestream2_get_be16(&gbc);
     w = bytestream2_get_be16(&gbc);
 
     ret = ff_set_dimensions(avctx, w, h);
     if (ret < 0)
         return ret;
 
     /* version 1 is identified by 0x1101
      * it uses byte-aligned opcodes rather than word-aligned */
019daa07
     if (ver == 1) {
bb2cb0a6
         avpriv_request_sample(avctx, "QuickDraw version 1");
         return AVERROR_PATCHWELCOME;
019daa07
     } else if (ver != 2) {
         avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
         return AVERROR_PATCHWELCOME;
bb2cb0a6
     }
 
019daa07
     bytestream2_skip(&gbc, 4+26);
bb2cb0a6
 
     while (bytestream2_get_bytes_left(&gbc) >= 4) {
         int bppcnt, bpp;
34efb8a1
         int rowbytes, pack_type;
bb2cb0a6
         int opcode = bytestream2_get_be16(&gbc);
 
         switch(opcode) {
         case PACKBITSRECT:
         case PACKBITSRGN:
             av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
 
             bytestream2_skip(&gbc, 30);
             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
 
             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
             if (bppcnt == 1 && bpp == 8) {
                 avctx->pix_fmt = AV_PIX_FMT_PAL8;
             } else {
                 av_log(avctx, AV_LOG_ERROR,
                        "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
                        bppcnt, bpp);
                 return AVERROR_INVALIDDATA;
             }
 
             /* jump to palette */
             bytestream2_skip(&gbc, 18);
             colors = bytestream2_get_be16(&gbc);
 
             if (colors < 0 || colors > 256) {
                 av_log(avctx, AV_LOG_ERROR,
                        "Error color count - %i(0x%X)\n", colors, colors);
                 return AVERROR_INVALIDDATA;
             }
             if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
                 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
                        bytestream2_get_bytes_left(&gbc));
                 return AVERROR_INVALIDDATA;
             }
251f9dcf
             if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
bb2cb0a6
                 return ret;
 
             parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
             p->palette_has_changed = 1;
 
             /* jump to image data */
             bytestream2_skip(&gbc, 18);
 
             if (opcode == PACKBITSRGN) {
                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
                 avpriv_report_missing_feature(avctx, "Packbit mask region");
             }
 
34efb8a1
             ret = decode_rle(avctx, p, &gbc, bppcnt);
             if (ret < 0)
                 return ret;
             *got_frame = 1;
             break;
         case DIRECTBITSRECT:
         case DIRECTBITSRGN:
             av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
 
             bytestream2_skip(&gbc, 4);
             rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
             if (rowbytes <= 250) {
                 avpriv_report_missing_feature(avctx, "Short rowbytes");
                 return AVERROR_PATCHWELCOME;
             }
 
             bytestream2_skip(&gbc, 10);
             pack_type = bytestream2_get_be16(&gbc);
 
             bytestream2_skip(&gbc, 16);
             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
 
             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
             if (bppcnt == 3 && bpp == 8) {
                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
             } else if (bppcnt == 4 && bpp == 8) {
                 avctx->pix_fmt = AV_PIX_FMT_ARGB;
             } else {
                 av_log(avctx, AV_LOG_ERROR,
                        "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
                        bppcnt, bpp);
                 return AVERROR_INVALIDDATA;
             }
 
             /* set packing when default is selected */
             if (pack_type == 0)
                 pack_type = bppcnt;
 
             if (pack_type != 3 && pack_type != 4) {
                 avpriv_request_sample(avctx, "Pack type %d", pack_type);
                 return AVERROR_PATCHWELCOME;
             }
             if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
                 return ret;
             }
 
             /* jump to data */
             bytestream2_skip(&gbc, 30);
 
             if (opcode == DIRECTBITSRGN) {
                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
                 avpriv_report_missing_feature(avctx, "DirectBit mask region");
             }
 
             ret = decode_rle(avctx, p, &gbc, bppcnt);
bb2cb0a6
             if (ret < 0)
                 return ret;
             *got_frame = 1;
             break;
         default:
             av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
             break;
         }
         /* exit the loop when a known pixel block has been found */
         if (*got_frame) {
             int eop, trail;
 
             /* re-align to a word */
             bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
 
             eop = bytestream2_get_be16(&gbc);
             trail = bytestream2_get_bytes_left(&gbc);
             if (eop != EOP)
                 av_log(avctx, AV_LOG_WARNING,
                        "Missing end of picture opcode (found 0x%04X)\n", eop);
             if (trail)
                 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
             break;
         }
     }
 
     if (*got_frame) {
         p->pict_type = AV_PICTURE_TYPE_I;
         p->key_frame = 1;
 
         return avpkt->size;
     } else {
         av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
 
         return AVERROR_INVALIDDATA;
     }
d08d7142
 }
 
e7e2df27
 AVCodec ff_qdraw_decoder = {
ec6402b7
     .name           = "qdraw",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_QDRAW,
ec6402b7
     .decode         = decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
d08d7142
 };