]> git.sesse.net Git - ffmpeg/blob - libavcodec/pcx.c
Merge commit '97bf7c03b1338a867da52c159a2afecbdedcfa88'
[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 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/imgutils.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "get_bits.h"
29
30 typedef struct PCXContext {
31     AVFrame picture;
32 } PCXContext;
33
34 static av_cold int pcx_init(AVCodecContext *avctx)
35 {
36     PCXContext *s = avctx->priv_data;
37
38     avcodec_get_frame_defaults(&s->picture);
39     avctx->coded_frame= &s->picture;
40
41     return 0;
42 }
43
44 static void pcx_rle_decode(GetByteContext *gb, uint8_t *dst,
45                            unsigned int bytes_per_scanline, int compressed)
46 {
47     unsigned int i = 0;
48     unsigned char run, value;
49
50     if (compressed) {
51         while (i<bytes_per_scanline) {
52             run = 1;
53             value = bytestream2_get_byte(gb);
54             if (value >= 0xc0) {
55                 run = value & 0x3f;
56                 value = bytestream2_get_byte(gb);
57             }
58             while (i<bytes_per_scanline && run--)
59                 dst[i++] = value;
60         }
61     } else {
62         bytestream2_get_buffer(gb, dst, bytes_per_scanline);
63     }
64 }
65
66 static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen)
67 {
68     int i;
69
70     pallen = FFMIN(pallen, bytestream2_get_bytes_left(gb) / 3);
71     for (i=0; i<pallen; i++)
72         *dst++ = 0xFF000000 | bytestream2_get_be24u(gb);
73     if (pallen < 256)
74         memset(dst, 0, (256 - pallen) * sizeof(*dst));
75 }
76
77 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
78                             AVPacket *avpkt)
79 {
80     PCXContext * const s = avctx->priv_data;
81     AVFrame *picture = data;
82     AVFrame * const p = &s->picture;
83     GetByteContext gb;
84     int compressed, xmin, ymin, xmax, ymax, ret;
85     unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
86                  bytes_per_scanline;
87     uint8_t *ptr, *scanline;
88
89     if (avpkt->size < 128)
90         return AVERROR_INVALIDDATA;
91
92     bytestream2_init(&gb, avpkt->data, avpkt->size);
93
94     if (bytestream2_get_byteu(&gb) != 0x0a || bytestream2_get_byteu(&gb) > 5) {
95         av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
96         return AVERROR_INVALIDDATA;
97     }
98
99     compressed = bytestream2_get_byteu(&gb);
100     bits_per_pixel = bytestream2_get_byteu(&gb);
101     xmin = bytestream2_get_le16u(&gb);
102     ymin = bytestream2_get_le16u(&gb);
103     xmax = bytestream2_get_le16u(&gb);
104     ymax = bytestream2_get_le16u(&gb);
105     avctx->sample_aspect_ratio.num = bytestream2_get_le16u(&gb);
106     avctx->sample_aspect_ratio.den = bytestream2_get_le16u(&gb);
107
108     if (xmax < xmin || ymax < ymin) {
109         av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
110         return AVERROR_INVALIDDATA;
111     }
112
113     w = xmax - xmin + 1;
114     h = ymax - ymin + 1;
115
116     bytestream2_skipu(&gb, 49);
117     nplanes            = bytestream2_get_byteu(&gb);
118     bytes_per_line     = bytestream2_get_le16u(&gb);
119     bytes_per_scanline = nplanes * bytes_per_line;
120
121     if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
122         av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
123         return AVERROR_INVALIDDATA;
124     }
125
126     switch ((nplanes<<8) + bits_per_pixel) {
127         case 0x0308:
128             avctx->pix_fmt = AV_PIX_FMT_RGB24;
129             break;
130         case 0x0108:
131         case 0x0104:
132         case 0x0102:
133         case 0x0101:
134         case 0x0401:
135         case 0x0301:
136         case 0x0201:
137             avctx->pix_fmt = AV_PIX_FMT_PAL8;
138             break;
139         default:
140             av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
141             return AVERROR_INVALIDDATA;
142     }
143
144     bytestream2_skipu(&gb, 60);
145
146     if (p->data[0])
147         avctx->release_buffer(avctx, p);
148
149     if (av_image_check_size(w, h, 0, avctx))
150         return AVERROR_INVALIDDATA;
151     if (w != avctx->width || h != avctx->height)
152         avcodec_set_dimensions(avctx, w, h);
153     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
154         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
155         return ret;
156     }
157
158     p->pict_type = AV_PICTURE_TYPE_I;
159
160     ptr    = p->data[0];
161     stride = p->linesize[0];
162
163     scanline = av_malloc(bytes_per_scanline);
164     if (!scanline)
165         return AVERROR(ENOMEM);
166
167     if (nplanes == 3 && bits_per_pixel == 8) {
168         for (y=0; y<h; y++) {
169             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
170
171             for (x=0; x<w; x++) {
172                 ptr[3*x  ] = scanline[x                    ];
173                 ptr[3*x+1] = scanline[x+ bytes_per_line    ];
174                 ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
175             }
176
177             ptr += stride;
178         }
179
180     } else if (nplanes == 1 && bits_per_pixel == 8) {
181         int palstart = avpkt->size - 769;
182
183         for (y=0; y<h; y++, ptr+=stride) {
184             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
185             memcpy(ptr, scanline, w);
186         }
187
188         if (bytestream2_tell(&gb) != palstart) {
189             av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
190             bytestream2_seek(&gb, palstart, SEEK_SET);
191         }
192         if (bytestream2_get_byte(&gb) != 12) {
193             av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
194             ret = AVERROR_INVALIDDATA;
195             goto end;
196         }
197
198     } else if (nplanes == 1) {   /* all packed formats, max. 16 colors */
199         GetBitContext s;
200
201         for (y=0; y<h; y++) {
202             init_get_bits(&s, scanline, bytes_per_scanline<<3);
203
204             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
205
206             for (x=0; x<w; x++)
207                 ptr[x] = get_bits(&s, bits_per_pixel);
208             ptr += stride;
209         }
210
211     } else {    /* planar, 4, 8 or 16 colors */
212         int i;
213
214         for (y=0; y<h; y++) {
215             pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
216
217             for (x=0; x<w; x++) {
218                 int m = 0x80 >> (x&7), v = 0;
219                 for (i=nplanes - 1; i>=0; i--) {
220                     v <<= 1;
221                     v  += !!(scanline[i*bytes_per_line + (x>>3)] & m);
222                 }
223                 ptr[x] = v;
224             }
225             ptr += stride;
226         }
227     }
228
229     ret = bytestream2_tell(&gb);
230     if (nplanes == 1 && bits_per_pixel == 8) {
231         pcx_palette(&gb, (uint32_t *) p->data[1], 256);
232         ret += 256 * 3;
233     } else if (bits_per_pixel * nplanes == 1) {
234         AV_WN32A(p->data[1]  , 0xFF000000);
235         AV_WN32A(p->data[1]+4, 0xFFFFFFFF);
236     } else if (bits_per_pixel < 8) {
237         bytestream2_seek(&gb, 16, SEEK_SET);
238         pcx_palette(&gb, (uint32_t *) p->data[1], 16);
239     }
240
241     *picture = s->picture;
242     *data_size = sizeof(AVFrame);
243
244 end:
245     av_free(scanline);
246     return ret;
247 }
248
249 static av_cold int pcx_end(AVCodecContext *avctx)
250 {
251     PCXContext *s = avctx->priv_data;
252
253     if(s->picture.data[0])
254         avctx->release_buffer(avctx, &s->picture);
255
256     return 0;
257 }
258
259 AVCodec ff_pcx_decoder = {
260     .name           = "pcx",
261     .type           = AVMEDIA_TYPE_VIDEO,
262     .id             = AV_CODEC_ID_PCX,
263     .priv_data_size = sizeof(PCXContext),
264     .init           = pcx_init,
265     .close          = pcx_end,
266     .decode         = pcx_decode_frame,
267     .capabilities   = CODEC_CAP_DR1,
268     .long_name      = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
269 };