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