History

fftools/ffmpeg: use a separate variable for discontinuity offset

This will allow to move normal offset handling to demuxer thread, since discontinuities currently have to be processed in the main thread, as the code uses some decoder-produced values.

Anton Khirnov committed 4 years ago

Commit 3b2bece1 parent ca38fe92 files · +32 −11Browse files

Unified · Split

2 files
  1. fftools/ffmpeg.c +28 −11
  2. fftools/ffmpeg.h +4 −0
fftools/ffmpeg.c+28−11
@@ -3694,8 +3694,8 @@ static void decode_flush(InputFile *ifile) · 3693 unmodified lines
36943694 }
36953695 }
36963696
3697−static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
3698− AVPacket *pkt)
3697+static void ts_discontinuity_detect(InputFile *ifile, InputStream *ist,
3698+ AVPacket *pkt)
36993699 {
37003700 const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
37013701 int disable_discontinuity_correction = copy_ts;
@@ -3716,13 +3716,13 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist, · 14 unmodified lines
37163716 if (fmt_is_discont) {
37173717 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
37183718 pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) {
3719− ifile->ts_offset -= delta;
3719+ ifile->ts_offset_discont -= delta;
37203720 av_log(NULL, AV_LOG_DEBUG,
37213721 "timestamp discontinuity for stream #%d:%d "
37223722 "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
37233723 ist->file_index, ist->st->index, ist->st->id,
37243724 av_get_media_type_string(ist->st->codecpar->codec_type),
3725− delta, ifile->ts_offset);
3725+ delta, ifile->ts_offset_discont);
37263726 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
37273727 if (pkt->pts != AV_NOPTS_VALUE)
37283728 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
@@ -3745,10 +3745,10 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist, · 16 unmodified lines
37453745 fmt_is_discont && ifile->last_ts != AV_NOPTS_VALUE) {
37463746 int64_t delta = pkt_dts - ifile->last_ts;
37473747 if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
3748− ifile->ts_offset -= delta;
3748+ ifile->ts_offset_discont -= delta;
37493749 av_log(NULL, AV_LOG_DEBUG,
37503750 "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
3751− delta, ifile->ts_offset);
3751+ delta, ifile->ts_offset_discont);
37523752 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
37533753 if (pkt->pts != AV_NOPTS_VALUE)
37543754 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
@@ -3758,6 +3758,26 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist, · 3 unmodified lines
37583758 ifile->last_ts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
37593759 }
37603760
3761+static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
3762+ AVPacket *pkt)
3763+{
3764+ int64_t offset = av_rescale_q(ifile->ts_offset_discont, AV_TIME_BASE_Q,
3765+ ist->st->time_base);
3766+
3767+ // apply previously-detected timestamp-discontinuity offset
3768+ // (to all streams, not just audio/video)
3769+ if (pkt->dts != AV_NOPTS_VALUE)
3770+ pkt->dts += offset;
3771+ if (pkt->pts != AV_NOPTS_VALUE)
3772+ pkt->pts += offset;
3773+
3774+ // detect timestamp discontinuities for audio/video
3775+ if ((ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
3776+ ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
3777+ pkt->dts != AV_NOPTS_VALUE)
3778+ ts_discontinuity_detect(ifile, ist, pkt);
3779+}
3780+
37613781 /*
37623782 * Return
37633783 * - 0 -- one packet was read and processed
@@ -3898,11 +3918,8 @@ static int process_input(int file_index) · 134 unmodified lines
38983918 if (pkt->dts != AV_NOPTS_VALUE)
38993919 pkt->dts += duration;
39003920
3901− // detect and correct timestamp discontinuities for audio/video
3902− if ((ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
3903− ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
3904− pkt->dts != AV_NOPTS_VALUE)
3905− ts_discontinuity_process(ifile, ist, pkt);
3921+ // detect and try to correct for timestamp discontinuities
3922+ ts_discontinuity_process(ifile, ist, pkt);
39063923
39073924 if (debug_ts) {
39083925 av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
fftools/ffmpeg.h+4−0
@@ -427,6 +427,10 @@ typedef struct InputFile { · 426 unmodified lines
427427 int input_sync_ref;
428428
429429 int64_t ts_offset;
430+ /**
431+ * Extra timestamp offset added by discontinuity handling.
432+ */
433+ int64_t ts_offset_discont;
430434 int64_t last_ts;
431435 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
432436 int64_t recording_time;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t