if (frame.bufferType == AvFrame::BUFFER_TYPE_PLAIN_ARRAY) { QImage::Format format = QImage::Format_RGB32; int width = frame.format.params.video.frameWidth; int height = frame.format.params.video.frameHeight; int size = frame.buffer.size(); const uint8_t * data = frame.buffer.data (); size_t frameSize = width * height; .... m_frameData = new uint8_t[frameSize*4]; memcpy(m_frameData, frame.buffer.data (), size); .... QImage image(m_frameData, width, height, format ); if(!image.isNull()) { emit newFrame(image); } }
void VideoWidget::paintEvent( QPaintEvent* event){ QPainter painter(this); if(drawingImage.isNull()) { painter.fillRect(contentsRect(), Qt::black); } else { painter.drawImage(this->contentsRect(), drawingImage); } }
OpenGLWidget::OpenGLWidget(int index, QWidget *parent) : VidWidget(index, parent){vidFrame = NULL; doubleBuffered = GL_TRUE; isCapturing = false;setAttribute(Qt::WA_PaintOnScreen);setAttribute(Qt::WA_NoSystemBackground);Display *display = x11Info().display();if(!display) return;int error_base, event_base;if(!glXQueryExtension(display, &error_base, &event_base)) return;int doublebuffervisual[] = {GLX_RGBA, GLX_DOUBLEBUFFER, None};XVisualInfo *visualinfo = glXChooseVisual(display, DefaultScreen(display), doublebuffervisual);if(visualinfo == NULL) { int singlebuffervisual[] = {GLX_RGBA, None}; visualinfo = glXChooseVisual(display, DefaultScreen(display), singlebuffervisual); if(visualinfo == NULL) return; doubleBuffered = false;}glxContext = glXCreateContext(display, visualinfo, NULL, GL_TRUE);connect(this, SIGNAL(needRepaint()), this, SLOT(repaint()), Qt::QueuedConnection);}void OpenGLWidget::resizeEvent(QResizeEvent *event){Display *display = x11Info().display();if(!display) {event->accept(); return;}if(!glxContext) {event->accept(); return;}glXMakeCurrent(display, winId(), glxContext);glViewport(0, 0, event->size().width(), event->size().height());glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-5, 5, -5, 5, 2, 12);gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);glMatrixMode(GL_MODELVIEW);update();event->accept();}void OpenGLWidget::paintEvent(QPaintEvent *event){if(!vidFrame) {event->accept(); return;}if(!isCapturing) { convertV4L2FmtToGLFmt(vidFrame->getV4L2PixFormat()); Display *display = x11Info().display(); if(!display || !glxContext) {event->ignore(); vidFrame = NULL; return;} glXMakeCurrent(display, winId(), glxContext); glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, width(), height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-5, 5, -5, 5, 2, 12); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); glMatrixMode(GL_MODELVIEW); isCapturing = true;}Display *display = x11Info().display();if(!display || !glxContext) {event->ignore(); vidFrame = NULL; return;}glXMakeCurrent(display, winId(), glxContext);glBindTexture(GL_TEXTURE_2D, 1);glPixelStorei(GL_UNPACK_ALIGNMENT, 1);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_LINEAR);glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);glTexImage2D(GL_TEXTURE_2D, 0, glFormat, vidFrame->getWidth(), vidFrame->getHeight(), 0, glFormat, GL_UNSIGNED_BYTE, vidFrame->getDeinterlacedData());glDepthFunc(GL_EQUAL);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, 1);glPushMatrix();glBegin(GL_POLYGON);glTexCoord2f(1.0, 0.0); glVertex2f(5.0, 5.0);glTexCoord2f(0.0, 0.0); glVertex2f(-5.0, 5.0);glTexCoord2f(0.0, 1.0); glVertex2f(-5.0, -5.0);glTexCoord2f(1.0, 1.0); glVertex2f(5.0, -5.0);glEnd();glPopMatrix();glDisable(GL_TEXTURE_2D);if(doubleBuffered) glXSwapBuffers(display, winId());else glFlush();vidFrame = NULL;event->accept();}