forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoTransform.cpp
More file actions
88 lines (71 loc) · 2.48 KB
/
Copy pathVideoTransform.cpp
File metadata and controls
88 lines (71 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "VideoTransform.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/pixdesc.h>
#if LIBAVCODEC_VERSION_MAJOR > 54
#include <libavutil/frame.h>
#endif
}
#include <sstream>
#include <iomanip>
#include <cassert>
#include <stdexcept>
namespace avtranscoder
{
VideoTransform::VideoTransform()
: _imageConvertContext(NULL)
, _isInit(false)
{
}
VideoTransform::~VideoTransform()
{
sws_freeContext(_imageConvertContext);
}
bool VideoTransform::init(const VideoFrame& src, const VideoFrame& dst)
{
const AVPixelFormat srcPixelFormat = src.getPixelFormat();
const AVPixelFormat dstPixelFormat = dst.getPixelFormat();
_imageConvertContext =
sws_getCachedContext(_imageConvertContext, src.getWidth(), src.getHeight(), srcPixelFormat, dst.getWidth(),
dst.getHeight(), dstPixelFormat, SWS_POINT, NULL, NULL, NULL);
if(!_imageConvertContext)
{
throw std::runtime_error("unable to create color convert context");
}
const char* srcPixFmt;
srcPixFmt = av_get_pix_fmt_name(srcPixelFormat);
const char* dstPixFmt;
dstPixFmt = av_get_pix_fmt_name(dstPixelFormat);
std::stringstream msg;
msg << "Video conversion from " << (srcPixFmt != NULL ? srcPixFmt : "None") << " to "
<< (dstPixFmt != NULL ? dstPixFmt : "None") << std::endl;
msg << "Source, width = " << src.getWidth() << std::endl;
msg << "Source, height = " << src.getHeight() << std::endl;
msg << "Destination, width = " << dst.getWidth() << std::endl;
msg << "Destination, height = " << dst.getHeight() << std::endl;
LOG_INFO(msg.str())
return true;
}
void VideoTransform::convert(const IFrame& srcFrame, IFrame& dstFrame)
{
const VideoFrame& src = static_cast<const VideoFrame&>(srcFrame);
VideoFrame& dst = static_cast<VideoFrame&>(dstFrame);
assert(src.getWidth() > 0);
assert(src.getHeight() > 0);
assert(src.getPixelFormat() != AV_PIX_FMT_NONE);
assert(dst.getDataSize() > 0);
if(!_isInit)
_isInit = init(src, dst);
if(!_imageConvertContext)
{
throw std::runtime_error("unknown color convert context");
}
// Convert
const int ret = sws_scale(_imageConvertContext, src.getData(), src.getLineSize(), 0, src.getHeight(), dst.getData(),
dst.getLineSize());
if(ret != (int)dst.getHeight())
throw std::runtime_error("error in color converter");
}
}