00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "bytestream.h"
00028 #include "get_bits.h"
00029
00030 typedef struct PCXContext {
00031 AVFrame picture;
00032 } PCXContext;
00033
00034 static av_cold int pcx_init(AVCodecContext *avctx) {
00035 PCXContext *s = avctx->priv_data;
00036
00037 avcodec_get_frame_defaults(&s->picture);
00038 avctx->coded_frame= &s->picture;
00039
00040 return 0;
00041 }
00042
00046 static const uint8_t *pcx_rle_decode(const uint8_t *src,
00047 const uint8_t *end,
00048 uint8_t *dst,
00049 unsigned int bytes_per_scanline,
00050 int compressed) {
00051 unsigned int i = 0;
00052 unsigned char run, value;
00053
00054 if (compressed) {
00055 while (i < bytes_per_scanline && src < end) {
00056 run = 1;
00057 value = *src++;
00058 if (value >= 0xc0 && src < end) {
00059 run = value & 0x3f;
00060 value = *src++;
00061 }
00062 while (i<bytes_per_scanline && run--)
00063 dst[i++] = value;
00064 }
00065 } else {
00066 memcpy(dst, src, bytes_per_scanline);
00067 src += bytes_per_scanline;
00068 }
00069
00070 return src;
00071 }
00072
00073 static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
00074 unsigned int i;
00075
00076 for (i=0; i<pallen; i++)
00077 *dst++ = bytestream_get_be24(src);
00078 if (pallen < 256)
00079 memset(dst, 0, (256 - pallen) * sizeof(*dst));
00080 }
00081
00082 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00083 AVPacket *avpkt) {
00084 const uint8_t *buf = avpkt->data;
00085 int buf_size = avpkt->size;
00086 PCXContext * const s = avctx->priv_data;
00087 AVFrame *picture = data;
00088 AVFrame * const p = &s->picture;
00089 int compressed, xmin, ymin, xmax, ymax;
00090 unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
00091 bytes_per_scanline;
00092 uint8_t *ptr;
00093 const uint8_t *buf_end = buf + buf_size;
00094 uint8_t const *bufstart = buf;
00095 uint8_t *scanline;
00096 int ret = -1;
00097
00098 if (buf[0] != 0x0a || buf[1] > 5) {
00099 av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
00100 return -1;
00101 }
00102
00103 compressed = buf[2];
00104 xmin = AV_RL16(buf+ 4);
00105 ymin = AV_RL16(buf+ 6);
00106 xmax = AV_RL16(buf+ 8);
00107 ymax = AV_RL16(buf+10);
00108
00109 if (xmax < xmin || ymax < ymin) {
00110 av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
00111 return -1;
00112 }
00113
00114 w = xmax - xmin + 1;
00115 h = ymax - ymin + 1;
00116
00117 bits_per_pixel = buf[3];
00118 bytes_per_line = AV_RL16(buf+66);
00119 nplanes = buf[65];
00120 bytes_per_scanline = nplanes * bytes_per_line;
00121
00122 if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8 ||
00123 (!compressed && bytes_per_scanline > buf_size / h)) {
00124 av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
00125 return -1;
00126 }
00127
00128 switch ((nplanes<<8) + bits_per_pixel) {
00129 case 0x0308:
00130 avctx->pix_fmt = PIX_FMT_RGB24;
00131 break;
00132 case 0x0108:
00133 case 0x0104:
00134 case 0x0102:
00135 case 0x0101:
00136 case 0x0401:
00137 case 0x0301:
00138 case 0x0201:
00139 avctx->pix_fmt = PIX_FMT_PAL8;
00140 break;
00141 default:
00142 av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
00143 return -1;
00144 }
00145
00146 buf += 128;
00147
00148 if (p->data[0])
00149 avctx->release_buffer(avctx, p);
00150
00151 if (av_image_check_size(w, h, 0, avctx))
00152 return -1;
00153 if (w != avctx->width || h != avctx->height)
00154 avcodec_set_dimensions(avctx, w, h);
00155 if (avctx->get_buffer(avctx, p) < 0) {
00156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00157 return -1;
00158 }
00159
00160 p->pict_type = AV_PICTURE_TYPE_I;
00161
00162 ptr = p->data[0];
00163 stride = p->linesize[0];
00164
00165 scanline = av_malloc(bytes_per_scanline);
00166 if (!scanline)
00167 return AVERROR(ENOMEM);
00168
00169 if (nplanes == 3 && bits_per_pixel == 8) {
00170 for (y=0; y<h; y++) {
00171 buf = pcx_rle_decode(buf, buf_end,
00172 scanline, bytes_per_scanline, compressed);
00173
00174 for (x=0; x<w; x++) {
00175 ptr[3*x ] = scanline[x ];
00176 ptr[3*x+1] = scanline[x+ bytes_per_line ];
00177 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
00178 }
00179
00180 ptr += stride;
00181 }
00182
00183 } else if (nplanes == 1 && bits_per_pixel == 8) {
00184 const uint8_t *palstart = bufstart + buf_size - 769;
00185
00186 for (y=0; y<h; y++, ptr+=stride) {
00187 buf = pcx_rle_decode(buf, buf_end,
00188 scanline, bytes_per_scanline, compressed);
00189 memcpy(ptr, scanline, w);
00190 }
00191
00192 if (buf != palstart) {
00193 av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
00194 buf = palstart;
00195 }
00196 if (*buf++ != 12) {
00197 av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
00198 goto end;
00199 }
00200
00201 } else if (nplanes == 1) {
00202 GetBitContext s;
00203
00204 for (y=0; y<h; y++) {
00205 init_get_bits(&s, scanline, bytes_per_scanline<<3);
00206
00207 buf = pcx_rle_decode(buf, buf_end,
00208 scanline, bytes_per_scanline, compressed);
00209
00210 for (x=0; x<w; x++)
00211 ptr[x] = get_bits(&s, bits_per_pixel);
00212 ptr += stride;
00213 }
00214
00215 } else {
00216 int i;
00217
00218 for (y=0; y<h; y++) {
00219 buf = pcx_rle_decode(buf, buf_end,
00220 scanline, bytes_per_scanline, compressed);
00221
00222 for (x=0; x<w; x++) {
00223 int m = 0x80 >> (x&7), v = 0;
00224 for (i=nplanes - 1; i>=0; i--) {
00225 v <<= 1;
00226 v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
00227 }
00228 ptr[x] = v;
00229 }
00230 ptr += stride;
00231 }
00232 }
00233
00234 if (nplanes == 1 && bits_per_pixel == 8) {
00235 pcx_palette(&buf, (uint32_t *) p->data[1], 256);
00236 } else if (bits_per_pixel < 8) {
00237 const uint8_t *palette = bufstart+16;
00238 pcx_palette(&palette, (uint32_t *) p->data[1], 16);
00239 }
00240
00241 *picture = s->picture;
00242 *data_size = sizeof(AVFrame);
00243
00244 ret = buf - bufstart;
00245 end:
00246 av_free(scanline);
00247 return ret;
00248 }
00249
00250 static av_cold int pcx_end(AVCodecContext *avctx) {
00251 PCXContext *s = avctx->priv_data;
00252
00253 if(s->picture.data[0])
00254 avctx->release_buffer(avctx, &s->picture);
00255
00256 return 0;
00257 }
00258
00259 AVCodec ff_pcx_decoder = {
00260 .name = "pcx",
00261 .type = AVMEDIA_TYPE_VIDEO,
00262 .id = CODEC_ID_PCX,
00263 .priv_data_size = sizeof(PCXContext),
00264 .init = pcx_init,
00265 .close = pcx_end,
00266 .decode = pcx_decode_frame,
00267 .capabilities = CODEC_CAP_DR1,
00268 .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
00269 };