Interlacing and deinterlacing

From Avisynth wiki
Jump to: navigation, search

Considering the number of queries raised at the Doom 9 forums about interlacing and deinterlacing issues, I have made this to cover probably 90% of all issues of such nature. All of my experience has been through the experimentation of these filters and reading the help files available in AviSynth. I have no experience with plugin deinterlacers, though. So all methods used here do not require any additional plugins.

This is meant to be a kind of crash course about interlaced video, so I'll start with some information about it, before we start messing with it. There are 2 main video formats: NTSC and PAL. PAL is used in Europe and Australia, while NTSC is used in North America and Japan. PAL has a screen resolution of 720x576 running at 25 fps. NTSC runs at 720x480 at 29.97 fps. The latter has a smaller image, but a greater frame rate.

Both video formats store information the same way (visually at least). When a video camcorder records video, it doesn't record full images, like a film camera does. Instead, it records two images at half size, and mixes them together line for line - interlacing them. When played on a TV we see each image separately because TV's only display every second line (say lines 2, 4, 6, 8, 10), and then displays the same frame again, but with the other lines it missed (lines 1, 3, 5, 7, 9).

Each frame requires two passes (Each pass contains a field. Pass and Field are interchangeable terms). So the image recorded on the even numbered lines gets displayed, and then the other on the odd numbered lines. This fools the eye in to thinking its seeing full sizes images at the simulated 50 or 59.94 fps.

While that looks great on TV, on a computer, its another story. Computers display full frames in a single pass (aka progressive), so we see both images together. To separate them and display the video properly the following code is necessary.

For TV content (NTSC or PAL) to be restored for computer (and not to be transferred back)

Video = AviSource ("xxxxxxxx.avi") # Or whatever input
# Separates the interlaced images and places them in individual frames
Video = SeparateFields (Video) 
# Recreates the full frame by placing the lines to the original position
Video = Bob (Video) 

This results in a high frame rate video that is usually too high for most computers and codecs so often so you should select even or odd numbered frames placing one of these lines at the end (doesn't matter which):

Video = SelectEven (Video)
Video = SelectOdd (Video)

Now if you have film content (either from a TV or DVD source), and you want to convert from one NTSC to PAL, or vice versa, you have to alter the height of the video to correspond to the other format. The other difference is framerate. Because film plays at 24 fps and PAL runs at 25, the film is most often just sped up, maintaining complete frames. This means no deinterlacing for PAL film.

However, with NTSC running at 29.97 fps, the solution is not so simple. Additonal frames are created to compensate for the difference in framerate. For every four frames that exist, a fifth must be created. Because TV's display the frames over two passes, a smooth solution is available. If you create a 'third' pass, for two frames in close proximity, the two passes sum up to a fifth frame. You also have to preserve the field order. even numbered fields must stay in the even numbered pass, and the same with It's probably best to learn by example.

Here's 4 frames of PAL Film (Numbers for the frames, and letters for fields)

1a 2a 3a 4a

1b 2b 3b 4b

These are displayed in this order: 1a, 1b, 2a, 2b...

To make the framerate for NTSC, we now need 5 frames. So we need to duplicate two fields. The rules for this are that frame NUMBERS must always be maintained or increased, NEVER decreased. The other rule is that the letters should always be alternating, so that you get a, b, a, b (even, odd, even, odd).

So the best result is this:

1a 2a 2a 3a 4a

1b 2b 3b 4b 4b

Fields 2a and 4b have been duplicated. Now frame 1 has two passes, frame 2 has three passes, frame 3 has two and frame 4 has three. This results in quite smooth motion but now we have to implement it.

PAL Film to NTSC Film

Video = AviSource ("xxxxxxxx.avi")
Video = BilinearResize (Video, 720, 480)
Video = SeparateFields (Video) # So that you may deal with the individual fields.
Video = SelectEvery (Video, 8, 0,1, 2,3,2, 4,5, 7,6,7) 
# Field 2a is 2, and 4b is 7. Framerate is automatically adjusted 
# to maintain the speed of playback
Video = Weave (Video) # Put the fields back into frames. Result is 30 fps
Video = AssumeFPS (Video, 29.97, True) # Corrects framerate, and slows audio down with it

This will result in some interlacing lines on certain frames and that is normal. But it will look great on a TV.

Converting NTSC to PAL for film requires the inverse process. You need to eliminate the duplicated fields, although the best way wouldn't look like it. Using DoubleWeave (), the result will produce frames like this

If NTSC film is originally this:

1a 2a 2a 3a 4a

1b 2b 3b 4b 4b

Then DoubleWeave will produce this:

1a 2a 2a 2a 2a 3a 3a 4a 4a 4a

1b 1b 2b 2b 3b 3b 4b 4b 4b 4b

       * **

Now there is at least one frame consisting of its corresponding paired fields. So we can use the Pulldown () function to extract these frames. For every five frames, they are the first and fourth, as shown.

So the code is:

NTSC Film to PAL Film

Video = AviSource ("xxxxxxxx.avi")
Video = DoubleWeave (Video)
Video = Pulldown (Video, 0, 3) # Returns video playing at 23.976 fps
Video = BilinearResize (Video, 720, 576)
Video = AssumeFPS (Video, 25, True)

This may not provide perfect results every time. It is not uncommon for people to cut into the video, disturbing the NTSC 3:2 Pulldown playback. In this case, adjust the pulldown numbers so that the second number is 3 larger than the first (say 1 & 4, or 2 & 5).

TV conversion coming soon!

Personal tools