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.

700 lines
28KB

  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, "bit_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 = avctx->extradata_size - AV_RB16(avctx->extradata);
  180. if (avpkt) {
  181. int image_size;
  182. if (avpkt->size < 2)
  183. return AVERROR_INVALIDDATA;
  184. image_size = avpkt->size - AV_RB16(avpkt->data);
  185. buf = avpkt->data;
  186. buf_size = bytestream_get_be16(&buf);
  187. if (buf_size <= 1 || image_size <= 1) {
  188. av_log(avctx, AV_LOG_ERROR,
  189. "Invalid image size received: %u -> image data offset: %d\n",
  190. buf_size, image_size);
  191. return AVERROR_INVALIDDATA;
  192. }
  193. } else {
  194. if (avctx->extradata_size < 2)
  195. return AVERROR_INVALIDDATA;
  196. buf = avctx->extradata;
  197. buf_size = bytestream_get_be16(&buf);
  198. if (buf_size <= 1 || palette_size < 0) {
  199. av_log(avctx, AV_LOG_ERROR,
  200. "Invalid palette size received: %u -> palette data offset: %d\n",
  201. buf_size, palette_size);
  202. return AVERROR_INVALIDDATA;
  203. }
  204. }
  205. if (buf_size > 8) {
  206. s->compression = bytestream_get_byte(&buf);
  207. s->bpp = bytestream_get_byte(&buf);
  208. s->ham = bytestream_get_byte(&buf);
  209. s->flags = bytestream_get_byte(&buf);
  210. s->transparency = bytestream_get_be16(&buf);
  211. s->masking = bytestream_get_byte(&buf);
  212. if (s->masking == MASK_HAS_MASK) {
  213. if (s->bpp >= 8) {
  214. avctx->pix_fmt = PIX_FMT_RGB32;
  215. av_freep(&s->mask_buf);
  216. av_freep(&s->mask_palbuf);
  217. s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);
  218. if (!s->mask_buf)
  219. return AVERROR(ENOMEM);
  220. s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  221. if (!s->mask_palbuf) {
  222. av_freep(&s->mask_buf);
  223. return AVERROR(ENOMEM);
  224. }
  225. }
  226. s->bpp++;
  227. } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {
  228. av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
  229. return AVERROR_PATCHWELCOME;
  230. }
  231. if (!s->bpp || s->bpp > 32) {
  232. av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
  233. return AVERROR_INVALIDDATA;
  234. } else if (s->ham >= 8) {
  235. av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
  236. return AVERROR_INVALIDDATA;
  237. }
  238. av_freep(&s->ham_buf);
  239. av_freep(&s->ham_palbuf);
  240. if (s->ham) {
  241. int i, count = FFMIN(palette_size / 3, 1 << s->ham);
  242. int ham_count;
  243. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  244. s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);
  245. if (!s->ham_buf)
  246. return AVERROR(ENOMEM);
  247. ham_count = 8 * (1 << s->ham);
  248. s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);
  249. if (!s->ham_palbuf) {
  250. av_freep(&s->ham_buf);
  251. return AVERROR(ENOMEM);
  252. }
  253. if (count) { // HAM with color palette attached
  254. // prefill with black and palette and set HAM take direct value mask to zero
  255. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
  256. for (i=0; i < count; i++) {
  257. s->ham_palbuf[i*2+1] = AV_RL24(palette + i*3);
  258. }
  259. count = 1 << s->ham;
  260. } else { // HAM with grayscale color palette
  261. count = 1 << s->ham;
  262. for (i=0; i < count; i++) {
  263. s->ham_palbuf[i*2] = 0; // take direct color value from palette
  264. s->ham_palbuf[i*2+1] = av_le2ne32(gray2rgb((i * 255) >> s->ham));
  265. }
  266. }
  267. for (i=0; i < count; i++) {
  268. uint32_t tmp = i << (8 - s->ham);
  269. tmp |= tmp >> s->ham;
  270. s->ham_palbuf[(i+count)*2] = 0x00FFFF; // just modify blue color component
  271. s->ham_palbuf[(i+count*2)*2] = 0xFFFF00; // just modify red color component
  272. s->ham_palbuf[(i+count*3)*2] = 0xFF00FF; // just modify green color component
  273. s->ham_palbuf[(i+count)*2+1] = tmp << 16;
  274. s->ham_palbuf[(i+count*2)*2+1] = tmp;
  275. s->ham_palbuf[(i+count*3)*2+1] = tmp << 8;
  276. }
  277. if (s->masking == MASK_HAS_MASK) {
  278. for (i = 0; i < ham_count; i++)
  279. s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static av_cold int decode_init(AVCodecContext *avctx)
  286. {
  287. IffContext *s = avctx->priv_data;
  288. int err;
  289. if (avctx->bits_per_coded_sample <= 8) {
  290. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  291. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  292. (avctx->extradata_size >= 2 && palette_size) ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  293. } else if (avctx->bits_per_coded_sample <= 32) {
  294. if (avctx->codec_tag != MKTAG('D','E','E','P'))
  295. avctx->pix_fmt = PIX_FMT_BGR32;
  296. } else {
  297. return AVERROR_INVALIDDATA;
  298. }
  299. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  300. return err;
  301. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  302. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  303. if (!s->planebuf)
  304. return AVERROR(ENOMEM);
  305. s->bpp = avctx->bits_per_coded_sample;
  306. avcodec_get_frame_defaults(&s->frame);
  307. if ((err = extract_header(avctx, NULL)) < 0)
  308. return err;
  309. s->frame.reference = 3;
  310. return 0;
  311. }
  312. /**
  313. * Decode interleaved plane buffer up to 8bpp
  314. * @param dst Destination buffer
  315. * @param buf Source buffer
  316. * @param buf_size
  317. * @param plane plane number to decode as
  318. */
  319. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  320. {
  321. const uint64_t *lut = plane8_lut[plane];
  322. do {
  323. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  324. AV_WN64A(dst, v);
  325. dst += 8;
  326. } while (--buf_size);
  327. }
  328. /**
  329. * Decode interleaved plane buffer up to 24bpp
  330. * @param dst Destination buffer
  331. * @param buf Source buffer
  332. * @param buf_size
  333. * @param plane plane number to decode as
  334. */
  335. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  336. {
  337. const uint32_t *lut = plane32_lut[plane];
  338. do {
  339. unsigned mask = (*buf >> 2) & ~3;
  340. dst[0] |= lut[mask++];
  341. dst[1] |= lut[mask++];
  342. dst[2] |= lut[mask++];
  343. dst[3] |= lut[mask];
  344. mask = (*buf++ << 2) & 0x3F;
  345. dst[4] |= lut[mask++];
  346. dst[5] |= lut[mask++];
  347. dst[6] |= lut[mask++];
  348. dst[7] |= lut[mask];
  349. dst += 8;
  350. } while (--buf_size);
  351. }
  352. #define DECODE_HAM_PLANE32(x) \
  353. first = buf[x] << 1; \
  354. second = buf[(x)+1] << 1; \
  355. delta &= pal[first++]; \
  356. delta |= pal[first]; \
  357. dst[x] = delta; \
  358. delta &= pal[second++]; \
  359. delta |= pal[second]; \
  360. dst[(x)+1] = delta
  361. /**
  362. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  363. *
  364. * @param dst the destination 24bpp buffer
  365. * @param buf the source 8bpp chunky buffer
  366. * @param pal the HAM decode table
  367. * @param buf_size the plane size in bytes
  368. */
  369. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  370. const uint32_t *const pal, unsigned buf_size)
  371. {
  372. uint32_t delta = 0;
  373. do {
  374. uint32_t first, second;
  375. DECODE_HAM_PLANE32(0);
  376. DECODE_HAM_PLANE32(2);
  377. DECODE_HAM_PLANE32(4);
  378. DECODE_HAM_PLANE32(6);
  379. buf += 8;
  380. dst += 8;
  381. } while (--buf_size);
  382. }
  383. static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
  384. const uint32_t *const pal, unsigned width)
  385. {
  386. do {
  387. *dst++ = pal[*buf++];
  388. } while (--width);
  389. }
  390. /**
  391. * Decode one complete byterun1 encoded line.
  392. *
  393. * @param dst the destination buffer where to store decompressed bitstream
  394. * @param dst_size the destination plane size in bytes
  395. * @param buf the source byterun1 compressed bitstream
  396. * @param buf_end the EOF of source byterun1 compressed bitstream
  397. * @return number of consumed bytes in byterun1 compressed bitstream
  398. */
  399. static int decode_byterun(uint8_t *dst, int dst_size,
  400. const uint8_t *buf, const uint8_t *const buf_end) {
  401. const uint8_t *const buf_start = buf;
  402. unsigned x;
  403. for (x = 0; x < dst_size && buf < buf_end;) {
  404. unsigned length;
  405. const int8_t value = *buf++;
  406. if (value >= 0) {
  407. length = value + 1;
  408. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  409. buf += length;
  410. } else if (value > -128) {
  411. length = -value + 1;
  412. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  413. } else { // noop
  414. continue;
  415. }
  416. x += length;
  417. }
  418. return buf - buf_start;
  419. }
  420. static int decode_frame_ilbm(AVCodecContext *avctx,
  421. void *data, int *data_size,
  422. AVPacket *avpkt)
  423. {
  424. IffContext *s = avctx->priv_data;
  425. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  426. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  427. const uint8_t *buf_end = buf+buf_size;
  428. int y, plane, res;
  429. if ((res = extract_header(avctx, avpkt)) < 0)
  430. return res;
  431. if (s->init) {
  432. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  433. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  434. return res;
  435. }
  436. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  437. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  438. return res;
  439. } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt == PIX_FMT_PAL8) {
  440. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  441. return res;
  442. }
  443. s->init = 1;
  444. if (avctx->codec_tag == MKTAG('A','C','B','M')) {
  445. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  446. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  447. for (plane = 0; plane < s->bpp; plane++) {
  448. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  449. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  450. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  451. buf += s->planesize;
  452. }
  453. }
  454. } else if (s->ham) { // HAM to PIX_FMT_BGR32
  455. memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
  456. for(y = 0; y < avctx->height; y++) {
  457. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  458. memset(s->ham_buf, 0, s->planesize * 8);
  459. for (plane = 0; plane < s->bpp; plane++) {
  460. const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
  461. if (start >= buf_end)
  462. break;
  463. decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
  464. }
  465. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  466. }
  467. }
  468. } else if (avctx->codec_tag == MKTAG('D','E','E','P')) {
  469. int raw_width = avctx->width * (av_get_bits_per_pixel(&av_pix_fmt_descriptors[avctx->pix_fmt]) >> 3);
  470. int x;
  471. for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
  472. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  473. memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
  474. buf += raw_width;
  475. if (avctx->pix_fmt == PIX_FMT_BGR32) {
  476. for(x = 0; x < avctx->width; x++)
  477. row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
  478. }
  479. }
  480. } else if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
  481. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  482. for(y = 0; y < avctx->height; y++ ) {
  483. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  484. memset(row, 0, avctx->width);
  485. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  486. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  487. buf += s->planesize;
  488. }
  489. }
  490. } else if (s->ham) { // HAM to PIX_FMT_BGR32
  491. for (y = 0; y < avctx->height; y++) {
  492. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  493. memset(s->ham_buf, 0, s->planesize * 8);
  494. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  495. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  496. buf += s->planesize;
  497. }
  498. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  499. }
  500. } else { // PIX_FMT_BGR32
  501. for(y = 0; y < avctx->height; y++ ) {
  502. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  503. memset(row, 0, avctx->width << 2);
  504. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  505. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  506. buf += s->planesize;
  507. }
  508. }
  509. }
  510. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  511. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  512. for(y = 0; y < avctx->height; y++ ) {
  513. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  514. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  515. buf += avctx->width + (avctx->width % 2); // padding if odd
  516. }
  517. } else if (s->ham) { // IFF-PBM: HAM to PIX_FMT_BGR32
  518. for (y = 0; y < avctx->height; y++) {
  519. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  520. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  521. buf += avctx->width + (avctx->width & 1); // padding if odd
  522. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  523. }
  524. } else {
  525. av_log_ask_for_sample(avctx, "unsupported bpp\n");
  526. return AVERROR_INVALIDDATA;
  527. }
  528. }
  529. *data_size = sizeof(AVFrame);
  530. *(AVFrame*)data = s->frame;
  531. return buf_size;
  532. }
  533. static int decode_frame_byterun1(AVCodecContext *avctx,
  534. void *data, int *data_size,
  535. AVPacket *avpkt)
  536. {
  537. IffContext *s = avctx->priv_data;
  538. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  539. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  540. const uint8_t *buf_end = buf+buf_size;
  541. int y, plane, res;
  542. if ((res = extract_header(avctx, avpkt)) < 0)
  543. return res;
  544. if (s->init) {
  545. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  546. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  547. return res;
  548. }
  549. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  550. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  551. return res;
  552. } else if (avctx->pix_fmt == PIX_FMT_PAL8) {
  553. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  554. return res;
  555. } else if (avctx->pix_fmt == PIX_FMT_RGB32 && avctx->bits_per_coded_sample <= 8) {
  556. if ((res = ff_cmap_read_palette(avctx, s->mask_palbuf)) < 0)
  557. return res;
  558. }
  559. s->init = 1;
  560. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  561. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  562. for(y = 0; y < avctx->height ; y++ ) {
  563. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  564. memset(row, 0, avctx->width);
  565. for (plane = 0; plane < s->bpp; plane++) {
  566. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  567. decodeplane8(row, s->planebuf, s->planesize, plane);
  568. }
  569. }
  570. } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to PIX_FMT_BGR32
  571. for (y = 0; y < avctx->height ; y++ ) {
  572. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  573. memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
  574. for (plane = 0; plane < s->bpp; plane++) {
  575. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  576. decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
  577. }
  578. lookup_pal_indicies((uint32_t *) row, s->mask_buf, s->mask_palbuf, avctx->width);
  579. }
  580. } else if (s->ham) { // HAM to PIX_FMT_BGR32
  581. for (y = 0; y < avctx->height ; y++) {
  582. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  583. memset(s->ham_buf, 0, s->planesize * 8);
  584. for (plane = 0; plane < s->bpp; plane++) {
  585. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  586. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  587. }
  588. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  589. }
  590. } else { //PIX_FMT_BGR32
  591. for(y = 0; y < avctx->height ; y++ ) {
  592. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  593. memset(row, 0, avctx->width << 2);
  594. for (plane = 0; plane < s->bpp; plane++) {
  595. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  596. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  597. }
  598. }
  599. }
  600. } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
  601. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  602. for(y = 0; y < avctx->height ; y++ ) {
  603. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  604. buf += decode_byterun(row, avctx->width, buf, buf_end);
  605. }
  606. } else if (s->ham) { // IFF-PBM: HAM to PIX_FMT_BGR32
  607. for (y = 0; y < avctx->height ; y++) {
  608. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  609. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  610. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  611. }
  612. } else {
  613. av_log_ask_for_sample(avctx, "unsupported bpp\n");
  614. return AVERROR_INVALIDDATA;
  615. }
  616. }
  617. *data_size = sizeof(AVFrame);
  618. *(AVFrame*)data = s->frame;
  619. return buf_size;
  620. }
  621. static av_cold int decode_end(AVCodecContext *avctx)
  622. {
  623. IffContext *s = avctx->priv_data;
  624. if (s->frame.data[0])
  625. avctx->release_buffer(avctx, &s->frame);
  626. av_freep(&s->planebuf);
  627. av_freep(&s->ham_buf);
  628. av_freep(&s->ham_palbuf);
  629. return 0;
  630. }
  631. AVCodec ff_iff_ilbm_decoder = {
  632. .name = "iff_ilbm",
  633. .type = AVMEDIA_TYPE_VIDEO,
  634. .id = CODEC_ID_IFF_ILBM,
  635. .priv_data_size = sizeof(IffContext),
  636. .init = decode_init,
  637. .close = decode_end,
  638. .decode = decode_frame_ilbm,
  639. .capabilities = CODEC_CAP_DR1,
  640. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  641. };
  642. AVCodec ff_iff_byterun1_decoder = {
  643. .name = "iff_byterun1",
  644. .type = AVMEDIA_TYPE_VIDEO,
  645. .id = CODEC_ID_IFF_BYTERUN1,
  646. .priv_data_size = sizeof(IffContext),
  647. .init = decode_init,
  648. .close = decode_end,
  649. .decode = decode_frame_byterun1,
  650. .capabilities = CODEC_CAP_DR1,
  651. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  652. };