Skip to content

Commit f78e266

Browse files
author
Clement Champetier
committed
util: added getAvailableFormats/Codecs functions
* Call them in other functions. * Hide them from SWIG (unknown type).
1 parent 0f56476 commit f78e266

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

src/AvTranscoder/util.cpp

Lines changed: 43 additions & 30 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

@@ -111,16 +109,28 @@ std::string getSampleFormatName(const AVSampleFormat sampleFormat)
111109
return formatName ? std::string(formatName) : "";
112110
}
113111

114-
NamesMap getAvailableFormatsNames()
112+
std::vector<AVOutputFormat*> getAvailableFormats()
115113
{
116-
NamesMap formatsNames;
114+
std::vector<AVOutputFormat*> formats;
117115

118116
AVOutputFormat* fmt = NULL;
119117
while((fmt = av_oformat_next(fmt)))
120118
{
121119
if(!fmt->name)
122120
continue;
123121

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);
124134
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
125135
}
126136
return formatsNames;
@@ -129,16 +139,13 @@ NamesMap getAvailableFormatsNames()
129139
NamesMap getAvailableVideoFormatsNames()
130140
{
131141
NamesMap formatsNames;
132-
133-
AVOutputFormat* fmt = NULL;
134-
while((fmt = av_oformat_next(fmt)))
142+
std::vector<AVOutputFormat*> formats = getAvailableFormats();
143+
for(size_t i = 0; i < formats.size(); ++i)
135144
{
136-
if(!fmt->name)
137-
continue;
138-
145+
AVOutputFormat* fmt = formats.at(i);
146+
// skip format which cannot handle video
139147
if(fmt->video_codec == AV_CODEC_ID_NONE)
140148
continue;
141-
142149
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
143150
}
144151
return formatsNames;
@@ -147,33 +154,42 @@ NamesMap getAvailableVideoFormatsNames()
147154
NamesMap getAvailableAudioFormatsNames()
148155
{
149156
NamesMap formatsNames;
150-
151-
AVOutputFormat* fmt = NULL;
152-
while((fmt = av_oformat_next(fmt)))
157+
std::vector<AVOutputFormat*> formats = getAvailableFormats();
158+
for(size_t i = 0; i < formats.size(); ++i)
153159
{
154-
if(!fmt->name)
155-
continue;
156-
160+
AVOutputFormat* fmt = formats.at(i);
161+
// skip format which cannot handle audio
157162
if(fmt->audio_codec == AV_CODEC_ID_NONE)
158163
continue;
159-
160164
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
161165
}
162166
return formatsNames;
163167
}
164168

165-
NamesMap getAvailableVideoCodecsNames()
169+
std::vector<AVCodec*> getAvailableCodecs()
166170
{
167-
NamesMap videoCodecsNames;
171+
std::vector<AVCodec*> codecs;
168172

169173
AVCodec* c = NULL;
170-
while((c = av_codec_next(c)) != NULL)
174+
while((c = av_codec_next(c)))
175+
{
176+
if(!c->name)
177+
continue;
178+
179+
codecs.push_back(c);
180+
}
181+
return codecs;
182+
}
183+
184+
NamesMap getAvailableVideoCodecsNames()
185+
{
186+
NamesMap videoCodecsNames;
187+
std::vector<AVCodec*> codecs = getAvailableCodecs();
188+
for(size_t i = 0; i < codecs.size(); ++i)
171189
{
190+
AVCodec* c = codecs.at(i);
172191
if(c->type == AVMEDIA_TYPE_VIDEO)
173192
{
174-
if(!c->name)
175-
continue;
176-
177193
videoCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
178194
}
179195
}
@@ -183,15 +199,12 @@ NamesMap getAvailableVideoCodecsNames()
183199
NamesMap getAvailableAudioCodecsNames()
184200
{
185201
NamesMap audioCodecsNames;
186-
187-
AVCodec* c = NULL;
188-
while((c = av_codec_next(c)) != NULL)
202+
std::vector<AVCodec*> codecs = getAvailableCodecs();
203+
for(size_t i = 0; i < codecs.size(); ++i)
189204
{
205+
AVCodec* c = codecs.at(i);
190206
if(c->type == AVMEDIA_TYPE_AUDIO)
191207
{
192-
if(!c->name)
193-
continue;
194-
195208
audioCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
196209
}
197210
}

src/AvTranscoder/util.hpp

Lines changed: 15 additions & 0 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
}
@@ -57,6 +59,12 @@ std::string AvExport getPixelFormatName(const AVPixelFormat pixelFormat);
5759
*/
5860
std::string AvExport getSampleFormatName(const AVSampleFormat sampleFormat);
5961

62+
#ifndef SWIG
63+
/**
64+
* @return The list of all formats available by FFmpeg / libav.
65+
*/
66+
std::vector<AVOutputFormat*> getAvailableFormats();
67+
#endif
6068
/**
6169
* @brief Get a map of short/long names of all formats available by FFmpeg / libav.
6270
* @note Need to call preloadCodecsAndFormats before using this function.
@@ -75,6 +83,13 @@ NamesMap AvExport getAvailableVideoFormatsNames();
7583
*/
7684
NamesMap AvExport getAvailableAudioFormatsNames();
7785

86+
#ifndef SWIG
87+
/**
88+
* @return The list of all codecs available by FFmpeg / libav.
89+
*/
90+
std::vector<AVCodec*> getAvailableCodecs();
91+
#endif
92+
7893
/**
7994
* @brief Get a map of short/long names of all video codecs available by FFmpeg / libav.
8095
* @note Need to call preloadCodecsAndFormats before using this function.

0 commit comments

Comments
 (0)