1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-02 03:06:28 +02:00
FFmpeg/libavcodec/pictordec.c
Michael Niedermayer ac627b3d38 Merge commit '716d413c13981da15323c7a3821860536eefdbbb'
* commit '716d413c13981da15323c7a3821860536eefdbbb':
  Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat

Conflicts:
	doc/examples/muxing.c
	ffmpeg.h
	ffmpeg_filter.c
	ffmpeg_opt.c
	ffplay.c
	ffprobe.c
	libavcodec/8bps.c
	libavcodec/aasc.c
	libavcodec/aura.c
	libavcodec/avcodec.h
	libavcodec/avs.c
	libavcodec/bfi.c
	libavcodec/bmp.c
	libavcodec/bmpenc.c
	libavcodec/c93.c
	libavcodec/cscd.c
	libavcodec/cyuv.c
	libavcodec/dpx.c
	libavcodec/dpxenc.c
	libavcodec/eatgv.c
	libavcodec/escape124.c
	libavcodec/ffv1.c
	libavcodec/flashsv.c
	libavcodec/fraps.c
	libavcodec/h264.c
	libavcodec/huffyuv.c
	libavcodec/iff.c
	libavcodec/imgconvert.c
	libavcodec/indeo3.c
	libavcodec/kmvc.c
	libavcodec/libopenjpegdec.c
	libavcodec/libopenjpegenc.c
	libavcodec/libx264.c
	libavcodec/ljpegenc.c
	libavcodec/mjpegdec.c
	libavcodec/mjpegenc.c
	libavcodec/motionpixels.c
	libavcodec/mpeg12.c
	libavcodec/mpeg12enc.c
	libavcodec/mpeg4videodec.c
	libavcodec/mpegvideo_enc.c
	libavcodec/pamenc.c
	libavcodec/pcxenc.c
	libavcodec/pgssubdec.c
	libavcodec/pngdec.c
	libavcodec/pngenc.c
	libavcodec/pnm.c
	libavcodec/pnmdec.c
	libavcodec/pnmenc.c
	libavcodec/ptx.c
	libavcodec/qdrw.c
	libavcodec/qpeg.c
	libavcodec/qtrleenc.c
	libavcodec/raw.c
	libavcodec/rawdec.c
	libavcodec/rl2.c
	libavcodec/sgidec.c
	libavcodec/sgienc.c
	libavcodec/snowdec.c
	libavcodec/snowenc.c
	libavcodec/sunrast.c
	libavcodec/targa.c
	libavcodec/targaenc.c
	libavcodec/tiff.c
	libavcodec/tiffenc.c
	libavcodec/tmv.c
	libavcodec/truemotion2.c
	libavcodec/utils.c
	libavcodec/vb.c
	libavcodec/vp3.c
	libavcodec/wnv1.c
	libavcodec/xl.c
	libavcodec/xwddec.c
	libavcodec/xwdenc.c
	libavcodec/yop.c
	libavdevice/v4l2.c
	libavdevice/x11grab.c
	libavfilter/avfilter.c
	libavfilter/avfilter.h
	libavfilter/buffersrc.c
	libavfilter/drawutils.c
	libavfilter/formats.c
	libavfilter/src_movie.c
	libavfilter/vf_ass.c
	libavfilter/vf_drawtext.c
	libavfilter/vf_fade.c
	libavfilter/vf_format.c
	libavfilter/vf_hflip.c
	libavfilter/vf_lut.c
	libavfilter/vf_overlay.c
	libavfilter/vf_pad.c
	libavfilter/vf_scale.c
	libavfilter/vf_transpose.c
	libavfilter/vf_yadif.c
	libavfilter/video.c
	libavfilter/vsrc_testsrc.c
	libavformat/movenc.c
	libavformat/mxf.h
	libavformat/utils.c
	libavformat/yuv4mpeg.c
	libavutil/imgutils.c
	libavutil/pixdesc.c
	libswscale/input.c
	libswscale/output.c
	libswscale/swscale_internal.h
	libswscale/swscale_unscaled.c
	libswscale/utils.c
	libswscale/x86/swscale_template.c
	libswscale/x86/yuv2rgb.c
	libswscale/x86/yuv2rgb_template.c
	libswscale/yuv2rgb.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2012-10-08 21:06:57 +02:00

275 lines
8.8 KiB
C

/*
* 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
*/
/**
* @file
* Pictor/PC Paint decoder
*/
#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "cga_data.h"
typedef struct PicContext {
AVFrame frame;
int width, height;
int nb_planes;
GetByteContext g;
} PicContext;
static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
{
while (run > 0) {
uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
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;
}
}
}
static void picmemset(PicContext *s, int value, int run,
int *x, int *y, int *plane, int bits_per_plane)
{
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) {
d = s->frame.data[0] + *y * s->frame.linesize[0];
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 av_cold int decode_init(AVCodecContext *avctx)
{
PicContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->frame);
return 0;
}
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
PicContext *s = avctx->priv_data;
uint32_t *palette;
int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
int i, x, y, plane, tmp;
bytestream2_init(&s->g, avpkt->data, avpkt->size);
if (bytestream2_get_bytes_left(&s->g) < 11)
return AVERROR_INVALIDDATA;
if (bytestream2_get_le16u(&s->g) != 0x1234)
return AVERROR_INVALIDDATA;
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;
if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
av_log_ask_for_sample(avctx, "unsupported bit depth\n");
return AVERROR_INVALIDDATA;
}
if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 8) {
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)
return AVERROR_INVALIDDATA;
} else {
etype = -1;
esize = 0;
}
avctx->pix_fmt = AV_PIX_FMT_PAL8;
if (s->width != avctx->width && s->height != avctx->height) {
if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
return -1;
avcodec_set_dimensions(avctx, s->width, s->height);
if (s->frame.data[0])
avctx->release_buffer(avctx, &s->frame);
}
if (avctx->get_buffer(avctx, &s->frame) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
s->frame.pict_type = AV_PICTURE_TYPE_I;
s->frame.palette_has_changed = 1;
pos_after_pal = bytestream2_tell(&s->g) + esize;
palette = (uint32_t*)s->frame.data[1];
if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
int idx = bytestream2_get_byte(&s->g);
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);
for (i = 0; i < npal; i++) {
int pal_idx = bytestream2_get_byte(&s->g);
palette[i] = ff_cga_palette[FFMIN(pal_idx, 16)];
}
} else if (etype == 3) {
npal = FFMIN(esize, 16);
for (i = 0; i < npal; i++) {
int pal_idx = bytestream2_get_byte(&s->g);
palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
}
} else if (etype == 4 || etype == 5) {
npal = FFMIN(esize / 3, 256);
for (i = 0; i < npal; i++) {
palette[i] = bytestream2_get_be24(&s->g) << 2;
palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
}
} else {
if (bpp == 1) {
npal = 2;
palette[0] = 0xFF000000;
palette[1] = 0xFFFFFFFF;
} 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);
// skip remaining palette bytes
bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
y = s->height - 1;
if (bytestream2_get_le16(&s->g)) {
x = 0;
plane = 0;
while (y >= 0 && 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 && y >= 0 &&
bytestream2_get_bytes_left(&s->g) > stop_size) {
int run = 1;
int val = bytestream2_get_byte(&s->g);
if (val == marker) {
run = bytestream2_get_byte(&s->g);
if (run == 0)
run = bytestream2_get_le16(&s->g);
val = bytestream2_get_byte(&s->g);
}
if (!bytestream2_get_bytes_left(&s->g))
break;
if (bits_per_plane == 8) {
picmemset_8bpp(s, val, run, &x, &y);
} else {
picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
}
}
}
} else {
while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
memcpy(s->frame.data[0] + y * s->frame.linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
bytestream2_skip(&s->g, avctx->width);
y--;
}
}
*data_size = sizeof(AVFrame);
*(AVFrame*)data = s->frame;
return avpkt->size;
}
static av_cold int decode_end(AVCodecContext *avctx)
{
PicContext *s = avctx->priv_data;
if (s->frame.data[0])
avctx->release_buffer(avctx, &s->frame);
return 0;
}
AVCodec ff_pictor_decoder = {
.name = "pictor",
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_PICTOR,
.priv_data_size = sizeof(PicContext),
.init = decode_init,
.close = decode_end,
.decode = decode_frame,
.capabilities = CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
};