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.

1913 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 + 5 <= src_end) {
  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. memcpy(dst + y*linesize + x * 4, src, length * 4);
  667. src += length * 4;
  668. x += length;
  669. i += length;
  670. if (x >= width) {
  671. x = 0;
  672. y += 1;
  673. if (y >= height)
  674. return;
  675. }
  676. }
  677. } else {
  678. int size = -opcode + 1;
  679. uint32_t pixel = AV_RN32(src);
  680. for (i = 0; i < size; i++) {
  681. *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
  682. x += 1;
  683. if (x >= width) {
  684. x = 0;
  685. y += 1;
  686. if (y >= height)
  687. return;
  688. }
  689. }
  690. src += 4;
  691. }
  692. }
  693. }
  694. /**
  695. * Decode DEEP TVDC 32-bit buffer
  696. * @param[out] dst Destination buffer
  697. * @param[in] src Source buffer
  698. * @param src_size Source buffer size (bytes)
  699. * @param width Width of destination buffer (pixels)
  700. * @param height Height of destination buffer (pixels)
  701. * @param linesize Line size of destination buffer (bytes)
  702. * @param[int] tvdc TVDC lookup table
  703. */
  704. 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)
  705. {
  706. int x = 0, y = 0, plane = 0;
  707. int8_t pixel = 0;
  708. int i, j;
  709. for (i = 0; i < src_size * 2;) {
  710. #define GETNIBBLE ((i & 1) ? (src[i>>1] & 0xF) : (src[i>>1] >> 4))
  711. int d = tvdc[GETNIBBLE];
  712. i++;
  713. if (d) {
  714. pixel += d;
  715. dst[y * linesize + x*4 + plane] = pixel;
  716. x++;
  717. } else {
  718. if (i >= src_size * 2)
  719. return;
  720. d = GETNIBBLE + 1;
  721. i++;
  722. d = FFMIN(d, width - x);
  723. for (j = 0; j < d; j++) {
  724. dst[y * linesize + x*4 + plane] = pixel;
  725. x++;
  726. }
  727. }
  728. if (x >= width) {
  729. plane++;
  730. if (plane >= 4) {
  731. y++;
  732. if (y >= height)
  733. return;
  734. plane = 0;
  735. }
  736. x = 0;
  737. pixel = 0;
  738. i = (i + 1) & ~1;
  739. }
  740. }
  741. }
  742. static void decode_short_horizontal_delta(uint8_t *dst,
  743. const uint8_t *buf, const uint8_t *buf_end,
  744. int w, int bpp, int dst_size)
  745. {
  746. int planepitch = FFALIGN(w, 16) >> 3;
  747. int pitch = planepitch * bpp;
  748. GetByteContext ptrs, gb;
  749. PutByteContext pb;
  750. unsigned ofssrc, pos;
  751. int i, k;
  752. bytestream2_init(&ptrs, buf, buf_end - buf);
  753. bytestream2_init_writer(&pb, dst, dst_size);
  754. for (k = 0; k < bpp; k++) {
  755. ofssrc = bytestream2_get_be32(&ptrs);
  756. pos = 0;
  757. if (!ofssrc)
  758. continue;
  759. if (ofssrc >= buf_end - buf)
  760. continue;
  761. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  762. while (bytestream2_peek_be16(&gb) != 0xFFFF && bytestream2_get_bytes_left(&gb) > 3) {
  763. int16_t offset = bytestream2_get_be16(&gb);
  764. unsigned noffset;
  765. if (offset >= 0) {
  766. unsigned data = bytestream2_get_be16(&gb);
  767. pos += offset * 2;
  768. noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
  769. bytestream2_seek_p(&pb, noffset, SEEK_SET);
  770. bytestream2_put_be16(&pb, data);
  771. } else {
  772. uint16_t count = bytestream2_get_be16(&gb);
  773. pos += 2 * -(offset + 2);
  774. for (i = 0; i < count; i++) {
  775. uint16_t data = bytestream2_get_be16(&gb);
  776. pos += 2;
  777. noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
  778. bytestream2_seek_p(&pb, noffset, SEEK_SET);
  779. bytestream2_put_be16(&pb, data);
  780. }
  781. }
  782. }
  783. }
  784. }
  785. static void decode_byte_vertical_delta(uint8_t *dst,
  786. const uint8_t *buf, const uint8_t *buf_end,
  787. int w, int xor, int bpp, int dst_size)
  788. {
  789. int ncolumns = ((w + 15) / 16) * 2;
  790. int dstpitch = ncolumns * bpp;
  791. unsigned ofsdst, ofssrc, opcode, x;
  792. GetByteContext ptrs, gb;
  793. PutByteContext pb;
  794. int i, j, k;
  795. bytestream2_init(&ptrs, buf, buf_end - buf);
  796. bytestream2_init_writer(&pb, dst, dst_size);
  797. for (k = 0; k < bpp; k++) {
  798. ofssrc = bytestream2_get_be32(&ptrs);
  799. if (!ofssrc)
  800. continue;
  801. if (ofssrc >= buf_end - buf)
  802. continue;
  803. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  804. for (j = 0; j < ncolumns; j++) {
  805. ofsdst = j + k * ncolumns;
  806. i = bytestream2_get_byte(&gb);
  807. while (i > 0) {
  808. opcode = bytestream2_get_byte(&gb);
  809. if (opcode == 0) {
  810. opcode = bytestream2_get_byte(&gb);
  811. x = bytestream2_get_byte(&gb);
  812. while (opcode) {
  813. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  814. if (xor && ofsdst < dst_size) {
  815. bytestream2_put_byte(&pb, dst[ofsdst] ^ x);
  816. } else {
  817. bytestream2_put_byte(&pb, x);
  818. }
  819. ofsdst += dstpitch;
  820. opcode--;
  821. }
  822. } else if (opcode < 0x80) {
  823. ofsdst += opcode * dstpitch;
  824. } else {
  825. opcode &= 0x7f;
  826. while (opcode) {
  827. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  828. if (xor && ofsdst < dst_size) {
  829. bytestream2_put_byte(&pb, dst[ofsdst] ^ bytestream2_get_byte(&gb));
  830. } else {
  831. bytestream2_put_byte(&pb, bytestream2_get_byte(&gb));
  832. }
  833. ofsdst += dstpitch;
  834. opcode--;
  835. }
  836. }
  837. i--;
  838. }
  839. }
  840. }
  841. }
  842. static void decode_delta_j(uint8_t *dst,
  843. const uint8_t *buf, const uint8_t *buf_end,
  844. int w, int h, int bpp, int dst_size)
  845. {
  846. int32_t pitch;
  847. uint8_t *ptr;
  848. uint32_t type, flag, cols, groups, rows, bytes;
  849. uint32_t offset;
  850. int planepitch_byte = (w + 7) / 8;
  851. int planepitch = ((w + 15) / 16) * 2;
  852. int kludge_j, b, g, r, d;
  853. GetByteContext gb;
  854. pitch = planepitch * bpp;
  855. kludge_j = w < 320 ? (320 - w) / 8 / 2 : 0;
  856. bytestream2_init(&gb, buf, buf_end - buf);
  857. while (bytestream2_get_bytes_left(&gb) >= 2) {
  858. type = bytestream2_get_be16(&gb);
  859. switch (type) {
  860. case 0:
  861. return;
  862. case 1:
  863. flag = bytestream2_get_be16(&gb);
  864. cols = bytestream2_get_be16(&gb);
  865. groups = bytestream2_get_be16(&gb);
  866. for (g = 0; g < groups; g++) {
  867. offset = bytestream2_get_be16(&gb);
  868. if (cols * bpp == 0 || bytestream2_get_bytes_left(&gb) < cols * bpp) {
  869. av_log(NULL, AV_LOG_ERROR, "cols*bpp is invalid (%"PRId32"*%d)", cols, bpp);
  870. return;
  871. }
  872. if (kludge_j)
  873. offset = ((offset / (320 / 8)) * pitch) + (offset % (320 / 8)) - kludge_j;
  874. else
  875. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  876. for (b = 0; b < cols; b++) {
  877. for (d = 0; d < bpp; d++) {
  878. uint8_t value = bytestream2_get_byte(&gb);
  879. if (offset >= dst_size)
  880. return;
  881. ptr = dst + offset;
  882. if (flag)
  883. ptr[0] ^= value;
  884. else
  885. ptr[0] = value;
  886. offset += planepitch;
  887. }
  888. }
  889. if ((cols * bpp) & 1)
  890. bytestream2_skip(&gb, 1);
  891. }
  892. break;
  893. case 2:
  894. flag = bytestream2_get_be16(&gb);
  895. rows = bytestream2_get_be16(&gb);
  896. bytes = bytestream2_get_be16(&gb);
  897. groups = bytestream2_get_be16(&gb);
  898. for (g = 0; g < groups; g++) {
  899. offset = bytestream2_get_be16(&gb);
  900. if (kludge_j)
  901. offset = ((offset / (320 / 8)) * pitch) + (offset % (320/ 8)) - kludge_j;
  902. else
  903. offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
  904. for (r = 0; r < rows; r++) {
  905. for (d = 0; d < bpp; d++) {
  906. unsigned noffset = offset + (r * pitch) + d * planepitch;
  907. if (!bytes || bytestream2_get_bytes_left(&gb) < bytes) {
  908. av_log(NULL, AV_LOG_ERROR, "bytes %"PRId32" is invalid", bytes);
  909. return;
  910. }
  911. for (b = 0; b < bytes; b++) {
  912. uint8_t value = bytestream2_get_byte(&gb);
  913. if (noffset >= dst_size)
  914. return;
  915. ptr = dst + noffset;
  916. if (flag)
  917. ptr[0] ^= value;
  918. else
  919. ptr[0] = value;
  920. noffset++;
  921. }
  922. }
  923. }
  924. if ((rows * bytes * bpp) & 1)
  925. bytestream2_skip(&gb, 1);
  926. }
  927. break;
  928. default:
  929. return;
  930. }
  931. }
  932. }
  933. static void decode_short_vertical_delta(uint8_t *dst,
  934. const uint8_t *buf, const uint8_t *buf_end,
  935. int w, int bpp, int dst_size)
  936. {
  937. int ncolumns = (w + 15) >> 4;
  938. int dstpitch = ncolumns * bpp * 2;
  939. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  940. GetByteContext ptrs, gb, dptrs, dgb;
  941. PutByteContext pb;
  942. int i, j, k;
  943. if (buf_end - buf <= 64)
  944. return;
  945. bytestream2_init(&ptrs, buf, buf_end - buf);
  946. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  947. bytestream2_init_writer(&pb, dst, dst_size);
  948. for (k = 0; k < bpp; k++) {
  949. ofssrc = bytestream2_get_be32(&ptrs);
  950. ofsdata = bytestream2_get_be32(&dptrs);
  951. if (!ofssrc)
  952. continue;
  953. if (ofssrc >= buf_end - buf)
  954. return;
  955. if (ofsdata >= buf_end - buf)
  956. return;
  957. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  958. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  959. for (j = 0; j < ncolumns; j++) {
  960. ofsdst = (j + k * ncolumns) * 2;
  961. i = bytestream2_get_byte(&gb);
  962. while (i > 0) {
  963. opcode = bytestream2_get_byte(&gb);
  964. if (opcode == 0) {
  965. opcode = bytestream2_get_byte(&gb);
  966. x = bytestream2_get_be16(&dgb);
  967. while (opcode) {
  968. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  969. bytestream2_put_be16(&pb, x);
  970. ofsdst += dstpitch;
  971. opcode--;
  972. }
  973. } else if (opcode < 0x80) {
  974. ofsdst += opcode * dstpitch;
  975. } else {
  976. opcode &= 0x7f;
  977. while (opcode) {
  978. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  979. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  980. ofsdst += dstpitch;
  981. opcode--;
  982. }
  983. }
  984. i--;
  985. }
  986. }
  987. }
  988. }
  989. static void decode_long_vertical_delta(uint8_t *dst,
  990. const uint8_t *buf, const uint8_t *buf_end,
  991. int w, int bpp, int dst_size)
  992. {
  993. int ncolumns = (w + 31) >> 5;
  994. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  995. unsigned ofsdst, ofssrc, ofsdata, opcode, x;
  996. GetByteContext ptrs, gb, dptrs, dgb;
  997. PutByteContext pb;
  998. int i, j, k, h;
  999. if (buf_end - buf <= 64)
  1000. return;
  1001. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  1002. bytestream2_init(&ptrs, buf, buf_end - buf);
  1003. bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
  1004. bytestream2_init_writer(&pb, dst, dst_size);
  1005. for (k = 0; k < bpp; k++) {
  1006. ofssrc = bytestream2_get_be32(&ptrs);
  1007. ofsdata = bytestream2_get_be32(&dptrs);
  1008. if (!ofssrc)
  1009. continue;
  1010. if (ofssrc >= buf_end - buf)
  1011. return;
  1012. if (ofsdata >= buf_end - buf)
  1013. return;
  1014. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1015. bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
  1016. for (j = 0; j < ncolumns; j++) {
  1017. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  1018. i = bytestream2_get_byte(&gb);
  1019. while (i > 0) {
  1020. opcode = bytestream2_get_byte(&gb);
  1021. if (opcode == 0) {
  1022. opcode = bytestream2_get_byte(&gb);
  1023. if (h && (j == (ncolumns - 1))) {
  1024. x = bytestream2_get_be16(&dgb);
  1025. bytestream2_skip(&dgb, 2);
  1026. } else {
  1027. x = bytestream2_get_be32(&dgb);
  1028. }
  1029. if (ofsdst + (opcode - 1LL) * dstpitch > bytestream2_size_p(&pb))
  1030. return;
  1031. while (opcode) {
  1032. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1033. if (h && (j == (ncolumns - 1))) {
  1034. bytestream2_put_be16(&pb, x);
  1035. } else {
  1036. bytestream2_put_be32(&pb, x);
  1037. }
  1038. ofsdst += dstpitch;
  1039. opcode--;
  1040. }
  1041. } else if (opcode < 0x80) {
  1042. ofsdst += opcode * dstpitch;
  1043. } else {
  1044. opcode &= 0x7f;
  1045. while (opcode) {
  1046. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1047. if (h && (j == (ncolumns - 1))) {
  1048. bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
  1049. bytestream2_skip(&dgb, 2);
  1050. } else {
  1051. bytestream2_put_be32(&pb, bytestream2_get_be32(&dgb));
  1052. }
  1053. ofsdst += dstpitch;
  1054. opcode--;
  1055. }
  1056. }
  1057. i--;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. static void decode_short_vertical_delta2(uint8_t *dst,
  1063. const uint8_t *buf, const uint8_t *buf_end,
  1064. int w, int bpp, int dst_size)
  1065. {
  1066. int ncolumns = (w + 15) >> 4;
  1067. int dstpitch = ncolumns * bpp * 2;
  1068. unsigned ofsdst, ofssrc, opcode, x;
  1069. GetByteContext ptrs, gb;
  1070. PutByteContext pb;
  1071. int i, j, k;
  1072. bytestream2_init(&ptrs, buf, buf_end - buf);
  1073. bytestream2_init_writer(&pb, dst, dst_size);
  1074. for (k = 0; k < bpp; k++) {
  1075. ofssrc = bytestream2_get_be32(&ptrs);
  1076. if (!ofssrc)
  1077. continue;
  1078. if (ofssrc >= buf_end - buf)
  1079. continue;
  1080. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1081. for (j = 0; j < ncolumns; j++) {
  1082. ofsdst = (j + k * ncolumns) * 2;
  1083. i = bytestream2_get_be16(&gb);
  1084. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1085. opcode = bytestream2_get_be16(&gb);
  1086. if (opcode == 0) {
  1087. opcode = bytestream2_get_be16(&gb);
  1088. x = bytestream2_get_be16(&gb);
  1089. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1090. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1091. bytestream2_put_be16(&pb, x);
  1092. ofsdst += dstpitch;
  1093. opcode--;
  1094. }
  1095. } else if (opcode < 0x8000) {
  1096. ofsdst += opcode * dstpitch;
  1097. } else {
  1098. opcode &= 0x7fff;
  1099. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1100. bytestream2_get_bytes_left_p(&pb) > 1) {
  1101. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1102. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1103. ofsdst += dstpitch;
  1104. opcode--;
  1105. }
  1106. }
  1107. i--;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. static void decode_long_vertical_delta2(uint8_t *dst,
  1113. const uint8_t *buf, const uint8_t *buf_end,
  1114. int w, int bpp, int dst_size)
  1115. {
  1116. int ncolumns = (w + 31) >> 5;
  1117. int dstpitch = ((w + 15) / 16 * 2) * bpp;
  1118. unsigned ofsdst, ofssrc, opcode, x;
  1119. unsigned skip = 0x80000000, mask = skip - 1;
  1120. GetByteContext ptrs, gb;
  1121. PutByteContext pb;
  1122. int i, j, k, h;
  1123. h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
  1124. bytestream2_init(&ptrs, buf, buf_end - buf);
  1125. bytestream2_init_writer(&pb, dst, dst_size);
  1126. for (k = 0; k < bpp; k++) {
  1127. ofssrc = bytestream2_get_be32(&ptrs);
  1128. if (!ofssrc)
  1129. continue;
  1130. if (ofssrc >= buf_end - buf)
  1131. continue;
  1132. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1133. for (j = 0; j < ncolumns; j++) {
  1134. ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
  1135. if (h && (j == (ncolumns - 1))) {
  1136. skip = 0x8000;
  1137. mask = skip - 1;
  1138. }
  1139. i = bytestream2_get_be32(&gb);
  1140. while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
  1141. opcode = bytestream2_get_be32(&gb);
  1142. if (opcode == 0) {
  1143. if (h && (j == ncolumns - 1)) {
  1144. opcode = bytestream2_get_be16(&gb);
  1145. x = bytestream2_get_be16(&gb);
  1146. } else {
  1147. opcode = bytestream2_get_be32(&gb);
  1148. x = bytestream2_get_be32(&gb);
  1149. }
  1150. if (ofsdst + (opcode - 1LL) * dstpitch > bytestream2_size_p(&pb))
  1151. return;
  1152. while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
  1153. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1154. if (h && (j == ncolumns - 1))
  1155. bytestream2_put_be16(&pb, x);
  1156. else
  1157. bytestream2_put_be32(&pb, x);
  1158. ofsdst += dstpitch;
  1159. opcode--;
  1160. }
  1161. } else if (opcode < skip) {
  1162. ofsdst += opcode * dstpitch;
  1163. } else {
  1164. opcode &= mask;
  1165. while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
  1166. bytestream2_get_bytes_left_p(&pb) > 1) {
  1167. bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
  1168. if (h && (j == ncolumns - 1)) {
  1169. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1170. } else {
  1171. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1172. }
  1173. ofsdst += dstpitch;
  1174. opcode--;
  1175. }
  1176. }
  1177. i--;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. static void decode_delta_d(uint8_t *dst,
  1183. const uint8_t *buf, const uint8_t *buf_end,
  1184. int w, int flag, int bpp, int dst_size)
  1185. {
  1186. int planepitch = FFALIGN(w, 16) >> 3;
  1187. int pitch = planepitch * bpp;
  1188. int planepitch_byte = (w + 7) / 8;
  1189. unsigned entries, ofssrc;
  1190. GetByteContext gb, ptrs;
  1191. PutByteContext pb;
  1192. int k;
  1193. if (buf_end - buf <= 4 * bpp)
  1194. return;
  1195. bytestream2_init_writer(&pb, dst, dst_size);
  1196. bytestream2_init(&ptrs, buf, bpp * 4);
  1197. for (k = 0; k < bpp; k++) {
  1198. ofssrc = bytestream2_get_be32(&ptrs);
  1199. if (!ofssrc)
  1200. continue;
  1201. if (ofssrc >= buf_end - buf)
  1202. continue;
  1203. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1204. entries = bytestream2_get_be32(&gb);
  1205. while (entries && bytestream2_get_bytes_left(&gb) >= 8) {
  1206. int32_t opcode = bytestream2_get_be32(&gb);
  1207. unsigned offset = bytestream2_get_be32(&gb);
  1208. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1209. if (opcode >= 0) {
  1210. uint32_t x = bytestream2_get_be32(&gb);
  1211. if (opcode && 4 + (opcode - 1LL) * pitch > bytestream2_get_bytes_left_p(&pb))
  1212. continue;
  1213. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1214. bytestream2_put_be32(&pb, x);
  1215. bytestream2_skip_p(&pb, pitch - 4);
  1216. opcode--;
  1217. }
  1218. } else {
  1219. opcode = -opcode;
  1220. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1221. bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
  1222. bytestream2_skip_p(&pb, pitch - 4);
  1223. opcode--;
  1224. }
  1225. }
  1226. entries--;
  1227. }
  1228. }
  1229. }
  1230. static void decode_delta_e(uint8_t *dst,
  1231. const uint8_t *buf, const uint8_t *buf_end,
  1232. int w, int flag, int bpp, int dst_size)
  1233. {
  1234. int planepitch = FFALIGN(w, 16) >> 3;
  1235. int pitch = planepitch * bpp;
  1236. int planepitch_byte = (w + 7) / 8;
  1237. unsigned entries, ofssrc;
  1238. GetByteContext gb, ptrs;
  1239. PutByteContext pb;
  1240. int k;
  1241. if (buf_end - buf <= 4 * bpp)
  1242. return;
  1243. bytestream2_init_writer(&pb, dst, dst_size);
  1244. bytestream2_init(&ptrs, buf, bpp * 4);
  1245. for (k = 0; k < bpp; k++) {
  1246. ofssrc = bytestream2_get_be32(&ptrs);
  1247. if (!ofssrc)
  1248. continue;
  1249. if (ofssrc >= buf_end - buf)
  1250. continue;
  1251. bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
  1252. entries = bytestream2_get_be16(&gb);
  1253. while (entries && bytestream2_get_bytes_left(&gb) >= 6) {
  1254. int16_t opcode = bytestream2_get_be16(&gb);
  1255. unsigned offset = bytestream2_get_be32(&gb);
  1256. bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
  1257. if (opcode >= 0) {
  1258. uint16_t x = bytestream2_get_be16(&gb);
  1259. while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
  1260. bytestream2_put_be16(&pb, x);
  1261. bytestream2_skip_p(&pb, pitch - 2);
  1262. opcode--;
  1263. }
  1264. } else {
  1265. opcode = -opcode;
  1266. while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
  1267. bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
  1268. bytestream2_skip_p(&pb, pitch - 2);
  1269. opcode--;
  1270. }
  1271. }
  1272. entries--;
  1273. }
  1274. }
  1275. }
  1276. static void decode_delta_l(uint8_t *dst,
  1277. const uint8_t *buf, const uint8_t *buf_end,
  1278. int w, int flag, int bpp, int dst_size)
  1279. {
  1280. GetByteContext off0, off1, dgb, ogb;
  1281. PutByteContext pb;
  1282. unsigned poff0, poff1;
  1283. int i, k, dstpitch;
  1284. int planepitch_byte = (w + 7) / 8;
  1285. int planepitch = ((w + 15) / 16) * 2;
  1286. int pitch = planepitch * bpp;
  1287. if (buf_end - buf <= 64)
  1288. return;
  1289. bytestream2_init(&off0, buf, buf_end - buf);
  1290. bytestream2_init(&off1, buf + 32, buf_end - (buf + 32));
  1291. bytestream2_init_writer(&pb, dst, dst_size);
  1292. dstpitch = flag ? (((w + 7) / 8) * bpp): 2;
  1293. for (k = 0; k < bpp; k++) {
  1294. poff0 = bytestream2_get_be32(&off0);
  1295. poff1 = bytestream2_get_be32(&off1);
  1296. if (!poff0)
  1297. continue;
  1298. if (2LL * poff0 >= buf_end - buf)
  1299. return;
  1300. if (2LL * poff1 >= buf_end - buf)
  1301. return;
  1302. bytestream2_init(&dgb, buf + 2 * poff0, buf_end - (buf + 2 * poff0));
  1303. bytestream2_init(&ogb, buf + 2 * poff1, buf_end - (buf + 2 * poff1));
  1304. while (bytestream2_peek_be16(&ogb) != 0xFFFF && bytestream2_get_bytes_left(&ogb) >= 4) {
  1305. uint32_t offset = bytestream2_get_be16(&ogb);
  1306. int16_t cnt = bytestream2_get_be16(&ogb);
  1307. uint16_t data;
  1308. offset = ((2 * offset) / planepitch_byte) * pitch + ((2 * offset) % planepitch_byte) + k * planepitch;
  1309. if (cnt < 0) {
  1310. if (bytestream2_get_bytes_left(&dgb) < 2)
  1311. break;
  1312. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1313. cnt = -cnt;
  1314. data = bytestream2_get_be16(&dgb);
  1315. for (i = 0; i < cnt; i++) {
  1316. bytestream2_put_be16(&pb, data);
  1317. bytestream2_skip_p(&pb, dstpitch - 2);
  1318. }
  1319. } else {
  1320. if (bytestream2_get_bytes_left(&dgb) < 2*cnt)
  1321. break;
  1322. bytestream2_seek_p(&pb, offset, SEEK_SET);
  1323. for (i = 0; i < cnt; i++) {
  1324. data = bytestream2_get_be16(&dgb);
  1325. bytestream2_put_be16(&pb, data);
  1326. bytestream2_skip_p(&pb, dstpitch - 2);
  1327. }
  1328. }
  1329. }
  1330. }
  1331. }
  1332. static int unsupported(AVCodecContext *avctx)
  1333. {
  1334. IffContext *s = avctx->priv_data;
  1335. avpriv_request_sample(avctx, "bitmap (compression 0x%0x, bpp %i, ham %i, interlaced %i)", s->compression, s->bpp, s->ham, s->is_interlaced);
  1336. return AVERROR_INVALIDDATA;
  1337. }
  1338. static int decode_frame(AVCodecContext *avctx,
  1339. void *data, int *got_frame,
  1340. AVPacket *avpkt)
  1341. {
  1342. IffContext *s = avctx->priv_data;
  1343. AVFrame *frame = data;
  1344. const uint8_t *buf = avpkt->data;
  1345. int buf_size = avpkt->size;
  1346. const uint8_t *buf_end = buf + buf_size;
  1347. int y, plane, res;
  1348. GetByteContext *gb = &s->gb;
  1349. const AVPixFmtDescriptor *desc;
  1350. bytestream2_init(gb, avpkt->data, avpkt->size);
  1351. if ((res = extract_header(avctx, avpkt)) < 0)
  1352. return res;
  1353. if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
  1354. return res;
  1355. s->frame = frame;
  1356. buf += bytestream2_tell(gb);
  1357. buf_size -= bytestream2_tell(gb);
  1358. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1359. if (!s->init && avctx->bits_per_coded_sample <= 8 - (s->masking == MASK_HAS_MASK) &&
  1360. avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1361. if ((res = cmap_read_palette(avctx, (uint32_t *)frame->data[1])) < 0)
  1362. return res;
  1363. } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  1364. avctx->pix_fmt == AV_PIX_FMT_RGB32) {
  1365. if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  1366. return res;
  1367. }
  1368. s->init = 1;
  1369. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1370. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  1371. memcpy(s->pal, s->frame->data[1], 256 * 4);
  1372. }
  1373. switch (s->compression) {
  1374. case 0x0:
  1375. if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  1376. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1377. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1378. for (plane = 0; plane < s->bpp; plane++) {
  1379. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1380. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1381. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1382. buf += s->planesize;
  1383. }
  1384. }
  1385. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1386. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1387. for (y = 0; y < avctx->height; y++) {
  1388. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1389. memset(s->ham_buf, 0, s->planesize * 8);
  1390. for (plane = 0; plane < s->bpp; plane++) {
  1391. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  1392. if (start >= buf_end)
  1393. break;
  1394. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  1395. }
  1396. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1397. }
  1398. } else
  1399. return unsupported(avctx);
  1400. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1401. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  1402. int x;
  1403. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1404. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1405. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  1406. buf += raw_width;
  1407. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  1408. for (x = 0; x < avctx->width; x++)
  1409. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  1410. }
  1411. }
  1412. } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1413. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1414. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))
  1415. memcpy(s->video[0], buf, FFMIN(buf_end - buf, s->video_size));
  1416. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1417. for (y = 0; y < avctx->height; y++) {
  1418. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1419. memset(row, 0, avctx->width);
  1420. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1421. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1422. buf += s->planesize;
  1423. }
  1424. }
  1425. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1426. for (y = 0; y < avctx->height; y++) {
  1427. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1428. memset(s->ham_buf, 0, s->planesize * 8);
  1429. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1430. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1431. buf += s->planesize;
  1432. }
  1433. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1434. }
  1435. } else { // AV_PIX_FMT_BGR32
  1436. for (y = 0; y < avctx->height; y++) {
  1437. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1438. memset(row, 0, avctx->width << 2);
  1439. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  1440. decodeplane32((uint32_t *)row, buf,
  1441. FFMIN(s->planesize, buf_end - buf), plane);
  1442. buf += s->planesize;
  1443. }
  1444. }
  1445. }
  1446. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1447. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1448. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1449. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1450. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  1451. buf += avctx->width + (avctx->width % 2); // padding if odd
  1452. }
  1453. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1454. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  1455. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1456. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  1457. buf += avctx->width + (avctx->width & 1); // padding if odd
  1458. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1459. }
  1460. } else
  1461. return unsupported(avctx);
  1462. } else {
  1463. return unsupported(avctx);
  1464. }
  1465. break;
  1466. case 0x1:
  1467. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
  1468. avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1469. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1470. uint8_t *video = s->video[0];
  1471. for (y = 0; y < avctx->height; y++) {
  1472. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1473. memset(row, 0, avctx->width);
  1474. for (plane = 0; plane < s->bpp; plane++) {
  1475. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1476. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1477. memcpy(video, s->planebuf, s->planesize);
  1478. video += s->planesize;
  1479. }
  1480. decodeplane8(row, s->planebuf, s->planesize, plane);
  1481. }
  1482. }
  1483. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  1484. for (y = 0; y < avctx->height; y++) {
  1485. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1486. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  1487. for (plane = 0; plane < s->bpp; plane++) {
  1488. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1489. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  1490. }
  1491. lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
  1492. }
  1493. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1494. uint8_t *video = s->video[0];
  1495. for (y = 0; y < avctx->height; y++) {
  1496. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1497. memset(s->ham_buf, 0, s->planesize * 8);
  1498. for (plane = 0; plane < s->bpp; plane++) {
  1499. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1500. if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
  1501. memcpy(video, s->planebuf, s->planesize);
  1502. video += s->planesize;
  1503. }
  1504. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  1505. }
  1506. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1507. }
  1508. } else { // AV_PIX_FMT_BGR32
  1509. for (y = 0; y < avctx->height; y++) {
  1510. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1511. memset(row, 0, avctx->width << 2);
  1512. for (plane = 0; plane < s->bpp; plane++) {
  1513. buf += decode_byterun(s->planebuf, s->planesize, gb);
  1514. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  1515. }
  1516. }
  1517. }
  1518. } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
  1519. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1520. for (y = 0; y < avctx->height; y++) {
  1521. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1522. buf += decode_byterun(row, avctx->width, gb);
  1523. }
  1524. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  1525. for (y = 0; y < avctx->height; y++) {
  1526. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1527. buf += decode_byterun(s->ham_buf, avctx->width, gb);
  1528. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1529. }
  1530. } else
  1531. return unsupported(avctx);
  1532. } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
  1533. if (av_get_bits_per_pixel(desc) == 32)
  1534. decode_deep_rle32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0]);
  1535. else
  1536. return unsupported(avctx);
  1537. } else if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
  1538. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1539. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1540. for (plane = 0; plane < s->bpp; plane++) {
  1541. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  1542. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1543. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  1544. buf += s->planesize;
  1545. }
  1546. }
  1547. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  1548. memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
  1549. for (y = 0; y < avctx->height; y++) {
  1550. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1551. memset(s->ham_buf, 0, s->planesize * 8);
  1552. for (plane = 0; plane < s->bpp; plane++) {
  1553. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  1554. if (start >= buf_end)
  1555. break;
  1556. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  1557. }
  1558. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1559. }
  1560. } else {
  1561. return unsupported(avctx);
  1562. }
  1563. } else {
  1564. return unsupported(avctx);
  1565. }
  1566. break;
  1567. case 0x2:
  1568. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  1569. for (plane = 0; plane < s->bpp; plane++) {
  1570. decode_byterun2(s->planebuf, avctx->height, s->planesize, gb);
  1571. for (y = 0; y < avctx->height; y++) {
  1572. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1573. decodeplane8(row, s->planebuf + s->planesize * y, s->planesize, plane);
  1574. }
  1575. }
  1576. } else {
  1577. return unsupported(avctx);
  1578. }
  1579. break;
  1580. case 0x4:
  1581. if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8') && avctx->pix_fmt == AV_PIX_FMT_RGB32)
  1582. decode_rgb8(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1583. else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N') && avctx->pix_fmt == AV_PIX_FMT_RGB444)
  1584. decode_rgbn(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
  1585. else
  1586. return unsupported(avctx);
  1587. break;
  1588. case 0x5:
  1589. if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
  1590. if (av_get_bits_per_pixel(desc) == 32)
  1591. decode_deep_tvdc32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0], s->tvdc);
  1592. else
  1593. return unsupported(avctx);
  1594. } else
  1595. return unsupported(avctx);
  1596. break;
  1597. case 0x300:
  1598. case 0x301:
  1599. decode_short_horizontal_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1600. break;
  1601. case 0x500:
  1602. case 0x501:
  1603. decode_byte_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->is_brush, s->bpp, s->video_size);
  1604. break;
  1605. case 0x700:
  1606. case 0x701:
  1607. if (s->is_short)
  1608. decode_short_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1609. else
  1610. decode_long_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1611. break;
  1612. case 0x800:
  1613. case 0x801:
  1614. if (s->is_short)
  1615. decode_short_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1616. else
  1617. decode_long_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
  1618. break;
  1619. case 0x4a00:
  1620. case 0x4a01:
  1621. decode_delta_j(s->video[0], buf, buf_end, avctx->width, avctx->height, s->bpp, s->video_size);
  1622. break;
  1623. case 0x6400:
  1624. case 0x6401:
  1625. if (s->is_interlaced)
  1626. return unsupported(avctx);
  1627. decode_delta_d(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1628. break;
  1629. case 0x6500:
  1630. case 0x6501:
  1631. if (s->is_interlaced)
  1632. return unsupported(avctx);
  1633. decode_delta_e(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
  1634. break;
  1635. case 0x6c00:
  1636. case 0x6c01:
  1637. decode_delta_l(s->video[0], buf, buf_end, avctx->width, s->is_short, s->bpp, s->video_size);
  1638. break;
  1639. default:
  1640. return unsupported(avctx);
  1641. }
  1642. if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
  1643. memcpy(s->video[1], s->video[0], s->video_size);
  1644. }
  1645. if (s->compression > 0xff) {
  1646. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  1647. buf = s->video[0];
  1648. for (y = 0; y < avctx->height; y++) {
  1649. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1650. memset(row, 0, avctx->width);
  1651. for (plane = 0; plane < s->bpp; plane++) {
  1652. decodeplane8(row, buf, s->planesize, plane);
  1653. buf += s->planesize;
  1654. }
  1655. }
  1656. memcpy(frame->data[1], s->pal, 256 * 4);
  1657. } else if (s->ham) {
  1658. int i, count = 1 << s->ham;
  1659. buf = s->video[0];
  1660. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof(uint32_t));
  1661. for (i = 0; i < count; i++) {
  1662. s->ham_palbuf[i*2+1] = s->pal[i];
  1663. }
  1664. for (i = 0; i < count; i++) {
  1665. uint32_t tmp = i << (8 - s->ham);
  1666. tmp |= tmp >> s->ham;
  1667. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF;
  1668. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00;
  1669. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF;
  1670. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  1671. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  1672. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  1673. }
  1674. if (s->masking == MASK_HAS_MASK) {
  1675. for (i = 0; i < 8 * (1 << s->ham); i++)
  1676. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  1677. }
  1678. for (y = 0; y < avctx->height; y++) {
  1679. uint8_t *row = &frame->data[0][y * frame->linesize[0]];
  1680. memset(s->ham_buf, 0, s->planesize * 8);
  1681. for (plane = 0; plane < s->bpp; plane++) {
  1682. decodeplane8(s->ham_buf, buf, s->planesize, plane);
  1683. buf += s->planesize;
  1684. }
  1685. decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
  1686. }
  1687. } else {
  1688. return unsupported(avctx);
  1689. }
  1690. if (!s->is_brush) {
  1691. FFSWAP(uint8_t *, s->video[0], s->video[1]);
  1692. }
  1693. }
  1694. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  1695. frame->key_frame = 1;
  1696. frame->pict_type = AV_PICTURE_TYPE_I;
  1697. } else {
  1698. frame->key_frame = 0;
  1699. frame->pict_type = AV_PICTURE_TYPE_P;
  1700. }
  1701. *got_frame = 1;
  1702. return buf_size;
  1703. }
  1704. #if CONFIG_IFF_ILBM_DECODER
  1705. AVCodec ff_iff_ilbm_decoder = {
  1706. .name = "iff",
  1707. .long_name = NULL_IF_CONFIG_SMALL("IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN"),
  1708. .type = AVMEDIA_TYPE_VIDEO,
  1709. .id = AV_CODEC_ID_IFF_ILBM,
  1710. .priv_data_size = sizeof(IffContext),
  1711. .init = decode_init,
  1712. .close = decode_end,
  1713. .decode = decode_frame,
  1714. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1715. .capabilities = AV_CODEC_CAP_DR1,
  1716. };
  1717. #endif