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.

1790 lines
66KB

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