Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 21st September 2023, 14:39   #1  |  Link
simon744
Registered User
 
Join Date: Sep 2023
Posts: 10
Encoding Interlaced Stream with Separated Fields

Hello, everyone! I'm writing with regards to a problem I've been having for maybe well over a month already, and which now is starting to drive me crazy.

I have a few interlaced MPEG2 (1080/25i, 1080/29.97i) streams on hand that I want to re-encode to AVC, while simultaneously changing the "Scan type, store method" to Separated Fields, and the "Scan Order" to TFF (Top Field First). The thing is I want to KEEP THEM INTERLACED...I'm not interested in de-interlacing.

I was trying with ffmpeg at first, but gave up on it after a few botched attempts (i,e., the stream becoming progressive, horrible scanline artifacts, etc.). Then I was recommended some AviSynth scripts to see if I'll get what I need, which too didn't help...this script not only made my source progressive, but also dropped the frame rate from 29.97 to 12.500fps (not gonna lie, though...I think this was for progressive > interlaced conversion):

Code:
LWlibavVideoSource("input.avi")
z_ConvertFormat(pixel_type="RGBPS", colorspace_op="709:709:709:l=>rgb:709:709:f")
Rife(gpu_thread=1, model=6, sc=true, sc_threshold=0.12) # defaults to double frame rate
z_ConvertFormat(pixel_type="YUV420P8", colorspace_op="rgb:709:709:f=>709:709:709:l")
AssumeTFF()
SeparateFields()
SelectEvery(4,0,3)
Weave()
This one too made the video progressive, but changed the fps to 25:

Code:
LWLibavVideoSource("input.avi")
AssumeTFF()
SeparateFields()
AssumeTFF()
SelectEvery(4, 0, 3, 2, 1)
Weave()
AssumeTFF()
I need the output to be recognized and treated as an interlaced stream by MediaInfo and by media players.

Here's what the majority of my sources look like:

Code:
Video
ID                                       : 4113 (0x1011)
Menu ID                                  : 1 (0x1)
Format                                   : MPEG Video
Format version                           : Version 2
Format profile                           : Main@High
Format settings                          : BVOP
Format settings, BVOP                    : Yes
Format settings, Matrix                  : Default
Format settings, GOP                     : Variable
Format settings, picture structure       : Field
Codec ID                                 : 2
Duration                                 : 3 min 58 s
Bit rate mode                            : Constant
Bit rate                                 : 37.2 Mb/s
Maximum bit rate                         : 33.3 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate                               : 25.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Interlaced
Scan order                               : Top Field First
Compression mode                         : Lossy
Bits/(Pixel*Frame)                       : 0.718
Time code of first frame                 : 00:00:13:11
Time code source                         : Group of pictures header
Stream size                              : 1.03 GiB (74%)
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
And here's what I need my outputs to look like:

Code:
Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High @L4.1
Format settings                          : 3 Ref Frames
Format settings, CABAC                   : No
Format settings, Reference frames        : 3 frames
Format settings, GOP                     : M=3, N=15
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 3 min 58 s
Bit rate mode                            : Variable
Bit rate                                 : 36.9 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 25.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Interlaced
Scan type, store method                  : Separated fields
Scan order                               : Top Field First
Bits/(Pixel*Frame)                       : 0.718
Stream size                              : 1.03 GiB (74%)
Default                                  : Yes
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
PLEASE, if anybody happens to know of a good script that'll smoothly do the conversion and make the needed changes for me, then I'd appreciate feedback GREATLY.
simon744 is offline   Reply With Quote
Old 21st September 2023, 14:42   #2  |  Link
simon744
Registered User
 
Join Date: Sep 2023
Posts: 10
Also, forgot to add some of the files also have 4:2:2 chroma subsampling, which too I'd like to preserve in the re-encoding process. Soo, will be glad to receive advice on how to keep it.

And please also include the exact plugins and filters I'll need for the scripts.

Thank you all in advance!
simon744 is offline   Reply With Quote
Old 21st September 2023, 14:52   #3  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,000
Scan type and store method is the option to your used MPEG4-AVC encoder. If you, for some reason, not like MBAFF for interlaced - you need to disable it in the AVC encoder options. Also if you can use MBAFF you need to provide interlaced options to AVC encoder. AVS do not make auto-deinterlacing but still send full frame to AVC encoder so it is task for AVC encoder configure person to set correct options for interlaced encoding.

"but also dropped the frame rate from 29.97 to 12.500fps"

You can set AssumeFPS() at the end of script to provide any required framerate to downstream MPEG encoder. Or set framerate manually in the encoder options. Also RIFE() may not work with interlaced frames directly - you need to SeparateFileds() before (or better to make deinterlace - maybe with QTGMC() to double frame rate).

"some of the files also have 4:2:2 chroma subsampling, which too I'd like to preserve in the re-encoding process. Soo, will be glad to receive advice on how to keep it."

You need compatible with 4:2:2 MPEG encoder and provide required options for encoding format. The only task for AVS is to provide compatible data format (interleaved or planar format and maybe minor tweaks for chroma location). But you need to get compatible 4:2:2 formats list from your MPEG encoding engine.

Last edited by DTL; 21st September 2023 at 15:00.
DTL is offline   Reply With Quote
Old 21st September 2023, 14:55   #4  |  Link
simon744
Registered User
 
Join Date: Sep 2023
Posts: 10
Quote:
Originally Posted by DTL View Post
Scan type and store method is the option to your used MPEG4-AVC encoder. If you, for some reason, not like MBAFF for interlaced - you need to disable it in the AVC encoder options. Also if you can use MBAFF you need to provide interlaced options to AVC encoder. AVS do not make auto-deinterlacing but still send full frame to AVC encoder so it is task for AVC encoder configure person to set correct options for interlaced encoding.

"but also dropped the frame rate from 29.97 to 12.500fps"

You can set AssumeFPS() at the end of script to provide any required framerate to downstream MPEG encoder. Or set framerate manually in the encoder options. Also RIFE() may not work with interlaced frames directly - you need to SeparateFileds() before (or better to make deinterlace - maybe with QTGMC() to double frame rate).
Thanks a lot for your input. I understand, but the problem is me not knowing how to do all of that haha. I'm relatively new to AviSynth and still do struggle with "constructing" scripts.

Do you know exactly how what I need can be achieved?

Last edited by simon744; 21st September 2023 at 14:59.
simon744 is offline   Reply With Quote
Old 21st September 2023, 17:08   #5  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,000
If you use x264 for encoding it is better to ask in the MPEG4-AVC forum section how to configure it to interlaced encoding. AVS only responsible to output undamaged interlaced frames to encoder. For ffmpeg it is also some different forum how to feed AVS output to ffmpeg and set all required command line options for its AVC-encoding library. Same is about 4:2:2 - you may need special build of x264 (with High 4:2:2 Profile) and compatible with 4:2:2 output from AVS. Or compable build of ffmpeg.

Typically encoder output debug text about its input format and encoding mode - if input something not expected you can ask here about AVS scrpiting to generate required output. So your question is partially between AVS and MPEG encoder and configuration of MPEG encoder.

Do you really want to double frame/field rate of your source ? Your expected output of AVC format shows standard 25 FPS frame rate.
DTL is offline   Reply With Quote
Old 21st September 2023, 18:21   #6  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,285
If the source content is interlaced, and it is encoded interlaced - you don't need avisynth or any scripts. (ie. the source is ok, and media players recognize it properly to begin with)

Just encode it interlaced - This does not change the "smoothness" or anything , it keeps it the content the same as the input. (In another forum you mentioned something about increasing smoothness, but provided no details about the content on the actual sources - mediainfo does not provide that information) . So the question is WHY are you doing this?

The interpolation scripts assumed your sources were 25p content encoded as 25i. The script interpolates the content to 50p (synthesizing new inbetween frames, creating more samples), then re-interlaces to 25i (50 fields/s interlaced content and encoding) - this changes the actual content and increases smoothness, at the risk of artifacts (sometimes severe) . If you don't know what you have, post a sample of the source. Nobody can help you properly if you don't provide the correct information

If you want AVC interlaced PAFF, x264 cannot do it - it only encodes MBAFF (There is a modified branch on github, but I don't know if it works and you have to compile to test it https://github.com/kierank/x264-paff) . MBAFF yields higher quality at the same bitrate compared to PAFF. MBAFF is compatible with most equipment and media players - many blu-rays and broadcasts use MBAFF

But if you require PAFF, you can use NVEnc (FFMpeg or NVEncC) to encode interlaced PAFF with separated fields, but the quality is significantly lower (ie. you need higher bitrates to achieve similar quality) . The desired "output" mediainfo is 36.9Mb/s - so that is quite high and should be enough bitrate if that is your target bitrate range. Then the question again, is why are you doing this - the bitrate is about the same as the source. A side benefit is NVEnc encodes very fast (but requires an NVidia card) . Alternatively, you can use a paid NLE like Premiere Pro , but the quality is again lower for AVC PAFF using it's bundled encoder.
poisondeathray is offline   Reply With Quote
Old 21st September 2023, 20:21   #7  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,000
I see your post at videohelp forum about doubling frame rate while keeping interlaced storage domain.

To do it with better quality you need to decompress interlaced fields to frames (QTGMC()). Next double framerate (I hope RIFE() is good enough for this). And downsample fileds to interlaced format again. So script is something like
Code:
Source("file.ext")
QTGMC(preset=..) // convert 25i to 50 fps progressige high quality
RIFE(..) // convert 50p to 100p fps in progressive domain
UserDefined2Resize(b=.., c=.., src_top=0.0001) // apply some vertical conditioning (low-pass anti-Gibbs filtering)
AssumeTFF()
SeparateFields()
SelectEvery(4,0,3)
Weave() // sample 100p to 50i
Though if your display device have frame rate upsampler it may do it realtime (maybe with lower quality in compare with RIFE() though). Also you need to check for compatibility of used level/profile of MPEG-AVC so your encoded 50i stream will be played OK at your target playback devices.

Last edited by DTL; 21st September 2023 at 20:24.
DTL is offline   Reply With Quote
Old 22nd September 2023, 22:38   #8  |  Link
simon744
Registered User
 
Join Date: Sep 2023
Posts: 10
Quote:
Originally Posted by poisondeathray View Post
Just encode it interlaced - This does not change the "smoothness" or anything , it keeps it the content the same as the input.
That's what I'm here for, man!! I don't know how to do that. I need help!

Quote:
(In another forum you mentioned something about increasing smoothness, but provided no details about the content on the actual sources - mediainfo does not provide that information) .
To be fair, I did provide a MediaInfo log of the source material I was working on, which should give a very clear picture and detail about "the content or the actual sources." In addition, I no longer seek to increase smoothness, or field/frame rate of my video. I simply need to re-encode, but interlaced, with separated fields.

Quote:
So the question is WHY are you doing this?
What a question! Because I want to, and need to.

Quote:
If you want AVC interlaced PAFF, x264 cannot do it - it only encodes MBAFF (There is a modified branch on github, but I don't know if it works and you have to compile to test it https://github.com/kierank/x264-paff) . MBAFF yields higher quality at the same bitrate compared to PAFF. MBAFF is compatible with most equipment and media players - many blu-rays and broadcasts use MBAFF

But if you require PAFF, you can use NVEnc (FFMpeg or NVEncC) to encode interlaced PAFF with separated fields, but the quality is significantly lower (ie. you need higher bitrates to achieve similar quality) .
Looks like what I want to achieve may not be fully possible, or at least, easy to achieve, with AviSynth. I wonder if I misunderstood or missed an important part about what that filter is designed to do, which I probably did.

Quote:
The desired "output" mediainfo is 36.9Mb/s - so that is quite high and should be enough bitrate if that is your target bitrate range.
That was just a random file I picked. I am not really looking to re-encode my files with such settings or presets that they end up having almost the same, or even a bitrate higher than their source.

Quote:
Then the question again, is why are you doing this - the bitrate is about the same as the source.
Once again, because I want to, and need to!
simon744 is offline   Reply With Quote
Old 22nd September 2023, 23:00   #9  |  Link
simon744
Registered User
 
Join Date: Sep 2023
Posts: 10
Quote:
Originally Posted by DTL View Post
I see your post at videohelp forum about doubling frame rate while keeping interlaced storage domain.
Yes, that's correct. However, I have now gained more knowledge and understanding of the "physics" behind the specific thing I'm looking for, and thus have decided *not* to make any attempts to double field/frame rate of the video. Instead, all I need is re-encoding the already interlaced files to interlaced with separated fields. It's important that the output be recognized and treated as interlaced by MediaInfo and media players.

Quote:
To do it with better quality you need to decompress interlaced fields to frames (QTGMC()). Next double framerate (I hope RIFE() is good enough for this). And downsample fileds to interlaced format again. So script is something like
Code:
Source("file.ext")
QTGMC(preset=..) // convert 25i to 50 fps progressige high quality
RIFE(..) // convert 50p to 100p fps in progressive domain
UserDefined2Resize(b=.., c=.., src_top=0.0001) // apply some vertical conditioning (low-pass anti-Gibbs filtering)
AssumeTFF()
SeparateFields()
SelectEvery(4,0,3)
Weave() // sample 100p to 50i
Thank you very much for the script! I have tried it, but the "Source" was red, which, I think meant that I had an important plugin missing from my setup to move forward with.

As to the dots inside the parentheses, as in...:
Code:
QTGMC(preset=..)
and/or:
Code:
RIFE(..)
Code:
UserDefined2Resize(b=.., c=.., src_top=0.0001)
Am I supposed to replace them with certain values to clarify settings/presets I'd like the program to utilize? If yes, then what would you recommend me to enter in place of them?
simon744 is offline   Reply With Quote
Old 22nd September 2023, 23:59   #10  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,478
Quote:
Originally Posted by simon744 View Post
Once again, because I want to, and need to!
One reason to ask such a question is that it may turn out that you don't need to after all - if your reason for thinking you need to turns out to be faulty - or it may allow someone to suggest an even more appropriate tool if we know more specifics. But just "I need to" doesn't help with either of those things.
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is online now   Reply With Quote
Old 23rd September 2023, 02:34   #11  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,285
Quote:
Originally Posted by simon744 View Post
To be fair, I did provide a MediaInfo log of the source material I was working on, which should give a very clear picture and detail about "the content or the actual sources." In addition, I no longer seek to increase smoothness, or field/frame rate of my video. I simply need to re-encode, but interlaced, with separated fields.
Not clear at all

Again, mediainfo provides no information about the actual content. Mediainfo only provides information about flagging and encoding type

e.g you could have progressive content encoded interlaced . You could have various pulldown patterns. You could have field blending. You could have phase shifting (looks combed but is progressive). Mediainfo does not report anything about the most important information - the content

Quote:
Looks like what I want to achieve may not be fully possible, or at least, easy to achieve, with AviSynth. I wonder if I misunderstood or missed an important part about what that filter is designed to do, which I probably did.
You don't need avisynth if you're just encoding interlaced. If you're doing some other manipulations, then maybe you can use it, but it's unclear what you have in terms of content. And you seem unable to communicate the relevant details. It's trivial to post a sample

Quote:

That was just a random file I picked. I am not really looking to re-encode my files with such settings or presets that they end up having almost the same, or even a bitrate higher than their source.
But why would you do that ? You increase the filesize and decrease the quality - that's the nature of lossy encoding. You could double the bitrate, double the filesize, and it would still be slightly lower in quality

eg. ffmpeg nvenc PAFF encoding @ 40Mb/s , audio copied from source. Assumes HD source with Top field first
Quote:
"ffmpeg" -i "input.ext" -vf setfield=tff -flags +ilme+ildct -pix_fmt nv12 -preset:v slow -c:v h264_nvenc -b:v 40000k -c:a copy "output.mkv"
poisondeathray is offline   Reply With Quote
Old 23rd September 2023, 10:25   #12  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,000
Quote:
Originally Posted by simon744 View Post
Thank you very much for the script! I have tried it, but the "Source" was red, which, I think meant that I had an important plugin missing from my setup to move forward with.

As to the dots inside the parentheses, as in...:
Code:
QTGMC(preset=..)
and/or:
Code:
RIFE(..)
Code:
UserDefined2Resize(b=.., c=.., src_top=0.0001)
Am I supposed to replace them with certain values to clarify settings/presets I'd like the program to utilize? If yes, then what would you recommend me to enter in place of them?
For Source() you need to set compatible and/or preffered source filter to read your source files.

And yes - for .. arguments you need to read the docs on the listed filters to set best params for your task (typically defining quality/performance balance like profile for QTGMC or more settings, also anti-Gibbs/anti-aliasing prefiltering before downsampling progressive to interlaced scan mode depends on your source sharpness and all previous filters non-linearity and your preffered 'makeup/look' of V-sharpness/acutance of the output and balance with possible aliasing).

Also as RIFE() typically (currently) only works with abstract float32 RGB samples only it is required more convert filters before and after like
Code:
z_ConvertFormat(pixel_type="RGBPS", colorspace_op="709:709:709:l=>rgb:709:709:f")
Rife(gpu_thread=1, model=6, sc=true, sc_threshold=0.12) # defaults to double frame rate
z_ConvertFormat(pixel_type="YUV420P8", colorspace_op="rgb:709:709:f=>709:709:709:l")
And their settings depends on chroma subsampling mode you want to use in total workflow. That example is for YV12 (4:2:0 8bit planar).
For 4:2:2 you need to change
Code:
z_ConvertFormat(pixel_type="YUV420P8", colorspace_op="rgb:709:709:f=>709:709:709:l")
line (pixel_type=) to compatible 4:2:2 format with downstream filters and your MPEG-AVC encoder.

"what would you recommend me to enter in place of them?"

You can try to start with

QTGMC(preset="Slow") - see examples at http://avisynth.nl/index.php/QTGMC
Rife(gpu_thread=1, model=6, sc=true, sc_threshold=0.12) # defaults to double frame rate
UserDefined2Resize(b=105, c=0, src_top=0.0001) - see https://forum.doom9.org/showthread.p...50#post1951250 for recommended b/c params combinations, increasing b/c makes things softer and decreases ringing,aliasing,sharpness,acutance. For simple start you can try replace it with Blur(amountV=0.5) (adjust param in about 0.2..1.0+ range).

You may skip difference between PAFF/MBAFF interlaced encoding modes in AVC because typically MBAFF produces better quality so maybe only supported mode in opensource freeware encoders like x264 or libx264 . MediaInfo will report you MBAFF interlaced. Like
Code:
Scan type                                : MBAFF
Scan type, store method                  : Interleaved fields
Scan order                               : Top Field First

Last edited by DTL; 23rd September 2023 at 10:54.
DTL is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 20:00.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.