Log in

View Full Version : From 16bit TIFFs to Y4M for x265?


LigH
16th April 2014, 08:05
I would like to create a Y4M file from a sequence of 16-bit-per-component (48 bit RGB) raw TIFF images for encoding in x265 with enabled HIGH_BIT_DEPTH flag.

Is this possible at all via AviSynth 2.6 and one of the AVS piping tools? Would ImageSource be able to read them as either TIFF or other 48 bit images? I rather doubt, it would have to be read as stacked LSB/MSB clip probably. ImmaSource won't handle that either. May one better try to make a specific converter without AviSynth being involved?

Samples can be found in e.g. http://media.xiph.org/tearsofsteel/tearsofsteel-4k-tiff/ or for other Blender movies.

foxyshadis
17th April 2014, 01:26
I'd work in vapoursynth, using VS-lsmashsource's LWLibavSource to load the TIFFs with format YUV420P16, then use fmtconv to sample it down to YUV420P10. You can use ffmpeg to write it out to y4m.

Note that x265 does not yet support reading from high-bit y4m, although that was discussed a month or two ago on the mailing list. You'll have to use raw YUV, or pipe the y4m through ffmpeg, either way specifying all of the video metadata manually. (And in that case, you might as well keep the raw TIFFs around unless it's just easier to work with a single file.)

LigH
23rd July 2015, 12:23
Coming back to this topic, now that x265 would support even Main12 profile ... which would be the recommended way now to use e.g. a subset of 16 bit-per-component TIFFs in 4K resolution from "Tears of Steel" I downloaded (frames 11270 to 12709 "graded_edit_%05d.tif", matching 60 seconds of scenes with different degrees of action)? I never used VapourSynth yet, a brief guide regarding possible quirks would be very welcome.

sneaker_ger
23rd July 2015, 12:55
Should be something like the following:
import vapoursynth as vs
core = vs.get_core()
ret = core.imwri.Read('graded_edit_%05d.tiff')
ret = core.fmtc.bitdepth(clip=ret, bits=12)
ret.set_output()

Then pipe as y4m:
vspipe --y4m script.vpy - | x265 - --y4m -o output.265

But somehow I don't have the ImageMagick plugin. I wonder if it's missing in the R27 installer?
http://forum.doom9.org/showthread.php?p=1731196#post1731196

LigH
23rd July 2015, 13:02
Just about to download; r27 is a lot smaller than r26. I'll compare the content...
__

It's not included and never will be due to its insane size.

... but here (http://forum.doom9.org/showthread.php?p=1709212#post1709212) seems to be a download link to DropBox.
__

OK, after installing imwri and fmtconv plugins, and studying the documentation a bit to get around several different error messages, I made my script up to the point that x265 accepts the incoming video stream and starts encoding...

tos_60s.vpyimport vapoursynth as vs
core = vs.get_core()
ret = core.imwri.Read('graded_edit_%05d.tif', firstnum=11270)
ret = core.std.AssumeFPS(clip=ret, fpsnum=24)
ret = core.fmtc.matrix (clip=ret, mat="709", col_fam=vs.YUV, bits=16)
ret = core.fmtc.resample (clip=ret, css="420")
ret = core.fmtc.bitdepth(clip=ret, bits=12)
ret.set_output()

tos_60s.vpy.batE:\Programme\VapourSynth\core64\vspipe --y4m tos_60s.vpy - | E:\Programme\x265\x265 - -D 12 --y4m -o tos_60s.vpy.hevc

sneaker_ger
23rd July 2015, 13:38
Well, unless you can compile ImageMagick and get it running yourself there don't seem to be many options left.
ffms2: fails on those tifs?
vsimagesource: deprecated
lsmash: needs start filename at 1 (or 0?), not 11270. (you could rename with a different program first). Crashes for me once I load for than 1 file, though.

As suggested by foxyshadis using lsmash:
import vapoursynth as vs
core = vs.get_core()
ret = core.lsmas.LWLibavSource('graded_edit_%05d.tif', format="YUV444P16")
ret = core.fmtc.matrix(clip=ret, mats="601", matd="709") #in case lsmash assumes BT.601 and you want BT.709
ret = core.fmtc.resample(clip=ret, css="420") #if you want 4:2:0 sampling
ret = core.fmtc.bitdepth(clip=ret, bits=12)
ret.set_output()

Lsmash does not seem to be able to output RGB48 for some reason so the YUV444P16 conversion can't be avoided although I don't trust it and it may be assuming BT.601.

sneaker_ger
23rd July 2015, 13:52
OK, after installing imwri and fmtconv plugins, and studying the documentation a bit to get around several different error messages, I made my script up to the point that x265 accepts the incoming video stream and starts encoding
Yeah, I just saw that test6 is still compatible. Your script looks good. (Only "bits=16" is unnecessary.)

LigH
23rd July 2015, 13:52
If I am not completely wrong, this is how to downscale to 720p in YUV444p16 and output as YUV420p12 eventually:

import vapoursynth as vs
core = vs.get_core()
ret = core.imwri.Read('graded_edit_%05d.tif', firstnum=11270)
ret = core.std.AssumeFPS(clip=ret, fpsnum=24)
ret = core.fmtc.matrix (clip=ret, mat="709", col_fam=vs.YUV, bits=16)
ret = core.resize.Spline(clip=ret, width=1280, height=536)
ret = core.std.AddBorders(clip=ret, top=92, bottom=92)
ret = core.fmtc.resample (clip=ret, css="420")
ret = core.fmtc.bitdepth(clip=ret, bits=12)
ret.set_output()

sneaker_ger
23rd July 2015, 14:10
Yes, but fmtc.resample can also do resizing:
import vapoursynth as vs
core = vs.get_core()
ret = core.imwri.Read('graded_edit_%05d.tif', firstnum=11270)
ret = core.std.AssumeFPS(clip=ret, fpsnum=24)
ret = core.fmtc.matrix (clip=ret, mat="709", col_fam=vs.YUV)
ret = core.fmtc.resample (clip=ret, w=1280, h=536, css="420")
ret = core.fmtc.bitdepth(clip=ret, bits=12)
ret = core.std.AddBorders(clip=ret, top=92, bottom=92)
ret.set_output()

I would always do adding borders last. Quality might be improved by leaving full range->limited range to fmtc.bitdepth step and do 32 bit float in-between but I assume the difference will be negligible.

LigH
23rd July 2015, 15:26
Despite all pessimism I heard about HEVC decoders not yet supporting Main12 ... no, I believe it was rather x265 not yet producing correct bitstreams. With x265 v1.7+367, it looks actually pretty well in MPC-HC 1.7.9 (just the colors appear a bit ... "vivid"):

tos_60s_720p_main12.vpy.hevc.mp4 (https://www.mediafire.com/download/88ajpt49y8p8fkt/tos_60s_720p_main12.vpy.hevc.mp4) (11 MB)