Repository navigation
Expand file tree
/
Copy pathBasicEncoder.cpp
More file actions
325 lines (278 loc) · 12.3 KB
/
Copy pathBasicEncoder.cpp
File metadata and controls
325 lines (278 loc) · 12.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Parts of this code are derived from the muxing.c example for FFmpeg: https://raw.githubusercontent.com/FFmpeg/FFmpeg/master/doc/examples/muxing.c
#include "stdafx.h"
extern "C"
{
#include <libavutil/avassert.h>
#include <libavutil/channel_layout.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
}
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
#include "BasicEncoder.h"
#include "AVException.h"
class UnmanagedPrivates
{
public:
const char* pFilename = NULL;
AVFormatContext* pFormatContext = NULL;
AVCodec* pVideoCodec = NULL;
AVCodec* pAudioCodec = NULL;
AVStream* pVideoStream = NULL;
AVStream* pAudioStream = NULL;
AVCodecContext* pVideoContext = NULL;
AVCodecContext* pAudioContext = NULL;
private:
StringWrapper swFilename;
public:
UnmanagedPrivates(String^ filename) : swFilename(filename)
{
pFilename = swFilename.StrPtr;
}
~UnmanagedPrivates()
{
if (pVideoContext)
avcodec_free_context(&pVideoContext);
if (pAudioContext)
avcodec_free_context(&pAudioContext);
if (pFormatContext)
avformat_free_context(pFormatContext); // this also frees pVideoStream/pAudioStream
}
};
void BasicFFEncode::BasicEncoder::Initialise()
{
if (_initialised)
return;
av_log_set_level(AV_LOG_QUIET);
av_register_all();
_initialised = true;
}
void addStream(AVCodecID codecId, AVFormatContext* formatContext, AVCodec** codec, AVStream** stream, AVCodecContext** codecContext)
{
*codec = avcodec_find_encoder(codecId);
if (!(*codec))
throw gcnew AVException("Could not find specified encoder."); // avcodec_get_name(codecId) for more info
*codecContext = avcodec_alloc_context3(*codec);
if (!(*codecContext))
throw gcnew AVException("Could not create specified codec context.");
*stream = avformat_new_stream(formatContext, NULL);
if (!*stream)
throw gcnew AVException("Could not create stream.");
(*stream)->id = formatContext->nb_streams - 1;
if (formatContext->oformat->flags & AVFMT_GLOBALHEADER)
(*codecContext)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
void openCodec(AVCodec* codec, AVCodecContext* context, AVStream* stream, AVDictionary* opts)
{
const char* codecName = avcodec_get_name(codec->id);
String^ prettyName = gcnew String(codecName);
AVDictionary* optsCopy = NULL;
av_dict_copy(&optsCopy, opts, 0);
int result = avcodec_open2(context, codec, &optsCopy);
av_dict_free(&optsCopy);
if (result < 0)
throw gcnew AVException("Could not open codec " + prettyName, result);
result = avcodec_parameters_from_context(stream->codecpar, context);
if (result < 0)
throw gcnew AVException("Could not copy parameters from context", result);
}
BasicFFEncode::BasicEncoder::BasicEncoder(String^ filename, BasicEncoderSettings^ settings)
{
Initialise();
_filename = filename;
_priv = new UnmanagedPrivates(filename);
AVDictionary* opts = NULL;
if (settings->Options)
for each (auto kvp in settings->Options)
{
StringWrapper swKey(kvp.Key);
StringWrapper swValue(kvp.Value);
av_dict_set(&opts, swKey.StrPtr, swValue.StrPtr, 0); // this duplicates the strings, so they can be deallocated immediately
}
try
{
// Allocate a media context, while attempting to deduce the format from the file name, defaulting to MPEG if automatic deduction failed
int result = avformat_alloc_output_context2(&_priv->pFormatContext, NULL, NULL, _priv->pFilename);
if (!_priv->pFormatContext)
result = avformat_alloc_output_context2(&_priv->pFormatContext, NULL, "mpeg", _priv->pFilename);
if (!_priv->pFormatContext || result < 0)
throw gcnew AVException("Unable to allocate output context.", result);
AVCodecID videoCodecId = _priv->pFormatContext->oformat->video_codec;
AVCodecID audioCodecId = _priv->pFormatContext->oformat->audio_codec;
if (_priv->pFormatContext->oformat->flags & AVFMT_NOFILE)
throw gcnew AVException("Selected output format has AVFMT_NOFILE flag set. " +
"Please choose another output by changing the file name extension");
// Add the audio and video streams using the default format codecs and initialize the codecs.
bool hasVideo = videoCodecId != AV_CODEC_ID_NONE;
if (hasVideo)
addStream(videoCodecId, _priv->pFormatContext, &_priv->pVideoCodec, &_priv->pVideoStream, &_priv->pVideoContext);
bool hasAudio = audioCodecId != AV_CODEC_ID_NONE;
if (hasAudio)
addStream(audioCodecId, _priv->pFormatContext, &_priv->pAudioCodec, &_priv->pAudioStream, &_priv->pAudioContext);
// todo
/* if (codecCtx->codec_id == AV_CODEC_ID_H264) {
av_opt_set(codecCtx->priv_data, "preset", "slow", 0);
}*/
if (hasVideo)
{
// Configure video encoding
_priv->pVideoContext->codec_id = videoCodecId;
settings->Video->CodecId = static_cast<BasicVideoCodecId>(videoCodecId);
_priv->pVideoContext->bit_rate = settings->Video->Bitrate;
_priv->pVideoContext->width = settings->Video->Width;
_priv->pVideoContext->height = settings->Video->Height;
_priv->pVideoContext->time_base.num = settings->Video->Timebase.Num;
_priv->pVideoContext->time_base.den = settings->Video->Timebase.Den;
_priv->pVideoContext->pix_fmt = static_cast<AVPixelFormat>(settings->Video->PixelFormat);
_priv->pVideoContext->gop_size = settings->Video->GopSize;
_priv->pVideoContext->sample_aspect_ratio.num = settings->Video->PixelAspectRatio.Num;
_priv->pVideoContext->sample_aspect_ratio.den = settings->Video->PixelAspectRatio.Den;
_priv->pVideoStream->time_base = _priv->pVideoContext->time_base;
// The following settings are from the muxing.c sample and should not be required in general:
//if (_priv->pVideoContext->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
// _priv->pVideoContext->max_b_frames = 2;
//}
//if (_priv->pVideoContext->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
// _priv->pVideoContext->mb_decision = 2;
//}
// Open video codec
openCodec(_priv->pVideoCodec, _priv->pVideoContext, _priv->pVideoStream, opts);
}
if (hasAudio)
{
// Configure audio encoding
settings->Audio->CodecId = static_cast<BasicAudioCodecId>(audioCodecId);
_priv->pAudioContext->sample_fmt = _priv->pAudioCodec->sample_fmts ? _priv->pAudioCodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
_priv->pAudioContext->bit_rate = settings->Audio->Bitrate;
_priv->pAudioContext->sample_rate = settings->Audio->SampleRate;
if (_priv->pAudioCodec->supported_samplerates) {
_priv->pAudioContext->sample_rate = _priv->pAudioCodec->supported_samplerates[0];
for (int i = 0; _priv->pAudioCodec->supported_samplerates[i]; i++)
if (_priv->pAudioCodec->supported_samplerates[i] == settings->Audio->SampleRate)
_priv->pAudioContext->sample_rate = settings->Audio->SampleRate;
settings->Audio->SampleRate = _priv->pAudioContext->sample_rate;
}
//_priv->pAudioContext->channels = av_get_channel_layout_nb_channels(_priv->pAudioContext->channel_layout); // was in the sample - not needed here?
_priv->pAudioContext->channel_layout = AV_CH_LAYOUT_STEREO;
if (_priv->pAudioCodec->channel_layouts) {
_priv->pAudioContext->channel_layout = _priv->pAudioCodec->channel_layouts[0];
for (int i = 0; _priv->pAudioCodec->channel_layouts[i]; i++)
if (_priv->pAudioCodec->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
_priv->pAudioContext->channel_layout = AV_CH_LAYOUT_STEREO;
}
_priv->pAudioContext->channels = av_get_channel_layout_nb_channels(_priv->pAudioContext->channel_layout);
settings->Audio->ChannelLayout = static_cast<BasicChannelLayout>(_priv->pAudioContext->channel_layout);
settings->Audio->Channels = _priv->pAudioContext->channels;
_priv->pAudioStream->time_base.num = 1;
_priv->pAudioStream->time_base.den = _priv->pAudioContext->sample_rate;
// Open audio codec
openCodec(_priv->pAudioCodec, _priv->pAudioContext, _priv->pAudioStream, opts);
settings->Audio->FrameSize = _priv->pAudioContext->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE ? 0 : _priv->pAudioContext->frame_size;
}
// Open the output file
result = avio_open(&_priv->pFormatContext->pb, _priv->pFilename, AVIO_FLAG_WRITE);
if (result < 0)
throw gcnew AVException("Unable to open the output file.", result);
// Write the stream header
result = avformat_write_header(_priv->pFormatContext, &opts);
if (result < 0)
throw gcnew AVException("Unable to write stream header.", result);
}
catch (Exception^)
{
delete _priv;
throw;
}
}
void writePendingPackets(AVCodecContext* context, AVStream* stream, AVFormatContext* formatContext)
{
AVPacket packet = { 0 };
av_init_packet(&packet);
int result;
while ((result = avcodec_receive_packet(context, &packet)) == 0)
{
av_packet_rescale_ts(&packet, context->time_base, stream->time_base);
packet.stream_index = stream->index;
int frame_result = av_interleaved_write_frame(formatContext, &packet);
if (frame_result < 0)
throw gcnew AVException("Could not write frame.", frame_result);
}
if (result != AVERROR(EAGAIN) && result != AVERROR_EOF)
throw gcnew AVException("Error while receiving encoded packet.", result);
}
BasicFFEncode::BasicEncoder::~BasicEncoder()
{
try
{
int result;
if (_priv->pVideoContext)
{
result = avcodec_send_frame(_priv->pVideoContext, NULL);
if (result < 0)
throw gcnew AVException("Could not flush video encoder.", result);
writePendingPackets(_priv->pVideoContext, _priv->pVideoStream, _priv->pFormatContext);
}
if (_priv->pAudioContext)
{
result = avcodec_send_frame(_priv->pAudioContext, NULL);
if (result < 0)
throw gcnew AVException("Could not flush audio encoder.", result);
writePendingPackets(_priv->pAudioContext, _priv->pAudioStream, _priv->pFormatContext);
}
result = av_write_trailer(_priv->pFormatContext);
if (result < 0)
throw gcnew AVException("Could not write file trailer", result);
result = avio_closep(&_priv->pFormatContext->pb);
if (result < 0)
throw gcnew AVException("Could not close file handle", result);
}
finally
{
delete _priv;
}
}
void BasicFFEncode::BasicEncoder::EncodeFrame(BasicVideoFrame^ frame, Int64 presentationTimestamp)
{
frame->pFrame->pts = presentationTimestamp;
int result = avcodec_send_frame(_priv->pVideoContext, frame->pFrame);
if (result < 0)
throw gcnew AVException("Could not encode video frame.", result);
writePendingPackets(_priv->pVideoContext, _priv->pVideoStream, _priv->pFormatContext);
// The codec may have kept a reference to this frame, making it un-writable. Allocate new buffers in this case to ensure the caller can still write to this frame.
av_frame_make_writable(frame->pFrame);
}
void BasicFFEncode::BasicEncoder::EncodeFrame(BasicAudioFrame^ frame, Int64 presentationTimestamp)
{
frame->pFrame->pts = presentationTimestamp;
int result = avcodec_send_frame(_priv->pAudioContext, frame->pFrame);
if (result < 0)
throw gcnew AVException("Could not encode audio frame.", result);
writePendingPackets(_priv->pAudioContext, _priv->pAudioStream, _priv->pFormatContext);
// The codec may have kept a reference to this frame, making it un-writable. Allocate new buffers in this case to ensure the caller can still write to this frame.
av_frame_make_writable(frame->pFrame);
}
BasicFFEncode::BasicRescaler::BasicRescaler(int srcWidth, int srcHeight, BasicPixelFormat srcFormat, int destWidth, int destHeight, BasicPixelFormat destFormat, BasicRescalerFlags flags)
{
_pContext = sws_getContext(srcWidth, srcHeight, static_cast<AVPixelFormat>(srcFormat), destWidth, destHeight, static_cast<AVPixelFormat>(destFormat), (int)flags, NULL, NULL, NULL);
if (!_pContext)
throw gcnew AVException("Could not create rescaler context.");
}
BasicFFEncode::BasicRescaler::BasicRescaler(BasicVideoFrame^ srcFrameTemplate, BasicVideoFrame^ destFrameTemplate, BasicRescalerFlags flags)
: BasicRescaler(srcFrameTemplate->Width, srcFrameTemplate->Height, srcFrameTemplate->PixelFormat, destFrameTemplate->Width, destFrameTemplate->Height, destFrameTemplate->PixelFormat, flags)
{
}
BasicFFEncode::BasicRescaler::~BasicRescaler()
{
sws_freeContext(_pContext);
}
void BasicFFEncode::BasicRescaler::RescaleFrame(BasicVideoFrame^ src, BasicVideoFrame^ dest)
{
sws_scale(_pContext, (const uint8_t * const *)src->pFrame->data, src->pFrame->linesize, 0, src->Height, dest->pFrame->data, dest->pFrame->linesize);
}