summaryrefslogtreecommitdiff
path: root/fftools/ffmpeg.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2023-04-27 14:11:34 +0200
committerAnton Khirnov <anton@khirnov.net>2023-05-15 11:03:15 +0200
commit2b99c6bfd57d00e7f06cda09d60db9dbefaa117e (patch)
tree03c1567b72c55abf0529f30c3d36e9839870065d /fftools/ffmpeg.c
parent09c686788e3be1f343ddf64be291e6df6946323f (diff)
downloadffmpeg-2b99c6bfd57d00e7f06cda09d60db9dbefaa117e.tar.gz
fftools/ffmpeg: move post-demux packet processing to ffmpeg_demux
That is a more appropriate place for this code and will allow hiding more of InputStream. The value of repeat_pict extracted from libavformat internal parser no longer needs to be trasmitted outside of the demuxing thread. Move readrate handling to the demuxer thread. This has to be done in the same commit, since it reads InputStream.dts,nb_packets, which are now set in the demuxer thread.
Diffstat (limited to 'fftools/ffmpeg.c')
-rw-r--r--fftools/ffmpeg.c188
1 files changed, 0 insertions, 188 deletions
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 2a39d767cd..ee92587798 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1772,154 +1772,6 @@ static void decode_flush(InputFile *ifile)
}
}
-static void ts_discontinuity_detect(InputFile *ifile, InputStream *ist,
- AVPacket *pkt)
-{
- const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
- int disable_discontinuity_correction = copy_ts;
- int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
- AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
-
- if (copy_ts && ist->next_dts != AV_NOPTS_VALUE &&
- fmt_is_discont && ist->st->pts_wrap_bits < 60) {
- int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
- pkt->time_base, AV_TIME_BASE_Q,
- AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
- if (FFABS(wrap_dts - ist->next_dts) < FFABS(pkt_dts - ist->next_dts)/10)
- disable_discontinuity_correction = 0;
- }
-
- if (ist->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
- int64_t delta = pkt_dts - ist->next_dts;
- if (fmt_is_discont) {
- if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
- pkt_dts + AV_TIME_BASE/10 < ist->dts) {
- ifile->ts_offset_discont -= delta;
- av_log(NULL, AV_LOG_WARNING,
- "timestamp discontinuity for stream #%d:%d "
- "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
- ist->file_index, ist->st->index, ist->st->id,
- av_get_media_type_string(ist->par->codec_type),
- delta, ifile->ts_offset_discont);
- pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
- if (pkt->pts != AV_NOPTS_VALUE)
- pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
- }
- } else {
- if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
- av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt->dts, ist->next_dts, pkt->stream_index);
- pkt->dts = AV_NOPTS_VALUE;
- }
- if (pkt->pts != AV_NOPTS_VALUE){
- int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
- delta = pkt_pts - ist->next_dts;
- if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
- av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt->pts, ist->next_dts, pkt->stream_index);
- pkt->pts = AV_NOPTS_VALUE;
- }
- }
- }
- } else if (ist->next_dts == AV_NOPTS_VALUE && !copy_ts &&
- fmt_is_discont && ifile->last_ts != AV_NOPTS_VALUE) {
- int64_t delta = pkt_dts - ifile->last_ts;
- if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
- ifile->ts_offset_discont -= delta;
- av_log(NULL, AV_LOG_DEBUG,
- "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
- delta, ifile->ts_offset_discont);
- pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
- if (pkt->pts != AV_NOPTS_VALUE)
- pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
- }
- }
-
- ifile->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
-}
-
-static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
- AVPacket *pkt)
-{
- int64_t offset = av_rescale_q(ifile->ts_offset_discont, AV_TIME_BASE_Q,
- pkt->time_base);
-
- // apply previously-detected timestamp-discontinuity offset
- // (to all streams, not just audio/video)
- if (pkt->dts != AV_NOPTS_VALUE)
- pkt->dts += offset;
- if (pkt->pts != AV_NOPTS_VALUE)
- pkt->pts += offset;
-
- // detect timestamp discontinuities for audio/video
- if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
- ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
- pkt->dts != AV_NOPTS_VALUE)
- ts_discontinuity_detect(ifile, ist, pkt);
-}
-
-static void ist_dts_update(InputStream *ist, AVPacket *pkt)
-{
- const AVCodecParameters *par = ist->par;
-
- if (!ist->saw_first_ts) {
- ist->first_dts =
- ist->dts = ist->st->avg_frame_rate.num ? - ist->par->video_delay * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
- if (pkt->pts != AV_NOPTS_VALUE) {
- ist->first_dts =
- ist->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
- }
- ist->saw_first_ts = 1;
- }
-
- if (ist->next_dts == AV_NOPTS_VALUE)
- ist->next_dts = ist->dts;
-
- if (pkt->dts != AV_NOPTS_VALUE)
- ist->next_dts = ist->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
-
- ist->dts = ist->next_dts;
- switch (par->codec_type) {
- case AVMEDIA_TYPE_AUDIO:
- av_assert1(pkt->duration >= 0);
- if (par->sample_rate) {
- ist->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
- par->sample_rate;
- } else {
- ist->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
- }
- break;
- case AVMEDIA_TYPE_VIDEO:
- if (ist->framerate.num) {
- // TODO: Remove work-around for c99-to-c89 issue 7
- AVRational time_base_q = AV_TIME_BASE_Q;
- int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
- ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
- } else if (pkt->duration) {
- ist->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
- } else if (ist->par->framerate.num != 0) {
- int fields = (ist->codec_desc &&
- (ist->codec_desc->props & AV_CODEC_PROP_FIELDS)) ?
- ist->last_pkt_repeat_pict + 1 : 2;
- AVRational field_rate = av_mul_q(ist->par->framerate,
- (AVRational){ 2, 1 });
-
- ist->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
- }
- break;
- }
-
- av_assert0(!pkt->opaque_ref);
- if (ist->streamcopy_needed) {
- DemuxPktData *pd;
-
- pkt->opaque_ref = av_buffer_allocz(sizeof(*pd));
- if (!pkt->opaque_ref)
- report_and_exit(AVERROR(ENOMEM));
- pd = (DemuxPktData*)pkt->opaque_ref->data;
-
- pd->dts_est = ist->dts;
- }
-}
-
/*
* Return
* - 0 -- one packet was read and processed
@@ -1979,46 +1831,6 @@ static int process_input(int file_index)
ist = ifile->streams[pkt->stream_index];
- ist->data_size += pkt->size;
- ist->nb_packets++;
-
- /* add the stream-global side data to the first packet */
- if (ist->nb_packets == 1) {
- for (i = 0; i < ist->st->nb_side_data; i++) {
- AVPacketSideData *src_sd = &ist->st->side_data[i];
- uint8_t *dst_data;
-
- if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
- continue;
-
- if (av_packet_get_side_data(pkt, src_sd->type, NULL))
- continue;
-
- dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
- if (!dst_data)
- report_and_exit(AVERROR(ENOMEM));
-
- memcpy(dst_data, src_sd->data, src_sd->size);
- }
- }
-
- // detect and try to correct for timestamp discontinuities
- ts_discontinuity_process(ifile, ist, pkt);
-
- // update estimated/predicted dts
- ist_dts_update(ist, pkt);
-
- if (debug_ts) {
- av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%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",
- ifile->index, pkt->stream_index,
- av_get_media_type_string(ist->par->codec_type),
- av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
- av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
- av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
- av_ts2str(input_files[ist->file_index]->ts_offset),
- av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
- }
-
sub2video_heartbeat(ist, pkt->pts);
process_input_packet(ist, pkt, 0);