File: pixelformat.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (298 lines) | stat: -rw-r--r-- 8,280 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***

  Olive - Non-Linear Video Editor
  Copyright (C) 2019 Olive Team

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

***/

#include "pixelformat.h"

#include "OpenImageIO/imagebuf.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFloat16>

#include "codec/oiio/oiiodecoder.h"
#include "common/define.h"
#include "core.h"

OLIVE_NAMESPACE_ENTER

bool PixelFormat::FormatHasAlphaChannel(const PixelFormat::Format &format)
{
  switch (format) {
  case PixelFormat::PIX_FMT_RGBA8:
  case PixelFormat::PIX_FMT_RGBA16U:
  case PixelFormat::PIX_FMT_RGBA16F:
  case PixelFormat::PIX_FMT_RGBA32F:
    return true;
  case PixelFormat::PIX_FMT_RGB8:
  case PixelFormat::PIX_FMT_RGB16U:
  case PixelFormat::PIX_FMT_RGB16F:
  case PixelFormat::PIX_FMT_RGB32F:
  case PixelFormat::PIX_FMT_INVALID:
  case PixelFormat::PIX_FMT_COUNT:
    break;
  }

  return false;
}

bool PixelFormat::FormatIsFloat(const PixelFormat::Format &format)
{
  switch (format) {
  case PixelFormat::PIX_FMT_RGB16F:
  case PixelFormat::PIX_FMT_RGBA16F:
  case PixelFormat::PIX_FMT_RGB32F:
  case PixelFormat::PIX_FMT_RGBA32F:
    return true;

  case PixelFormat::PIX_FMT_RGB8:
  case PixelFormat::PIX_FMT_RGBA8:
  case PixelFormat::PIX_FMT_RGB16U:
  case PixelFormat::PIX_FMT_RGBA16U:
  case PixelFormat::PIX_FMT_INVALID:
  case PixelFormat::PIX_FMT_COUNT:
    break;
  }

  return false;
}

OIIO::TypeDesc::BASETYPE PixelFormat::GetOIIOTypeDesc(const PixelFormat::Format &format)
{
  switch (format) {
  case PixelFormat::PIX_FMT_RGB8:
  case PixelFormat::PIX_FMT_RGBA8:
    return OIIO::TypeDesc::UINT8;
  case PixelFormat::PIX_FMT_RGB16U:
  case PixelFormat::PIX_FMT_RGBA16U:
    return OIIO::TypeDesc::UINT16;
  case PixelFormat::PIX_FMT_RGB16F:
  case PixelFormat::PIX_FMT_RGBA16F:
    return OIIO::TypeDesc::HALF;
  case PixelFormat::PIX_FMT_RGB32F:
  case PixelFormat::PIX_FMT_RGBA32F:
    return OIIO::TypeDesc::FLOAT;
  case PixelFormat::PIX_FMT_INVALID:
  case PixelFormat::PIX_FMT_COUNT:
    break;
  }

  return OIIO::TypeDesc::UNKNOWN;
}

QString PixelFormat::GetName(const PixelFormat::Format &format)
{
  switch (format) {
  case PixelFormat::PIX_FMT_RGB8:
  case PixelFormat::PIX_FMT_RGBA8:
    return tr("8-bit");
  case PixelFormat::PIX_FMT_RGB16U:
  case PixelFormat::PIX_FMT_RGBA16U:
    return tr("16-bit Integer");
  case PixelFormat::PIX_FMT_RGB16F:
  case PixelFormat::PIX_FMT_RGBA16F:
    return tr("Half-Float (16-bit)");
  case PixelFormat::PIX_FMT_RGB32F:
  case PixelFormat::PIX_FMT_RGBA32F:
    return tr("Full-Float (32-bit)");
  case PixelFormat::PIX_FMT_INVALID:
  case PixelFormat::PIX_FMT_COUNT:
    break;
  }

  return tr("Unknown (%1)").arg(format);
}

PixelFormat* PixelFormat::instance_ = nullptr;

void PixelFormat::CreateInstance()
{
  instance_ = new PixelFormat();
}

void PixelFormat::DestroyInstance()
{
  delete instance_;
}

PixelFormat *PixelFormat::instance()
{
  return instance_;
}

PixelFormat::Format PixelFormat::GetConfiguredFormatForMode(RenderMode::Mode mode)
{
  return static_cast<PixelFormat::Format>(Core::GetPreferenceForRenderMode(mode, QStringLiteral("PixelFormat")).toInt());
}

void PixelFormat::SetConfiguredFormatForMode(RenderMode::Mode mode, PixelFormat::Format format)
{
  if (format != GetConfiguredFormatForMode(mode)) {
    Core::SetPreferenceForRenderMode(mode, QStringLiteral("PixelFormat"), format);

    emit FormatChanged();
  }
}

PixelFormat::Format PixelFormat::OIIOFormatToOliveFormat(OIIO::TypeDesc desc, bool has_alpha)
{
  if (desc == OIIO::TypeDesc::UINT8) {
    return has_alpha ? PixelFormat::PIX_FMT_RGBA8 : PixelFormat::PIX_FMT_RGB8;
  } else if (desc == OIIO::TypeDesc::UINT16) {
    return has_alpha ? PixelFormat::PIX_FMT_RGBA16U : PixelFormat::PIX_FMT_RGB16U;
  } else if (desc == OIIO::TypeDesc::HALF) {
    return has_alpha ? PixelFormat::PIX_FMT_RGBA16F : PixelFormat::PIX_FMT_RGB16F;
  } else if (desc == OIIO::TypeDesc::FLOAT) {
    return has_alpha ? PixelFormat::PIX_FMT_RGBA32F : PixelFormat::PIX_FMT_RGB32F;
  }

  return PixelFormat::PIX_FMT_INVALID;
}

PixelFormat::Format PixelFormat::GetFormatWithAlphaChannel(PixelFormat::Format f)
{
  switch (f) {
  case PIX_FMT_INVALID:
  case PIX_FMT_COUNT:
    break;
  case PIX_FMT_RGB8:
  case PIX_FMT_RGBA8:
    return PIX_FMT_RGBA8;
  case PIX_FMT_RGB16U:
  case PIX_FMT_RGBA16U:
    return PIX_FMT_RGBA16U;
  case PIX_FMT_RGB16F:
  case PIX_FMT_RGBA16F:
    return PIX_FMT_RGBA16F;
  case PIX_FMT_RGB32F:
  case PIX_FMT_RGBA32F:
    return PIX_FMT_RGBA32F;
  }

  return PIX_FMT_INVALID;
}

PixelFormat::Format PixelFormat::GetFormatWithoutAlphaChannel(PixelFormat::Format f)
{
  switch (f) {
  case PIX_FMT_INVALID:
  case PIX_FMT_COUNT:
    break;
  case PIX_FMT_RGB8:
  case PIX_FMT_RGBA8:
    return PIX_FMT_RGB8;
  case PIX_FMT_RGB16U:
  case PIX_FMT_RGBA16U:
    return PIX_FMT_RGB16U;
  case PIX_FMT_RGB16F:
  case PIX_FMT_RGBA16F:
    return PIX_FMT_RGB16F;
  case PIX_FMT_RGB32F:
  case PIX_FMT_RGBA32F:
    return PIX_FMT_RGB32F;
  }

  return PIX_FMT_INVALID;
}

int PixelFormat::GetBufferSize(const PixelFormat::Format &format, const int &width, const int &height)
{
  return BytesPerPixel(format) * width * height;
}

int PixelFormat::BytesPerPixel(const PixelFormat::Format &format)
{
  return BytesPerChannel(format) * ChannelCount(format);
}

int PixelFormat::BytesPerChannel(const PixelFormat::Format &format)
{
  switch (format) {
  case PixelFormat::PIX_FMT_RGB8:
  case PixelFormat::PIX_FMT_RGBA8:
    return 1;
  case PixelFormat::PIX_FMT_RGB16U:
  case PixelFormat::PIX_FMT_RGB16F:
  case PixelFormat::PIX_FMT_RGBA16U:
  case PixelFormat::PIX_FMT_RGBA16F:
    return 2;
  case PixelFormat::PIX_FMT_RGB32F:
  case PixelFormat::PIX_FMT_RGBA32F:
    return 4;
  case PixelFormat::PIX_FMT_INVALID:
  case PixelFormat::PIX_FMT_COUNT:
    break;
  }

  qFatal("Invalid pixel format requested");

  // qFatal will abort so we won't get here, but this suppresses compiler warnings
  return 0;
}

int PixelFormat::ChannelCount(const PixelFormat::Format &format)
{
  if (PixelFormat::FormatHasAlphaChannel(format)) {
    return kRGBAChannels;
  } else {
    return kRGBChannels;
  }
}

FramePtr PixelFormat::ConvertPixelFormat(FramePtr frame, const PixelFormat::Format &dest_format)
{
  if (frame->format() == dest_format) {
    return frame;
  }

  // Create a destination frame with the same parameters
  FramePtr converted = Frame::Create();
  converted->set_video_params(VideoParams(frame->video_params().width(),
                                                   frame->video_params().height(),
                                                   dest_format));
  converted->set_timestamp(frame->timestamp());
  converted->allocate();

  // Do the conversion through OIIO - create a buffer for the source image
  OIIO::ImageBuf src(OIIO::ImageSpec(frame->width(),
                                     frame->height(),
                                     ChannelCount(frame->format()),
                                     GetOIIOTypeDesc(frame->format())));

  // Set the pixels (this is necessary as opposed to an OIIO buffer wrapper since Frame has
  // linesizes)
  OIIODecoder::FrameToBuffer(frame, &src);

  // Create a destination OIIO buffer with our destination format
  OIIO::ImageBuf dst(OIIO::ImageSpec(converted->width(),
                                     converted->height(),
                                     ChannelCount(converted->format()),
                                     GetOIIOTypeDesc(converted->format())));

  if (dst.copy_pixels(src)) {

    // Convert our buffer back to a frame
    OIIODecoder::BufferToFrame(&dst, converted);

    return converted;
  } else {
    return nullptr;
  }
}

OLIVE_NAMESPACE_EXIT