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.

906 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 = FFMIN3(value + 1, dst_size - x, buf_end - buf);
  452. memcpy(dst + x, buf, length);
  453. buf += length;
  454. } else if (value > -128) {
  455. length = FFMIN(-value + 1, dst_size - x);
  456. memset(dst + x, *buf++, length);
  457. } else { // noop
  458. continue;
  459. }
  460. x += length;
  461. }
  462. if (x < dst_size) {
  463. av_log(NULL, AV_LOG_WARNING, "decode_byterun ended before plane size\n");
  464. memset(dst+x, 0, dst_size - x);
  465. }
  466. return buf - buf_start;
  467. }
  468. #define DECODE_RGBX_COMMON(type) \
  469. if (!length) { \
  470. length = bytestream2_get_byte(gb); \
  471. if (!length) { \
  472. length = bytestream2_get_be16(gb); \
  473. if (!length) \
  474. return; \
  475. } \
  476. } \
  477. for (i = 0; i < length; i++) { \
  478. *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
  479. x += 1; \
  480. if (x >= width) { \
  481. y += 1; \
  482. if (y >= height) \
  483. return; \
  484. x = 0; \
  485. } \
  486. }
  487. /**
  488. * Decode RGB8 buffer
  489. * @param[out] dst Destination buffer
  490. * @param width Width of destination buffer (pixels)
  491. * @param height Height of destination buffer (pixels)
  492. * @param linesize Line size of destination buffer (bytes)
  493. */
  494. static void decode_rgb8(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  495. {
  496. int x = 0, y = 0, i, length;
  497. while (bytestream2_get_bytes_left(gb) >= 4) {
  498. uint32_t pixel = 0xFF000000 | bytestream2_get_be24(gb);
  499. length = bytestream2_get_byte(gb) & 0x7F;
  500. DECODE_RGBX_COMMON(uint32_t)
  501. }
  502. }
  503. /**
  504. * Decode RGBN buffer
  505. * @param[out] dst Destination buffer
  506. * @param width Width of destination buffer (pixels)
  507. * @param height Height of destination buffer (pixels)
  508. * @param linesize Line size of destination buffer (bytes)
  509. */
  510. static void decode_rgbn(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  511. {
  512. int x = 0, y = 0, i, length;
  513. while (bytestream2_get_bytes_left(gb) >= 2) {
  514. uint32_t pixel = bytestream2_get_be16u(gb);
  515. length = pixel & 0x7;
  516. pixel >>= 4;
  517. DECODE_RGBX_COMMON(uint16_t)
  518. }
  519. }
  520. /**
  521. * Decode DEEP RLE 32-bit buffer
  522. * @param[out] dst Destination buffer
  523. * @param[in] src Source buffer
  524. * @param src_size Source buffer size (bytes)
  525. * @param width Width of destination buffer (pixels)
  526. * @param height Height of destination buffer (pixels)
  527. * @param linesize Line size of destination buffer (bytes)
  528. */
  529. static void decode_deep_rle32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
  530. {
  531. const uint8_t *src_end = src + src_size;
  532. int x = 0, y = 0, i;
  533. while (src + 5 <= src_end) {
  534. int opcode;
  535. opcode = *(int8_t *)src++;
  536. if (opcode >= 0) {
  537. int size = opcode + 1;
  538. for (i = 0; i < size; i++) {
  539. int length = FFMIN(size - i, width);
  540. memcpy(dst + y*linesize + x * 4, src, length * 4);
  541. src += length * 4;
  542. x += length;
  543. i += length;
  544. if (x >= width) {
  545. x = 0;
  546. y += 1;
  547. if (y >= height)
  548. return;
  549. }
  550. }
  551. } else {
  552. int size = -opcode + 1;
  553. uint32_t pixel = AV_RN32(src);
  554. for (i = 0; i < size; i++) {
  555. *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
  556. x += 1;
  557. if (x >= width) {
  558. x = 0;
  559. y += 1;
  560. if (y >= height)
  561. return;
  562. }
  563. }
  564. src += 4;
  565. }
  566. }
  567. }
  568. /**
  569. * Decode DEEP TVDC 32-bit buffer
  570. * @param[out] dst Destination buffer
  571. * @param[in] src Source buffer
  572. * @param src_size Source buffer size (bytes)
  573. * @param width Width of destination buffer (pixels)
  574. * @param height Height of destination buffer (pixels)
  575. * @param linesize Line size of destination buffer (bytes)
  576. * @param[int] tvdc TVDC lookup table
  577. */
  578. 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)
  579. {
  580. int x = 0, y = 0, plane = 0;
  581. int8_t pixel = 0;
  582. int i, j;
  583. for (i = 0; i < src_size * 2;) {
  584. #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
  585. int d = tvdc[GETNIBBLE];
  586. i++;
  587. if (d) {
  588. pixel += d;
  589. dst[y * linesize + x*4 + plane] = pixel;
  590. x++;
  591. } else {
  592. if (i >= src_size * 2)
  593. return;
  594. d = GETNIBBLE + 1;
  595. i++;
  596. d = FFMIN(d, width - x);
  597. for (j = 0; j < d; j++) {
  598. dst[y * linesize + x*4 + plane] = pixel;
  599. x++;
  600. }
  601. }
  602. if (x >= width) {
  603. plane++;
  604. if (plane >= 4) {
  605. y++;
  606. if (y >= height)
  607. return;
  608. plane = 0;
  609. }
  610. x = 0;
  611. pixel = 0;
  612. i = (i + 1) & ~1;
  613. }
  614. }
  615. }
  616. static int unsupported(AVCodecContext *avctx)
  617. {
  618. IffContext *s = avctx->priv_data;
  619. avpriv_request_sample(avctx, "bitmap (compression %i, bpp %i, ham %i)", s->compression, s->bpp, s->ham);
  620. return AVERROR_INVALIDDATA;
  621. }
  622. static int decode_frame(AVCodecContext *avctx,
  623. void *data, int *got_frame,
  624. AVPacket *avpkt)
  625. {
  626. IffContext *s = avctx->priv_data;
  627. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  628. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  629. const uint8_t *buf_end = buf + buf_size;
  630. int y, plane, res;
  631. GetByteContext gb;
  632. const AVPixFmtDescriptor *desc;
  633. if ((res = extract_header(avctx, avpkt)) < 0)
  634. return res;
  635. if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
  636. return res;
  637. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  638. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  639. avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  640. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame->data[1])) < 0)
  641. return res;
  642. } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  643. avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  644. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  645. return res;
  646. }
  647. s->init = 1;
  648. switch (s->compression) {
  649. case 0:
  650. if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  651. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  652. memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
  653. for (plane = 0; plane < s->bpp; plane++) {
  654. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  655. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  656. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  657. buf += s->planesize;
  658. }
  659. }
  660. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  661. memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
  662. for (y = 0; y < avctx->height; y++) {
  663. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  664. memset(s->ham_buf, 0, s->planesize * 8);
  665. for (plane = 0; plane < s->bpp; plane++) {
  666. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  667. if (start >= buf_end)
  668. break;
  669. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  670. }
  671. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  672. }
  673. } else
  674. return unsupported(avctx);
  675. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  676. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  677. int x;
  678. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  679. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  680. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  681. buf += raw_width;
  682. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  683. for (x = 0; x < avctx->width; x++)
  684. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  685. }
  686. }
  687. } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  688. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  689. for (y = 0; y < avctx->height; y++) {
  690. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  691. memset(row, 0, avctx->width);
  692. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  693. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  694. buf += s->planesize;
  695. }
  696. }
  697. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  698. for (y = 0; y < avctx->height; y++) {
  699. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  700. memset(s->ham_buf, 0, s->planesize * 8);
  701. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  702. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  703. buf += s->planesize;
  704. }
  705. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  706. }
  707. } else { // AV_PIX_FMT_BGR32
  708. for (y = 0; y < avctx->height; y++) {
  709. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  710. memset(row, 0, avctx->width << 2);
  711. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  712. decodeplane32((uint32_t *)row, buf,
  713. FFMIN(s->planesize, buf_end - buf), plane);
  714. buf += s->planesize;
  715. }
  716. }
  717. }
  718. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  719. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  720. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  721. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  722. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  723. buf += avctx->width + (avctx->width % 2); // padding if odd
  724. }
  725. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  726. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  727. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  728. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  729. buf += avctx->width + (avctx->width & 1); // padding if odd
  730. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  731. }
  732. } else
  733. return unsupported(avctx);
  734. }
  735. break;
  736. case 1:
  737. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  738. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  739. for (y = 0; y < avctx->height; y++) {
  740. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  741. memset(row, 0, avctx->width);
  742. for (plane = 0; plane < s->bpp; plane++) {
  743. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  744. decodeplane8(row, s->planebuf, s->planesize, plane);
  745. }
  746. }
  747. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  748. for (y = 0; y < avctx->height; y++) {
  749. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  750. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  751. for (plane = 0; plane < s->bpp; plane++) {
  752. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  753. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  754. }
  755. lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
  756. }
  757. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  758. for (y = 0; y < avctx->height; y++) {
  759. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  760. memset(s->ham_buf, 0, s->planesize * 8);
  761. for (plane = 0; plane < s->bpp; plane++) {
  762. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  763. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  764. }
  765. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  766. }
  767. } else { // AV_PIX_FMT_BGR32
  768. for (y = 0; y < avctx->height; y++) {
  769. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  770. memset(row, 0, avctx->width << 2);
  771. for (plane = 0; plane < s->bpp; plane++) {
  772. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  773. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  774. }
  775. }
  776. }
  777. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  778. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  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(row, avctx->width, buf, buf_end);
  782. }
  783. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  784. for (y = 0; y < avctx->height; y++) {
  785. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  786. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  787. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  788. }
  789. } else
  790. return unsupported(avctx);
  791. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
  792. if (av_get_bits_per_pixel(desc) == 32)
  793. decode_deep_rle32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0]);
  794. else
  795. return unsupported(avctx);
  796. }
  797. break;
  798. case 4:
  799. bytestream2_init(&gb, buf, buf_size);
  800. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8'))
  801. decode_rgb8(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
  802. else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N'))
  803. decode_rgbn(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
  804. else
  805. return unsupported(avctx);
  806. break;
  807. case 5:
  808. if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  809. if (av_get_bits_per_pixel(desc) == 32)
  810. decode_deep_tvdc32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0], s->tvdc);
  811. else
  812. return unsupported(avctx);
  813. } else
  814. return unsupported(avctx);
  815. break;
  816. default:
  817. return unsupported(avctx);
  818. }
  819. if ((res = av_frame_ref(data, s->frame)) < 0)
  820. return res;
  821. *got_frame = 1;
  822. return buf_size;
  823. }
  824. #if CONFIG_IFF_ILBM_DECODER
  825. AVCodec ff_iff_ilbm_decoder = {
  826. .name = "iff",
  827. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  828. .type = AVMEDIA_TYPE_VIDEO,
  829. .id = AV_CODEC_ID_IFF_ILBM,
  830. .priv_data_size = sizeof(IffContext),
  831. .init = decode_init,
  832. .close = decode_end,
  833. .decode = decode_frame,
  834. .capabilities = CODEC_CAP_DR1,
  835. };
  836. #endif
  837. #if CONFIG_IFF_BYTERUN1_DECODER
  838. AVCodec ff_iff_byterun1_decoder = {
  839. .name = "iff",
  840. .long_name = NULL_IF_CONFIG_SMALL("IFF"),
  841. .type = AVMEDIA_TYPE_VIDEO,
  842. .id = AV_CODEC_ID_IFF_BYTERUN1,
  843. .priv_data_size = sizeof(IffContext),
  844. .init = decode_init,
  845. .close = decode_end,
  846. .decode = decode_frame,
  847. .capabilities = CODEC_CAP_DR1,
  848. };
  849. #endif