Audacity 3.2.0
FFmpegFunctions.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 FFmpegFunctions.h
6
7 Dmitry Vedenko
8
9**********************************************************************/
10
11#pragma once
12
13#include <memory>
14#include <vector>
15
16#include <wx/string.h>
17
18#include "AVCodecFunctions.h"
19#include "AVFormatFunctions.h"
20#include "AVUtilFunctions.h"
21#include "AVCodecID.h"
22
34
35class StringSetting;
36
37extern FFMPEG_SUPPORT_API StringSetting AVFormatPath;
38
39class FFmpegFunctions;
40template <typename T>
41class AVAllocator : public std::allocator<T>
42{
43public:
44 typedef size_t size_type;
45 typedef T* pointer;
46 typedef const T* const_pointer;
47
48 template <typename _Tp1> struct rebind
49 {
51 };
52
53 pointer allocate(size_type n) noexcept;
54
55 void deallocate(pointer p, size_type ) noexcept;
56
58
60 : std::allocator<T>(a)
61 , mFFmpeg(a.mFFmpeg)
62 {
63 }
64
65 template <class U>
67 : std::allocator<T>(a)
68 , mFFmpeg(a.mFFmpeg)
69 {
70 }
71
72private:
73 template <class U> friend class AVAllocator;
74
75 std::shared_ptr<FFmpegFunctions> mFFmpeg;
76};
77
78template<typename T>
79using AVDataBuffer = std::vector<T, AVAllocator<T>>;
80
81struct FFMPEG_SUPPORT_API FFmpegFunctions :
85{
88
89 static std::shared_ptr<FFmpegFunctions> Load(bool fromUserPathOnly = false);
90
91 AVCodecIDFwd (*GetAVCodecID)(AudacityAVCodecID) = nullptr;
92 AudacityAVCodecID (*GetAudacityCodecID)(AVCodecIDFwd) = nullptr;
93
94 static std::vector<wxString> GetSearchPaths(bool fromUserPathOnly);
95
96 std::unique_ptr<AVIOContextWrapper> CreateAVIOContext() const;
97 std::unique_ptr<AVFormatContextWrapper> CreateAVFormatContext() const;
98
99 std::unique_ptr<AVStreamWrapper> CreateAVStreamWrapper(AVStream* stream, bool forEncoding) const;
100
102 std::unique_ptr<AVPacketWrapper> CreateAVPacketWrapper() const;
103
105 std::unique_ptr<AVFrameWrapper> CreateAVFrameWrapper() const;
106
107 std::unique_ptr<AVInputFormatWrapper> CreateAVInputFormatWrapper(AVInputFormat* inputFormat) const;
108 std::unique_ptr<AVOutputFormatWrapper> CreateAVOutputFormatWrapper(const AVOutputFormat* outputFormat) const;
109
110 std::unique_ptr<AVCodecWrapper> CreateDecoder(AVCodecIDFwd codecID) const;
111 std::unique_ptr<AVCodecWrapper> CreateEncoder(AVCodecIDFwd codecID) const;
112 std::unique_ptr<AVCodecWrapper> CreateEncoder(const char* codecName) const;
113
114 std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapper(AVCodecContext* context) const;
115 std::unique_ptr<AVCodecContextWrapper> CreateAVCodecContextWrapperFromCodec(std::unique_ptr<AVCodecWrapper> codec) const;
116
117 std::unique_ptr<AVOutputFormatWrapper> GuessOutputFormat(const char* short_name, const char* filename, const char* mime_type);
118
119 std::unique_ptr<AVChannelLayoutWrapper> CreateDefaultChannelLayout(int channelsCount) const;
120 std::unique_ptr<AVChannelLayoutWrapper> CreateLegacyChannelLayout(uint64_t layout, int channelsCount) const;
121 std::unique_ptr<AVChannelLayoutWrapper> CreateAVChannelLayout(const AVChannelLayout* layout) const;
122
123 const std::vector<const AVOutputFormatWrapper*>& GetOutputFormats() const;
124 const std::vector<const AVCodecWrapper*>& GetCodecs() const;
125
126 template<typename T>
127 AVDataBuffer<T> CreateMemoryBuffer(int preallocatedSize) const
128 {
129 return AVDataBuffer<T>(preallocatedSize, T {}, AVAllocator<T>());
130 }
131
132private:
133 void FillCodecsList();
134 void FillOuptutFormatsList();
135
136 struct Private;
137 std::unique_ptr<Private> mPrivate;
138
139 std::vector<const AVCodecWrapper*> mCodecPointers;
140 std::vector<std::unique_ptr<AVCodecWrapper>> mCodecs;
141
142 std::vector<const AVOutputFormatWrapper*> mOutputFormatPointers;
143 std::vector<std::unique_ptr<AVOutputFormatWrapper>> mOutputFormats;
144};
145
146template<typename T>
149{
150 if (mFFmpeg)
151 return static_cast<pointer>(mFFmpeg->av_malloc(n * sizeof(T)));
152 else
153 return static_cast<pointer>(::malloc(n * sizeof(T)));
154}
155
156template<typename T>
158 typename AVAllocator<T>::pointer p,
159 typename AVAllocator<T>::size_type ) noexcept
160{
161 if (mFFmpeg)
162 mFFmpeg->av_free(p);
163 else
164 ::free(p);
165}
166
167template<typename T>
169 : mFFmpeg(FFmpegFunctions::Load())
170{
171}
int AVCodecIDFwd
Definition: AVCodecID.h:407
std::vector< T, AVAllocator< T > > AVDataBuffer
FFMPEG_SUPPORT_API StringSetting AVFormatPath
size_t size_type
void deallocate(pointer p, size_type) noexcept
AVAllocator(const AVAllocator &a)
AVAllocator(const AVAllocator< U > &a)
std::shared_ptr< FFmpegFunctions > mFFmpeg
const T * const_pointer
friend class AVAllocator
pointer allocate(size_type n) noexcept
Specialization of Setting for strings.
Definition: Prefs.h:370
THEME_RESOURCES_API void Load()
void free(void *ptr)
Definition: VectorOps.h:34
STL namespace.
AVAllocator< _Tp1 > other
AVDataBuffer< T > CreateMemoryBuffer(int preallocatedSize) const
std::vector< const AVOutputFormatWrapper * > mOutputFormatPointers
std::vector< const AVCodecWrapper * > mCodecPointers
std::vector< std::unique_ptr< AVCodecWrapper > > mCodecs
std::vector< std::unique_ptr< AVOutputFormatWrapper > > mOutputFormats
std::unique_ptr< Private > mPrivate