/src/ffmpeg/libavcodec/pcx.c
Line | Count | Source |
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 FFmpeg. |
9 | | * |
10 | | * FFmpeg 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 | | * FFmpeg 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 FFmpeg; 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/mem.h" |
26 | | #include "avcodec.h" |
27 | | #include "bytestream.h" |
28 | | #include "codec_internal.h" |
29 | | #include "decode.h" |
30 | | #include "get_bits.h" |
31 | | |
32 | 142k | #define PCX_HEADER_SIZE 128 |
33 | | |
34 | | static int pcx_rle_decode(GetByteContext *gb, |
35 | | uint8_t *dst, |
36 | | unsigned int bytes_per_scanline, |
37 | | int compressed) |
38 | 614k | { |
39 | 614k | unsigned int i = 0; |
40 | 614k | unsigned char run, value; |
41 | | |
42 | 614k | if (bytestream2_get_bytes_left(gb) < 1) |
43 | 2.70k | return AVERROR_INVALIDDATA; |
44 | | |
45 | 612k | if (compressed) { |
46 | 2.43M | while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)>0) { |
47 | 1.94M | run = 1; |
48 | 1.94M | value = bytestream2_get_byte(gb); |
49 | 1.94M | if (value >= 0xc0 && bytestream2_get_bytes_left(gb)>0) { |
50 | 160k | run = value & 0x3f; |
51 | 160k | value = bytestream2_get_byte(gb); |
52 | 160k | } |
53 | 8.23M | while (i < bytes_per_scanline && run--) |
54 | 6.28M | dst[i++] = value; |
55 | 1.94M | } |
56 | 490k | } else { |
57 | 121k | bytestream2_get_buffer(gb, dst, bytes_per_scanline); |
58 | 121k | } |
59 | 612k | return 0; |
60 | 614k | } |
61 | | |
62 | | static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen) |
63 | 1.74k | { |
64 | 1.74k | int i; |
65 | | |
66 | 1.74k | pallen = FFMIN(pallen, bytestream2_get_bytes_left(gb) / 3); |
67 | 85.5k | for (i = 0; i < pallen; i++) |
68 | 83.8k | *dst++ = 0xFF000000 | bytestream2_get_be24u(gb); |
69 | 1.74k | if (pallen < 256) |
70 | 1.51k | memset(dst, 0, (256 - pallen) * sizeof(*dst)); |
71 | 1.74k | } |
72 | | |
73 | | static int pcx_decode_frame(AVCodecContext *avctx, AVFrame *p, |
74 | | int *got_frame, AVPacket *avpkt) |
75 | 142k | { |
76 | 142k | GetByteContext gb; |
77 | 142k | int compressed, xmin, ymin, xmax, ymax; |
78 | 142k | int ret; |
79 | 142k | unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, y, x, |
80 | 142k | bytes_per_scanline; |
81 | 142k | uint8_t *ptr, *scanline; |
82 | 142k | ptrdiff_t stride; |
83 | | |
84 | 142k | if (avpkt->size < PCX_HEADER_SIZE) { |
85 | 107k | av_log(avctx, AV_LOG_ERROR, "Packet too small\n"); |
86 | 107k | return AVERROR_INVALIDDATA; |
87 | 107k | } |
88 | | |
89 | 35.1k | bytestream2_init(&gb, avpkt->data, avpkt->size); |
90 | | |
91 | 35.1k | if (bytestream2_get_byteu(&gb) != 0x0a || bytestream2_get_byteu(&gb) > 5) { |
92 | 894 | av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n"); |
93 | 894 | return AVERROR_INVALIDDATA; |
94 | 894 | } |
95 | | |
96 | 34.2k | compressed = bytestream2_get_byteu(&gb); |
97 | 34.2k | bits_per_pixel = bytestream2_get_byteu(&gb); |
98 | 34.2k | xmin = bytestream2_get_le16u(&gb); |
99 | 34.2k | ymin = bytestream2_get_le16u(&gb); |
100 | 34.2k | xmax = bytestream2_get_le16u(&gb); |
101 | 34.2k | ymax = bytestream2_get_le16u(&gb); |
102 | 34.2k | avctx->sample_aspect_ratio.num = bytestream2_get_le16u(&gb); |
103 | 34.2k | avctx->sample_aspect_ratio.den = bytestream2_get_le16u(&gb); |
104 | | |
105 | 34.2k | if (xmax < xmin || ymax < ymin) { |
106 | 846 | av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n"); |
107 | 846 | return AVERROR_INVALIDDATA; |
108 | 846 | } |
109 | | |
110 | 33.3k | w = xmax - xmin + 1; |
111 | 33.3k | h = ymax - ymin + 1; |
112 | | |
113 | 33.3k | bytestream2_skipu(&gb, 49); |
114 | 33.3k | nplanes = bytestream2_get_byteu(&gb); |
115 | 33.3k | bytes_per_line = bytestream2_get_le16u(&gb); |
116 | 33.3k | bytes_per_scanline = nplanes * bytes_per_line; |
117 | | |
118 | 33.3k | if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 || |
119 | 33.1k | (!compressed && bytes_per_scanline > bytestream2_get_bytes_left(&gb) / h)) { |
120 | 468 | av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n"); |
121 | 468 | return AVERROR_INVALIDDATA; |
122 | 468 | } |
123 | | |
124 | 32.9k | switch ((nplanes << 8) + bits_per_pixel) { |
125 | 26.7k | case 0x0308: |
126 | 26.7k | avctx->pix_fmt = AV_PIX_FMT_RGB24; |
127 | 26.7k | break; |
128 | 2.24k | case 0x0108: |
129 | 2.67k | case 0x0104: |
130 | 3.27k | case 0x0102: |
131 | 3.84k | case 0x0101: |
132 | 4.78k | case 0x0401: |
133 | 5.42k | case 0x0301: |
134 | 5.70k | case 0x0201: |
135 | 5.70k | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
136 | 5.70k | break; |
137 | 491 | default: |
138 | 491 | av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n"); |
139 | 491 | return AVERROR_INVALIDDATA; |
140 | 32.9k | } |
141 | | |
142 | 32.4k | bytestream2_skipu(&gb, 60); |
143 | | |
144 | 32.4k | if ((ret = ff_set_dimensions(avctx, w, h)) < 0) |
145 | 431 | return ret; |
146 | | |
147 | 32.0k | if ((ret = ff_get_buffer(avctx, p, 0)) < 0) |
148 | 230 | return ret; |
149 | | |
150 | 31.7k | p->pict_type = AV_PICTURE_TYPE_I; |
151 | | |
152 | 31.7k | ptr = p->data[0]; |
153 | 31.7k | stride = p->linesize[0]; |
154 | | |
155 | 31.7k | scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE); |
156 | 31.7k | if (!scanline) |
157 | 0 | return AVERROR(ENOMEM); |
158 | | |
159 | 31.7k | if (nplanes == 3 && bits_per_pixel == 8) { |
160 | 52.3k | for (y = 0; y < h; y++) { |
161 | 27.0k | ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed); |
162 | 27.0k | if (ret < 0) |
163 | 1.27k | goto end; |
164 | | |
165 | 127k | for (x = 0; x < w; x++) { |
166 | 101k | ptr[3 * x] = scanline[x]; |
167 | 101k | ptr[3 * x + 1] = scanline[x + bytes_per_line]; |
168 | 101k | ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)]; |
169 | 101k | } |
170 | | |
171 | 25.8k | ptr += stride; |
172 | 25.8k | } |
173 | 26.5k | } else if (nplanes == 1 && bits_per_pixel == 8) { |
174 | 2.18k | int palstart = avpkt->size - 769; |
175 | | |
176 | 2.18k | if (avpkt->size < 769) { |
177 | 1.04k | av_log(avctx, AV_LOG_ERROR, "File is too short\n"); |
178 | 1.04k | ret = avctx->err_recognition & AV_EF_EXPLODE ? |
179 | 1.04k | AVERROR_INVALIDDATA : avpkt->size; |
180 | 1.04k | goto end; |
181 | 1.04k | } |
182 | | |
183 | 154k | for (y = 0; y < h; y++, ptr += stride) { |
184 | 154k | ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed); |
185 | 154k | if (ret < 0) |
186 | 207 | goto end; |
187 | 153k | memcpy(ptr, scanline, w); |
188 | 153k | } |
189 | | |
190 | 930 | if (bytestream2_tell(&gb) != palstart) { |
191 | 735 | av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n"); |
192 | 735 | bytestream2_seek(&gb, palstart, SEEK_SET); |
193 | 735 | } |
194 | 930 | if (bytestream2_get_byte(&gb) != 12) { |
195 | 697 | av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n"); |
196 | 697 | ret = avctx->err_recognition & AV_EF_EXPLODE ? |
197 | 697 | AVERROR_INVALIDDATA : avpkt->size; |
198 | 697 | goto end; |
199 | 697 | } |
200 | 3.01k | } else if (nplanes == 1) { /* all packed formats, max. 16 colors */ |
201 | 1.40k | GetBitContext s; |
202 | | |
203 | 366k | for (y = 0; y < h; y++) { |
204 | 365k | init_get_bits8(&s, scanline, bytes_per_scanline); |
205 | | |
206 | 365k | ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed); |
207 | 365k | if (ret < 0) |
208 | 529 | goto end; |
209 | | |
210 | 6.12M | for (x = 0; x < w; x++) |
211 | 5.75M | ptr[x] = get_bits(&s, bits_per_pixel); |
212 | 364k | ptr += stride; |
213 | 364k | } |
214 | 1.61k | } else { /* planar, 4, 8 or 16 colors */ |
215 | 1.61k | int i; |
216 | | |
217 | 69.2k | for (y = 0; y < h; y++) { |
218 | 68.3k | ret = pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed); |
219 | 68.3k | if (ret < 0) |
220 | 701 | goto end; |
221 | | |
222 | 1.78M | for (x = 0; x < w; x++) { |
223 | 1.71M | int m = 0x80 >> (x & 7), v = 0; |
224 | 5.49M | for (i = nplanes - 1; i >= 0; i--) { |
225 | 3.77M | v <<= 1; |
226 | 3.77M | v += !!(scanline[i * bytes_per_line + (x >> 3)] & m); |
227 | 3.77M | } |
228 | 1.71M | ptr[x] = v; |
229 | 1.71M | } |
230 | 67.6k | ptr += stride; |
231 | 67.6k | } |
232 | 1.61k | } |
233 | | |
234 | 27.3k | ret = bytestream2_tell(&gb); |
235 | 27.3k | if (nplanes == 1 && bits_per_pixel == 8) { |
236 | 233 | pcx_palette(&gb, (uint32_t *)p->data[1], 256); |
237 | 233 | ret += 256 * 3; |
238 | 27.0k | } else if (bits_per_pixel * nplanes == 1) { |
239 | 277 | AV_WN32A(p->data[1] , 0xFF000000); |
240 | 277 | AV_WN32A(p->data[1]+4, 0xFFFFFFFF); |
241 | 26.8k | } else if (bits_per_pixel < 8) { |
242 | 1.51k | bytestream2_seek(&gb, 16, SEEK_SET); |
243 | 1.51k | pcx_palette(&gb, (uint32_t *)p->data[1], 16); |
244 | 1.51k | } |
245 | | |
246 | 27.3k | *got_frame = 1; |
247 | | |
248 | 31.7k | end: |
249 | 31.7k | av_free(scanline); |
250 | 31.7k | return ret; |
251 | 27.3k | } |
252 | | |
253 | | const FFCodec ff_pcx_decoder = { |
254 | | .p.name = "pcx", |
255 | | CODEC_LONG_NAME("PC Paintbrush PCX image"), |
256 | | .p.type = AVMEDIA_TYPE_VIDEO, |
257 | | .p.id = AV_CODEC_ID_PCX, |
258 | | FF_CODEC_DECODE_CB(pcx_decode_frame), |
259 | | .p.capabilities = AV_CODEC_CAP_DR1, |
260 | | }; |