annotate dxa.c @ 4726:ecb663d00b6b libavcodec

factorize
author michael
date Sun, 25 Mar 2007 14:02:54 +0000
parents 97e3364d267a
children b3ee9a1526b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4671
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
1 /*
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
2 * Feeble Files/ScummVM DXA decoder
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
3 * Copyright (c) 2007 Konstantin Shishkov
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
4 *
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
6 *
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
11 *
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
16 *
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
20 *
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
21 */
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
22
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
23 /**
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
24 * @file dxa.c
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
25 * DXA Video decoder
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
26 */
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
27
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
28 #include <stdio.h>
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
29 #include <stdlib.h>
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
30
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
31 #include "common.h"
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
32 #include "avcodec.h"
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
33
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
34 #include <zlib.h>
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
35
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
36 /*
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
37 * Decoder context
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
38 */
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
39 typedef struct DxaDecContext {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
40 AVCodecContext *avctx;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
41 AVFrame pic, prev;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
42
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
43 int dsize;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
44 uint8_t *decomp_buf;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
45 uint32_t pal[256];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
46 } DxaDecContext;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
47
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
48 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
49 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
50
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
51 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
52 {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
53 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
54 int i, j, k;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
55 int type, x, y, d, d2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
56 int stride = c->pic.linesize[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
57 uint32_t mask;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
58
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
59 code = src + 12;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
60 data = code + ((avctx->width * avctx->height) >> 4);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
61 mv = data + AV_RB32(src + 0);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
62 msk = mv + AV_RB32(src + 4);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
63
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
64 for(j = 0; j < avctx->height; j += 4){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
65 for(i = 0; i < avctx->width; i += 4){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
66 tmp = dst + i;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
67 tmp2 = ref + i;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
68 type = *code++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
69 switch(type){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
70 case 4: // motion compensation
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
71 x = (*mv) >> 4; if(x & 8) x = 8 - x;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
72 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
73 tmp2 += x + y*stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
74 case 0: // skip
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
75 case 5: // skip in method 12
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
76 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
77 memcpy(tmp, tmp2, 4);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
78 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
79 tmp2 += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
80 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
81 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
82 case 1: // masked change
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
83 case 10: // masked change with only half of pixels changed
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
84 case 11: // cases 10-15 are for method 12 only
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
85 case 12:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
86 case 13:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
87 case 14:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
88 case 15:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
89 if(type == 1){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
90 mask = AV_RB16(msk);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
91 msk += 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
92 }else{
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
93 type -= 10;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
94 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
95 msk++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
96 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
97 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
98 for(x = 0; x < 4; x++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
99 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
100 mask <<= 1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
101 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
102 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
103 tmp2 += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
104 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
105 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
106 case 2: // fill block
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
107 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
108 memset(tmp, data[0], 4);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
109 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
110 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
111 data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
112 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
113 case 3: // raw block
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
114 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
115 memcpy(tmp, data, 4);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
116 data += 4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
117 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
118 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
119 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
120 case 8: // subblocks - method 13 only
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
121 mask = *msk++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
122 for(k = 0; k < 4; k++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
123 d = ((k & 1) << 1) + ((k & 2) * stride);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
124 d2 = ((k & 1) << 1) + ((k & 2) * stride);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
125 tmp2 = ref + i + d2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
126 switch(mask & 0xC0){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
127 case 0x80: // motion compensation
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
128 x = (*mv) >> 4; if(x & 8) x = 8 - x;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
129 y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
130 tmp2 += x + y*stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
131 case 0x00: // skip
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
132 tmp[d + 0 ] = tmp2[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
133 tmp[d + 1 ] = tmp2[1];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
134 tmp[d + 0 + stride] = tmp2[0 + stride];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
135 tmp[d + 1 + stride] = tmp2[1 + stride];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
136 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
137 case 0x40: // fill
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
138 tmp[d + 0 ] = data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
139 tmp[d + 1 ] = data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
140 tmp[d + 0 + stride] = data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
141 tmp[d + 1 + stride] = data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
142 data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
143 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
144 case 0xC0: // raw
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
145 tmp[d + 0 ] = *data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
146 tmp[d + 1 ] = *data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
147 tmp[d + 0 + stride] = *data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
148 tmp[d + 1 + stride] = *data++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
149 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
150 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
151 mask <<= 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
152 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
153 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
154 case 32: // vector quantization - 2 colors
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
155 mask = AV_RB16(msk);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
156 msk += 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
157 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
158 for(x = 0; x < 4; x++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
159 tmp[x] = data[mask & 1];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
160 mask >>= 1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
161 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
162 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
163 tmp2 += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
164 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
165 data += 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
166 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
167 case 33: // vector quantization - 3 or 4 colors
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
168 case 34:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
169 mask = AV_RB32(msk);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
170 msk += 4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
171 for(y = 0; y < 4; y++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
172 for(x = 0; x < 4; x++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
173 tmp[x] = data[mask & 3];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
174 mask >>= 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
175 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
176 tmp += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
177 tmp2 += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
178 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
179 data += type - 30;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
180 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
181 default:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
182 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
183 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
184 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
185 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
186 dst += stride * 4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
187 ref += stride * 4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
188 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
189 return 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
190 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
191
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
192 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
193 {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
194 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
195 uint8_t *outptr, *srcptr, *tmpptr;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
196 unsigned long dsize;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
197 int i, j, compr;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
198 int stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
199 int orig_buf_size = buf_size;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
200 int pc = 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
201
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
202 /* make the palette available on the way out */
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
203 if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
204 int r, g, b;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
205
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
206 buf += 4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
207 for(i = 0; i < 256; i++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
208 r = *buf++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
209 g = *buf++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
210 b = *buf++;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
211 c->pal[i] = (r << 16) | (g << 8) | b;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
212 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
213 pc = 1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
214 buf_size -= 768+4;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
215 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
216
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
217 if(avctx->get_buffer(avctx, &c->pic) < 0){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
218 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
219 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
220 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
221 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
222 c->pic.palette_has_changed = pc;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
223
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
224 outptr = c->pic.data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
225 srcptr = c->decomp_buf;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
226 tmpptr = c->prev.data[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
227 stride = c->pic.linesize[0];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
228
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
229 if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
230 compr = -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
231 else
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
232 compr = buf[4];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
233
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
234 dsize = c->dsize;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
235 if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
236 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
237 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
238 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
239 switch(compr){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
240 case -1:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
241 c->pic.key_frame = 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
242 c->pic.pict_type = FF_P_TYPE;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
243 if(c->prev.data[0])
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
244 memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
245 else{ // Should happen only when first frame is 'NULL'
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
246 memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
247 c->pic.key_frame = 1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
248 c->pic.pict_type = FF_I_TYPE;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
249 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
250 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
251 case 2:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
252 case 3:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
253 case 4:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
254 case 5:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
255 c->pic.key_frame = !(compr & 1);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
256 c->pic.pict_type = (compr & 1) ? FF_P_TYPE : FF_I_TYPE;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
257 for(j = 0; j < avctx->height; j++){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
258 if(compr & 1){
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
259 for(i = 0; i < avctx->width; i++)
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
260 outptr[i] = srcptr[i] ^ tmpptr[i];
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
261 tmpptr += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
262 }else
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
263 memcpy(outptr, srcptr, avctx->width);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
264 outptr += stride;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
265 srcptr += avctx->width;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
266 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
267 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
268 case 12: // ScummVM coding
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
269 case 13:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
270 c->pic.key_frame = 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
271 c->pic.pict_type = FF_P_TYPE;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
272 decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
273 break;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
274 default:
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
275 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
276 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
277 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
278
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
279 FFSWAP(AVFrame, c->pic, c->prev);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
280 if(c->pic.data[0])
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
281 avctx->release_buffer(avctx, &c->pic);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
282
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
283 *data_size = sizeof(AVFrame);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
284 *(AVFrame*)data = c->prev;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
285
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
286 /* always report that the buffer was completely consumed */
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
287 return orig_buf_size;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
288 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
289
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
290 static int decode_init(AVCodecContext *avctx)
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
291 {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
292 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
293
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
294 c->avctx = avctx;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
295 avctx->pix_fmt = PIX_FMT_PAL8;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
296
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
297 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
298 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
299 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
300
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
301 c->dsize = avctx->width * avctx->height * 2;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
302 if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
303 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
304 return -1;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
305 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
306
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
307 return 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
308 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
309
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
310 static int decode_end(AVCodecContext *avctx)
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
311 {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
312 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
313
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
314 av_freep(&c->decomp_buf);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
315 if(c->prev.data[0])
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
316 avctx->release_buffer(avctx, &c->prev);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
317 if(c->pic.data[0])
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
318 avctx->release_buffer(avctx, &c->pic);
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
319
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
320 return 0;
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
321 }
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
322
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
323 AVCodec dxa_decoder = {
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
324 "dxa",
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
325 CODEC_TYPE_VIDEO,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
326 CODEC_ID_DXA,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
327 sizeof(DxaDecContext),
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
328 decode_init,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
329 NULL,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
330 decode_end,
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
331 decode_frame
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
332 };
97e3364d267a DXA demuxer and decoder
kostya
parents:
diff changeset
333