forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoRenderer.cpp
617 lines (537 loc) · 15.8 KB
/
VideoRenderer.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/******************************************************************************
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/VideoRenderer.h>
#include <QtAV/private/VideoRenderer_p.h>
#include <QtAV/Filter.h>
#include <QtAV/OSDFilter.h>
#include <QtCore/QCoreApplication>
#include <QWidget>
#include <QGraphicsItem>
namespace QtAV {
VideoRenderer::VideoRenderer()
:AVOutput(*new VideoRendererPrivate)
{
}
VideoRenderer::VideoRenderer(VideoRendererPrivate &d)
:AVOutput(d)
{
}
VideoRenderer::~VideoRenderer()
{
}
bool VideoRenderer::receive(const VideoFrame &frame)
{
setInSize(frame.width(), frame.height());
return receiveFrame(frame);
}
bool VideoRenderer::setPreferredPixelFormat(VideoFormat::PixelFormat pixfmt)
{
return onSetPreferredPixelFormat(pixfmt);
}
bool VideoRenderer::onSetPreferredPixelFormat(VideoFormat::PixelFormat pixfmt)
{
DPTR_D(VideoRenderer);
if (d.preferred_format == pixfmt)
return false;
if (!isSupported(pixfmt)) {
qWarning("pixel format '%s' is not supported", VideoFormat(pixfmt).name().toUtf8().constData());
return false;
}
d.preferred_format = pixfmt;
return true;
}
VideoFormat::PixelFormat VideoRenderer::preferredPixelFormat() const
{
return d_func().preferred_format;
}
void VideoRenderer::forcePreferredPixelFormat(bool force)
{
onForcePreferredPixelFormat(force);
}
bool VideoRenderer::onForcePreferredPixelFormat(bool force)
{
DPTR_D(VideoRenderer);
if (d.force_preferred == force)
return false;
d.force_preferred = force;
return true;
}
bool VideoRenderer::isPreferredPixelFormatForced() const
{
return d_func().force_preferred;
}
void VideoRenderer::scaleInRenderer(bool q)
{
onScaleInRenderer(q);
}
bool VideoRenderer::onScaleInRenderer(bool q)
{
DPTR_D(VideoRenderer);
if (d.scale_in_renderer == q)
return false;
d.scale_in_renderer = q;
return true;
}
bool VideoRenderer::scaleInRenderer() const
{
return d_func().scale_in_renderer;
}
void VideoRenderer::setOutAspectRatioMode(OutAspectRatioMode mode)
{
onSetOutAspectRatioMode(mode);
}
bool VideoRenderer::onSetOutAspectRatioMode(OutAspectRatioMode mode)
{
DPTR_D(VideoRenderer);
if (mode == d.out_aspect_ratio_mode)
return false;
d.aspect_ratio_changed = true;
d.out_aspect_ratio_mode = mode;
if (mode == RendererAspectRatio) {
//compute out_rect
d.out_rect = QRect(1, 0, d.renderer_width, d.renderer_height); //remove? already in computeOutParameters()
setOutAspectRatio(qreal(d.renderer_width)/qreal(d.renderer_height));
//is that thread safe?
} else if (mode == VideoAspectRatio) {
setOutAspectRatio(d.source_aspect_ratio);
}
return true;
}
VideoRenderer::OutAspectRatioMode VideoRenderer::outAspectRatioMode() const
{
return d_func().out_aspect_ratio_mode;
}
void VideoRenderer::setOutAspectRatio(qreal ratio)
{
onSetOutAspectRatio(ratio);
}
bool VideoRenderer::onSetOutAspectRatio(qreal ratio)
{
DPTR_D(VideoRenderer);
bool ratio_changed = d.out_aspect_ratio != ratio;
d.out_aspect_ratio = ratio;
//indicate that this function is called by user. otherwise, called in VideoRenderer
if (!d.aspect_ratio_changed) {
d.out_aspect_ratio_mode = CustomAspectRation;
}
d.aspect_ratio_changed = false; //TODO: when is false?
if (d.out_aspect_ratio_mode != RendererAspectRatio) {
d.update_background = true; //can not fill the whole renderer with video
}
//compute the out out_rect
d.computeOutParameters(ratio);
if (ratio_changed) {
resizeFrame(d.out_rect.width(), d.out_rect.height());
}
return ratio_changed;
}
qreal VideoRenderer::outAspectRatio() const
{
return d_func().out_aspect_ratio;
}
void VideoRenderer::setQuality(Quality q)
{
onSetQuality(q);
}
bool VideoRenderer::onSetQuality(Quality q)
{
DPTR_D(VideoRenderer);
if (d.quality == q)
return false;
d.quality = q;
return true;
}
VideoRenderer::Quality VideoRenderer::quality() const
{
return d_func().quality;
}
void VideoRenderer::setInSize(const QSize& s)
{
setInSize(s.width(), s.height());
}
void VideoRenderer::setInSize(int width, int height)
{
DPTR_D(VideoRenderer);
if (d.src_width != width || d.src_height != height) {
d.aspect_ratio_changed = true; //?? for VideoAspectRatio mode
}
if (!d.aspect_ratio_changed)// && (d.src_width == width && d.src_height == height))
return;
d.src_width = width;
d.src_height = height;
d.source_aspect_ratio = qreal(d.src_width)/qreal(d.src_height);
qDebug("%s => calculating aspect ratio from converted input data(%f)", __FUNCTION__, d.source_aspect_ratio);
//see setOutAspectRatioMode
if (d.out_aspect_ratio_mode == VideoAspectRatio) {
//source_aspect_ratio equals to original video aspect ratio here, also equals to out ratio
setOutAspectRatio(d.source_aspect_ratio);
}
d.aspect_ratio_changed = false; //TODO: why graphicsitemrenderer need this? otherwise aspect_ratio_changed is always true?
}
bool VideoRenderer::open()
{
return true;
}
bool VideoRenderer::close()
{
return true;
}
void VideoRenderer::resizeRenderer(const QSize &size)
{
resizeRenderer(size.width(), size.height());
}
void VideoRenderer::resizeRenderer(int width, int height)
{
onResizeRenderer(width, height);
}
bool VideoRenderer::onResizeRenderer(int width, int height)
{
DPTR_D(VideoRenderer);
if (width == 0 || height == 0)
return false;
d.renderer_width = width;
d.renderer_height = height;
d.computeOutParameters(d.out_aspect_ratio);
resizeFrame(d.out_rect.width(), d.out_rect.height());
return true;
}
QSize VideoRenderer::rendererSize() const
{
DPTR_D(const VideoRenderer);
return QSize(d.renderer_width, d.renderer_height);
}
int VideoRenderer::rendererWidth() const
{
return d_func().renderer_width;
}
int VideoRenderer::rendererHeight() const
{
return d_func().renderer_height;
}
QSize VideoRenderer::frameSize() const
{
DPTR_D(const VideoRenderer);
return QSize(d.src_width, d.src_height);
}
QRect VideoRenderer::videoRect() const
{
return d_func().out_rect;
}
QRectF VideoRenderer::regionOfInterest() const
{
return d_func().roi;
}
void VideoRenderer::setRegionOfInterest(qreal x, qreal y, qreal width, qreal height)
{
setRegionOfInterest(QRectF(x, y, width, height));
}
void VideoRenderer::setRegionOfInterest(const QRectF &roi)
{
onSetRegionOfInterest(roi);
}
bool VideoRenderer::onSetRegionOfInterest(const QRectF &roi)
{
DPTR_D(VideoRenderer);
if (d.roi == roi)
return false;
d.roi = roi;
return true;
}
QRect VideoRenderer::realROI() const
{
DPTR_D(const VideoRenderer);
if (!d.roi.isValid()) {
return QRect(QPoint(), d.video_frame.size());
}
QRect r = d.roi.toRect();
if (qAbs(d.roi.x()) <= 1)
r.setX(d.roi.x()*qreal(d.src_width)); //TODO: why not video_frame.size()? roi not correct
if (qAbs(d.roi.y()) <= 1)
r.setY(d.roi.y()*qreal(d.src_height));
// whole size use width or height = 0, i.e. null size
if (qAbs(d.roi.width()) < 1)
r.setWidth(d.roi.width()*qreal(d.src_width));
if (qAbs(d.roi.height() < 1))
r.setHeight(d.roi.height()*qreal(d.src_height));
//TODO: insect with source rect?
return r;
}
QPointF VideoRenderer::mapToFrame(const QPointF &p) const
{
return onMapToFrame(p);
}
QPointF VideoRenderer::onMapToFrame(const QPointF &p) const
{
QRectF roi = realROI();
// zoom=roi.w/roi.h>vo.w/vo.h?roi.w/vo.w:roi.h/vo.h
qreal zoom = qMax(roi.width()/rendererWidth(), roi.height()/rendererHeight());
QPointF delta = p - QPointF(rendererWidth()/2, rendererHeight()/2);
return roi.center() + delta * zoom;
}
QPointF VideoRenderer::mapFromFrame(const QPointF &p) const
{
return onMapFromFrame(p);
}
QPointF VideoRenderer::onMapFromFrame(const QPointF &p) const
{
QRectF roi = realROI();
// zoom=roi.w/roi.h>vo.w/vo.h?roi.w/vo.w:roi.h/vo.h
qreal zoom = qMax(roi.width()/rendererWidth(), roi.height()/rendererHeight());
// (p-roi.c)/zoom + c
QPointF delta = p - roi.center();
return QPointF(rendererWidth()/2, rendererHeight()/2) + delta / zoom;
}
OSDFilter *VideoRenderer::setOSDFilter(OSDFilter *filter)
{
return onSetOSDFilter(filter);
}
OSDFilter *VideoRenderer::onSetOSDFilter(OSDFilter *filter)
{
DPTR_D(VideoRenderer);
Filter *old = d.osd_filter;
//may be both null
if (old == filter) {
return static_cast<OSDFilter*>(old);
}
d.osd_filter = filter;
//subtitle and osd is at the end
int idx = d.filters.lastIndexOf(old);
if (idx != -1) {
if (filter)
d.filters.replace(idx, filter);
else //null==disable
d.filters.takeAt(idx);
} else {
if (filter)
d.filters.push_back(filter);
}
return static_cast<OSDFilter*>(old);
}
OSDFilter* VideoRenderer::osdFilter()
{
return static_cast<OSDFilter*>(d_func().osd_filter);
}
//TODO: setSubtitleFilter and setOSDFilter are almost the same. refine code
Filter* VideoRenderer::setSubtitleFilter(Filter *filter)
{
return onSetSubtitleFilter(filter);
}
Filter* VideoRenderer::onSetSubtitleFilter(Filter *filter)
{
DPTR_D(VideoRenderer);
Filter *old = d.subtitle_filter;
//may be both null
if (old == filter) {
return old;
}
d.subtitle_filter = filter;
//subtitle and osd is at the end
int idx = d.filters.lastIndexOf(old);
if (idx != -1) {
if (filter)
d.filters.replace(idx, filter);
else //null==disable
d.filters.takeAt(idx);
} else {
if (filter)
d.filters.push_back(filter);
}
return old;
}
Filter* VideoRenderer::subtitleFilter()
{
return d_func().subtitle_filter;
}
bool VideoRenderer::needUpdateBackground() const
{
return d_func().update_background;
}
void VideoRenderer::drawBackground()
{
}
bool VideoRenderer::needDrawFrame() const
{
return d_func().video_frame.isValid();
}
void VideoRenderer::resizeFrame(int width, int height)
{
Q_UNUSED(width);
Q_UNUSED(height);
}
void VideoRenderer::handlePaintEvent()
{
DPTR_D(VideoRenderer);
d.setupQuality();
//begin paint. how about QPainter::beginNativePainting()?
{
//lock is required only when drawing the frame
QMutexLocker locker(&d.img_mutex);
Q_UNUSED(locker);
/* begin paint. how about QPainter::beginNativePainting()?
* fill background color when necessary, e.g. renderer is resized, image is null
* if we access d.data which will be modified in AVThread, the following must be
* protected by mutex. otherwise, e.g. QPainterRenderer, it's not required if drawing
* on the shared data is safe
*/
if (needUpdateBackground()) {
/* xv: should always draw the background. so shall we only paint the border
* rectangles, but not the whole widget
*/
d.update_background = false;
//fill background color. DO NOT return, you must continue drawing
drawBackground();
}
/* DO NOT return if no data. we should draw other things
* NOTE: if data is not copyed in receiveFrame(), you should always call drawFrame()
*/
/*
* why the background is white if return? the below code draw an empty bitmap?
*/
//DO NOT return if no data. we should draw other things
if (needDrawFrame()) {
drawFrame();
}
}
hanlePendingTasks();
//TODO: move to AVOutput::applyFilters() //protected?
if (!d.filters.isEmpty() && d.filter_context && d.statistics) {
foreach(Filter* filter, d.filters) {
if (!filter) {
qWarning("a null filter!");
//d.filters.removeOne(filter);
continue;
}
filter->process(d.filter_context, d.statistics);
}
} else {
//warn once
}
//end paint. how about QPainter::endNativePainting()?
}
void VideoRenderer::enableDefaultEventFilter(bool e)
{
d_func().default_event_filter = e;
}
bool VideoRenderer::isDefaultEventFilterEnabled() const
{
return d_func().default_event_filter;
}
qreal VideoRenderer::brightness() const
{
return d_func().brightness;
}
bool VideoRenderer::setBrightness(qreal brightness)
{
return onSetBrightness(brightness);
}
bool VideoRenderer::onSetBrightness(qreal brightness)
{
if (!onChangingBrightness(brightness))
return false;
d_func().brightness = brightness;
if (widget()) {
widget()->update();
}
if (graphicsItem()) {
graphicsItem()->update();
}
return true;
}
qreal VideoRenderer::contrast() const
{
return d_func().contrast;
}
bool VideoRenderer::setContrast(qreal contrast)
{
return onSetContrast(contrast);
}
bool VideoRenderer::onSetContrast(qreal contrast)
{
if (!onChangingContrast(contrast))
return false;
d_func().contrast = contrast;
if (widget()) {
widget()->update();
}
if (graphicsItem()) {
graphicsItem()->update();
}
return true;
}
qreal VideoRenderer::hue() const
{
return d_func().hue;
}
bool VideoRenderer::setHue(qreal hue)
{
return onSetHue(hue);
}
bool VideoRenderer::onSetHue(qreal hue)
{
if (!onChangingHue(hue))
return false;
d_func().hue = hue;
if (widget()) {
widget()->update();
}
if (graphicsItem()) {
graphicsItem()->update();
}
return true;
}
qreal VideoRenderer::saturation() const
{
return d_func().saturation;
}
bool VideoRenderer::setSaturation(qreal saturation)
{
return onSetSaturation(saturation);
}
bool VideoRenderer::onSetSaturation(qreal saturation)
{
if (!onChangingSaturation(saturation))
return false;
d_func().saturation = saturation;
if (widget()) {
widget()->update();
}
if (graphicsItem()) {
graphicsItem()->update();
}
return true;
}
bool VideoRenderer::onChangingBrightness(qreal b)
{
Q_UNUSED(b);
return false;
}
bool VideoRenderer::onChangingContrast(qreal c)
{
Q_UNUSED(c);
return false;
}
bool VideoRenderer::onChangingHue(qreal h)
{
Q_UNUSED(h);
return false;
}
bool VideoRenderer::onChangingSaturation(qreal s)
{
Q_UNUSED(s);
return false;
}
} //namespace QtAV