📄 encoder.c
字号:
/* **************************************************************************** * * MX21 encoder program * * HISTORY * * 31-Aug-04 Aaron Created * *****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include "jpeg_enc_interface.h"/* Get parameters from config file */static int set_jpeg_enc_cfg(jpeg_enc_parameters * params, FILE * fp_cfg){ char buf[100]; //Default parameters memset(params, 0, sizeof(jpeg_enc_parameters)); params->mode = SEQUENTIAL_MODE; params->quality = 75; if(fp_cfg) { //yuv_type fgets(buf, sizeof(buf), fp_cfg); if( !strncmp(buf, "420", 3) ) params->yuv_format = YUV_420_NONINTERLEAVED; else if( !strncmp(buf, "422", 3) ) params->yuv_format = YUV_422_NONINTERLEAVED; else if( !strncmp(buf, "444", 3) ) params->yuv_format = YUV_444_NONINTERLEAVED; else { printf("Wrong yuv_type!\n"); return 1; } //quality fgets(buf, sizeof(buf), fp_cfg); params->quality = strtoul(buf, NULL, 0); //restart_markers fgets(buf, sizeof(buf), fp_cfg); params->restart_markers = strtoul(buf, NULL, 0); //y_width fgets(buf, sizeof(buf), fp_cfg); params->y_width = strtoul(buf, NULL, 0); //y_height fgets(buf, sizeof(buf), fp_cfg); params->y_height = strtoul(buf, NULL, 0); //u_width fgets(buf, sizeof(buf), fp_cfg); params->u_width = strtoul(buf, NULL, 0); //u_height fgets(buf, sizeof(buf), fp_cfg); params->u_height = strtoul(buf, NULL, 0); //v_width fgets(buf, sizeof(buf), fp_cfg); params->v_width = strtoul(buf, NULL, 0); //v_height fgets(buf, sizeof(buf), fp_cfg); params->v_height = strtoul(buf, NULL, 0); if( (params->y_width == 0) || (params->y_height == 0) || (params->u_width == 0) || (params->u_height == 0) || (params->v_width == 0) || (params->v_height == 0) ) { printf("Invalid width or/and height specified for components\n"); return(1); } } return 0;}/* Encoder function to convert yuv data to jpg */int cjpeg(int yuv_ptr, int data_type, int img_width, int img_height, char *_out_file_name, char *_config_file_name){ int y_ptr = 0, u_ptr = 0, v_ptr = 0; int y_size = 0, u_size = 0, v_size = 0; U8 * i_buff; U8 * y_buff; U8 * u_buff; U8 * v_buff; U8 * out_buff_ref; U8 * out_buff; U8 * temp_ptr; U8 return_val; jpeg_enc_parameters * params; FILE * fp_out, * fp_cfg; U16 i; jpeg_enc_object * obj_ptr; U8 number_mem_info; jpeg_enc_memory_info * mem_info; if( (void*)yuv_ptr == NULL ) { printf("Invalid non interleaved input pointer specified\n"); exit(-1); } fp_out = fopen(_out_file_name, "wb"); if( fp_out == NULL ) { printf("Unable to open outputfile specified\n"); exit(-1); } fp_cfg = fopen(_config_file_name, "rb"); if( fp_cfg == NULL ) { printf("Unable to open configfile specified\n"); exit(-1); } /* -------------------------------------------- * Allocate memory for Encoder Object * -------------------------------------------*/ obj_ptr = (jpeg_enc_object *) malloc(sizeof(jpeg_enc_object)); /* -------------------------------------------- * Fill up the parameter structure of JPEG Encoder * -------------------------------------------*/ params = &(obj_ptr->parameters); if( set_jpeg_enc_cfg(params, fp_cfg) ) { printf("Unable to set configure for jpeg encoder\n"); exit(-1); } /* -------------------------------------------- * Allocate memory for Input and Output Buffers * -------------------------------------------*/ if( (params->yuv_format == YUV_444_NONINTERLEAVED) || (params->yuv_format == YUV_420_NONINTERLEAVED) || (params->yuv_format == YUV_422_NONINTERLEAVED) ) { y_size = params->y_width*params->y_height; u_size = params->u_width*params->u_height; v_size = params->v_width*params->v_height; y_buff = (U8 *) malloc(sizeof(U8)*y_size); u_buff = (U8 *) malloc(sizeof(U8)*u_size); v_buff = (U8 *) malloc(sizeof(U8)*v_size); if( params->yuv_format == YUV_420_NONINTERLEAVED ) { y_ptr = yuv_ptr; u_ptr = y_ptr + y_size; v_ptr = u_ptr + u_size; } else if( params->yuv_format == YUV_422_NONINTERLEAVED ) { int y, u, v, m; y_ptr = (int)malloc(y_size); u_ptr = (int)malloc(u_size); v_ptr = (int)malloc(v_size); for(m = 0, y = 0, u = 0, v =0; y < y_size; y += 2, u ++, v ++) { ((char *)y_ptr)[y] = ((char *)yuv_ptr)[m ++]; ((char *)u_ptr)[u] = ((char *)yuv_ptr)[m ++]; ((char *)y_ptr)[y + 1] = ((char *)yuv_ptr)[m ++]; ((char *)v_ptr)[v] = ((char *)yuv_ptr)[m ++]; } } else { int m, y; y_ptr = (int)malloc(y_size); u_ptr = (int)malloc(u_size); v_ptr = (int)malloc(v_size); for(m = 0, y = 0; y < y_size; y ++) { ((char *)y_ptr)[y] = ((char *)yuv_ptr)[m ++]; ((char *)u_ptr)[y] = ((char *)yuv_ptr)[m ++]; ((char *)v_ptr)[y] = ((char *)yuv_ptr)[m ++]; m ++; //skip z } } memcpy(y_buff, (void*)y_ptr, y_size); memcpy(u_buff, (void*)u_ptr, u_size); memcpy(v_buff, (void*)v_ptr, v_size); i_buff = NULL; } else { // Interleaved input not supported now y_buff = NULL; u_buff = NULL; v_buff = NULL; i_buff = NULL; } /* ----------------------------------------------------------- * Read data from YUV buffer to Input Buffers * ----------------------------------------------------------*/ /* Maximum of 6 bpp */ /* Need a copy of outbuff */ /* Allocating the size of the output buffer for 24bpp. There is a chance * that for some very random images for high quality even 24bpp is * not sufficient then the size of the buffer has to be increased furthur */ out_buff_ref = (U8 *) malloc(3*sizeof(U8)*params->y_width*params->y_height + 1000); out_buff = out_buff_ref; /* -------------------------------------------- * QUERY MEMORY REQUIREMENTS * -------------------------------------------*/ return_val = jpeg_enc_query_mem_req(obj_ptr); if(return_val != JPEGENC_ERR_NO_ERROR) { printf("JPEG encoder returned an error when jpeg_enc_query_mem_req was called \n"); printf("Return Val %d\n",return_val); exit(-1); } /* -------------------------------------------- * ALLOCATE MEMORY REQUESTED BY CODEC * -------------------------------------------*/ number_mem_info = obj_ptr->mem_infos.no_entries; for(i = 0; i < number_mem_info; i++) { /* This example code ignores the 'alignment' and * 'memory_type', but some other applications might want * to allocate memory based on them */ mem_info = &(obj_ptr->mem_infos.mem_info[i]); mem_info->memptr = (void *) malloc(mem_info->size); } /* -------------------------------------------- * Call encoder Init routine * -------------------------------------------*/ return_val = jpeg_enc_init(obj_ptr); if(return_val != JPEGENC_ERR_NO_ERROR) { printf("JPEG encoder returned an error when jpeg_enc_init was called \n"); printf("Return Val %d\n",return_val); exit(-1); } /* -------------------------------------------- * CALL JPEG ENCODER. * -------------------------------------------*/ return_val = jpeg_enc_encode_frame(obj_ptr, i_buff, y_buff, u_buff, v_buff, &out_buff); if(return_val != JPEGENC_ERR_NO_ERROR) { printf("JPEG encoder returned an error when jpeg_enc_encode_frame was called \n"); printf("Return Val %d\n",return_val); exit(-1); } /* -------------------------------------------- * FREE MEMORY REQUESTED BY CODEC * -------------------------------------------*/ number_mem_info = obj_ptr->mem_infos.no_entries; for(i = 0; i < number_mem_info; i++) { mem_info = &(obj_ptr->mem_infos.mem_info[i]); free(mem_info->memptr); } /* -------------------------------------------- * Write the Output buffer into a file * -------------------------------------------*/ for(temp_ptr = out_buff_ref; temp_ptr < out_buff; temp_ptr++) { fputc(*temp_ptr,fp_out); } /* -------------------------------------------- * Free the Input and Output buffers * -------------------------------------------*/ if( (params->yuv_format == YUV_444_NONINTERLEAVED) || (params->yuv_format == YUV_420_NONINTERLEAVED) || (params->yuv_format == YUV_422_NONINTERLEAVED) ) { free(y_buff); free(u_buff); free(v_buff); if( (params->yuv_format == YUV_444_NONINTERLEAVED) || (params->yuv_format == YUV_422_NONINTERLEAVED) ) { free((void *)y_ptr); free((void *)u_ptr); free((void *)v_ptr); } } else { //free(i_buff); } free(out_buff_ref); free(obj_ptr); fclose(fp_out); return 1; } long length; FILE *fp;int main(int argc, char **argv){ if(argc < 2) { printf("Usage :%s Infile.\n", argv[0]); printf("Infile : Make sure that infile name is still.YUV.\n"); printf("Notice : Support the format of YUV420 only.\n "); exit( 0 ); } if (!strcmp(argv[1], "still.YUV" )) { fp = fopen("still.YUV", "r+b"); if(fp==NULL) { printf( "file not found!\n" ); exit( -1 ); } else { printf ("File can open!\n" ); fseek(fp, 0L, 2); length=ftell(fp); printf("File size is: %ld.\n", (long)length); fseek(fp, 0L, 0); char *ptr = (char*)malloc( length + 1 ); fread(ptr, length, 1, fp); cjpeg((int)ptr, 0, 640, 480, "./final_jpeg.jpeg", "./jpeg_420.cfg"); free (ptr); printf("Converted image successful!\n"); exit(-1); if(fclose (fp)) { printf ("File is not closed!"); exit(-1); } } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -