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.

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