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.

331 lines
11KB

  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. /**
  76. * Convert CMAP buffer (stored in extradata) to lavc palette format
  77. */
  78. int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  79. {
  80. int count, i;
  81. if (avctx->bits_per_coded_sample > 8) {
  82. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. count = 1 << avctx->bits_per_coded_sample;
  86. if (avctx->extradata_size < count * 3) {
  87. av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
  88. return AVERROR_INVALIDDATA;
  89. }
  90. for (i=0; i < count; i++) {
  91. pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
  92. }
  93. return 0;
  94. }
  95. static av_cold int decode_init(AVCodecContext *avctx)
  96. {
  97. IffContext *s = avctx->priv_data;
  98. int err;
  99. if (avctx->bits_per_coded_sample <= 8) {
  100. avctx->pix_fmt = PIX_FMT_PAL8;
  101. } else if (avctx->bits_per_coded_sample <= 32) {
  102. avctx->pix_fmt = PIX_FMT_BGR32;
  103. } else {
  104. return AVERROR_INVALIDDATA;
  105. }
  106. s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
  107. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  108. if (!s->planebuf)
  109. return AVERROR(ENOMEM);
  110. s->frame.reference = 1;
  111. if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
  112. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  113. return err;
  114. }
  115. return avctx->bits_per_coded_sample <= 8 ?
  116. ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
  117. }
  118. /**
  119. * Decode interleaved plane buffer up to 8bpp
  120. * @param dst Destination buffer
  121. * @param buf Source buffer
  122. * @param buf_size
  123. * @param plane plane number to decode as
  124. */
  125. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
  126. {
  127. const uint64_t *lut = plane8_lut[plane];
  128. while (buf_size--) {
  129. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  130. AV_WN64A(dst, v);
  131. dst += 8;
  132. }
  133. }
  134. /**
  135. * Decode interleaved plane buffer up to 24bpp
  136. * @param dst Destination buffer
  137. * @param buf Source buffer
  138. * @param buf_size
  139. * @param plane plane number to decode as
  140. */
  141. static void decodeplane32(uint32_t *dst, const uint8_t *const buf, int buf_size, int plane)
  142. {
  143. GetBitContext gb;
  144. int i;
  145. const int b = buf_size * 8;
  146. init_get_bits(&gb, buf, buf_size * 8);
  147. for(i = 0; i < b; i++) {
  148. dst[i] |= get_bits1(&gb) << plane;
  149. }
  150. }
  151. static int decode_frame_ilbm(AVCodecContext *avctx,
  152. void *data, int *data_size,
  153. AVPacket *avpkt)
  154. {
  155. IffContext *s = avctx->priv_data;
  156. const uint8_t *buf = avpkt->data;
  157. int buf_size = avpkt->size;
  158. const uint8_t *buf_end = buf+buf_size;
  159. int y, plane;
  160. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  161. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  162. return -1;
  163. }
  164. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  165. for(y = 0; y < avctx->height; y++ ) {
  166. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  167. memset(row, 0, avctx->width);
  168. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  169. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  170. buf += s->planesize;
  171. }
  172. }
  173. } else { // PIX_FMT_BGR32
  174. for(y = 0; y < avctx->height; y++ ) {
  175. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  176. memset(row, 0, avctx->width << 2);
  177. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  178. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
  179. buf += s->planesize;
  180. }
  181. }
  182. }
  183. *data_size = sizeof(AVFrame);
  184. *(AVFrame*)data = s->frame;
  185. return buf_size;
  186. }
  187. static int decode_frame_byterun1(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, x;
  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; plane++) {
  206. for(x = 0; x < s->planesize && buf < buf_end; ) {
  207. int8_t value = *buf++;
  208. unsigned length;
  209. if (value >= 0) {
  210. length = value + 1;
  211. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  212. buf += length;
  213. } else if (value > -128) {
  214. length = -value + 1;
  215. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  216. } else { //noop
  217. continue;
  218. }
  219. x += length;
  220. }
  221. decodeplane8(row, s->planebuf, s->planesize, plane);
  222. }
  223. }
  224. } else { //PIX_FMT_BGR32
  225. for(y = 0; y < avctx->height ; y++ ) {
  226. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  227. memset(row, 0, avctx->width << 2);
  228. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  229. for(x = 0; x < s->planesize && buf < buf_end; ) {
  230. int8_t value = *buf++;
  231. unsigned length;
  232. if (value >= 0) {
  233. length = value + 1;
  234. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  235. buf += length;
  236. } else if (value > -128) {
  237. length = -value + 1;
  238. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  239. } else { // noop
  240. continue;
  241. }
  242. x += length;
  243. }
  244. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, plane);
  245. }
  246. }
  247. }
  248. } else {
  249. for(y = 0; y < avctx->height ; y++ ) {
  250. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  251. for(x = 0; x < avctx->width && buf < buf_end; ) {
  252. int8_t value = *buf++;
  253. unsigned length;
  254. if (value >= 0) {
  255. length = value + 1;
  256. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  257. buf += length;
  258. } else if (value > -128) {
  259. length = -value + 1;
  260. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  261. } else { //noop
  262. continue;
  263. }
  264. x += length;
  265. }
  266. }
  267. }
  268. *data_size = sizeof(AVFrame);
  269. *(AVFrame*)data = s->frame;
  270. return buf_size;
  271. }
  272. static av_cold int decode_end(AVCodecContext *avctx)
  273. {
  274. IffContext *s = avctx->priv_data;
  275. if (s->frame.data[0])
  276. avctx->release_buffer(avctx, &s->frame);
  277. av_freep(&s->planebuf);
  278. return 0;
  279. }
  280. AVCodec iff_ilbm_decoder = {
  281. "iff_ilbm",
  282. AVMEDIA_TYPE_VIDEO,
  283. CODEC_ID_IFF_ILBM,
  284. sizeof(IffContext),
  285. decode_init,
  286. NULL,
  287. decode_end,
  288. decode_frame_ilbm,
  289. CODEC_CAP_DR1,
  290. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  291. };
  292. AVCodec iff_byterun1_decoder = {
  293. "iff_byterun1",
  294. AVMEDIA_TYPE_VIDEO,
  295. CODEC_ID_IFF_BYTERUN1,
  296. sizeof(IffContext),
  297. decode_init,
  298. NULL,
  299. decode_end,
  300. decode_frame_byterun1,
  301. CODEC_CAP_DR1,
  302. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  303. };