00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/imgutils.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "cga_data.h"
00031
00032 typedef struct PicContext {
00033 AVFrame frame;
00034 int width, height;
00035 int nb_planes;
00036 } PicContext;
00037
00038 static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
00039 {
00040 while (run > 0) {
00041 uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
00042 if (*x + run >= s->width) {
00043 int n = s->width - *x;
00044 memset(d + *x, value, n);
00045 run -= n;
00046 *x = 0;
00047 *y -= 1;
00048 if (*y < 0)
00049 break;
00050 } else {
00051 memset(d + *x, value, run);
00052 *x += run;
00053 break;
00054 }
00055 }
00056 }
00057
00058 static void picmemset(PicContext *s, int value, int run, int *x, int *y, int *plane, int bits_per_plane)
00059 {
00060 uint8_t *d;
00061 int shift = *plane * bits_per_plane;
00062 int mask = ((1 << bits_per_plane) - 1) << shift;
00063 value <<= shift;
00064
00065 while (run > 0) {
00066 int j;
00067 for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
00068 d = s->frame.data[0] + *y * s->frame.linesize[0];
00069 d[*x] |= (value >> j) & mask;
00070 *x += 1;
00071 if (*x == s->width) {
00072 *y -= 1;
00073 *x = 0;
00074 if (*y < 0) {
00075 *y = s->height - 1;
00076 *plane += 1;
00077 value <<= bits_per_plane;
00078 mask <<= bits_per_plane;
00079 if (*plane >= s->nb_planes)
00080 break;
00081 }
00082 }
00083 }
00084 run--;
00085 }
00086 }
00087
00088 static const uint8_t cga_mode45_index[6][4] = {
00089 [0] = { 0, 3, 5, 7 },
00090 [1] = { 0, 2, 4, 6 },
00091 [2] = { 0, 3, 4, 7 },
00092 [3] = { 0, 11, 13, 15 },
00093 [4] = { 0, 10, 12, 14 },
00094 [5] = { 0, 11, 12, 15 },
00095 };
00096
00097 static av_cold int decode_init(AVCodecContext *avctx)
00098 {
00099 PicContext *s = avctx->priv_data;
00100
00101 avcodec_get_frame_defaults(&s->frame);
00102 return 0;
00103 }
00104
00105 static int decode_frame(AVCodecContext *avctx,
00106 void *data, int *data_size,
00107 AVPacket *avpkt)
00108 {
00109 PicContext *s = avctx->priv_data;
00110 int buf_size = avpkt->size;
00111 const uint8_t *buf = avpkt->data;
00112 const uint8_t *buf_end = avpkt->data + buf_size;
00113 uint32_t *palette;
00114 int bits_per_plane, bpp, etype, esize, npal;
00115 int i, x, y, plane;
00116
00117 if (buf_size < 11)
00118 return AVERROR_INVALIDDATA;
00119
00120 if (bytestream_get_le16(&buf) != 0x1234)
00121 return AVERROR_INVALIDDATA;
00122 s->width = bytestream_get_le16(&buf);
00123 s->height = bytestream_get_le16(&buf);
00124 buf += 4;
00125 bits_per_plane = *buf & 0xF;
00126 s->nb_planes = (*buf++ >> 4) + 1;
00127 bpp = s->nb_planes ? bits_per_plane*s->nb_planes : bits_per_plane;
00128 if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
00129 av_log_ask_for_sample(avctx, "unsupported bit depth\n");
00130 return AVERROR_INVALIDDATA;
00131 }
00132
00133 if (*buf == 0xFF || bpp == 8) {
00134 buf += 2;
00135 etype = bytestream_get_le16(&buf);
00136 esize = bytestream_get_le16(&buf);
00137 if (buf_end - buf < esize)
00138 return AVERROR_INVALIDDATA;
00139 } else {
00140 etype = -1;
00141 esize = 0;
00142 }
00143
00144 avctx->pix_fmt = PIX_FMT_PAL8;
00145
00146 if (s->width != avctx->width && s->height != avctx->height) {
00147 if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
00148 return -1;
00149 avcodec_set_dimensions(avctx, s->width, s->height);
00150 if (s->frame.data[0])
00151 avctx->release_buffer(avctx, &s->frame);
00152 }
00153
00154 if (avctx->get_buffer(avctx, &s->frame) < 0){
00155 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00156 return -1;
00157 }
00158 memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
00159 s->frame.pict_type = AV_PICTURE_TYPE_I;
00160 s->frame.palette_has_changed = 1;
00161
00162 palette = (uint32_t*)s->frame.data[1];
00163 if (etype == 1 && esize > 1 && *buf < 6) {
00164 int idx = *buf;
00165 npal = 4;
00166 for (i = 0; i < npal; i++)
00167 palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
00168 } else if (etype == 2) {
00169 npal = FFMIN(esize, 16);
00170 for (i = 0; i < npal; i++)
00171 palette[i] = ff_cga_palette[ FFMIN(buf[i], 16)];
00172 } else if (etype == 3) {
00173 npal = FFMIN(esize, 16);
00174 for (i = 0; i < npal; i++)
00175 palette[i] = ff_ega_palette[ FFMIN(buf[i], 63)];
00176 } else if (etype == 4 || etype == 5) {
00177 npal = FFMIN(esize / 3, 256);
00178 for (i = 0; i < npal; i++) {
00179 palette[i] = AV_RB24(buf + i*3) << 2;
00180 palette[i] |= 0xFF << 24 | palette[i] >> 6 & 0x30303;
00181 }
00182 } else {
00183 if (bpp == 1) {
00184 npal = 2;
00185 palette[0] = 0xFF000000;
00186 palette[1] = 0xFFFFFFFF;
00187 } else if (bpp == 2) {
00188 npal = 4;
00189 for (i = 0; i < npal; i++)
00190 palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
00191 } else {
00192 npal = 16;
00193 memcpy(palette, ff_cga_palette, npal * 4);
00194 }
00195 }
00196
00197 memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
00198 buf += esize;
00199
00200
00201 y = s->height - 1;
00202 if (bytestream_get_le16(&buf)) {
00203 x = 0;
00204 plane = 0;
00205 while (y >= 0 && buf_end - buf >= 6) {
00206 const uint8_t *buf_pend = buf + FFMIN(AV_RL16(buf), buf_end - buf);
00207
00208 int marker = buf[4];
00209 buf += 5;
00210
00211 while (plane < s->nb_planes && y >= 0 && buf_pend - buf >= 1) {
00212 int run = 1;
00213 int val = *buf++;
00214 if (val == marker) {
00215 run = *buf++;
00216 if (run == 0)
00217 run = bytestream_get_le16(&buf);
00218 val = *buf++;
00219 }
00220 if (buf > buf_end)
00221 break;
00222
00223 if (bits_per_plane == 8) {
00224 picmemset_8bpp(s, val, run, &x, &y);
00225 } else {
00226 picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
00227 }
00228 }
00229 }
00230 } else {
00231 while (y >= 0 && buf < buf_end) {
00232 memcpy(s->frame.data[0] + y * s->frame.linesize[0], buf, FFMIN(avctx->width, buf_end - buf));
00233 buf += avctx->width;
00234 y--;
00235 }
00236 }
00237
00238 *data_size = sizeof(AVFrame);
00239 *(AVFrame*)data = s->frame;
00240 return buf_size;
00241 }
00242
00243 static av_cold int decode_end(AVCodecContext *avctx)
00244 {
00245 PicContext *s = avctx->priv_data;
00246 if (s->frame.data[0])
00247 avctx->release_buffer(avctx, &s->frame);
00248 return 0;
00249 }
00250
00251 AVCodec ff_pictor_decoder = {
00252 .name = "pictor",
00253 .type = AVMEDIA_TYPE_VIDEO,
00254 .id = CODEC_ID_PICTOR,
00255 .priv_data_size = sizeof(PicContext),
00256 decode_init,
00257 .close = decode_end,
00258 .decode = decode_frame,
00259 .capabilities = CODEC_CAP_DR1,
00260 .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
00261 };