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.

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