ffconv/ffcpp/Scaler.cpp

34 lines
950 B
C++

#include "Scaler.h"
#include <stdexcept>
namespace ffcpp {
ffcpp::Scaler::Scaler(int srcWidth, int srcHeight, AVPixelFormat srcPixFmt, int dstWidth, int dstHeight,
AVPixelFormat dstPixFmt) {
_dstWidth = dstWidth;
_dstHeight = dstHeight;
_dstPixFmt = dstPixFmt;
_swsContext = sws_getContext(srcWidth, srcHeight, srcPixFmt, dstWidth, dstHeight, dstPixFmt, SWS_BICUBIC,
nullptr, nullptr, nullptr);
if(!_swsContext) {
throw new std::runtime_error("cannot create scale context");
}
}
Frame Scaler::scale(Frame &inFrame) {
Frame outFrame(_dstWidth, _dstHeight, _dstPixFmt);
AVFrame* fin = inFrame;
AVFrame* fout = outFrame;
fout->pts = fin->pts;
int res = sws_scale(_swsContext, (uint8_t const * const *)fin->data, fin->linesize, 0, fin->height, fout->data, fout->linesize);
if(res < 0) {
throw new std::runtime_error("scale error");
}
return outFrame;
}
}