IFF: decode last 8 pixels per line
[libav.git] / libavcodec / iff.c
1 /*
2 * IFF PBM/ILBM bitmap decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * IFF PBM/ILBM bitmap decoder
26 */
27
28 #include "bytestream.h"
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "iff.h"
32
33 typedef struct {
34 AVFrame frame;
35 int planesize;
36 uint8_t * planebuf;
37 } IffContext;
38
39 #define LUT8_PART(plane, v) \
40 AV_LE2ME64C(UINT64_C(0x0000000)<<32 | v) << plane, \
41 AV_LE2ME64C(UINT64_C(0x1000000)<<32 | v) << plane, \
42 AV_LE2ME64C(UINT64_C(0x0010000)<<32 | v) << plane, \
43 AV_LE2ME64C(UINT64_C(0x1010000)<<32 | v) << plane, \
44 AV_LE2ME64C(UINT64_C(0x0000100)<<32 | v) << plane, \
45 AV_LE2ME64C(UINT64_C(0x1000100)<<32 | v) << plane, \
46 AV_LE2ME64C(UINT64_C(0x0010100)<<32 | v) << plane, \
47 AV_LE2ME64C(UINT64_C(0x1010100)<<32 | v) << plane, \
48 AV_LE2ME64C(UINT64_C(0x0000001)<<32 | v) << plane, \
49 AV_LE2ME64C(UINT64_C(0x1000001)<<32 | v) << plane, \
50 AV_LE2ME64C(UINT64_C(0x0010001)<<32 | v) << plane, \
51 AV_LE2ME64C(UINT64_C(0x1010001)<<32 | v) << plane, \
52 AV_LE2ME64C(UINT64_C(0x0000101)<<32 | v) << plane, \
53 AV_LE2ME64C(UINT64_C(0x1000101)<<32 | v) << plane, \
54 AV_LE2ME64C(UINT64_C(0x0010101)<<32 | v) << plane, \
55 AV_LE2ME64C(UINT64_C(0x1010101)<<32 | v) << plane
56
57 #define LUT8(plane) { \
58 LUT8_PART(plane, 0x0000000), \
59 LUT8_PART(plane, 0x1000000), \
60 LUT8_PART(plane, 0x0010000), \
61 LUT8_PART(plane, 0x1010000), \
62 LUT8_PART(plane, 0x0000100), \
63 LUT8_PART(plane, 0x1000100), \
64 LUT8_PART(plane, 0x0010100), \
65 LUT8_PART(plane, 0x1010100), \
66 LUT8_PART(plane, 0x0000001), \
67 LUT8_PART(plane, 0x1000001), \
68 LUT8_PART(plane, 0x0010001), \
69 LUT8_PART(plane, 0x1010001), \
70 LUT8_PART(plane, 0x0000101), \
71 LUT8_PART(plane, 0x1000101), \
72 LUT8_PART(plane, 0x0010101), \
73 LUT8_PART(plane, 0x1010101), \
74 }
75
76 // 8 planes * 8-bit mask
77 static const uint64_t plane8_lut[8][256] = {
78 LUT8(0), LUT8(1), LUT8(2), LUT8(3),
79 LUT8(4), LUT8(5), LUT8(6), LUT8(7),
80 };
81
82 /**
83 * Convert CMAP buffer (stored in extradata) to lavc palette format
84 */
85 int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
86 {
87 int count, i;
88
89 if (avctx->bits_per_coded_sample > 8) {
90 av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
91 return AVERROR_INVALIDDATA;
92 }
93
94 count = 1 << avctx->bits_per_coded_sample;
95 if (avctx->extradata_size < count * 3) {
96 av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
97 return AVERROR_INVALIDDATA;
98 }
99 for (i=0; i < count; i++) {
100 pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
101 }
102 return 0;
103 }
104
105 static av_cold int decode_init(AVCodecContext *avctx)
106 {
107 IffContext *s = avctx->priv_data;
108 int err;
109
110 if (avctx->bits_per_coded_sample <= 8) {
111 avctx->pix_fmt = PIX_FMT_PAL8;
112 } else if (avctx->bits_per_coded_sample <= 32) {
113 avctx->pix_fmt = PIX_FMT_BGR32;
114 } else {
115 return AVERROR_INVALIDDATA;
116 }
117
118 s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
119 s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
120 if (!s->planebuf)
121 return AVERROR(ENOMEM);
122
123 s->frame.reference = 1;
124 if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
125 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
126 return err;
127 }
128
129 return avctx->bits_per_coded_sample <= 8 ?
130 ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
131 }
132
133 /**
134 * Decode interleaved plane buffer up to 8bpp
135 * @param dst Destination buffer
136 * @param buf Source buffer
137 * @param buf_size
138 * @param bps bits_per_coded_sample (must be <= 8)
139 * @param plane plane number to decode as
140 */
141 static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int bps, int plane)
142 {
143 const uint64_t *lut = plane8_lut[plane];
144 while (buf_size--) {
145 uint64_t v = AV_RN64A(dst) | lut[*buf++];
146 AV_WN64A(dst, v);
147 dst += 8;
148 }
149 }
150
151 /**
152 * Decode interleaved plane buffer up to 24bpp
153 * @param dst Destination buffer
154 * @param buf Source buffer
155 * @param buf_size
156 * @param bps bits_per_coded_sample
157 * @param plane plane number to decode as
158 */
159 static void decodeplane32(uint32_t *dst, const uint8_t *const buf, int buf_size, int bps, int plane)
160 {
161 GetBitContext gb;
162 int i;
163 const int b = buf_size * 8;
164 init_get_bits(&gb, buf, buf_size * 8);
165 for(i = 0; i < b; i++) {
166 dst[i] |= get_bits1(&gb) << plane;
167 }
168 }
169
170 static int decode_frame_ilbm(AVCodecContext *avctx,
171 void *data, int *data_size,
172 AVPacket *avpkt)
173 {
174 IffContext *s = avctx->priv_data;
175 const uint8_t *buf = avpkt->data;
176 int buf_size = avpkt->size;
177 const uint8_t *buf_end = buf+buf_size;
178 int y, plane;
179
180 if (avctx->reget_buffer(avctx, &s->frame) < 0){
181 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
182 return -1;
183 }
184
185 if (avctx->pix_fmt == PIX_FMT_PAL8) {
186 for(y = 0; y < avctx->height; y++ ) {
187 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
188 memset(row, 0, avctx->width);
189 for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
190 decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
191 buf += s->planesize;
192 }
193 }
194 } else { // PIX_FMT_BGR32
195 for(y = 0; y < avctx->height; y++ ) {
196 uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
197 memset(row, 0, avctx->width << 2);
198 for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
199 decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
200 buf += s->planesize;
201 }
202 }
203 }
204
205 *data_size = sizeof(AVFrame);
206 *(AVFrame*)data = s->frame;
207 return buf_size;
208 }
209
210 static int decode_frame_byterun1(AVCodecContext *avctx,
211 void *data, int *data_size,
212 AVPacket *avpkt)
213 {
214 IffContext *s = avctx->priv_data;
215 const uint8_t *buf = avpkt->data;
216 int buf_size = avpkt->size;
217 const uint8_t *buf_end = buf+buf_size;
218 int y, plane, x;
219
220 if (avctx->reget_buffer(avctx, &s->frame) < 0){
221 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
222 return -1;
223 }
224
225 if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
226 if (avctx->pix_fmt == PIX_FMT_PAL8) {
227 for(y = 0; y < avctx->height ; y++ ) {
228 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
229 memset(row, 0, avctx->width);
230 for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
231 for(x = 0; x < s->planesize && buf < buf_end; ) {
232 int8_t value = *buf++;
233 unsigned length;
234 if (value >= 0) {
235 length = value + 1;
236 memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
237 buf += length;
238 } else if (value > -128) {
239 length = -value + 1;
240 memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
241 } else { //noop
242 continue;
243 }
244 x += length;
245 }
246 decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
247 }
248 }
249 } else { //PIX_FMT_BGR32
250 for(y = 0; y < avctx->height ; y++ ) {
251 uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
252 memset(row, 0, avctx->width << 2);
253 for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
254 for(x = 0; x < s->planesize && buf < buf_end; ) {
255 int8_t value = *buf++;
256 unsigned length;
257 if (value >= 0) {
258 length = value + 1;
259 memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
260 buf += length;
261 } else if (value > -128) {
262 length = -value + 1;
263 memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
264 } else { // noop
265 continue;
266 }
267 x += length;
268 }
269 decodeplane32((uint32_t *) row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
270 }
271 }
272 }
273 } else {
274 for(y = 0; y < avctx->height ; y++ ) {
275 uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
276 for(x = 0; x < avctx->width && buf < buf_end; ) {
277 int8_t value = *buf++;
278 unsigned length;
279 if (value >= 0) {
280 length = value + 1;
281 memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
282 buf += length;
283 } else if (value > -128) {
284 length = -value + 1;
285 memset(row + x, *buf++, FFMIN(length, avctx->width - x));
286 } else { //noop
287 continue;
288 }
289 x += length;
290 }
291 }
292 }
293
294 *data_size = sizeof(AVFrame);
295 *(AVFrame*)data = s->frame;
296 return buf_size;
297 }
298
299 static av_cold int decode_end(AVCodecContext *avctx)
300 {
301 IffContext *s = avctx->priv_data;
302 if (s->frame.data[0])
303 avctx->release_buffer(avctx, &s->frame);
304 av_freep(&s->planebuf);
305 return 0;
306 }
307
308 AVCodec iff_ilbm_decoder = {
309 "iff_ilbm",
310 AVMEDIA_TYPE_VIDEO,
311 CODEC_ID_IFF_ILBM,
312 sizeof(IffContext),
313 decode_init,
314 NULL,
315 decode_end,
316 decode_frame_ilbm,
317 CODEC_CAP_DR1,
318 .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
319 };
320
321 AVCodec iff_byterun1_decoder = {
322 "iff_byterun1",
323 AVMEDIA_TYPE_VIDEO,
324 CODEC_ID_IFF_BYTERUN1,
325 sizeof(IffContext),
326 decode_init,
327 NULL,
328 decode_end,
329 decode_frame_byterun1,
330 CODEC_CAP_DR1,
331 .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
332 };