forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoFrame.cpp
More file actions
127 lines (109 loc) · 3.55 KB
/
Copy pathVideoFrame.cpp
File metadata and controls
127 lines (109 loc) · 3.55 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "VideoFrame.hpp"
#include <AvTranscoder/util.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/pixdesc.h>
}
#include <stdexcept>
#include <stdlib.h>
namespace avtranscoder
{
VideoFrameDesc::VideoFrameDesc(const size_t width, const size_t height, const std::string& pixelFormatName)
: _width(width)
, _height(height)
, _pixelFormat(getAVPixelFormat(pixelFormatName))
, _fps(1.0)
{
}
VideoFrameDesc::VideoFrameDesc(const ProfileLoader::Profile& profile)
: _width(0)
, _height(0)
, _pixelFormat(AV_PIX_FMT_NONE)
, _fps(1.0)
{
setParameters(profile);
}
void VideoFrameDesc::setParameters(const ProfileLoader::Profile& profile)
{
// width
if(profile.count(constants::avProfileWidth))
_width = atoi(profile.find(constants::avProfileWidth)->second.c_str());
// height
if(profile.count(constants::avProfileHeight))
_height = atoi(profile.find(constants::avProfileHeight)->second.c_str());
// pixel format
if(profile.count(constants::avProfilePixelFormat))
_pixelFormat = getAVPixelFormat(profile.find(constants::avProfilePixelFormat)->second);
// fps
if(profile.count(constants::avProfileFrameRate))
_fps = atof(profile.find(constants::avProfileFrameRate)->second.c_str());
}
VideoFrame::VideoFrame(const VideoFrameDesc& desc, const bool forceDataAllocation)
: IFrame()
, _desc(desc)
{
_frame->width = desc._width;
_frame->height = desc._height;
_frame->format = desc._pixelFormat;
if(forceDataAllocation)
allocateData();
}
VideoFrame::~VideoFrame()
{
if(_frame->buf[0])
av_frame_unref(_frame);
if(_dataAllocated)
freeData();
}
size_t VideoFrame::getDataSize() const
{
if(getPixelFormat() == AV_PIX_FMT_NONE)
{
LOG_WARN("Incorrect pixel format when get size of video frame: return a size of 0.")
return 0;
}
const size_t size = avpicture_get_size(getPixelFormat(), getWidth(), getHeight());
if(size == 0)
throw std::runtime_error("Unable to determine image buffer size: " + getDescriptionFromErrorCode(size));
return size;
}
void VideoFrame::allocateData()
{
if(_dataAllocated)
LOG_WARN("The VideoFrame seems to already have allocated data. This could lead to memory leaks.")
// Set Frame properties
_frame->width = _desc._width;
_frame->height = _desc._height;
_frame->format = _desc._pixelFormat;
// Allocate data
const int ret = avpicture_alloc(reinterpret_cast<AVPicture*>(_frame), _desc._pixelFormat, _desc._width, _desc._height);
if(ret < 0)
{
const std::string formatName = getPixelFormatName(_desc._pixelFormat);
std::stringstream msg;
msg << "Unable to allocate an image frame of ";
msg << "width = " << _frame->width << ", ";
msg << "height = " << _frame->height << ", ";
msg << "pixel format = " << (formatName.empty() ? "none" : formatName);
LOG_ERROR(msg.str())
throw std::bad_alloc();
}
_dataAllocated = true;
}
void VideoFrame::freeData()
{
avpicture_free(reinterpret_cast<AVPicture*>(_frame));
_dataAllocated = false;
}
void VideoFrame::assignBuffer(const unsigned char* ptrValue)
{
const int ret =
avpicture_fill(reinterpret_cast<AVPicture*>(_frame), ptrValue, getPixelFormat(), getWidth(), getHeight());
if(ret < 0)
{
std::stringstream msg;
msg << "Unable to assign an image buffer of " << getDataSize() << " bytes: " << getDescriptionFromErrorCode(ret);
throw std::runtime_error(msg.str());
}
}
}