📄 test_jpege_host.c
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////////// test_jpege_host.c (JPEG Encoder Test Harness for host)//// 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"////////////////////////////////////////////////////////////////////////////////// Constants////////////////////////////////////////////////////////////////////////////////#define ERROR (-1)#define SUCCESS (0)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; char *g_out_filename; int g_frames_to_skip; int g_number_of_frames; unsigned char *g_p_ybuf; unsigned char *g_p_ubuf; unsigned char *g_p_vbuf;#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; char out_filename[100]; int frame_no; #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; int frame_loop_cnt; #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) frame_no = g_frames_to_skip; #endif settings.format = SPVEJPEG_YUV420_PLANAR; settings.quality = 75; image_width = g_input_image_width; image_height = g_input_image_height; p_buffer_y = g_p_ybuf; p_buffer_u = g_p_ubuf; p_buffer_v = g_p_vbuf; // 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; } for (frame_loop_cnt=0; frame_loop_cnt<g_number_of_frames; frame_loop_cnt++) { #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) sprintf(out_filename, "%s%03d%s",g_out_filename,frame_no,".jpg"); output_file = fopen(out_filename, "wb"); if(output_file == NULL) { printf("Can't open output file\n"); return -1; } printf("Frame - %2d written to %s\n", (frame_loop_cnt+1), out_filename); frame_no++; #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; // write bitstream to jpeg file #if !defined(SPI_TARGET_DEVICE) && !defined(SPI_TARGET_TCS) if (output_file) { tmp = fwrite (p_bit_buffer, 1, num_bytes, output_file); if ( tmp != num_bytes) { printf ("Error writing to jpeg file\n"); return 1; } fclose(output_file); } #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 (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 ("\nComparing 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(void){ 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 print_usage(void){ printf("\n"); printf("\n"); printf("\nArguments: \n"); printf(" -? or --help : print this help message\n"); printf(" -d or --default : print default settings\n"); printf(" -y or --yuv_file _string_ : inp.yuv : YUV (input) filename in case check_result is disabled.\n"); printf(" -j or --jpeg_file _string_ : out00.jpg : JPEG (output) bitstream filename\n"); printf(" -w or --width _num_ : 720 : Width of yuv frame \n"); printf(" -h or --height _num_ : 480 : Height of yuv frame \n"); printf(" -s or --skip_count _num_ : 0 : Number of frames to be skipped from yuv file \n"); printf(" -n or --no_of_frames _num_ : 1 : Number of frames to be encoded. \n"); printf(" -c or --check_result _string_ : on : 'on/off' Select whether the output file should be compared with reference file. \n"); printf(" @ _string_ : : configuration file \n"); printf("\n"); printf(" Note: 1. If 'check_result' is enabled, yuv frame part of the binary is used instead of from 'yuv_file' \n"); printf(" and output is compared with reference jpeg which is also part of the binary.\n"); printf(" 2. Different output files are generated for each frame. 'nnn.jpg' is appended to 'jpeg_file'. \n"); printf(" where 'nnn' is the frame number starting from 'skip_count'.\n"); printf("\n"); printf("\n");}void set_default_arguments(void){ g_in_filename = "inp.yuv"; g_out_filename = "out"; g_input_image_width = 720; g_input_image_height = 480; g_frames_to_skip = 0; g_number_of_frames = 1; g_compare_output = 1; g_p_ybuf = y_frame_data; g_p_ubuf = u_frame_data; g_p_vbuf = v_frame_data;}void print_default_arguments(void){ if (g_compare_output) { printf("\nYUV input part of the binary"); } else { printf("\nYUV input from '%s' file - '%d' frame", g_in_filename, (g_frames_to_skip+1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -