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.

371 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. /**
  105. * Convert CMAP buffer (stored in extradata) to lavc palette format
  106. */
  107. int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  108. {
  109. int count, i;
  110. if (avctx->bits_per_coded_sample > 8) {
  111. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  112. return AVERROR_INVALIDDATA;
  113. }
  114. count = 1 << avctx->bits_per_coded_sample;
  115. if (avctx->extradata_size < count * 3) {
  116. av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
  117. return AVERROR_INVALIDDATA;
  118. }
  119. for (i=0; i < count; i++) {
  120. pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
  121. }
  122. return 0;
  123. }
  124. static av_cold int decode_init(AVCodecContext *avctx)
  125. {
  126. IffContext *s = avctx->priv_data;
  127. int err;
  128. if (avctx->bits_per_coded_sample <= 8) {
  129. avctx->pix_fmt = PIX_FMT_PAL8;
  130. } else if (avctx->bits_per_coded_sample <= 32) {
  131. avctx->pix_fmt = PIX_FMT_BGR32;
  132. } else {
  133. return AVERROR_INVALIDDATA;
  134. }
  135. if ((err = avcodec_check_dimensions(avctx, avctx->width, avctx->height)))
  136. return err;
  137. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  138. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  139. if (!s->planebuf)
  140. return AVERROR(ENOMEM);
  141. s->frame.reference = 1;
  142. if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
  143. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  144. return err;
  145. }
  146. return avctx->bits_per_coded_sample <= 8 ?
  147. ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
  148. }
  149. /**
  150. * Decode interleaved plane buffer up to 8bpp
  151. * @param dst Destination buffer
  152. * @param buf Source buffer
  153. * @param buf_size
  154. * @param plane plane number to decode as
  155. */
  156. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  157. {
  158. const uint64_t *lut = plane8_lut[plane];
  159. while (buf_size--) {
  160. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  161. AV_WN64A(dst, v);
  162. dst += 8;
  163. }
  164. }
  165. /**
  166. * Decode interleaved plane buffer up to 24bpp
  167. * @param dst Destination buffer
  168. * @param buf Source buffer
  169. * @param buf_size
  170. * @param plane plane number to decode as
  171. */
  172. static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
  173. {
  174. const uint32_t *lut = plane32_lut[plane];
  175. do {
  176. unsigned mask = (*buf >> 2) & ~3;
  177. dst[0] |= lut[mask++];
  178. dst[1] |= lut[mask++];
  179. dst[2] |= lut[mask++];
  180. dst[3] |= lut[mask];
  181. mask = (*buf++ << 2) & 0x3F;
  182. dst[4] |= lut[mask++];
  183. dst[5] |= lut[mask++];
  184. dst[6] |= lut[mask++];
  185. dst[7] |= lut[mask];
  186. dst += 8;
  187. } while (--buf_size);
  188. }
  189. static int decode_frame_ilbm(AVCodecContext *avctx,
  190. void *data, int *data_size,
  191. AVPacket *avpkt)
  192. {
  193. IffContext *s = avctx->priv_data;
  194. const uint8_t *buf = avpkt->data;
  195. int buf_size = avpkt->size;
  196. const uint8_t *buf_end = buf+buf_size;
  197. int y, plane;
  198. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  199. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  200. return -1;
  201. }
  202. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  203. for(y = 0; y < avctx->height; y++ ) {
  204. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  205. memset(row, 0, avctx->width);
  206. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  207. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  208. buf += s->planesize;
  209. }
  210. }
  211. } else { // PIX_FMT_BGR32
  212. for(y = 0; y < avctx->height; y++ ) {
  213. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  214. memset(row, 0, avctx->width << 2);
  215. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  216. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  217. buf += s->planesize;
  218. }
  219. }
  220. }
  221. *data_size = sizeof(AVFrame);
  222. *(AVFrame*)data = s->frame;
  223. return buf_size;
  224. }
  225. static int decode_frame_byterun1(AVCodecContext *avctx,
  226. void *data, int *data_size,
  227. AVPacket *avpkt)
  228. {
  229. IffContext *s = avctx->priv_data;
  230. const uint8_t *buf = avpkt->data;
  231. int buf_size = avpkt->size;
  232. const uint8_t *buf_end = buf+buf_size;
  233. int y, plane, x;
  234. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  235. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  236. return -1;
  237. }
  238. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  239. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  240. for(y = 0; y < avctx->height ; y++ ) {
  241. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  242. memset(row, 0, avctx->width);
  243. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  244. for(x = 0; x < s->planesize && buf < buf_end; ) {
  245. int8_t value = *buf++;
  246. unsigned length;
  247. if (value >= 0) {
  248. length = value + 1;
  249. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  250. buf += length;
  251. } else if (value > -128) {
  252. length = -value + 1;
  253. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  254. } else { //noop
  255. continue;
  256. }
  257. x += length;
  258. }
  259. decodeplane8(row, s->planebuf, s->planesize, plane);
  260. }
  261. }
  262. } else { //PIX_FMT_BGR32
  263. for(y = 0; y < avctx->height ; y++ ) {
  264. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  265. memset(row, 0, avctx->width << 2);
  266. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  267. for(x = 0; x < s->planesize && buf < buf_end; ) {
  268. int8_t value = *buf++;
  269. unsigned length;
  270. if (value >= 0) {
  271. length = value + 1;
  272. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  273. buf += length;
  274. } else if (value > -128) {
  275. length = -value + 1;
  276. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  277. } else { // noop
  278. continue;
  279. }
  280. x += length;
  281. }
  282. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  283. }
  284. }
  285. }
  286. } else {
  287. for(y = 0; y < avctx->height ; y++ ) {
  288. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  289. for(x = 0; x < avctx->width && buf < buf_end; ) {
  290. int8_t value = *buf++;
  291. unsigned length;
  292. if (value >= 0) {
  293. length = value + 1;
  294. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  295. buf += length;
  296. } else if (value > -128) {
  297. length = -value + 1;
  298. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  299. } else { //noop
  300. continue;
  301. }
  302. x += length;
  303. }
  304. }
  305. }
  306. *data_size = sizeof(AVFrame);
  307. *(AVFrame*)data = s->frame;
  308. return buf_size;
  309. }
  310. static av_cold int decode_end(AVCodecContext *avctx)
  311. {
  312. IffContext *s = avctx->priv_data;
  313. if (s->frame.data[0])
  314. avctx->release_buffer(avctx, &s->frame);
  315. av_freep(&s->planebuf);
  316. return 0;
  317. }
  318. AVCodec iff_ilbm_decoder = {
  319. "iff_ilbm",
  320. AVMEDIA_TYPE_VIDEO,
  321. CODEC_ID_IFF_ILBM,
  322. sizeof(IffContext),
  323. decode_init,
  324. NULL,
  325. decode_end,
  326. decode_frame_ilbm,
  327. CODEC_CAP_DR1,
  328. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  329. };
  330. AVCodec iff_byterun1_decoder = {
  331. "iff_byterun1",
  332. AVMEDIA_TYPE_VIDEO,
  333. CODEC_ID_IFF_BYTERUN1,
  334. sizeof(IffContext),
  335. decode_init,
  336. NULL,
  337. decode_end,
  338. decode_frame_byterun1,
  339. CODEC_CAP_DR1,
  340. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  341. };