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.

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