-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantizer.c
More file actions
428 lines (336 loc) · 12 KB
/
quantizer.c
File metadata and controls
428 lines (336 loc) · 12 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* @file quantizer.c
* @author JJGA
* @date July 2017
* @brief quantize one scanline using LHE.
*
* This module reads the image signal (Y,U,V) and transform one scanline into hops.
*
* @see https://github.com/jjaranda13/LHE_Pi
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include "include/globals.h"
#include "include/quantizer.h"
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
// la cache realmente podria ser de 10KB cache_hops[255][7][6]; e incluso de la mitad (5KB) pues es simetrica
//unsigned char cache_hops[256][7][6]; //10KB cache [Y][h1][hop_number]
unsigned char cache_hops[256][7][3]; //5KB cache [Y][h1][hop_number]
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void init_quantizer(){
///this function pre computes the cache
///la cache es cache[hop0][h1][hop] = 10.000 bytes = 10 KB
if (DEBUG) printf ("ENTER in init_quantizer()...\n");
if (!downsampler_initialized) {
printf(" Error: first you must initialize downsampler !!!!!");
exit(0);
}
for (int hop0=0;hop0<=255;hop0++){
for (int hop1=4; hop1<=10;hop1++) {
//ratio for possitive hops. max ratio=3 min ratio=1
float maxr=2.7f;
float minr=1.0f;//si fuese menor, un hop mayor podria ser inferior a un hop menor
// le ponemos rango 0.55 y no rango 8 para que acierte mejor, ya que la prediccion
// que vamos a usar es mala (pixel izquierdo) y asi mejora
const float range=0.70f;//0.55f;//0.8f; //con rango menor da mas calidad pero se gastan mas bits!!!!
double rpos = min (maxr,pow(range*((255-hop0)/hop1),1.0f/3.0f));
rpos=max(minr,rpos);
//ratio for negative hops. max ratio=3 min ratio=1
double rneg = min(maxr,pow(range*(hop0/hop1),1.0f/3.0f));
rneg=max(minr,rneg);
//compute hops 0,1,2,6,7,8 (hops 3,4,5 are known and dont need cache)
// 6,7 and 8 are not needed because cache is symmetrix. they are not computed
//-------------------------------------------------------------------
int h=(int)(hop0-hop1*rneg*rneg*rneg);
int hop_min=1;
int hop_max=255-hop_min;
h=min(hop_max,h);h=max(h,hop_min);
//cache_hops[hop0][hop1-4][0] = 0;//(unsigned char)h;//(hop0-hop1*rneg*rneg*rneg);
cache_hops[hop0][hop1-4][0] = (unsigned char)h;//(hop0-hop1*rneg*rneg*rneg);
h=(int)(hop0-hop1*rneg*rneg);
h=min(hop_max,h);h=max(h,hop_min);
cache_hops[hop0][hop1-4][1] = (unsigned char)h;//(hop0-hop1*rneg*rneg);
h=(int)(hop0-hop1*rneg);
h=min(hop_max,h);h=max(h,hop_min);
cache_hops[hop0][hop1-4][2] = (unsigned char)h;//(hop0-hop1*rneg);
/*
h=(int)(hop0+hop1*rpos);
h=min(hop_max,h);h=max(h,hop_min);
cache_hops[hop0][hop1-4][3] = (unsigned char)h;//(hop0+hop1*rpos);
h=(int)(hop0+hop1*rpos*rpos);
h=min(hop_max,h);h=max(h,hop_min);
cache_hops[hop0][hop1-4][4] = (unsigned char) h;//(hop0+hop1*rpos*rpos);
h=(int)(hop0+hop1*rpos*rpos*rpos);
h=min(hop_max,h);h=max(h,hop_min);
cache_hops[hop0][hop1-4][5] = (unsigned char)h;//(hop0+hop1*rpos*rpos*rpos);
*/
}//endfor hop1
}//endfor hop0
//memory allocation for result and hops
//---------------------------------------
//esto debe estar en el downsampler
//width_down_Y=width_orig_Y/pppy;
//height_down_Y=height_orig_Y/pppx;
//aqui hay que comprobar el modelo de color con la variable yuv_model
//-----------------------------------------
//esto debe estar en el downsampler
//width_down_UV=width_down_Y/2;
//height_down_UV=height_down_Y/2;
hops_Y=malloc(height_down_Y*sizeof (unsigned char *));
hops_U=malloc(height_down_UV*sizeof (unsigned char *));
hops_V=malloc(height_down_UV*sizeof (unsigned char *));
result_Y=malloc(height_down_Y*sizeof (unsigned char *));
result_U=malloc(height_down_UV*sizeof (unsigned char *));
result_V=malloc(height_down_UV*sizeof (unsigned char *));
for (int i=0;i<height_down_Y;i++)
{
hops_Y[i]=malloc(width_down_Y* sizeof (unsigned char));
result_Y[i]=malloc(width_down_Y* sizeof (unsigned char));
}
for (int i=0;i<height_down_UV;i++)
{
hops_U[i]=malloc(width_down_UV* sizeof (unsigned char));
hops_V[i]=malloc(width_down_UV* sizeof (unsigned char));
result_U[i]=malloc(width_down_UV* sizeof (unsigned char));
result_V[i]=malloc(width_down_UV* sizeof (unsigned char));
}
// these lines print the cache, for debug purposes
//----------------------------------
/*
for (int hop0=0;hop0<=255;hop0++)
{
for (int hop1=4;hop1<=10;hop1++)
{
for (int hn=0;hn<=2;hn++)
{
printf( "y=%d h1=%d h%d=%d \n",hop0,hop1,hn,cache_hops[hop0][hop1-4][hn]);
}
printf ( "y=%d h1=%d h3=%d \n",hop0,hop1,(hop0-hop1));
printf ( "y=%d h1=%d h4=%d \n",hop0,hop1,(hop0));
printf ( "y=%d h1=%d h5=%d \n",hop0,hop1,(hop0+hop1));
for (int hn=3;hn<=5;hn++)
{
printf( "y=%d h1=%d h%d=%d \n",hop0,hop1,hn+3,cache_hops[hop0][hop1-4][hn]);
}
}
}
*/
inteligent_discard_Y=malloc(height_down_Y*sizeof(bool));
inteligent_discard_U=malloc(height_down_UV*sizeof(bool));
inteligent_discard_V=malloc(height_down_UV*sizeof(bool));
inteligent_discard_mode = DEFAULT_INTELIGENT_DISCARD_MODE;
quantizer_initialized=true;
}
void close_quantizer()
{
for (int i=0;i<height_down_Y;i++)
{
free (hops_Y[i]);
free(result_Y[i]);
}
for (int i=0;i<height_down_UV;i++)
{
free(hops_U[i]);
free (hops_V[i]);
free (result_U[i]);
free (result_V[i]);
}
free (hops_Y);
free(hops_U);
free (hops_V);
free (result_Y);
free(result_U);
free(result_V);
free (inteligent_discard_Y);
free (inteligent_discard_U);
free (inteligent_discard_V);
quantizer_initialized=false;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bool quantize_scanline(unsigned char **orig_YUV, int y,int width, unsigned char **hops,unsigned char **result_YUV) {
/// this function quantize the luminances or chrominances of one scanline
/// inputs : orig_YUV (which can be orig_down_Y, orig_down_U or orig_down_V), line, width,
/// outputs: hops, result_YUV (which can be result_Y, result_U or result_V)
const int max_h1=10;
const int min_h1=4;
const int start_h1=(max_h1+min_h1)/2;
int h1=start_h1;
bool last_small_hop=true; //last hop was small
bool small_hop=true;//current hop is small
bool softline=true; //inteligent discard
int emin=255;//error min
int error=0;//computed error
unsigned char oc;
unsigned char hop0;//prediction
unsigned char quantum; //final quantum asigned value
unsigned char hop_value;//data from cache
unsigned char hop_number;// final assigned hop
//mejora de prediccion
int grad=0;
for (int x=0;x<width;)
{
//--------------------- PHASE 1: PREDICTION---------------------------------------------------------
if (x%2 == 0 && x < width-1)
oc = (orig_YUV[y][x] + orig_YUV[y][x+1])/2;
else
oc=orig_YUV[y][x];//original color
if (x==0)
hop0=127;
else
hop0=quantum;
//-------------------------PHASE 2: HOPS COMPUTATION-------------------------------
hop0 = hop0+grad > 255? 255: hop0+grad < 1? 1:hop0+grad;
hop_number=4;// prediction corresponds with hop_number=4
quantum=hop0;//this is the initial predicted quantum, the value of prediction
small_hop=true;//i supossed initially that hop will be small (3,4,5)
emin=oc-hop0 ; if (emin<0) emin=-emin;//minimum error achieved
if (emin>h1/2) //only enter in computation if emin>threshold
{
//positive hops
//--------------
if (oc>=hop0)
{
//case hop0 (most frequent)
//--------------------------
if ((quantum +h1)>255) goto phase3;
//case hop1 (frequent)
//---------------------
error=emin-h1;
if (error<0) error=-error;
if (error<emin)
{
hop_number=5;
emin=error;
quantum+=h1;
}
else goto phase3;
// case hops 6 to 8 (less frequent)
// --------------------------------
for (int i=3;i<6;i++)
{
//cache normal
//hop_value=cache_hops[hop0][h1-4][i];//indexes(i) are 3 to 5
//cache de 5KB simetrica
hop_value=255-cache_hops[255-hop0][h1-4][5-i];//indexes are 2 to 0
error=oc-hop_value;
if (error<0) error=-error;
if (error<emin)
{
hop_number=i+3;
emin=error;
quantum=hop_value;
}
else break;
}
}
//negative hops
//--------------
else
{
//case hop0 (most frequent)
//--------------------------
if ((quantum -h1)<0) goto phase3;
//case hop1 (frequent)
//-------------------
error=emin-h1;
if (error<0) error=-error;
if (error<emin)
{
hop_number=3;
emin=error;
quantum-=h1;
}
else goto phase3;
// case hops 2 to 0 (less frequent)
// --------------------------------
for (int i=2;i>=0;i--)
{
hop_value=cache_hops[hop0][h1-4][i];//indexes are 2 to 0
//hop_value=255-cache_hops[255-hop0][h1-4][5-i];//indexes are 2 to 0
error=hop_value-oc;
if (error<0) error=-error;
if (error<emin)
{
hop_number=i;
emin=error;
quantum=hop_value;
}
else break;
}
}
}//endif emin
//------------- PHASE 3: assignment of final quantized value --------------------------
phase3:
result_YUV[y][x]=quantum;
//*result_signal=quantum; result_signal++;
//prev_color=quantum;
//hop_number=5;
hops[y][x]=hop_number;
//*result_hops=hop_number; result_hops++;
//hops_type[hop_number]++;
//------------- PHASE 4: h1 logic --------------------------
if (hop_number>5 || hop_number<3)
small_hop=false;//true by default
if (small_hop && last_small_hop)
{
if (h1>min_h1)
h1--;
}
else
{
h1=max_h1;
}
last_small_hop=small_hop;
if (hop_number==5)
grad=1;
else if (hop_number==3)
grad=-1;
else if (!small_hop)
grad=0;
if (x>2)
{
switch (inteligent_discard_mode)
{
case 0: // Never soft-line
softline=false;
break;
case 1: // Only Hop0 is soft-line
if (hop_number != 4)
softline=false;
break;
case 2: // Hop0 & Hop1 is soft-line
if (hop_number>5 || hop_number<3)
softline=false;
break;
case 3: // Hop0, Hop1 & Hop2 are softline
if (hop_number>6 || hop_number<2)
softline=false;
break;
case 4: // Hop0, Hop1, Hop2 & Hop3 are softline
if (hop_number>7 || hop_number<1)
softline=false;
break;
case 5: // Allways everything is softline
softline=true;
break;
default:
if (hop_number>6 || hop_number<2)
softline=false;
break;
}
}
#ifdef JUMP_TO_EVENS
if (hop_number != 4)
x++;
else
x = (x + 2) & ~(1);
#else /* JUMP_TO_EVENS */
x++;
#endif /* JUMP_TO_EVENS */
}
return softline;
}