-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathVideoDecoder.cpp
More file actions
141 lines (114 loc) · 4.07 KB
/
Copy pathVideoDecoder.cpp
File metadata and controls
141 lines (114 loc) · 4.07 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "VideoDecoder.hpp"
#include <AvTranscoder/codec/ICodec.hpp>
#include <AvTranscoder/stream/InputStream.hpp>
#include <AvTranscoder/data/decoded/VideoFrame.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/pixdesc.h>
}
#include <stdexcept>
namespace avtranscoder
{
VideoDecoder::VideoDecoder(InputStream& inputStream)
: _inputStream(&inputStream)
, _isSetup(false)
{
}
VideoDecoder::~VideoDecoder()
{
if(_isSetup)
_inputStream->getVideoCodec().closeCodec();
}
void VideoDecoder::setupDecoder(const ProfileLoader::Profile& profile)
{
// check the given profile
const bool isValid = ProfileLoader::checkVideoProfile(profile);
if(!isValid && !profile.empty())
{
const std::string msg("Invalid video profile to setup decoder.");
LOG_ERROR(msg)
throw std::runtime_error(msg);
}
if(!profile.empty())
{
LOG_INFO("Setup video decoder with:\n" << profile)
}
VideoCodec& codec = _inputStream->getVideoCodec();
// set threads before any other options
if(profile.count(constants::avProfileThreads))
codec.getOption(constants::avProfileThreads).setString(profile.at(constants::avProfileThreads));
else
codec.getOption(constants::avProfileThreads).setInt(codec.getAVCodecContext().thread_count);
// set decoder options
for(ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it)
{
if((*it).first == constants::avProfileIdentificator || (*it).first == constants::avProfileIdentificatorHuman ||
(*it).first == constants::avProfileType || (*it).first == constants::avProfileCodec ||
(*it).first == constants::avProfileThreads)
continue;
try
{
Option& decodeOption = codec.getOption((*it).first);
decodeOption.setString((*it).second);
}
catch(std::exception& e)
{
LOG_WARN("VideoDecoder - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what())
}
}
// open decoder
codec.openCodec();
_isSetup = true;
}
bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer)
{
bool decodeNextFrame = false;
if(!_isSetup)
setupDecoder();
int got_frame = 0;
while(!got_frame)
{
CodedData data;
// reading
const bool nextPacketRead = _inputStream->readNextPacket(data);
// decoding
// @note could be called several times to return the remaining frames (last call with an empty packet)
// @see CODEC_CAP_DELAY
int ret = avcodec_send_packet(&_inputStream->getVideoCodec().getAVCodecContext(), &data.getAVPacket());
if (ret < 0 && (nextPacketRead || ret != AVERROR_EOF))
throw std::runtime_error("An error occurred sending video packet to decoder: " + getDescriptionFromErrorCode(ret));
ret = avcodec_receive_frame(&_inputStream->getVideoCodec().getAVCodecContext(), &frameBuffer.getAVFrame());
if (ret == 0)
got_frame = true;
else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
got_frame = false;
else
throw std::runtime_error("An error occurred receiving video packet from decoder: " + getDescriptionFromErrorCode(ret));
// if no frame could be decompressed
if ((!nextPacketRead && ret == 0) || !got_frame)
decodeNextFrame = false;
else
decodeNextFrame = true;
// if no frame read and decompressed
if(!nextPacketRead && !decodeNextFrame)
{
data.clear();
return false;
}
}
if(decodeNextFrame)
incrementNbDecodedFrames();
return decodeNextFrame;
}
bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer, const std::vector<size_t> channelIndexArray)
{
LOG_WARN("Decoding of a specific video channel is not supported.");
return false;
}
void VideoDecoder::flushDecoder()
{
avcodec_flush_buffers(&_inputStream->getVideoCodec().getAVCodecContext());
}
}