胡斌

count max audio stream should be mixed

fix bug for finished transcoding without all files transcoded
@@ -7,7 +7,8 @@ _all_processed(true), @@ -7,7 +7,8 @@ _all_processed(true),
7 _one2one(true), 7 _one2one(true),
8 _nOutputWidth(320), 8 _nOutputWidth(320),
9 _cur_out_v_ts(0), 9 _cur_out_v_ts(0),
10 -_cur_out_a_ts(0) 10 +_cur_out_a_ts(0),
  11 +_max_audio(1)
11 { 12 {
12 if (_one2one) { 13 if (_one2one) {
13 _nOutputHeight = 480; 14 _nOutputHeight = 480;
@@ -255,11 +256,12 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -255,11 +256,12 @@ int CAVTranscoder::open_output_file(const char *filename)
255 int16_t * psrc = (int16_t *)pFrame->extended_data[0]; 256 int16_t * psrc = (int16_t *)pFrame->extended_data[0];
256 int16_t * pdst = (int16_t *)pDstFrame->extended_data[0]; 257 int16_t * pdst = (int16_t *)pDstFrame->extended_data[0];
257 for (int i = 0; i < 1024; i++,pdst++,psrc++) { 258 for (int i = 0; i < 1024; i++,pdst++,psrc++) {
258 - *pdst += *psrc; 259 + *pdst += (*psrc/_max_audio);
259 } 260 }
260 } 261 }
261 } 262 }
262 263
  264 +
263 pDstFrame->pts = _cur_out_a_ts; 265 pDstFrame->pts = _cur_out_a_ts;
264 pDstFrame->pkt_dts = _cur_out_a_ts; 266 pDstFrame->pkt_dts = _cur_out_a_ts;
265 pDstFrame->pkt_pts = _cur_out_a_ts; 267 pDstFrame->pkt_pts = _cur_out_a_ts;
@@ -268,7 +270,6 @@ int CAVTranscoder::open_output_file(const char *filename) @@ -268,7 +270,6 @@ int CAVTranscoder::open_output_file(const char *filename)
268 270
269 int got_frame = 0; 271 int got_frame = 0;
270 encode_write_frame(pDstFrame, 1, &got_frame); 272 encode_write_frame(pDstFrame, 1, &got_frame);
271 -// av_frame_free(&pDstFrame);  
272 273
273 return 0; 274 return 0;
274 } 275 }
@@ -399,6 +400,7 @@ int CAVTranscoder::encode_write_frame(AVFrame *filt_frame, unsigned int stream_i @@ -399,6 +400,7 @@ int CAVTranscoder::encode_write_frame(AVFrame *filt_frame, unsigned int stream_i
399 av_init_packet(&enc_pkt); 400 av_init_packet(&enc_pkt);
400 ret = enc_func(_ofmt_ctx->streams[stream_index]->codec, &enc_pkt, 401 ret = enc_func(_ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
401 filt_frame, got_frame); 402 filt_frame, got_frame);
  403 + av_frame_unref(filt_frame);
402 av_frame_free(&filt_frame); 404 av_frame_free(&filt_frame);
403 if (ret < 0) 405 if (ret < 0)
404 return ret; 406 return ret;
@@ -438,3 +440,8 @@ int CAVTranscoder::flush_encoder(unsigned int stream_index) @@ -438,3 +440,8 @@ int CAVTranscoder::flush_encoder(unsigned int stream_index)
438 } 440 }
439 return ret; 441 return ret;
440 } 442 }
  443 +
  444 +void CAVTranscoder::set_max_audio(int max_audio)
  445 +{
  446 + _max_audio = max_audio;
  447 +}
@@ -39,5 +39,8 @@ private: @@ -39,5 +39,8 @@ private:
39 int flush_encoder(unsigned int stream_index); 39 int flush_encoder(unsigned int stream_index);
40 40
41 void * _a_frame_pool; 41 void * _a_frame_pool;
  42 + int _max_audio;
  43 +public:
  44 + void set_max_audio(int max_audio);
42 }; 45 };
43 46
@@ -577,6 +577,28 @@ void add_media_info(media_info m) @@ -577,6 +577,28 @@ void add_media_info(media_info m)
577 sorted_media.insert(it, m); 577 sorted_media.insert(it, m);
578 } 578 }
579 579
  580 +
  581 +list<media_info> audio_files;
  582 +
  583 +int count_audio_files(float mid)
  584 +{
  585 + int files = 0;
  586 + list<media_info>::iterator it = audio_files.begin();
  587 + for (; it != audio_files.end(); it++){
  588 + if (it->start_time < mid){
  589 + if (it->end_time > mid) {
  590 + files++;
  591 + }
  592 + }
  593 + else {
  594 + break;
  595 + }
  596 + }
  597 + return files;
  598 +}
  599 +
  600 +int max_audio = 1;
  601 +
580 void add_media_infos() 602 void add_media_infos()
581 { 603 {
582 sorted_media.clear(); 604 sorted_media.clear();
@@ -597,15 +619,51 @@ void add_media_infos() @@ -597,15 +619,51 @@ void add_media_infos()
597 m.start_time_ms = f.start_time * 1000; 619 m.start_time_ms = f.start_time * 1000;
598 m.end_time_ms = f.end_time * 1000; 620 m.end_time_ms = f.end_time * 1000;
599 add_media_info(m); 621 add_media_info(m);
600 - /* 622 +
601 m.t_type = tt_end; 623 m.t_type = tt_end;
602 m.type_time = m.end_time; 624 m.type_time = m.end_time;
603 - add_media_info(m);*/ 625 + add_media_info(m);
604 } 626 }
605 627
  628 + list<media_info> audio_type_files;
  629 +
606 list<media_info>::iterator it = sorted_media.begin(); 630 list<media_info>::iterator it = sorted_media.begin();
607 for (; it != sorted_media.end(); it++){ 631 for (; it != sorted_media.end(); it++){
608 - sorted_infos.push_back(*it); 632 + if (it->m_type == mt_audio) {
  633 + if (it->t_type == tt_start) {
  634 + audio_files.push_back(*it);
  635 + }
  636 + audio_type_files.push_back(*it);
  637 + }
  638 + }
  639 +
  640 + it = sorted_media.begin();
  641 + for (; it != sorted_media.end();){
  642 + if (it->t_type == tt_end) {
  643 + it = sorted_media.erase(it);
  644 + }
  645 + else
  646 + {
  647 + it++;
  648 + }
  649 + }
  650 +
  651 + it = audio_type_files.begin();
  652 + media_info last = *it;
  653 +
  654 + for (it++; it != audio_type_files.end(); it++) {
  655 + if (it->t_type == tt_end) {
  656 + if (last.t_type == tt_start) {
  657 + float mid = (it->type_time + last.type_time) / 2;
  658 + int files_with_mid = count_audio_files(mid);
  659 + if (files_with_mid > max_audio) {
  660 + max_audio = files_with_mid;
  661 + }
  662 + }
  663 + }
  664 + else {
  665 + last = *it;
  666 + }
609 } 667 }
610 } 668 }
611 669
@@ -894,6 +952,7 @@ int process_av_files() @@ -894,6 +952,7 @@ int process_av_files()
894 avfilter_register_all(); 952 avfilter_register_all();
895 953
896 CAVTranscoder videoTranscoder; 954 CAVTranscoder videoTranscoder;
  955 + videoTranscoder.set_max_audio(max_audio);
897 956
898 int64_t cur_time = 0; 957 int64_t cur_time = 0;
899 bool has_file = sorted_media.size(); 958 bool has_file = sorted_media.size();
@@ -924,7 +983,7 @@ int process_av_files() @@ -924,7 +983,7 @@ int process_av_files()
924 cur_time = videoTranscoder.transcode(); 983 cur_time = videoTranscoder.transcode();
925 } 984 }
926 985
927 - while (videoTranscoder.all_processed()){ 986 + while (!videoTranscoder.all_processed()){
928 cur_time = videoTranscoder.transcode(); 987 cur_time = videoTranscoder.transcode();
929 } 988 }
930 989