source: trunk/src/multimedia/video/qvideoframe.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 21.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtMultimedia module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvideoframe.h"
43
44#include <private/qimagevideobuffer_p.h>
45#include <private/qmemoryvideobuffer_p.h>
46
47#include <qimage.h>
48#include <qpair.h>
49#include <qsize.h>
50#include <qvariant.h>
51#include <qvector.h>
52
53QT_BEGIN_NAMESPACE
54
55class QVideoFramePrivate : public QSharedData
56{
57public:
58 QVideoFramePrivate()
59 : startTime(-1)
60 , endTime(-1)
61 , data(0)
62 , mappedBytes(0)
63 , bytesPerLine(0)
64 , pixelFormat(QVideoFrame::Format_Invalid)
65 , fieldType(QVideoFrame::ProgressiveFrame)
66 , buffer(0)
67 {
68 }
69
70 QVideoFramePrivate(const QSize &size, QVideoFrame::PixelFormat format)
71 : size(size)
72 , startTime(-1)
73 , endTime(-1)
74 , data(0)
75 , mappedBytes(0)
76 , bytesPerLine(0)
77 , pixelFormat(format)
78 , fieldType(QVideoFrame::ProgressiveFrame)
79 , buffer(0)
80 {
81 }
82
83 ~QVideoFramePrivate()
84 {
85 delete buffer;
86 }
87
88 QSize size;
89 qint64 startTime;
90 qint64 endTime;
91 uchar *data;
92 int mappedBytes;
93 int bytesPerLine;
94 QVideoFrame::PixelFormat pixelFormat;
95 QVideoFrame::FieldType fieldType;
96 QAbstractVideoBuffer *buffer;
97
98private:
99 Q_DISABLE_COPY(QVideoFramePrivate)
100};
101
102/*!
103 \class QVideoFrame
104 \brief The QVideoFrame class provides a representation of a frame of video data.
105 \since 4.6
106
107 A QVideoFrame encapsulates the data of a video frame, and information about the frame.
108
109 The contents of a video frame can be mapped to memory using the map() function. While
110 mapped the video data can accessed using the bits() function which returns a pointer to a
111 buffer, the total size of which is given by the mappedBytes(), and the size of each line is given
112 by bytesPerLine(). The return value of the handle() function may be used to access frame data
113 using the internal buffer's native APIs.
114
115 The video data in a QVideoFrame is encapsulated in a QAbstractVideoBuffer. A QVideoFrame
116 may be constructed from any buffer type by subclassing the QAbstractVideoBuffer class.
117
118 \note QVideoFrame is explicitly shared, any change made to video frame will also apply to any
119 copies.
120*/
121
122/*!
123 \enum QVideoFrame::PixelFormat
124
125 Enumerates video data types.
126
127 \value Format_Invalid
128 The frame is invalid.
129
130 \value Format_ARGB32
131 The frame is stored using a 32-bit ARGB format (0xAARRGGBB). This is equivalent to
132 QImage::Format_ARGB32.
133
134 \value Format_ARGB32_Premultiplied
135 The frame stored using a premultiplied 32-bit ARGB format (0xAARRGGBB). This is equivalent
136 to QImage::Format_ARGB32_Premultiplied.
137
138 \value Format_RGB32
139 The frame stored using a 32-bit RGB format (0xffRRGGBB). This is equivalent to
140 QImage::Format_RGB32
141
142 \value Format_RGB24
143 The frame is stored using a 24-bit RGB format (8-8-8). This is equivalent to
144 QImage::Format_RGB888
145
146 \value Format_RGB565
147 The frame is stored using a 16-bit RGB format (5-6-5). This is equivalent to
148 QImage::Format_RGB16.
149
150 \value Format_RGB555
151 The frame is stored using a 16-bit RGB format (5-5-5). This is equivalent to
152 QImage::Format_RGB555.
153
154 \value Format_ARGB8565_Premultiplied
155 The frame is stored using a 24-bit premultiplied ARGB format (8-6-6-5).
156
157 \value Format_BGRA32
158 The frame is stored using a 32-bit ARGB format (0xBBGGRRAA).
159
160 \value Format_BGRA32_Premultiplied
161 The frame is stored using a premultiplied 32bit BGRA format.
162
163 \value Format_BGR32
164 The frame is stored using a 32-bit BGR format (0xBBGGRRff).
165
166 \value Format_BGR24
167 The frame is stored using a 24-bit BGR format (0xBBGGRR).
168
169 \value Format_BGR565
170 The frame is stored using a 16-bit BGR format (5-6-5).
171
172 \value Format_BGR555
173 The frame is stored using a 16-bit BGR format (5-5-5).
174
175 \value Format_BGRA5658_Premultiplied
176 The frame is stored using a 24-bit premultiplied BGRA format (5-6-5-8).
177
178 \value Format_AYUV444
179 The frame is stored using a packed 32-bit AYUV format (0xAAYYUUVV).
180
181 \value Format_AYUV444_Premultiplied
182 The frame is stored using a packed premultiplied 32-bit AYUV format (0xAAYYUUVV).
183
184 \value Format_YUV444
185 The frame is stored using a 24-bit packed YUV format (8-8-8).
186
187 \value Format_YUV420P
188 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
189 horizontally and vertically sub-sampled, i.e. the height and width of the U and V planes are
190 half that of the Y plane.
191
192 \value Format_YV12
193 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
194 horizontally and vertically sub-sampled, i.e. the height and width of the V and U planes are
195 half that of the Y plane.
196
197 \value Format_UYVY
198 The frame is stored using an 8-bit per component packed YUV format with the U and V planes
199 horizontally sub-sampled (U-Y-V-Y), i.e. two horizontally adjacent pixels are stored as a 32-bit
200 macropixel which has a Y value for each pixel and common U and V values.
201
202 \value Format_YUYV
203 The frame is stored using an 8-bit per component packed YUV format with the U and V planes
204 horizontally sub-sampled (Y-U-Y-V), i.e. two horizontally adjacent pixels are stored as a 32-bit
205 macropixel which has a Y value for each pixel and common U and V values.
206
207 \value Format_NV12
208 The frame is stored using an 8-bit per component semi-planar YUV format with a Y plane (Y)
209 followed by a horizontally and vertically sub-sampled, packed UV plane (U-V).
210
211 \value Format_NV21
212 The frame is stored using an 8-bit per component semi-planar YUV format with a Y plane (Y)
213 followed by a horizontally and vertically sub-sampled, packed VU plane (V-U).
214
215 \value Format_IMC1
216 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
217 horizontally and vertically sub-sampled. This is similar to the Format_YUV420P type, except
218 that the bytes per line of the U and V planes are padded out to the same stride as the Y plane.
219
220 \value Format_IMC2
221 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
222 horizontally and vertically sub-sampled. This is similar to the Format_YUV420P type, except
223 that the lines of the U and V planes are interleaved, i.e. each line of U data is followed by a
224 line of V data creating a single line of the same stride as the Y data.
225
226 \value Format_IMC3
227 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
228 horizontally and vertically sub-sampled. This is similar to the Format_YV12 type, except that
229 the bytes per line of the V and U planes are padded out to the same stride as the Y plane.
230
231 \value Format_IMC4
232 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
233 horizontally and vertically sub-sampled. This is similar to the Format_YV12 type, except that
234 the lines of the V and U planes are interleaved, i.e. each line of V data is followed by a line
235 of U data creating a single line of the same stride as the Y data.
236
237 \value Format_Y8
238 The frame is stored using an 8-bit greyscale format.
239
240 \value Format_Y16
241 The frame is stored using a 16-bit linear greyscale format. Little endian.
242
243 \value Format_User
244 Start value for user defined pixel formats.
245*/
246
247/*!
248 \enum QVideoFrame::FieldType
249
250 Specifies the field an interlaced video frame belongs to.
251
252 \value ProgressiveFrame The frame is not interlaced.
253 \value TopField The frame contains a top field.
254 \value BottomField The frame contains a bottom field.
255 \value InterlacedFrame The frame contains a merged top and bottom field.
256*/
257
258/*!
259 Constructs a null video frame.
260*/
261
262QVideoFrame::QVideoFrame()
263 : d(new QVideoFramePrivate)
264{
265}
266
267/*!
268 Constructs a video frame from a \a buffer of the given pixel \a format and \a size in pixels.
269
270 \note This doesn't increment the reference count of the video buffer.
271*/
272
273QVideoFrame::QVideoFrame(
274 QAbstractVideoBuffer *buffer, const QSize &size, PixelFormat format)
275 : d(new QVideoFramePrivate(size, format))
276{
277 d->buffer = buffer;
278}
279
280/*!
281 Constructs a video frame of the given pixel \a format and \a size in pixels.
282
283 The \a bytesPerLine (stride) is the length of each scan line in bytes, and \a bytes is the total
284 number of bytes that must be allocated for the frame.
285*/
286
287QVideoFrame::QVideoFrame(int bytes, const QSize &size, int bytesPerLine, PixelFormat format)
288 : d(new QVideoFramePrivate(size, format))
289{
290 if (bytes > 0) {
291 QByteArray data;
292 data.resize(bytes);
293
294 // Check the memory was successfully allocated.
295 if (!data.isEmpty())
296 d->buffer = new QMemoryVideoBuffer(data, bytesPerLine);
297 }
298}
299
300/*!
301 Constructs a video frame from an \a image.
302
303 \note This will construct an invalid video frame if there is no frame type equivalent to the
304 image format.
305
306 \sa pixelFormatFromImageFormat()
307*/
308
309QVideoFrame::QVideoFrame(const QImage &image)
310 : d(new QVideoFramePrivate(
311 image.size(), pixelFormatFromImageFormat(image.format())))
312{
313 if (d->pixelFormat != Format_Invalid)
314 d->buffer = new QImageVideoBuffer(image);
315}
316
317/*!
318 Constructs a copy of \a other.
319*/
320
321QVideoFrame::QVideoFrame(const QVideoFrame &other)
322 : d(other.d)
323{
324}
325
326/*!
327 Assigns the contents of \a other to a video frame.
328*/
329
330QVideoFrame &QVideoFrame::operator =(const QVideoFrame &other)
331{
332 d = other.d;
333
334 return *this;
335}
336
337/*!
338 Destroys a video frame.
339*/
340
341QVideoFrame::~QVideoFrame()
342{
343}
344
345/*!
346 Identifies whether a video frame is valid.
347
348 An invalid frame has no video buffer associated with it.
349
350 Returns true if the frame is valid, and false if it is not.
351*/
352
353bool QVideoFrame::isValid() const
354{
355 return d->buffer != 0;
356}
357
358/*!
359 Returns the color format of a video frame.
360*/
361
362QVideoFrame::PixelFormat QVideoFrame::pixelFormat() const
363{
364 return d->pixelFormat;
365}
366
367/*!
368 Returns the type of a video frame's handle.
369*/
370
371QAbstractVideoBuffer::HandleType QVideoFrame::handleType() const
372{
373 return d->buffer ? d->buffer->handleType() : QAbstractVideoBuffer::NoHandle;
374}
375
376/*!
377 Returns the size of a video frame.
378*/
379
380QSize QVideoFrame::size() const
381{
382 return d->size;
383}
384
385/*!
386 Returns the width of a video frame.
387*/
388
389int QVideoFrame::width() const
390{
391 return d->size.width();
392}
393
394/*!
395 Returns the height of a video frame.
396*/
397
398int QVideoFrame::height() const
399{
400 return d->size.height();
401}
402
403/*!
404 Returns the field an interlaced video frame belongs to.
405
406 If the video is not interlaced this will return WholeFrame.
407*/
408
409QVideoFrame::FieldType QVideoFrame::fieldType() const
410{
411 return d->fieldType;
412}
413
414/*!
415 Sets the \a field an interlaced video frame belongs to.
416*/
417
418void QVideoFrame::setFieldType(QVideoFrame::FieldType field)
419{
420 d->fieldType = field;
421}
422
423/*!
424 Identifies if a video frame's contents are currently mapped to system memory.
425
426 This is a convenience function which checks that the \l {QAbstractVideoBuffer::MapMode}{MapMode}
427 of the frame is not equal to QAbstractVideoBuffer::NotMapped.
428
429 Returns true if the contents of the video frame are mapped to system memory, and false
430 otherwise.
431
432 \sa mapMode() QAbstractVideoBuffer::MapMode
433*/
434
435bool QVideoFrame::isMapped() const
436{
437 return d->buffer != 0 && d->buffer->mapMode() != QAbstractVideoBuffer::NotMapped;
438}
439
440/*!
441 Identifies if the mapped contents of a video frame will be persisted when the frame is unmapped.
442
443 This is a convenience function which checks if the \l {QAbstractVideoBuffer::MapMode}{MapMode}
444 contains the QAbstractVideoBuffer::WriteOnly flag.
445
446 Returns true if the video frame will be updated when unmapped, and false otherwise.
447
448 \note The result of altering the data of a frame that is mapped in read-only mode is undefined.
449 Depending on the buffer implementation the changes may be persisted, or worse alter a shared
450 buffer.
451
452 \sa mapMode(), QAbstractVideoBuffer::MapMode
453*/
454
455bool QVideoFrame::isWritable() const
456{
457 return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::WriteOnly);
458}
459
460/*!
461 Identifies if the mapped contents of a video frame were read from the frame when it was mapped.
462
463 This is a convenience function which checks if the \l {QAbstractVideoBuffer::MapMode}{MapMode}
464 contains the QAbstractVideoBuffer::WriteOnly flag.
465
466 Returns true if the contents of the mapped memory were read from the video frame, and false
467 otherwise.
468
469 \sa mapMode(), QAbstractVideoBuffer::MapMode
470*/
471
472bool QVideoFrame::isReadable() const
473{
474 return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::ReadOnly);
475}
476
477/*!
478 Returns the mode a video frame was mapped to system memory in.
479
480 \sa map(), QAbstractVideoBuffer::MapMode
481*/
482
483QAbstractVideoBuffer::MapMode QVideoFrame::mapMode() const
484{
485 return d->buffer != 0 ? d->buffer->mapMode() : QAbstractVideoBuffer::NotMapped;
486}
487
488/*!
489 Maps the contents of a video frame to memory.
490
491 The map \a mode indicates whether the contents of the mapped memory should be read from and/or
492 written to the frame. If the map mode includes the QAbstractVideoBuffer::ReadOnly flag the
493 mapped memory will be populated with the content of the video frame when mapped. If the map
494 mode inclues the QAbstractVideoBuffer::WriteOnly flag the content of the mapped memory will be
495 persisted in the frame when unmapped.
496
497 While mapped the contents of a video frame can be accessed directly through the pointer returned
498 by the bits() function.
499
500 When access to the data is no longer needed be sure to call the unmap() function to release the
501 mapped memory.
502
503 Returns true if the buffer was mapped to memory in the given \a mode and false otherwise.
504
505 \sa unmap(), mapMode(), bits()
506*/
507
508bool QVideoFrame::map(QAbstractVideoBuffer::MapMode mode)
509{
510 if (d->buffer != 0 && d->data == 0) {
511 Q_ASSERT(d->bytesPerLine == 0);
512 Q_ASSERT(d->mappedBytes == 0);
513
514 d->data = d->buffer->map(mode, &d->mappedBytes, &d->bytesPerLine);
515
516 return d->data != 0;
517 }
518
519 return false;
520}
521
522/*!
523 Releases the memory mapped by the map() function.
524
525 If the \l {QAbstractVideoBuffer::MapMode}{MapMode} included the QAbstractVideoBuffer::WriteOnly
526 flag this will persist the current content of the mapped memory to the video frame.
527
528 \sa map()
529*/
530
531void QVideoFrame::unmap()
532{
533 if (d->data != 0) {
534 d->mappedBytes = 0;
535 d->bytesPerLine = 0;
536 d->data = 0;
537
538 d->buffer->unmap();
539 }
540}
541
542/*!
543 Returns the number of bytes in a scan line.
544
545 \note This is the bytes per line of the first plane only. The bytes per line of subsequent
546 planes should be calculated as per the frame type.
547
548 This value is only valid while the frame data is \l {map()}{mapped}.
549
550 \sa bits(), map(), mappedBytes()
551*/
552
553int QVideoFrame::bytesPerLine() const
554{
555 return d->bytesPerLine;
556}
557
558/*!
559 Returns a pointer to the start of the frame data buffer.
560
561 This value is only valid while the frame data is \l {map()}{mapped}.
562
563 \sa map(), mappedBytes(), bytesPerLine()
564*/
565
566uchar *QVideoFrame::bits()
567{
568 return d->data;
569}
570
571/*!
572 Returns a pointer to the start of the frame data buffer.
573
574 This value is only valid while the frame data is \l {map()}{mapped}.
575
576 \sa map(), mappedBytes(), bytesPerLine()
577*/
578
579const uchar *QVideoFrame::bits() const
580{
581 return d->data;
582}
583
584/*!
585 Returns the number of bytes occupied by the mapped frame data.
586
587 This value is only valid while the frame data is \l {map()}{mapped}.
588
589 \sa map()
590*/
591
592int QVideoFrame::mappedBytes() const
593{
594 return d->mappedBytes;
595}
596
597/*!
598 Returns a type specific handle to a video frame's buffer.
599
600 For an OpenGL texture this would be the texture ID.
601
602 \sa QAbstractVideoBuffer::handle()
603*/
604
605QVariant QVideoFrame::handle() const
606{
607 return d->buffer != 0 ? d->buffer->handle() : QVariant();
608}
609
610/*!
611 Returns the presentation time when the frame should be displayed.
612*/
613
614qint64 QVideoFrame::startTime() const
615{
616 return d->startTime;
617}
618
619/*!
620 Sets the presentation \a time when the frame should be displayed.
621*/
622
623void QVideoFrame::setStartTime(qint64 time)
624{
625 d->startTime = time;
626}
627
628/*!
629 Returns the presentation time when a frame should stop being displayed.
630*/
631
632qint64 QVideoFrame::endTime() const
633{
634 return d->endTime;
635}
636
637/*!
638 Sets the presentation \a time when a frame should stop being displayed.
639*/
640
641void QVideoFrame::setEndTime(qint64 time)
642{
643 d->endTime = time;
644}
645
646/*!
647 Returns an video pixel format equivalent to an image \a format. If there is no equivalent
648 format QVideoFrame::InvalidType is returned instead.
649*/
650
651QVideoFrame::PixelFormat QVideoFrame::pixelFormatFromImageFormat(QImage::Format format)
652{
653 switch (format) {
654 case QImage::Format_Invalid:
655 case QImage::Format_Mono:
656 case QImage::Format_MonoLSB:
657 case QImage::Format_Indexed8:
658 return Format_Invalid;
659 case QImage::Format_RGB32:
660 return Format_RGB32;
661 case QImage::Format_ARGB32:
662 return Format_ARGB32;
663 case QImage::Format_ARGB32_Premultiplied:
664 return Format_ARGB32_Premultiplied;
665 case QImage::Format_RGB16:
666 return Format_RGB565;
667 case QImage::Format_ARGB8565_Premultiplied:
668 return Format_ARGB8565_Premultiplied;
669 case QImage::Format_RGB666:
670 case QImage::Format_ARGB6666_Premultiplied:
671 return Format_Invalid;
672 case QImage::Format_RGB555:
673 return Format_RGB555;
674 case QImage::Format_ARGB8555_Premultiplied:
675 return Format_Invalid;
676 case QImage::Format_RGB888:
677 return Format_RGB24;
678 case QImage::Format_RGB444:
679 case QImage::Format_ARGB4444_Premultiplied:
680 return Format_Invalid;
681 case QImage::NImageFormats:
682 return Format_Invalid;
683 }
684 return Format_Invalid;
685}
686
687/*!
688 Returns an image format equivalent to a video frame pixel \a format. If there is no equivalent
689 format QImage::Format_Invalid is returned instead.
690*/
691
692QImage::Format QVideoFrame::imageFormatFromPixelFormat(PixelFormat format)
693{
694 switch (format) {
695 case Format_Invalid:
696 return QImage::Format_Invalid;
697 case Format_ARGB32:
698 return QImage::Format_ARGB32;
699 case Format_ARGB32_Premultiplied:
700 return QImage::Format_ARGB32_Premultiplied;
701 case Format_RGB32:
702 return QImage::Format_RGB32;
703 case Format_RGB24:
704 return QImage::Format_RGB888;
705 case Format_RGB565:
706 return QImage::Format_RGB16;
707 case Format_RGB555:
708 return QImage::Format_RGB555;
709 case Format_ARGB8565_Premultiplied:
710 return QImage::Format_ARGB8565_Premultiplied;
711 case Format_BGRA32:
712 case Format_BGRA32_Premultiplied:
713 case Format_BGR32:
714 case Format_BGR24:
715 return QImage::Format_Invalid;
716 case Format_BGR565:
717 case Format_BGR555:
718 case Format_BGRA5658_Premultiplied:
719 case Format_AYUV444:
720 case Format_AYUV444_Premultiplied:
721 case Format_YUV444:
722 case Format_YUV420P:
723 case Format_YV12:
724 case Format_UYVY:
725 case Format_YUYV:
726 case Format_NV12:
727 case Format_NV21:
728 case Format_IMC1:
729 case Format_IMC2:
730 case Format_IMC3:
731 case Format_IMC4:
732 case Format_Y8:
733 case Format_Y16:
734 return QImage::Format_Invalid;
735 case Format_User:
736 return QImage::Format_Invalid;
737 }
738 return QImage::Format_Invalid;
739}
740
741QT_END_NAMESPACE
742
Note: See TracBrowser for help on using the repository browser.