-
Notifications
You must be signed in to change notification settings - Fork 10
/
QVideoFrameBits.cpp
142 lines (125 loc) · 4.37 KB
/
QVideoFrameBits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "QVideoFrameBits.h"
#include <QDebug>
QVideoFrameBits::QVideoFrameBits( QImage* image, QVideoFrame* videoFrame, const QVideoSurfaceFormat& surfaceFormat, int videoOutputOrientation ) :
m_SourceBits( nullptr ),
m_SourceTopToBottom( true ),
m_SourceSize( 0, 0 ),
m_SourceDelta( QPoint( 4, 0 ) ),
m_SourceClipRect( 0, 0, 0, 0 ),
m_VideoOutputOrientation( 0 ),
m_Mirror( false ),
m_TransformBits( nullptr ),
m_TransformSize( 0, 0 ),
m_TransformDelta( 4, 0 )
{
Q_UNUSED( surfaceFormat )
if ( image )
{
setSource( *image );
}
setVideoOutputOrientation( videoOutputOrientation );
}
void QVideoFrameBits::setSource( uchar* bits, const QSize& sourceSize, const QPoint& sourceDelta, bool topToBottom )
{
m_SourceBits = bits;
m_SourceSize = sourceSize;
m_SourceDelta = sourceDelta;
m_SourceTopToBottom = topToBottom;
update();
}
void QVideoFrameBits::setSource( QImage& image, QVideoFrame* videoFrame, const QVideoSurfaceFormat& surfaceFormat )
{
#ifdef Q_OS_ANDROID
bool topToBottom = true;
#else
bool topToBottom = ( surfaceFormat.scanLineDirection() == QVideoSurfaceFormat::TopToBottom );
#endif
setSource(
image.bits(),
QSize( image.width(), image.height() ),
QPoint( image.bytesPerLine() / image.width(), image.bytesPerLine() ),
topToBottom
);
}
void QVideoFrameBits::setSourceClipRect( const QRect& sourceClipRect )
{
m_SourceClipRect = sourceClipRect;
update();
}
void QVideoFrameBits::setVideoOutputOrientation( int videoOutputOrientation )
{
m_VideoOutputOrientation = videoOutputOrientation;
update();
}
void QVideoFrameBits::setMirror( bool mirror )
{
m_Mirror = mirror;
update();
}
void QVideoFrameBits::update()
{
if ( !m_SourceBits )
{
m_TransformSize = QSize( 0, 0 );
m_TransformBits = nullptr;
m_TransformDelta = QPoint( 0, 0 );
return;
}
const bool& mirror = m_Mirror;
const int clipX = !m_SourceClipRect.isEmpty() ? m_SourceClipRect.x() : 0;
const int clipY = !m_SourceClipRect.isEmpty() ? m_SourceClipRect.y() : 0;
const int clipWidth = !m_SourceClipRect.isEmpty() ? m_SourceClipRect.width() : m_SourceSize.width();
const int clipHeight = !m_SourceClipRect.isEmpty() ? m_SourceClipRect.height() : m_SourceSize.height();
const int& sourceDeltaX = m_SourceDelta.x();
const int& sourceDeltaY = m_SourceDelta.y();
switch ( m_VideoOutputOrientation )
{
case 0:
default:
m_TransformSize = QSize( clipWidth, clipHeight );
m_TransformBits = m_SourceBits + ( !mirror ? clipX : clipX + clipWidth - 1 ) * sourceDeltaX + ( clipY ) * sourceDeltaY;
m_TransformDelta = QPoint( !mirror ? sourceDeltaX : -sourceDeltaX, sourceDeltaY );
break;
case 90:
m_TransformSize = QSize( clipHeight, clipWidth );
m_TransformBits = m_SourceBits + ( !mirror ? clipX + clipWidth - 1 : clipX ) * sourceDeltaX + ( clipY ) * sourceDeltaY;
m_TransformDelta = QPoint( sourceDeltaY, !mirror ? -sourceDeltaX : sourceDeltaX );
break;
case 180:
m_TransformSize = QSize( clipWidth, clipHeight );
m_TransformBits = m_SourceBits + ( !mirror ? clipX + clipWidth - 1 : clipX ) * sourceDeltaX + ( clipY + clipHeight - 1 ) * sourceDeltaY;
m_TransformDelta = QPoint( !mirror ? -sourceDeltaX : sourceDeltaX, -sourceDeltaY );
break;
case 270:
m_TransformSize = QSize( clipHeight, clipWidth );
m_TransformBits = m_SourceBits + ( !mirror ? clipX : clipX + clipWidth - 1 ) * sourceDeltaX + ( clipY + clipHeight - 1 ) * sourceDeltaY;
m_TransformDelta = QPoint( -sourceDeltaY, !mirror ? sourceDeltaX : -sourceDeltaX );
break;
}
}
void QVideoFrameBits::getScanLine( int y, uchar* bits )
{
if ( !m_TransformBits )
{
return;
}
if ( m_SourceTopToBottom )
{
y = m_SourceSize.height() - 1 - y;
}
uchar* src = m_TransformBits + y * m_TransformDelta.y();
for ( int x = 0; x < m_TransformSize.width(); x++ )
{
memcpy( bits, src, 4 );
bits += 4;
src += m_TransformDelta.x();
}
}
uchar* QVideoFrameBits::getPixelAddress( int x, int y )
{
if ( !m_TransformBits )
{
return nullptr;
}
return m_TransformBits + y * m_TransformDelta.y() + x * m_TransformDelta.x();
}