00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "bytestream.h"
00029
00030 typedef struct AnmContext {
00031 AVFrame frame;
00032 int palette[AVPALETTE_COUNT];
00033 int x;
00034 } AnmContext;
00035
00036 static av_cold int decode_init(AVCodecContext *avctx)
00037 {
00038 AnmContext *s = avctx->priv_data;
00039 const uint8_t *buf;
00040 int i;
00041
00042 avctx->pix_fmt = PIX_FMT_PAL8;
00043
00044 if (avctx->extradata_size != 16*8 + 4*256)
00045 return -1;
00046
00047 avcodec_get_frame_defaults(&s->frame);
00048 s->frame.reference = 1;
00049
00050 buf = avctx->extradata + 16*8;
00051 for (i = 0; i < 256; i++)
00052 s->palette[i] = bytestream_get_le32(&buf);
00053
00054 return 0;
00055 }
00056
00072 static inline int op(uint8_t **dst, const uint8_t *dst_end,
00073 const uint8_t **buf, const uint8_t *buf_end,
00074 int pixel, int count,
00075 int *x, int width, int linesize)
00076 {
00077 int remaining = width - *x;
00078 while(count > 0) {
00079 int striplen = FFMIN(count, remaining);
00080 if (buf) {
00081 striplen = FFMIN(striplen, buf_end - *buf);
00082 if (*buf >= buf_end)
00083 goto exhausted;
00084 memcpy(*dst, *buf, striplen);
00085 *buf += striplen;
00086 } else if (pixel >= 0)
00087 memset(*dst, pixel, striplen);
00088 *dst += striplen;
00089 remaining -= striplen;
00090 count -= striplen;
00091 if (remaining <= 0) {
00092 *dst += linesize - width;
00093 remaining = width;
00094 }
00095 if (linesize > 0) {
00096 if (*dst >= dst_end) goto exhausted;
00097 } else {
00098 if (*dst <= dst_end) goto exhausted;
00099 }
00100 }
00101 *x = width - remaining;
00102 return 0;
00103
00104 exhausted:
00105 *x = width - remaining;
00106 return 1;
00107 }
00108
00109 static int decode_frame(AVCodecContext *avctx,
00110 void *data, int *data_size,
00111 AVPacket *avpkt)
00112 {
00113 AnmContext *s = avctx->priv_data;
00114 const uint8_t *buf = avpkt->data;
00115 const int buf_size = avpkt->size;
00116 const uint8_t *buf_end = buf + buf_size;
00117 uint8_t *dst, *dst_end;
00118 int count;
00119
00120 if(avctx->reget_buffer(avctx, &s->frame) < 0){
00121 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00122 return -1;
00123 }
00124 dst = s->frame.data[0];
00125 dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
00126
00127 if (buf[0] != 0x42) {
00128 av_log_ask_for_sample(avctx, "unknown record type\n");
00129 return buf_size;
00130 }
00131 if (buf[1]) {
00132 av_log_ask_for_sample(avctx, "padding bytes not supported\n");
00133 return buf_size;
00134 }
00135 buf += 4;
00136
00137 s->x = 0;
00138 do {
00139
00140 #define OP(buf, pixel, count) \
00141 op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
00142
00143 int type = bytestream_get_byte(&buf);
00144 count = type & 0x7F;
00145 type >>= 7;
00146 if (count) {
00147 if (OP(type ? NULL : &buf, -1, count)) break;
00148 } else if (!type) {
00149 int pixel;
00150 count = bytestream_get_byte(&buf);
00151 pixel = bytestream_get_byte(&buf);
00152 if (OP(NULL, pixel, count)) break;
00153 } else {
00154 int pixel;
00155 type = bytestream_get_le16(&buf);
00156 count = type & 0x3FFF;
00157 type >>= 14;
00158 if (!count) {
00159 if (type == 0)
00160 break;
00161 if (type == 2) {
00162 av_log_ask_for_sample(avctx, "unknown opcode");
00163 return AVERROR_INVALIDDATA;
00164 }
00165 continue;
00166 }
00167 pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
00168 if (type == 1) count += 0x4000;
00169 if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
00170 }
00171 } while (buf + 1 < buf_end);
00172
00173 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00174
00175 *data_size = sizeof(AVFrame);
00176 *(AVFrame*)data = s->frame;
00177 return buf_size;
00178 }
00179
00180 static av_cold int decode_end(AVCodecContext *avctx)
00181 {
00182 AnmContext *s = avctx->priv_data;
00183 if (s->frame.data[0])
00184 avctx->release_buffer(avctx, &s->frame);
00185 return 0;
00186 }
00187
00188 AVCodec ff_anm_decoder = {
00189 "anm",
00190 AVMEDIA_TYPE_VIDEO,
00191 CODEC_ID_ANM,
00192 sizeof(AnmContext),
00193 decode_init,
00194 NULL,
00195 decode_end,
00196 decode_frame,
00197 CODEC_CAP_DR1,
00198 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
00199 };