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.

1767 lines
65KB

  1. /*
  2. * IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN bitmap decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  5. * Copyright (c) 2016 Paul B Mahol
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN bitmap decoder
  26. */
  27. #include <stdint.h>
  28. #include "libavutil/imgutils.h"
  29. #include "bytestream.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "mathops.h"
  33. // TODO: masking bits
  34. typedef enum {
  35. MASK_NONE,
  36. MASK_HAS_MASK,
  37. MASK_HAS_TRANSPARENT_COLOR,
  38. MASK_LASSO
  39. } mask_type;
  40. typedef struct IffContext {
  41. AVFrame *frame;
  42. int planesize;
  43. uint8_t * planebuf;
  44. uint8_t * ham_buf; ///< temporary buffer for planar to chunky conversation
  45. uint32_t *ham_palbuf; ///< HAM decode table
  46. uint32_t *mask_buf; ///< temporary buffer for palette indices
  47. uint32_t *mask_palbuf; ///< masking palette table
  48. unsigned compression; ///< delta compression method used
  49. unsigned is_short; ///< short compression method used
  50. unsigned is_interlaced;///< video is interlaced
  51. unsigned is_brush; ///< video is in ANBR format
  52. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  53. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  54. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  55. unsigned transparency; ///< TODO: transparency color index in palette
  56. unsigned masking; ///< TODO: masking method used
  57. int init; // 1 if buffer and palette data already initialized, 0 otherwise
  58. int16_t tvdc[16]; ///< TVDC lookup table
  59. GetByteContext gb;
  60. uint8_t *video[2];
  61. unsigned video_size;
  62. uint32_t *pal[2];
  63. } IffContext;
  64. #define LUT8_PART(plane, v) \
  65. AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
  66. AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
  67. AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
  68. AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
  69. AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
  70. AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
  71. AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
  72. AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
  73. AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
  74. AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
  75. AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
  76. AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
  77. AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
  78. AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
  79. AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
  80. AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
  81. #define LUT8(plane) { \
  82. LUT8_PART(plane, 0x0000000), \
  83. LUT8_PART(plane, 0x1000000), \
  84. LUT8_PART(plane, 0x0010000), \
  85. LUT8_PART(plane, 0x1010000), \
  86. LUT8_PART(plane, 0x0000100), \
  87. LUT8_PART(plane, 0x1000100), \
  88. LUT8_PART(plane, 0x0010100), \
  89. LUT8_PART(plane, 0x1010100), \
  90. LUT8_PART(plane, 0x0000001), \
  91. LUT8_PART(plane, 0x1000001), \
  92. LUT8_PART(plane, 0x0010001), \
  93. LUT8_PART(plane, 0x1010001), \
  94. LUT8_PART(plane, 0x0000101), \
  95. LUT8_PART(plane, 0x1000101), \
  96. LUT8_PART(plane, 0x0010101), \
  97. LUT8_PART(plane, 0x1010101), \
  98. }
  99. // 8 planes * 8-bit mask
  100. static const uint64_t plane8_lut[8][256] = {
  101. LUT8(0), LUT8(1), LUT8(2), LUT8(3),
  102. LUT8(4), LUT8(5), LUT8(6), LUT8(7),
  103. };
  104. #define LUT32(plane) { \
  105. 0, 0, 0, 0, \
  106. 0, 0, 0, 1 << plane, \
  107. 0, 0, 1 << plane, 0, \
  108. 0, 0, 1 << plane, 1 << plane, \
  109. 0, 1 << plane, 0, 0, \
  110. 0, 1 << plane, 0, 1 << plane, \
  111. 0, 1 << plane, 1 << plane, 0, \
  112. 0, 1 << plane, 1 << plane, 1 << plane, \
  113. 1 << plane, 0, 0, 0, \
  114. 1 << plane, 0, 0, 1 << plane, \
  115. 1 << plane, 0, 1 << plane, 0, \
  116. 1 << plane, 0, 1 << plane, 1 << plane, \
  117. 1 << plane, 1 << plane, 0, 0, \
  118. 1 << plane, 1 << plane, 0, 1 << plane, \
  119. 1 << plane, 1 << plane, 1 << plane, 0, \
  120. 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
  121. }
  122. // 32 planes * 4-bit mask * 4 lookup tables each
  123. static const uint32_t plane32_lut[32][16*4] = {
  124. LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
  125. LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
  126. LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
  127. LUT32(12), LUT32(13), LUT32(14), LUT32(15),
  128. LUT32(16), LUT32(17), LUT32(18), LUT32(19),
  129. LUT32(20), LUT32(21), LUT32(22), LUT32(23),
  130. LUT32(24), LUT32(25), LUT32(26), LUT32(27),
  131. LUT32(28), LUT32(29), LUT32(30), LUT32(31),
  132. };
  133. // Gray to RGB, required for palette table of grayscale images with bpp < 8
  134. static av_always_inline uint32_t gray2rgb(const uint32_t x) {
  135. return x << 16 | x << 8 | x;
  136. }
  137. /**
  138. * Convert CMAP buffer (stored in extradata) to lavc palette format
  139. */
  140. static int cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  141. {
  142. IffContext *s = avctx->priv_data;
  143. int count, i;
  144. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  145. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  146. if (avctx->bits_per_coded_sample > 8) {
  147. av_log(avctx, AV_LOG_ERROR, "bits_per_coded_sample > 8 not supported\n");
  148. return AVERROR_INVALIDDATA;
  149. }
  150. count = 1 << avctx->bits_per_coded_sample;
  151. // If extradata is smaller than actually needed, fill the remaining with black.
  152. count = FFMIN(palette_size / 3, count);
  153. if (count) {
  154. for (i = 0; i < count; i++)
  155. pal[i] = 0xFF000000 | AV_RB24(palette + i*3);
  156. if (s->flags && count >= 32) { // EHB
  157. for (i = 0; i < 32; i++)
  158. pal[i + 32] = 0xFF000000 | (AV_RB24(palette + i*3) & 0xFEFEFE) >> 1;
  159. count = FFMAX(count, 64);
  160. }
  161. } else { // Create gray-scale color palette for bps < 8
  162. count = 1 << avctx->bits_per_coded_sample;
  163. for (i = 0; i < count; i++)
  164. pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
  165. }
  166. if (s->masking == MASK_HAS_MASK) {
  167. memcpy(pal + (1 << avctx->bits_per_coded_sample), pal, count * 4);
  168. for (i = 0; i < count; i++)
  169. pal[i] &= 0xFFFFFF;
  170. } else if (s->masking == MASK_HAS_TRANSPARENT_COLOR &&
  171. s->transparency < 1 << avctx->bits_per_coded_sample)
  172. pal[s->transparency] &= 0xFFFFFF;
  173. return 0;
  174. }
  175. /**
  176. * Extracts the IFF extra context and updates internal
  177. * decoder structures.
  178. *
  179. * @param avctx the AVCodecContext where to extract extra context to
  180. * @param avpkt the AVPacket to extract extra context from or NULL to use avctx
  181. * @return >= 0 in case of success, a negative error code otherwise
  182. */
  183. static int extract_header(AVCodecContext *const avctx,
  184. const AVPacket *const avpkt)
  185. {
  186. IffContext *s = avctx->priv_data;
  187. const uint8_t *buf;
  188. unsigned buf_size = 0;
  189. int i, palette_size;
  190. if (avctx->extradata_size < 2) {
  191. av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
  192. return AVERROR_INVALIDDATA;
  193. }
  194. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  195. if (avpkt && avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  196. uint32_t chunk_id;
  197. uint64_t data_size;
  198. GetByteContext *gb = &s->gb;
  199. bytestream2_skip(gb, 4);
  200. while (bytestream2_get_bytes_left(gb) >= 1) {
  201. chunk_id = bytestream2_get_le32(gb);
  202. data_size = bytestream2_get_be32(gb);
  203. if (chunk_id == MKTAG('B', 'M', 'H', 'D')) {
  204. bytestream2_skip(gb, data_size + (data_size & 1));
  205. } else if (chunk_id == MKTAG('A', 'N', 'H', 'D')) {
  206. unsigned extra;
  207. if (data_size < 40)
  208. return AVERROR_INVALIDDATA;
  209. s->compression = (bytestream2_get_byte(gb) << 8) | (s->compression & 0xFF);
  210. bytestream2_skip(gb, 19);
  211. extra = bytestream2_get_be32(gb);
  212. s->is_short = !(extra & 1);
  213. s->is_brush = extra == 2;
  214. s->is_interlaced = !!(extra & 0x40);
  215. data_size -= 24;
  216. bytestream2_skip(gb, data_size + (data_size & 1));
  217. } else if (chunk_id == MKTAG('D', 'L', 'T', 'A') ||
  218. chunk_id == MKTAG('B', 'O', 'D', 'Y')) {
  219. if (chunk_id == MKTAG('B','O','D','Y'))
  220. s->compression &= 0xFF;
  221. break;
  222. } else if (chunk_id == MKTAG('C', 'M', 'A', 'P')) {
  223. int count = data_size / 3;
  224. uint32_t *pal = s->pal[0];
  225. if (count > 256)
  226. return AVERROR_INVALIDDATA;
  227. if (s->ham) {
  228. for (i = 0; i < count; i++)
  229. pal[i] = 0xFF000000 | bytestream2_get_le24(gb);
  230. } else {
  231. for (i = 0; i < count; i++)
  232. pal[i] = 0xFF000000 | bytestream2_get_be24(gb);
  233. }
  234. bytestream2_skip(gb, data_size & 1);
  235. } else {
  236. bytestream2_skip(gb, data_size + (data_size&1));
  237. }
  238. }
  239. } else if (!avpkt) {
  240. buf = avctx->extradata;
  241. buf_size = bytestream_get_be16(&buf);
  242. if (buf_size <= 1 || palette_size < 0) {
  243. av_log(avctx, AV_LOG_ERROR,
  244. "Invalid palette size received: %u -> palette data offset: %d\n",
  245. buf_size, palette_size);
  246. return AVERROR_INVALIDDATA;
  247. }
  248. }
  249. if (buf_size >= 41) {
  250. s->compression = bytestream_get_byte(&buf);
  251. s->bpp = bytestream_get_byte(&buf);
  252. s->ham = bytestream_get_byte(&buf);
  253. s->flags = bytestream_get_byte(&buf);
  254. s->transparency = bytestream_get_be16(&buf);
  255. s->masking = bytestream_get_byte(&buf);
  256. for (i = 0; i < 16; i++)
  257. s->tvdc[i] = bytestream_get_be16(&buf);
  258. if (s->masking == MASK_HAS_MASK) {
  259. if (s->bpp >= 8 && !s->ham) {
  260. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  261. av_freep(&s->mask_buf);
  262. av_freep(&s->mask_palbuf);
  263. s->mask_buf = av_malloc((s->planesize * 32) + AV_INPUT_BUFFER_PADDING_SIZE);
  264. if (!s->mask_buf)
  265. return AVERROR(ENOMEM);
  266. if (s->bpp > 16) {
  267. av_log(avctx, AV_LOG_ERROR, "bpp %d too large for palette\n", s->bpp);
  268. av_freep(&s->mask_buf);
  269. return AVERROR(ENOMEM);
  270. }
  271. s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + AV_INPUT_BUFFER_PADDING_SIZE);
  272. if (!s->mask_palbuf) {
  273. av_freep(&s->mask_buf);
  274. return AVERROR(ENOMEM);
  275. }
  276. }
  277. s->bpp++;
  278. } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {
  279. av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
  280. return AVERROR_PATCHWELCOME;
  281. }
  282. if (!s->bpp || s->bpp > 32) {
  283. av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
  284. return AVERROR_INVALIDDATA;
  285. } else if (s->ham >= 8) {
  286. av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
  287. return AVERROR_INVALIDDATA;
  288. }
  289. av_freep(&s->ham_buf);
  290. av_freep(&s->ham_palbuf);
  291. if (s->ham) {
  292. int i, count = FFMIN(palette_size / 3, 1 << s->ham);
  293. int ham_count;
  294. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  295. s->ham_buf = av_malloc((s->planesize * 8) + AV_INPUT_BUFFER_PADDING_SIZE);
  296. if (!s->ham_buf)
  297. return AVERROR(ENOMEM);
  298. ham_count = 8 * (1 << s->ham);
  299. s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + AV_INPUT_BUFFER_PADDING_SIZE);
  300. if (!s->ham_palbuf) {
  301. av_freep(&s->ham_buf);
  302. return AVERROR(ENOMEM);
  303. }
  304. if (count) { // HAM with color palette attached
  305. // prefill with black and palette and set HAM take direct value mask to zero
  306. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
  307. for (i=0; i < count; i++) {
  308. s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);
  309. }
  310. count = 1 << s->ham;
  311. } else { // HAM with grayscale color palette
  312. count = 1 << s->ham;
  313. for (i=0; i < count; i++) {
  314. s->ham_palbuf[i*2] = 0xFF000000; // take direct color value from palette
  315. s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));
  316. }
  317. }
  318. for (i=0; i < count; i++) {
  319. uint32_t tmp = i << (8 - s->ham);
  320. tmp |= tmp >> s->ham;
  321. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF; // just modify blue color component
  322. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00; // just modify red color component
  323. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF; // just modify green color component
  324. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  325. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  326. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  327. }
  328. if (s->masking == MASK_HAS_MASK) {
  329. for (i = 0; i < ham_count; i++)
  330. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  331. }
  332. }
  333. }
  334. return 0;
  335. }
  336. static av_cold int decode_end(AVCodecContext *avctx)
  337. {
  338. IffContext *s = avctx->priv_data;
  339. av_freep(&s->planebuf);
  340. av_freep(&s->ham_buf);
  341. av_freep(&s->ham_palbuf);
  342. av_freep(&s->video[0]);
  343. av_freep(&s->video[1]);
  344. av_freep(&s->pal[0]);
  345. av_freep(&s->pal[1]);
  346. return 0;
  347. }
  348. static av_cold int decode_init(AVCodecContext *avctx)
  349. {
  350. IffContext *s = avctx->priv_data;
  351. int err;
  352. if (avctx->bits_per_coded_sample <= 8) {
  353. int palette_size;
  354. if (avctx->extradata_size >= 2)
  355. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  356. else
  357. palette_size = 0;
  358. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  359. (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  360. } else if (avctx->bits_per_coded_sample <= 32) {
  361. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8')) {
  362. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  363. } else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N')) {
  364. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  365. } else if (avctx->codec_tag != MKTAG('D', 'E', 'E', 'P')) {
  366. if (avctx->bits_per_coded_sample == 24) {
  367. avctx->pix_fmt = AV_PIX_FMT_0BGR32;
  368. } else if (avctx->bits_per_coded_sample == 32) {
  369. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  370. } else {
  371. avpriv_request_sample(avctx, "unknown bits_per_coded_sample");
  372. return AVERROR_PATCHWELCOME;
  373. }
  374. }
  375. } else {
  376. return AVERROR_INVALIDDATA;
  377. }
  378. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  379. return err;
  380. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  381. s->planebuf = av_malloc(s->planesize + AV_INPUT_BUFFER_PADDING_SIZE);
  382. if (!s->planebuf)
  383. return AVERROR(ENOMEM);
  384. s->bpp = avctx->bits_per_coded_sample;
  385. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  386. s->video_size = FFALIGN(avctx->width, 2) * avctx->height * s->bpp;
  387. s->video[0] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
  388. s->video[1] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
  389. s->pal[0] = av_calloc(256, sizeof(*s->pal[0]));
  390. s->pal[1] = av_calloc(256, sizeof(*s->pal[1]));
  391. if (!s->video[0] || !s->video[1] || !s->pal[0] || !s->pal[1])
  392. return AVERROR(ENOMEM);
  393. }
  394. if ((err = extract_header(avctx, NULL)) < 0)
  395. return err;
  396. return 0;
  397. }
  398. /**
  399. * Decode interleaved plane buffer up to 8bpp
  400. * @param dst Destination buffer
  401. * @param buf Source buffer
  402. * @param buf_size
  403. * @param plane plane number to decode as
  404. */
  405. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  406. {
  407. const uint64_t *lut = plane8_lut[plane];
  408. if (plane >= 8) {
  409. av_log(NULL, AV_LOG_WARNING, "Ignoring extra planes beyond 8\n");
  410. return;
  411. }
  412. do {
  413. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  414. AV_WN64A(dst, v);
  415. dst += 8;
  416. } while (--buf_size);
  417. }
  418. /**
  419. * Decode interleaved plane buffer up to 24bpp
  420. * @param dst Destination buffer
  421. * @param buf Source buffer
  422. * @param buf_size
  423. * @param plane plane number to decode as
  424. */
  425. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  426. {
  427. const uint32_t *lut = plane32_lut[plane];
  428. do {
  429. unsigned mask = (*buf >> 2) & ~3;
  430. dst[0] |= lut[mask++];
  431. dst[1] |= lut[mask++];
  432. dst[2] |= lut[mask++];
  433. dst[3] |= lut[mask];
  434. mask = (*buf++ << 2) & 0x3F;
  435. dst[4] |= lut[mask++];
  436. dst[5] |= lut[mask++];
  437. dst[6] |= lut[mask++];
  438. dst[7] |= lut[mask];
  439. dst += 8;
  440. } while (--buf_size);
  441. }
  442. #define DECODE_HAM_PLANE32(x) \
  443. first = buf[x] << 1; \
  444. second = buf[(x)+1] << 1; \
  445. delta &= pal[first++]; \
  446. delta |= pal[first]; \
  447. dst[x] = delta; \
  448. delta &= pal[second++]; \
  449. delta |= pal[second]; \
  450. dst[(x)+1] = delta
  451. /**
  452. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  453. *
  454. * @param dst the destination 24bpp buffer
  455. * @param buf the source 8bpp chunky buffer
  456. * @param pal the HAM decode table
  457. * @param buf_size the plane size in bytes
  458. */
  459. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  460. const uint32_t *const pal, unsigned buf_size)
  461. {
  462. uint32_t delta = pal[1]; /* first palette entry */
  463. do {
  464. uint32_t first, second;
  465. DECODE_HAM_PLANE32(0);
  466. DECODE_HAM_PLANE32(2);
  467. DECODE_HAM_PLANE32(4);
  468. DECODE_HAM_PLANE32(6);
  469. buf += 8;
  470. dst += 8;
  471. } while (--buf_size);
  472. }
  473. static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
  474. const uint32_t *const pal, unsigned width)
  475. {
  476. do {
  477. *dst++ = pal[*buf++];
  478. } while (--width);
  479. }
  480. /**
  481. * Decode one complete byterun1 encoded line.
  482. *
  483. * @param dst the destination buffer where to store decompressed bitstream
  484. * @param dst_size the destination plane size in bytes
  485. * @param buf the source byterun1 compressed bitstream
  486. * @param buf_end the EOF of source byterun1 compressed bitstream
  487. * @return number of consumed bytes in byterun1 compressed bitstream
  488. */
  489. static int decode_byterun(uint8_t *dst, int dst_size,
  490. GetByteContext *gb)
  491. {
  492. unsigned x;
  493. for (x = 0; x < dst_size && bytestream2_get_bytes_left(gb) > 0;) {
  494. unsigned length;
  495. const int8_t value = bytestream2_get_byte(gb);
  496. if (value >= 0) {
  497. length = FFMIN3(value + 1, dst_size - x, bytestream2_get_bytes_left(gb));
  498. bytestream2_get_buffer(gb, dst + x, length);
  499. if (length < value + 1)
  500. bytestream2_skip(gb, value + 1 - length);
  501. } else if (value > -128) {
  502. length = FFMIN(-value + 1, dst_size - x);
  503. memset(dst + x, bytestream2_get_byte(gb), length);
  504. } else { // noop
  505. continue;
  506. }
  507. x += length;
  508. }
  509. if (x < dst_size) {
  510. av_log(NULL, AV_LOG_WARNING, "decode_byterun ended before plane size\n");
  511. memset(dst+x, 0, dst_size - x);
  512. }
  513. return bytestream2_tell(gb);
  514. }
  515. #define DECODE_RGBX_COMMON(type) \
  516. if (!length) { \
  517. length = bytestream2_get_byte(gb); \
  518. if (!length) { \
  519. length = bytestream2_get_be16(gb); \
  520. if (!length) \
  521. return; \
  522. } \
  523. } \
  524. for (i = 0; i < length; i++) { \
  525. *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
  526. x += 1; \
  527. if (x >= width) { \
  528. y += 1; \
  529. if (y >= height) \
  530. return; \
  531. x = 0; \
  532. } \
  533. }
  534. /**
  535. * Decode RGB8 buffer
  536. * @param[out] dst Destination buffer
  537. * @param width Width of destination buffer (pixels)
  538. * @param height Height of destination buffer (pixels)
  539. * @param linesize Line size of destination buffer (bytes)
  540. */
  541. static void decode_rgb8(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  542. {
  543. int x = 0, y = 0, i, length;
  544. while (bytestream2_get_bytes_left(gb) >= 4) {
  545. uint32_t pixel = 0xFF000000 | bytestream2_get_be24(gb);
  546. length = bytestream2_get_byte(gb) & 0x7F;
  547. DECODE_RGBX_COMMON(uint32_t)
  548. }
  549. }
  550. /**
  551. * Decode RGBN buffer
  552. * @param[out] dst Destination buffer
  553. * @param width Width of destination buffer (pixels)
  554. * @param height Height of destination buffer (pixels)
  555. * @param linesize Line size of destination buffer (bytes)
  556. */
  557. static void decode_rgbn(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
  558. {
  559. int x = 0, y = 0, i, length;
  560. while (bytestream2_get_bytes_left(gb) >= 2) {
  561. uint32_t pixel = bytestream2_get_be16u(gb);
  562. length = pixel & 0x7;
  563. pixel >>= 4;
  564. DECODE_RGBX_COMMON(uint16_t)
  565. }
  566. }
  567. /**
  568. * Decode DEEP RLE 32-bit buffer
  569. * @param[out] dst Destination buffer
  570. * @param[in] src Source buffer
  571. * @param src_size Source buffer size (bytes)
  572. * @param width Width of destination buffer (pixels)
  573. * @param height Height of destination buffer (pixels)
  574. * @param linesize Line size of destination buffer (bytes)
  575. */
  576. static void decode_deep_rle32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
  577. {
  578. const uint8_t *src_end = src + src_size;
  579. int x = 0, y = 0, i;
  580. while (src + 5 <= src_end) {
  581. int opcode;
  582. opcode = *(int8_t *)src++;
  583. if (opcode >= 0) {
  584. int size = opcode + 1;
  585. for (i = 0; i < size; i++) {
  586. int length = FFMIN(size - i, width);
  587. memcpy(dst + y*linesize + x * 4, src, length * 4);
  588. src += length * 4;
  589. x += length;
  590. i += length;
  591. if (x >= width) {
  592. x = 0;
  593. y += 1;
  594. if (y >= height)
  595. return;
  596. }
  597. }
  598. } else {
  599. int size = -opcode + 1;
  600. uint32_t pixel = AV_RN32(src);
  601. for (i = 0; i < size; i++) {
  602. *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
  603. x += 1;
  604. if (x >= width) {
  605. x = 0;
  606. y += 1;
  607. if (y >= height)
  608. return;
  609. }
  610. }
  611. src += 4;
  612. }
  613. }
  614. }
  615. /**
  616. * Decode DEEP TVDC 32-bit buffer
  617. * @param[out] dst Destination buffer
  618. * @param[in] src Source buffer
  619. * @param src_size Source buffer size (bytes)
  620. * @param width Width of destination buffer (pixels)
  621. * @param height Height of destination buffer (pixels)
  622. * @param linesize Line size of destination buffer (bytes)
  623. * @param[int] tvdc TVDC lookup table
  624. */
  625. 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)
  626. {
  627. int x = 0, y = 0, plane = 0;
  628. int8_t pixel = 0;
  629. int i, j;
  630. for (i = 0; i < src_size * 2;) {
  631. #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
  632. int d = tvdc[GETNIBBLE];
  633. i++;
  634. if (d) {
  635. pixel += d;
  636. dst[y * linesize + x*4 + plane] = pixel;
  637. x++;
  638. } else {
  639. if (i >= src_size * 2)
  640. return;
  641. d = GETNIBBLE + 1;
  642. i++;
  643. d = FFMIN(d, width - x);
  644. for (j = 0; j < d; j++) {
  645. dst[y * linesize + x*4 + plane] = pixel;
  646. x++;
  647. }
  648. }
  649. if (x >= width) {
  650. plane++;
  651. if (plane >= 4) {
  652. y++;
  653. if (y >= height)
  654. return;
  655. plane = 0;
  656. }
  657. x = 0;
  658. pixel = 0;
  659. i = (i + 1) & ~1;
  660. }
  661. }
  662. }
  663. static void decode_short_horizontal_delta(uint8_t *dst,
  664. const uint8_t *buf, const uint8_t *buf_end,
  665. int w, int bpp, int dst_size)
  666. {
  667. int planepitch = FFALIGN(w, 16) >> 3;
  668. int pitch = planepitch * bpp;
  669. GetByteContext ptrs, gb;
  670. PutByteContext pb;
  671. unsigned ofssrc, pos;
  672. int i, k;
  673. bytestream2_init(&ptrs, buf, buf_end - buf);
  674. bytestream2_init_writer(&pb, dst, dst_size);
  675. for (k = 0; k < bpp; k++) {
  676. ofssrc = bytestream2_get_be32(&ptrs);
  677. pos = 0;
  678. if (!ofssrc)
  679. continue;
  680. if (ofssrc >= buf_end - buf)
  681. continue;
  682. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  683. while (bytestream2_peek_be16(&gb) != 0xFFFF && bytestream2_get_bytes_left(&gb) > 3) {
  684. int16_t offset = bytestream2_get_be16(&gb);
  685. unsigned noffset;
  686. if (offset >= 0) {
  687. unsigned data = bytestream2_get_be16(&gb);
  688. pos += offset * 2;
  689. noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
  690. bytestream2_seek_p(&pb, noffset, SEEK_SET);
  691. bytestream2_put_be16(&pb, data);
  692. } else {
  693. uint16_t count = bytestream2_get_be16(&gb);
  694. pos += 2 * -(offset + 2);
  695. for (i = 0; i < count; i++) {
  696. uint16_t data = bytestream2_get_be16(&gb);
  697. pos += 2;
  698. noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
  699. bytestream2_seek_p(&pb, noffset, SEEK_SET);
  700. bytestream2_put_be16(&pb, data);
  701. }
  702. }
  703. }
  704. }
  705. }
  706. static void decode_byte_vertical_delta(uint8_t *dst,
  707. const uint8_t *buf, const uint8_t *buf_end,
  708. int w, int xor, int bpp, int dst_size)
  709. {
  710. int ncolumns = ((w + 15) / 16) * 2;
  711. int dstpitch = ncolumns * bpp;
  712. unsigned ofsdst, ofssrc, opcode, x;
  713. GetByteContext ptrs, gb;
  714. PutByteContext pb;
  715. int i, j, k;
  716. bytestream2_init(&ptrs, buf, buf_end - buf);
  717. bytestream2_init_writer(&pb, dst, dst_size);
  718. for (k = 0; k < bpp; k++) {
  719. ofssrc = bytestream2_get_be32(&ptrs);
  720. if (!ofssrc)
  721. continue;
  722. if (ofssrc >= buf_end - buf)
  723. continue;
  724. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  725. for (j = 0; j < ncolumns; j++) {
  726. ofsdst = j + k * ncolumns;
  727. i = bytestream2_get_byte(&gb);
  728. while (i > 0) {
  729. opcode = bytestream2_get_byte(&gb);
  730. if (opcode == 0) {
  731. opcode = bytestream2_get_byte(&gb);
  732. x = bytestream2_get_byte(&gb);
  733. while (opcode) {
  734. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  735. if (xor && ofsdst < dst_size) {
  736. bytestream2_put_byte(&pb, dst[ofsdst] ^ x);
  737. } else {
  738. bytestream2_put_byte(&pb, x);
  739. }
  740. ofsdst += dstpitch;
  741. opcode--;
  742. }
  743. } else if (opcode < 0x80) {
  744. ofsdst += opcode * dstpitch;
  745. } else {
  746. opcode &= 0x7f;
  747. while (opcode) {
  748. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  749. if (xor && ofsdst < dst_size) {
  750. bytestream2_put_byte(&pb, dst[ofsdst] ^ bytestream2_get_byte(&gb));
  751. } else {
  752. bytestream2_put_byte(&pb, bytestream2_get_byte(&gb));
  753. }
  754. ofsdst += dstpitch;
  755. opcode--;
  756. }
  757. }
  758. i--;
  759. }
  760. }
  761. }
  762. }
  763. static void decode_delta_j(uint8_t *dst,
  764. const uint8_t *buf, const uint8_t *buf_end,
  765. int w, int h, int bpp, int dst_size)
  766. {
  767. int32_t pitch;
  768. uint8_t *ptr;
  769. uint32_t type, flag, cols, groups, rows, bytes;
  770. uint32_t offset;
  771. int planepitch_byte = (w + 7) / 8;
  772. int planepitch = ((w + 15) / 16) * 2;
  773. int kludge_j, b, g, r, d;
  774. GetByteContext gb;
  775. pitch = planepitch * bpp;
  776. kludge_j = w < 320 ? (320 - w) / 8 / 2 : 0;
  777. bytestream2_init(&gb, buf, buf_end - buf);
  778. while (bytestream2_get_bytes_left(&gb) >= 2) {
  779. type = bytestream2_get_be16(&gb);
  780. switch (type) {
  781. case 0:
  782. return;
  783. case 1:
  784. flag = bytestream2_get_be16(&gb);
  785. cols = bytestream2_get_be16(&gb);
  786. groups = bytestream2_get_be16(&gb);
  787. for (g = 0; g < groups; g++) {
  788. offset = bytestream2_get_be16(&gb);
  789. if (cols * bpp == 0 || bytestream2_get_bytes_left(&gb) < cols * bpp)
  790. return;
  791. if (kludge_j)
  792. offset = ((offset / (320 / 8)) * pitch) + (offset % (320 / 8)) - kludge_j;
  793. else
  794. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  795. for (b = 0; b < cols; b++) {
  796. for (d = 0; d < bpp; d++) {
  797. uint8_t value = bytestream2_get_byte(&gb);
  798. if (offset >= dst_size)
  799. return;
  800. ptr = dst + offset;
  801. if (flag)
  802. ptr[0] ^= value;
  803. else
  804. ptr[0] = value;
  805. offset += planepitch;
  806. }
  807. }
  808. if ((cols * bpp) & 1)
  809. bytestream2_skip(&gb, 1);
  810. }
  811. break;
  812. case 2:
  813. flag = bytestream2_get_be16(&gb);
  814. rows = bytestream2_get_be16(&gb);
  815. bytes = bytestream2_get_be16(&gb);
  816. groups = bytestream2_get_be16(&gb);
  817. for (g = 0; g < groups; g++) {
  818. offset = bytestream2_get_be16(&gb);
  819. if (kludge_j)
  820. offset = ((offset / (320 / 8)) * pitch) + (offset % (320/ 8)) - kludge_j;
  821. else
  822. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  823. for (r = 0; r < rows; r++) {
  824. for (d = 0; d < bpp; d++) {
  825. unsigned noffset = offset + (r * pitch) + d * planepitch;
  826. if (!bytes || bytestream2_get_bytes_left(&gb) < bytes)
  827. return;
  828. for (b = 0; b < bytes; b++) {
  829. uint8_t value = bytestream2_get_byte(&gb);
  830. if (noffset >= dst_size)
  831. return;
  832. ptr = dst + noffset;
  833. if (flag)
  834. ptr[0] ^= value;
  835. else
  836. ptr[0] = value;
  837. noffset++;
  838. }
  839. }
  840. }
  841. if ((rows * bytes * bpp) & 1)
  842. bytestream2_skip(&gb, 1);
  843. }
  844. break;
  845. default:
  846. return;
  847. }
  848. }
  849. }
  850. static void decode_short_vertical_delta(uint8_t *dst,
  851. const uint8_t *buf, const uint8_t *buf_end,
  852. int w, int bpp, int dst_size)
  853. {
  854. int ncolumns = (w + 15) >> 4;
  855. int dstpitch = ncolumns * bpp * 2;
  856. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  857. GetByteContext ptrs, gb, dptrs, dgb;
  858. PutByteContext pb;
  859. int i, j, k;
  860. if (buf_end - buf <= 64)
  861. return;
  862. bytestream2_init(&ptrs, buf, buf_end - buf);
  863. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  864. bytestream2_init_writer(&pb, dst, dst_size);
  865. for (k = 0; k < bpp; k++) {
  866. ofssrc = bytestream2_get_be32(&ptrs);
  867. ofsdata = bytestream2_get_be32(&dptrs);
  868. if (!ofssrc)
  869. continue;
  870. if (ofssrc >= buf_end - buf)
  871. return;
  872. if (ofsdata >= buf_end - buf)
  873. return;
  874. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  875. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  876. for (j = 0; j < ncolumns; j++) {
  877. ofsdst = (j + k * ncolumns) * 2;
  878. i = bytestream2_get_byte(&gb);
  879. while (i > 0) {
  880. opcode = bytestream2_get_byte(&gb);
  881. if (opcode == 0) {
  882. opcode = bytestream2_get_byte(&gb);
  883. x = bytestream2_get_be16(&dgb);
  884. while (opcode) {
  885. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  886. bytestream2_put_be16(&pb, x);
  887. ofsdst += dstpitch;
  888. opcode--;
  889. }
  890. } else if (opcode < 0x80) {
  891. ofsdst += opcode * dstpitch;
  892. } else {
  893. opcode &= 0x7f;
  894. while (opcode) {
  895. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  896. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  897. ofsdst += dstpitch;
  898. opcode--;
  899. }
  900. }
  901. i--;
  902. }
  903. }
  904. }
  905. }
  906. static void decode_long_vertical_delta(uint8_t *dst,
  907. const uint8_t *buf, const uint8_t *buf_end,
  908. int w, int bpp, int dst_size)
  909. {
  910. int ncolumns = (w + 31) >> 5;
  911. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  912. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  913. GetByteContext ptrs, gb, dptrs, dgb;
  914. PutByteContext pb;
  915. int i, j, k, h;
  916. if (buf_end - buf <= 64)
  917. return;
  918. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  919. bytestream2_init(&ptrs, buf, buf_end - buf);
  920. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  921. bytestream2_init_writer(&pb, dst, dst_size);
  922. for (k = 0; k < bpp; k++) {
  923. ofssrc = bytestream2_get_be32(&ptrs);
  924. ofsdata = bytestream2_get_be32(&dptrs);
  925. if (!ofssrc)
  926. continue;
  927. if (ofssrc >= buf_end - buf)
  928. return;
  929. if (ofsdata >= buf_end - buf)
  930. return;
  931. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  932. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  933. for (j = 0; j < ncolumns; j++) {
  934. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  935. i = bytestream2_get_byte(&gb);
  936. while (i > 0) {
  937. opcode = bytestream2_get_byte(&gb);
  938. if (opcode == 0) {
  939. opcode = bytestream2_get_byte(&gb);
  940. if (h && (j == (ncolumns - 1))) {
  941. x = bytestream2_get_be16(&dgb);
  942. bytestream2_skip(&dgb, 2);
  943. } else {
  944. x = bytestream2_get_be32(&dgb);
  945. }
  946. while (opcode) {
  947. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  948. if (h && (j == (ncolumns - 1))) {
  949. bytestream2_put_be16(&pb, x);
  950. } else {
  951. bytestream2_put_be32(&pb, x);
  952. }
  953. ofsdst += dstpitch;
  954. opcode--;
  955. }
  956. } else if (opcode < 0x80) {
  957. ofsdst += opcode * dstpitch;
  958. } else {
  959. opcode &= 0x7f;
  960. while (opcode) {
  961. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  962. if (h && (j == (ncolumns - 1))) {
  963. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  964. bytestream2_skip(&dgb, 2);
  965. } else {
  966. bytestream2_put_be32(&pb, bytestream2_get_be32(&dgb));
  967. }
  968. ofsdst += dstpitch;
  969. opcode--;
  970. }
  971. }
  972. i--;
  973. }
  974. }
  975. }
  976. }
  977. static void decode_short_vertical_delta2(uint8_t *dst,
  978. const uint8_t *buf, const uint8_t *buf_end,
  979. int w, int bpp, int dst_size)
  980. {
  981. int ncolumns = (w + 15) >> 4;
  982. int dstpitch = ncolumns * bpp * 2;
  983. unsigned ofsdst, ofssrc, opcode, x;
  984. GetByteContext ptrs, gb;
  985. PutByteContext pb;
  986. int i, j, k;
  987. bytestream2_init(&ptrs, buf, buf_end - buf);
  988. bytestream2_init_writer(&pb, dst, dst_size);
  989. for (k = 0; k < bpp; k++) {
  990. ofssrc = bytestream2_get_be32(&ptrs);
  991. if (!ofssrc)
  992. continue;
  993. if (ofssrc >= buf_end - buf)
  994. continue;
  995. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  996. for (j = 0; j < ncolumns; j++) {
  997. ofsdst = (j + k * ncolumns) * 2;
  998. i = bytestream2_get_be16(&gb);
  999. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1000. opcode = bytestream2_get_be16(&gb);
  1001. if (opcode == 0) {
  1002. opcode = bytestream2_get_be16(&gb);
  1003. x = bytestream2_get_be16(&gb);
  1004. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1005. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1006. bytestream2_put_be16(&pb, x);
  1007. ofsdst += dstpitch;
  1008. opcode--;
  1009. }
  1010. } else if (opcode < 0x8000) {
  1011. ofsdst += opcode * dstpitch;
  1012. } else {
  1013. opcode &= 0x7fff;
  1014. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1015. bytestream2_get_bytes_left_p(&pb) > 1) {
  1016. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1017. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1018. ofsdst += dstpitch;
  1019. opcode--;
  1020. }
  1021. }
  1022. i--;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. static void decode_long_vertical_delta2(uint8_t *dst,
  1028. const uint8_t *buf, const uint8_t *buf_end,
  1029. int w, int bpp, int dst_size)
  1030. {
  1031. int ncolumns = (w + 31) >> 5;
  1032. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  1033. unsigned ofsdst, ofssrc, opcode, x;
  1034. unsigned skip = 0x80000000, mask = skip - 1;
  1035. GetByteContext ptrs, gb;
  1036. PutByteContext pb;
  1037. int i, j, k, h;
  1038. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  1039. bytestream2_init(&ptrs, buf, buf_end - buf);
  1040. bytestream2_init_writer(&pb, dst, dst_size);
  1041. for (k = 0; k < bpp; k++) {
  1042. ofssrc = bytestream2_get_be32(&ptrs);
  1043. if (!ofssrc)
  1044. continue;
  1045. if (ofssrc >= buf_end - buf)
  1046. continue;
  1047. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1048. for (j = 0; j < ncolumns; j++) {
  1049. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  1050. if (h && (j == (ncolumns - 1))) {
  1051. skip = 0x8000;
  1052. mask = skip - 1;
  1053. }
  1054. i = bytestream2_get_be32(&gb);
  1055. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1056. opcode = bytestream2_get_be32(&gb);
  1057. if (opcode == 0) {
  1058. if (h && (j == ncolumns - 1)) {
  1059. opcode = bytestream2_get_be16(&gb);
  1060. x = bytestream2_get_be16(&gb);
  1061. } else {
  1062. opcode = bytestream2_get_be32(&gb);
  1063. x = bytestream2_get_be32(&gb);
  1064. }
  1065. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1066. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1067. if (h && (j == ncolumns - 1))
  1068. bytestream2_put_be16(&pb, x);
  1069. else
  1070. bytestream2_put_be32(&pb, x);
  1071. ofsdst += dstpitch;
  1072. opcode--;
  1073. }
  1074. } else if (opcode < skip) {
  1075. ofsdst += opcode * dstpitch;
  1076. } else {
  1077. opcode &= mask;
  1078. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1079. bytestream2_get_bytes_left_p(&pb) > 1) {
  1080. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1081. if (h && (j == ncolumns - 1)) {
  1082. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1083. } else {
  1084. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1085. }
  1086. ofsdst += dstpitch;
  1087. opcode--;
  1088. }
  1089. }
  1090. i--;
  1091. }
  1092. }
  1093. }
  1094. }
  1095. static void decode_delta_d(uint8_t *dst,
  1096. const uint8_t *buf, const uint8_t *buf_end,
  1097. int w, int flag, int bpp, int dst_size)
  1098. {
  1099. int planepitch = FFALIGN(w, 16) >> 3;
  1100. int pitch = planepitch * bpp;
  1101. int planepitch_byte = (w + 7) / 8;
  1102. unsigned entries, ofssrc;
  1103. GetByteContext gb, ptrs;
  1104. PutByteContext pb;
  1105. int k;
  1106. if (buf_end - buf <= 4 * bpp)
  1107. return;
  1108. bytestream2_init_writer(&pb, dst, dst_size);
  1109. bytestream2_init(&ptrs, buf, bpp * 4);
  1110. for (k = 0; k < bpp; k++) {
  1111. ofssrc = bytestream2_get_be32(&ptrs);
  1112. if (!ofssrc)
  1113. continue;
  1114. if (ofssrc >= buf_end - buf)
  1115. continue;
  1116. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1117. entries = bytestream2_get_be32(&gb);
  1118. while (entries && bytestream2_get_bytes_left(&gb) >= 8) {
  1119. int32_t opcode = bytestream2_get_be32(&gb);
  1120. unsigned offset = bytestream2_get_be32(&gb);
  1121. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1122. if (opcode >= 0) {
  1123. uint32_t x = bytestream2_get_be32(&gb);
  1124. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1125. bytestream2_put_be32(&pb, x);
  1126. bytestream2_skip_p(&pb, pitch - 4);
  1127. opcode--;
  1128. }
  1129. } else {
  1130. opcode = -opcode;
  1131. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1132. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1133. bytestream2_skip_p(&pb, pitch - 4);
  1134. opcode--;
  1135. }
  1136. }
  1137. entries--;
  1138. }
  1139. }
  1140. }
  1141. static void decode_delta_e(uint8_t *dst,
  1142. const uint8_t *buf, const uint8_t *buf_end,
  1143. int w, int flag, int bpp, int dst_size)
  1144. {
  1145. int planepitch = FFALIGN(w, 16) >> 3;
  1146. int pitch = planepitch * bpp;
  1147. int planepitch_byte = (w + 7) / 8;
  1148. unsigned entries, ofssrc;
  1149. GetByteContext gb, ptrs;
  1150. PutByteContext pb;
  1151. int k;
  1152. if (buf_end - buf <= 4 * bpp)
  1153. return;
  1154. bytestream2_init_writer(&pb, dst, dst_size);
  1155. bytestream2_init(&ptrs, buf, bpp * 4);
  1156. for (k = 0; k < bpp; k++) {
  1157. ofssrc = bytestream2_get_be32(&ptrs);
  1158. if (!ofssrc)
  1159. continue;
  1160. if (ofssrc >= buf_end - buf)
  1161. continue;
  1162. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1163. entries = bytestream2_get_be16(&gb);
  1164. while (entries && bytestream2_get_bytes_left(&gb) >= 6) {
  1165. int16_t opcode = bytestream2_get_be16(&gb);
  1166. unsigned offset = bytestream2_get_be32(&gb);
  1167. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1168. if (opcode >= 0) {
  1169. uint16_t x = bytestream2_get_be16(&gb);
  1170. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1171. bytestream2_put_be16(&pb, x);
  1172. bytestream2_skip_p(&pb, pitch - 2);
  1173. opcode--;
  1174. }
  1175. } else {
  1176. opcode = -opcode;
  1177. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1178. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1179. bytestream2_skip_p(&pb, pitch - 2);
  1180. opcode--;
  1181. }
  1182. }
  1183. entries--;
  1184. }
  1185. }
  1186. }
  1187. static void decode_delta_l(uint8_t *dst,
  1188. const uint8_t *buf, const uint8_t *buf_end,
  1189. int w, int flag, int bpp, int dst_size)
  1190. {
  1191. GetByteContext off0, off1, dgb, ogb;
  1192. PutByteContext pb;
  1193. unsigned poff0, poff1;
  1194. int i, k, dstpitch;
  1195. int planepitch_byte = (w + 7) / 8;
  1196. int planepitch = ((w + 15) / 16) * 2;
  1197. int pitch = planepitch * bpp;
  1198. if (buf_end - buf <= 64)
  1199. return;
  1200. bytestream2_init(&off0, buf, buf_end - buf);
  1201. bytestream2_init(&off1, buf + 32, buf_end - (buf + 32));
  1202. bytestream2_init_writer(&pb, dst, dst_size);
  1203. dstpitch = flag ? (((w + 7) / 8) * bpp): 2;
  1204. for (k = 0; k < bpp; k++) {
  1205. poff0 = bytestream2_get_be32(&off0);
  1206. poff1 = bytestream2_get_be32(&off1);
  1207. if (!poff0)
  1208. continue;
  1209. if (2LL * poff0 >= buf_end - buf)
  1210. return;
  1211. if (2LL * poff1 >= buf_end - buf)
  1212. return;
  1213. bytestream2_init(&dgb, buf + 2 * poff0, buf_end - (buf + 2 * poff0));
  1214. bytestream2_init(&ogb, buf + 2 * poff1, buf_end - (buf + 2 * poff1));
  1215. while ((bytestream2_peek_be16(&ogb)) != 0xFFFF && bytestream2_get_bytes_left(&ogb) >= 4) {
  1216. uint32_t offset = bytestream2_get_be16(&ogb);
  1217. int16_t cnt = bytestream2_get_be16(&ogb);
  1218. uint16_t data;
  1219. offset = ((2 * offset) / planepitch_byte) * pitch + ((2 * offset) % planepitch_byte) + k * planepitch;
  1220. if (cnt < 0) {
  1221. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1222. cnt = -cnt;
  1223. data = bytestream2_get_be16(&dgb);
  1224. for (i = 0; i < cnt; i++) {
  1225. bytestream2_put_be16(&pb, data);
  1226. bytestream2_skip_p(&pb, dstpitch - 2);
  1227. }
  1228. } else {
  1229. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1230. for (i = 0; i < cnt; i++) {
  1231. data = bytestream2_get_be16(&dgb);
  1232. bytestream2_put_be16(&pb, data);
  1233. bytestream2_skip_p(&pb, dstpitch - 2);
  1234. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. static int unsupported(AVCodecContext *avctx)
  1240. {
  1241. IffContext *s = avctx->priv_data;
  1242. avpriv_request_sample(avctx, "bitmap (compression 0x%0x, bpp %i, ham %i, interlaced %i)", s->compression, s->bpp, s->ham, s->is_interlaced);
  1243. return AVERROR_INVALIDDATA;
  1244. }
  1245. static int decode_frame(AVCodecContext *avctx,
  1246. void *data, int *got_frame,
  1247. AVPacket *avpkt)
  1248. {
  1249. IffContext *s = avctx->priv_data;
  1250. AVFrame *frame = data;
  1251. const uint8_t *buf = avpkt->data;
  1252. int buf_size = avpkt->size;
  1253. const uint8_t *buf_end = buf + buf_size;
  1254. int y, plane, res;
  1255. GetByteContext *gb = &s->gb;
  1256. const AVPixFmtDescriptor *desc;
  1257. bytestream2_init(gb, avpkt->data, avpkt->size);
  1258. if ((res = extract_header(avctx, avpkt)) < 0)
  1259. return res;
  1260. if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
  1261. return res;
  1262. s->frame = frame;
  1263. buf += bytestream2_tell(gb);
  1264. buf_size -= bytestream2_tell(gb);
  1265. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1266. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  1267. avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1268. if ((res = cmap_read_palette(avctx, (uint32_t *)frame->data[1])) < 0)
  1269. return res;
  1270. } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  1271. avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  1272. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  1273. return res;
  1274. }
  1275. s->init = 1;
  1276. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1277. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  1278. memcpy(s->pal[0], s->frame->data[1], 256 * 4);
  1279. }
  1280. switch (s->compression) {
  1281. case 0x0:
  1282. if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  1283. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1284. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1285. for (plane = 0; plane < s->bpp; plane++) {
  1286. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1287. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1288. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1289. buf += s->planesize;
  1290. }
  1291. }
  1292. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1293. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1294. for (y = 0; y < avctx->height; y++) {
  1295. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1296. memset(s->ham_buf, 0, s->planesize * 8);
  1297. for (plane = 0; plane < s->bpp; plane++) {
  1298. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  1299. if (start >= buf_end)
  1300. break;
  1301. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  1302. }
  1303. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1304. }
  1305. } else
  1306. return unsupported(avctx);
  1307. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1308. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  1309. int x;
  1310. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1311. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1312. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  1313. buf += raw_width;
  1314. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  1315. for (x = 0; x < avctx->width; x++)
  1316. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  1317. }
  1318. }
  1319. } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1320. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1321. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))
  1322. memcpy(s->video[0], buf, FFMIN(buf_end - buf, s->video_size));
  1323. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1324. for (y = 0; y < avctx->height; y++) {
  1325. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1326. memset(row, 0, avctx->width);
  1327. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1328. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1329. buf += s->planesize;
  1330. }
  1331. }
  1332. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1333. for (y = 0; y < avctx->height; y++) {
  1334. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1335. memset(s->ham_buf, 0, s->planesize * 8);
  1336. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1337. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1338. buf += s->planesize;
  1339. }
  1340. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1341. }
  1342. } else { // AV_PIX_FMT_BGR32
  1343. for (y = 0; y < avctx->height; y++) {
  1344. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1345. memset(row, 0, avctx->width << 2);
  1346. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1347. decodeplane32((uint32_t *)row, buf,
  1348. FFMIN(s->planesize, buf_end - buf), plane);
  1349. buf += s->planesize;
  1350. }
  1351. }
  1352. }
  1353. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1354. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1355. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1356. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1357. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  1358. buf += avctx->width + (avctx->width % 2); // padding if odd
  1359. }
  1360. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1361. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1362. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1363. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  1364. buf += avctx->width + (avctx->width & 1); // padding if odd
  1365. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1366. }
  1367. } else
  1368. return unsupported(avctx);
  1369. }
  1370. break;
  1371. case 0x1:
  1372. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1373. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1374. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1375. uint8_t *video = s->video[0];
  1376. for (y = 0; y < avctx->height; y++) {
  1377. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1378. memset(row, 0, avctx->width);
  1379. for (plane = 0; plane < s->bpp; plane++) {
  1380. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1381. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1382. memcpy(video, s->planebuf, s->planesize);
  1383. video += s->planesize;
  1384. }
  1385. decodeplane8(row, s->planebuf, s->planesize, plane);
  1386. }
  1387. }
  1388. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  1389. for (y = 0; y < avctx->height; y++) {
  1390. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1391. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  1392. for (plane = 0; plane < s->bpp; plane++) {
  1393. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1394. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  1395. }
  1396. lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
  1397. }
  1398. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1399. uint8_t *video = s->video[0];
  1400. for (y = 0; y < avctx->height; y++) {
  1401. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1402. memset(s->ham_buf, 0, s->planesize * 8);
  1403. for (plane = 0; plane < s->bpp; plane++) {
  1404. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1405. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1406. memcpy(video, s->planebuf, s->planesize);
  1407. video += s->planesize;
  1408. }
  1409. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  1410. }
  1411. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1412. }
  1413. } else { // AV_PIX_FMT_BGR32
  1414. for (y = 0; y < avctx->height; y++) {
  1415. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1416. memset(row, 0, avctx->width << 2);
  1417. for (plane = 0; plane < s->bpp; plane++) {
  1418. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1419. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  1420. }
  1421. }
  1422. }
  1423. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1424. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1425. for (y = 0; y < avctx->height; y++) {
  1426. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1427. buf += decode_byterun(row, avctx->width, gb);
  1428. }
  1429. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1430. for (y = 0; y < avctx->height; y++) {
  1431. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1432. buf += decode_byterun(s->ham_buf, avctx->width, gb);
  1433. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1434. }
  1435. } else
  1436. return unsupported(avctx);
  1437. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
  1438. if (av_get_bits_per_pixel(desc) == 32)
  1439. decode_deep_rle32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0]);
  1440. else
  1441. return unsupported(avctx);
  1442. }
  1443. break;
  1444. case 0x4:
  1445. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8') && avctx->pix_fmt == AV_PIX_FMT_RGB32)
  1446. decode_rgb8(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1447. else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N') && avctx->pix_fmt == AV_PIX_FMT_RGB444)
  1448. decode_rgbn(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1449. else
  1450. return unsupported(avctx);
  1451. break;
  1452. case 0x5:
  1453. if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1454. if (av_get_bits_per_pixel(desc) == 32)
  1455. decode_deep_tvdc32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0], s->tvdc);
  1456. else
  1457. return unsupported(avctx);
  1458. } else
  1459. return unsupported(avctx);
  1460. break;
  1461. case 0x300:
  1462. case 0x301:
  1463. decode_short_horizontal_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1464. break;
  1465. case 0x500:
  1466. case 0x501:
  1467. decode_byte_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->is_brush, s->bpp, s->video_size);
  1468. break;
  1469. case 0x700:
  1470. case 0x701:
  1471. if (s->is_short)
  1472. decode_short_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1473. else
  1474. decode_long_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1475. break;
  1476. case 0x800:
  1477. case 0x801:
  1478. if (s->is_short)
  1479. decode_short_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1480. else
  1481. decode_long_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1482. break;
  1483. case 0x4a00:
  1484. case 0x4a01:
  1485. decode_delta_j(s->video[0], buf, buf_end, avctx->width, avctx->height, s->bpp, s->video_size);
  1486. break;
  1487. case 0x6400:
  1488. case 0x6401:
  1489. if (s->is_interlaced)
  1490. return unsupported(avctx);
  1491. decode_delta_d(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1492. break;
  1493. case 0x6500:
  1494. case 0x6501:
  1495. if (s->is_interlaced)
  1496. return unsupported(avctx);
  1497. decode_delta_e(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1498. break;
  1499. case 0x6c00:
  1500. case 0x6c01:
  1501. decode_delta_l(s->video[0], buf, buf_end, avctx->width, s->is_short, s->bpp, s->video_size);
  1502. break;
  1503. default:
  1504. return unsupported(avctx);
  1505. }
  1506. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1507. memcpy(s->pal[1], s->pal[0], 256 * 4);
  1508. memcpy(s->video[1], s->video[0], s->video_size);
  1509. }
  1510. if (s->compression > 0xff) {
  1511. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1512. buf = s->video[0];
  1513. for (y = 0; y < avctx->height; y++) {
  1514. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1515. memset(row, 0, avctx->width);
  1516. for (plane = 0; plane < s->bpp; plane++) {
  1517. decodeplane8(row, buf, s->planesize, plane);
  1518. buf += s->planesize;
  1519. }
  1520. }
  1521. memcpy(frame->data[1], s->pal[0], 256 * 4);
  1522. } else if (s->ham) {
  1523. int i, count = 1 << s->ham;
  1524. buf = s->video[0];
  1525. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof(uint32_t));
  1526. for (i = 0; i < count; i++) {
  1527. s->ham_palbuf[i*2+1] = s->pal[0][i];
  1528. }
  1529. for (i = 0; i < count; i++) {
  1530. uint32_t tmp = i << (8 - s->ham);
  1531. tmp |= tmp >> s->ham;
  1532. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF;
  1533. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00;
  1534. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF;
  1535. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  1536. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  1537. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  1538. }
  1539. if (s->masking == MASK_HAS_MASK) {
  1540. for (i = 0; i < 8 * (1 << s->ham); i++)
  1541. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  1542. }
  1543. for (y = 0; y < avctx->height; y++) {
  1544. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1545. memset(s->ham_buf, 0, s->planesize * 8);
  1546. for (plane = 0; plane < s->bpp; plane++) {
  1547. decodeplane8(s->ham_buf, buf, s->planesize, plane);
  1548. buf += s->planesize;
  1549. }
  1550. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1551. }
  1552. } else {
  1553. return unsupported(avctx);
  1554. }
  1555. if (!s->is_brush) {
  1556. FFSWAP(uint8_t *, s->video[0], s->video[1]);
  1557. FFSWAP(uint32_t *, s->pal[0], s->pal[1]);
  1558. }
  1559. }
  1560. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  1561. frame->key_frame = 1;
  1562. frame->pict_type = AV_PICTURE_TYPE_I;
  1563. } else {
  1564. frame->key_frame = 0;
  1565. frame->pict_type = AV_PICTURE_TYPE_P;
  1566. }
  1567. *got_frame = 1;
  1568. return buf_size;
  1569. }
  1570. #if CONFIG_IFF_ILBM_DECODER
  1571. AVCodec ff_iff_ilbm_decoder = {
  1572. .name = "iff",
  1573. .long_name = NULL_IF_CONFIG_SMALL("IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN"),
  1574. .type = AVMEDIA_TYPE_VIDEO,
  1575. .id = AV_CODEC_ID_IFF_ILBM,
  1576. .priv_data_size = sizeof(IffContext),
  1577. .init = decode_init,
  1578. .close = decode_end,
  1579. .decode = decode_frame,
  1580. .capabilities = AV_CODEC_CAP_DR1,
  1581. };
  1582. #endif