1 /*
2 * IFF ACBM/DEEP/ILBM/PBM 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 ACBM/DEEP/ILBM/PBM bitmap decoder
26 */
27
33
34 // TODO: masking bits
41
48 uint32_t *
mask_buf;
///< temporary buffer for palette indices
51 unsigned bpp;
///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
52 unsigned ham;
///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
53 unsigned flags;
///< 1 for EHB, 0 is no extra half darkening
54 unsigned transparency;
///< TODO: transparency color index in palette
55 unsigned masking;
///< TODO: masking method used
56 int init;
// 1 if buffer and palette data already initialized, 0 otherwise
57 int16_t tvdc[16];
///< TVDC lookup table
59
60 #define LUT8_PART(plane, v) \
61 AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
62 AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
63 AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
64 AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
65 AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
66 AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
67 AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
68 AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
69 AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
70 AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
71 AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
72 AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
73 AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
74 AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
75 AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
76 AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
77
78 #define LUT8(plane) { \
79 LUT8_PART(plane, 0x0000000), \
80 LUT8_PART(plane, 0x1000000), \
81 LUT8_PART(plane, 0x0010000), \
82 LUT8_PART(plane, 0x1010000), \
83 LUT8_PART(plane, 0x0000100), \
84 LUT8_PART(plane, 0x1000100), \
85 LUT8_PART(plane, 0x0010100), \
86 LUT8_PART(plane, 0x1010100), \
87 LUT8_PART(plane, 0x0000001), \
88 LUT8_PART(plane, 0x1000001), \
89 LUT8_PART(plane, 0x0010001), \
90 LUT8_PART(plane, 0x1010001), \
91 LUT8_PART(plane, 0x0000101), \
92 LUT8_PART(plane, 0x1000101), \
93 LUT8_PART(plane, 0x0010101), \
94 LUT8_PART(plane, 0x1010101), \
95 }
96
97 // 8 planes * 8-bit mask
101 };
102
103 #define LUT32(plane) { \
104 0, 0, 0, 0, \
105 0, 0, 0, 1 << plane, \
106 0, 0, 1 << plane, 0, \
107 0, 0, 1 << plane, 1 << plane, \
108 0, 1 << plane, 0, 0, \
109 0, 1 << plane, 0, 1 << plane, \
110 0, 1 << plane, 1 << plane, 0, \
111 0, 1 << plane, 1 << plane, 1 << plane, \
112 1 << plane, 0, 0, 0, \
113 1 << plane, 0, 0, 1 << plane, \
114 1 << plane, 0, 1 << plane, 0, \
115 1 << plane, 0, 1 << plane, 1 << plane, \
116 1 << plane, 1 << plane, 0, 0, \
117 1 << plane, 1 << plane, 0, 1 << plane, \
118 1 << plane, 1 << plane, 1 << plane, 0, \
119 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
120 }
121
122 // 32 planes * 4-bit mask * 4 lookup tables each
132 };
133
134 // Gray to RGB, required for palette table of grayscale images with bpp < 8
136 return x << 16 | x << 8 | x;
137 }
138
139 /**
140 * Convert CMAP buffer (stored in extradata) to lavc palette format
141 */
143 {
148
152 }
153
155 // If extradata is smaller than actually needed, fill the remaining with black.
156 count =
FFMIN(palette_size / 3, count);
157 if (count) {
158 for (i=0; i <
count; i++) {
159 pal[i] = 0xFF000000 |
AV_RB24(palette + i*3);
160 }
161 if (s->
flags && count >= 32) {
// EHB
162 for (i = 0; i < 32; i++)
163 pal[i + 32] = 0xFF000000 | (
AV_RB24(palette + i*3) & 0xFEFEFE) >> 1;
164 count =
FFMAX(count, 64);
165 }
166 } else { // Create gray-scale color palette for bps < 8
168
169 for (i=0; i <
count; i++) {
171 }
172 }
175 for (i = 0; i <
count; i++)
176 pal[i] &= 0xFFFFFF;
180 return 0;
181 }
182
183 /**
184 * Extracts the IFF extra context and updates internal
185 * decoder structures.
186 *
187 * @param avctx the AVCodecContext where to extract extra context to
188 * @param avpkt the AVPacket to extract extra context from or NULL to use avctx
189 * @return 0 in case of success, a negative error code otherwise
190 */
194 unsigned buf_size;
196 int i, palette_size;
197
201 }
203
204 if (avpkt) {
205 int image_size;
210 buf_size = bytestream_get_be16(&buf);
211 if (buf_size <= 1 || image_size <= 1) {
213 "Invalid image size received: %u -> image data offset: %d\n",
214 buf_size, image_size);
216 }
217 } else {
219 buf_size = bytestream_get_be16(&buf);
220 if (buf_size <= 1 || palette_size < 0) {
222 "Invalid palette size received: %u -> palette data offset: %d\n",
223 buf_size, palette_size);
225 }
226 }
227
228 if (buf_size >= 41) {
230 s->
bpp = bytestream_get_byte(&buf);
231 s->
ham = bytestream_get_byte(&buf);
232 s->
flags = bytestream_get_byte(&buf);
234 s->
masking = bytestream_get_byte(&buf);
235 for (i = 0; i < 16; i++)
236 s->
tvdc[i] = bytestream_get_be16(&buf);
237
239 if (s->
bpp >= 8 && !s->
ham) {
250 }
255 }
256 }
261 }
262 if (!s->
bpp || s->
bpp > 32) {
265 }
else if (s->
ham >= 8) {
268 }
269
272
275 int ham_count;
277
281
282 ham_count = 8 * (1 << s->
ham);
287 }
288
289 if (count) { // HAM with color palette attached
290 // prefill with black and palette and set HAM take direct value mask to zero
291 memset(s->
ham_palbuf, 0, (1 << s->
ham) * 2 * sizeof (uint32_t));
292 for (i=0; i <
count; i++) {
294 }
296 } else { // HAM with grayscale color palette
298 for (i=0; i <
count; i++) {
299 s->
ham_palbuf[i*2] = 0xFF000000;
// take direct color value from palette
301 }
302 }
303 for (i=0; i <
count; i++) {
304 uint32_t tmp = i << (8 - s->
ham);
305 tmp |= tmp >> s->
ham;
306 s->
ham_palbuf[(i+
count)*2] = 0xFF00FFFF;
// just modify blue color component
307 s->
ham_palbuf[(i+count*2)*2] = 0xFFFFFF00;
// just modify red color component
308 s->
ham_palbuf[(i+count*3)*2] = 0xFFFF00FF;
// just modify green color component
310 s->
ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
311 s->
ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
312 }
314 for (i = 0; i < ham_count; i++)
316 }
317 }
318 }
319
320 return 0;
321 }
322
324 {
326 int err;
327
329 int palette_size;
330
333 else
334 palette_size = 0;
347 } else {
350 }
351 }
352 } else {
354 }
355
357 return err;
362
367
369 return err;
370
371 return 0;
372 }
373
374 /**
375 * Decode interleaved plane buffer up to 8bpp
376 * @param dst Destination buffer
377 * @param buf Source buffer
378 * @param buf_size
379 * @param plane plane number to decode as
380 */
382 {
384 if (plane >= 8) {
386 return;
387 }
388 do {
389 uint64_t
v =
AV_RN64A(dst) | lut[*buf++];
391 dst += 8;
392 } while (--buf_size);
393 }
394
395 /**
396 * Decode interleaved plane buffer up to 24bpp
397 * @param dst Destination buffer
398 * @param buf Source buffer
399 * @param buf_size
400 * @param plane plane number to decode as
401 */
403 {
405 do {
406 unsigned mask = (*buf >> 2) & ~3;
407 dst[0] |= lut[mask++];
408 dst[1] |= lut[mask++];
409 dst[2] |= lut[mask++];
411 mask = (*buf++ << 2) & 0x3F;
412 dst[4] |= lut[mask++];
413 dst[5] |= lut[mask++];
414 dst[6] |= lut[mask++];
416 dst += 8;
417 } while (--buf_size);
418 }
419
420 #define DECODE_HAM_PLANE32(x) \
421 first = buf[x] << 1; \
422 second = buf[(x)+1] << 1; \
423 delta &= pal[first++]; \
424 delta |= pal[first]; \
425 dst[x] = delta; \
426 delta &= pal[second++]; \
427 delta |= pal[second]; \
428 dst[(x)+1] = delta
429
430 /**
431 * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
432 *
433 * @param dst the destination 24bpp buffer
434 * @param buf the source 8bpp chunky buffer
435 * @param pal the HAM decode table
436 * @param buf_size the plane size in bytes
437 */
439 const uint32_t *const pal, unsigned buf_size)
440 {
441 uint32_t
delta = pal[1];
/* first palette entry */
442 do {
443 uint32_t first, second;
448 buf += 8;
449 dst += 8;
450 } while (--buf_size);
451 }
452
454 const uint32_t *
const pal,
unsigned width)
455 {
456 do {
457 *dst++ = pal[*buf++];
458 } while (--width);
459 }
460
461 /**
462 * Decode one complete byterun1 encoded line.
463 *
464 * @param dst the destination buffer where to store decompressed bitstream
465 * @param dst_size the destination plane size in bytes
466 * @param buf the source byterun1 compressed bitstream
467 * @param buf_end the EOF of source byterun1 compressed bitstream
468 * @return number of consumed bytes in byterun1 compressed bitstream
469 */
473 unsigned x;
474 for (x = 0; x < dst_size && buf < buf_end;) {
476 const int8_t
value = *buf++;
477 if (value >= 0) {
478 length = value + 1;
479 memcpy(dst + x, buf,
FFMIN3(length, dst_size - x, buf_end - buf));
481 } else if (value > -128) {
482 length = -value + 1;
483 memset(dst + x, *buf++,
FFMIN(length, dst_size - x));
484 } else { // noop
485 continue;
486 }
488 }
489 return buf - buf_start;
490 }
491
492 #define DECODE_RGBX_COMMON(type) \
493 if (!length) { \
494 length = bytestream2_get_byte(gb); \
495 if (!length) { \
496 length = bytestream2_get_be16(gb); \
497 if (!length) \
498 return; \
499 } \
500 } \
501 for (i = 0; i < length; i++) { \
502 *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
503 x += 1; \
504 if (x >= width) { \
505 y += 1; \
506 if (y >= height) \
507 return; \
508 x = 0; \
509 } \
510 }
511
512 /**
513 * Decode RGB8 buffer
514 * @param[out] dst Destination buffer
515 * @param width Width of destination buffer (pixels)
516 * @param height Height of destination buffer (pixels)
517 * @param linesize Line size of destination buffer (bytes)
518 */
520 {
523 uint32_t
pixel = 0xFF000000 | bytestream2_get_be24(gb);
524 length = bytestream2_get_byte(gb) & 0x7F;
526 }
527 }
528
529 /**
530 * Decode RGBN buffer
531 * @param[out] dst Destination buffer
532 * @param width Width of destination buffer (pixels)
533 * @param height Height of destination buffer (pixels)
534 * @param linesize Line size of destination buffer (bytes)
535 */
537 {
540 uint32_t
pixel = bytestream2_get_be16u(gb);
541 length = pixel & 0x7;
542 pixel >>= 4;
544 }
545 }
546
547 /**
548 * Decode DEEP RLE 32-bit buffer
549 * @param[out] dst Destination buffer
550 * @param[in] src Source buffer
551 * @param src_size Source buffer size (bytes)
552 * @param width Width of destination buffer (pixels)
553 * @param height Height of destination buffer (pixels)
554 * @param linesize Line size of destination buffer (bytes)
555 */
557 {
558 const uint8_t *src_end = src + src_size;
560 while (src + 5 <= src_end) {
561 int opcode;
562 opcode = *(int8_t *)src++;
563 if (opcode >= 0) {
564 int size = opcode + 1;
565 for (i = 0; i <
size; i++) {
567 memcpy(dst +
y*linesize + x * 4, src, length * 4);
568 src += length * 4;
571 if (x >= width) {
572 x = 0;
575 return;
576 }
577 }
578 } else {
579 int size = -opcode + 1;
581 for (i = 0; i <
size; i++) {
582 *(uint32_t *)(dst +
y*linesize + x * 4) =
pixel;
583 x += 1;
584 if (x >= width) {
585 x = 0;
588 return;
589 }
590 }
591 src += 4;
592 }
593 }
594 }
595
596 /**
597 * Decode DEEP TVDC 32-bit buffer
598 * @param[out] dst Destination buffer
599 * @param[in] src Source buffer
600 * @param src_size Source buffer size (bytes)
601 * @param width Width of destination buffer (pixels)
602 * @param height Height of destination buffer (pixels)
603 * @param linesize Line size of destination buffer (bytes)
604 * @param[int] tvdc TVDC lookup table
605 */
607 {
608 int x = 0,
y = 0, plane = 0;
610 int i, j;
611
612 for (i = 0; i < src_size * 2;) {
613 #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
615 i++;
616 if (d) {
617 pixel += d;
618 dst[
y * linesize + x*4 + plane] =
pixel;
619 x++;
620 } else {
621 if (i >= src_size * 2)
622 return;
624 i++;
625 d =
FFMIN(d, width - x);
626 for (j = 0; j < d; j++) {
627 dst[
y * linesize + x*4 + plane] =
pixel;
628 x++;
629 }
630 }
631 if (x >= width) {
632 plane++;
633 if (plane >= 4) {
636 return;
637 plane = 0;
638 }
639 x = 0;
640 pixel = 0;
641 i = (i + 1) & ~1;
642 }
643 }
644 }
645
647 {
651 }
652
654 void *
data,
int *got_frame,
656 {
660 const uint8_t *buf_end = buf+buf_size;
663
676 }
678
680 case 0:
684 for (plane = 0; plane < s->
bpp; plane++) {
685 for(y = 0; y < avctx->
height && buf < buf_end; y++ ) {
689 }
690 }
691 }
else if (s->
ham) {
// HAM to AV_PIX_FMT_BGR32
693 for(y = 0; y < avctx->
height; y++) {
696 for (plane = 0; plane < s->
bpp; plane++) {
698 if (start >= buf_end)
699 break;
701 }
703 }
704 } else
709 int x;
710 for(y = 0; y < avctx->
height && buf < buf_end; y++ ) {
712 memcpy(row, buf,
FFMIN(raw_width, buf_end - buf));
713 buf += raw_width;
715 for(x = 0; x < avctx->
width; x++)
716 row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
717 }
718 }
719 }
else if (avctx->
codec_tag ==
MKTAG(
'I',
'L',
'B',
'M')) {
// interleaved
721 for(y = 0; y < avctx->
height; y++ ) {
723 memset(row, 0, avctx->
width);
724 for (plane = 0; plane < s->
bpp && buf < buf_end; plane++) {
727 }
728 }
729 }
else if (s->
ham) {
// HAM to AV_PIX_FMT_BGR32
730 for (y = 0; y < avctx->
height; y++) {
733 for (plane = 0; plane < s->
bpp && buf < buf_end; plane++) {
736 }
738 }
739 } else { // AV_PIX_FMT_BGR32
740 for(y = 0; y < avctx->
height; y++ ) {
742 memset(row, 0, avctx->
width << 2);
743 for (plane = 0; plane < s->
bpp && buf < buf_end; plane++) {
746 }
747 }
748 }
749 }
else if (avctx->
codec_tag ==
MKTAG(
'P',
'B',
'M',
' ')) {
// IFF-PBM
751 for(y = 0; y < avctx->
height && buf_end >
buf; y++ ) {
753 memcpy(row, buf,
FFMIN(avctx->
width, buf_end - buf));
754 buf += avctx->
width + (avctx->
width % 2);
// padding if odd
755 }
756 }
else if (s->
ham) {
// IFF-PBM: HAM to AV_PIX_FMT_BGR32
757 for (y = 0; y < avctx->
height && buf_end >
buf; y++) {
760 buf += avctx->
width + (avctx->
width & 1);
// padding if odd
762 }
763 } else
765 }
766 break;
767 case 1:
770 for(y = 0; y < avctx->
height ; y++ ) {
772 memset(row, 0, avctx->
width);
773 for (plane = 0; plane < s->
bpp; plane++) {
776 }
777 }
779 for (y = 0; y < avctx->
height ; y++ ) {
782 for (plane = 0; plane < s->
bpp; plane++) {
785 }
787 }
788 }
else if (s->
ham) {
// HAM to AV_PIX_FMT_BGR32
789 for (y = 0; y < avctx->
height ; y++) {
792 for (plane = 0; plane < s->
bpp; plane++) {
795 }
797 }
798 } else { //AV_PIX_FMT_BGR32
799 for(y = 0; y < avctx->
height ; y++ ) {
801 memset(row, 0, avctx->
width << 2);
802 for (plane = 0; plane < s->
bpp; plane++) {
805 }
806 }
807 }
808 }
else if (avctx->
codec_tag ==
MKTAG(
'P',
'B',
'M',
' ')) {
// IFF-PBM
810 for(y = 0; y < avctx->
height ; y++ ) {
813 }
814 }
else if (s->
ham) {
// IFF-PBM: HAM to AV_PIX_FMT_BGR32
815 for (y = 0; y < avctx->
height ; y++) {
819 }
820 } else
822 }
else if (avctx->
codec_tag ==
MKTAG(
'D',
'E',
'E',
'P')) {
// IFF-DEEP
826 else
828 }
829 break;
830 case 4:
836 else
838 break;
839 case 5:
844 else
846 } else
848 break;
849 default:
851 }
852
855
856 *got_frame = 1;
857
858 return buf_size;
859 }
860
862 {
868 return 0;
869 }
870
871 #if CONFIG_IFF_ILBM_DECODER
872 AVCodec ff_iff_ilbm_decoder = {
882 };
883 #endif
884 #if CONFIG_IFF_BYTERUN1_DECODER
885 AVCodec ff_iff_byterun1_decoder = {
895 };
896 #endif