Repository navigation
Expand file tree
/
Copy pathffmodule.h
More file actions
261 lines (228 loc) · 6.62 KB
/
Copy pathffmodule.h
File metadata and controls
261 lines (228 loc) · 6.62 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#pragma once
#include <vector>
#include <string>
#define _CRT_SECURE_NO_WARNINGS
#ifdef __cplusplus
extern "C" {
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/channel_layout.h"
#include "libavutil/fifo.h"
#include "libavutil/opt.h"
#include "libavutil/error.h"
#include "libavutil/dict.h"
#include "libavutil/pixfmt.h"
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/timestamp.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext.h"
#include "libavutil/mathematics.h"
#include "libavutil/avutil.h"
#include "libavutil/frame.h"
#include "libavutil/mem.h"
#ifdef __cplusplus
}
#endif
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swscale.lib")
/**
Encoder.
*/
#define Encodec_libx264 "libx264" //H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264)
#define Encodec_h264_nvenc "h264_nvenc" //NVIDIA NVENC H.264 encoder (codec h264)
#define Encodec_h264_qsv "h264_qsv" //H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration) (codec h264)
#define Encodec_libx265 "libx265" //H.265 / HEVC (codec hevc)
#define Encodec_hevc_nvenc "hevc_nvenc" //NVIDIA NVENC hevc encoder (codec hevc)
#define Encodec_hevc_qsv "hevc_qsv" //HEVC (Intel Quick Sync Video acceleration) (codec hevc)
#define Encodec_prores "prores" //Apple ProRes
#define Encodec_prores_aw "prores_aw" //Apple ProRes (codec prores)
#define Encodec_prores_ks "prores_ks" //Apple ProRes (iCodec Pro) (codec prores)
static std::string string_format(const char* format, ...)
{
char buff[1024] = { 0 };
va_list args;
va_start(args, format);
vsprintf_s(buff, sizeof(buff), format, args);
va_end(args);
std::string str(buff);
return str;
}
/**
ReturnInfo struct.
For function return information.
*/
struct ReturnInfo
{
std::string ret_str = "";
int ret_code = 0;
};
/**
MediaInfo struct.
Include media duration,video and audio information.
*/
struct MediaInfo
{
int duration = 0; //Second
//Video
float video_bitrate = 0.0; //Kbit/s (Kbps)
float video_framerate = 0.0; //fps
long video_frames = 0;
int height = 0, width = 0;
std::string video_encodename;
//Audio
std::string audio_encodename;
int audio_channels = 0;
float audio_bitrate = 0.0; //Kbps
int sample_rate = 0; //KHz
ReturnInfo ret;
};
/**
Type for storing frames.
Must call free_frames() after using.
*/
typedef std::vector<AVFrame*> Frames;
/**
Free memory allocated in Frames.
*/
static int free_frames(Frames* src)
{
for (int a = 0; a < src->size(); a++)
av_frame_free(&(*src)[a]);
src->clear();
return 0;
}
/**
For transcode.
*/
typedef struct StreamContext {
AVCodecContext* dec_ctx;
AVCodecContext* enc_ctx;
} StreamContext;
class ffmodule
{
public:
ffmodule();
virtual ~ffmodule();
public:
/**
* Get media information.
*
* @param inputfile - Video file.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
MediaInfo GetMediaInfo(std::string inputfile);
/**
* Convert AVFrame to PNG file.
*
* @param pFrame - Pointer to user-supplied AVFrame.
* pFrame must be allocated by user before call this function.
*
* @param out_file - Path to save the PNG.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo Frame2PNG(AVFrame* pFrame, std::string out_file);
/**
* Separate video frames to PNG files.
*
* @param inputfile - Video file you want to separate.
*
* @param output - Path to save images.
*
* @param startframe - Start separating from here.
* Must be within the frame range.
*
* @param framecount - The frams you want to separate.
* Negative or 0 to separate all frames after startframe.
*
* @param decoder - Decoder.See ffpmeg AVHWDeviceType.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo SeparateFrames(std::string inputfile, std::string output, int startframe, int framecount, enum AVHWDeviceType decoder);//cuda dxva2 qsv d3d11va opencl vulkan
/**
* Separate video frames to memory.
*
* @param inputfile - Video file you want to separate.
*
* @param output - Pointer to store frames data.
*
* @param startframe - Start separating from here.
* Must be within the frame range.
*
* @param framecount - The frams you want to separate.
* Negative or 0 to separate all frames after startframe.
*
* @param decoder - Decoder.See ffpmeg AVHWDeviceType.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo SeparateFrames(std::string inputfile, Frames *output, int startframe, int framecount, enum AVHWDeviceType decoder);//cuda dxva2 qsv d3d11va opencl vulkan
/**
* Separate audio to media files.
*
* @param inputfile - Video file you want to separate.
*
* @param output - Path to save audio.
* File name force to "output_audio",the format is the same as the original format.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo SeparateAudio(std::string inputfile, std::string output);
/**
* Compose image trail to video.
*
* @param images_input - Image files path.
* Naming must be in the same format. (Such as image-00001.png, image-00002.png...)
*
* @param outfile - Path and file name to save media composed.
*
* @param codec - Video encoder.
* The availability of the encoder depends on your hardware platform.
*
* @param codec - Pixel format.
* The availability of the pixel format depends on the encoder you chose.
*
* @param bitrate - Video bitrate.
*
* @param framerate - Video frame rate.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo ComposeVideo(std::string images_input, std::string outfile, std::string codec, AVPixelFormat pix_fmt, int bitrate, int framerate);
/**
* Compose audio and video.
*
* @param video_input - Video file.
*
* @param audio_input - Audio file.
*
* @param output - Path and file name to save media composed.
*
* @return Code 0 on success, a negative value on failure, string return information.
*
*/
ReturnInfo ComposeAudio(std::string video_input, std::string audio_input, std::string output);
private:
};