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

📄 vo_gl.c

📁 DawnLightPlayer,一个新的基于ffmpeg的全功能播放器
💻 C
字号:
/********************************************** * Dawn Light Player * *   vo_gl.c * * Created by kf701 * 10:55:13 03/15/08 CST * * $Id$ **********************************************/#if ENABLE_VO_GL#include <GL/gl.h>#include <GL/glut.h>#include "swscale.h"#include "avoutput.h"#include "global.h"static pthread_mutex_t vo_mutex;static void vo_lock_init(){	pthread_mutex_init( &vo_mutex, NULL );}static void vo_lock_free(){	pthread_mutex_destroy( &vo_mutex );}static void vo_lock(){	pthread_mutex_lock( &vo_mutex );}static void vo_unlock(){	pthread_mutex_unlock( &vo_mutex );}static volatile int need_post_display = 0;static GLuint texName;static int dx, dy, dw, dh, ww, wh;static uint8_t *image = NULL;static int src_pic_fmt;static int my_pic_fmt = PIX_FMT_RGB32; /* fix me, from GL */static AVPicture *my_pic;static void glAdjustAlignment( int stride ){	GLint gl_alignment;	if ( stride % 8 == 0 )		gl_alignment = 8;	else if ( stride % 4 == 0 )		gl_alignment = 4;	else if ( stride % 2 == 0 )		gl_alignment = 2;	else		gl_alignment = 1;	glPixelStorei( GL_UNPACK_ALIGNMENT, gl_alignment );}static void create_clear_texture( GLenum target, GLenum fmt,                                  GLint filter, int w, int h, unsigned char val ){	GLfloat fval = (GLfloat)val / 255.0;	GLfloat border[4] = { fval, fval, fval, fval };	GLenum clrfmt = (fmt == GL_ALPHA) ? GL_ALPHA : GL_LUMINANCE;	char *init = malloc( w * h );	memset( init, val, w * h );	glAdjustAlignment( w );	glPixelStorei( GL_UNPACK_ROW_LENGTH, w );	glTexImage2D( target, 0, fmt, w, h, 0, clrfmt, GL_UNSIGNED_BYTE, init );	glTexParameterf( target, GL_TEXTURE_PRIORITY, 1.0 );	glTexParameteri( target, GL_TEXTURE_MIN_FILTER, filter );	glTexParameteri( target, GL_TEXTURE_MAG_FILTER, filter );	glTexParameteri( target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );	glTexParameteri( target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );	glTexParameterfv( target, GL_TEXTURE_BORDER_COLOR, border );	free( init );}static void init_texture( void ){#if 1	glDisable(GL_BLEND);	glDisable(GL_DEPTH_TEST);	glDepthMask(GL_FALSE);	glDisable(GL_CULL_FACE);	glDrawBuffer(GL_FRONT);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);#endif	glClearColor( 0.0, 0.0, 0.0, 0.0 );	glClear( GL_COLOR_BUFFER_BIT );	glShadeModel( GL_FLAT );	glEnable(GL_TEXTURE_2D);	glGenTextures( 1, &texName );	glBindTexture( GL_TEXTURE_2D, texName );	glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );#if 0	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );#endif	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0 );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, dw, dh,	              0, GL_RGBA, GL_UNSIGNED_BYTE, image );}static void set_perpective(void){	glMatrixMode( GL_PROJECTION );	glLoadIdentity();	glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );	glMatrixMode( GL_MODELVIEW );	glLoadIdentity();	glTranslatef( 0.0, 0.0, -3.0 );}static void gl_display(void){	glClear(GL_COLOR_BUFFER_BIT);	glBegin( GL_POLYGON );	glTexCoord2f( 0.0, 0.0 );	glVertex2f( -1.0, 1.0 );	glTexCoord2f( 1.0, 0.0 );	glVertex2f( 1.0, 1.0 );	glTexCoord2f( 1.0, 1.0 );	glVertex2f( 1.0, -1.0 );	glTexCoord2f( 0.0, 1.0 );	glVertex2f( -1.0, -1.0 );	glEnd();	glFlush();}static void gl_keyboard (unsigned char key, int x, int y){	switch (key)	{	case 27:		dlp_exit(-2);		break;	default:		break;	}}static void gl_idlefunc(void){	if ( need_post_display )	{		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, dw, dh,		                GL_RGBA, GL_UNSIGNED_BYTE, image);		glutPostRedisplay();	}}static int vo_gl_init(void){	int ret;	int argc = 3;	char *argv[] = { "DawnLightPlayer", "-vo", "gl", NULL };	dx = dy = 200;	dw = dlpctxp->pwidth;	dh = dlpctxp->pheight;	ww = dw;	wh = dh;	src_pic_fmt = dlpctxp->pixfmt;	/*-----------------------------------------------------------------------------	 *  my picture for rgb	 *-----------------------------------------------------------------------------*/	my_pic = av_mallocz(sizeof(AVPicture));	ret = avpicture_alloc(my_pic, my_pic_fmt, dw, dh);	if ( -1 == ret )	{		av_log(NULL, AV_LOG_ERROR, "avpicture alloc error\n");		return -1;	}	image = my_pic->data[0];	vo_lock_init();	glutInit( &argc, argv );	glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );	glutInitWindowSize( ww, wh );	glutInitWindowPosition( dx, dy );	glutCreateWindow( "DawnLightPlayer" );	init_texture();	set_perpective();	glutDisplayFunc( gl_display );	//glutIdleFunc( gl_idlefunc );	glutKeyboardFunc( gl_keyboard );	return 0;}static int vo_gl_uninit(void){	vo_lock();	glFinish();	glFlush();	glDeleteTextures( 1, &texName );	avpicture_free( my_pic );	av_free( my_pic );	my_pic = NULL;	vo_lock_free();	return 0;}static int vo_gl_vfmt2rgb(AVPicture *dst, AVPicture *src){	static struct SwsContext *img_convert_ctx;	img_convert_ctx = sws_getCachedContext(img_convert_ctx,	                                       dw, dh, src_pic_fmt,	                                       dw, dh, my_pic_fmt, SWS_BICUBIC, NULL, NULL, NULL);	sws_scale(img_convert_ctx, src->data, src->linesize,	          0, dh, dst->data, dst->linesize);	return 0;}static void vo_gl_display(AVPicture *pict){	vo_gl_vfmt2rgb( my_pic, pict );	image = my_pic->data[0];#if 1	glEnable(GL_TEXTURE_2D);	glBindTexture( GL_TEXTURE_2D, texName );	glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, dw, dh,	                 GL_RGBA, GL_UNSIGNED_BYTE, image );	glClear( GL_COLOR_BUFFER_BIT );	glBegin( GL_POLYGON );	glTexCoord2f( 0.0, 0.0 );	glVertex2f( -1.0, 1.0 );	glTexCoord2f( 1.0, 0.0 );	glVertex2f( 1.0, 1.0 );	glTexCoord2f( 1.0, 1.0 );	glVertex2f( 1.0, -1.0 );	glTexCoord2f( 0.0, 1.0 );	glVertex2f( -1.0, -1.0 );	glEnd();	glFlush();#else	vo_lock();	need_post_display = 1;	vo_unlock();#endif}static void vo_gl_event_loop(void){	glutMainLoop();}vo_t vo_gl ={	.id = VO_ID_GL,	.name = "gl",	.vo_init = vo_gl_init,	.vo_uninit = vo_gl_uninit,	.vo_display = vo_gl_display,	.vo_event_loop = vo_gl_event_loop,};#endif /* ENABLE_VO_GL */

⌨️ 快捷键说明

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