Index: libs/libmythtv/avformatdecoder.cpp =================================================================== --- libs/libmythtv/avformatdecoder.cpp (revision 25760) +++ libs/libmythtv/avformatdecoder.cpp (working copy) @@ -70,7 +70,7 @@ #define MAX_AC3_FRAME_SIZE 6144 -static const bool use_reordered_opaque = false; +static const bool force_reordered_opaque = false; static const float eps = 1E-5; @@ -259,6 +259,10 @@ start_code_state(0xffffffff), lastvpts(0), lastapts(0), lastccptsu(0), + faulty_pts(0), faulty_dts(0), + pts_detected(false), + last_dts_for_fault_detection(0), + last_pts_for_fault_detection(0), using_null_videoout(use_null_videoout), video_codec_id(kCodec_NONE), no_hardware_decoders(no_hardware_decode), @@ -647,6 +651,11 @@ lastapts = 0; lastvpts = 0; lastccptsu = 0; + faulty_pts = faulty_dts = 0; + last_dts_for_fault_detection = 0; + last_pts_for_fault_detection = 0; + pts_detected = false; + av_read_frame_flush(ic); // Only reset the internal state if we're using our seeking, @@ -2717,6 +2726,10 @@ gopset = false; prevgoppos = 0; lastapts = lastvpts = lastccptsu = 0; + faulty_pts = faulty_dts = 0; + last_dts_for_fault_detection = 0; + last_pts_for_fault_detection = 0; + pts_detected = false; // fps debugging info float avFPS = normalized_fps(stream, context); @@ -2819,6 +2832,10 @@ gopset = false; prevgoppos = 0; lastapts = lastvpts = lastccptsu = 0; + faulty_pts = faulty_dts = 0; + last_dts_for_fault_detection = 0; + last_pts_for_fault_detection = 0; + pts_detected = false; // fps debugging info float avFPS = normalized_fps(stream, context); @@ -2991,12 +3008,28 @@ return false; } - if ((use_reordered_opaque || pkt->dts == (int64_t)AV_NOPTS_VALUE) && + if(pkt->dts != (int64_t)AV_NOPTS_VALUE){ + faulty_dts += pkt->dts <= last_dts_for_fault_detection; + last_dts_for_fault_detection = pkt->dts; + } + if(mpa_pic.reordered_opaque != (int64_t)AV_NOPTS_VALUE){ + faulty_pts += mpa_pic.reordered_opaque <= last_pts_for_fault_detection; + last_pts_for_fault_detection = mpa_pic.reordered_opaque; + pts_detected = true; + } + + // Prefer reordered_opaque (PTS) timestamps if they are not faulty and exist, + // also use fixups for missing PTS instead of selecting DTS to avoid + // oscillating between PTS and DTS. + // Select DTS timestamps if PTS is more faulty or never detected + if ((force_reordered_opaque || faulty_pts <= faulty_dts || + pkt->dts == (int64_t)AV_NOPTS_VALUE) && mpa_pic.reordered_opaque != (int64_t)AV_NOPTS_VALUE) { pts = (long long)mpa_pic.reordered_opaque; } - else if (pkt->dts != (int64_t)AV_NOPTS_VALUE) + else if ((faulty_dts < faulty_pts || !pts_detected) && + pkt->dts != (int64_t)AV_NOPTS_VALUE) { pts = (long long)pkt->dts; } Index: libs/libmythtv/avformatdecoder.h =================================================================== --- libs/libmythtv/avformatdecoder.h (revision 25760) +++ libs/libmythtv/avformatdecoder.h (working copy) @@ -270,6 +270,12 @@ long long lastapts; long long lastccptsu; + int64_t faulty_pts; + int64_t faulty_dts; + int64_t last_dts_for_fault_detection; + int64_t last_pts_for_fault_detection; + bool pts_detected; + bool using_null_videoout; MythCodecID video_codec_id; bool no_hardware_decoders;