Qt 4.8
qvideoframe.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtMultimedia module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
54 
56 {
57 public:
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 
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 
84  {
85  delete buffer;
86  }
87 
97 
98 private:
100 };
101 
272  : d(new QVideoFramePrivate)
273 {
274 }
275 
284  : d(new QVideoFramePrivate(size, format))
285 {
286  d->buffer = buffer;
287 }
288 
297  : d(new QVideoFramePrivate(size, format))
298 {
299  if (bytes > 0) {
301  data.resize(bytes);
302 
303  // Check the memory was successfully allocated.
304  if (!data.isEmpty())
305  d->buffer = new QMemoryVideoBuffer(data, bytesPerLine);
306  }
307 }
308 
319  : d(new QVideoFramePrivate(
320  image.size(), pixelFormatFromImageFormat(image.format())))
321 {
322  if (d->pixelFormat != Format_Invalid)
323  d->buffer = new QImageVideoBuffer(image);
324 }
325 
331  : d(other.d)
332 {
333 }
334 
340 {
341  d = other.d;
342 
343  return *this;
344 }
345 
351 {
352 }
353 
363 {
364  return d->buffer != 0;
365 }
366 
372 {
373  return d->pixelFormat;
374 }
375 
381 {
383 }
384 
390 {
391  return d->size;
392 }
393 
399 {
400  return d->size.width();
401 }
402 
408 {
409  return d->size.height();
410 }
411 
419 {
420  return d->fieldType;
421 }
422 
428 {
429  d->fieldType = field;
430 }
431 
445 {
446  return d->buffer != 0 && d->buffer->mapMode() != QAbstractVideoBuffer::NotMapped;
447 }
448 
465 {
466  return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::WriteOnly);
467 }
468 
482 {
483  return d->buffer != 0 && (d->buffer->mapMode() & QAbstractVideoBuffer::ReadOnly);
484 }
485 
493 {
495 }
496 
518 {
519  if (d->buffer != 0 && d->data == 0) {
520  Q_ASSERT(d->bytesPerLine == 0);
521  Q_ASSERT(d->mappedBytes == 0);
522 
523  d->data = d->buffer->map(mode, &d->mappedBytes, &d->bytesPerLine);
524 
525  return d->data != 0;
526  }
527 
528  return false;
529 }
530 
541 {
542  if (d->data != 0) {
543  d->mappedBytes = 0;
544  d->bytesPerLine = 0;
545  d->data = 0;
546 
547  d->buffer->unmap();
548  }
549 }
550 
563 {
564  return d->bytesPerLine;
565 }
566 
576 {
577  return d->data;
578 }
579 
588 const uchar *QVideoFrame::bits() const
589 {
590  return d->data;
591 }
592 
602 {
603  return d->mappedBytes;
604 }
605 
615 {
616  return d->buffer != 0 ? d->buffer->handle() : QVariant();
617 }
618 
624 {
625  return d->startTime;
626 }
627 
633 {
634  d->startTime = time;
635 }
636 
642 {
643  return d->endTime;
644 }
645 
651 {
652  d->endTime = time;
653 }
654 
661 {
662  switch (format) {
664  case QImage::Format_Mono:
667  return Format_Invalid;
669  return Format_RGB32;
671  return Format_ARGB32;
675  return Format_RGB565;
680  return Format_Invalid;
682  return Format_RGB555;
684  return Format_Invalid;
686  return Format_RGB24;
689  return Format_Invalid;
690  case QImage::NImageFormats:
691  return Format_Invalid;
692  }
693  return Format_Invalid;
694 }
695 
702 {
703  switch (format) {
704  case Format_Invalid:
705  return QImage::Format_Invalid;
706  case Format_ARGB32:
707  return QImage::Format_ARGB32;
710  case Format_RGB32:
711  return QImage::Format_RGB32;
712  case Format_RGB24:
713  return QImage::Format_RGB888;
714  case Format_RGB565:
715  return QImage::Format_RGB16;
716  case Format_RGB555:
717  return QImage::Format_RGB555;
720  case Format_BGRA32:
722  case Format_BGR32:
723  case Format_BGR24:
724  return QImage::Format_Invalid;
725  case Format_BGR565:
726  case Format_BGR555:
728  case Format_AYUV444:
730  case Format_YUV444:
731  case Format_YUV420P:
732  case Format_YV12:
733  case Format_UYVY:
734  case Format_YUYV:
735  case Format_NV12:
736  case Format_NV21:
737  case Format_IMC1:
738  case Format_IMC2:
739  case Format_IMC3:
740  case Format_IMC4:
741  case Format_Y8:
742  case Format_Y16:
743  return QImage::Format_Invalid;
744  case Format_User:
745  return QImage::Format_Invalid;
746  }
747  return QImage::Format_Invalid;
748 }
749 
751 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
uchar * bits()
Returns a pointer to the start of the frame data buffer.
double d
Definition: qnumeric_p.h:62
QVideoFrame()
Constructs a null video frame.
Format
The following image formats are available in Qt.
Definition: qimage.h:91
MapMode
Enumerates how a video buffer&#39;s data is mapped to memory.
virtual void unmap()=0
Releases the memory mapped by the map() function.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
PixelFormat pixelFormat() const
Returns the color format of a video frame.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int mappedBytes() const
Returns the number of bytes occupied by the mapped frame data.
static PixelFormat pixelFormatFromImageFormat(QImage::Format format)
Returns an video pixel format equivalent to an image format.
int bytesPerLine() const
Returns the number of bytes in a scan line.
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
bool isWritable() const
Identifies if the mapped contents of a video frame will be persisted when the frame is unmapped...
QVideoFrame & operator=(const QVideoFrame &other)
Assigns the contents of other to a video frame.
The QVideoFrame class provides a representation of a frame of video data.
Definition: qvideoframe.h:61
HandleType
Identifies the type of a video buffers handle.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void unmap()
Releases the memory mapped by the map() function.
QAbstractVideoBuffer * buffer
Definition: qvideoframe.cpp:96
virtual MapMode mapMode() const =0
Returns the mode a video buffer is mapped in.
unsigned char uchar
Definition: qglobal.h:994
int width() const
Returns the width.
Definition: qsize.h:126
PixelFormat
Enumerates video data types.
Definition: qvideoframe.h:72
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QImage::Format imageFormatFromPixelFormat(PixelFormat format)
Returns an image format equivalent to a video frame pixel format.
QAbstractVideoBuffer::MapMode mapMode() const
Returns the mode a video frame was mapped to system memory in.
QSize size() const
Returns the size of a video frame.
void setFieldType(FieldType)
Sets the field an interlaced video frame belongs to.
virtual uchar * map(MapMode mode, int *numBytes, int *bytesPerLine)=0
Maps the contents of a video buffer to memory.
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
~QVideoFrame()
Destroys a video frame.
void setEndTime(qint64 time)
Sets the presentation time when a frame should stop being displayed.
__int64 qint64
Definition: qglobal.h:942
int height() const
Returns the height of a video frame.
void setStartTime(qint64 time)
Sets the presentation time when the frame should be displayed.
FieldType fieldType() const
Returns the field an interlaced video frame belongs to.
QAbstractVideoBuffer::HandleType handleType() const
Returns the type of a video frame&#39;s handle.
The QSharedData class is a base class for shared data objects.
Definition: qshareddata.h:56
The QMemoryVideoBuffer class provides a system memory allocated video data buffer.
The QAbstractVideoBuffer class is an abstraction for video data.
QVideoFrame::FieldType fieldType
Definition: qvideoframe.cpp:95
void resize(int size)
Sets the size of the byte array to size bytes.
int height() const
Returns the height.
Definition: qsize.h:129
bool isMapped() const
Identifies if a video frame&#39;s contents are currently mapped to system memory.
virtual QVariant handle() const
Returns a type specific handle to the data buffer.
qint64 startTime() const
Returns the presentation time when the frame should be displayed.
bool isReadable() const
Identifies if the mapped contents of a video frame were read from the frame when it was mapped...
int width() const
Returns the width of a video frame.
QVideoFramePrivate(const QSize &size, QVideoFrame::PixelFormat format)
Definition: qvideoframe.cpp:70
QVideoFrame::PixelFormat pixelFormat
Definition: qvideoframe.cpp:94
qint64 endTime() const
Returns the presentation time when a frame should stop being displayed.
bool isValid() const
Identifies whether a video frame is valid.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QExplicitlySharedDataPointer< QVideoFramePrivate > d
Definition: qvideoframe.h:158
HandleType handleType() const
Returns the type of a video buffer&#39;s handle.
QVariant handle() const
Returns a type specific handle to a video frame&#39;s buffer.
bool map(QAbstractVideoBuffer::MapMode mode)
Maps the contents of a video frame to memory.
FieldType
Specifies the field an interlaced video frame belongs to.
Definition: qvideoframe.h:64