胡斌

new feature, if no video in all student and teacher,the output mp4 is also no video

@@ -34,13 +34,14 @@ int g_fps; @@ -34,13 +34,14 @@ int g_fps;
34 34
35 #define ensure_no_zero(x, v) if(!x) x = v 35 #define ensure_no_zero(x, v) if(!x) x = v
36 36
37 -CAVTranscoder::CAVTranscoder(bool bOne2One, bool one2one_same_size, bool one2many_inflat, int width_teacher, int height_teacher, int width_student, int height_student, bool has_teacher, int max_audio,int keep_4_3) : 37 +CAVTranscoder::CAVTranscoder(bool bOne2One, bool one2one_same_size, bool one2many_inflat, int width_teacher, int height_teacher, int width_student, int height_student, bool has_teacher, int max_audio, int max_video, int keep_4_3) :
38 _start_time(INT64_MAX), 38 _start_time(INT64_MAX),
39 _all_processed(true), 39 _all_processed(true),
40 _nOutputWidth(320), 40 _nOutputWidth(320),
41 _cur_out_v_ts(0), 41 _cur_out_v_ts(0),
42 _cur_out_a_ts(0), 42 _cur_out_a_ts(0),
43 -_max_audio(1), 43 +_max_audio(max_audio),
  44 +_max_video(max_video),
44 _sws_ctx_w_h(NULL), 45 _sws_ctx_w_h(NULL),
45 _sws_ctx_h_w(NULL), 46 _sws_ctx_h_w(NULL),
46 _sws_ctx_h_h(NULL), 47 _sws_ctx_h_h(NULL),
@@ -133,6 +134,8 @@ _one2one_same_size(one2one_same_size) @@ -133,6 +134,8 @@ _one2one_same_size(one2one_same_size)
133 init_scale_context(&_sws_ctx_h_h, &_scaled_frame_h_h, _student_height, _student_height, _scaled_height, _scaled_height); 134 init_scale_context(&_sws_ctx_h_h, &_scaled_frame_h_h, _student_height, _student_height, _scaled_height, _scaled_height);
134 } 135 }
135 RGB2YUV(blank_r, blank_g, blank_b, &_blank_y, &_blank_u, &_blank_v); 136 RGB2YUV(blank_r, blank_g, blank_b, &_blank_y, &_blank_u, &_blank_v);
  137 +
  138 + _n_streams = max_video > 0 ? 2 : 1;
136 } 139 }
137 140
138 141
@@ -182,7 +185,9 @@ double CAVTranscoder::transcode() @@ -182,7 +185,9 @@ double CAVTranscoder::transcode()
182 185
183 _all_processed = decoders_got_frame.size() == 0; 186 _all_processed = decoders_got_frame.size() == 0;
184 187
185 - mix_and_output_vframe(decoders_got_frame); 188 + if (_max_video > 0) {
  189 + mix_and_output_vframe(decoders_got_frame);
  190 + }
186 191
187 _cur_v_time += g_vframe_duration; 192 _cur_v_time += g_vframe_duration;
188 193
@@ -214,8 +219,9 @@ bool CAVTranscoder::all_processed() @@ -214,8 +219,9 @@ bool CAVTranscoder::all_processed()
214 219
215 int CAVTranscoder::close() 220 int CAVTranscoder::close()
216 { 221 {
217 - flush_encoder(0);  
218 - flush_encoder(1); 222 + for (int i = 0; i < _n_streams; i++){
  223 + flush_encoder(i);
  224 + }
219 av_write_trailer(_ofmt_ctx); 225 av_write_trailer(_ofmt_ctx);
220 226
221 free_scale_context(&_sws_ctx_w_h, &_scaled_frame_w_h); 227 free_scale_context(&_sws_ctx_w_h, &_scaled_frame_w_h);
@@ -238,7 +244,7 @@ int CAVTranscoder::close() @@ -238,7 +244,7 @@ int CAVTranscoder::close()
238 av_bitstream_filter_close(aacbsfc); 244 av_bitstream_filter_close(aacbsfc);
239 #endif 245 #endif
240 unsigned int i; 246 unsigned int i;
241 - for (i = 0; i<2; i++) 247 + for (i = 0; i< _n_streams; i++)
242 { 248 {
243 if (_ofmt_ctx && _ofmt_ctx->nb_streams > i && _ofmt_ctx->streams[i] && _ofmt_ctx->streams[i]->codec) 249 if (_ofmt_ctx && _ofmt_ctx->nb_streams > i && _ofmt_ctx->streams[i] && _ofmt_ctx->streams[i]->codec)
244 avcodec_close(_ofmt_ctx->streams[i]->codec); 250 avcodec_close(_ofmt_ctx->streams[i]->codec);
@@ -267,7 +273,9 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -267,7 +273,9 @@ int CAVTranscoder::open_output_file(const char *filename)
267 return AVERROR_UNKNOWN; 273 return AVERROR_UNKNOWN;
268 } 274 }
269 275
270 - for (i = 0; i < 2; i++) { 276 +
  277 +
  278 + for (i = 0; i < _n_streams; i++) {
271 out_stream = avformat_new_stream(_ofmt_ctx, NULL); 279 out_stream = avformat_new_stream(_ofmt_ctx, NULL);
272 if (!out_stream) { 280 if (!out_stream) {
273 av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n"); 281 av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
@@ -279,7 +287,7 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -279,7 +287,7 @@ int CAVTranscoder::open_output_file(const char *filename)
279 if (_ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) 287 if (_ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
280 enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER; 288 enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
281 289
282 - if (0 == i) { 290 + if (1 == i) {
283 encoder = avcodec_find_encoder(AV_CODEC_ID_H264); 291 encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
284 if (!encoder) { 292 if (!encoder) {
285 av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n"); 293 av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
@@ -449,7 +457,7 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -449,7 +457,7 @@ int CAVTranscoder::open_output_file(const char *filename)
449 _cur_out_a_ts += 1024; 457 _cur_out_a_ts += 1024;
450 458
451 int got_frame = 0; 459 int got_frame = 0;
452 - encode_write_frame(pDstFrame, 1, &got_frame); 460 + encode_write_frame(pDstFrame, 0, &got_frame);
453 461
454 return 0; 462 return 0;
455 } 463 }
@@ -598,7 +606,7 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -598,7 +606,7 @@ int CAVTranscoder::open_output_file(const char *filename)
598 606
599 //send to encoder 607 //send to encoder
600 int got_frame = 0; 608 int got_frame = 0;
601 - encode_write_frame(pDstFrame, 0, &got_frame); 609 + encode_write_frame(pDstFrame, 1, &got_frame);
602 610
603 return 0; 611 return 0;
604 } 612 }
@@ -1100,7 +1108,7 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -1100,7 +1108,7 @@ int CAVTranscoder::open_output_file(const char *filename)
1100 1108
1101 //send to encoder 1109 //send to encoder
1102 int got_frame = 0; 1110 int got_frame = 0;
1103 - encode_write_frame(pDstFrame, 0, &got_frame); 1111 + encode_write_frame(pDstFrame, 1, &got_frame);
1104 1112
1105 return 0; 1113 return 0;
1106 } 1114 }
@@ -1111,7 +1119,7 @@ int CAVTranscoder::encode_write_frame(AVFrame *filt_frame, unsigned int stream_i @@ -1111,7 +1119,7 @@ int CAVTranscoder::encode_write_frame(AVFrame *filt_frame, unsigned int stream_i
1111 AVPacket enc_pkt; 1119 AVPacket enc_pkt;
1112 1120
1113 int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) = 1121 int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
1114 - stream_index == 0 ? avcodec_encode_video2 : avcodec_encode_audio2; 1122 + stream_index == 1 ? avcodec_encode_video2 : avcodec_encode_audio2;
1115 1123
1116 if (!got_frame) 1124 if (!got_frame)
1117 got_frame = &got_frame_local; 1125 got_frame = &got_frame_local;
@@ -1193,8 +1201,3 @@ int CAVTranscoder::free_scale_context(SwsContext ** ctx, AVFrame ** frame) @@ -1193,8 +1201,3 @@ int CAVTranscoder::free_scale_context(SwsContext ** ctx, AVFrame ** frame)
1193 } 1201 }
1194 return 0; 1202 return 0;
1195 } 1203 }
1196 -  
1197 -void CAVTranscoder::set_max_audio(int max_audio)  
1198 -{  
1199 - _max_audio = max_audio;  
1200 -}  
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 class CAVTranscoder 5 class CAVTranscoder
6 { 6 {
7 public: 7 public:
8 - CAVTranscoder(bool bOne2One, bool one2one_same_size, bool one2many_inflat, int width_teacher, int height_teacher, int student_width, int student_height, bool has_teacher, int max_audio, int keep_4_3= true); 8 + CAVTranscoder(bool bOne2One, bool one2one_same_size, bool one2many_inflat, int width_teacher, int height_teacher, int student_width, int student_height, bool has_teacher, int max_audio, int max_video, int keep_4_3= true);
9 virtual ~CAVTranscoder(); 9 virtual ~CAVTranscoder();
10 10
11 int add(media_info & info); 11 int add(media_info & info);
@@ -45,6 +45,8 @@ private: @@ -45,6 +45,8 @@ private:
45 45
46 void * _a_frame_pool; 46 void * _a_frame_pool;
47 int _max_audio; 47 int _max_audio;
  48 + int _max_video;
  49 + int _n_streams;
48 int _scaled_width; 50 int _scaled_width;
49 int _scaled_height; 51 int _scaled_height;
50 int _pip_width; 52 int _pip_width;
@@ -75,8 +77,6 @@ private: @@ -75,8 +77,6 @@ private:
75 int _src_height_student; 77 int _src_height_student;
76 78
77 uint8_t _blank_y, _blank_u, _blank_v; 79 uint8_t _blank_y, _blank_u, _blank_v;
78 -public:  
79 - void set_max_audio(int max_audio);  
80 private: 80 private:
81 int scale_fill_one2one_studentframe(AVFrame * pDstFrame, int y); 81 int scale_fill_one2one_studentframe(AVFrame * pDstFrame, int y);
82 int fill_one2one_student_frame(AVFrame * pDstFrame, int y); 82 int fill_one2one_student_frame(AVFrame * pDstFrame, int y);
@@ -637,6 +637,8 @@ void add_media_info_with_end(media_info m) @@ -637,6 +637,8 @@ void add_media_info_with_end(media_info m)
637 } 637 }
638 638
639 list<media_info> audio_files; 639 list<media_info> audio_files;
  640 +list<media_info> video_files;
  641 +
640 642
641 int count_audio_files(float mid) 643 int count_audio_files(float mid)
642 { 644 {
@@ -655,7 +657,25 @@ int count_audio_files(float mid) @@ -655,7 +657,25 @@ int count_audio_files(float mid)
655 return files; 657 return files;
656 } 658 }
657 659
  660 +int count_video_files(float mid)
  661 +{
  662 + int files = 0;
  663 + list<media_info>::iterator it = video_files.begin();
  664 + for (; it != video_files.end(); it++){
  665 + if (it->start_time < mid){
  666 + if (it->end_time > mid) {
  667 + files++;
  668 + }
  669 + }
  670 + else {
  671 + break;
  672 + }
  673 + }
  674 + return files;
  675 +}
  676 +
658 int max_audio = 1; 677 int max_audio = 1;
  678 +int max_video = 0;
659 int width = 0; 679 int width = 0;
660 int height = 0; 680 int height = 0;
661 int width_teacher = 0; 681 int width_teacher = 0;
@@ -705,6 +725,9 @@ void add_media_infos() @@ -705,6 +725,9 @@ void add_media_infos()
705 if (it->m_type == mt_audio) { 725 if (it->m_type == mt_audio) {
706 audio_files.push_back(*it); 726 audio_files.push_back(*it);
707 } 727 }
  728 + else if (it->m_type == mt_video) {
  729 + video_files.push_back(*it);
  730 + }
708 } 731 }
709 732
710 list<media_info> audio_type_files; 733 list<media_info> audio_type_files;
@@ -800,6 +823,38 @@ void add_media_infos() @@ -800,6 +823,38 @@ void add_media_infos()
800 } 823 }
801 } 824 }
802 } 825 }
  826 +
  827 +
  828 + list<media_info> video_type_files;
  829 +
  830 + it = sorted_media_with_end.begin();
  831 + for (; it != sorted_media_with_end.end(); it++){
  832 + if (it->m_type == mt_video) {
  833 + video_type_files.push_back(*it);
  834 + }
  835 + }
  836 +
  837 + it = video_type_files.begin();
  838 + if (it != video_type_files.end()) {
  839 + media_info last = *it;
  840 +
  841 + for (it++; it != video_type_files.end(); it++) {
  842 + if (it->t_type == tt_end) {
  843 + if (last.t_type == tt_start) {
  844 + float mid = (it->type_time + last.type_time) / 2;
  845 + int files_with_mid = count_video_files(mid);
  846 + if (files_with_mid > max_video) {
  847 + max_video = files_with_mid;
  848 + }
  849 + }
  850 + }
  851 + else {
  852 + last = *it;
  853 + }
  854 + }
  855 + }
  856 +
  857 +
803 } 858 }
804 859
805 void unifiy_start_time() 860 void unifiy_start_time()
@@ -1097,7 +1152,10 @@ int process_av_files(char * record_info, int piptype, bool one2one_same_size, in @@ -1097,7 +1152,10 @@ int process_av_files(char * record_info, int piptype, bool one2one_same_size, in
1097 { 1152 {
1098 time_t start, end; 1153 time_t start, end;
1099 time(&start); 1154 time(&start);
1100 - load_record_info(record_info); 1155 + if (load_record_info(record_info)){
  1156 + printf("\nerror open record info file: %s", record_info);
  1157 + return -1;
  1158 + }
1101 1159
1102 get_outinfo_file_name(record_info); 1160 get_outinfo_file_name(record_info);
1103 1161
@@ -1139,8 +1197,7 @@ int process_av_files(char * record_info, int piptype, bool one2one_same_size, in @@ -1139,8 +1197,7 @@ int process_av_files(char * record_info, int piptype, bool one2one_same_size, in
1139 } 1197 }
1140 } 1198 }
1141 1199
1142 - CAVTranscoder videoTranscoder(one2one, one2one_same_size, one_to_many_inflat, width_teacher, height_teacher, width_student, height_student, has_teacher, max_audio);  
1143 - videoTranscoder.set_max_audio(max_audio); 1200 + CAVTranscoder videoTranscoder(one2one, one2one_same_size, one_to_many_inflat, width_teacher, height_teacher, width_student, height_student, has_teacher, max_audio, max_video);
1144 1201
1145 int64_t cur_time = 0; 1202 int64_t cur_time = 0;
1146 bool has_file = sorted_media.size()!=0; 1203 bool has_file = sorted_media.size()!=0;