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.

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