Skip to content

Commit a138e77

Browse files
committed
Merge pull request #236 from avTranscoder/fix_utilFunctionsToGetListOfFormatsAndCodecs
Fix util functions to get list of formats and codecs
2 parents 046cf9c + 6ce13be commit a138e77

File tree

4 files changed

+227
-84
lines changed

4 files changed

+227
-84
lines changed

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void OutputFile::setupWrapping(const ProfileLoader::Profile& profile)
266266
}
267267

268268
// check if output format indicated is valid with the filename extension
269-
if(!matchFormat(profile.find(constants::avProfileFormat)->second, getFilename()))
269+
if(!av_guess_format(profile.find(constants::avProfileFormat)->second.c_str(), getFilename().c_str(), NULL))
270270
{
271271
throw std::runtime_error("Invalid format according to the file extension.");
272272
}

src/AvTranscoder/util.cpp

Lines changed: 94 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "util.hpp"
22

33
extern "C" {
4-
#include <libavcodec/avcodec.h>
5-
#include <libavformat/avformat.h>
64
#include <libavutil/pixdesc.h>
75
}
86

@@ -12,13 +10,7 @@ extern "C" {
1210
namespace avtranscoder
1311
{
1412

15-
bool matchFormat(const std::string& format, const std::string& filename)
16-
{
17-
AVOutputFormat* avOutputFormat = av_guess_format(format.c_str(), filename.c_str(), NULL);
18-
return avOutputFormat != NULL;
19-
}
20-
21-
std::vector<std::string> getPixelFormats(const std::string& videoCodecName)
13+
std::vector<std::string> getSupportedPixelFormats(const std::string& videoCodecName)
2214
{
2315
std::vector<std::string> pixelFormats;
2416

@@ -63,7 +55,7 @@ std::vector<std::string> getPixelFormats(const std::string& videoCodecName)
6355
return pixelFormats;
6456
}
6557

66-
std::vector<std::string> getSampleFormats(const std::string& audioCodecName)
58+
std::vector<std::string> getSupportedSampleFormats(const std::string& audioCodecName)
6759
{
6860
std::vector<std::string> sampleFormats;
6961

@@ -117,77 +109,109 @@ std::string getSampleFormatName(const AVSampleFormat sampleFormat)
117109
return formatName ? std::string(formatName) : "";
118110
}
119111

120-
NamesArray getFormatsNames()
112+
std::vector<AVOutputFormat*> getAvailableFormats()
121113
{
122-
NamesArray formatsNames;
114+
std::vector<AVOutputFormat*> formats;
123115

124116
AVOutputFormat* fmt = NULL;
125117
while((fmt = av_oformat_next(fmt)))
126118
{
127-
// skip undefined codec
128-
if(fmt->video_codec == AV_CODEC_ID_NONE)
119+
if(!fmt->name)
129120
continue;
130121

131-
if(!fmt->name && !fmt->long_name)
122+
formats.push_back(fmt);
123+
}
124+
return formats;
125+
}
126+
127+
NamesMap getAvailableFormatsNames()
128+
{
129+
NamesMap formatsNames;
130+
std::vector<AVOutputFormat*> formats = getAvailableFormats();
131+
for(size_t i = 0; i < formats.size(); ++i)
132+
{
133+
AVOutputFormat* fmt = formats.at(i);
134+
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
135+
}
136+
return formatsNames;
137+
}
138+
139+
NamesMap getAvailableVideoFormatsNames()
140+
{
141+
NamesMap formatsNames;
142+
std::vector<AVOutputFormat*> formats = getAvailableFormats();
143+
for(size_t i = 0; i < formats.size(); ++i)
144+
{
145+
AVOutputFormat* fmt = formats.at(i);
146+
// skip format which cannot handle video
147+
if(fmt->video_codec == AV_CODEC_ID_NONE)
132148
continue;
149+
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
150+
}
151+
return formatsNames;
152+
}
133153

134-
formatsNames.push_back(
135-
std::make_pair(std::string(fmt->name ? fmt->name : ""), std::string(fmt->long_name ? fmt->long_name : "")));
154+
NamesMap getAvailableAudioFormatsNames()
155+
{
156+
NamesMap formatsNames;
157+
std::vector<AVOutputFormat*> formats = getAvailableFormats();
158+
for(size_t i = 0; i < formats.size(); ++i)
159+
{
160+
AVOutputFormat* fmt = formats.at(i);
161+
// skip format which cannot handle audio
162+
if(fmt->audio_codec == AV_CODEC_ID_NONE)
163+
continue;
164+
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
136165
}
137166
return formatsNames;
138167
}
139168

140-
NamesArray getVideoCodecsNames()
169+
std::vector<AVCodec*> getAvailableCodecs()
141170
{
142-
NamesArray videoCodecsNames;
171+
std::vector<AVCodec*> codecs;
143172

144173
AVCodec* c = NULL;
145-
while((c = av_codec_next(c)) != NULL)
174+
while((c = av_codec_next(c)))
146175
{
147-
if(c->type == AVMEDIA_TYPE_VIDEO)
148-
{
149-
if(!c->name && !c->long_name)
150-
continue;
151-
152-
std::pair<std::string, std::string> codecNames(std::string(c->name ? c->name : ""),
153-
std::string(c->long_name ? c->long_name : ""));
176+
if(!c->name)
177+
continue;
154178

155-
// skip duplicates
156-
if(std::find(videoCodecsNames.begin(), videoCodecsNames.end(), codecNames) != videoCodecsNames.end())
157-
continue;
179+
codecs.push_back(c);
180+
}
181+
return codecs;
182+
}
158183

159-
videoCodecsNames.push_back(codecNames);
184+
NamesMap getAvailableVideoCodecsNames()
185+
{
186+
NamesMap videoCodecsNames;
187+
std::vector<AVCodec*> codecs = getAvailableCodecs();
188+
for(size_t i = 0; i < codecs.size(); ++i)
189+
{
190+
AVCodec* c = codecs.at(i);
191+
if(c->type == AVMEDIA_TYPE_VIDEO)
192+
{
193+
videoCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
160194
}
161195
}
162196
return videoCodecsNames;
163197
}
164198

165-
NamesArray getAudioCodecsNames()
199+
NamesMap getAvailableAudioCodecsNames()
166200
{
167-
NamesArray audioCodecsNames;
168-
169-
AVCodec* c = NULL;
170-
while((c = av_codec_next(c)) != NULL)
201+
NamesMap audioCodecsNames;
202+
std::vector<AVCodec*> codecs = getAvailableCodecs();
203+
for(size_t i = 0; i < codecs.size(); ++i)
171204
{
205+
AVCodec* c = codecs.at(i);
172206
if(c->type == AVMEDIA_TYPE_AUDIO)
173207
{
174-
if(!c->name && !c->long_name)
175-
continue;
176-
177-
std::pair<std::string, std::string> codecNames(std::string(c->name ? c->name : ""),
178-
std::string(c->long_name ? c->long_name : ""));
179-
180-
// skip duplicates
181-
if(std::find(audioCodecsNames.begin(), audioCodecsNames.end(), codecNames) != audioCodecsNames.end())
182-
continue;
183-
184-
audioCodecsNames.push_back(codecNames);
208+
audioCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
185209
}
186210
}
187211
return audioCodecsNames;
188212
}
189213

190-
OptionArrayMap getOutputFormatOptions()
214+
OptionArrayMap getAvailableOptionsPerOutputFormat()
191215
{
192216
OptionArrayMap optionsPerFormat;
193217

@@ -196,24 +220,22 @@ OptionArrayMap getOutputFormatOptions()
196220
// iterate on formats
197221
while(outputFormat)
198222
{
199-
// add only format with video track
200-
// outputFormat->audio_codec ?
201-
if(outputFormat->video_codec != AV_CODEC_ID_NONE)
223+
if(!outputFormat->name)
224+
continue;
225+
226+
const std::string outputFormatName(outputFormat->name);
227+
OptionArray options;
228+
if(outputFormat->priv_class)
202229
{
203-
if(outputFormat->priv_class)
204-
{
205-
const std::string outputFormatName(outputFormat->name);
206-
OptionArray options;
207-
loadOptions(options, (void*)&outputFormat->priv_class, 0);
208-
optionsPerFormat.insert(std::make_pair(outputFormatName, options));
209-
}
230+
loadOptions(options, (void*)&outputFormat->priv_class, 0);
210231
}
232+
optionsPerFormat.insert(std::make_pair(outputFormatName, options));
211233
outputFormat = av_oformat_next(outputFormat);
212234
}
213235
return optionsPerFormat;
214236
}
215237

216-
OptionArrayMap getVideoCodecOptions()
238+
OptionArrayMap getAvailableOptionsPerVideoCodec()
217239
{
218240
OptionArrayMap videoCodecOptions;
219241

@@ -222,23 +244,26 @@ OptionArrayMap getVideoCodecOptions()
222244
// iterate on codecs
223245
while(codec)
224246
{
247+
if(!codec->name)
248+
continue;
249+
225250
// add only video codec
226251
if(codec->type == AVMEDIA_TYPE_VIDEO)
227252
{
253+
const std::string videoCodecName(codec->name);
254+
OptionArray options;
228255
if(codec->priv_class)
229256
{
230-
std::string videoCodecName(codec->name);
231-
OptionArray options;
232257
loadOptions(options, (void*)&codec->priv_class, 0);
233-
videoCodecOptions.insert(std::make_pair(videoCodecName, options));
234258
}
259+
videoCodecOptions.insert(std::make_pair(videoCodecName, options));
235260
}
236261
codec = av_codec_next(codec);
237262
}
238263
return videoCodecOptions;
239264
}
240265

241-
OptionArrayMap getAudioCodecOptions()
266+
OptionArrayMap getAvailableOptionsPerAudioCodec()
242267
{
243268
OptionArrayMap audioCodecOptions;
244269

@@ -247,16 +272,19 @@ OptionArrayMap getAudioCodecOptions()
247272
// iterate on codecs
248273
while(codec)
249274
{
275+
if(!codec->name)
276+
continue;
277+
250278
// add only audio codec
251279
if(codec->type == AVMEDIA_TYPE_AUDIO)
252280
{
281+
const std::string audioCodecName(codec->name);
282+
OptionArray options;
253283
if(codec->priv_class)
254284
{
255-
std::string audioCodecName(codec->name);
256-
OptionArray options;
257285
loadOptions(options, (void*)&codec->priv_class, 0);
258-
audioCodecOptions.insert(std::make_pair(audioCodecName, options));
259286
}
287+
audioCodecOptions.insert(std::make_pair(audioCodecName, options));
260288
}
261289
codec = av_codec_next(codec);
262290
}

src/AvTranscoder/util.hpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "Option.hpp"
66

77
extern "C" {
8+
#include <libavcodec/avcodec.h>
9+
#include <libavformat/avformat.h>
810
#include <libavutil/pixfmt.h>
911
#include <libavutil/samplefmt.h>
1012
}
@@ -17,26 +19,21 @@ namespace avtranscoder
1719
{
1820

1921
typedef std::map<std::string, OptionArray> OptionArrayMap;
20-
typedef std::vector<std::pair<std::string, std::string> > NamesArray; //< short/long names of format/video codec/audio codec
21-
22-
/**
23-
* @brief Check if a format name corresponds to the format of a given filename
24-
*/
25-
bool AvExport matchFormat(const std::string& format, const std::string& filename);
22+
typedef std::map<std::string, std::string> NamesMap; //< short/long names of format/video codec/audio codec
2623

2724
/**
2825
* @brief Get pixel format supported by a video codec.
2926
* @param videoCodecName: the video codec name (empty if not indicated, and so get all pixel formats supported by all video
3027
* codecs).
3128
*/
32-
std::vector<std::string> AvExport getPixelFormats(const std::string& videoCodecName = "");
29+
std::vector<std::string> AvExport getSupportedPixelFormats(const std::string& videoCodecName = "");
3330

3431
/**
3532
* @brief Get sample format supported by an audio codec.
3633
* @param audioCodecName: the audio codec name (empty if not indicated, and so get all sample formats supported by all audio
3734
* codecs).
3835
*/
39-
std::vector<std::string> AvExport getSampleFormats(const std::string& audioCodecName = "");
36+
std::vector<std::string> AvExport getSupportedSampleFormats(const std::string& audioCodecName = "");
4037

4138
/**
4239
* @brief Get the corresponding AVPixelFormat from the pixel format name
@@ -64,34 +61,65 @@ std::string AvExport getSampleFormatName(const AVSampleFormat sampleFormat);
6461

6562
#ifndef SWIG
6663
/**
67-
* @brief Get array of short/long names of all format supported by FFmpeg / libav.
64+
* @return The list of all formats available in FFmpeg / libav.
65+
*/
66+
std::vector<AVOutputFormat*> getAvailableFormats();
67+
#endif
68+
/**
69+
* @brief Get a map of short/long names of all formats available in FFmpeg / libav.
70+
* @note Need to call preloadCodecsAndFormats before using this function.
71+
*/
72+
NamesMap AvExport getAvailableFormatsNames();
73+
74+
/**
75+
* @brief Get a map of short/long names of all formats dedicate for video available in FFmpeg / libav.
76+
* @note Need to call preloadCodecsAndFormats before using this function.
77+
*/
78+
NamesMap AvExport getAvailableVideoFormatsNames();
79+
80+
/**
81+
* @brief Get a map of short/long names of all formats dedicate for video available in FFmpeg / libav.
82+
* @note Need to call preloadCodecsAndFormats before using this function.
83+
*/
84+
NamesMap AvExport getAvailableAudioFormatsNames();
85+
86+
#ifndef SWIG
87+
/**
88+
* @return The list of all codecs available in FFmpeg / libav.
6889
*/
69-
NamesArray AvExport getFormatsNames();
90+
std::vector<AVCodec*> getAvailableCodecs();
91+
#endif
7092

7193
/**
72-
* @brief Get array of short/long names of all video codec supported by FFmpeg / libav.
94+
* @brief Get a map of short/long names of all video codecs available in FFmpeg / libav.
95+
* @note Need to call preloadCodecsAndFormats before using this function.
7396
*/
74-
NamesArray AvExport getVideoCodecsNames();
97+
NamesMap AvExport getAvailableVideoCodecsNames();
7598

7699
/**
77-
* @brief Get array of short/long names of all audio codec supported by FFmpeg / libav.
100+
* @brief Get a map of short/long names of all audio codecs available in FFmpeg / libav.
101+
* @note Need to call preloadCodecsAndFormats before using this function.
78102
*/
79-
NamesArray AvExport getAudioCodecsNames();
103+
NamesMap AvExport getAvailableAudioCodecsNames();
80104

105+
#ifndef SWIG
81106
/**
82107
* @brief Get the list of options for each output format
108+
* @note Need to call preloadCodecsAndFormats before using this function.
83109
*/
84-
OptionArrayMap AvExport getOutputFormatOptions();
110+
OptionArrayMap AvExport getAvailableOptionsPerOutputFormat();
85111

86112
/**
87113
* @brief Get the list of options for each video codec
114+
* @note Need to call preloadCodecsAndFormats before using this function.
88115
*/
89-
OptionArrayMap AvExport getVideoCodecOptions();
116+
OptionArrayMap AvExport getAvailableOptionsPerVideoCodec();
90117

91118
/**
92119
* @brief Get the list of options for each audio codec
120+
* @note Need to call preloadCodecsAndFormats before using this function.
93121
*/
94-
OptionArrayMap AvExport getAudioCodecOptions();
122+
OptionArrayMap AvExport getAvailableOptionsPerAudioCodec();
95123
#endif
96124
}
97125

0 commit comments

Comments
 (0)