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

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

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 21.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 \preliminary
106 \since 4.6
107
108 A QVideoFrame encapsulates the data of a video frame, and information about the frame.
109
110 The contents of a video frame can be mapped to memory using the map() function. While
111 mapped the video data can accessed using the bits() function which returns a pointer to a
112 buffer, the total size of which is given by the mappedBytes(), and the size of each line is given
113 by bytesPerLine(). The return value of the handle() function may be used to access frame data
114 using the internal buffer's native APIs.
115
116 The video data in a QVideoFrame is encapsulated in a QAbstractVideoBuffer. A QVideoFrame
117 may be constructed from any buffer type by subclassing the QAbstractVideoBuffer class.
118
119 \note QVideoFrame is explicitly shared, any change made to video frame will also apply to any
120 copies.
121*/
122
123/*!
124 \enum QVideoFrame::PixelFormat
125
126 Enumerates video data types.
127
128 \value Format_Invalid
129 The frame is invalid.
130
131 \value Format_ARGB32
132 The frame is stored using a 32-bit ARGB format (0xAARRGGBB). This is equivalent to
133 QImage::Format_ARGB32.
134
135 \value Format_ARGB32_Premultiplied
136 The frame stored using a premultiplied 32-bit ARGB format (0xAARRGGBB). This is equivalent
137 to QImage::Format_ARGB32_Premultiplied.
138
139 \value Format_RGB32
140 The frame stored using a 32-bit RGB format (0xffRRGGBB). This is equivalent to
141 QImage::Format_RGB32
142
143 \value Format_RGB24
144 The frame is stored using a 24-bit RGB format (8-8-8). This is equivalent to
145 QImage::Format_RGB888
146
147 \value Format_RGB565
148 The frame is stored using a 16-bit RGB format (5-6-5). This is equivalent to
149 QImage::Format_RGB16.
150
151 \value Format_RGB555
152 The frame is stored using a 16-bit RGB format (5-5-5). This is equivalent to
153 QImage::Format_RGB555.
154
155 \value Format_ARGB8565_Premultiplied
156 The frame is stored using a 24-bit premultiplied ARGB format (8-6-6-5).
157
158 \value Format_BGRA32
159 The frame is stored using a 32-bit ARGB format (0xBBGGRRAA).
160
161 \value Format_BGRA32_Premultiplied
162 The frame is stored using a premultiplied 32bit BGRA format.
163
164 \value Format_BGR32
165 The frame is stored using a 32-bit BGR format (0xBBGGRRff).
166
167 \value Format_BGR24
168 The frame is stored using a 24-bit BGR format (0xBBGGRR).
169
170 \value Format_BGR565
171 The frame is stored using a 16-bit BGR format (5-6-5).
172
173 \value Format_BGR555
174 The frame is stored using a 16-bit BGR format (5-5-5).
175
176 \value Format_BGRA5658_Premultiplied
177 The frame is stored using a 24-bit premultiplied BGRA format (5-6-5-8).
178
179 \value Format_AYUV444
180 The frame is stored using a packed 32-bit AYUV format (0xAAYYUUVV).
181
182 \value Format_AYUV444_Premultiplied
183 The frame is stored using a packed premultiplied 32-bit AYUV format (0xAAYYUUVV).
184
185 \value Format_YUV444
186 The frame is stored using a 24-bit packed YUV format (8-8-8).
187
188 \value Format_YUV420P
189 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
190 horizontally and vertically sub-sampled, i.e. the height and width of the U and V planes are
191 half that of the Y plane.
192
193 \value Format_YV12
194 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
195 horizontally and vertically sub-sampled, i.e. the height and width of the V and U planes are
196 half that of the Y plane.
197
198 \value Format_UYVY
199 The frame is stored using an 8-bit per component packed YUV format with the U and V planes
200 horizontally sub-sampled (U-Y-V-Y), i.e. two horizontally adjacent pixels are stored as a 32-bit
201 macropixel which has a Y value for each pixel and common U and V values.
202
203 \value Format_YUYV
204 The frame is stored using an 8-bit per component packed YUV format with the U and V planes
205 horizontally sub-sampled (Y-U-Y-V), i.e. two horizontally adjacent pixels are stored as a 32-bit
206 macropixel which has a Y value for each pixel and common U and V values.
207
208 \value Format_NV12
209 The frame is stored using an 8-bit per component semi-planar YUV format with a Y plane (Y)
210 followed by a horizontally and vertically sub-sampled, packed UV plane (U-V).
211
212 \value Format_NV21
213 The frame is stored using an 8-bit per component semi-planar YUV format with a Y plane (Y)
214 followed by a horizontally and vertically sub-sampled, packed VU plane (V-U).
215
216 \value Format_IMC1
217 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
218 horizontally and vertically sub-sampled. This is similar to the Format_YUV420P type, except
219 that the bytes per line of the U and V planes are padded out to the same stride as the Y plane.
220
221 \value Format_IMC2
222 The frame is stored using an 8-bit per component planar YUV format with the U and V planes
223 horizontally and vertically sub-sampled. This is similar to the Format_YUV420P type, except
224 that the lines of the U and V planes are interleaved, i.e. each line of U data is followed by a
225 line of V data creating a single line of the same stride as the Y data.
226
227 \value Format_IMC3
228 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
229 horizontally and vertically sub-sampled. This is similar to the Format_YV12 type, except that
230 the bytes per line of the V and U planes are padded out to the same stride as the Y plane.
231
232 \value Format_IMC4
233 The frame is stored using an 8-bit per component planar YVU format with the V and U planes
234 horizontally and vertically sub-sampled. This is similar to the Format_YV12 type, except that
235 the lines of the V and U planes are interleaved, i.e. each line of V data is followed by a line
236 of U data creating a single line of the same stride as the Y data.
237
238 \value Format_Y8
239 The frame is stored using an 8-bit greyscale format.
240
241 \value Format_Y16
242 The frame is stored using a 16-bit linear greyscale format. Little endian.
243
244 \value Format_User
245 Start value for user defined pixel formats.
246*/
247
248/*!
249 \enum QVideoFrame::FieldType
250
251 Specifies the field an interlaced video frame belongs to.
252
253 \value ProgressiveFrame The frame is not interlaced.
254 \value TopField The frame contains a top field.
255 \value BottomField The frame contains a bottom field.
256 \value InterlacedFrame The frame contains a merged top and bottom field.
257*/
258
259/*!
260 Constructs a null video frame.
261*/
262
263QVideoFrame::QVideoFrame()
264 : d(new QVideoFramePrivate)
265{
266}
267
268/*!
269 Constructs a video frame from a \a buffer of the given pixel \a format and \a size in pixels.
270
271 \note This doesn't increment the reference count of the video buffer.
272*/
273
274QVideoFrame::QVideoFrame(
275 QAbstractVideoBuffer *buffer, const QSize &size, PixelFormat format)
276 : d(new QVideoFramePrivate(size, format))
277{
278 d->buffer = buffer;
279}
280
281/*!
282 Constructs a video frame of the given pixel \a format and \a size in pixels.
283
284 The \a bytesPerLine (stride) is the length of each scan line in bytes, and \a bytes is the total
285 number of bytes that must be allocated for the frame.
286*/
287
288QVideoFrame::QVideoFrame(int bytes, const QSize &size, int bytesPerLine, PixelFormat format)
289 : d(new QVideoFramePrivate(size, format))
290{
291 if (bytes > 0) {
292 QByteArray data;
293 data.resize(bytes);
294
295 // Check the memory was successfully allocated.
296 if (!data.isEmpty())
297 d->buffer = new QMemoryVideoBuffer(data, bytesPerLine);
298 }
299}
300
301/*!
302 Constructs a video frame from an \a image.
303
304 \note This will construct an invalid video frame if there is no frame type equivalent to the
305 image format.
306
307 \sa pixelFormatFromImageFormat()
308*/
309
310QVideoFrame::QVideoFrame(const QImage &image)
311 : d(new QVideoFramePrivate(
312 image.size(), pixelFormatFromImageFormat(image.format())))
313{
314 if (d->pixelFormat != Format_Invalid)
315 d->buffer = new QImageVideoBuffer(image);
316}
317
318/*!
319 Constructs a copy of \a other.
320*/
321
322QVideoFrame::QVideoFrame(const QVideoFrame &other)
323 : d(other.d)
324{
325}
326
327/*!
328 Assigns the contents of \a other to a video frame.
329*/
330
331QVideoFrame &QVideoFrame::operator =(const QVideoFrame &other)
332{
333 d = other.d;
334
335 return *this;
336}
337
338/*!
339 Destroys a video frame.
340*/
341
342QVideoFrame::~QVideoFrame()
343{
344}
345
346/*!
347 Identifies whether a video frame is valid.
348
349 An invalid frame has no video buffer associated with it.
350
351 Returns true if the frame is valid, and false if it is not.
352*/
353
354bool QVideoFrame::isValid() const
355{
356 return d->buffer != 0;
357}
358
359/*!
360 Returns the color format of a video frame.
361*/
362
363QVideoFrame::PixelFormat QVideoFrame::pixelFormat() const
364{
365 return d->pixelFormat;
366}
367
368/*!
369 Returns the type of a video frame's handle.
370*/
371
372QAbstractVideoBuffer::HandleType QVideoFrame::handleType() const
373{
374 return d->buffer ? d->buffer->handleType() : QAbstractVideoBuffer::NoHandle;
375}
376
377/*!
378 Returns the size of a video frame.
379*/
380
381QSize QVideoFrame::size() const
382{
383 return d->size;
384}
385
386/*!
387 Returns the width of a video frame.
388*/
389
390int QVideoFrame::width() const
391{
392 return d->size.width();
393}
394
395/*!
396 Returns the height of a video frame.
397*/
398
399int QVideoFrame::height() const
400{
401 return d->size.height();
402}
403
404/*!
405 Returns the field an interlaced video frame belongs to.
406
407 If the video is not interlaced this will return WholeFrame.
408*/
409
410QVideoFrame::FieldType QVideoFrame::fieldType() const
411{
412 return d->fieldType;
413}
414
415/*!
416 Sets the \a field an interlaced video frame belongs to.
417*/
418
419void QVideoFrame::setFieldType(QVideoFrame::FieldType field)
420{
421 d->fieldType = field;
422}
423
424/*!
425 Identifies if a video frame's contents are currently mapped to system memory.
426
427 This is a convenience function which checks that the \l {QAbstractVideoBuffer::MapMode}{MapMode}
428 of the frame is not equal to QAbstractVideoBuffer::NotMapped.
429
430 Returns true if the contents of the video frame are mapped to system memory, and false
431 otherwise.
432
433 \sa mapMode() QAbstractVideoBuffer::MapMode
434*/
435
436bool QVideoFrame::isMapped() const
437{
438 return d->buffer != 0 && d->buffer->mapMode() != QAbstractVideoBuffer::NotMapped;
439}
440
441/*!
442 Identifies if the mapped contents of a video frame will be persisted when the frame is unmapped.
443
444 This is a convenience function which checks if the \l {QAbstractVideoBuffer::MapMode}{MapMode}
445 contains the QAbstractVideoBuffer::WriteOnly flag.
446
447 Returns true if the video frame will be updated when unmapped, and false otherwise.
448
449 \note The result of altering the data of a frame that is mapped in read-only mode is undefined.
450 Depending on the buffer implementation the changes may be persisted, or worse alter a shared
451 buffer.
452
453 \sa mapMode(), QAbstractVideoBuffer::MapMode
454*/
455
456bool QVideoFrame::isWritable() const
457{
458 return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::WriteOnly);
459}
460
461/*!
462 Identifies if the mapped contents of a video frame were read from the frame when it was mapped.
463
464 This is a convenience function which checks if the \l {QAbstractVideoBuffer::MapMode}{MapMode}
465 contains the QAbstractVideoBuffer::WriteOnly flag.
466
467 Returns true if the contents of the mapped memory were read from the video frame, and false
468 otherwise.
469
470 \sa mapMode(), QAbstractVideoBuffer::MapMode
471*/
472
473bool QVideoFrame::isReadable() const
474{
475 return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::ReadOnly);
476}
477
478/*!
479 Returns the mode a video frame was mapped to system memory in.
480
481 \sa map(), QAbstractVideoBuffer::MapMode
482*/
483
484QAbstractVideoBuffer::MapMode QVideoFrame::mapMode() const
485{
486 return d->buffer != 0 ? d->buffer->mapMode() : QAbstractVideoBuffer::NotMapped;
487}
488
489/*!
490 Maps the contents of a video frame to memory.
491
492 The map \a mode indicates whether the contents of the mapped memory should be read from and/or
493 written to the frame. If the map mode includes the QAbstractVideoBuffer::ReadOnly flag the
494 mapped memory will be populated with the content of the video frame when mapped. If the map
495 mode inclues the QAbstractVideoBuffer::WriteOnly flag the content of the mapped memory will be
496 persisted in the frame when unmapped.
497
498 While mapped the contents of a video frame can be accessed directly through the pointer returned
499 by the bits() function.
500
501 When access to the data is no longer needed be sure to call the unmap() function to release the
502 mapped memory.
503
504 Returns true if the buffer was mapped to memory in the given \a mode and false otherwise.
505
506 \sa unmap(), mapMode(), bits()
507*/
508
509bool QVideoFrame::map(QAbstractVideoBuffer::MapMode mode)
510{
511 if (d->buffer != 0 && d->data == 0) {
512 Q_ASSERT(d->bytesPerLine == 0);
513 Q_ASSERT(d->mappedBytes == 0);
514
515 d->data = d->buffer->map(mode, &d->mappedBytes, &d->bytesPerLine);
516
517 return d->data != 0;
518 }
519
520 return false;
521}
522
523/*!
524 Releases the memory mapped by the map() function.
525
526 If the \l {QAbstractVideoBuffer::MapMode}{MapMode} included the QAbstractVideoBuffer::WriteOnly
527 flag this will persist the current content of the mapped memory to the video frame.
528
529 \sa map()
530*/
531
532void QVideoFrame::unmap()
533{
534 if (d->data != 0) {
535 d->mappedBytes = 0;
536 d->bytesPerLine = 0;
537 d->data = 0;
538
539 d->buffer->unmap();
540 }
541}
542
543/*!
544 Returns the number of bytes in a scan line.
545
546 \note This is the bytes per line of the first plane only. The bytes per line of subsequent
547 planes should be calculated as per the frame type.
548
549 This value is only valid while the frame data is \l {map()}{mapped}.
550
551 \sa bits(), map(), mappedBytes()
552*/
553
554int QVideoFrame::bytesPerLine() const
555{
556 return d->bytesPerLine;
557}
558
559/*!
560 Returns a pointer to the start of the frame data buffer.
561
562 This value is only valid while the frame data is \l {map()}{mapped}.
563
564 \sa map(), mappedBytes(), bytesPerLine()
565*/
566
567uchar *QVideoFrame::bits()
568{
569 return d->data;
570}
571
572/*!
573 Returns a pointer to the start of the frame data buffer.
574
575 This value is only valid while the frame data is \l {map()}{mapped}.
576
577 \sa map(), mappedBytes(), bytesPerLine()
578*/
579
580const uchar *QVideoFrame::bits() const
581{
582 return d->data;
583}
584
585/*!
586 Returns the number of bytes occupied by the mapped frame data.
587
588 This value is only valid while the frame data is \l {map()}{mapped}.
589
590 \sa map()
591*/
592
593int QVideoFrame::mappedBytes() const
594{
595 return d->mappedBytes;
596}
597
598/*!
599 Returns a type specific handle to a video frame's buffer.
600
601 For an OpenGL texture this would be the texture ID.
602
603 \sa QAbstractVideoBuffer::handle()
604*/
605
606QVariant QVideoFrame::handle() const
607{
608 return d->buffer != 0 ? d->buffer->handle() : QVariant();
609}
610
611/*!
612 Returns the presentation time when the frame should be displayed.
613*/
614
615qint64 QVideoFrame::startTime() const
616{
617 return d->startTime;
618}
619
620/*!
621 Sets the presentation \a time when the frame should be displayed.
622*/
623
624void QVideoFrame::setStartTime(qint64 time)
625{
626 d->startTime = time;
627}
628
629/*!
630 Returns the presentation time when a frame should stop being displayed.
631*/
632
633qint64 QVideoFrame::endTime() const
634{
635 return d->endTime;
636}
637
638/*!
639 Sets the presentation \a time when a frame should stop being displayed.
640*/
641
642void QVideoFrame::setEndTime(qint64 time)
643{
644 d->endTime = time;
645}
646
647/*!
648 Returns an video pixel format equivalent to an image \a format. If there is no equivalent
649 format QVideoFrame::InvalidType is returned instead.
650*/
651
652QVideoFrame::PixelFormat QVideoFrame::pixelFormatFromImageFormat(QImage::Format format)
653{
654 switch (format) {
655 case QImage::Format_Invalid:
656 case QImage::Format_Mono:
657 case QImage::Format_MonoLSB:
658 case QImage::Format_Indexed8:
659 return Format_Invalid;
660 case QImage::Format_RGB32:
661 return Format_RGB32;
662 case QImage::Format_ARGB32:
663 return Format_ARGB32;
664 case QImage::Format_ARGB32_Premultiplied:
665 return Format_ARGB32_Premultiplied;
666 case QImage::Format_RGB16:
667 return Format_RGB565;
668 case QImage::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.