| [9816] | 1 | |
|---|
| 2 | #include "FFmpegClocks.hpp" |
|---|
| 3 | |
|---|
| 4 | #include <cmath> |
|---|
| 5 | #include <algorithm> |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | namespace osgFFmpeg { |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | namespace |
|---|
| 16 | { |
|---|
| 17 | |
|---|
| 18 | const double AV_SYNC_THRESHOLD = 0.01; |
|---|
| 19 | const double AV_NOSYNC_THRESHOLD = 10.0; |
|---|
| 20 | |
|---|
| 21 | inline double clamp(const double value, const double min, const double max) |
|---|
| 22 | { |
|---|
| 23 | return (std::min)((std::max)(value, min), max); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | FFmpegClocks::FFmpegClocks() : |
|---|
| 31 | m_video_clock(0), |
|---|
| 32 | m_start_time(0), |
|---|
| 33 | m_last_frame_delay(0.040), |
|---|
| 34 | m_last_frame_pts(0), |
|---|
| 35 | m_last_actual_delay(0), |
|---|
| 36 | m_frame_time(0), |
|---|
| 37 | m_audio_buffer_end_pts(0), |
|---|
| 38 | m_audio_delay(0.0), |
|---|
| 39 | m_audio_disabled(false), |
|---|
| 40 | m_rewind(false) |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | void FFmpegClocks::reset(const double start_time) |
|---|
| 48 | { |
|---|
| 49 | ScopedLock lock(m_mutex); |
|---|
| 50 | |
|---|
| 51 | m_video_clock = start_time; |
|---|
| 52 | |
|---|
| 53 | m_start_time = start_time; |
|---|
| 54 | m_last_frame_delay = 0.040; |
|---|
| 55 | m_last_frame_pts = start_time - m_last_frame_delay; |
|---|
| 56 | m_frame_time = start_time; |
|---|
| 57 | |
|---|
| 58 | m_audio_buffer_end_pts = start_time; |
|---|
| 59 | m_audio_timer.setStartTick(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void FFmpegClocks::rewindAudio() |
|---|
| 65 | { |
|---|
| 66 | ScopedLock lock(m_mutex); |
|---|
| 67 | |
|---|
| 68 | m_audio_buffer_end_pts = m_start_time; |
|---|
| 69 | m_audio_timer.setStartTick(); |
|---|
| 70 | |
|---|
| 71 | m_rewind = ! m_rewind; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | void FFmpegClocks::rewindVideo() |
|---|
| 77 | { |
|---|
| 78 | ScopedLock lock(m_mutex); |
|---|
| 79 | |
|---|
| 80 | if (m_audio_disabled) |
|---|
| 81 | return; |
|---|
| 82 | |
|---|
| 83 | m_video_clock = m_start_time; |
|---|
| 84 | |
|---|
| 85 | m_last_frame_delay = 0.040; |
|---|
| 86 | m_last_frame_pts = m_start_time - m_last_frame_delay; |
|---|
| 87 | m_frame_time = m_start_time; |
|---|
| 88 | |
|---|
| 89 | m_rewind = ! m_rewind; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | void FFmpegClocks::audioSetBufferEndPts(const double pts) |
|---|
| 95 | { |
|---|
| 96 | ScopedLock lock(m_mutex); |
|---|
| 97 | |
|---|
| 98 | m_audio_buffer_end_pts = pts; |
|---|
| 99 | m_audio_timer.setStartTick(); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | void FFmpegClocks::audioAdjustBufferEndPts(double increment) |
|---|
| 105 | { |
|---|
| 106 | ScopedLock lock(m_mutex); |
|---|
| 107 | |
|---|
| 108 | m_audio_buffer_end_pts += increment; |
|---|
| 109 | m_audio_timer.setStartTick(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | void FFmpegClocks::audioSetDelay(const double delay) |
|---|
| 115 | { |
|---|
| 116 | m_audio_delay = delay; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void FFmpegClocks::audioDisable() |
|---|
| 122 | { |
|---|
| 123 | ScopedLock lock(m_mutex); |
|---|
| 124 | |
|---|
| 125 | m_audio_disabled = true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | double FFmpegClocks::videoSynchClock(const AVFrame * const frame, const double time_base, double pts) |
|---|
| 131 | { |
|---|
| 132 | if (pts != 0) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | m_video_clock = pts; |
|---|
| 136 | } |
|---|
| 137 | else |
|---|
| 138 | { |
|---|
| 139 | |
|---|
| 140 | pts = m_video_clock; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | double frame_delay = time_base; |
|---|
| 146 | frame_delay += frame->repeat_pict * (frame_delay * 0.5); |
|---|
| 147 | |
|---|
| 148 | m_video_clock += frame_delay; |
|---|
| 149 | |
|---|
| 150 | return pts; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | double FFmpegClocks::videoRefreshSchedule(const double pts) |
|---|
| 156 | { |
|---|
| 157 | ScopedLock lock(m_mutex); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | double delay = pts - m_last_frame_pts; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | if (delay <= 0.0 || delay >= 1.0) |
|---|
| 171 | delay = m_last_frame_delay; |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | m_last_frame_delay = delay; |
|---|
| 175 | m_last_frame_pts = pts; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | m_frame_time += delay; |
|---|
| 182 | |
|---|
| 183 | const double audio_time = getAudioTime(); |
|---|
| 184 | const double actual_delay = (! m_rewind) ? |
|---|
| 185 | clamp(m_frame_time - audio_time, -0.5*delay, 2.5*delay) : |
|---|
| 186 | m_last_actual_delay; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | m_last_actual_delay = actual_delay; |
|---|
| 197 | |
|---|
| 198 | return actual_delay; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | double FFmpegClocks::getStartTime() const |
|---|
| 204 | { |
|---|
| 205 | return m_start_time; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | double FFmpegClocks::getAudioTime() const |
|---|
| 211 | { |
|---|
| 212 | return m_audio_buffer_end_pts + m_audio_timer.time_s() - m_audio_delay; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | } |
|---|