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.

601 lines
23KB

  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. unsigned compression; ///< delta compression method used
  44. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  45. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  46. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  47. unsigned transparency; ///< TODO: transparency color index in palette
  48. unsigned masking; ///< TODO: masking method used
  49. int init; // 1 if buffer and palette data already initialized, 0 otherwise
  50. } IffContext;
  51. #define LUT8_PART(plane, v) \
  52. AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
  53. AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
  54. AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
  55. AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
  56. AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
  57. AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
  58. AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
  59. AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
  60. AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
  61. AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
  62. AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
  63. AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
  64. AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
  65. AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
  66. AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
  67. AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
  68. #define LUT8(plane) { \
  69. LUT8_PART(plane, 0x0000000), \
  70. LUT8_PART(plane, 0x1000000), \
  71. LUT8_PART(plane, 0x0010000), \
  72. LUT8_PART(plane, 0x1010000), \
  73. LUT8_PART(plane, 0x0000100), \
  74. LUT8_PART(plane, 0x1000100), \
  75. LUT8_PART(plane, 0x0010100), \
  76. LUT8_PART(plane, 0x1010100), \
  77. LUT8_PART(plane, 0x0000001), \
  78. LUT8_PART(plane, 0x1000001), \
  79. LUT8_PART(plane, 0x0010001), \
  80. LUT8_PART(plane, 0x1010001), \
  81. LUT8_PART(plane, 0x0000101), \
  82. LUT8_PART(plane, 0x1000101), \
  83. LUT8_PART(plane, 0x0010101), \
  84. LUT8_PART(plane, 0x1010101), \
  85. }
  86. // 8 planes * 8-bit mask
  87. static const uint64_t plane8_lut[8][256] = {
  88. LUT8(0), LUT8(1), LUT8(2), LUT8(3),
  89. LUT8(4), LUT8(5), LUT8(6), LUT8(7),
  90. };
  91. #define LUT32(plane) { \
  92. 0, 0, 0, 0, \
  93. 0, 0, 0, 1 << plane, \
  94. 0, 0, 1 << plane, 0, \
  95. 0, 0, 1 << plane, 1 << plane, \
  96. 0, 1 << plane, 0, 0, \
  97. 0, 1 << plane, 0, 1 << plane, \
  98. 0, 1 << plane, 1 << plane, 0, \
  99. 0, 1 << plane, 1 << plane, 1 << plane, \
  100. 1 << plane, 0, 0, 0, \
  101. 1 << plane, 0, 0, 1 << plane, \
  102. 1 << plane, 0, 1 << plane, 0, \
  103. 1 << plane, 0, 1 << plane, 1 << plane, \
  104. 1 << plane, 1 << plane, 0, 0, \
  105. 1 << plane, 1 << plane, 0, 1 << plane, \
  106. 1 << plane, 1 << plane, 1 << plane, 0, \
  107. 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
  108. }
  109. // 32 planes * 4-bit mask * 4 lookup tables each
  110. static const uint32_t plane32_lut[32][16*4] = {
  111. LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
  112. LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
  113. LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
  114. LUT32(12), LUT32(13), LUT32(14), LUT32(15),
  115. LUT32(16), LUT32(17), LUT32(18), LUT32(19),
  116. LUT32(20), LUT32(21), LUT32(22), LUT32(23),
  117. LUT32(24), LUT32(25), LUT32(26), LUT32(27),
  118. LUT32(28), LUT32(29), LUT32(30), LUT32(31),
  119. };
  120. // Gray to RGB, required for palette table of grayscale images with bpp < 8
  121. static av_always_inline uint32_t gray2rgb(const uint32_t x) {
  122. return x << 16 | x << 8 | x;
  123. }
  124. /**
  125. * Convert CMAP buffer (stored in extradata) to lavc palette format
  126. */
  127. static int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  128. {
  129. int count, i;
  130. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  131. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  132. if (avctx->bits_per_coded_sample > 8) {
  133. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. count = 1 << avctx->bits_per_coded_sample;
  137. // If extradata is smaller than actually needed, fill the remaining with black.
  138. count = FFMIN(palette_size / 3, count);
  139. if (count) {
  140. for (i=0; i < count; i++) {
  141. pal[i] = 0xFF000000 | AV_RB24(palette + i*3);
  142. }
  143. } else { // Create gray-scale color palette for bps < 8
  144. count = 1 << avctx->bits_per_coded_sample;
  145. for (i=0; i < count; i++) {
  146. pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
  147. }
  148. }
  149. return 0;
  150. }
  151. /**
  152. * Extracts the IFF extra context and updates internal
  153. * decoder structures.
  154. *
  155. * @param avctx the AVCodecContext where to extract extra context to
  156. * @param avpkt the AVPacket to extract extra context from or NULL to use avctx
  157. * @return 0 in case of success, a negative error code otherwise
  158. */
  159. static int extract_header(AVCodecContext *const avctx,
  160. const AVPacket *const avpkt) {
  161. const uint8_t *buf;
  162. unsigned buf_size;
  163. IffContext *s = avctx->priv_data;
  164. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  165. if (avpkt) {
  166. int image_size;
  167. if (avpkt->size < 2)
  168. return AVERROR_INVALIDDATA;
  169. image_size = avpkt->size - AV_RB16(avpkt->data);
  170. buf = avpkt->data;
  171. buf_size = bytestream_get_be16(&buf);
  172. if (buf_size <= 1 || image_size <= 1) {
  173. av_log(avctx, AV_LOG_ERROR,
  174. "Invalid image size received: %u -> image data offset: %d\n",
  175. buf_size, image_size);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. } else {
  179. if (avctx->extradata_size < 2)
  180. return AVERROR_INVALIDDATA;
  181. buf = avctx->extradata;
  182. buf_size = bytestream_get_be16(&buf);
  183. if (buf_size <= 1 || palette_size < 0) {
  184. av_log(avctx, AV_LOG_ERROR,
  185. "Invalid palette size received: %u -> palette data offset: %d\n",
  186. buf_size, palette_size);
  187. return AVERROR_INVALIDDATA;
  188. }
  189. }
  190. if (buf_size > 8) {
  191. s->compression = bytestream_get_byte(&buf);
  192. s->bpp = bytestream_get_byte(&buf);
  193. s->ham = bytestream_get_byte(&buf);
  194. s->flags = bytestream_get_byte(&buf);
  195. s->transparency = bytestream_get_be16(&buf);
  196. s->masking = bytestream_get_byte(&buf);
  197. if (s->masking == MASK_HAS_TRANSPARENT_COLOR) {
  198. av_log(avctx, AV_LOG_ERROR, "Transparency not supported\n");
  199. return AVERROR_PATCHWELCOME;
  200. } else if (s->masking != MASK_NONE) {
  201. av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
  202. return AVERROR_PATCHWELCOME;
  203. }
  204. if (!s->bpp || s->bpp > 32) {
  205. av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
  206. return AVERROR_INVALIDDATA;
  207. } else if (s->ham >= 8) {
  208. av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
  209. return AVERROR_INVALIDDATA;
  210. }
  211. av_freep(&s->ham_buf);
  212. av_freep(&s->ham_palbuf);
  213. if (s->ham) {
  214. int i, count = FFMIN(palette_size / 3, 1 << s->ham);
  215. const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
  216. s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);
  217. if (!s->ham_buf)
  218. return AVERROR(ENOMEM);
  219. s->ham_palbuf = av_malloc((8 * (1 << s->ham) * sizeof (uint32_t)) + FF_INPUT_BUFFER_PADDING_SIZE);
  220. if (!s->ham_palbuf) {
  221. av_freep(&s->ham_buf);
  222. return AVERROR(ENOMEM);
  223. }
  224. if (count) { // HAM with color palette attached
  225. // prefill with black and palette and set HAM take direct value mask to zero
  226. memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
  227. for (i=0; i < count; i++) {
  228. s->ham_palbuf[i*2+1] = AV_RL24(palette + i*3);
  229. }
  230. count = 1 << s->ham;
  231. } else { // HAM with grayscale color palette
  232. count = 1 << s->ham;
  233. for (i=0; i < count; i++) {
  234. s->ham_palbuf[i*2] = 0; // take direct color value from palette
  235. s->ham_palbuf[i*2+1] = av_le2ne32(gray2rgb((i * 255) >> s->ham));
  236. }
  237. }
  238. for (i=0; i < count; i++) {
  239. uint32_t tmp = i << (8 - s->ham);
  240. tmp |= tmp >> s->ham;
  241. s->ham_palbuf[(i+count)*2] = 0x00FFFF; // just modify blue color component
  242. s->ham_palbuf[(i+count*2)*2] = 0xFFFF00; // just modify red color component
  243. s->ham_palbuf[(i+count*3)*2] = 0xFF00FF; // just modify green color component
  244. s->ham_palbuf[(i+count)*2+1] = tmp << 16;
  245. s->ham_palbuf[(i+count*2)*2+1] = tmp;
  246. s->ham_palbuf[(i+count*3)*2+1] = tmp << 8;
  247. }
  248. } else if (s->flags & 1) { // EHB (ExtraHalfBrite) color palette
  249. av_log(avctx, AV_LOG_ERROR, "ExtraHalfBrite (EHB) mode not supported\n");
  250. return AVERROR_PATCHWELCOME;
  251. }
  252. }
  253. return 0;
  254. }
  255. static av_cold int decode_init(AVCodecContext *avctx)
  256. {
  257. IffContext *s = avctx->priv_data;
  258. int err;
  259. if (avctx->bits_per_coded_sample <= 8) {
  260. int palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
  261. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
  262. (avctx->extradata_size >= 2 && palette_size) ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  263. } else if (avctx->bits_per_coded_sample <= 32) {
  264. avctx->pix_fmt = PIX_FMT_BGR32;
  265. } else {
  266. return AVERROR_INVALIDDATA;
  267. }
  268. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  269. return err;
  270. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  271. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  272. if (!s->planebuf)
  273. return AVERROR(ENOMEM);
  274. s->bpp = avctx->bits_per_coded_sample;
  275. avcodec_get_frame_defaults(&s->frame);
  276. if ((err = extract_header(avctx, NULL)) < 0)
  277. return err;
  278. s->frame.reference = 1;
  279. return 0;
  280. }
  281. /**
  282. * Decode interleaved plane buffer up to 8bpp
  283. * @param dst Destination buffer
  284. * @param buf Source buffer
  285. * @param buf_size
  286. * @param plane plane number to decode as
  287. */
  288. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  289. {
  290. const uint64_t *lut = plane8_lut[plane];
  291. do {
  292. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  293. AV_WN64A(dst, v);
  294. dst += 8;
  295. } while (--buf_size);
  296. }
  297. /**
  298. * Decode interleaved plane buffer up to 24bpp
  299. * @param dst Destination buffer
  300. * @param buf Source buffer
  301. * @param buf_size
  302. * @param plane plane number to decode as
  303. */
  304. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  305. {
  306. const uint32_t *lut = plane32_lut[plane];
  307. do {
  308. unsigned mask = (*buf >> 2) & ~3;
  309. dst[0] |= lut[mask++];
  310. dst[1] |= lut[mask++];
  311. dst[2] |= lut[mask++];
  312. dst[3] |= lut[mask];
  313. mask = (*buf++ << 2) & 0x3F;
  314. dst[4] |= lut[mask++];
  315. dst[5] |= lut[mask++];
  316. dst[6] |= lut[mask++];
  317. dst[7] |= lut[mask];
  318. dst += 8;
  319. } while (--buf_size);
  320. }
  321. #define DECODE_HAM_PLANE32(x) \
  322. first = buf[x] << 1; \
  323. second = buf[(x)+1] << 1; \
  324. delta &= pal[first++]; \
  325. delta |= pal[first]; \
  326. dst[x] = delta; \
  327. delta &= pal[second++]; \
  328. delta |= pal[second]; \
  329. dst[(x)+1] = delta
  330. /**
  331. * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
  332. *
  333. * @param dst the destination 24bpp buffer
  334. * @param buf the source 8bpp chunky buffer
  335. * @param pal the HAM decode table
  336. * @param buf_size the plane size in bytes
  337. */
  338. static void decode_ham_plane32(uint32_t *dst, const uint8_t *buf,
  339. const uint32_t *const pal, unsigned buf_size)
  340. {
  341. uint32_t delta = 0;
  342. do {
  343. uint32_t first, second;
  344. DECODE_HAM_PLANE32(0);
  345. DECODE_HAM_PLANE32(2);
  346. DECODE_HAM_PLANE32(4);
  347. DECODE_HAM_PLANE32(6);
  348. buf += 8;
  349. dst += 8;
  350. } while (--buf_size);
  351. }
  352. /**
  353. * Decode one complete byterun1 encoded line.
  354. *
  355. * @param dst the destination buffer where to store decompressed bitstream
  356. * @param dst_size the destination plane size in bytes
  357. * @param buf the source byterun1 compressed bitstream
  358. * @param buf_end the EOF of source byterun1 compressed bitstream
  359. * @return number of consumed bytes in byterun1 compressed bitstream
  360. */
  361. static int decode_byterun(uint8_t *dst, int dst_size,
  362. const uint8_t *buf, const uint8_t *const buf_end) {
  363. const uint8_t *const buf_start = buf;
  364. unsigned x;
  365. for (x = 0; x < dst_size && buf < buf_end;) {
  366. unsigned length;
  367. const int8_t value = *buf++;
  368. if (value >= 0) {
  369. length = value + 1;
  370. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  371. buf += length;
  372. } else if (value > -128) {
  373. length = -value + 1;
  374. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  375. } else { // noop
  376. continue;
  377. }
  378. x += length;
  379. }
  380. return buf - buf_start;
  381. }
  382. static int decode_frame_ilbm(AVCodecContext *avctx,
  383. void *data, int *data_size,
  384. AVPacket *avpkt)
  385. {
  386. IffContext *s = avctx->priv_data;
  387. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  388. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  389. const uint8_t *buf_end = buf+buf_size;
  390. int y, plane, res;
  391. if ((res = extract_header(avctx, avpkt)) < 0)
  392. return res;
  393. if (s->init) {
  394. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  395. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  396. return res;
  397. }
  398. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  399. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  400. return res;
  401. } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt != PIX_FMT_GRAY8) {
  402. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  403. return res;
  404. }
  405. s->init = 1;
  406. if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
  407. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  408. for(y = 0; y < avctx->height; y++ ) {
  409. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  410. memset(row, 0, avctx->width);
  411. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  412. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  413. buf += s->planesize;
  414. }
  415. }
  416. } else if (s->ham) { // HAM to PIX_FMT_BGR32
  417. for (y = 0; y < avctx->height; y++) {
  418. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  419. memset(s->ham_buf, 0, avctx->width);
  420. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  421. decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
  422. buf += s->planesize;
  423. }
  424. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  425. }
  426. } else { // PIX_FMT_BGR32
  427. for(y = 0; y < avctx->height; y++ ) {
  428. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  429. memset(row, 0, avctx->width << 2);
  430. for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
  431. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  432. buf += s->planesize;
  433. }
  434. }
  435. }
  436. } else if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) { // IFF-PBM
  437. for(y = 0; y < avctx->height; y++ ) {
  438. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  439. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  440. buf += avctx->width + (avctx->width % 2); // padding if odd
  441. }
  442. } else { // IFF-PBM: HAM to PIX_FMT_BGR32
  443. for (y = 0; y < avctx->height; y++) {
  444. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  445. memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
  446. buf += avctx->width + (avctx->width & 1); // padding if odd
  447. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, avctx->width);
  448. }
  449. }
  450. *data_size = sizeof(AVFrame);
  451. *(AVFrame*)data = s->frame;
  452. return buf_size;
  453. }
  454. static int decode_frame_byterun1(AVCodecContext *avctx,
  455. void *data, int *data_size,
  456. AVPacket *avpkt)
  457. {
  458. IffContext *s = avctx->priv_data;
  459. const uint8_t *buf = avpkt->size >= 2 ? avpkt->data + AV_RB16(avpkt->data) : NULL;
  460. const int buf_size = avpkt->size >= 2 ? avpkt->size - AV_RB16(avpkt->data) : 0;
  461. const uint8_t *buf_end = buf+buf_size;
  462. int y, plane, res;
  463. if ((res = extract_header(avctx, avpkt)) < 0)
  464. return res;
  465. if (s->init) {
  466. if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  467. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  468. return res;
  469. }
  470. } else if ((res = avctx->get_buffer(avctx, &s->frame)) < 0) {
  471. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  472. return res;
  473. } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt != PIX_FMT_GRAY8) {
  474. if ((res = ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
  475. return res;
  476. }
  477. s->init = 1;
  478. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  479. if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) {
  480. for(y = 0; y < avctx->height ; y++ ) {
  481. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  482. memset(row, 0, avctx->width);
  483. for (plane = 0; plane < s->bpp; plane++) {
  484. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  485. decodeplane8(row, s->planebuf, s->planesize, plane);
  486. }
  487. }
  488. } else if (s->ham) { // HAM to PIX_FMT_BGR32
  489. for (y = 0; y < avctx->height ; y++) {
  490. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  491. memset(s->ham_buf, 0, avctx->width);
  492. for (plane = 0; plane < s->bpp; plane++) {
  493. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  494. decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
  495. }
  496. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
  497. }
  498. } else { //PIX_FMT_BGR32
  499. for(y = 0; y < avctx->height ; y++ ) {
  500. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  501. memset(row, 0, avctx->width << 2);
  502. for (plane = 0; plane < s->bpp; plane++) {
  503. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  504. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  505. }
  506. }
  507. }
  508. } else if (avctx->pix_fmt == PIX_FMT_PAL8 || avctx->pix_fmt == PIX_FMT_GRAY8) { // IFF-PBM
  509. for(y = 0; y < avctx->height ; y++ ) {
  510. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  511. buf += decode_byterun(row, avctx->width, buf, buf_end);
  512. }
  513. } else { // IFF-PBM: HAM to PIX_FMT_BGR32
  514. for (y = 0; y < avctx->height ; y++) {
  515. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  516. buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
  517. decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, avctx->width);
  518. }
  519. }
  520. *data_size = sizeof(AVFrame);
  521. *(AVFrame*)data = s->frame;
  522. return buf_size;
  523. }
  524. static av_cold int decode_end(AVCodecContext *avctx)
  525. {
  526. IffContext *s = avctx->priv_data;
  527. if (s->frame.data[0])
  528. avctx->release_buffer(avctx, &s->frame);
  529. av_freep(&s->planebuf);
  530. av_freep(&s->ham_buf);
  531. av_freep(&s->ham_palbuf);
  532. return 0;
  533. }
  534. AVCodec ff_iff_ilbm_decoder = {
  535. .name = "iff_ilbm",
  536. .type = AVMEDIA_TYPE_VIDEO,
  537. .id = CODEC_ID_IFF_ILBM,
  538. .priv_data_size = sizeof(IffContext),
  539. .init = decode_init,
  540. .close = decode_end,
  541. .decode = decode_frame_ilbm,
  542. .capabilities = CODEC_CAP_DR1,
  543. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  544. };
  545. AVCodec ff_iff_byterun1_decoder = {
  546. .name = "iff_byterun1",
  547. .type = AVMEDIA_TYPE_VIDEO,
  548. .id = CODEC_ID_IFF_BYTERUN1,
  549. .priv_data_size = sizeof(IffContext),
  550. .init = decode_init,
  551. .close = decode_end,
  552. .decode = decode_frame_byterun1,
  553. .capabilities = CODEC_CAP_DR1,
  554. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  555. };