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