-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathutil.cpp
More file actions
251 lines (215 loc) · 6.08 KB
/
Copy pathutil.cpp
File metadata and controls
251 lines (215 loc) · 6.08 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "util.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixdesc.h>
}
#include <utility>
#include <algorithm>
namespace avtranscoder
{
bool matchFormat( const std::string& format, const std::string& filename )
{
AVOutputFormat* avOutputFormat = av_guess_format( format.c_str(), filename.c_str(), NULL);
return avOutputFormat != NULL;
}
std::vector<std::string> getPixelFormats( const std::string& videoCodecName )
{
std::vector<std::string> pixelFormats;
// all video codec concerned
if( videoCodecName == "" )
{
const AVPixFmtDescriptor* pixFmtDesc = NULL;
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT( 51, 44, 0 )
for( int pix_fmt = 0; pix_fmt < PIX_FMT_NB; ++pix_fmt )
pixFmtDesc = &av_pix_fmt_descriptors[pix_fmt];
#else
while( ( pixFmtDesc = av_pix_fmt_desc_next( pixFmtDesc ) ) != NULL )
#endif
{
if( ! pixFmtDesc->name )
continue;
pixelFormats.push_back( std::string( pixFmtDesc->name ) );
}
}
// specific video codec
else
{
const AVCodec* videoCodec = avcodec_find_encoder_by_name( videoCodecName.c_str() );
if( videoCodec && videoCodec->pix_fmts != NULL )
{
size_t pix_fmt = 0;
while( videoCodec->pix_fmts[pix_fmt] != -1 )
{
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT( 51, 44, 0 )
const AVPixFmtDescriptor* pix_desc = &av_pix_fmt_descriptors[ videoCodec->pix_fmts[pix_fmt] ];
#else
const AVPixFmtDescriptor* pix_desc = av_pix_fmt_desc_get( videoCodec->pix_fmts[pix_fmt] );
#endif
if( ! pix_desc || ! pix_desc->name )
continue;
pixelFormats.push_back( std::string( pix_desc->name ) );
++pix_fmt;
}
}
}
return pixelFormats;
}
std::vector<std::string> getSampleFormats( const std::string& audioCodecName )
{
std::vector<std::string> sampleFormats;
// all audio codec concerned
if( audioCodecName.empty() )
{
for( size_t sampleFormat = 0; sampleFormat < AV_SAMPLE_FMT_NB; ++sampleFormat)
{
sampleFormats.push_back( av_get_sample_fmt_name( static_cast<AVSampleFormat>( sampleFormat ) ) );
}
}
// specific audio codec
else
{
const AVCodec* audioCodec = avcodec_find_encoder_by_name( audioCodecName.c_str() );
if( audioCodec && audioCodec->sample_fmts != NULL )
{
size_t sample_fmt = 0;
while( audioCodec->sample_fmts[sample_fmt] != -1 )
{
const char* sampleFormatName = av_get_sample_fmt_name( audioCodec->sample_fmts[sample_fmt] );
if( sampleFormatName )
sampleFormats.push_back( std::string( sampleFormatName ) );
sample_fmt++;
}
}
}
return sampleFormats;
}
AVPixelFormat getAVPixelFormat( const std::string& pixelFormat )
{
return av_get_pix_fmt( pixelFormat.c_str() );
}
AVSampleFormat getAVSampleFormat( const std::string& sampleFormat )
{
return av_get_sample_fmt( sampleFormat.c_str() );
}
NamesArray getFormatsNames()
{
NamesArray formatsNames;
AVOutputFormat* fmt = NULL;
while( ( fmt = av_oformat_next( fmt ) ) )
{
// skip undefined codec
if( fmt->video_codec == AV_CODEC_ID_NONE )
continue;
if( ! fmt->name && ! fmt->long_name )
continue;
formatsNames.push_back( std::make_pair( std::string( fmt->name ? fmt->name : "" ), std::string( fmt->long_name ? fmt->long_name : "" ) ) );
}
return formatsNames;
}
NamesArray getVideoCodecsNames()
{
NamesArray videoCodecsNames;
AVCodec* c = NULL;
while( ( c = av_codec_next( c ) ) != NULL )
{
if( c->type == AVMEDIA_TYPE_VIDEO)
{
if( ! c->name && ! c->long_name )
continue;
std::pair< std::string, std::string > codecNames( std::string( c->name ? c->name : "" ), std::string( c->long_name ? c->long_name : "" ) );
// skip duplicates
if( std::find( videoCodecsNames.begin(), videoCodecsNames.end(), codecNames ) != videoCodecsNames.end() )
continue;
videoCodecsNames.push_back( codecNames );
}
}
return videoCodecsNames;
}
NamesArray getAudioCodecsNames()
{
NamesArray audioCodecsNames;
AVCodec* c = NULL;
while( ( c = av_codec_next( c ) ) != NULL )
{
if( c->type == AVMEDIA_TYPE_AUDIO )
{
if( ! c->name && ! c->long_name )
continue;
std::pair< std::string, std::string > codecNames( std::string( c->name ? c->name : "" ), std::string( c->long_name ? c->long_name : "" ) );
// skip duplicates
if( std::find( audioCodecsNames.begin(), audioCodecsNames.end(), codecNames ) != audioCodecsNames.end() )
continue;
audioCodecsNames.push_back( codecNames );
}
}
return audioCodecsNames;
}
OptionArrayMap getOutputFormatOptions()
{
OptionArrayMap optionsPerFormat;
AVOutputFormat* outputFormat = av_oformat_next( NULL );
// iterate on formats
while( outputFormat )
{
// add only format with video track
// outputFormat->audio_codec ?
if( outputFormat->video_codec != AV_CODEC_ID_NONE )
{
if( outputFormat->priv_class )
{
const std::string outputFormatName( outputFormat->name );
OptionArray options;
loadOptions( options, (void*)&outputFormat->priv_class, 0 );
optionsPerFormat.insert( std::make_pair( outputFormatName, options ) );
}
}
outputFormat = av_oformat_next( outputFormat );
}
return optionsPerFormat;
}
OptionArrayMap getVideoCodecOptions()
{
OptionArrayMap videoCodecOptions;
AVCodec* codec = av_codec_next( NULL );
// iterate on codecs
while( codec )
{
// add only video codec
if( codec->type == AVMEDIA_TYPE_VIDEO )
{
if( codec->priv_class )
{
std::string videoCodecName( codec->name );
OptionArray options;
loadOptions( options, (void*)&codec->priv_class, 0 );
videoCodecOptions.insert( std::make_pair( videoCodecName, options ) );
}
}
codec = av_codec_next( codec );
}
return videoCodecOptions;
}
OptionArrayMap getAudioCodecOptions()
{
OptionArrayMap audioCodecOptions;
AVCodec* codec = av_codec_next( NULL );
// iterate on codecs
while( codec )
{
// add only audio codec
if( codec->type == AVMEDIA_TYPE_AUDIO )
{
if( codec->priv_class )
{
std::string audioCodecName( codec->name );
OptionArray options;
loadOptions( options, (void*)&codec->priv_class, 0 );
audioCodecOptions.insert( std::make_pair( audioCodecName, options ) );
}
}
codec = av_codec_next( codec );
}
return audioCodecOptions;
}
}