diff --git a/ffmpeg.c b/ffmpeg.c index 5adec2b290..983e2fb486 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -713,10 +713,12 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) if (pkt->duration > 0) av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), - ost->st->time_base); + ost->mux_timebase); } } + av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && @@ -907,13 +909,13 @@ static void do_audio_out(OutputFile *of, OutputStream *ost, update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:audio " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", - av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), - av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); + av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base), + av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base)); } output_packet(of, &pkt, ost); @@ -993,15 +995,15 @@ static void do_subtitle_out(OutputFile *of, av_init_packet(&pkt); pkt.data = subtitle_out; pkt.size = subtitle_out_size; - pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); - pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base); + pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase); + pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) { /* XXX: the pts correction is handled here. Maybe handling it in the codec would be better */ if (i == 0) - pkt.pts += 90 * sub->start_display_time; + pkt.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); else - pkt.pts += 90 * sub->end_display_time; + pkt.pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); } pkt.dts = pkt.pts; output_packet(of, &pkt, ost); @@ -1187,7 +1189,7 @@ static void do_video_out(OutputFile *of, mux_par->field_order = AV_FIELD_PROGRESSIVE; pkt.data = (uint8_t *)in_picture; pkt.size = sizeof(AVPicture); - pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base); + pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->mux_timebase); pkt.flags |= AV_PKT_FLAG_KEY; output_packet(of, &pkt, ost); @@ -1283,13 +1285,13 @@ static void do_video_out(OutputFile *of, if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY)) pkt.pts = ost->sync_opts; - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:video " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", - av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->st->time_base), - av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->st->time_base)); + av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->mux_timebase), + av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->mux_timebase)); } frame_size = pkt.size; @@ -1862,7 +1864,7 @@ static void flush_encoders(void) av_packet_unref(&pkt); continue; } - av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); + av_packet_rescale_ts(&pkt, enc->time_base, ost->mux_timebase); pkt_size = pkt.size; output_packet(of, &pkt, ost); if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) { @@ -1897,7 +1899,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p OutputFile *of = output_files[ost->file_index]; InputFile *f = input_files [ist->file_index]; int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; - int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base); + int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase); AVPicture pict; AVPacket opkt; @@ -1938,14 +1940,14 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p ost->sync_opts++; if (pkt->pts != AV_NOPTS_VALUE) - opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time; + opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time; else opkt.pts = AV_NOPTS_VALUE; if (pkt->dts == AV_NOPTS_VALUE) - opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->st->time_base); + opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase); else - opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base); + opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase); opkt.dts -= ost_tb_start_time; if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) { @@ -1954,10 +1956,11 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p duration = ist->dec_ctx->frame_size; opkt.dts = opkt.pts = av_rescale_delta(ist->st->time_base, pkt->dts, (AVRational){1, ist->dec_ctx->sample_rate}, duration, &ist->filter_in_rescale_delta_last, - ost->st->time_base) - ost_tb_start_time; + ost->mux_timebase) - ost_tb_start_time; } - opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base); + opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); + opkt.flags = pkt->flags; // FIXME remove the following 2 lines they shall be replaced by the bitstream filters if ( ost->st->codecpar->codec_id != AV_CODEC_ID_H264 @@ -2822,6 +2825,10 @@ static int check_init_output_file(OutputFile *of, int file_index) for (i = 0; i < of->ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; + /* try to improve muxing time_base (only possible if nothing has been written yet) */ + if (!av_fifo_size(ost->muxing_queue)) + ost->mux_timebase = ost->st->time_base; + while (av_fifo_size(ost->muxing_queue)) { AVPacket pkt; av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); @@ -2981,6 +2988,8 @@ static int init_output_stream_streamcopy(OutputStream *ost) break; } + ost->mux_timebase = ist->st->time_base; + return 0; } @@ -3236,7 +3245,7 @@ static int init_output_stream_encode(OutputStream *ost) } break; case AVMEDIA_TYPE_SUBTITLE: - enc_ctx->time_base = (AVRational){1, 1000}; + enc_ctx->time_base = AV_TIME_BASE_Q; if (!enc_ctx->width) { enc_ctx->width = input_streams[ost->source_index]->st->codecpar->width; enc_ctx->height = input_streams[ost->source_index]->st->codecpar->height; @@ -3249,6 +3258,8 @@ static int init_output_stream_encode(OutputStream *ost) break; } + ost->mux_timebase = enc_ctx->time_base; + return 0; } diff --git a/ffmpeg.h b/ffmpeg.h index 458bb8a3dc..ca35ccc260 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -446,6 +446,8 @@ typedef struct OutputStream { int64_t first_pts; /* dts of the last packet sent to the muxer */ int64_t last_mux_dts; + // the timebase of the packets sent to the muxer + AVRational mux_timebase; int nb_bitstream_filters; uint8_t *bsf_extradata_updated; diff --git a/tests/ref/fate/binsub-movtextenc b/tests/ref/fate/binsub-movtextenc index 6efe2c015a..22ee85a2f8 100644 --- a/tests/ref/fate/binsub-movtextenc +++ b/tests/ref/fate/binsub-movtextenc @@ -1 +1 @@ -ef264064c522389d0cf267c4d6235561 +af6a8f38d7c11d9af7823cc44554d2ad diff --git a/tests/ref/fate/sub2video b/tests/ref/fate/sub2video index ace6d38538..4e034a5e91 100644 --- a/tests/ref/fate/sub2video +++ b/tests/ref/fate/sub2video @@ -4,13 +4,13 @@ #codec_id 0: rawvideo #dimensions 0: 720x480 #sar 0: 0/1 -#tb 1: 1/1000 +#tb 1: 1/1000000 #media_type 1: subtitle #codec_id 1: dvd_subtitle 0, 0, 0, 1, 518400, 0x83c27b82 0, 1, 1, 1, 518400, 0x4051c7f9 0, 2, 2, 1, 518400, 0xfb00e17e -1, 499, 499, 4960, 1015, 0x19e092d2, F=0x0 +1, 499000, 499000, 4960000, 1015, 0x19e092d2, F=0x0 0, 3, 3, 1, 518400, 0x192abb74 0, 4, 4, 1, 518400, 0x4669a88b 0, 5, 5, 1, 518400, 0xaababe00 @@ -58,129 +58,129 @@ 0, 47, 47, 1, 518400, 0xde69683f 0, 48, 48, 1, 518400, 0x7df08fba 0, 49, 49, 1, 518400, 0xbab197ea -1, 15355, 15355, 4733, 2094, 0x3c171425, F=0x0 +1, 15355000, 15355000, 4733000, 2094, 0x3c171425, F=0x0 0, 77, 77, 1, 518400, 0x902285d9 0, 100, 100, 1, 518400, 0xbab197ea -1, 48797, 48797, 2560, 2480, 0x7c0edf21, F=0x0 +1, 48797000, 48797000, 2560000, 2480, 0x7c0edf21, F=0x0 0, 244, 244, 1, 518400, 0x7a11c812 0, 257, 257, 1, 518400, 0xbab197ea -1, 51433, 51433, 2366, 3059, 0xc95b8a05, F=0x0 +1, 51433000, 51433000, 2366000, 3059, 0xc95b8a05, F=0x0 0, 258, 258, 1, 518400, 0x34cdddee 0, 269, 269, 1, 518400, 0xbab197ea -1, 53910, 53910, 2696, 2095, 0x61bb15ed, F=0x0 +1, 53910000, 53910000, 2696000, 2095, 0x61bb15ed, F=0x0 0, 270, 270, 1, 518400, 0x4db4ce51 0, 283, 283, 1, 518400, 0xbab197ea -1, 56663, 56663, 1262, 1013, 0xc9ae89b7, F=0x0 +1, 56663000, 56663000, 1262000, 1013, 0xc9ae89b7, F=0x0 0, 284, 284, 1, 518400, 0xe6bc0ea9 0, 290, 290, 1, 518400, 0xbab197ea -1, 58014, 58014, 1661, 969, 0xe01878f0, F=0x0 +1, 58014000, 58014000, 1661000, 969, 0xe01878f0, F=0x0 0, 291, 291, 1, 518400, 0xa8643af7 0, 298, 298, 1, 518400, 0xbab197ea -1, 67724, 67724, 1365, 844, 0xe7db4fc1, F=0x0 +1, 67724000, 67724000, 1365000, 844, 0xe7db4fc1, F=0x0 0, 339, 339, 1, 518400, 0xb1885c67 0, 345, 345, 1, 518400, 0xbab197ea -1, 69175, 69175, 1558, 802, 0xf48531ba, F=0x0 +1, 69175000, 69175000, 1558000, 802, 0xf48531ba, F=0x0 0, 346, 346, 1, 518400, 0x378e3fd0 0, 354, 354, 1, 518400, 0xbab197ea -1, 70819, 70819, 1865, 1709, 0xb4d5a1bd, F=0x0 +1, 70819000, 70819000, 1865000, 1709, 0xb4d5a1bd, F=0x0 0, 355, 355, 1, 518400, 0xa3782469 0, 363, 363, 1, 518400, 0xbab197ea -1, 72762, 72762, 1968, 2438, 0x99d7bc82, F=0x0 +1, 72762000, 72762000, 1968000, 2438, 0x99d7bc82, F=0x0 0, 364, 364, 1, 518400, 0xba23a0d5 0, 374, 374, 1, 518400, 0xbab197ea -1, 74806, 74806, 1831, 2116, 0x96514097, F=0x0 +1, 74806000, 74806000, 1831000, 2116, 0x96514097, F=0x0 0, 375, 375, 1, 518400, 0x129de2f8 0, 383, 383, 1, 518400, 0xbab197ea -1, 76716, 76716, 1262, 1822, 0xefccc72e, F=0x0 +1, 76716000, 76716000, 1262000, 1822, 0xefccc72e, F=0x0 0, 384, 384, 1, 518400, 0x19772f0f 0, 390, 390, 1, 518400, 0xbab197ea -1, 78051, 78051, 1524, 987, 0x7b927a27, F=0x0 +1, 78051000, 78051000, 1524000, 987, 0x7b927a27, F=0x0 0, 391, 391, 1, 518400, 0x56f54e73 0, 398, 398, 1, 518400, 0xbab197ea -1, 79644, 79644, 2662, 2956, 0x190778f7, F=0x0 +1, 79644000, 79644000, 2662000, 2956, 0x190778f7, F=0x0 0, 399, 399, 1, 518400, 0x300b5247 -1, 82380, 82380, 2764, 3094, 0xc021b7d3, F=0x0 +1, 82380000, 82380000, 2764000, 3094, 0xc021b7d3, F=0x0 0, 412, 412, 1, 518400, 0xbab197ea 0, 413, 413, 1, 518400, 0x6fd028fa 0, 426, 426, 1, 518400, 0xbab197ea -1, 85225, 85225, 2366, 2585, 0x74d0048f, F=0x0 +1, 85225000, 85225000, 2366000, 2585, 0x74d0048f, F=0x0 0, 427, 427, 1, 518400, 0x01f80e9d 0, 438, 438, 1, 518400, 0xbab197ea -1, 87652, 87652, 1831, 634, 0x8832fda1, F=0x0 +1, 87652000, 87652000, 1831000, 634, 0x8832fda1, F=0x0 0, 439, 439, 1, 518400, 0xb48d90c0 0, 447, 447, 1, 518400, 0xbab197ea -1, 91531, 91531, 2332, 2080, 0x97a1146f, F=0x0 +1, 91531000, 91531000, 2332000, 2080, 0x97a1146f, F=0x0 0, 458, 458, 1, 518400, 0xcb5a0173 0, 469, 469, 1, 518400, 0xbab197ea -1, 95510, 95510, 3299, 2964, 0x8b8f6684, F=0x0 +1, 95510000, 95510000, 3299000, 2964, 0x8b8f6684, F=0x0 0, 478, 478, 1, 518400, 0xb8a323e4 0, 494, 494, 1, 518400, 0xbab197ea -1, 98872, 98872, 2161, 1875, 0x9002ef71, F=0x0 +1, 98872000, 98872000, 2161000, 1875, 0x9002ef71, F=0x0 0, 495, 495, 1, 518400, 0xc43518ba 0, 505, 505, 1, 518400, 0xbab197ea -1, 101124, 101124, 4096, 3872, 0x20c6ed9c, F=0x0 +1, 101124000, 101124000, 4096000, 3872, 0x20c6ed9c, F=0x0 0, 506, 506, 1, 518400, 0x04e38692 0, 526, 526, 1, 518400, 0xbab197ea -1, 105303, 105303, 2730, 3094, 0xf203a663, F=0x0 +1, 105303000, 105303000, 2730000, 3094, 0xf203a663, F=0x0 0, 527, 527, 1, 518400, 0x856b0ee5 0, 540, 540, 1, 518400, 0xbab197ea -1, 108106, 108106, 2059, 2404, 0x41a7b429, F=0x0 +1, 108106000, 108106000, 2059000, 2404, 0x41a7b429, F=0x0 0, 541, 541, 1, 518400, 0x3e5beee2 0, 551, 551, 1, 518400, 0xbab197ea -1, 141556, 141556, 1661, 1088, 0xde20aa20, F=0x0 +1, 141556000, 141556000, 1661000, 1088, 0xde20aa20, F=0x0 0, 708, 708, 1, 518400, 0xb8bc1365 0, 716, 716, 1, 518400, 0xbab197ea 0, 817, 817, 1, 518400, 0x83efa32d -1, 163445, 163445, 1331, 339, 0x8bd186ef, F=0x0 +1, 163445000, 163445000, 1331000, 339, 0x8bd186ef, F=0x0 0, 824, 824, 1, 518400, 0xbab197ea 0, 840, 840, 1, 518400, 0x03ea0e90 -1, 168049, 168049, 1900, 1312, 0x0bf20e8d, F=0x0 +1, 168049000, 168049000, 1900000, 1312, 0x0bf20e8d, F=0x0 0, 850, 850, 1, 518400, 0xbab197ea -1, 170035, 170035, 1524, 1279, 0xb6c2dafe, F=0x0 +1, 170035000, 170035000, 1524000, 1279, 0xb6c2dafe, F=0x0 0, 851, 851, 1, 518400, 0x8780239e 0, 858, 858, 1, 518400, 0xbab197ea 0, 861, 861, 1, 518400, 0x6eb72347 -1, 172203, 172203, 1695, 1826, 0x9a1ac769, F=0x0 +1, 172203000, 172203000, 1695000, 1826, 0x9a1ac769, F=0x0 0, 869, 869, 1, 518400, 0xbab197ea -1, 173947, 173947, 1934, 1474, 0xa9b03cdc, F=0x0 +1, 173947000, 173947000, 1934000, 1474, 0xa9b03cdc, F=0x0 0, 870, 870, 1, 518400, 0x9c4a3a3d 0, 879, 879, 1, 518400, 0xbab197ea -1, 175957, 175957, 1763, 1019, 0x20409355, F=0x0 +1, 175957000, 175957000, 1763000, 1019, 0x20409355, F=0x0 0, 880, 880, 1, 518400, 0xc9ebfa89 0, 889, 889, 1, 518400, 0xbab197ea 0, 946, 946, 1, 518400, 0xbaf801ef -1, 189295, 189295, 1968, 1596, 0x408c726e, F=0x0 +1, 189295000, 189295000, 1968000, 1596, 0x408c726e, F=0x0 0, 956, 956, 1, 518400, 0xbab197ea -1, 191356, 191356, 1228, 1517, 0xae8c5c2b, F=0x0 +1, 191356000, 191356000, 1228000, 1517, 0xae8c5c2b, F=0x0 0, 957, 957, 1, 518400, 0x59f4e72f 0, 963, 963, 1, 518400, 0xbab197ea -1, 192640, 192640, 1763, 2506, 0xa458d6d4, F=0x0 +1, 192640000, 192640000, 1763000, 2506, 0xa458d6d4, F=0x0 0, 964, 964, 1, 518400, 0x9d5b9d69 0, 972, 972, 1, 518400, 0xbab197ea -1, 195193, 195193, 1092, 1074, 0x397ba9a8, F=0x0 +1, 195193000, 195193000, 1092000, 1074, 0x397ba9a8, F=0x0 0, 976, 976, 1, 518400, 0x923d1ce7 0, 981, 981, 1, 518400, 0xbab197ea -1, 196361, 196361, 1524, 1715, 0x695ca41e, F=0x0 +1, 196361000, 196361000, 1524000, 1715, 0x695ca41e, F=0x0 0, 982, 982, 1, 518400, 0x6e652cd2 0, 989, 989, 1, 518400, 0xbab197ea -1, 197946, 197946, 1160, 789, 0xc63a189e, F=0x0 +1, 197946000, 197946000, 1160000, 789, 0xc63a189e, F=0x0 0, 990, 990, 1, 518400, 0x25113966 0, 996, 996, 1, 518400, 0xbab197ea -1, 199230, 199230, 1627, 1846, 0xeea8c599, F=0x0 +1, 199230000, 199230000, 1627000, 1846, 0xeea8c599, F=0x0 0, 997, 997, 1, 518400, 0x2dc83609 0, 1004, 1004, 1, 518400, 0xbab197ea -1, 200924, 200924, 1763, 922, 0xd4a87222, F=0x0 +1, 200924000, 200924000, 1763000, 922, 0xd4a87222, F=0x0 0, 1005, 1005, 1, 518400, 0x90483bc6 0, 1013, 1013, 1, 518400, 0xbab197ea 0, 1053, 1053, 1, 518400, 0x3de86ab7 -1, 210600, 210600, 1831, 665, 0x55580135, F=0x0 +1, 210600000, 210600000, 1831000, 665, 0x55580135, F=0x0 0, 1062, 1062, 1, 518400, 0xbab197ea -1, 214771, 214771, 1558, 1216, 0x50d1f6c5, F=0x0 +1, 214771000, 214771000, 1558000, 1216, 0x50d1f6c5, F=0x0 0, 1074, 1074, 1, 518400, 0x8c320e68 0, 1082, 1082, 1, 518400, 0xbab197ea 0, 1128, 1128, 1, 518400, 0x81e977b2 -1, 225640, 225640, 2127, 2133, 0x670c11a5, F=0x0 +1, 225640000, 225640000, 2127000, 2133, 0x670c11a5, F=0x0 0, 1139, 1139, 1, 518400, 0xbab197ea -1, 227834, 227834, 1262, 1264, 0xc1d9fc57, F=0x0 +1, 227834000, 227834000, 1262000, 1264, 0xc1d9fc57, F=0x0 0, 1140, 1140, 1, 518400, 0xb046dd30 0, 1145, 1145, 1, 518400, 0xbab197ea