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.

901 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 {
  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, 1 << plane, \
  99. 0, 0, 1 << plane, 0, \
  100. 0, 0, 1 << plane, 1 << plane, \
  101. 0, 1 << plane, 0, 0, \
  102. 0, 1 << plane, 0, 1 << plane, \
  103. 0, 1 << plane, 1 << plane, 0, \
  104. 0, 1 << plane, 1 << plane, 1 << plane, \
  105. 1 << plane, 0, 0, 0, \
  106. 1 << plane, 0, 0, 1 << plane, \
  107. 1 << plane, 0, 1 << plane, 0, \
  108. 1 << plane, 0, 1 << plane, 1 << plane, \
  109. 1 << plane, 1 << plane, 0, 0, \
  110. 1 << plane, 1 << plane, 0, 1 << plane, \
  111. 1 << plane, 1 << plane, 1 << plane, 0, \
  112. 1 << plane, 1 << plane, 1 << plane, 1 << 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->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_end(AVCodecContext *avctx)
  297. {
  298. IffContext *s = avctx->priv_data;
  299. av_frame_free(&s->frame);
  300. av_freep(&s->planebuf);
  301. av_freep(&s->ham_buf);
  302. av_freep(&s->ham_palbuf);
  303. return 0;
  304. }
  305. static av_cold int decode_init(AVCodecContext *avctx)
  306. {
  307. IffContext *s = avctx->priv_data;
  308. int err;
  309. if (avctx->bits_per_coded_sample <= 8) {
  310. int palette_size;
  311. if (avctx->extradata_size >= 2)
  312. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  313. else
  314. palette_size = 0;
  315. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  316. (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  317. } else if (avctx->bits_per_coded_sample <= 32) {
  318. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8')) {
  319. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  320. } else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N')) {
  321. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  322. } else if (avctx->codec_tag != MKTAG('D', 'E', 'E', 'P')) {
  323. if (avctx->bits_per_coded_sample == 24) {
  324. avctx->pix_fmt = AV_PIX_FMT_0BGR32;
  325. } else if (avctx->bits_per_coded_sample == 32) {
  326. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  327. } else {
  328. avpriv_request_sample(avctx, "unknown bits_per_coded_sample");
  329. return AVERROR_PATCHWELCOME;
  330. }
  331. }
  332. } else {
  333. return AVERROR_INVALIDDATA;
  334. }
  335. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  336. return err;
  337. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  338. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  339. if (!s->planebuf)
  340. return AVERROR(ENOMEM);
  341. s->bpp = avctx->bits_per_coded_sample;
  342. s->frame = av_frame_alloc();
  343. if (!s->frame) {
  344. decode_end(avctx);
  345. return AVERROR(ENOMEM);
  346. }
  347. if ((err = extract_header(avctx, NULL)) < 0)
  348. return err;
  349. return 0;
  350. }
  351. /**
  352. * Decode interleaved plane buffer up to 8bpp
  353. * @param dst Destination buffer
  354. * @param buf Source buffer
  355. * @param buf_size
  356. * @param plane plane number to decode as
  357. */
  358. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  359. {
  360. const uint64_t *lut = plane8_lut[plane];
  361. if (plane >= 8) {
  362. av_log(NULL, AV_LOG_WARNING, "Ignoring extra planes beyond 8\n");
  363. return;
  364. }
  365. do {
  366. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  367. AV_WN64A(dst, v);
  368. dst += 8;
  369. } while (--buf_size);
  370. }
  371. /**
  372. * Decode interleaved plane buffer up to 24bpp
  373. * @param dst Destination buffer
  374. * @param buf Source buffer
  375. * @param buf_size
  376. * @param plane plane number to decode as
  377. */
  378. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  379. {
  380. const uint32_t *lut = plane32_lut[plane];
  381. do {
  382. unsigned mask = (*buf >> 2) & ~3;
  383. dst[0] |= lut[mask++];
  384. dst[1] |= lut[mask++];
  385. dst[2] |= lut[mask++];
  386. dst[3] |= lut[mask];
  387. mask = (*buf++ << 2) & 0x3F;
  388. dst[4] |= lut[mask++];
  389. dst[5] |= lut[mask++];
  390. dst[6] |= lut[mask++];
  391. dst[7] |= lut[mask];
  392. dst += 8;
  393. } while (--buf_size);
  394. }
  395. #define DECODE_HAM_PLANE32(x) \
  396. first = buf[x] << 1; \
  397. second = buf[(x)+1] << 1; \
  398. delta &= pal[first++]; \
  399. delta |= pal[first]; \
  400. dst[x] = delta; \
  401. delta &= pal[second++]; \
  402. delta |= pal[second]; \
  403. dst[(x)+1] = delta
  404. /**
  405. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  406. *
  407. * @param dst the destination 24bpp buffer
  408. * @param buf the source 8bpp chunky buffer
  409. * @param pal the HAM decode table
  410. * @param buf_size the plane size in bytes
  411. */
  412. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  413. const uint32_t *const pal, unsigned buf_size)
  414. {
  415. uint32_t delta = pal[1]; /* first palette entry */
  416. do {
  417. uint32_t first, second;
  418. DECODE_HAM_PLANE32(0);
  419. DECODE_HAM_PLANE32(2);
  420. DECODE_HAM_PLANE32(4);
  421. DECODE_HAM_PLANE32(6);
  422. buf += 8;
  423. dst += 8;
  424. } while (--buf_size);
  425. }
  426. static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
  427. const uint32_t *const pal, unsigned width)
  428. {
  429. do {
  430. *dst++ = pal[*buf++];
  431. } while (--width);
  432. }
  433. /**
  434. * Decode one complete byterun1 encoded line.
  435. *
  436. * @param dst the destination buffer where to store decompressed bitstream
  437. * @param dst_size the destination plane size in bytes
  438. * @param buf the source byterun1 compressed bitstream
  439. * @param buf_end the EOF of source byterun1 compressed bitstream
  440. * @return number of consumed bytes in byterun1 compressed bitstream
  441. */
  442. static int decode_byterun(uint8_t *dst, int dst_size,
  443. const uint8_t *buf, const uint8_t *const buf_end)
  444. {
  445. const uint8_t *const buf_start = buf;
  446. unsigned x;
  447. for (x = 0; x < dst_size && buf < buf_end;) {
  448. unsigned length;
  449. const int8_t value = *buf++;
  450. if (value >= 0) {
  451. length = value + 1;
  452. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  453. buf += length;
  454. } else if (value > -128) {
  455. length = -value + 1;
  456. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  457. } else { // noop
  458. continue;
  459. }
  460. x += length;
  461. }
  462. return buf - buf_start;
  463. }
  464. #define DECODE_RGBX_COMMON(type) \
  465. if (!length) { \
  466. length = bytestream2_get_byte(gb); \
  467. if (!length) { \
  468. length = bytestream2_get_be16(gb); \
  469. if (!length) \
  470. return; \
  471. } \
  472. } \
  473. for (i = 0; i < length; i++) { \
  474. *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
  475. x += 1; \
  476. if (x >= width) { \
  477. y += 1; \
  478. if (y >= height) \
  479. return; \
  480. x = 0; \
  481. } \
  482. }
  483. /**
  484. * Decode RGB8 buffer
  485. * @param[out] dst Destination buffer
  486. * @param width Width of destination buffer (pixels)
  487. * @param height Height of destination buffer (pixels)
  488. * @param linesize Line size of destination buffer (bytes)
  489. */
  490. static void decode_rgb8(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  491. {
  492. int x = 0, y = 0, i, length;
  493. while (bytestream2_get_bytes_left(gb) >= 4) {
  494. uint32_t pixel = 0xFF000000 | bytestream2_get_be24(gb);
  495. length = bytestream2_get_byte(gb) & 0x7F;
  496. DECODE_RGBX_COMMON(uint32_t)
  497. }
  498. }
  499. /**
  500. * Decode RGBN buffer
  501. * @param[out] dst Destination buffer
  502. * @param width Width of destination buffer (pixels)
  503. * @param height Height of destination buffer (pixels)
  504. * @param linesize Line size of destination buffer (bytes)
  505. */
  506. static void decode_rgbn(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  507. {
  508. int x = 0, y = 0, i, length;
  509. while (bytestream2_get_bytes_left(gb) >= 2) {
  510. uint32_t pixel = bytestream2_get_be16u(gb);
  511. length = pixel & 0x7;
  512. pixel >>= 4;
  513. DECODE_RGBX_COMMON(uint16_t)
  514. }
  515. }
  516. /**
  517. * Decode DEEP RLE 32-bit buffer
  518. * @param[out] dst Destination buffer
  519. * @param[in] src Source buffer
  520. * @param src_size Source buffer size (bytes)
  521. * @param width Width of destination buffer (pixels)
  522. * @param height Height of destination buffer (pixels)
  523. * @param linesize Line size of destination buffer (bytes)
  524. */
  525. static void decode_deep_rle32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
  526. {
  527. const uint8_t *src_end = src + src_size;
  528. int x = 0, y = 0, i;
  529. while (src + 5 <= src_end) {
  530. int opcode;
  531. opcode = *(int8_t *)src++;
  532. if (opcode >= 0) {
  533. int size = opcode + 1;
  534. for (i = 0; i < size; i++) {
  535. int length = FFMIN(size - i, width);
  536. memcpy(dst + y*linesize + x * 4, src, length * 4);
  537. src += length * 4;
  538. x += length;
  539. i += length;
  540. if (x >= width) {
  541. x = 0;
  542. y += 1;
  543. if (y >= height)
  544. return;
  545. }
  546. }
  547. } else {
  548. int size = -opcode + 1;
  549. uint32_t pixel = AV_RN32(src);
  550. for (i = 0; i < size; i++) {
  551. *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
  552. x += 1;
  553. if (x >= width) {
  554. x = 0;
  555. y += 1;
  556. if (y >= height)
  557. return;
  558. }
  559. }
  560. src += 4;
  561. }
  562. }
  563. }
  564. /**
  565. * Decode DEEP TVDC 32-bit buffer
  566. * @param[out] dst Destination buffer
  567. * @param[in] src Source buffer
  568. * @param src_size Source buffer size (bytes)
  569. * @param width Width of destination buffer (pixels)
  570. * @param height Height of destination buffer (pixels)
  571. * @param linesize Line size of destination buffer (bytes)
  572. * @param[int] tvdc TVDC lookup table
  573. */
  574. 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)
  575. {
  576. int x = 0, y = 0, plane = 0;
  577. int8_t pixel = 0;
  578. int i, j;
  579. for (i = 0; i < src_size * 2;) {
  580. #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
  581. int d = tvdc[GETNIBBLE];
  582. i++;
  583. if (d) {
  584. pixel += d;
  585. dst[y * linesize + x*4 + plane] = pixel;
  586. x++;
  587. } else {
  588. if (i >= src_size * 2)
  589. return;
  590. d = GETNIBBLE + 1;
  591. i++;
  592. d = FFMIN(d, width - x);
  593. for (j = 0; j < d; j++) {
  594. dst[y * linesize + x*4 + plane] = pixel;
  595. x++;
  596. }
  597. }
  598. if (x >= width) {
  599. plane++;
  600. if (plane >= 4) {
  601. y++;
  602. if (y >= height)
  603. return;
  604. plane = 0;
  605. }
  606. x = 0;
  607. pixel = 0;
  608. i = (i + 1) & ~1;
  609. }
  610. }
  611. }
  612. static int unsupported(AVCodecContext *avctx)
  613. {
  614. IffContext *s = avctx->priv_data;
  615. avpriv_request_sample(avctx, "bitmap (compression %i, bpp %i, ham %i)", s->compression, s->bpp, s->ham);
  616. return AVERROR_INVALIDDATA;
  617. }
  618. static int decode_frame(AVCodecContext *avctx,
  619. void *data, int *got_frame,
  620. AVPacket *avpkt)
  621. {
  622. IffContext *s = avctx->priv_data;
  623. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  624. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  625. const uint8_t *buf_end = buf + buf_size;
  626. int y, plane, res;
  627. GetByteContext gb;
  628. if ((res = extract_header(avctx, avpkt)) < 0)
  629. return res;
  630. if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
  631. return res;
  632. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  633. avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  634. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame->data[1])) < 0)
  635. return res;
  636. } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  637. avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  638. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  639. return res;
  640. }
  641. s->init = 1;
  642. switch (s->compression) {
  643. case 0:
  644. if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  645. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  646. memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
  647. for (plane = 0; plane < s->bpp; plane++) {
  648. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  649. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  650. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  651. buf += s->planesize;
  652. }
  653. }
  654. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  655. memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
  656. for (y = 0; y < avctx->height; y++) {
  657. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  658. memset(s->ham_buf, 0, s->planesize * 8);
  659. for (plane = 0; plane < s->bpp; plane++) {
  660. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  661. if (start >= buf_end)
  662. break;
  663. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  664. }
  665. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  666. }
  667. } else
  668. return unsupported(avctx);
  669. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  670. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  671. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  672. int x;
  673. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  674. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  675. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  676. buf += raw_width;
  677. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  678. for (x = 0; x < avctx->width; x++)
  679. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  680. }
  681. }
  682. } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  683. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  684. for (y = 0; y < avctx->height; y++) {
  685. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  686. memset(row, 0, avctx->width);
  687. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  688. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  689. buf += s->planesize;
  690. }
  691. }
  692. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  693. for (y = 0; y < avctx->height; y++) {
  694. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  695. memset(s->ham_buf, 0, s->planesize * 8);
  696. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  697. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  698. buf += s->planesize;
  699. }
  700. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  701. }
  702. } else { // AV_PIX_FMT_BGR32
  703. for (y = 0; y < avctx->height; y++) {
  704. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  705. memset(row, 0, avctx->width << 2);
  706. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  707. decodeplane32((uint32_t *)row, buf,
  708. FFMIN(s->planesize, buf_end - buf), plane);
  709. buf += s->planesize;
  710. }
  711. }
  712. }
  713. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  714. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  715. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  716. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  717. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  718. buf += avctx->width + (avctx->width % 2); // padding if odd
  719. }
  720. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  721. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  722. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  723. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  724. buf += avctx->width + (avctx->width & 1); // padding if odd
  725. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  726. }
  727. } else
  728. return unsupported(avctx);
  729. }
  730. break;
  731. case 1:
  732. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  733. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  734. for (y = 0; y < avctx->height; y++) {
  735. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  736. memset(row, 0, avctx->width);
  737. for (plane = 0; plane < s->bpp; plane++) {
  738. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  739. decodeplane8(row, s->planebuf, s->planesize, plane);
  740. }
  741. }
  742. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  743. for (y = 0; y < avctx->height; y++) {
  744. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  745. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  746. for (plane = 0; plane < s->bpp; plane++) {
  747. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  748. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  749. }
  750. lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
  751. }
  752. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  753. for (y = 0; y < avctx->height; y++) {
  754. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  755. memset(s->ham_buf, 0, s->planesize * 8);
  756. for (plane = 0; plane < s->bpp; plane++) {
  757. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  758. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  759. }
  760. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  761. }
  762. } else { // AV_PIX_FMT_BGR32
  763. for (y = 0; y < avctx->height; y++) {
  764. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  765. memset(row, 0, avctx->width << 2);
  766. for (plane = 0; plane < s->bpp; plane++) {
  767. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  768. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  769. }
  770. }
  771. }
  772. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  773. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  774. for (y = 0; y < avctx->height; y++) {
  775. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  776. buf += decode_byterun(row, avctx->width, buf, buf_end);
  777. }
  778. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  779. for (y = 0; y < avctx->height; y++) {
  780. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  781. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  782. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  783. }
  784. } else
  785. return unsupported(avctx);
  786. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
  787. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  788. if (av_get_bits_per_pixel(desc) == 32)
  789. decode_deep_rle32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0]);
  790. else
  791. return unsupported(avctx);
  792. }
  793. break;
  794. case 4:
  795. bytestream2_init(&gb, buf, buf_size);
  796. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8'))
  797. decode_rgb8(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
  798. else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N'))
  799. decode_rgbn(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
  800. else
  801. return unsupported(avctx);
  802. break;
  803. case 5:
  804. if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  805. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  806. if (av_get_bits_per_pixel(desc) == 32)
  807. decode_deep_tvdc32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0], s->tvdc);
  808. else
  809. return unsupported(avctx);
  810. } else
  811. return unsupported(avctx);
  812. break;
  813. default:
  814. return unsupported(avctx);
  815. }
  816. if ((res = av_frame_ref(data, s->frame)) < 0)
  817. return res;
  818. *got_frame = 1;
  819. return buf_size;
  820. }
  821. #if CONFIG_IFF_ILBM_DECODER
  822. AVCodec ff_iff_ilbm_decoder = {
  823. .name = "iff",
  824. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  825. .type = AVMEDIA_TYPE_VIDEO,
  826. .id = AV_CODEC_ID_IFF_ILBM,
  827. .priv_data_size = sizeof(IffContext),
  828. .init = decode_init,
  829. .close = decode_end,
  830. .decode = decode_frame,
  831. .capabilities = CODEC_CAP_DR1,
  832. };
  833. #endif
  834. #if CONFIG_IFF_BYTERUN1_DECODER
  835. AVCodec ff_iff_byterun1_decoder = {
  836. .name = "iff",
  837. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  838. .type = AVMEDIA_TYPE_VIDEO,
  839. .id = AV_CODEC_ID_IFF_BYTERUN1,
  840. .priv_data_size = sizeof(IffContext),
  841. .init = decode_init,
  842. .close = decode_end,
  843. .decode = decode_frame,
  844. .capabilities = CODEC_CAP_DR1,
  845. };
  846. #endif