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.

1914 lines
71KB

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