2010-06-08 14:55:06 +03:00
|
|
|
/*
|
|
|
|
* Pictor/PC Paint decoder
|
|
|
|
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-02 13:59:35 +03:00
|
|
|
* @file
|
2010-06-08 14:55:06 +03:00
|
|
|
* Pictor/PC Paint decoder
|
|
|
|
*/
|
|
|
|
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2010-06-08 14:55:06 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
#include "cga_data.h"
|
2022-03-16 22:09:54 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2010-06-08 14:55:06 +03:00
|
|
|
|
|
|
|
typedef struct PicContext {
|
|
|
|
int width, height;
|
|
|
|
int nb_planes;
|
2012-01-16 05:47:07 +03:00
|
|
|
GetByteContext g;
|
2010-06-08 14:55:06 +03:00
|
|
|
} PicContext;
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
|
|
|
|
int *x, int *y)
|
2010-06-08 14:55:06 +03:00
|
|
|
{
|
|
|
|
while (run > 0) {
|
2012-11-21 23:34:46 +03:00
|
|
|
uint8_t *d = frame->data[0] + *y * frame->linesize[0];
|
2010-06-08 14:55:06 +03:00
|
|
|
if (*x + run >= s->width) {
|
|
|
|
int n = s->width - *x;
|
|
|
|
memset(d + *x, value, n);
|
|
|
|
run -= n;
|
|
|
|
*x = 0;
|
|
|
|
*y -= 1;
|
|
|
|
if (*y < 0)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
memset(d + *x, value, run);
|
|
|
|
*x += run;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:33:18 +02:00
|
|
|
static void picmemset(PicContext *s, AVFrame *frame, unsigned value, int run,
|
2012-01-16 05:47:07 +03:00
|
|
|
int *x, int *y, int *plane, int bits_per_plane)
|
2010-06-08 14:55:06 +03:00
|
|
|
{
|
|
|
|
uint8_t *d;
|
|
|
|
int shift = *plane * bits_per_plane;
|
2017-03-21 03:20:35 +02:00
|
|
|
unsigned mask = ((1U << bits_per_plane) - 1) << shift;
|
2019-04-13 01:17:12 +02:00
|
|
|
int xl = *x;
|
|
|
|
int yl = *y;
|
|
|
|
int planel = *plane;
|
2019-08-02 23:10:35 +02:00
|
|
|
int pixels_per_value = 8/bits_per_plane;
|
2010-06-08 14:55:06 +03:00
|
|
|
value <<= shift;
|
|
|
|
|
2019-04-13 01:19:03 +02:00
|
|
|
d = frame->data[0] + yl * frame->linesize[0];
|
2010-06-08 14:55:06 +03:00
|
|
|
while (run > 0) {
|
|
|
|
int j;
|
|
|
|
for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
|
2019-04-13 01:17:12 +02:00
|
|
|
d[xl] |= (value >> j) & mask;
|
|
|
|
xl += 1;
|
2019-08-02 23:10:35 +02:00
|
|
|
while (xl == s->width) {
|
2019-04-13 01:17:12 +02:00
|
|
|
yl -= 1;
|
|
|
|
xl = 0;
|
|
|
|
if (yl < 0) {
|
|
|
|
yl = s->height - 1;
|
|
|
|
planel += 1;
|
|
|
|
if (planel >= s->nb_planes)
|
|
|
|
goto end;
|
2017-02-26 21:28:00 +02:00
|
|
|
value <<= bits_per_plane;
|
|
|
|
mask <<= bits_per_plane;
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
2019-04-13 01:19:03 +02:00
|
|
|
d = frame->data[0] + yl * frame->linesize[0];
|
2019-08-02 23:10:35 +02:00
|
|
|
if (s->nb_planes == 1 &&
|
|
|
|
run*pixels_per_value >= s->width &&
|
2021-08-31 19:48:18 +02:00
|
|
|
pixels_per_value < (s->width / pixels_per_value * pixels_per_value)
|
2019-08-02 23:10:35 +02:00
|
|
|
) {
|
|
|
|
for (; xl < pixels_per_value; xl ++) {
|
|
|
|
j = (j < bits_per_plane ? 8 : j) - bits_per_plane;
|
|
|
|
d[xl] |= (value >> j) & mask;
|
|
|
|
}
|
|
|
|
av_memcpy_backptr(d+xl, pixels_per_value, s->width - xl);
|
|
|
|
run -= s->width / pixels_per_value;
|
2021-08-31 19:48:18 +02:00
|
|
|
xl = s->width / pixels_per_value * pixels_per_value;
|
2019-08-02 23:10:35 +02:00
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
run--;
|
|
|
|
}
|
2019-04-13 01:17:12 +02:00
|
|
|
end:
|
|
|
|
*x = xl;
|
|
|
|
*y = yl;
|
|
|
|
*plane = planel;
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t cga_mode45_index[6][4] = {
|
|
|
|
[0] = { 0, 3, 5, 7 }, // mode4, palette#1, low intensity
|
|
|
|
[1] = { 0, 2, 4, 6 }, // mode4, palette#2, low intensity
|
|
|
|
[2] = { 0, 3, 4, 7 }, // mode5, low intensity
|
|
|
|
[3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
|
|
|
|
[4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
|
|
|
|
[5] = { 0, 11, 12, 15 }, // mode5, high intensity
|
|
|
|
};
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2010-06-08 14:55:06 +03:00
|
|
|
{
|
|
|
|
PicContext *s = avctx->priv_data;
|
|
|
|
uint32_t *palette;
|
2012-01-16 05:47:07 +03:00
|
|
|
int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
|
2013-03-12 05:20:18 +03:00
|
|
|
int i, x, y, plane, tmp, ret, val;
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2012-01-16 05:47:07 +03:00
|
|
|
bytestream2_init(&s->g, avpkt->data, avpkt->size);
|
|
|
|
|
|
|
|
if (bytestream2_get_bytes_left(&s->g) < 11)
|
2010-06-08 14:55:06 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-01-16 05:47:07 +03:00
|
|
|
if (bytestream2_get_le16u(&s->g) != 0x1234)
|
2010-06-08 14:55:06 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-16 05:47:07 +03:00
|
|
|
|
|
|
|
s->width = bytestream2_get_le16u(&s->g);
|
|
|
|
s->height = bytestream2_get_le16u(&s->g);
|
|
|
|
bytestream2_skip(&s->g, 4);
|
|
|
|
tmp = bytestream2_get_byteu(&s->g);
|
|
|
|
bits_per_plane = tmp & 0xF;
|
|
|
|
s->nb_planes = (tmp >> 4) + 1;
|
|
|
|
bpp = bits_per_plane * s->nb_planes;
|
2010-06-08 14:55:06 +03:00
|
|
|
if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
|
2013-03-14 14:52:12 +03:00
|
|
|
avpriv_request_sample(avctx, "Unsupported bit depth");
|
2012-12-23 20:10:05 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
|
2012-10-29 20:43:42 +03:00
|
|
|
if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 1 || bpp == 4 || bpp == 8) {
|
2012-01-16 05:47:07 +03:00
|
|
|
bytestream2_skip(&s->g, 2);
|
|
|
|
etype = bytestream2_get_le16(&s->g);
|
|
|
|
esize = bytestream2_get_le16(&s->g);
|
|
|
|
if (bytestream2_get_bytes_left(&s->g) < esize)
|
2010-06-08 14:55:06 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
} else {
|
|
|
|
etype = -1;
|
|
|
|
esize = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2013-09-03 22:45:25 +03:00
|
|
|
if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
|
|
|
|
return -1;
|
2022-11-22 22:54:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
There are 2 coding modes, RLE and RAW.
|
|
|
|
Undamaged RAW should be proportional to W*H and thus bigger than RLE
|
|
|
|
RLE codes the most compressed runs by
|
|
|
|
1 byte for val (=marker)
|
|
|
|
1 byte run (=0)
|
|
|
|
2 bytes run
|
|
|
|
1 byte val
|
|
|
|
thats 5 bytes and the maximum run we can code is 65535
|
|
|
|
|
|
|
|
The RLE decoder can exit prematurly but it does not on any image available
|
|
|
|
Based on this the formula is assumed correct for undamaged images.
|
|
|
|
If an image is found which exploits the special end
|
|
|
|
handling and breaks this formula then this needs to be adapted.
|
|
|
|
*/
|
|
|
|
if (bytestream2_get_bytes_left(&s->g) < s->width * s->height / 65535 * 5)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2017-02-07 16:49:09 +02:00
|
|
|
if (s->width != avctx->width || s->height != avctx->height) {
|
2013-10-27 12:02:26 +03:00
|
|
|
ret = ff_set_dimensions(avctx, s->width, s->height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
|
|
|
memset(frame->data[0], 0, s->height * frame->linesize[0]);
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
2023-05-18 03:39:57 +02:00
|
|
|
#if FF_API_PALETTE_HAS_CHANGED
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->palette_has_changed = 1;
|
2023-05-18 03:39:57 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2012-01-16 05:47:07 +03:00
|
|
|
pos_after_pal = bytestream2_tell(&s->g) + esize;
|
2012-11-21 23:34:46 +03:00
|
|
|
palette = (uint32_t*)frame->data[1];
|
2012-01-16 05:47:07 +03:00
|
|
|
if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
|
|
|
|
int idx = bytestream2_get_byte(&s->g);
|
2010-06-08 14:55:06 +03:00
|
|
|
npal = 4;
|
|
|
|
for (i = 0; i < npal; i++)
|
|
|
|
palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
|
|
|
|
} else if (etype == 2) {
|
|
|
|
npal = FFMIN(esize, 16);
|
2012-01-16 05:47:07 +03:00
|
|
|
for (i = 0; i < npal; i++) {
|
|
|
|
int pal_idx = bytestream2_get_byte(&s->g);
|
2013-02-10 20:31:05 +03:00
|
|
|
palette[i] = ff_cga_palette[FFMIN(pal_idx, 15)];
|
2012-01-16 05:47:07 +03:00
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
} else if (etype == 3) {
|
|
|
|
npal = FFMIN(esize, 16);
|
2012-01-16 05:47:07 +03:00
|
|
|
for (i = 0; i < npal; i++) {
|
|
|
|
int pal_idx = bytestream2_get_byte(&s->g);
|
|
|
|
palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
|
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
} else if (etype == 4 || etype == 5) {
|
|
|
|
npal = FFMIN(esize / 3, 256);
|
2011-11-13 05:08:25 +03:00
|
|
|
for (i = 0; i < npal; i++) {
|
2012-01-16 05:47:07 +03:00
|
|
|
palette[i] = bytestream2_get_be24(&s->g) << 2;
|
2012-01-17 03:40:45 +03:00
|
|
|
palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
|
2011-11-13 05:08:25 +03:00
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
} else {
|
|
|
|
if (bpp == 1) {
|
|
|
|
npal = 2;
|
2011-11-13 05:08:25 +03:00
|
|
|
palette[0] = 0xFF000000;
|
|
|
|
palette[1] = 0xFFFFFFFF;
|
2010-06-08 14:55:06 +03:00
|
|
|
} else if (bpp == 2) {
|
|
|
|
npal = 4;
|
|
|
|
for (i = 0; i < npal; i++)
|
|
|
|
palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
|
|
|
|
} else {
|
|
|
|
npal = 16;
|
|
|
|
memcpy(palette, ff_cga_palette, npal * 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fill remaining palette entries
|
|
|
|
memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
|
2012-01-16 05:47:07 +03:00
|
|
|
// skip remaining palette bytes
|
|
|
|
bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2012-11-10 02:31:14 +03:00
|
|
|
val = 0;
|
2010-06-08 14:55:06 +03:00
|
|
|
y = s->height - 1;
|
2012-01-16 05:47:07 +03:00
|
|
|
if (bytestream2_get_le16(&s->g)) {
|
2011-12-02 11:57:34 +03:00
|
|
|
x = 0;
|
|
|
|
plane = 0;
|
2013-09-03 22:49:12 +03:00
|
|
|
while (bytestream2_get_bytes_left(&s->g) >= 6) {
|
2012-01-16 05:47:07 +03:00
|
|
|
int stop_size, marker, t1, t2;
|
|
|
|
|
|
|
|
t1 = bytestream2_get_bytes_left(&s->g);
|
|
|
|
t2 = bytestream2_get_le16(&s->g);
|
|
|
|
stop_size = t1 - FFMIN(t1, t2);
|
|
|
|
// ignore uncompressed block size
|
|
|
|
bytestream2_skip(&s->g, 2);
|
|
|
|
marker = bytestream2_get_byte(&s->g);
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2013-09-03 22:49:12 +03:00
|
|
|
while (plane < s->nb_planes &&
|
2012-01-16 05:47:07 +03:00
|
|
|
bytestream2_get_bytes_left(&s->g) > stop_size) {
|
2010-06-08 14:55:06 +03:00
|
|
|
int run = 1;
|
2012-11-10 02:31:14 +03:00
|
|
|
val = bytestream2_get_byte(&s->g);
|
2010-06-08 14:55:06 +03:00
|
|
|
if (val == marker) {
|
2012-01-16 05:47:07 +03:00
|
|
|
run = bytestream2_get_byte(&s->g);
|
2010-06-08 14:55:06 +03:00
|
|
|
if (run == 0)
|
2012-01-16 05:47:07 +03:00
|
|
|
run = bytestream2_get_le16(&s->g);
|
|
|
|
val = bytestream2_get_byte(&s->g);
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bits_per_plane == 8) {
|
2012-11-21 23:34:46 +03:00
|
|
|
picmemset_8bpp(s, frame, val, run, &x, &y);
|
2010-06-08 14:55:06 +03:00
|
|
|
if (y < 0)
|
2013-08-24 22:30:46 +03:00
|
|
|
goto finish;
|
2010-06-08 14:55:06 +03:00
|
|
|
} else {
|
2012-11-21 23:34:46 +03:00
|
|
|
picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-10 02:31:14 +03:00
|
|
|
|
2018-09-12 02:48:04 +02:00
|
|
|
if (s->nb_planes - plane > 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2017-02-20 13:31:43 +02:00
|
|
|
if (plane < s->nb_planes && x < avctx->width) {
|
2012-11-10 02:31:14 +03:00
|
|
|
int run = (y + 1) * avctx->width - x;
|
|
|
|
if (bits_per_plane == 8)
|
2013-03-12 05:20:18 +03:00
|
|
|
picmemset_8bpp(s, frame, val, run, &x, &y);
|
2012-11-10 02:31:14 +03:00
|
|
|
else
|
2013-03-12 05:20:18 +03:00
|
|
|
picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
|
2012-11-10 02:31:14 +03:00
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
} else {
|
2012-01-17 03:40:45 +03:00
|
|
|
while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
|
2013-03-12 05:20:18 +03:00
|
|
|
memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
|
2012-01-17 03:40:45 +03:00
|
|
|
bytestream2_skip(&s->g, avctx->width);
|
2011-12-02 11:57:34 +03:00
|
|
|
y--;
|
|
|
|
}
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
2013-08-24 22:30:46 +03:00
|
|
|
finish:
|
2010-06-08 14:55:06 +03:00
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2012-01-16 05:47:07 +03:00
|
|
|
return avpkt->size;
|
2010-06-08 14:55:06 +03:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_pictor_decoder = {
|
|
|
|
.p.name = "pictor",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Pictor/PC Paint"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_PICTOR,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(PicContext),
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame),
|
2010-06-08 14:55:06 +03:00
|
|
|
};
|