avopenglwidget.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. #include "avopenglwidget.h"
  2. #include <QDebug>
  3. #include <QFont>
  4. #include <QFontMetrics>
  5. #include <QPainter>
  6. #include <QTimer>
  7. #include <climits>
  8. #include <new>
  9. AVOpenGLWidget::AVOpenGLWidget(QWidget* parent)
  10. : QOpenGLWidget(parent)
  11. , m_program(nullptr)
  12. , m_textureId(0)
  13. , m_frameData(nullptr)
  14. , m_frameWidth(0)
  15. , m_frameHeight(0)
  16. , m_frameFormat(0)
  17. , m_frameUpdated(false)
  18. , m_initialized(false)
  19. , m_keepAspectRatio(true)
  20. , m_gray(false)
  21. , m_threshold(false)
  22. , m_thresholdValue(0.5f)
  23. , m_blur(false)
  24. , m_blurRadius(1.0f)
  25. , m_reverse(false)
  26. , m_colorReduce(false)
  27. , m_colorReduceLevel(0)
  28. , m_gamma(false)
  29. , m_gammaValue(1.0f)
  30. , m_contrastBright(false)
  31. , m_contrast(1.0f)
  32. , m_brightness(0.0f)
  33. , m_mirror(false)
  34. , m_noVideoTip(QStringLiteral("视频未开始"))
  35. , m_tipTexture(0)
  36. , m_tipAngle(0.0f)
  37. {
  38. // 设置OpenGL组件的尺寸策略,确保有足够的显示空间
  39. setMinimumSize(320, 240);
  40. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  41. // setAttribute(Qt::WA_NoSystemBackground, true);
  42. // setAttribute(Qt::WA_OpaquePaintEvent, true);
  43. // setAutoFillBackground(false);
  44. // 设置顶点坐标
  45. m_vertices[0] = -1.0f;
  46. m_vertices[1] = -1.0f;
  47. m_vertices[2] = 1.0f;
  48. m_vertices[3] = -1.0f;
  49. m_vertices[4] = -1.0f;
  50. m_vertices[5] = 1.0f;
  51. m_vertices[6] = 1.0f;
  52. m_vertices[7] = 1.0f;
  53. // 设置纹理坐标
  54. m_texCoords[0] = 0.0f;
  55. m_texCoords[1] = 1.0f;
  56. m_texCoords[2] = 1.0f;
  57. m_texCoords[3] = 1.0f;
  58. m_texCoords[4] = 0.0f;
  59. m_texCoords[5] = 0.0f;
  60. m_texCoords[6] = 1.0f;
  61. m_texCoords[7] = 0.0f;
  62. // 3D文本旋转动画定时器
  63. m_tipTimer = new QTimer(this);
  64. connect(m_tipTimer, &QTimer::timeout, this, [this]() {
  65. m_tipAngle += 2.0f;
  66. if (m_tipAngle > 360.0f)
  67. m_tipAngle -= 360.0f;
  68. update();
  69. });
  70. m_tipTimer->start(30);
  71. // 初始化时生成一次纹理,防止初次显示时未生成
  72. m_frameData = nullptr;
  73. m_frameUpdated = false;
  74. m_tipImage = QImage();
  75. m_tipTexture = 0;
  76. }
  77. AVOpenGLWidget::~AVOpenGLWidget()
  78. {
  79. Close();
  80. }
  81. bool AVOpenGLWidget::Open(unsigned int width, unsigned int height)
  82. {
  83. QMutexLocker locker(&m_mutex);
  84. m_frameWidth = width;
  85. m_frameHeight = height;
  86. // 如果已经有数据,释放它
  87. if (m_frameData) {
  88. delete[] m_frameData;
  89. }
  90. // 分配新的内存
  91. m_frameData = new unsigned char[width * height * 4]; // RGBA格式
  92. memset(m_frameData, 0, width * height * 4);
  93. return true;
  94. }
  95. void AVOpenGLWidget::Close()
  96. {
  97. makeCurrent();
  98. if (m_textureId) {
  99. glDeleteTextures(1, &m_textureId);
  100. m_textureId = 0;
  101. }
  102. if (m_program) {
  103. delete m_program;
  104. m_program = nullptr;
  105. }
  106. doneCurrent();
  107. // 释放帧数据
  108. QMutexLocker locker(&m_mutex);
  109. if (m_frameData) {
  110. delete[] m_frameData;
  111. m_frameData = nullptr;
  112. }
  113. m_frameWidth = 0;
  114. m_frameHeight = 0;
  115. m_frameUpdated = false;
  116. m_initialized = false;
  117. }
  118. void AVOpenGLWidget::initializeGL()
  119. {
  120. initializeOpenGLFunctions();
  121. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  122. // 创建统一shader,支持多特效
  123. if (m_program) {
  124. delete m_program;
  125. m_program = nullptr;
  126. }
  127. m_program = new QOpenGLShaderProgram();
  128. m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
  129. "attribute vec2 vertexIn;\n"
  130. "attribute vec2 textureIn;\n"
  131. "varying vec2 textureOut;\n"
  132. "void main(void)\n"
  133. "{\n"
  134. " gl_Position = vec4(vertexIn, 0.0, 1.0);\n"
  135. " textureOut = textureIn;\n"
  136. "}\n");
  137. m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  138. R"Raw(
  139. varying vec2 textureOut;
  140. uniform sampler2D texture;
  141. uniform vec2 uTextureSize;
  142. uniform bool uGray;
  143. uniform bool uThreshold;
  144. uniform float uThresholdValue;
  145. uniform bool uBlur;
  146. uniform float uBlurRadius;
  147. uniform bool uReverse;
  148. uniform bool uColorReduce;
  149. uniform int uColorReduceLevel;
  150. uniform bool uGamma;
  151. uniform float uGammaValue;
  152. uniform bool uContrastBright;
  153. uniform float uContrast;
  154. uniform float uBrightness;
  155. uniform bool uMirror;
  156. void main(void)
  157. {
  158. vec2 uv = textureOut;
  159. if (uMirror) {
  160. uv.x = 1.0 - uv.x;
  161. }
  162. vec4 color = texture2D(texture, uv);
  163. // 灰度
  164. if (uGray) {
  165. float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
  166. color = vec4(gray, gray, gray, color.a);
  167. }
  168. // 二值化
  169. if (uThreshold) {
  170. float v = dot(color.rgb, vec3(0.299, 0.587, 0.114));
  171. float th = v > uThresholdValue ? 1.0 : 0.0;
  172. color = vec4(th, th, th, color.a);
  173. }
  174. // 简单3x3均值模糊
  175. if (uBlur) {
  176. vec2 tex_offset = vec2(1.0) / uTextureSize;
  177. vec4 sum = vec4(0.0);
  178. for (int dx = -1; dx <= 1; ++dx)
  179. for (int dy = -1; dy <= 1; ++dy)
  180. sum += texture2D(texture, uv + vec2(dx, dy) * tex_offset * uBlurRadius);
  181. color = sum / 9.0;
  182. }
  183. // 反色
  184. if (uReverse) {
  185. color.rgb = vec3(1.0) - color.rgb;
  186. }
  187. // 色彩减少
  188. if (uColorReduce) {
  189. color.rgb = floor(color.rgb * float(uColorReduceLevel)) / float(uColorReduceLevel);
  190. }
  191. // 伽马
  192. if (uGamma) {
  193. color.rgb = pow(color.rgb, vec3(1.0 / uGammaValue));
  194. }
  195. // 对比度/亮度
  196. if (uContrastBright) {
  197. color.rgb = color.rgb * uContrast + uBrightness;
  198. }
  199. gl_FragColor = color;
  200. }
  201. )Raw");
  202. m_program->bindAttributeLocation("vertexIn", 0);
  203. m_program->bindAttributeLocation("textureIn", 1);
  204. m_program->link();
  205. // 创建纹理
  206. glGenTextures(1, &m_textureId);
  207. glBindTexture(GL_TEXTURE_2D, m_textureId);
  208. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  209. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  210. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  211. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  212. glBindTexture(GL_TEXTURE_2D, 0);
  213. m_initialized = true;
  214. }
  215. void AVOpenGLWidget::resizeGL(int width, int height)
  216. {
  217. glViewport(0, 0, width, height);
  218. }
  219. // 文本转OpenGL纹理
  220. void AVOpenGLWidget::updateTipTexture()
  221. {
  222. QFont font;
  223. font.setPointSize(48);
  224. font.setBold(true);
  225. QFontMetrics fm(font);
  226. int w = fm.horizontalAdvance(m_noVideoTip) + 40;
  227. int h = fm.height() + 40;
  228. QImage img(w, h, QImage::Format_ARGB32_Premultiplied);
  229. img.fill(Qt::transparent);
  230. QPainter p(&img);
  231. p.setFont(font);
  232. p.setPen(Qt::white);
  233. p.setRenderHint(QPainter::Antialiasing);
  234. p.drawText(img.rect(), Qt::AlignCenter, m_noVideoTip);
  235. p.end();
  236. m_tipImage = img;
  237. if (m_tipTexture) {
  238. glDeleteTextures(1, &m_tipTexture);
  239. }
  240. glGenTextures(1, &m_tipTexture);
  241. glBindTexture(GL_TEXTURE_2D, m_tipTexture);
  242. glTexImage2D(GL_TEXTURE_2D,
  243. 0,
  244. GL_RGBA,
  245. img.width(),
  246. img.height(),
  247. 0,
  248. GL_BGRA,
  249. GL_UNSIGNED_BYTE,
  250. img.bits());
  251. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  252. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  253. glBindTexture(GL_TEXTURE_2D, 0);
  254. }
  255. // 3D绘制方法
  256. void AVOpenGLWidget::drawNoVideoTip3D()
  257. {
  258. glClearColor(0, 0, 0, 1);
  259. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  260. glEnable(GL_DEPTH_TEST);
  261. glEnable(GL_TEXTURE_2D);
  262. glBindTexture(GL_TEXTURE_2D, m_tipTexture);
  263. glMatrixMode(GL_PROJECTION);
  264. glLoadIdentity();
  265. float aspect = float(width()) / float(height());
  266. glOrtho(-aspect, aspect, -1, 1, -10, 10);
  267. glMatrixMode(GL_MODELVIEW);
  268. glLoadIdentity();
  269. glTranslatef(0, 0, 0);
  270. glRotatef(m_tipAngle, 0, 1, 0);
  271. float w = float(m_tipImage.width()) / width();
  272. float h = float(m_tipImage.height()) / height();
  273. glBegin(GL_QUADS);
  274. glTexCoord2f(0, 1);
  275. glVertex3f(-w, -h, 0);
  276. glTexCoord2f(1, 1);
  277. glVertex3f(w, -h, 0);
  278. glTexCoord2f(1, 0);
  279. glVertex3f(w, h, 0);
  280. glTexCoord2f(0, 0);
  281. glVertex3f(-w, h, 0);
  282. glEnd();
  283. glBindTexture(GL_TEXTURE_2D, 0);
  284. glDisable(GL_TEXTURE_2D);
  285. glDisable(GL_DEPTH_TEST);
  286. }
  287. void AVOpenGLWidget::paintGL()
  288. {
  289. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  290. QMutexLocker locker(&m_mutex);
  291. if (!m_frameData || m_frameWidth <= 0 || m_frameHeight <= 0 || !m_frameUpdated) {
  292. if (m_tipTexture == 0)
  293. updateTipTexture();
  294. drawNoVideoTip3D();
  295. return;
  296. }
  297. // 绑定纹理并更新数据
  298. glBindTexture(GL_TEXTURE_2D, m_textureId);
  299. glTexImage2D(GL_TEXTURE_2D,
  300. 0,
  301. GL_RGBA,
  302. m_frameWidth,
  303. m_frameHeight,
  304. 0,
  305. GL_RGBA,
  306. GL_UNSIGNED_BYTE,
  307. m_frameData);
  308. m_program->bind();
  309. m_program->setUniformValue("texture", 0);
  310. m_program->setUniformValue("uTextureSize", QVector2D(m_frameWidth, m_frameHeight));
  311. m_program->setUniformValue("uGray", m_gray);
  312. m_program->setUniformValue("uThreshold", m_threshold);
  313. m_program->setUniformValue("uThresholdValue", m_thresholdValue);
  314. m_program->setUniformValue("uBlur", m_blur);
  315. m_program->setUniformValue("uBlurRadius", m_blurRadius);
  316. m_program->setUniformValue("uReverse", m_reverse);
  317. m_program->setUniformValue("uColorReduce", m_colorReduce);
  318. m_program->setUniformValue("uColorReduceLevel", m_colorReduceLevel);
  319. m_program->setUniformValue("uGamma", m_gamma);
  320. m_program->setUniformValue("uGammaValue", m_gammaValue);
  321. m_program->setUniformValue("uContrastBright", m_contrastBright);
  322. m_program->setUniformValue("uContrast", m_contrast);
  323. m_program->setUniformValue("uBrightness", m_brightness);
  324. m_program->setUniformValue("uMirror", m_mirror);
  325. m_program->enableAttributeArray(0);
  326. m_program->enableAttributeArray(1);
  327. m_program->setAttributeArray(0, m_vertices, 2);
  328. m_program->setAttributeArray(1, m_texCoords, 2);
  329. // 保持比例
  330. if (m_keepAspectRatio) {
  331. QSize widgetSize = size();
  332. double widgetRatio = double(widgetSize.width()) / widgetSize.height();
  333. double videoRatio = double(m_frameWidth) / m_frameHeight;
  334. int x = 0, y = 0, w = widgetSize.width(), h = widgetSize.height();
  335. if (widgetRatio > videoRatio) {
  336. w = int(h * videoRatio);
  337. x = (widgetSize.width() - w) / 2;
  338. } else {
  339. h = int(w / videoRatio);
  340. y = (widgetSize.height() - h) / 2;
  341. }
  342. glViewport(x, y, w, h);
  343. } else {
  344. glViewport(0, 0, width(), height());
  345. }
  346. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  347. m_program->disableAttributeArray(0);
  348. m_program->disableAttributeArray(1);
  349. m_program->release();
  350. glBindTexture(GL_TEXTURE_2D, 0);
  351. }
  352. void AVOpenGLWidget::updateFrame(const AVOpenGLWidget::RGBAFrame& frame)
  353. {
  354. if (!frame.data || frame.width <= 0 || frame.height <= 0)
  355. return;
  356. QMutexLocker locker(&m_mutex);
  357. // 如果尺寸变化,重新分配内存
  358. if (m_frameWidth != frame.width || m_frameHeight != frame.height) {
  359. if (m_frameData) {
  360. delete[] m_frameData;
  361. m_frameData = nullptr;
  362. }
  363. m_frameWidth = frame.width;
  364. m_frameHeight = frame.height;
  365. // 检查内存分配大小是否合理
  366. size_t dataSize = static_cast<size_t>(m_frameWidth) * m_frameHeight * 4;
  367. if (dataSize > 0 && dataSize < SIZE_MAX / 4) {
  368. try {
  369. m_frameData = new unsigned char[dataSize]; // RGBA格式
  370. } catch (const std::bad_alloc&) {
  371. m_frameData = nullptr;
  372. return;
  373. }
  374. } else {
  375. return;
  376. }
  377. }
  378. // 确保m_frameData已正确分配
  379. if (!m_frameData) {
  380. return;
  381. }
  382. // 复制帧数据
  383. memcpy(m_frameData, frame.data, m_frameWidth * m_frameHeight * 4);
  384. m_frameUpdated = true;
  385. // 请求重绘
  386. update();
  387. }
  388. bool AVOpenGLWidget::convertFromAVFrame(AVFrame* frame)
  389. {
  390. if (!frame || frame->width <= 0 || frame->height <= 0 || !frame->data[0])
  391. return false;
  392. QMutexLocker locker(&m_mutex);
  393. // 如果尺寸变化,重新分配内存
  394. if (m_frameWidth != frame->width || m_frameHeight != frame->height) {
  395. if (m_frameData) {
  396. delete[] m_frameData;
  397. m_frameData = nullptr;
  398. }
  399. m_frameWidth = frame->width;
  400. m_frameHeight = frame->height;
  401. // 检查内存分配大小是否合理
  402. size_t dataSize = static_cast<size_t>(m_frameWidth) * m_frameHeight * 4;
  403. if (dataSize > 0 && dataSize < SIZE_MAX / 4) {
  404. try {
  405. m_frameData = new unsigned char[dataSize]; // RGBA格式
  406. } catch (const std::bad_alloc&) {
  407. m_frameData = nullptr;
  408. return false;
  409. }
  410. } else {
  411. return false;
  412. }
  413. }
  414. // 确保m_frameData已正确分配,即使尺寸没有变化
  415. if (!m_frameData) {
  416. // 尺寸没有变化但m_frameData为空,需要重新分配
  417. size_t dataSize = static_cast<size_t>(m_frameWidth) * m_frameHeight * 4;
  418. if (dataSize > 0 && dataSize < SIZE_MAX / 4) {
  419. try {
  420. m_frameData = new unsigned char[dataSize]; // RGBA格式
  421. } catch (const std::bad_alloc&) {
  422. m_frameData = nullptr;
  423. return false;
  424. }
  425. } else {
  426. return false;
  427. }
  428. }
  429. // 检查源数据是否有效(防止frame.data[0]为空字符或无效数据)
  430. if (!frame->data[0] || frame->linesize[0] <= 0) {
  431. qDebug() << "Invalid frame data or linesize";
  432. return false;
  433. }
  434. // 根据不同的像素格式进行转换
  435. switch (frame->format) {
  436. case AV_PIX_FMT_RGBA: {
  437. // 直接复制RGBA数据
  438. for (int y = 0; y < frame->height; y++) {
  439. memcpy(m_frameData + y * m_frameWidth * 4,
  440. frame->data[0] + y * frame->linesize[0],
  441. frame->width * 4);
  442. }
  443. } break;
  444. case AV_PIX_FMT_RGB24: {
  445. // RGB24转RGBA
  446. for (int y = 0; y < frame->height; y++) {
  447. uint8_t* src = frame->data[0] + y * frame->linesize[0];
  448. uint8_t* dst = m_frameData + y * m_frameWidth * 4;
  449. for (int x = 0; x < frame->width; x++) {
  450. *dst++ = *src++; // R
  451. *dst++ = *src++; // G
  452. *dst++ = *src++; // B
  453. *dst++ = 255; // A
  454. }
  455. }
  456. } break;
  457. case AV_PIX_FMT_BGR0:
  458. case AV_PIX_FMT_BGRA: {
  459. // BGRA转RGBA
  460. for (int y = 0; y < frame->height; y++) {
  461. uint8_t* src = frame->data[0] + y * frame->linesize[0];
  462. uint8_t* dst = m_frameData + y * m_frameWidth * 4;
  463. for (int x = 0; x < frame->width; x++) {
  464. uint8_t b = *src++;
  465. uint8_t g = *src++;
  466. uint8_t r = *src++;
  467. uint8_t a = *src++;
  468. *dst++ = r;
  469. *dst++ = g;
  470. *dst++ = b;
  471. *dst++ = a;
  472. }
  473. }
  474. } break;
  475. case AV_PIX_FMT_BGR24: {
  476. // BGR24转RGBA
  477. for (int y = 0; y < frame->height; y++) {
  478. uint8_t* src = frame->data[0] + y * frame->linesize[0];
  479. uint8_t* dst = m_frameData + y * m_frameWidth * 4;
  480. for (int x = 0; x < frame->width; x++) {
  481. uint8_t b = *src++;
  482. uint8_t g = *src++;
  483. uint8_t r = *src++;
  484. *dst++ = r; // R
  485. *dst++ = g; // G
  486. *dst++ = b; // B
  487. *dst++ = 255; // A (设为不透明)
  488. }
  489. }
  490. } break;
  491. case AV_PIX_FMT_YUV420P: // 添加对YUV420P格式的支持
  492. {
  493. // 检查YUV平面数据是否有效
  494. if (!frame->data[1] || !frame->data[2])
  495. return false;
  496. // YUV420P转RGBA
  497. for (int y = 0; y < frame->height; y++) {
  498. uint8_t* dst = m_frameData + y * m_frameWidth * 4;
  499. for (int x = 0; x < frame->width; x++) {
  500. int Y = frame->data[0][y * frame->linesize[0] + x];
  501. int U = frame->data[1][(y / 2) * frame->linesize[1] + (x / 2)];
  502. int V = frame->data[2][(y / 2) * frame->linesize[2] + (x / 2)];
  503. // YUV转RGB公式
  504. int C = Y - 16;
  505. int D = U - 128;
  506. int E = V - 128;
  507. int R = (298 * C + 409 * E + 128) >> 8;
  508. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  509. int B = (298 * C + 516 * D + 128) >> 8;
  510. // 限制RGB值在0-255范围内
  511. R = R < 0 ? 0 : (R > 255 ? 255 : R);
  512. G = G < 0 ? 0 : (G > 255 ? 255 : G);
  513. B = B < 0 ? 0 : (B > 255 ? 255 : B);
  514. *dst++ = R; // R
  515. *dst++ = G; // G
  516. *dst++ = B; // B
  517. *dst++ = 255; // A
  518. }
  519. }
  520. } break;
  521. case AV_PIX_FMT_NV12: {
  522. // 检查NV12平面数据是否有效
  523. if (!frame->data[1])
  524. return false;
  525. // NV12转RGBA
  526. for (int y = 0; y < frame->height; y++) {
  527. uint8_t* dst = m_frameData + y * m_frameWidth * 4;
  528. for (int x = 0; x < frame->width; x++) {
  529. int Y = frame->data[0][y * frame->linesize[0] + x];
  530. int U = frame->data[1][(y / 2) * frame->linesize[1] + (x / 2) * 2];
  531. int V = frame->data[1][(y / 2) * frame->linesize[1] + (x / 2) * 2 + 1];
  532. // YUV转RGB公式
  533. int C = Y - 16;
  534. int D = U - 128;
  535. int E = V - 128;
  536. int R = (298 * C + 409 * E + 128) >> 8;
  537. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  538. int B = (298 * C + 516 * D + 128) >> 8;
  539. // 限制RGB值在0-255范围内
  540. R = R < 0 ? 0 : (R > 255 ? 255 : R);
  541. G = G < 0 ? 0 : (G > 255 ? 255 : G);
  542. B = B < 0 ? 0 : (B > 255 ? 255 : B);
  543. *dst++ = R; // R
  544. *dst++ = G; // G
  545. *dst++ = B; // B
  546. *dst++ = 255; // A
  547. }
  548. }
  549. } break;
  550. default:
  551. // 对于其他格式,可以考虑使用FFmpeg的sws_scale函数
  552. qDebug() << "Unsupported pixel format:" << frame->format;
  553. return false;
  554. }
  555. m_frameUpdated = true;
  556. update();
  557. return true;
  558. }
  559. bool AVOpenGLWidget::Render(AVFrame* frame)
  560. {
  561. if (!m_initialized && isValid()) {
  562. makeCurrent();
  563. initializeGL();
  564. doneCurrent();
  565. }
  566. if (!frame) {
  567. update(); // 仅刷新显示
  568. return true;
  569. }
  570. bool result = convertFromAVFrame(frame);
  571. // 释放传入的AVFrame,因为现在使用QueuedConnection异步调用
  572. // 需要在这里释放内存,避免内存泄漏
  573. av_frame_free(&frame);
  574. return result;
  575. }
  576. void AVOpenGLWidget::clearFrame()
  577. {
  578. QMutexLocker locker(&m_mutex);
  579. m_frameUpdated = false;
  580. update();
  581. }
  582. void AVOpenGLWidget::setGray(bool on)
  583. {
  584. if (m_gray != on) {
  585. m_gray = on;
  586. update();
  587. }
  588. }
  589. void AVOpenGLWidget::setThreshold(bool on, float value)
  590. {
  591. if (m_threshold != on || m_thresholdValue != value) {
  592. m_threshold = on;
  593. m_thresholdValue = value;
  594. update();
  595. }
  596. }
  597. void AVOpenGLWidget::setBlur(bool on, float radius)
  598. {
  599. if (m_blur != on || m_blurRadius != radius) {
  600. m_blur = on;
  601. m_blurRadius = radius;
  602. update();
  603. }
  604. }
  605. void AVOpenGLWidget::setReverse(bool on)
  606. {
  607. if (m_reverse != on) {
  608. m_reverse = on;
  609. update();
  610. }
  611. }
  612. void AVOpenGLWidget::setColorReduce(bool on, int level)
  613. {
  614. if (m_colorReduce != on || m_colorReduceLevel != level) {
  615. m_colorReduce = on;
  616. m_colorReduceLevel = level;
  617. update();
  618. }
  619. }
  620. void AVOpenGLWidget::setGamma(bool on, float gamma)
  621. {
  622. if (m_gamma != on || m_gammaValue != gamma) {
  623. m_gamma = on;
  624. m_gammaValue = gamma;
  625. update();
  626. }
  627. }
  628. void AVOpenGLWidget::setContrastBright(bool on, float contrast, float brightness)
  629. {
  630. if (m_contrastBright != on || m_contrast != contrast || m_brightness != brightness) {
  631. m_contrastBright = on;
  632. m_contrast = contrast;
  633. m_brightness = brightness;
  634. update();
  635. }
  636. }
  637. void AVOpenGLWidget::setMirror(bool on)
  638. {
  639. if (m_mirror != on) {
  640. m_mirror = on;
  641. update();
  642. }
  643. }
  644. void AVOpenGLWidget::setNoVideoTip(const QString& tip)
  645. {
  646. if (m_noVideoTip != tip) {
  647. m_noVideoTip = tip;
  648. updateTipTexture();
  649. update();
  650. }
  651. }
  652. void AVOpenGLWidget::showEndTip(const QString& tip)
  653. {
  654. QMutexLocker locker(&m_mutex);
  655. m_noVideoTip = tip;
  656. if (m_tipTexture) {
  657. glDeleteTextures(1, &m_tipTexture);
  658. m_tipTexture = 0;
  659. }
  660. m_frameUpdated = false;
  661. if (m_frameData) {
  662. delete[] m_frameData;
  663. m_frameData = nullptr;
  664. }
  665. updateTipTexture();
  666. update();
  667. }
  668. void AVOpenGLWidget::onShowYUV(QSharedPointer<VideoFrame> frame)
  669. {
  670. if (!frame || frame->getPixelW() <= 0 || frame->getPixelH() <= 0) {
  671. return;
  672. }
  673. QMutexLocker locker(&m_mutex);
  674. uint32_t width = frame->getPixelW();
  675. uint32_t height = frame->getPixelH();
  676. // 如果尺寸变化,重新分配内存
  677. if (m_frameWidth != static_cast<int>(width) || m_frameHeight != static_cast<int>(height)) {
  678. if (m_frameData) {
  679. delete[] m_frameData;
  680. m_frameData = nullptr;
  681. }
  682. m_frameWidth = static_cast<int>(width);
  683. m_frameHeight = static_cast<int>(height);
  684. // 检查内存分配大小是否合理
  685. size_t dataSize = static_cast<size_t>(m_frameWidth) * m_frameHeight * 4;
  686. if (dataSize > 0 && dataSize < SIZE_MAX / 4) {
  687. try {
  688. m_frameData = new unsigned char[dataSize]; // RGBA格式
  689. } catch (const std::bad_alloc&) {
  690. m_frameData = nullptr;
  691. return;
  692. }
  693. } else {
  694. return;
  695. }
  696. }
  697. // 确保m_frameData已正确分配
  698. if (!m_frameData) {
  699. size_t dataSize = static_cast<size_t>(m_frameWidth) * m_frameHeight * 4;
  700. if (dataSize > 0 && dataSize < SIZE_MAX / 4) {
  701. try {
  702. m_frameData = new unsigned char[dataSize]; // RGBA格式
  703. } catch (const std::bad_alloc&) {
  704. m_frameData = nullptr;
  705. return;
  706. }
  707. } else {
  708. return;
  709. }
  710. }
  711. AVPixelFormat fmt = frame->getFormat();
  712. uint8_t* y = frame->getData(0);
  713. uint8_t* u = frame->getData(1);
  714. uint8_t* v = frame->getData(2);
  715. int lsY = frame->getLineSize(0);
  716. int lsU = frame->getLineSize(1);
  717. int lsV = frame->getLineSize(2);
  718. if (!y)
  719. return;
  720. auto clamp = [](int v) { return v < 0 ? 0 : (v > 255 ? 255 : v); };
  721. switch (fmt) {
  722. case AV_PIX_FMT_YUV420P:
  723. case AV_PIX_FMT_YUVJ420P:
  724. if (!u || !v)
  725. return;
  726. for (int yy = 0; yy < m_frameHeight; ++yy) {
  727. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  728. const uint8_t* srcY = y + yy * lsY;
  729. const uint8_t* srcU = u + (yy / 2) * lsU;
  730. const uint8_t* srcV = v + (yy / 2) * lsV;
  731. for (int xx = 0; xx < m_frameWidth; ++xx) {
  732. int Y = srcY[xx];
  733. int Uc = srcU[xx / 2];
  734. int Vc = srcV[xx / 2];
  735. int C = Y - 16, D = Uc - 128, E = Vc - 128;
  736. int R = (298 * C + 409 * E + 128) >> 8;
  737. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  738. int B = (298 * C + 516 * D + 128) >> 8;
  739. *dst++ = clamp(R);
  740. *dst++ = clamp(G);
  741. *dst++ = clamp(B);
  742. *dst++ = 255;
  743. }
  744. }
  745. break;
  746. case AV_PIX_FMT_YUV422P:
  747. if (!u || !v)
  748. return;
  749. for (int yy = 0; yy < m_frameHeight; ++yy) {
  750. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  751. const uint8_t* srcY = y + yy * lsY;
  752. const uint8_t* srcU = u + yy * lsU;
  753. const uint8_t* srcV = v + yy * lsV;
  754. for (int xx = 0; xx < m_frameWidth; ++xx) {
  755. int Y = srcY[xx];
  756. int Uc = srcU[xx / 2];
  757. int Vc = srcV[xx / 2];
  758. int C = Y - 16, D = Uc - 128, E = Vc - 128;
  759. int R = (298 * C + 409 * E + 128) >> 8;
  760. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  761. int B = (298 * C + 516 * D + 128) >> 8;
  762. *dst++ = clamp(R);
  763. *dst++ = clamp(G);
  764. *dst++ = clamp(B);
  765. *dst++ = 255;
  766. }
  767. }
  768. break;
  769. case AV_PIX_FMT_YUV444P:
  770. if (!u || !v)
  771. return;
  772. for (int yy = 0; yy < m_frameHeight; ++yy) {
  773. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  774. const uint8_t* srcY = y + yy * lsY;
  775. const uint8_t* srcU = u + yy * lsU;
  776. const uint8_t* srcV = v + yy * lsV;
  777. for (int xx = 0; xx < m_frameWidth; ++xx) {
  778. int Y = srcY[xx];
  779. int Uc = srcU[xx];
  780. int Vc = srcV[xx];
  781. int C = Y - 16, D = Uc - 128, E = Vc - 128;
  782. int R = (298 * C + 409 * E + 128) >> 8;
  783. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  784. int B = (298 * C + 516 * D + 128) >> 8;
  785. *dst++ = clamp(R);
  786. *dst++ = clamp(G);
  787. *dst++ = clamp(B);
  788. *dst++ = 255;
  789. }
  790. }
  791. break;
  792. case AV_PIX_FMT_NV12: // Y + UV interleaved
  793. if (!u)
  794. return;
  795. for (int yy = 0; yy < m_frameHeight; ++yy) {
  796. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  797. const uint8_t* srcY = y + yy * lsY;
  798. const uint8_t* srcUV = u + (yy / 2) * lsU;
  799. for (int xx = 0; xx < m_frameWidth; ++xx) {
  800. int Y = srcY[xx];
  801. int Uc = srcUV[(xx / 2) * 2 + 0];
  802. int Vc = srcUV[(xx / 2) * 2 + 1];
  803. int C = Y - 16, D = Uc - 128, E = Vc - 128;
  804. int R = (298 * C + 409 * E + 128) >> 8;
  805. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  806. int B = (298 * C + 516 * D + 128) >> 8;
  807. *dst++ = clamp(R);
  808. *dst++ = clamp(G);
  809. *dst++ = clamp(B);
  810. *dst++ = 255;
  811. }
  812. }
  813. break;
  814. case AV_PIX_FMT_NV21: // Y + VU interleaved
  815. if (!u)
  816. return;
  817. for (int yy = 0; yy < m_frameHeight; ++yy) {
  818. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  819. const uint8_t* srcY = y + yy * lsY;
  820. const uint8_t* srcVU = u + (yy / 2) * lsU;
  821. for (int xx = 0; xx < m_frameWidth; ++xx) {
  822. int Y = srcY[xx];
  823. int Vc = srcVU[(xx / 2) * 2 + 0];
  824. int Uc = srcVU[(xx / 2) * 2 + 1];
  825. int C = Y - 16, D = Uc - 128, E = Vc - 128;
  826. int R = (298 * C + 409 * E + 128) >> 8;
  827. int G = (298 * C - 100 * D - 208 * E + 128) >> 8;
  828. int B = (298 * C + 516 * D + 128) >> 8;
  829. *dst++ = clamp(R);
  830. *dst++ = clamp(G);
  831. *dst++ = clamp(B);
  832. *dst++ = 255;
  833. }
  834. }
  835. break;
  836. case AV_PIX_FMT_GRAY8:
  837. for (int yy = 0; yy < m_frameHeight; ++yy) {
  838. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  839. const uint8_t* srcY = y + yy * lsY;
  840. for (int xx = 0; xx < m_frameWidth; ++xx) {
  841. int Y = srcY[xx];
  842. *dst++ = Y;
  843. *dst++ = Y;
  844. *dst++ = Y;
  845. *dst++ = 255;
  846. }
  847. }
  848. break;
  849. case AV_PIX_FMT_RGB24:
  850. for (int yy = 0; yy < m_frameHeight; ++yy) {
  851. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  852. const uint8_t* src = y + yy * lsY;
  853. for (int xx = 0; xx < m_frameWidth; ++xx) {
  854. uint8_t r = src[xx * 3 + 0];
  855. uint8_t g = src[xx * 3 + 1];
  856. uint8_t b = src[xx * 3 + 2];
  857. *dst++ = r;
  858. *dst++ = g;
  859. *dst++ = b;
  860. *dst++ = 255;
  861. }
  862. }
  863. break;
  864. case AV_PIX_FMT_RGBA:
  865. for (int yy = 0; yy < m_frameHeight; ++yy) {
  866. uint8_t* dst = m_frameData + yy * m_frameWidth * 4;
  867. const uint8_t* src = y + yy * lsY;
  868. memcpy(dst, src, m_frameWidth * 4);
  869. }
  870. break;
  871. default:
  872. // 不支持的格式:尝试走现有 convertFromAVFrame 流程(如果可用),否则返回
  873. // 这里无法拿到 AVFrame*,先简单忽略
  874. return;
  875. }
  876. m_frameUpdated = true;
  877. update();
  878. }