⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpege_context.c

📁 motion Jpeg 在SPI DSP平台优化好的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
				derive_huffman_table (&p_jenc->ac_huff_tbl[table_index], &p_jenc->comp_info[i].d_ac_huff_tbl);			}		}        break;    default:        // should not get here. TODO: better error handling        spi_printf ("table_type is wrong in jpege_add_huff_table\n");        exit (1);    }}void set_default_tables(    JPEGE_CONTEXT *p_jenc){    // luma quant table    set_quant_table (&p_jenc->quant_tbl[0],  std_luma_quant);    scale_quant_table (&p_jenc->quant_tbl[0], p_jenc->quant_scale_factor);    // chroma quant table    set_quant_table (&p_jenc->quant_tbl[1], std_chroma_quant);    scale_quant_table (&p_jenc->quant_tbl[1], p_jenc->quant_scale_factor);    // dc luma table    set_huffman_table (&p_jenc->dc_huff_tbl[0], std_dc_luma_bits, std_dc_luma_values);    // dc chroma table    set_huffman_table (&p_jenc->dc_huff_tbl[1], std_dc_chroma_bits, std_dc_chroma_values);    // ac luma table    set_huffman_table (&p_jenc->ac_huff_tbl[0], std_ac_luma_bits, std_ac_luma_values);    // ac chroma table    set_huffman_table (&p_jenc->ac_huff_tbl[1], std_ac_chroma_bits, std_ac_chroma_values);}JPEGE_STATUS jpege_set_default(    JPEGE_CONTEXT *p_jenc){	JPEGE_STATUS status = STATUS_SUCCESS;/* Input format related */	p_jenc->input_format		= DEFAULT_INPUT_FORMAT;    switch (p_jenc->input_format)    {                               // index, id,  xscale, yscale, qtbl, dc_huff, ac_huff    case YUV444_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 1;        p_jenc->max_v_sample_factor = 1;        break;    case YUV422_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 2;        p_jenc->max_v_sample_factor = 1;        break;    case YUV420_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 2;        p_jenc->max_v_sample_factor = 2;        break;    case IMAGE_FORMAT_UNKNOWN:    default:        SPI_ASSERT (1);        break;	}/* Image size related */	p_jenc->encode_width		= DEFAULT_WIDTH;	p_jenc->encode_height		= DEFAULT_HEIGHT;/* Tables */	p_jenc->quant_scale_factor	= DEFAULT_QUANT_SCALE;    set_default_tables (p_jenc);    p_jenc->MCUs_x = p_jenc->comp_info[2].width_in_blocks;    p_jenc->MCUs_y = p_jenc->comp_info[2].height_in_blocks;    p_jenc->num_scans = DEFAULT_SCANs;    p_jenc->comps_in_scan = DEFAULT_COMPONENTS_IN_SCAN;    p_jenc->p_scan_info = NULL;    p_jenc->data_precision = DEFAULT_PRECISION;    p_jenc->restart_interval = 0;    p_jenc->restart_in_rows = 0;    /* Fill in default JFIF marker parameters.  Note that whether the marker    * will actually be written is determined by jpeg_set_colorspace.    *    * By default, the library emits JFIF version code 1.01.    * An application that wants to emit JFIF 1.02 extension markers should set    * JFIF_minor_version to 2.  We could probably get away with just defaulting    * to 1.02, but there may still be some decoders in use that will complain    * about that; saying 1.01 should minimize compatibility problems.    */    p_jenc->jfif_info.major_version = 1;    // Default JFIF version = 1.01    p_jenc->jfif_info.minor_version = 1;    p_jenc->jfif_info.density_unit = 0;	    // Pixel size is unknown by default    p_jenc->jfif_info.x_density = 1;		// Pixel aspect ratio is square by default    p_jenc->jfif_info.y_density = 1;/* Initialise component information */    switch (p_jenc->input_format)    {                                       // index,   id,  xscale, yscale, qtbl, dc_huff, ac_huff    case YUV444_PLANAR:        status |= jpege_set_component (p_jenc,    0,     1,    1,      1,      0,     0,     0);             status |= jpege_set_component (p_jenc,    1,     2,    1,      1,      1,     1,     1);        status |= jpege_set_component (p_jenc,    2,     3,    1,      1,      1,     1,     1);        break;    case YUV422_PLANAR:        status |= jpege_set_component (p_jenc,    0,     1,    2,      1,      0,     0,     0);        status |= jpege_set_component (p_jenc,    1,     2,    1,      1,      1,     1,     1);        status |= jpege_set_component (p_jenc,    2,     3,    1,      1,      1,     1,     1);        break;    case YUV420_PLANAR:        status |= jpege_set_component (p_jenc,    0,     1,    2,      2,      0,     0,     0);        status |= jpege_set_component (p_jenc,    1,     2,    1,      1,      1,     1,     1);        status |= jpege_set_component (p_jenc,    2,     3,    1,      1,      1,     1,     1);        break;    default:        break;    }        return status; }//////////////////////////////////////////////////////////////////void jpege_set_qp (    JPEGE_CONTEXT *p_jenc,    int qp)//// Description:		This is the same quality to scale_factor translation as the IJG code.//////////////////////////////////////////////////////////////////{    int i;    p_jenc->quant_scale_factor = qp;    if (p_jenc->quant_scale_factor == 0)    {        p_jenc->quant_scale_factor = 1;    }        for (i = 0; i < p_jenc->num_components; i++)    {        scale_quant_table (&p_jenc->quant_tbl[p_jenc->comp_info[i].quant_tbl_num], p_jenc->quant_scale_factor);    }}void jpege_put_all_tables(	JPEGE_CONTEXT *p_jenc){	int i;	for (i=0; i<MAX_NUM_QUANT_TBLS; i++)	{		p_jenc->quant_tbl[i].table_sent = false;	}	for (i=0; i<MAX_NUM_DC_HUFF_TBLS; i++)	{		p_jenc->dc_huff_tbl[i].table_sent = false;	}	for (i=0; i<MAX_NUM_AC_HUFF_TBLS; i++)	{		p_jenc->ac_huff_tbl[i].table_sent = false;	}}void jpege_set_component_formats(    JPEGE_CONTEXT *p_jenc,	IMAGE_FORMAT in_format){/* Input format related */	p_jenc->input_format = in_format;    switch (p_jenc->input_format)    {                               // index, id,  xscale, yscale, qtbl, dc_huff, ac_huff    case YUV444_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 1;        p_jenc->max_v_sample_factor = 1;	    p_jenc->comp_info[0].h_samp_factor = 1;		p_jenc->comp_info[0].v_samp_factor = 1;	    p_jenc->comp_info[1].h_samp_factor = 1;		p_jenc->comp_info[1].v_samp_factor = 1;	    p_jenc->comp_info[2].h_samp_factor = 1;		p_jenc->comp_info[2].v_samp_factor = 1;        break;    case YUV422_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 2;        p_jenc->max_v_sample_factor = 1;	    p_jenc->comp_info[0].h_samp_factor = 2;		p_jenc->comp_info[0].v_samp_factor = 1;	    p_jenc->comp_info[1].h_samp_factor = 1;		p_jenc->comp_info[1].v_samp_factor = 1;	    p_jenc->comp_info[2].h_samp_factor = 1;		p_jenc->comp_info[2].v_samp_factor = 1;        break;    case YUV420_PLANAR:	    p_jenc->num_components		= 3;        p_jenc->max_h_sample_factor = 2;        p_jenc->max_v_sample_factor = 2;	    p_jenc->comp_info[0].h_samp_factor = 2;		p_jenc->comp_info[0].v_samp_factor = 2;	    p_jenc->comp_info[1].h_samp_factor = 1;		p_jenc->comp_info[1].v_samp_factor = 1;	    p_jenc->comp_info[2].h_samp_factor = 1;		p_jenc->comp_info[2].v_samp_factor = 1;        break;    case IMAGE_FORMAT_UNKNOWN:    default:        SPI_ASSERT (1);        break;	}}JPEGE_STATUS jpege_set_component_sizes(    JPEGE_CONTEXT *p_jenc){	JPEGE_STATUS status = STATUS_SUCCESS;    JPEGE_COMPONENT_INFO *p_comp_info;    int component_index, i;    int num_blocks, padded_num_blocks;    int xscale, yscale;    int s_width, s_height;	for (component_index=0; component_index<p_jenc->num_components; component_index++)	{	    p_comp_info = &p_jenc->comp_info[component_index];		xscale = p_jenc->max_h_sample_factor / p_comp_info->h_samp_factor;		yscale = p_jenc->max_v_sample_factor / p_comp_info->v_samp_factor;		s_width = (p_jenc->encode_width + xscale - 1) / xscale;		s_height = (p_jenc->encode_height + yscale - 1) / yscale;		p_comp_info->scaled_width = (s_width + BLOCK_WIDTH - 1) & ~(BLOCK_WIDTH - 1);		p_comp_info->scaled_height = (s_height + BLOCK_HEIGHT - 1) & ~(BLOCK_HEIGHT - 1);		p_comp_info->actual_width = (s_width);		p_comp_info->actual_height = (s_height);		p_comp_info->width_in_blocks = p_comp_info->scaled_width / BLOCK_WIDTH;		p_comp_info->height_in_blocks = p_comp_info->scaled_height / BLOCK_WIDTH;		num_blocks = p_comp_info->width_in_blocks * p_comp_info->height_in_blocks;		p_comp_info->num_blocks = num_blocks;		// For efficient parallel operation, pad the memory buffers to a multiple of SPI_LANES. 		padded_num_blocks = ((p_comp_info->height_in_blocks + SPI_LANES - 1) & ~(SPI_LANES - 1))  * p_comp_info->width_in_blocks;		p_comp_info->padded_num_blocks = padded_num_blocks;		if ((p_comp_info->p_bit_buffers = (BIT_BUFFER *) spi_malloc (padded_num_blocks * sizeof (BIT_BUFFER))) == NULL)		{			spi_printf ("\n Malloc for p_bit_buffers for %d component failed.\n",component_index);			return STATUS_FAIL;		}		if ((p_comp_info->p_mem_buffer = (unsigned char *) spi_malloc (padded_num_blocks * BLOCK_BIT_BUFFER_SIZE)) == NULL)		{			spi_printf ("\n Malloc for p_mem_buffer for %d component failed.\n",component_index);			return STATUS_FAIL;		}		for (i = 0; i < num_blocks; i++)		{			p_comp_info->p_bit_buffers[i].buffer_size = BLOCK_BIT_BUFFER_SIZE;			p_comp_info->p_bit_buffers[i].p_buffer = p_comp_info->p_mem_buffer + i * BLOCK_BIT_BUFFER_SIZE;			reset_bit_buffer (&p_comp_info->p_bit_buffers[i]);		}	}    return status; }void jpege_free_component_input_buffers (JPEGE_CONTEXT *p_jenc){    int i;    for (i = 0; i < p_jenc->num_components; i++)    {		if (p_jenc->input_buffer_allocated[i] == true)		{			spi_free (p_jenc->p_input_buffer[i]);		}		p_jenc->p_input_buffer[i] = NULL;    }}void jpege_free_component_tables (JPEGE_CONTEXT *p_jenc){    int i;    JPEGE_COMPONENT_INFO *p_comp;    for (i = 0; i < p_jenc->num_components; i++)    {        p_comp = &p_jenc->comp_info[i];        spi_free (p_comp->d_ac_huff_tbl.code_length);        spi_free (p_comp->d_ac_huff_tbl.code_word);        spi_free (p_comp->d_dc_huff_tbl.code_length);        spi_free (p_comp->d_dc_huff_tbl.code_word);		p_comp->d_ac_huff_tbl.code_length = NULL;		p_comp->d_ac_huff_tbl.code_word = NULL;		p_comp->d_dc_huff_tbl.code_length = NULL;		p_comp->d_dc_huff_tbl.code_word = NULL;    }}void jpege_free_component_bit_buffers(	JPEGE_CONTEXT *p_jenc){	int i;    for (i = 0; i < p_jenc->num_components; i++)    {		spi_free(p_jenc->comp_info[i].p_bit_buffers);		spi_free(p_jenc->comp_info[i].p_mem_buffer);		p_jenc->comp_info[i].p_bit_buffers = NULL;		p_jenc->comp_info[i].p_mem_buffer = NULL;	}}void copy_and_pad_image (	unsigned char *p_inp, 	int inp_width, 	int inp_height, 	unsigned char *p_out, 	int pad_width, 	int pad_height){	int i,j;	unsigned char *p_tmp, *p_last;	p_last = p_out;	for (i=0; i<inp_height; i++)	{		p_last = p_out;		for (j=0; j<inp_width; j++)		{			*p_out++ = *p_inp++;		}		for (j=inp_width; j<pad_width; j++)		{			*p_out++ = *(p_inp-1);		}	}	for (i=inp_height; i<pad_height; i++)	{		p_tmp = p_last;		for (j=0; j<pad_width; j++)		{			*p_out++ = *p_tmp++;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -