fftools/ffmpeg_mux: factor timestamps processing out of write_packet()

This commit is contained in:
Anton Khirnov 2023-12-14 13:51:27 +01:00
parent 3dc319587f
commit 7a7550ec28
1 changed files with 30 additions and 20 deletions

View File

@ -135,20 +135,9 @@ static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
pkt->size, *latency ? latency : "N/A"); pkt->size, *latency ? latency : "N/A");
} }
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
{ {
MuxStream *ms = ms_from_ost(ost); OutputStream *ost = &ms->ost;
AVFormatContext *s = mux->fc;
int64_t fs;
uint64_t frame_num;
int ret;
fs = filesize(s->pb);
atomic_store(&mux->last_filesize, fs);
if (fs >= mux->limit_filesize) {
ret = AVERROR_EOF;
goto fail;
}
#if FFMPEG_OPT_VSYNC_DROP #if FFMPEG_OPT_VSYNC_DROP
if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP) if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
@ -174,7 +163,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base); av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
pkt->time_base = ost->st->time_base; pkt->time_base = ost->st->time_base;
if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
if (pkt->dts != AV_NOPTS_VALUE && if (pkt->dts != AV_NOPTS_VALUE &&
pkt->pts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE &&
pkt->dts > pkt->pts) { pkt->dts > pkt->pts) {
@ -188,7 +177,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) && if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
pkt->dts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
ms->last_mux_dts != AV_NOPTS_VALUE) { ms->last_mux_dts != AV_NOPTS_VALUE) {
int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
if (pkt->dts < max) { if (pkt->dts < max) {
int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
if (exit_on_error) if (exit_on_error)
@ -197,8 +186,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
"previous: %"PRId64", current: %"PRId64"; ", "previous: %"PRId64", current: %"PRId64"; ",
ms->last_mux_dts, pkt->dts); ms->last_mux_dts, pkt->dts);
if (exit_on_error) { if (exit_on_error) {
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
av_log(ost, loglevel, "changing to %"PRId64". This may result " av_log(ost, loglevel, "changing to %"PRId64". This may result "
@ -212,14 +200,36 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
} }
ms->last_mux_dts = pkt->dts; ms->last_mux_dts = pkt->dts;
if (debug_ts)
mux_log_debug_ts(ost, pkt);
return 0;
}
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
{
MuxStream *ms = ms_from_ost(ost);
AVFormatContext *s = mux->fc;
int64_t fs;
uint64_t frame_num;
int ret;
fs = filesize(s->pb);
atomic_store(&mux->last_filesize, fs);
if (fs >= mux->limit_filesize) {
ret = AVERROR_EOF;
goto fail;
}
ret = mux_fixup_ts(mux, ms, pkt);
if (ret < 0)
goto fail;
ms->data_size_mux += pkt->size; ms->data_size_mux += pkt->size;
frame_num = atomic_fetch_add(&ost->packets_written, 1); frame_num = atomic_fetch_add(&ost->packets_written, 1);
pkt->stream_index = ost->index; pkt->stream_index = ost->index;
if (debug_ts)
mux_log_debug_ts(ost, pkt);
if (ms->stats.io) if (ms->stats.io)
enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num); enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);