forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoFrame.cpp
401 lines (347 loc) · 10.7 KB
/
VideoFrame.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/VideoFrame.h"
#include "QtAV/private/Frame_p.h"
#include "QtAV/ImageConverter.h"
#include "QtAV/ImageConverterTypes.h"
#include "QtAV/SurfaceInterop.h"
#include "QtAV/private/AVCompat.h"
#include <QtCore/QSharedPointer>
#include <QtGui/QImage>
// FF_API_PIX_FMT
#ifdef PixelFormat
#undef PixelFormat
#endif
namespace QtAV {
class VideoFramePrivate : public FramePrivate
{
public:
VideoFramePrivate()
: FramePrivate()
, width(0)
, height(0)
, displayAspectRatio(0)
, format(VideoFormat::Format_Invalid)
, textures(4, 0)
, conv(0)
{}
VideoFramePrivate(int w, int h, const VideoFormat& fmt)
: FramePrivate()
, width(w)
, height(h)
, displayAspectRatio(0)
, format(fmt)
, textures(4, 0)
, conv(0)
{
planes.resize(format.planeCount());
line_sizes.resize(format.planeCount());
textures.resize(format.planeCount());
}
~VideoFramePrivate() {}
bool convertTo(const VideoFormat& fmt) {
return convertTo(fmt.pixelFormatFFmpeg());
}
bool convertTo(VideoFormat::PixelFormat fmt) {
return convertTo(VideoFormat::pixelFormatToFFmpeg(fmt));
}
bool convertTo(QImage::Format fmt) {
return convertTo(VideoFormat::pixelFormatFromImageFormat(fmt));
}
bool convertTo(int fffmt) {
if (fffmt == format.pixelFormatFFmpeg())
return true;
if (!conv)
return false;
conv->setInFormat(format.pixelFormatFFmpeg());
conv->setOutFormat(fffmt);
conv->setInSize(width, height);
conv->setOutSize(width, height);
if (!conv->convert(planes.constData(), line_sizes.constData())) {
format.setPixelFormat(VideoFormat::Format_Invalid);
return false;
}
format.setPixelFormatFFmpeg(fffmt);
data = conv->outData();
planes = conv->outPlanes();
line_sizes = conv->outLineSizes();
planes.resize(format.planeCount());
line_sizes.resize(format.planeCount());
textures.resize(format.planeCount());
return true;
}
bool convertTo(const VideoFormat& fmt, const QSizeF &dstSize, const QRectF &roi) {
if (fmt == format.pixelFormatFFmpeg()
&& roi == QRectF(0, 0, width, height)
&& dstSize == roi.size())
return true;
if (!conv) {
format.setPixelFormat(VideoFormat::Format_Invalid);
return false;
}
format = fmt;
data = conv->outData();
planes = conv->outPlanes();
line_sizes = conv->outLineSizes();
planes.resize(fmt.planeCount());
line_sizes.resize(fmt.planeCount());
textures.resize(fmt.planeCount());
return false;
}
int width, height;
float displayAspectRatio;
VideoFormat format;
QVector<int> textures;
ImageConverter *conv;
VideoSurfaceInteropPtr surface_interop;
};
VideoFrame::VideoFrame()
: Frame(*new VideoFramePrivate())
{
}
VideoFrame::VideoFrame(int width, int height, const VideoFormat &format)
: Frame(*new VideoFramePrivate(width, height, format))
{
}
VideoFrame::VideoFrame(const QByteArray& data, int width, int height, const VideoFormat &format)
: Frame(*new VideoFramePrivate(width, height, format))
{
Q_D(VideoFrame);
d->data = data;
}
VideoFrame::VideoFrame(const QVector<int>& textures, int width, int height, const VideoFormat &format)
: Frame(*new VideoFramePrivate(width, height, format))
{
Q_D(VideoFrame);
d->textures = textures;
}
VideoFrame::VideoFrame(const QImage& image)
: Frame(*new VideoFramePrivate(image.width(), image.height(), VideoFormat(image.format())))
{
// TODO: call const image.bits()?
setBits((uchar*)image.bits(), 0);
setBytesPerLine(image.bytesPerLine(), 0);
}
/*!
Constructs a shallow copy of \a other. Since VideoFrame is
explicitly shared, these two instances will reflect the same frame.
*/
VideoFrame::VideoFrame(const VideoFrame &other)
: Frame(other)
{
}
/*!
Assigns the contents of \a other to this video frame. Since VideoFrame is
explicitly shared, these two instances will reflect the same frame.
*/
VideoFrame &VideoFrame::operator =(const VideoFrame &other)
{
d_ptr = other.d_ptr;
return *this;
}
VideoFrame::~VideoFrame()
{
}
int VideoFrame::channelCount() const
{
Q_D(const VideoFrame);
if (!d->format.isValid())
return 0;
return d->format.channels();
}
VideoFrame VideoFrame::clone() const
{
Q_D(const VideoFrame);
if (!d->format.isValid())
return VideoFrame();
int bytes = 0;
for (int i = 0; i < d->format.planeCount(); i) {
bytes = bytesPerLine(i)*planeHeight(i);
}
QByteArray buf(bytes, 0);
char *dst = buf.data(); //must before buf is shared, otherwise data will be detached.
VideoFrame f(buf, width(), height(), d->format);
for (int i = 0; i < d->format.planeCount(); i) {
f.setBits((quint8*)dst, i);
f.setBytesPerLine(bytesPerLine(i), i);
const int plane_size = bytesPerLine(i)*planeHeight(i);
memcpy(dst, bits(i), plane_size);
dst = plane_size;
}
f.setDisplayAspectRatio(d->displayAspectRatio);
//f.setImageConverter(d->conv);
return f;
}
int VideoFrame::allocate()
{
Q_D(VideoFrame);
if (pixelFormatFFmpeg() == QTAV_PIX_FMT_C(NONE) || width() <=0 || height() <= 0) {
qWarning("Not valid format(%s) or size(%dx%d)", qPrintable(format().name()), width(), height());
return 0;
}
#if 0
const int align = 16;
int bytes = av_image_get_buffer_size((AVPixelFormat)d->format.pixelFormatFFmpeg(), width(), height(), align);
d->data.resize(bytes);
av_image_fill_arrays(d->planes.data(), d->line_sizes.data()
, (const uint8_t*)d->data.constData()
, (AVPixelFormat)d->format.pixelFormatFFmpeg()
, width(), height(), align);
return bytes;
#endif
int bytes = avpicture_get_size((AVPixelFormat)pixelFormatFFmpeg(), width(), height());
if (d->data.size() < bytes) {
d->data = QByteArray(bytes, 0);
}
init();
return bytes;
}
VideoFormat VideoFrame::format() const
{
return d_func()->format;
}
VideoFormat::PixelFormat VideoFrame::pixelFormat() const
{
return d_func()->format.pixelFormat();
}
QImage::Format VideoFrame::imageFormat() const
{
return d_func()->format.imageFormat();
}
int VideoFrame::pixelFormatFFmpeg() const
{
return d_func()->format.pixelFormatFFmpeg();
}
bool VideoFrame::isValid() const
{
Q_D(const VideoFrame);
return d->width > 0 && d->height > 0 && d->format.isValid(); //data not empty?
}
QSize VideoFrame::size() const
{
Q_D(const VideoFrame);
return QSize(d->width, d->height);
}
int VideoFrame::width() const
{
return d_func()->width;
}
int VideoFrame::height() const
{
return d_func()->height;
}
int VideoFrame::effectivePlaneWidth(int plane) const
{
Q_D(const VideoFrame);
return effectiveBytesPerLine(plane)/d->format.bytesPerPixel(plane); //padded bpl?
}
int VideoFrame::planeWidth(int plane) const
{
Q_D(const VideoFrame);
return bytesPerLine(plane)/d->format.bytesPerPixel(plane);
}
int VideoFrame::planeHeight(int plane) const
{
Q_D(const VideoFrame);
if (plane == 0)
return d->height;
return d->format.chromaHeight(d->height);
}
float VideoFrame::displayAspectRatio() const
{
Q_D(const VideoFrame);
if (d->displayAspectRatio > 0)
return d->displayAspectRatio;
if (d->width > 0 && d->height > 0)
return (float)d->width / (float)d->height;
return 1;
}
void VideoFrame::setDisplayAspectRatio(float displayAspectRatio)
{
d_func()->displayAspectRatio = displayAspectRatio;
}
int VideoFrame::effectiveBytesPerLine(int plane) const
{
Q_D(const VideoFrame);
return d->format.bytesPerLine(width(), plane);
}
void VideoFrame::setImageConverter(ImageConverter *conv)
{
d_func()->conv = conv;
}
bool VideoFrame::convertTo(const VideoFormat& fmt)
{
Q_D(VideoFrame);
if (fmt == d->format) //TODO: check whether own the data
return true;
return d->convertTo(fmt);
}
bool VideoFrame::convertTo(VideoFormat::PixelFormat fmt)
{
return d_func()->convertTo(fmt);
}
bool VideoFrame::convertTo(QImage::Format fmt)
{
return d_func()->convertTo(fmt);
}
bool VideoFrame::convertTo(int fffmt)
{
return d_func()->convertTo(fffmt);
}
bool VideoFrame::convertTo(const VideoFormat& fmt, const QSizeF &dstSize, const QRectF &roi)
{
return d_func()->convertTo(fmt, dstSize, roi);
}
void *VideoFrame::map(SurfaceType type, void *handle, int plane)
{
Q_D(VideoFrame);
const QVariant v = d->metadata.value("surface_interop");
if (!v.isValid())
return 0;
d->surface_interop = v.value<VideoSurfaceInteropPtr>();
if (!d->surface_interop)
return 0;
if (plane > planeCount())
return 0;
return d->surface_interop->map(type, format(), handle, plane);
}
void VideoFrame::unmap(void *handle)
{
Q_D(VideoFrame);
if (!d->surface_interop)
return;
d->surface_interop->unmap(handle);
}
int VideoFrame::texture(int plane) const
{
Q_D(const VideoFrame);
if (d->textures.size() <= plane)
return -1;
return d->textures[plane];
}
void VideoFrame::init()
{
Q_D(VideoFrame);
AVPicture picture;
AVPixelFormat fff = (AVPixelFormat)d->format.pixelFormatFFmpeg();
//int bytes = avpicture_get_size(fff, width(), height());
//d->data.resize(bytes);
avpicture_fill(&picture, (uint8_t*)d->data.constData(), fff, width(), height());
setBits(picture.data);
setBytesPerLine(picture.linesize);
}
} //namespace QtAV