ReClock by SlySoft, Inc.
Web site:
http://www.slysoft.com
Please take time to test ReClock, and report success or failures to make it work with your PC. Have a look on changelog.txt and on my website for latest modifications.
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
What is ReClock?
----------------------------------------------------------------------------------------------------------------------------------------
Said simply the purpose of ReClock is to definitely get rid (I hope) of jerky playback of AVI and MPEG material on a PC (or a PC connected to a TV).
ReClock is born from my own frustration. I have a fast PC, a good video card, and when I play a DVD or DIVX on my brand new Home Cinema, I get dropped frames here and there for no reason, or a completely jerky and un-watch able movie. This is very annoying.
The following sections will give you a complete and I hope clear explanation of what cause jerky playback, and how ReClock will try to solve these problems.
The last section will give you the instructions on how to install and use ReClock.
----------------------------------------------------------------------------------------------------------------------------------------
The not so great history of frame rates
----------------------------------------------------------------------------------------------------------------------------------------
In basic terms, a video can be thought of as being made up of numerous snapshots, called frames. The frame rate is the number of frames displayed each second
As you know there exist 3 common broadcasting formats: cinema, TV, and computers
Cinema is the oldest. It is the format of the movies you see in your favourite theater. Cinema movies play at 24 frames per second ( fps). It’s very simple: every 1/24th of a second, your see a new frame. This is also called “progressive scan”.
A television, however, does not deal with video in terms of frames. Instead, it displays video using half-frames, called fields. Each frame contains exactly two fields. One field is made up of the odd horizontal lines in a frame. This is called the odd field or the top field since it contains the top line of the image. The other field is made up of the even horizontal lines in a frame. This is called the even field or bottom field. This way to broadcast video is called “interlaced scan”
Now there are three common TV standards: PAL, SECAM and NTSC. All of them use “interlaced scan”.
Let’s start with PAL, which is the European TV standard. It is also used for DVD and DIVX material. PAL material is played at 25 fps (or 50 fields per second). You see the first problem here: how can we play a 24 fps movie on a 50 fields per second PAL TV? Well first of all, we can present each movie frame two consecutive times to make the movie play at 48 fields per second. But playing the movie like this would give jerky playback every second because one movie picture would be missing. So the movie is just played 50/48 times faster to match 50 fields per second. So a cinema movie that has duration of 60mn plays on PAL in 57mn36 seconds. That’s why movies you watch on your PAL TV are always a bit shorter in time.
SECAM is the French brother of PAL and works exactly the same way. It is still used in France because it gives better colours when broadcasted by radio waves (less sensitive to noise).
And now here is NTSC, the American brother of PAL. NTSC is also used for DVD and DIVX material, and plays somewhere near 29.97 fps (to be exact it is 4.5 MHz/286/525). You see a bigger problem here. How can we play cinema movies on NTSC. Accelerate them? Sure no, because you would notice that the film plays much too fast (a 60mn movie would play in 48mn3s). So NTSC engineers came with a solution called “telecine” or “3:2 pulldown” which is quite complicated. To convert a film that runs at 24 fps to run at 29.97 fps, it is first necessary to slow down the video by 0.1% to 23.976 fps. Then approximately 6 frames are added to the video each second, bringing the frame rate to 29.97 fps. This is done by adding one extra frame to each group of 4 film frames. Although they could simply duplicate 1 out of every 4 frames to produce the extra frame, this method is not used. This is because the duplication of one frame would cause that frame to be displayed for twice as long as the other 3 frames, which leads to jerkier motion. Fortunately, film producers can make use of the field-based nature of video to more gradually introduce the extra frame. Instead of adding a whole new frame at once, 2 fields are introduced separately to each group of 4 film frames. Since 2 fields make up a frame, this method is equivalent to adding 1 new frame. However, since the 2 duplicated fields are not added at the same time, this reduces the jerkiness of the video.
Let’s finish with computers. Computers are quite simple, they just do work like cinema and use “progressive scan”. But they use many more frame rates: 60 Hz, 75 Hz, 85 Hz, 100 Hz when watching your monitor, 50 Hz when connected to a PAL TV, and 60 Hz when connected to a NTSC TV. To obtain a smooth playback on a PC you just have to make sure that your video card uses a refresh rate that is an exact multiple of the movie you play. You can already see that PAL can be played fine at 50 Hz, 75 Hz or 100 Hz, but NTSC cannot be played without begin jerky because all we have is 60 fps which is not a multiple of 29.97 fps.
----------------------------------------------------------------------------------------------------------------------------------------
The nightmare of badly born DIVX
----------------------------------------------------------------------------------------------------------------------------------------
DIVX for the most are created from DVD material, and DVD material generally comes from movie material.
DIVX created from PAL material are generally fine, because they were created from 24 fps material simply accelerated to 25 fps or from direct 25 fps material. PAL is a good guy.
And now you have telecine for NTSC. Since telecine can only be displayed correctly on “interlaced scan” hardware, it must be removed for PC playback. This operation is called “inverse telecine”. Doing this on a film will revert the frame rate of the movie to 23.976 fps. Funny, but … jerky on every PC you will use to play the movie.
Some people that create DIVX from NTSC don’t even know that inverse telecine must be done. So those DIVX stays at 29.97 fps and will have artefacts when watched on your PC because interlacing artefacts do not compress well at all.
And to add another thing to the story, some DIVX has sound and movie sync problems that are solved by … modifying the frame rate a little bit so the movie duration matches the sound track duration. This is a quick and dirty way to do it. Imagine a 25 fps DIVX that is modified to play at 25.001 fps; well every 1000 frames (that’s only 40 seconds) the playback will become jerky.
----------------------------------------------------------------------------------------------------------------------------------------
The bigger nightmare of PC hardware
----------------------------------------------------------------------------------------------------------------------------------------
When you connect your PAL DVD player to your TV, things are really simple. Your player read the movie from the disc at 25 fps, and sends the signal to the TV at 25 fps.
Now imagine that your player is a little too fast and send the movie to the TV at 25.01 fps. What will happen? Jerkiness? No … In fact your TV is smart, and will stay synchronised with your player as long at the player plays near 25 fps. To be more precise no player in the world plays the movie exactly at 25 fps because clocks are never accurate. So every player play the movie “near” 25 fps, but this is no problem for your TV since it is locked to the video signal it receive.
Now when you play a DIVX on your PC and watch it on your PC what happens? First of all, if the refresh rate of your monitor is not a multiple of the frame rate of your DIVX, jerkiness will happen for sure. Do you remember that DIVX can be 24 fps, 25 fps, 23.976 fps, 29.97 fps, or even 25.001 fps? This is the first and main cause of jerky playback.
The other source of jerkiness is much more subtle and harder to understand. Suppose you play a 25 fps movie on you PC. Well your PC isn’t smarter that any DVD player, it will play it “near” 25 fps because it uses its internal clock, which is inaccurate. But it should not be a problem because DVD players also do that? Bad luck, it is a very big problem, because your video card does not synchronise its speed with the speed of the movie like a TV would do with a DVD player. In a PC the clocks used in the video card (to show the video) and in the PC (to play the video) are completely distinct, thus there always is deviation between them, and then jerkiness is inevitable.
----------------------------------------------------------------------------------------------------------------------------------------
The better history of DirectShow
----------------------------------------------------------------------------------------------------------------------------------------
DirectShow is the DirectX component that plays or record video and audio on your PC. DirectShow contains many modules called “filters” and connect them in a “filter graph” to finally render the movie.
Let’s take an example, with the steps needed to play a DIVX movie:
- First you need to demux the audio and video from the AVI file: one filter will do that and will produce two streams (audio + video)
- Then you need to decode audio stream (MP3 for example): a MP3 decoder filter will do that
- You also need to decode video: a DIVX decoder filter will do that
- You must render the decoded movie in a window: a video renderer filter is needed
- You must play decoded audio: an audio renderer is used.
DirectShow was cleverly designed because it will automatically search and find what filters are the best to render a movie. For example, the audio and video renderers are completely generic (they are provided by Microsoft) and will eat the output of every decoder filter in the world.
That’s one reason why there exist many DIVX players out there. A player is only a nutshell where filter graphs are built and run.
Now, DirectShow has another interesting feature. When building a filter graph, it set up a “reference clock” that is used to provide a unique time to all the filters in the graph. All filters will play their stuff at the speed of the reference clock.
The default reference clock in DirectShow is provided by the Microsoft sound renderer. Why? Because in order to play sound correctly, your sound card must receive samples exactly at the speed they will be played. So the default reference clock is in fact synced to a hardware clock somewhere in your sound card. Video frames just follow this clock, making jerkiness inevitable.
You may ask why Microsoft made this choice. They could have chosen to sync the reference clock to the video and all would be nice. I suppose they didn’t for at least two reasons:
- Video cards do not have a high-resolution hardware timer available to make a clock.
- Sound playback would become problematic since the sound card would not receive its samples at the speed they would be played. So the sound would not stay synchronised with video
----------------------------------------------------------------------------------------------------------------------------------------
Here comes ReClock
----------------------------------------------------------------------------------------------------------------------------------------
Now I think you understand what does ReClock. It provides a reference clock that is synchronised with your video hardware. How? By replacing entirely the default sound renderer with a new rewritten DirectShow filter that is somehow cleverer.
But by doing so, ReClock must solve the two problems we saw in the last section:
- Video cards do not have a high-resolution hardware timer available to make a clock: well, this is not completely true, since many of them have something that will help us. ReClock provides a reference clock based on a high-resolution timer based on hardware on your motherboard or your processor. Let’s call this clock the “system clock”. Then, ReClock will correct the system clock with information gathered in real time from your video card if they are available. How it does that will be my little secret
- Sound playback would become problematic: this is true. How can we solve that? By varying the speed of the sound in real time to force the sound to stay in sync with the video. This is another reason why we replace the sound renderer with our own one. There are two ways to change sound speed: playback speed and pitch. Normally, ReClock will choose to change the pitch of the sound in real time by adjusting the audio clock because it’s quite easy to do and does not degrade too much sound quality. It also allows very fine tuning on the video vs. sound synchronisation. This will however change a little bit the sound you will hear, but your ears should not notice that with such a low correction. Starting with version 1.4, you can also ask ReClock to combine audio pitch change with audio time stretching witch allow to change the playback speed without noticeable pitch artefacts.
Another thing that does ReClock is to change the global playback rate of the movie in order to match its frame rate to a multiple of the refresh rate of your monitor. Here is how it does that:
- CINEMA mode is detected if the monitor has a refresh rate that is a multiple of 24 Hz (72 Hz, etc…) and if the movie has a frame rate between 23.75 and 24.25. Then the playback rate of the movie (including sound) is modified to match exactly 24 fps.
- PAL mode is detected if CINEMA mode wasn’t possible and if the monitor has a refresh rate that is a multiple of 25 Hz (50 Hz, 75 Hz, 100 Hz, etc…) and if the movie has a frame rate between 23.75 and 25.25. Then the playback rate of the movie (including sound) is modified to match exactly 25 fps.
- NTSC mode is detected if the monitor has a refresh rate that is a multiple of 30 Hz (60 Hz, 120 Hz, etc…). Then the playback rate of the movie (including sound) is modified to match exactly 30 fps.
- CUSTOM mode is detected when the frame rate of the movie +/- 2% is a multiple of the monitor refresh rate. Then the playback rate of the movie (including sound) is modified to match exactly a multiple of the monitor refresh rate.
Finally if nothing matched, ReClock will try to match the playback rate is not modified, but the reference clock is still synchronised to the video card in order to obtain a stable clock.
Side note: you may ask if NTSC will work well with TV since PC does 30 fps where the TV wants 29.97 fps. That’s a good question to which I do not have a complete response, but strangely I observed that when I use ReClock with the excellent TvTool in NTSC mode with a GeForce 3, I get a clock correction that make the reference clock work near … 29.97 fps. So it seems that when the PC uses TV out in NTSC mode, the refresh rate is not 60 Hz, but 29.97*2 Hz, but I’m not sure at all of that, since it may be a coincidence. Anyway playing NTSC at 30 fps on a NTSC TV should not be a problem; as you may know NTSC was originally a black/white standard at 30 Hz that became 29.97 Hz for technical reasons when colour appeared.
So by adjusting the reference clock to match your video card, adjusting the sound to stay in sync with picture, and modifying the global playback rate, ReClock should allow a smooth movie experience on your monitor and even on your TV.
But as I said before, ReClock will not work with all PC because it uses some functions that are not available on all of them. Firstly your PC must support high-resolution timers (nearly every modern PC has them). Secondly, your video card must support some special calls. I know that most nVidia, ATI, Intel i815 and Matrox G200 do support those calls, but some cheaper or older cards may not. If those functions are not supported, ReClock will not load at all or will display an error message and the Microsoft audio renderer will be used.
Moreover, ReClock has some constraints you need to understand:
- ReClock requires DirectX 9.0 or later.
- ReClock will load and work only if your movie has sound, because ReClock is an audio renderer.
- ReClock will not always find the frame rate of the material. For example streaming will never be supported.
- When your run ReClock for the first time for a specific resolution and refresh rate, the reference clock will be inaccurate during the first minutes of playback while being corrected. This can cause jerky playback at the beginning of the movie. The time to obtain an accurate clock will depend on many factors (initial error of clock, PC speed …). During this period, you will see a flashing yellow/red icon in the system tray. After this adjustment the icon will become green (or yellow), and ReClock will store the correction in the registry, so when you will open another media, the clock correction parameters will be retrieved.
- Changing the playback speed can be noticeable on sound especially when 23.976 fps material is up rated to 25 fps. Doing so will accelerate the sound by 4%, and sound will seem to be a little high pitched. If you don’t like that, you can enable the “audio time stretching” function (explained later in this document).
----------------------------------------------------------------------------------------------------------------------------------------
Let’s be a little more technical
----------------------------------------------------------------------------------------------------------------------------------------
Now that you know what DirectShow is and what does ReClock, I will explain here some technical knowledge that will be useful to fully use ReClock.
Originally, Microsoft designed one DirectShow audio renderer called the “WaveOut renderer”. This renderer was based on an old audio technology dating back to Windows 3.0 (let’s call it the Wave API). Later, when DirectX started to emerge, Microsoft designed a new audio renderer called the “DirectSound renderer” which is based on DirectSound technology (the DirectSound API). The later should provide better sound quality with less CPU overhead and less latency. On a “normal” PC with DirectX 9.0 or later, the “DirectSound renderer” is generally automatically used.
At the same time DirectSound emerged, PC started to be used to play DVD’s. As you know, nearly all DVD’s have a 5.1 Dolby or DTS soundtrack (i.e. 6 independent audio channels), that can be played on multiple speakers by your PC and the appropriate audio card, or send to an external AC3 decoder/amplifier with an SPDIF compatible soundcard. You can see those are two different ways to handle sound. When you play some media (MP3, DIVX, and DVD) and your soundcard provides directly the sound to the speakers, you are using PCM (Pulse Coded Modulation) sound, and I will call this “PCM mode”. When you are sending an AC3 or DTS stream over SPDIF to an external decoder/amplifier you’re doing AC3 pass-through mode, and I will call this “SPDIF mode”.
PCM and SPDIF modes are very different by nature:
- In PCM mode, ReClock receives the raw audio waves in digital form and transmits them to the audio card, but it can tweak them because it understands what is inside. So the sound can be reshaped (pitch or playback rate can be changed, and dynamic compression can be applied). Altering the playback rate allows ReClock to re sync the sound with the video. Playing a video at 25 fps with a sound that was “designed” to play at 24 fps is no problem.
- Now in SPDIF mode, ReClock receive a binary encoded AC3 or DTS stream and transmit it to the external decoder/amplifier via SPDIF link. But it can’t understand what is inside because this content needs to be decoded to be understood and played (that’s the purpose of an AC3 decoder). So how can ReClock alter the playback rate in SPDIF? Well, in fact it can’t do it nicely. AC3 frames are divided into chunks that have a sound duration of 32ms (a chunk is also called a frame or packet). Each frame is independent of the previous and of the next. To shorten the sound, ReClock drop frames, and to lengthen the sound, ReClock repeat frames. This is called the drop/repeat algorithm. Usually dropping or repeating an AC3 frame is not noticeable if it does not happen too often (remember a frame is only 32ms long), but it is not possible to play a media at a speed that is very far from its original speed (playing a 24 fps file at 25 fps would make a frame drop every 25 frame and it is very noticeable). That’s why in SPDIF mode, ReClock will only accept to alter the speed of media files very slightly (it won’t try to accelerate a 24 fps file to 25 fps).
Now DirectShow also have its oddities. When the “WaveOut renderer” was the only way to play sound, it was extended to handle SPDIF so it could take PCM or SPDIF input. When the “DirectSound renderer” appeared, it was plagued with problem in SPDIF handling, and many audio card manufacturers also had problems to handle SPDIF with DirectSound. So, this renderer is not commonly used to handle SPDIF. From this, you see that the best way to handle PCM sound is to use DirectSound and the best way to handle SPDIF sound is to use WaveOut. So, as ReClock needs to handle both PCM and SPDIF modes, it provides you the two worlds: WaveOut API and DirectSound API, so you have the choice. In this regard, ReClock is a hybrid audio renderer. Please have a look on a section “How to optimize SPDIF output” for a method that will allow you to minimize drop/repeats in SPDIF mode.
Version 1.6 also comes with a new audio renderer called "Kernel Streaming". This API is the lower one available in Windows to produce sound. It talks directly to audio WDM cards drivers, and has many advantages :
- lower possible audio latency
- complete bypass of the windows internal audio resampler that degrades sound quality
... and some disadvantages :
- no audio volume control
- will not work with every soundcard in ReClock especially when streaming AC3 sound
So if you want top notch audio quality, you can use Kernel Streaming with ReClock.
Let’s now speak about video renderers inside DirectShow. Just like for sound, Microsoft made with time different versions of video renderers:
- First the “Old renderer”: it uses old video technology is slow and should not be used anymore.
- Then the “Overlay mixer” came: this one uses “overlay” capability of video cards based on DirectDraw. Overlay is a technology that allows an application to display efficiently and with good quality pictures on screen but it has limitations: not all cards can do it, and you are limited to one video on screen (one overlay) at one time; also overlay can cause problems when using TV output. Besides its limitations, the overlay mixer is still widely used today
- When Windows XP came out, the Video Mixing Renderer appeared, also called the “VMR7”: this one is based on Direct3D, and has no limitations. But your card must be able to handle it (recent card and recent drivers). VMR7 is used by Windows Media Player on XP.
- And finally when DirectX 9.0 appeared, a second and distinct version of the VMR came out and this one is called the “VMR9”: it is an up rated VMR7 renderer with even more capabilities. You must use a good player if you want to try this one (for example Zoom Player)
- Some players like BSPlayer have their own video renderer (based on overlay for BSPlayer).