yading@10: /* yading@10: * Deluxe Paint Animation decoder yading@10: * Copyright (c) 2009 Peter Ross yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Deluxe Paint Animation decoder yading@10: */ yading@10: yading@10: #include "avcodec.h" yading@10: #include "bytestream.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct AnmContext { yading@10: AVFrame *frame; yading@10: int palette[AVPALETTE_COUNT]; yading@10: GetByteContext gb; yading@10: int x; ///< x coordinate position yading@10: } AnmContext; yading@10: yading@10: static av_cold int decode_init(AVCodecContext *avctx) yading@10: { yading@10: AnmContext *s = avctx->priv_data; yading@10: int i; yading@10: yading@10: avctx->pix_fmt = AV_PIX_FMT_PAL8; yading@10: yading@10: s->frame = av_frame_alloc(); yading@10: if (!s->frame) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size); yading@10: if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: bytestream2_skipu(&s->gb, 16 * 8); yading@10: for (i = 0; i < 256; i++) yading@10: s->palette[i] = bytestream2_get_le32u(&s->gb); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: /** yading@10: * Perform decode operation yading@10: * @param dst pointer to destination image buffer yading@10: * @param dst_end pointer to end of destination image buffer yading@10: * @param gb GetByteContext (optional, see below) yading@10: * @param pixel Fill color (optional, see below) yading@10: * @param count Pixel count yading@10: * @param x Pointer to x-axis counter yading@10: * @param width Image width yading@10: * @param linesize Destination image buffer linesize yading@10: * @return non-zero if destination buffer is exhausted yading@10: * yading@10: * a copy operation is achieved when 'gb' is set yading@10: * a fill operation is achieved when 'gb' is null and pixel is >= 0 yading@10: * a skip operation is achieved when 'gb' is null and pixel is < 0 yading@10: */ yading@10: static inline int op(uint8_t **dst, const uint8_t *dst_end, yading@10: GetByteContext *gb, yading@10: int pixel, int count, yading@10: int *x, int width, int linesize) yading@10: { yading@10: int remaining = width - *x; yading@10: while(count > 0) { yading@10: int striplen = FFMIN(count, remaining); yading@10: if (gb) { yading@10: if (bytestream2_get_bytes_left(gb) < striplen) yading@10: goto exhausted; yading@10: bytestream2_get_bufferu(gb, *dst, striplen); yading@10: } else if (pixel >= 0) yading@10: memset(*dst, pixel, striplen); yading@10: *dst += striplen; yading@10: remaining -= striplen; yading@10: count -= striplen; yading@10: if (remaining <= 0) { yading@10: *dst += linesize - width; yading@10: remaining = width; yading@10: } yading@10: if (linesize > 0) { yading@10: if (*dst >= dst_end) goto exhausted; yading@10: } else { yading@10: if (*dst <= dst_end) goto exhausted; yading@10: } yading@10: } yading@10: *x = width - remaining; yading@10: return 0; yading@10: yading@10: exhausted: yading@10: *x = width - remaining; yading@10: return 1; yading@10: } yading@10: yading@10: static int decode_frame(AVCodecContext *avctx, yading@10: void *data, int *got_frame, yading@10: AVPacket *avpkt) yading@10: { yading@10: AnmContext *s = avctx->priv_data; yading@10: const int buf_size = avpkt->size; yading@10: uint8_t *dst, *dst_end; yading@10: int count, ret; yading@10: yading@10: if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) yading@10: return ret; yading@10: dst = s->frame->data[0]; yading@10: dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height; yading@10: yading@10: bytestream2_init(&s->gb, avpkt->data, buf_size); yading@10: yading@10: if (bytestream2_get_byte(&s->gb) != 0x42) { yading@10: avpriv_request_sample(avctx, "Unknown record type"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: if (bytestream2_get_byte(&s->gb)) { yading@10: avpriv_request_sample(avctx, "Padding bytes"); yading@10: return AVERROR_PATCHWELCOME; yading@10: } yading@10: bytestream2_skip(&s->gb, 2); yading@10: yading@10: s->x = 0; yading@10: do { yading@10: /* if statements are ordered by probability */ yading@10: #define OP(gb, pixel, count) \ yading@10: op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0]) yading@10: yading@10: int type = bytestream2_get_byte(&s->gb); yading@10: count = type & 0x7F; yading@10: type >>= 7; yading@10: if (count) { yading@10: if (OP(type ? NULL : &s->gb, -1, count)) break; yading@10: } else if (!type) { yading@10: int pixel; yading@10: count = bytestream2_get_byte(&s->gb); /* count==0 gives nop */ yading@10: pixel = bytestream2_get_byte(&s->gb); yading@10: if (OP(NULL, pixel, count)) break; yading@10: } else { yading@10: int pixel; yading@10: type = bytestream2_get_le16(&s->gb); yading@10: count = type & 0x3FFF; yading@10: type >>= 14; yading@10: if (!count) { yading@10: if (type == 0) yading@10: break; // stop yading@10: if (type == 2) { yading@10: avpriv_request_sample(avctx, "Unknown opcode"); yading@10: return AVERROR_PATCHWELCOME; yading@10: } yading@10: continue; yading@10: } yading@10: pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1; yading@10: if (type == 1) count += 0x4000; yading@10: if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break; yading@10: } yading@10: } while (bytestream2_get_bytes_left(&s->gb) > 0); yading@10: yading@10: memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE); yading@10: yading@10: *got_frame = 1; yading@10: if ((ret = av_frame_ref(data, s->frame)) < 0) yading@10: return ret; yading@10: yading@10: return buf_size; yading@10: } yading@10: yading@10: static av_cold int decode_end(AVCodecContext *avctx) yading@10: { yading@10: AnmContext *s = avctx->priv_data; yading@10: yading@10: av_frame_free(&s->frame); yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_anm_decoder = { yading@10: .name = "anm", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .id = AV_CODEC_ID_ANM, yading@10: .priv_data_size = sizeof(AnmContext), yading@10: .init = decode_init, yading@10: .close = decode_end, yading@10: .decode = decode_frame, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"), yading@10: };