]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcx.c
lzf: update pointer p after realloc
[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 #define PCX_HEADER_SIZE 128
32
33 /**
34  * @return advanced src pointer
35  */
36 static void pcx_rle_decode(GetByteContext *gb,
37                            uint8_t *dst,
38                            unsigned int bytes_per_scanline,
39                            int compressed)
40 {
41     unsigned int i = 0;
42     unsigned char run, value;
43
44     if (compressed) {
45         while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)) {
46             run   = 1;
47             value = bytestream2_get_byte(gb);
48             if (value >= 0xc0 && bytestream2_get_bytes_left(gb)) {
49                 run   = value & 0x3f;
50                 value = bytestream2_get_byte(gb);
51             }
52             while (i < bytes_per_scanline && run--)
53                 dst[i++] = value;
54         }
55     } else {
56         bytestream2_get_buffer(gb, dst, bytes_per_scanline);
57     }
58 }
59
60 static void pcx_palette(GetByteContext *gb, uint32_t *dst,
61                         unsigned int pallen)
62 {
63     unsigned int i;
64
65     for (i = 0; i < pallen; i++)
66         *dst++ = bytestream2_get_be24(gb);
67     if (pallen < 256)
68         memset(dst, 0, (256 - pallen) * sizeof(*dst));
69 }
70
71 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
72                             AVPacket *avpkt)
73 {
74     const uint8_t *buf = avpkt->data;
75     int buf_size       = avpkt->size;
76     AVFrame *const p   = data;
77     GetByteContext gb;
78     int compressed, xmin, ymin, xmax, ymax;
79     unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
80                  bytes_per_scanline;
81     uint8_t *ptr;
82     uint8_t *scanline;
83     int ret = -1;
84
85     if (buf_size < PCX_HEADER_SIZE) {
86         av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
87         return AVERROR_INVALIDDATA;
88     }
89
90     if (buf[0] != 0x0a || buf[1] > 5) {
91         av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
92         return AVERROR_INVALIDDATA;
93     }
94
95     compressed = buf[2];
96     xmin       = AV_RL16(buf + 4);
97     ymin       = AV_RL16(buf + 6);
98     xmax       = AV_RL16(buf + 8);
99     ymax       = AV_RL16(buf + 10);
100
101     if (xmax < xmin || ymax < ymin) {
102         av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
103         return AVERROR_INVALIDDATA;
104     }
105
106     w = xmax - xmin + 1;
107     h = ymax - ymin + 1;
108
109     bits_per_pixel     = buf[3];
110     bytes_per_line     = AV_RL16(buf + 66);
111     nplanes            = buf[65];
112     bytes_per_scanline = nplanes * bytes_per_line;
113
114     if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 ||
115         (!compressed && bytes_per_scanline > buf_size / h)) {
116         av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
117         return AVERROR_INVALIDDATA;
118     }
119
120     switch ((nplanes << 8) + bits_per_pixel) {
121     case 0x0308:
122         avctx->pix_fmt = AV_PIX_FMT_RGB24;
123         break;
124     case 0x0108:
125     case 0x0104:
126     case 0x0102:
127     case 0x0101:
128     case 0x0401:
129     case 0x0301:
130     case 0x0201:
131         avctx->pix_fmt = AV_PIX_FMT_PAL8;
132         break;
133     default:
134         av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
135         return AVERROR_INVALIDDATA;
136     }
137
138     bytestream2_init(&gb, buf + PCX_HEADER_SIZE, buf_size - PCX_HEADER_SIZE);
139
140     if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
141         return ret;
142
143     if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
144         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
145         return ret;
146     }
147
148     p->pict_type = AV_PICTURE_TYPE_I;
149
150     ptr    = p->data[0];
151     stride = p->linesize[0];
152
153     scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE);
154     if (!scanline)
155         return AVERROR(ENOMEM);
156
157     if (nplanes == 3 && bits_per_pixel == 8) {
158         for (y = 0; y < h; y++) {
159             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
160
161             for (x = 0; x < w; x++) {
162                 ptr[3 * x]     = scanline[x];
163                 ptr[3 * x + 1] = scanline[x + bytes_per_line];
164                 ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
165             }
166
167             ptr += stride;
168         }
169     } else if (nplanes == 1 && bits_per_pixel == 8) {
170         for (y = 0; y < h; y++, ptr += stride) {
171             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
172             memcpy(ptr, scanline, w);
173         }
174
175         if (bytestream2_get_byte(&gb) != 12) {
176             av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
177             ret = avctx->err_recognition & AV_EF_EXPLODE ?
178                   AVERROR_INVALIDDATA : buf_size;
179             goto end;
180         }
181     } else if (nplanes == 1) {   /* all packed formats, max. 16 colors */
182         GetBitContext s;
183
184         for (y = 0; y < h; y++) {
185             init_get_bits(&s, scanline, bytes_per_scanline << 3);
186
187             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
188
189             for (x = 0; x < w; x++)
190                 ptr[x] = get_bits(&s, bits_per_pixel);
191             ptr += stride;
192         }
193     } else {    /* planar, 4, 8 or 16 colors */
194         int i;
195
196         for (y = 0; y < h; y++) {
197             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
198
199             for (x = 0; x < w; x++) {
200                 int m = 0x80 >> (x & 7), v = 0;
201                 for (i = nplanes - 1; i >= 0; i--) {
202                     v <<= 1;
203                     v  += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
204                 }
205                 ptr[x] = v;
206             }
207             ptr += stride;
208         }
209     }
210
211     if (nplanes == 1 && bits_per_pixel == 8) {
212         if (bytestream2_get_bytes_left(&gb) < 768) {
213             av_log(avctx, AV_LOG_ERROR, "Palette truncated\n");
214             ret = AVERROR_INVALIDDATA;
215             goto end;
216         }
217
218         pcx_palette(&gb, (uint32_t *)p->data[1], 256);
219     } else if (bits_per_pixel < 8) {
220         GetByteContext gb1;
221         bytestream2_init(&gb1, avpkt->data + 16, 48);
222         pcx_palette(&gb1, (uint32_t *)p->data[1], 16);
223     }
224
225     *got_frame = 1;
226
227     ret = bytestream2_tell(&gb);
228 end:
229     av_free(scanline);
230     return ret;
231 }
232
233 AVCodec ff_pcx_decoder = {
234     .name         = "pcx",
235     .long_name    = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
236     .type         = AVMEDIA_TYPE_VIDEO,
237     .id           = AV_CODEC_ID_PCX,
238     .decode       = pcx_decode_frame,
239     .capabilities = AV_CODEC_CAP_DR1,
240 };