-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcvencoderrunable.cpp
217 lines (150 loc) · 6.64 KB
/
bcvencoderrunable.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "bcvencoderrunable.h"
#include "bcv_file.h"
#include<QDebug>
#include <QOpenGLFramebufferObject>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QGLWidget>
#include <QDir>
#include <iostream>
#include <QDataStream>
using namespace std;
bcvencoderrunable::bcvencoderrunable( BCVEncoder* bcvencoder ): QVideoFilterRunnable()
{
m_bcvencoder = bcvencoder;
}
//float clamp( float value, int lv, int hv ){
// if( value < lv )
// value = lv;
// else if( value > hv )
// value = hv;
// return value;
//}
//quint32 turnRGB2int( int r, int g, int b ){
// quint32 val;
// val = r;
// val = ( val << 8 ) + g;
// val = ( val << 8 ) + b;
// return val;
//}
//QRgb convertYUVtoRGB( QDataStream& out, int y, int u, int v ){
// float r = ( (float)y + 1.402f*(float)v );
// float g = ( (float)y - 0.344f*(float)u - 0.714f*(float)v );
// float b = ( (float)y + 1.772f*(float)u );
// int int_r = clamp( r, 0, 255 );
// int int_g = clamp( g, 0, 255 );
// int int_b = clamp( b, 0, 255 );
// //qint32 rgb = turnRGB2int( int_r, int_g, int_b );
// //out << rgb;
// return qRgb( int_r, int_g, int_b );
//}
QVideoFrame bcvencoderrunable::run( QVideoFrame *input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags ){
Q_UNUSED(surfaceFormat);
Q_UNUSED(flags);
//initialize frame header
// QDataStream out(&m_bcvencoder->file);
_bcv_video_frame frameHeader;
frameHeader.index = (uint32_t)m_bcvencoder->m_totalFrame;
frameHeader.hr_bpm = (int16_t)120;
frameHeader.rr_bpm = (int16_t)60;
frameHeader.interval = (uint32_t)10;
frameHeader.lamda = (uint16_t) 3;
frameHeader.eb_ts = (int8_t)0;
memset( frameHeader.reserved, '0', sizeof(uint8_t) * 17 ); // 17 = 32 - 15 ( meaningful information )
m_bcvencoder->frameHeaderList.push_back(frameHeader);
if( input->isValid() ){
//m_bcvencoder->frameList.push_back(input);
// QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
// /*If source of the videoOuput is Media, then the handleType will be openGL texture ID which cannot be mapped,
// * so should do more to process it, not finished yet*/
if( input->handleType() == QAbstractVideoBuffer::GLTextureHandle ){
qDebug() << "handle type is GLTextureHandle";
// GLuint texture;
// texture = input->handle().toUInt();
// f->glBindTexture(GL_TEXTURE_2D, texture);
// glBindTexture(GL_TEXTURE_2D, texture ); // Set as the current texture
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// QImage im("test.png");
// QImage tex = QGLWidget::convertToGLFormat(im);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
// bool result = tex.save("test.png");
// if( result )
// printf("file save successful\n");
// else
// printf("file save fail\n");
}else if( input->handleType() == QAbstractVideoBuffer::NoHandle ){
bool mapResult = input->map(QAbstractVideoBuffer::ReadOnly);
if( !mapResult )
printf("map error\n");
if( input->pixelFormat() == QVideoFrame::Format_NV12 ){
int size = input->width() * input->height();
int halfSize = size/2;
unsigned char* tmpY;
unsigned char* tmpUV;
tmpY = (unsigned char*)malloc( size );
tmpUV = (unsigned char*)malloc( halfSize );
memcpy( (char*)tmpY, input->bits(0), size );
memcpy( (char*)tmpUV, input->bits(1), halfSize );
m_bcvencoder->YList.push_back( tmpY );
m_bcvencoder->UVList.push_back( tmpUV );
// int width = input->width();
// unsigned char Y[size];
// unsigned char UV[halfSize];
// for( int i=0; i<size; i++ ){
// Y[i] = input->bits(0)[i];
// }
// for( int i=0; i<halfSize; i++ ){
// UV[i] = input->bits(1)[i];
// }
// out.writeRawData( (char*)Y, size );
// out.writeRawData( (char*)UV, halfSize );
// QImage img( input->width(), input->height(), QImage::Format_RGB32 );
// img.fill(QColor(Qt::white).rgb());
// for( int i=0, k=0, y=0, x=0; i< size; i+=2, k+=2 ){
// //i ->pointer of Y data
// //k ->pointer of UV data
// //x, y ->position of image
// int y1 = input->bits(0)[i];
// int y2 = input->bits(0)[i+1];
// int y3 = input->bits(0)[i+input->width()];
// int y4 = input->bits(0)[i+input->width()+1];
// int u = input->bits(1)[k];
// int v = input->bits(1)[k+1];
// u -= 128;
// v -= 128;
// img.setPixel( x, y, convertYUVtoRGB( out, y1, u, v ) );
// img.setPixel( x, y+1, convertYUVtoRGB( out, y2, u, v ) );
// img.setPixel( x+1, y, convertYUVtoRGB( out, y3, u, v ) );
// img.setPixel( x+1, y+1, convertYUVtoRGB( out, y4, u, v ) );
// if( i!=0 && ((i+2) % input->width()) == 0 ){
// //cross a line
// i+= width;
// }
// x += 2;
// if( x == width ){
// x = 0;
// y += 2;
// }
// }
// QString dir = QDir::currentPath().append( "/test" +QString::number( m_bcvencoder->m_totalFrame ) + ".png" );
// //qDebug() << "save dir = " << dir;
// bool result = img.save( dir );
// if( !result )
// printf("file save fail\n");
// fflush(stdout);
if( input->isMapped() )
input->unmap();
}else{
qDebug() << "non support image format " << input->pixelFormat();
}
}else{
qDebug() << " non supprt handle type " << input->handleType();
}
// fflush(stdout);
m_bcvencoder->m_totalFrame++;
return *input;
}
}