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.

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