00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 #include "msrledec.h"
00034
00035 typedef struct AascContext {
00036 AVCodecContext *avctx;
00037 GetByteContext gb;
00038 AVFrame frame;
00039
00040 uint32_t palette[AVPALETTE_COUNT];
00041 int palette_size;
00042 } AascContext;
00043
00044 static av_cold int aasc_decode_init(AVCodecContext *avctx)
00045 {
00046 AascContext *s = avctx->priv_data;
00047 uint8_t *ptr;
00048 int i;
00049
00050 s->avctx = avctx;
00051 switch (avctx->bits_per_coded_sample) {
00052 case 8:
00053 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00054
00055 ptr = avctx->extradata;
00056 s->palette_size = FFMIN(avctx->extradata_size, AVPALETTE_SIZE);
00057 for (i = 0; i < s->palette_size / 4; i++) {
00058 s->palette[i] = 0xFFU << 24 | AV_RL32(ptr);
00059 ptr += 4;
00060 }
00061 break;
00062 case 16:
00063 avctx->pix_fmt = AV_PIX_FMT_RGB555;
00064 break;
00065 case 24:
00066 avctx->pix_fmt = AV_PIX_FMT_BGR24;
00067 break;
00068 default:
00069 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
00070 return -1;
00071 }
00072 avcodec_get_frame_defaults(&s->frame);
00073
00074 return 0;
00075 }
00076
00077 static int aasc_decode_frame(AVCodecContext *avctx,
00078 void *data, int *got_frame,
00079 AVPacket *avpkt)
00080 {
00081 const uint8_t *buf = avpkt->data;
00082 int buf_size = avpkt->size;
00083 AascContext *s = avctx->priv_data;
00084 int compr, i, stride, psize;
00085
00086 if (buf_size < 4) {
00087 av_log(avctx, AV_LOG_ERROR, "frame too short\n");
00088 return AVERROR_INVALIDDATA;
00089 }
00090
00091 s->frame.reference = 3;
00092 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00093 if (avctx->reget_buffer(avctx, &s->frame)) {
00094 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00095 return -1;
00096 }
00097
00098 compr = AV_RL32(buf);
00099 buf += 4;
00100 buf_size -= 4;
00101 psize = avctx->bits_per_coded_sample / 8;
00102 switch (avctx->codec_tag) {
00103 case MKTAG('A', 'A', 'S', '4'):
00104 bytestream2_init(&s->gb, buf - 4, buf_size + 4);
00105 ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
00106 break;
00107 case MKTAG('A', 'A', 'S', 'C'):
00108 switch(compr){
00109 case 0:
00110 stride = (avctx->width * psize + psize) & ~psize;
00111 for(i = avctx->height - 1; i >= 0; i--){
00112 if(avctx->width * psize > buf_size){
00113 av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
00114 break;
00115 }
00116 memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width * psize);
00117 buf += stride;
00118 buf_size -= stride;
00119 }
00120 break;
00121 case 1:
00122 bytestream2_init(&s->gb, buf, buf_size);
00123 ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
00124 break;
00125 default:
00126 av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
00127 return -1;
00128 }
00129 break;
00130 default:
00131 av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
00132 return -1;
00133 }
00134
00135 if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
00136 memcpy(s->frame.data[1], s->palette, s->palette_size);
00137
00138 *got_frame = 1;
00139 *(AVFrame*)data = s->frame;
00140
00141
00142 return buf_size;
00143 }
00144
00145 static av_cold int aasc_decode_end(AVCodecContext *avctx)
00146 {
00147 AascContext *s = avctx->priv_data;
00148
00149
00150 if (s->frame.data[0])
00151 avctx->release_buffer(avctx, &s->frame);
00152
00153 return 0;
00154 }
00155
00156 AVCodec ff_aasc_decoder = {
00157 .name = "aasc",
00158 .type = AVMEDIA_TYPE_VIDEO,
00159 .id = AV_CODEC_ID_AASC,
00160 .priv_data_size = sizeof(AascContext),
00161 .init = aasc_decode_init,
00162 .close = aasc_decode_end,
00163 .decode = aasc_decode_frame,
00164 .capabilities = CODEC_CAP_DR1,
00165 .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
00166 };