forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoDecoder.cpp
More file actions
139 lines (115 loc) · 3.38 KB
/
Copy pathVideoDecoder.cpp
File metadata and controls
139 lines (115 loc) · 3.38 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
#include "VideoDecoder.hpp"
#include <AvTranscoder/codec/ICodec.hpp>
#include <AvTranscoder/stream/InputStream.hpp>
#include <AvTranscoder/frame/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 )
, _frame ( NULL )
{
}
VideoDecoder::~VideoDecoder()
{
if( _frame != NULL )
{
#if LIBAVCODEC_VERSION_MAJOR > 54
av_frame_free( &_frame );
#else
#if LIBAVCODEC_VERSION_MAJOR > 53
avcodec_free_frame( &_frame );
#else
av_free( _frame );
#endif
#endif
_frame = NULL;
}
}
void VideoDecoder::setup()
{
_inputStream->getVideoCodec().open();
#if LIBAVCODEC_VERSION_MAJOR > 54
_frame = av_frame_alloc();
#else
_frame = avcodec_alloc_frame();
#endif
if( _frame == NULL )
{
throw std::runtime_error( "unable to setup frame buffer" );
}
}
bool VideoDecoder::decodeNextFrame( Frame& frameBuffer )
{
if( ! decodeNextFrame() )
return false;
size_t decodedSize = avpicture_get_size( (AVPixelFormat)_frame->format, _frame->width, _frame->height );
if( decodedSize == 0 )
return false;
VideoFrame& imageBuffer = static_cast<VideoFrame&>( frameBuffer );
imageBuffer.resize( decodedSize );
// Copy pixel data from an AVPicture into one contiguous buffer.
avpicture_layout( (AVPicture*)_frame, (AVPixelFormat)_frame->format, _frame->width, _frame->height, imageBuffer.getData(), frameBuffer.getSize() );
return true;
}
bool VideoDecoder::decodeNextFrame( Frame& frameBuffer, const size_t subStreamIndex )
{
return false;
}
bool VideoDecoder::decodeNextFrame()
{
int got_frame = 0;
while( ! got_frame )
{
CodedData data;
bool nextPacketRead = _inputStream->readNextPacket( data );
if( ! nextPacketRead ) // error or end of file
data.clear();
int ret = avcodec_decode_video2( &_inputStream->getVideoCodec().getAVCodecContext(), _frame, &got_frame, &data.getAVPacket() );
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // no frame could be decompressed
return false;
if( ret < 0 )
{
throw std::runtime_error( "an error occured during video decoding - " + getDescriptionFromErrorCode( ret ) );
}
}
return true;
}
void VideoDecoder::flushDecoder()
{
avcodec_flush_buffers( &_inputStream->getVideoCodec().getAVCodecContext() );
}
void VideoDecoder::setProfile( const ProfileLoader::Profile& 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 ).setString( "auto" );
// 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::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() )
}
}
}
}