forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoDecoder.cpp
More file actions
127 lines (105 loc) · 3.55 KB
/
Copy pathVideoDecoder.cpp
File metadata and controls
127 lines (105 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 "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()
{
}
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
_inputStream->getVideoCodec().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
const int ret = avcodec_decode_video2(&_inputStream->getVideoCodec().getAVCodecContext(), &frameBuffer.getAVFrame(),
&got_frame, &data.getAVPacket());
if(ret < 0)
{
throw std::runtime_error("An error occurred during video decoding: " + getDescriptionFromErrorCode(ret));
}
// if no frame could be decompressed
if(!nextPacketRead && ret == 0 && got_frame == 0)
decodeNextFrame = false;
else
decodeNextFrame = true;
// if no frame read and decompressed
if(!nextPacketRead && !decodeNextFrame)
{
data.clear();
return false;
}
}
return decodeNextFrame;
}
bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer, const std::vector<size_t> channelIndexArray)
{
return false;
}
void VideoDecoder::flushDecoder()
{
avcodec_flush_buffers(&_inputStream->getVideoCodec().getAVCodecContext());
}
}