📄 test_jpege_device.c
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////////// test_jpege_device.c (JPEG Encoder Test Harness for device)//// Notice: COPYRIGHT (C) STREAM PROCESSORS, INC. 2005-2007// THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE SPI// END-USER LICENSE AGREEMENT (EULA). THE PROGRAM MAY ONLY// BE USED IN A MANNER EXPLICITLY SPECIFIED IN THE EULA,// WHICH INCLUDES LIMITATIONS ON COPYING, MODIFYING,// REDISTRIBUTION AND WARANTIES. UNAUTHORIZED USE OF THIS// PROGRAM IS STRICTLY PROHIBITED. YOU MAY OBTAIN A COPY OF// THE EULA FROM WWW.STREAMPROCESSORS.COM. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #includes ////////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include <string.h>#include <stdlib.h>#include "spi_common.h"#include "spvejpeg.h"extern int g_input_image_width;extern int g_input_image_height;extern unsigned char y_frame_data[];extern unsigned char u_frame_data[];extern unsigned char v_frame_data[];extern int output_size;extern unsigned char output_data[];int g_compare_output = 1;#if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) char *g_in_filename = "inp.yuv"; char *g_out_filename = "out.jpg"; int g_frames_to_skip = 0;#endiftypedef struct MY_JPEGE_SETTINGS{ SPVEJPEG_IMAGE_FORMAT format; int quality; // quality 0 - 100 // If bother restart0 and restart1 are zeros, no restart marker. If both are non-zero, restart0 is used.} MY_JPEGE_SETTINGS;//------------------------------------------------------------------// Description:// Example application that uses the SPI JPEG encoder library.//------------------------------------------------------------------int jpeg_encode_mem_to_mem (){ void *p_encoder; #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) int tmp; FILE *output_file; #endif MY_JPEGE_SETTINGS settings; SPVEJPEG_IMAGE_SPECS input_image_spec; SPVEJPEG_BITSTREAM_BUFFER bitstream_buffer; unsigned char *p_bit_buffer; int bit_buffer_size; int num_bytes; int image_width, image_height; unsigned char *p_buffer_y, *p_buffer_u, *p_buffer_v; int status = 0; int mem_allocated_y = 0; int mem_allocated_uv = 0;#if (defined(PROFILE_SPVEJPEG) && (defined(SPI_TARGET_DEVICE))) SPI_PERF_T spvejpeg_timer ; spi_perf_init (&spvejpeg_timer , "spvejpeg_timer") ;#endif #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) output_file = fopen(g_out_filename, "wb"); if(output_file == NULL) { printf("Can't open output file\n"); return -1; } #endif settings.format = SPVEJPEG_YUV420_PLANAR; settings.quality = 75; image_width = g_input_image_width; image_height = g_input_image_height; p_buffer_y = y_frame_data; p_buffer_u = u_frame_data; p_buffer_v = v_frame_data; // allocate buffer for jpeg encoder to write bits into bit_buffer_size = (image_width * image_height * 3) >> 1; p_bit_buffer = (unsigned char *) spi_malloc (bit_buffer_size); if (p_bit_buffer == NULL) { spi_printf("\n Error: Malloc of p_bit_buffer failed. \n"); return -1; } spi_printf ("Opening JPEG encoder\n"); if ( (spvejpeg_open (&p_encoder)) != SPVEJPEG_ERR_SUCCESS) { spi_printf("\n Error: spvejpeg_open failed. \n"); return -1; } if ( (spvejpeg_set_quality (p_encoder, settings.quality)) != SPVEJPEG_ERR_SUCCESS) { spi_printf("\n Error: spvejpeg_set_quality failed. \n"); return -1; } // Do multiple times for multiple frames {#if (defined(PROFILE_SPVEJPEG) && (defined(SPI_TARGET_DEVICE))) spi_perf_start (&spvejpeg_timer) ;#endif input_image_spec.buffer.p_y_buffer = p_buffer_y; input_image_spec.buffer.p_u_buffer = p_buffer_u; input_image_spec.buffer.p_v_buffer = p_buffer_v; input_image_spec.buffer.frame_width = image_width; input_image_spec.buffer.frame_height = image_height; input_image_spec.width = image_width; input_image_spec.height = image_height; input_image_spec.x_offset = 0; input_image_spec.y_offset = 0; input_image_spec.format = settings.format; if ( (spvejpeg_prep_encode_frame (p_encoder, &input_image_spec)) != SPVEJPEG_ERR_SUCCESS) { spi_printf("\n Error: spvejpeg_prep_encode_frame failed. \n"); return -1; } bitstream_buffer.p_buffer = p_bit_buffer; bitstream_buffer.total_num_bytes = bit_buffer_size; bitstream_buffer.used_num_bytes = 0; if ( (spvejpeg_encode_frame (p_encoder, &bitstream_buffer)) != SPVEJPEG_ERR_SUCCESS) { spi_printf("\n Error: spvejpeg_encode_frame failed. \n"); return -1; } num_bytes = bitstream_buffer.used_num_bytes; #if (defined(PROFILE_SPVEJPEG) && (defined(SPI_TARGET_DEVICE))) spi_perf_stop (&spvejpeg_timer) ; spi_perf_print (&spvejpeg_timer, 1);#endif // write bitstream to jpeg file #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) tmp = fwrite (p_bit_buffer, 1, num_bytes, output_file); if ( tmp != num_bytes) { printf ("Error writing to jpeg file\n"); return 1; } #endif } spi_printf("Closing JPEG encoder\n"); if ( (spvejpeg_close (p_encoder)) != SPVEJPEG_ERR_SUCCESS) { spi_printf("\n Error: spvejpeg_close failed. \n"); return -1; } #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) if (output_file) { fclose(output_file); } #endif if (mem_allocated_y) { spi_free (p_buffer_y); } if (mem_allocated_uv) { spi_free (p_buffer_u); spi_free (p_buffer_v); } status = 0; if (g_compare_output) { spi_printf ("Comparing results with known data ...\n"); { int i; if (num_bytes != output_size) { spi_printf ("Different size. Failed\n"); status = 1; } if (status == 0) { for (i = 0; i < output_size; i++) { if (p_bit_buffer[i] != output_data[i]) { spi_printf ("Byte #%d is different. Failed", i); status = 1; } } } if (status == 0) { spi_printf ("Compare Successful!\n"); } } } if (p_bit_buffer) spi_free(p_bit_buffer); return status;}int dspmain(){ return jpeg_encode_mem_to_mem();}void dspmain_task(void){ jpeg_encode_mem_to_mem();}#if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS)void parse_command_line_args(int argc, char *argv[]){ int args_processed = 1; g_in_filename = argv[args_processed]; if (++args_processed >= argc) return; g_out_filename = argv[args_processed]; if (++args_processed >= argc) return; g_input_image_width = atoi(argv[args_processed]); if (++args_processed >= argc) return; g_input_image_height = atoi(argv[args_processed]); if (++args_processed >= argc) return; g_frames_to_skip = atoi(argv[args_processed]); if (++args_processed >= argc) return;}int process_command_line_args(void){ FILE *input_file; int skip_size; int ysize, uvsize; input_file = fopen(g_in_filename, "rb"); if(input_file == NULL) { printf("Can't open input file\n"); return -1; } skip_size = (g_input_image_width * g_input_image_height * 3 * g_frames_to_skip) >> 1; if ((fseek(input_file, skip_size, SEEK_SET)) == EOF) { printf("Can't fseek input file\n"); return -1; } ysize = g_input_image_width * g_input_image_height; uvsize = (g_input_image_width * g_input_image_height) >> 2; if ((fread(y_frame_data, 1, ysize, input_file)) != ysize) { printf("Can't fread input file\n"); return -1; } if ((fread(u_frame_data, 1, uvsize, input_file)) != uvsize) { printf("Can't fread input file\n"); return -1; } if ((fread(v_frame_data, 1, uvsize, input_file)) != uvsize) { printf("Can't fread input file\n"); return -1; } if (input_file) { fclose(input_file); } return 0;}int main(int argc, char *argv[]){ int exit_code; if (argc > 1) { g_compare_output = 0; parse_command_line_args(argc, argv); if (process_command_line_args() == -1) { return -1; } } spi_printf("\nYUV input from '%s' file - '%d' frame", g_in_filename, (g_frames_to_skip+1)); spi_printf("\nWidth = %d", g_input_image_width); spi_printf("\nHeight = %d", g_input_image_height); spi_printf("\nJPEG output in '%s' file", g_out_filename); spi_printf("\nComparison of output is %s.", (g_compare_output?("enabled"):("disabled"))); spi_printf("\n\n"); exit_code = dspmain(); return exit_code;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -