Bladeren bron

V0.8.3
1、正在修改RGB888转YUV420格式的转换方式,目前还有问题

Apple 1 maand geleden
bovenliggende
commit
6808456a64
2 gewijzigde bestanden met toevoegingen van 60 en 24 verwijderingen
  1. 57 22
      CPlayer/Player/PlayerGLWidget.cpp
  2. 3 2
      show1/widget.cpp

+ 57 - 22
CPlayer/Player/PlayerGLWidget.cpp

@@ -55,33 +55,68 @@ void PlayerGLWidget::convertQImageToYUV420(const QImage& image, Image_YUV420& yu
 {
     int width = image.width();
     int height = image.height();
-    int ySize = width * height;
-    int uvSize = ySize / 4;
+
+    int frameSize = width * height;
+    int chromaSize = frameSize / 4;
+
+    uint8_t* yuv = new uint8_t[frameSize + chromaSize * 2];
+    uint8_t* yPlane = yuv;
+    uint8_t* uPlane = yuv + frameSize;
+    uint8_t* vPlane = yuv + frameSize + chromaSize;
+
+    auto rgb = image.bits();
+
+    // for (int y = 0; y < height; ++y) {
+    //     for (int x = 0; x < width; ++x) {
+    //         QColor color = image.pixelColor(x, y);
+    //         int r = color.red();
+    //         int g = color.green();
+    //         int b = color.blue();
+
+    //         int yIndex = y * width + x;
+    //         yuv420.yData[yIndex] = static_cast<unsigned char>((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
+
+    //         if (y % 2 == 0 && x % 2 == 0) {
+    //             int uvIndex = (y / 2) * (width / 2) + (x / 2);
+    //             yuv420.uData[uvIndex] = static_cast<unsigned char>((-0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
+    //             yuv420.vData[uvIndex] = static_cast<unsigned char>((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
+    //         }
+    //     }
+    // }
+
+    for (int j = 0; j < height; j++) {
+        for (int i = 0; i < width; i++) {
+            int r = rgb[(j * width + i) * 3];
+            int g = rgb[(j * width + i) * 3 + 1];
+            int b = rgb[(j * width + i) * 3 + 2];
+
+            // int y = (0.257 * r) + (0.504 * g) + (0.098 * b) + 16;
+            // int u = -(0.148 * r) - (0.291 * g) + (0.439 * b) + 128;
+            // int v = (0.439 * r) - (0.368 * g) - (0.071 * b) + 128;
+
+            int y = (0.299 * r) + (0.587 * g) + (0.114 * b);
+            int u = (-0.169 * r) - (0.331 * g) + (0.5 * b) + 128;
+            int v = (0.5 * r) - (0.419 * g) - (0.081 * b) + 128;
+
+            yPlane[j * width + i] = std::clamp(y, 0, 255);
+
+            if (j % 2 == 0 && i % 2 == 0) {
+                uPlane[(j / 2) * (width / 2) + (i / 2)] = std::clamp(u, 0, 255);
+                vPlane[(j / 2) * (width / 2) + (i / 2)] = std::clamp(v, 0, 255);
+            }
+        }
+    }
 
     yuv420.width = width;
     yuv420.height = height;
+    yuv420.yData.resize(frameSize);
+    yuv420.uData.resize(chromaSize);
+    yuv420.vData.resize(chromaSize);
 
-    yuv420.yData.resize(ySize);
-    yuv420.uData.resize(uvSize);
-    yuv420.vData.resize(uvSize);
-
-    for (int y = 0; y < height; ++y) {
-        for (int x = 0; x < width; ++x) {
-            QColor color = image.pixelColor(x, y);
-            int r = color.red();
-            int g = color.green();
-            int b = color.blue();
+    yuv420.yData.append(reinterpret_cast<char*>(yPlane), frameSize);
+    yuv420.uData.append(reinterpret_cast<char*>(uPlane), chromaSize);
+    yuv420.vData.append(reinterpret_cast<char*>(vPlane), chromaSize);
 
-            int yIndex = y * width + x;
-            yuv420.yData[yIndex] = static_cast<unsigned char>((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
-
-            if (y % 2 == 0 && x % 2 == 0) {
-                int uvIndex = (y / 2) * (width / 2) + (x / 2);
-                yuv420.uData[uvIndex] = static_cast<unsigned char>((-0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
-                yuv420.vData[uvIndex] = static_cast<unsigned char>((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
-            }
-        }
-    }
 }
 
 void PlayerGLWidget::initializeGL()

+ 3 - 2
show1/widget.cpp

@@ -59,10 +59,11 @@ void widget::initCameraPlayer()
     /* 创建摄像机播放器 */
     m_cameraPlayer = new CameraPlayer;
 
-    QString imagePath = QApplication::applicationDirPath() + "/2.png";
+    QString imagePath = QApplication::applicationDirPath() + "/2.jpg";
     QImage image(imagePath);
     // QImage image(1653, 899, QImage::Format_RGB888);
-    // QImage image(640, 480, QImage::Format_RGB888);
+    // QImage image(1666, 720, QImage::Format_RGB888);
+    // QImage image(1280, 720, QImage::Format_RGB888);
     m_cameraPlayer->setImage(image);
 
     m_cameraPlayer->initCamera("192.1.2.73", 8000, "admin", "LH123456");