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.

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