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
/
Copy pathcompression.cpp
398 lines (336 loc) · 11.4 KB
/
compression.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
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
#include "yeti.h"
#include "codec_inst.h"
#include "prediction.h"
#include "threading.h"
#include "convert.h"
#include "huffyuv_a.h"
#include "convert_yv12.h"
DWORD CodecInst::CompressBegin(LPBITMAPINFOHEADER lpbiIn, LPBITMAPINFOHEADER lpbiOut)
{
if(m_started == 0x1337)
{
CompressEnd();
}
m_started = 0;
m_nullframes = GetPrivateProfileInt(SettingsHeading, "nullframes", FALSE, SettingsFile) > 0;
m_deltaframes = GetPrivateProfileInt(SettingsHeading, "deltaframes", FALSE, SettingsFile) > 0;
if (int error = CompressQuery(lpbiIn, lpbiOut) != ICERR_OK)
{
return error;
}
m_width = lpbiIn->biWidth;
m_height = lpbiIn->biHeight;
m_format = lpbiIn->biBitCount;
m_length = EIGHTH(m_width * m_height * m_format);
size_t buffer_size;
if(m_format < RGB24)
{
buffer_size = EIGHTH(ALIGN_ROUND(m_width, 32) * m_height * m_format) + 1024;
}
else
{
buffer_size = QUADRUPLE(ALIGN_ROUND(m_width, 16) * m_height) + 1024;
}
m_buffer = (BYTE*)ALIGNED_MALLOC(m_buffer, buffer_size, 16, "buffer");
m_buffer2 = (BYTE*)ALIGNED_MALLOC(m_buffer2, buffer_size, 16, "buffer2");
m_deltaBuffer = (BYTE*)ALIGNED_MALLOC(m_deltaBuffer, buffer_size, 16, "delta");
m_colorTransBuffer = (BYTE*)ALIGNED_MALLOC(m_colorTransBuffer, buffer_size, 16, "colorT");
m_prevFrame = (BYTE*)ALIGNED_MALLOC(m_prevFrame, buffer_size, 16, "prev");
ZeroMemory(m_prevFrame, buffer_size);
if(!m_buffer || !m_buffer2 || !m_prevFrame || !m_deltaBuffer || !m_colorTransBuffer)
{
ALIGNED_FREE(m_buffer, "buffer");
ALIGNED_FREE(m_buffer2, "buffer2");
ALIGNED_FREE(m_deltaBuffer, "delta");
ALIGNED_FREE(m_colorTransBuffer, "colorT");
ALIGNED_FREE(m_prevFrame, "prev");
return (DWORD)ICERR_MEMORY;
}
if (!m_compressWorker.InitCompressBuffers(m_width * m_height))
{
return (DWORD)ICERR_MEMORY;
}
DWORD code = InitThreads(true);
if(code != ICERR_OK)
{
return code;
}
m_started = 0x1337;
return ICERR_OK;
}
// get the maximum size a compressed frame will take;
// 105% of image size + 1KB should be plenty even for random static
DWORD CodecInst::CompressGetSize(LPBITMAPINFOHEADER lpbiIn, LPBITMAPINFOHEADER lpbiOut)
{
return (DWORD)(ALIGN_ROUND(lpbiIn->biWidth, 16) * lpbiIn->biHeight * lpbiIn->biBitCount / 8 * 1.05 + 1024); // TODO: Correct?
}
// release resources when compression is done
DWORD CodecInst::CompressEnd()
{
if( m_started == 0x1337)
{
EndThreads();
ALIGNED_FREE(m_buffer, "buffer");
ALIGNED_FREE(m_buffer2, "buffer2");
ALIGNED_FREE(m_deltaBuffer, "delta");
ALIGNED_FREE(m_prevFrame, "prev");
ALIGNED_FREE(m_colorTransBuffer, "colorT");
m_compressWorker.FreeCompressBuffers();
}
m_started = 0;
return ICERR_OK;
}
// called to compress a frame; the actual compression will be
// handed off to other functions depending on the color space and settings
DWORD CodecInst::Compress(ICCOMPRESS* icinfo, DWORD /*dwSize*/)
{
m_out = (BYTE*)icinfo->lpOutput;
m_in = (BYTE*)icinfo->lpInput;
if(icinfo->lFrameNum == 0 && m_started != 0x1337)
{
DWORD error = CompressBegin(icinfo->lpbiInput, icinfo->lpbiOutput);
if(error != ICERR_OK)
{
return error;
}
}
const size_t pixels = m_width * m_height;
unsigned int upos = ALIGN_ROUND(pixels + 16, 16);
unsigned int vpos = ALIGN_ROUND(pixels + HALF(pixels) + 32, 16);
if(m_compressFormat == YV12)
{
if(m_format == RGB24)
{
ConvertRGB24toYV12_SSE2(m_in, m_colorTransBuffer, m_colorTransBuffer + upos, m_colorTransBuffer + vpos, m_width, m_height);
m_in = m_colorTransBuffer;
}
else if(m_format == RGB32)
{
ConvertRGB32toYV12_SSE2(m_in, m_colorTransBuffer, m_colorTransBuffer + upos, m_colorTransBuffer + vpos, m_width, m_height);
m_in = m_colorTransBuffer;
}
else if (m_format == YUY2)
{
isse_yuy2_to_yv12(m_in, m_width*2 , m_width*2, m_colorTransBuffer, m_colorTransBuffer + upos, m_colorTransBuffer + vpos, m_width, m_width/2, m_height);
m_in = m_colorTransBuffer;
}
}
else
{
if(m_format == RGB24)
{
ConvertRGB24toYV16_SSE2(m_in,m_colorTransBuffer,m_colorTransBuffer+upos,m_colorTransBuffer+vpos, m_width, m_height);
m_in = m_colorTransBuffer;
}
else if(m_format == RGB32)
{
ConvertRGB32toYV16_SSE2(m_in,m_colorTransBuffer,m_colorTransBuffer+upos,m_colorTransBuffer+vpos, m_width, m_height);
m_in = m_colorTransBuffer;
}
}
return m_compressFormat == YV12 ? CompressYV12(icinfo) : CompressYUV16(icinfo);
}
unsigned int CodecInst::HandleTwoCompressionThreads(unsigned int chan_size)
{
unsigned int channel_sizes[3];
channel_sizes[0] = chan_size;
unsigned int current_size = chan_size+9;
HANDLE events[2];
events[0] = m_threads[0].m_doneEvent;
events[1] = m_threads[1].m_doneEvent;
int pos = WaitForMultipleObjects(2, &events[0], false, INFINITE);
pos -= WAIT_OBJECT_0;
ThreadData * finished = &m_threads[pos];
ThreadData * running = &m_threads[!pos];
((DWORD*)(m_out+1))[pos] = current_size;
channel_sizes[pos+1] = finished->m_size;
memcpy(m_out+current_size, (unsigned char*)finished->m_dest, channel_sizes[pos+1]);
current_size += finished->m_size;
pos = !pos;
((DWORD*)(m_out+1))[pos] = current_size;
WaitForSingleObject(running->m_doneEvent,INFINITE);
finished = running;
channel_sizes[pos+1] = finished->m_size;
memcpy(m_out+current_size, (unsigned char*)finished->m_dest, channel_sizes[pos+1]);
current_size += finished->m_size;
if (channel_sizes[0] >= channel_sizes[1] && channel_sizes[0] >= channel_sizes[2] && 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 (channel_sizes[1] >= channel_sizes[2] && 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[0].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[0].m_priority=THREAD_PRIORITY_ABOVE_NORMAL;
}
return current_size;
}
DWORD CodecInst::CompressYUV16(ICCOMPRESS* icinfo)
{
const size_t pixels = m_width * m_height;
const size_t y_stride = ALIGN_ROUND(pixels + 16, 16);
const size_t c_stride = ALIGN_ROUND(HALF(pixels) + 32, 16);
const size_t len = y_stride + DOUBLE(c_stride);
if(m_nullframes)
{
size_t pos = HALF(len) + 15;
pos &= (~15);
if(memcmp(m_in + pos, m_prevFrame + pos, len - pos) == 0 && memcmp(m_in, m_prevFrame, pos) == 0)
{
icinfo->lpbiOutput->biSizeImage = 0;
*icinfo->lpdwFlags = 0;
return (DWORD)ICERR_OK;
}
}
BYTE frameType;
BYTE* source = m_in;
if(m_deltaframes
&& (icinfo->dwFlags & ICCOMPRESS_KEYFRAME) != ICCOMPRESS_KEYFRAME
&& icinfo->lFrameNum > 0
&& InterframeEncode(m_deltaBuffer, m_in, m_prevFrame, len, DOUBLE(len)))
{
source = m_deltaBuffer;
*icinfo->lpdwFlags |= AVIIF_LASTPART;
frameType = YUY2_DELTAFRAME;
}
else
{
*icinfo->lpdwFlags |= AVIIF_KEYFRAME;
icinfo->dwFlags |= ICCOMPRESS_KEYFRAME;
frameType = YUY2_KEYFRAME;
}
if(m_nullframes || m_deltaframes)
{
memcpy(m_prevFrame, m_in, len);
}
BYTE* ysrc = (source != m_deltaBuffer) ? m_deltaBuffer :m_colorTransBuffer;
BYTE* usrc = ysrc + y_stride;
BYTE* vsrc = usrc + c_stride;
BYTE* ydest = m_buffer2;
BYTE* udest = ydest + y_stride;
BYTE* vdest = udest + c_stride;
BYTE *y, *u, *v;
if (icinfo->lpbiInput->biCompression == FOURCC_YV16)
{
y = source;
v = y + pixels;
u = v + HALF(pixels);
ydest += ((int)y)&15;
udest += ((int)u)&15;
vdest += ((int)v)&15;
}
else if (icinfo->lpbiInput->biBitCount > YUY2)
{
y = source;
u = source + ALIGN_ROUND(pixels + 16, 16);
v = source + ALIGN_ROUND(pixels + HALF(pixels) + 32, 16);
}
else
{
if (icinfo->lpbiInput->biCompression == FOURCC_YUY2)
{
Split_YUY2(source, ysrc, usrc, vsrc, m_width, m_height);
}
else
{
Split_UYVY(source, ysrc, usrc, vsrc, m_width, m_height);
}
y = ysrc;
u = usrc;
v = vsrc;
}
BYTE* ucomp = m_buffer;
BYTE* vcomp = m_buffer + ALIGN_ROUND(pixels + 64, 16);
m_threads[0].m_source = u;
m_threads[0].m_dest = ucomp;
m_threads[0].m_length = HALF(pixels);
SetEvent(m_threads[0].m_startEvent);
m_threads[1].m_source = v;
m_threads[1].m_dest = vcomp;
m_threads[1].m_length = HALF(pixels);
SetEvent(m_threads[1].m_startEvent);
Block_Predict_YUV16(y, ydest, m_width, pixels, true);
size_t y_size = m_compressWorker.Compact(ydest, m_out + 9, pixels);
size_t size = HandleTwoCompressionThreads(y_size);
m_out[0] = frameType;
icinfo->lpbiOutput->biSizeImage = size;
assert( *(__int64*)(m_out+*(UINT32*)(m_out+1)) != 0 );
assert( *(__int64*)(m_out+*(UINT32*)(m_out+5)) != 0 );
return ICERR_OK;
}
DWORD CodecInst::CompressYV12(ICCOMPRESS* icinfo)
{
const size_t pixels = m_width * m_height;
const size_t c_len = FOURTH(pixels);
const size_t len = (icinfo->lpbiInput->biBitCount != YV12) ? ALIGN_ROUND(pixels + HALF(pixels) + 32, 16) + c_len : pixels + HALF(pixels);
if(m_nullframes)
{
// compare in two parts, video is more likely to change in middle than at bottom
unsigned int pos = HALF(len) + 15;
pos &= (~15);
if(memcmp(m_in + pos, m_prevFrame + pos, len - pos) == 0 && memcmp(m_in, m_prevFrame, pos) == 0)
{
icinfo->lpbiOutput->biSizeImage = 0;
*icinfo->lpdwFlags = 0;
return (DWORD)ICERR_OK;
}
}
BYTE* source = m_in;
BYTE frameType;
if(m_deltaframes
&& (icinfo->dwFlags & ICCOMPRESS_KEYFRAME) != ICCOMPRESS_KEYFRAME
&& icinfo->lFrameNum > 0
&& InterframeEncode(m_deltaBuffer, m_in, m_prevFrame, len, DOUBLE(len)))
{
source = m_deltaBuffer;
*icinfo->lpdwFlags |= AVIIF_LASTPART;
frameType = YV12_DELTAFRAME;
}
else
{
*icinfo->lpdwFlags |= AVIIF_KEYFRAME;
icinfo->dwFlags |= ICCOMPRESS_KEYFRAME;
frameType = YV12_KEYFRAME;
}
if(m_nullframes || m_deltaframes)
{
memcpy(m_prevFrame, m_in, len);
}
const BYTE* ysrc = source;
const BYTE* vsrc = ysrc + pixels;
const BYTE* usrc = vsrc + c_len;
if(icinfo->lpbiInput->biBitCount != YV12)
{
usrc = ysrc + ALIGN_ROUND(pixels + 16, 16);
vsrc = ysrc + ALIGN_ROUND(pixels + HALF(pixels) + 32, 16);
}
BYTE* ucomp = m_buffer2;
BYTE* vcomp = m_buffer2 + ALIGN_ROUND(HALF(pixels) + 64, 16);
m_threads[0].m_source = vsrc;
m_threads[0].m_dest = vcomp;
m_threads[0].m_length = c_len;
SetEvent(m_threads[0].m_startEvent);
m_threads[1].m_source = usrc;
m_threads[1].m_dest = ucomp;
m_threads[1].m_length = c_len;
SetEvent(m_threads[1].m_startEvent);
BYTE* ydest = m_buffer;
ydest += (unsigned int)ysrc & 15;
Block_Predict(ysrc, ydest, m_width, pixels);
size_t y_size = m_compressWorker.Compact(ydest, m_out + 9, pixels);
size_t size = HandleTwoCompressionThreads(y_size);
m_out[0] = frameType;
icinfo->lpbiOutput->biSizeImage = size;
return (DWORD)ICERR_OK;
}