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.

1915 lines
71KB

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