-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathentropy_threading.c
516 lines (420 loc) · 13.3 KB
/
entropy_threading.c
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*! @file
* @brief
*
* @version 1.0.0
*
* (C) Copyright 2017 GoPro Inc (http://gopro.com/).
*
* Licensed under either:
* - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
* - MIT license, http://opensource.org/licenses/MIT
* at your option.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "config.h"
#include "timing.h"
#include "decoder.h"
#include <math.h>
#ifndef DEBUG
#define DEBUG (1 && _DEBUG)
#endif
#ifndef TIMING
#define TIMING (1 && _TIMING)
#endif
#ifndef XMMOPT
#define XMMOPT (1 && _XMMOPT)
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <emmintrin.h> // Intel aligned alloc and free
#include "config.h"
#include "dump.h"
#include "decoder.h"
#include "codec.h"
#include "vlc.h"
#include "codebooks.h" // References to the codebooks
#include "debug.h"
#include "color.h" // Color formats supported by image processing routines
#include "image.h"
#include "filter.h"
#include "spatial.h"
#include "temporal.h"
//#include "logo40x5.h"
#include "convert.h"
#include "wavelet.h"
#include "bitstream.h"
#include "frame.h"
#include "cpuid.h"
#include "exception.h"
#ifndef DUMP
#define DUMP (0 && _DUMP)
#endif
#define ERROR_TOLERANT 1
#if (DEBUG && _WIN32)
#include <tchar.h> // For printing debug string in the console window
#endif
#define _DECODE_TRANSFORM 1 // Enable concurrent decoding and inverse transform
#define _TRANSFORM_FIELDPLUS 1 // Use the field plus transform
#if _SIF // In SIF resolution, enable the _DECODE_TRANSFORM switch
#if _DECODE_TRANSFORM == 0
#define _DECODE_TRANSFORM 1
#endif
#endif
#ifndef _FSMBUFFER
#define _FSMBUFFER 0
#endif
// Turn off saturation in this file
#ifdef SATURATE
#undef SATURATE
#endif
#define SATURATE(x) (assert(PIXEL_MIN <= (x) && (x) <= PIXEL_MAX), (x))
#define SATURATE8S(x) (assert(PIXEL8S_MIN <= (x) && (x) <= PIXEL8S_MAX), (x))
//#define SATURATE8S(x) SATURATE_8S(x)
//#define SATURATE(x) (x)
// Enable or disable function inlining
#if 1 //DEBUG
#define inline
#else
#define inline __forceinline
#endif
// Pixel size used for computing the compression ratio
#define BITS_PER_PIXEL 8
// Default processor capabilities
#define DEFAULT_FEATURES (_CPU_FEATURE_MMX )
// Macros for the finite state machine decoder
#if _INDIVIDUAL_LUT
#define GetFSMTableEntry(fsm, index) (FSMENTRY *)fsm->next_state+index
#define ResetFSM(fsm) fsm->next_state = fsm->table.entries[0]
#define UpdateFSM(fsm, next) fsm->next_state = fsm->table.entries[next]
#define GetFSMTableEntryIndividual(fsm, index) (FSMENTRY *)fsm->table.entries_ind[(fsm->next_state_index << FSM_INDEX_SIZE) | index]
#define ResetFSMIndividual(fsm) fsm->next_state_index = 0
#define UpdateFSMIndividual(fsm, next) fsm->next_state_index = next
#else
#define GetFSMTableEntry(fsm, index) (FSMENTRY *)fsm->next_state+index
#define ResetFSM(fsm) fsm->next_state = fsm->table.entries
#define UpdateFSM(fsm, next) fsm->next_state = fsm->table.entries+((int)next << FSM_INDEX_SIZE)
#endif
bool DecodeBandFSM16sNoGapWithPeaks(FSM *fsm, BITSTREAM *stream, PIXEL16S *image, int width, int height, int pitch, PIXEL *peaks, int level, int quant);
bool DecodeBandFSM16sNoGap(FSM *fsm, BITSTREAM *stream, PIXEL16S *image, int width, int height, int pitch);
#if _THREADED
void DecodeEntropy(DECODER *decoder, int work_index, int thread_index, FSM *fsm, int *initFsm)
{
struct entropy_data_new *data;
BITSTREAM *stream;
PIXEL *rowptr;
int width;
int height;
int pitch;
PIXEL *peaks;
int level;
int quant;
IMAGE *wavelet;
int band_index;
int active_codebook;
int difference_coding;
int result = true;
int skip = 0;
data = &decoder->entropy_worker_new.entropy_data[work_index];
// Get the processing parameters
active_codebook = data->active_codebook;
difference_coding = data->difference_coding;
stream = &data->stream;
rowptr = data->rowptr;
width = data->width;
height = data->height;
pitch = data->pitch;
peaks = data->peaks;
level = data->level;
quant = data->quant;
wavelet = data->wavelet;
band_index = data->band_index;
if(decoder->frame.resolution == DECODED_RESOLUTION_HALF_HORIZONTAL)
{
if(wavelet->level == 1 && (band_index == 1 || band_index == 3))
skip = 1;
}
if(!skip)
{
if(*initFsm != active_codebook)
{
*initFsm = active_codebook;
memcpy(fsm, &decoder->fsm[active_codebook], sizeof(FSM));
}
// Unlock access to the transform data
//Unlock(&decoder->entropy_worker_new.lock);
DeQuantFSM(fsm, quant);
//Do stuff
if(level && peaks)
{
result = DecodeBandFSM16sNoGapWithPeaks(fsm, stream, (PIXEL16S *)rowptr,
width, height, pitch, peaks, level, 1);
}
else
{
result = DecodeBandFSM16sNoGap(fsm, stream, (PIXEL16S *)rowptr, width, height, pitch);
}
if(difference_coding)
{
int x,y;
PIXEL *line = rowptr;
for(y=0;y<height;y++)
{
for(x=1;x<width;x++)
{
line[x] += line[x-1];
}
line += pitch/2;
}
}
}
if (result)
{
// Call thread safe routine to update the band valid flags
UpdateWaveletBandValidFlags(decoder, wavelet, band_index);
{
int num_entries;
//int work_index = -1;
struct transform_queue *data;
data = &decoder->transform_queue;
{
int curr_entry;
TRANSFORM *transform;
IMAGE *wavelet;
int channel;
int index;
int precision;
//int32_t lPreviousCount;
Lock(&decoder->entropy_worker_new.lock);
num_entries = data->num_entries;
Unlock(&decoder->entropy_worker_new.lock);
if(num_entries>0)
{
do
{
Lock(&decoder->entropy_worker_new.lock);
// Get the transform parameters
for(curr_entry=0; curr_entry<data->free_entry; curr_entry++)
{
assert(0 <= curr_entry && curr_entry < DECODING_QUEUE_LENGTH);
transform = data->queue[curr_entry].transform;
assert(transform != NULL);
channel = data->queue[curr_entry].channel;
assert(0 <= channel && channel < TRANSFORM_MAX_CHANNELS);
index = data->queue[curr_entry].index;
assert(0 <= index && index < TRANSFORM_MAX_WAVELETS);
wavelet = transform->wavelet[index];
assert(wavelet != NULL);
precision = data->queue[curr_entry].precision;
if(data->queue[curr_entry].done == 0 && BANDS_ALL_VALID(wavelet))
{
data->queue[curr_entry].done = 1;
data->next_entry++;
data->num_entries--;
break;
}
else
{
wavelet = NULL;
}
}
// Unlock access to the transform queue
Unlock(&decoder->entropy_worker_new.lock);
if(wavelet && BANDS_ALL_VALID(wavelet))
{
SCRATCH local;
// int localsize;
InitScratchBuffer(&local, decoder->threads_buffer[thread_index],
decoder->threads_buffer_size);
/* PushScratchBuffer(&local, &decoder->scratch);
localsize = (local.free_size/decoder->entropy_worker_new.pool.thread_count) & ~15;
local.base_ptr += localsize*thread_index;
local.free_ptr += localsize*thread_index;
local.free_size = localsize; */
// Apply the inverse wavelet transform to reconstruct the lower level wavelet
ReconstructWaveletBand(decoder, transform, channel, wavelet, index, precision,
&local, 0);
}
} while(wavelet && BANDS_ALL_VALID(wavelet));
}
}
}
}
}
THREAD_PROC(EntropyWorkerThreadProc, lpParam)
{
DECODER *decoder = (DECODER *)lpParam;
#if (1 && DEBUG)
FILE *logfile = decoder->logfile;
#endif
THREAD_ERROR error = THREAD_ERROR_OKAY;
int thread_index;
int initFsm = -1;
FSM fsm;
if (decoder->thread_cntrl.affinity)
{
#ifdef _WIN32
HANDLE hCurrentThread = GetCurrentThread();
SetThreadAffinityMask(hCurrentThread, decoder->thread_cntrl.affinity);
#else
pthread_t thread = pthread_self();
uint32_t thread_affinity = decoder->thread_cntrl.affinity;
SetThreadAffinityMask(thread, &thread_affinity);
#endif
}
// Set the handler for system exceptions
SetDefaultExceptionHandler();
// Determine the index of this worker thread
error = PoolThreadGetIndex(&decoder->entropy_worker_new.pool, &thread_index);
assert(error == THREAD_ERROR_OKAY);
for (;;)
{
THREAD_MESSAGE message = THREAD_MESSAGE_NONE;
error = PoolThreadWaitForMessage(&decoder->entropy_worker_new.pool, thread_index, &message);
// Received a signal to begin?
if(error == THREAD_ERROR_OKAY &&
(message == THREAD_MESSAGE_START || message == THREAD_MESSAGE_MORE_WORK))
{
for(;;)
{
int work_index = -1;
error = PoolThreadWaitForWork(&decoder->entropy_worker_new.pool, &work_index, thread_index);
// Is there another band to process?
if (error == THREAD_ERROR_OKAY)
{
DecodeEntropy(decoder, work_index, thread_index, &fsm, &initFsm);
}
else if(error == THREAD_ERROR_NOWORK)
{
PoolThreadSignalDone(&decoder->entropy_worker_new.pool, thread_index);
break;
}
}
}
else if (error == THREAD_ERROR_OKAY && message == THREAD_MESSAGE_STOP)
{
// The worker thread has been told to terminate itself
break;
}
else if (error != THREAD_ERROR_OKAY)
{
// If the wait failed it probably means that the thread pool is shutting down
break;
}
}
//OutputDebugString("thread end");
return (THREAD_RETURN_TYPE)error;
}
THREAD_PROC(ParallelThreadProc, lpParam)
{
DECODER *decoder = (DECODER *)lpParam;
#if (1 && DEBUG)
FILE *logfile = decoder->logfile;
#endif
THREAD_ERROR error = THREAD_ERROR_OKAY;
int thread_index;
if(decoder->thread_cntrl.affinity)
{
#ifdef _WIN32
HANDLE hCurrentThread = GetCurrentThread();
SetThreadAffinityMask(hCurrentThread,decoder->thread_cntrl.affinity);
#else
pthread_t thread = pthread_self();
uint32_t thread_affinity = decoder->thread_cntrl.affinity;
SetThreadAffinityMask(thread, &thread_affinity);
#endif
}
// Set the handler for system exceptions
SetDefaultExceptionHandler();
// Determine the index of this worker thread
error = PoolThreadGetIndex(&decoder->decoder_thread.pool, &thread_index);
assert(error == THREAD_ERROR_OKAY);
for (;;)
{
THREAD_MESSAGE message = THREAD_MESSAGE_NONE;
error = PoolThreadWaitForMessage(&decoder->decoder_thread.pool, thread_index, &message);
// Received a signal to begin?
if(error == THREAD_ERROR_OKAY &&
(message == THREAD_MESSAGE_START || message == THREAD_MESSAGE_MORE_WORK))
{
for(;;)
{
int work_index = -1;
error = PoolThreadWaitForWork(&decoder->decoder_thread.pool, &work_index, thread_index);
// Is there another band to process?
if (error == THREAD_ERROR_OKAY)
{
bool result = true;
BITSTREAM *input;
uint8_t *output;
int pitch;
ColorParam *colorparams;
// do the second channel
TAGVALUE segment;
int sample_type;
input = decoder->decoder_thread.input;
output = decoder->decoder_thread.output;
pitch = decoder->decoder_thread.pitch;
colorparams = decoder->decoder_thread.colorparams;
decoder->entropy_worker_new.next_queue_num = 0;
decoder->entropy_worker_new.threads_used = 0;
// Get the type of sample
segment = GetTagValue(input);
//assert(segment.tuple.tag == CODEC_TAG_SAMPLE);
if (!IsValidSegment(input, segment, CODEC_TAG_SAMPLE)) {
decoder->error = CODEC_ERROR_BITSTREAM;
}
if(decoder->error == CODEC_ERROR_OKAY)
{
sample_type = segment.tuple.value;
switch (sample_type)
{
case SAMPLE_TYPE_GROUP: // Group of frames (decode the first frame)
result = DecodeSampleGroup(decoder, input, output, pitch, colorparams);
break;
case SAMPLE_TYPE_FRAME: // Decode the second or later frame in a group
result = DecodeSampleFrame(decoder, input, output, pitch, colorparams);
break;
case SAMPLE_TYPE_IFRAME: // Decode a sample that represents an isolated frame
result = DecodeSampleIntraFrame(decoder, input, output, pitch, colorparams);
break;
case SAMPLE_TYPE_SEQUENCE_HEADER:
// The video sequence header is ignored
result = true;
break;
default:
// Need to fill the output frame
//error = CODEC_ERROR_SAMPLE_TYPE;
result = false;
}
}
}
else if(error == THREAD_ERROR_NOWORK)
{
PoolThreadSignalDone(&decoder->decoder_thread.pool, thread_index);
break;
}
}
}
else if (error == THREAD_ERROR_OKAY && message == THREAD_MESSAGE_STOP)
{
// The worker thread has been told to terminate itself
break;
}
else if (error != THREAD_ERROR_OKAY)
{
// If the wait failed it probably means that the thread pool is shutting down
break;
}
}
return (THREAD_RETURN_TYPE)error;
}
#endif