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.

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