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.

1894 lines
70KB

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