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.

627 lines
24KB

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