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.

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