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.

1568 lines
58KB

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