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.

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