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
|
/**********************************************************************
Audacity: A Digital Audio Editor
FFmpegFunctions.h
Dmitry Vedenko
**********************************************************************/
#pragma once
#include <memory>
#include <vector>
#include <wx/string.h>
#include "AVCodecFunctions.h"
#include "AVFormatFunctions.h"
#include "AVUtilFunctions.h"
#include "AVCodecID.h"
#include "wrappers/AVDictionaryWrapper.h"
#include "wrappers/AVIOContextWrapper.h"
#include "wrappers/AVFormatContextWrapper.h"
#include "wrappers/AVStreamWrapper.h"
#include "wrappers/AVPacketWrapper.h"
#include "wrappers/AVFrameWrapper.h"
#include "wrappers/AVInputFormatWrapper.h"
#include "wrappers/AVOutputFormatWrapper.h"
#include "wrappers/AVCodecWrapper.h"
#include "wrappers/AVCodecContextWrapper.h"
#include "wrappers/AVChannelLayoutWrapper.h"
class StringSetting;
extern FFMPEG_SUPPORT_API StringSetting AVFormatPath;
class FFmpegFunctions;
template <typename T>
class AVAllocator : public std::allocator<T>
{
public:
typedef size_t size_type;
typedef T* pointer;
typedef const T* const_pointer;
template <typename _Tp1> struct rebind
{
typedef AVAllocator<_Tp1> other;
};
pointer allocate(size_type n) noexcept;
void deallocate(pointer p, size_type ) noexcept;
AVAllocator();
AVAllocator(const AVAllocator& a)
: std::allocator<T>(a)
, mFFmpeg(a.mFFmpeg)
{
}
template <class U>
AVAllocator(const AVAllocator<U>& a)
: std::allocator<T>(a)
, mFFmpeg(a.mFFmpeg)
{
}
private:
template <class U> friend class AVAllocator;
std::shared_ptr<FFmpegFunctions> mFFmpeg;
};
template<typename T>
using AVDataBuffer = std::vector<T, AVAllocator<T>>;
struct FFMPEG_SUPPORT_API FFmpegFunctions :
AVCodecFunctions,
AVFormatFunctions,
AVUtilFunctions
{
FFmpegFunctions();
~FFmpegFunctions();
static std::shared_ptr<FFmpegFunctions> Load(bool fromUserPathOnly = false);
AVCodecIDFwd (*GetAVCodecID)(AudacityAVCodecID) = nullptr;
AudacityAVCodecID (*GetAudacityCodecID)(AVCodecIDFwd) = nullptr;
static std::vector<wxString> GetSearchPaths(bool fromUserPathOnly);
std::unique_ptr<AVIOContextWrapper> CreateAVIOContext() const;
std::unique_ptr<AVFormatContextWrapper> CreateAVFormatContext() const;
std::unique_ptr<AVStreamWrapper> CreateAVStreamWrapper(AVStream* stream, bool forEncoding) const;
//! @post return value is not null
std::unique_ptr<AVPacketWrapper> CreateAVPacketWrapper() const;
//! @post return value is not null
std::unique_ptr<AVFrameWrapper> CreateAVFrameWrapper() const;
std::unique_ptr<AVInputFormatWrapper> CreateAVInputFormatWrapper(AVInputFormat* inputFormat) const;
std::unique_ptr<AVOutputFormatWrapper> CreateAVOutputFormatWrapper(const AVOutputFormat* outputFormat) const;
std::unique_ptr<AVCodecWrapper> CreateDecoder(AVCodecIDFwd codecID) const;
std::unique_ptr<AVCodecWrapper> CreateEncoder(AVCodecIDFwd codecID) const;
std::unique_ptr<AVCodecWrapper> CreateEncoder(const char* codecName) const;
std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapper(AVCodecContext* context) const;
std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapperFromCodec(std::unique_ptr<AVCodecWrapper> codec) const;
std::unique_ptr<AVOutputFormatWrapper> GuessOutputFormat(const char* short_name, const char* filename, const char* mime_type);
std::unique_ptr<AVChannelLayoutWrapper> CreateDefaultChannelLayout(int channelsCount) const;
std::unique_ptr<AVChannelLayoutWrapper> CreateLegacyChannelLayout(uint64_t layout, int channelsCount) const;
std::unique_ptr<AVChannelLayoutWrapper> CreateAVChannelLayout(const AVChannelLayout* layout) const;
const std::vector<const AVOutputFormatWrapper*>& GetOutputFormats() const;
const std::vector<const AVCodecWrapper*>& GetCodecs() const;
template<typename T>
AVDataBuffer<T> CreateMemoryBuffer(int preallocatedSize) const
{
return AVDataBuffer<T>(preallocatedSize, T {}, AVAllocator<T>());
}
private:
void FillCodecsList();
void FillOuptutFormatsList();
struct Private;
std::unique_ptr<Private> mPrivate;
std::vector<const AVCodecWrapper*> mCodecPointers;
std::vector<std::unique_ptr<AVCodecWrapper>> mCodecs;
std::vector<const AVOutputFormatWrapper*> mOutputFormatPointers;
std::vector<std::unique_ptr<AVOutputFormatWrapper>> mOutputFormats;
};
template<typename T>
typename AVAllocator<T>::pointer
AVAllocator<T>::allocate(typename AVAllocator<T>::size_type n) noexcept
{
if (mFFmpeg)
return static_cast<pointer>(mFFmpeg->av_malloc(n * sizeof(T)));
else
return static_cast<pointer>(::malloc(n * sizeof(T)));
}
template<typename T>
void AVAllocator<T>::deallocate(
typename AVAllocator<T>::pointer p,
typename AVAllocator<T>::size_type ) noexcept
{
if (mFFmpeg)
mFFmpeg->av_free(p);
else
::free(p);
}
template<typename T>
AVAllocator<T>::AVAllocator()
: mFFmpeg(FFmpegFunctions::Load())
{
}
|