void * jpeg_compress_init(char * opts) { struct compress_jpeg_state *s; int frame_idx; s = (struct compress_jpeg_state *) malloc(sizeof(struct compress_jpeg_state)); for (frame_idx = 0; frame_idx < 2; frame_idx++) { s->out[frame_idx] = NULL; } s->decoded = NULL; if(opts && strcmp(opts, "help") == 0) { printf("JPEG comperssion usage:\n"); printf("\t-c JPEG[:<quality>]\n"); return NULL; } else if(opts && strcmp(opts, "list_devices") == 0) { printf("CUDA devices:\n"); gpujpeg_print_devices_info(); return NULL; } if(opts) { char *tok, *save_ptr = NULL; gpujpeg_set_default_parameters(&s->encoder_param); tok = strtok_r(opts, ":", &save_ptr); s->encoder_param.quality = atoi(tok); int ret; printf("Initializing CUDA device %d...\n", cuda_device); ret = gpujpeg_init_device(cuda_device, TRUE); if(ret != 0) { fprintf(stderr, "[JPEG] initializing CUDA device %d failed.\n", cuda_device); return NULL; } tok = strtok_r(NULL, ":", &save_ptr); if(tok) { fprintf(stderr, "[JPEG] WARNING: Trailing configuration parameters.\n"); } } else { gpujpeg_set_default_parameters(&s->encoder_param); printf("[JPEG] setting default encode parameters (quality: %d)\n", s->encoder_param.quality ); } s->encoder = NULL; /* not yet configured */ return s; }
struct module * jpeg_compress_init(struct module *parent, char * opts) { struct state_video_compress_jpeg *s; int frame_idx; s = (struct state_video_compress_jpeg *) malloc(sizeof(struct state_video_compress_jpeg)); for (frame_idx = 0; frame_idx < 2; frame_idx++) { s->out[frame_idx] = NULL; } s->decoded = NULL; if(opts && strcmp(opts, "help") == 0) { printf("JPEG comperssion usage:\n"); printf("\t-c JPEG[:<quality>[:<restart_interval>]]\n"); return &compress_init_noerr; } else if(opts && strcmp(opts, "list_devices") == 0) { printf("CUDA devices:\n"); gpujpeg_print_devices_info(); return &compress_init_noerr; } s->restart_interval = -1; gpujpeg_set_default_parameters(&s->encoder_param); if(opts) { parse_fmt(s, opts); } else { printf("[JPEG] setting default encode parameters (quality: %d)\n", s->encoder_param.quality ); } int ret; printf("Initializing CUDA device %d...\n", cuda_devices[0]); ret = gpujpeg_init_device(cuda_devices[0], TRUE); if(ret != 0) { fprintf(stderr, "[JPEG] initializing CUDA device %d failed.\n", cuda_devices[0]); return NULL; } s->encoder = NULL; /* not yet configured */ platform_spin_init(&s->spin); module_init_default(&s->module_data); s->module_data.cls = MODULE_CLASS_DATA; s->module_data.priv_data = s; s->module_data.deleter = jpeg_compress_done; s->module_data.msg_callback = compress_change_callback; module_register(&s->module_data, parent); return &s->module_data; }
/** Documented at declaration */ struct gpujpeg_decoder* gpujpeg_decoder_create() { struct gpujpeg_decoder* decoder = malloc(sizeof(struct gpujpeg_decoder)); if ( decoder == NULL ) return NULL; // Get coder struct gpujpeg_coder* coder = &decoder->coder; // Set parameters memset(decoder, 0, sizeof(struct gpujpeg_decoder)); gpujpeg_set_default_parameters(&coder->param); gpujpeg_image_set_default_parameters(&coder->param_image); coder->param_image.comp_count = 0; coder->param_image.width = 0; coder->param_image.height = 0; coder->param.restart_interval = 0; int result = 1; // Create reader decoder->reader = gpujpeg_reader_create(); if ( decoder->reader == NULL ) result = 0; // Allocate quantization tables in device memory for ( int comp_type = 0; comp_type < GPUJPEG_COMPONENT_TYPE_COUNT; comp_type++ ) { if ( cudaSuccess != cudaMalloc((void**)&decoder->table_quantization[comp_type].d_table, 64 * sizeof(uint16_t)) ) result = 0; } // Allocate huffman tables in device memory for ( int comp_type = 0; comp_type < GPUJPEG_COMPONENT_TYPE_COUNT; comp_type++ ) { for ( int huff_type = 0; huff_type < GPUJPEG_HUFFMAN_TYPE_COUNT; huff_type++ ) { if ( cudaSuccess != cudaMalloc((void**)&decoder->d_table_huffman[comp_type][huff_type], sizeof(struct gpujpeg_table_huffman_decoder)) ) result = 0; } } gpujpeg_cuda_check_error("Decoder table allocation"); // Init huffman encoder if ( gpujpeg_huffman_gpu_decoder_init() != 0 ) result = 0; if ( result == 0 ) { gpujpeg_decoder_destroy(decoder); return NULL; } // Timers GPUJPEG_CUSTOM_TIMER_CREATE(decoder->def); GPUJPEG_CUSTOM_TIMER_CREATE(decoder->in_gpu); return decoder; }
/** Documented at declaration */ void gpujpeg_image_convert(const char* input, const char* output, struct gpujpeg_image_parameters param_image_from, struct gpujpeg_image_parameters param_image_to) { assert(param_image_from.width == param_image_to.width); assert(param_image_from.height == param_image_to.height); assert(param_image_from.comp_count == param_image_to.comp_count); // Load image int image_size = gpujpeg_image_calculate_size(¶m_image_from); uint8_t* image = NULL; if ( gpujpeg_image_load_from_file(input, &image, &image_size) != 0 ) { fprintf(stderr, "[GPUJPEG] [Error] Failed to load image [%s]!\n", input); return; } struct gpujpeg_coder coder; gpujpeg_set_default_parameters(&coder.param); coder.param.color_space_internal = GPUJPEG_RGB; // Initialize coder and preprocessor coder.param_image = param_image_from; assert(gpujpeg_coder_init(&coder) == 0); assert(gpujpeg_preprocessor_encoder_init(&coder) == 0); // Perform preprocessor assert(cudaMemcpy(coder.d_data_raw, image, coder.data_raw_size * sizeof(uint8_t), cudaMemcpyHostToDevice) == cudaSuccess); assert(gpujpeg_preprocessor_encode(&coder) == 0); // Save preprocessor result uint8_t* buffer = NULL; assert(cudaMallocHost((void**)&buffer, coder.data_size * sizeof(uint8_t)) == cudaSuccess); assert(buffer != NULL); assert(cudaMemcpy(buffer, coder.d_data, coder.data_size * sizeof(uint8_t), cudaMemcpyDeviceToHost) == cudaSuccess); // Deinitialize decoder gpujpeg_coder_deinit(&coder); // Initialize coder and postprocessor coder.param_image = param_image_to; assert(gpujpeg_coder_init(&coder) == 0); assert(gpujpeg_preprocessor_decoder_init(&coder) == 0); // Perform postprocessor assert(cudaMemcpy(coder.d_data, buffer, coder.data_size * sizeof(uint8_t), cudaMemcpyHostToDevice) == cudaSuccess); assert(gpujpeg_preprocessor_decode(&coder) == 0); // Save preprocessor result assert(cudaMemcpy(coder.data_raw, coder.d_data_raw, coder.data_raw_size * sizeof(uint8_t), cudaMemcpyDeviceToHost) == cudaSuccess); if ( gpujpeg_image_save_to_file(output, coder.data_raw, coder.data_raw_size) != 0 ) { fprintf(stderr, "[GPUJPEG] [Error] Failed to save image [%s]!\n", output); return; } // Deinitialize decoder gpujpeg_coder_deinit(&coder); }
static void parse_fmt(struct state_video_compress_jpeg *s, char *fmt) { if(fmt) { char *tok, *save_ptr = NULL; gpujpeg_set_default_parameters(&s->encoder_param); tok = strtok_r(fmt, ":", &save_ptr); s->encoder_param.quality = atoi(tok); tok = strtok_r(NULL, ":", &save_ptr); if(tok) { s->restart_interval = atoi(tok); } tok = strtok_r(NULL, ":", &save_ptr); if(tok) { fprintf(stderr, "[JPEG] WARNING: Trailing configuration parameters.\n"); } } }
int main(int argc, char **argv) { struct gpujpeg_parameters param; gpujpeg_set_default_parameters(¶m); struct gpujpeg_image_parameters param_image; param_image.width = 1920; param_image.height = 1080; param_image.comp_count = 3; // (for now, it must be 3) param_image.color_space = GPUJPEG_RGB; // or GPUJPEG_YCBCR_ITU_R or GPUJPEG_YCBCR_JPEG // (default value is GPUJPEG_RGB) param_image.sampling_factor = GPUJPEG_4_4_4; if (gpujpeg_init_device(0, GPUJPEG_VERBOSE)) { printf("Unable to init device\n"); return -1; } struct gpujpeg_encoder* encoder = gpujpeg_encoder_create(¶m, ¶m_image); if (encoder == NULL) return -1; int image_size = 0; uint8_t* image = NULL; //if (gpujpeg_image_load_from_file("input_image.rgb", &image, &image_size) != 0) // return -1; /* uint8_t* encoder_input = NULL; uint8_t* image_compressed = NULL; int image_compressed_size = 0; if (gpujpeg_encoder_encode(encoder, &encoder_input, &image_compressed, &image_compressed_size) != 0 ) return -1; */ return 0; }