2010-06-08 13:55:06 +02:00
|
|
|
/*
|
|
|
|
* Pictor/PC Paint decoder
|
|
|
|
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2010-06-08 13:55:06 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-06-08 13:55:06 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-06-08 13:55:06 +02:00
|
|
|
* 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-06-08 13:55:06 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-02 12:59:35 +02:00
|
|
|
* @file
|
2010-06-08 13:55:06 +02:00
|
|
|
* Pictor/PC Paint decoder
|
|
|
|
*/
|
|
|
|
|
2011-02-07 14:37:08 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2010-06-08 13:55:06 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
#include "cga_data.h"
|
2012-11-10 13:22:56 +01:00
|
|
|
#include "internal.h"
|
2010-06-08 13:55:06 +02:00
|
|
|
|
|
|
|
typedef struct PicContext {
|
|
|
|
int width, height;
|
|
|
|
int nb_planes;
|
2012-01-16 03:47:07 +01:00
|
|
|
GetByteContext g;
|
2010-06-08 13:55:06 +02:00
|
|
|
} PicContext;
|
|
|
|
|
2012-11-21 21:34:46 +01:00
|
|
|
static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
|
|
|
|
int *x, int *y)
|
2010-06-08 13:55:06 +02:00
|
|
|
{
|
|
|
|
while (run > 0) {
|
2012-11-21 21:34:46 +01:00
|
|
|
uint8_t *d = frame->data[0] + *y * frame->linesize[0];
|
2010-06-08 13:55:06 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 21:34:46 +01:00
|
|
|
static void picmemset(PicContext *s, AVFrame *frame, int value, int run,
|
2012-01-16 03:47:07 +01:00
|
|
|
int *x, int *y, int *plane, int bits_per_plane)
|
2010-06-08 13:55:06 +02:00
|
|
|
{
|
|
|
|
uint8_t *d;
|
|
|
|
int shift = *plane * bits_per_plane;
|
|
|
|
int mask = ((1 << bits_per_plane) - 1) << shift;
|
|
|
|
value <<= shift;
|
|
|
|
|
|
|
|
while (run > 0) {
|
|
|
|
int j;
|
|
|
|
for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
|
2012-11-21 21:34:46 +01:00
|
|
|
d = frame->data[0] + *y * frame->linesize[0];
|
2010-06-08 13:55:06 +02:00
|
|
|
d[*x] |= (value >> j) & mask;
|
|
|
|
*x += 1;
|
|
|
|
if (*x == s->width) {
|
|
|
|
*y -= 1;
|
|
|
|
*x = 0;
|
|
|
|
if (*y < 0) {
|
|
|
|
*y = s->height - 1;
|
|
|
|
*plane += 1;
|
|
|
|
value <<= bits_per_plane;
|
|
|
|
mask <<= bits_per_plane;
|
|
|
|
if (*plane >= s->nb_planes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
static int decode_frame(AVCodecContext *avctx,
|
2012-11-13 19:35:22 +01:00
|
|
|
void *data, int *got_frame,
|
2010-06-08 13:55:06 +02:00
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
PicContext *s = avctx->priv_data;
|
2012-11-21 21:34:46 +01:00
|
|
|
AVFrame *frame = data;
|
2010-06-08 13:55:06 +02:00
|
|
|
uint32_t *palette;
|
2012-01-16 03:47:07 +01:00
|
|
|
int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
|
2012-11-21 21:34:46 +01:00
|
|
|
int i, x, y, plane, tmp, ret;
|
2010-06-08 13:55:06 +02:00
|
|
|
|
2012-01-16 03:47:07 +01:00
|
|
|
bytestream2_init(&s->g, avpkt->data, avpkt->size);
|
|
|
|
|
|
|
|
if (bytestream2_get_bytes_left(&s->g) < 11)
|
2010-06-08 13:55:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-01-16 03:47:07 +01:00
|
|
|
if (bytestream2_get_le16u(&s->g) != 0x1234)
|
2010-06-08 13:55:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-16 03:47:07 +01: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 13:55:06 +02:00
|
|
|
if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
|
2013-06-29 05:41:58 +02:00
|
|
|
avpriv_request_sample(avctx, "Unsupported bit depth");
|
2012-12-23 18:10:05 +01:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
|
|
|
|
2012-01-16 03:47:07 +01:00
|
|
|
if (bytestream2_peek_byte(&s->g) == 0xFF) {
|
|
|
|
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 13:55:06 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
} else {
|
|
|
|
etype = -1;
|
|
|
|
esize = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2010-06-08 13:55:06 +02:00
|
|
|
|
|
|
|
if (s->width != avctx->width && s->height != avctx->height) {
|
2013-10-27 10:02:26 +01:00
|
|
|
ret = ff_set_dimensions(avctx, s->width, s->height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 21:34:46 +01:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
|
2010-06-08 13:55:06 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-11-21 21:34:46 +01:00
|
|
|
return ret;
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
2012-11-21 21:34:46 +01:00
|
|
|
memset(frame->data[0], 0, s->height * frame->linesize[0]);
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
frame->palette_has_changed = 1;
|
2010-06-08 13:55:06 +02:00
|
|
|
|
2012-01-16 03:47:07 +01:00
|
|
|
pos_after_pal = bytestream2_tell(&s->g) + esize;
|
2012-11-21 21:34:46 +01:00
|
|
|
palette = (uint32_t*)frame->data[1];
|
2012-01-16 03:47:07 +01:00
|
|
|
if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
|
|
|
|
int idx = bytestream2_get_byte(&s->g);
|
2010-06-08 13:55:06 +02: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 03:47:07 +01:00
|
|
|
for (i = 0; i < npal; i++) {
|
|
|
|
int pal_idx = bytestream2_get_byte(&s->g);
|
|
|
|
palette[i] = ff_cga_palette[FFMIN(pal_idx, 16)];
|
|
|
|
}
|
2010-06-08 13:55:06 +02:00
|
|
|
} else if (etype == 3) {
|
|
|
|
npal = FFMIN(esize, 16);
|
2012-01-16 03:47:07 +01: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 13:55:06 +02:00
|
|
|
} else if (etype == 4 || etype == 5) {
|
|
|
|
npal = FFMIN(esize / 3, 256);
|
|
|
|
for (i = 0; i < npal; i++)
|
2012-01-16 03:47:07 +01:00
|
|
|
palette[i] = bytestream2_get_be24(&s->g) << 2;
|
2010-06-08 13:55:06 +02:00
|
|
|
} else {
|
|
|
|
if (bpp == 1) {
|
|
|
|
npal = 2;
|
|
|
|
palette[0] = 0x000000;
|
|
|
|
palette[1] = 0xFFFFFF;
|
|
|
|
} 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 03:47:07 +01:00
|
|
|
// skip remaining palette bytes
|
|
|
|
bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
|
2010-06-08 13:55:06 +02:00
|
|
|
|
|
|
|
x = 0;
|
|
|
|
y = s->height - 1;
|
|
|
|
plane = 0;
|
2012-01-16 03:47:07 +01:00
|
|
|
if (bytestream2_get_le16(&s->g)) {
|
|
|
|
while (bytestream2_get_bytes_left(&s->g) >= 6) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
while (plane < s->nb_planes &&
|
|
|
|
bytestream2_get_bytes_left(&s->g) > stop_size) {
|
2010-06-08 13:55:06 +02:00
|
|
|
int run = 1;
|
2012-01-16 03:47:07 +01:00
|
|
|
int val = bytestream2_get_byte(&s->g);
|
2010-06-08 13:55:06 +02:00
|
|
|
if (val == marker) {
|
2012-01-16 03:47:07 +01:00
|
|
|
run = bytestream2_get_byte(&s->g);
|
2010-06-08 13:55:06 +02:00
|
|
|
if (run == 0)
|
2012-01-16 03:47:07 +01:00
|
|
|
run = bytestream2_get_le16(&s->g);
|
|
|
|
val = bytestream2_get_byte(&s->g);
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
2012-01-16 03:47:07 +01:00
|
|
|
if (!bytestream2_get_bytes_left(&s->g))
|
2010-06-08 13:55:06 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (bits_per_plane == 8) {
|
2012-11-21 21:34:46 +01:00
|
|
|
picmemset_8bpp(s, frame, val, run, &x, &y);
|
2010-06-08 13:55:06 +02:00
|
|
|
if (y < 0)
|
2013-08-24 21:30:46 +02:00
|
|
|
goto finish;
|
2010-06-08 13:55:06 +02:00
|
|
|
} else {
|
2012-11-21 21:34:46 +01:00
|
|
|
picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-24 21:30:46 +02:00
|
|
|
avpriv_request_sample(avctx, "Uncompressed image");
|
2012-01-16 03:47:07 +01:00
|
|
|
return avpkt->size;
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
2013-08-24 21:30:46 +02:00
|
|
|
finish:
|
2010-06-08 13:55:06 +02:00
|
|
|
|
2012-11-13 19:35:22 +01:00
|
|
|
*got_frame = 1;
|
2012-01-16 03:47:07 +01:00
|
|
|
return avpkt->size;
|
2010-06-08 13:55:06 +02:00
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_pictor_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "pictor",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
|
2011-07-17 12:54:31 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_PICTOR,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(PicContext),
|
|
|
|
.decode = decode_frame,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2010-06-08 13:55:06 +02:00
|
|
|
};
|