PDA

View Full Version : Postprocessing mpeg2 video with avisynth


hatall
24th April 2004, 07:14
I have a mpeg2 clip and want to apply some avisynth filters resulting a filtered mpeg2. How I write the outputted mpeg2 ? I was able to play it in Media Player Classic and tried to open in VirtualDubMod, setting "Direct Stream Copy" but it hasn't mpeg2 output. And I don't know if VirtualDubMod alters the input. I only want to read a mpeg2 clip, apply some avisynth filters and write the outputted mpeg2.:confused:

stickboy
24th April 2004, 07:41
You will need to re-encode the video (e.g. with TMPGEnc Plus, CCE, etc.).

AviSynth is not an encoder, and anything that alters video frames will require re-encoding.

hatall
24th April 2004, 09:18
It is what I don't want to. This mpeg was encoded using CCE and I don't want to pass through it again.:(
Is there no way of just writing it down ? I mean, it's all there, I can even see the output in Media Player ... isn't the output mpeg2 already ?

stickboy
24th April 2004, 10:46
No, you're seeing decompressed video.

When you open a video, AviSynth decompresses the video frames. If it didn't do this, you couldn't filter it at all.

SoonUDie
24th April 2004, 15:36
I second what everyone else has said. You'll need to re-encode the video. This will probably take a long time... sorry.

To my knowledge, Avisynth takes the source you give it, decompresses it, filters it and then serves it to the application you load the .avs into (such as VirtualDub). You end up with uncompressed data. This is true both for the video and audio Avisynth puts out.

rfmmars
24th April 2004, 20:14
If you don't need the final output file to be MPEG2, then you could use the new MJPEG codec from LeadTools. This I think the best codec on the market, and only costs $9.95 US. The demo is free at www.leadtools.com

If MPEG2 must be kept, frameserve to a MPEG2 encoder using I frames only and very high data rate (8000 to 15,000 bps). There is a MPEG2 plugin encoder for VDMOD or VD.....YMPEG free for the downloading.

richard
photorecall.net

hatall
27th April 2004, 06:31
@rfmmars: I didn't understand what you said about using only I frames. And the others (P,B)?

I need the final output file to be mpeg2 'cause I wanna make a svcd; so I cannot use such high bitrates (8000-15000), can I ?!
This clip was a result of a CCE encoding and has the mosquito noise in some frames due to the low svcd bitrate, I guess (the source is very clean). I loaded it with an avs script in Media Player Classic with the filters undot, deen and unfilter and the result was good. So I want to leave it this way. Going thru CCE again I likely will end up with some noise again.
I tried to put these filters before encoding but the output wasn't good compared to filtering after.
One (more :))thing I don't understand: the output file must be a type readable by Media Player Classic. Is it right ? What type is this outputted file ? I mean, this uncompressed video must have a type.
The last one (for now): I'm using MpegDecoder to read this mpeg2 source. I wanted to read with Mpeg2Dec3dg but only got error "MPEG2Source: couldn't open file". Is it not possible ?

Thanxs in advance.

neuron2
27th April 2004, 06:49
Originally posted by hatall
The last one (for now): I'm using MpegDecoder to read this mpeg2 source. I wanted to read with Mpeg2Dec3dg but only got error "MPEG2Source: couldn't open file". Is it not possible ?It should work fine. Please post the script that produces this error.

rfmmars
27th April 2004, 08:12
First let me give you how I have done this, the encoders are different, I use Ligos 3.5 mpeg2 encoder frameserved by Virtualdub 1.54, frameserved by my AVIsynth 2.54. Since all my work is with old 8mm film, I don't have the need for sound.

This script will load MEPG2 files

loadplugin("c:\windows\system32\mpegdecoder.dll")
mpegsource("e:\KanasCity\xmas67.mpG")
#complementparity
separatefields
#bob
#SWAPFIELDS
doubleweave

Notic the plug-in I am using (mpegdecoder.dll). If you want to have sound,I have been told you must Demux the audio first and add a loadaudiosource routine to it.

So once the script has been written, I open it,the .AVS file,just like it was a .AVI file. for you it would be the same but you would want VD to frame serve to your encoder without VD doing any filtering, or maybe you would. The output file name I use is video.vdr.avi

It's still mpeg2 but VD doesn't know it.

For me my re-encoded MPEG2 file from my Ligos encoder would be 15,000 fps I frames only because this new file is going to be rendered and recompressed again in it's final form, either as a SVCD or a DVD. It is in that last editing program where the data rate downsized is done.

I think that your encoder will be able to do the same. So by doing the above I have limited my losses while filtering.

Mosquito noise is something I have read about but don't know what it is. Is this a PAL thing or NTSC or both? I don't do much with SCVD because it will be always be a drop in quality, and with DVD blanks down at the $.59 US level, I just don't see the point unless it is just for reference saving.

I am certainly not the last or first word when It comes to AVISYNTH scripting, I am just learning like you.

Best of luck

richard

stickboy
27th April 2004, 08:21
Originally posted by hatall
One (more :))thing I don't understand: the output file must be a type readable by Media Player Classic. Is it right ? What type is this outputted file ? I mean, this uncompressed video must have a type.
It's raw YV12 video. (Or raw YUY2 video, or maybe raw RGB video, depending on what colorspace you're in.)

hatall
28th April 2004, 21:43
Hummm... I'm realizing I'll not be able to filter this output. At least it won't be worth doing.
I did a test filtering the mpeg2 output and reencoding in CCE again and got the same noise. :(
Maybe only if filtering right on mpeg2 ...

@stickboy
So, mine should be raw YUV2. But Media Player Classic understand this type ? In the file/open it doesn't appear.

@rfmmars
Unfortunately, the prices here are not like yours - U$1.7 the cheapest one :eek: . Besides, I still have many CD-Rs to turn to coasters :D .
Thanxs for your explanation but I think I can't go that way. Your problem is to improve a bad source and mine is to make a good one worse :D . I mean, I'm doing a dvd-to-svcd backup and my problem is in the encoding phase due to bitrate restrictions. I thought I could filter this output but I'm realizing I can't.
I can't figure out how I could have only I frames using CCE, maybe lowering the GOP size to 1. But does it give better quality ? You should end up with a big file, shouldn't you ?
I guess the mosquito noise is common to both NTSC and PAL as it is a noise. See my example: my source is NTSC but it doesn't matter. If it were PAL I would have to lower the bitrate same way to be inside SVCD standard.

@neuron2
This is my postprocessing script (the +++ are not included)

+++++++++++++++++++++++++++++++++++++++
LoadPlugin("C:\Video\avisynth254\Filters\MPEG2Dec3dg.dll")
LoadPlugin("C:\Video\avisynth254\Filters\MPEGDecoder.dll")
LoadPlugin("C:\Video\avisynth254\Filters\unfilter.dll")
LoadPlugin("C:\Video\avisynth254\Filters\undot.dll")
LoadPlugin("C:\Video\avisynth254\Filters\Deen.dll")
Mpeg2Source("C:\clips\test.mpv")
###MpegSource("C:\clips\test.mpv")
Undot()
Deen()
unfilter(25,25)
+++++++++++++++++++++++++++++++++++++++

Using MpegDecoder (MpegSource) I can read .mpg and .mpv files. Using Mpeg2Dec3dg (Mpeg2Source) got this error:

MPEG2Source: couldn't open file
(c:\clips\post.avs, line7)

This is the script that read the original source (the +++ are not included):
+++++++++++++++++++++++++++++++++++++++
####
## DLL Section ##
####
LoadPlugin("C:\Video\avisynth254\Filters\MPEG2Dec3dg.dll")

####
## Parameters
####
movie="C:\clips\test.d2v"
####
## Main section and static filters ###
####

Mpeg2Source(movie)
Trim(14015,15500)
BicubicResize(480, 366, 0, 0.6, 6, 0, 708, 480)
AddBorders(0, 57, 0, 57)
LetterBox(0, 0, 16, 16)
ResampleAudio(44100)
ConvertToYUY2(interlaced=false)

function ResampleAudio(clip v1, int rate) {
v2 = Blankclip()
v1 = AudioDub(v1,v2)
return v1
}
+++++++++++++++++++++++++++++++++++++

rfmmars
28th April 2004, 21:56
By selecting I frames only, it gives you tha advantage of editing at any point on the clip, frame accurate. Yes the file size is larger.

Sorry that there is much price differece in DVD blanks over there.

richard

neuron2
28th April 2004, 22:41
Originally posted by hatall
@neuron2
This is my postprocessing script (the +++ are not included)

+++++++++++++++++++++++++++++++++++++++
LoadPlugin("C:\Video\avisynth254\Filters\MPEG2Dec3dg.dll")
LoadPlugin("C:\Video\avisynth254\Filters\MPEGDecoder.dll")
LoadPlugin("C:\Video\avisynth254\Filters\unfilter.dll")
LoadPlugin("C:\Video\avisynth254\Filters\undot.dll")
LoadPlugin("C:\Video\avisynth254\Filters\Deen.dll")
Mpeg2Source("C:\clips\test.mpv")
###MpegSource("C:\clips\test.mpv")
Undot()
Deen()
unfilter(25,25)
+++++++++++++++++++++++++++++++++++++++

Using MpegDecoder (MpegSource) I can read .mpg and .mpv files. Using Mpeg2Dec3dg (Mpeg2Source) got this error:

MPEG2Source: couldn't open file
(c:\clips\post.avs, line7)
Mpeg2source() can open only D2V files. You have to open the MPEG2 file with DVD2AVIdg and use "Save Project" to make the D2V file.

stickboy
29th April 2004, 05:59
Originally posted by hatall
@stickboy
So, mine should be raw YUV2. But Media Player Classic understand this type ? In the file/open it doesn't appear.What file?

If you're asking what happens if you load an AviSynth script in VirtualDub and use Direct Stream Copy mode, then the file format of the output file is AVI. (VirtualDub only outputs AVI files.) Its fourCC value will correspond to an uncompressed video format appropriate for that color space.

hatall
30th April 2004, 14:01
Originally posted by neuron2
Mpeg2source() can open only D2V files. You have to open the MPEG2 file with DVD2AVIdg and use "Save Project" to make the D2V file.
So... this was the problem! I didn't know that, thanxs.

@stickboy
I'm talking about "playing" my postprocessing script in Media Classic player. As I'm using MpegDecoder it must output something that this player understands. According to you I can conclude that it understands raw YUV2. Am I right?
Is there any filter that shows what kind of video is being outputted ?

neuron2
30th April 2004, 15:20
Info()

stickboy
30th April 2004, 18:10
Originally posted by hatall
@stickboy
I'm talking about "playing" my postprocessing script in Media Classic player. As I'm using MpegDecoder it must output something that this player understands. According to you I can conclude that it understands raw YUV2. Am I right?Yes. (Probably raw YV12 in your case, if you're decoding an MPEG without a colorspace conversion. And there is no such thing as YUV2 (http://forum.doom9.org/showthread.php?s=&threadid=73305).)

t_2
30th April 2004, 22:45
It seems that a few points need to be clarified here.

First: When you open a real video file (i.e., "video.mpeg", "video.avi", etc.) in VirtualDub you can save the video using direct stream copy without recompressing IF...

1. it is an avi file (this doesn't work for MPEG files (correct me someone if I'm wrong on this) AND...

2. you don't do any processing i.e., filtering or color space conversions.

Secondly, when you open an avs file in VirtualDub, you are doing more than simply opening a real video file(s). You are running Avisynth at the same time, only you don't "see" this because it is running in the backgrouond feeding frames of the video one at a time to VirtualDub. If you want to convince yourself of this open your avs script in VirtualDub then uninstall AviSynth and try to open it again. Of course it won't open.

Therefore, if you do any filtering of an avs script (say by changing the size or adjusting the color, saturation, etc.) in VirtualDub you have to recompress if you want to save the result as a real video file. Remember an avs file isn't a real video file, it is an AviSynth project file, something like a "*.ppj" file in Adobe Premire. The only difference is ppj files can only be opened/viewed in Premire whereas while AviSynth avs files can only be opened in Avisynth, they can only be viewed in a player/editor other than AviSynth for the simple reason that AviSynth doesn't have its own player (or user interface for that matter. Note: a user interface is a window that the program runs in, which has a menu, and subwindows, like the timeline, monitor, etc.) Failing to understand this subtle distinction between opening and viewing an avs file (which after all is just a .txt file with an avs extension) is what fools many into thinking that an avs script is a video file. Editing, whether in Premire or AviSynth always involves changing a project file, the only difference is that in Premire the project file is unintelligable to the user and can only be modified indirectly via the user interface (say by clicking with the mouse at the point on the timeline where you want to cut the clip and saving the changes) whereas with Avisynth, the project file is modified directly by the user, and as a result the computer isn't the only one who understands it. Obviously a Premire project file isn't a video file and ppj isn't a video file extension. This is clear because when you open a ppj file along with the video, you see Premire's user interface (= window) You can't compress a Premire project file as if it were a video file. And when you open a Premire project file, you are doing more than opening a video file(s), you are running Premire at the same time. To prove this to yourself, open a premire project file, then uninstall Premire (STOP!! Don't believe I really expect you to do this!!) and try to open the project file again. Obviously it won't open.

Thirdly, There are several concepts that need to be kept clear. ColorSpace, Compression method (using a codec), and containers. There are 2 basic ways of storing color information in a file:

YUY: Here the pixels are represented by 3 values, one is intensity, this is the same as a black and white picture of a color video. This first number simply gives the absolute brightness of a pixel. The 2nd and third numbers are simply the X and Y coordinates of a given pixel as ploted on a color map. In this way you can map out all the colors of the rainbow. YUY is one way of defining the color of a pixel with 3 numbers. There is another way...

RGB. Here each pixel is represented by 3 numbers again, this time there is no separate intensity number, Instead you have a value for Red, Green and Blue. In this way you can map out all the colors of the rainbow. The color of a pixel is defined again by 3 numbers, usually between 0 and 255


All/most-of the other color spaces you see (like, RGB32,YUY2 & YV12) can be grouped in some way in these two families.

Both RGB and YUY can be compressed or uncompressed. If they are compressed this can only be done by a codec. Compression method differs from Codec in this way. There are many different mpeg4 codecs, like Xvid, Divx5, Nero, and QuickTime, but they all produce a video stream that is MPEG4 compliant. This means that although one codec may be better than another at compressing or opening/playing a video file, they should all be able to open/play each other's video streams. Of course making this happen in the real world may be a little more difficult. In Avi for example, many codecs have their own FOURCC code which forces the computer to use that codec to open the file, getting the computer to use another MPEG4 compliant codec to open the file may involve changing this code in the video you want to play/edit.

The compressed video stream must be put in a container before it can become a file in the strict sense. Avi is a container. A container as I understand it, is primarilly a conventional way of grouping multimedia information i.e., video, audio and subtitle streams, etc. What makes this more confusing is that, unlike avi, which is only a container, Mpeg2 and Mpeg4 are not only compression methods, there are also Mpeg2 containers( with mpeg file extensions) and Mpeg4 containers (with mp4 file extensions). So for example an MPEG4 video stream compressed with Xvid codec can be "incarnated" in the file sense of the word into either an avi file or an ogm file or an mp4 file. And changing between any of these containers shouldn't necessarily involve recompressing.

So, when you go from one container to another, say from avi to mpeg or from avi to ogm, and you don't change color space, compression method (i.e., codec) or use any filters then theoretically you don't need to recompress, but anytime you change the video stream, either by altering its color space or filtering it, you must recompress even if you are saving to the same container, i.e., mpeg to mpeg. I say theoretically because, even if a direct stream copy is theoretically possible, there is no guarantee that any given program has implemented it. In the real world you might have to recompress anyway.

I hope this makes things clear. This is all stuff that I only slowly realized and wished that someone would have explained to me in one place. I apologize if you already know most of this, perhaps it will be useful to others. If I have made any misstatements feel free to correct me.

hatall
5th May 2004, 03:50
Originally posted by stickboy
Yes. (Probably raw YV12 in your case, if you're decoding an MPEG without a colorspace conversion. And there is no such thing as YUV2.)

So,ok. I don't know much of colorspaces to discuss it. If everybody says there's no YUV2 I believe :D . The correct is YUY2, though. But, as in that thread (http://forum.doom9.org/showthread.php?s=&threadid=73305), many people write YUV2.
I got confused with YUV 4:2:2 and thought it was a short name. But, Y and V are quite similar, aren't them ? :)

t_2, thanks for your explanation. Although I have to read it 2 or 3 more times to understand everything you said :D.
But

Remember an avs file isn't a real video file, it is an AviSynth project file, something like a "*.ppj" file in Adobe Premire. The only difference is ppj files can only be opened/viewed in Premire whereas while AviSynth avs files can only be opened in Avisynth, they can only be viewed in a player/editor other than AviSynth for the simple reason that AviSynth doesn't have its own player

It's clear to me that an avs script is only a batch file that will be processed by avisynth and its filters. I think it's something like this:

mpeg2 frame -> [avisynth frame processing] -> outputted frame -> [other program]

The type of the outputted frame will depend on the avisynth filters, but if it can be played in Media Player Classic and WMP it must be something understandable by them. So, I said my outputted frame should be raw YUY2 (:D).
I guess when you associate .avs with a program like Media Player Classic and double-click the script, avisynth intercepts this call, processes the frame and forwards it to the program. Isn't it ?