AVTranscoder.cpp
13.6 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "AVTranscoder.h"
extern "C" {
#include <libswscale/swscale.h>
}
#ifdef WIN32
#pragma comment(lib,"swscale.lib")
#endif
#define SCALED_W 80
#define SCALED_H 60
#define SRC_W 320
#define SRC_H 240
CAVTranscoder::CAVTranscoder():
_start_time(INT64_MAX),
_all_processed(true),
_one2one(true),
_nOutputWidth(320),
_cur_out_v_ts(0),
_cur_out_a_ts(0),
_max_audio(1),
_swsCtx(NULL)
{
if (_one2one) {
_nOutputHeight = 480;
}
else {
_nOutputHeight = 240;
_swsCtx = sws_getContext(SRC_W, SRC_H, PIX_FMT_YUV420P,
SCALED_W, SCALED_H, PIX_FMT_YUV420P, SWS_BILINEAR,
NULL, NULL, NULL);
}
}
CAVTranscoder::~CAVTranscoder()
{
}
int CAVTranscoder::add(media_info & info)
{
_all_processed = false;
if (_start_time == INT64_MAX) {
_start_time = info.start_time_ms;
_cur_v_time = _start_time;
_cur_a_time = _start_time;
}
vector < CAVDecoder *>::iterator it = _decoders.begin();
for (; it != _decoders.end(); it++) {
if ((*it)->getuid() == info.uid){
(*it)->add(info);
break;
}
}
if (it == _decoders.end()) {
CAVDecoder * pVideoDecoder = new CAVDecoder();
pVideoDecoder->add(info);
_decoders.push_back(pVideoDecoder);
}
return 0;
}
int64_t CAVTranscoder::transcode()
{
vector<CAVDecoder *> decoders_got_frame;
vector <CAVDecoder *>::iterator it = _decoders.begin();
for (; it != _decoders.end();) {
if((*it)->get_one_v_frame()){
decoders_got_frame.push_back(*it);
}
else {
it = _decoders.erase(it);
continue;
}
it++;
}
_all_processed = decoders_got_frame.size() == 0;
mix_and_output_vframe(decoders_got_frame);
_cur_v_time += VFRAME_DURATION_MS;
//sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
while (_cur_a_time < _cur_v_time)
{
decoders_got_frame.clear();
vector < CAVDecoder *>::iterator it = _decoders.begin();
for (; it != _decoders.end();) {
if ((*it)->get_one_a_frame()){
decoders_got_frame.push_back(*it);
}
else {
it = _decoders.erase(it);
continue;
}
it++;
}
mix_and_output_aframe(decoders_got_frame);
_cur_a_time += AFRAME_DURATION_MS;
}
return _cur_v_time;
}
bool CAVTranscoder::all_processed()
{
return _all_processed;
}
int CAVTranscoder::close()
{
if (_swsCtx) {
sws_freeContext(_swsCtx);
_swsCtx = NULL;
}
flush_encoder(0);
flush_encoder(1);
av_write_trailer(_ofmt_ctx);
#if USE_H264BSF
av_bitstream_filter_close(h264bsfc);
#endif
#if USE_AACBSF
av_bitstream_filter_close(aacbsfc);
#endif
int i;
for (i = 0; i<2; i++)
{
if (_ofmt_ctx && _ofmt_ctx->nb_streams > i && _ofmt_ctx->streams[i] && _ofmt_ctx->streams[i]->codec)
avcodec_close(_ofmt_ctx->streams[i]->codec);
}
if (_ofmt_ctx && !(_ofmt_ctx->oformat->flags & AVFMT_NOFILE))
avio_close(_ofmt_ctx->pb);
avformat_free_context(_ofmt_ctx);
return 0;
}
int CAVTranscoder::open_output_file(const char *filename)
{
AVStream *out_stream;
AVCodecContext *enc_ctx;
AVCodec *encoder;
int ret;
unsigned int i;
_ofmt_ctx = NULL;
avformat_alloc_output_context2(&_ofmt_ctx, NULL, NULL, filename);
if (!_ofmt_ctx) {
av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
return AVERROR_UNKNOWN;
}
for (i = 0; i < 2; i++) {
out_stream = avformat_new_stream(_ofmt_ctx, NULL);
if (!out_stream) {
av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
return AVERROR_UNKNOWN;
}
enc_ctx = out_stream->codec;
if (_ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (0 == i) {
encoder = avcodec_find_encoder(AV_CODEC_ID_H264);;
if (!encoder) {
av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
return AVERROR_INVALIDDATA;
}
/* In this example, we transcode to same properties (picture size,
* sample rate etc.). These properties can be changed for output
* streams easily using filters */
enc_ctx->height = _nOutputHeight;
enc_ctx->width = _nOutputWidth;
enc_ctx->sample_aspect_ratio.den = 1;
enc_ctx->sample_aspect_ratio.num = 0;
/* take first format from list of supported formats */
enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
/* video time_base can be set to whatever is handy and supported by encoder */
enc_ctx->time_base.num = 1;
enc_ctx->time_base.den = 20;
enc_ctx->me_range = 16;
enc_ctx->max_qdiff = 4;
enc_ctx->qmin = 10;
enc_ctx->qmax = 30;
enc_ctx->qcompress = 0.6;
enc_ctx->framerate.den = 20;
enc_ctx->framerate.num = 1;
AVDictionary * d = NULL;
char *k = av_strdup("preset"); // if your strings are already allocated,
char *v = av_strdup("ultrafast"); // you can avoid copying them like this
av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
/* Third parameter can be used to pass settings to encoder */
ret = avcodec_open2(enc_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
return ret;
}
}
else {
encoder = avcodec_find_encoder(AV_CODEC_ID_AAC);;
if (!encoder) {
av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
return AVERROR_INVALIDDATA;
}
enc_ctx->sample_rate = 48000;
enc_ctx->channel_layout = AV_CH_LAYOUT_MONO;
enc_ctx->channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_MONO);
/* take first format from list of supported formats */
enc_ctx->sample_fmt = AV_SAMPLE_FMT_S16; //AV_SAMPLE_FMT_FLTP;
enc_ctx->time_base.num = 1;
enc_ctx->time_base.den = enc_ctx->sample_rate;
enc_ctx->bit_rate = 64000;
/* Third parameter can be used to pass settings to encoder */
ret = avcodec_open2(enc_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
return ret;
}
}
}
av_dump_format(_ofmt_ctx, 0, filename, 1);
if (!(_ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&_ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
return ret;
}
}
/* init muxer, write output file header */
ret = avformat_write_header(_ofmt_ctx, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
return ret;
}
return 0;
}
int CAVTranscoder::mix_and_output_vframe(vector<CAVDecoder *> & decoders_got_frame)
{
if (_one2one){
return mix_and_output_one2one_vframe(decoders_got_frame);
}
else {
return mix_and_output_one2many_vframe(decoders_got_frame);
}
}
int CAVTranscoder::mix_and_output_aframe(vector<CAVDecoder *> & decoders_got_frame)
{
AVFrame *pDstFrame = av_frame_alloc();
pDstFrame->nb_samples = 1024;
pDstFrame->channel_layout = AV_CH_LAYOUT_MONO;
pDstFrame->channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_MONO);
pDstFrame->format = AV_SAMPLE_FMT_S16;
pDstFrame->sample_rate = 48000;
av_frame_get_buffer(pDstFrame, 0);
av_samples_set_silence(pDstFrame->data, 0, 1024, pDstFrame->channels, (AVSampleFormat)pDstFrame->format);
vector < CAVDecoder *>::iterator it = decoders_got_frame.begin();
for (; it != decoders_got_frame.end(); it++) {
AVFrame * pFrame = (*it)->_cur_a_frame;
if (pFrame) {
int16_t * psrc = (int16_t *)pFrame->extended_data[0];
int16_t * pdst = (int16_t *)pDstFrame->extended_data[0];
for (int i = 0; i < 1024; i++,pdst++,psrc++) {
*pdst += (*psrc/_max_audio);
}
(*it)->free_cur_a_frame();
}
}
pDstFrame->pts = _cur_out_a_ts;
pDstFrame->pkt_dts = _cur_out_a_ts;
pDstFrame->pkt_pts = _cur_out_a_ts;
pDstFrame->pkt_duration = 1024;
_cur_out_a_ts += 1024;
int got_frame = 0;
encode_write_frame(pDstFrame, 1, &got_frame);
return 0;
}
int CAVTranscoder::mix_and_output_one2many_vframe(vector<CAVDecoder *> & decoders_got_frame)
{
int idxTeacher = -1;
for (int i = 0; i < decoders_got_frame.size(); i++){
if (decoders_got_frame[i]->_media_role == mr_teacher) {
idxTeacher = i;
break;
}
}
AVFrame *pDstFrame = av_frame_alloc();
int nDstSize = avpicture_get_size(AV_PIX_FMT_YUV420P, _nOutputWidth, _nOutputHeight);
uint8_t *dstbuf = new uint8_t[nDstSize];
avpicture_fill((AVPicture*)pDstFrame, dstbuf, AV_PIX_FMT_YUV420P, _nOutputWidth, _nOutputHeight);
if (idxTeacher != -1) {
//copy teacher frame to dest frame
CAVDecoder * pDecoder = decoders_got_frame[idxTeacher];
AVFrame * pFrame = pDecoder->_cur_v_frame;
if (pFrame) {
fillDestFrame(pDstFrame, pFrame, 0, 0);
}
else {//fill with pure color
memset(pDstFrame->data[0], 0, _nOutputWidth * _nOutputHeight);
memset(pDstFrame->data[1], 0x80, _nOutputWidth *_nOutputHeight / 4);
memset(pDstFrame->data[2], 0x80, _nOutputWidth * _nOutputHeight / 4);
}
for (int i = 0; i < decoders_got_frame.size(); i++){
if (i != idxTeacher) {
//scale eacher frame
//copy each frame to the dest frame
}
}
}
else {
memset(pDstFrame->data[0], 0, _nOutputWidth * _nOutputHeight);
memset(pDstFrame->data[1], 0x80, _nOutputWidth *_nOutputHeight / 4);
memset(pDstFrame->data[2], 0x80, _nOutputWidth * _nOutputHeight / 4);
for (int i = 0; i < decoders_got_frame.size(); i++){
if (i != idxTeacher) {
//scale eacher frame
//copy each frame to the dest frame
}
}
}
//fill the timestamp of dest frame
pDstFrame->pts = _cur_out_v_ts;
pDstFrame->pkt_dts = _cur_out_v_ts;
pDstFrame->pkt_pts = _cur_out_v_ts;
pDstFrame->format = AV_PIX_FMT_YUV420P;
pDstFrame->width = _nOutputWidth;
pDstFrame->height = _nOutputHeight;
_cur_out_v_ts++;
//send to encoder
int got_frame = 0;
encode_write_frame(pDstFrame, 0, &got_frame);
delete dstbuf;
return 0;
}
int CAVTranscoder::fillDestFrame(AVFrame * pDstFrame, AVFrame * pSrcFrame, int x, int y)
{
if (!pSrcFrame){
return 0;
}
for (int i = 0; i < pSrcFrame->height; i++) {
memcpy(pDstFrame->data[0] + (y + i)*pDstFrame->linesize[0] + x, pSrcFrame->data[0] + i * pSrcFrame->linesize[0], pSrcFrame->linesize[0]>0 ? pSrcFrame->linesize[0] : -pSrcFrame->linesize[0]);
}
for (int i = 0; i < pSrcFrame->height / 2; i++){
memcpy(pDstFrame->data[1] + (y / 2 + i)*pDstFrame->linesize[1] + x / 2, pSrcFrame->data[1] + i * pSrcFrame->linesize[1], pSrcFrame->linesize[1]>0 ? pSrcFrame->linesize[1] : -pSrcFrame->linesize[1]);
memcpy(pDstFrame->data[2] + (y / 2 + i)*pDstFrame->linesize[2] + x / 2, pSrcFrame->data[2] + i * pSrcFrame->linesize[2], pSrcFrame->linesize[2]>0 ? pSrcFrame->linesize[2] : -pSrcFrame->linesize[2]);
}
return 0;
}
int CAVTranscoder::mix_and_output_one2one_vframe(vector<CAVDecoder *> & decoders_got_frame)
{
//prepare one2one base frame
AVFrame *pDstFrame = av_frame_alloc();
int nDstSize = avpicture_get_size(AV_PIX_FMT_YUV420P,_nOutputWidth, _nOutputHeight);
uint8_t *dstbuf = new uint8_t[nDstSize];
avpicture_fill((AVPicture*)pDstFrame, dstbuf, AV_PIX_FMT_YUV420P, _nOutputWidth, _nOutputHeight);
memset(dstbuf, 0x80, nDstSize);
if (decoders_got_frame.size() == 2){
fillDestFrame(pDstFrame, decoders_got_frame[0]->_cur_v_frame, 0, decoders_got_frame[0]->_media_role == mr_teacher ? 0 : 240);
decoders_got_frame[0]->free_cur_v_frame();
fillDestFrame(pDstFrame, decoders_got_frame[1]->_cur_v_frame, 0, decoders_got_frame[1]->_media_role == mr_teacher ? 0 : 240);
decoders_got_frame[1]->free_cur_v_frame();
}
else if (decoders_got_frame.size() == 1)
{
fillDestFrame(pDstFrame, decoders_got_frame[0]->_cur_v_frame, 0, 0);
decoders_got_frame[0]->free_cur_v_frame();
//todo: fill the bottom half image with pure color
}
else {
//fill with last image?
}
//fill the timestamp of dest frame
pDstFrame->pts = _cur_out_v_ts;
pDstFrame->pkt_dts = _cur_out_v_ts;
pDstFrame->pkt_pts = _cur_out_v_ts;
pDstFrame->format = AV_PIX_FMT_YUV420P;
pDstFrame->width = _nOutputWidth;
pDstFrame->height = _nOutputHeight;
_cur_out_v_ts++;
//send to encoder
int got_frame = 0;
encode_write_frame(pDstFrame, 0, &got_frame);
delete dstbuf;
return 0;
}
int CAVTranscoder::encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
int ret;
int got_frame_local;
AVPacket enc_pkt;
int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
stream_index == 0 ? avcodec_encode_video2 : avcodec_encode_audio2;
if (!got_frame)
got_frame = &got_frame_local;
/* encode filtered frame */
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = enc_func(_ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
filt_frame, got_frame);
av_frame_free(&filt_frame);
if (ret < 0)
return ret;
if (!(*got_frame))
return 0;
/* prepare packet for muxing */
enc_pkt.stream_index = stream_index;
av_packet_rescale_ts(&enc_pkt,
_ofmt_ctx->streams[stream_index]->codec->time_base,
_ofmt_ctx->streams[stream_index]->time_base);
/* mux encoded frame */
ret = av_interleaved_write_frame(_ofmt_ctx, &enc_pkt);
return ret;
}
int CAVTranscoder::flush_encoder(unsigned int stream_index)
{
int ret;
int got_frame;
if (!(_ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
return 0;
while (1) {
av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
ret = encode_write_frame(NULL, stream_index, &got_frame);
if (ret < 0)
break;
if (!got_frame)
return 0;
}
return ret;
}
void CAVTranscoder::set_max_audio(int max_audio)
{
_max_audio = max_audio;
}