This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathZmbvCodec.cpp
More file actions
408 lines (369 loc) · 9.81 KB
/
ZmbvCodec.cpp
File metadata and controls
408 lines (369 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// This file is a part of Mesen
// It is a heavily modified version of the zmbv.h/cpp file found in DOSBox's code.
/*
* Copyright (C) 2002-2011 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "miniz.h"
#include "ZmbvCodec.h"
#define DBZV_VERSION_HIGH 0
#define DBZV_VERSION_LOW 1
#define COMPRESSION_NONE 0
#define COMPRESSION_ZLIB 1
#define MAX_VECTOR 16
#define Mask_KeyFrame 0x01
#define Mask_DeltaPalette 0x02
int ZmbvCodec::NeededSize( int _width, int _height, zmbv_format_t _format) {
int f;
switch (_format) {
case ZMBV_FORMAT_8BPP:f = 1;break;
case ZMBV_FORMAT_15BPP:f = 2;break;
case ZMBV_FORMAT_16BPP:f = 2;break;
case ZMBV_FORMAT_32BPP:f = 4;break;
default:
return -1;
}
f = f*_width*_height + 2*(1+(_width/8)) * (1+(_height/8))+1024;
return f + f/1000;
}
bool ZmbvCodec::SetupBuffers(zmbv_format_t _format, int blockwidth, int blockheight) {
FreeBuffers();
palsize = 0;
switch (_format) {
case ZMBV_FORMAT_8BPP:
pixelsize = 1;
palsize = 256;
break;
case ZMBV_FORMAT_15BPP:
pixelsize = 2;
break;
case ZMBV_FORMAT_16BPP:
pixelsize = 2;
break;
case ZMBV_FORMAT_32BPP:
pixelsize = 4;
break;
default:
return false;
};
bufsize = (height+2*MAX_VECTOR)*pitch*pixelsize+2048;
buf1 = new unsigned char[bufsize];
buf2 = new unsigned char[bufsize];
work = new unsigned char[bufsize];
int xblocks = (width/blockwidth);
int xleft = width % blockwidth;
if (xleft) xblocks++;
int yblocks = (height/blockheight);
int yleft = height % blockheight;
if (yleft) yblocks++;
blockcount=yblocks*xblocks;
blocks=new FrameBlock[blockcount];
if (!buf1 || !buf2 || !work || !blocks) {
FreeBuffers();
return false;
}
int y,x,i;
i=0;
for (y=0;y<yblocks;y++) {
for (x=0;x<xblocks;x++) {
blocks[i].start=((y*blockheight)+MAX_VECTOR)*pitch+
(x*blockwidth)+MAX_VECTOR;
if (xleft && x==(xblocks-1)) {
blocks[i].dx=xleft;
} else {
blocks[i].dx=blockwidth;
}
if (yleft && y==(yblocks-1)) {
blocks[i].dy=yleft;
} else {
blocks[i].dy=blockheight;
}
i++;
}
}
memset(buf1,0,bufsize);
memset(buf2,0,bufsize);
memset(work,0,bufsize);
oldframe=buf1;
newframe=buf2;
format = _format;
_bufSize = NeededSize(width, height, format);
_buf = new uint8_t[_bufSize];
return true;
}
void ZmbvCodec::CreateVectorTable(void) {
int x,y,s;
VectorCount=1;
VectorTable[0].x=VectorTable[0].y=0;
for (s=1;s<=10;s++) {
for (y=0-s;y<=0+s;y++) for (x=0-s;x<=0+s;x++) {
if (abs(x)==s || abs(y)==s) {
VectorTable[VectorCount].x=x;
VectorTable[VectorCount].y=y;
VectorCount++;
}
}
}
}
template<class P>
INLINE int ZmbvCodec::PossibleBlock(int vx,int vy,FrameBlock * block) {
int ret=0;
P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
P * pnew=((P*)newframe)+block->start;;
for (int y=0;y<block->dy;y+=4) {
for (int x=0;x<block->dx;x+=4) {
int test=0-((pold[x]-pnew[x])&0x00ffffff);
ret-=(test>>31);
}
pold+=pitch*4;
pnew+=pitch*4;
}
return ret;
}
template<class P>
INLINE int ZmbvCodec::CompareBlock(int vx,int vy,FrameBlock * block) {
int ret=0;
P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
P * pnew=((P*)newframe)+block->start;;
for (int y=0;y<block->dy;y++) {
for (int x=0;x<block->dx;x++) {
int test=0-((pold[x]-pnew[x])&0x00ffffff);
ret-=(test>>31);
}
pold+=pitch;
pnew+=pitch;
}
return ret;
}
template<class P>
INLINE void ZmbvCodec::AddXorBlock(int vx,int vy,FrameBlock * block) {
P * pold=((P*)oldframe)+block->start+(vy*pitch)+vx;
P * pnew=((P*)newframe)+block->start;
for (int y=0;y<block->dy;y++) {
for (int x=0;x<block->dx;x++) {
*((P*)&work[workUsed])=pnew[x] ^ pold[x];
workUsed+=sizeof(P);
}
pold+=pitch;
pnew+=pitch;
}
}
template<class P>
void ZmbvCodec::AddXorFrame(void) {
signed char * vectors=(signed char*)&work[workUsed];
/* Align the following xor data on 4 byte boundary*/
workUsed=(workUsed + blockcount*2 +3) & ~3;
for (int b=0;b<blockcount;b++) {
FrameBlock * block=&blocks[b];
int bestvx = 0;
int bestvy = 0;
int bestchange=CompareBlock<P>(0,0, block);
int possibles=64;
for (int v=0;v<VectorCount && possibles;v++) {
if (bestchange<4) break;
int vx = VectorTable[v].x;
int vy = VectorTable[v].y;
if (PossibleBlock<P>(vx, vy, block) < 4) {
possibles--;
int testchange=CompareBlock<P>(vx,vy, block);
if (testchange<bestchange) {
bestchange=testchange;
bestvx = vx;
bestvy = vy;
}
}
}
vectors[b*2+0]=(bestvx << 1);
vectors[b*2+1]=(bestvy << 1);
if (bestchange) {
vectors[b*2+0]|=1;
AddXorBlock<P>(bestvx, bestvy, block);
}
}
}
bool ZmbvCodec::SetupCompress( int _width, int _height, uint32_t compressionLevel ) {
width = _width;
height = _height;
pitch = _width + 2*MAX_VECTOR;
format = ZMBV_FORMAT_NONE;
if (deflateInit (&zstream, compressionLevel) != Z_OK)
return false;
return true;
}
bool ZmbvCodec::PrepareCompressFrame(int flags, zmbv_format_t _format, char * pal)
{
int i;
unsigned char *firstByte;
if (_format != format) {
if (!SetupBuffers( _format, 16, 16))
return false;
flags|=1; //Force a keyframe
}
/* replace oldframe with new frame */
unsigned char *copyFrame = newframe;
newframe = oldframe;
oldframe = copyFrame;
compressInfo.linesDone = 0;
compressInfo.writeSize = _bufSize;
compressInfo.writeDone = 1;
compressInfo.writeBuf = (unsigned char *)_buf;
/* Set a pointer to the first byte which will contain info about this frame */
firstByte = compressInfo.writeBuf;
*firstByte = 0;
//Reset the work buffer
workUsed = 0;workPos = 0;
if (flags & 1) {
/* Make a keyframe */
*firstByte |= Mask_KeyFrame;
KeyframeHeader * header = (KeyframeHeader *)(compressInfo.writeBuf + compressInfo.writeDone);
header->high_version = DBZV_VERSION_HIGH;
header->low_version = DBZV_VERSION_LOW;
header->compression = COMPRESSION_ZLIB;
header->format = format;
header->blockwidth = 16;
header->blockheight = 16;
compressInfo.writeDone += sizeof(KeyframeHeader);
/* Copy the new frame directly over */
if (palsize) {
if (pal)
memcpy(&palette, pal, sizeof(palette));
else
memset(&palette,0, sizeof(palette));
/* keyframes get the full palette */
for (i=0;i<palsize;i++) {
work[workUsed++] = palette[i*4+0];
work[workUsed++] = palette[i*4+1];
work[workUsed++] = palette[i*4+2];
}
}
/* Restart deflate */
deflateReset(&zstream);
} else {
if (palsize && pal && memcmp(pal, palette, palsize * 4)) {
*firstByte |= Mask_DeltaPalette;
for(i=0;i<palsize;i++) {
work[workUsed++]=palette[i*4+0] ^ pal[i*4+0];
work[workUsed++]=palette[i*4+1] ^ pal[i*4+1];
work[workUsed++]=palette[i*4+2] ^ pal[i*4+2];
}
memcpy(&palette,pal, palsize * 4);
}
}
return true;
}
void ZmbvCodec::CompressLines(int lineCount, void *lineData[])
{
int linePitch = pitch * pixelsize;
int lineWidth = width * pixelsize;
int i = 0;
unsigned char *destStart = newframe + pixelsize*(MAX_VECTOR+(compressInfo.linesDone+MAX_VECTOR)*pitch);
while ( i < lineCount && (compressInfo.linesDone < height)) {
memcpy(destStart, lineData[i], lineWidth );
destStart += linePitch;
i++; compressInfo.linesDone++;
}
}
int ZmbvCodec::FinishCompressFrame(uint8_t** compressedData)
{
unsigned char firstByte = *compressInfo.writeBuf;
if (firstByte & Mask_KeyFrame) {
int i;
/* Add the full frame data */
unsigned char * readFrame = newframe + pixelsize*(MAX_VECTOR+MAX_VECTOR*pitch);
for (i=0;i<height;i++) {
memcpy(&work[workUsed], readFrame, width*pixelsize);
readFrame += pitch*pixelsize;
workUsed += width*pixelsize;
}
} else {
/* Add the delta frame data */
switch (format) {
case ZMBV_FORMAT_8BPP:
AddXorFrame<int8_t>();
break;
case ZMBV_FORMAT_15BPP:
case ZMBV_FORMAT_16BPP:
AddXorFrame<int16_t>();
break;
default:
case ZMBV_FORMAT_32BPP:
AddXorFrame<int32_t>();
break;
}
}
/* Create the actual frame with compression */
zstream.next_in = (Bytef *)work;
zstream.avail_in = workUsed;
zstream.total_in = 0;
zstream.next_out = (Bytef *)(compressInfo.writeBuf + compressInfo.writeDone);
zstream.avail_out = compressInfo.writeSize - compressInfo.writeDone;
zstream.total_out = 0;
deflate(&zstream, Z_SYNC_FLUSH);
*compressedData = _buf;
return compressInfo.writeDone + zstream.total_out;
}
void ZmbvCodec::FreeBuffers()
{
if (blocks) {
delete[] blocks;
blocks= nullptr;
}
if (buf1) {
delete[] buf1;
buf1= nullptr;
}
if (buf2) {
delete[] buf2;
buf2= nullptr;
}
if (work) {
delete[] work;
work= nullptr;
}
if(_buf) {
delete[] _buf;
_buf = nullptr;
}
}
ZmbvCodec::ZmbvCodec()
{
CreateVectorTable();
blocks = nullptr;
buf1 = nullptr;
buf2 = nullptr;
work = nullptr;
memset( &zstream, 0, sizeof(zstream));
}
int ZmbvCodec::CompressFrame(bool isKeyFrame, uint8_t *frameData, uint8_t** compressedData)
{
if(!PrepareCompressFrame(isKeyFrame ? 1 : 0, ZMBV_FORMAT_32BPP, nullptr)) {
return -1;
}
for(int i = 0; i < height; i++) {
void * rowPointer = frameData + i*width*4;
CompressLines(1, &rowPointer);
}
return FinishCompressFrame(compressedData);
}
const char* ZmbvCodec::GetFourCC()
{
return "ZMBV";
}