Skip to content

Commit 56f322e

Browse files
author
Clement Champetier
committed
util: get format/videoCodec/audioCodec names with a map
Instead of two functions for each case: short and long names.
1 parent b06a78f commit 56f322e

File tree

2 files changed

+24
-105
lines changed

2 files changed

+24
-105
lines changed

src/AvTranscoder/util.cpp

Lines changed: 17 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern "C" {
66
#include <libavutil/pixdesc.h>
77
}
88

9+
#include <utility>
10+
911
namespace avtranscoder
1012
{
1113

@@ -116,27 +118,9 @@ AVSampleFormat getAVSampleFormat( const std::string& sampleFormat )
116118
return av_get_sample_fmt( sampleFormat.c_str() );
117119
}
118120

119-
std::vector<std::string> getFormatsLongNames()
120-
{
121-
std::vector<std::string> formatsLongNames;
122-
123-
AVOutputFormat* fmt = NULL;
124-
while( ( fmt = av_oformat_next( fmt ) ) )
125-
{
126-
// skip undefined codec
127-
if( fmt->video_codec == AV_CODEC_ID_NONE )
128-
continue;
129-
130-
if( ! fmt->long_name )
131-
continue;
132-
133-
formatsLongNames.push_back( std::string( fmt->long_name ) );
134-
}
135-
return formatsLongNames;
136-
}
137-
std::vector<std::string> getFormatsShortNames()
121+
NamesArray getFormatsNames()
138122
{
139-
std::vector<std::string> formatsShortNames;
123+
NamesArray formatsNames;
140124

141125
AVOutputFormat* fmt = NULL;
142126
while( ( fmt = av_oformat_next( fmt ) ) )
@@ -145,47 +129,17 @@ std::vector<std::string> getFormatsShortNames()
145129
if( fmt->video_codec == AV_CODEC_ID_NONE )
146130
continue;
147131

148-
if( ! fmt->name )
132+
if( ! fmt->name && ! fmt->long_name )
149133
continue;
150134

151-
formatsShortNames.push_back( std::string( fmt->name ) );
135+
formatsNames.push_back( std::make_pair( std::string( fmt->name ? fmt->name : "" ), std::string( fmt->long_name ? fmt->long_name : "" ) ) );
152136
}
153-
return formatsShortNames;
137+
return formatsNames;
154138
}
155139

156-
std::vector<std::string> getVideoCodecsLongNames()
157-
{
158-
std::vector<std::string> videoCodecsLongNames;
159-
160-
AVCodec* c = NULL;
161-
while( ( c = av_codec_next( c ) ) != NULL )
162-
{
163-
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 53, 34, 0 )
164-
if( ! c->encode )
165-
continue;
166-
#else
167-
if( ! c->encode2 )
168-
continue;
169-
#endif
170-
switch( c->type )
171-
{
172-
case AVMEDIA_TYPE_VIDEO:
173-
{
174-
if( ! c->long_name )
175-
continue;
176-
177-
videoCodecsLongNames.push_back( std::string( c->long_name ) );
178-
break;
179-
}
180-
default:
181-
break;
182-
}
183-
}
184-
return videoCodecsLongNames;
185-
}
186-
std::vector<std::string> getVideoCodecsShortNames()
140+
NamesArray getVideoCodecsNames()
187141
{
188-
std::vector<std::string> videoCodecsShortNames;
142+
NamesArray videoCodecsNames;
189143

190144
AVCodec* c = NULL;
191145
while( ( c = av_codec_next( c ) ) != NULL )
@@ -201,52 +155,22 @@ std::vector<std::string> getVideoCodecsShortNames()
201155
{
202156
case AVMEDIA_TYPE_VIDEO:
203157
{
204-
if( ! c->name )
158+
if( ! c->name && ! c->long_name )
205159
continue;
206160

207-
videoCodecsShortNames.push_back( std::string( c->name ) );
161+
videoCodecsNames.push_back( std::make_pair( std::string( c->name ? c->name : "" ), std::string( c->long_name ? c->long_name : "" ) ) );
208162
break;
209163
}
210164
default:
211165
break;
212166
}
213167
}
214-
return videoCodecsShortNames;
168+
return videoCodecsNames;
215169
}
216170

217-
std::vector<std::string> getAudioCodecsLongNames()
218-
{
219-
std::vector<std::string> audioCodecsLongNames;
220-
221-
AVCodec* c = NULL;
222-
while( ( c = av_codec_next( c ) ) != NULL )
223-
{
224-
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 53, 34, 0 )
225-
if( ! c->encode )
226-
continue;
227-
#else
228-
if( ! c->encode2 )
229-
continue;
230-
#endif
231-
switch( c->type )
232-
{
233-
case AVMEDIA_TYPE_AUDIO:
234-
{
235-
if( ! c->long_name )
236-
continue;
237-
238-
audioCodecsLongNames.push_back( std::string( c->long_name ) );
239-
break;
240-
}
241-
default:
242-
break;
243-
}
244-
}
245-
return audioCodecsLongNames;
246-
}
247-
std::vector<std::string> getAudioCodecsShortNames()
171+
NamesArray getAudioCodecsNames()
248172
{
249-
std::vector<std::string> audioCodecsShortNames;
173+
NamesArray audioCodecsNames;
250174

251175
AVCodec* c = NULL;
252176
while( ( c = av_codec_next( c ) ) != NULL )
@@ -262,17 +186,17 @@ std::vector<std::string> getAudioCodecsShortNames()
262186
{
263187
case AVMEDIA_TYPE_AUDIO:
264188
{
265-
if( ! c->name )
189+
if( ! c->name && ! c->long_name )
266190
continue;
267191

268-
audioCodecsShortNames.push_back( std::string( c->name ) );
192+
audioCodecsNames.push_back( std::make_pair( std::string( c->name ? c->name : "" ), std::string( c->long_name ? c->long_name : "" ) ) );
269193
break;
270194
}
271195
default:
272196
break;
273197
}
274198
}
275-
return audioCodecsShortNames;
199+
return audioCodecsNames;
276200
}
277201

278202
OptionArrayMap getOutputFormatOptions()

src/AvTranscoder/util.hpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace avtranscoder
1717
{
1818

1919
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
2021

2122
/**
2223
* @brief Get format name from a given filename
@@ -53,25 +54,19 @@ AVPixelFormat getAVPixelFormat( const std::string& pixelFormat );
5354
AVSampleFormat getAVSampleFormat( const std::string& sampleFormat );
5455

5556
/**
56-
* @brief Get long name of all format supported by FFmpeg / libav.
57-
* @see getFormatsShortNames: to get short names
57+
* @brief Get array of short/long names of all format supported by FFmpeg / libav.
5858
*/
59-
std::vector<std::string> getFormatsLongNames();
60-
std::vector<std::string> getFormatsShortNames();
59+
NamesArray getFormatsNames();
6160

6261
/**
63-
* @brief Get long name of all video codec supported by FFmpeg / libav.
64-
* @see getVideoCodecsShortNames: to get short names
62+
* @brief Get array of short/long names of all video codec supported by FFmpeg / libav.
6563
*/
66-
std::vector<std::string> getVideoCodecsLongNames();
67-
std::vector<std::string> getVideoCodecsShortNames();
64+
NamesArray getVideoCodecsNames();
6865

6966
/**
70-
* @brief Get long name of all audio codec supported by FFmpeg / libav.
71-
* @see getAudioCodecsShortNames: to get short names
67+
* @brief Get array of short/long names of all audio codec supported by FFmpeg / libav.
7268
*/
73-
std::vector<std::string> getAudioCodecsLongNames();
74-
std::vector<std::string> getAudioCodecsShortNames();
69+
NamesArray getAudioCodecsNames();
7570

7671
/**
7772
* @brief Get the list of options for each output format

0 commit comments

Comments
 (0)