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.

828 lines
33KB

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