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

📄 image.cpp

📁 h264编解码.用C++实现了图像的编解码功能。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "global.h"
#include "image.h"

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <memory.h>

#define max(a, b)  (((a) > (b)) ? (a) : (b)) 
#define min(a, b)  (((a) < (b)) ? (a) : (b)) 

extern int SliceHeader(int num, int type, int qp);
int intra_image(struct img_par *img,struct inp_par *inp);
int inter_image(struct img_par *img,struct inp_par *inp);
extern void Init_intra_mb_pos();
int image(struct img_par *img,struct inp_par *inp)
{
  int size;
  if (img->number%inp->intra_upd == 0)  
    size=intra_image(img,inp); 
  else
  {
	oneforthpix(img);
    size=inter_image(img,inp); 
  }
  return size;
}

int intra_image(struct img_par *img,struct inp_par *inp)
{
	int i,j;
	int mb_width=img->width>>4;
	int mb_height=img->height>>4;

  img->type = INTRA_IMG;   
  img->qp = inp->qp0;  
  
  SliceHeader(img->number, I_SLICE, img->qp);
    
  img->cod_counter = 0;
  img->current_mb_nr = 1;

  //topleft MB
  img->mb_x=0;
  img->mb_y=0;
  img->block_y = 0; 
  img->pix_y   = 0;
  img->pix_c_y = 0;
  img->block_x = 0;           
  img->pix_x   = 0;          
  img->block_c_x = 0;         
  img->pix_c_x   = 0;
  macroblock_intra_topleft(img);
  IntraMb_Chroma(img);
  img->i16offset = (img->imod == INTRA_MB_NEW)? ((img->cbp&15?13:1) + img->i16offset + ((img->cbp&0x30)>>2)) :0;/*yummy*/
  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);

  //top MB
  for (i=1; i <mb_width ; ++i) 
  {
	  img->mb_x = i;
      img->block_x = i<<2;        // luma block           
      img->pix_x   = i<<4;     // luma pixel           
      img->block_c_x = i<<1;    // chroma block         
      img->pix_c_x   = i<<3; // chroma pixel         
      
      img->current_mb_nr++;
	  macroblock_intra_top(img);
	  IntraMb_Chroma(img);
	  img->i16offset = (img->imod == INTRA_MB_NEW)? ((img->cbp&15?13:1) + img->i16offset + ((img->cbp&0x30)>>2)) :0;/*yummy*/
	  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
  }

  for (j=1; j <mb_height; ++j) 
  {
	img->mb_y = j;
    img->block_y = j<<2;  // vertical luma block posision 
    img->pix_y   = j<<4; // vertical luma macroblock posision 
    img->pix_c_y = j<<3;  // vertical chroma macroblock posision
	
	img->mb_x=0;
	img->block_x = 0;           
	img->pix_x   = 0;          
	img->block_c_x = 0;         
	img->pix_c_x   = 0;
	img->current_mb_nr++;
	macroblock_intra_left(img);
	IntraMb_Chroma(img);
	img->i16offset = (img->imod == INTRA_MB_NEW)? ((img->cbp&15?13:1) + img->i16offset + ((img->cbp&0x30)>>2)) :0;/*yummy*/
	WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
    
    for (i=1; i <mb_width-1; ++i) 
    {
	  img->mb_x = i;
      img->block_x = i<<2;        // luma block           
      img->pix_x   = i<<4;     // luma pixel           
      img->block_c_x = i<<1;    // chroma block         
      img->pix_c_x   = i<<3; // chroma pixel         
      
      img->current_mb_nr++;
	  macroblock_intra_other(img);
	  IntraMb_Chroma(img);
	  img->i16offset = (img->imod == INTRA_MB_NEW)? ((img->cbp&15?13:1) + img->i16offset + ((img->cbp&0x30)>>2)) :0;/*yummy*/
	  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
    }	
	img->mb_x = i;
	img->block_x = i<<2;           
	img->pix_x   = i<<4;          
	img->block_c_x = i<<1;         
	img->pix_c_x   = i<<3;
	img->current_mb_nr++;
	macroblock_intra_right(img);
	IntraMb_Chroma(img);
	img->i16offset = (img->imod == INTRA_MB_NEW)? ((img->cbp&15?13:1) + img->i16offset + ((img->cbp&0x30)>>2)) :0;/*yummy*/
	WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
  }
  AlignBits();
  return bb.cExist;
}

int inter_image(struct img_par *img,struct inp_par *inp)
{
  int tot_intra_sad,cost;
  int i,j;
  int mb_width=img->width>>4;
  int mb_height=img->height>>4;
  int Intra_Threshold=2000;

  img->type = INTER_IMG;      
  img->qp = inp->qpN;
  
  Init_intra_mb_pos();

  SliceHeader(img->number, P_SLICE, img->qp);
     
  img->cod_counter = 0;
  img->current_mb_nr = 1;
  //topleft MB
  img->mb_x=0;
  img->mb_y=0;
  img->block_y = 0; 
  img->pix_y   = 0;
  img->pix_c_y = 0;
  img->block_x = 0;           
  img->pix_x   = 0;          
  img->block_c_x = 0;         
  img->pix_c_x   = 0;
  
  cost = Motion_Estimation(img,MAX_VALUE);
  if(cost>Intra_Threshold) tot_intra_sad=macroblock_intra_topleft(img);
  if(cost<=tot_intra_sad) img->imod = INTRA_MB_INTER;
  macroblock_inter(img,tot_intra_sad);
  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);

  //top MB
  for (i=1; i < mb_width ; ++i) 
  {
      img->mb_x=i;
      img->block_x = i<<2;  // luma block           
      img->pix_x   = i<<4;  // luma pixel           
      img->block_c_x = i<<1;// chroma block         
      img->pix_c_x   = i<<3;// chroma pixel       
      
      img->current_mb_nr++;
	  cost = Motion_Estimation(img,MAX_VALUE);
	  if(cost>Intra_Threshold) tot_intra_sad=macroblock_intra_top(img);
	  if(cost<=tot_intra_sad) img->imod = INTRA_MB_INTER;
	  macroblock_inter(img,tot_intra_sad);
	  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
  }

  for (j=1; j < mb_height; ++j) 
  {
    img->mb_y=j;
    img->block_y = j<<2;  // vertical luma block posision 
    img->pix_y   = j<<4; // vertical luma macroblock posision 
    img->pix_c_y = j<<3;  // vertical chroma macroblock posision
	
	img->mb_x=0;
	img->block_x = 0;           
	img->pix_x   = 0;          
	img->block_c_x = 0;         
	img->pix_c_x   = 0;
	img->current_mb_nr++;
	cost = Motion_Estimation(img,MAX_VALUE);
	if(cost>Intra_Threshold) tot_intra_sad=macroblock_intra_left(img);
	if(cost<=tot_intra_sad) img->imod = INTRA_MB_INTER;
	macroblock_inter(img,tot_intra_sad);
	WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
    
    for (i=1; i < mb_width-1; ++i) 
    {
      img->mb_x=i;
      img->block_x = i<<2;   // luma block           
      img->pix_x   = i<<4;   // luma pixel           
      img->block_c_x = i<<1; // chroma block         
      img->pix_c_x   = i<<3; // chroma pixel          
      
      img->current_mb_nr++;
	  cost = Motion_Estimation(img,MAX_VALUE);
	  if(cost>Intra_Threshold) 
		  tot_intra_sad=macroblock_intra_other(img);
	  if(cost<=tot_intra_sad) 
		  img->imod = INTRA_MB_INTER;
	  macroblock_inter(img,tot_intra_sad);
	  WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
    }
    img->mb_x=i;
	img->block_x = i<<2;           
	img->pix_x   = i<<4;          
	img->block_c_x = i<<1;         
	img->pix_c_x   = i<<3;
	img->current_mb_nr++;
	cost = Motion_Estimation(img,MAX_VALUE);
	if(cost>Intra_Threshold) tot_intra_sad=macroblock_intra_right(img);
	if(cost<=tot_intra_sad) img->imod = INTRA_MB_INTER;
	macroblock_inter(img,tot_intra_sad);
	WriteMb_CAVLC(img,img->intra_pred_modes,img->cbp);
  }
  AlignBits();
  return bb.cExist;
}

#define CLIP(x) max(0,min(255,(x)))
#define FILTER1(a) CLIP(((a)+16)>>5) 
#define TAP6FILTER(a,b,c,d,e,f)  (((c)+(d))*20-((b)+(e))*5+(a)+(f))
#define TAP6FILTER1(a,b,c,d,e,f) CLIP((((c)+(d))*20-((b)+(e))*5+(a)+(f)+16)>>5)
#define TAP6FILTER2(a,b,c,d,e,f) CLIP((((c)+(d))*20-((b)+(e))*5+(a)+(f)+512)>>10)

#define IAVGONEP(a,b) (((a)+(b)+1)>>1)
#define restrict  
int imgY_tmp[360*296+2048];

void onehalfpix(struct img_par *img)
{
  int i,j;
  int width=img->width;
  int width1=width+IMG_PAD_SIZE;
  int height= img->height;
  int height1= height+IMG_PAD_SIZE; 
  
  byte*  restrict o00 = imgY;
  int*  restrict itmp=imgY_tmp;
  byte*  restrict i20 = Eipol[2];
  int p00,p01,p02,p03,p04,p05,p06,p07,p08,p09,p10,p11,p12,p13,p14,p15;
  int p16,p17,p18,pp1,pp2;
  int q00,q01,q02,q03,q04,q05,q06,q07,q08,q09,q10,q11,q12,q13,q14,q15;
  
  //image extension
  p00=o00[0];
  o00[-4]=p00;o00[-3]=p00;o00[-2]=p00;o00[-1]=p00;
  o00+=width;
  p00=o00[-1];
  o00[0]=p00;o00[1]=p00;o00[2]=p00;o00[3]=p00;
  	
  o00 -= (width+4);
  for(j=1;j<5;j++)
  {	
  	memcpy(o00-j*width1,o00,width1);
  }
  
  o00 += (width1+4);
  for(j=1;j<height;j++)
  {
  	p00=o00[0];
  	o00[-4]=p00;o00[-3]=p00;o00[-2]=p00;o00[-1]=p00;
  	o00+=width;
  	p00=o00[-1];
  	o00[0]=p00;o00[1]=p00;o00[2]=p00;o00[3]=p00;
  	o00+=IMG_PAD_SIZE;
  }
  
  o00 -= (width1+4);
  for(j=1;j<5;j++)
  {	
  	memcpy(o00+j*width1,o00,width1);
  }
  //end extension
  
  
  o00 = EimgY;
  for (j=0; j < height1; j++)
  {
  	p00=o00[ 0];
	p01=o00[ 1];
	p02=o00[ 2];
	p03=o00[ 3];
	p04=o00[ 4];
	p05=o00[ 5];
	p06=o00[ 6];
	p07=o00[ 7];
	p08=o00[ 8];
	p09=o00[ 9];
	p10=o00[10];
	p11=o00[11];
	p12=o00[12];
	p13=o00[13];
	p14=o00[14];
	p15=o00[15];
	p16=o00[16];
	p17=o00[17];
	p18=o00[18];
	pp1=pp2=p00;
	
	itmp[ 0]=q00=TAP6FILTER(pp2,pp1,p00,p01,p02,p03);
	itmp[ 1]=q01=TAP6FILTER(pp1,p00,p01,p02,p03,p04);
	itmp[ 2]=q02=TAP6FILTER(p00,p01,p02,p03,p04,p05);
	itmp[ 3]=q03=TAP6FILTER(p01,p02,p03,p04,p05,p06);
	itmp[ 4]=q04=TAP6FILTER(p02,p03,p04,p05,p06,p07);
	itmp[ 5]=q05=TAP6FILTER(p03,p04,p05,p06,p07,p08);
	itmp[ 6]=q06=TAP6FILTER(p04,p05,p06,p07,p08,p09);
	itmp[ 7]=q07=TAP6FILTER(p05,p06,p07,p08,p09,p10);
	itmp[ 8]=q08=TAP6FILTER(p06,p07,p08,p09,p10,p11);
	itmp[ 9]=q09=TAP6FILTER(p07,p08,p09,p10,p11,p12);
	itmp[10]=q10=TAP6FILTER(p08,p09,p10,p11,p12,p13);
	itmp[11]=q11=TAP6FILTER(p09,p10,p11,p12,p13,p14);
	itmp[12]=q12=TAP6FILTER(p10,p11,p12,p13,p14,p15);
	itmp[13]=q13=TAP6FILTER(p11,p12,p13,p14,p15,p16);
	itmp[14]=q14=TAP6FILTER(p12,p13,p14,p15,p16,p17);
	itmp[15]=q15=TAP6FILTER(p13,p14,p15,p16,p17,p18);
	i20[ 0]=FILTER1(q00);
	i20[ 1]=FILTER1(q01);
	i20[ 2]=FILTER1(q02);
	i20[ 3]=FILTER1(q03);
	i20[ 4]=FILTER1(q04);
	i20[ 5]=FILTER1(q05);
	i20[ 6]=FILTER1(q06);
	i20[ 7]=FILTER1(q07);
	i20[ 8]=FILTER1(q08);
	i20[ 9]=FILTER1(q09);
	i20[10]=FILTER1(q10);
	i20[11]=FILTER1(q11);
	i20[12]=FILTER1(q12);
	i20[13]=FILTER1(q13);
	i20[14]=FILTER1(q14);
	i20[15]=FILTER1(q15);
	o00+=16;	i20+=16;	itmp+=16;
	
	for (i=16; i < width1-16; i+=16)
	{
		pp2=p14;
		pp1=p15;
		p00=p16;
		p01=p17;
		p02=p18;
		p03=o00[ 3];
		p04=o00[ 4];
		p05=o00[ 5];
		p06=o00[ 6];
		p07=o00[ 7];
		p08=o00[ 8];
		p09=o00[ 9];
		p10=o00[10];
		p11=o00[11];
		p12=o00[12];
		p13=o00[13];
		p14=o00[14];
		p15=o00[15];
		p16=o00[16];
		p17=o00[17];
		p18=o00[18];
		
		itmp[ 0]=q00=TAP6FILTER(pp2,pp1,p00,p01,p02,p03);
		itmp[ 1]=q01=TAP6FILTER(pp1,p00,p01,p02,p03,p04);
		itmp[ 2]=q02=TAP6FILTER(p00,p01,p02,p03,p04,p05);
		itmp[ 3]=q03=TAP6FILTER(p01,p02,p03,p04,p05,p06);
		itmp[ 4]=q04=TAP6FILTER(p02,p03,p04,p05,p06,p07);
		itmp[ 5]=q05=TAP6FILTER(p03,p04,p05,p06,p07,p08);
		itmp[ 6]=q06=TAP6FILTER(p04,p05,p06,p07,p08,p09);
		itmp[ 7]=q07=TAP6FILTER(p05,p06,p07,p08,p09,p10);
		itmp[ 8]=q08=TAP6FILTER(p06,p07,p08,p09,p10,p11);
		itmp[ 9]=q09=TAP6FILTER(p07,p08,p09,p10,p11,p12);
		itmp[10]=q10=TAP6FILTER(p08,p09,p10,p11,p12,p13);
		itmp[11]=q11=TAP6FILTER(p09,p10,p11,p12,p13,p14);
		itmp[12]=q12=TAP6FILTER(p10,p11,p12,p13,p14,p15);
		itmp[13]=q13=TAP6FILTER(p11,p12,p13,p14,p15,p16);
		itmp[14]=q14=TAP6FILTER(p12,p13,p14,p15,p16,p17);
		itmp[15]=q15=TAP6FILTER(p13,p14,p15,p16,p17,p18);
		i20[ 0]=FILTER1(q00);
		i20[ 1]=FILTER1(q01);
		i20[ 2]=FILTER1(q02);
		i20[ 3]=FILTER1(q03);
		i20[ 4]=FILTER1(q04);
		i20[ 5]=FILTER1(q05);
		i20[ 6]=FILTER1(q06);
		i20[ 7]=FILTER1(q07);
		i20[ 8]=FILTER1(q08);
		i20[ 9]=FILTER1(q09);
		i20[10]=FILTER1(q10);
		i20[11]=FILTER1(q11);
		i20[12]=FILTER1(q12);
		i20[13]=FILTER1(q13);
		i20[14]=FILTER1(q14);
		i20[15]=FILTER1(q15);
		o00+=16;	i20+=16;	itmp+=16;
	}
	pp2=p14;
	pp1=p15;
	p00=p16;
	p01=p17;
	p02=p18;
	p03=o00[ 3];
	p04=o00[ 4];
	p05=o00[ 5];
	p06=o00[ 6];
	p07=o00[ 7];
	p08=p09=p10=p07;
	
	itmp[ 0]=q00=TAP6FILTER(pp2,pp1,p00,p01,p02,p03);
	itmp[ 1]=q01=TAP6FILTER(pp1,p00,p01,p02,p03,p04);
	itmp[ 2]=q02=TAP6FILTER(p00,p01,p02,p03,p04,p05);
	itmp[ 3]=q03=TAP6FILTER(p01,p02,p03,p04,p05,p06);
	itmp[ 4]=q04=TAP6FILTER(p02,p03,p04,p05,p06,p07);
	itmp[ 5]=q05=TAP6FILTER(p03,p04,p05,p06,p07,p08);
	itmp[ 6]=q06=TAP6FILTER(p04,p05,p06,p07,p08,p09);
	itmp[ 7]=q07=TAP6FILTER(p05,p06,p07,p08,p09,p10);

	i20[ 0]=FILTER1(q00);
	i20[ 1]=FILTER1(q01);
	i20[ 2]=FILTER1(q02);
	i20[ 3]=FILTER1(q03);
	i20[ 4]=FILTER1(q04);
	i20[ 5]=FILTER1(q05);
	i20[ 6]=FILTER1(q06);
	i20[ 7]=FILTER1(q07);

	o00+=8;	i20+=8;	itmp+=8;
  }
  memcpy(Eipol[0],EimgY,width1*height1);
  //ipol[0]=imgY;
}

void onehalfpix1(struct img_par *img)
{
  int i,j;
  int width=img->width+IMG_PAD_SIZE;
  int width2=width<<1;
  int width3=width+width2;
  int width4=width<<2;
  int width8=width<<3;
  int width16=width*15;
  int height=img->height+IMG_PAD_SIZE;
  
  byte*  restrict o0  = EimgY;
  byte*  restrict o1  = EimgY+width;
  byte*  restrict o2  = EimgY+width2;
  byte*  restrict o3  = EimgY+width3;
  byte*  restrict o4  = EimgY+width4;
  byte*  restrict o5  = EimgY+width4+width;
  byte*  restrict o6  = EimgY+width4+width2;
  byte*  restrict o7  = EimgY+width4+width3;
  byte*  restrict o8  = EimgY+width8;
  byte*  restrict o9  = EimgY+width8+width;
  byte*  restrict o10 = EimgY+width8+width2;
  byte*  restrict o11 = EimgY+width8+width3;
  byte*  restrict o12 = EimgY+width8+width4;
  byte*  restrict o13 = EimgY+width8+width4+width;
  byte*  restrict o14 = EimgY+width8+width4+width2;
  byte*  restrict o15 = EimgY+width8+width4+width3;

⌨️ 快捷键说明

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