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