smoother vsync with predictive frame skipping
From: Mark Spieth <mspieth@digivation.com.au>
---
mythtv/libs/libmyth/audiooutput.h | 9 +
mythtv/libs/libmyth/audiooutputbase.cpp | 177 ++++++++++++++++------
mythtv/libs/libmyth/audiooutputbase.h | 35 +++-
mythtv/libs/libmythfreesurround/freesurround.cpp | 54 ++-----
mythtv/libs/libmythtv/avformatdecoder.cpp | 23 +++
mythtv/libs/libmythtv/avformatdecoder.h | 2
mythtv/libs/libmythtv/mythplayer.cpp | 160 ++++++++++++++++----
mythtv/libs/libmythtv/mythplayer.h | 5 +
mythtv/libs/libmythtv/vsync.cpp | 147 ++++++++++++------
mythtv/libs/libmythtv/vsync.h | 19 +-
mythtv/programs/mythtranscode/transcode.cpp | 6 -
11 files changed, 451 insertions(+), 186 deletions(-)
diff --git a/mythtv/libs/libmyth/audiooutput.h b/mythtv/libs/libmyth/audiooutput.h
index 62cdda0..a532519 100644
|
a
|
b
|
|
| 4 | 4 | #include <QString> |
| 5 | 5 | #include <QVector> |
| 6 | 6 | |
| | 7 | #include "compat.h" |
| 7 | 8 | #include "audiosettings.h" |
| 8 | 9 | #include "audiooutputsettings.h" |
| 9 | 10 | #include "mythcorecontext.h" |
| … |
… |
class MPUBLIC AudioOutput : public VolumeBase, public OutputListeners
|
| 71 | 72 | |
| 72 | 73 | virtual void Reset(void) = 0; |
| 73 | 74 | |
| 74 | | virtual bool AddFrames(void *buffer, int samples, long long timecode) = 0; |
| | 75 | virtual bool AddFrames(void *buffer, int samples, int64_t timecode) = 0; |
| 75 | 76 | |
| 76 | | virtual void SetTimecode(long long timecode) = 0; |
| | 77 | virtual void SetTimecode(int64_t timecode) = 0; |
| 77 | 78 | virtual bool IsPaused(void) const = 0; |
| 78 | 79 | virtual void Pause(bool paused) = 0; |
| 79 | 80 | virtual void PauseUntilBuffered(void) = 0; |
| … |
… |
class MPUBLIC AudioOutput : public VolumeBase, public OutputListeners
|
| 81 | 82 | // Wait for all data to finish playing |
| 82 | 83 | virtual void Drain(void) = 0; |
| 83 | 84 | |
| 84 | | virtual int GetAudiotime(void) = 0; |
| | 85 | virtual int64_t GetAudiotime(void) = 0; |
| 85 | 86 | |
| 86 | 87 | /// report amount of audio buffered in milliseconds. |
| 87 | | virtual int GetAudioBufferedTime(void) { return 0; } |
| | 88 | virtual int64_t GetAudioBufferedTime(void) { return 0; } |
| 88 | 89 | |
| 89 | 90 | virtual void SetSourceBitrate(int ) { } |
| 90 | 91 | |
diff --git a/mythtv/libs/libmyth/audiooutputbase.cpp b/mythtv/libs/libmyth/audiooutputbase.cpp
index a0040e9..0a84ab1 100644
|
a
|
b
|
AudioOutputBase::AudioOutputBase(const AudioSettings &settings) :
|
| 56 | 56 | passthru(false), |
| 57 | 57 | enc(false), reenc(false), |
| 58 | 58 | stretchfactor(1.0f), |
| | 59 | eff_stretchfactor(100000), |
| 59 | 60 | |
| 60 | 61 | source(settings.source), killaudio(false), |
| 61 | 62 | |
| … |
… |
void AudioOutputBase::SetStretchFactorLocked(float lstretchfactor)
|
| 231 | 232 | return; |
| 232 | 233 | |
| 233 | 234 | stretchfactor = lstretchfactor; |
| | 235 | eff_stretchfactor = (int)(100000.0f * lstretchfactor + 0.5); |
| 234 | 236 | if (pSoundStretch) |
| 235 | 237 | { |
| 236 | 238 | VBGENERAL(QString("Changing time stretch to %1").arg(stretchfactor)); |
| … |
… |
void AudioOutputBase::SetStretchFactorLocked(float lstretchfactor)
|
| 254 | 256 | bytes_per_frame = source_channels * |
| 255 | 257 | AudioOutputSettings::SampleSize(FORMAT_FLT); |
| 256 | 258 | waud = raud = 0; |
| | 259 | reset_active.Ref(); |
| 257 | 260 | } |
| 258 | 261 | } |
| 259 | 262 | } |
| … |
… |
bool AudioOutputBase::ToggleUpmix(void)
|
| 285 | 288 | return false; |
| 286 | 289 | |
| 287 | 290 | upmix_default = !upmix_default; |
| | 291 | reset_active.Ref(); |
| 288 | 292 | |
| 289 | 293 | const AudioSettings settings(format, source_channels, codec, |
| 290 | 294 | source_samplerate, passthru); |
| … |
… |
void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings)
|
| 379 | 383 | QMutexLocker lockav(&avsync_lock); |
| 380 | 384 | |
| 381 | 385 | waud = raud = 0; |
| | 386 | reset_active.Clear(); |
| 382 | 387 | actually_paused = processing = false; |
| 383 | 388 | |
| 384 | 389 | channels = settings.channels; |
| … |
… |
void AudioOutputBase::Reset()
|
| 651 | 656 | QMutexLocker lock(&audio_buflock); |
| 652 | 657 | QMutexLocker lockav(&avsync_lock); |
| 653 | 658 | |
| 654 | | raud = waud = audbuf_timecode = audiotime = frames_buffered = 0; |
| | 659 | audbuf_timecode = audiotime = frames_buffered = 0; |
| | 660 | waud = raud; // empty ring buffer |
| | 661 | reset_active.Ref(); |
| 655 | 662 | current_seconds = -1; |
| 656 | 663 | was_paused = !pauseaudio; |
| 657 | 664 | |
| … |
… |
void AudioOutputBase::Reset()
|
| 665 | 672 | * Used by mythmusic for seeking since it doesn't provide timecodes to |
| 666 | 673 | * AddFrames() |
| 667 | 674 | */ |
| 668 | | void AudioOutputBase::SetTimecode(long long timecode) |
| | 675 | void AudioOutputBase::SetTimecode(int64_t timecode) |
| 669 | 676 | { |
| 670 | 677 | audbuf_timecode = audiotime = timecode; |
| 671 | | frames_buffered = (long long)((timecode * source_samplerate) / 1000); |
| | 678 | frames_buffered = (int64_t)((timecode * source_samplerate) / 1000); |
| 672 | 679 | } |
| 673 | 680 | |
| 674 | 681 | /** |
| … |
… |
int AudioOutputBase::audioready()
|
| 723 | 730 | /** |
| 724 | 731 | * Calculate the timecode of the samples that are about to become audible |
| 725 | 732 | */ |
| 726 | | int AudioOutputBase::GetAudiotime(void) |
| | 733 | int64_t AudioOutputBase::GetAudiotime(void) |
| 727 | 734 | { |
| 728 | 735 | if (audbuf_timecode == 0) |
| 729 | 736 | return 0; |
| 730 | 737 | |
| 731 | | int soundcard_buffer = 0; |
| 732 | 738 | int obpf = output_bytes_per_frame; |
| 733 | | int totalbuffer; |
| 734 | | long long oldaudiotime; |
| | 739 | int64_t oldaudiotime; |
| 735 | 740 | |
| 736 | 741 | /* We want to calculate 'audiotime', which is the timestamp of the audio |
| 737 | | which is leaving the sound card at this instant. |
| | 742 | Which is leaving the sound card at this instant. |
| 738 | 743 | |
| 739 | 744 | We use these variables: |
| 740 | 745 | |
| … |
… |
int AudioOutputBase::GetAudiotime(void)
|
| 746 | 751 | 'totalbuffer' is the total # of bytes in our audio buffer, and the |
| 747 | 752 | sound card's buffer. */ |
| 748 | 753 | |
| 749 | | soundcard_buffer = GetBufferedOnSoundcard(); // bytes |
| 750 | 754 | |
| 751 | 755 | QMutexLocker lockav(&avsync_lock); |
| 752 | 756 | |
| | 757 | int64_t soundcard_buffer = GetBufferedOnSoundcard(); // bytes |
| | 758 | int64_t main_buffer = audioready(); |
| | 759 | |
| 753 | 760 | /* audioready tells us how many bytes are in audiobuffer |
| 754 | 761 | scaled appropriately if output format != internal format */ |
| 755 | | totalbuffer = audioready() + soundcard_buffer; |
| 756 | | |
| 757 | | if (needs_upmix && upmixer) |
| 758 | | totalbuffer += upmixer->frameLatency() * obpf; |
| 759 | | |
| 760 | | if (pSoundStretch) |
| 761 | | { |
| 762 | | totalbuffer += pSoundStretch->numUnprocessedSamples() * obpf / |
| 763 | | stretchfactor; |
| 764 | | totalbuffer += pSoundStretch->numSamples() * obpf; |
| 765 | | } |
| 766 | | |
| 767 | | if (encoder) |
| 768 | | totalbuffer += encoder->Buffered(); |
| 769 | 762 | |
| 770 | 763 | oldaudiotime = audiotime; |
| 771 | 764 | |
| 772 | | audiotime = audbuf_timecode - (long long)(totalbuffer) * 100000 * |
| 773 | | stretchfactor / (obpf * effdsp); |
| | 765 | // timecode is the stretch adjusted version |
| | 766 | // of major post-stretched buffer contents |
| | 767 | // processing latencies are catered for in AddSamples/SetAudiotime to eliminate |
| | 768 | // race |
| | 769 | audiotime = audbuf_timecode - (( (main_buffer + soundcard_buffer) * eff_stretchfactor ) / (effdsp * obpf)); |
| 774 | 770 | |
| 775 | 771 | /* audiotime should never go backwards, but we might get a negative |
| 776 | 772 | value if GetBufferedOnSoundcard() isn't updated by the driver very |
| … |
… |
int AudioOutputBase::GetAudiotime(void)
|
| 778 | 774 | if (audiotime < oldaudiotime) |
| 779 | 775 | audiotime = oldaudiotime; |
| 780 | 776 | |
| 781 | | VBAUDIOTS(QString("GetAudiotime audt=%3 atc=%4 tb=%5 sb=%6 " |
| 782 | | "sr=%7 obpf=%8 sf=%9") |
| | 777 | VBAUDIOTS(QString("GetAudiotime audt=%1 atc=%2 mb=%3 sb=%4 tb=%5 " |
| | 778 | "sr=%6 obpf=%7 bpf=%8 sf=%9 %10 %11") |
| 783 | 779 | .arg(audiotime).arg(audbuf_timecode) |
| 784 | | .arg(totalbuffer).arg(soundcard_buffer) |
| 785 | | .arg(samplerate).arg(obpf).arg(stretchfactor)); |
| | 780 | .arg(main_buffer) |
| | 781 | .arg(soundcard_buffer) |
| | 782 | .arg(main_buffer+soundcard_buffer) |
| | 783 | .arg(samplerate).arg(obpf).arg(bytes_per_frame).arg(stretchfactor) |
| | 784 | .arg((main_buffer + soundcard_buffer) * eff_stretchfactor) |
| | 785 | .arg(( (main_buffer + soundcard_buffer) * eff_stretchfactor ) / (effdsp * obpf)) |
| | 786 | ); |
| | 787 | |
| | 788 | return audiotime; |
| | 789 | } |
| | 790 | |
| | 791 | /** |
| | 792 | * Set the timecode of the top of the ringbuffer |
| | 793 | * Exclude all other processing elements as they dont vary |
| | 794 | * between AddSamples calls |
| | 795 | */ |
| | 796 | void AudioOutputBase::SetAudiotime(int frames, int64_t timecode) |
| | 797 | { |
| | 798 | int64_t processframes_stretched = 0; |
| | 799 | int64_t processframes_unstretched = 0; |
| | 800 | |
| | 801 | if (needs_upmix && upmixer) |
| | 802 | processframes_unstretched -= upmixer->frameLatency(); |
| 786 | 803 | |
| 787 | | return (int)audiotime; |
| | 804 | if (pSoundStretch) |
| | 805 | { |
| | 806 | processframes_unstretched -= pSoundStretch->numUnprocessedSamples(); |
| | 807 | processframes_stretched -= pSoundStretch->numSamples(); |
| | 808 | } |
| | 809 | |
| | 810 | if (encoder) |
| | 811 | // the input buffered data is still in audio_bytes_per_sample format |
| | 812 | processframes_stretched -= encoder->Buffered() / output_bytes_per_frame; |
| | 813 | |
| | 814 | int64_t old_audbuf_timecode = audbuf_timecode; |
| | 815 | |
| | 816 | audbuf_timecode = timecode + |
| | 817 | (((frames + processframes_unstretched) * 100000) + |
| | 818 | (processframes_stretched * eff_stretchfactor )) / effdsp; |
| | 819 | |
| | 820 | // check for timecode wrap and reset audiotime if detected |
| | 821 | // timecode will always be monotonic asc if not seeked and reset |
| | 822 | // happens if seek or pause happens |
| | 823 | if (audbuf_timecode < old_audbuf_timecode) |
| | 824 | audiotime = 0; |
| | 825 | |
| | 826 | VBAUDIOTS(QString("SetAudiotime atc=%1 tc=%2 f=%3 pfu=%4 pfs=%5") |
| | 827 | .arg(audbuf_timecode) |
| | 828 | .arg(timecode) |
| | 829 | .arg(frames) |
| | 830 | .arg(processframes_unstretched) |
| | 831 | .arg(processframes_stretched)); |
| | 832 | #ifdef AUDIOTSTESTING |
| | 833 | GetAudiotime(); |
| | 834 | #endif |
| 788 | 835 | } |
| 789 | 836 | |
| 790 | 837 | /** |
| … |
… |
int AudioOutputBase::GetAudiotime(void)
|
| 792 | 839 | * audible and the samples most recently added to the audiobuffer, i.e. the |
| 793 | 840 | * time in ms representing the sum total of buffered samples |
| 794 | 841 | */ |
| 795 | | int AudioOutputBase::GetAudioBufferedTime(void) |
| | 842 | int64_t AudioOutputBase::GetAudioBufferedTime(void) |
| 796 | 843 | { |
| 797 | 844 | int ret = audbuf_timecode - GetAudiotime(); |
| 798 | 845 | // Pulse can give us values that make this -ve |
| … |
… |
int AudioOutputBase::CopyWithUpmix(char *buffer, int frames, int &org_waud)
|
| 938 | 985 | * |
| 939 | 986 | * Returns false if there's not enough space right now |
| 940 | 987 | */ |
| 941 | | bool AudioOutputBase::AddFrames(void *buffer, int frames, long long timecode) |
| | 988 | bool AudioOutputBase::AddFrames(void *buffer, int in_frames, int64_t timecode) |
| 942 | 989 | { |
| 943 | 990 | int org_waud = waud, afree = audiofree(); |
| 944 | | int bpf = bytes_per_frame, len = frames * source_bytes_per_frame; |
| | 991 | int frames = in_frames; |
| | 992 | int bpf = bytes_per_frame, len = in_frames * source_bytes_per_frame; |
| 945 | 993 | int used = kAudioRingBufferSize - afree; |
| 946 | 994 | bool music = false; |
| | 995 | int bdiff; |
| 947 | 996 | |
| 948 | 997 | VBAUDIOTS(QString("AddFrames frames=%1, bytes=%2, used=%3, free=%4, " |
| 949 | 998 | "timecode=%5 needsupmix=%6") |
| … |
… |
bool AudioOutputBase::AddFrames(void *buffer, int frames, long long timecode)
|
| 965 | 1014 | if (timecode < 0) |
| 966 | 1015 | { |
| 967 | 1016 | // Send original samples to mythmusic visualisation |
| 968 | | timecode = (long long)(frames_buffered) * 1000 / source_samplerate; |
| | 1017 | timecode = (int64_t)(frames_buffered) * 1000 / source_samplerate; |
| 969 | 1018 | frames_buffered += frames; |
| 970 | 1019 | dispatchVisual((uchar *)buffer, len, timecode, source_channels, |
| 971 | 1020 | output_settings->FormatToBits(format)); |
| … |
… |
bool AudioOutputBase::AddFrames(void *buffer, int frames, long long timecode)
|
| 1018 | 1067 | .arg(src_strerror(error))); |
| 1019 | 1068 | |
| 1020 | 1069 | buffer = src_out; |
| 1021 | | frames = src_data.output_frames_gen; |
| | 1070 | in_frames = frames = src_data.output_frames_gen; |
| 1022 | 1071 | } |
| 1023 | 1072 | else if (processing) |
| 1024 | 1073 | buffer = src_in; |
| … |
… |
bool AudioOutputBase::AddFrames(void *buffer, int frames, long long timecode)
|
| 1026 | 1075 | /* we want the timecode of the last sample added but we are given the |
| 1027 | 1076 | timecode of the first - add the time in ms that the frames added |
| 1028 | 1077 | represent */ |
| 1029 | | audbuf_timecode = timecode + ((long long)(frames) * 100000 / effdsp); |
| 1030 | 1078 | |
| 1031 | 1079 | // Copy samples into audiobuffer, with upmix if necessary |
| 1032 | 1080 | if ((len = CopyWithUpmix((char *)buffer, frames, org_waud)) <= 0) |
| 1033 | | return true; |
| | 1081 | { |
| | 1082 | //return true; |
| | 1083 | goto done; |
| | 1084 | } |
| 1034 | 1085 | |
| 1035 | 1086 | frames = len / bpf; |
| 1036 | 1087 | |
| 1037 | | int bdiff = kAudioRingBufferSize - waud; |
| | 1088 | bdiff = kAudioRingBufferSize - waud; |
| 1038 | 1089 | |
| 1039 | 1090 | if (pSoundStretch) |
| 1040 | 1091 | { |
| … |
… |
bool AudioOutputBase::AddFrames(void *buffer, int frames, long long timecode)
|
| 1112 | 1163 | |
| 1113 | 1164 | waud = org_waud; |
| 1114 | 1165 | |
| | 1166 | done: |
| | 1167 | SetAudiotime(in_frames, timecode); |
| | 1168 | |
| 1115 | 1169 | return true; |
| 1116 | 1170 | } |
| 1117 | 1171 | |
| … |
… |
void AudioOutputBase::OutputAudioLoop(void)
|
| 1159 | 1213 | uchar *fragment_buf = new uchar[fragment_size + 16]; |
| 1160 | 1214 | uchar *fragment = (uchar *)AOALIGN(fragment_buf[0]); |
| 1161 | 1215 | |
| | 1216 | // to reduce startup latency, write silence in 8ms chunks |
| | 1217 | int zero_fragment_size = (int)(0.008*samplerate/channels); |
| | 1218 | zero_fragment_size *= bytes_per_frame; // make sure its a multiple of bytes_per_frame |
| | 1219 | if (zero_fragment_size > fragment_size) |
| | 1220 | zero_fragment_size = fragment_size; |
| | 1221 | |
| 1162 | 1222 | bzero(zeros, fragment_size); |
| 1163 | 1223 | |
| 1164 | 1224 | while (!killaudio) |
| … |
… |
void AudioOutputBase::OutputAudioLoop(void)
|
| 1207 | 1267 | continue; |
| 1208 | 1268 | } |
| 1209 | 1269 | |
| | 1270 | #ifdef AUDIOTSTESTING |
| | 1271 | VBAUDIOTS("WriteAudio Start"); |
| | 1272 | #endif |
| 1210 | 1273 | Status(); |
| 1211 | 1274 | |
| 1212 | | if (GetAudioData(fragment, fragment_size, true)) |
| 1213 | | WriteAudio(fragment, fragment_size); |
| | 1275 | // delay setting raud until after phys buffer is filled |
| | 1276 | // so GetAudiotime will be accurate without locking |
| | 1277 | reset_active.TestAndDeref(); |
| | 1278 | int next_raud = raud; |
| | 1279 | if (GetAudioData(fragment, fragment_size, true, &next_raud)) |
| | 1280 | { |
| | 1281 | if (!reset_active.TestAndDeref()) |
| | 1282 | { |
| | 1283 | WriteAudio(fragment, fragment_size); |
| | 1284 | if (!reset_active.TestAndDeref()) |
| | 1285 | raud = next_raud; |
| | 1286 | } |
| | 1287 | } |
| | 1288 | #ifdef AUDIOTSTESTING |
| | 1289 | GetAudiotime(); |
| | 1290 | VBAUDIOTS("WriteAudio Done"); |
| | 1291 | #endif |
| | 1292 | |
| 1214 | 1293 | } |
| 1215 | 1294 | |
| 1216 | 1295 | delete[] zeros; |
| … |
… |
void AudioOutputBase::OutputAudioLoop(void)
|
| 1227 | 1306 | * nothing. Otherwise, we'll copy less than 'size' bytes if that's all that's |
| 1228 | 1307 | * available. Returns the number of bytes copied. |
| 1229 | 1308 | */ |
| 1230 | | int AudioOutputBase::GetAudioData(uchar *buffer, int size, bool full_buffer) |
| | 1309 | int AudioOutputBase::GetAudioData(uchar *buffer, int size, bool full_buffer, int *local_raud) |
| 1231 | 1310 | { |
| 1232 | 1311 | |
| | 1312 | #define LRPOS audiobuffer + *local_raud |
| 1233 | 1313 | // re-check audioready() in case things changed. |
| 1234 | 1314 | // for example, ClearAfterSeek() might have run |
| 1235 | 1315 | int avail_size = audioready(); |
| 1236 | 1316 | int frag_size = size; |
| 1237 | 1317 | int written_size = size; |
| 1238 | 1318 | |
| | 1319 | if (local_raud == NULL) |
| | 1320 | local_raud = &raud; |
| | 1321 | |
| 1239 | 1322 | if (!full_buffer && (size > avail_size)) |
| 1240 | 1323 | { |
| 1241 | 1324 | // when full_buffer is false, return any available data |
| … |
… |
int AudioOutputBase::GetAudioData(uchar *buffer, int size, bool full_buffer)
|
| 1261 | 1344 | { |
| 1262 | 1345 | if (fromFloats) |
| 1263 | 1346 | off = AudioOutputUtil::fromFloat(output_format, buffer, |
| 1264 | | RPOS, bdiff); |
| | 1347 | LRPOS, bdiff); |
| 1265 | 1348 | else |
| 1266 | 1349 | { |
| 1267 | | memcpy(buffer, RPOS, bdiff); |
| | 1350 | memcpy(buffer, LRPOS, bdiff); |
| 1268 | 1351 | off = bdiff; |
| 1269 | 1352 | } |
| 1270 | 1353 | |
| 1271 | 1354 | frag_size -= bdiff; |
| 1272 | | raud = 0; |
| | 1355 | *local_raud = 0; |
| 1273 | 1356 | } |
| 1274 | 1357 | if (frag_size > 0) |
| 1275 | 1358 | { |
| 1276 | 1359 | if (fromFloats) |
| 1277 | 1360 | AudioOutputUtil::fromFloat(output_format, buffer + off, |
| 1278 | | RPOS, frag_size); |
| | 1361 | LRPOS, frag_size); |
| 1279 | 1362 | else |
| 1280 | | memcpy(buffer + off, RPOS, frag_size); |
| | 1363 | memcpy(buffer + off, LRPOS, frag_size); |
| 1281 | 1364 | } |
| 1282 | 1365 | |
| 1283 | | raud += frag_size; |
| | 1366 | *local_raud += frag_size; |
| 1284 | 1367 | |
| 1285 | 1368 | // Mute individual channels through mono->stereo duplication |
| 1286 | 1369 | MuteState mute_state = GetMuteState(); |
diff --git a/mythtv/libs/libmyth/audiooutputbase.h b/mythtv/libs/libmyth/audiooutputbase.h
index 78fe44d..20cb9db 100644
|
a
|
b
|
class FreeSurround;
|
| 31 | 31 | class AudioOutputDigitalEncoder; |
| 32 | 32 | struct AVCodecContext; |
| 33 | 33 | |
| | 34 | class AsyncLooseLock |
| | 35 | { |
| | 36 | public: |
| | 37 | AsyncLooseLock() { head = tail = 0; } |
| | 38 | void Clear() { head = tail = 0; } |
| | 39 | void Ref() { head++; } |
| | 40 | bool TestAndDeref() { bool r; if ((r=(head != tail))) tail++; return r; } |
| | 41 | private: |
| | 42 | int head; |
| | 43 | int tail; |
| | 44 | }; |
| | 45 | |
| 34 | 46 | class AudioOutputBase : public AudioOutput, public QThread |
| 35 | 47 | { |
| 36 | 48 | public: |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 59 | 71 | int GetSWVolume(void); |
| 60 | 72 | |
| 61 | 73 | // timecode is in milliseconds. |
| 62 | | virtual bool AddFrames(void *buffer, int frames, long long timecode); |
| | 74 | virtual bool AddFrames(void *buffer, int frames, int64_t timecode); |
| 63 | 75 | |
| 64 | | virtual void SetTimecode(long long timecode); |
| | 76 | virtual void SetTimecode(int64_t timecode); |
| 65 | 77 | virtual bool IsPaused(void) const { return actually_paused; } |
| 66 | 78 | virtual void Pause(bool paused); |
| 67 | 79 | void PauseUntilBuffered(void); |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 69 | 81 | // Wait for all data to finish playing |
| 70 | 82 | virtual void Drain(void); |
| 71 | 83 | |
| 72 | | virtual int GetAudiotime(void); |
| 73 | | virtual int GetAudioBufferedTime(void); |
| | 84 | virtual int64_t GetAudiotime(void); |
| | 85 | virtual int64_t GetAudioBufferedTime(void); |
| 74 | 86 | |
| 75 | 87 | // Send output events showing current progress |
| 76 | 88 | virtual void Status(void); |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 85 | 97 | |
| 86 | 98 | static const uint kAudioSRCInputSize = 16384<<1; |
| 87 | 99 | static const uint kAudioSRCOutputSize = 16384<<3; |
| 88 | | /// Audio Buffer Size -- should be divisible by 12,10,8,6,4,2.. |
| 89 | | static const uint kAudioRingBufferSize = 1536000; |
| | 100 | /// Audio Buffer Size -- should be divisible by 32,24,16,12,10,8,6,4,2.. |
| | 101 | static const uint kAudioRingBufferSize = 3072000; |
| 90 | 102 | |
| 91 | 103 | protected: |
| 92 | 104 | // Following function must be called from subclass constructor |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 107 | 119 | virtual bool StartOutputThread(void); |
| 108 | 120 | virtual void StopOutputThread(void); |
| 109 | 121 | |
| 110 | | int GetAudioData(uchar *buffer, int buf_size, bool fill_buffer); |
| | 122 | int GetAudioData(uchar *buffer, int buf_size, bool fill_buffer, int *local_raud = NULL); |
| 111 | 123 | |
| 112 | 124 | void OutputAudioLoop(void); |
| 113 | 125 | |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 142 | 154 | bool passthru, enc, reenc; |
| 143 | 155 | |
| 144 | 156 | float stretchfactor; |
| | 157 | int eff_stretchfactor; // scaled to 100000 as effdsp is |
| 145 | 158 | AudioOutputSource source; |
| 146 | 159 | |
| 147 | 160 | bool killaudio; |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 157 | 170 | private: |
| 158 | 171 | int CopyWithUpmix(char *buffer, int frames, int &org_waud); |
| 159 | 172 | AudioOutputSettings *output_settingsraw; |
| | 173 | void SetAudiotime(int frames, int64_t timecode); |
| 160 | 174 | AudioOutputSettings *output_settings; |
| 161 | 175 | bool need_resampler; |
| 162 | 176 | SRC_STATE *src_ctx; |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 177 | 191 | |
| 178 | 192 | bool processing; |
| 179 | 193 | |
| 180 | | long long frames_buffered; |
| | 194 | int64_t frames_buffered; |
| 181 | 195 | |
| 182 | 196 | bool audio_thread_exists; |
| 183 | 197 | |
| … |
… |
class AudioOutputBase : public AudioOutput, public QThread
|
| 190 | 204 | QMutex avsync_lock; |
| 191 | 205 | |
| 192 | 206 | // timecode of audio leaving the soundcard (same units as timecodes) |
| 193 | | long long audiotime; |
| | 207 | int64_t audiotime; |
| 194 | 208 | |
| 195 | 209 | /* Audio circular buffer */ |
| 196 | 210 | int raud, waud; /* read and write positions */ |
| 197 | 211 | // timecode of audio most recently placed into buffer |
| 198 | | long long audbuf_timecode; |
| | 212 | int64_t audbuf_timecode; |
| | 213 | AsyncLooseLock reset_active; |
| 199 | 214 | |
| 200 | 215 | QMutex killAudioLock; |
| 201 | 216 | |
diff --git a/mythtv/libs/libmythfreesurround/freesurround.cpp b/mythtv/libs/libmythfreesurround/freesurround.cpp
index 5e8b1f5..aef65a3 100644
|
a
|
b
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 29 | 29 | using namespace std; |
| 30 | 30 | |
| 31 | 31 | #include "compat.h" |
| | 32 | #include "mythverbose.h" |
| 32 | 33 | #include "freesurround.h" |
| 33 | 34 | #include "el_processor.h" |
| 34 | 35 | |
| 35 | 36 | #include <QString> |
| 36 | 37 | #include <QDateTime> |
| 37 | 38 | |
| 38 | | #if 0 |
| 39 | | #define VERBOSE(args...) \ |
| 40 | | do { \ |
| 41 | | QDateTime dtmp = QDateTime::currentDateTime(); \ |
| 42 | | QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \ |
| 43 | | std::cout << dtime.toLocal8Bit().constData() << " " \ |
| 44 | | << QString(args).toLocal8Bit().constData() << std::endl; \ |
| 45 | | } while (0) |
| 46 | | #else |
| 47 | | #define VERBOSE(args...) |
| 48 | | #endif |
| 49 | | #if 0 |
| 50 | | #define VERBOSE1(args...) \ |
| 51 | | do { \ |
| 52 | | QDateTime dtmp = QDateTime::currentDateTime(); \ |
| 53 | | QString dtime = dtmp.toString("yyyy-MM-dd hh:mm:ss.zzz"); \ |
| 54 | | std::cout << dtime.toLocal8Bit().constData() << " " \ |
| 55 | | << QString(args).toLocal8Bit().constData() << std::endl; \ |
| 56 | | } while (0) |
| 57 | | #else |
| 58 | | #define VERBOSE1(args...) |
| 59 | | #endif |
| 60 | | |
| 61 | 39 | // our default internal block size, in floats |
| 62 | 40 | static const unsigned default_block_size = 8192; |
| 63 | 41 | // Gain of center and lfe channels in passive mode (sqrt 0.5) |
| … |
… |
FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode) :
|
| 161 | 139 | processed_size(0), |
| 162 | 140 | surround_mode(smode) |
| 163 | 141 | { |
| 164 | | VERBOSE(QString("FreeSurround::FreeSurround rate %1 moviemode %2") |
| | 142 | VERBOSE(VB_AUDIO+VB_EXTRA, QString("FreeSurround::FreeSurround rate %1 moviemode %2") |
| 165 | 143 | .arg(srate).arg(moviemode)); |
| 166 | 144 | |
| 167 | 145 | if (moviemode) |
| … |
… |
FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode) :
|
| 193 | 171 | channel_select++; |
| 194 | 172 | if (channel_select>=6) |
| 195 | 173 | channel_select = 0; |
| 196 | | VERBOSE(QString("FreeSurround::FreeSurround channel_select %1") |
| | 174 | VERBOSE(VB_AUDIO+VB_EXTRA, QString("FreeSurround::FreeSurround channel_select %1") |
| 197 | 175 | .arg(channel_select)); |
| 198 | 176 | #endif |
| 199 | | VERBOSE(QString("FreeSurround::FreeSurround done")); |
| | 177 | VERBOSE(VB_AUDIO+VB_EXTRA, QString("FreeSurround::FreeSurround done")); |
| 200 | 178 | } |
| 201 | 179 | |
| 202 | 180 | void FreeSurround::SetParams() |
| … |
… |
FreeSurround::fsurround_params::fsurround_params(int32_t center_width,
|
| 224 | 202 | |
| 225 | 203 | FreeSurround::~FreeSurround() |
| 226 | 204 | { |
| 227 | | VERBOSE(QString("FreeSurround::~FreeSurround")); |
| | 205 | VERBOSE(VB_AUDIO+VB_EXTRA, QString("FreeSurround::~FreeSurround")); |
| 228 | 206 | close(); |
| 229 | 207 | if (bufs) |
| 230 | 208 | { |
| 231 | 209 | bp.release((void*)1); |
| 232 | 210 | bufs = NULL; |
| 233 | 211 | } |
| 234 | | VERBOSE(QString("FreeSurround::~FreeSurround done")); |
| | 212 | VERBOSE(VB_AUDIO+VB_EXTRA, QString("FreeSurround::~FreeSurround done")); |
| 235 | 213 | } |
| 236 | 214 | |
| 237 | 215 | uint FreeSurround::putFrames(void* buffer, uint numFrames, uint numChannels) |
| … |
… |
uint FreeSurround::putFrames(void* buffer, uint numFrames, uint numChannels)
|
| 289 | 267 | break; |
| 290 | 268 | } |
| 291 | 269 | ic += numFrames; |
| 292 | | in_count = ic; |
| 293 | 270 | processed = process; |
| 294 | 271 | if (ic != bs) |
| | 272 | { |
| | 273 | // dont modify unless no processing is to be done |
| | 274 | // for audiotime consistency |
| | 275 | in_count = ic; |
| 295 | 276 | break; |
| 296 | | in_count = 0; |
| | 277 | } |
| | 278 | // process_block takes some time so dont update in and out count |
| | 279 | // before its finished so that Audiotime is correctly calculated |
| 297 | 280 | if (process) |
| 298 | 281 | process_block(); |
| | 282 | in_count = 0; |
| 299 | 283 | out_count = bs; |
| 300 | 284 | processed_size = bs; |
| 301 | 285 | break; |
| 302 | 286 | } |
| 303 | 287 | |
| 304 | | VERBOSE1(QString("FreeSurround::putFrames %1 %2 used %4 generated %5") |
| | 288 | VERBOSE(VB_AUDIO+VB_TIMESTAMP+VB_EXTRA, QString("FreeSurround::putFrames %1 #ch %2 used %4 generated %5") |
| 305 | 289 | .arg(numFrames).arg(numChannels).arg(i).arg(out_count)); |
| 306 | 290 | |
| 307 | 291 | return i; |
| … |
… |
uint FreeSurround::receiveFrames(void *buffer, uint maxFrames)
|
| 318 | 302 | switch (surround_mode) |
| 319 | 303 | { |
| 320 | 304 | case SurroundModePassive: |
| 321 | | for (uint i = 0; i < maxFrames; i++) |
| | 305 | for (i = 0; i < maxFrames; i++) |
| 322 | 306 | { |
| 323 | 307 | *output++ = bufs->l[outindex]; |
| 324 | 308 | *output++ = bufs->r[outindex]; |
| … |
… |
uint FreeSurround::receiveFrames(void *buffer, uint maxFrames)
|
| 341 | 325 | float *ls = &outputs[3][outindex]; |
| 342 | 326 | float *rs = &outputs[4][outindex]; |
| 343 | 327 | float *lfe = &outputs[5][outindex]; |
| 344 | | for (uint i = 0; i < maxFrames; i++) |
| | 328 | for (i = 0; i < maxFrames; i++) |
| 345 | 329 | { |
| 346 | 330 | *output++ = *l++; |
| 347 | 331 | *output++ = *r++; |
| … |
… |
uint FreeSurround::receiveFrames(void *buffer, uint maxFrames)
|
| 361 | 345 | float *ls = &bufs->ls[outindex]; |
| 362 | 346 | float *rs = &bufs->rs[outindex]; |
| 363 | 347 | float *lfe = &bufs->lfe[outindex]; |
| 364 | | for (uint i = 0; i < maxFrames; i++) |
| | 348 | for (i = 0; i < maxFrames; i++) |
| 365 | 349 | { |
| 366 | 350 | *output++ = *l++; |
| 367 | 351 | *output++ = *r++; |
| … |
… |
uint FreeSurround::receiveFrames(void *buffer, uint maxFrames)
|
| 376 | 360 | break; |
| 377 | 361 | } |
| 378 | 362 | out_count = oc; |
| 379 | | VERBOSE1(QString("FreeSurround::receiveFrames %1").arg(maxFrames)); |
| | 363 | VERBOSE(VB_AUDIO+VB_TIMESTAMP+VB_EXTRA, QString("FreeSurround::receiveFrames %1").arg(maxFrames)); |
| 380 | 364 | return maxFrames; |
| 381 | 365 | } |
| 382 | 366 | |
diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
index 260c361..a751f4f 100644
|
a
|
b
|
AvFormatDecoder::AvFormatDecoder(MythPlayer *parent,
|
| 232 | 232 | start_code_state(0xffffffff), |
| 233 | 233 | lastvpts(0), lastapts(0), |
| 234 | 234 | lastccptsu(0), |
| | 235 | firstvpts(0), firstvptsinuse(false), |
| 235 | 236 | using_null_videoout(use_null_videoout), |
| 236 | 237 | video_codec_id(kCodec_NONE), |
| 237 | 238 | no_hardware_decoders(no_hardware_decode), |
| … |
… |
void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
|
| 672 | 673 | if (decoded_video_frame) |
| 673 | 674 | GetPlayer()->DiscardVideoFrame(decoded_video_frame); |
| 674 | 675 | } |
| | 676 | |
| | 677 | if (doflush) |
| | 678 | { |
| | 679 | firstvpts = 0; |
| | 680 | firstvptsinuse = true; |
| | 681 | } |
| 675 | 682 | } |
| 676 | 683 | |
| 677 | 684 | void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset) |
| … |
… |
void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
|
| 2656 | 2663 | |
| 2657 | 2664 | gopset = false; |
| 2658 | 2665 | prevgoppos = 0; |
| | 2666 | firstvpts = |
| 2659 | 2667 | lastapts = lastvpts = lastccptsu = 0; |
| | 2668 | firstvptsinuse = true; |
| 2660 | 2669 | |
| 2661 | 2670 | // fps debugging info |
| 2662 | 2671 | float avFPS = normalized_fps(stream, context); |
| … |
… |
bool AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
|
| 2766 | 2775 | |
| 2767 | 2776 | gopset = false; |
| 2768 | 2777 | prevgoppos = 0; |
| | 2778 | firstvpts = |
| 2769 | 2779 | lastapts = lastvpts = lastccptsu = 0; |
| | 2780 | firstvptsinuse = true; |
| 2770 | 2781 | |
| 2771 | 2782 | // fps debugging info |
| 2772 | 2783 | float avFPS = normalized_fps(stream, context); |
| … |
… |
bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
|
| 2995 | 3006 | framesPlayed++; |
| 2996 | 3007 | |
| 2997 | 3008 | lastvpts = temppts; |
| | 3009 | if (!firstvpts && firstvptsinuse) |
| | 3010 | firstvpts = temppts; |
| 2998 | 3011 | |
| 2999 | 3012 | return true; |
| 3000 | 3013 | } |
| … |
… |
bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
|
| 3745 | 3758 | skipaudio = false; |
| 3746 | 3759 | } |
| 3747 | 3760 | |
| | 3761 | // skip any audio frames preceding first video frame |
| | 3762 | if (firstvptsinuse && firstvpts && (lastapts < firstvpts)) |
| | 3763 | { |
| | 3764 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, |
| | 3765 | LOC + QString("discarding early audio timecode %1 %2 %3") |
| | 3766 | .arg(pkt->pts).arg(pkt->dts).arg(lastapts)); |
| | 3767 | break; |
| | 3768 | } |
| | 3769 | firstvptsinuse = false; |
| | 3770 | |
| 3748 | 3771 | avcodeclock->lock(); |
| 3749 | 3772 | data_size = 0; |
| 3750 | 3773 | |
diff --git a/mythtv/libs/libmythtv/avformatdecoder.h b/mythtv/libs/libmythtv/avformatdecoder.h
index 23d990b..3bff3b3 100644
|
a
|
b
|
class AvFormatDecoder : public DecoderBase
|
| 262 | 262 | long long lastvpts; |
| 263 | 263 | long long lastapts; |
| 264 | 264 | long long lastccptsu; |
| | 265 | long long firstvpts; |
| | 266 | bool firstvptsinuse; |
| 265 | 267 | |
| 266 | 268 | bool using_null_videoout; |
| 267 | 269 | MythCodecID video_codec_id; |
diff --git a/mythtv/libs/libmythtv/mythplayer.cpp b/mythtv/libs/libmythtv/mythplayer.cpp
index 0c0f50b..7e9b12d 100644
|
a
|
b
|
MythPlayer::MythPlayer(bool muted)
|
| 235 | 235 | // Audio and video synchronization stuff |
| 236 | 236 | videosync(NULL), avsync_delay(0), |
| 237 | 237 | avsync_adjustment(0), avsync_avg(0), |
| | 238 | avsync_predictor(0), avsync_predictor_enabled(false), |
| 238 | 239 | refreshrate(0), |
| 239 | 240 | lastsync(false), repeat_delay(0), |
| 240 | 241 | // Time Code stuff |
| … |
… |
void MythPlayer::SetVideoParams(int width, int height, double fps,
|
| 870 | 871 | video_frame_rate = fps; |
| 871 | 872 | float temp_speed = (play_speed == 0.0f) ? |
| 872 | 873 | audio.GetStretchFactor() : play_speed; |
| 873 | | frame_interval = (int)(1000000.0f / video_frame_rate / temp_speed); |
| | 874 | SetFrameInterval(kScan_Progressive, 1.0 / (video_frame_rate * temp_speed)); |
| 874 | 875 | } |
| 875 | 876 | |
| 876 | 877 | if (videoOutput) |
| … |
… |
int MythPlayer::NextCaptionTrack(int mode)
|
| 1564 | 1565 | return NextCaptionTrack(nextmode); |
| 1565 | 1566 | } |
| 1566 | 1567 | |
| | 1568 | void MythPlayer::SetFrameInterval(FrameScanType scan, double frame_period) |
| | 1569 | { |
| | 1570 | frame_interval = (int)(1000000.0f * frame_period + 0.5f); |
| | 1571 | if (!avsync_predictor_enabled) |
| | 1572 | avsync_predictor = 0; |
| | 1573 | avsync_predictor_enabled = false; |
| | 1574 | |
| | 1575 | VERBOSE(VB_PLAYBACK, LOC + QString("SetFrameInterval ps:%1 scan:%2") |
| | 1576 | .arg(play_speed).arg(scan) |
| | 1577 | ); |
| | 1578 | if (play_speed < 1 || play_speed > 2 || refreshrate <= 0) |
| | 1579 | return; |
| | 1580 | |
| | 1581 | avsync_predictor_enabled = ((frame_interval-(frame_interval/200)) < refreshrate); |
| | 1582 | } |
| | 1583 | |
| | 1584 | void MythPlayer::ResetAVSync(void) |
| | 1585 | { |
| | 1586 | avsync_avg = 0; |
| | 1587 | if (!avsync_predictor_enabled || avsync_predictor >= refreshrate) |
| | 1588 | avsync_predictor = 0; |
| | 1589 | prevtc = 0; |
| | 1590 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V sync reset"); |
| | 1591 | } |
| | 1592 | |
| 1567 | 1593 | void MythPlayer::InitAVSync(void) |
| 1568 | 1594 | { |
| 1569 | 1595 | videosync->Start(); |
| … |
… |
void MythPlayer::InitAVSync(void)
|
| 1586 | 1612 | .arg(refreshrate).arg(frame_interval); |
| 1587 | 1613 | VERBOSE(VB_PLAYBACK, LOC + msg); |
| 1588 | 1614 | |
| | 1615 | SetFrameInterval(m_scan, 1.0 / (video_frame_rate * play_speed)); |
| | 1616 | |
| 1589 | 1617 | // try to get preferential scheduling, but ignore if we fail to. |
| 1590 | 1618 | myth_nice(-19); |
| 1591 | 1619 | } |
| 1592 | 1620 | } |
| 1593 | 1621 | |
| | 1622 | int64_t MythPlayer::AVSyncGetAudiotime(void) |
| | 1623 | { |
| | 1624 | int64_t currentaudiotime = 0; |
| | 1625 | if (normal_speed) |
| | 1626 | { |
| | 1627 | currentaudiotime = audio.GetAudioTime(); |
| | 1628 | } |
| | 1629 | return currentaudiotime; |
| | 1630 | } |
| | 1631 | |
| 1594 | 1632 | #define MAXDIVERGE 3.0f |
| 1595 | 1633 | #define DIVERGELIMIT 30.0f |
| 1596 | 1634 | void MythPlayer::AVSync(bool limit_delay) |
| 1597 | 1635 | { |
| 1598 | 1636 | float diverge = 0.0f; |
| 1599 | 1637 | int frameDelay = m_double_framerate ? frame_interval / 2 : frame_interval; |
| | 1638 | int vsync_delay_clock = 0; |
| | 1639 | int64_t currentaudiotime = 0; |
| | 1640 | |
| 1600 | 1641 | // attempt to reduce fps for standalone PIP |
| 1601 | 1642 | if (player_ctx->IsPIP() && framesPlayed % 2) |
| 1602 | 1643 | { |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1631 | 1672 | if (kScan_Detect == m_scan || kScan_Ignore == m_scan) |
| 1632 | 1673 | ps = kScan_Progressive; |
| 1633 | 1674 | |
| | 1675 | bool dropframe = false; |
| | 1676 | QString dbg; |
| | 1677 | |
| | 1678 | if (avsync_predictor_enabled) // && !prebuffering) |
| | 1679 | { |
| | 1680 | avsync_predictor += frame_interval; |
| | 1681 | if (avsync_predictor >= refreshrate) |
| | 1682 | { |
| | 1683 | int refreshperiodsinframe = avsync_predictor/refreshrate; |
| | 1684 | avsync_predictor -= refreshrate * refreshperiodsinframe; |
| | 1685 | } |
| | 1686 | else |
| | 1687 | { |
| | 1688 | dropframe = true; |
| | 1689 | dbg = "A/V predict drop frame, "; |
| | 1690 | } |
| | 1691 | } |
| | 1692 | |
| 1634 | 1693 | if (diverge < -MAXDIVERGE) |
| 1635 | 1694 | { |
| | 1695 | dropframe = true; |
| 1636 | 1696 | // If video is way behind of audio, adjust for it... |
| 1637 | | QString dbg = QString("Video is %1 frames behind audio (too slow), ") |
| | 1697 | dbg = QString("Video is %1 frames behind audio (too slow), ") |
| 1638 | 1698 | .arg(-diverge); |
| | 1699 | } |
| 1639 | 1700 | |
| | 1701 | if (dropframe) |
| | 1702 | { |
| 1640 | 1703 | // Reset A/V Sync |
| 1641 | 1704 | lastsync = true; |
| 1642 | 1705 | |
| | 1706 | currentaudiotime = AVSyncGetAudiotime(); |
| | 1707 | |
| 1643 | 1708 | if (buffer && !using_null_videoout && |
| 1644 | 1709 | videoOutput->hasHWAcceleration() && |
| 1645 | 1710 | !videoOutput->IsSyncLocked()) |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1664 | 1729 | if (buffer) |
| 1665 | 1730 | videoOutput->PrepareFrame(buffer, ps, osd); |
| 1666 | 1731 | |
| 1667 | | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString("AVSync waitforframe %1 %2") |
| | 1732 | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + QString("AVSync waitforframe %1 %2") |
| 1668 | 1733 | .arg(avsync_adjustment).arg(m_double_framerate)); |
| 1669 | | videosync->WaitForFrame(frameDelay + avsync_adjustment + repeat_delay); |
| 1670 | | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, "AVSync show"); |
| | 1734 | vsync_delay_clock = videosync->WaitForFrame(frameDelay + avsync_adjustment + repeat_delay); |
| | 1735 | currentaudiotime = AVSyncGetAudiotime(); |
| | 1736 | VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + "AVSync show"); |
| 1671 | 1737 | videoOutput->Show(ps); |
| 1672 | 1738 | |
| 1673 | 1739 | if (videoOutput->IsErrored()) |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1696 | 1762 | videoOutput->PrepareFrame(buffer, ps, osd); |
| 1697 | 1763 | |
| 1698 | 1764 | // Display the second field |
| 1699 | | videosync->WaitForFrame(frameDelay + avsync_adjustment); |
| | 1765 | //videosync->AdvanceTrigger(); |
| | 1766 | vsync_delay_clock = videosync->WaitForFrame(frameDelay + avsync_adjustment); |
| 1700 | 1767 | videoOutput->Show(ps); |
| 1701 | 1768 | } |
| 1702 | 1769 | |
| 1703 | 1770 | repeat_delay = frame_interval * buffer->repeat_pict * 0.5; |
| 1704 | 1771 | |
| 1705 | 1772 | if (repeat_delay) |
| 1706 | | VERBOSE(VB_TIMESTAMP, QString("A/V repeat_pict, adding %1 repeat " |
| | 1773 | VERBOSE(VB_TIMESTAMP, LOC + QString("A/V repeat_pict, adding %1 repeat " |
| 1707 | 1774 | "delay").arg(repeat_delay)); |
| 1708 | 1775 | } |
| 1709 | 1776 | else |
| 1710 | 1777 | { |
| 1711 | | videosync->WaitForFrame(frameDelay); |
| | 1778 | vsync_delay_clock = videosync->WaitForFrame(frameDelay); |
| | 1779 | currentaudiotime = AVSyncGetAudiotime(); |
| 1712 | 1780 | } |
| 1713 | 1781 | |
| 1714 | 1782 | if (output_jmeter) |
| 1715 | | output_jmeter->RecordCycleTime(); |
| | 1783 | { |
| | 1784 | if (output_jmeter->RecordCycleTime()) |
| | 1785 | { |
| | 1786 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V avsync_delay: %1, " |
| | 1787 | "avsync_avg: %2") |
| | 1788 | .arg(avsync_delay / 1000).arg(avsync_avg / 1000) |
| | 1789 | ); |
| | 1790 | } |
| | 1791 | } |
| 1716 | 1792 | |
| 1717 | 1793 | avsync_adjustment = 0; |
| 1718 | 1794 | |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1720 | 1796 | { |
| 1721 | 1797 | // If audio is way behind of video, adjust for it... |
| 1722 | 1798 | // by cutting the frame rate in half for the length of this frame |
| 1723 | | avsync_adjustment = refreshrate; |
| | 1799 | //avsync_adjustment = refreshrate; |
| | 1800 | avsync_adjustment = frame_interval; |
| 1724 | 1801 | lastsync = true; |
| 1725 | 1802 | VERBOSE(VB_PLAYBACK, LOC + |
| 1726 | 1803 | QString("Video is %1 frames ahead of audio,\n" |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1730 | 1807 | if (audio.HasAudioOut() && normal_speed) |
| 1731 | 1808 | { |
| 1732 | 1809 | long long currentaudiotime = audio.GetAudioTime(); |
| 1733 | | VERBOSE(VB_TIMESTAMP, QString( |
| | 1810 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString( |
| 1734 | 1811 | "A/V timecodes audio %1 video %2 frameinterval %3 " |
| 1735 | | "avdel %4 avg %5 tcoffset %6") |
| | 1812 | "avdel %4 avg %5 tcoffset %6" |
| | 1813 | " avp %7 avpen %8" |
| | 1814 | " avdc %9" |
| | 1815 | ) |
| 1736 | 1816 | .arg(currentaudiotime) |
| 1737 | 1817 | .arg(buffer->timecode) |
| 1738 | 1818 | .arg(frame_interval) |
| 1739 | | .arg(buffer->timecode - currentaudiotime) |
| | 1819 | .arg(buffer->timecode - currentaudiotime - (int)(vsync_delay_clock*audio.GetStretchFactor()+500)/1000) |
| 1740 | 1820 | .arg(avsync_avg) |
| 1741 | 1821 | .arg(tc_wrap[TC_AUDIO]) |
| | 1822 | .arg(avsync_predictor) |
| | 1823 | .arg(avsync_predictor_enabled) |
| | 1824 | .arg(vsync_delay_clock) |
| 1742 | 1825 | ); |
| 1743 | 1826 | if (currentaudiotime != 0 && buffer->timecode != 0) |
| 1744 | 1827 | { // currentaudiotime == 0 after a seek |
| 1745 | 1828 | // The time at the start of this frame (ie, now) is given by |
| 1746 | 1829 | // last->timecode |
| 1747 | | int delta = (int)((buffer->timecode - prevtc)/play_speed) - (frame_interval / 1000); |
| 1748 | | prevtc = buffer->timecode; |
| 1749 | | //cerr << delta << " "; |
| 1750 | | |
| 1751 | | // If the timecode is off by a frame (dropped frame) wait to sync |
| 1752 | | if (delta > (int) frame_interval / 1200 && |
| 1753 | | delta < (int) frame_interval / 1000 * 3 && |
| 1754 | | prevrp == 0) |
| | 1830 | if (prevtc != 0) |
| 1755 | 1831 | { |
| 1756 | | // wait an extra frame interval |
| 1757 | | avsync_adjustment += frame_interval; |
| | 1832 | int delta = (int)((buffer->timecode - prevtc)/play_speed) - (frame_interval / 1000); |
| | 1833 | // If the timecode is off by a frame (dropped frame) wait to sync |
| | 1834 | if (delta > (int) frame_interval / 1200 && |
| | 1835 | delta < (int) frame_interval / 1000 * 3 && |
| | 1836 | prevrp == 0) |
| | 1837 | { |
| | 1838 | // wait an extra frame interval |
| | 1839 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V delay %1").arg(delta)); |
| | 1840 | avsync_adjustment += frame_interval; |
| | 1841 | //videosync->AdvanceTrigger(); |
| | 1842 | //if (m_double_framerate) |
| | 1843 | // videosync->AdvanceTrigger(); |
| | 1844 | } |
| 1758 | 1845 | } |
| | 1846 | prevtc = buffer->timecode; |
| 1759 | 1847 | prevrp = buffer->repeat_pict; |
| 1760 | 1848 | |
| 1761 | | avsync_delay = (buffer->timecode - currentaudiotime) * 1000;//usec |
| | 1849 | avsync_delay = (buffer->timecode - currentaudiotime) * 1000 - (int)(vsync_delay_clock*audio.GetStretchFactor()); //usec |
| 1762 | 1850 | // prevents major jitter when pts resets during dvd title |
| 1763 | 1851 | if (avsync_delay > 2000000 && limit_delay) |
| 1764 | 1852 | avsync_delay = 90000; |
| 1765 | 1853 | avsync_avg = (avsync_delay + (avsync_avg * 3)) / 4; |
| 1766 | 1854 | |
| | 1855 | int avsync_used = avsync_avg; |
| | 1856 | if (labs(avsync_used) > labs(avsync_delay)) |
| | 1857 | avsync_used = avsync_delay; |
| | 1858 | |
| 1767 | 1859 | /* If the audio time codes and video diverge, shift |
| 1768 | 1860 | the video by one interlaced field (1/2 frame) */ |
| 1769 | 1861 | if (!lastsync) |
| 1770 | 1862 | { |
| 1771 | | if (avsync_avg > frame_interval * 3 / 2) |
| | 1863 | if (avsync_used > refreshrate) |
| 1772 | 1864 | { |
| 1773 | 1865 | avsync_adjustment += refreshrate; |
| 1774 | | lastsync = true; |
| | 1866 | //lastsync = true; |
| | 1867 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V avg high extend"); |
| 1775 | 1868 | } |
| 1776 | | else if (avsync_avg < 0 - frame_interval * 3 / 2) |
| | 1869 | else if (avsync_used < 0 - refreshrate) |
| 1777 | 1870 | { |
| 1778 | 1871 | avsync_adjustment -= refreshrate; |
| 1779 | | lastsync = true; |
| | 1872 | //lastsync = true; |
| | 1873 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + "A/V avg high skip"); |
| 1780 | 1874 | } |
| 1781 | 1875 | } |
| 1782 | 1876 | else |
| … |
… |
void MythPlayer::AVSync(bool limit_delay)
|
| 1784 | 1878 | } |
| 1785 | 1879 | else |
| 1786 | 1880 | { |
| 1787 | | avsync_avg = 0; |
| | 1881 | ResetAVSync(); |
| 1788 | 1882 | } |
| 1789 | 1883 | } |
| | 1884 | else |
| | 1885 | { |
| | 1886 | VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC + QString("A/V no sync proc ns:%1").arg(normal_speed)); |
| | 1887 | } |
| 1790 | 1888 | } |
| 1791 | 1889 | |
| 1792 | 1890 | void MythPlayer::DisplayPauseFrame(void) |
| … |
… |
void MythPlayer::ChangeSpeed(void)
|
| 3018 | 3116 | } |
| 3019 | 3117 | |
| 3020 | 3118 | float temp_speed = (play_speed == 0.0) ? audio.GetStretchFactor() : play_speed; |
| 3021 | | frame_interval = (int) (1000000.0f * ffrew_skip / video_frame_rate / |
| 3022 | | temp_speed); |
| | 3119 | SetFrameInterval(m_scan, ffrew_skip / (video_frame_rate * temp_speed)); |
| 3023 | 3120 | |
| 3024 | 3121 | VERBOSE(VB_PLAYBACK, LOC + "Play speed: " + |
| 3025 | 3122 | QString("rate: %1 speed: %2 skip: %3 => new interval %4") |
| … |
… |
void MythPlayer::ClearAfterSeek(bool clearvideobuffers)
|
| 3331 | 3428 | commBreakMap.SetTracker(framesPlayed); |
| 3332 | 3429 | commBreakMap.ResetLastSkip(); |
| 3333 | 3430 | needNewPauseFrame = true; |
| | 3431 | ResetAVSync(); |
| 3334 | 3432 | } |
| 3335 | 3433 | |
| 3336 | 3434 | void MythPlayer::SetPlayerInfo( |
diff --git a/mythtv/libs/libmythtv/mythplayer.h b/mythtv/libs/libmythtv/mythplayer.h
index 5b662c2..8495402 100644
|
a
|
b
|
class MPUBLIC MythPlayer
|
| 475 | 475 | void WrapTimecode(long long &timecode, TCTypes tc_type); |
| 476 | 476 | void InitAVSync(void); |
| 477 | 477 | virtual void AVSync(bool limit_delay = false); |
| | 478 | void ResetAVSync(void); |
| | 479 | int64_t AVSyncGetAudiotime(void); |
| | 480 | void SetFrameInterval(FrameScanType scan, double speed); |
| 478 | 481 | void FallbackDeint(void); |
| 479 | 482 | void CheckExtraAudioDecode(void); |
| 480 | 483 | |
| … |
… |
class MPUBLIC MythPlayer
|
| 656 | 659 | int avsync_delay; |
| 657 | 660 | int avsync_adjustment; |
| 658 | 661 | int avsync_avg; |
| | 662 | int avsync_predictor; |
| | 663 | bool avsync_predictor_enabled; |
| 659 | 664 | int refreshrate; |
| 660 | 665 | bool lastsync; |
| 661 | 666 | bool decode_extra_audio; |
diff --git a/mythtv/libs/libmythtv/vsync.cpp b/mythtv/libs/libmythtv/vsync.cpp
index fb6d017..6123cd1 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 d506ada..92d1fb9 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 */ |
diff --git a/mythtv/programs/mythtranscode/transcode.cpp b/mythtv/programs/mythtranscode/transcode.cpp
index a2105eb..468f714 100644
|
a
|
b
|
class AudioReencodeBuffer : public AudioOutput
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // timecode is in milliseconds. |
| 85 | | virtual bool AddFrames(void *buffer, int frames, long long timecode) |
| | 85 | virtual bool AddFrames(void *buffer, int frames, int64_t timecode) |
| 86 | 86 | { |
| 87 | 87 | int freebuf = bufsize - audiobuffer_len; |
| 88 | 88 | |
| … |
… |
class AudioReencodeBuffer : public AudioOutput
|
| 111 | 111 | return true; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | | virtual void SetTimecode(long long timecode) |
| | 114 | virtual void SetTimecode(int64_t timecode) |
| 115 | 115 | { |
| 116 | 116 | last_audiotime = timecode; |
| 117 | 117 | } |
| … |
… |
class AudioReencodeBuffer : public AudioOutput
|
| 132 | 132 | // Do nothing |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | | virtual int GetAudiotime(void) |
| | 135 | virtual int64_t GetAudiotime(void) |
| 136 | 136 | { |
| 137 | 137 | return last_audiotime; |
| 138 | 138 | } |