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.

1773 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;
  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;
  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->mask_buf);
  343. av_freep(&s->mask_palbuf);
  344. av_freep(&s->video[0]);
  345. av_freep(&s->video[1]);
  346. av_freep(&s->pal);
  347. return 0;
  348. }
  349. static av_cold int decode_init(AVCodecContext *avctx)
  350. {
  351. IffContext *s = avctx->priv_data;
  352. int err;
  353. if (avctx->bits_per_coded_sample <= 8) {
  354. int palette_size;
  355. if (avctx->extradata_size >= 2)
  356. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  357. else
  358. palette_size = 0;
  359. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  360. (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  361. } else if (avctx->bits_per_coded_sample <= 32) {
  362. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8')) {
  363. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  364. } else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N')) {
  365. avctx->pix_fmt = AV_PIX_FMT_RGB444;
  366. } else if (avctx->codec_tag != MKTAG('D', 'E', 'E', 'P')) {
  367. if (avctx->bits_per_coded_sample == 24) {
  368. avctx->pix_fmt = AV_PIX_FMT_0BGR32;
  369. } else if (avctx->bits_per_coded_sample == 32) {
  370. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  371. } else {
  372. avpriv_request_sample(avctx, "unknown bits_per_coded_sample");
  373. return AVERROR_PATCHWELCOME;
  374. }
  375. }
  376. } else {
  377. return AVERROR_INVALIDDATA;
  378. }
  379. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  380. return err;
  381. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  382. s->planebuf = av_malloc(s->planesize + AV_INPUT_BUFFER_PADDING_SIZE);
  383. if (!s->planebuf)
  384. return AVERROR(ENOMEM);
  385. s->bpp = avctx->bits_per_coded_sample;
  386. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  387. s->video_size = FFALIGN(avctx->width, 2) * avctx->height * s->bpp;
  388. s->video[0] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
  389. s->video[1] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
  390. s->pal = av_calloc(256, sizeof(*s->pal));
  391. if (!s->video[0] || !s->video[1] || !s->pal)
  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. av_log(NULL, AV_LOG_ERROR, "cols*bpp is invalid (%d*%d)", cols, bpp);
  791. return;
  792. }
  793. if (kludge_j)
  794. offset = ((offset / (320 / 8)) * pitch) + (offset % (320 / 8)) - kludge_j;
  795. else
  796. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  797. for (b = 0; b < cols; b++) {
  798. for (d = 0; d < bpp; d++) {
  799. uint8_t value = bytestream2_get_byte(&gb);
  800. if (offset >= dst_size)
  801. return;
  802. ptr = dst + offset;
  803. if (flag)
  804. ptr[0] ^= value;
  805. else
  806. ptr[0] = value;
  807. offset += planepitch;
  808. }
  809. }
  810. if ((cols * bpp) & 1)
  811. bytestream2_skip(&gb, 1);
  812. }
  813. break;
  814. case 2:
  815. flag = bytestream2_get_be16(&gb);
  816. rows = bytestream2_get_be16(&gb);
  817. bytes = bytestream2_get_be16(&gb);
  818. groups = bytestream2_get_be16(&gb);
  819. for (g = 0; g < groups; g++) {
  820. offset = bytestream2_get_be16(&gb);
  821. if (kludge_j)
  822. offset = ((offset / (320 / 8)) * pitch) + (offset % (320/ 8)) - kludge_j;
  823. else
  824. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  825. for (r = 0; r < rows; r++) {
  826. for (d = 0; d < bpp; d++) {
  827. unsigned noffset = offset + (r * pitch) + d * planepitch;
  828. if (!bytes || bytestream2_get_bytes_left(&gb) < bytes) {
  829. av_log(NULL, AV_LOG_ERROR, "bytes %d is invalid", bytes);
  830. return;
  831. }
  832. for (b = 0; b < bytes; b++) {
  833. uint8_t value = bytestream2_get_byte(&gb);
  834. if (noffset >= dst_size)
  835. return;
  836. ptr = dst + noffset;
  837. if (flag)
  838. ptr[0] ^= value;
  839. else
  840. ptr[0] = value;
  841. noffset++;
  842. }
  843. }
  844. }
  845. if ((rows * bytes * bpp) & 1)
  846. bytestream2_skip(&gb, 1);
  847. }
  848. break;
  849. default:
  850. return;
  851. }
  852. }
  853. }
  854. static void decode_short_vertical_delta(uint8_t *dst,
  855. const uint8_t *buf, const uint8_t *buf_end,
  856. int w, int bpp, int dst_size)
  857. {
  858. int ncolumns = (w + 15) >> 4;
  859. int dstpitch = ncolumns * bpp * 2;
  860. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  861. GetByteContext ptrs, gb, dptrs, dgb;
  862. PutByteContext pb;
  863. int i, j, k;
  864. if (buf_end - buf <= 64)
  865. return;
  866. bytestream2_init(&ptrs, buf, buf_end - buf);
  867. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  868. bytestream2_init_writer(&pb, dst, dst_size);
  869. for (k = 0; k < bpp; k++) {
  870. ofssrc = bytestream2_get_be32(&ptrs);
  871. ofsdata = bytestream2_get_be32(&dptrs);
  872. if (!ofssrc)
  873. continue;
  874. if (ofssrc >= buf_end - buf)
  875. return;
  876. if (ofsdata >= buf_end - buf)
  877. return;
  878. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  879. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  880. for (j = 0; j < ncolumns; j++) {
  881. ofsdst = (j + k * ncolumns) * 2;
  882. i = bytestream2_get_byte(&gb);
  883. while (i > 0) {
  884. opcode = bytestream2_get_byte(&gb);
  885. if (opcode == 0) {
  886. opcode = bytestream2_get_byte(&gb);
  887. x = bytestream2_get_be16(&dgb);
  888. while (opcode) {
  889. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  890. bytestream2_put_be16(&pb, x);
  891. ofsdst += dstpitch;
  892. opcode--;
  893. }
  894. } else if (opcode < 0x80) {
  895. ofsdst += opcode * dstpitch;
  896. } else {
  897. opcode &= 0x7f;
  898. while (opcode) {
  899. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  900. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  901. ofsdst += dstpitch;
  902. opcode--;
  903. }
  904. }
  905. i--;
  906. }
  907. }
  908. }
  909. }
  910. static void decode_long_vertical_delta(uint8_t *dst,
  911. const uint8_t *buf, const uint8_t *buf_end,
  912. int w, int bpp, int dst_size)
  913. {
  914. int ncolumns = (w + 31) >> 5;
  915. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  916. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  917. GetByteContext ptrs, gb, dptrs, dgb;
  918. PutByteContext pb;
  919. int i, j, k, h;
  920. if (buf_end - buf <= 64)
  921. return;
  922. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  923. bytestream2_init(&ptrs, buf, buf_end - buf);
  924. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  925. bytestream2_init_writer(&pb, dst, dst_size);
  926. for (k = 0; k < bpp; k++) {
  927. ofssrc = bytestream2_get_be32(&ptrs);
  928. ofsdata = bytestream2_get_be32(&dptrs);
  929. if (!ofssrc)
  930. continue;
  931. if (ofssrc >= buf_end - buf)
  932. return;
  933. if (ofsdata >= buf_end - buf)
  934. return;
  935. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  936. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  937. for (j = 0; j < ncolumns; j++) {
  938. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  939. i = bytestream2_get_byte(&gb);
  940. while (i > 0) {
  941. opcode = bytestream2_get_byte(&gb);
  942. if (opcode == 0) {
  943. opcode = bytestream2_get_byte(&gb);
  944. if (h && (j == (ncolumns - 1))) {
  945. x = bytestream2_get_be16(&dgb);
  946. bytestream2_skip(&dgb, 2);
  947. } else {
  948. x = bytestream2_get_be32(&dgb);
  949. }
  950. while (opcode) {
  951. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  952. if (h && (j == (ncolumns - 1))) {
  953. bytestream2_put_be16(&pb, x);
  954. } else {
  955. bytestream2_put_be32(&pb, x);
  956. }
  957. ofsdst += dstpitch;
  958. opcode--;
  959. }
  960. } else if (opcode < 0x80) {
  961. ofsdst += opcode * dstpitch;
  962. } else {
  963. opcode &= 0x7f;
  964. while (opcode) {
  965. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  966. if (h && (j == (ncolumns - 1))) {
  967. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  968. bytestream2_skip(&dgb, 2);
  969. } else {
  970. bytestream2_put_be32(&pb, bytestream2_get_be32(&dgb));
  971. }
  972. ofsdst += dstpitch;
  973. opcode--;
  974. }
  975. }
  976. i--;
  977. }
  978. }
  979. }
  980. }
  981. static void decode_short_vertical_delta2(uint8_t *dst,
  982. const uint8_t *buf, const uint8_t *buf_end,
  983. int w, int bpp, int dst_size)
  984. {
  985. int ncolumns = (w + 15) >> 4;
  986. int dstpitch = ncolumns * bpp * 2;
  987. unsigned ofsdst, ofssrc, opcode, x;
  988. GetByteContext ptrs, gb;
  989. PutByteContext pb;
  990. int i, j, k;
  991. bytestream2_init(&ptrs, buf, buf_end - buf);
  992. bytestream2_init_writer(&pb, dst, dst_size);
  993. for (k = 0; k < bpp; k++) {
  994. ofssrc = bytestream2_get_be32(&ptrs);
  995. if (!ofssrc)
  996. continue;
  997. if (ofssrc >= buf_end - buf)
  998. continue;
  999. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1000. for (j = 0; j < ncolumns; j++) {
  1001. ofsdst = (j + k * ncolumns) * 2;
  1002. i = bytestream2_get_be16(&gb);
  1003. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1004. opcode = bytestream2_get_be16(&gb);
  1005. if (opcode == 0) {
  1006. opcode = bytestream2_get_be16(&gb);
  1007. x = bytestream2_get_be16(&gb);
  1008. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1009. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1010. bytestream2_put_be16(&pb, x);
  1011. ofsdst += dstpitch;
  1012. opcode--;
  1013. }
  1014. } else if (opcode < 0x8000) {
  1015. ofsdst += opcode * dstpitch;
  1016. } else {
  1017. opcode &= 0x7fff;
  1018. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1019. bytestream2_get_bytes_left_p(&pb) > 1) {
  1020. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1021. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1022. ofsdst += dstpitch;
  1023. opcode--;
  1024. }
  1025. }
  1026. i--;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. static void decode_long_vertical_delta2(uint8_t *dst,
  1032. const uint8_t *buf, const uint8_t *buf_end,
  1033. int w, int bpp, int dst_size)
  1034. {
  1035. int ncolumns = (w + 31) >> 5;
  1036. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  1037. unsigned ofsdst, ofssrc, opcode, x;
  1038. unsigned skip = 0x80000000, mask = skip - 1;
  1039. GetByteContext ptrs, gb;
  1040. PutByteContext pb;
  1041. int i, j, k, h;
  1042. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  1043. bytestream2_init(&ptrs, buf, buf_end - buf);
  1044. bytestream2_init_writer(&pb, dst, dst_size);
  1045. for (k = 0; k < bpp; k++) {
  1046. ofssrc = bytestream2_get_be32(&ptrs);
  1047. if (!ofssrc)
  1048. continue;
  1049. if (ofssrc >= buf_end - buf)
  1050. continue;
  1051. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1052. for (j = 0; j < ncolumns; j++) {
  1053. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  1054. if (h && (j == (ncolumns - 1))) {
  1055. skip = 0x8000;
  1056. mask = skip - 1;
  1057. }
  1058. i = bytestream2_get_be32(&gb);
  1059. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1060. opcode = bytestream2_get_be32(&gb);
  1061. if (opcode == 0) {
  1062. if (h && (j == ncolumns - 1)) {
  1063. opcode = bytestream2_get_be16(&gb);
  1064. x = bytestream2_get_be16(&gb);
  1065. } else {
  1066. opcode = bytestream2_get_be32(&gb);
  1067. x = bytestream2_get_be32(&gb);
  1068. }
  1069. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1070. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1071. if (h && (j == ncolumns - 1))
  1072. bytestream2_put_be16(&pb, x);
  1073. else
  1074. bytestream2_put_be32(&pb, x);
  1075. ofsdst += dstpitch;
  1076. opcode--;
  1077. }
  1078. } else if (opcode < skip) {
  1079. ofsdst += opcode * dstpitch;
  1080. } else {
  1081. opcode &= mask;
  1082. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1083. bytestream2_get_bytes_left_p(&pb) > 1) {
  1084. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1085. if (h && (j == ncolumns - 1)) {
  1086. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1087. } else {
  1088. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1089. }
  1090. ofsdst += dstpitch;
  1091. opcode--;
  1092. }
  1093. }
  1094. i--;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. static void decode_delta_d(uint8_t *dst,
  1100. const uint8_t *buf, const uint8_t *buf_end,
  1101. int w, int flag, int bpp, int dst_size)
  1102. {
  1103. int planepitch = FFALIGN(w, 16) >> 3;
  1104. int pitch = planepitch * bpp;
  1105. int planepitch_byte = (w + 7) / 8;
  1106. unsigned entries, ofssrc;
  1107. GetByteContext gb, ptrs;
  1108. PutByteContext pb;
  1109. int k;
  1110. if (buf_end - buf <= 4 * bpp)
  1111. return;
  1112. bytestream2_init_writer(&pb, dst, dst_size);
  1113. bytestream2_init(&ptrs, buf, bpp * 4);
  1114. for (k = 0; k < bpp; k++) {
  1115. ofssrc = bytestream2_get_be32(&ptrs);
  1116. if (!ofssrc)
  1117. continue;
  1118. if (ofssrc >= buf_end - buf)
  1119. continue;
  1120. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1121. entries = bytestream2_get_be32(&gb);
  1122. while (entries && bytestream2_get_bytes_left(&gb) >= 8) {
  1123. int32_t opcode = bytestream2_get_be32(&gb);
  1124. unsigned offset = bytestream2_get_be32(&gb);
  1125. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1126. if (opcode >= 0) {
  1127. uint32_t x = bytestream2_get_be32(&gb);
  1128. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1129. bytestream2_put_be32(&pb, x);
  1130. bytestream2_skip_p(&pb, pitch - 4);
  1131. opcode--;
  1132. }
  1133. } else {
  1134. opcode = -opcode;
  1135. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1136. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1137. bytestream2_skip_p(&pb, pitch - 4);
  1138. opcode--;
  1139. }
  1140. }
  1141. entries--;
  1142. }
  1143. }
  1144. }
  1145. static void decode_delta_e(uint8_t *dst,
  1146. const uint8_t *buf, const uint8_t *buf_end,
  1147. int w, int flag, int bpp, int dst_size)
  1148. {
  1149. int planepitch = FFALIGN(w, 16) >> 3;
  1150. int pitch = planepitch * bpp;
  1151. int planepitch_byte = (w + 7) / 8;
  1152. unsigned entries, ofssrc;
  1153. GetByteContext gb, ptrs;
  1154. PutByteContext pb;
  1155. int k;
  1156. if (buf_end - buf <= 4 * bpp)
  1157. return;
  1158. bytestream2_init_writer(&pb, dst, dst_size);
  1159. bytestream2_init(&ptrs, buf, bpp * 4);
  1160. for (k = 0; k < bpp; k++) {
  1161. ofssrc = bytestream2_get_be32(&ptrs);
  1162. if (!ofssrc)
  1163. continue;
  1164. if (ofssrc >= buf_end - buf)
  1165. continue;
  1166. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1167. entries = bytestream2_get_be16(&gb);
  1168. while (entries && bytestream2_get_bytes_left(&gb) >= 6) {
  1169. int16_t opcode = bytestream2_get_be16(&gb);
  1170. unsigned offset = bytestream2_get_be32(&gb);
  1171. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1172. if (opcode >= 0) {
  1173. uint16_t x = bytestream2_get_be16(&gb);
  1174. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1175. bytestream2_put_be16(&pb, x);
  1176. bytestream2_skip_p(&pb, pitch - 2);
  1177. opcode--;
  1178. }
  1179. } else {
  1180. opcode = -opcode;
  1181. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1182. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1183. bytestream2_skip_p(&pb, pitch - 2);
  1184. opcode--;
  1185. }
  1186. }
  1187. entries--;
  1188. }
  1189. }
  1190. }
  1191. static void decode_delta_l(uint8_t *dst,
  1192. const uint8_t *buf, const uint8_t *buf_end,
  1193. int w, int flag, int bpp, int dst_size)
  1194. {
  1195. GetByteContext off0, off1, dgb, ogb;
  1196. PutByteContext pb;
  1197. unsigned poff0, poff1;
  1198. int i, k, dstpitch;
  1199. int planepitch_byte = (w + 7) / 8;
  1200. int planepitch = ((w + 15) / 16) * 2;
  1201. int pitch = planepitch * bpp;
  1202. if (buf_end - buf <= 64)
  1203. return;
  1204. bytestream2_init(&off0, buf, buf_end - buf);
  1205. bytestream2_init(&off1, buf + 32, buf_end - (buf + 32));
  1206. bytestream2_init_writer(&pb, dst, dst_size);
  1207. dstpitch = flag ? (((w + 7) / 8) * bpp): 2;
  1208. for (k = 0; k < bpp; k++) {
  1209. poff0 = bytestream2_get_be32(&off0);
  1210. poff1 = bytestream2_get_be32(&off1);
  1211. if (!poff0)
  1212. continue;
  1213. if (2LL * poff0 >= buf_end - buf)
  1214. return;
  1215. if (2LL * poff1 >= buf_end - buf)
  1216. return;
  1217. bytestream2_init(&dgb, buf + 2 * poff0, buf_end - (buf + 2 * poff0));
  1218. bytestream2_init(&ogb, buf + 2 * poff1, buf_end - (buf + 2 * poff1));
  1219. while (bytestream2_peek_be16(&ogb) != 0xFFFF && bytestream2_get_bytes_left(&ogb) >= 4) {
  1220. uint32_t offset = bytestream2_get_be16(&ogb);
  1221. int16_t cnt = bytestream2_get_be16(&ogb);
  1222. uint16_t data;
  1223. offset = ((2 * offset) / planepitch_byte) * pitch + ((2 * offset) % planepitch_byte) + k * planepitch;
  1224. if (cnt < 0) {
  1225. if (bytestream2_get_bytes_left(&dgb) < 2)
  1226. break;
  1227. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1228. cnt = -cnt;
  1229. data = bytestream2_get_be16(&dgb);
  1230. for (i = 0; i < cnt; i++) {
  1231. bytestream2_put_be16(&pb, data);
  1232. bytestream2_skip_p(&pb, dstpitch - 2);
  1233. }
  1234. } else {
  1235. if (bytestream2_get_bytes_left(&dgb) < 2*cnt)
  1236. break;
  1237. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1238. for (i = 0; i < cnt; i++) {
  1239. data = bytestream2_get_be16(&dgb);
  1240. bytestream2_put_be16(&pb, data);
  1241. bytestream2_skip_p(&pb, dstpitch - 2);
  1242. }
  1243. }
  1244. }
  1245. }
  1246. }
  1247. static int unsupported(AVCodecContext *avctx)
  1248. {
  1249. IffContext *s = avctx->priv_data;
  1250. avpriv_request_sample(avctx, "bitmap (compression 0x%0x, bpp %i, ham %i, interlaced %i)", s->compression, s->bpp, s->ham, s->is_interlaced);
  1251. return AVERROR_INVALIDDATA;
  1252. }
  1253. static int decode_frame(AVCodecContext *avctx,
  1254. void *data, int *got_frame,
  1255. AVPacket *avpkt)
  1256. {
  1257. IffContext *s = avctx->priv_data;
  1258. AVFrame *frame = data;
  1259. const uint8_t *buf = avpkt->data;
  1260. int buf_size = avpkt->size;
  1261. const uint8_t *buf_end = buf + buf_size;
  1262. int y, plane, res;
  1263. GetByteContext *gb = &s->gb;
  1264. const AVPixFmtDescriptor *desc;
  1265. bytestream2_init(gb, avpkt->data, avpkt->size);
  1266. if ((res = extract_header(avctx, avpkt)) < 0)
  1267. return res;
  1268. if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
  1269. return res;
  1270. s->frame = frame;
  1271. buf += bytestream2_tell(gb);
  1272. buf_size -= bytestream2_tell(gb);
  1273. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1274. if (!s->init && avctx->bits_per_coded_sample <= 8 - (s->masking == MASK_HAS_MASK) &&
  1275. avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1276. if ((res = cmap_read_palette(avctx, (uint32_t *)frame->data[1])) < 0)
  1277. return res;
  1278. } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  1279. avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  1280. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  1281. return res;
  1282. }
  1283. s->init = 1;
  1284. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1285. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  1286. memcpy(s->pal, s->frame->data[1], 256 * 4);
  1287. }
  1288. switch (s->compression) {
  1289. case 0x0:
  1290. if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  1291. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1292. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1293. for (plane = 0; plane < s->bpp; plane++) {
  1294. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1295. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1296. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1297. buf += s->planesize;
  1298. }
  1299. }
  1300. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1301. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1302. for (y = 0; y < avctx->height; y++) {
  1303. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1304. memset(s->ham_buf, 0, s->planesize * 8);
  1305. for (plane = 0; plane < s->bpp; plane++) {
  1306. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  1307. if (start >= buf_end)
  1308. break;
  1309. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  1310. }
  1311. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1312. }
  1313. } else
  1314. return unsupported(avctx);
  1315. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1316. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  1317. int x;
  1318. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1319. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1320. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  1321. buf += raw_width;
  1322. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  1323. for (x = 0; x < avctx->width; x++)
  1324. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  1325. }
  1326. }
  1327. } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1328. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1329. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))
  1330. memcpy(s->video[0], buf, FFMIN(buf_end - buf, s->video_size));
  1331. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1332. for (y = 0; y < avctx->height; y++) {
  1333. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1334. memset(row, 0, avctx->width);
  1335. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1336. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1337. buf += s->planesize;
  1338. }
  1339. }
  1340. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1341. for (y = 0; y < avctx->height; y++) {
  1342. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1343. memset(s->ham_buf, 0, s->planesize * 8);
  1344. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1345. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1346. buf += s->planesize;
  1347. }
  1348. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1349. }
  1350. } else { // AV_PIX_FMT_BGR32
  1351. for (y = 0; y < avctx->height; y++) {
  1352. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1353. memset(row, 0, avctx->width << 2);
  1354. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1355. decodeplane32((uint32_t *)row, buf,
  1356. FFMIN(s->planesize, buf_end - buf), plane);
  1357. buf += s->planesize;
  1358. }
  1359. }
  1360. }
  1361. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1362. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1363. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1364. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1365. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  1366. buf += avctx->width + (avctx->width % 2); // padding if odd
  1367. }
  1368. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1369. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1370. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1371. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  1372. buf += avctx->width + (avctx->width & 1); // padding if odd
  1373. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1374. }
  1375. } else
  1376. return unsupported(avctx);
  1377. }
  1378. break;
  1379. case 0x1:
  1380. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1381. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1382. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1383. uint8_t *video = s->video[0];
  1384. for (y = 0; y < avctx->height; y++) {
  1385. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1386. memset(row, 0, avctx->width);
  1387. for (plane = 0; plane < s->bpp; plane++) {
  1388. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1389. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1390. memcpy(video, s->planebuf, s->planesize);
  1391. video += s->planesize;
  1392. }
  1393. decodeplane8(row, s->planebuf, s->planesize, plane);
  1394. }
  1395. }
  1396. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  1397. for (y = 0; y < avctx->height; y++) {
  1398. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1399. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  1400. for (plane = 0; plane < s->bpp; plane++) {
  1401. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1402. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  1403. }
  1404. lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
  1405. }
  1406. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1407. uint8_t *video = s->video[0];
  1408. for (y = 0; y < avctx->height; y++) {
  1409. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1410. memset(s->ham_buf, 0, s->planesize * 8);
  1411. for (plane = 0; plane < s->bpp; plane++) {
  1412. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1413. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1414. memcpy(video, s->planebuf, s->planesize);
  1415. video += s->planesize;
  1416. }
  1417. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  1418. }
  1419. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1420. }
  1421. } else { // AV_PIX_FMT_BGR32
  1422. for (y = 0; y < avctx->height; y++) {
  1423. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1424. memset(row, 0, avctx->width << 2);
  1425. for (plane = 0; plane < s->bpp; plane++) {
  1426. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1427. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  1428. }
  1429. }
  1430. }
  1431. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1432. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1433. for (y = 0; y < avctx->height; y++) {
  1434. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1435. buf += decode_byterun(row, avctx->width, gb);
  1436. }
  1437. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1438. for (y = 0; y < avctx->height; y++) {
  1439. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1440. buf += decode_byterun(s->ham_buf, avctx->width, gb);
  1441. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1442. }
  1443. } else
  1444. return unsupported(avctx);
  1445. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
  1446. if (av_get_bits_per_pixel(desc) == 32)
  1447. decode_deep_rle32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0]);
  1448. else
  1449. return unsupported(avctx);
  1450. }
  1451. break;
  1452. case 0x4:
  1453. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8') && avctx->pix_fmt == AV_PIX_FMT_RGB32)
  1454. decode_rgb8(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1455. else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N') && avctx->pix_fmt == AV_PIX_FMT_RGB444)
  1456. decode_rgbn(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1457. else
  1458. return unsupported(avctx);
  1459. break;
  1460. case 0x5:
  1461. if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1462. if (av_get_bits_per_pixel(desc) == 32)
  1463. decode_deep_tvdc32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0], s->tvdc);
  1464. else
  1465. return unsupported(avctx);
  1466. } else
  1467. return unsupported(avctx);
  1468. break;
  1469. case 0x300:
  1470. case 0x301:
  1471. decode_short_horizontal_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1472. break;
  1473. case 0x500:
  1474. case 0x501:
  1475. decode_byte_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->is_brush, s->bpp, s->video_size);
  1476. break;
  1477. case 0x700:
  1478. case 0x701:
  1479. if (s->is_short)
  1480. decode_short_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1481. else
  1482. decode_long_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1483. break;
  1484. case 0x800:
  1485. case 0x801:
  1486. if (s->is_short)
  1487. decode_short_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1488. else
  1489. decode_long_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1490. break;
  1491. case 0x4a00:
  1492. case 0x4a01:
  1493. decode_delta_j(s->video[0], buf, buf_end, avctx->width, avctx->height, s->bpp, s->video_size);
  1494. break;
  1495. case 0x6400:
  1496. case 0x6401:
  1497. if (s->is_interlaced)
  1498. return unsupported(avctx);
  1499. decode_delta_d(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1500. break;
  1501. case 0x6500:
  1502. case 0x6501:
  1503. if (s->is_interlaced)
  1504. return unsupported(avctx);
  1505. decode_delta_e(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1506. break;
  1507. case 0x6c00:
  1508. case 0x6c01:
  1509. decode_delta_l(s->video[0], buf, buf_end, avctx->width, s->is_short, s->bpp, s->video_size);
  1510. break;
  1511. default:
  1512. return unsupported(avctx);
  1513. }
  1514. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1515. memcpy(s->video[1], s->video[0], s->video_size);
  1516. }
  1517. if (s->compression > 0xff) {
  1518. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1519. buf = s->video[0];
  1520. for (y = 0; y < avctx->height; y++) {
  1521. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1522. memset(row, 0, avctx->width);
  1523. for (plane = 0; plane < s->bpp; plane++) {
  1524. decodeplane8(row, buf, s->planesize, plane);
  1525. buf += s->planesize;
  1526. }
  1527. }
  1528. memcpy(frame->data[1], s->pal, 256 * 4);
  1529. } else if (s->ham) {
  1530. int i, count = 1 << s->ham;
  1531. buf = s->video[0];
  1532. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof(uint32_t));
  1533. for (i = 0; i < count; i++) {
  1534. s->ham_palbuf[i*2+1] = s->pal[i];
  1535. }
  1536. for (i = 0; i < count; i++) {
  1537. uint32_t tmp = i << (8 - s->ham);
  1538. tmp |= tmp >> s->ham;
  1539. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF;
  1540. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00;
  1541. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF;
  1542. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  1543. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  1544. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  1545. }
  1546. if (s->masking == MASK_HAS_MASK) {
  1547. for (i = 0; i < 8 * (1 << s->ham); i++)
  1548. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  1549. }
  1550. for (y = 0; y < avctx->height; y++) {
  1551. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1552. memset(s->ham_buf, 0, s->planesize * 8);
  1553. for (plane = 0; plane < s->bpp; plane++) {
  1554. decodeplane8(s->ham_buf, buf, s->planesize, plane);
  1555. buf += s->planesize;
  1556. }
  1557. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1558. }
  1559. } else {
  1560. return unsupported(avctx);
  1561. }
  1562. if (!s->is_brush) {
  1563. FFSWAP(uint8_t *, s->video[0], s->video[1]);
  1564. }
  1565. }
  1566. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  1567. frame->key_frame = 1;
  1568. frame->pict_type = AV_PICTURE_TYPE_I;
  1569. } else {
  1570. frame->key_frame = 0;
  1571. frame->pict_type = AV_PICTURE_TYPE_P;
  1572. }
  1573. *got_frame = 1;
  1574. return buf_size;
  1575. }
  1576. #if CONFIG_IFF_ILBM_DECODER
  1577. AVCodec ff_iff_ilbm_decoder = {
  1578. .name = "iff",
  1579. .long_name = NULL_IF_CONFIG_SMALL("IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN"),
  1580. .type = AVMEDIA_TYPE_VIDEO,
  1581. .id = AV_CODEC_ID_IFF_ILBM,
  1582. .priv_data_size = sizeof(IffContext),
  1583. .init = decode_init,
  1584. .close = decode_end,
  1585. .decode = decode_frame,
  1586. .capabilities = AV_CODEC_CAP_DR1,
  1587. };
  1588. #endif