static void cleanup_state(struct state_video_compress_jpeg *s) { int frame_idx; for (frame_idx = 0; frame_idx < 2; frame_idx++) { if(s->out[frame_idx]) { int x; for (x = 0; x < (int) s->out[frame_idx]->tile_count; ++x) { free(s->out[frame_idx]->tiles[x].data); } } vf_free(s->out[frame_idx]); } if(s->encoder) gpujpeg_encoder_destroy(s->encoder); }
/** Documented at declaration */ struct gpujpeg_encoder* gpujpeg_encoder_create(struct gpujpeg_parameters* param, struct gpujpeg_image_parameters* param_image) { assert(param_image->comp_count == 1 || param_image->comp_count == 3); assert(param_image->comp_count <= GPUJPEG_MAX_COMPONENT_COUNT); assert(param->quality >= 0 && param->quality <= 100); assert(param->restart_interval >= 0); assert(param->interleaved == 0 || param->interleaved == 1); struct gpujpeg_encoder* encoder = (struct gpujpeg_encoder*) malloc(sizeof(struct gpujpeg_encoder)); if ( encoder == NULL ) return NULL; // Get coder struct gpujpeg_coder* coder = &encoder->coder; // Set parameters memset(encoder, 0, sizeof(struct gpujpeg_encoder)); coder->param_image = *param_image; coder->param = *param; int result = 1; // Create writer encoder->writer = gpujpeg_writer_create(encoder); if ( encoder->writer == NULL ) result = 0; // Initialize coder if ( gpujpeg_coder_init(coder) != 0 ) result = 0; // Init preprocessor if ( gpujpeg_preprocessor_encoder_init(&encoder->coder) != 0 ) { fprintf(stderr, "Failed to init preprocessor!"); 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**)&encoder->table_quantization[comp_type].d_table, 64 * sizeof(uint16_t)) ) result = 0; if ( cudaSuccess != cudaMalloc((void**)&encoder->table_quantization[comp_type].d_table_forward, 64 * sizeof(float)) ) result = 0; } gpujpeg_cuda_check_error("Encoder table allocation", return NULL); // Init quantization tables for encoder for ( int comp_type = 0; comp_type < GPUJPEG_COMPONENT_TYPE_COUNT; comp_type++ ) { if ( gpujpeg_table_quantization_encoder_init(&encoder->table_quantization[comp_type], (enum gpujpeg_component_type)comp_type, coder->param.quality) != 0 ) result = 0; } gpujpeg_cuda_check_error("Quantization init", return NULL); // Init huffman tables for encoder 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 ( gpujpeg_table_huffman_encoder_init(&encoder->table_huffman[comp_type][huff_type], (enum gpujpeg_component_type)comp_type, (enum gpujpeg_huffman_type)huff_type) != 0 ) result = 0; } } gpujpeg_cuda_check_error("Encoder table init", return NULL); // Init huffman encoder if ( gpujpeg_huffman_gpu_encoder_init(encoder) != 0 ) result = 0; if ( result == 0 ) { gpujpeg_encoder_destroy(encoder); return NULL; } // Timers GPUJPEG_CUSTOM_TIMER_CREATE(encoder->def); GPUJPEG_CUSTOM_TIMER_CREATE(encoder->in_gpu); return encoder; }