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

📄 store.cpp

📁 mpeg2编码解码源程序代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/* store.c, picture output routines                                         *//* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. *//* * Disclaimer of Warranty * * These software programs are available to the user without any license fee or * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims * any and all warranties, whether express, implied, or statuary, including any * implied warranties or merchantability or of fitness for a particular * purpose.  In no event shall the copyright-holder be liable for any * incidental, punitive, or consequential damages of any kind whatsoever * arising from the use of these programs. * * This disclaimer of warranty extends to the user of these programs and user's * customers, employees, agents, transferees, successors, and assigns. * * The MPEG Software Simulation Group does not represent or warrant that the * programs furnished hereunder are free of infringement of any third-party * patents. * * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware, * are subject to royalty fees to patent holders.  Many of these patents are * general enough such that they are unavoidable regardless of implementation * design. * */#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include "config.h"#include "global.h"
#include "io.h"/* private prototypes */static void store_one (char *outname, unsigned char *src[],  int offset, int incr, int height);static void store_yuv (char *outname, unsigned char *src[],  int offset, int incr, int height);static void store_sif (char *outname, unsigned char *src[],  int offset, int incr, int height);static void store_ppm_tga (char *outname, unsigned char *src[],  int offset, int incr, int height, int tgaflag);static void store_yuv1 (char *name, unsigned char *src,  int offset, int incr, int width, int height,unsigned char *des);static void putbyte (int c);static void putword (int w);static void conv422to444 (unsigned char *src, unsigned char *dst);static void conv420to422 (unsigned char *src, unsigned char *dst);#define OBFRSIZE 4096static unsigned char obfr[OBFRSIZE];static unsigned char *optr;static int outfile;/* * store a picture as either one frame or two fields */
#define WIDTHBYTES(i)           ((unsigned)((i+31)&(~31))/8) 
void SaveBMP(CString s,
			 unsigned char *src[],			// BGR buffer
							UINT width,				// pixels
							UINT height)
{

	unsigned char *buf1;
	int Y,U,V,B,G,R;
	buf1=(BYTE*)malloc(width*height*3);
	for (int j=0;j<height;j++) 
		for (int i=0;i<width*3;i+=3) 
		{
			Y=*(src[0]+i/3+(height-j-1)*width);
			U=*(src[1]+(i/6)+(height-j-1)/4*width);
			V=*(src[2]+(i/6)+(height-j-1)/4*width);

			B=Y+1.772*(U-128);
			G=Y-0.34414*(U-128)-0.71414*(V-128);
			R=Y+1.402*(V-128);
			
			if(B<=255&&B>=0)
			  *(buf1+i+j*width*3)=B;
			else if(B>255)
			  *(buf1+i+j*width*3)=255;
			else 
			  *(buf1+i+j*width*3)=0;

		
			if(G<=255&&G>=0)
			*(buf1+i+1+j*width*3)=G;
			else if(G>255)
             *(buf1+i+1+j*width*3)=255;
			else
				*(buf1+1+i+j*width*3)=0;
	
			if(R<=255&&R>=0)
			*(buf1+2+i+j*width*3)=R;
			else if(R>255)
				*(buf1+2+i+j*width*3)=255;
			else
				*(buf1+2+i+j*width*3)=0;
			
		}
		
//	memcpy(Framedst,buf1,width*height*3);
		////////////////////////////
	short res1=0;
    short res2=0;
    long pixoff=54;
    long compression=0;
    long cmpsize=0;
    long colors=0;
    long impcol=0;
	char m1='B';
	char m2='M';


	DWORD widthDW = WIDTHBYTES(width * 24);

	long bmfsize=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
  							widthDW * height;	
	long byteswritten=0;

	BITMAPINFOHEADER header;
  	header.biSize=40; 						// header size
	header.biWidth=width;
	header.biHeight=height;
	header.biPlanes=1;
	header.biBitCount=24;					// RGB encoded, 24 bit
	header.biCompression=BI_RGB;			// no compression
	header.biSizeImage=0;
	header.biXPelsPerMeter=0;
	header.biYPelsPerMeter=0;
	header.biClrUsed=0;
	header.biClrImportant=0;

	FILE *fp;	
	fp=fopen(s,"wb");
	if (fp==NULL) {
	
		return;
	}

	// should probably check for write errors here...
	
	fwrite((BYTE  *)&(m1),1,1,fp); byteswritten+=1;
	fwrite((BYTE  *)&(m2),1,1,fp); byteswritten+=1;
	fwrite((long  *)&(bmfsize),4,1,fp);	byteswritten+=4;
	fwrite((int  *)&(res1),2,1,fp); byteswritten+=2;
	fwrite((int  *)&(res2),2,1,fp); byteswritten+=2;
	fwrite((long  *)&(pixoff),4,1,fp); byteswritten+=4;

	fwrite((BITMAPINFOHEADER *)&header,sizeof(BITMAPINFOHEADER),1,fp);
	byteswritten+=sizeof(BITMAPINFOHEADER);
	


	long row=0;
	long rowidx;
	long row_size;
	row_size=header.biWidth*3;
    long rc;
	for (row=0;row<header.biHeight;row++) {
		
		rowidx=(long unsigned)row*row_size;						      

	
		// write a row
		rc=fwrite((void  *)(buf1+rowidx),row_size,1,fp);
		if (rc!=1) {
			
	
			break;
		}
		byteswritten+=row_size;	

		// pad to DWORD
		
		for (DWORD count=row_size;count<widthDW;count++) {
			char dummy=0;
			fwrite(&dummy,1,1,fp);
			byteswritten++;							  
		}

	}


           
	fclose(fp);

	free(buf1);
	
}
void Write_Frame(unsigned char *src[],int frame){  char outname[FILENAME_LENGTH];  if (progressive_sequence || progressive_frame || Frame_Store_Flag)  {    /* progressive */    sprintf(outname,Output_Picture_Filename,frame,'f');
	CString s;
    s=outname;
    BYTE *buf;

	/*
    SaveBMP(	
	        src,	// output path
	      
			horizontal_size,				// pixels
			vertical_size);
	*/    store_one(outname,src,0,Coded_Picture_Width,vertical_size);  }  else  {    /* interlaced */
	 sprintf(outname,Output_Picture_Filename,frame,'a');
	 
    // store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);     sprintf(outname,Output_Picture_Filename,frame,'b');

	  //  store_one(outname,src,   //   Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);  }}/* * store one frame or one field */static void store_one(char *outname,unsigned char *src[],int offset,int incr,int height){  switch (Output_Type)  {  case T_YUV:    store_yuv(outname,src,offset,incr,height);    break;  case T_SIF:    store_sif(outname,src,offset,incr,height);    break;  case T_TGA:    store_ppm_tga(outname,src,offset,incr,height,1);    break;  case T_PPM:    store_ppm_tga(outname,src,offset,incr,height,0);    break;#ifdef DISPLAY  case T_X11:    dither(src);    break;#endif  default:    break;  }}

unsigned char *y,*u,*v;/* separate headerless files for y, u and v */

static void store_yuv(char *outname,unsigned char *src[],int offset,int incr,int height){  int hsize;  char tmpname[FILENAME_LENGTH];  hsize = horizontal_size;
 
  y=(BYTE*)malloc(hsize*height*3);
  u=(BYTE*)malloc(hsize*height*3);
  v=(BYTE*)malloc(hsize*height*3);
  

  sprintf(tmpname,"%s.Y",outname);  store_yuv1(tmpname,src[0],offset,incr,hsize,height,y);  if (chroma_format!=CHROMA444)  {    offset>>=1; incr>>=1; hsize>>=1;  }  if (chroma_format==CHROMA420)  {    height>>=1;  }
  sprintf(tmpname,"%s.U",outname);  store_yuv1(tmpname,src[1],offset,incr,hsize,height,u);  sprintf(tmpname,"%s.V",outname);  store_yuv1(tmpname,src[2],offset,incr,hsize,height,v);
  

  
  CString s;
  s=outname;
  s=s+".bmp";
  BYTE *buf;
  SaveBMP(
	       s,
	        src,	// output path
			hsize*2,				// pixels
			height*2);

  free(y);
  free(u);
  free(v);

}

/* auxiliary routine */static void store_yuv1(char *name,unsigned char *src,int offset,int incr,int width,int height,unsigned char *des){  int i, j;  unsigned char *p,*q;
  if (!Quiet_Flag)    fprintf(stderr,"saving %s\n",name);/*  if ((outfile = _open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)  {    sprintf(Error_Text,"Couldn't create %s\n",name);    Error(Error_Text);  }*/  optr=obfr;  for (i=0; i<height; i++)  {    p = src + offset + incr*i;
	q = des + offset + incr*i;    for (j=0; j<width; j++)
    {
	//	putbyte(*p);
		*q=*p;
		q++;
		p++;
	}
  }
 // if (optr!=obfr)  //  _write(outfile,obfr,optr-obfr);
     //  _close(outfile);}/* * store as headerless file in U,Y,V,Y format */static void store_sif (char *outname,unsigned char *src[],int offset,int incr,int height){  int i,j;  unsigned char *py, *pu, *pv;  static unsigned char *u422, *v422;

⌨️ 快捷键说明

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