Browse code

PCX encoder that handles 1-, 8-, and 24-bpp pixfmts.

Patch by Daniel Verkamp, daniel drv nu

Originally committed as revision 18077 to svn://svn.ffmpeg.org/ffmpeg/trunk

Daniel Verkamp authored on 2009/03/21 02:23:45
Showing 9 changed files
... ...
@@ -5,6 +5,7 @@ version <next>:
5 5
 - deprecated old scaler removed
6 6
 - VQF demuxer
7 7
 - Alpha channel scaler
8
+- PCX encoder
8 9
 
9 10
 
10 11
 
... ...
@@ -224,6 +224,7 @@ LAVF_TESTS = $(addprefix regtest-,              \
224 224
         voc                                     \
225 225
         ogg                                     \
226 226
         pixfmt                                  \
227
+        pcx                                     \
227 228
     )
228 229
 
229 230
 REGFILES = $(addprefix tests/data/,$(addsuffix .$(1),$(2:regtest-%=%)))
... ...
@@ -251,7 +251,7 @@ following image formats are supported:
251 251
     @tab PAM is a PNM extension with alpha support.
252 252
 @item PBM          @tab X @tab X
253 253
     @tab Portable BitMap image
254
-@item PCX          @tab   @tab X
254
+@item PCX          @tab X @tab X
255 255
     @tab PC Paintbrush
256 256
 @item PGM          @tab X @tab X
257 257
     @tab Portable GrayMap image
... ...
@@ -159,6 +159,7 @@ OBJS-$(CONFIG_NUV_DECODER)             += nuv.o rtjpeg.o
159 159
 OBJS-$(CONFIG_PAM_ENCODER)             += pnmenc.o pnm.o
160 160
 OBJS-$(CONFIG_PBM_ENCODER)             += pnmenc.o pnm.o
161 161
 OBJS-$(CONFIG_PCX_DECODER)             += pcx.o
162
+OBJS-$(CONFIG_PCX_ENCODER)             += pcxenc.o
162 163
 OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o pnm.o
163 164
 OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o pnm.o
164 165
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o
... ...
@@ -127,7 +127,7 @@ void avcodec_register_all(void)
127 127
     REGISTER_DECODER (NUV, nuv);
128 128
     REGISTER_ENCODER (PAM, pam);
129 129
     REGISTER_ENCODER (PBM, pbm);
130
-    REGISTER_DECODER (PCX, pcx);
130
+    REGISTER_ENCDEC  (PCX, pcx);
131 131
     REGISTER_ENCODER (PGM, pgm);
132 132
     REGISTER_ENCODER (PGMYUV, pgmyuv);
133 133
     REGISTER_ENCDEC  (PNG, png);
134 134
new file mode 100644
... ...
@@ -0,0 +1,206 @@
0
+/*
1
+ * PC Paintbrush PCX (.pcx) image encoder
2
+ * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * PCX image encoder
23
+ * @file libavcodec/pcxenc.c
24
+ * @author Daniel Verkamp
25
+ * @sa http://www.qzx.com/pc-gpe/pcx.txt
26
+ */
27
+
28
+#include "avcodec.h"
29
+#include "bytestream.h"
30
+
31
+typedef struct PCXContext {
32
+    AVFrame picture;
33
+} PCXContext;
34
+
35
+static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF };
36
+
37
+static av_cold int pcx_encode_init(AVCodecContext *avctx)
38
+{
39
+    PCXContext *s = avctx->priv_data;
40
+
41
+    avcodec_get_frame_defaults(&s->picture);
42
+    avctx->coded_frame = &s->picture;
43
+
44
+    return 0;
45
+}
46
+
47
+/**
48
+ * PCX run-length encoder
49
+ * @param dst output buffer
50
+ * @param dst_size size of output buffer
51
+ * @param src input buffer
52
+ * @param src_plane_size size of one plane of input buffer in bytes
53
+ * @param nplanes number of planes in input buffer
54
+ * @return number of bytes written to dst or -1 on error
55
+ * @bug will not work for nplanes != 1 && bpp != 8
56
+ */
57
+static int pcx_rle_encode(      uint8_t *dst, int dst_size,
58
+                          const uint8_t *src, int src_plane_size, int nplanes)
59
+{
60
+    int p;
61
+    const uint8_t *dst_start = dst;
62
+
63
+    // check worst-case upper bound on dst_size
64
+    if (dst_size < 2LL * src_plane_size * nplanes || src_plane_size <= 0)
65
+        return -1;
66
+
67
+    for (p = 0; p < nplanes; p++) {
68
+        int count = 1;
69
+        const uint8_t *src_plane = src + p;
70
+        const uint8_t *src_plane_end = src_plane + src_plane_size * nplanes;
71
+        uint8_t prev = *src_plane;
72
+        src_plane += nplanes;
73
+
74
+        for (; ; src_plane += nplanes) {
75
+            if (src_plane < src_plane_end && *src_plane == prev && count < 0x3F) {
76
+                // current byte is same as prev
77
+                ++count;
78
+            } else {
79
+                // output prev * count
80
+                if (count != 1 || prev >= 0xC0)
81
+                    *dst++ = 0xC0 | count;
82
+                *dst++ = prev;
83
+
84
+                if (src_plane == src_plane_end)
85
+                    break;
86
+
87
+                // start new run
88
+                count = 1;
89
+                prev = *src_plane;
90
+            }
91
+        }
92
+    }
93
+
94
+    return dst - dst_start;
95
+}
96
+
97
+static int pcx_encode_frame(AVCodecContext *avctx,
98
+                            unsigned char *buf, int buf_size, void *data)
99
+{
100
+    PCXContext *s = avctx->priv_data;
101
+    AVFrame *const pict = &s->picture;
102
+    const uint8_t *buf_start = buf;
103
+    const uint8_t *buf_end   = buf + buf_size;
104
+
105
+    int bpp, nplanes, i, y, line_bytes, written;
106
+    const uint32_t *pal = NULL;
107
+    const uint8_t *src;
108
+
109
+    *pict = *(AVFrame *)data;
110
+    pict->pict_type = FF_I_TYPE;
111
+    pict->key_frame = 1;
112
+
113
+    if (avctx->width > 65535 || avctx->height > 65535) {
114
+        av_log(avctx, AV_LOG_ERROR, "image dimensions do not fit in 16 bits\n");
115
+        return -1;
116
+    }
117
+
118
+    switch (avctx->pix_fmt) {
119
+    case PIX_FMT_RGB24:
120
+        bpp = 8;
121
+        nplanes = 3;
122
+        break;
123
+    case PIX_FMT_RGB8:
124
+    case PIX_FMT_BGR8:
125
+    case PIX_FMT_RGB4_BYTE:
126
+    case PIX_FMT_BGR4_BYTE:
127
+    case PIX_FMT_GRAY8:
128
+    case PIX_FMT_PAL8:
129
+        bpp = 8;
130
+        nplanes = 1;
131
+        pal = (uint32_t *)pict->data[1];
132
+        break;
133
+    case PIX_FMT_MONOBLACK:
134
+        bpp = 1;
135
+        nplanes = 1;
136
+        pal = monoblack_pal;
137
+        break;
138
+    default:
139
+        av_log(avctx, AV_LOG_ERROR, "unsupported pixfmt\n");
140
+        return -1;
141
+    }
142
+
143
+    line_bytes = (avctx->width * bpp + 7) >> 3;
144
+    line_bytes = (line_bytes + 1) & ~1;
145
+
146
+    bytestream_put_byte(&buf, 10);                  // manufacturer
147
+    bytestream_put_byte(&buf, 5);                   // version
148
+    bytestream_put_byte(&buf, 1);                   // encoding
149
+    bytestream_put_byte(&buf, bpp);                 // bits per pixel per plane
150
+    bytestream_put_le16(&buf, 0);                   // x min
151
+    bytestream_put_le16(&buf, 0);                   // y min
152
+    bytestream_put_le16(&buf, avctx->width - 1);    // x max
153
+    bytestream_put_le16(&buf, avctx->height - 1);   // y max
154
+    bytestream_put_le16(&buf, 0);                   // horizontal DPI
155
+    bytestream_put_le16(&buf, 0);                   // vertical DPI
156
+    for (i = 0; i < 16; i++)
157
+        bytestream_put_be24(&buf, pal ? pal[i] : 0);// palette (<= 16 color only)
158
+    bytestream_put_byte(&buf, 0);                   // reserved
159
+    bytestream_put_byte(&buf, nplanes);             // number of planes
160
+    bytestream_put_le16(&buf, line_bytes);          // scanline plane size in bytes
161
+
162
+    while (buf - buf_start < 128)
163
+        *buf++= 0;
164
+
165
+    src = pict->data[0];
166
+
167
+    for (y = 0; y < avctx->height; y++) {
168
+        if ((written = pcx_rle_encode(buf, buf_end - buf,
169
+                                      src, line_bytes, nplanes)) < 0) {
170
+            av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
171
+            return -1;
172
+        }
173
+        buf += written;
174
+        src += pict->linesize[0];
175
+    }
176
+
177
+    if (nplanes == 1 && bpp == 8) {
178
+        if (buf_end - buf < 257) {
179
+            av_log(avctx, AV_LOG_ERROR, "buffer too small\n");
180
+            return -1;
181
+        }
182
+        bytestream_put_byte(&buf, 12);
183
+        for (i = 0; i < 256; i++) {
184
+            bytestream_put_be24(&buf, pal[i]);
185
+        }
186
+    }
187
+
188
+    return buf - buf_start;
189
+}
190
+
191
+AVCodec pcx_encoder = {
192
+    "pcx",
193
+    CODEC_TYPE_VIDEO,
194
+    CODEC_ID_PCX,
195
+    sizeof(PCXContext),
196
+    pcx_encode_init,
197
+    pcx_encode_frame,
198
+    NULL,
199
+    .pix_fmts = (enum PixelFormat[]){
200
+        PIX_FMT_RGB24,
201
+        PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
202
+        PIX_FMT_MONOBLACK,
203
+        PIX_FMT_NONE},
204
+    .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
205
+};
... ...
@@ -428,7 +428,7 @@ AVOutputFormat image2_muxer = {
428 428
     "image2",
429 429
     NULL_IF_CONFIG_SMALL("image2 sequence"),
430 430
     "",
431
-    "bmp,jpeg,jpg,ljpg,pam,pbm,pgm,pgmyuv,png,ppm,sgi,tif,tiff,jp2",
431
+    "bmp,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,ppm,sgi,tif,tiff,jp2",
432 432
     sizeof(VideoData),
433 433
     CODEC_ID_NONE,
434 434
     CODEC_ID_MJPEG,
... ...
@@ -138,3 +138,6 @@ ac2c17f1a27d928e8b82f21dbafdd715 *./tests/data/b-libav-yuv440p.yuv
138 138
 304128 ./tests/data/b-libav-yuv440p.yuv
139 139
 10c8507ad38d0ce5e8cd0f1dd49b0d26 *./tests/data/b-libav-yuvj440p.yuv
140 140
 304128 ./tests/data/b-libav-yuvj440p.yuv
141
+574fb8fe0b2fe8cc0b3ded8549c052d4 *./tests/data/b-libav02.pcx
142
+./tests/data/b-libav%02d.pcx CRC=0x6f775c0d
143
+363436 ./tests/data/b-libav02.pcx
... ...
@@ -601,6 +601,10 @@ if [ -n "$do_jpg" ] ; then
601 601
 do_image_formats jpg "-flags +bitexact -dct fastint -idct simple -pix_fmt yuvj420p" "-f image2"
602 602
 fi
603 603
 
604
+if [ -n "$do_pcx" ] ; then
605
+do_image_formats pcx
606
+fi
607
+
604 608
 # audio only
605 609
 
606 610
 if [ -n "$do_wav" ] ; then