]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcx.c
mpeg12: do not fail on zero dimensions in the sequence header.
[ffmpeg] / libavcodec / pcx.c
1 /*
2  * PC Paintbrush PCX (.pcx) image decoder
3  * Copyright (c) 2007, 2008 Ivo van Poorten
4  *
5  * This decoder does not support CGA palettes. I am unable to find samples
6  * and Netpbm cannot generate them.
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "get_bits.h"
29 #include "internal.h"
30
31 /**
32  * @return advanced src pointer
33  */
34 static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
35                             unsigned int bytes_per_scanline, int compressed) {
36     unsigned int i = 0;
37     unsigned char run, value;
38
39     if (compressed) {
40         while (i<bytes_per_scanline) {
41             run = 1;
42             value = *src++;
43             if (value >= 0xc0) {
44                 run = value & 0x3f;
45                 value = *src++;
46             }
47             while (i<bytes_per_scanline && run--)
48                 dst[i++] = value;
49         }
50     } else {
51         memcpy(dst, src, bytes_per_scanline);
52         src += bytes_per_scanline;
53     }
54
55     return src;
56 }
57
58 static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
59     unsigned int i;
60
61     for (i=0; i<pallen; i++)
62         *dst++ = bytestream_get_be24(src);
63     if (pallen < 256)
64         memset(dst, 0, (256 - pallen) * sizeof(*dst));
65 }
66
67 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
68                             AVPacket *avpkt) {
69     const uint8_t *buf = avpkt->data;
70     int buf_size = avpkt->size;
71     AVFrame * const p = data;
72     int compressed, xmin, ymin, xmax, ymax;
73     unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
74                  bytes_per_scanline;
75     uint8_t *ptr;
76     uint8_t const *bufstart = buf;
77     uint8_t *scanline;
78     int ret = -1;
79
80     if (buf[0] != 0x0a || buf[1] > 5) {
81         av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
82         return AVERROR_INVALIDDATA;
83     }
84
85     compressed = buf[2];
86     xmin = AV_RL16(buf+ 4);
87     ymin = AV_RL16(buf+ 6);
88     xmax = AV_RL16(buf+ 8);
89     ymax = AV_RL16(buf+10);
90
91     if (xmax < xmin || ymax < ymin) {
92         av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
93         return AVERROR_INVALIDDATA;
94     }
95
96     w = xmax - xmin + 1;
97     h = ymax - ymin + 1;
98
99     bits_per_pixel     = buf[3];
100     bytes_per_line     = AV_RL16(buf+66);
101     nplanes            = buf[65];
102     bytes_per_scanline = nplanes * bytes_per_line;
103
104     if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
105         av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
106         return AVERROR_INVALIDDATA;
107     }
108
109     switch ((nplanes<<8) + bits_per_pixel) {
110         case 0x0308:
111             avctx->pix_fmt = AV_PIX_FMT_RGB24;
112             break;
113         case 0x0108:
114         case 0x0104:
115         case 0x0102:
116         case 0x0101:
117         case 0x0401:
118         case 0x0301:
119         case 0x0201:
120             avctx->pix_fmt = AV_PIX_FMT_PAL8;
121             break;
122         default:
123             av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
124             return AVERROR_INVALIDDATA;
125     }
126
127     buf += 128;
128
129     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
130         return ret;
131     if (w != avctx->width || h != avctx->height)
132         avcodec_set_dimensions(avctx, w, h);
133     if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
134         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
135         return ret;
136     }
137
138     p->pict_type = AV_PICTURE_TYPE_I;
139
140     ptr    = p->data[0];
141     stride = p->linesize[0];
142
143     scanline = av_malloc(bytes_per_scanline);
144     if (!scanline)
145         return AVERROR(ENOMEM);
146
147     if (nplanes == 3 && bits_per_pixel == 8) {
148         for (y=0; y<h; y++) {
149             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
150
151             for (x=0; x<w; x++) {
152                 ptr[3*x  ] = scanline[x                    ];
153                 ptr[3*x+1] = scanline[x+ bytes_per_line    ];
154                 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
155             }
156
157             ptr += stride;
158         }
159
160     } else if (nplanes == 1 && bits_per_pixel == 8) {
161         const uint8_t *palstart = bufstart + buf_size - 769;
162
163         for (y=0; y<h; y++, ptr+=stride) {
164             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
165             memcpy(ptr, scanline, w);
166         }
167
168         if (buf != palstart) {
169             av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
170             buf = palstart;
171         }
172         if (*buf++ != 12) {
173             av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
174             goto end;
175         }
176
177     } else if (nplanes == 1) {   /* all packed formats, max. 16 colors */
178         GetBitContext s;
179
180         for (y=0; y<h; y++) {
181             init_get_bits(&s, scanline, bytes_per_scanline<<3);
182
183             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
184
185             for (x=0; x<w; x++)
186                 ptr[x] = get_bits(&s, bits_per_pixel);
187             ptr += stride;
188         }
189
190     } else {    /* planar, 4, 8 or 16 colors */
191         int i;
192
193         for (y=0; y<h; y++) {
194             buf = pcx_rle_decode(buf, scanline, bytes_per_scanline, compressed);
195
196             for (x=0; x<w; x++) {
197                 int m = 0x80 >> (x&7), v = 0;
198                 for (i=nplanes - 1; i>=0; i--) {
199                     v <<= 1;
200                     v  += !!(scanline[i*bytes_per_line + (x>>3)] & m);
201                 }
202                 ptr[x] = v;
203             }
204             ptr += stride;
205         }
206     }
207
208     if (nplanes == 1 && bits_per_pixel == 8) {
209         pcx_palette(&buf, (uint32_t *) p->data[1], 256);
210     } else if (bits_per_pixel < 8) {
211         const uint8_t *palette = bufstart+16;
212         pcx_palette(&palette, (uint32_t *) p->data[1], 16);
213     }
214
215     *got_frame = 1;
216
217     ret = buf - bufstart;
218 end:
219     av_free(scanline);
220     return ret;
221 }
222
223 AVCodec ff_pcx_decoder = {
224     .name           = "pcx",
225     .type           = AVMEDIA_TYPE_VIDEO,
226     .id             = AV_CODEC_ID_PCX,
227     .decode         = pcx_decode_frame,
228     .capabilities   = CODEC_CAP_DR1,
229     .long_name      = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
230 };