Skip to content

Commit

Permalink
Merge pull request #236 from avTranscoder/fix_utilFunctionsToGetListO…
Browse files Browse the repository at this point in the history
…fFormatsAndCodecs

Fix util functions to get list of formats and codecs
  • Loading branch information
valnoel committed Mar 10, 2016
2 parents 046cf9c + 6ce13be commit a138e77
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/AvTranscoder/file/OutputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void OutputFile::setupWrapping(const ProfileLoader::Profile& profile)
}

// check if output format indicated is valid with the filename extension
if(!matchFormat(profile.find(constants::avProfileFormat)->second, getFilename()))
if(!av_guess_format(profile.find(constants::avProfileFormat)->second.c_str(), getFilename().c_str(), NULL))
{
throw std::runtime_error("Invalid format according to the file extension.");
}
Expand Down
160 changes: 94 additions & 66 deletions src/AvTranscoder/util.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "util.hpp"

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixdesc.h>
}

Expand All @@ -12,13 +10,7 @@ extern "C" {
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> getSupportedPixelFormats(const std::string& videoCodecName)
{
std::vector<std::string> pixelFormats;

Expand Down Expand Up @@ -63,7 +55,7 @@ std::vector<std::string> getPixelFormats(const std::string& videoCodecName)
return pixelFormats;
}

std::vector<std::string> getSampleFormats(const std::string& audioCodecName)
std::vector<std::string> getSupportedSampleFormats(const std::string& audioCodecName)
{
std::vector<std::string> sampleFormats;

Expand Down Expand Up @@ -117,77 +109,109 @@ std::string getSampleFormatName(const AVSampleFormat sampleFormat)
return formatName ? std::string(formatName) : "";
}

NamesArray getFormatsNames()
std::vector<AVOutputFormat*> getAvailableFormats()
{
NamesArray formatsNames;
std::vector<AVOutputFormat*> formats;

AVOutputFormat* fmt = NULL;
while((fmt = av_oformat_next(fmt)))
{
// skip undefined codec
if(fmt->video_codec == AV_CODEC_ID_NONE)
if(!fmt->name)
continue;

if(!fmt->name && !fmt->long_name)
formats.push_back(fmt);
}
return formats;
}

NamesMap getAvailableFormatsNames()
{
NamesMap formatsNames;
std::vector<AVOutputFormat*> formats = getAvailableFormats();
for(size_t i = 0; i < formats.size(); ++i)
{
AVOutputFormat* fmt = formats.at(i);
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
}
return formatsNames;
}

NamesMap getAvailableVideoFormatsNames()
{
NamesMap formatsNames;
std::vector<AVOutputFormat*> formats = getAvailableFormats();
for(size_t i = 0; i < formats.size(); ++i)
{
AVOutputFormat* fmt = formats.at(i);
// skip format which cannot handle video
if(fmt->video_codec == AV_CODEC_ID_NONE)
continue;
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
}
return formatsNames;
}

formatsNames.push_back(
std::make_pair(std::string(fmt->name ? fmt->name : ""), std::string(fmt->long_name ? fmt->long_name : "")));
NamesMap getAvailableAudioFormatsNames()
{
NamesMap formatsNames;
std::vector<AVOutputFormat*> formats = getAvailableFormats();
for(size_t i = 0; i < formats.size(); ++i)
{
AVOutputFormat* fmt = formats.at(i);
// skip format which cannot handle audio
if(fmt->audio_codec == AV_CODEC_ID_NONE)
continue;
formatsNames.insert(std::make_pair(std::string(fmt->name), std::string(fmt->long_name ? fmt->long_name : "")));
}
return formatsNames;
}

NamesArray getVideoCodecsNames()
std::vector<AVCodec*> getAvailableCodecs()
{
NamesArray videoCodecsNames;
std::vector<AVCodec*> codecs;

AVCodec* c = NULL;
while((c = av_codec_next(c)) != NULL)
while((c = av_codec_next(c)))
{
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 : ""));
if(!c->name)
continue;

// skip duplicates
if(std::find(videoCodecsNames.begin(), videoCodecsNames.end(), codecNames) != videoCodecsNames.end())
continue;
codecs.push_back(c);
}
return codecs;
}

videoCodecsNames.push_back(codecNames);
NamesMap getAvailableVideoCodecsNames()
{
NamesMap videoCodecsNames;
std::vector<AVCodec*> codecs = getAvailableCodecs();
for(size_t i = 0; i < codecs.size(); ++i)
{
AVCodec* c = codecs.at(i);
if(c->type == AVMEDIA_TYPE_VIDEO)
{
videoCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
}
}
return videoCodecsNames;
}

NamesArray getAudioCodecsNames()
NamesMap getAvailableAudioCodecsNames()
{
NamesArray audioCodecsNames;

AVCodec* c = NULL;
while((c = av_codec_next(c)) != NULL)
NamesMap audioCodecsNames;
std::vector<AVCodec*> codecs = getAvailableCodecs();
for(size_t i = 0; i < codecs.size(); ++i)
{
AVCodec* c = codecs.at(i);
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);
audioCodecsNames.insert(std::make_pair(std::string(c->name), std::string(c->long_name ? c->long_name : "")));
}
}
return audioCodecsNames;
}

OptionArrayMap getOutputFormatOptions()
OptionArrayMap getAvailableOptionsPerOutputFormat()
{
OptionArrayMap optionsPerFormat;

Expand All @@ -196,24 +220,22 @@ OptionArrayMap getOutputFormatOptions()
// iterate on formats
while(outputFormat)
{
// add only format with video track
// outputFormat->audio_codec ?
if(outputFormat->video_codec != AV_CODEC_ID_NONE)
if(!outputFormat->name)
continue;

const std::string outputFormatName(outputFormat->name);
OptionArray options;
if(outputFormat->priv_class)
{
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));
}
loadOptions(options, (void*)&outputFormat->priv_class, 0);
}
optionsPerFormat.insert(std::make_pair(outputFormatName, options));
outputFormat = av_oformat_next(outputFormat);
}
return optionsPerFormat;
}

OptionArrayMap getVideoCodecOptions()
OptionArrayMap getAvailableOptionsPerVideoCodec()
{
OptionArrayMap videoCodecOptions;

Expand All @@ -222,23 +244,26 @@ OptionArrayMap getVideoCodecOptions()
// iterate on codecs
while(codec)
{
if(!codec->name)
continue;

// add only video codec
if(codec->type == AVMEDIA_TYPE_VIDEO)
{
const std::string videoCodecName(codec->name);
OptionArray options;
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));
}
videoCodecOptions.insert(std::make_pair(videoCodecName, options));
}
codec = av_codec_next(codec);
}
return videoCodecOptions;
}

OptionArrayMap getAudioCodecOptions()
OptionArrayMap getAvailableOptionsPerAudioCodec()
{
OptionArrayMap audioCodecOptions;

Expand All @@ -247,16 +272,19 @@ OptionArrayMap getAudioCodecOptions()
// iterate on codecs
while(codec)
{
if(!codec->name)
continue;

// add only audio codec
if(codec->type == AVMEDIA_TYPE_AUDIO)
{
const std::string audioCodecName(codec->name);
OptionArray options;
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));
}
audioCodecOptions.insert(std::make_pair(audioCodecName, options));
}
codec = av_codec_next(codec);
}
Expand Down
62 changes: 45 additions & 17 deletions src/AvTranscoder/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Option.hpp"

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixfmt.h>
#include <libavutil/samplefmt.h>
}
Expand All @@ -17,26 +19,21 @@ namespace avtranscoder
{

typedef std::map<std::string, OptionArray> OptionArrayMap;
typedef std::vector<std::pair<std::string, std::string> > NamesArray; //< short/long names of format/video codec/audio codec

/**
* @brief Check if a format name corresponds to the format of a given filename
*/
bool AvExport matchFormat(const std::string& format, const std::string& filename);
typedef std::map<std::string, std::string> NamesMap; //< short/long names of format/video codec/audio codec

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

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

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

#ifndef SWIG
/**
* @brief Get array of short/long names of all format supported by FFmpeg / libav.
* @return The list of all formats available in FFmpeg / libav.
*/
std::vector<AVOutputFormat*> getAvailableFormats();
#endif
/**
* @brief Get a map of short/long names of all formats available in FFmpeg / libav.
* @note Need to call preloadCodecsAndFormats before using this function.
*/
NamesMap AvExport getAvailableFormatsNames();

/**
* @brief Get a map of short/long names of all formats dedicate for video available in FFmpeg / libav.
* @note Need to call preloadCodecsAndFormats before using this function.
*/
NamesMap AvExport getAvailableVideoFormatsNames();

/**
* @brief Get a map of short/long names of all formats dedicate for video available in FFmpeg / libav.
* @note Need to call preloadCodecsAndFormats before using this function.
*/
NamesMap AvExport getAvailableAudioFormatsNames();

#ifndef SWIG
/**
* @return The list of all codecs available in FFmpeg / libav.
*/
NamesArray AvExport getFormatsNames();
std::vector<AVCodec*> getAvailableCodecs();
#endif

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

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

#ifndef SWIG
/**
* @brief Get the list of options for each output format
* @note Need to call preloadCodecsAndFormats before using this function.
*/
OptionArrayMap AvExport getOutputFormatOptions();
OptionArrayMap AvExport getAvailableOptionsPerOutputFormat();

/**
* @brief Get the list of options for each video codec
* @note Need to call preloadCodecsAndFormats before using this function.
*/
OptionArrayMap AvExport getVideoCodecOptions();
OptionArrayMap AvExport getAvailableOptionsPerVideoCodec();

/**
* @brief Get the list of options for each audio codec
* @note Need to call preloadCodecsAndFormats before using this function.
*/
OptionArrayMap AvExport getAudioCodecOptions();
OptionArrayMap AvExport getAvailableOptionsPerAudioCodec();
#endif
}

Expand Down

0 comments on commit a138e77

Please sign in to comment.