1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qgstvideobuffer_p.h"
41
42QT_BEGIN_NAMESPACE
43
44#if GST_CHECK_VERSION(1,0,0)
45QGstVideoBuffer::QGstVideoBuffer(GstBuffer *buffer, const GstVideoInfo &info)
46 : QAbstractPlanarVideoBuffer(NoHandle)
47 , m_videoInfo(info)
48#else
49QGstVideoBuffer::QGstVideoBuffer(GstBuffer *buffer, int bytesPerLine)
50 : QAbstractVideoBuffer(NoHandle)
51 , m_bytesPerLine(bytesPerLine)
52#endif
53 , m_buffer(buffer)
54{
55 gst_buffer_ref(buf: m_buffer);
56}
57
58#if GST_CHECK_VERSION(1,0,0)
59QGstVideoBuffer::QGstVideoBuffer(GstBuffer *buffer, const GstVideoInfo &info,
60 QGstVideoBuffer::HandleType handleType,
61 const QVariant &handle)
62 : QAbstractPlanarVideoBuffer(handleType)
63 , m_videoInfo(info)
64#else
65QGstVideoBuffer::QGstVideoBuffer(GstBuffer *buffer, int bytesPerLine,
66 QGstVideoBuffer::HandleType handleType,
67 const QVariant &handle)
68 : QAbstractVideoBuffer(handleType)
69 , m_bytesPerLine(bytesPerLine)
70#endif
71 , m_buffer(buffer)
72 , m_handle(handle)
73{
74 gst_buffer_ref(buf: m_buffer);
75}
76
77QGstVideoBuffer::~QGstVideoBuffer()
78{
79 unmap();
80
81 gst_buffer_unref(buf: m_buffer);
82}
83
84
85QAbstractVideoBuffer::MapMode QGstVideoBuffer::mapMode() const
86{
87 return m_mode;
88}
89
90#if GST_CHECK_VERSION(1,0,0)
91
92int QGstVideoBuffer::map(MapMode mode, int *numBytes, int bytesPerLine[4], uchar *data[4])
93{
94 const GstMapFlags flags = GstMapFlags(((mode & ReadOnly) ? GST_MAP_READ : 0)
95 | ((mode & WriteOnly) ? GST_MAP_WRITE : 0));
96
97 if (mode == NotMapped || m_mode != NotMapped) {
98 return 0;
99 } else if (m_videoInfo.finfo->n_planes == 0) { // Encoded
100 if (gst_buffer_map(buffer: m_buffer, info: &m_frame.map[0], flags)) {
101 if (numBytes)
102 *numBytes = m_frame.map[0].size;
103 bytesPerLine[0] = -1;
104 data[0] = static_cast<uchar *>(m_frame.map[0].data);
105
106 m_mode = mode;
107
108 return 1;
109 }
110 } else if (gst_video_frame_map(frame: &m_frame, info: &m_videoInfo, buffer: m_buffer, flags)) {
111 if (numBytes)
112 *numBytes = m_frame.info.size;
113
114 for (guint i = 0; i < m_frame.info.finfo->n_planes; ++i) {
115 bytesPerLine[i] = m_frame.info.stride[i];
116 data[i] = static_cast<uchar *>(m_frame.data[i]);
117 }
118
119 m_mode = mode;
120
121 return m_frame.info.finfo->n_planes;
122 }
123 return 0;
124}
125
126#else
127
128uchar *QGstVideoBuffer::map(MapMode mode, int *numBytes, int *bytesPerLine)
129{
130 if (mode != NotMapped && m_mode == NotMapped) {
131 if (numBytes)
132 *numBytes = m_buffer->size;
133 if (bytesPerLine)
134 *bytesPerLine = m_bytesPerLine;
135
136 m_mode = mode;
137
138 return m_buffer->data;
139 } else {
140 return 0;
141 }
142}
143
144#endif
145
146void QGstVideoBuffer::unmap()
147{
148#if GST_CHECK_VERSION(1,0,0)
149 if (m_mode != NotMapped) {
150 if (m_videoInfo.finfo->n_planes == 0)
151 gst_buffer_unmap(buffer: m_buffer, info: &m_frame.map[0]);
152 else
153 gst_video_frame_unmap(frame: &m_frame);
154 }
155#endif
156 m_mode = NotMapped;
157}
158
159QT_END_NAMESPACE
160

source code of qtmultimedia/src/gsttools/qgstvideobuffer.cpp