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 = 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 = PIX_FMT_RGB555;
00064 break;
00065 case 24:
00066 avctx->pix_fmt = 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 *data_size,
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 s->frame.reference = 3;
00087 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00088 if (avctx->reget_buffer(avctx, &s->frame)) {
00089 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00090 return -1;
00091 }
00092
00093 compr = AV_RL32(buf);
00094 buf += 4;
00095 buf_size -= 4;
00096 psize = avctx->bits_per_coded_sample / 8;
00097 switch (avctx->codec_tag) {
00098 case MKTAG('A', 'A', 'S', '4'):
00099 bytestream2_init(&s->gb, buf - 4, buf_size + 4);
00100 ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
00101 break;
00102 case MKTAG('A', 'A', 'S', 'C'):
00103 switch(compr){
00104 case 0:
00105 stride = (avctx->width * psize + psize) & ~psize;
00106 for(i = avctx->height - 1; i >= 0; i--){
00107 if(avctx->width * psize > buf_size){
00108 av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
00109 break;
00110 }
00111 memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width * psize);
00112 buf += stride;
00113 buf_size -= stride;
00114 }
00115 break;
00116 case 1:
00117 bytestream2_init(&s->gb, buf, buf_size);
00118 ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
00119 break;
00120 default:
00121 av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
00122 return -1;
00123 }
00124 break;
00125 default:
00126 av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
00127 return -1;
00128 }
00129
00130 if (avctx->pix_fmt == PIX_FMT_PAL8)
00131 memcpy(s->frame.data[1], s->palette, s->palette_size);
00132
00133 *data_size = sizeof(AVFrame);
00134 *(AVFrame*)data = s->frame;
00135
00136
00137 return buf_size;
00138 }
00139
00140 static av_cold int aasc_decode_end(AVCodecContext *avctx)
00141 {
00142 AascContext *s = avctx->priv_data;
00143
00144
00145 if (s->frame.data[0])
00146 avctx->release_buffer(avctx, &s->frame);
00147
00148 return 0;
00149 }
00150
00151 AVCodec ff_aasc_decoder = {
00152 .name = "aasc",
00153 .type = AVMEDIA_TYPE_VIDEO,
00154 .id = AV_CODEC_ID_AASC,
00155 .priv_data_size = sizeof(AascContext),
00156 .init = aasc_decode_init,
00157 .close = aasc_decode_end,
00158 .decode = aasc_decode_frame,
00159 .capabilities = CODEC_CAP_DR1,
00160 .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
00161 };