问题: 通过ffmpeg 将视频文件转换成图片,然后使用opengl 连续渲染图片,图片水平方向会出现抖动、闪烁问题。
原视频 1920 * 1080 60fps
可以很明显看出来,红色方块边缘出现闪烁现象,但vlc播放原视频却没有这个问题,很平滑向左面移动。
尝试过将图片缩小,转换成yuv,结果边缘都会出现闪烁问题。
请大家们指导指导,到底是那里出问题了 ?
实现代码:
#include<vector>
#include <string>
#include <iostream>
#include "glew.h"
#include "glut.h"
using namespace std;
GLuint myTex;
int flags = 0;
long currentFrame = 1;
long totalFrameNumber = 0;
char m_file[50];
int m_img_width, m_img_height;
BYTE* loadFileData(const char* filename) {
FILE* img = NULL;
fopen_s(&img, filename, "rb");
unsigned long size = 0;
fseek(img, 18, SEEK_SET);
fread(&m_img_width, 4, 1, img);
fread(&m_img_height, 4, 1, img);
fseek(img, 0, SEEK_END);
size = ftell(img) - 54;
unsigned char *data = (unsigned char*)malloc(size);
fseek(img, 54, SEEK_SET); // image data
fread(data, size, 1, img);
fclose(img);
return data;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
BYTE* data = loadFileData(m_file);
int w = m_img_width;
int h = m_img_height;
glGenTextures(1, &myTex);
glBindTexture(GL_TEXTURE_2D, myTex);
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_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
glEnable(GL_TEXTURE_2D); // 启用纹理
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glEnd();
//glPopAttrib();
glDisable(GL_TEXTURE_2D);
//glFlush();
glutSwapBuffers();
free(data);
}
void timefunc(int value) {
if (currentFrame <= 599) {
sprintf(m_file, "out\\%d.bmp", ++currentFrame);
}
else {
sprintf(m_file, "out\\%d.bmp", currentFrame);
}
display();
glutTimerFunc(40, timefunc, 0);
}
int main(int argc, char* argv[])
{
sprintf(m_file, "out\\%d.bmp", currentFrame);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1920, 1081);
glutInitWindowPosition(0, 0);
glutCreateWindow("example");
glEnable(GL_TEXTURE_2D); // 启用纹理
glutDisplayFunc(display);
//glutIdleFunc(stepDisplay);
glutTimerFunc(40, timefunc, 0);
glutMainLoop();
system("pause");
return 0;
}