00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "bytestream.h"
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "iff.h"
00031
00032 typedef struct {
00033 AVFrame frame;
00034 int planesize;
00035 uint8_t * planebuf;
00036 } IffContext;
00037
00041 int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
00042 {
00043 int count, i;
00044
00045 if (avctx->bits_per_coded_sample > 8) {
00046 av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
00047 return AVERROR_INVALIDDATA;
00048 }
00049
00050 count = 1 << avctx->bits_per_coded_sample;
00051 if (avctx->extradata_size < count * 3) {
00052 av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
00053 return AVERROR_INVALIDDATA;
00054 }
00055 for (i=0; i < count; i++) {
00056 pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
00057 }
00058 return 0;
00059 }
00060
00061 static av_cold int decode_init(AVCodecContext *avctx)
00062 {
00063 IffContext *s = avctx->priv_data;
00064 int err;
00065
00066 if (avctx->bits_per_coded_sample <= 8) {
00067 avctx->pix_fmt = PIX_FMT_PAL8;
00068 } else if (avctx->bits_per_coded_sample <= 32) {
00069 avctx->pix_fmt = PIX_FMT_BGR32;
00070 } else {
00071 return AVERROR_INVALIDDATA;
00072 }
00073
00074 s->planesize = avctx->width / 8;
00075 s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
00076 if (!s->planebuf)
00077 return AVERROR(ENOMEM);
00078
00079 s->frame.reference = 1;
00080 if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
00081 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00082 return err;
00083 }
00084
00085 return avctx->bits_per_coded_sample <= 8 ?
00086 ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
00087 }
00088
00097 #define DECLARE_DECODEPLANE(suffix, type) \
00098 static void decodeplane##suffix(void *dst, const uint8_t *const buf, int buf_size, int bps, int plane) \
00099 { \
00100 GetBitContext gb; \
00101 int i, b; \
00102 init_get_bits(&gb, buf, buf_size * 8); \
00103 for(i = 0; i < (buf_size * 8 + bps - 1) / bps; i++) { \
00104 for (b = 0; b < bps; b++) { \
00105 ((type *)dst)[ i*bps + b ] |= get_bits1(&gb) << plane; \
00106 } \
00107 } \
00108 }
00109 DECLARE_DECODEPLANE(8, uint8_t)
00110 DECLARE_DECODEPLANE(32, uint32_t)
00111
00112 static int decode_frame_ilbm(AVCodecContext *avctx,
00113 void *data, int *data_size,
00114 AVPacket *avpkt)
00115 {
00116 IffContext *s = avctx->priv_data;
00117 const uint8_t *buf = avpkt->data;
00118 int buf_size = avpkt->size;
00119 const uint8_t *buf_end = buf+buf_size;
00120 int y, plane;
00121
00122 if (avctx->reget_buffer(avctx, &s->frame) < 0){
00123 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00124 return -1;
00125 }
00126
00127 for(y = 0; y < avctx->height; y++ ) {
00128 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
00129 memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
00130 for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
00131 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00132 decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
00133 } else {
00134 decodeplane32(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
00135 }
00136 buf += s->planesize;
00137 }
00138 }
00139
00140 *data_size = sizeof(AVFrame);
00141 *(AVFrame*)data = s->frame;
00142 return buf_size;
00143 }
00144
00145 static int decode_frame_byterun1(AVCodecContext *avctx,
00146 void *data, int *data_size,
00147 AVPacket *avpkt)
00148 {
00149 IffContext *s = avctx->priv_data;
00150 const uint8_t *buf = avpkt->data;
00151 int buf_size = avpkt->size;
00152 const uint8_t *buf_end = buf+buf_size;
00153 int y, plane, x;
00154
00155 if (avctx->reget_buffer(avctx, &s->frame) < 0){
00156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00157 return -1;
00158 }
00159
00160 for(y = 0; y < avctx->height ; y++ ) {
00161 uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
00162 if (avctx->codec_tag == MKTAG('I','L','B','M')) {
00163 memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4));
00164 for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
00165 for(x = 0; x < s->planesize && buf < buf_end; ) {
00166 int8_t value = *buf++;
00167 int length;
00168 if (value >= 0) {
00169 length = value + 1;
00170 memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
00171 buf += length;
00172 } else if (value > -128) {
00173 length = -value + 1;
00174 memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
00175 } else {
00176 continue;
00177 }
00178 x += length;
00179 }
00180 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00181 decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
00182 } else {
00183 decodeplane32(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
00184 }
00185 }
00186 } else {
00187 for(x = 0; x < avctx->width && buf < buf_end; ) {
00188 int8_t value = *buf++;
00189 int length;
00190 if (value >= 0) {
00191 length = value + 1;
00192 memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
00193 buf += length;
00194 } else if (value > -128) {
00195 length = -value + 1;
00196 memset(row + x, *buf++, FFMIN(length, avctx->width - x));
00197 } else {
00198 continue;
00199 }
00200 x += length;
00201 }
00202 }
00203 }
00204
00205 *data_size = sizeof(AVFrame);
00206 *(AVFrame*)data = s->frame;
00207 return buf_size;
00208 }
00209
00210 static av_cold int decode_end(AVCodecContext *avctx)
00211 {
00212 IffContext *s = avctx->priv_data;
00213 if (s->frame.data[0])
00214 avctx->release_buffer(avctx, &s->frame);
00215 av_freep(&s->planebuf);
00216 return 0;
00217 }
00218
00219 AVCodec iff_ilbm_decoder = {
00220 "iff_ilbm",
00221 AVMEDIA_TYPE_VIDEO,
00222 CODEC_ID_IFF_ILBM,
00223 sizeof(IffContext),
00224 decode_init,
00225 NULL,
00226 decode_end,
00227 decode_frame_ilbm,
00228 CODEC_CAP_DR1,
00229 .long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
00230 };
00231
00232 AVCodec iff_byterun1_decoder = {
00233 "iff_byterun1",
00234 AVMEDIA_TYPE_VIDEO,
00235 CODEC_ID_IFF_BYTERUN1,
00236 sizeof(IffContext),
00237 decode_init,
00238 NULL,
00239 decode_end,
00240 decode_frame_byterun1,
00241 CODEC_CAP_DR1,
00242 .long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
00243 };