You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

898 lines
35KB

  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. * @file
  24. * IFF ACBM/DEEP/ILBM/PBM bitmap decoder
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "bytestream.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. // TODO: masking bits
  32. typedef enum {
  33. MASK_NONE,
  34. MASK_HAS_MASK,
  35. MASK_HAS_TRANSPARENT_COLOR,
  36. MASK_LASSO
  37. } mask_type;
  38. typedef struct {
  39. AVFrame frame;
  40. int planesize;
  41. uint8_t * planebuf;
  42. uint8_t * ham_buf; ///< temporary buffer for planar to chunky conversation
  43. uint32_t *ham_palbuf; ///< HAM decode table
  44. uint32_t *mask_buf; ///< temporary buffer for palette indices
  45. uint32_t *mask_palbuf; ///< masking palette table
  46. unsigned compression; ///< delta compression method used
  47. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  48. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  49. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  50. unsigned transparency; ///< TODO: transparency color index in palette
  51. unsigned masking; ///< TODO: masking method used
  52. int init; // 1 if buffer and palette data already initialized, 0 otherwise
  53. int16_t tvdc[16]; ///< TVDC lookup table
  54. } IffContext;
  55. #define LUT8_PART(plane, v) \
  56. AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
  57. AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
  58. AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
  59. AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
  60. AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
  61. AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
  62. AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
  63. AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
  64. AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
  65. AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
  66. AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
  67. AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
  68. AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
  69. AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
  70. AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
  71. AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
  72. #define LUT8(plane) { \
  73. LUT8_PART(plane, 0x0000000), \
  74. LUT8_PART(plane, 0x1000000), \
  75. LUT8_PART(plane, 0x0010000), \
  76. LUT8_PART(plane, 0x1010000), \
  77. LUT8_PART(plane, 0x0000100), \
  78. LUT8_PART(plane, 0x1000100), \
  79. LUT8_PART(plane, 0x0010100), \
  80. LUT8_PART(plane, 0x1010100), \
  81. LUT8_PART(plane, 0x0000001), \
  82. LUT8_PART(plane, 0x1000001), \
  83. LUT8_PART(plane, 0x0010001), \
  84. LUT8_PART(plane, 0x1010001), \
  85. LUT8_PART(plane, 0x0000101), \
  86. LUT8_PART(plane, 0x1000101), \
  87. LUT8_PART(plane, 0x0010101), \
  88. LUT8_PART(plane, 0x1010101), \
  89. }
  90. // 8 planes * 8-bit mask
  91. static const uint64_t plane8_lut[8][256] = {
  92. LUT8(0), LUT8(1), LUT8(2), LUT8(3),
  93. LUT8(4), LUT8(5), LUT8(6), LUT8(7),
  94. };
  95. #define LUT32(plane) { \
  96. 0, 0, 0, 0, \
  97. 0, 0, 0, 1 << plane, \
  98. 0, 0, 1 << plane, 0, \
  99. 0, 0, 1 << plane, 1 << plane, \
  100. 0, 1 << plane, 0, 0, \
  101. 0, 1 << plane, 0, 1 << plane, \
  102. 0, 1 << plane, 1 << plane, 0, \
  103. 0, 1 << plane, 1 << plane, 1 << plane, \
  104. 1 << plane, 0, 0, 0, \
  105. 1 << plane, 0, 0, 1 << plane, \
  106. 1 << plane, 0, 1 << plane, 0, \
  107. 1 << plane, 0, 1 << plane, 1 << plane, \
  108. 1 << plane, 1 << plane, 0, 0, \
  109. 1 << plane, 1 << plane, 0, 1 << plane, \
  110. 1 << plane, 1 << plane, 1 << plane, 0, \
  111. 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
  112. }
  113. // 32 planes * 4-bit mask * 4 lookup tables each
  114. static const uint32_t plane32_lut[32][16*4] = {
  115. LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
  116. LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
  117. LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
  118. LUT32(12), LUT32(13), LUT32(14), LUT32(15),
  119. LUT32(16), LUT32(17), LUT32(18), LUT32(19),
  120. LUT32(20), LUT32(21), LUT32(22), LUT32(23),
  121. LUT32(24), LUT32(25), LUT32(26), LUT32(27),
  122. LUT32(28), LUT32(29), LUT32(30), LUT32(31),
  123. };
  124. // Gray to RGB, required for palette table of grayscale images with bpp < 8
  125. static av_always_inline uint32_t gray2rgb(const uint32_t x) {
  126. return x << 16 | x << 8 | x;
  127. }
  128. /**
  129. * Convert CMAP buffer (stored in extradata) to lavc palette format
  130. */
  131. static int cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  132. {
  133. IffContext *s = avctx->priv_data;
  134. int count, i;
  135. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  136. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  137. if (avctx->bits_per_coded_sample > 8) {
  138. av_log(avctx, AV_LOG_ERROR, "bits_per_coded_sample > 8 not supported\n");
  139. return AVERROR_INVALIDDATA;
  140. }
  141. count = 1 << avctx->bits_per_coded_sample;
  142. // If extradata is smaller than actually needed, fill the remaining with black.
  143. count = FFMIN(palette_size / 3, count);
  144. if (count) {
  145. for (i=0; i < count; i++) {
  146. pal[i] = 0xFF000000 | AV_RB24(palette + i*3);
  147. }
  148. if (s->flags && count >= 32) { // EHB
  149. for (i = 0; i < 32; i++)
  150. pal[i + 32] = 0xFF000000 | (AV_RB24(palette + i*3) & 0xFEFEFE) >> 1;
  151. count = FFMAX(count, 64);
  152. }
  153. } else { // Create gray-scale color palette for bps < 8
  154. count = 1 << avctx->bits_per_coded_sample;
  155. for (i=0; i < count; i++) {
  156. pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
  157. }
  158. }
  159. if (s->masking == MASK_HAS_MASK) {
  160. memcpy(pal + (1 << avctx->bits_per_coded_sample), pal, count * 4);
  161. for (i = 0; i < count; i++)
  162. pal[i] &= 0xFFFFFF;
  163. } else if (s->masking == MASK_HAS_TRANSPARENT_COLOR &&
  164. s->transparency < 1 << avctx->bits_per_coded_sample)
  165. pal[s->transparency] &= 0xFFFFFF;
  166. return 0;
  167. }
  168. /**
  169. * Extracts the IFF extra context and updates internal
  170. * decoder structures.
  171. *
  172. * @param avctx the AVCodecContext where to extract extra context to
  173. * @param avpkt the AVPacket to extract extra context from or NULL to use avctx
  174. * @return 0 in case of success, a negative error code otherwise
  175. */
  176. static int extract_header(AVCodecContext *const avctx,
  177. const AVPacket *const avpkt) {
  178. const uint8_t *buf;
  179. unsigned buf_size;
  180. IffContext *s = avctx->priv_data;
  181. int i, palette_size;
  182. if (avctx->extradata_size < 2) {
  183. av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
  184. return AVERROR_INVALIDDATA;
  185. }
  186. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  187. if (avpkt) {
  188. int image_size;
  189. if (avpkt->size < 2)
  190. return AVERROR_INVALIDDATA;
  191. image_size = avpkt->size - AV_RB16(avpkt->data);
  192. buf = avpkt->data;
  193. buf_size = bytestream_get_be16(&buf);
  194. if (buf_size <= 1 || image_size <= 1) {
  195. av_log(avctx, AV_LOG_ERROR,
  196. "Invalid image size received: %u -> image data offset: %d\n",
  197. buf_size, image_size);
  198. return AVERROR_INVALIDDATA;
  199. }
  200. } else {
  201. buf = avctx->extradata;
  202. buf_size = bytestream_get_be16(&buf);
  203. if (buf_size <= 1 || palette_size < 0) {
  204. av_log(avctx, AV_LOG_ERROR,
  205. "Invalid palette size received: %u -> palette data offset: %d\n",
  206. buf_size, palette_size);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. }
  210. if (buf_size >= 41) {
  211. s->compression = bytestream_get_byte(&buf);
  212. s->bpp = bytestream_get_byte(&buf);
  213. s->ham = bytestream_get_byte(&buf);
  214. s->flags = bytestream_get_byte(&buf);
  215. s->transparency = bytestream_get_be16(&buf);
  216. s->masking = bytestream_get_byte(&buf);
  217. for (i = 0; i < 16; i++)
  218. s->tvdc[i] = bytestream_get_be16(&buf);
  219. if (s->masking == MASK_HAS_MASK) {
  220. if (s->bpp >= 8 && !s->ham) {
  221. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  222. av_freep(&s->mask_buf);
  223. av_freep(&s->mask_palbuf);
  224. s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);
  225. if (!s->mask_buf)
  226. return AVERROR(ENOMEM);
  227. if (s->bpp > 16) {
  228. av_log(avctx, AV_LOG_ERROR, "bpp %d too large for palette\n", s->bpp);
  229. av_freep(&s->mask_buf);
  230. return AVERROR(ENOMEM);
  231. }
  232. s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  233. if (!s->mask_palbuf) {
  234. av_freep(&s->mask_buf);
  235. return AVERROR(ENOMEM);
  236. }
  237. }
  238. s->bpp++;
  239. } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {
  240. av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
  241. return AVERROR_PATCHWELCOME;
  242. }
  243. if (!s->bpp || s->bpp > 32) {
  244. av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
  245. return AVERROR_INVALIDDATA;
  246. } else if (s->ham >= 8) {
  247. av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
  248. return AVERROR_INVALIDDATA;
  249. }
  250. av_freep(&s->ham_buf);
  251. av_freep(&s->ham_palbuf);
  252. if (s->ham) {
  253. int i, count = FFMIN(palette_size / 3, 1 << s->ham);
  254. int ham_count;
  255. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  256. s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);
  257. if (!s->ham_buf)
  258. return AVERROR(ENOMEM);
  259. ham_count = 8 * (1 << s->ham);
  260. s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  261. if (!s->ham_palbuf) {
  262. av_freep(&s->ham_buf);
  263. return AVERROR(ENOMEM);
  264. }
  265. if (count) { // HAM with color palette attached
  266. // prefill with black and palette and set HAM take direct value mask to zero
  267. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
  268. for (i=0; i < count; i++) {
  269. s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);
  270. }
  271. count = 1 << s->ham;
  272. } else { // HAM with grayscale color palette
  273. count = 1 << s->ham;
  274. for (i=0; i < count; i++) {
  275. s->ham_palbuf[i*2] = 0xFF000000; // take direct color value from palette
  276. s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));
  277. }
  278. }
  279. for (i=0; i < count; i++) {
  280. uint32_t tmp = i << (8 - s->ham);
  281. tmp |= tmp >> s->ham;
  282. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF; // just modify blue color component
  283. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00; // just modify red color component
  284. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF; // just modify green color component
  285. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  286. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  287. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  288. }
  289. if (s->masking == MASK_HAS_MASK) {
  290. for (i = 0; i < ham_count; i++)
  291. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  292. }
  293. }
  294. }
  295. return 0;
  296. }
  297. static av_cold int decode_init(AVCodecContext *avctx)
  298. {
  299. IffContext *s = avctx->priv_data;
  300. int err;
  301. if (avctx->bits_per_coded_sample <= 8) {
  302. int palette_size;
  303. if (avctx->extradata_size >= 2)
  304. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  305. else
  306. palette_size = 0;
  307. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  308. (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  309. } else if (avctx->bits_per_coded_sample <= 32) {
  310. if (avctx->codec_tag == MKTAG('R','G','B','8')) {
  311. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  312. } else if (avctx->codec_tag == MKTAG('R','G','B','N')) {
  313. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  314. } else if (avctx->codec_tag != MKTAG('D','E','E','P')) {
  315. if (avctx->bits_per_coded_sample == 24) {
  316. avctx->pix_fmt = AV_PIX_FMT_0BGR32;
  317. } else if (avctx->bits_per_coded_sample == 32) {
  318. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  319. } else {
  320. av_log_ask_for_sample(avctx, "unknown bits_per_coded_sample\n");
  321. return AVERROR_PATCHWELCOME;
  322. }
  323. }
  324. } else {
  325. return AVERROR_INVALIDDATA;
  326. }
  327. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  328. return err;
  329. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  330. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  331. if (!s->planebuf)
  332. return AVERROR(ENOMEM);
  333. s->bpp = avctx->bits_per_coded_sample;
  334. avcodec_get_frame_defaults(&s->frame);
  335. if ((err = extract_header(avctx, NULL)) < 0)
  336. return err;
  337. s->frame.reference = 3;
  338. return 0;
  339. }
  340. /**
  341. * Decode interleaved plane buffer up to 8bpp
  342. * @param dst Destination buffer
  343. * @param buf Source buffer
  344. * @param buf_size
  345. * @param plane plane number to decode as
  346. */
  347. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  348. {
  349. const uint64_t *lut = plane8_lut[plane];
  350. if (plane >= 8) {
  351. av_log(NULL, AV_LOG_WARNING, "Ignoring extra planes beyond 8\n");
  352. return;
  353. }
  354. do {
  355. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  356. AV_WN64A(dst, v);
  357. dst += 8;
  358. } while (--buf_size);
  359. }
  360. /**
  361. * Decode interleaved plane buffer up to 24bpp
  362. * @param dst Destination buffer
  363. * @param buf Source buffer
  364. * @param buf_size
  365. * @param plane plane number to decode as
  366. */
  367. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  368. {
  369. const uint32_t *lut = plane32_lut[plane];
  370. do {
  371. unsigned mask = (*buf >> 2) & ~3;
  372. dst[0] |= lut[mask++];
  373. dst[1] |= lut[mask++];
  374. dst[2] |= lut[mask++];
  375. dst[3] |= lut[mask];
  376. mask = (*buf++ << 2) & 0x3F;
  377. dst[4] |= lut[mask++];
  378. dst[5] |= lut[mask++];
  379. dst[6] |= lut[mask++];
  380. dst[7] |= lut[mask];
  381. dst += 8;
  382. } while (--buf_size);
  383. }
  384. #define DECODE_HAM_PLANE32(x) \
  385. first = buf[x] << 1; \
  386. second = buf[(x)+1] << 1; \
  387. delta &= pal[first++]; \
  388. delta |= pal[first]; \
  389. dst[x] = delta; \
  390. delta &= pal[second++]; \
  391. delta |= pal[second]; \
  392. dst[(x)+1] = delta
  393. /**
  394. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  395. *
  396. * @param dst the destination 24bpp buffer
  397. * @param buf the source 8bpp chunky buffer
  398. * @param pal the HAM decode table
  399. * @param buf_size the plane size in bytes
  400. */
  401. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  402. const uint32_t *const pal, unsigned buf_size)
  403. {
  404. uint32_t delta = pal[1]; /* first palette entry */
  405. do {
  406. uint32_t first, second;
  407. DECODE_HAM_PLANE32(0);
  408. DECODE_HAM_PLANE32(2);
  409. DECODE_HAM_PLANE32(4);
  410. DECODE_HAM_PLANE32(6);
  411. buf += 8;
  412. dst += 8;
  413. } while (--buf_size);
  414. }
  415. static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
  416. const uint32_t *const pal, unsigned width)
  417. {
  418. do {
  419. *dst++ = pal[*buf++];
  420. } while (--width);
  421. }
  422. /**
  423. * Decode one complete byterun1 encoded line.
  424. *
  425. * @param dst the destination buffer where to store decompressed bitstream
  426. * @param dst_size the destination plane size in bytes
  427. * @param buf the source byterun1 compressed bitstream
  428. * @param buf_end the EOF of source byterun1 compressed bitstream
  429. * @return number of consumed bytes in byterun1 compressed bitstream
  430. */
  431. static int decode_byterun(uint8_t *dst, int dst_size,
  432. const uint8_t *buf, const uint8_t *const buf_end) {
  433. const uint8_t *const buf_start = buf;
  434. unsigned x;
  435. for (x = 0; x < dst_size && buf < buf_end;) {
  436. unsigned length;
  437. const int8_t value = *buf++;
  438. if (value >= 0) {
  439. length = value + 1;
  440. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  441. buf += length;
  442. } else if (value > -128) {
  443. length = -value + 1;
  444. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  445. } else { // noop
  446. continue;
  447. }
  448. x += length;
  449. }
  450. return buf - buf_start;
  451. }
  452. #define DECODE_RGBX_COMMON(type) \
  453. if (!length) { \
  454. length = bytestream2_get_byte(gb); \
  455. if (!length) { \
  456. length = bytestream2_get_be16(gb); \
  457. if (!length) \
  458. return; \
  459. } \
  460. } \
  461. for (i = 0; i < length; i++) { \
  462. *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
  463. x += 1; \
  464. if (x >= width) { \
  465. y += 1; \
  466. if (y >= height) \
  467. return; \
  468. x = 0; \
  469. } \
  470. }
  471. /**
  472. * Decode RGB8 buffer
  473. * @param[out] dst Destination buffer
  474. * @param width Width of destination buffer (pixels)
  475. * @param height Height of destination buffer (pixels)
  476. * @param linesize Line size of destination buffer (bytes)
  477. */
  478. static void decode_rgb8(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  479. {
  480. int x = 0, y = 0, i, length;
  481. while (bytestream2_get_bytes_left(gb) >= 4) {
  482. uint32_t pixel = 0xFF000000 | bytestream2_get_be24(gb);
  483. length = bytestream2_get_byte(gb) & 0x7F;
  484. DECODE_RGBX_COMMON(uint32_t)
  485. }
  486. }
  487. /**
  488. * Decode RGBN buffer
  489. * @param[out] dst Destination buffer
  490. * @param width Width of destination buffer (pixels)
  491. * @param height Height of destination buffer (pixels)
  492. * @param linesize Line size of destination buffer (bytes)
  493. */
  494. static void decode_rgbn(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  495. {
  496. int x = 0, y = 0, i, length;
  497. while (bytestream2_get_bytes_left(gb) >= 2) {
  498. uint32_t pixel = bytestream2_get_be16u(gb);
  499. length = pixel & 0x7;
  500. pixel >>= 4;
  501. DECODE_RGBX_COMMON(uint16_t)
  502. }
  503. }
  504. /**
  505. * Decode DEEP RLE 32-bit buffer
  506. * @param[out] dst Destination buffer
  507. * @param[in] src Source buffer
  508. * @param src_size Source buffer size (bytes)
  509. * @param width Width of destination buffer (pixels)
  510. * @param height Height of destination buffer (pixels)
  511. * @param linesize Line size of destination buffer (bytes)
  512. */
  513. static void decode_deep_rle32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
  514. {
  515. const uint8_t *src_end = src + src_size;
  516. int x = 0, y = 0, i;
  517. while (src + 5 <= src_end) {
  518. int opcode;
  519. opcode = *(int8_t *)src++;
  520. if (opcode >= 0) {
  521. int size = opcode + 1;
  522. for (i = 0; i < size; i++) {
  523. int length = FFMIN(size - i, width);
  524. memcpy(dst + y*linesize + x * 4, src, length * 4);
  525. src += length * 4;
  526. x += length;
  527. i += length;
  528. if (x >= width) {
  529. x = 0;
  530. y += 1;
  531. if (y >= height)
  532. return;
  533. }
  534. }
  535. } else {
  536. int size = -opcode + 1;
  537. uint32_t pixel = AV_RN32(src);
  538. for (i = 0; i < size; i++) {
  539. *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
  540. x += 1;
  541. if (x >= width) {
  542. x = 0;
  543. y += 1;
  544. if (y >= height)
  545. return;
  546. }
  547. }
  548. src += 4;
  549. }
  550. }
  551. }
  552. /**
  553. * Decode DEEP TVDC 32-bit buffer
  554. * @param[out] dst Destination buffer
  555. * @param[in] src Source buffer
  556. * @param src_size Source buffer size (bytes)
  557. * @param width Width of destination buffer (pixels)
  558. * @param height Height of destination buffer (pixels)
  559. * @param linesize Line size of destination buffer (bytes)
  560. * @param[int] tvdc TVDC lookup table
  561. */
  562. static void decode_deep_tvdc32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize, const int16_t *tvdc)
  563. {
  564. int x = 0, y = 0, plane = 0;
  565. int8_t pixel = 0;
  566. int i, j;
  567. for (i = 0; i < src_size * 2;) {
  568. #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
  569. int d = tvdc[GETNIBBLE];
  570. i++;
  571. if (d) {
  572. pixel += d;
  573. dst[y * linesize + x*4 + plane] = pixel;
  574. x++;
  575. } else {
  576. if (i >= src_size * 2)
  577. return;
  578. d = GETNIBBLE + 1;
  579. i++;
  580. d = FFMIN(d, width - x);
  581. for (j = 0; j < d; j++) {
  582. dst[y * linesize + x*4 + plane] = pixel;
  583. x++;
  584. }
  585. }
  586. if (x >= width) {
  587. plane++;
  588. if (plane >= 4) {
  589. y++;
  590. if (y >= height)
  591. return;
  592. plane = 0;
  593. }
  594. x = 0;
  595. pixel = 0;
  596. i = (i + 1) & ~1;
  597. }
  598. }
  599. }
  600. static int unsupported(AVCodecContext *avctx)
  601. {
  602. IffContext *s = avctx->priv_data;
  603. av_log_ask_for_sample(avctx, "unsupported bitmap (compression %i, bpp %i, ham %i)\n", s->compression, s->bpp, s->ham);
  604. return AVERROR_INVALIDDATA;
  605. }
  606. static int decode_frame(AVCodecContext *avctx,
  607. void *data, int *got_frame,
  608. AVPacket *avpkt)
  609. {
  610. IffContext *s = avctx->priv_data;
  611. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  612. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  613. const uint8_t *buf_end = buf+buf_size;
  614. int y, plane, res;
  615. GetByteContext gb;
  616. if ((res = extract_header(avctx, avpkt)) < 0)
  617. return res;
  618. if (s->init) {
  619. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  620. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  621. return res;
  622. }
  623. } else if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
  624. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  625. return res;
  626. } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  627. if ((res = cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  628. return res;
  629. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB32 && avctx->bits_per_coded_sample <= 8) {
  630. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  631. return res;
  632. }
  633. s->init = 1;
  634. switch (s->compression) {
  635. case 0:
  636. if (avctx->codec_tag == MKTAG('A','C','B','M')) {
  637. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  638. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  639. for (plane = 0; plane < s->bpp; plane++) {
  640. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  641. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  642. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  643. buf += s->planesize;
  644. }
  645. }
  646. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  647. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  648. for(y = 0; y < avctx->height; y++) {
  649. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  650. memset(s->ham_buf, 0, s->planesize * 8);
  651. for (plane = 0; plane < s->bpp; plane++) {
  652. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  653. if (start >= buf_end)
  654. break;
  655. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  656. }
  657. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  658. }
  659. } else
  660. return unsupported(avctx);
  661. } else if (avctx->codec_tag == MKTAG('D','E','E','P')) {
  662. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  663. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  664. int x;
  665. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  666. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  667. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  668. buf += raw_width;
  669. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  670. for(x = 0; x < avctx->width; x++)
  671. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  672. }
  673. }
  674. } else if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
  675. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  676. for(y = 0; y < avctx->height; y++ ) {
  677. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  678. memset(row, 0, avctx->width);
  679. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  680. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  681. buf += s->planesize;
  682. }
  683. }
  684. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  685. for (y = 0; y < avctx->height; y++) {
  686. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  687. memset(s->ham_buf, 0, s->planesize * 8);
  688. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  689. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  690. buf += s->planesize;
  691. }
  692. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  693. }
  694. } else { // AV_PIX_FMT_BGR32
  695. for(y = 0; y < avctx->height; y++ ) {
  696. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  697. memset(row, 0, avctx->width << 2);
  698. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  699. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  700. buf += s->planesize;
  701. }
  702. }
  703. }
  704. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  705. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  706. for(y = 0; y < avctx->height && buf_end > buf; y++ ) {
  707. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  708. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  709. buf += avctx->width + (avctx->width % 2); // padding if odd
  710. }
  711. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  712. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  713. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  714. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  715. buf += avctx->width + (avctx->width & 1); // padding if odd
  716. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  717. }
  718. } else
  719. return unsupported(avctx);
  720. }
  721. break;
  722. case 1:
  723. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  724. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  725. for(y = 0; y < avctx->height ; y++ ) {
  726. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  727. memset(row, 0, avctx->width);
  728. for (plane = 0; plane < s->bpp; plane++) {
  729. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  730. decodeplane8(row, s->planebuf, s->planesize, plane);
  731. }
  732. }
  733. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  734. for (y = 0; y < avctx->height ; y++ ) {
  735. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  736. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  737. for (plane = 0; plane < s->bpp; plane++) {
  738. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  739. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  740. }
  741. lookup_pal_indicies((uint32_t *) row, s->mask_buf, s->mask_palbuf, avctx->width);
  742. }
  743. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  744. for (y = 0; y < avctx->height ; y++) {
  745. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  746. memset(s->ham_buf, 0, s->planesize * 8);
  747. for (plane = 0; plane < s->bpp; plane++) {
  748. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  749. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  750. }
  751. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  752. }
  753. } else { //AV_PIX_FMT_BGR32
  754. for(y = 0; y < avctx->height ; y++ ) {
  755. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  756. memset(row, 0, avctx->width << 2);
  757. for (plane = 0; plane < s->bpp; plane++) {
  758. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  759. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  760. }
  761. }
  762. }
  763. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  764. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  765. for(y = 0; y < avctx->height ; y++ ) {
  766. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  767. buf += decode_byterun(row, avctx->width, buf, buf_end);
  768. }
  769. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  770. for (y = 0; y < avctx->height ; y++) {
  771. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  772. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  773. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  774. }
  775. } else
  776. return unsupported(avctx);
  777. } else if (avctx->codec_tag == MKTAG('D','E','E','P')) { // IFF-DEEP
  778. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  779. if (av_get_bits_per_pixel(desc) == 32)
  780. decode_deep_rle32(s->frame.data[0], buf, buf_size, avctx->width, avctx->height, s->frame.linesize[0]);
  781. else
  782. return unsupported(avctx);
  783. }
  784. break;
  785. case 4:
  786. bytestream2_init(&gb, buf, buf_size);
  787. if (avctx->codec_tag == MKTAG('R','G','B','8'))
  788. decode_rgb8(&gb, s->frame.data[0], avctx->width, avctx->height, s->frame.linesize[0]);
  789. else if (avctx->codec_tag == MKTAG('R','G','B','N'))
  790. decode_rgbn(&gb, s->frame.data[0], avctx->width, avctx->height, s->frame.linesize[0]);
  791. else
  792. return unsupported(avctx);
  793. break;
  794. case 5:
  795. if (avctx->codec_tag == MKTAG('D','E','E','P')) {
  796. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  797. if (av_get_bits_per_pixel(desc) == 32)
  798. decode_deep_tvdc32(s->frame.data[0], buf, buf_size, avctx->width, avctx->height, s->frame.linesize[0], s->tvdc);
  799. else
  800. return unsupported(avctx);
  801. } else
  802. return unsupported(avctx);
  803. break;
  804. default:
  805. return unsupported(avctx);
  806. }
  807. *got_frame = 1;
  808. *(AVFrame*)data = s->frame;
  809. return buf_size;
  810. }
  811. static av_cold int decode_end(AVCodecContext *avctx)
  812. {
  813. IffContext *s = avctx->priv_data;
  814. if (s->frame.data[0])
  815. avctx->release_buffer(avctx, &s->frame);
  816. av_freep(&s->planebuf);
  817. av_freep(&s->ham_buf);
  818. av_freep(&s->ham_palbuf);
  819. return 0;
  820. }
  821. #if CONFIG_IFF_ILBM_DECODER
  822. AVCodec ff_iff_ilbm_decoder = {
  823. .name = "iff",
  824. .type = AVMEDIA_TYPE_VIDEO,
  825. .id = AV_CODEC_ID_IFF_ILBM,
  826. .priv_data_size = sizeof(IffContext),
  827. .init = decode_init,
  828. .close = decode_end,
  829. .decode = decode_frame,
  830. .capabilities = CODEC_CAP_DR1,
  831. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  832. };
  833. #endif
  834. #if CONFIG_IFF_BYTERUN1_DECODER
  835. AVCodec ff_iff_byterun1_decoder = {
  836. .name = "iff",
  837. .type = AVMEDIA_TYPE_VIDEO,
  838. .id = AV_CODEC_ID_IFF_BYTERUN1,
  839. .priv_data_size = sizeof(IffContext),
  840. .init = decode_init,
  841. .close = decode_end,
  842. .decode = decode_frame,
  843. .capabilities = CODEC_CAP_DR1,
  844. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  845. };
  846. #endif