obs-StreamFX/source/ffmpeg/swscale.cpp

207 lines
5 KiB
C++
Raw Normal View History

2020-01-13 00:52:30 +00:00
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "swscale.hpp"
#include <stdexcept>
2020-01-13 21:40:15 +00:00
using namespace ffmpeg;
2020-01-13 00:52:30 +00:00
2020-01-13 21:40:15 +00:00
swscale::swscale() {}
swscale::~swscale()
2020-01-13 00:52:30 +00:00
{
finalize();
}
2020-01-13 21:40:15 +00:00
void swscale::set_source_size(uint32_t width, uint32_t height)
2020-01-13 00:52:30 +00:00
{
source_size.first = width;
source_size.second = height;
}
2020-01-13 21:40:15 +00:00
void swscale::get_source_size(uint32_t& width, uint32_t& height)
2020-01-13 00:52:30 +00:00
{
width = this->source_size.first;
height = this->source_size.second;
}
2020-01-13 21:40:15 +00:00
std::pair<uint32_t, uint32_t> swscale::get_source_size()
2020-01-13 00:52:30 +00:00
{
return this->source_size;
}
2020-01-13 21:40:15 +00:00
uint32_t swscale::get_source_width()
2020-01-13 00:52:30 +00:00
{
return this->source_size.first;
}
2020-01-13 21:40:15 +00:00
uint32_t swscale::get_source_height()
2020-01-13 00:52:30 +00:00
{
return this->source_size.second;
}
2020-01-13 21:40:15 +00:00
void swscale::set_source_format(AVPixelFormat format)
2020-01-13 00:52:30 +00:00
{
source_format = format;
}
2020-01-13 21:40:15 +00:00
AVPixelFormat swscale::get_source_format()
2020-01-13 00:52:30 +00:00
{
return this->source_format;
}
2020-01-13 21:40:15 +00:00
void swscale::set_source_color(bool full_range, AVColorSpace space)
2020-01-13 00:52:30 +00:00
{
source_full_range = full_range;
source_colorspace = space;
}
2020-01-13 21:40:15 +00:00
void swscale::set_source_colorspace(AVColorSpace space)
2020-01-13 00:52:30 +00:00
{
this->source_colorspace = space;
}
2020-01-13 21:40:15 +00:00
AVColorSpace swscale::get_source_colorspace()
2020-01-13 00:52:30 +00:00
{
return this->source_colorspace;
}
2020-01-13 21:40:15 +00:00
void swscale::set_source_full_range(bool full_range)
2020-01-13 00:52:30 +00:00
{
this->source_full_range = full_range;
}
2020-01-13 21:40:15 +00:00
bool swscale::is_source_full_range()
2020-01-13 00:52:30 +00:00
{
return this->source_full_range;
}
2020-01-13 21:40:15 +00:00
void swscale::set_target_size(uint32_t width, uint32_t height)
2020-01-13 00:52:30 +00:00
{
target_size.first = width;
target_size.second = height;
}
2020-01-13 21:40:15 +00:00
void swscale::get_target_size(uint32_t& width, uint32_t& height)
2020-01-13 00:52:30 +00:00
{
width = target_size.first;
height = target_size.second;
}
2020-01-13 21:40:15 +00:00
std::pair<uint32_t, uint32_t> swscale::get_target_size()
2020-01-13 00:52:30 +00:00
{
return this->target_size;
}
2020-01-13 21:40:15 +00:00
uint32_t swscale::get_target_width()
2020-01-13 00:52:30 +00:00
{
return this->target_size.first;
}
2020-01-13 21:40:15 +00:00
uint32_t swscale::get_target_height()
2020-01-13 00:52:30 +00:00
{
return this->target_size.second;
}
2020-01-13 21:40:15 +00:00
void swscale::set_target_format(AVPixelFormat format)
2020-01-13 00:52:30 +00:00
{
target_format = format;
}
2020-01-13 21:40:15 +00:00
AVPixelFormat swscale::get_target_format()
2020-01-13 00:52:30 +00:00
{
return this->target_format;
}
2020-01-13 21:40:15 +00:00
void swscale::set_target_color(bool full_range, AVColorSpace space)
2020-01-13 00:52:30 +00:00
{
target_full_range = full_range;
target_colorspace = space;
}
2020-01-13 21:40:15 +00:00
void swscale::set_target_colorspace(AVColorSpace space)
2020-01-13 00:52:30 +00:00
{
this->target_colorspace = space;
}
2020-01-13 21:40:15 +00:00
AVColorSpace swscale::get_target_colorspace()
2020-01-13 00:52:30 +00:00
{
return this->target_colorspace;
}
2020-01-13 21:40:15 +00:00
void swscale::set_target_full_range(bool full_range)
2020-01-13 00:52:30 +00:00
{
this->target_full_range = full_range;
}
2020-01-13 21:40:15 +00:00
bool swscale::is_target_full_range()
2020-01-13 00:52:30 +00:00
{
return this->target_full_range;
}
2020-01-13 21:40:15 +00:00
bool swscale::initialize(int flags)
2020-01-13 00:52:30 +00:00
{
if (this->context) {
return false;
}
if (source_size.first == 0 || source_size.second == 0 || source_format == AV_PIX_FMT_NONE
2020-01-13 21:40:15 +00:00
|| source_colorspace == AVCOL_SPC_UNSPECIFIED) {
2020-01-13 00:52:30 +00:00
throw std::invalid_argument("not all source parameters were set");
}
if (target_size.first == 0 || target_size.second == 0 || target_format == AV_PIX_FMT_NONE
2020-01-13 21:40:15 +00:00
|| target_colorspace == AVCOL_SPC_UNSPECIFIED) {
2020-01-13 00:52:30 +00:00
throw std::invalid_argument("not all target parameters were set");
}
this->context = sws_getContext(source_size.first, source_size.second, source_format, target_size.first,
2020-01-13 21:40:15 +00:00
target_size.second, target_format, flags, nullptr, nullptr, nullptr);
2020-01-13 00:52:30 +00:00
if (!this->context) {
return false;
}
sws_setColorspaceDetails(this->context, sws_getCoefficients(source_colorspace), source_full_range ? 1 : 0,
2020-01-13 21:40:15 +00:00
sws_getCoefficients(target_colorspace), target_full_range ? 1 : 0, 1L << 16 | 0L,
1L << 16 | 0L, 1L << 16 | 0L);
2020-01-13 00:52:30 +00:00
return true;
}
2020-01-13 21:40:15 +00:00
bool swscale::finalize()
2020-01-13 00:52:30 +00:00
{
if (this->context) {
sws_freeContext(this->context);
this->context = nullptr;
return true;
}
return false;
}
2020-01-13 21:40:15 +00:00
int32_t swscale::convert(const uint8_t* const source_data[], const int source_stride[], int32_t source_row,
int32_t source_rows, uint8_t* const target_data[], const int target_stride[])
2020-01-13 00:52:30 +00:00
{
if (!this->context) {
return 0;
}
int height =
2020-01-13 21:40:15 +00:00
sws_scale(this->context, source_data, source_stride, source_row, source_rows, target_data, target_stride);
2020-01-13 00:52:30 +00:00
return height;
}