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.

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