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.

242 lines
7.6KB

  1. /*
  2. * IFF PBM/ILBM bitmap decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/iff.c
  23. * IFF PBM/ILBM bitmap decoder
  24. */
  25. #include "bytestream.h"
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. typedef struct {
  29. AVFrame frame;
  30. int planesize;
  31. uint8_t * planebuf;
  32. } IffContext;
  33. /**
  34. * Convert CMAP buffer (stored in extradata) to lavc palette format
  35. */
  36. int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
  37. {
  38. int count, i;
  39. if (avctx->bits_per_coded_sample > 8) {
  40. av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
  41. return AVERROR_INVALIDDATA;
  42. }
  43. count = 1 << avctx->bits_per_coded_sample;
  44. if (avctx->extradata_size < count * 3) {
  45. av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
  46. return AVERROR_INVALIDDATA;
  47. }
  48. for (i=0; i < count; i++) {
  49. pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
  50. }
  51. return 0;
  52. }
  53. static av_cold int decode_init(AVCodecContext *avctx)
  54. {
  55. IffContext *s = avctx->priv_data;
  56. if (avctx->bits_per_coded_sample <= 8) {
  57. avctx->pix_fmt = PIX_FMT_PAL8;
  58. } else if (avctx->bits_per_coded_sample <= 32) {
  59. avctx->pix_fmt = PIX_FMT_BGR32;
  60. } else {
  61. return AVERROR_INVALIDDATA;
  62. }
  63. s->planesize = avctx->width / 8;
  64. s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
  65. if (!s->planebuf)
  66. return AVERROR(ENOMEM);
  67. s->frame.reference = 1;
  68. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  69. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  70. return AVERROR_UNKNOWN;
  71. }
  72. return avctx->bits_per_coded_sample <= 8 ?
  73. ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
  74. }
  75. /**
  76. * Decode interleaved plane buffer
  77. * @param dst Destination buffer
  78. * @param buf Source buffer
  79. * @param buf_size
  80. * @param bps bits_per_coded_sample
  81. * @param plane plane number to decode as
  82. */
  83. #define DECLARE_DECODEPLANE(suffix, type) \
  84. static void decodeplane##suffix(void *dst, const uint8_t const *buf, int buf_size, int bps, int plane) \
  85. { \
  86. GetBitContext gb; \
  87. int i, b; \
  88. init_get_bits(&gb, buf, buf_size * 8); \
  89. for(i = 0; i < (buf_size * 8 + bps - 1) / bps; i++) { \
  90. for (b = 0; b < bps; b++) { \
  91. ((type *)dst)[ i*bps + b ] |= get_bits1(&gb) << plane; \
  92. } \
  93. } \
  94. }
  95. DECLARE_DECODEPLANE(8, uint8_t)
  96. DECLARE_DECODEPLANE(32, uint32_t)
  97. static int decode_frame_ilbm(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. AVPacket *avpkt)
  100. {
  101. IffContext *s = avctx->priv_data;
  102. const uint8_t *buf = avpkt->data;
  103. int buf_size = avpkt->size;
  104. const uint8_t *buf_end = buf+buf_size;
  105. int y, plane;
  106. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  107. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  108. return -1;
  109. }
  110. for(y = 0; y < avctx->height; y++ ) {
  111. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  112. memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
  113. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  114. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  115. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  116. } else { // PIX_FMT_BGR32
  117. decodeplane32(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  118. }
  119. buf += s->planesize;
  120. }
  121. }
  122. *data_size = sizeof(AVFrame);
  123. *(AVFrame*)data = s->frame;
  124. return buf_size;
  125. }
  126. static int decode_frame_byterun1(AVCodecContext *avctx,
  127. void *data, int *data_size,
  128. AVPacket *avpkt)
  129. {
  130. IffContext *s = avctx->priv_data;
  131. const uint8_t *buf = avpkt->data;
  132. int buf_size = avpkt->size;
  133. const uint8_t *buf_end = buf+buf_size;
  134. int y, plane, x;
  135. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  136. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  137. return -1;
  138. }
  139. for(y = 0; y < avctx->height ; y++ ) {
  140. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  141. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  142. memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
  143. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  144. for(x = 0; x < s->planesize && buf < buf_end; ) {
  145. int8_t value = *buf++;
  146. int length;
  147. if (value >= 0) {
  148. length = value + 1;
  149. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  150. buf += length;
  151. } else if (value > -128) {
  152. length = -value + 1;
  153. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  154. } else { //noop
  155. continue;
  156. }
  157. x += length;
  158. }
  159. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  160. decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  161. } else { //PIX_FMT_BGR32
  162. decodeplane32(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  163. }
  164. }
  165. } else {
  166. for(x = 0; x < avctx->width && buf < buf_end; ) {
  167. int8_t value = *buf++;
  168. int length;
  169. if (value >= 0) {
  170. length = value + 1;
  171. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  172. buf += length;
  173. } else if (value > -128) {
  174. length = -value + 1;
  175. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  176. } else { //noop
  177. continue;
  178. }
  179. x += length;
  180. }
  181. }
  182. }
  183. *data_size = sizeof(AVFrame);
  184. *(AVFrame*)data = s->frame;
  185. return buf_size;
  186. }
  187. static av_cold int decode_end(AVCodecContext *avctx)
  188. {
  189. IffContext *s = avctx->priv_data;
  190. if (s->frame.data[0])
  191. avctx->release_buffer(avctx, &s->frame);
  192. av_freep(&s->planebuf);
  193. return 0;
  194. }
  195. AVCodec iff_ilbm_decoder = {
  196. "iff_ilbm",
  197. CODEC_TYPE_VIDEO,
  198. CODEC_ID_IFF_ILBM,
  199. sizeof(IffContext),
  200. decode_init,
  201. NULL,
  202. decode_end,
  203. decode_frame_ilbm,
  204. CODEC_CAP_DR1,
  205. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  206. };
  207. AVCodec iff_byterun1_decoder = {
  208. "iff_byterun1",
  209. CODEC_TYPE_VIDEO,
  210. CODEC_ID_IFF_BYTERUN1,
  211. sizeof(IffContext),
  212. decode_init,
  213. NULL,
  214. decode_end,
  215. decode_frame_byterun1,
  216. CODEC_CAP_DR1,
  217. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  218. };