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.

1752 lines
64KB

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