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.

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