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.

382 lines
13KB

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