This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompression.cpp
281 lines (236 loc) · 7.08 KB
/
decompression.cpp
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
#include "compact.h"
#include "yeti.h"
#include "codec_inst.h"
#include "prediction.h"
#include "threading.h"
#include "convert_yv12.h"
#include "huffyuv_a.h"
// initialize the codec for decompression
DWORD CodecInst::DecompressBegin(LPBITMAPINFOHEADER lpbiIn, LPBITMAPINFOHEADER lpbiOut)
{
if(m_started == 0x1337)
{
DecompressEnd();
}
m_started = 0;
if(int error = DecompressQuery(lpbiIn, lpbiOut) != ICERR_OK)
{
return error;
}
m_width = lpbiIn->biWidth;
m_height = lpbiIn->biHeight;
m_format = lpbiOut->biBitCount;
m_length = EIGHTH(m_width * m_height * m_format);
const int buffer_size = (m_format >= RGB24) ? ALIGN_ROUND(QUADRUPLE(m_width), 8) * m_height + 2048 : m_length + 2048;
m_buffer = (BYTE*)ALIGNED_MALLOC(m_buffer, buffer_size, 16, "buffer");
m_buffer2 = (BYTE*)ALIGNED_MALLOC(m_buffer2, buffer_size, 16, "buffer2");
m_prevFrame = (BYTE*)ALIGNED_MALLOC(m_prevFrame, buffer_size, 16, "prev");
if(!m_buffer || !m_buffer2 || !m_prevFrame)
{
return (DWORD)ICERR_MEMORY;
}
int code = InitThreads(false);
if (code != ICERR_OK)
{
return code;
}
m_started = 0x1337;
return ICERR_OK;
}
DWORD CodecInst::DecompressEnd()
{
if(m_started)
{
EndThreads();
ALIGNED_FREE(m_buffer,"buffer");
ALIGNED_FREE(m_buffer2,"buffer2");
ALIGNED_FREE(m_prevFrame, "prev");
m_compressWorker.FreeCompressBuffers();
}
m_started = 0;
return ICERR_OK;
}
void CodecInst::Decode3Channels(BYTE* dst1, unsigned int len1, BYTE* dst2, unsigned int len2, BYTE* dst3, unsigned int len3)
{
const BYTE* src1 = m_in + 9;
const BYTE* src2 = m_in + *(UINT32*)(m_in+1);
const BYTE* src3 = m_in + *(UINT32*)(m_in+5);
int size1 = *(UINT32*)(m_in+1);
int size2 = *(UINT32*)(m_in+5);
int size3 = m_compressed_size - size2;
size2 -= size1;
size1 -= 9;
// Compressed size should approximate decoding time.
// The priority of the largest channel is boosted to improve
// load balancing when there are fewer cores than channels
if (size1 >= size2 && size1 >= size3 && m_threads[0].m_priority != THREAD_PRIORITY_BELOW_NORMAL)
{
SetThreadPriority(m_threads[0].m_thread, THREAD_PRIORITY_BELOW_NORMAL);
SetThreadPriority(m_threads[1].m_thread, THREAD_PRIORITY_BELOW_NORMAL);
m_threads[0].m_priority = THREAD_PRIORITY_BELOW_NORMAL;
m_threads[1].m_priority = THREAD_PRIORITY_BELOW_NORMAL;
}
else if (size2 >= size3 && m_threads[0].m_priority != THREAD_PRIORITY_ABOVE_NORMAL)
{
SetThreadPriority(m_threads[0].m_thread, THREAD_PRIORITY_ABOVE_NORMAL);
SetThreadPriority(m_threads[1].m_thread, THREAD_PRIORITY_NORMAL);
m_threads[0].m_priority = THREAD_PRIORITY_ABOVE_NORMAL;
m_threads[1].m_priority = THREAD_PRIORITY_NORMAL;
}
else if(m_threads[1].m_priority != THREAD_PRIORITY_ABOVE_NORMAL)
{
SetThreadPriority(m_threads[0].m_thread, THREAD_PRIORITY_NORMAL);
SetThreadPriority(m_threads[1].m_thread, THREAD_PRIORITY_ABOVE_NORMAL);
m_threads[0].m_priority = THREAD_PRIORITY_NORMAL;
m_threads[1].m_priority = THREAD_PRIORITY_ABOVE_NORMAL;
}
m_threads[0].m_source = src2;
m_threads[0].m_dest = dst2;
m_threads[0].m_length = len2;
SetEvent(m_threads[0].m_startEvent);
m_threads[1].m_source = src3;
m_threads[1].m_dest = dst3;
m_threads[1].m_length = len3;
SetEvent(m_threads[1].m_startEvent);
m_compressWorker.Uncompact(src1, dst1, len1);
WAIT_FOR_THREADS(2);
}
void CodecInst::YUY2Decompress(DWORD flags)
{
BYTE* dst = m_out;
BYTE* dst2 = m_buffer;
if(m_format == YUY2)
{
dst = m_buffer;
dst2 = m_out;
}
const size_t pixels = m_width * m_height;
const size_t half = HALF(pixels);
BYTE* ydst = dst;
BYTE* udst = ydst + pixels;
BYTE* vdst = udst + half;
Decode3Channels(ydst, pixels, udst, half, vdst, half);
// special case: RLE detected a solid Y value (compressed size = 2),
// need to set 2nd Y value for restoration to work right
if(*(UINT32*)(m_in+1) == 11) //TODO: Needed?
{
dst[1] = dst[0];
}
Interleave_And_Restore_YUY2(dst2, ydst, udst, vdst, m_width, m_height);
if((flags & ICDECOMPRESS_NOTKEYFRAME) == ICDECOMPRESS_NOTKEYFRAME)
{
//MessageBox(HWND_DESKTOP, "DeltaFrame", "Info", MB_OK);
InterframeDecode(dst2, dst2, m_prevFrame, DOUBLE(pixels));
}
memcpy(m_prevFrame, dst2, DOUBLE(pixels));
if(m_format == YUY2 || (flags & ICDECOMPRESS_PREROLL) == ICDECOMPRESS_PREROLL)
{
return;
}
if(m_format == RGB24)
{
mmx_YUY2toRGB24(dst2, m_out, m_buffer + DOUBLE(pixels), DOUBLE(m_width));
}
else
{
mmx_YUY2toRGB32(dst2, m_out, m_buffer + DOUBLE(pixels), DOUBLE(m_width));
}
}
void CodecInst::YV12Decompress(DWORD flags)
{
BYTE* dst = m_out;
BYTE* dst2 = m_buffer;
if (m_format == YUY2)
{
dst = m_buffer;
dst2 = m_out;
}
const size_t pixels = m_width * m_height;
const size_t fourth = FOURTH(pixels);
BYTE* ydst = dst;
BYTE* udst = ydst + pixels;
BYTE* vdst = udst + fourth;
Decode3Channels(ydst, pixels, udst, fourth, vdst, fourth);
Restore_YV12(ydst, udst, vdst, m_width, m_height);
const size_t length = pixels + HALF(pixels);
if((flags & ICDECOMPRESS_NOTKEYFRAME) == ICDECOMPRESS_NOTKEYFRAME)
{
InterframeDecode(dst, dst, m_prevFrame, length);
}
memcpy(m_prevFrame, dst, length);
if(m_format == YV12 || (flags & ICDECOMPRESS_PREROLL) == ICDECOMPRESS_PREROLL)
{
return;
}
//upsample if needed
isse_yv12_to_yuy2(dst, dst + pixels + fourth, dst + pixels, m_width, m_width, HALF(m_width), dst2, DOUBLE(m_width), m_height);
if(m_format == YUY2)
{
return;
}
// upsample to RGB
if(m_format == RGB32)
{
mmx_YUY2toRGB32(dst2, m_out, dst2 + DOUBLE(pixels), DOUBLE(m_width));
}
else
{
mmx_YUY2toRGB24(dst2, m_out, dst2 + DOUBLE(pixels), DOUBLE(m_width));
}
}
DWORD CodecInst::Decompress(ICDECOMPRESS* idcinfo)
{
#ifdef _DEBUG
try
{
#endif
DWORD return_code = ICERR_OK;
if(m_started != 0x1337)
{
DecompressBegin(idcinfo->lpbiInput, idcinfo->lpbiOutput);
}
m_out = (BYTE*)idcinfo->lpOutput;
m_in = (BYTE*)idcinfo->lpInput;
idcinfo->lpbiOutput->biSizeImage = m_length;
m_compressed_size = idcinfo->lpbiInput->biSizeImage;
// according to the avi specs, the calling application is responsible for handling null frames.
if(m_compressed_size == 0)
{
#ifdef _DEBUG
MessageBox (HWND_DESKTOP, "Received request to decode a null frame", "Error", MB_OK | MB_ICONEXCLAMATION);
#endif
return ICERR_OK;
}
switch(m_in[0] & ~KEYFRAME)
{
case YUY2_FRAME:
{
YUY2Decompress(idcinfo->dwFlags);
break;
}
case YV12_FRAME:
{
YV12Decompress(idcinfo->dwFlags);
break;
}
default:
{
#ifdef _DEBUG
char emsg[128];
sprintf_s(emsg, 128, "Unrecognized frame type: %d", m_in[0]);
MessageBox (HWND_DESKTOP, emsg, "Error", MB_OK | MB_ICONEXCLAMATION);
#endif
return_code = (DWORD)ICERR_ERROR;
break;
}
}
return return_code;
#ifdef _DEBUG
}
catch( ... )
{
MessageBox (HWND_DESKTOP, "Exception caught in decompress main", "Error", MB_OK | MB_ICONEXCLAMATION);
return (DWORD)ICERR_INTERNAL;
}
#endif
}