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