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
|
#ifndef libavwrapper_H
#define libavwrapper_H
#ifndef UINT64_C
#define UINT64_C(c) (c ## ULL)
#endif
#include "Poco/Mutex.h"
#include "Poco/ScopedLock.h"
#include "Poco/StringTokenizer.h"
#include "Poco/File.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/pixfmt.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>
}
#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#include <ffms.h>
namespace libav {
static bool b_is_one_time_inited=false;
// Some libavcodec calls are not reentrant
void maybeInitFFMpegLib();
static int sws_flags = SWS_BICUBIC;
class video_decoder
{
public:
video_decoder(){
maybeInitFFMpegLib();
pixfmts[0] = FFMS_GetPixFmt("rgb24");
pixfmts[1] = -1;
h=0;
w=0;
source=NULL;
loaded=false;
err.Buffer = errmsg;
err.BufferSize = sizeof(errmsg);
err.ErrorType = FFMS_ERROR_SUCCESS;
err.SubType = FFMS_ERROR_SUCCESS;
}
~video_decoder(){
cleanup();
}
void cleanup();
bool open(const std::string& filename);
float get_framerate(){
if (loaded) return (((float)props->FPSNumerator)/((float)props->FPSDenominator));
else return -1.0f;
}
int get_number_frames(){
if (loaded) return props->NumFrames;
else return -1;
}
int get_number_channels(){
return 3; //this is what we convert to
}
int get_width(){
return w;
}
int get_height(){
return h;
}
bool fetch_frame(int width,int height,int wanted){
if (FFMS_SetOutputFormatV2(source, pixfmts, width, height, FFMS_RESIZER_BICUBIC, &err)) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
return false;
}
frame = FFMS_GetFrame(source, wanted%props->NumFrames, &err);
if (frame == NULL) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
return false;
}
return true;
}
FFMS_VideoSource *source;
FFMS_VideoProperties *props;
FFMS_Frame *frame;
FFMS_ErrorInfo err;
char errmsg[1024];
int pixfmts[2];
bool loaded;
int h,w;
};
class audio_decoder
{
public:
audio_decoder(){
maybeInitFFMpegLib();
source=nullptr;
props=nullptr;
loaded=false;
err.Buffer = errmsg;
err.BufferSize = sizeof(errmsg);
err.ErrorType = FFMS_ERROR_SUCCESS;
err.SubType = FFMS_ERROR_SUCCESS;
}
~audio_decoder(){
cleanup();
}
void cleanup();
bool open(const std::string& filename);
int get_format(){
if (props) return props->SampleFormat;
else return 0;
}
int get_sample_rate(){
if (props) return props->SampleRate;
else return 0;
}
int get_bit_depth(){
if (props) return props->BitsPerSample;
else return 0;
}
int get_number_channels(){
if (props) return props->Channels;
else return 0;
}
int get_number_samples(){
if (props) return props->NumSamples;
else return 0;
}
int64_t get_channel_layout(){
if (props) return props->ChannelLayout;
else return 0;
}
float get_duration(){
if (props) return ((float)props->NumSamples)/props->SampleRate;
else return 0;
}
bool get_samples(void *buf,int64_t start, int64_t count){
if (source) {
if (FFMS_GetAudio(source, buf, start, count, &err)) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
return false;
}
return true;
}
return false;
}
FFMS_AudioSource *source;
FFMS_AudioProperties *props;
FFMS_Frame *frame;
FFMS_ErrorInfo err;
char errmsg[1024];
bool loaded;
};
class exporter {
public:
exporter(){
sws_ctx = NULL;
fragmentation=false;
}
virtual ~exporter(){
if (NULL != sws_ctx) {
sws_freeContext(sws_ctx);
sws_ctx = NULL;
}
};
bool setup(int w,int h, int bitRate, int frameRate, std::string container, bool _fragmentation);
bool record(std::string filename);
bool encodeFrame(unsigned char *pixels, uint16_t *samples);
bool encodeFrame(unsigned char *pixels,AVPacket *audiopkt); //is possible to just copy the packets?
bool encodeFrame(unsigned char *pixels,bool keyframe=false);
bool encodeFrame(uint16_t *samples);
void finishRecord();
int get_audio_framesize(){return audioframesize;};
float get_audio_step(){return audiostep;};
AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,enum AVCodecID codec_id); //AVCodecID
bool open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st);
int open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st);
void write_audio_frame(AVFormatContext *oc, AVStream *st,uint16_t *samples);
void write_audio_frame(AVFormatContext *oc, AVStream *st,AVPacket *pkt);
void close_audio(AVFormatContext *oc, AVStream *st);
void write_video_frame(AVFormatContext *oc, AVStream *st, uint8_t *pixels, bool keyframe=false);
void close_video(AVFormatContext *oc, AVStream *st);
private:
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *audio_st, *video_st;
AVCodec *audio_codec, *video_codec;
double audio_pts, video_pts;
struct SwsContext *sws_ctx;
int audioframesize;
float audiostep;
int w;
int h;
int bitRate;
int frameRate;
std::string container;
int outputframe;
// video output //
AVFrame *frame;
AVPicture src_picture, dst_picture;
int frame_count;
uint8_t *outPixels;
bool fragmentation;
//************************************************************//
// audio output //
float t, tincr, tincr2;
int audio_input_frame_size;
};
}
#endif // libavwrapper_H
|