Rough Guide to converting 8bit video source to 16bit Scene Referred Linear Openexr, alternatively 16bit .tif image sequences or 10bit Lossless h264.

By the way, this is not just 8bit in a 16bit file, type of conversion, shoe box in a steamer trunk analogy and the usual “no point all you get is bigger file sizes”, this method generates additional data.

It works by denoising the source and with the LSB (Least Significant Bit) data of each frame and stacks it with the MSB (Most Significant Bit), the extra data bolstering each image frame and giving more data to grade with and option to dither with an encoder friendly dithering method to reduce the risk of banding.

http://en.wikipedia.org/wiki/Bit_plane

Here’s a link to a 8bit & 16bit version of the same frame from a Canon MOV 1920×1088 source.

Samples.zip

Welcome comments if you can see a degrading of the image by stacking LSB/MSB .

Please test them with a grade and as you manipulate them check the histograms for combing, compare the performance of the 8bit against the 16bit, whether you feel there’s any point in generating bigger files for the added ‘gradeability’ is your choice, but hard disk space is cheap. :-)

Direct import of the MOV into Blenders 32bit float Compositing Nodes and float data in the VSE fared no better than the 8bit source in the samples above where as the 16bit stood up to more processing. 32bit float processing of 8bit sources in Blender is not enough to prevent combing of the 8bit source.

The process set out below assumes the video sources are encoded based on a Rec601 or Rec709 transfer curve, the process outlined below uses an inverse Rec601/Rec709 curve to linearise the source rather than using the inverse of a typical sRGB 0.45. Which does not create a true scene linear output for video sources they weren’t gamma encoded with sRGB curve to start with but a Rec601/709. :-)

So there should be no squeezing of shadow detail by inverting the wrong gamma curve. :-) ‘Scene’ referred linear rather than treating as a ‘Display’ referred source. This is nothing to do with ‘crushing blacks’ from an incorrect color space conversion to RGB by scaling luma.

Prerequisites:

Avisynth 2.5.x (2.5.8 currently)

http://sourceforge.net/projects/avisynth2/files/

FFmpegSource2 plugin (Don’t use 2.16 it’s buggy for full range luma sources)

FFMS2-2.1.5

AVS2yuv.exe

http://akuvian.org/src/avisynth/avs2yuv/

Imagemagick Q16 or hdri compiled options.

If your on Linux doing all this with Wine then then AVS2yuv will happily pipe data to a Linux build of Imagemagick.

http://www.imagemagick.org

x264 10bit [JEEB Patched]

http://x264.fushizen.eu

Grab the 32bit or 64bit 10bit build from top of thread.

AVSPmod, Virtualdub or Favourite Text Editor.
(You may have problems with AVSPmod 2.2.0 version, try 2.1.8 if thats the case.)

http://forum.doom9.org

Dither Plugin

http://ldesoras.free.fr/src/dither-1.13.3.zip

dfttest Plugin

http://web.missouri.edu/~kes25c/dfttestv18.zip

Masktools2 version a48 or above

http://manao4.free.fr/masktools-v2.0a48.zip

Process:

Install Avisynth.

Copy various plugin zips to Avisynths plugins folder in Program Files\Avisynth 2.5\ unzip them to folders, then copy out the .dll’s and .avsi’s directly into Avisynths plugins folder, this simplifies paths to various plugins when writing scripts.

Install Imagemagick Q16 / hdri. Open a console or cmd and type identify -version. Hopefully you’ll see something similar to:

Version: ImageMagick 6.7.0-8 2011-06-26 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP HDRI

To get up and running create a folder and put x264.exe, AVS2yuv.exe and your test movie file into it.

Everything that follows assumes Canon HDSLR h264AVC 1920×1088 source. Edit scripts to suit source if it differs. If you’re importing 4:3 PAR sources you may wish to resize to 1:1 PAR within the scripts below. For HDV 1440×1080 source add the line:

Dither_resize16 (1920, 1080, 0, 0, 1440, 1080, "spline36", cplace="mpeg2")

after Dither_Convert_8_to_16 () and ensure that when using the AVS2yuv command lines that you amend –input-res and/or -size accordingly. ie: 1920×1080

Open AVSPmod or your favourite text editor and create to .avs scripts suitably named. Strongly suggest AVSPmod, or Virtualdub.

This script for RGB output to go to Imagemagick for image sequences:

LoadPlugin("c:\Program Files\AviSynth 2.5\plugins\ffms2.dll")
LoadPlugin("c:\Program Files\AviSynth 2.5\plugins\mt_masktools-25.dll")
FFmpegSource2("MVI_0000.MOV")
Dither_convert_8_to_16 ()
#Dither_convert_yuv_to_rgb(matrix="601", tv_range=false, cplace="MPEG2", chromak="bicubic", lsb_in=true, output="rgb48y")
#Dither_y_gamma_to_linear (tv_range_in=false, tv_range_out=false, curve="709")
#Dither_convey_rgb48_on_yv12 (SelectEvery (3, 0),SelectEvery (3, 1),SelectEvery (3, 2) )
DitherPost()

And 2nd script to go to 10bit Lossless x264:

LoadPlugin("c:\Program Files\AviSynth 2.5\plugins\ffms2.dll")
LoadPlugin("c:\Program Files\AviSynth 2.5\plugins\mt_masktools-25.dll")
FFmpegSource2("MVI_0000.MOV")
Dither_convert_8_to_16 ()
#Dither_convey_yuv4xxp16_on_yvxx()
DitherPost()

Open and preview your first script in AVSPmod or Virtualdub to test that the basics are in place. You should simply see the first frame of your video. Then do the same with your second script and test that.

You may get an error message about certain plugins having ‘_’ underscores rather than ‘-’ hyphens, but really just make sure your Avisynth plugins name and the names and locations in the script pointing to them match.

If all is working, we can move onto AVS2yuv. :-)

Unfortunately neither Virtualdub or AVSPmod can export 16bit data from within Avisynth, luckily AVS2yuv or AVS2pipe (which includes audio) can pipe the 16bit data to an external application like Imagemagick and a 10bit build of x264.

So to get the 10bit or 16bit data out we first have to unhash the #Dither… lines from our scripts and resave them. You’ll need to add a hash in front of the DitherPost() entry. DitherPost() takes the 16bit data back to 8bit. We needed that line active so we could preview in Virtualdub or AVSPmod. ;-)

Once the scripts are tweaked test them out by opening a console, cd’ing to the folder you created holding the scripts, source file, encoder etc and enter this (assumes Linux, if on Windows remove the wine bit) into the console for linear .exr’s:

wine ./avs2yuv.exe -raw "script_Lin_RGB.avs" -o - | convert -depth 16 -size 1920x1088 -set colorspace RGB -colorspace RGB rgb:- "output_%06d.exr"

If you get out of memory errors try this on the CLI:

identify -list resource

Which will show you how much resources Imagemagick considers available. For example you may have 3GB memory and 6GB map. Imagemagick can be limited to use only so much RAM and the rest of the video data cached in /tmp (on Linux)

To limit resources available to Imagemagick add -limit memory 1GiB -limit map 2GiB in between:

convert and -depth

Change the GiB or MiB values above to fall within the resources listed by Imagemagick for your hardware.

For 10bit Lossless h264:

wine ./avs2yuv.exe -raw "script_YCC.avs" -o - | ./x264-64-10bit.exe - --demuxer raw --input-depth 16 --input-res 1920x1088 --fps 25 --fullrange on --preset ultrafast --qp 0 --output "10bitOutput.mp4"

Obviously adjust input-res to suit your source. Assuming 1920×1088. You might prefer .avi or .mkv as your output container rather than mp4. ;-)

If you prefer sRGB gamma 16bit .tif’s then hash out the Dither-y-gamma…… line from the RGB script and change the Imagemagick line to .tif rather than .exr or if you would prefer linear light .tif’s or .pngs for example you can embed an icc profile into your file with imagemagick by adding -profile NameOfYourICC.icc to the Imagemagick route on the CLI. I add the -profile whatever.icc before the -set colorspace stuff.

If you need a linear light sRGB ICC profile for any color managed application (that uses ICC’s) including After Effects 7.0 upwards then you can get them here on Stu Maschwitz awesome blog: http://prolost.com/blog/2006/2/7/linear-color-workflow-in-ae7-part-1.html and direct link to icc zip file here: LinearProfiles.zip

Just unzip and drop the ICC you want to use into the same location as the avs script & source video files. Make sure the filename in the CLI call is the same as the ICC file name. I always take an additional step of removing any spaces from the file name and/or replacing the spaces with underscores as this is the CLI and spaces can trip it all up. :-)

I intend making this process a bit easier via AVSPmod python macros, but for now to get up and running.

This is really the basics, there are many functions available within Avisynth to include in our scripts, many now at 16bit precision including gamma aware resizing, noise reduction, color correction, adding grain as well as hundreds of 8bit functions including sharpening, optical flow based slow motion, deinterlacing, upsampling, the list goes on.

Let me know how you get on and if there’s anything specific you’re after. I’m going to look at batch conversion python script for AVSPmod next.