UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
LDGM_session Class Referenceabstract

Parent class for CPU and GPU implementations of LDGM coding. More...

#include <ldgm-session.h>

Inheritance diagram for LDGM_session:
Inheritance graph
Collaboration diagram for LDGM_session:
Collaboration graph

Public Member Functions

 LDGM_session ()
 
virtual ~LDGM_session ()
 
virtual void * alloc_buf (int size)=0
 
void create_edges (Tanner_graph *g)
 
virtual char * decode_frame (char *received_data, int buf_size, int *frame_size, std::map< int, int > valid_data)=0
 
virtual void encode (char *data, char *parity)=0
 
char * encode_frame (char *frame, int frame_size, int *out_buf_size)
 
char * encode_hdr_frame (char *hdr, int hdr_size, char *frame, int frame_size, int *out_buf_size)
 
virtual void encode_naive (char *data, char *parity)=0
 
virtual void free_out_buf (char *buf)=0
 
unsigned int get_packet_size () const
 
bool needs_decoding (Tanner_graph *tanner_graph)
 
void set_params (unsigned short k, unsigned short m, unsigned short rw)
 
void set_pcMatrix (char *matrix)
 
- Public Member Functions inherited from Coding_session

Protected Attributes

double elapsed_sum2
 
char * lost_ptr
 
unsigned short max_row_weight
 
long no_frames2
 
unsigned short packet_size
 
unsigned short param_k
 
unsigned short param_m
 
int * pcm
 
char * pcMatrix
 
char * received_ptr
 
unsigned short row_weight
 

Static Protected Attributes

static const int HEADER_SIZE = 4
 

Detailed Description

Parent class for CPU and GPU implementations of LDGM coding.

This class implements common mechanisms used during LDGM coding, however actual coding implementation has to be provided in child classes.

Definition at line 36 of file ldgm-session.h.

Constructor & Destructor Documentation

◆ LDGM_session()

LDGM_session::LDGM_session ( )
inline

Definition at line 40 of file ldgm-session.h.

40 : /* constructor **/
41 pcMatrix(NULL), pcm(NULL), param_k(0), param_m(0), row_weight(0),
43 lost_ptr(NULL), elapsed_sum2(0.0), no_frames2(0)
44 {
45 }
unsigned short param_m
double elapsed_sum2
unsigned short row_weight
unsigned short max_row_weight
unsigned short param_k
unsigned short packet_size
char * received_ptr

◆ ~LDGM_session()

LDGM_session::~LDGM_session ( )
virtual

Definition at line 410 of file ldgm-session.cpp.

410 {
411 free(pcm);
412}

References pcm.

Member Function Documentation

◆ alloc_buf()

virtual void * LDGM_session::alloc_buf ( int size)
pure virtual

Implemented in LDGM_session_cpu, and LDGM_session_gpu.

Referenced by encode_frame(), and encode_hdr_frame().

◆ create_edges()

void LDGM_session::create_edges ( Tanner_graph * g)

Definition at line 357 of file ldgm-session.cpp.

358{
359// printf ( "graph: %p, param_k: %d, param_m: %d\n", graph, param_k, param_m );
360 for ( int m = 0; m < param_m; ++m) {
361 for ( int k = 0; k < max_row_weight+2; ++k ) {
362 int idx = pcm [ m*(max_row_weight+2) + k];
363 if( idx > -1 ) {
364 auto &node = graph->nodes.at(idx);
365 node.neighbours.push_back(param_k + param_m + m);
366 auto &n2 = graph->nodes.at(param_k + param_m + m);
367 n2.neighbours.push_back(idx);
368 }
369 }
370 }
371 /* it = graph->nodes.find(0);
372 * while ( it != graph->nodes.end() )
373 * {
374 * printf ( "\nneighbours of node %d: ", it->first );
375 * for (vector<int>::iterator i = it->second.neighbours.begin();
376 * i != it->second.neighbours.end(); ++i)
377 * printf ( "%d, ", *i );
378 * it++;
379 * }
380 */
381
382 //add edges representing the staircase matrix
383 /* for ( int i = param_k + param_m; i < param_k + 2*param_m; ++i) {
384 * it = graph->nodes.find(i);
385 * (*it).second.neighbours.push_back(i - param_m);
386 * it = graph->nodes.find(i - param_m);
387 * (*it).second.neighbours.push_back(i);
388 * if(i > param_m + param_k) {
389 * it = graph->nodes.find(i);
390 * (*it).second.neighbours.push_back(i - param_m - 1);
391 * it = graph->nodes.find(i - param_m - 1);
392 * (*it).second.neighbours.push_back(i);
393 * }
394 * }
395 */
396 return ;
397} /* ----- end of function create_edges ----- */

References max_row_weight, Tanner_graph::nodes, param_k, param_m, and pcm.

Referenced by LDGM_session_cpu::decode_frame().

◆ decode_frame()

virtual char * LDGM_session::decode_frame ( char * received_data,
int buf_size,
int * frame_size,
std::map< int, int > valid_data )
pure virtual

Method decode should recover original source data

Parameters
received_dataReceived data (source and parity)
buf_sizeSize of the received buffer
frame_sizeOutput parameter for storing size of the decoded frame
valid_dataMap storing pairs <offset, number of bytes> of received data
Returns
Recovered source data

Implements Coding_session.

Implemented in LDGM_session_cpu, and LDGM_session_gpu.

◆ encode()

virtual void LDGM_session::encode ( char * data,
char * parity )
pure virtual

Implemented in LDGM_session_cpu, and LDGM_session_gpu.

Referenced by encode_frame(), and encode_hdr_frame().

◆ encode_frame()

char * LDGM_session::encode_frame ( char * source_data,
int frame_size,
int * buf_size )
virtual

Method encode should compute parity data for the given source data and add suitable FEC parity header.

Parameters
source_dataData to encode
frame_sizeSize of the frame being encoded
buf_sizeOutput parameter for storing total size of the returned buffer
Returns
Computed parity data with appropriate FEC parity header

Implements Coding_session.

Definition at line 124 of file ldgm-session.cpp.

125{
126 //printf("encode_frame\n");
127 Timer_util interval;
128 interval.start();
129
130 int buf_size;
131 int ps;
132 short header_size = LDGM_session::HEADER_SIZE;
133 int align_coef = param_k*sizeof(int);
134
135 if ( (frame_size + header_size) % align_coef == 0 )
136 buf_size = frame_size + header_size;
137 else
138 buf_size = ( ( (frame_size + header_size) / align_coef ) + 1 ) * align_coef;
139
140 ps = buf_size/param_k;
141
142 packet_size = ps;
143 //printf ( "ps: %d\n", ps );
144 buf_size += param_m*ps;
145 *out_buf_size = buf_size;
146
147 void *out_buf;
148 out_buf = alloc_buf(buf_size);
149 if (!out_buf)
150 {
151 printf ( "Unable to allocate memory\n" );
152 return NULL;
153 }
154 memset(out_buf, 0, header_size);
155 memset((char*)out_buf + frame_size + header_size, 0, align_coef);
156
157 //Insert frame size and copy input data into buffer
158
159 int *hdr = (int*)out_buf;
160 *hdr = frame_size;
161
162 memcpy( ((char*)out_buf) + header_size, frame, frame_size);
163
164 //Timer_util t;
165 //printf("2buf_size %d\n",buf_size);
166 //printf("2packet_size %d\n",packet_size);
167 this->encode ( (char*)out_buf, ((char*)out_buf)+param_k*ps );
168
169
170 interval.end();
171 // printf("time: %e\n",elapsed/1000.0 );
172 this->elapsed_sum2 += interval.elapsed_time_ms();
173 this->no_frames2++;
174
175 return (char*)out_buf;
176
177// printf ( "SSE: %.3lf\n", t.elapsed_time(start, end) );
178
179 /* void *out_buf_check;
180 * error = posix_memalign(&out_buf_check, 16, buf_size);
181 * memset(out_buf_check, 0, buf_size);
182 * hdr = (int*)out_buf_check;
183 * *hdr = frame_size;
184 * memcpy(((char*)out_buf_check)+header_size, frame, frame_size);
185 * clock_gettime(CLOCK_MONOTONIC, &start);
186 * encode_naive ( (char*)out_buf, ((char*)out_buf_check)+param_k*ps );
187 * clock_gettime(CLOCK_MONOTONIC, &end);
188 * printf ( "CPU: %.3lf\n", t.elapsed_time(start, end) );
189 *
190 * int e = memcmp(out_buf, out_buf_check, buf_size);
191 * printf ( "memcpy on parities: %d\n", e );
192 * char *a = (char*)out_buf;
193 * char *b = (char*)out_buf_check;
194 * bool equal = true;
195 * int idx = 0;
196 * for ( int i = 0; i < param_m*ps; i++)
197 * {
198 * if(a[i] != b[i])
199 * {
200 * idx = i;
201 * printf ( "Error at index %d: %d vs %d\n", i, (unsigned char)a[i],
202 * (unsigned char)b[i] );
203 * equal = false;
204 * }
205 * }
206 *
207 * printf ( "Parities %s match.\n", (equal)?"":"do not" );
208 */
209
210// printf ( "\n\nEncoded block:\n" );
211// printf ( "%-13s\v%10d B\n", "Frame hdr size:", header_size );
212// printf ( "%-13s\v%10d B\n", "Frame size:", frame_size );
213// printf ( "Padding size:\t%-d B\n", buf_size-frame_size-header_size-param_m*ps );
214// printf ( "Parity size:\t%-d B\n", param_m*ps );
215// printf ( "-------------------------------\n");
216// printf ( "Total block size:\t%d B\n", buf_size );
217// printf ( "Symbol size:\t%d\n\n\n", ps );
218
219
220
221
222}
struct video_frame * frame
static const int HEADER_SIZE
virtual void * alloc_buf(int size)=0
virtual void encode(char *data, char *parity)=0
This class implements timer utils.
Definition timer-util.h:26
void end()
Definition timer-util.h:36
double elapsed_time_ms() const
Definition timer-util.h:52
void start()
Definition timer-util.h:32

References alloc_buf(), elapsed_sum2, Timer_util::elapsed_time_ms(), encode(), Timer_util::end(), frame, HEADER_SIZE, no_frames2, packet_size, param_k, param_m, and Timer_util::start().

Referenced by demo().

Here is the call graph for this function:

◆ encode_hdr_frame()

char * LDGM_session::encode_hdr_frame ( char * hdr,
int hdr_size,
char * frame,
int frame_size,
int * out_buf_size )

Definition at line 225 of file ldgm-session.cpp.

226{
227 int buf_size;
228 int ps;
229 short header_size = LDGM_session::HEADER_SIZE;
230 int overall_size = my_hdr_size + frame_size;
231 int align_coef = param_k*sizeof(int);
232
233 if ( (overall_size + header_size) % align_coef == 0 )
234 buf_size = overall_size + header_size;
235 else
236 buf_size = ( ( (overall_size + header_size) / align_coef ) + 1 ) * align_coef;
237
238 ps = buf_size/param_k;
239
240 packet_size = ps;
241// printf ( "ps: %d\n", ps );
242 buf_size += param_m*ps;
243 *out_buf_size = buf_size;
244
245 void *out_buf;
246 out_buf = alloc_buf(buf_size);
247 if (!out_buf)
248 {
249 printf ( "Unable to allocate aligned memory\n" );
250 return NULL;
251 }
252 memset(out_buf, 0, buf_size);
253
254 //Insert frame size and copy input data into buffer
255
256 int32_t *hdr = (int32_t*)out_buf;
257 *hdr = overall_size;
258
259 memcpy( ((char*)out_buf) + header_size, my_hdr, my_hdr_size);
260 memcpy( ((char*)out_buf) + header_size + my_hdr_size, frame, frame_size);
261
262#if 0
263 int my_frame_size=my_hdr_size+frame_size;
264
265 // printf("my_frame_size: %d\n",my_frame_size );
266
267 int crc;
268 crc = crc32buf((char*)out_buf+header_size,my_frame_size);
269
270 memcpy( ((char*)out_buf) + 4, &crc, 4);
271 memcpy( ((char*)out_buf) + 8, &my_frame_size, 4);
272 assert(LDGM_session::HEADER_SIZE >= 12);
273#endif
274
275 Timer_util interval;
276 interval.start();
277
278 this->encode ( (char*)out_buf, ((char*)out_buf)+param_k*ps );
279
280 interval.end();
281 long elapsed;
282
283 elapsed = interval.elapsed_time_us();
284 // printf("time: %e\n",elapsed/1000.0 );
285 this->elapsed_sum2+=elapsed/1000.0;
286 this->no_frames2++;
287
288
289 // gettimeofday(&t1,0);
290 // long elapsed;
291
292 // elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
293 // // printf("time: %e\n",elapsed/1000.0 );
294 // this->elapsed_sum2+=elapsed/1000.0;
295 // this->no_frames2++;
296 // i++;
297
298 // if(i%100==0){
299 // printf("TIME GPU: %f ms\n",this->elapsed_sum2/(double)this->no_frames2 );
300 // printf("time: %f ms\n",elapsed/1000.0 );
301 // }
302
303// printf ( "SSE: %.3lf\n", t.elapsed_time(start, end) );
304
305 /* void *out_buf_check;
306 * error = posix_memalign(&out_buf_check, 16, buf_size);
307 * memset(out_buf_check, 0, buf_size);
308 * hdr = (int*)out_buf_check;
309 * *hdr = frame_size;
310 * memcpy(((char*)out_buf_check)+header_size, frame, frame_size);
311 * clock_gettime(CLOCK_MONOTONIC, &start);
312 * encode_naive ( (char*)out_buf, ((char*)out_buf_check)+param_k*ps );
313 * clock_gettime(CLOCK_MONOTONIC, &end);
314 * printf ( "CPU: %.3lf\n", t.elapsed_time(start, end) );
315 *
316 * int e = memcmp(out_buf, out_buf_check, buf_size);
317 * printf ( "memcpy on parities: %d\n", e );
318 * char *a = (char*)out_buf;
319 * char *b = (char*)out_buf_check;
320 * bool equal = true;
321 * int idx = 0;
322 * for ( int i = 0; i < param_m*ps; i++)
323 * {
324 * if(a[i] != b[i])
325 * {
326 * idx = i;
327 * printf ( "Error at index %d: %d vs %d\n", i, (unsigned char)a[i],
328 * (unsigned char)b[i] );
329 * equal = false;
330 * }
331 * }
332 *
333 * printf ( "Parities %s match.\n", (equal)?"":"do not" );
334 */
335
336// printf ( "\n\nEncoded block:\n" );
337// printf ( "%-13s\v%10d B\n", "Frame hdr size:", header_size );
338// printf ( "%-13s\v%10d B\n", "Frame size:", frame_size );
339// printf ( "Padding size:\t%-d B\n", buf_size-frame_size-header_size-param_m*ps );
340// printf ( "Parity size:\t%-d B\n", param_m*ps );
341// printf ( "-------------------------------\n");
342// printf ( "Total block size:\t%d B\n", buf_size );
343// printf ( "Symbol size:\t%d\n\n\n", ps );
344
345
346 return (char*)out_buf;
347
348}
long elapsed_time_us() const
Definition timer-util.h:58
uint32_t crc32buf(const char *buf, size_t len)
Definition crc_32.c:176

References alloc_buf(), crc32buf(), elapsed_sum2, Timer_util::elapsed_time_us(), encode(), Timer_util::end(), frame, HEADER_SIZE, no_frames2, packet_size, param_k, param_m, and Timer_util::start().

Here is the call graph for this function:

◆ encode_naive()

virtual void LDGM_session::encode_naive ( char * data,
char * parity )
pure virtual

Implemented in LDGM_session_cpu, and LDGM_session_gpu.

◆ free_out_buf()

virtual void LDGM_session::free_out_buf ( char * buf)
pure virtual

Implemented in LDGM_session_cpu, and LDGM_session_gpu.

◆ get_packet_size()

unsigned int LDGM_session::get_packet_size ( ) const
inline

Definition at line 59 of file ldgm-session.h.

60 {
61 return packet_size;
62 }

References packet_size.

Referenced by LDGM_session_gpu::encode().

◆ needs_decoding()

bool LDGM_session::needs_decoding ( Tanner_graph * tanner_graph)

This method checks whether the given graph needs further decoding

Parameters
tanner_graphGiven Tanner graph
Returns
true if the given graph needs decoding, false otherwise

Definition at line 400 of file ldgm-session.cpp.

401{
402 for ( int i = 0; i < param_k; i++)
403 if( ! graph->nodes.at(i).isDone() ) {
404 return true;
405 }
406 return false;
407} /* ----- end of method LDGM_session::needs_decoding ----- */

References Tanner_graph::nodes, and param_k.

Referenced by LDGM_session_cpu::decode_frame().

◆ set_params()

void LDGM_session::set_params ( unsigned short k,
unsigned short m,
unsigned short rw )
inline

Definition at line 92 of file ldgm-session.h.

96 {
97 param_k = k;
98 param_m = m;
99 row_weight = rw;
100 }

References param_k, param_m, and row_weight.

Referenced by demo().

◆ set_pcMatrix()

void LDGM_session::set_pcMatrix ( char * matrix)

Definition at line 40 of file ldgm-session.cpp.

41{
42 FILE *f;
43
44 f = fopen(fname, "rb");
45 if (!f)
46 {
47 printf ( "Error opening matrix file\n" );
48 printf ( "exiting\n" );
49 abort();
50 }
51 unsigned int k_f, m_f, w_f;
52 if (fscanf(f, "%d %d %d", &k_f, &m_f, &w_f) != 3) {
53 throw string("Parity matrix read error!");
54 }
55// printf ( "In matrix file: K %d M %d Columns %d\n", k_f, m_f, w_f );
56
57 if (w_f < 2 || w_f > MAX_W) {
58 throw string("Invalid parameter in parity matrix (allowed range [2.." + to_string(MAX_W) + "])!");
59 }
60
61 if ( k_f != param_k || m_f != param_m)
62 {
63 ostringstream oss;
64 oss << "Parity matrix size mismatch\nExpected K = " << param_k << "% M = " << param_m <<
65 "\nReceived K = " << k_f << ", M = " << m_f << "\n";
66 throw oss.str();
67 }
68 if (fseek (f, 1, SEEK_CUR ) != 0) {
69 perror("fseek");
70 }
71
72 pcm = (int*) malloc(w_f*param_m*sizeof(int));
73 for ( int i = 0; i < (int)w_f*param_m; i++) {
74 if (fread ( pcm+i, sizeof(int), 1, f) != 1) {
75 throw string("Parity matrix read error!");
76 }
77 }
78 this->max_row_weight = w_f - 2; //w_f stores number of columns in adjacency list
79
80 /* for ( int i = 0; i < param_m; i++)
81 * {
82 * for ( int j = 0; j < w_f; j++)
83 * printf ( "[%3d]", (unsigned int)(pcm[i*w_f+j]) );
84 * printf ( "\n" );
85 * }
86 */
87
88
89
90
91 fclose(f);
92
93
94
95 /*
96 * this->max_row_weight = 0;
97 * int max_weight = 0;
98 * for ( int i = 0; i < param_m; ++i) {
99 * for ( int j = 0; j < param_k; ++j) {
100 * if(*(matrix + i*param_k + j) == 1) {
101 * *(pcMatrix + i*param_k + j) = *(matrix + i*param_k + j);
102 * max_weight++;
103 * }
104 * }
105 * if ( max_weight > this->max_row_weight )
106 * this->max_row_weight = max_weight;
107 * max_weight = 0;
108 * }
109 *
110 * pcm = (int*) malloc (m*(max_row_weight+2)*sizeof(int*));
111 * struct coding_params params;
112 * params.m = m;
113 * params.k = k;
114 * params.max_row_weight = max_row_weight;
115 *
116 * make_compact_pcm ( matrix, pcm, params);
117 */
118
119
120 return ;
121} /* ----- end of method Coding_session::set_pcMatrix ----- */
constexpr const int MAX_W

References max_row_weight, MAX_W, param_k, param_m, and pcm.

Referenced by demo().

Member Data Documentation

◆ elapsed_sum2

◆ HEADER_SIZE

const int LDGM_session::HEADER_SIZE = 4
staticprotected

◆ lost_ptr

char* LDGM_session::lost_ptr
protected

Definition at line 119 of file ldgm-session.h.

◆ max_row_weight

unsigned short LDGM_session::max_row_weight
protected

◆ no_frames2

◆ packet_size

◆ param_k

◆ param_m

◆ pcm

◆ pcMatrix

char* LDGM_session::pcMatrix
protected

Definition at line 109 of file ldgm-session.h.

◆ received_ptr

char* LDGM_session::received_ptr
protected

Definition at line 118 of file ldgm-session.h.

◆ row_weight

unsigned short LDGM_session::row_weight
protected

Definition at line 114 of file ldgm-session.h.

Referenced by set_params().


The documentation for this class was generated from the following files: