Browse code

IFF PBM/ILBM bitmap decoder

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

Peter Ross authored on 2010/02/03 18:56:16
Showing 6 changed files
... ...
@@ -53,6 +53,7 @@ version <next>:
53 53
 - RTP depacketization of H.263
54 54
 - Bink demuxer and Bink Audio decoder
55 55
 - enable symbol versioning by default for linkers that support it
56
+- IFF PBM/ILBM bitmap decoder
56 57
 
57 58
 
58 59
 
... ...
@@ -387,6 +387,10 @@ following image formats are supported:
387 387
     @tab Used in Quake II.
388 388
 @item id RoQ video           @tab  X  @tab  X
389 389
     @tab Used in Quake III, Jedi Knight 2, other computer games.
390
+@item IFF ILBM               @tab     @tab  X
391
+    @tab IFF interlaved bitmap
392
+@item IFF ByteRun1           @tab     @tab  X
393
+    @tab IFF run length encoded bitmap
390 394
 @item Intel H.263            @tab     @tab  X
391 395
 @item Intel Indeo 2          @tab     @tab  X
392 396
 @item Intel Indeo 3          @tab     @tab  X
... ...
@@ -144,6 +144,8 @@ OBJS-$(CONFIG_H264_VAAPI_HWACCEL)      += vaapi_h264.o
144 144
 OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o
145 145
 OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o
146 146
 OBJS-$(CONFIG_IDCIN_DECODER)           += idcinvideo.o
147
+OBJS-$(CONFIG_IFF_BYTERUN1_DECODER)    += iff.o
148
+OBJS-$(CONFIG_IFF_ILBM_DECODER)        += iff.o
147 149
 OBJS-$(CONFIG_IMC_DECODER)             += imc.o
148 150
 OBJS-$(CONFIG_INDEO2_DECODER)          += indeo2.o
149 151
 OBJS-$(CONFIG_INDEO3_DECODER)          += indeo3.o
... ...
@@ -114,6 +114,8 @@ void avcodec_register_all(void)
114 114
     REGISTER_DECODER (H264_VDPAU, h264_vdpau);
115 115
     REGISTER_ENCDEC  (HUFFYUV, huffyuv);
116 116
     REGISTER_DECODER (IDCIN, idcin);
117
+    REGISTER_DECODER (IFF_BYTERUN1, iff_byterun1);
118
+    REGISTER_DECODER (IFF_ILBM, iff_ilbm);
117 119
     REGISTER_DECODER (INDEO2, indeo2);
118 120
     REGISTER_DECODER (INDEO3, indeo3);
119 121
     REGISTER_DECODER (INTERPLAY_VIDEO, interplay_video);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 51
33
+#define LIBAVCODEC_VERSION_MINOR 52
34 34
 #define LIBAVCODEC_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -204,6 +204,8 @@ enum CodecID {
204 204
     CODEC_ID_R210,
205 205
     CODEC_ID_ANM,
206 206
     CODEC_ID_BINKVIDEO,
207
+    CODEC_ID_IFF_ILBM,
208
+    CODEC_ID_IFF_BYTERUN1,
207 209
 
208 210
     /* various PCM "codecs" */
209 211
     CODEC_ID_PCM_S16LE= 0x10000,
210 212
new file mode 100644
... ...
@@ -0,0 +1,223 @@
0
+/*
1
+ * IFF PBM/ILBM bitmap 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/iff.c
23
+ * IFF PBM/ILBM bitmap decoder
24
+ */
25
+
26
+#include "bytestream.h"
27
+#include "avcodec.h"
28
+
29
+/**
30
+ * Convert CMAP buffer (stored in extradata) to lavc palette format
31
+ */
32
+int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
33
+{
34
+    int count, i;
35
+
36
+    if (avctx->bits_per_coded_sample > 8) {
37
+        av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
38
+        return AVERROR_INVALIDDATA;
39
+    }
40
+
41
+    count = 1 << avctx->bits_per_coded_sample;
42
+    if (avctx->extradata_size < count * 3) {
43
+        av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
44
+        return AVERROR_INVALIDDATA;
45
+    }
46
+    for (i=0; i < count; i++) {
47
+        pal[i] = AV_RB24( avctx->extradata + i*3 );
48
+    }
49
+    return 0;
50
+}
51
+
52
+static av_cold int decode_init(AVCodecContext *avctx)
53
+{
54
+    AVFrame *frame = avctx->priv_data;
55
+
56
+    avctx->pix_fmt = PIX_FMT_PAL8;
57
+    frame->reference = 1;
58
+
59
+    if (avctx->get_buffer(avctx, frame) < 0) {
60
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
61
+        return AVERROR_UNKNOWN;
62
+    }
63
+    return ff_cmap_read_palette(avctx, (uint32_t*)frame->data[1]);
64
+}
65
+
66
+/**
67
+ * Interleaved memcpy
68
+ */
69
+static void imemcpy(uint8_t *dst, const uint8_t const *buf, int x, int bps, int plane, int length)
70
+{
71
+    int i, b;
72
+    for(i = 0; i < length; i++) {
73
+        int value = buf[i];
74
+        for (b = 0; b < bps; b++) {
75
+            if (value & (1<<b))
76
+                dst[ (x+i)*bps + 7 - b] |= 1<<plane;
77
+       }
78
+    }
79
+}
80
+
81
+/**
82
+ * Interleaved memset
83
+ */
84
+static void imemset(uint8_t *dst, int value, int x, int bps, int plane, int length)
85
+{
86
+    int i, b;
87
+    for(i = 0; i < length; i++) {
88
+        for (b = 0; b < bps; b++) {
89
+            if (value & (1<<b))
90
+                dst[ (x+i)*bps + 7 - b] |= 1<<plane;
91
+       }
92
+    }
93
+}
94
+
95
+static int decode_frame_ilbm(AVCodecContext *avctx,
96
+                            void *data, int *data_size,
97
+                            AVPacket *avpkt)
98
+{
99
+    AVFrame *frame = avctx->priv_data;
100
+    const uint8_t *buf = avpkt->data;
101
+    int buf_size = avpkt->size;
102
+    int planewidth = avctx->width / avctx->bits_per_coded_sample;
103
+    int y, plane;
104
+
105
+    if (avctx->reget_buffer(avctx, frame) < 0){
106
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
107
+        return -1;
108
+    }
109
+
110
+    if (buf_size < avctx->width * avctx->height) {
111
+        av_log(avctx, AV_LOG_ERROR, "buffer underflow\n");
112
+        return -1;
113
+    }
114
+
115
+    for(y = 0; y < avctx->height; y++ ) {
116
+        uint8_t *row = &frame->data[0][ y*frame->linesize[0] ];
117
+        memset(row, 0, avctx->width);
118
+        for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
119
+            imemcpy(row, buf, 0, avctx->bits_per_coded_sample, plane, planewidth);
120
+            buf += planewidth;
121
+        }
122
+    }
123
+
124
+    *data_size = sizeof(AVFrame);
125
+    *(AVFrame*)data = *frame;
126
+    return buf_size;
127
+}
128
+
129
+static int decode_frame_byterun1(AVCodecContext *avctx,
130
+                            void *data, int *data_size,
131
+                            AVPacket *avpkt)
132
+{
133
+    AVFrame *frame = avctx->priv_data;
134
+    const uint8_t *buf = avpkt->data;
135
+    int buf_size = avpkt->size;
136
+    const uint8_t *buf_end = buf+buf_size;
137
+    int planewidth = avctx->width / avctx->bits_per_coded_sample;
138
+    int y, plane, x;
139
+
140
+    if (avctx->reget_buffer(avctx, frame) < 0){
141
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
142
+        return -1;
143
+    }
144
+
145
+    for(y = 0; y < avctx->height ; y++ ) {
146
+        uint8_t *row = &frame->data[0][ y*frame->linesize[0] ];
147
+        if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
148
+            memset(row, 0, avctx->width);
149
+            for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
150
+                for(x = 0; x < planewidth && buf < buf_end; ) {
151
+                    char value = *buf++;
152
+                    int length;
153
+                    if (value >= 0) {
154
+                        length = value + 1;
155
+                        imemcpy(row, buf, x, avctx->bits_per_coded_sample, plane, FFMIN3(length, buf_end - buf, planewidth - x));
156
+                        buf += length;
157
+                    } else if (value > -128) {
158
+                        length = -value + 1;
159
+                        imemset(row, *buf++, x, avctx->bits_per_coded_sample, plane, FFMIN(length, planewidth - x));
160
+                    } else { //noop
161
+                        continue;
162
+                    }
163
+                    x += length;
164
+                }
165
+            }
166
+        } else {
167
+            for(x = 0; x < avctx->width && buf < buf_end; ) {
168
+                char value = *buf++;
169
+                int length;
170
+                if (value >= 0) {
171
+                    length = value + 1;
172
+                    memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
173
+                    buf += length;
174
+                } else if (value > -128) {
175
+                    length = -value + 1;
176
+                    memset(row + x, *buf++, FFMIN(length, avctx->width - x));
177
+                } else { //noop
178
+                    continue;
179
+                }
180
+                x += length;
181
+            }
182
+        }
183
+    }
184
+
185
+    *data_size = sizeof(AVFrame);
186
+    *(AVFrame*)data = *frame;
187
+    return buf_size;
188
+}
189
+
190
+static av_cold int decode_end(AVCodecContext *avctx)
191
+{
192
+    AVFrame *frame = avctx->priv_data;
193
+    if (frame->data[0])
194
+        avctx->release_buffer(avctx, frame);
195
+    return 0;
196
+}
197
+
198
+AVCodec iff_ilbm_decoder = {
199
+    "iff_ilbm",
200
+    CODEC_TYPE_VIDEO,
201
+    CODEC_ID_IFF_ILBM,
202
+    sizeof(AVFrame),
203
+    decode_init,
204
+    NULL,
205
+    decode_end,
206
+    decode_frame_ilbm,
207
+    CODEC_CAP_DR1,
208
+    .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
209
+};
210
+
211
+AVCodec iff_byterun1_decoder = {
212
+    "iff_byterun1",
213
+    CODEC_TYPE_VIDEO,
214
+    CODEC_ID_IFF_BYTERUN1,
215
+    sizeof(AVFrame),
216
+    decode_init,
217
+    NULL,
218
+    decode_end,
219
+    decode_frame_byterun1,
220
+    CODEC_CAP_DR1,
221
+    .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
222
+};