+ bytestream2_skip(&gbc, 26);
+
+ while (bytestream2_get_bytes_left(&gbc) >= 4) {
+ int bppcnt, bpp;
+ int rowbytes, pack_type;
+ 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;
+ }
+ if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+ 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");
+ }
+
+ 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);
+ 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;
+ }