1 /* Webcamoid, webcam capture application.
2  * Copyright (C) 2016  Gonzalo Exequiel Pedone
3  *
4  * Webcamoid is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Webcamoid is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Webcamoid. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Web-Site: http://webcamoid.github.io/
18  */
19 
20 #ifndef AKVIDEOCAPS_H
21 #define AKVIDEOCAPS_H
22 
23 #include <QObject>
24 
25 #include "akcommons.h"
26 
27 #define AkFourCC(a, b, c, d) \
28     (((quint32(a) & 0xff) << 24) \
29    | ((quint32(b) & 0xff) << 16) \
30    | ((quint32(c) & 0xff) <<  8) \
31    |  (quint32(d) & 0xff))
32 
AkFourCCS(const QString & fourcc)33 inline quint32 AkFourCCS(const QString &fourcc)
34 {
35     if (fourcc.size() != 4)
36         return 0;
37 
38     return AkFourCC(fourcc[0].toLatin1(),
39                     fourcc[1].toLatin1(),
40                     fourcc[2].toLatin1(),
41                     fourcc[3].toLatin1());
42 }
43 
44 #define AkFourCCR(a, b, c, d) \
45     (((quint32(d) & 0xff) << 24) \
46    | ((quint32(c) & 0xff) << 16) \
47    | ((quint32(b) & 0xff) <<  8) \
48    |  (quint32(a) & 0xff))
49 
AkFourCCRS(const QString & fourcc)50 inline quint32 AkFourCCRS(const QString &fourcc)
51 {
52     if (fourcc.size() != 4)
53         return 0;
54 
55     return AkFourCCR(fourcc[0].toLatin1(),
56                      fourcc[1].toLatin1(),
57                      fourcc[2].toLatin1(),
58                      fourcc[3].toLatin1());
59 }
60 
61 #define AK_FOURCC_NULL AkFourCC(0, 0, 0, 0)
62 
63 class AkVideoCapsPrivate;
64 class AkCaps;
65 class AkFrac;
66 
67 class AKCOMMONS_EXPORT AkVideoCaps: public QObject
68 {
69     Q_OBJECT
70     Q_ENUMS(PixelFormat)
71     Q_PROPERTY(PixelFormat format
72                READ format
73                WRITE setFormat
74                RESET resetFormat
75                NOTIFY formatChanged)
76     Q_PROPERTY(int bpp
77                READ bpp)
78     Q_PROPERTY(QSize size
79                READ size
80                WRITE setSize
81                RESET resetSize
82                NOTIFY sizeChanged)
83     Q_PROPERTY(int width
84                READ width
85                WRITE setWidth
86                RESET resetWidth
87                NOTIFY widthChanged)
88     Q_PROPERTY(int height
89                READ height
90                WRITE setHeight
91                RESET resetHeight
92                NOTIFY heightChanged)
93     Q_PROPERTY(AkFrac fps
94                READ fps
95                WRITE setFps
96                RESET resetFps
97                NOTIFY fpsChanged)
98     Q_PROPERTY(int align
99                READ align
100                WRITE setAlign
101                RESET resetAlign
102                NOTIFY alignChanged)
103     Q_PROPERTY(size_t pictureSize
104                READ pictureSize)
105     Q_PROPERTY(int planes
106                READ planes)
107 
108     public:
109         enum PixelFormat
110         {
111             Format_none = -1,
112             Format_yuv420p,
113             Format_yvu420p,
114             Format_yuyv422,
115             Format_rgb24,
116             Format_bgr24,
117             Format_yuv422p,
118             Format_yuv444p,
119             Format_yuv410p,
120             Format_yuv411p,
121             Format_gray,
122             Format_monow,
123             Format_monob,
124             Format_pal8,
125             Format_yuvj420p,
126             Format_yuvj422p,
127             Format_yuvj444p,
128             Format_uyvy422,
129             Format_vyuy422,
130             Format_uyyvyy411,
131             Format_bgr8,
132             Format_bgr4,
133             Format_bgr4_byte,
134             Format_rgb8,
135             Format_rgb4,
136             Format_rgb4_byte,
137             Format_nv12,
138             Format_nv21,
139             Format_argb,
140             Format_rgba,
141             Format_abgr,
142             Format_bgra,
143             Format_gray16be,
144             Format_gray16le,
145             Format_yuv440p,
146             Format_yuvj440p,
147             Format_yuva420p,
148             Format_rgb48be,
149             Format_rgb48le,
150             Format_rgb565be,
151             Format_rgb565le,
152             Format_rgb555be,
153             Format_rgb555le,
154             Format_argb555be,
155             Format_argb555le,
156             Format_bgr565be,
157             Format_bgr565le,
158             Format_bgr555be,
159             Format_bgr555le,
160             Format_rgb666,
161             Format_argb1665,
162             Format_argb1666,
163             Format_bgr666,
164             Format_argb6666,
165             Format_abgr6666,
166             Format_yuv420p16le,
167             Format_yuv420p16be,
168             Format_yuv422p16le,
169             Format_yuv422p16be,
170             Format_yuv444p16le,
171             Format_yuv444p16be,
172             Format_rgb444le,
173             Format_rgb444be,
174             Format_argb444be,
175             Format_argb444le,
176             Format_bgr444le,
177             Format_bgr444be,
178             Format_ya8,
179             Format_bgr48be,
180             Format_bgr48le,
181             Format_yuv420p9be,
182             Format_yuv420p9le,
183             Format_yuv420p10be,
184             Format_yuv420p10le,
185             Format_yuv422p10be,
186             Format_yuv422p10le,
187             Format_yuv444p9be,
188             Format_yuv444p9le,
189             Format_yuv444p10be,
190             Format_yuv444p10le,
191             Format_yuv422p9be,
192             Format_yuv422p9le,
193             Format_gbrp,
194             Format_gbrp9be,
195             Format_gbrp9le,
196             Format_gbrp10be,
197             Format_gbrp10le,
198             Format_gbrp16be,
199             Format_gbrp16le,
200             Format_yuva422p,
201             Format_yuva444p,
202             Format_yuva420p9be,
203             Format_yuva420p9le,
204             Format_yuva422p9be,
205             Format_yuva422p9le,
206             Format_yuva444p9be,
207             Format_yuva444p9le,
208             Format_yuva420p10be,
209             Format_yuva420p10le,
210             Format_yuva422p10be,
211             Format_yuva422p10le,
212             Format_yuva444p10be,
213             Format_yuva444p10le,
214             Format_yuva420p16be,
215             Format_yuva420p16le,
216             Format_yuva422p16be,
217             Format_yuva422p16le,
218             Format_yuva444p16be,
219             Format_yuva444p16le,
220             Format_xyz12le,
221             Format_xyz12be,
222             Format_nv16,
223             Format_nv20le,
224             Format_nv20be,
225             Format_rgba64be,
226             Format_rgba64le,
227             Format_bgra64be,
228             Format_bgra64le,
229             Format_yvyu422,
230             Format_ya16be,
231             Format_ya16le,
232             Format_gbrap,
233             Format_gbrap16be,
234             Format_gbrap16le,
235             Format_0rgb,
236             Format_rgb0,
237             Format_0bgr,
238             Format_bgr0,
239             Format_yuv420p12be,
240             Format_yuv420p12le,
241             Format_yuv420p14be,
242             Format_yuv420p14le,
243             Format_yuv422p12be,
244             Format_yuv422p12le,
245             Format_yuv422p14be,
246             Format_yuv422p14le,
247             Format_yuv444p12be,
248             Format_yuv444p12le,
249             Format_yuv444p14be,
250             Format_yuv444p14le,
251             Format_gbrp12be,
252             Format_gbrp12le,
253             Format_gbrp14be,
254             Format_gbrp14le,
255             Format_yuvj411p,
256             Format_bayer_bggr8,
257             Format_bayer_rggb8,
258             Format_bayer_gbrg8,
259             Format_bayer_grbg8,
260             Format_bayer_bggr16le,
261             Format_bayer_bggr16be,
262             Format_bayer_rggb16le,
263             Format_bayer_rggb16be,
264             Format_bayer_gbrg16le,
265             Format_bayer_gbrg16be,
266             Format_bayer_grbg16le,
267             Format_bayer_grbg16be,
268             Format_yuv440p10le,
269             Format_yuv440p10be,
270             Format_yuv440p12le,
271             Format_yuv440p12be,
272             Format_ayuv64le,
273             Format_ayuv64be,
274             Format_p010le,
275             Format_p010be,
276             Format_gbrap12be,
277             Format_gbrap12le,
278             Format_gbrap10be,
279             Format_gbrap10le,
280             Format_gray12be,
281             Format_gray12le,
282             Format_gray10be,
283             Format_gray10le,
284             Format_p016le,
285             Format_p016be,
286             Format_gray9be,
287             Format_gray9le,
288             Format_gbrpf32be,
289             Format_gbrpf32le,
290             Format_gbrapf32be,
291             Format_gbrapf32le,
292             Format_gray14be,
293             Format_gray14le,
294             Format_grayf32be,
295             Format_grayf32le,
296             Format_rgbp,
297             Format_rgbap,
298             Format_argb1887,
299             Format_bgra1888,
300             Format_yuv444,
301             Format_gray2,
302             Format_gray4,
303             Format_gray24,
304             Format_gray32,
305         };
306 
307         AkVideoCaps(QObject *parent=nullptr);
308         AkVideoCaps(PixelFormat format,
309                     int width,
310                     int height,
311                     const AkFrac &fps,
312                     int align=1);
313         AkVideoCaps(PixelFormat format,
314                     const QSize &size,
315                     const AkFrac &fps,
316                     int align=1);
317         AkVideoCaps(const AkCaps &caps);
318         AkVideoCaps(const AkVideoCaps &other);
319         ~AkVideoCaps();
320         AkVideoCaps &operator =(const AkVideoCaps &other);
321         AkVideoCaps &operator =(const AkCaps &caps);
322         bool operator ==(const AkVideoCaps &other) const;
323         bool operator !=(const AkVideoCaps &other) const;
324         operator bool() const;
325         operator AkCaps() const;
326 
327         Q_INVOKABLE PixelFormat format() const;
328         Q_INVOKABLE quint32 fourCC() const;
329         Q_INVOKABLE int bpp() const;
330         Q_INVOKABLE QSize size() const;
331         Q_INVOKABLE int width() const;
332         Q_INVOKABLE int height() const;
333         Q_INVOKABLE AkFrac fps() const;
334         Q_INVOKABLE AkFrac &fps();
335         Q_INVOKABLE int align() const;
336         Q_INVOKABLE size_t pictureSize() const;
337 
338         Q_INVOKABLE static AkVideoCaps fromMap(const QVariantMap &caps);
339         Q_INVOKABLE QVariantMap toMap() const;
340         Q_INVOKABLE AkVideoCaps &update(const AkCaps &caps);
341         Q_INVOKABLE size_t planeOffset(int plane) const;
342         Q_INVOKABLE size_t lineOffset(int plane, int y) const;
343         Q_INVOKABLE size_t bytesPerLine(int plane) const;
344         Q_INVOKABLE int planes() const;
345         Q_INVOKABLE size_t planeSize(int plane) const;
346 
347         Q_INVOKABLE static int bitsPerPixel(PixelFormat pixelFormat);
348         Q_INVOKABLE static int bitsPerPixel(const QString &pixelFormat);
349         Q_INVOKABLE static QString pixelFormatToString(PixelFormat pixelFormat);
350         Q_INVOKABLE static PixelFormat pixelFormatFromString(const QString &pixelFormat);
351         Q_INVOKABLE static quint32 fourCC(PixelFormat pixelFormat);
352         Q_INVOKABLE static quint32 fourCC(const QString &pixelFormat);
353 
354     private:
355         AkVideoCapsPrivate *d;
356 
357     Q_SIGNALS:
358         void formatChanged(PixelFormat format);
359         void sizeChanged(const QSize &size);
360         void widthChanged(int width);
361         void heightChanged(int height);
362         void fpsChanged(const AkFrac &fps);
363         void alignChanged(int height);
364 
365     public Q_SLOTS:
366         void setFormat(PixelFormat format);
367         void setSize(const QSize &size);
368         void setWidth(int width);
369         void setHeight(int height);
370         void setFps(const AkFrac &fps);
371         void setAlign(int align);
372         void resetFormat();
373         void resetSize();
374         void resetWidth();
375         void resetHeight();
376         void resetFps();
377         void resetAlign();
378         void clear();
379 };
380 
381 AKCOMMONS_EXPORT QDebug operator <<(QDebug debug, const AkVideoCaps &caps);
382 AKCOMMONS_EXPORT QDebug operator <<(QDebug debug, AkVideoCaps::PixelFormat format);
383 AKCOMMONS_EXPORT QDataStream &operator >>(QDataStream &istream, AkVideoCaps &caps);
384 AKCOMMONS_EXPORT QDataStream &operator <<(QDataStream &ostream, const AkVideoCaps &caps);
385 
386 Q_DECLARE_METATYPE(AkVideoCaps)
387 Q_DECLARE_METATYPE(AkVideoCaps::PixelFormat)
388 
389 #endif // AKVIDEOCAPS_H
390