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