videoframeprocessor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "videoframeprocessor.h"
  2. #include <QVideoSurfaceFormat>
  3. //#include "private/qvideoframe_p.h"
  4. VideoFrameProcessor::VideoFrameProcessor(QObject *parent) : QAbstractVideoSurface(parent)
  5. {
  6. hasFrame = false;
  7. // timer.setSingleShot(true);
  8. // connect(&timer, SIGNAL(timeout()), this, SLOT(OnFrameTimeout()));
  9. // timer.start(10000);
  10. }
  11. VideoFrameProcessor::~VideoFrameProcessor()
  12. {
  13. }
  14. bool VideoFrameProcessor::present(const QVideoFrame &frame)
  15. {
  16. if(frame.isValid())
  17. {
  18. QVideoFrame videoFrame(frame);
  19. if(videoFrame.map(QAbstractVideoBuffer::ReadOnly))
  20. {
  21. //curFrame = qt_imageFromVideoFrame(videoFrame);
  22. // curFrame = QImage(
  23. // videoFrame.bits(),
  24. // videoFrame.width(),
  25. // videoFrame.height(),
  26. // videoFrame.bytesPerLine(),
  27. // QVideoFrame::imageFormatFromPixelFormat(videoFrame.pixelFormat()));
  28. if(!hasFrame)
  29. {
  30. //disconnect(&timer, SIGNAL(timeout()), this, SLOT(OnFrameTimeout()));
  31. //timer.stop();
  32. mediaResolution = videoFrame.size();
  33. hasFrame = true;
  34. }
  35. emit newFrame(QImage(
  36. videoFrame.bits(),
  37. videoFrame.width(),
  38. videoFrame.height(),
  39. videoFrame.bytesPerLine(),
  40. QVideoFrame::imageFormatFromPixelFormat(videoFrame.pixelFormat())));
  41. }
  42. videoFrame.unmap();
  43. }
  44. return true;
  45. }
  46. QList<QVideoFrame::PixelFormat> VideoFrameProcessor::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
  47. {
  48. Q_UNUSED(handleType);
  49. return QList<QVideoFrame::PixelFormat>()
  50. << QVideoFrame::Format_RGB32
  51. << QVideoFrame::Format_ARGB32
  52. << QVideoFrame::Format_ARGB32_Premultiplied
  53. << QVideoFrame::Format_RGB565
  54. << QVideoFrame::Format_RGB555
  55. << QVideoFrame::Format_ARGB8565_Premultiplied
  56. << QVideoFrame::Format_RGB24;
  57. // << QVideoFrame::Format_BGRA32
  58. // << QVideoFrame::Format_BGRA32_Premultiplied
  59. // << QVideoFrame::Format_BGR32
  60. // << QVideoFrame::Format_BGR24
  61. // << QVideoFrame::Format_BGR565
  62. // << QVideoFrame::Format_BGR555
  63. // << QVideoFrame::Format_BGRA5658_Premultiplied
  64. // << QVideoFrame::Format_AYUV444
  65. // << QVideoFrame::Format_AYUV444_Premultiplied
  66. // << QVideoFrame::Format_YUV444
  67. // << QVideoFrame::Format_YUV420P
  68. // << QVideoFrame::Format_YV12
  69. // << QVideoFrame::Format_UYVY
  70. // << QVideoFrame::Format_YUYV
  71. // << QVideoFrame::Format_NV12
  72. // << QVideoFrame::Format_NV21
  73. // << QVideoFrame::Format_IMC1
  74. // << QVideoFrame::Format_IMC2
  75. // << QVideoFrame::Format_IMC3
  76. // << QVideoFrame::Format_IMC4
  77. // << QVideoFrame::Format_Y8
  78. // << QVideoFrame::Format_Y16
  79. // << QVideoFrame::Format_Jpeg
  80. // << QVideoFrame::Format_CameraRaw
  81. // << QVideoFrame::Format_AdobeDng;
  82. }
  83. bool VideoFrameProcessor::isFormatSupported(const QVideoSurfaceFormat &format) const
  84. {
  85. const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
  86. const QSize size = format.frameSize();
  87. return imageFormat != QImage::Format_Invalid
  88. && !size.isEmpty()
  89. && format.handleType() == QAbstractVideoBuffer::NoHandle;
  90. }
  91. void VideoFrameProcessor::OnFrameTimeout()
  92. {
  93. if(!hasFrame)
  94. {
  95. mediaResolution.setWidth(640);
  96. mediaResolution.setHeight(480);
  97. hasFrame = true;
  98. }
  99. }