Skip to content

Commit

Permalink
Start implementing some CPU version of missing funtions
Browse files Browse the repository at this point in the history
Running decode on an image with saving RGB file but missing data

Signed-off-by: Anthony Liot <[email protected]>
  • Loading branch information
wolfviking0 committed Dec 16, 2023
1 parent cbd8a3b commit 89878b2
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 39 deletions.
133 changes: 121 additions & 12 deletions src/gpujpeg_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ gpujpeg_get_devices_info(void)
#endif
}
#else
// TODO: NEED IMPLEMENTATION
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_get_devices_info(): NOT YET IMPLEMENTED\n");
#endif
return devices_info;
}
Expand Down Expand Up @@ -261,7 +262,8 @@ gpujpeg_init_device(int device_id, int flags)
return -1;
}
#else
// TODO: NEED IMPLEMENTATION
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_init_device(): NOT YET IMPLEMENTED\n");
#endif
return 0;
}
Expand Down Expand Up @@ -440,6 +442,7 @@ void gpujpeg_set_device(int index)
cudaSetDevice(index);
#else
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_set_device(): NOT YET IMPLEMENTED\n");
#endif
}

Expand All @@ -463,7 +466,8 @@ gpujpeg_component_print8(struct gpujpeg_component* component, uint8_t* d_data)
cudaFreeHost(data);
#else
// TODO: NEED IMPLEMENTATION
#endif
printf("[WARNING] gpujpeg_component_print8(): NOT YET IMPLEMENTED\n");
#endif
}

/* Documented at declaration */
Expand All @@ -486,7 +490,8 @@ gpujpeg_component_print16(struct gpujpeg_component* component, int16_t* d_data)
cudaFreeHost(data);
#else
// TODO: NEED IMPLEMENTATION
#endif
printf("[WARNING] gpujpeg_component_print16(): NOT YET IMPLEMENTED\n");
#endif
}

/* Documented at declaration */
Expand All @@ -508,6 +513,7 @@ gpujpeg_coder_init(struct gpujpeg_coder * coder)
}
#else
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_coder_init(): NOT YET IMPLEMENTED\n");
#endif
// Initialize coder for no image
coder->param.quality = -1;
Expand Down Expand Up @@ -594,6 +600,18 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("Coder color component device allocation", return 0);
#else
// TODO: NEED IMPLEMENTATION
// (Re)allocate color components in host memory
if (coder->component != NULL) {
free(coder->component);
coder->component = NULL;
}
coder->component = (struct gpujpeg_component*)malloc(param_image->comp_count * sizeof(struct gpujpeg_component));
// (Re)allocate color components in device memory
if (coder->d_component != NULL) {
free(coder->d_component);
coder->d_component = NULL;
}
coder->d_component = (struct gpujpeg_component*)malloc(param_image->comp_count * sizeof(struct gpujpeg_component));
#endif
coder->component_allocated_size = param_image->comp_count;
}
Expand Down Expand Up @@ -738,6 +756,19 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("Coder segment device allocation", return 0);
#else
// TODO: NEED IMPLEMENTATION
// (Re)allocate segments in host memory
if (coder->segment != NULL) {
free(coder->segment);
coder->segment = NULL;
}
coder->segment = (struct gpujpeg_segment*) malloc(coder->segment_count * sizeof(struct gpujpeg_segment));

// (Re)allocate segments in device memory
if (coder->d_segment != NULL) {
free(coder->d_segment);
coder->d_segment = NULL;
}
coder->d_segment = (struct gpujpeg_segment*) malloc(coder->segment_count * sizeof(struct gpujpeg_segment));
#endif
coder->segment_allocated_size = coder->segment_count;
}
Expand Down Expand Up @@ -870,6 +901,27 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("Coder quantized data device allocation", return 0);
#else
// TODO: NEED IMPLEMENTATION
// (Re)allocate preprocessor data in device memory
if (coder->d_data != NULL) {
free(coder->d_data);
coder->d_data = NULL;
}
coder->d_data = (uint8_t*) malloc((coder->data_size + idct_overhead) * sizeof(uint8_t));

// (Re)allocated DCT and quantizer data in host memory
if (coder->data_quantized != NULL) {
free(coder->data_quantized);
coder->data_quantized = NULL;
}
coder->data_quantized = (int16_t*) malloc(coder->data_size * sizeof(int16_t));

// (Re)allocated DCT and quantizer data in device memory
if (coder->d_data_quantized != NULL) {
free(coder->d_data_quantized);
coder->d_data_quantized = NULL;
}

coder->d_data_quantized = (int16_t*) malloc((coder->data_size + idct_overhead) * sizeof(int16_t));
#endif
coder->data_allocated_size = coder->data_size + idct_overhead;
}
Expand All @@ -882,7 +934,8 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("d_data memset failed", return 0);
#else
// TODO: NEED IMPLEMENTATION
#endif
memset(coder->d_data, 0, coder->data_size * sizeof(uint8_t));
#endif
}

// Set data buffer to color components
Expand Down Expand Up @@ -934,6 +987,26 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("Huffman temp buffer device allocation", return 0);
#else
// TODO: NEED IMPLEMENTATION
// (Re)allocate huffman coder data in host memory
if (coder->data_compressed != NULL) {
free(coder->data_compressed);
coder->data_compressed = NULL;
}
coder->data_compressed = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));

// (Re)allocate huffman coder data in device memory
if (coder->d_data_compressed != NULL) {
free(coder->d_data_compressed);
coder->d_data_compressed = NULL;
}
coder->d_data_compressed = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));

// (Re)allocate Huffman coder temporary buffer
if (coder->d_temp_huffman != NULL) {
free(coder->d_temp_huffman);
coder->d_temp_huffman = NULL;
}
coder->d_temp_huffman = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));
#endif
coder->data_compressed_allocated_size = max_compressed_data_size;
}
Expand Down Expand Up @@ -965,6 +1038,19 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
gpujpeg_cuda_check_error("Coder block list device allocation", return 0);
#else
// TODO: NEED IMPLEMENTATION
// (Re)allocate list of block indices in host memory
if (coder->block_list != NULL) {
free(coder->block_list);
coder->block_list = NULL;
}
coder->block_list = (uint64_t*)malloc(coder->block_count * sizeof(*coder->block_list));

// (Re)allocate list of block indices in device memory
if (coder->d_block_list != NULL) {
free(coder->d_block_list);
coder->d_block_list = NULL;
}
coder->d_block_list = (uint64_t*)malloc(coder->block_count * sizeof(*coder->d_block_list));
#endif
coder->block_allocated_size = coder->block_count;
}
Expand Down Expand Up @@ -1041,7 +1127,15 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
cudaMemcpyAsync(coder->d_segment, coder->segment, coder->segment_count * sizeof(struct gpujpeg_segment), cudaMemcpyHostToDevice, stream);
gpujpeg_cuda_check_error("Coder segment copy", return 0);
#else
// TODO: NEED IMPLEMENTATION
// TODO: NEED IMPLEMENTATION
// Copy components to device memory
memcpy(coder->d_component, coder->component, coder->param_image.comp_count * sizeof(struct gpujpeg_component));

// Copy block lists to device memory
memcpy(coder->d_block_list, coder->block_list, coder->block_count * sizeof(*coder->d_block_list));

// Copy segments to device memory
memcpy(coder->d_segment, coder->segment, coder->segment_count * sizeof(struct gpujpeg_segment));
#endif
coder->allocated_gpu_memory_size = allocated_gpu_memory_size;

Expand Down Expand Up @@ -1098,7 +1192,8 @@ gpujpeg_coder_deinit(struct gpujpeg_coder* coder)
if ( coder->d_block_list != NULL )
cudaFree(coder->d_block_list);
#else
// TODO: NEED IMPLEMENTATION
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_coder_deinit(): NOT YET IMPLEMENTED\n");
#endif
GPUJPEG_CUSTOM_TIMER_DESTROY(coder->duration_memory_to, return -1);
GPUJPEG_CUSTOM_TIMER_DESTROY(coder->duration_memory_from, return -1);
Expand Down Expand Up @@ -1143,7 +1238,8 @@ static void *gpujpeg_cuda_malloc_host(size_t size) {
GPUJPEG_CHECK_EX(cudaMallocHost(&ptr, size), "Could not alloc host pointer", return NULL);
#else
// TODO: NEED IMPLEMENTATION
#endif
printf("[WARNING] gpujpeg_cuda_malloc_host(): NOT YET IMPLEMENTED\n");
#endif
return ptr;
}

Expand Down Expand Up @@ -1180,7 +1276,12 @@ gpujpeg_image_load_from_file(const char* filename, uint8_t** image, size_t* imag
}
#else
// TODO: NEED IMPLEMENTATION
#endif
data = (uint8_t*)malloc(*image_size * sizeof(uint8_t));
if ( *image_size != fread(data, sizeof(uint8_t), *image_size, file) ) {
fprintf(stderr, "[GPUJPEG] [Error] Failed to load image data [%zd bytes] from file %s!\n", *image_size, filename);
return -1;
}
#endif
fclose(file);

*image = data;
Expand Down Expand Up @@ -1266,7 +1367,8 @@ gpujpeg_image_destroy(uint8_t* image)
cudaFreeHost(image);
#else
// TODO: NEED IMPLEMENTATION
#endif
printf("[WARNING] gpujpeg_image_destroy(): NOT YET IMPLEMENTED\n");
#endif
return 0;
}

Expand Down Expand Up @@ -1684,7 +1786,8 @@ gpujpeg_opengl_texture_register(int texture_id, enum gpujpeg_opengl_texture_type
#endif
#else
// TODO: NEED IMPLEMENTATION
#endif
printf("[WARNING] gpujpeg_opengl_texture_register(): NOT YET IMPLEMENTED\n");
#endif
}

/* Documented at declaration */
Expand All @@ -1704,6 +1807,7 @@ gpujpeg_opengl_texture_unregister(struct gpujpeg_opengl_texture* texture)
cudaFreeHost(texture);
#else
// TODO: NEED TO BE IMPLEMENTED
printf("[WARNING] gpujpeg_opengl_texture_unregister(): NOT YET IMPLEMENTED\n");
#endif
#else
(void) texture;
Expand Down Expand Up @@ -1751,6 +1855,7 @@ gpujpeg_opengl_texture_map(struct gpujpeg_opengl_texture* texture, size_t* data_
*data_size = d_data_size;
#else
// TODO: NEED TO BE IMPLEMENTED
printf("[WARNING] gpujpeg_opengl_texture_map(): NOT YET IMPLEMENTED\n");
(void) data_size;
GPUJPEG_MISSING_OPENGL(return NULL);
#endif
Expand Down Expand Up @@ -1792,6 +1897,7 @@ gpujpeg_opengl_texture_unmap(struct gpujpeg_opengl_texture* texture)
#endif
#else
// TODO: NEED IMPLEMENTATION
printf("[WARNING] gpujpeg_opengl_texture_unmap(): NOT YET IMPLEMENTED\n");
#endif
}

Expand Down Expand Up @@ -1990,7 +2096,10 @@ float gpujpeg_custom_timer_get_duration(cudaEvent_t start, cudaEvent_t stop) {
return elapsedTime;
}
#else
// TODO: NEED IMPLEMENTATION
float gpujpeg_custom_timer_get_duration(float start, float stop) {
float elapsedTime = NAN;
return elapsedTime;
}
#endif

/* vi: set expandtab sw=4 : */
11 changes: 9 additions & 2 deletions src/gpujpeg_common_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ struct gpujpeg_timer {
#ifdef GPUJPEG_USE_CUDA
cudaEvent_t start;
cudaEvent_t stop;
#endif
#else
float start;
float stop;
#endif
};

#ifdef GPUJPEG_USE_CUDA
Expand Down Expand Up @@ -147,7 +150,8 @@ struct gpujpeg_timer {
*
* @param name
*/
#define GPUJPEG_CUSTOM_TIMER_DURATION(name) 0
#define GPUJPEG_CUSTOM_TIMER_DURATION(name) \
(name).started == 1 ? gpujpeg_custom_timer_get_duration((name).start, (name).stop) : (name).started == 0 ? 0 : ( fprintf(stderr, "Debug timer disabled!\n"), 0)
#endif

#ifdef __cplusplus
Expand Down Expand Up @@ -463,6 +467,9 @@ gpujpeg_image_parameters_equals(const struct gpujpeg_image_parameters *p1 , cons
#ifdef GPUJPEG_USE_CUDA
float
gpujpeg_custom_timer_get_duration(cudaEvent_t start, cudaEvent_t stop);
#else
float
gpujpeg_custom_timer_get_duration(float start, float stop);
#endif

#ifdef __cplusplus
Expand Down
23 changes: 21 additions & 2 deletions src/gpujpeg_dct_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ gpujpeg_idct_cpu(struct gpujpeg_decoder* decoder)
cudaMemcpy(component->data_quantized, component->d_data_quantized, component->data_size * sizeof(uint16_t), cudaMemcpyDeviceToHost);
#else
// TODO: NEED IMPLEMENTATION
#endif
memcpy(component->data_quantized, component->d_data_quantized, component->data_size * sizeof(uint16_t));
#endif



Expand Down Expand Up @@ -257,6 +258,24 @@ gpujpeg_idct_cpu(struct gpujpeg_decoder* decoder)
cudaFreeHost(data);
#else
// TODO: NEED IMPLEMENTATION
#endif
data = (uint8_t*)malloc(component->data_size * sizeof(uint8_t));
for ( int y = 0; y < height; y++ ) {
for ( int x = 0; x < width; x++ ) {
for ( int c = 0; c < (GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE); c++ ) {
int coefficient_index = (y * width + x) * (GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE) + c;
int16_t coefficient = component->data_quantized[coefficient_index];
coefficient += 128;
if ( coefficient > 255 )
coefficient = 255;
if ( coefficient < 0 )
coefficient = 0;
int index = ((y * GPUJPEG_BLOCK_SIZE) + (c / GPUJPEG_BLOCK_SIZE)) * component->data_width + ((x * GPUJPEG_BLOCK_SIZE) + (c % GPUJPEG_BLOCK_SIZE));
data[index] = coefficient;
}
}
}
memcpy(component->d_data, data, component->data_size * sizeof(uint8_t));
free(data);
#endif
}
}
Loading

0 comments on commit 89878b2

Please sign in to comment.