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.

291 lines
9.5KB

  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. unsigned planesize;
  33. uint8_t * planebuf;
  34. } IffContext;
  35. /**
  36. * Convert CMAP buffer (stored in extradata) to lavc palette format
  37. */
  38. int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  39. {
  40. unsigned count, i;
  41. if (avctx->bits_per_coded_sample > 8) {
  42. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  43. return AVERROR_INVALIDDATA;
  44. }
  45. count = 1 << avctx->bits_per_coded_sample;
  46. if (avctx->extradata_size < count * 3) {
  47. av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
  48. return AVERROR_INVALIDDATA;
  49. }
  50. for (i=0; i < count; i++) {
  51. pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
  52. }
  53. return 0;
  54. }
  55. static av_cold int decode_init(AVCodecContext *avctx)
  56. {
  57. IffContext *s = avctx->priv_data;
  58. int err;
  59. if (avctx->bits_per_coded_sample <= 8) {
  60. avctx->pix_fmt = PIX_FMT_PAL8;
  61. } else if (avctx->bits_per_coded_sample <= 32) {
  62. avctx->pix_fmt = PIX_FMT_BGR32;
  63. } else {
  64. return AVERROR_INVALIDDATA;
  65. }
  66. s->planesize = avctx->width >> 3;
  67. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  68. if (!s->planebuf)
  69. return AVERROR(ENOMEM);
  70. s->frame.reference = 1;
  71. if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
  72. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  73. return err;
  74. }
  75. return avctx->bits_per_coded_sample <= 8 ?
  76. ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
  77. }
  78. /**
  79. * Decode interleaved plane buffer up to 8bpp
  80. * @param dst Destination buffer
  81. * @param buf Source buffer
  82. * @param buf_size
  83. * @param bps bits_per_coded_sample (must be <= 8)
  84. * @param plane plane number to decode as
  85. */
  86. static void decodeplane8(uint8_t *dst, const uint8_t *const buf, int buf_size, int bps, int plane)
  87. {
  88. GetBitContext gb;
  89. unsigned int i;
  90. const unsigned b = (buf_size * 8) + bps - 1;
  91. init_get_bits(&gb, buf, buf_size * 8);
  92. for(i = 0; i < b; i++) {
  93. dst[i] |= get_bits1(&gb) << plane;
  94. }
  95. }
  96. /**
  97. * Decode interleaved plane buffer up to 24bpp
  98. * @param dst Destination buffer
  99. * @param buf Source buffer
  100. * @param buf_size
  101. * @param bps bits_per_coded_sample
  102. * @param plane plane number to decode as
  103. */
  104. static void decodeplane32(uint32_t *dst, const uint8_t *const buf, int buf_size, int bps, int plane)
  105. {
  106. GetBitContext gb;
  107. unsigned i;
  108. const unsigned b = (buf_size * 8) + bps - 1;
  109. init_get_bits(&gb, buf, buf_size * 8);
  110. for(i = 0; i < b; i++) {
  111. dst[i] |= get_bits1(&gb) << plane;
  112. }
  113. }
  114. static int decode_frame_ilbm(AVCodecContext *avctx,
  115. void *data, int *data_size,
  116. AVPacket *avpkt)
  117. {
  118. IffContext *s = avctx->priv_data;
  119. const uint8_t *buf = avpkt->data;
  120. unsigned buf_size = avpkt->size;
  121. const uint8_t *buf_end = buf+buf_size;
  122. unsigned y, plane;
  123. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  124. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  125. return -1;
  126. }
  127. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  128. for(y = 0; y < avctx->height; y++ ) {
  129. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  130. memset(row, 0, avctx->width);
  131. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  132. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  133. buf += s->planesize;
  134. }
  135. }
  136. } else { // PIX_FMT_BGR32
  137. for(y = 0; y < avctx->height; y++ ) {
  138. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  139. memset(row, 0, avctx->width << 2);
  140. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  141. decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  142. buf += s->planesize;
  143. }
  144. }
  145. }
  146. *data_size = sizeof(AVFrame);
  147. *(AVFrame*)data = s->frame;
  148. return buf_size;
  149. }
  150. static int decode_frame_byterun1(AVCodecContext *avctx,
  151. void *data, int *data_size,
  152. AVPacket *avpkt)
  153. {
  154. IffContext *s = avctx->priv_data;
  155. const uint8_t *buf = avpkt->data;
  156. unsigned buf_size = avpkt->size;
  157. const uint8_t *buf_end = buf+buf_size;
  158. unsigned y, plane, x;
  159. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  160. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  161. return -1;
  162. }
  163. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  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; plane++) {
  169. for(x = 0; x < s->planesize && buf < buf_end; ) {
  170. int8_t value = *buf++;
  171. unsigned length;
  172. if (value >= 0) {
  173. length = value + 1;
  174. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  175. buf += length;
  176. } else if (value > -128) {
  177. length = -value + 1;
  178. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  179. } else { //noop
  180. continue;
  181. }
  182. x += length;
  183. }
  184. decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  185. }
  186. }
  187. } else { //PIX_FMT_BGR32
  188. for(y = 0; y < avctx->height ; y++ ) {
  189. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  190. memset(row, 0, avctx->width << 2);
  191. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  192. for(x = 0; x < s->planesize && buf < buf_end; ) {
  193. int8_t value = *buf++;
  194. unsigned length;
  195. if (value >= 0) {
  196. length = value + 1;
  197. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  198. buf += length;
  199. } else if (value > -128) {
  200. length = -value + 1;
  201. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  202. } else { // noop
  203. continue;
  204. }
  205. x += length;
  206. }
  207. decodeplane32((uint32_t *) row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  208. }
  209. }
  210. }
  211. } else {
  212. for(y = 0; y < avctx->height ; y++ ) {
  213. uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
  214. for(x = 0; x < avctx->width && buf < buf_end; ) {
  215. int8_t value = *buf++;
  216. unsigned length;
  217. if (value >= 0) {
  218. length = value + 1;
  219. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  220. buf += length;
  221. } else if (value > -128) {
  222. length = -value + 1;
  223. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  224. } else { //noop
  225. continue;
  226. }
  227. x += length;
  228. }
  229. }
  230. }
  231. *data_size = sizeof(AVFrame);
  232. *(AVFrame*)data = s->frame;
  233. return buf_size;
  234. }
  235. static av_cold int decode_end(AVCodecContext *avctx)
  236. {
  237. IffContext *s = avctx->priv_data;
  238. if (s->frame.data[0])
  239. avctx->release_buffer(avctx, &s->frame);
  240. av_freep(&s->planebuf);
  241. return 0;
  242. }
  243. AVCodec iff_ilbm_decoder = {
  244. "iff_ilbm",
  245. AVMEDIA_TYPE_VIDEO,
  246. CODEC_ID_IFF_ILBM,
  247. sizeof(IffContext),
  248. decode_init,
  249. NULL,
  250. decode_end,
  251. decode_frame_ilbm,
  252. CODEC_CAP_DR1,
  253. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  254. };
  255. AVCodec iff_byterun1_decoder = {
  256. "iff_byterun1",
  257. AVMEDIA_TYPE_VIDEO,
  258. CODEC_ID_IFF_BYTERUN1,
  259. sizeof(IffContext),
  260. decode_init,
  261. NULL,
  262. decode_end,
  263. decode_frame_byterun1,
  264. CODEC_CAP_DR1,
  265. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  266. };