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

📄 win.cpp

📁 嵌入式linux与arm板视频图像解析及静态图像对传
💻 CPP
字号:
#include "win.h"QLabel* PixmapLabel1;QPainter *paint;QImage img;uchar *ppm;//unsigned char *rgb;bool GetNextFrame(AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx,     int videoStream, AVFrame *pFrame){    static AVPacket packet;    static int      bytesRemaining=0;    static uint8_t  *rawData;    static bool     fFirstTime=true;    int             bytesDecoded;    int             frameFinished;    // First time we're called, set packet.data to NULL to indicate it    // doesn't have to be freed    if(fFirstTime)    {        fFirstTime=false;        packet.data=NULL;    }    // Decode packets until we have decoded a complete frame    while(true)    {        // Work on the current packet until we have decoded all of it        while(bytesRemaining > 0)        {            // Decode the next chunk of data            bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame,                &frameFinished, rawData, bytesRemaining);            // Was there an error?            if(bytesDecoded < 0)            {                fprintf(stderr, "Error while decoding frame\n");                return false;            }            bytesRemaining-=bytesDecoded;            rawData+=bytesDecoded;            // Did we finish the current frame? Then we can return            if(frameFinished)                return true;        }        // Read the next packet, skipping all packets that aren't for this        // stream        do        {            // Free old packet            if(packet.data!=NULL)                av_free_packet(&packet);            // Read new packet            if(av_read_packet(pFormatCtx, &packet)<0)                goto loop_exit;        } while(packet.stream_index!=videoStream);        bytesRemaining=packet.size;        rawData=packet.data;    }loop_exit:    // Decode the rest of the last frame    bytesDecoded=avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,         rawData, bytesRemaining);    // Free last packet    if(packet.data!=NULL)        av_free_packet(&packet);    return frameFinished!=0;}uchar* SaveFrame(AVFrame *pFrame, int width, int height, int iFrame){    //FILE *pFile;    //char *szFilename = new char [32];	ppm = new uchar [230400];	char *temp="P6\n320 240\n255\n";    int  y;    // Open file    /*sprintf(szFilename, "frame%d.ppm", iFrame);    pFile=fopen(szFilename, "wb");    if(pFile==NULL)	{				//'{' is made by me.		fprintf(stderr, "open frame file error\n");        return NULL;	}*/	memset(ppm, 0, 230400);    //Write header	    //sprintf(ppm, "P6\n%d %d\n255\n", width, height);	//memcpy(ppm, temp, 15);    // Write pixel data    for(y=0; y<height; y++)	{        //fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);		memcpy(ppm+width*3*y, pFrame->data[0]+y*pFrame->linesize[0], width*3);	}	//fwrite(ppm, 1, width*3*height+15, pFile);    // Close file    //fclose(pFile);	return ppm;}static AVInputFormat *file_iformat;void* film(void* data){	AVFormatContext *pFormatCtx;    int             i, videoStream;    AVCodecContext  *pCodecCtx;    AVCodec         *pCodec;    AVFrame         *pFrame;     AVFrame         *pFrameRGB;    int             numBytes;    uint8_t         *buffer;	    // Register all formats and codecs    av_register_all();    // Open video file    if(av_open_input_file(&pFormatCtx, "movie.wmv", NULL, 0, NULL) < 0)	{        fprintf(stderr, "Open video file\n");        return NULL; // Couldn't open file    }    fprintf(stderr, "Open video file and nb_streams is %d\n", pFormatCtx->nb_streams);    // Retrieve stream information    	if((pFormatCtx)<0)	{        fprintf(stderr, "Retrieve stream information\n");        return NULL; // Couldn't find stream information    }    // Dump information about file onto standard error    dump_format(pFormatCtx, 0, "Open video file\n", false);    // Find the first video stream    videoStream=-1;    for(i = 0; i < pFormatCtx->nb_streams; i ++)        if(pFormatCtx->streams[i]->codec.codec_type==CODEC_TYPE_VIDEO)        {            videoStream=i;            break;        }            if(videoStream==-1)	{        fprintf(stderr, "Didn't find a video stream and pFormatCtx->nb_streams is %d\n", pFormatCtx->nb_streams);        return NULL; // Didn't find a video stream    }    // Get a pointer to the codec context for the video stream    pCodecCtx=&pFormatCtx->streams[videoStream]->codec;    // Find the decoder for the video stream    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);    if(pCodec==NULL)	{        fprintf(stderr, "Codec not found\n");        return NULL; // Codec not found	}    // Inform the codec that we can handle truncated bitstreams -- i.e.,    // bitstreams where frame boundaries can fall in the middle of packets    if(pCodec->capabilities & CODEC_CAP_TRUNCATED)        pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;    // Open codec    if(avcodec_open(pCodecCtx, pCodec)<0)	{		fprintf(stderr, "pFrameRGB==NULL\n");        return NULL; // Could not open codec	}    // Hack to correct wrong frame rates that seem to be generated by some     // codecs    // if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)        \	pCodecCtx->frame_rate_base=1000;    // Allocate video frame    pFrame=avcodec_alloc_frame();    // Allocate an AVFrame structure    pFrameRGB=avcodec_alloc_frame();    if(pFrameRGB==NULL)	{		fprintf(stderr, "pFrameRGB==NULL\n");        return NULL;	}    // Determine required buffer size and allocate buffer    numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);    buffer=new uint8_t[numBytes];    // Assign appropriate parts of buffer to image planes in pFrameRGB    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);    // Read frames and save first five frames to disk    i=0;    while(GetNextFrame(pFormatCtx, pCodecCtx, videoStream, pFrame))    {        img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame,           pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);				/*style of ppm_file ok 		FILE *pFile;		pFile=fopen("win.ppm", "wb");    	if(pFile==NULL)		{				//'{' is made by me.			fprintf(stderr, "open frame file error\n");        	return NULL;		}		fwrite(SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i), 1, 320*3*240+15, pFile);    	// Close file    	fclose(pFile);		if(i==10) exit(0);*/        // Save the frame to disk		//rgb=SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i);		//exchange_r_b( (char*)rgb,320*240 );		SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i);		compress_Jpeg( ppm, 320, 240,40,1,0);		//img.loadFromData(jpegImg(), jpegSize(), 0);		//printf("size of jpe is %d\n", jpegSize());		sender(jpegImg(), jpegSize());		//img.loadFromData(SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i), pCodecCtx->width*pCodecCtx->height*3+15, 0);		delete(ppm);        //QImage img(SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i));		//paint->drawImage(0, 0, img);    }    // Free the RGB image    delete [] buffer;    av_free(pFrameRGB);    // Free the YUV frame    av_free(pFrame);    // Close the codec    avcodec_close(pCodecCtx);    // Close the video file    av_close_input_file(pFormatCtx);	pthread_exit(NULL);    return NULL;}// --------------------------------RTP--------------------------//	RTPSession sess;RTPUDPv4TransmissionParams transparams;RTPSessionParams sessparams;unsigned long destip;int destport;int portbase;int status;QString str;int init_rtp(){	portbase = 6000;	destip = inet_addr("192.168.0.10");	if (destip == INADDR_NONE) {		printf("Bad IP address specified.\\n");		return -1;	}	destip = ntohl(destip);	destport = atoi("5000");	sessparams.SetOwnTimestampUnit(1.0/10.0);				sessparams.SetAcceptOwnPackets(true);	transparams.SetPortbase(portbase);		status = sess.Create(sessparams,&transparams);		checkerror(status);	RTPIPv4Address addr(destip,destport);		status = sess.AddDestination(addr);	checkerror(status);	sess.SetDefaultPayloadType(0);	sess.SetDefaultMark(false);	sess.SetDefaultTimestampIncrement(10);}		void checkerror(int rtperr){	if (rtperr < 0)	{		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;		exit(-1);	}}int sender(unsigned char *movie, int len){	int i, index;	char buffer[128];	char num[5];  	  	/*	if (argc != 3) {    printf("Usage: ./sender destip destport\\n");    return -1;  	}*/  		index = 1;	/*  	do {    	sprintf(buffer, "%d: RTP packet", index );    	sess.SendPacket(buffer, strlen(buffer));    	printf("Send Packet %d!\n",index);				index ++;			if(index>10)		{			sprintf(buffer, " ");			sess.SendPacket(buffer, strlen(buffer));			printf("Send End_Flag Packet %d\n",index);			break;		}  	} while(1);*/	//draw image	img.loadFromData(movie, len, 0);	paint->drawImage(0, 0, img);		/*	for(int i=0; i<3; i++)	{		//sprintf(buffer, "mypack %d", index++);		//sess.SendPacket((unsigned char *)buffer, strlen(buffer));		sess.SendPacket(movie, len);	}*/	//sprintf(buffer, "mypack %d", index++);	//sess.SendPacket(buffer, strlen(buffer));	//status = sess.SendPacket((void *)movie, 1000);	//checkerror(status);		for(i=0; i<len/1000+1; i++)	{		if(i==len/1000)		{			//printf("%dst\n", i);			printf("%d\n", len%1000);			status = sess.SendPacket((void *)(movie+i*1000), len%1000);			checkerror(status);		}		else		{			//printf("%dst\n", i);			printf("1000\n");			status = sess.SendPacket((void *)(movie+i*1000), 1000);			checkerror(status);		}		usleep(1000);	}		num[0]='0'+len/1000;	num[1]='0'+len%1000/100;	num[2]='0'+len%100/10;	num[3]='0'+len%10;	num[4]='\0';	str = (const char *)num;	qWarning(str);		status = sess.SendPacket(num, 4);	checkerror(status);		//sess.BYEDestroy(RTPTime(10,0),0,0);	/*	FILE *pFile;	pFile=fopen("win.jpg", "wb");	if(pFile==NULL)	{				//'{' is made by me.		fprintf(stderr, "open frame file error\n");		return NULL;	}	fwrite(movie, 1, len, pFile);	fclose(pFile);	exit(0);*/  	return 0;}//------------------------RTP--------------------------------///* *  Constructs a Form1 as a child of 'parent', with the *  name 'name' and widget flags set to 'f'. * *  The dialog will by default be modeless, unless you set 'modal' to *  TRUE to construct a modal dialog. */Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )    : QDialog( parent, name, modal, fl ){	    if ( !name )	setName( "Form1" );    PixmapLabel1 = new QLabel( this, "PixmapLabel1" );    PixmapLabel1->setGeometry( QRect( 120, 60, 320, 240 ) );    PushButton1 = new QPushButton( this, "PushButton1" );    PushButton1->setGeometry( QRect( 240, 350, 101, 31 ) );    QFont PushButton1_font(  PushButton1->font() );    PushButton1_font.setFamily( "unifont" );    PushButton1_font.setPointSize( 16 );    PushButton1->setFont( PushButton1_font ); 	PushButton1->setText( QString::fromUtf8("开始") );	paint = new QPainter(PixmapLabel1);    //languageChange();    resize( QSize(592, 480).expandedTo(minimumSizeHint()) );    clearWState( WState_Polished );    // signals and slots connections    connect( PushButton1, SIGNAL( clicked() ), this, SLOT( pix_chg() ) );	init_rtp();}/* *  Destroys the object and frees any allocated resources */Form1::~Form1(){    // no need to delete child widgets, Qt does it all for us}/* *  Sets the strings of the subwidgets using the current *  language. */void Form1::pix_chg(){	pthread_t th_a;	int ret;	ret = pthread_create(&th_a,NULL,film,NULL);	if( ret )	{		perror("create pthread:");		exit(1);	}	//pthread_join(th_a,NULL);}void Form1::pix_show(){	QPixmap img("voice.png");	PixmapLabel1->setPixmap(img);	usleep(1);}

⌨️ 快捷键说明

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