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 "avframe-queue.hpp"
|
|
|
|
#include "tools.hpp"
|
|
|
|
|
2021-06-08 02:12:55 +00:00
|
|
|
using namespace streamfx::ffmpeg;
|
2020-01-13 21:40:15 +00:00
|
|
|
|
|
|
|
std::shared_ptr<AVFrame> avframe_queue::create_frame()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<AVFrame> frame = std::shared_ptr<AVFrame>(av_frame_alloc(), [](AVFrame* frame) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
});
|
2020-01-13 21:40:15 +00:00
|
|
|
frame->width = this->_resolution.first;
|
|
|
|
frame->height = this->_resolution.second;
|
|
|
|
frame->format = this->_format;
|
2020-01-13 00:52:30 +00:00
|
|
|
|
|
|
|
int res = av_frame_get_buffer(frame.get(), 32);
|
|
|
|
if (res < 0) {
|
2020-04-02 15:05:02 +00:00
|
|
|
throw std::runtime_error(tools::get_error_description(res));
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
avframe_queue::avframe_queue() {}
|
2020-01-13 00:52:30 +00:00
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
avframe_queue::~avframe_queue()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void avframe_queue::set_resolution(int32_t const width, int32_t const height)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
this->_resolution.first = width;
|
|
|
|
this->_resolution.second = height;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void avframe_queue::get_resolution(int32_t& width, int32_t& height)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
width = this->_resolution.first;
|
|
|
|
height = this->_resolution.second;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
int32_t avframe_queue::get_width()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
return this->_resolution.first;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
int32_t avframe_queue::get_height()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
return this->_resolution.second;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
void avframe_queue::set_pixel_format(AVPixelFormat const format)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
this->_format = format;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
AVPixelFormat avframe_queue::get_pixel_format()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
return this->_format;
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
void avframe_queue::precache(std::size_t count)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t n = 0; n < count; n++) {
|
2020-01-13 00:52:30 +00:00
|
|
|
push(create_frame());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
void avframe_queue::clear()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
std::unique_lock<std::mutex> ulock(this->_lock);
|
|
|
|
_frames.clear();
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
void avframe_queue::push(std::shared_ptr<AVFrame> const frame)
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
std::unique_lock<std::mutex> ulock(this->_lock);
|
|
|
|
_frames.push_back(frame);
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
std::shared_ptr<AVFrame> avframe_queue::pop()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
std::unique_lock<std::mutex> ulock(this->_lock);
|
2020-01-13 00:52:30 +00:00
|
|
|
std::shared_ptr<AVFrame> ret;
|
|
|
|
while (ret == nullptr) {
|
2020-01-13 21:40:15 +00:00
|
|
|
if (_frames.size() == 0) {
|
2020-01-13 00:52:30 +00:00
|
|
|
ret = create_frame();
|
|
|
|
} else {
|
2020-01-13 21:40:15 +00:00
|
|
|
ret = _frames.front();
|
2020-01-13 00:52:30 +00:00
|
|
|
if (ret == nullptr) {
|
|
|
|
ret = create_frame();
|
|
|
|
} else {
|
2020-01-13 21:40:15 +00:00
|
|
|
_frames.pop_front();
|
2020-08-10 01:29:05 +00:00
|
|
|
if ((static_cast<int32_t>(ret->width) != this->_resolution.first)
|
|
|
|
|| (static_cast<int32_t>(ret->height) != this->_resolution.second)
|
2020-01-13 21:40:15 +00:00
|
|
|
|| (ret->format != this->_format)) {
|
2020-01-13 00:52:30 +00:00
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
std::shared_ptr<AVFrame> avframe_queue::pop_only()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
std::unique_lock<std::mutex> ulock(this->_lock);
|
|
|
|
if (_frames.size() == 0) {
|
2020-01-13 00:52:30 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-13 21:40:15 +00:00
|
|
|
std::shared_ptr<AVFrame> ret = _frames.front();
|
2020-01-13 00:52:30 +00:00
|
|
|
if (ret == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-13 21:40:15 +00:00
|
|
|
_frames.pop_front();
|
2020-01-13 00:52:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:15 +00:00
|
|
|
bool avframe_queue::empty()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
return _frames.empty();
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
std::size_t avframe_queue::size()
|
2020-01-13 00:52:30 +00:00
|
|
|
{
|
2020-01-13 21:40:15 +00:00
|
|
|
return _frames.size();
|
2020-01-13 00:52:30 +00:00
|
|
|
}
|