Skip to content

Commit

Permalink
avformat/utils: detect MPEG streams with faulty DTS and discard affec…
Browse files Browse the repository at this point in the history
…ted DTS

Fixes issue2.ts

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
  • Loading branch information
michaelni authored and FernetMenta committed Mar 3, 2014
1 parent 463edf9 commit 9fd89d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/ffmpeg/libavformat/avformat.h
Expand Up @@ -873,6 +873,13 @@ typedef struct AVStream {
*/
int pts_wrap_behavior;

/**
* Internal data to analyze DTS and detect faulty mpeg streams
*/
int64_t last_dts_for_order_check;
uint8_t dts_ordered;
uint8_t dts_misordered;

} AVStream;

#define AV_PROGRAM_RUNNING 1
Expand Down
25 changes: 25 additions & 0 deletions lib/ffmpeg/libavformat/utils.c
Expand Up @@ -1104,6 +1104,28 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
if (s->flags & AVFMT_FLAG_NOFILLIN)
return;

if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pkt->dts != AV_NOPTS_VALUE) {
if (pkt->dts == pkt->pts && st->last_dts_for_order_check != AV_NOPTS_VALUE) {
if (st->last_dts_for_order_check <= pkt->dts) {
st->dts_ordered++;
} else {
av_log(s, st->dts_misordered ? AV_LOG_DEBUG : AV_LOG_WARNING,
"DTS %"PRIi64" < %"PRIi64" out of order\n",
pkt->dts,
st->last_dts_for_order_check);
st->dts_misordered++;
}
if (st->dts_ordered + st->dts_misordered > 250) {
st->dts_ordered >>= 1;
st->dts_misordered >>= 1;
}
}

st->last_dts_for_order_check = pkt->dts;
if (st->dts_ordered < 8*st->dts_misordered && pkt->dts == pkt->pts)
pkt->dts = AV_NOPTS_VALUE;
}

if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
pkt->dts= AV_NOPTS_VALUE;

Expand Down Expand Up @@ -1627,6 +1649,7 @@ void ff_read_frame_flush(AVFormatContext *s)
st->parser = NULL;
}
st->last_IP_pts = AV_NOPTS_VALUE;
st->last_dts_for_order_check = AV_NOPTS_VALUE;
if(st->first_dts == AV_NOPTS_VALUE) st->cur_dts = RELATIVE_TS_BASE;
else st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */
st->reference_dts = AV_NOPTS_VALUE;
Expand Down Expand Up @@ -2407,6 +2430,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
st= ic->streams[i];
st->cur_dts= st->first_dts;
st->last_IP_pts = AV_NOPTS_VALUE;
st->last_dts_for_order_check = AV_NOPTS_VALUE;
st->reference_dts = AV_NOPTS_VALUE;
}
}
Expand Down Expand Up @@ -3390,6 +3414,7 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
/* default pts setting is MPEG-like */
avpriv_set_pts_info(st, 33, 1, 90000);
st->last_IP_pts = AV_NOPTS_VALUE;
st->last_dts_for_order_check = AV_NOPTS_VALUE;
for(i=0; i<MAX_REORDER_DELAY+1; i++)
st->pts_buffer[i]= AV_NOPTS_VALUE;
st->reference_dts = AV_NOPTS_VALUE;
Expand Down

0 comments on commit 9fd89d4

Please sign in to comment.