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.

393 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 "libavutil/imgutils.h"
  27. #include "bytestream.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. typedef struct {
  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_init(AVCodecContext *avctx)
  134. {
  135. IffContext *s = avctx->priv_data;
  136. int err;
  137. if (avctx->bits_per_coded_sample <= 8) {
  138. avctx->pix_fmt = (avctx->bits_per_coded_sample < 8 ||
  139. avctx->extradata_size) ? AV_PIX_FMT_PAL8
  140. : AV_PIX_FMT_GRAY8;
  141. } else if (avctx->bits_per_coded_sample <= 32) {
  142. avctx->pix_fmt = AV_PIX_FMT_BGR32;
  143. } else {
  144. return AVERROR_INVALIDDATA;
  145. }
  146. if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
  147. return err;
  148. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  149. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  150. if (!s->planebuf)
  151. return AVERROR(ENOMEM);
  152. avcodec_get_frame_defaults(&s->frame);
  153. return 0;
  154. }
  155. /**
  156. * Decode interleaved plane buffer up to 8bpp
  157. * @param dst Destination buffer
  158. * @param buf Source buffer
  159. * @param buf_size
  160. * @param plane plane number to decode as
  161. */
  162. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  163. {
  164. const uint64_t *lut = plane8_lut[plane];
  165. do {
  166. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  167. AV_WN64A(dst, v);
  168. dst += 8;
  169. } while (--buf_size);
  170. }
  171. /**
  172. * Decode interleaved plane buffer up to 24bpp
  173. * @param dst Destination buffer
  174. * @param buf Source buffer
  175. * @param buf_size
  176. * @param plane plane number to decode as
  177. */
  178. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  179. {
  180. const uint32_t *lut = plane32_lut[plane];
  181. do {
  182. unsigned mask = (*buf >> 2) & ~3;
  183. dst[0] |= lut[mask++];
  184. dst[1] |= lut[mask++];
  185. dst[2] |= lut[mask++];
  186. dst[3] |= lut[mask];
  187. mask = (*buf++ << 2) & 0x3F;
  188. dst[4] |= lut[mask++];
  189. dst[5] |= lut[mask++];
  190. dst[6] |= lut[mask++];
  191. dst[7] |= lut[mask];
  192. dst += 8;
  193. } while (--buf_size);
  194. }
  195. /**
  196. * Decode one complete byterun1 encoded line.
  197. *
  198. * @param dst the destination buffer where to store decompressed bitstream
  199. * @param dst_size the destination plane size in bytes
  200. * @param buf the source byterun1 compressed bitstream
  201. * @param buf_end the EOF of source byterun1 compressed bitstream
  202. * @return number of consumed bytes in byterun1 compressed bitstream
  203. */
  204. static int decode_byterun(uint8_t *dst, int dst_size,
  205. const uint8_t *buf, const uint8_t *const buf_end)
  206. {
  207. const uint8_t *const buf_start = buf;
  208. unsigned x;
  209. for (x = 0; x < dst_size && buf < buf_end;) {
  210. unsigned length;
  211. const int8_t value = *buf++;
  212. if (value >= 0) {
  213. length = value + 1;
  214. memcpy(dst + x, buf, FFMIN3(length, dst_size - x, buf_end - buf));
  215. buf += length;
  216. } else if (value > -128) {
  217. length = -value + 1;
  218. memset(dst + x, *buf++, FFMIN(length, dst_size - x));
  219. } else { // noop
  220. continue;
  221. }
  222. x += length;
  223. }
  224. return buf - buf_start;
  225. }
  226. static int decode_frame_ilbm(AVCodecContext *avctx,
  227. void *data, int *got_frame,
  228. AVPacket *avpkt)
  229. {
  230. IffContext *s = avctx->priv_data;
  231. const uint8_t *buf = avpkt->data;
  232. int buf_size = avpkt->size;
  233. const uint8_t *buf_end = buf + buf_size;
  234. int y, plane, res;
  235. if ((res = ff_reget_buffer(avctx, &s->frame)) < 0)
  236. return res;
  237. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  238. avctx->pix_fmt != AV_PIX_FMT_GRAY8) {
  239. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame.data[1])) < 0)
  240. return res;
  241. }
  242. s->init = 1;
  243. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  244. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  245. for (y = 0; y < avctx->height; y++) {
  246. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  247. memset(row, 0, avctx->width);
  248. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end;
  249. plane++) {
  250. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  251. buf += s->planesize;
  252. }
  253. }
  254. } else { // AV_PIX_FMT_BGR32
  255. for (y = 0; y < avctx->height; y++) {
  256. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  257. memset(row, 0, avctx->width << 2);
  258. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end;
  259. plane++) {
  260. decodeplane32((uint32_t *)row, buf,
  261. FFMIN(s->planesize, buf_end - buf), plane);
  262. buf += s->planesize;
  263. }
  264. }
  265. }
  266. } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) { // IFF-PBM
  267. for (y = 0; y < avctx->height && buf < buf_end; y++) {
  268. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  269. memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
  270. buf += avctx->width + (avctx->width % 2); // padding if odd
  271. }
  272. }
  273. if ((res = av_frame_ref(data, &s->frame)) < 0)
  274. return res;
  275. *got_frame = 1;
  276. return buf_size;
  277. }
  278. static int decode_frame_byterun1(AVCodecContext *avctx,
  279. void *data, int *got_frame,
  280. AVPacket *avpkt)
  281. {
  282. IffContext *s = avctx->priv_data;
  283. const uint8_t *buf = avpkt->data;
  284. int buf_size = avpkt->size;
  285. const uint8_t *buf_end = buf + buf_size;
  286. int y, plane, res;
  287. if ((res = ff_reget_buffer(avctx, &s->frame)) < 0)
  288. return res;
  289. if (!s->init && avctx->bits_per_coded_sample <= 8 &&
  290. avctx->pix_fmt != AV_PIX_FMT_GRAY8) {
  291. if ((res = cmap_read_palette(avctx, (uint32_t *)s->frame.data[1])) < 0)
  292. return res;
  293. }
  294. s->init = 1;
  295. if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M')) { // interleaved
  296. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  297. for (y = 0; y < avctx->height; y++) {
  298. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  299. memset(row, 0, avctx->width);
  300. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  301. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  302. decodeplane8(row, s->planebuf, s->planesize, plane);
  303. }
  304. }
  305. } else { // AV_PIX_FMT_BGR32
  306. for (y = 0; y < avctx->height; y++) {
  307. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  308. memset(row, 0, avctx->width << 2);
  309. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  310. buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
  311. decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
  312. }
  313. }
  314. }
  315. } else {
  316. for (y = 0; y < avctx->height; y++) {
  317. uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
  318. buf += decode_byterun(row, avctx->width, buf, buf_end);
  319. }
  320. }
  321. if ((res = av_frame_ref(data, &s->frame)) < 0)
  322. return res;
  323. *got_frame = 1;
  324. return buf_size;
  325. }
  326. static av_cold int decode_end(AVCodecContext *avctx)
  327. {
  328. IffContext *s = avctx->priv_data;
  329. av_frame_unref(&s->frame);
  330. av_freep(&s->planebuf);
  331. return 0;
  332. }
  333. AVCodec ff_iff_ilbm_decoder = {
  334. .name = "iff_ilbm",
  335. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  336. .type = AVMEDIA_TYPE_VIDEO,
  337. .id = AV_CODEC_ID_IFF_ILBM,
  338. .priv_data_size = sizeof(IffContext),
  339. .init = decode_init,
  340. .close = decode_end,
  341. .decode = decode_frame_ilbm,
  342. .capabilities = CODEC_CAP_DR1,
  343. };
  344. AVCodec ff_iff_byterun1_decoder = {
  345. .name = "iff_byterun1",
  346. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  347. .type = AVMEDIA_TYPE_VIDEO,
  348. .id = AV_CODEC_ID_IFF_BYTERUN1,
  349. .priv_data_size = sizeof(IffContext),
  350. .init = decode_init,
  351. .close = decode_end,
  352. .decode = decode_frame_byterun1,
  353. .capabilities = CODEC_CAP_DR1,
  354. };