commit 08ce0149ce184d26173e34e0a6888543e981b830
Author: Mark Spieth <mspieth@digivation.com.au>
Date: Sun Aug 15 17:35:09 2010 +1000
vsync calc optimizations and pll out
diff --git a/mythtv/libs/libmythtv/vsync.cpp b/mythtv/libs/libmythtv/vsync.cpp
index 41d3736..ddedceb 100644
|
a
|
b
|
VideoSync::VideoSync(VideoOutput *video_output,
|
| 128 | 128 | bool halve_frame_interval) : |
| 129 | 129 | m_video_output(video_output), m_frame_interval(frameint), |
| 130 | 130 | m_refresh_interval(refreshint), m_interlaced(halve_frame_interval), |
| 131 | | m_delay(-1) |
| | 131 | m_nexttrigger(0), |
| | 132 | m_delay(-1), |
| | 133 | m_synchronous(false) |
| 132 | 134 | { |
| 133 | | bzero(&m_nexttrigger, sizeof(m_nexttrigger)); |
| | 135 | int fieldint = frameint; |
| | 136 | if (halve_frame_interval) |
| | 137 | fieldint /= 2; |
| | 138 | double sync_factor = fieldint * 2.0f / refreshint; |
| | 139 | sync_factor = sync_factor - round(sync_factor); |
| | 140 | m_synchronous = (sync_factor >= -0.005) && (sync_factor <= 0.005); |
| | 141 | VERBOSE(VB_PLAYBACK, LOC + QString("Set video sync frame interval to %1 (synced:%2)") |
| | 142 | .arg(m_frame_interval).arg(m_synchronous)); |
| 134 | 143 | } |
| 135 | 144 | |
| 136 | | void VideoSync::Start(void) |
| | 145 | int64_t GetTime(void) |
| 137 | 146 | { |
| 138 | | gettimeofday(&m_nexttrigger, NULL); // now |
| | 147 | struct timeval now_tv; |
| | 148 | gettimeofday(&now_tv, NULL); // now |
| | 149 | return now_tv.tv_sec * 1000000LL + now_tv.tv_usec; |
| 139 | 150 | } |
| 140 | 151 | |
| 141 | | void VideoSync::OffsetTimeval(struct timeval& tv, int offset) |
| | 152 | void VideoSync::Start(void) |
| 142 | 153 | { |
| 143 | | tv.tv_usec += offset; |
| 144 | | while (tv.tv_usec > 999999) |
| 145 | | { |
| 146 | | tv.tv_sec++; |
| 147 | | tv.tv_usec -= 1000000; |
| 148 | | } |
| 149 | | while (tv.tv_usec < 0) |
| 150 | | { |
| 151 | | tv.tv_sec--; |
| 152 | | tv.tv_usec += 1000000; |
| 153 | | } |
| | 154 | m_nexttrigger = GetTime(); |
| 154 | 155 | } |
| 155 | 156 | |
| 156 | 157 | /** \fn VideoSync::CalcDelay() |
| … |
… |
void VideoSync::OffsetTimeval(struct timeval& tv, int offset)
|
| 166 | 167 | */ |
| 167 | 168 | int VideoSync::CalcDelay() |
| 168 | 169 | { |
| 169 | | struct timeval now; |
| 170 | | gettimeofday(&now, NULL); |
| | 170 | int64_t now = GetTime(); |
| 171 | 171 | //cout << "CalcDelay: next: " << timeval_str(m_nexttrigger) << " now " |
| 172 | 172 | // << timeval_str(now) << endl; |
| 173 | 173 | |
| 174 | | int ret_val = (m_nexttrigger.tv_sec - now.tv_sec) * 1000000 + |
| 175 | | (m_nexttrigger.tv_usec - now.tv_usec); |
| | 174 | int ret_val = m_nexttrigger - now; |
| 176 | 175 | |
| 177 | 176 | //cout << "delay " << ret_val << endl; |
| 178 | 177 | |
| … |
… |
int VideoSync::CalcDelay()
|
| 184 | 183 | ret_val = m_frame_interval * 4; |
| 185 | 184 | |
| 186 | 185 | // set nexttrigger to our new target time |
| 187 | | m_nexttrigger.tv_sec = now.tv_sec; |
| 188 | | m_nexttrigger.tv_usec = now.tv_usec; |
| 189 | | OffsetTimeval(m_nexttrigger, ret_val); |
| | 186 | m_nexttrigger = now; |
| | 187 | m_nexttrigger += ret_val; |
| 190 | 188 | } |
| 191 | 189 | |
| 192 | | if (ret_val < -m_frame_interval) |
| | 190 | if ((ret_val < -m_frame_interval) && (m_frame_interval >= m_refresh_interval)) |
| 193 | 191 | { |
| 194 | 192 | ret_val = -m_frame_interval; |
| 195 | 193 | |
| 196 | 194 | // set nexttrigger to our new target time |
| 197 | | m_nexttrigger.tv_sec = now.tv_sec; |
| 198 | | m_nexttrigger.tv_usec = now.tv_usec; |
| 199 | | OffsetTimeval(m_nexttrigger, ret_val); |
| | 195 | m_nexttrigger = now; |
| | 196 | m_nexttrigger += ret_val; |
| 200 | 197 | } |
| 201 | 198 | |
| 202 | 199 | return ret_val; |
| … |
… |
int VideoSync::CalcDelay()
|
| 213 | 210 | void VideoSync::KeepPhase() |
| 214 | 211 | { |
| 215 | 212 | // cerr << m_delay << endl; |
| 216 | | if (m_delay < -(m_refresh_interval/2)) |
| 217 | | OffsetTimeval(m_nexttrigger, 200); |
| 218 | | else if (m_delay > -500) |
| 219 | | OffsetTimeval(m_nexttrigger, -2000); |
| | 213 | if (m_synchronous) |
| | 214 | { |
| | 215 | if (m_delay < -(m_refresh_interval - 500)) |
| | 216 | m_nexttrigger += 200; |
| | 217 | else if (m_delay > -500) |
| | 218 | m_nexttrigger += -2000; |
| | 219 | } |
| | 220 | else |
| | 221 | { |
| | 222 | if (m_delay < -(m_refresh_interval + 500)) |
| | 223 | m_nexttrigger += 200; |
| | 224 | else if (m_delay >= 0) |
| | 225 | m_nexttrigger += -2000; |
| | 226 | } |
| 220 | 227 | } |
| 221 | 228 | |
| 222 | 229 | #ifndef _WIN32 |
| … |
… |
void DRMVideoSync::Start(void)
|
| 306 | 313 | VideoSync::Start(); |
| 307 | 314 | } |
| 308 | 315 | |
| 309 | | void DRMVideoSync::WaitForFrame(int sync_delay) |
| | 316 | int DRMVideoSync::WaitForFrame(int sync_delay) |
| 310 | 317 | { |
| 311 | 318 | // Offset for externally-provided A/V sync delay |
| 312 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 319 | m_nexttrigger += sync_delay; |
| 313 | 320 | |
| 314 | 321 | m_delay = CalcDelay(); |
| 315 | 322 | //cerr << "WaitForFrame at : " << m_delay; |
| … |
… |
void DRMVideoSync::WaitForFrame(int sync_delay)
|
| 329 | 336 | if (m_delay > 0) |
| 330 | 337 | { |
| 331 | 338 | // Wait for any remaining retrace intervals in one pass. |
| 332 | | int n = m_delay / m_refresh_interval + 1; |
| | 339 | int n = (m_delay + m_refresh_interval - 1) / m_refresh_interval; |
| 333 | 340 | |
| 334 | 341 | drm_wait_vblank_t blank; |
| 335 | 342 | blank.request.type = DRM_VBLANK_RELATIVE; |
| … |
… |
void DRMVideoSync::WaitForFrame(int sync_delay)
|
| 339 | 346 | //cerr << "Wait " << n << " intervals. Count " << blank.request.sequence; |
| 340 | 347 | //cerr << " Delay " << m_delay << endl; |
| 341 | 348 | } |
| | 349 | return m_delay; |
| 342 | 350 | |
| 343 | 351 | KeepPhase(); |
| 344 | 352 | } |
| … |
… |
void OpenGLVideoSync::Start(void)
|
| 409 | 417 | #endif /* USING_OPENGL_VSYNC */ |
| 410 | 418 | } |
| 411 | 419 | |
| 412 | | void OpenGLVideoSync::WaitForFrame(int sync_delay) |
| | 420 | int OpenGLVideoSync::WaitForFrame(int sync_delay) |
| 413 | 421 | { |
| 414 | 422 | (void) sync_delay; |
| 415 | 423 | #ifdef USING_OPENGL_VSYNC |
| | 424 | //#define GLVSYNCDEBUG |
| | 425 | #ifdef GLVSYNCDEBUG |
| | 426 | int refreshcount = 0; |
| | 427 | #endif |
| 416 | 428 | const QString msg1("First A/V Sync"), msg2("Second A/V Sync"); |
| 417 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 429 | m_nexttrigger += sync_delay; |
| 418 | 430 | |
| 419 | 431 | if (m_video_output && m_video_output->IsEmbedding()) |
| 420 | 432 | { |
| 421 | 433 | m_delay = CalcDelay(); |
| 422 | 434 | if (m_delay > 0) |
| 423 | 435 | usleep(m_delay); |
| 424 | | return; |
| | 436 | return 0; |
| 425 | 437 | } |
| 426 | 438 | |
| 427 | 439 | if (!m_context) |
| 428 | | return; |
| | 440 | return 0; |
| 429 | 441 | |
| 430 | 442 | unsigned int frameNum = m_context->GetVideoSyncCount(); |
| 431 | 443 | |
| | 444 | #ifdef GLVSYNCDEBUG |
| | 445 | int delay1 = m_delay; |
| | 446 | int delay2; |
| | 447 | #endif |
| 432 | 448 | // Always sync to the next retrace execpt when we are very late. |
| 433 | 449 | if ((m_delay = CalcDelay()) > -(m_refresh_interval/2)) |
| 434 | 450 | { |
| | 451 | #ifdef GLVSYNCDEBUG |
| | 452 | delay2 = m_delay; |
| | 453 | #endif |
| 435 | 454 | m_context->WaitForVideoSync(2, (frameNum+1)%2 ,&frameNum); |
| 436 | 455 | m_delay = CalcDelay(); |
| | 456 | #ifdef GLVSYNCDEBUG |
| | 457 | refreshcount++; |
| | 458 | #endif |
| 437 | 459 | } |
| | 460 | #ifdef GLVSYNCDEBUG |
| | 461 | else |
| | 462 | delay2 = m_delay; |
| | 463 | #endif |
| 438 | 464 | |
| | 465 | #ifdef GLVSYNCDEBUG |
| | 466 | int delay3 = m_delay; |
| | 467 | #endif |
| 439 | 468 | // Wait for any remaining retrace intervals in one pass. |
| 440 | 469 | if (m_delay > 0) |
| 441 | 470 | { |
| 442 | | uint n = m_delay / m_refresh_interval + 1; |
| | 471 | //uint n = m_delay / m_refresh_interval + 1; |
| | 472 | uint n = (m_delay + m_refresh_interval - 1) / m_refresh_interval; |
| 443 | 473 | m_context->WaitForVideoSync((n+1), (frameNum+n)%(n+1), &frameNum); |
| | 474 | #ifdef GLVSYNCDEBUG |
| | 475 | refreshcount += (int)n; |
| | 476 | #endif |
| 444 | 477 | m_delay = CalcDelay(); |
| 445 | 478 | } |
| | 479 | #ifdef GLVSYNCDEBUG |
| | 480 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, QString("VS: WFF: ri:%1 fi:%2 delay1:%3 delay2:%4 delay3:%5 skip:%6 finaldelay:%7") |
| | 481 | .arg(m_refresh_interval) |
| | 482 | .arg(m_frame_interval) |
| | 483 | .arg(delay1) |
| | 484 | .arg(delay2) |
| | 485 | .arg(delay3) |
| | 486 | .arg(refreshcount) |
| | 487 | .arg(m_delay) |
| | 488 | ); |
| | 489 | #endif |
| | 490 | |
| | 491 | return m_delay; |
| 446 | 492 | |
| 447 | 493 | KeepPhase(); |
| | 494 | #ifdef GLVSYNCDEBUG |
| | 495 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, "VS: AdvanceTrigger"); |
| | 496 | #endif |
| 448 | 497 | #endif /* USING_OPENGL_VSYNC */ |
| 449 | 498 | } |
| 450 | 499 | #endif /* !_WIN32 */ |
| … |
… |
bool RTCVideoSync::TryInit(void)
|
| 491 | 540 | return true; |
| 492 | 541 | } |
| 493 | 542 | |
| 494 | | void RTCVideoSync::WaitForFrame(int sync_delay) |
| | 543 | int RTCVideoSync::WaitForFrame(int sync_delay) |
| 495 | 544 | { |
| 496 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 545 | m_nexttrigger += sync_delay; |
| 497 | 546 | |
| 498 | 547 | m_delay = CalcDelay(); |
| 499 | 548 | |
| … |
… |
void RTCVideoSync::WaitForFrame(int sync_delay)
|
| 506 | 555 | if ((val < 0) && (m_delay > 0)) |
| 507 | 556 | usleep(m_delay); |
| 508 | 557 | } |
| | 558 | return 0; |
| 509 | 559 | } |
| 510 | 560 | #endif /* __linux__ */ |
| 511 | 561 | |
| … |
… |
bool VDPAUVideoSync::TryInit(void)
|
| 529 | 579 | return true; |
| 530 | 580 | } |
| 531 | 581 | |
| 532 | | void VDPAUVideoSync::WaitForFrame(int sync_delay) |
| | 582 | int VDPAUVideoSync::WaitForFrame(int sync_delay) |
| 533 | 583 | { |
| 534 | 584 | // Offset for externally-provided A/V sync delay |
| 535 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 585 | m_nexttrigger += sync_delay; |
| 536 | 586 | m_delay = CalcDelay(); |
| 537 | 587 | |
| 538 | 588 | if (m_delay < 0) |
| … |
… |
void VDPAUVideoSync::WaitForFrame(int sync_delay)
|
| 540 | 590 | |
| 541 | 591 | VideoOutputVDPAU *vo = (VideoOutputVDPAU *)(m_video_output); |
| 542 | 592 | vo->SetNextFrameDisplayTimeOffset(m_delay); |
| | 593 | return 0; |
| 543 | 594 | } |
| 544 | 595 | #endif |
| 545 | 596 | |
| … |
… |
bool BusyWaitVideoSync::TryInit(void)
|
| 560 | 611 | return true; |
| 561 | 612 | } |
| 562 | 613 | |
| 563 | | void BusyWaitVideoSync::WaitForFrame(int sync_delay) |
| | 614 | int BusyWaitVideoSync::WaitForFrame(int sync_delay) |
| 564 | 615 | { |
| 565 | 616 | // Offset for externally-provided A/V sync delay |
| 566 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 617 | m_nexttrigger += sync_delay; |
| 567 | 618 | |
| 568 | 619 | m_delay = CalcDelay(); |
| 569 | 620 | |
| … |
… |
void BusyWaitVideoSync::WaitForFrame(int sync_delay)
|
| 589 | 640 | if (cnt > 1) |
| 590 | 641 | m_cheat -= 200; |
| 591 | 642 | } |
| | 643 | return 0; |
| 592 | 644 | } |
| 593 | 645 | |
| 594 | 646 | USleepVideoSync::USleepVideoSync(VideoOutput *vo, |
| … |
… |
bool USleepVideoSync::TryInit(void)
|
| 606 | 658 | return true; |
| 607 | 659 | } |
| 608 | 660 | |
| 609 | | void USleepVideoSync::WaitForFrame(int sync_delay) |
| | 661 | int USleepVideoSync::WaitForFrame(int sync_delay) |
| 610 | 662 | { |
| 611 | 663 | // Offset for externally-provided A/V sync delay |
| 612 | | OffsetTimeval(m_nexttrigger, sync_delay); |
| | 664 | m_nexttrigger += sync_delay; |
| 613 | 665 | |
| 614 | 666 | m_delay = CalcDelay(); |
| 615 | 667 | if (m_delay > 0) |
| 616 | 668 | usleep(m_delay); |
| | 669 | return 0; |
| 617 | 670 | } |
| 618 | 671 | |
diff --git a/mythtv/libs/libmythtv/vsync.h b/mythtv/libs/libmythtv/vsync.h
index f3f267f..47b75c0 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
|
| 90 | 91 | uint frame_interval, uint refresh_interval, |
| 91 | 92 | bool interlaced); |
| 92 | 93 | protected: |
| 93 | | static void OffsetTimeval(struct timeval& tv, int offset); |
| 94 | 94 | int CalcDelay(void); |
| 95 | 95 | void KeepPhase(void); |
| 96 | 96 | |
| … |
… |
class VideoSync
|
| 98 | 98 | int m_frame_interval; // of video |
| 99 | 99 | int m_refresh_interval; // of display |
| 100 | 100 | bool m_interlaced; |
| 101 | | struct timeval m_nexttrigger; |
| | 101 | int64_t m_nexttrigger; |
| 102 | 102 | int m_delay; |
| | 103 | bool m_synchronous; |
| 103 | 104 | |
| 104 | 105 | static int m_forceskip; |
| 105 | 106 | }; |
| … |
… |
class DRMVideoSync : public VideoSync
|
| 121 | 122 | QString getName(void) const { return QString("DRM"); } |
| 122 | 123 | bool TryInit(void); |
| 123 | 124 | void Start(void); |
| 124 | | void WaitForFrame(int sync_delay); |
| | 125 | int WaitForFrame(int sync_delay); |
| 125 | 126 | |
| 126 | 127 | private: |
| 127 | 128 | int m_dri_fd; |
| … |
… |
class OpenGLVideoSync : public VideoSync
|
| 162 | 163 | QString getName(void) const { return QString("SGI OpenGL"); } |
| 163 | 164 | bool TryInit(void); |
| 164 | 165 | void Start(void); |
| 165 | | void WaitForFrame(int sync_delay); |
| | 166 | int WaitForFrame(int sync_delay); |
| 166 | 167 | |
| 167 | 168 | private: |
| 168 | 169 | MythRenderOpenGL *m_context; |
| … |
… |
class RTCVideoSync : public VideoSync
|
| 190 | 191 | |
| 191 | 192 | QString getName(void) const { return QString("RTC"); } |
| 192 | 193 | bool TryInit(void); |
| 193 | | void WaitForFrame(int sync_delay); |
| | 194 | int WaitForFrame(int sync_delay); |
| 194 | 195 | |
| 195 | 196 | private: |
| 196 | 197 | int m_rtcfd; |
| … |
… |
class VDPAUVideoSync : public VideoSync
|
| 210 | 211 | |
| 211 | 212 | QString getName(void) const { return QString("VDPAU"); } |
| 212 | 213 | bool TryInit(void); |
| 213 | | void WaitForFrame(int sync_delay); |
| | 214 | int WaitForFrame(int sync_delay); |
| 214 | 215 | |
| 215 | 216 | private: |
| 216 | 217 | }; |
| … |
… |
class BusyWaitVideoSync : public VideoSync
|
| 237 | 238 | |
| 238 | 239 | QString getName(void) const { return QString("USleep with busy wait"); } |
| 239 | 240 | bool TryInit(void); |
| 240 | | void WaitForFrame(int sync_delay); |
| | 241 | int WaitForFrame(int sync_delay); |
| 241 | 242 | |
| 242 | 243 | private: |
| 243 | 244 | int m_cheat; |
| … |
… |
class USleepVideoSync : public VideoSync
|
| 264 | 265 | |
| 265 | 266 | QString getName(void) const { return QString("USleep"); } |
| 266 | 267 | bool TryInit(void); |
| 267 | | void WaitForFrame(int sync_delay); |
| | 268 | int WaitForFrame(int sync_delay); |
| 268 | 269 | }; |
| 269 | 270 | #endif /* VSYNC_H_INCLUDED */ |