Initial code (pre-GitHub)

Contains:
- ffmpeg object wrappers
- base encoder class
- Apply ProRes encoder (prores_aw)
- OBS plugin structure
This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2018-11-13 19:04:13 +01:00
parent e1d41695a2
commit ec75fe23fe
24 changed files with 2515 additions and 0 deletions
+151
View File
@@ -0,0 +1,151 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include "avframe-queue.hpp"
#include "tools.hpp"
AVFrame* ffmpeg::avframe_queue::create_frame()
{
AVFrame* frame = av_frame_alloc();
frame->width = this->resolution.first;
frame->height = this->resolution.second;
frame->format = this->format;
int res = av_frame_get_buffer(frame, 0);
if (res < 0) {
throw std::exception(ffmpeg::tools::get_error_description(res));
}
return frame;
}
void ffmpeg::avframe_queue::destroy_frame(AVFrame* frame)
{
if (frame == nullptr)
return;
av_frame_free(&frame);
}
ffmpeg::avframe_queue::avframe_queue() {}
ffmpeg::avframe_queue::~avframe_queue()
{
clear();
}
void ffmpeg::avframe_queue::set_resolution(uint32_t width, uint32_t height)
{
this->resolution.first = width;
this->resolution.second = height;
}
void ffmpeg::avframe_queue::get_resolution(uint32_t& width, uint32_t& height)
{
width = this->resolution.first;
height = this->resolution.second;
}
uint32_t ffmpeg::avframe_queue::get_width()
{
return this->resolution.first;
}
uint32_t ffmpeg::avframe_queue::get_height()
{
return this->resolution.second;
}
void ffmpeg::avframe_queue::set_pixel_format(AVPixelFormat format)
{
this->format = format;
}
AVPixelFormat ffmpeg::avframe_queue::get_pixel_format()
{
return this->format;
}
void ffmpeg::avframe_queue::precache(size_t count)
{
for (size_t n = 0; n < count; n++) {
push(create_frame());
}
}
void ffmpeg::avframe_queue::clear()
{
std::unique_lock<std::mutex> ulock(this->lock);
for (AVFrame* frame : frames) {
destroy_frame(frame);
}
frames.clear();
}
void ffmpeg::avframe_queue::push(AVFrame* frame)
{
std::unique_lock<std::mutex> ulock(this->lock);
frames.push_back(frame);
}
AVFrame* ffmpeg::avframe_queue::pop()
{
std::unique_lock<std::mutex> ulock(this->lock);
AVFrame* ret = nullptr;
while (ret == nullptr) {
if (frames.size() == 0) {
ret = create_frame();
} else {
ret = frames.front();
if (ret == nullptr) {
ret = create_frame();
} else {
frames.pop_front();
if ((ret->width != this->resolution.first) || (ret->height != this->resolution.second)
|| (ret->format != this->format)) {
destroy_frame(ret);
ret = nullptr;
}
}
}
}
return ret;
}
AVFrame* ffmpeg::avframe_queue::pop_only()
{
std::unique_lock<std::mutex> ulock(this->lock);
if (frames.size() == 0) {
return nullptr;
}
AVFrame* ret = frames.front();
if (ret == nullptr) {
return nullptr;
}
frames.pop_front();
return ret;
}
bool ffmpeg::avframe_queue::empty()
{
return frames.empty();
}
size_t ffmpeg::avframe_queue::size()
{
return frames.size();
}
+70
View File
@@ -0,0 +1,70 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef OBS_FFMPEG_FFMPEG_AVFRAME_QUEUE
#define OBS_FFMPEG_FFMPEG_AVFRAME_QUEUE
#pragma once
#include <mutex>
#include <deque>
extern "C" {
#include <libavutil/frame.h>
}
namespace ffmpeg {
class avframe_queue {
std::deque<AVFrame*> frames;
std::mutex lock;
std::pair<uint32_t, uint32_t> resolution;
AVPixelFormat format = AV_PIX_FMT_NONE;
AVFrame* create_frame();
void destroy_frame(AVFrame* frame);
public:
avframe_queue();
~avframe_queue();
void set_resolution(uint32_t width, uint32_t height);
void get_resolution(uint32_t& width, uint32_t& height);
uint32_t get_width();
uint32_t get_height();
void set_pixel_format(AVPixelFormat format);
AVPixelFormat get_pixel_format();
void precache(size_t count);
void clear();
void push(AVFrame* frame);
AVFrame* pop();
AVFrame* pop_only();
bool empty();
size_t size();
};
} // namespace ffmpeg
#endif OBS_FFMPEG_FFMPEG_AVFRAME_QUEUE
+196
View File
@@ -0,0 +1,196 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include "swscale.hpp"
#include <stdexcept>
ffmpeg::swscale::swscale() {}
ffmpeg::swscale::~swscale()
{
finalize();
}
void ffmpeg::swscale::set_source_size(uint32_t width, uint32_t height)
{
source_size.first = width;
source_size.second = height;
}
void ffmpeg::swscale::get_source_size(uint32_t& width, uint32_t& height)
{
width = this->source_size.first;
height = this->source_size.second;
}
std::pair<uint32_t, uint32_t> ffmpeg::swscale::get_source_size()
{
return this->source_size;
}
uint32_t ffmpeg::swscale::get_source_width()
{
return this->source_size.first;
}
uint32_t ffmpeg::swscale::get_source_height()
{
return this->source_size.second;
}
void ffmpeg::swscale::set_source_format(AVPixelFormat format)
{
source_format = format;
}
AVPixelFormat ffmpeg::swscale::get_source_format()
{
return this->source_format;
}
void ffmpeg::swscale::set_source_color(bool full_range, AVColorSpace space)
{
source_full_range = full_range;
source_colorspace = space;
}
void ffmpeg::swscale::set_source_colorspace(AVColorSpace space)
{
this->source_colorspace = space;
}
AVColorSpace ffmpeg::swscale::get_source_colorspace()
{
return this->source_colorspace;
}
void ffmpeg::swscale::set_source_full_range(bool full_range)
{
this->source_full_range = full_range;
}
bool ffmpeg::swscale::is_source_full_range()
{
return this->source_full_range;
}
void ffmpeg::swscale::set_target_size(uint32_t width, uint32_t height)
{
target_size.first = width;
target_size.second = height;
}
void ffmpeg::swscale::get_target_size(uint32_t& width, uint32_t& height) {}
std::pair<uint32_t, uint32_t> ffmpeg::swscale::get_target_size()
{
return this->target_size;
}
uint32_t ffmpeg::swscale::get_target_width()
{
return this->target_size.first;
}
uint32_t ffmpeg::swscale::get_target_height()
{
return this->target_size.second;
}
void ffmpeg::swscale::set_target_format(AVPixelFormat format)
{
target_format = format;
}
AVPixelFormat ffmpeg::swscale::get_target_format()
{
return this->target_format;
}
void ffmpeg::swscale::set_target_color(bool full_range, AVColorSpace space)
{
target_full_range = full_range;
target_colorspace = space;
}
void ffmpeg::swscale::set_target_colorspace(AVColorSpace space)
{
this->target_colorspace = space;
}
AVColorSpace ffmpeg::swscale::get_target_colorspace()
{
return this->target_colorspace;
}
void ffmpeg::swscale::set_target_full_range(bool full_range)
{
this->target_full_range = full_range;
}
bool ffmpeg::swscale::is_target_full_range()
{
return this->target_full_range;
}
bool ffmpeg::swscale::initialize(int flags)
{
if (this->context) {
return false;
}
if (source_size.first == 0 || source_size.second == 0 || source_format == AV_PIX_FMT_NONE
|| source_colorspace == AVCOL_SPC_UNSPECIFIED) {
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
|| target_colorspace == AVCOL_SPC_UNSPECIFIED) {
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,
target_size.second, target_format, flags, nullptr, nullptr, nullptr);
if (!this->context) {
return false;
}
sws_setColorspaceDetails(this->context, sws_getCoefficients(source_colorspace), source_full_range ? 1 : 0,
sws_getCoefficients(target_colorspace), target_full_range ? 1 : 0, 1l << 16 | 0l,
1l << 16 | 0l, 1l << 16 | 0l);
return true;
}
bool ffmpeg::swscale::finalize()
{
if (this->context) {
sws_freeContext(this->context);
this->context = nullptr;
return true;
}
return false;
}
int32_t ffmpeg::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[])
{
if (!this->context) {
return 0;
}
int height =
sws_scale(this->context, source_data, source_stride, source_row, source_rows, target_data, target_stride);
return height;
}
+82
View File
@@ -0,0 +1,82 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef OBS_FFMPEG_FFMPEG_SWSCALE
#define OBS_FFMPEG_FFMPEG_SWSCALE
#pragma once
#include <cinttypes>
#include <utility>
extern "C" {
#include <libavutil/pixfmt.h>
#include <libswscale/swscale.h>
}
namespace ffmpeg {
class swscale {
std::pair<uint32_t, uint32_t> source_size;
AVPixelFormat source_format = AV_PIX_FMT_NONE;
bool source_full_range = false;
AVColorSpace source_colorspace = AVCOL_SPC_UNSPECIFIED;
std::pair<uint32_t, uint32_t> target_size;
AVPixelFormat target_format = AV_PIX_FMT_NONE;
bool target_full_range = false;
AVColorSpace target_colorspace = AVCOL_SPC_UNSPECIFIED;
SwsContext* context = nullptr;
public:
swscale();
~swscale();
void set_source_size(uint32_t width, uint32_t height);
void get_source_size(uint32_t& width, uint32_t& height);
std::pair<uint32_t, uint32_t> get_source_size();
uint32_t get_source_width();
uint32_t get_source_height();
void set_source_format(AVPixelFormat format);
AVPixelFormat get_source_format();
void set_source_color(bool full_range, AVColorSpace space);
void set_source_colorspace(AVColorSpace space);
AVColorSpace get_source_colorspace();
void set_source_full_range(bool full_range);
bool is_source_full_range();
void set_target_size(uint32_t width, uint32_t height);
void get_target_size(uint32_t& width, uint32_t& height);
std::pair<uint32_t, uint32_t> get_target_size();
uint32_t get_target_width();
uint32_t get_target_height();
void set_target_format(AVPixelFormat format);
AVPixelFormat get_target_format();
void set_target_color(bool full_range, AVColorSpace space);
void set_target_colorspace(AVColorSpace space);
AVColorSpace get_target_colorspace();
void set_target_full_range(bool full_range);
bool is_target_full_range();
bool initialize(int flags);
bool finalize();
int32_t 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[]);
};
} // namespace ffmpeg
#endif OBS_FFMPEG_FFMPEG_SWSCALE
+126
View File
@@ -0,0 +1,126 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include "tools.hpp"
#include <stdexcept>
extern "C" {
#include <libavutil/error.h>
#include <libavutil/pixdesc.h>
}
const char* ffmpeg::tools::get_pixel_format_name(AVPixelFormat v)
{
return av_get_pix_fmt_name(v);
}
const char* ffmpeg::tools::get_color_space_name(AVColorSpace v)
{
switch (v) {
case AVCOL_SPC_RGB:
return "RGB";
case AVCOL_SPC_BT709:
return "BT.709";
case AVCOL_SPC_FCC:
return "FCC Title 47 CoFR 73.682 (a)(20)";
case AVCOL_SPC_BT470BG:
return "BT.601 625";
case AVCOL_SPC_SMPTE170M:
case AVCOL_SPC_SMPTE240M:
return "BT.601 525";
case AVCOL_SPC_YCGCO:
return "ITU-T SG16";
case AVCOL_SPC_BT2020_NCL:
return "BT.2020 NCL";
case AVCOL_SPC_BT2020_CL:
return "BT.2020 CL";
case AVCOL_SPC_SMPTE2085:
return "SMPTE 2085";
case AVCOL_SPC_CHROMA_DERIVED_NCL:
return "Chroma NCL";
case AVCOL_SPC_CHROMA_DERIVED_CL:
return "Chroma CL";
case AVCOL_SPC_ICTCP:
return "BT.2100";
case AVCOL_SPC_NB:
return "Not Part of ABI";
}
return "Unknown";
}
const char* ffmpeg::tools::get_error_description(int error)
{
switch (error) {
case AVERROR(EPERM):
return "Permission Denied";
case AVERROR(ENOMEM):
return "Out Of Memory";
case AVERROR(EINVAL):
return "Invalid Value for Parameter";
}
return "Not Translated Yet";
}
AVPixelFormat ffmpeg::tools::obs_videoformat_to_avpixelformat(video_format v)
{
switch (v) {
// 32-Bits
case VIDEO_FORMAT_BGRX:
return AV_PIX_FMT_BGRA;
case VIDEO_FORMAT_BGRA:
return AV_PIX_FMT_BGRA;
case VIDEO_FORMAT_RGBA:
return AV_PIX_FMT_RGBA;
case VIDEO_FORMAT_I444:
return AV_PIX_FMT_YUV444P;
case VIDEO_FORMAT_YUY2:
return AV_PIX_FMT_YUYV422;
case VIDEO_FORMAT_YVYU:
return AV_PIX_FMT_YVYU422;
case VIDEO_FORMAT_UYVY:
return AV_PIX_FMT_UYVY422;
case VIDEO_FORMAT_I420:
return AV_PIX_FMT_YUV420P;
case VIDEO_FORMAT_NV12:
return AV_PIX_FMT_NV12;
}
throw std::invalid_argument("unknown format");
}
AVColorSpace ffmpeg::tools::obs_videocolorspace_to_avcolorspace(video_colorspace v)
{
switch (v) {
case VIDEO_CS_DEFAULT:
case VIDEO_CS_709:
return AVCOL_SPC_BT709;
case VIDEO_CS_601:
return AVCOL_SPC_SMPTE170M;
}
throw std::invalid_argument("unknown color space");
}
AVColorRange ffmpeg::tools::obs_videorangetype_to_avcolorrange(video_range_type v)
{
switch (v) {
case VIDEO_RANGE_DEFAULT:
case VIDEO_RANGE_FULL:
return AVCOL_RANGE_JPEG;
case VIDEO_RANGE_PARTIAL:
return AVCOL_RANGE_MPEG;
}
throw std::invalid_argument("unknown range");
}
+44
View File
@@ -0,0 +1,44 @@
// FFMPEG Video Encoder Integration for OBS Studio
// Copyright (C) 2018 - 2018 Michael Fabian Dirks
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef OBS_FFMPEG_FFMPEG_UTILITY
#define OBS_FFMPEG_FFMPEG_UTILITY
#pragma once
#include <obs.h>
extern "C" {
#include <libavutil/pixfmt.h>
}
namespace ffmpeg {
namespace tools {
const char* get_pixel_format_name(AVPixelFormat v);
const char* get_color_space_name(AVColorSpace v);
const char* get_error_description(int error);
AVPixelFormat obs_videoformat_to_avpixelformat(video_format v);
AVColorSpace obs_videocolorspace_to_avcolorspace(video_colorspace v);
AVColorRange obs_videorangetype_to_avcolorrange(video_range_type v);
}
} // namespace ffmpeg
#endif OBS_FFMPEG_FFMPEG_UTILITY