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.

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