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.

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