diff --git a/mythtv/libs/libmythtv/mythplayer.cpp b/mythtv/libs/libmythtv/mythplayer.cpp
index 5b13fbc..8a4c621 100644
a
|
b
|
MythPlayer::MythPlayer(bool muted) |
237 | 237 | // Audio and video synchronization stuff |
238 | 238 | videosync(NULL), avsync_delay(0), |
239 | 239 | avsync_adjustment(0), avsync_avg(0), |
| 240 | avsync_predictor(0), avsync_predictor_enabled(false), |
240 | 241 | refreshrate(0), |
241 | 242 | lastsync(false), repeat_delay(0), |
242 | 243 | // Time Code stuff |
… |
… |
void MythPlayer::SetVideoParams(int width, int height, double fps, |
872 | 873 | video_frame_rate = fps; |
873 | 874 | float temp_speed = (play_speed == 0.0f) ? |
874 | 875 | audio.GetStretchFactor() : play_speed; |
875 | | frame_interval = (int)(1000000.0f / video_frame_rate / temp_speed); |
| 876 | SetFrameInterval(kScan_Progressive, 1.0 / (video_frame_rate * temp_speed)); |
876 | 877 | } |
877 | 878 | |
878 | 879 | if (videoOutput) |
… |
… |
int MythPlayer::NextCaptionTrack(int mode) |
1613 | 1614 | return NextCaptionTrack(nextmode); |
1614 | 1615 | } |
1615 | 1616 | |
| 1617 | void MythPlayer::SetFrameInterval(FrameScanType scan, double frame_period) |
| 1618 | { |
| 1619 | frame_interval = (int)(1000000.0f * frame_period + 0.5f); |
| 1620 | if (!avsync_predictor_enabled) |
| 1621 | avsync_predictor = 0; |
| 1622 | avsync_predictor_enabled = false; |
| 1623 | |
| 1624 | VERBOSE(VB_PLAYBACK, LOC + QString("SetFrameInterval ps:%1 scan:%2") |
| 1625 | .arg(play_speed).arg(scan) |
| 1626 | ); |
| 1627 | if (play_speed < 1 || play_speed > 2 || refreshrate <= 0) |
| 1628 | return; |
| 1629 | |
| 1630 | avsync_predictor_enabled = ((frame_interval-(frame_interval/200)) < refreshrate); |
| 1631 | } |
| 1632 | |
| 1633 | void MythPlayer::ResetAVSync(void) |
| 1634 | { |
| 1635 | avsync_avg = 0; |
| 1636 | if (!avsync_predictor_enabled || avsync_predictor >= refreshrate) |
| 1637 | avsync_predictor = 0; |
| 1638 | prevtc = 0; |
| 1639 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V sync reset"); |
| 1640 | } |
| 1641 | |
1616 | 1642 | void MythPlayer::InitAVSync(void) |
1617 | 1643 | { |
1618 | 1644 | videosync->Start(); |
… |
… |
void MythPlayer::InitAVSync(void) |
1636 | 1662 | .arg(1000000.0 / frame_interval, 0, 'f', 3); |
1637 | 1663 | VERBOSE(VB_PLAYBACK, LOC + msg); |
1638 | 1664 | |
| 1665 | SetFrameInterval(m_scan, 1.0 / (video_frame_rate * play_speed)); |
| 1666 | |
1639 | 1667 | // try to get preferential scheduling, but ignore if we fail to. |
1640 | 1668 | myth_nice(-19); |
1641 | 1669 | } |
1642 | 1670 | } |
1643 | 1671 | |
| 1672 | int64_t MythPlayer::AVSyncGetAudiotime(void) |
| 1673 | { |
| 1674 | int64_t currentaudiotime = 0; |
| 1675 | if (normal_speed) |
| 1676 | { |
| 1677 | currentaudiotime = audio.GetAudioTime(); |
| 1678 | } |
| 1679 | return currentaudiotime; |
| 1680 | } |
| 1681 | |
1644 | 1682 | #define MAXDIVERGE 3.0f |
1645 | 1683 | #define DIVERGELIMIT 30.0f |
1646 | 1684 | void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1647 | 1685 | { |
1648 | 1686 | int repeat_pict = 0; |
1649 | | int64_t timecode = audio.GetAudioTime(); |
| 1687 | int64_t timecode = 0; |
1650 | 1688 | |
1651 | 1689 | if (buffer) |
1652 | 1690 | { |
1653 | 1691 | repeat_pict = buffer->repeat_pict; |
1654 | 1692 | timecode = buffer->timecode; |
1655 | 1693 | } |
| 1694 | else |
| 1695 | timecode = audio.GetAudioTime(); |
1656 | 1696 | |
1657 | 1697 | float diverge = 0.0f; |
1658 | 1698 | int frameDelay = m_double_framerate ? frame_interval / 2 : frame_interval; |
| 1699 | int vsync_delay_clock = 0; |
| 1700 | int64_t currentaudiotime = 0; |
1659 | 1701 | |
1660 | 1702 | // attempt to reduce fps for standalone PIP |
1661 | 1703 | if (player_ctx->IsPIP() && framesPlayed % 2) |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1685 | 1727 | if (kScan_Detect == m_scan || kScan_Ignore == m_scan) |
1686 | 1728 | ps = kScan_Progressive; |
1687 | 1729 | |
| 1730 | bool dropframe = false; |
| 1731 | QString dbg; |
| 1732 | |
| 1733 | if (avsync_predictor_enabled) // && !prebuffering) |
| 1734 | { |
| 1735 | avsync_predictor += frame_interval; |
| 1736 | if (avsync_predictor >= refreshrate) |
| 1737 | { |
| 1738 | int refreshperiodsinframe = avsync_predictor/refreshrate; |
| 1739 | avsync_predictor -= refreshrate * refreshperiodsinframe; |
| 1740 | } |
| 1741 | else |
| 1742 | { |
| 1743 | dropframe = true; |
| 1744 | dbg = "A/V predict drop frame, "; |
| 1745 | } |
| 1746 | } |
| 1747 | |
1688 | 1748 | if (diverge < -MAXDIVERGE) |
1689 | 1749 | { |
| 1750 | dropframe = true; |
1690 | 1751 | // If video is way behind of audio, adjust for it... |
1691 | | QString dbg = QString("Video is %1 frames behind audio (too slow), ") |
| 1752 | dbg = QString("Video is %1 frames behind audio (too slow), ") |
1692 | 1753 | .arg(-diverge); |
| 1754 | } |
1693 | 1755 | |
| 1756 | if (dropframe) |
| 1757 | { |
1694 | 1758 | // Reset A/V Sync |
1695 | 1759 | lastsync = true; |
1696 | 1760 | |
| 1761 | currentaudiotime = AVSyncGetAudiotime(); |
1697 | 1762 | if (!using_null_videoout && |
1698 | 1763 | videoOutput->hasHWAcceleration() && |
1699 | 1764 | !videoOutput->IsSyncLocked()) |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1720 | 1785 | osdLock.lock(); |
1721 | 1786 | videoOutput->PrepareFrame(buffer, ps, osd); |
1722 | 1787 | osdLock.unlock(); |
1723 | | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString("AVSync waitforframe %1 %2") |
| 1788 | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, |
| 1789 | LOC + QString("AVSync waitforframe %1 %2") |
1724 | 1790 | .arg(avsync_adjustment).arg(m_double_framerate)); |
1725 | | videosync->WaitForFrame(frameDelay + avsync_adjustment + repeat_delay); |
1726 | | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, "AVSync show"); |
| 1791 | vsync_delay_clock = videosync->WaitForFrame |
| 1792 | (frameDelay + avsync_adjustment + repeat_delay); |
| 1793 | currentaudiotime = AVSyncGetAudiotime(); |
| 1794 | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + "AVSync show"); |
1727 | 1795 | videoOutput->Show(ps); |
1728 | 1796 | |
1729 | 1797 | if (videoOutput->IsErrored()) |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1751 | 1819 | videoOutput->PrepareFrame(buffer, ps, osd); |
1752 | 1820 | osdLock.unlock(); |
1753 | 1821 | // Display the second field |
1754 | | videosync->WaitForFrame(frameDelay + avsync_adjustment); |
| 1822 | //videosync->AdvanceTrigger(); |
| 1823 | vsync_delay_clock = videosync->WaitForFrame(frameDelay + avsync_adjustment); |
1755 | 1824 | videoOutput->Show(ps); |
1756 | 1825 | } |
1757 | 1826 | |
1758 | 1827 | repeat_delay = frame_interval * repeat_pict * 0.5; |
1759 | 1828 | |
1760 | 1829 | if (repeat_delay) |
1761 | | VERBOSE(VB_TIMESTAMP, QString("A/V repeat_pict, adding %1 repeat " |
| 1830 | VERBOSE(VB_TIMESTAMP, LOC + QString("A/V repeat_pict, adding %1 repeat " |
1762 | 1831 | "delay").arg(repeat_delay)); |
1763 | 1832 | } |
1764 | 1833 | else |
1765 | 1834 | { |
1766 | | videosync->WaitForFrame(frameDelay); |
| 1835 | vsync_delay_clock = videosync->WaitForFrame(frameDelay); |
| 1836 | currentaudiotime = AVSyncGetAudiotime(); |
1767 | 1837 | } |
1768 | 1838 | |
1769 | 1839 | if (output_jmeter) |
1770 | | output_jmeter->RecordCycleTime(); |
| 1840 | { |
| 1841 | if (output_jmeter->RecordCycleTime()) |
| 1842 | { |
| 1843 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V avsync_delay: %1, " |
| 1844 | "avsync_avg: %2") |
| 1845 | .arg(avsync_delay / 1000).arg(avsync_avg / 1000) |
| 1846 | ); |
| 1847 | } |
| 1848 | } |
1771 | 1849 | |
1772 | 1850 | avsync_adjustment = 0; |
1773 | 1851 | |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1775 | 1853 | { |
1776 | 1854 | // If audio is way behind of video, adjust for it... |
1777 | 1855 | // by cutting the frame rate in half for the length of this frame |
1778 | | avsync_adjustment = refreshrate; |
| 1856 | //avsync_adjustment = refreshrate; |
| 1857 | avsync_adjustment = frame_interval; |
1779 | 1858 | lastsync = true; |
1780 | 1859 | VERBOSE(VB_PLAYBACK, LOC + |
1781 | 1860 | QString("Video is %1 frames ahead of audio,\n" |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1784 | 1863 | |
1785 | 1864 | if (audio.HasAudioOut() && normal_speed) |
1786 | 1865 | { |
| 1866 | // must be sampled here due to Show delays |
1787 | 1867 | int64_t currentaudiotime = audio.GetAudioTime(); |
1788 | | VERBOSE(VB_TIMESTAMP, QString( |
| 1868 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString( |
1789 | 1869 | "A/V timecodes audio %1 video %2 frameinterval %3 " |
1790 | | "avdel %4 avg %5 tcoffset %6") |
| 1870 | "avdel %4 avg %5 tcoffset %6" |
| 1871 | " avp %7 avpen %8" |
| 1872 | " avdc %9" |
| 1873 | ) |
1791 | 1874 | .arg(currentaudiotime) |
1792 | 1875 | .arg(timecode) |
1793 | 1876 | .arg(frame_interval) |
1794 | | .arg(timecode - currentaudiotime) |
| 1877 | .arg(timecode - currentaudiotime - (int)(vsync_delay_clock*audio.GetStretchFactor()+500)/1000) |
1795 | 1878 | .arg(avsync_avg) |
1796 | 1879 | .arg(tc_wrap[TC_AUDIO]) |
| 1880 | .arg(avsync_predictor) |
| 1881 | .arg(avsync_predictor_enabled) |
| 1882 | .arg(vsync_delay_clock) |
1797 | 1883 | ); |
1798 | 1884 | if (currentaudiotime != 0 && timecode != 0) |
1799 | 1885 | { // currentaudiotime == 0 after a seek |
1800 | 1886 | // The time at the start of this frame (ie, now) is given by |
1801 | 1887 | // last->timecode |
1802 | | int delta = (int)((timecode - prevtc)/play_speed) - (frame_interval / 1000); |
1803 | | prevtc = timecode; |
1804 | | //cerr << delta << " "; |
1805 | | |
1806 | | // If the timecode is off by a frame (dropped frame) wait to sync |
1807 | | if (delta > (int) frame_interval / 1200 && |
1808 | | delta < (int) frame_interval / 1000 * 3 && |
1809 | | prevrp == 0) |
| 1888 | if (prevtc != 0) |
1810 | 1889 | { |
1811 | | // wait an extra frame interval |
1812 | | avsync_adjustment += frame_interval; |
| 1890 | int delta = (int)((timecode - prevtc)/play_speed) - (frame_interval / 1000); |
| 1891 | // If the timecode is off by a frame (dropped frame) wait to sync |
| 1892 | if (delta > (int) frame_interval / 1200 && |
| 1893 | delta < (int) frame_interval / 1000 * 3 && |
| 1894 | prevrp == 0) |
| 1895 | { |
| 1896 | // wait an extra frame interval |
| 1897 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V delay %1").arg(delta)); |
| 1898 | avsync_adjustment += frame_interval; |
| 1899 | //videosync->AdvanceTrigger(); |
| 1900 | //if (m_double_framerate) |
| 1901 | // videosync->AdvanceTrigger(); |
| 1902 | } |
1813 | 1903 | } |
| 1904 | prevtc = timecode; |
1814 | 1905 | prevrp = repeat_pict; |
1815 | 1906 | |
1816 | | avsync_delay = (timecode - currentaudiotime) * 1000;//usec |
| 1907 | avsync_delay = (timecode - currentaudiotime) * 1000 - (int)(vsync_delay_clock*audio.GetStretchFactor()); //usec |
1817 | 1908 | // prevents major jitter when pts resets during dvd title |
1818 | 1909 | if (avsync_delay > 2000000 && limit_delay) |
1819 | 1910 | avsync_delay = 90000; |
1820 | 1911 | avsync_avg = (avsync_delay + (avsync_avg * 3)) / 4; |
1821 | 1912 | |
| 1913 | int avsync_used = avsync_avg; |
| 1914 | if (labs(avsync_used) > labs(avsync_delay)) |
| 1915 | avsync_used = avsync_delay; |
| 1916 | |
1822 | 1917 | /* If the audio time codes and video diverge, shift |
1823 | 1918 | the video by one interlaced field (1/2 frame) */ |
1824 | 1919 | if (!lastsync) |
1825 | 1920 | { |
1826 | | if (avsync_avg > frame_interval * 3 / 2) |
| 1921 | if (avsync_used > refreshrate) |
1827 | 1922 | { |
1828 | 1923 | avsync_adjustment += refreshrate; |
1829 | | lastsync = true; |
| 1924 | //lastsync = true; |
| 1925 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V avg high extend"); |
1830 | 1926 | } |
1831 | | else if (avsync_avg < 0 - frame_interval * 3 / 2) |
| 1927 | else if (avsync_used < 0 - refreshrate) |
1832 | 1928 | { |
1833 | 1929 | avsync_adjustment -= refreshrate; |
1834 | | lastsync = true; |
| 1930 | //lastsync = true; |
| 1931 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V avg high skip"); |
1835 | 1932 | } |
1836 | 1933 | } |
1837 | 1934 | else |
… |
… |
void MythPlayer::AVSync(VideoFrame *buffer, bool limit_delay) |
1839 | 1936 | } |
1840 | 1937 | else |
1841 | 1938 | { |
1842 | | avsync_avg = 0; |
| 1939 | ResetAVSync(); |
1843 | 1940 | } |
1844 | 1941 | } |
| 1942 | else |
| 1943 | { |
| 1944 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V no sync proc ns:%1").arg(normal_speed)); |
| 1945 | } |
1845 | 1946 | } |
1846 | 1947 | |
1847 | 1948 | void MythPlayer::RefreshPauseFrame(void) |
… |
… |
void MythPlayer::ClearAfterSeek(bool clearvideobuffers) |
3489 | 3590 | commBreakMap.SetTracker(framesPlayed); |
3490 | 3591 | commBreakMap.ResetLastSkip(); |
3491 | 3592 | needNewPauseFrame = true; |
| 3593 | ResetAVSync(); |
3492 | 3594 | } |
3493 | 3595 | |
3494 | 3596 | void MythPlayer::SetPlayerInfo(TV *tv, QWidget *widget, |
diff --git a/mythtv/libs/libmythtv/mythplayer.h b/mythtv/libs/libmythtv/mythplayer.h
index 8090e35..f55c8f9 100644
a
|
b
|
class MPUBLIC MythPlayer |
515 | 515 | void WrapTimecode(int64_t &timecode, TCTypes tc_type); |
516 | 516 | void InitAVSync(void); |
517 | 517 | virtual void AVSync(VideoFrame *buffer, bool limit_delay = false); |
| 518 | void ResetAVSync(void); |
| 519 | int64_t AVSyncGetAudiotime(void); |
| 520 | void SetFrameInterval(FrameScanType scan, double speed); |
518 | 521 | void FallbackDeint(void); |
519 | 522 | void CheckExtraAudioDecode(void); |
520 | 523 | |
… |
… |
class MPUBLIC MythPlayer |
703 | 706 | int avsync_delay; |
704 | 707 | int avsync_adjustment; |
705 | 708 | int avsync_avg; |
| 709 | int avsync_predictor; |
| 710 | bool avsync_predictor_enabled; |
706 | 711 | int refreshrate; |
707 | 712 | bool lastsync; |
708 | 713 | bool decode_extra_audio; |
diff --git a/mythtv/libs/libmythtv/vsync.cpp b/mythtv/libs/libmythtv/vsync.cpp
index 1f08fc5..75528bc 100644
a
|
b
|
VideoSync::VideoSync(VideoOutput *video_output, |
130 | 130 | bool halve_frame_interval) : |
131 | 131 | m_video_output(video_output), m_frame_interval(frameint), |
132 | 132 | m_refresh_interval(refreshint), m_interlaced(halve_frame_interval), |
133 | | m_delay(-1) |
| 133 | m_nexttrigger(0), |
| 134 | m_delay(-1), |
| 135 | m_synchronous(false) |
134 | 136 | { |
135 | | bzero(&m_nexttrigger, sizeof(m_nexttrigger)); |
| 137 | int fieldint = frameint; |
| 138 | if (halve_frame_interval) |
| 139 | fieldint /= 2; |
| 140 | double sync_factor = fieldint * 2.0f / refreshint; |
| 141 | sync_factor = sync_factor - round(sync_factor); |
| 142 | m_synchronous = (sync_factor >= -0.005) && (sync_factor <= 0.005); |
| 143 | VERBOSE(VB_PLAYBACK, LOC + QString("Set video sync frame interval to %1 (synced:%2)") |
| 144 | .arg(m_frame_interval).arg(m_synchronous)); |
136 | 145 | } |
137 | 146 | |
138 | | void VideoSync::Start(void) |
| 147 | int64_t GetTime(void) |
139 | 148 | { |
140 | | gettimeofday(&m_nexttrigger, NULL); // now |
| 149 | struct timeval now_tv; |
| 150 | gettimeofday(&now_tv, NULL); // now |
| 151 | return now_tv.tv_sec * 1000000LL + now_tv.tv_usec; |
141 | 152 | } |
142 | 153 | |
143 | | void VideoSync::OffsetTimeval(struct timeval& tv, int offset) |
| 154 | void VideoSync::Start(void) |
144 | 155 | { |
145 | | tv.tv_usec += offset; |
146 | | while (tv.tv_usec > 999999) |
147 | | { |
148 | | tv.tv_sec++; |
149 | | tv.tv_usec -= 1000000; |
150 | | } |
151 | | while (tv.tv_usec < 0) |
152 | | { |
153 | | tv.tv_sec--; |
154 | | tv.tv_usec += 1000000; |
155 | | } |
| 156 | m_nexttrigger = GetTime(); |
156 | 157 | } |
157 | 158 | |
158 | 159 | /** \fn VideoSync::CalcDelay() |
… |
… |
void VideoSync::OffsetTimeval(struct timeval& tv, int offset) |
168 | 169 | */ |
169 | 170 | int VideoSync::CalcDelay() |
170 | 171 | { |
171 | | struct timeval now; |
172 | | gettimeofday(&now, NULL); |
| 172 | int64_t now = GetTime(); |
173 | 173 | //cout << "CalcDelay: next: " << timeval_str(m_nexttrigger) << " now " |
174 | 174 | // << timeval_str(now) << endl; |
175 | 175 | |
176 | | int ret_val = (m_nexttrigger.tv_sec - now.tv_sec) * 1000000 + |
177 | | (m_nexttrigger.tv_usec - now.tv_usec); |
| 176 | int ret_val = m_nexttrigger - now; |
178 | 177 | |
179 | 178 | //cout << "delay " << ret_val << endl; |
180 | 179 | |
… |
… |
int VideoSync::CalcDelay() |
186 | 185 | ret_val = m_frame_interval * 4; |
187 | 186 | |
188 | 187 | // set nexttrigger to our new target time |
189 | | m_nexttrigger.tv_sec = now.tv_sec; |
190 | | m_nexttrigger.tv_usec = now.tv_usec; |
191 | | OffsetTimeval(m_nexttrigger, ret_val); |
| 188 | m_nexttrigger = now; |
| 189 | m_nexttrigger += ret_val; |
192 | 190 | } |
193 | 191 | |
194 | | if (ret_val < -m_frame_interval) |
| 192 | if ((ret_val < -m_frame_interval) && (m_frame_interval >= m_refresh_interval)) |
195 | 193 | { |
196 | 194 | ret_val = -m_frame_interval; |
197 | 195 | |
198 | 196 | // set nexttrigger to our new target time |
199 | | m_nexttrigger.tv_sec = now.tv_sec; |
200 | | m_nexttrigger.tv_usec = now.tv_usec; |
201 | | OffsetTimeval(m_nexttrigger, ret_val); |
| 197 | m_nexttrigger = now; |
| 198 | m_nexttrigger += ret_val; |
202 | 199 | } |
203 | 200 | |
204 | 201 | return ret_val; |
… |
… |
int VideoSync::CalcDelay() |
215 | 212 | void VideoSync::KeepPhase() |
216 | 213 | { |
217 | 214 | // cerr << m_delay << endl; |
218 | | if (m_delay < -(m_refresh_interval/2)) |
219 | | OffsetTimeval(m_nexttrigger, 200); |
220 | | else if (m_delay > -500) |
221 | | OffsetTimeval(m_nexttrigger, -2000); |
| 215 | if (m_synchronous) |
| 216 | { |
| 217 | if (m_delay < -(m_refresh_interval - 500)) |
| 218 | m_nexttrigger += 200; |
| 219 | else if (m_delay > -500) |
| 220 | m_nexttrigger += -2000; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | if (m_delay < -(m_refresh_interval + 500)) |
| 225 | m_nexttrigger += 200; |
| 226 | else if (m_delay >= 0) |
| 227 | m_nexttrigger += -2000; |
| 228 | } |
222 | 229 | } |
223 | 230 | |
224 | 231 | #ifndef _WIN32 |
… |
… |
void DRMVideoSync::Start(void) |
308 | 315 | VideoSync::Start(); |
309 | 316 | } |
310 | 317 | |
311 | | void DRMVideoSync::WaitForFrame(int sync_delay) |
| 318 | int DRMVideoSync::WaitForFrame(int sync_delay) |
312 | 319 | { |
313 | 320 | // Offset for externally-provided A/V sync delay |
314 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 321 | m_nexttrigger += sync_delay; |
315 | 322 | |
316 | 323 | m_delay = CalcDelay(); |
317 | 324 | //cerr << "WaitForFrame at : " << m_delay; |
… |
… |
void DRMVideoSync::WaitForFrame(int sync_delay) |
331 | 338 | if (m_delay > 0) |
332 | 339 | { |
333 | 340 | // Wait for any remaining retrace intervals in one pass. |
334 | | int n = m_delay / m_refresh_interval + 1; |
| 341 | int n = (m_delay + m_refresh_interval - 1) / m_refresh_interval; |
335 | 342 | |
336 | 343 | drm_wait_vblank_t blank; |
337 | 344 | blank.request.type = DRM_VBLANK_RELATIVE; |
… |
… |
void DRMVideoSync::WaitForFrame(int sync_delay) |
341 | 348 | //cerr << "Wait " << n << " intervals. Count " << blank.request.sequence; |
342 | 349 | //cerr << " Delay " << m_delay << endl; |
343 | 350 | } |
| 351 | return m_delay; |
344 | 352 | |
345 | 353 | KeepPhase(); |
346 | 354 | } |
… |
… |
void OpenGLVideoSync::Start(void) |
412 | 420 | #endif /* USING_OPENGL_VSYNC */ |
413 | 421 | } |
414 | 422 | |
415 | | void OpenGLVideoSync::WaitForFrame(int sync_delay) |
| 423 | int OpenGLVideoSync::WaitForFrame(int sync_delay) |
416 | 424 | { |
417 | 425 | (void) sync_delay; |
418 | 426 | #ifdef USING_OPENGL_VSYNC |
| 427 | //#define GLVSYNCDEBUG |
| 428 | #ifdef GLVSYNCDEBUG |
| 429 | int refreshcount = 0; |
| 430 | #endif |
419 | 431 | const QString msg1("First A/V Sync"), msg2("Second A/V Sync"); |
420 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 432 | m_nexttrigger += sync_delay; |
421 | 433 | |
422 | 434 | if (m_video_output && m_video_output->IsEmbedding()) |
423 | 435 | { |
424 | 436 | m_delay = CalcDelay(); |
425 | 437 | if (m_delay > 0) |
426 | 438 | usleep(m_delay); |
427 | | return; |
| 439 | return 0; |
428 | 440 | } |
429 | 441 | |
430 | 442 | if (!m_context) |
431 | | return; |
| 443 | return 0; |
432 | 444 | |
433 | 445 | unsigned int frameNum = m_context->GetVideoSyncCount(); |
434 | 446 | |
| 447 | #ifdef GLVSYNCDEBUG |
| 448 | int delay1 = m_delay; |
| 449 | int delay2; |
| 450 | #endif |
435 | 451 | // Always sync to the next retrace execpt when we are very late. |
436 | 452 | if ((m_delay = CalcDelay()) > -(m_refresh_interval/2)) |
437 | 453 | { |
| 454 | #ifdef GLVSYNCDEBUG |
| 455 | delay2 = m_delay; |
| 456 | #endif |
438 | 457 | m_context->WaitForVideoSync(2, (frameNum+1)%2 ,&frameNum); |
439 | 458 | m_delay = CalcDelay(); |
| 459 | #ifdef GLVSYNCDEBUG |
| 460 | refreshcount++; |
| 461 | #endif |
440 | 462 | } |
| 463 | #ifdef GLVSYNCDEBUG |
| 464 | else |
| 465 | delay2 = m_delay; |
| 466 | #endif |
441 | 467 | |
| 468 | #ifdef GLVSYNCDEBUG |
| 469 | int delay3 = m_delay; |
| 470 | #endif |
442 | 471 | // Wait for any remaining retrace intervals in one pass. |
443 | 472 | if (m_delay > 0) |
444 | 473 | { |
445 | | uint n = m_delay / m_refresh_interval + 1; |
| 474 | //uint n = m_delay / m_refresh_interval + 1; |
| 475 | uint n = (m_delay + m_refresh_interval - 1) / m_refresh_interval; |
446 | 476 | m_context->WaitForVideoSync((n+1), (frameNum+n)%(n+1), &frameNum); |
| 477 | #ifdef GLVSYNCDEBUG |
| 478 | refreshcount += (int)n; |
| 479 | #endif |
447 | 480 | m_delay = CalcDelay(); |
448 | 481 | } |
| 482 | #ifdef GLVSYNCDEBUG |
| 483 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, QString("VS: WFF: ri:%1 fi:%2 delay1:%3 delay2:%4 delay3:%5 skip:%6 finaldelay:%7") |
| 484 | .arg(m_refresh_interval) |
| 485 | .arg(m_frame_interval) |
| 486 | .arg(delay1) |
| 487 | .arg(delay2) |
| 488 | .arg(delay3) |
| 489 | .arg(refreshcount) |
| 490 | .arg(m_delay) |
| 491 | ); |
| 492 | #endif |
| 493 | |
| 494 | return m_delay; |
449 | 495 | |
450 | 496 | KeepPhase(); |
| 497 | #ifdef GLVSYNCDEBUG |
| 498 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, "VS: AdvanceTrigger"); |
| 499 | #endif |
451 | 500 | #endif /* USING_OPENGL_VSYNC */ |
452 | 501 | } |
453 | 502 | #endif /* !_WIN32 */ |
… |
… |
bool RTCVideoSync::TryInit(void) |
494 | 543 | return true; |
495 | 544 | } |
496 | 545 | |
497 | | void RTCVideoSync::WaitForFrame(int sync_delay) |
| 546 | int RTCVideoSync::WaitForFrame(int sync_delay) |
498 | 547 | { |
499 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 548 | m_nexttrigger += sync_delay; |
500 | 549 | |
501 | 550 | m_delay = CalcDelay(); |
502 | 551 | |
… |
… |
void RTCVideoSync::WaitForFrame(int sync_delay) |
509 | 558 | if ((val < 0) && (m_delay > 0)) |
510 | 559 | usleep(m_delay); |
511 | 560 | } |
| 561 | return 0; |
512 | 562 | } |
513 | 563 | #endif /* __linux__ */ |
514 | 564 | |
… |
… |
bool VDPAUVideoSync::TryInit(void) |
532 | 582 | return true; |
533 | 583 | } |
534 | 584 | |
535 | | void VDPAUVideoSync::WaitForFrame(int sync_delay) |
| 585 | int VDPAUVideoSync::WaitForFrame(int sync_delay) |
536 | 586 | { |
537 | 587 | // Offset for externally-provided A/V sync delay |
538 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 588 | m_nexttrigger += sync_delay; |
539 | 589 | m_delay = CalcDelay(); |
540 | 590 | |
541 | 591 | if (m_delay < 0) |
… |
… |
void VDPAUVideoSync::WaitForFrame(int sync_delay) |
543 | 593 | |
544 | 594 | VideoOutputVDPAU *vo = (VideoOutputVDPAU *)(m_video_output); |
545 | 595 | vo->SetNextFrameDisplayTimeOffset(m_delay); |
| 596 | return 0; |
546 | 597 | } |
547 | 598 | #endif |
548 | 599 | |
… |
… |
bool BusyWaitVideoSync::TryInit(void) |
563 | 614 | return true; |
564 | 615 | } |
565 | 616 | |
566 | | void BusyWaitVideoSync::WaitForFrame(int sync_delay) |
| 617 | int BusyWaitVideoSync::WaitForFrame(int sync_delay) |
567 | 618 | { |
568 | 619 | // Offset for externally-provided A/V sync delay |
569 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 620 | m_nexttrigger += sync_delay; |
570 | 621 | |
571 | 622 | m_delay = CalcDelay(); |
572 | 623 | |
… |
… |
void BusyWaitVideoSync::WaitForFrame(int sync_delay) |
592 | 643 | if (cnt > 1) |
593 | 644 | m_cheat -= 200; |
594 | 645 | } |
| 646 | return 0; |
595 | 647 | } |
596 | 648 | |
597 | 649 | USleepVideoSync::USleepVideoSync(VideoOutput *vo, |
… |
… |
bool USleepVideoSync::TryInit(void) |
609 | 661 | return true; |
610 | 662 | } |
611 | 663 | |
612 | | void USleepVideoSync::WaitForFrame(int sync_delay) |
| 664 | int USleepVideoSync::WaitForFrame(int sync_delay) |
613 | 665 | { |
614 | 666 | // Offset for externally-provided A/V sync delay |
615 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| 667 | m_nexttrigger += sync_delay; |
616 | 668 | |
617 | 669 | m_delay = CalcDelay(); |
618 | 670 | if (m_delay > 0) |
619 | 671 | usleep(m_delay); |
| 672 | return 0; |
620 | 673 | } |
621 | 674 | |
diff --git a/mythtv/libs/libmythtv/vsync.h b/mythtv/libs/libmythtv/vsync.h
index 156cdad..6c185dc 100644
a
|
b
|
class VideoSync |
64 | 64 | virtual void Start(void); |
65 | 65 | |
66 | 66 | /** \brief Waits for next a frame or field. |
| 67 | * Returns delay to real frame timing in usec |
67 | 68 | * |
68 | 69 | * Start(void), WaitForFrame(void), and Stop(void) should |
69 | 70 | * always be called from same thread, to prevent bad |
… |
… |
class VideoSync |
72 | 73 | * \param sync_delay time until the desired frame or field |
73 | 74 | * \sa CalcDelay(void), KeepPhase(void) |
74 | 75 | */ |
75 | | virtual void WaitForFrame(int sync_delay) = 0; |
| 76 | virtual int WaitForFrame(int sync_delay) = 0; |
76 | 77 | |
77 | 78 | /// \brief Returns the (minimum) refresh interval of the output device. |
78 | 79 | int getRefreshInterval(void) const { return m_refresh_interval; } |
… |
… |
class VideoSync |
92 | 93 | uint frame_interval, uint refresh_interval, |
93 | 94 | bool interlaced); |
94 | 95 | protected: |
95 | | static void OffsetTimeval(struct timeval& tv, int offset); |
96 | 96 | int CalcDelay(void); |
97 | 97 | void KeepPhase(void); |
98 | 98 | |
… |
… |
class VideoSync |
100 | 100 | int m_frame_interval; // of video |
101 | 101 | int m_refresh_interval; // of display |
102 | 102 | bool m_interlaced; |
103 | | struct timeval m_nexttrigger; |
| 103 | int64_t m_nexttrigger; |
104 | 104 | int m_delay; |
| 105 | bool m_synchronous; |
105 | 106 | |
106 | 107 | static int m_forceskip; |
107 | 108 | }; |
… |
… |
class DRMVideoSync : public VideoSync |
123 | 124 | QString getName(void) const { return QString("DRM"); } |
124 | 125 | bool TryInit(void); |
125 | 126 | void Start(void); |
126 | | void WaitForFrame(int sync_delay); |
| 127 | int WaitForFrame(int sync_delay); |
127 | 128 | |
128 | 129 | private: |
129 | 130 | int m_dri_fd; |
… |
… |
class OpenGLVideoSync : public VideoSync |
164 | 165 | QString getName(void) const { return QString("SGI OpenGL"); } |
165 | 166 | bool TryInit(void); |
166 | 167 | void Start(void); |
167 | | void WaitForFrame(int sync_delay); |
| 168 | int WaitForFrame(int sync_delay); |
168 | 169 | |
169 | 170 | private: |
170 | 171 | MythRenderOpenGL *m_context; |
… |
… |
class RTCVideoSync : public VideoSync |
192 | 193 | |
193 | 194 | QString getName(void) const { return QString("RTC"); } |
194 | 195 | bool TryInit(void); |
195 | | void WaitForFrame(int sync_delay); |
| 196 | int WaitForFrame(int sync_delay); |
196 | 197 | |
197 | 198 | private: |
198 | 199 | int m_rtcfd; |
… |
… |
class VDPAUVideoSync : public VideoSync |
212 | 213 | |
213 | 214 | QString getName(void) const { return QString("VDPAU"); } |
214 | 215 | bool TryInit(void); |
215 | | void WaitForFrame(int sync_delay); |
| 216 | int WaitForFrame(int sync_delay); |
216 | 217 | |
217 | 218 | private: |
218 | 219 | }; |
… |
… |
class BusyWaitVideoSync : public VideoSync |
239 | 240 | |
240 | 241 | QString getName(void) const { return QString("USleep with busy wait"); } |
241 | 242 | bool TryInit(void); |
242 | | void WaitForFrame(int sync_delay); |
| 243 | int WaitForFrame(int sync_delay); |
243 | 244 | |
244 | 245 | private: |
245 | 246 | int m_cheat; |
… |
… |
class USleepVideoSync : public VideoSync |
266 | 267 | |
267 | 268 | QString getName(void) const { return QString("USleep"); } |
268 | 269 | bool TryInit(void); |
269 | | void WaitForFrame(int sync_delay); |
| 270 | int WaitForFrame(int sync_delay); |
270 | 271 | }; |
271 | 272 | #endif /* VSYNC_H_INCLUDED */ |