View Full Version : automated detection for interlacing?
actionman133
8th April 2004, 13:29
im trying to make some automated code for determining whether a clip is compressed interlaced or progressive, but i havent been able to find any IsInterlaced function or the like. IsPlanar seems to return True on interlaced clips and False on progressive, but that may be coincidental.
i want to know so that the clip will be sampled correctly when it is converted from YV12 to YUY2, automatically. if IsPlanar the right way to go i can simply do:
ConvertToYUY2 (IsPlanar)
is IsPlanar the way to go or is there a proper function for testing whether the clip is interlaced? by the way, these clips are loaded by MPEG2Source.
neuron2
8th April 2004, 14:55
The Decomb filter package has an IsCombed() function. But it is usable only within the Avisynth ConditionalFilter(). That may serve your needs.
LigH
10th April 2004, 10:11
YV12 is always "planar"; this is not related to "interlaced".
"Planar" video formats (like YV12) store all the component parts of each pixel (respective subsampling averages) together independently, e.g. first all Y pixel components, then all V (2 pixel)² components, and finally all U (2 pixel)² components.
In contrast, "packed" video formats (like YUY2, UYVY, or RGB[A]) store pixel by pixel with all components (e.g. Y for the first pixel, U for both first, Y for the second, V for both first - then the next 2 pixels follow).
Mug Funky
10th April 2004, 15:17
with YV12 source:
converttoyuy2(interlaced=true)
that should do you :)
comb detection is one of the big conundrums of video. a human can tell by looking at a frame if there's interlacing, but a computer cannot tell this as easily (ie. at a reasonable speed).
LigH
11th April 2004, 00:26
Originally posted by Mug Funky
with YV12 source:
converttoyuy2(interlaced=true)
Oh, well - there are two different questions, and everyone shall not try to answer both of them in the same line:
1) Is the video stream interlaced?
That means: Was the encoder once set up to encode video frames as separate fields?
This question shall be answered by the function "IsInterlaced()". If the MPEG2 stream has its flags set according to the meaning "interlaced MPEG encoding", I hope that the function "MPEG2Source()" will set this video clip attribute accordingly. But even progressive content is often encoded with interlaced flags...
If the video was encoded as interlaced (the usuall rippers, or the DVD2AVI preview, or the Bitrate Viewer, will report this stream bitflag), one shall use 'MPEG2Source("*.d2v", iPP=true)' and 'ConvertToYUY2(interlaced=true)', for progressive encoding both shall be false - else miscoloring may be the result.
See examples 1 (http://www.ligh.de/images/TR1.png) | 2 (http://www.ligh.de/images/TR2.png) | 3 (http://www.ligh.de/images/TR3.png) for accidently progressive processing of an interlaced video stream in YV12
2) Is the video content interlaced?
That means: Are there two different fields in the same frame, recorded at two different timestamps, visible by two weaved pairs of alternating lines?
This is the fact which the filter "IsCombed()" tries to detect.
actionman133
11th April 2004, 15:56
LigH has gotten the concept right: if the video is encoded in an interlaced format, even if the image APPEARS to be progressive.
and the simple 'ConvertToYUY2 (Interlaced = True)' is not right because I want this code to be in an importable script where any given source can be interlaced or progressive... therefore it needs to detect which method it was encoded with and convert accordingly.
LigH, you mentioned using an IsInterlaced() function... it doesn't work, saying that there is no such fuction. I'm running version 2.54 at the moment. Does 2.55 have it?
Wilbert
11th April 2004, 22:26
IsInterlaced() function
AFAIK there is no such function. dvd2avi/mpeg2dec3 could be modified to return a flag for this ...
LigH
11th April 2004, 22:50
Sorry... :stupid:
There are the following "clip attributes" related to this topic:
IsFieldBased() / IsFrameBased(); GetParity()
I'd say that "IsFieldBased" is most similar to "was it encoded with interlaced flag".
Richard Berg
12th April 2004, 00:27
No, IsFieldBased() returns whether each frame is actually a half-height field, e.g. after a SeparateFields was performed.
LigH
12th April 2004, 00:54
Wow.
So is there no hint from any clip attribute, if I shall use "ConvertToYUY2(interlaced=true)" or "ConvertToYUY2(interlaced=false)"? I'm afraid I'm getting slightly confused...
___
I just checked the following:
I created a small clip using AviSynth:
ColorBars(720,576)
AssumeFPS(25)
Trim(0,249)
I encoded it to progressive (frame based) MPEG2 video (p.m2v) on one hand, and to interlaced (field based) MPEG2 video (i.m2v) on the other.
I compared the information on the progressive and the interlaced video with AviSynth and MPEG2Dec3:
LoadPlugin("E:\Programme\Gordian Knot\mpeg2dec3.dll")
p=MPEG2Source("p.d2v",iPP=false).Info()
i=MPEG2Source("i.d2v",iPP=true).Info()
Subtract(i,p).Levels(96,1.0,160,0,255)
Guess the resulting image...
Plain gray?
Yes, you are right.
Now I tried the same with video converted to YUY2:
LoadPlugin("E:\Programme\Gordian Knot\mpeg2dec3.dll")
p=MPEG2Source("p.d2v",iPP=false).ConvertToYUY2(interlaced=false).Info()
i=MPEG2Source("i.d2v",iPP=true).ConvertToYUY2(interlaced=true).Info()
Subtract(i,p).Levels(96,1.0,160,0,255)
Now guess again the resulting image...
Plain gray again?
This time, you are wrong!
And now I sit there in my corner and wonder why AviSynth 2.5 provides enhanced support for interlaced video...
zettai
14th April 2004, 13:10
So does this mean that in Avisynth there is currently no way of telling if an mpeg2 source is encoded as interlaced except looking to see whether seeing interlacing=true in a colourspace conversion looks better?
If interlacing is just an mpeg2 flag then it could easily be set incorrectly...
LigH
14th April 2004, 13:38
Not really - there are several flags related to interlaced encoding of MPEG-2 video. bbMPEG has the most detailed Advanced options (practically, you may set up each bit flag in an MPEG header), and there are 3 settings: "Progressive Sequence", "Progressive Frame", and "Field Pictures". The difference seems to be "very slightly" at first, but I'm afraid that they are much more important...
zettai
14th April 2004, 14:17
Well either I am understanding this wrong but it seems that unless we can get the frame type from dvd2avi then we can have problems.
Here's a little test I did with some easily identifiable chroma areas on a hybrid NTSC source that (in dvd2avi) stores some frames as "progressive" and others as "interlaced" but is mostly NTSC in the Video Type.
function CheckUpsampling(clip a) {
b=converttoYUY2(a,interlaced=false).crop(270,180,-270,-180)
\.pointresize(360,480).subtitle("interlaced=false")
c=converttoYUY2(a,interlaced=true).crop(270,180,-270,-180)
\.pointresize(360,480).subtitle("interlaced=true")
stackhorizontal(b,c)
}
MPEG2Source("L:\BCBA_0619\suprgals2.d2v")
CheckUpsampling()
Frame 378 (http://amvs.34sp.com/frame378.png)
In this first one it suggests that we should use interlaced=false. Fine.
Frame 381 (http://amvs.34sp.com/frame381.png)
Uh-oh.. now we get an interlaced frame. It certainly looks to me that interlaced=true is the way to go.
Of course if you don't specify which to do then ConvertToYUY2 will just use interlaced=false.
So what can you do? Well, I'm not sure to be honest. It would be fine if all the frames had the same result but with a source like this (where I can't force film because it is a hybrid) I just don't know...
Selecting ipp=true in mpeg2source does noting as that is only for post processing deblock and dering.
In short, I'm lost :)
[EDIT: formatting change]
scharfis_brain
14th April 2004, 14:54
how about a consturct like this:
scriptclip(x,"x.converttoyuy2(interlaced=iscombed(x,20))")
I don't know whether its syntax is correct, but I assum you can imagoine, what I mean :)
zettai
14th April 2004, 15:32
Yeah, IsCombed does the trick pretty well.
function CheckUpsampling(clip a) {
b=converttoYUY2(a,interlaced=false)
\.crop(270,180,-270,-180).pointresize(360,480).subtitle("interlaced=false")
c=converttoYUY2(a,interlaced=true).crop(270,180,-270,-180)
\.pointresize(360,480).subtitle("interlaced=true")
d=conditionalfilter(a,b,c,"IsCombed","equals","false").subtitle("iscombed")
stackhorizontal(b,c,d)
}
MPEG2Source("L:\BCBA_0619\suprgals2.d2v")
CheckUpsampling()
gives this version of frame 378 (http://www.amvs.34sp.com/frame378a.png) and then this version of frame 381 (http://www.amvs.34sp.com/frame381a.png)
zettai
14th April 2004, 16:02
The thing I'm not quite understanding is why dvd2avi and mpeg2dec can't tell us what kind of frame type we have and what sort of processing is required.
I mean, this dvd is obviously encoded so that a DVD player can play it back without upsampling the chroma incorrectly every few frames... and sure enough on this source some of the frames are labelled as Progressive and others are labelled as Interlaced but there seems to be no procedure for dvd2avi to communicate this information via mpeg2dec (unless I just haven't spotted it yet)
Surely there should be some sort of hint that you can enable to allow mpeg2dec to define whether a certain frame is or is not progressively encoded that can be picked up by avisynth filters such as ConvertToYUY2 when changing colourspace.
Of course, there is the chance that the frame type is labelled incorrectly but that's another issue that would probably need resolving with IsCombed. However, I don't see why we should have to use something like IsCombed when a properly encoded mpeg2 stream should be able to tell us automatically how to deal with the data.
neuron2
15th April 2004, 14:28
Upsampling issues are on my work list for DVD2AVIdg/MPEG2DEC3dg. Maybe you will get your wish.
http://neuron2.net/misc/Work_List.html
zettai
15th April 2004, 14:31
Thank you!
Good luck with the updates - I'm especially looking forward to this faster random frame access :D
SeeMoreDigital
15th April 2004, 14:56
This is a great idea. I mentioned something along these lines here: -
http://forum.doom9.org/showthread.php?s=&threadid=73880
I also provided a link to some Mpeg4 interlaced video content with the fields clearly identified.
Cheers
DEIT: spewling!
LigH
15th April 2004, 19:11
But still, I even wonder how AviSynth stores frames internally in YV12 mode: YV12 video uses 4:2:0 chroma subsampling, therefore a chroma value represents the average of 4 pixels (2 besides and 2 above each other). If it stores them progressively, then one average represents the lines 1&2 and the next lines 3&4; if it stores them interlacedly, one average represents lines 1&3 and the next lines 2&4. But if no (clip / frame) attribute is available, then I wonder if AviSynth works this way at all!
Richard Berg
15th April 2004, 20:00
It stores them the same. Different filters are free to interpret it in different ways.
sh0dan
16th April 2004, 15:56
@LigH: since AviSynth cannot know if any given source is interlaced or not, all conversion have the "interlaced=true/false" flag.
For most filters interlaced/non-interlaced doesn't matter. If you have interlaced material, a SeparateFields() will give you progressive chroma placement anyway.
I tried doing chroma upsampling based on whether each separate frame was interlaced or not for teleconed material, and found that it lead to chroma shimmering.
LigH
16th April 2004, 16:27
I still don't get it...
Let's assume progressive material, encoded as progressive MPEG2. If MPEG2Dec3 decodes it to progressive YV12, chroma values will represent an average for lines 1&2, and for lines 3&4, which is correct for progressive material. Fine.
Let's assume progressive material, encoded as interlaced MPEG2. If MPEG2Dec3 decodes it to interlaced YV12, chroma values will represent an average for lines 1&3, and for lines 2&4, which is not really correct for progressive material, but probably does not hurt much, except for red/blue diagonals, maybe.
Let's assume interlaced material, encoded as interlaced MPEG2. If MPEG2Dec3 decodes it to interlaced YV12, chroma values will represent an average for lines 1&3, and for lines 2&4, which is correct for interlaced material. Fine. But if MPEG2Dec3 decodes it to progressive YV12, chroma values will represent an average for lines 1&2, and for lines 3&4, which is definitely wrong for interlaced material, and will result in color shimering.
And now you tell me, it doesn't matter at all how AviSynth stores plain YV12 video, progressively or interlacedly, although the chroma channels may share line 1 only with either line 2 (p) or line 3 (i), and line 4 only with either line 3 (p) or line 2 (i). Where is my fault here? :confused:
SeeMoreDigital
16th April 2004, 16:56
This is great stuff!
LigH
17th April 2004, 00:01
:o
Suddenly, a flash struck my mind, now I know how it works:
I shall not try to build any relationship between pixels and component planes. I shall see the components (luminance / chrominances) as independent planes: Luminance is a plane at full resolution. Chrominances are planes at half resolution.
If I want them to be filtered as interlaced, I shall apply SeparateFields() - then the luminance plane gets split info fields by alternating lines in its plane, and the chrominance planes get split into fields by alternating lines in their planes.
Anyway, it would be nice to know if the image source filter read the image content from one whole frame, or if it weaved it from two fields; although interlaced color space conversion shall only be applied if the content is combed, but not just always if the source was encoded field-based.
Therefore, please excuse me confusing you - sometimes I even step onto my own slow uptake line...
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.