Skip to content

Commit 89878b2

Browse files
committed
Start implementing some CPU version of missing funtions
Running decode on an image with saving RGB file but missing data Signed-off-by: Anthony Liot <[email protected]>
1 parent cbd8a3b commit 89878b2

5 files changed

+237
-39
lines changed

src/gpujpeg_common.c

+121-12
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ gpujpeg_get_devices_info(void)
160160
#endif
161161
}
162162
#else
163-
// TODO: NEED IMPLEMENTATION
163+
// TODO: NEED IMPLEMENTATION
164+
printf("[WARNING] gpujpeg_get_devices_info(): NOT YET IMPLEMENTED\n");
164165
#endif
165166
return devices_info;
166167
}
@@ -261,7 +262,8 @@ gpujpeg_init_device(int device_id, int flags)
261262
return -1;
262263
}
263264
#else
264-
// TODO: NEED IMPLEMENTATION
265+
// TODO: NEED IMPLEMENTATION
266+
printf("[WARNING] gpujpeg_init_device(): NOT YET IMPLEMENTED\n");
265267
#endif
266268
return 0;
267269
}
@@ -440,6 +442,7 @@ void gpujpeg_set_device(int index)
440442
cudaSetDevice(index);
441443
#else
442444
// TODO: NEED IMPLEMENTATION
445+
printf("[WARNING] gpujpeg_set_device(): NOT YET IMPLEMENTED\n");
443446
#endif
444447
}
445448

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

469473
/* Documented at declaration */
@@ -486,7 +490,8 @@ gpujpeg_component_print16(struct gpujpeg_component* component, int16_t* d_data)
486490
cudaFreeHost(data);
487491
#else
488492
// TODO: NEED IMPLEMENTATION
489-
#endif
493+
printf("[WARNING] gpujpeg_component_print16(): NOT YET IMPLEMENTED\n");
494+
#endif
490495
}
491496

492497
/* Documented at declaration */
@@ -508,6 +513,7 @@ gpujpeg_coder_init(struct gpujpeg_coder * coder)
508513
}
509514
#else
510515
// TODO: NEED IMPLEMENTATION
516+
printf("[WARNING] gpujpeg_coder_init(): NOT YET IMPLEMENTED\n");
511517
#endif
512518
// Initialize coder for no image
513519
coder->param.quality = -1;
@@ -594,6 +600,18 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
594600
gpujpeg_cuda_check_error("Coder color component device allocation", return 0);
595601
#else
596602
// TODO: NEED IMPLEMENTATION
603+
// (Re)allocate color components in host memory
604+
if (coder->component != NULL) {
605+
free(coder->component);
606+
coder->component = NULL;
607+
}
608+
coder->component = (struct gpujpeg_component*)malloc(param_image->comp_count * sizeof(struct gpujpeg_component));
609+
// (Re)allocate color components in device memory
610+
if (coder->d_component != NULL) {
611+
free(coder->d_component);
612+
coder->d_component = NULL;
613+
}
614+
coder->d_component = (struct gpujpeg_component*)malloc(param_image->comp_count * sizeof(struct gpujpeg_component));
597615
#endif
598616
coder->component_allocated_size = param_image->comp_count;
599617
}
@@ -738,6 +756,19 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
738756
gpujpeg_cuda_check_error("Coder segment device allocation", return 0);
739757
#else
740758
// TODO: NEED IMPLEMENTATION
759+
// (Re)allocate segments in host memory
760+
if (coder->segment != NULL) {
761+
free(coder->segment);
762+
coder->segment = NULL;
763+
}
764+
coder->segment = (struct gpujpeg_segment*) malloc(coder->segment_count * sizeof(struct gpujpeg_segment));
765+
766+
// (Re)allocate segments in device memory
767+
if (coder->d_segment != NULL) {
768+
free(coder->d_segment);
769+
coder->d_segment = NULL;
770+
}
771+
coder->d_segment = (struct gpujpeg_segment*) malloc(coder->segment_count * sizeof(struct gpujpeg_segment));
741772
#endif
742773
coder->segment_allocated_size = coder->segment_count;
743774
}
@@ -870,6 +901,27 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
870901
gpujpeg_cuda_check_error("Coder quantized data device allocation", return 0);
871902
#else
872903
// TODO: NEED IMPLEMENTATION
904+
// (Re)allocate preprocessor data in device memory
905+
if (coder->d_data != NULL) {
906+
free(coder->d_data);
907+
coder->d_data = NULL;
908+
}
909+
coder->d_data = (uint8_t*) malloc((coder->data_size + idct_overhead) * sizeof(uint8_t));
910+
911+
// (Re)allocated DCT and quantizer data in host memory
912+
if (coder->data_quantized != NULL) {
913+
free(coder->data_quantized);
914+
coder->data_quantized = NULL;
915+
}
916+
coder->data_quantized = (int16_t*) malloc(coder->data_size * sizeof(int16_t));
917+
918+
// (Re)allocated DCT and quantizer data in device memory
919+
if (coder->d_data_quantized != NULL) {
920+
free(coder->d_data_quantized);
921+
coder->d_data_quantized = NULL;
922+
}
923+
924+
coder->d_data_quantized = (int16_t*) malloc((coder->data_size + idct_overhead) * sizeof(int16_t));
873925
#endif
874926
coder->data_allocated_size = coder->data_size + idct_overhead;
875927
}
@@ -882,7 +934,8 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
882934
gpujpeg_cuda_check_error("d_data memset failed", return 0);
883935
#else
884936
// TODO: NEED IMPLEMENTATION
885-
#endif
937+
memset(coder->d_data, 0, coder->data_size * sizeof(uint8_t));
938+
#endif
886939
}
887940

888941
// Set data buffer to color components
@@ -934,6 +987,26 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
934987
gpujpeg_cuda_check_error("Huffman temp buffer device allocation", return 0);
935988
#else
936989
// TODO: NEED IMPLEMENTATION
990+
// (Re)allocate huffman coder data in host memory
991+
if (coder->data_compressed != NULL) {
992+
free(coder->data_compressed);
993+
coder->data_compressed = NULL;
994+
}
995+
coder->data_compressed = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));
996+
997+
// (Re)allocate huffman coder data in device memory
998+
if (coder->d_data_compressed != NULL) {
999+
free(coder->d_data_compressed);
1000+
coder->d_data_compressed = NULL;
1001+
}
1002+
coder->d_data_compressed = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));
1003+
1004+
// (Re)allocate Huffman coder temporary buffer
1005+
if (coder->d_temp_huffman != NULL) {
1006+
free(coder->d_temp_huffman);
1007+
coder->d_temp_huffman = NULL;
1008+
}
1009+
coder->d_temp_huffman = (uint8_t*)malloc(max_compressed_data_size * sizeof(uint8_t));
9371010
#endif
9381011
coder->data_compressed_allocated_size = max_compressed_data_size;
9391012
}
@@ -965,6 +1038,19 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
9651038
gpujpeg_cuda_check_error("Coder block list device allocation", return 0);
9661039
#else
9671040
// TODO: NEED IMPLEMENTATION
1041+
// (Re)allocate list of block indices in host memory
1042+
if (coder->block_list != NULL) {
1043+
free(coder->block_list);
1044+
coder->block_list = NULL;
1045+
}
1046+
coder->block_list = (uint64_t*)malloc(coder->block_count * sizeof(*coder->block_list));
1047+
1048+
// (Re)allocate list of block indices in device memory
1049+
if (coder->d_block_list != NULL) {
1050+
free(coder->d_block_list);
1051+
coder->d_block_list = NULL;
1052+
}
1053+
coder->d_block_list = (uint64_t*)malloc(coder->block_count * sizeof(*coder->d_block_list));
9681054
#endif
9691055
coder->block_allocated_size = coder->block_count;
9701056
}
@@ -1041,7 +1127,15 @@ gpujpeg_coder_init_image(struct gpujpeg_coder * coder, const struct gpujpeg_para
10411127
cudaMemcpyAsync(coder->d_segment, coder->segment, coder->segment_count * sizeof(struct gpujpeg_segment), cudaMemcpyHostToDevice, stream);
10421128
gpujpeg_cuda_check_error("Coder segment copy", return 0);
10431129
#else
1044-
// TODO: NEED IMPLEMENTATION
1130+
// TODO: NEED IMPLEMENTATION
1131+
// Copy components to device memory
1132+
memcpy(coder->d_component, coder->component, coder->param_image.comp_count * sizeof(struct gpujpeg_component));
1133+
1134+
// Copy block lists to device memory
1135+
memcpy(coder->d_block_list, coder->block_list, coder->block_count * sizeof(*coder->d_block_list));
1136+
1137+
// Copy segments to device memory
1138+
memcpy(coder->d_segment, coder->segment, coder->segment_count * sizeof(struct gpujpeg_segment));
10451139
#endif
10461140
coder->allocated_gpu_memory_size = allocated_gpu_memory_size;
10471141

@@ -1098,7 +1192,8 @@ gpujpeg_coder_deinit(struct gpujpeg_coder* coder)
10981192
if ( coder->d_block_list != NULL )
10991193
cudaFree(coder->d_block_list);
11001194
#else
1101-
// TODO: NEED IMPLEMENTATION
1195+
// TODO: NEED IMPLEMENTATION
1196+
printf("[WARNING] gpujpeg_coder_deinit(): NOT YET IMPLEMENTED\n");
11021197
#endif
11031198
GPUJPEG_CUSTOM_TIMER_DESTROY(coder->duration_memory_to, return -1);
11041199
GPUJPEG_CUSTOM_TIMER_DESTROY(coder->duration_memory_from, return -1);
@@ -1143,7 +1238,8 @@ static void *gpujpeg_cuda_malloc_host(size_t size) {
11431238
GPUJPEG_CHECK_EX(cudaMallocHost(&ptr, size), "Could not alloc host pointer", return NULL);
11441239
#else
11451240
// TODO: NEED IMPLEMENTATION
1146-
#endif
1241+
printf("[WARNING] gpujpeg_cuda_malloc_host(): NOT YET IMPLEMENTED\n");
1242+
#endif
11471243
return ptr;
11481244
}
11491245

@@ -1180,7 +1276,12 @@ gpujpeg_image_load_from_file(const char* filename, uint8_t** image, size_t* imag
11801276
}
11811277
#else
11821278
// TODO: NEED IMPLEMENTATION
1183-
#endif
1279+
data = (uint8_t*)malloc(*image_size * sizeof(uint8_t));
1280+
if ( *image_size != fread(data, sizeof(uint8_t), *image_size, file) ) {
1281+
fprintf(stderr, "[GPUJPEG] [Error] Failed to load image data [%zd bytes] from file %s!\n", *image_size, filename);
1282+
return -1;
1283+
}
1284+
#endif
11841285
fclose(file);
11851286

11861287
*image = data;
@@ -1266,7 +1367,8 @@ gpujpeg_image_destroy(uint8_t* image)
12661367
cudaFreeHost(image);
12671368
#else
12681369
// TODO: NEED IMPLEMENTATION
1269-
#endif
1370+
printf("[WARNING] gpujpeg_image_destroy(): NOT YET IMPLEMENTED\n");
1371+
#endif
12701372
return 0;
12711373
}
12721374

@@ -1684,7 +1786,8 @@ gpujpeg_opengl_texture_register(int texture_id, enum gpujpeg_opengl_texture_type
16841786
#endif
16851787
#else
16861788
// TODO: NEED IMPLEMENTATION
1687-
#endif
1789+
printf("[WARNING] gpujpeg_opengl_texture_register(): NOT YET IMPLEMENTED\n");
1790+
#endif
16881791
}
16891792

16901793
/* Documented at declaration */
@@ -1704,6 +1807,7 @@ gpujpeg_opengl_texture_unregister(struct gpujpeg_opengl_texture* texture)
17041807
cudaFreeHost(texture);
17051808
#else
17061809
// TODO: NEED TO BE IMPLEMENTED
1810+
printf("[WARNING] gpujpeg_opengl_texture_unregister(): NOT YET IMPLEMENTED\n");
17071811
#endif
17081812
#else
17091813
(void) texture;
@@ -1751,6 +1855,7 @@ gpujpeg_opengl_texture_map(struct gpujpeg_opengl_texture* texture, size_t* data_
17511855
*data_size = d_data_size;
17521856
#else
17531857
// TODO: NEED TO BE IMPLEMENTED
1858+
printf("[WARNING] gpujpeg_opengl_texture_map(): NOT YET IMPLEMENTED\n");
17541859
(void) data_size;
17551860
GPUJPEG_MISSING_OPENGL(return NULL);
17561861
#endif
@@ -1792,6 +1897,7 @@ gpujpeg_opengl_texture_unmap(struct gpujpeg_opengl_texture* texture)
17921897
#endif
17931898
#else
17941899
// TODO: NEED IMPLEMENTATION
1900+
printf("[WARNING] gpujpeg_opengl_texture_unmap(): NOT YET IMPLEMENTED\n");
17951901
#endif
17961902
}
17971903

@@ -1990,7 +2096,10 @@ float gpujpeg_custom_timer_get_duration(cudaEvent_t start, cudaEvent_t stop) {
19902096
return elapsedTime;
19912097
}
19922098
#else
1993-
// TODO: NEED IMPLEMENTATION
2099+
float gpujpeg_custom_timer_get_duration(float start, float stop) {
2100+
float elapsedTime = NAN;
2101+
return elapsedTime;
2102+
}
19942103
#endif
19952104

19962105
/* vi: set expandtab sw=4 : */

src/gpujpeg_common_internal.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ struct gpujpeg_timer {
7575
#ifdef GPUJPEG_USE_CUDA
7676
cudaEvent_t start;
7777
cudaEvent_t stop;
78-
#endif
78+
#else
79+
float start;
80+
float stop;
81+
#endif
7982
};
8083

8184
#ifdef GPUJPEG_USE_CUDA
@@ -147,7 +150,8 @@ struct gpujpeg_timer {
147150
*
148151
* @param name
149152
*/
150-
#define GPUJPEG_CUSTOM_TIMER_DURATION(name) 0
153+
#define GPUJPEG_CUSTOM_TIMER_DURATION(name) \
154+
(name).started == 1 ? gpujpeg_custom_timer_get_duration((name).start, (name).stop) : (name).started == 0 ? 0 : ( fprintf(stderr, "Debug timer disabled!\n"), 0)
151155
#endif
152156

153157
#ifdef __cplusplus
@@ -463,6 +467,9 @@ gpujpeg_image_parameters_equals(const struct gpujpeg_image_parameters *p1 , cons
463467
#ifdef GPUJPEG_USE_CUDA
464468
float
465469
gpujpeg_custom_timer_get_duration(cudaEvent_t start, cudaEvent_t stop);
470+
#else
471+
float
472+
gpujpeg_custom_timer_get_duration(float start, float stop);
466473
#endif
467474

468475
#ifdef __cplusplus

src/gpujpeg_dct_cpu.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ gpujpeg_idct_cpu(struct gpujpeg_decoder* decoder)
217217
cudaMemcpy(component->data_quantized, component->d_data_quantized, component->data_size * sizeof(uint16_t), cudaMemcpyDeviceToHost);
218218
#else
219219
// TODO: NEED IMPLEMENTATION
220-
#endif
220+
memcpy(component->data_quantized, component->d_data_quantized, component->data_size * sizeof(uint16_t));
221+
#endif
221222

222223

223224

@@ -257,6 +258,24 @@ gpujpeg_idct_cpu(struct gpujpeg_decoder* decoder)
257258
cudaFreeHost(data);
258259
#else
259260
// TODO: NEED IMPLEMENTATION
260-
#endif
261+
data = (uint8_t*)malloc(component->data_size * sizeof(uint8_t));
262+
for ( int y = 0; y < height; y++ ) {
263+
for ( int x = 0; x < width; x++ ) {
264+
for ( int c = 0; c < (GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE); c++ ) {
265+
int coefficient_index = (y * width + x) * (GPUJPEG_BLOCK_SIZE * GPUJPEG_BLOCK_SIZE) + c;
266+
int16_t coefficient = component->data_quantized[coefficient_index];
267+
coefficient += 128;
268+
if ( coefficient > 255 )
269+
coefficient = 255;
270+
if ( coefficient < 0 )
271+
coefficient = 0;
272+
int index = ((y * GPUJPEG_BLOCK_SIZE) + (c / GPUJPEG_BLOCK_SIZE)) * component->data_width + ((x * GPUJPEG_BLOCK_SIZE) + (c % GPUJPEG_BLOCK_SIZE));
273+
data[index] = coefficient;
274+
}
275+
}
276+
}
277+
memcpy(component->d_data, data, component->data_size * sizeof(uint8_t));
278+
free(data);
279+
#endif
261280
}
262281
}

0 commit comments

Comments
 (0)