Browse code

Pictor/PC Paint decoder

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

Peter Ross authored on 2010/06/08 20:55:06
Showing 6 changed files
... ...
@@ -10,6 +10,7 @@ version <next>:
10 10
 - VP8 de/encoding via libvpx
11 11
 - CODEC_CAP_EXPERIMENTAL added
12 12
 - Demuxer for On2's IVF format
13
+- Pictor/PC Paint decoder
13 14
 
14 15
 
15 16
 
... ...
@@ -281,6 +281,8 @@ following image formats are supported:
281 281
     @tab Portable GrayMap image
282 282
 @item PGMYUV       @tab X @tab X
283 283
     @tab PGM with U and V components in YUV 4:2:0
284
+@item PIC          @tab @tab X
285
+    @tab Pictor/PC Paint
284 286
 @item PNG          @tab X @tab X
285 287
     @tab 2/4 bpp not supported yet
286 288
 @item PPM          @tab X @tab X
... ...
@@ -264,6 +264,7 @@ OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o pnm.o
264 264
 OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmdec.o pnm.o
265 265
 OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o pnm.o
266 266
 OBJS-$(CONFIG_PGSSUB_DECODER)          += pgssubdec.o
267
+OBJS-$(CONFIG_PICTOR_DECODER)          += pictordec.o cga_data.o
267 268
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o
268 269
 OBJS-$(CONFIG_PNG_ENCODER)             += png.o pngenc.o
269 270
 OBJS-$(CONFIG_PPM_DECODER)             += pnmdec.o pnm.o
... ...
@@ -152,6 +152,7 @@ void avcodec_register_all(void)
152 152
     REGISTER_ENCDEC  (PCX, pcx);
153 153
     REGISTER_ENCDEC  (PGM, pgm);
154 154
     REGISTER_ENCDEC  (PGMYUV, pgmyuv);
155
+    REGISTER_DECODER (PICTOR, pictor);
155 156
     REGISTER_ENCDEC  (PNG, png);
156 157
     REGISTER_ENCDEC  (PPM, ppm);
157 158
     REGISTER_DECODER (PTX, ptx);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 74
33
+#define LIBAVCODEC_VERSION_MINOR 75
34 34
 #define LIBAVCODEC_VERSION_MICRO  1
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -211,6 +211,7 @@ enum CodecID {
211 211
     CODEC_ID_KGV1,
212 212
     CODEC_ID_YOP,
213 213
     CODEC_ID_VP8,
214
+    CODEC_ID_PICTOR,
214 215
 
215 216
     /* various PCM "codecs" */
216 217
     CODEC_ID_PCM_S16LE= 0x10000,
217 218
new file mode 100644
... ...
@@ -0,0 +1,250 @@
0
+/*
1
+ * Pictor/PC Paint decoder
2
+ * Copyright (c) 2010 Peter Ross <pross@xvid.org>
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
+ * @file libavcodec/picdec.c
23
+ * Pictor/PC Paint decoder
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "bytestream.h"
28
+#include "cga_data.h"
29
+
30
+typedef struct PicContext {
31
+    AVFrame frame;
32
+    int width, height;
33
+    int nb_planes;
34
+} PicContext;
35
+
36
+static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
37
+{
38
+    while (run > 0) {
39
+        uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
40
+        if (*x + run >= s->width) {
41
+            int n = s->width - *x;
42
+            memset(d + *x, value, n);
43
+            run -= n;
44
+            *x = 0;
45
+            *y -= 1;
46
+            if (*y < 0)
47
+                break;
48
+        } else {
49
+            memset(d + *x, value, run);
50
+            *x += run;
51
+            break;
52
+        }
53
+    }
54
+}
55
+
56
+static void picmemset(PicContext *s, int value, int run, int *x, int *y, int *plane, int bits_per_plane)
57
+{
58
+    uint8_t *d;
59
+    int shift = *plane * bits_per_plane;
60
+    int mask  = ((1 << bits_per_plane) - 1) << shift;
61
+    value   <<= shift;
62
+
63
+    while (run > 0) {
64
+        int j;
65
+        for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
66
+            d = s->frame.data[0] + *y * s->frame.linesize[0];
67
+            d[*x] |= (value >> j) & mask;
68
+            *x += 1;
69
+            if (*x == s->width) {
70
+                *y -= 1;
71
+                *x = 0;
72
+                if (*y < 0) {
73
+                   *y = s->height - 1;
74
+                   *plane += 1;
75
+                   value <<= bits_per_plane;
76
+                   mask  <<= bits_per_plane;
77
+                   if (*plane >= s->nb_planes)
78
+                       break;
79
+                }
80
+            }
81
+        }
82
+        run--;
83
+    }
84
+}
85
+
86
+static const uint8_t cga_mode45_index[6][4] = {
87
+    [0] = { 0, 3,  5,   7 }, // mode4, palette#1, low intensity
88
+    [1] = { 0, 2,  4,   6 }, // mode4, palette#2, low intensity
89
+    [2] = { 0, 3,  4,   7 }, // mode5, low intensity
90
+    [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
91
+    [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
92
+    [5] = { 0, 11, 12, 15 }, // mode5, high intensity
93
+};
94
+
95
+static int decode_frame(AVCodecContext *avctx,
96
+                        void *data, int *data_size,
97
+                        AVPacket *avpkt)
98
+{
99
+    PicContext *s = avctx->priv_data;
100
+    int buf_size = avpkt->size;
101
+    const uint8_t *buf = avpkt->data;
102
+    const uint8_t *buf_end = avpkt->data + buf_size;
103
+    uint32_t *palette;
104
+    int bits_per_plane, bpp, etype, esize, npal;
105
+    int i, x, y, plane;
106
+
107
+    if (buf_size < 11)
108
+        return AVERROR_INVALIDDATA;
109
+
110
+    if (bytestream_get_le16(&buf) != 0x1234)
111
+        return AVERROR_INVALIDDATA;
112
+    s->width  = bytestream_get_le16(&buf);
113
+    s->height = bytestream_get_le16(&buf);
114
+    buf += 4;
115
+    bits_per_plane    = *buf & 0xF;
116
+    s->nb_planes      = (*buf++ >> 4) + 1;
117
+    bpp               = s->nb_planes ? bits_per_plane*s->nb_planes : bits_per_plane;
118
+    if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
119
+        av_log_ask_for_sample(s, "unsupported bit depth\n");
120
+        return AVERROR_INVALIDDATA;
121
+    }
122
+
123
+    if (*buf == 0xFF) {
124
+        buf += 2;
125
+        etype  = bytestream_get_le16(&buf);
126
+        esize  = bytestream_get_le16(&buf);
127
+        if (buf_end - buf < esize)
128
+            return AVERROR_INVALIDDATA;
129
+    } else {
130
+        etype = -1;
131
+        esize = 0;
132
+    }
133
+
134
+    avctx->pix_fmt = PIX_FMT_PAL8;
135
+
136
+    if (s->width != avctx->width && s->height != avctx->height) {
137
+        if (avcodec_check_dimensions(avctx, s->width, s->height) < 0)
138
+            return -1;
139
+        avcodec_set_dimensions(avctx, s->width, s->height);
140
+        if (s->frame.data[0])
141
+            avctx->release_buffer(avctx, &s->frame);
142
+    }
143
+
144
+    if (avctx->get_buffer(avctx, &s->frame) < 0){
145
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
146
+        return -1;
147
+    }
148
+    memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
149
+    s->frame.pict_type           = FF_I_TYPE;
150
+    s->frame.palette_has_changed = 1;
151
+
152
+    palette = (uint32_t*)s->frame.data[1];
153
+    if (etype == 1 && esize > 1 && *buf < 6) {
154
+        int idx = *buf;
155
+        npal = 4;
156
+        for (i = 0; i < npal; i++)
157
+            palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
158
+    } else if (etype == 2) {
159
+        npal = FFMIN(esize, 16);
160
+        for (i = 0; i < npal; i++)
161
+            palette[i] = ff_cga_palette[ FFMIN(buf[i], 16)];
162
+    } else if (etype == 3) {
163
+        npal = FFMIN(esize, 16);
164
+        for (i = 0; i < npal; i++)
165
+            palette[i] = ff_ega_palette[ FFMIN(buf[i], 63)];
166
+    } else if (etype == 4 || etype == 5) {
167
+        npal = FFMIN(esize / 3, 256);
168
+        for (i = 0; i < npal; i++)
169
+            palette[i] = AV_RB24(buf + i*3) << 2;
170
+    } else {
171
+        if (bpp == 1) {
172
+            npal = 2;
173
+            palette[0] = 0x000000;
174
+            palette[1] = 0xFFFFFF;
175
+        } else if (bpp == 2) {
176
+            npal = 4;
177
+            for (i = 0; i < npal; i++)
178
+                palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
179
+        } else {
180
+            npal = 16;
181
+            memcpy(palette, ff_cga_palette, npal * 4);
182
+        }
183
+    }
184
+    // fill remaining palette entries
185
+    memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
186
+    buf += esize;
187
+
188
+
189
+    x = 0;
190
+    y = s->height - 1;
191
+    plane = 0;
192
+    if (bytestream_get_le16(&buf)) {
193
+        while (buf_end - buf >= 6) {
194
+            const uint8_t *buf_pend = buf + FFMIN(AV_RL16(buf), buf_end - buf);
195
+            //ignore uncompressed block size reported at buf[2]
196
+            int marker = buf[4];
197
+            buf += 5;
198
+
199
+            while (plane < s->nb_planes && buf_pend - buf >= 1) {
200
+                int run = 1;
201
+                int val = *buf++;
202
+                if (val == marker) {
203
+                    run = *buf++;
204
+                    if (run == 0)
205
+                        run = bytestream_get_le16(&buf);
206
+                    val = *buf++;
207
+                }
208
+                if (buf > buf_end)
209
+                    break;
210
+
211
+                if (bits_per_plane == 8) {
212
+                    picmemset_8bpp(s, val, run, &x, &y);
213
+                    if (y < 0)
214
+                        break;
215
+                } else {
216
+                    picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
217
+                }
218
+            }
219
+        }
220
+    } else {
221
+        av_log_ask_for_sample(s, "uncompressed image\n");
222
+        return buf_size;
223
+    }
224
+
225
+    *data_size = sizeof(AVFrame);
226
+    *(AVFrame*)data = s->frame;
227
+    return buf_size;
228
+}
229
+
230
+static av_cold int decode_end(AVCodecContext *avctx)
231
+{
232
+    PicContext *s = avctx->priv_data;
233
+    if (s->frame.data[0])
234
+        avctx->release_buffer(avctx, &s->frame);
235
+    return 0;
236
+}
237
+
238
+AVCodec pictor_decoder = {
239
+    "pictor",
240
+    CODEC_TYPE_VIDEO,
241
+    CODEC_ID_PICTOR,
242
+    sizeof(PicContext),
243
+    NULL,
244
+    NULL,
245
+    decode_end,
246
+    decode_frame,
247
+    CODEC_CAP_DR1,
248
+    .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
249
+};