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.

730 lines
29KB

  1. /*
  2. * IFF PBM/ILBM bitmap decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * IFF PBM/ILBM bitmap decoder
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "bytestream.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. // TODO: masking bits
  31. typedef enum {
  32. MASK_NONE,
  33. MASK_HAS_MASK,
  34. MASK_HAS_TRANSPARENT_COLOR,
  35. MASK_LASSO
  36. } mask_type;
  37. typedef struct {
  38. AVFrame frame;
  39. int planesize;
  40. uint8_t * planebuf;
  41. uint8_t * ham_buf; ///< temporary buffer for planar to chunky conversation
  42. uint32_t *ham_palbuf; ///< HAM decode table
  43. uint32_t *mask_buf; ///< temporary buffer for palette indices
  44. uint32_t *mask_palbuf; ///< masking palette table
  45. unsigned compression; ///< delta compression method used
  46. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  47. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  48. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  49. unsigned transparency; ///< TODO: transparency color index in palette
  50. unsigned masking; ///< TODO: masking method used
  51. int init; // 1 if buffer and palette data already initialized, 0 otherwise
  52. } IffContext;
  53. #define LUT8_PART(plane, v) \
  54. AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
  55. AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
  56. AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
  57. AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
  58. AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
  59. AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
  60. AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
  61. AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
  62. AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
  63. AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
  64. AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
  65. AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
  66. AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
  67. AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
  68. AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
  69. AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
  70. #define LUT8(plane) { \
  71. LUT8_PART(plane, 0x0000000), \
  72. LUT8_PART(plane, 0x1000000), \
  73. LUT8_PART(plane, 0x0010000), \
  74. LUT8_PART(plane, 0x1010000), \
  75. LUT8_PART(plane, 0x0000100), \
  76. LUT8_PART(plane, 0x1000100), \
  77. LUT8_PART(plane, 0x0010100), \
  78. LUT8_PART(plane, 0x1010100), \
  79. LUT8_PART(plane, 0x0000001), \
  80. LUT8_PART(plane, 0x1000001), \
  81. LUT8_PART(plane, 0x0010001), \
  82. LUT8_PART(plane, 0x1010001), \
  83. LUT8_PART(plane, 0x0000101), \
  84. LUT8_PART(plane, 0x1000101), \
  85. LUT8_PART(plane, 0x0010101), \
  86. LUT8_PART(plane, 0x1010101), \
  87. }
  88. // 8 planes * 8-bit mask
  89. static const uint64_t plane8_lut[8][256] = {
  90. LUT8(0), LUT8(1), LUT8(2), LUT8(3),
  91. LUT8(4), LUT8(5), LUT8(6), LUT8(7),
  92. };
  93. #define LUT32(plane) { \
  94. 0, 0, 0, 0, \
  95. 0, 0, 0, 1 << plane, \
  96. 0, 0, 1 << plane, 0, \
  97. 0, 0, 1 << plane, 1 << plane, \
  98. 0, 1 << plane, 0, 0, \
  99. 0, 1 << plane, 0, 1 << plane, \
  100. 0, 1 << plane, 1 << plane, 0, \
  101. 0, 1 << plane, 1 << plane, 1 << plane, \
  102. 1 << plane, 0, 0, 0, \
  103. 1 << plane, 0, 0, 1 << plane, \
  104. 1 << plane, 0, 1 << plane, 0, \
  105. 1 << plane, 0, 1 << plane, 1 << plane, \
  106. 1 << plane, 1 << plane, 0, 0, \
  107. 1 << plane, 1 << plane, 0, 1 << plane, \
  108. 1 << plane, 1 << plane, 1 << plane, 0, \
  109. 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
  110. }
  111. // 32 planes * 4-bit mask * 4 lookup tables each
  112. static const uint32_t plane32_lut[32][16*4] = {
  113. LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
  114. LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
  115. LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
  116. LUT32(12), LUT32(13), LUT32(14), LUT32(15),
  117. LUT32(16), LUT32(17), LUT32(18), LUT32(19),
  118. LUT32(20), LUT32(21), LUT32(22), LUT32(23),
  119. LUT32(24), LUT32(25), LUT32(26), LUT32(27),
  120. LUT32(28), LUT32(29), LUT32(30), LUT32(31),
  121. };
  122. // Gray to RGB, required for palette table of grayscale images with bpp < 8
  123. static av_always_inline uint32_t gray2rgb(const uint32_t x) {
  124. return x << 16 | x << 8 | x;
  125. }
  126. /**
  127. * Convert CMAP buffer (stored in extradata) to lavc palette format
  128. */
  129. static int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  130. {
  131. IffContext *s = avctx->priv_data;
  132. int count, i;
  133. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  134. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  135. if (avctx->bits_per_coded_sample > 8) {
  136. av_log(avctx, AV_LOG_ERROR, "bits_per_coded_sample > 8 not supported\n");
  137. return AVERROR_INVALIDDATA;
  138. }
  139. count = 1 << avctx->bits_per_coded_sample;
  140. // If extradata is smaller than actually needed, fill the remaining with black.
  141. count = FFMIN(palette_size / 3, count);
  142. if (count) {
  143. for (i=0; i < count; i++) {
  144. pal[i] = 0xFF000000 | AV_RB24(palette + i*3);
  145. }
  146. if (s->flags && count >= 32) { // EHB
  147. for (i = 0; i < 32; i++)
  148. pal[i + 32] = 0xFF000000 | (AV_RB24(palette + i*3) & 0xFEFEFE) >> 1;
  149. count = FFMAX(count, 64);
  150. }
  151. } else { // Create gray-scale color palette for bps < 8
  152. count = 1 << avctx->bits_per_coded_sample;
  153. for (i=0; i < count; i++) {
  154. pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
  155. }
  156. }
  157. if (s->masking == MASK_HAS_MASK) {
  158. memcpy(pal + (1 << avctx->bits_per_coded_sample), pal, count * 4);
  159. for (i = 0; i < count; i++)
  160. pal[i] &= 0xFFFFFF;
  161. } else if (s->masking == MASK_HAS_TRANSPARENT_COLOR &&
  162. s->transparency < 1 << avctx->bits_per_coded_sample)
  163. pal[s->transparency] &= 0xFFFFFF;
  164. return 0;
  165. }
  166. /**
  167. * Extracts the IFF extra context and updates internal
  168. * decoder structures.
  169. *
  170. * @param avctx the AVCodecContext where to extract extra context to
  171. * @param avpkt the AVPacket to extract extra context from or NULL to use avctx
  172. * @return 0 in case of success, a negative error code otherwise
  173. */
  174. static int extract_header(AVCodecContext *const avctx,
  175. const AVPacket *const avpkt) {
  176. const uint8_t *buf;
  177. unsigned buf_size;
  178. IffContext *s = avctx->priv_data;
  179. int palette_size;
  180. if (avctx->extradata_size < 2) {
  181. av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
  182. return AVERROR_INVALIDDATA;
  183. }
  184. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  185. if (avpkt) {
  186. int image_size;
  187. if (avpkt->size < 2)
  188. return AVERROR_INVALIDDATA;
  189. image_size = avpkt->size - AV_RB16(avpkt->data);
  190. buf = avpkt->data;
  191. buf_size = bytestream_get_be16(&buf);
  192. if (buf_size <= 1 || image_size <= 1) {
  193. av_log(avctx, AV_LOG_ERROR,
  194. "Invalid image size received: %u -> image data offset: %d\n",
  195. buf_size, image_size);
  196. return AVERROR_INVALIDDATA;
  197. }
  198. } else {
  199. buf = avctx->extradata;
  200. buf_size = bytestream_get_be16(&buf);
  201. if (buf_size <= 1 || palette_size < 0) {
  202. av_log(avctx, AV_LOG_ERROR,
  203. "Invalid palette size received: %u -> palette data offset: %d\n",
  204. buf_size, palette_size);
  205. return AVERROR_INVALIDDATA;
  206. }
  207. }
  208. if (buf_size > 8) {
  209. s->compression = bytestream_get_byte(&buf);
  210. s->bpp = bytestream_get_byte(&buf);
  211. s->ham = bytestream_get_byte(&buf);
  212. s->flags = bytestream_get_byte(&buf);
  213. s->transparency = bytestream_get_be16(&buf);
  214. s->masking = bytestream_get_byte(&buf);
  215. if (s->masking == MASK_HAS_MASK) {
  216. if (s->bpp >= 8) {
  217. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  218. av_freep(&s->mask_buf);
  219. av_freep(&s->mask_palbuf);
  220. s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);
  221. if (!s->mask_buf)
  222. return AVERROR(ENOMEM);
  223. if (s->bpp > 16) {
  224. av_log(avctx, AV_LOG_ERROR, "bpp %d too large for palette\n", s->bpp);
  225. av_freep(&s->mask_buf);
  226. return AVERROR(ENOMEM);
  227. }
  228. s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  229. if (!s->mask_palbuf) {
  230. av_freep(&s->mask_buf);
  231. return AVERROR(ENOMEM);
  232. }
  233. }
  234. s->bpp++;
  235. } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {
  236. av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
  237. return AVERROR_PATCHWELCOME;
  238. }
  239. if (!s->bpp || s->bpp > 32) {
  240. av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
  241. return AVERROR_INVALIDDATA;
  242. } else if (s->ham >= 8) {
  243. av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
  244. return AVERROR_INVALIDDATA;
  245. }
  246. av_freep(&s->ham_buf);
  247. av_freep(&s->ham_palbuf);
  248. if (s->ham) {
  249. int i, count = FFMIN(palette_size / 3, 1 << s->ham);
  250. int ham_count;
  251. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  252. s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);
  253. if (!s->ham_buf)
  254. return AVERROR(ENOMEM);
  255. ham_count = 8 * (1 << s->ham);
  256. s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  257. if (!s->ham_palbuf) {
  258. av_freep(&s->ham_buf);
  259. return AVERROR(ENOMEM);
  260. }
  261. if (count) { // HAM with color palette attached
  262. // prefill with black and palette and set HAM take direct value mask to zero
  263. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
  264. for (i=0; i < count; i++) {
  265. s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);
  266. }
  267. count = 1 << s->ham;
  268. } else { // HAM with grayscale color palette
  269. count = 1 << s->ham;
  270. for (i=0; i < count; i++) {
  271. s->ham_palbuf[i*2] = 0xFF000000; // take direct color value from palette
  272. s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));
  273. }
  274. }
  275. for (i=0; i < count; i++) {
  276. uint32_t tmp = i << (8 - s->ham);
  277. tmp |= tmp >> s->ham;
  278. s->ham_palbuf[(i+count)*2] = 0xFF00FFFF; // just modify blue color component
  279. s->ham_palbuf[(i+count*2)*2] = 0xFFFFFF00; // just modify red color component
  280. s->ham_palbuf[(i+count*3)*2] = 0xFFFF00FF; // just modify green color component
  281. s->ham_palbuf[(i+count)*2+1] = 0xFF000000 | tmp << 16;
  282. s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
  283. s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
  284. }
  285. if (s->masking == MASK_HAS_MASK) {
  286. for (i = 0; i < ham_count; i++)
  287. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  288. }
  289. }
  290. }
  291. return 0;
  292. }
  293. static av_cold int decode_init(AVCodecContext *avctx)
  294. {
  295. IffContext *s = avctx->priv_data;
  296. int err;
  297. if (avctx->bits_per_coded_sample <= 8) {
  298. int palette_size;
  299. if (avctx->extradata_size >= 2)
  300. palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  301. else
  302. palette_size = 0;
  303. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  304. (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  305. } else if (avctx->bits_per_coded_sample <= 32) {
  306. if (avctx->codec_tag != MKTAG('D','E','E','P')) {
  307. if (avctx->bits_per_coded_sample == 24) {
  308. avctx->pix_fmt = AV_PIX_FMT_RGB0;
  309. } else if (avctx->bits_per_coded_sample == 32) {
  310. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  311. } else {
  312. av_log_ask_for_sample(avctx, "unknown bits_per_coded_sample\n");
  313. return AVERROR_PATCHWELCOME;
  314. }
  315. }
  316. } else {
  317. return AVERROR_INVALIDDATA;
  318. }
  319. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  320. return err;
  321. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  322. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  323. if (!s->planebuf)
  324. return AVERROR(ENOMEM);
  325. s->bpp = avctx->bits_per_coded_sample;
  326. avcodec_get_frame_defaults(&s->frame);
  327. if ((err = extract_header(avctx, NULL)) < 0)
  328. return err;
  329. s->frame.reference = 3;
  330. return 0;
  331. }
  332. /**
  333. * Decode interleaved plane buffer up to 8bpp
  334. * @param dst Destination buffer
  335. * @param buf Source buffer
  336. * @param buf_size
  337. * @param plane plane number to decode as
  338. */
  339. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  340. {
  341. const uint64_t *lut = plane8_lut[plane];
  342. if (plane >= 8) {
  343. av_log(0, AV_LOG_WARNING, "Ignoring extra planes beyond 8\n");
  344. return;
  345. }
  346. do {
  347. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  348. AV_WN64A(dst, v);
  349. dst += 8;
  350. } while (--buf_size);
  351. }
  352. /**
  353. * Decode interleaved plane buffer up to 24bpp
  354. * @param dst Destination buffer
  355. * @param buf Source buffer
  356. * @param buf_size
  357. * @param plane plane number to decode as
  358. */
  359. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  360. {
  361. const uint32_t *lut = plane32_lut[plane];
  362. do {
  363. unsigned mask = (*buf >> 2) & ~3;
  364. dst[0] |= lut[mask++];
  365. dst[1] |= lut[mask++];
  366. dst[2] |= lut[mask++];
  367. dst[3] |= lut[mask];
  368. mask = (*buf++ << 2) & 0x3F;
  369. dst[4] |= lut[mask++];
  370. dst[5] |= lut[mask++];
  371. dst[6] |= lut[mask++];
  372. dst[7] |= lut[mask];
  373. dst += 8;
  374. } while (--buf_size);
  375. }
  376. #define DECODE_HAM_PLANE32(x) \
  377. first = buf[x] << 1; \
  378. second = buf[(x)+1] << 1; \
  379. delta &= pal[first++]; \
  380. delta |= pal[first]; \
  381. dst[x] = delta; \
  382. delta &= pal[second++]; \
  383. delta |= pal[second]; \
  384. dst[(x)+1] = delta
  385. /**
  386. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  387. *
  388. * @param dst the destination 24bpp buffer
  389. * @param buf the source 8bpp chunky buffer
  390. * @param pal the HAM decode table
  391. * @param buf_size the plane size in bytes
  392. */
  393. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  394. const uint32_t *const pal, unsigned buf_size)
  395. {
  396. uint32_t delta = 0;
  397. do {
  398. uint32_t first, second;
  399. DECODE_HAM_PLANE32(0);
  400. DECODE_HAM_PLANE32(2);
  401. DECODE_HAM_PLANE32(4);
  402. DECODE_HAM_PLANE32(6);
  403. buf += 8;
  404. dst += 8;
  405. } while (--buf_size);
  406. }
  407. static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
  408. const uint32_t *const pal, unsigned width)
  409. {
  410. do {
  411. *dst++ = pal[*buf++];
  412. } while (--width);
  413. }
  414. /**
  415. * Decode one complete byterun1 encoded line.
  416. *
  417. * @param dst the destination buffer where to store decompressed bitstream
  418. * @param dst_size the destination plane size in bytes
  419. * @param buf the source byterun1 compressed bitstream
  420. * @param buf_end the EOF of source byterun1 compressed bitstream
  421. * @return number of consumed bytes in byterun1 compressed bitstream
  422. */
  423. static int decode_byterun(uint8_t *dst, int dst_size,
  424. const uint8_t *buf, const uint8_t *const buf_end) {
  425. const uint8_t *const buf_start = buf;
  426. unsigned x;
  427. for (x = 0; x < dst_size && buf < buf_end;) {
  428. unsigned length;
  429. const int8_t value = *buf++;
  430. if (value >= 0) {
  431. length = value + 1;
  432. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  433. buf += length;
  434. } else if (value > -128) {
  435. length = -value + 1;
  436. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  437. } else { // noop
  438. continue;
  439. }
  440. x += length;
  441. }
  442. return buf - buf_start;
  443. }
  444. static int decode_frame_ilbm(AVCodecContext *avctx,
  445. void *data, int *data_size,
  446. AVPacket *avpkt)
  447. {
  448. IffContext *s = avctx->priv_data;
  449. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  450. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  451. const uint8_t *buf_end = buf+buf_size;
  452. int y, plane, res;
  453. if ((res = extract_header(avctx, avpkt)) < 0)
  454. return res;
  455. if (s->init) {
  456. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  457. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  458. return res;
  459. }
  460. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  461. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  462. return res;
  463. } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  464. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  465. return res;
  466. }
  467. s->init = 1;
  468. if (avctx->codec_tag == MKTAG('A','C','B','M')) {
  469. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  470. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  471. for (plane = 0; plane < s->bpp; plane++) {
  472. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  473. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  474. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  475. buf += s->planesize;
  476. }
  477. }
  478. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  479. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  480. for(y = 0; y < avctx->height; y++) {
  481. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  482. memset(s->ham_buf, 0, s->planesize * 8);
  483. for (plane = 0; plane < s->bpp; plane++) {
  484. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  485. if (start >= buf_end)
  486. break;
  487. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  488. }
  489. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  490. }
  491. }
  492. } else if (avctx->codec_tag == MKTAG('D','E','E','P')) {
  493. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  494. int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
  495. int x;
  496. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  497. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  498. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  499. buf += raw_width;
  500. if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
  501. for(x = 0; x < avctx->width; x++)
  502. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  503. }
  504. }
  505. } else if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
  506. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  507. for(y = 0; y < avctx->height; y++ ) {
  508. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  509. memset(row, 0, avctx->width);
  510. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  511. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  512. buf += s->planesize;
  513. }
  514. }
  515. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  516. for (y = 0; y < avctx->height; y++) {
  517. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  518. memset(s->ham_buf, 0, s->planesize * 8);
  519. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  520. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  521. buf += s->planesize;
  522. }
  523. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  524. }
  525. } else { // AV_PIX_FMT_BGR32
  526. for(y = 0; y < avctx->height; y++ ) {
  527. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  528. memset(row, 0, avctx->width << 2);
  529. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  530. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  531. buf += s->planesize;
  532. }
  533. }
  534. }
  535. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  536. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  537. for(y = 0; y < avctx->height && buf_end > buf; y++ ) {
  538. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  539. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  540. buf += avctx->width + (avctx->width % 2); // padding if odd
  541. }
  542. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  543. for (y = 0; y < avctx->height && buf_end > buf; y++) {
  544. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  545. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  546. buf += avctx->width + (avctx->width & 1); // padding if odd
  547. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  548. }
  549. } else {
  550. av_log_ask_for_sample(avctx, "unsupported bpp\n");
  551. return AVERROR_INVALIDDATA;
  552. }
  553. }
  554. *data_size = sizeof(AVFrame);
  555. *(AVFrame*)data = s->frame;
  556. return buf_size;
  557. }
  558. static int decode_frame_byterun1(AVCodecContext *avctx,
  559. void *data, int *data_size,
  560. AVPacket *avpkt)
  561. {
  562. IffContext *s = avctx->priv_data;
  563. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  564. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  565. const uint8_t *buf_end = buf+buf_size;
  566. int y, plane, res;
  567. if ((res = extract_header(avctx, avpkt)) < 0)
  568. return res;
  569. if (s->init) {
  570. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  571. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  572. return res;
  573. }
  574. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  575. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  576. return res;
  577. } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  578. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  579. return res;
  580. } else if (avctx->pix_fmt == AV_PIX_FMT_RGB32 && avctx->bits_per_coded_sample <= 8) {
  581. if ((res = ff_cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  582. return res;
  583. }
  584. s->init = 1;
  585. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  586. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  587. for(y = 0; y < avctx->height ; y++ ) {
  588. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  589. memset(row, 0, avctx->width);
  590. for (plane = 0; plane < s->bpp; plane++) {
  591. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  592. decodeplane8(row, s->planebuf, s->planesize, plane);
  593. }
  594. }
  595. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
  596. for (y = 0; y < avctx->height ; y++ ) {
  597. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  598. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  599. for (plane = 0; plane < s->bpp; plane++) {
  600. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  601. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  602. }
  603. lookup_pal_indicies((uint32_t *) row, s->mask_buf, s->mask_palbuf, avctx->width);
  604. }
  605. } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
  606. for (y = 0; y < avctx->height ; y++) {
  607. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  608. memset(s->ham_buf, 0, s->planesize * 8);
  609. for (plane = 0; plane < s->bpp; plane++) {
  610. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  611. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  612. }
  613. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  614. }
  615. } else { //AV_PIX_FMT_BGR32
  616. for(y = 0; y < avctx->height ; y++ ) {
  617. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  618. memset(row, 0, avctx->width << 2);
  619. for (plane = 0; plane < s->bpp; plane++) {
  620. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  621. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  622. }
  623. }
  624. }
  625. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  626. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  627. for(y = 0; y < avctx->height ; y++ ) {
  628. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  629. buf += decode_byterun(row, avctx->width, buf, buf_end);
  630. }
  631. } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
  632. for (y = 0; y < avctx->height ; y++) {
  633. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  634. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  635. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  636. }
  637. } else {
  638. av_log_ask_for_sample(avctx, "unsupported bpp\n");
  639. return AVERROR_INVALIDDATA;
  640. }
  641. }
  642. *data_size = sizeof(AVFrame);
  643. *(AVFrame*)data = s->frame;
  644. return buf_size;
  645. }
  646. static av_cold int decode_end(AVCodecContext *avctx)
  647. {
  648. IffContext *s = avctx->priv_data;
  649. if (s->frame.data[0])
  650. avctx->release_buffer(avctx, &s->frame);
  651. av_freep(&s->planebuf);
  652. av_freep(&s->ham_buf);
  653. av_freep(&s->ham_palbuf);
  654. return 0;
  655. }
  656. #if CONFIG_IFF_ILBM_DECODER
  657. AVCodec ff_iff_ilbm_decoder = {
  658. .name = "iff_ilbm",
  659. .type = AVMEDIA_TYPE_VIDEO,
  660. .id = AV_CODEC_ID_IFF_ILBM,
  661. .priv_data_size = sizeof(IffContext),
  662. .init = decode_init,
  663. .close = decode_end,
  664. .decode = decode_frame_ilbm,
  665. .capabilities = CODEC_CAP_DR1,
  666. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  667. };
  668. #endif
  669. #if CONFIG_IFF_BYTERUN1_DECODER
  670. AVCodec ff_iff_byterun1_decoder = {
  671. .name = "iff_byterun1",
  672. .type = AVMEDIA_TYPE_VIDEO,
  673. .id = AV_CODEC_ID_IFF_BYTERUN1,
  674. .priv_data_size = sizeof(IffContext),
  675. .init = decode_init,
  676. .close = decode_end,
  677. .decode = decode_frame_byterun1,
  678. .capabilities = CODEC_CAP_DR1,
  679. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  680. };
  681. #endif