AVTranscoder.cpp
9.9 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
#include "AVTranscoder.h"
CAVTranscoder::CAVTranscoder():
_start_time(INT64_MAX),
_all_processed(true),
_one2one(false),
_nOutputWidth(320)
{
if (_one2one) {
_nOutputHeight = 480;
}
else {
_nOutputHeight = 240;
}
}
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 += 50;
//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()
{
return 0;
}
int CAVTranscoder::open_output_file(const char *filename)
{
AVStream *out_stream;
AVCodecContext *dec_ctx, *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 = 1;
/* 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;
/* 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;
/* 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)
{
vector < CAVDecoder *>::iterator it = decoders_got_frame.begin();
for (; it != decoders_got_frame.end(); it++) {
(*it)->free_cur_a_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;
}
}
if (idxTeacher != -1) {
// the dest frame is the teacher frame
for (int i = 0; i < decoders_got_frame.size(); i++){
if (i != idxTeacher) {
//scale eacher frame
//copy each frame to the dest frame
}
}
// set the timestamp of teacher frame
//send to encoder
}
else {
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(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) {
idxTeacher = i;
break;
}
}
//fill the timestamp of dest frame
//send to encoder
}
return 0;
}
int CAVTranscoder::fillDestFrame(AVFrame * pDstFrame, AVFrame * pSrcFrame, int x, int y)
{
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]);
}
for (int i = 0; i < pSrcFrame->height / 2; i++){
memcpy(pDstFrame->data[1] + (y / 2)*pDstFrame->linesize[1] + x / 2, pSrcFrame->data[1] + i * pSrcFrame->linesize[1], pSrcFrame->linesize[1]);
memcpy(pDstFrame->data[2] + (y / 2)*pDstFrame->linesize[2] + x / 2, pSrcFrame->data[2] + i * 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);
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);
fillDestFrame(pDstFrame, decoders_got_frame[1]->_cur_v_frame, 0, decoders_got_frame[1]->_media_role == mr_teacher ? 0 : 240);
}
else {
fillDestFrame(pDstFrame, decoders_got_frame[0]->_cur_v_frame, 0, 0);
//todo: fill the bottom half image with pure color
}
//fill the timestamp of dest frame
//send to encoder
return 0;
}
int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
int ret;
int got_frame_local;
AVPacket enc_pkt;
#if 0
int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
(ifmt_ctx->streams[stream_index]->codec->codec_type ==
AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
if (!got_frame)
got_frame = &got_frame_local;
av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
/* 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);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* mux encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
#endif
return ret;
}
#if 0
static int 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;
}
#endif