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.

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