胡斌

refactored to use video width and height defined as variable

@@ -8,10 +8,7 @@ extern "C" { @@ -8,10 +8,7 @@ extern "C" {
8 #pragma comment(lib,"swscale.lib") 8 #pragma comment(lib,"swscale.lib")
9 #endif 9 #endif
10 10
11 -#define SCALED_W 100  
12 -#define SCALED_H 75  
13 -#define SRC_W 320  
14 -#define SRC_H 240 11 +
15 uint8_t blank_r = 0x16; 12 uint8_t blank_r = 0x16;
16 uint8_t blank_g = 0x5a; 13 uint8_t blank_g = 0x5a;
17 uint8_t blank_b = 0x82; 14 uint8_t blank_b = 0x82;
@@ -23,26 +20,30 @@ _nOutputWidth(320), @@ -23,26 +20,30 @@ _nOutputWidth(320),
23 _cur_out_v_ts(0), 20 _cur_out_v_ts(0),
24 _cur_out_a_ts(0), 21 _cur_out_a_ts(0),
25 _max_audio(1), 22 _max_audio(1),
26 -_swsCtx_320_240(NULL),  
27 -_swsCtx_240_320(NULL),  
28 -_swsCtx_240_240(NULL),  
29 -_scaledFrame_320_240(NULL),  
30 -_scaledFrame_240_320(NULL),  
31 -_scaledFrame_240_240(NULL), 23 +_scaled_width(100),
  24 +_scaled_height(75),
  25 +_src_width(320),
  26 +_src_height(240),
  27 +_sws_ctx_w_h(NULL),
  28 +_sws_ctx_h_w(NULL),
  29 +_sws_ctx_h_h(NULL),
  30 +_scaled_frame_w_h(NULL),
  31 +_scaled_frame_h_w(NULL),
  32 +_scaled_frame_h_h(NULL),
32 _last_videos_got(-1), 33 _last_videos_got(-1),
33 _teacherFrame(NULL), 34 _teacherFrame(NULL),
34 _studentFrame(NULL) 35 _studentFrame(NULL)
35 { 36 {
36 _one2one = bOne2One; 37 _one2one = bOne2One;
37 if (_one2one) { 38 if (_one2one) {
38 - _nOutputHeight = 480; 39 + _nOutputHeight = _src_height * 2;
39 } 40 }
40 else { 41 else {
41 - _nOutputHeight = 240; 42 + _nOutputHeight = _src_height;
42 43
43 - init_scale_context(&_swsCtx_320_240, &_scaledFrame_320_240, SRC_W, SRC_H, SCALED_W, SCALED_H);  
44 - init_scale_context(&_swsCtx_240_320, &_scaledFrame_240_320, SRC_H, SRC_W, SCALED_H, SCALED_W);  
45 - init_scale_context(&_swsCtx_240_240, &_scaledFrame_240_240, SRC_H, SRC_H, SCALED_H, SCALED_H); 44 + init_scale_context(&_sws_ctx_w_h, &_scaled_frame_w_h, _src_width, _src_height, _scaled_width, _scaled_height);
  45 + init_scale_context(&_sws_ctx_h_w, &_scaled_frame_h_w, _src_height, _src_width, _scaled_height, _scaled_width);
  46 + init_scale_context(&_sws_ctx_h_h, &_scaled_frame_h_h, _src_height, _src_height, _scaled_height, _scaled_height);
46 } 47 }
47 RGB2YUV(blank_r, blank_g, blank_b, &_blank_y, &_blank_u, &_blank_v); 48 RGB2YUV(blank_r, blank_g, blank_b, &_blank_y, &_blank_u, &_blank_v);
48 } 49 }
@@ -129,9 +130,9 @@ int CAVTranscoder::close() @@ -129,9 +130,9 @@ int CAVTranscoder::close()
129 flush_encoder(1); 130 flush_encoder(1);
130 av_write_trailer(_ofmt_ctx); 131 av_write_trailer(_ofmt_ctx);
131 132
132 - free_scale_context(&_swsCtx_320_240, &_scaledFrame_320_240);  
133 - free_scale_context(&_swsCtx_240_320, &_scaledFrame_240_320);  
134 - free_scale_context(&_swsCtx_240_240, &_scaledFrame_240_240); 133 + free_scale_context(&_sws_ctx_w_h, &_scaled_frame_w_h);
  134 + free_scale_context(&_sws_ctx_h_w, &_scaled_frame_h_w);
  135 + free_scale_context(&_sws_ctx_h_h, &_scaled_frame_h_h);
135 136
136 137
137 #if USE_H264BSF 138 #if USE_H264BSF
@@ -367,36 +368,36 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -367,36 +368,36 @@ int CAVTranscoder::open_output_file(const char *filename)
367 continue; 368 continue;
368 } 369 }
369 int h = 0; 370 int h = 0;
370 - if (pFrame->width == SRC_W && pFrame->height == SRC_H) {  
371 - h = sws_scale(_swsCtx_320_240, pFrame->data, pFrame->linesize, 0, pFrame->height,  
372 - _scaledFrame_320_240->data, _scaledFrame_320_240->linesize); 371 + if (pFrame->width == _src_width && pFrame->height == _src_height) {
  372 + h = sws_scale(_sws_ctx_w_h, pFrame->data, pFrame->linesize, 0, pFrame->height,
  373 + _scaled_frame_w_h->data, _scaled_frame_w_h->linesize);
373 if (h <= 0){ 374 if (h <= 0){
374 printf("\nscale output result:%d?,ignored", h); 375 printf("\nscale output result:%d?,ignored", h);
375 continue; 376 continue;
376 } 377 }
377 - _scaledFrame_320_240->pkt_dts = pFrame->pkt_dts;//pass rotation  
378 - fillDestFrame(pDstFrame, _scaledFrame_320_240, SRC_W - (imageIdx % 4 + 1) * (SCALED_H + 5) + 4, SRC_H - SCALED_H + 3 - (SCALED_H + 1)*(imageIdx / 4), (SCALED_W - SCALED_H) / 2, 0, SCALED_H - 3, SCALED_H - 3); 378 + _scaled_frame_w_h->pkt_dts = pFrame->pkt_dts;//pass rotation
  379 + fillDestFrame(pDstFrame, _scaled_frame_w_h, _src_width - (imageIdx % 4 + 1) * (_scaled_height + 5) + 4, _src_height - _scaled_height + 3 - (_scaled_height + 1)*(imageIdx / 4), (_scaled_width - _scaled_height) / 2, 0, _scaled_height - 3, _scaled_height - 3);
379 } 380 }
380 - else if (pFrame->width == SRC_H && pFrame->height == SRC_W) {  
381 - h = sws_scale(_swsCtx_240_320, pFrame->data, pFrame->linesize, 0, pFrame->height,  
382 - _scaledFrame_240_320->data, _scaledFrame_240_320->linesize); 381 + else if (pFrame->width == _src_height && pFrame->height == _src_width) {
  382 + h = sws_scale(_sws_ctx_h_w, pFrame->data, pFrame->linesize, 0, pFrame->height,
  383 + _scaled_frame_h_w->data, _scaled_frame_h_w->linesize);
383 if (h <= 0){ 384 if (h <= 0){
384 printf("\nscale output result:%d?,ignored", h); 385 printf("\nscale output result:%d?,ignored", h);
385 continue; 386 continue;
386 } 387 }
387 - _scaledFrame_240_320->pkt_dts = pFrame->pkt_dts;//pass rotation  
388 - fillDestFrame(pDstFrame, _scaledFrame_240_320, SRC_W - (imageIdx % 4 + 1) * (SCALED_H + 5) + 4, SRC_H - SCALED_H + 3 - (SCALED_H + 1)*(imageIdx / 4), 0, (SCALED_W - SCALED_H) / 2, SCALED_H - 3, SCALED_H - 3); 388 + _scaled_frame_h_w->pkt_dts = pFrame->pkt_dts;//pass rotation
  389 + fillDestFrame(pDstFrame, _scaled_frame_h_w, _src_width - (imageIdx % 4 + 1) * (_scaled_height + 5) + 4, _src_height - _scaled_height + 3 - (_scaled_height + 1)*(imageIdx / 4), 0, (_scaled_width - _scaled_height) / 2, _scaled_height - 3, _scaled_height - 3);
389 390
390 } 391 }
391 - else if (pFrame->width == SRC_H && pFrame->height == SRC_H) {  
392 - h = sws_scale(_swsCtx_240_240, pFrame->data, pFrame->linesize, 0, pFrame->height,  
393 - _scaledFrame_240_240->data, _scaledFrame_240_240->linesize); 392 + else if (pFrame->width == _src_height && pFrame->height == _src_height) {
  393 + h = sws_scale(_sws_ctx_h_h, pFrame->data, pFrame->linesize, 0, pFrame->height,
  394 + _scaled_frame_h_h->data, _scaled_frame_h_h->linesize);
394 if (h <= 0){ 395 if (h <= 0){
395 printf("\nscale output result:%d?,ignored", h); 396 printf("\nscale output result:%d?,ignored", h);
396 continue; 397 continue;
397 } 398 }
398 - _scaledFrame_240_240->pkt_dts = pFrame->pkt_dts;//pass rotation  
399 - fillDestFrame(pDstFrame, _scaledFrame_240_240, SRC_W - (imageIdx % 4 + 1) * (SCALED_H + 5) + 4, SRC_H - SCALED_H + 3 - (SCALED_H + 1)*(imageIdx / 4), 0, 0, SCALED_H - 3, SCALED_H - 3); 399 + _scaled_frame_h_h->pkt_dts = pFrame->pkt_dts;//pass rotation
  400 + fillDestFrame(pDstFrame, _scaled_frame_h_h, _src_width - (imageIdx % 4 + 1) * (_scaled_height + 5) + 4, _src_height - _scaled_height + 3 - (_scaled_height + 1)*(imageIdx / 4), 0, 0, _scaled_height - 3, _scaled_height - 3);
400 } 401 }
401 else { 402 else {
402 printf("\nthe frame resolution %dx%d is unexpected! ignored!", pFrame->width, pFrame->height); 403 printf("\nthe frame resolution %dx%d is unexpected! ignored!", pFrame->width, pFrame->height);
@@ -606,7 +607,7 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -606,7 +607,7 @@ int CAVTranscoder::open_output_file(const char *filename)
606 } 607 }
607 608
608 if (_teacherFrame) { 609 if (_teacherFrame) {
609 - if (_teacherFrame->width == SRC_W && _teacherFrame->height == SRC_H) { 610 + if (_teacherFrame->width == _src_width && _teacherFrame->height == _src_height) {
610 if (_teacherFrame->pkt_dts != 90){ 611 if (_teacherFrame->pkt_dts != 90){
611 fillDestFrame(pDstFrame, _teacherFrame, 0, 0); 612 fillDestFrame(pDstFrame, _teacherFrame, 0, 0);
612 } 613 }
@@ -614,10 +615,10 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -614,10 +615,10 @@ int CAVTranscoder::open_output_file(const char *filename)
614 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2); 615 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
615 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8); 616 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
616 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8); 617 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
617 - fillDestFrame(pDstFrame, _teacherFrame, (SRC_W - SRC_H) / 2, 0, (SRC_W - SRC_H) / 2, 0, SRC_H, SRC_H); 618 + fillDestFrame(pDstFrame, _teacherFrame, (_src_width - _src_height) / 2, 0, (_src_width - _src_height) / 2, 0, _src_height, _src_height);
618 } 619 }
619 } 620 }
620 - else if (_teacherFrame->width == SRC_H && _teacherFrame->height == SRC_W) { 621 + else if (_teacherFrame->width == _src_height && _teacherFrame->height == _src_width) {
621 if (_teacherFrame->pkt_dts == 90){ 622 if (_teacherFrame->pkt_dts == 90){
622 fillDestFrame(pDstFrame, _teacherFrame, 0, 0); 623 fillDestFrame(pDstFrame, _teacherFrame, 0, 0);
623 } 624 }
@@ -625,60 +626,60 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -625,60 +626,60 @@ int CAVTranscoder::open_output_file(const char *filename)
625 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2); 626 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
626 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8); 627 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
627 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8); 628 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
628 - fillDestFrame(pDstFrame, _teacherFrame, (SRC_W - SRC_H) / 2, 0, 0, (SRC_W - SRC_H) / 2, SRC_H, SRC_H); 629 + fillDestFrame(pDstFrame, _teacherFrame, (_src_width - _src_height) / 2, 0, 0, (_src_width - _src_height) / 2, _src_height, _src_height);
629 } 630 }
630 } 631 }
631 - else if (_teacherFrame->width == SRC_H && _teacherFrame->height == SRC_H) { 632 + else if (_teacherFrame->width == _src_height && _teacherFrame->height == _src_height) {
632 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2); 633 memset(pDstFrame->data[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
633 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8); 634 memset(pDstFrame->data[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
634 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8); 635 memset(pDstFrame->data[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
635 - fillDestFrame(pDstFrame, _teacherFrame, (SRC_W - SRC_H) / 2, 0); 636 + fillDestFrame(pDstFrame, _teacherFrame, (_src_width - _src_height) / 2, 0);
636 } 637 }
637 else { 638 else {
638 printf("\nresolution: %dx%d unexpected!", _studentFrame->width, _studentFrame->height); 639 printf("\nresolution: %dx%d unexpected!", _studentFrame->width, _studentFrame->height);
639 } 640 }
640 if (_studentFrame) { 641 if (_studentFrame) {
641 - if (_studentFrame->width == SRC_W) { 642 + if (_studentFrame->width == _src_width) {
642 if (_studentFrame->pkt_dts != 90){ 643 if (_studentFrame->pkt_dts != 90){
643 - fillDestFrame(pDstFrame, _studentFrame, 0, 240); 644 + fillDestFrame(pDstFrame, _studentFrame, 0, _src_height);
644 } 645 }
645 else { 646 else {
646 - memset(pDstFrame->data[0] + 240 * pDstFrame->linesize[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);  
647 - memset(pDstFrame->data[1] + 120 * pDstFrame->linesize[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);  
648 - memset(pDstFrame->data[2] + 120 * pDstFrame->linesize[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);  
649 - fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 240, (SRC_W - SRC_H) / 2, 0, SRC_H, SRC_H); 647 + memset(pDstFrame->data[0] + _src_height * pDstFrame->linesize[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
  648 + memset(pDstFrame->data[1] + _src_height / 2 * pDstFrame->linesize[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
  649 + memset(pDstFrame->data[2] + _src_height / 2 * pDstFrame->linesize[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
  650 + fillDestFrame(pDstFrame, _studentFrame, (_src_width - _src_height) / 2, _src_height, (_src_width - _src_height) / 2, 0, _src_height, _src_height);
650 } 651 }
651 } 652 }
652 else if (_studentFrame->pkt_dts == 90){ 653 else if (_studentFrame->pkt_dts == 90){
653 - fillDestFrame(pDstFrame, _studentFrame, 0, 240); 654 + fillDestFrame(pDstFrame, _studentFrame, 0, _src_height);
654 } 655 }
655 else { 656 else {
656 - memset(pDstFrame->data[0] + 240 * pDstFrame->linesize[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);  
657 - memset(pDstFrame->data[1] + 120 * pDstFrame->linesize[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);  
658 - memset(pDstFrame->data[2] + 120 * pDstFrame->linesize[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);  
659 - fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 240, 0, (SRC_W - SRC_H) / 2, SRC_H, SRC_H); 657 + memset(pDstFrame->data[0] + _src_height * pDstFrame->linesize[0], _blank_y, _nOutputWidth * _nOutputHeight / 2);
  658 + memset(pDstFrame->data[1] + _src_height / 2 * pDstFrame->linesize[1], _blank_u, _nOutputWidth * _nOutputHeight / 8);
  659 + memset(pDstFrame->data[2] + _src_height / 2 * pDstFrame->linesize[2], _blank_v, _nOutputWidth * _nOutputHeight / 8);
  660 + fillDestFrame(pDstFrame, _studentFrame, (_src_width - _src_height) / 2, _src_height, 0, (_src_width - _src_height) / 2, _src_height, _src_height);
660 } 661 }
661 } 662 }
662 } 663 }
663 else if (_studentFrame) { 664 else if (_studentFrame) {
664 - if (_studentFrame->width == SRC_W && _studentFrame->height == SRC_H) { 665 + if (_studentFrame->width == _src_width && _studentFrame->height == _src_height) {
665 if (_studentFrame->pkt_dts != 90){ 666 if (_studentFrame->pkt_dts != 90){
666 fillDestFrame(pDstFrame, _studentFrame, 0, 0); 667 fillDestFrame(pDstFrame, _studentFrame, 0, 0);
667 } 668 }
668 else { 669 else {
669 - fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 0, (SRC_W - SRC_H) / 2, 0, SRC_H, SRC_H); 670 + fillDestFrame(pDstFrame, _studentFrame, (_src_width - _src_height) / 2, 0, (_src_width - _src_height) / 2, 0, _src_height, _src_height);
670 } 671 }
671 } 672 }
672 - else if (_studentFrame->width == SRC_H && _studentFrame->height == SRC_W) { 673 + else if (_studentFrame->width == _src_height && _studentFrame->height == _src_width) {
673 if (_studentFrame->pkt_dts == 90){ 674 if (_studentFrame->pkt_dts == 90){
674 fillDestFrame(pDstFrame, _studentFrame, 0, 0); 675 fillDestFrame(pDstFrame, _studentFrame, 0, 0);
675 } 676 }
676 else { 677 else {
677 - fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 0, 0, (SRC_W - SRC_H) / 2, SRC_H, SRC_H); 678 + fillDestFrame(pDstFrame, _studentFrame, (_src_width - _src_height) / 2, 0, 0, (_src_width - _src_height) / 2, _src_height, _src_height);
678 } 679 }
679 } 680 }
680 - else if (_studentFrame->width == SRC_H && _studentFrame->height == SRC_H) {  
681 - fillDestFrame(pDstFrame, _studentFrame, (SRC_W - SRC_H) / 2, 0); 681 + else if (_studentFrame->width == _src_height && _studentFrame->height == _src_height) {
  682 + fillDestFrame(pDstFrame, _studentFrame, (_src_width - _src_height) / 2, 0);
682 } 683 }
683 else { 684 else {
684 printf("\nresolution: %dx%d unexpected!", _studentFrame->width, _studentFrame->height); 685 printf("\nresolution: %dx%d unexpected!", _studentFrame->width, _studentFrame->height);
@@ -44,12 +44,16 @@ private: @@ -44,12 +44,16 @@ private:
44 44
45 void * _a_frame_pool; 45 void * _a_frame_pool;
46 int _max_audio; 46 int _max_audio;
47 - struct SwsContext * _swsCtx_320_240;  
48 - struct SwsContext * _swsCtx_240_320;  
49 - struct SwsContext * _swsCtx_240_240;  
50 - AVFrame * _scaledFrame_320_240;  
51 - AVFrame * _scaledFrame_240_320;  
52 - AVFrame * _scaledFrame_240_240; 47 + int _scaled_width;
  48 + int _scaled_height;
  49 + int _src_width;
  50 + int _src_height;
  51 + struct SwsContext * _sws_ctx_w_h;
  52 + struct SwsContext * _sws_ctx_h_w;
  53 + struct SwsContext * _sws_ctx_h_h;
  54 + AVFrame * _scaled_frame_w_h;
  55 + AVFrame * _scaled_frame_h_w;
  56 + AVFrame * _scaled_frame_h_h;
53 int _last_videos_got; 57 int _last_videos_got;
54 AVFrame * _teacherFrame; 58 AVFrame * _teacherFrame;
55 AVFrame * _studentFrame; // for one2one,keep the last frame 59 AVFrame * _studentFrame; // for one2one,keep the last frame