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