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
|
/***
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 "frame.h"
#include <QDebug>
#include <QtGlobal>
#include <QtMath>
OLIVE_NAMESPACE_ENTER
Frame::Frame() :
timestamp_(0),
sample_aspect_ratio_(1)
{
}
FramePtr Frame::Create()
{
return std::make_shared<Frame>();
}
const VideoParams &Frame::video_params() const
{
return params_;
}
void Frame::set_video_params(const VideoParams ¶ms)
{
params_ = params;
// Align linesize to 16
linesize_ = qCeil(static_cast<double>(width()) / 16.0) * 16;
}
int Frame::linesize_pixels() const
{
return linesize_;
}
int Frame::linesize_bytes() const
{
return linesize_pixels() * PixelFormat::BytesPerPixel(params_.format());
}
const int &Frame::width() const
{
return params_.effective_width();
}
const int &Frame::height() const
{
return params_.effective_height();
}
const PixelFormat::Format &Frame::format() const
{
return params_.format();
}
Color Frame::get_pixel(int x, int y) const
{
if (!contains_pixel(x, y)) {
return Color();
}
int pixel_index = y * linesize_pixels() + x;
int byte_offset = PixelFormat::GetBufferSize(video_params().format(), pixel_index, 1);
return Color(data_.data() + byte_offset, video_params().format());
}
bool Frame::contains_pixel(int x, int y) const
{
return (is_allocated() && x >= 0 && x < width() && y >= 0 && y < height());
}
void Frame::set_pixel(int x, int y, const Color &c)
{
if (!contains_pixel(x, y)) {
return;
}
int pixel_index = y * linesize_pixels() + x;
int byte_offset = PixelFormat::GetBufferSize(video_params().format(), pixel_index, 1);
c.toData(data_.data() + byte_offset, video_params().format());
}
const rational &Frame::sample_aspect_ratio() const
{
return sample_aspect_ratio_;
}
void Frame::set_sample_aspect_ratio(const rational &aspect_ratio)
{
sample_aspect_ratio_ = aspect_ratio;
}
const rational &Frame::timestamp() const
{
return timestamp_;
}
void Frame::set_timestamp(const rational ×tamp)
{
timestamp_ = timestamp;
}
const int64_t &Frame::native_timestamp()
{
return native_timestamp_;
}
void Frame::set_native_timestamp(const int64_t ×tamp)
{
native_timestamp_ = timestamp;
}
char *Frame::data()
{
return data_.data();
}
const char *Frame::const_data() const
{
return data_.constData();
}
void Frame::allocate()
{
// Assume this frame is intended to be a video frame
if (!params_.is_valid()) {
qWarning() << "Tried to allocate a frame with invalid parameters";
return;
}
data_.resize(PixelFormat::GetBufferSize(params_.format(), linesize_, params_.height()));
}
bool Frame::is_allocated() const
{
return !data_.isEmpty();
}
void Frame::destroy()
{
data_.clear();
}
int Frame::allocated_size() const
{
return data_.size();
}
OLIVE_NAMESPACE_EXIT
|