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 const std::vector<const AVOutputFormatWrapper*>& GetOutputFormats() const;
120 const std::vector<const AVCodecWrapper*>& GetCodecs() const;
121
122 std::unique_ptr<AVFifoBufferWrapper> CreateFifoBuffer(int size) const;
123
124 template<typename T>
125 AVDataBuffer<T> CreateMemoryBuffer(int preallocatedSize) const
126 {
127 return AVDataBuffer<T>(preallocatedSize, T {}, AVAllocator<T>());
128 }
129
130private:
131 void FillCodecsList();
132 void FillOuptutFormatsList();
133
134 struct Private;
135 std::unique_ptr<Private> mPrivate;
136
137 std::vector<const AVCodecWrapper*> mCodecPointers;
138 std::vector<std::unique_ptr<AVCodecWrapper>> mCodecs;
139
140 std::vector<const AVOutputFormatWrapper*> mOutputFormatPointers;
141 std::vector<std::unique_ptr<AVOutputFormatWrapper>> mOutputFormats;
142};
143
144template<typename T>
147{
148 if (mFFmpeg)
149 return static_cast<pointer>(mFFmpeg->av_malloc(n * sizeof(T)));
150 else
151 return static_cast<pointer>(::malloc(n * sizeof(T)));
152}
153
154template<typename T>
156 typename AVAllocator<T>::pointer p,
157 typename AVAllocator<T>::size_type ) noexcept
158{
159 if (mFFmpeg)
160 mFFmpeg->av_free(p);
161 else
162 ::free(p);
163}
164
165template<typename T>
167 : mFFmpeg(FFmpegFunctions::Load())
168{
169}
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:363
THEME_RESOURCES_API void Load()
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