-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'work-rp' into develop (see #15 wip)
- Loading branch information
Showing
20 changed files
with
777 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
# gdb | ||
.gdb_history | ||
|
||
*.ezw | ||
perf/ | ||
build/ | ||
compile_commands.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef BITSTREAM_H | ||
#define BITSTREAM_H | ||
#include <stdio.h> | ||
#include "queue.h" | ||
|
||
// 0b00111000 | ||
#define SYMB_MASK 0x38 | ||
|
||
typedef struct { | ||
unsigned short threshold_pow; | ||
unsigned int num_bytes; | ||
unsigned char *bytes; | ||
} mini_header; | ||
|
||
enum file_op_mode { | ||
A, // append | ||
W // write | ||
}; | ||
|
||
// OPTIMIZE: Maybe pass by reference? Soo many copies!! | ||
// OPTIMIZE: Maybe pass by const reference when not mutating? | ||
|
||
mini_header *create_mini_header(unsigned int threshold, Queue *symbols); | ||
void write_bitstream_file(const char* filename, enum file_op_mode mode, | ||
mini_header *m_hdr, unsigned int dim); | ||
Queue* read_bitstream_file(const char* filename, Queue *header_queue, unsigned char *dim_pow); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef BITSTREAM_H | ||
#define BITSTREAM_H | ||
#include "queue.h" | ||
|
||
// TODO: | ||
// A bitstream is a main header followed by groups of "mini-headers" and | ||
// symbols packed into bytes | ||
// Operations that need to be implemented: | ||
// * Initialise bitstream | ||
// * Close bitstream | ||
// * Pack symbols into bytes | ||
// * Create header and mini-headers | ||
// * Write next set of symbols | ||
// * Read next set of symbols | ||
|
||
// WARNING: | ||
// The __packed__ attribute will result in unsafe behaviour on non x86 | ||
// systems. Use this code with care. | ||
// ref: https://stackoverflow.com/a/8568441 | ||
// | ||
// Group 10 symbols together to save space | ||
// | ||
// Change of plans....... no more structs | ||
// Just fall back to 2 symbols per byte, 2 MSBs are discarded | ||
// I just want to finish this.... | ||
struct __attribute__((__packed__)) symbols_group { | ||
unsigned char s0 : 3; | ||
unsigned char s1 : 3; | ||
unsigned char s2 : 3; | ||
unsigned char s3 : 3; | ||
unsigned char s4 : 3; | ||
unsigned char s5 : 3; | ||
unsigned char s6 : 3; | ||
unsigned char s7 : 3; | ||
unsigned char s8 : 3; | ||
unsigned char s9 : 3; | ||
}; | ||
|
||
struct symbol_set { | ||
unsigned short threshold; | ||
unsigned int size; | ||
struct symbols_group *symbols; | ||
}; | ||
|
||
void symbol_set_init(struct symbol_set *s_set, unsigned short threshold, unsigned int size); | ||
void symbol_group_insert(struct symbols_group *s_grp, int count, unsigned char symbol); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <stdlib.h> | ||
#include <math.h> | ||
#include "utils.h" | ||
#include "bitstream.h" | ||
|
||
mini_header *create_mini_header(unsigned int threshold, Queue *symbols) | ||
{ | ||
mini_header *m_hdr = malloc(sizeof(mini_header)); | ||
m_hdr->num_bytes = (unsigned int) (symbols->len / 2) + (symbols->len % 2); | ||
unsigned char curr_byte = 0xff; | ||
unsigned char *bytes = (unsigned char *) calloc(m_hdr->num_bytes, sizeof(unsigned char)); | ||
|
||
// NOTE: Do we really need this?? | ||
Node *node_pair[2] = {NULL}; | ||
|
||
m_hdr->threshold_pow = (unsigned short) log2(threshold); | ||
unsigned int ind = 0; | ||
while(symbols->head) { | ||
curr_byte = 0xff; | ||
// Pack pairs of symbols into bytes | ||
for(int i = 0; i < 2; i++) { | ||
node_pair[i] = dequeue(symbols); | ||
if(node_pair[i]) { | ||
curr_byte = (curr_byte << 3) | *(unsigned char *) node_pair[i]->data; | ||
} | ||
} | ||
bytes[ind] = curr_byte; | ||
ind++; | ||
} | ||
m_hdr->bytes = bytes; | ||
// DEBUG_INT("t", m_hdr->threshold_pow); | ||
// DEBUG_INT("n", m_hdr->num_bytes); | ||
// DEBUG_ARR_BYTE(m_hdr->bytes, m_hdr->num_bytes); | ||
return m_hdr; | ||
} | ||
|
||
void write_bitstream_file(const char* filename, enum file_op_mode mode, | ||
mini_header *m_hdr, unsigned int dim) | ||
{ | ||
FILE *fptr = NULL; | ||
unsigned char dim_pow = log2(dim); | ||
// DEBUG_INT("num_bytes", m_hdr->num_bytes); | ||
if(mode == A) { | ||
fptr = fopen(filename, "ab"); | ||
} | ||
else if(mode == W) { | ||
fptr = fopen(filename, "wb"); | ||
if(fwrite(&dim_pow, sizeof(dim_pow), 1, fptr) != 1) { | ||
fprintf(stderr, "Error while writing file: %s", filename); | ||
exit(1); | ||
} | ||
} | ||
if(fwrite(&m_hdr->threshold_pow, sizeof(m_hdr->threshold_pow), 1, fptr) != 1) { | ||
fprintf(stderr, "Error while writing file: %s", filename); | ||
exit(1); | ||
} | ||
// if(fwrite(&m_hdr->num_bytes, sizeof(m_hdr->num_bytes), 1, fptr) != 1) { | ||
if(fwrite(&m_hdr->num_bytes, sizeof(unsigned int), 1, fptr) != 1) { | ||
fprintf(stderr, "Error while writing file: %s", filename); | ||
exit(1); | ||
} | ||
// DEBUG_ARR_BYTE(m_hdr->bytes, m_hdr->num_bytes); | ||
if(fwrite(m_hdr->bytes, sizeof(m_hdr->bytes[0]), m_hdr->num_bytes, fptr) != m_hdr->num_bytes) { | ||
fprintf(stderr, "Error while writing file: %s", filename); | ||
exit(1); | ||
} | ||
fclose(fptr); | ||
} | ||
|
||
Queue* read_bitstream_file(const char* filename, Queue *header_queue, unsigned char *dim_pow) | ||
{ | ||
FILE *fptr = fopen(filename, "rb"); | ||
|
||
fread(dim_pow, sizeof(unsigned char), 1, fptr); | ||
|
||
unsigned short threshold_pow; | ||
while(fread(&threshold_pow, sizeof(unsigned short), 1, fptr) != 0) { | ||
mini_header *curr_hdr = (mini_header *) malloc(sizeof(mini_header)); | ||
curr_hdr->threshold_pow = threshold_pow; | ||
fread(&curr_hdr->num_bytes, sizeof(unsigned int), 1, fptr); | ||
curr_hdr->bytes = calloc(curr_hdr->num_bytes, sizeof(unsigned int)); | ||
fread(curr_hdr->bytes, sizeof(unsigned char), curr_hdr->num_bytes, fptr); | ||
header_queue = enqueue(header_queue, curr_hdr); | ||
} | ||
return header_queue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "bitstream.h.old" | ||
#include "smap.h" | ||
|
||
void symbol_set_init(struct symbol_set *s_set, unsigned short threshold, unsigned int size) | ||
{ | ||
s_set->threshold = threshold; | ||
s_set->size = size; | ||
} | ||
|
||
void symbol_group_insert(struct symbols_group *s_grp, int count, unsigned char symbol) | ||
{ | ||
switch(count) { | ||
case 0: | ||
s_grp->s0 = symbol; | ||
break; | ||
case 1: | ||
s_grp->s1 = symbol; | ||
break; | ||
case 2: | ||
s_grp->s2 = symbol; | ||
break; | ||
case 3: | ||
s_grp->s3 = symbol; | ||
break; | ||
case 4: | ||
s_grp->s4 = symbol; | ||
break; | ||
case 5: | ||
s_grp->s5 = symbol; | ||
break; | ||
case 6: | ||
s_grp->s6 = symbol; | ||
break; | ||
case 7: | ||
s_grp->s7 = symbol; | ||
break; | ||
case 8: | ||
s_grp->s8 = symbol; | ||
break; | ||
case 9: | ||
s_grp->s9 = symbol; | ||
break; | ||
default: return; | ||
} | ||
} |
Oops, something went wrong.