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.

399 lines
14KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 <stdint.h>
  27. #include "libavutil/imgutils.h"
  28. #include "bytestream.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. typedef struct IffContext {
  32. AVFrame *frame;
  33. int planesize;
  34. uint8_t * planebuf;
  35. int init; // 1 if buffer and palette data already initialized, 0 otherwise
  36. } IffContext;
  37. #define LUT8_PART(plane, v) \
  38. AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane, \
  39. AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane, \
  40. AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane, \
  41. AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane, \
  42. AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane, \
  43. AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane, \
  44. AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane, \
  45. AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane, \
  46. AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane, \
  47. AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane, \
  48. AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane, \
  49. AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane, \
  50. AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane, \
  51. AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane, \
  52. AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane, \
  53. AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
  54. #define LUT8(plane) { \
  55. LUT8_PART(plane, 0x0000000), \
  56. LUT8_PART(plane, 0x1000000), \
  57. LUT8_PART(plane, 0x0010000), \
  58. LUT8_PART(plane, 0x1010000), \
  59. LUT8_PART(plane, 0x0000100), \
  60. LUT8_PART(plane, 0x1000100), \
  61. LUT8_PART(plane, 0x0010100), \
  62. LUT8_PART(plane, 0x1010100), \
  63. LUT8_PART(plane, 0x0000001), \
  64. LUT8_PART(plane, 0x1000001), \
  65. LUT8_PART(plane, 0x0010001), \
  66. LUT8_PART(plane, 0x1010001), \
  67. LUT8_PART(plane, 0x0000101), \
  68. LUT8_PART(plane, 0x1000101), \
  69. LUT8_PART(plane, 0x0010101), \
  70. LUT8_PART(plane, 0x1010101), \
  71. }
  72. // 8 planes * 8-bit mask
  73. static const uint64_t plane8_lut[8][256] = {
  74. LUT8(0), LUT8(1), LUT8(2), LUT8(3),
  75. LUT8(4), LUT8(5), LUT8(6), LUT8(7),
  76. };
  77. #define LUT32(plane) { \
  78. 0, 0, 0, 0, \
  79. 0, 0, 0, 1 << plane, \
  80. 0, 0, 1 << plane, 0, \
  81. 0, 0, 1 << plane, 1 << plane, \
  82. 0, 1 << plane, 0, 0, \
  83. 0, 1 << plane, 0, 1 << plane, \
  84. 0, 1 << plane, 1 << plane, 0, \
  85. 0, 1 << plane, 1 << plane, 1 << plane, \
  86. 1 << plane, 0, 0, 0, \
  87. 1 << plane, 0, 0, 1 << plane, \
  88. 1 << plane, 0, 1 << plane, 0, \
  89. 1 << plane, 0, 1 << plane, 1 << plane, \
  90. 1 << plane, 1 << plane, 0, 0, \
  91. 1 << plane, 1 << plane, 0, 1 << plane, \
  92. 1 << plane, 1 << plane, 1 << plane, 0, \
  93. 1 << plane, 1 << plane, 1 << plane, 1 << plane, \
  94. }
  95. // 32 planes * 4-bit mask * 4 lookup tables each
  96. static const uint32_t plane32_lut[32][16*4] = {
  97. LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
  98. LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
  99. LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
  100. LUT32(12), LUT32(13), LUT32(14), LUT32(15),
  101. LUT32(16), LUT32(17), LUT32(18), LUT32(19),
  102. LUT32(20), LUT32(21), LUT32(22), LUT32(23),
  103. LUT32(24), LUT32(25), LUT32(26), LUT32(27),
  104. LUT32(28), LUT32(29), LUT32(30), LUT32(31),
  105. };
  106. // Gray to RGB, required for palette table of grayscale images with bpp < 8
  107. static av_always_inline uint32_t gray2rgb(const uint32_t x) {
  108. return x << 16 | x << 8 | x;
  109. }
  110. /**
  111. * Convert CMAP buffer (stored in extradata) to lavc palette format
  112. */
  113. static int cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  114. {
  115. int count, i;
  116. if (avctx->bits_per_coded_sample > 8) {
  117. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  118. return AVERROR_INVALIDDATA;
  119. }
  120. count = 1 << avctx->bits_per_coded_sample;
  121. // If extradata is smaller than actually needed, fill the remaining with black.
  122. count = FFMIN(avctx->extradata_size / 3, count);
  123. if (count) {
  124. for (i = 0; i < count; i++)
  125. pal[i] = 0xFF000000 | AV_RB24(avctx->extradata + i * 3);
  126. } else { // Create gray-scale color palette for bps < 8
  127. count = 1 << avctx->bits_per_coded_sample;
  128. for (i = 0; i < count; i++)
  129. pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
  130. }
  131. return 0;
  132. }
  133. static av_cold int decode_end(AVCodecContext *avctx)
  134. {
  135. IffContext *s = avctx->priv_data;
  136. av_frame_free(&s->frame);
  137. av_freep(&s->planebuf);
  138. return 0;
  139. }
  140. static av_cold int decode_init(AVCodecContext *avctx)
  141. {
  142. IffContext *s = avctx->priv_data;
  143. int err;
  144. if (avctx->bits_per_coded_sample <= 8) {
  145. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8 ||
  146. avctx->extradata_size) ? AV_PIX_FMT_PAL8
  147. : AV_PIX_FMT_GRAY8;
  148. } else if (avctx->bits_per_coded_sample <= 32) {
  149. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  150. } else {
  151. return AVERROR_INVALIDDATA;
  152. }
  153. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  154. return err;
  155. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  156. s->planebuf = av_malloc(s->planesize + AV_INPUT_BUFFER_PADDING_SIZE);
  157. if (!s->planebuf)
  158. return AVERROR(ENOMEM);
  159. s->frame = av_frame_alloc();
  160. if (!s->frame) {
  161. decode_end(avctx);
  162. return AVERROR(ENOMEM);
  163. }
  164. return 0;
  165. }
  166. /**
  167. * Decode interleaved plane buffer up to 8bpp
  168. * @param dst Destination buffer
  169. * @param buf Source buffer
  170. * @param buf_size
  171. * @param plane plane number to decode as
  172. */
  173. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  174. {
  175. const uint64_t *lut = plane8_lut[plane];
  176. do {
  177. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  178. AV_WN64A(dst, v);
  179. dst += 8;
  180. } while (--buf_size);
  181. }
  182. /**
  183. * Decode interleaved plane buffer up to 24bpp
  184. * @param dst Destination buffer
  185. * @param buf Source buffer
  186. * @param buf_size
  187. * @param plane plane number to decode as
  188. */
  189. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  190. {
  191. const uint32_t *lut = plane32_lut[plane];
  192. do {
  193. unsigned mask = (*buf >> 2) & ~3;
  194. dst[0] |= lut[mask++];
  195. dst[1] |= lut[mask++];
  196. dst[2] |= lut[mask++];
  197. dst[3] |= lut[mask];
  198. mask = (*buf++ << 2) & 0x3F;
  199. dst[4] |= lut[mask++];
  200. dst[5] |= lut[mask++];
  201. dst[6] |= lut[mask++];
  202. dst[7] |= lut[mask];
  203. dst += 8;
  204. } while (--buf_size);
  205. }
  206. /**
  207. * Decode one complete byterun1 encoded line.
  208. *
  209. * @param dst the destination buffer where to store decompressed bitstream
  210. * @param dst_size the destination plane size in bytes
  211. * @param buf the source byterun1 compressed bitstream
  212. * @param buf_end the EOF of source byterun1 compressed bitstream
  213. * @return number of consumed bytes in byterun1 compressed bitstream
  214. */
  215. static int decode_byterun(uint8_t *dst, int dst_size,
  216. const uint8_t *buf, const uint8_t *const buf_end)
  217. {
  218. const uint8_t *const buf_start = buf;
  219. unsigned x;
  220. for (x = 0; x < dst_size && buf < buf_end;) {
  221. unsigned length;
  222. const int8_t value = *buf++;
  223. if (value >= 0) {
  224. length = value + 1;
  225. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  226. buf += length;
  227. } else if (value > -128) {
  228. length = -value + 1;
  229. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  230. } else { // noop
  231. continue;
  232. }
  233. x += length;
  234. }
  235. return buf - buf_start;
  236. }
  237. static int decode_frame_ilbm(AVCodecContext *avctx,
  238. void *data, int *got_frame,
  239. AVPacket *avpkt)
  240. {
  241. IffContext *s = avctx->priv_data;
  242. const uint8_t *buf = avpkt->data;
  243. int buf_size = avpkt->size;
  244. const uint8_t *buf_end = buf + buf_size;
  245. int y, plane, res;
  246. if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
  247. return res;
  248. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  249. avctx->pix_fmt != AV_PIX_FMT_GRAY8) {
  250. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame->data[1])) < 0)
  251. return res;
  252. }
  253. s->init = 1;
  254. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  255. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  256. for (y = 0; y < avctx->height; y++) {
  257. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  258. memset(row, 0, avctx->width);
  259. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end;
  260. plane++) {
  261. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  262. buf += s->planesize;
  263. }
  264. }
  265. } else { // AV_PIX_FMT_BGR32
  266. for (y = 0; y < avctx->height; y++) {
  267. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  268. memset(row, 0, avctx->width << 2);
  269. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end;
  270. plane++) {
  271. decodeplane32((uint32_t *)row, buf,
  272. FFMIN(s->planesize, buf_end - buf), plane);
  273. buf += s->planesize;
  274. }
  275. }
  276. }
  277. } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) { // IFF-PBM
  278. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  279. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  280. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  281. buf += avctx->width + (avctx->width % 2); // padding if odd
  282. }
  283. }
  284. if ((res = av_frame_ref(data, s->frame)) < 0)
  285. return res;
  286. *got_frame = 1;
  287. return buf_size;
  288. }
  289. static int decode_frame_byterun1(AVCodecContext *avctx,
  290. void *data, int *got_frame,
  291. AVPacket *avpkt)
  292. {
  293. IffContext *s = avctx->priv_data;
  294. const uint8_t *buf = avpkt->data;
  295. int buf_size = avpkt->size;
  296. const uint8_t *buf_end = buf + buf_size;
  297. int y, plane, res;
  298. if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
  299. return res;
  300. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  301. avctx->pix_fmt != AV_PIX_FMT_GRAY8) {
  302. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame->data[1])) < 0)
  303. return res;
  304. }
  305. s->init = 1;
  306. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  307. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  308. for (y = 0; y < avctx->height; y++) {
  309. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  310. memset(row, 0, avctx->width);
  311. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  312. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  313. decodeplane8(row, s->planebuf, s->planesize, plane);
  314. }
  315. }
  316. } else { // AV_PIX_FMT_BGR32
  317. for (y = 0; y < avctx->height; y++) {
  318. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  319. memset(row, 0, avctx->width << 2);
  320. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  321. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  322. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  323. }
  324. }
  325. }
  326. } else {
  327. for (y = 0; y < avctx->height; y++) {
  328. uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
  329. buf += decode_byterun(row, avctx->width, buf, buf_end);
  330. }
  331. }
  332. if ((res = av_frame_ref(data, s->frame)) < 0)
  333. return res;
  334. *got_frame = 1;
  335. return buf_size;
  336. }
  337. AVCodec ff_iff_ilbm_decoder = {
  338. .name = "iff_ilbm",
  339. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. .id = AV_CODEC_ID_IFF_ILBM,
  342. .priv_data_size = sizeof(IffContext),
  343. .init = decode_init,
  344. .close = decode_end,
  345. .decode = decode_frame_ilbm,
  346. .capabilities = AV_CODEC_CAP_DR1,
  347. };
  348. AVCodec ff_iff_byterun1_decoder = {
  349. .name = "iff_byterun1",
  350. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .id = AV_CODEC_ID_IFF_BYTERUN1,
  353. .priv_data_size = sizeof(IffContext),
  354. .init = decode_init,
  355. .close = decode_end,
  356. .decode = decode_frame_byterun1,
  357. .capabilities = AV_CODEC_CAP_DR1,
  358. };