AVTranscoder.cpp 24.5 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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
#include "AVTranscoder.h"
#include "tools.h"

extern "C" {
#include <libswscale/swscale.h> 
}
#ifdef WIN32
#pragma comment(lib,"swscale.lib")
#endif

#define SCALED_W 100
#define SCALED_H 75
#define SRC_W 320
#define SRC_H 240
uint8_t blank_r = 0x16;
uint8_t blank_g = 0x5a;
uint8_t blank_b = 0x82;

CAVTranscoder::CAVTranscoder(bool bOne2One):
_start_time(INT64_MAX),
_all_processed(true),
_nOutputWidth(320),
_cur_out_v_ts(0),
_cur_out_a_ts(0),
_max_audio(1),
_swsCtx(NULL),
_swsCtxPortrait(NULL),
_scaledFrame(NULL),
_scaledPortraitFrame(NULL),
_last_videos_got(-1),
_teacherFrame(NULL),
_studentFrame(NULL)
{
	_one2one = bOne2One;
	if (_one2one) {
		_nOutputHeight = 480;
	}
	else {
		_nOutputHeight = 240;
		_swsCtx = sws_getContext(SRC_W, SRC_H, AV_PIX_FMT_YUV420P,
			SCALED_W, SCALED_H, AV_PIX_FMT_YUV420P, SWS_BILINEAR,
			NULL, NULL, NULL);
		_swsCtxPortrait = sws_getContext(SRC_H, SRC_W, AV_PIX_FMT_YUV420P,
			SCALED_H, SCALED_W, AV_PIX_FMT_YUV420P, SWS_BILINEAR,
			NULL, NULL, NULL);
		_scaledFrame = av_frame_alloc();
		_scaledFrame->format = AV_PIX_FMT_YUV420P;
		_scaledFrame->width = SCALED_W;
		_scaledFrame->height = SCALED_H;
		_scaledFrame->pts = 0;

		_scaledPortraitFrame = av_frame_alloc();
		_scaledPortraitFrame->format = AV_PIX_FMT_YUV420P;
		_scaledPortraitFrame->width = SCALED_H;
		_scaledPortraitFrame->height = SCALED_W;
		_scaledPortraitFrame->pts = 0;

		int ret = av_frame_get_buffer(_scaledFrame, 32);
		if (ret != 0)
		{
			printf("Error alloc frame buffer for scaling video frame");
		}
		ret = av_frame_get_buffer(_scaledPortraitFrame, 32);
		if (ret != 0)
		{
			printf("Error alloc frame buffer for scaling portrait video frame");
		}
	}
	RGB2YUV(blank_r, blank_g, blank_b, &_blank_y, &_blank_u, &_blank_v);
}


CAVTranscoder::~CAVTranscoder()
{
	av_frame_free(&_teacherFrame);
	av_frame_free(&_studentFrame);
}

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 = (double)_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;
	
	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()
{
	flush_encoder(0);
	flush_encoder(1);
	av_write_trailer(_ofmt_ctx);

	if (_swsCtx) {
		sws_freeContext(_swsCtx);
		_swsCtx = NULL;
	}

	if (_swsCtxPortrait){
		sws_freeContext(_swsCtxPortrait);
		_swsCtxPortrait = NULL;
	}
	if (_scaledFrame) {
		av_frame_free(&_scaledFrame);
	}
	if (_scaledPortraitFrame) {
		av_frame_free(&_scaledPortraitFrame);
	}

#if USE_H264BSF  
	av_bitstream_filter_close(h264bsfc);
#endif  
#if USE_AACBSF  
	av_bitstream_filter_close(aacbsfc);
#endif  
	unsigned 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.6f;
				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 = PCM_FORMAT_FOR_AAC_ENCODE; //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 audio 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 = PCM_FORMAT_FOR_AAC_ENCODE;
		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) {
#ifdef PCM_USING_FLTP_FOR_AAC_ENCODE
				int16_t * psrc = (int16_t *)pFrame->extended_data[0];
				int16_t * pdst = (int16_t *)pDstFrame->extended_data[0];
#else
				float * psrc = (float *)pFrame->extended_data[0];
				float * pdst = (float *)pDstFrame->extended_data[0];
#endif
				for (int i = 0; i < 1024; i++,pdst++,psrc++) {
					*pdst += (*psrc/_max_audio);
				}
			}
		}


		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 (unsigned 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();
		pDstFrame->format = AV_PIX_FMT_YUV420P;
		pDstFrame->width = _nOutputWidth;
		pDstFrame->height = _nOutputHeight;
		int ret = av_frame_get_buffer(pDstFrame, 32);
		if (ret < 0) {
			printf("\n error av_frame_get_buffer: %d", ret);
			return -1;
		}

		if (idxTeacher != -1) {
			//copy teacher frame to dest frame
			CAVDecoder * pDecoder = decoders_got_frame[idxTeacher];
			AVFrame * pFrame = pDecoder->_cur_v_frame;
			if (pFrame) {
				av_frame_free(&_teacherFrame);
				_teacherFrame = pFrame;
				pDecoder->_cur_v_frame = NULL;
			}
		}
		if (_teacherFrame) {
			fillDestFrame(pDstFrame, _teacherFrame, 0, 0);
		}
		else
		{//fill with pure color
			memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight);
			memset(pDstFrame->data[1], _blank_u, _nOutputWidth *_nOutputHeight / 4);
			memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 4);
		}

		int imageIdx = 0;
		for (unsigned int i = 0; i < decoders_got_frame.size(); i++){
			if (i != idxTeacher) {
				//scale eacher frame
				CAVDecoder * pDecoder = decoders_got_frame[i];
				AVFrame * pFrame = pDecoder->_cur_v_frame;
				if (!pFrame) {
					continue;
				}
				int h = 0;
				if (pFrame->width == SRC_W && pFrame->height == SRC_H) {
					h = sws_scale(_swsCtx, pFrame->data, pFrame->linesize, 0, pFrame->height,
						_scaledFrame->data, _scaledFrame->linesize);
					if (h <= 0){
						printf("\nscale output result:%d?,ignored", h);
						continue;
					}
					_scaledFrame->pkt_dts = pFrame->pkt_dts;//pass rotation
					fillDestFrame(pDstFrame, _scaledFrame, SRC_W - (imageIdx % 4 + 1) * (SCALED_H + 5) + 4, SRC_H - SCALED_H + 3 - (SCALED_H + 4)*(imageIdx / 4), (SCALED_W - SCALED_H) / 2, 0, SCALED_H - 3, SCALED_H - 3);
				}
				else if (pFrame->width == SRC_H && pFrame->height == SRC_W) {
					h = sws_scale(_swsCtxPortrait, pFrame->data, pFrame->linesize, 0, pFrame->height,
						_scaledPortraitFrame->data, _scaledPortraitFrame->linesize);
					if (h <= 0){
						printf("\nscale output result:%d?,ignored", h);
						continue;
					}
					_scaledPortraitFrame->pkt_dts = pFrame->pkt_dts;//pass rotation
					fillDestFrame(pDstFrame, _scaledPortraitFrame, SRC_W - (imageIdx % 4 + 1) * (SCALED_H + 5) + 4, SRC_H - SCALED_H + 3 - (SCALED_H + 4)*(imageIdx / 4), 0, (SCALED_W - SCALED_H) / 2, SCALED_H - 3, SCALED_H - 3);

				}
				else {
					printf("\nthe frame resolution %dx%d is unexpected! ignored!", pFrame->width, pFrame->height);
					continue;
				}

				imageIdx++;
			}
		}

		//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);

		return 0;
	}

	int CAVTranscoder::fillDestFrame(AVFrame * pDstFrame, AVFrame * pSrcFrame, int x, int y)
	{
		if (!pSrcFrame){
			return 0;
		}

		if (pSrcFrame->pkt_dts == 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]);
			}
		}
		else if (pSrcFrame->pkt_dts == 180) {
			unsigned char * startSrcY = pSrcFrame->data[0] + pSrcFrame->width + (pSrcFrame->height- 1 ) * pSrcFrame->linesize[0];
			for (int i = 0; i < pSrcFrame->height; i++)	{
				unsigned char * psrc = startSrcY - i * pSrcFrame->linesize[0];
				unsigned char * pdst = pDstFrame->data[0] + (y + i)*pDstFrame->linesize[0] + x;
				unsigned char * pdst_end = pdst + pSrcFrame->linesize[0];
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
			}
			unsigned char * startSrcU = pSrcFrame->data[1] + pSrcFrame->width/2 + (pSrcFrame->height/2 -1) * pSrcFrame->linesize[1];
			unsigned char * startSrcV = pSrcFrame->data[2] + pSrcFrame->width/2 + (pSrcFrame->height/2 -1) * pSrcFrame->linesize[2];
			for (int i = 0; i < pSrcFrame->height / 2; i++){
				unsigned char * psrc = startSrcU - i * pSrcFrame->linesize[1];
				unsigned char * pdst = pDstFrame->data[1] + (y/2 + i)*pDstFrame->linesize[1] + x/2;
				unsigned char * pdst_end = pdst + pSrcFrame->linesize[1];
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
				psrc = startSrcV - i * pSrcFrame->linesize[2];
				pdst = pDstFrame->data[2] + (y / 2 + i)*pDstFrame->linesize[2] + x / 2;
				pdst_end = pdst + pSrcFrame->linesize[2];
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
			}
		}
		else if (pSrcFrame->pkt_dts == 90) {
			for (int i = 0; i < pSrcFrame->height; i++)	{
				unsigned char * psrc = pSrcFrame->data[0] + i * pSrcFrame->linesize[0];
				unsigned char * psrc_end = psrc + pSrcFrame->width;
				unsigned char * pdst = pDstFrame->data[0] + y * pDstFrame->linesize[0] + pSrcFrame->height -1 - i;
				for (; psrc < psrc_end; psrc++ ,pdst += pDstFrame->linesize[0]) {
					*pdst = *psrc;
				}
			}

			for (int i = 0; i < pSrcFrame->height/2; i++)	{
				unsigned char * psrc = pSrcFrame->data[1] + i * pSrcFrame->linesize[1];
				unsigned char * psrc_end = psrc + pSrcFrame->width/2;
				unsigned char * pdst = pDstFrame->data[1] + y /2 * pDstFrame->linesize[1] + pSrcFrame->height/2 -1 - i;
				for (; psrc < psrc_end; psrc++, pdst += pDstFrame->linesize[1]) {
					*pdst = *psrc;
				}
				psrc = pSrcFrame->data[2] + i * pSrcFrame->linesize[2];
				psrc_end = psrc + pSrcFrame->width / 2;
				pdst = pDstFrame->data[2] + y / 2 * pDstFrame->linesize[2] + pSrcFrame->height / 2 - 1 - i;
				for (; psrc < psrc_end; psrc++, pdst += pDstFrame->linesize[2]) {
					*pdst = *psrc;
				}
			}
			}
		return 0;
	}

	int CAVTranscoder::fillDestFrame(AVFrame * pDstFrame, AVFrame * pSrcFrame, int x, int y, int srcx, int srcy, int w, int h)
	{
		if (pSrcFrame->pkt_dts == 0) {
			for (int i = 0; i < h; i++)	{
				memcpy(pDstFrame->data[0] + (y + i)*pDstFrame->linesize[0] + x, pSrcFrame->data[0] + (i + srcy) * pSrcFrame->linesize[0] + srcx, w);
			}

			for (int i = 0; i < h / 2; i++){
				memcpy(pDstFrame->data[1] + (y / 2 + i)*pDstFrame->linesize[1] + x / 2, pSrcFrame->data[1] + (i + srcy / 2) * pSrcFrame->linesize[1], w / 2);
				memcpy(pDstFrame->data[2] + (y / 2 + i)*pDstFrame->linesize[2] + x / 2, pSrcFrame->data[2] + (i + srcy / 2) * pSrcFrame->linesize[2], w / 2);
			}
		}
		else if (pSrcFrame->pkt_dts == 180) {
			unsigned char * startSrcY = pSrcFrame->data[0] + pSrcFrame->width + (pSrcFrame->height - srcy - 1) * pSrcFrame->linesize[0] - srcx;
			for (int i = 0; i < h; i++)	{
				unsigned char * psrc = startSrcY - i * pSrcFrame->linesize[0];
				unsigned char * pdst = pDstFrame->data[0] + (y + i)*pDstFrame->linesize[0] + x;
				unsigned char * pdst_end = pdst + w;
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
			}
			unsigned char * startSrcU = pSrcFrame->data[1] + pSrcFrame->width / 2 + ((pSrcFrame->height - srcy -1)/ 2) * pSrcFrame->linesize[1] - srcx / 2;
			unsigned char * startSrcV = pSrcFrame->data[2] + pSrcFrame->width / 2 + ((pSrcFrame->height - srcy -1)/ 2) * pSrcFrame->linesize[2] - srcx / 2;
			for (int i = 0; i < h / 2; i++){
				unsigned char * psrc = startSrcU - i * pSrcFrame->linesize[1];
				unsigned char * pdst = pDstFrame->data[1] + (y / 2 + i)*pDstFrame->linesize[1] + x / 2;
				unsigned char * pdst_end = pdst + w/2;
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
				psrc = startSrcV - i * pSrcFrame->linesize[2];
				pdst = pDstFrame->data[2] + (y / 2 + i)*pDstFrame->linesize[2] + x / 2;
				pdst_end = pdst + w/2;
				for (; pdst < pdst_end; psrc--, pdst++) {
					*pdst = *psrc;
				}
			}
		}
		else if (pSrcFrame->pkt_dts == 90) {
			unsigned char * startSrcY = pSrcFrame->data[0] + srcy * pSrcFrame->linesize[0] + srcx;
			for (int i = 0; i < h; i++)	{
				unsigned char * psrc = startSrcY + i * pSrcFrame->linesize[0];
				unsigned char * psrc_end = psrc + w;
				unsigned char * pdst = pDstFrame->data[0] + y*pDstFrame->linesize[0] + h -1 - i + x;
				for (; psrc < psrc_end; psrc++, pdst+=pDstFrame->linesize[0]) {
					*pdst = *psrc;
				}
			}

			unsigned char * startSrcU = pSrcFrame->data[1] + srcy/2 * pSrcFrame->linesize[1] + srcx/2;
			for (int i = 0; i < h/2; i++)	{
				unsigned char * psrc = startSrcU + i* pSrcFrame->linesize[1];
				unsigned char * psrc_end = psrc + w/2;
				unsigned char * pdst = pDstFrame->data[1] + y/2*pDstFrame->linesize[1] + h/2 -1 - i +x/2;
				for (; psrc < psrc_end; psrc++, pdst += pDstFrame->linesize[1]) {
					*pdst = *psrc;
				}
			}

			unsigned char * startSrcV= pSrcFrame->data[2] + srcy / 2 * pSrcFrame->linesize[2] + srcx / 2;
			for (int i = 0; i < h / 2; i++)	{
				unsigned char * psrc = startSrcV + i* pSrcFrame->linesize[2];
				unsigned char * psrc_end = psrc + w / 2;
				unsigned char * pdst = pDstFrame->data[2] + y / 2 * pDstFrame->linesize[2] + h / 2 -1 - i + x/2;
				for (; psrc < psrc_end; psrc++, pdst += pDstFrame->linesize[2]) {
					*pdst = *psrc;
				}
			}
		}
		return 0;
	}

	int CAVTranscoder::mix_and_output_one2one_vframe(vector<CAVDecoder *> & decoders_got_frame)
	{
		//prepare one2one base frame
		AVFrame *pDstFrame = av_frame_alloc();
		pDstFrame->format = AV_PIX_FMT_YUV420P;
		pDstFrame->width = _nOutputWidth;
		pDstFrame->height = _nOutputHeight;
		int ret = av_frame_get_buffer(pDstFrame, 32);
		if (ret < 0) {
			printf("\n error av_frame_get_buffer: %d", ret);
			return -1;
		}

		for (unsigned int i = 0; i < decoders_got_frame.size(); i++) {
			if (decoders_got_frame[i]->_media_role == mr_teacher) {
				AVFrame * pFrame = decoders_got_frame[i]->_cur_v_frame;
				if (pFrame) {
					av_frame_free(&_teacherFrame);
					_teacherFrame = pFrame;
					decoders_got_frame[i]->_cur_v_frame = NULL;
				}
			}
			else {
				AVFrame * pFrame = decoders_got_frame[i]->_cur_v_frame;
				if (pFrame) {
					av_frame_free(&_studentFrame);
					_studentFrame = pFrame;
					decoders_got_frame[i]->_cur_v_frame = NULL;
				}
			}
		}

		if (!_teacherFrame || !_studentFrame) {
			memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight);
			memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 4);
			memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 4);
		}

		if (_teacherFrame) {
			fillDestFrame(pDstFrame, _teacherFrame, 0, 0);
			if (_studentFrame) {
				if (_studentFrame->width == SRC_W) {
					if (_studentFrame->pkt_dts != 90){
						fillDestFrame(pDstFrame, _studentFrame, 0, 240);
					}
					else {
						fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 240, (SRC_W - SRC_H) / 2, 0, SRC_H, SRC_H);
					}
				}
				else if (_studentFrame->pkt_dts == 90){
					fillDestFrame(pDstFrame, _studentFrame, 0, 240);
				}
				else {
					memset(pDstFrame->data[0] + 240 * pDstFrame->linesize[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
					memset(pDstFrame->data[1] + 120 * pDstFrame->linesize[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
					memset(pDstFrame->data[2] + 120 * pDstFrame->linesize[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
					fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 240, 0, (SRC_W - SRC_H) / 2, SRC_H, SRC_H);
				}
			}
		}
		else if (_studentFrame) {
			if (_studentFrame->width == SRC_W) {
				if (_studentFrame->pkt_dts != 90){
					fillDestFrame(pDstFrame, _studentFrame, 0, 0);
				}
				else {
					fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 0, (SRC_W - SRC_H) / 2, 0, SRC_W, SRC_H);
				}
			}
			else if (_studentFrame->pkt_dts == 90){
				fillDestFrame(pDstFrame, _studentFrame, 0, 0);
			}
			else {
				fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 0, 0, (SRC_W - SRC_H) / 2, SRC_W, SRC_H);
			}
		}

		if (decoders_got_frame.size() == 2){
			if (_last_videos_got != 2) {
				_last_videos_got = 2;
				printf("\n--get 2 video:%"PRIu64", %s, %s\n", _cur_out_v_ts, decoders_got_frame[0]->get_cur_vfile().c_str(), decoders_got_frame[1]->get_cur_vfile().c_str());
			}
		}
		else if (decoders_got_frame.size() == 1)
		{
			if (_last_videos_got != 1) {
				_last_videos_got = 1;
				printf("\n--get 1 video:%"PRIu64", %s\n", _cur_out_v_ts, decoders_got_frame[0]->get_cur_vfile().c_str());
			}
		}
		else {
			if (_last_videos_got != 0) {
				_last_videos_got = 0;
				printf("\n--get 0 video:%"PRIu64"\n", _cur_out_v_ts);
			}
		}
#if 0
		if (_cur_out_v_ts > (26 * 60 + 57) *(1000 /50)) {
			printf("\n--get %d video:%"PRIu64"\n", decoders_got_frame.size(), _cur_out_v_ts);
		}
#endif
		//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);

		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;
}