ffconv/ffcpp/Scaler.cpp

46 lines
1.4 KiB
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");
}
}
Scaler::Scaler(AVCodecContext *decoderCtx, AVCodecContext *encoderCtx)
: Scaler(decoderCtx->width, decoderCtx->height, decoderCtx->pix_fmt,
encoderCtx->width, encoderCtx->height, encoderCtx->pix_fmt) {
}
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;
}
bool Scaler::needScaling(AVCodecContext *decoderCtx, AVCodecContext *encoderCtx) {
return (decoderCtx->width != encoderCtx->width ||
decoderCtx->height != encoderCtx->height ||
decoderCtx->pix_fmt != encoderCtx->pix_fmt);
}
}