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
00030 #include "libavutil/intreadwrite.h"
00031 #include "avcodec.h"
00032
00033 #include <zlib.h>
00034
00035
00036
00037
00038 typedef struct DxaDecContext {
00039 AVFrame pic, prev;
00040
00041 int dsize;
00042 uint8_t *decomp_buf;
00043 uint32_t pal[256];
00044 } DxaDecContext;
00045
00046 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
00047 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
00048
00049 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
00050 {
00051 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
00052 int i, j, k;
00053 int type, x, y, d, d2;
00054 int stride = c->pic.linesize[0];
00055 uint32_t mask;
00056
00057 code = src + 12;
00058 data = code + ((avctx->width * avctx->height) >> 4);
00059 mv = data + AV_RB32(src + 0);
00060 msk = mv + AV_RB32(src + 4);
00061
00062 for(j = 0; j < avctx->height; j += 4){
00063 for(i = 0; i < avctx->width; i += 4){
00064 tmp = dst + i;
00065 tmp2 = ref + i;
00066 type = *code++;
00067 switch(type){
00068 case 4:
00069 x = (*mv) >> 4; if(x & 8) x = 8 - x;
00070 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
00071 tmp2 += x + y*stride;
00072 case 0:
00073 case 5:
00074 for(y = 0; y < 4; y++){
00075 memcpy(tmp, tmp2, 4);
00076 tmp += stride;
00077 tmp2 += stride;
00078 }
00079 break;
00080 case 1:
00081 case 10:
00082 case 11:
00083 case 12:
00084 case 13:
00085 case 14:
00086 case 15:
00087 if(type == 1){
00088 mask = AV_RB16(msk);
00089 msk += 2;
00090 }else{
00091 type -= 10;
00092 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
00093 msk++;
00094 }
00095 for(y = 0; y < 4; y++){
00096 for(x = 0; x < 4; x++){
00097 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
00098 mask <<= 1;
00099 }
00100 tmp += stride;
00101 tmp2 += stride;
00102 }
00103 break;
00104 case 2:
00105 for(y = 0; y < 4; y++){
00106 memset(tmp, data[0], 4);
00107 tmp += stride;
00108 }
00109 data++;
00110 break;
00111 case 3:
00112 for(y = 0; y < 4; y++){
00113 memcpy(tmp, data, 4);
00114 data += 4;
00115 tmp += stride;
00116 }
00117 break;
00118 case 8:
00119 mask = *msk++;
00120 for(k = 0; k < 4; k++){
00121 d = ((k & 1) << 1) + ((k & 2) * stride);
00122 d2 = ((k & 1) << 1) + ((k & 2) * stride);
00123 tmp2 = ref + i + d2;
00124 switch(mask & 0xC0){
00125 case 0x80:
00126 x = (*mv) >> 4; if(x & 8) x = 8 - x;
00127 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
00128 tmp2 += x + y*stride;
00129 case 0x00:
00130 tmp[d + 0 ] = tmp2[0];
00131 tmp[d + 1 ] = tmp2[1];
00132 tmp[d + 0 + stride] = tmp2[0 + stride];
00133 tmp[d + 1 + stride] = tmp2[1 + stride];
00134 break;
00135 case 0x40:
00136 tmp[d + 0 ] = data[0];
00137 tmp[d + 1 ] = data[0];
00138 tmp[d + 0 + stride] = data[0];
00139 tmp[d + 1 + stride] = data[0];
00140 data++;
00141 break;
00142 case 0xC0:
00143 tmp[d + 0 ] = *data++;
00144 tmp[d + 1 ] = *data++;
00145 tmp[d + 0 + stride] = *data++;
00146 tmp[d + 1 + stride] = *data++;
00147 break;
00148 }
00149 mask <<= 2;
00150 }
00151 break;
00152 case 32:
00153 mask = AV_RB16(msk);
00154 msk += 2;
00155 for(y = 0; y < 4; y++){
00156 for(x = 0; x < 4; x++){
00157 tmp[x] = data[mask & 1];
00158 mask >>= 1;
00159 }
00160 tmp += stride;
00161 tmp2 += stride;
00162 }
00163 data += 2;
00164 break;
00165 case 33:
00166 case 34:
00167 mask = AV_RB32(msk);
00168 msk += 4;
00169 for(y = 0; y < 4; y++){
00170 for(x = 0; x < 4; x++){
00171 tmp[x] = data[mask & 3];
00172 mask >>= 2;
00173 }
00174 tmp += stride;
00175 tmp2 += stride;
00176 }
00177 data += type - 30;
00178 break;
00179 default:
00180 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
00181 return -1;
00182 }
00183 }
00184 dst += stride * 4;
00185 ref += stride * 4;
00186 }
00187 return 0;
00188 }
00189
00190 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00191 {
00192 const uint8_t *buf = avpkt->data;
00193 int buf_size = avpkt->size;
00194 DxaDecContext * const c = avctx->priv_data;
00195 uint8_t *outptr, *srcptr, *tmpptr;
00196 unsigned long dsize;
00197 int i, j, compr;
00198 int stride;
00199 int orig_buf_size = buf_size;
00200 int pc = 0;
00201
00202
00203 if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
00204 int r, g, b;
00205
00206 buf += 4;
00207 for(i = 0; i < 256; i++){
00208 r = *buf++;
00209 g = *buf++;
00210 b = *buf++;
00211 c->pal[i] = 0xFF << 24 | r << 16 | g << 8 | b;
00212 }
00213 pc = 1;
00214 buf_size -= 768+4;
00215 }
00216
00217 if(avctx->get_buffer(avctx, &c->pic) < 0){
00218 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00219 return -1;
00220 }
00221 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
00222 c->pic.palette_has_changed = pc;
00223
00224 outptr = c->pic.data[0];
00225 srcptr = c->decomp_buf;
00226 tmpptr = c->prev.data[0];
00227 stride = c->pic.linesize[0];
00228
00229 if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
00230 compr = -1;
00231 else
00232 compr = buf[4];
00233
00234 dsize = c->dsize;
00235 if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
00236 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
00237 return -1;
00238 }
00239 switch(compr){
00240 case -1:
00241 c->pic.key_frame = 0;
00242 c->pic.pict_type = AV_PICTURE_TYPE_P;
00243 if(c->prev.data[0])
00244 memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
00245 else{
00246 memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
00247 c->pic.key_frame = 1;
00248 c->pic.pict_type = AV_PICTURE_TYPE_I;
00249 }
00250 break;
00251 case 2:
00252 case 3:
00253 case 4:
00254 case 5:
00255 c->pic.key_frame = !(compr & 1);
00256 c->pic.pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00257 for(j = 0; j < avctx->height; j++){
00258 if(compr & 1){
00259 for(i = 0; i < avctx->width; i++)
00260 outptr[i] = srcptr[i] ^ tmpptr[i];
00261 tmpptr += stride;
00262 }else
00263 memcpy(outptr, srcptr, avctx->width);
00264 outptr += stride;
00265 srcptr += avctx->width;
00266 }
00267 break;
00268 case 12:
00269 case 13:
00270 c->pic.key_frame = 0;
00271 c->pic.pict_type = AV_PICTURE_TYPE_P;
00272 decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
00273 break;
00274 default:
00275 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
00276 return -1;
00277 }
00278
00279 FFSWAP(AVFrame, c->pic, c->prev);
00280 if(c->pic.data[0])
00281 avctx->release_buffer(avctx, &c->pic);
00282
00283 *data_size = sizeof(AVFrame);
00284 *(AVFrame*)data = c->prev;
00285
00286
00287 return orig_buf_size;
00288 }
00289
00290 static av_cold int decode_init(AVCodecContext *avctx)
00291 {
00292 DxaDecContext * const c = avctx->priv_data;
00293
00294 avctx->pix_fmt = PIX_FMT_PAL8;
00295
00296 avcodec_get_frame_defaults(&c->pic);
00297 avcodec_get_frame_defaults(&c->prev);
00298
00299 c->dsize = avctx->width * avctx->height * 2;
00300 if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
00301 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00302 return -1;
00303 }
00304
00305 return 0;
00306 }
00307
00308 static av_cold int decode_end(AVCodecContext *avctx)
00309 {
00310 DxaDecContext * const c = avctx->priv_data;
00311
00312 av_freep(&c->decomp_buf);
00313 if(c->prev.data[0])
00314 avctx->release_buffer(avctx, &c->prev);
00315 if(c->pic.data[0])
00316 avctx->release_buffer(avctx, &c->pic);
00317
00318 return 0;
00319 }
00320
00321 AVCodec ff_dxa_decoder = {
00322 .name = "dxa",
00323 .type = AVMEDIA_TYPE_VIDEO,
00324 .id = CODEC_ID_DXA,
00325 .priv_data_size = sizeof(DxaDecContext),
00326 .init = decode_init,
00327 .close = decode_end,
00328 .decode = decode_frame,
00329 .capabilities = CODEC_CAP_DR1,
00330 .long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
00331 };