Skip to content

Commit 810780a

Browse files
committed
Added cache for hops calculation
1 parent 863820f commit 810780a

4 files changed

Lines changed: 56 additions & 40 deletions

File tree

player/decoder.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ int decode_stream_2(int width, int height, get_bits_context * ctx) {
134134
uint8_t *hops = NULL;
135135
yuv_image *img, *img_up;
136136

137+
build_hop_cache();
137138
init_player(width * 2, height * 2);
138139
hops = (uint8_t *)malloc(sizeof(uint8_t)* width);
139140
img = allocate_yuv_image(width, height);
@@ -190,10 +191,10 @@ int decode_stream_2(int width, int height, get_bits_context * ctx) {
190191
if (frame_counter == 1) {
191192
gettimeofday(&t_ini, NULL);
192193
}
193-
if (frame_counter == 30) {
194+
if (frame_counter == 60) {
194195
gettimeofday(&t_fin, NULL);
195196
secs = timeval_diff(&t_fin, &t_ini);
196-
printf("fps are %f\n", 30/secs);
197+
printf("INFO: fps are %f\n", 60/secs);
197198
frame_counter = 0;
198199
}
199200

player/get_bits.c

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,9 @@
1212
* @see https://github.com/jjaranda13/LHE_Pi
1313
*/
1414

15-
#include "get_bits.h"
16-
#ifdef _WIN32
17-
#include "String.h"
18-
#include <windows.h>
19-
#elif __linux__
20-
#include <unistd.h>
21-
#endif
22-
2315
#include <stdlib.h>
2416

17+
#include "get_bits.h"
2518

2619

2720
void init_get_bits(FILE * file, get_bits_context * ctx) {
@@ -31,26 +24,18 @@ void init_get_bits(FILE * file, get_bits_context * ctx) {
3124
}
3225

3326
uint8_t get_bit(get_bits_context * ctx) {
34-
size_t readed;
27+
3528
uint8_t byte, mask = 1;
3629
uint32_t buffer = ctx->buffer;
3730
int buffer_left = ctx->buffer_left;
3831

3932
if (ctx->buffer_left == 0) {
4033
if (!feof(stdin) && fread(&buffer, sizeof(uint32_t), 1, ctx->handler) != 1)
4134
{
42-
printf("End bit read");
35+
printf("INFO: Finished reading the stream\n");
4336
fflush(stdout);
4437
exit(1);
4538
}
46-
/*while (readed != 1) {
47-
#ifdef _WIN32
48-
Sleep(10);
49-
#elif __linux__
50-
usleep(10 * 1000);
51-
#endif
52-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
53-
}*/
5439
ctx->buffer = buffer;
5540
buffer_left = 32;
5641
byte = buffer;
@@ -89,8 +74,8 @@ uint8_t get_bit(get_bits_context * ctx) {
8974
}
9075

9176
uint8_t get_aligned_byte(get_bits_context * ctx) {
77+
9278
int buffer_left = 0;
93-
size_t readed;
9479
uint8_t byte = 0;
9580
uint32_t buffer;
9681

@@ -101,18 +86,10 @@ uint8_t get_aligned_byte(get_bits_context * ctx) {
10186
if (buffer_left == 0) {
10287
if (!feof(stdin) && fread(&buffer, sizeof(uint32_t), 1, ctx->handler) != 1)
10388
{
104-
printf("End bytes read");
89+
printf("INFO: Finished reading the stream\n");
10590
fflush(stdout);
10691
exit(1);
10792
}
108-
/*while (readed != 1) {
109-
#ifdef _WIN32
110-
Sleep(10);
111-
#elif __linux__
112-
usleep(10 * 1000);
113-
#endif
114-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
115-
}*/
11693
ctx->buffer = buffer;
11794
buffer_left = 32;
11895
byte = buffer;

player/quantizer_decoder.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,46 @@ void free_quantizer_decoder(uint8_t* component_value) {
1717
return;
1818
}
1919

20+
21+
void build_hop_cache() {
22+
int h;
23+
const int hop_min = 1, hop_max = 255-1;
24+
const float maxr = MAX_R, minr = MIN_R, range = RANGE;
25+
double rpos, rneg;
26+
27+
for (int hop0=0;hop0<=255;hop0++) {
28+
for (int hop1=MIN_H1; hop1<=MAX_H1;hop1++) {
29+
30+
rpos = min(maxr,pow(range*((255-hop0)/hop1),1.0f/3.0f));
31+
rpos = max(minr,rpos);
32+
33+
rneg = min(maxr,pow(range*(hop0/hop1),1.0f/3.0f));
34+
rneg=max(minr,rneg);
35+
36+
h=(int)(hop0-hop1*rneg*rneg*rneg);
37+
h=min(hop_max,h);
38+
h=max(h,hop_min);
39+
cache_hops[hop0][hop1-4][0] = (uint8_t)h;//(hop0-hop1*rneg*rneg*rneg);
40+
41+
h=(int)(hop0-hop1*rneg*rneg);
42+
h=min(hop_max,h);
43+
h=max(h,hop_min);
44+
cache_hops[hop0][hop1-4][1] = (uint8_t)h;//(hop0-hop1*rneg*rneg);
45+
46+
h=(int)(hop0-hop1*rneg);
47+
h=min(hop_max,h);
48+
h=max(h,hop_min);
49+
cache_hops[hop0][hop1-4][2] = (uint8_t)h;//(hop0-hop1*rneg);
50+
51+
}
52+
}
53+
54+
}
2055
void decode_line_quantizer(uint8_t * hops, uint8_t * component_value, int hops_lenght) {
2156

2257
char gradient = 0;
2358
unsigned char h1 = START_H1, hop0;
2459
bool last_small_hop = true, small_hop;
25-
double positive_ratio, negative_ratio;
2660
uint8_t current_hop;
2761

2862
for (int x = 0; x < hops_lenght; x++) {
@@ -40,8 +74,6 @@ void decode_line_quantizer(uint8_t * hops, uint8_t * component_value, int hops_l
4074
hop0 += gradient;
4175
#endif
4276

43-
calculate_ranges(hop0, h1, &positive_ratio, &negative_ratio);
44-
4577
switch (current_hop)
4678
{
4779
case HOP_0:
@@ -51,38 +83,39 @@ void decode_line_quantizer(uint8_t * hops, uint8_t * component_value, int hops_l
5183
component_value[x] = hop0 + h1;
5284
break;
5385
case HOP_P2:
54-
component_value[x] = hop0 + (int)ceil(h1*positive_ratio);
86+
component_value[x] = 255-cache_hops[255-hop0][h1-4][2];
5587
break;
5688
case HOP_P3:
57-
component_value[x] = hop0 + (int)ceil(h1*positive_ratio*positive_ratio);
89+
component_value[x] = 255-cache_hops[255-hop0][h1-4][1];
5890
break;
5991
case HOP_P4:
6092
#ifdef IS_MAX_HOPS
6193
component_value[x] = 255;
6294
#else
63-
component_value[x] = hop0 + (int)ceil(h1*positive_ratio*positive_ratio*positive_ratio);
95+
component_value[x] = 255-cache_hops[255-hop0][h1-4][0];
6496
#endif
6597
break;
6698
case HOP_N1:
6799
component_value[x] = hop0 - h1;
68100
break;
69101
case HOP_N2:
70-
component_value[x] = hop0 - (int)ceil(h1*negative_ratio);
102+
component_value[x] = cache_hops[hop0][h1-4][2];
71103
break;
72104
case HOP_N3:
73-
component_value[x] = hop0 - (int)ceil(h1*negative_ratio*negative_ratio);
105+
component_value[x] = cache_hops[hop0][h1-4][1];
74106
break;
75107
case HOP_N4:
76108
#ifdef IS_MAX_HOPS
77109
component_value[x] = 0;
78110
#else
79-
component_value[x] = hop0 - (int)ceil(h1*negative_ratio*negative_ratio*negative_ratio);
111+
component_value[x] = cache_hops[hop0][h1-4][0];
80112
#endif
81113
break;
82114
default:
83115
printf("ERROR: Unexpected symbol were found.");
84116
break;
85117
}
118+
86119
small_hop = is_small_hop(current_hop);
87120
h1 = adapt_h1(h1, small_hop, last_small_hop);
88121
#ifdef IS_GRADIENT

player/quantizer_decoder.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
/**
33
* @file entropic_decoder.h
4-
* @author Francisco José Juan Quintanilla
4+
* @author Francisco Jos� Juan Quintanilla
55
* @date Sep 2017
66
* @brief Quantizer decoder for LHE_rpi codec.
77
*
@@ -73,6 +73,9 @@
7373
#define HOP_N4 0
7474
#define HOP_P4 8
7575

76+
77+
uint8_t cache_hops[256][7][3]; //5KB cache [Y][h1][hop_number]
78+
7679
/**
7780
* @brief Allocates memory for the quantizer.
7881
*
@@ -104,6 +107,8 @@ void free_quantizer_decoder(uint8_t* component_value);
104107
void decode_line_quantizer(uint8_t * hops, uint8_t * component_value, int hops_lenght);
105108

106109

110+
void build_hop_cache();
111+
107112
/////////////////////////////////////////////////////////////////////////
108113
// PRIVATE FUNCTIONS. NOT TO BE CALLED FROM OUTSIDE
109114
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)