Because write_packet() fakely writes packets to muxer by queueing them when muxer hasn't been initialized, it should also increment frame_number fakely. This is required because code in do_streamcopy() rely on frame_number. Should fix Ticket6227
Unified · Split
| @@ -669,12 +669,28 @@ static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, · 668 unmodified lines | ||
| 669 | 669 | } |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | −static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) | |
| 672 | +static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue) | |
| 673 | 673 | { |
| 674 | 674 | AVFormatContext *s = of->ctx; |
| 675 | 675 | AVStream *st = ost->st; |
| 676 | 676 | int ret; |
| 677 | 677 | |
| 678 | + /* | |
| 679 | + * Audio encoders may split the packets -- #frames in != #packets out. | |
| 680 | + * But there is no reordering, so we can limit the number of output packets | |
| 681 | + * by simply dropping them here. | |
| 682 | + * Counting encoded video frames needs to be done separately because of | |
| 683 | + * reordering, see do_video_out(). | |
| 684 | + * Do not count the packet when unqueued because it has been counted when queued. | |
| 685 | + */ | |
| 686 | + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { | |
| 687 | + if (ost->frame_number >= ost->max_frames) { | |
| 688 | + av_packet_unref(pkt); | |
| 689 | + return; | |
| 690 | + } | |
| 691 | + ost->frame_number++; | |
| 692 | + } | |
| 693 | + | |
| 678 | 694 | if (!of->header_written) { |
| 679 | 695 | AVPacket tmp_pkt = {0}; |
| 680 | 696 | /* the muxer is not initialized yet, buffer the packet */ |
| @@ -703,20 +719,6 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) · 22 unmodified lines | ||
| 703 | 719 | (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) |
| 704 | 720 | pkt->pts = pkt->dts = AV_NOPTS_VALUE; |
| 705 | 721 | |
| 706 | − /* | |
| 707 | − * Audio encoders may split the packets -- #frames in != #packets out. | |
| 708 | − * But there is no reordering, so we can limit the number of output packets | |
| 709 | − * by simply dropping them here. | |
| 710 | − * Counting encoded video frames needs to be done separately because of | |
| 711 | − * reordering, see do_video_out() | |
| 712 | − */ | |
| 713 | − if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { | |
| 714 | − if (ost->frame_number >= ost->max_frames) { | |
| 715 | − av_packet_unref(pkt); | |
| 716 | − return; | |
| 717 | − } | |
| 718 | − ost->frame_number++; | |
| 719 | − } | |
| 720 | 722 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 721 | 723 | int i; |
| 722 | 724 | uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, |
| @@ -861,10 +863,10 @@ static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) · 138 unmodified lines | ||
| 861 | 863 | goto finish; |
| 862 | 864 | idx++; |
| 863 | 865 | } else |
| 864 | − write_packet(of, pkt, ost); | |
| 866 | + write_packet(of, pkt, ost, 0); | |
| 865 | 867 | } |
| 866 | 868 | } else |
| 867 | − write_packet(of, pkt, ost); | |
| 869 | + write_packet(of, pkt, ost, 0); | |
| 868 | 870 | |
| 869 | 871 | finish: |
| 870 | 872 | if (ret < 0 && ret != AVERROR_EOF) { |
| @@ -2972,7 +2974,7 @@ static int check_init_output_file(OutputFile *of, int file_index) · 2101 unmodified lines | ||
| 2972 | 2974 | while (av_fifo_size(ost->muxing_queue)) { |
| 2973 | 2975 | AVPacket pkt; |
| 2974 | 2976 | av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); |
| 2975 | − write_packet(of, &pkt, ost); | |
| 2977 | + write_packet(of, &pkt, ost, 1); | |
| 2976 | 2978 | } |
| 2977 | 2979 | } |
| 2978 | 2980 | |