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.

918 lines
36KB

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