iff.c 7.6 KB

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