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.

261 lines
8.0KB

  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
  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 up to 8bpp
  79. * @param dst Destination buffer
  80. * @param buf Source buffer
  81. * @param buf_size
  82. * @param bps bits_per_coded_sample (must be <= 8)
  83. * @param plane plane number to decode as
  84. */
  85. static void decodeplane8(uint8_t *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. dst[ i*bps + b ] |= get_bits1(&gb) << plane;
  93. }
  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. int i, b;
  108. init_get_bits(&gb, buf, buf_size * 8);
  109. for(i = 0; i < (buf_size * 8 + bps - 1) / bps; i++) {
  110. for (b = 0; b < bps; b++) {
  111. dst[ i*bps + b ] |= get_bits1(&gb) << plane;
  112. }
  113. }
  114. }
  115. static int decode_frame_ilbm(AVCodecContext *avctx,
  116. void *data, int *data_size,
  117. AVPacket *avpkt)
  118. {
  119. IffContext *s = avctx->priv_data;
  120. const uint8_t *buf = avpkt->data;
  121. int buf_size = avpkt->size;
  122. const uint8_t *buf_end = buf+buf_size;
  123. int y, plane;
  124. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  125. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  126. return -1;
  127. }
  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->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
  131. for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
  132. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  133. decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  134. } else { // PIX_FMT_BGR32
  135. decodeplane32(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
  136. }
  137. buf += s->planesize;
  138. }
  139. }
  140. *data_size = sizeof(AVFrame);
  141. *(AVFrame*)data = s->frame;
  142. return buf_size;
  143. }
  144. static int decode_frame_byterun1(AVCodecContext *avctx,
  145. void *data, int *data_size,
  146. AVPacket *avpkt)
  147. {
  148. IffContext *s = avctx->priv_data;
  149. const uint8_t *buf = avpkt->data;
  150. int buf_size = avpkt->size;
  151. const uint8_t *buf_end = buf+buf_size;
  152. int y, plane, x;
  153. if (avctx->reget_buffer(avctx, &s->frame) < 0){
  154. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  155. return -1;
  156. }
  157. for(y = 0; y < avctx->height ; y++ ) {
  158. uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
  159. if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
  160. memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
  161. for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
  162. for(x = 0; x < s->planesize && buf < buf_end; ) {
  163. int8_t value = *buf++;
  164. int length;
  165. if (value >= 0) {
  166. length = value + 1;
  167. memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
  168. buf += length;
  169. } else if (value > -128) {
  170. length = -value + 1;
  171. memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
  172. } else { //noop
  173. continue;
  174. }
  175. x += length;
  176. }
  177. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  178. decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  179. } else { //PIX_FMT_BGR32
  180. decodeplane32(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
  181. }
  182. }
  183. } else {
  184. for(x = 0; x < avctx->width && buf < buf_end; ) {
  185. int8_t value = *buf++;
  186. int length;
  187. if (value >= 0) {
  188. length = value + 1;
  189. memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
  190. buf += length;
  191. } else if (value > -128) {
  192. length = -value + 1;
  193. memset(row + x, *buf++, FFMIN(length, avctx->width - x));
  194. } else { //noop
  195. continue;
  196. }
  197. x += length;
  198. }
  199. }
  200. }
  201. *data_size = sizeof(AVFrame);
  202. *(AVFrame*)data = s->frame;
  203. return buf_size;
  204. }
  205. static av_cold int decode_end(AVCodecContext *avctx)
  206. {
  207. IffContext *s = avctx->priv_data;
  208. if (s->frame.data[0])
  209. avctx->release_buffer(avctx, &s->frame);
  210. av_freep(&s->planebuf);
  211. return 0;
  212. }
  213. AVCodec iff_ilbm_decoder = {
  214. "iff_ilbm",
  215. AVMEDIA_TYPE_VIDEO,
  216. CODEC_ID_IFF_ILBM,
  217. sizeof(IffContext),
  218. decode_init,
  219. NULL,
  220. decode_end,
  221. decode_frame_ilbm,
  222. CODEC_CAP_DR1,
  223. .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
  224. };
  225. AVCodec iff_byterun1_decoder = {
  226. "iff_byterun1",
  227. AVMEDIA_TYPE_VIDEO,
  228. CODEC_ID_IFF_BYTERUN1,
  229. sizeof(IffContext),
  230. decode_init,
  231. NULL,
  232. decode_end,
  233. decode_frame_byterun1,
  234. CODEC_CAP_DR1,
  235. .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
  236. };