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.

333 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 bps bits_per_coded_sample (must be <= 8)
  124. * @param plane plane number to decode as
  125. */
  126. static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int bps, int plane)
  127. {
  128. const uint64_t *lut = plane8_lut[plane];
  129. while (buf_size--) {
  130. uint64_t v = AV_RN64A(dst) | lut[*buf++];
  131. AV_WN64A(dst, v);
  132. dst += 8;
  133. }
  134. }
  135. /**
  136. * Decode interleaved plane buffer up to 24bpp
  137. * @param dst Destination buffer
  138. * @param buf Source buffer
  139. * @param buf_size
  140. * @param bps bits_per_coded_sample
  141. * @param plane plane number to decode as
  142. */
  143. static void decodeplane32(uint32_t *dst, const uint8_t *const buf, int buf_size, int bps, int plane)
  144. {
  145. GetBitContext gb;
  146. int i;
  147. const int b = buf_size * 8;
  148. init_get_bits(&gb, buf, buf_size * 8);
  149. for(i = 0; i < b; i++) {
  150. dst[i] |= get_bits1(&gb) << plane;
  151. }
  152. }
  153. static int decode_frame_ilbm(AVCodecContext *avctx,
  154. void *data, int *data_size,
  155. AVPacket *avpkt)
  156. {
  157. IffContext *s = avctx->priv_data;
  158. const uint8_t *buf = avpkt->data;
  159. int buf_size = avpkt->size;
  160. const uint8_t *buf_end = buf+buf_size;
  161. int y, plane;
  162. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  163. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  164. return -1;
  165. }
  166. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  167. for(y = 0; y < avctx->height; y++ ) {
  168. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  169. memset(row, 0, avctx->width);
  170. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  171. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  172. buf += s->planesize;
  173. }
  174. }
  175. } else { // PIX_FMT_BGR32
  176. for(y = 0; y < avctx->height; y++ ) {
  177. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  178. memset(row, 0, avctx->width << 2);
  179. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  180. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  181. buf += s->planesize;
  182. }
  183. }
  184. }
  185. *data_size = sizeof(AVFrame);
  186. *(AVFrame*)data = s->frame;
  187. return buf_size;
  188. }
  189. static int decode_frame_byterun1(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, x;
  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->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  203. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  204. for(y = 0; y < avctx->height ; y++ ) {
  205. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  206. memset(row, 0, avctx->width);
  207. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  208. for(x = 0; x < s->planesize && buf < buf_end; ) {
  209. int8_t value = *buf++;
  210. unsigned length;
  211. if (value >= 0) {
  212. length = value + 1;
  213. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  214. buf += length;
  215. } else if (value > -128) {
  216. length = -value + 1;
  217. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  218. } else { //noop
  219. continue;
  220. }
  221. x += length;
  222. }
  223. decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  224. }
  225. }
  226. } else { //PIX_FMT_BGR32
  227. for(y = 0; y < avctx->height ; y++ ) {
  228. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  229. memset(row, 0, avctx->width << 2);
  230. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  231. for(x = 0; x < s->planesize && buf < buf_end; ) {
  232. int8_t value = *buf++;
  233. unsigned length;
  234. if (value >= 0) {
  235. length = value + 1;
  236. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  237. buf += length;
  238. } else if (value > -128) {
  239. length = -value + 1;
  240. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  241. } else { // noop
  242. continue;
  243. }
  244. x += length;
  245. }
  246. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  247. }
  248. }
  249. }
  250. } else {
  251. for(y = 0; y < avctx->height ; y++ ) {
  252. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  253. for(x = 0; x < avctx->width && buf < buf_end; ) {
  254. int8_t value = *buf++;
  255. unsigned length;
  256. if (value >= 0) {
  257. length = value + 1;
  258. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  259. buf += length;
  260. } else if (value > -128) {
  261. length = -value + 1;
  262. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  263. } else { //noop
  264. continue;
  265. }
  266. x += length;
  267. }
  268. }
  269. }
  270. *data_size = sizeof(AVFrame);
  271. *(AVFrame*)data = s->frame;
  272. return buf_size;
  273. }
  274. static av_cold int decode_end(AVCodecContext *avctx)
  275. {
  276. IffContext *s = avctx->priv_data;
  277. if (s->frame.data[0])
  278. avctx->release_buffer(avctx, &s->frame);
  279. av_freep(&s->planebuf);
  280. return 0;
  281. }
  282. AVCodec iff_ilbm_decoder = {
  283. "iff_ilbm",
  284. AVMEDIA_TYPE_VIDEO,
  285. CODEC_ID_IFF_ILBM,
  286. sizeof(IffContext),
  287. decode_init,
  288. NULL,
  289. decode_end,
  290. decode_frame_ilbm,
  291. CODEC_CAP_DR1,
  292. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  293. };
  294. AVCodec iff_byterun1_decoder = {
  295. "iff_byterun1",
  296. AVMEDIA_TYPE_VIDEO,
  297. CODEC_ID_IFF_BYTERUN1,
  298. sizeof(IffContext),
  299. decode_init,
  300. NULL,
  301. decode_end,
  302. decode_frame_byterun1,
  303. CODEC_CAP_DR1,
  304. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  305. };