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