Skip to content

Commit 8425c10

Browse files
committed
Merge branch 'player'
2 parents 5c4a735 + 810780a commit 8425c10

File tree

4 files changed

+84
-36
lines changed

4 files changed

+84
-36
lines changed

player/decoder.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#elif __linux__
1010
#include <unistd.h>
1111
#endif
12+
#include <sys/time.h>
1213

1314
#include "quantizer_decoder.h"
1415
#include "entropic_decoder.h"
@@ -23,6 +24,8 @@
2324
#define U_STATE 1
2425
#define V_STATE 2
2526

27+
double timeval_diff(struct timeval *a, struct timeval *b);
28+
2629
int decode_stream(int width, int height, FILE * file) {
2730

2831
int status, subframe = 0, component_state = Y_STATE;
@@ -124,13 +127,14 @@ int decode_stream(int width, int height, FILE * file) {
124127

125128
int decode_stream_2(int width, int height, get_bits_context * ctx) {
126129

127-
int status, subframe = 0, component_state = Y_STATE;
130+
int status, subframe = 0, component_state = Y_STATE, frame_counter = 0;
128131
unsigned int subframe_counter[8] = { 0 };
129132
bool is_Y[1080] = { false }, is_U[1080] = { false },
130133
is_V[1080] = { false }, first = true;
131134
uint8_t *hops = NULL;
132135
yuv_image *img, *img_up;
133136

137+
build_hop_cache();
134138
init_player(width * 2, height * 2);
135139
hops = (uint8_t *)malloc(sizeof(uint8_t)* width);
136140
img = allocate_yuv_image(width, height);
@@ -150,6 +154,8 @@ int decode_stream_2(int width, int height, get_bits_context * ctx) {
150154
while (true) {
151155

152156
int readed_hops, line_num, past_subframe;
157+
double secs;
158+
struct timeval t_ini, t_fin;
153159
uint16_t headers;
154160
uint8_t header_high;
155161

@@ -181,6 +187,17 @@ int decode_stream_2(int width, int height, get_bits_context * ctx) {
181187
}
182188

183189
if (is_frame_completed(subframe, past_subframe)) {
190+
frame_counter++;
191+
if (frame_counter == 1) {
192+
gettimeofday(&t_ini, NULL);
193+
}
194+
if (frame_counter == 60) {
195+
gettimeofday(&t_fin, NULL);
196+
secs = timeval_diff(&t_fin, &t_ini);
197+
printf("INFO: fps are %f\n", 60/secs);
198+
frame_counter = 0;
199+
}
200+
184201
reconstruct_frame(img, is_Y, is_U, is_V, height, width);
185202
upsample_frame_adaptative(img, img_up);
186203
handle_window();
@@ -553,3 +570,7 @@ void free_yuv_image(yuv_image * img) {
553570
free((*img).V_data);
554571
free(img);
555572
}
573+
574+
double timeval_diff(struct timeval *a, struct timeval *b) {
575+
return ((double)(a->tv_sec +(double)a->tv_usec/1000000)-(double)(b->tv_sec + (double)b->tv_usec/1000000));
576+
}

player/get_bits.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +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
15+
#include <stdlib.h>
2216

17+
#include "get_bits.h"
2318

2419

2520
void init_get_bits(FILE * file, get_bits_context * ctx) {
@@ -29,20 +24,17 @@ void init_get_bits(FILE * file, get_bits_context * ctx) {
2924
}
3025

3126
uint8_t get_bit(get_bits_context * ctx) {
32-
size_t readed;
27+
3328
uint8_t byte, mask = 1;
3429
uint32_t buffer = ctx->buffer;
3530
int buffer_left = ctx->buffer_left;
3631

3732
if (ctx->buffer_left == 0) {
38-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
39-
while (readed != 1) {
40-
#ifdef _WIN32
41-
Sleep(10);
42-
#elif __linux__
43-
usleep(10 * 1000);
44-
#endif
45-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
33+
if (!feof(stdin) && fread(&buffer, sizeof(uint32_t), 1, ctx->handler) != 1)
34+
{
35+
printf("INFO: Finished reading the stream\n");
36+
fflush(stdout);
37+
exit(1);
4638
}
4739
ctx->buffer = buffer;
4840
buffer_left = 32;
@@ -82,8 +74,8 @@ uint8_t get_bit(get_bits_context * ctx) {
8274
}
8375

8476
uint8_t get_aligned_byte(get_bits_context * ctx) {
77+
8578
int buffer_left = 0;
86-
size_t readed;
8779
uint8_t byte = 0;
8880
uint32_t buffer;
8981

@@ -92,14 +84,11 @@ uint8_t get_aligned_byte(get_bits_context * ctx) {
9284
buffer = ctx->buffer;
9385

9486
if (buffer_left == 0) {
95-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
96-
while (readed != 1) {
97-
#ifdef _WIN32
98-
Sleep(10);
99-
#elif __linux__
100-
usleep(10 * 1000);
101-
#endif
102-
readed = fread(&buffer, sizeof(uint32_t), 1, ctx->handler);
87+
if (!feof(stdin) && fread(&buffer, sizeof(uint32_t), 1, ctx->handler) != 1)
88+
{
89+
printf("INFO: Finished reading the stream\n");
90+
fflush(stdout);
91+
exit(1);
10392
}
10493
ctx->buffer = buffer;
10594
buffer_left = 32;

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)