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.

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