Jpeg2000 encoding for digital cinema

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
elmarikon

Jpeg2000 encoding for digital cinema

Post by elmarikon »

Hi guys!

I am trying to convert TIFFs to Jpeg2000 codstream images (j2c) for digital cinema use.
With Jasper it is just sth like "geojasper -f <infile> -T jpc -F <outfile>"
That works fine for me excapt jasper can't do colorspace conversion.

With ImageMagick I gave it a try like this:

"convert -compress jpeg2000 -colorspace xyz <infile.tiff> <outfile.jpc>"
(of cause i gave the whole thing a try without the 'colorspace xyz' option as well)

For D-cinema the sequence has to be wrapped into a mxf container. I am doing this with asdcplib.
The geojasper images work fine, but when I try the ImageMagick results I get an error:
"unexpected number of components: 1"

Can that be true, and if:
Can anyone tell me how to change the number of components in the image?
I have been going allmost through all the manual and couldn't find a thing about that.

atb!
swk
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Jpeg2000 encoding for digital cinema

Post by magick »

ImageMagick uses 1 component for grayscale images and 3 for RGB (and 4 for RGBA). To fix, add -type truecolor before your JPC image filename.
elmarikon

Re: Jpeg2000 encoding for digital cinema

Post by elmarikon »

Wow!

Thanx a lot! What a quick precise and 100% correct answer!
That was my problem

thanx! thanx! thanx! thanx! thanx!


SwK
elmarikon

Re: Jpeg2000 encoding for digital cinema

Post by elmarikon »

Hi again!

I am still working on thet D-cinema issue.
With last suggestion, I managed asdcplib to accept the images as input files. So far so good....
But the server won't play the file. Only green and magenta on the screen.

I found out some file size limitations, which i followed, but most of the stuff, written in the specs is like chinese for me:-(

Did anyone ever try to use ImageMagick for D-cinema?
Has anyone got a hint for me?

here are the specs, the 'interesting' part is on pages 39 - 41:

http://www.dcimovies.com/DCIDigitalCine ... ecv1_2.pdf


thanx in advance!

ElMarikon
JaniceSeptember

Re: Jpeg2000 encoding for digital cinema

Post by JaniceSeptember »

I am still alive on thet D-cinema issue. With endure suggestion, I managed asdcplib to acquire the images as ascribe files. So far so good.... But the server will not play the file. Only blooming and amethyst on the screen. I begin out some book admeasurement limitations, which i followed, but a lot of of the stuff, accounting in the specs is like chinese for me
jusiponen

Re: Jpeg2000 encoding for digital cinema

Post by jusiponen »

I'm also experimenting with DCP creation.

Dolby server gave me a black screen when I had ImageMagick create the Jpeg2000 files. I've had some success with the following workflow:

Code: Select all

@echo off
REM #############################################
REM ## Requires: ImageMagick & Opencinematools ##
REM #############################################

REM ###########################
REM ## Make temp directories ##
REM ###########################
mkdir sound
mkdir tiff
mkdir jp2k
mkdir TESTCLIP_XYZ

REM ##########################
REM ## Extract sound assets ##
REM ##########################
ffmpeg -y -i testclip.mov sound\TESTCLIP_XYZ.wav

REM ##########################
REM ## Extract image assets ##
REM ##########################
ffmpeg -y -i testclip.mov -f image2 -vcodec tiff tiff\%%06d.tif

REM #########################################
REM ## Scale and convert to XYZ colorspace ##
REM #########################################
mogrify -verbose -resize 2048x1080 -colorspace xyz -type truecolor tiff\*.tif

REM ##########################
REM ## Jpeg2000 compression ##
REM ##########################
image_to_j2k -cinema2K 24 -ImgDir tiff -OutFor j2c

REM ##################################
REM ## Move j2c files, delete tiffs ##
REM ##################################
move tiff\*.j2c jp2k\
del tiff\*.tif
rmdir tiff

echo.
echo  Read before you continue !!!
echo.
echo  Split sound\TESTCLIP_XYZ.wav in two separate 1 channel WAV files called chan_00_L.wav and chan_01_R.wav.
echo  (24bit PCM, Split stereo to mono, Export to multiple files, WAV (Microsoft)/24bit signed)
echo.
pause

REM ###########################
REM ## MXF wrap image assets ##
REM ###########################
asdcp-test.exe -c TESTCLIP_XYZ/TESTCLIP_XYZ.jp2k.mxf jp2k

REM ###########################
REM ## MXF wrap sound assets ##
REM ###########################
asdcp-test -c TESTCLIP_XYZ/TESTCLIP_XYZ.pcm.mxf sound\chan_00_L.wav sound\chan_01_R.wav 

REM #################################################################################################
REM ## Create CPL, PKL, ASSETMAP & VOLINDEX                                                        ##
REM ## Opencinematools emits XML namespaces incompatible with Dolby Servers (can not ingest),      ##
REM ## fixing with following sed search-and-replace operations:                                    ##
REM ## volindex: s/smpte-ra.org\/schemas\/429-9\/2007\/AM/digicine.com\/PROTO-ASDCP-VL-20040311#/g ##
REM ## cpl: s/smpte-ra.org\/schemas\/429-7\/2006\/CPL/digicine.com\/PROTO-ASDCP-CPL-20040511#/g    ##
REM ## pkl: s/smpte-ra.org\/schemas\/429-8\/2007\/PKL/digicine.com\/PROTO-ASDCP-PKL-20040311#/g    ##
REM ## assetmap: s/smpte-ra.org\/schemas\/429-9\/2007\/AM/digicine.com\/PROTO-ASDCP-AM-20040311#/g ##
REM #################################################################################################
cd TESTCLIP_XYZ
mkcpl --kind trailer --title "TESTCLIP_XYZ" --norating TESTCLIP_XYZ.jp2k.mxf TESTCLIP_XYZ.pcm.mxf | sed -f ..\cpl.sed > TESTCLIP_XYZ.cpl.xml
mkpkl --annotation "TESTCLIP_XYZ" TESTCLIP_XYZ.jp2k.mxf TESTCLIP_XYZ.pcm.mxf TESTCLIP_XYZ.cpl.xml | sed -f ..\pkl.sed > TESTCLIP_XYZ.pkl.xml
mkmap --annotation "TESTCLIP_XYZ" TESTCLIP_XYZ.jp2k.mxf TESTCLIP_XYZ.pcm.mxf TESTCLIP_XYZ.cpl.xml TESTCLIP_XYZ.pkl.xml

type VOLINDEX.xml | sed -f ..\volindex.sed > VOLINDEX.tmp
type ASSETMAP.xml | sed -f ..\assetmap.sed > ASSETMAP.tmp
move /y VOLINDEX.tmp VOLINDEX.xml
move /y ASSETMAP.tmp ASSETMAP.xml
cd ..
Ingests and plays fine, but colors are wrong (horrifyingly red...). I suspect XYZ conversion is not happening, or if it is then ImageMagick is converting the image back to RGB colorspace when writing out the TIFF.

I inspected the TIFFs before and after ImageMagick with TortoiseSVN Image Diff utility and it showed almost no difference (what delta there was, I'd attribute to rounding in colorspace conversion math). The diffrence in color before and after is huge (as I'd expect) if I have ImageMagick write out JPGs instead of TIFFs, but image_to_j2k doesn't accept JPG as input.

Until I can figure out the color space conversion, I'm pretty much stuck...
jusiponen

Re: Jpeg2000 encoding for digital cinema

Post by jusiponen »

Turns out you can get the colors right by changing my previuos workflow like this:

Substitute this part...

Code: Select all

mogrify -verbose -resize 2048x1080 -colorspace xyz -type truecolor tiff\*.tif
...with this

Code: Select all

cd tiff
for %%F IN (*.tif) DO convert %%F -alpha Off -resize 2048x1080 -recolor "0.412453 0.357580 0.180423 0.212671 0.715160 0.072169 0.019334 0.119193 0.950227" -depth 12 ..\jp2k\%%F
cd ..
The "-alpha Off" switch has to be there, othervise you'll get RGB tiffs.

One more thing -- the asset map needs two fixes, not just one, thus:

Code: Select all

type ASSETMAP.xml | sed -f ..\assetmap.sed | sed -f ..\assetmap2.sed > ASSETMAP.tmp

(assetmap2.sed = "s/file:\/\/\///g")
Now if only the scaling, recoloring and JPEG2000 compression could be done in a single mogrify command I'd be one happy camper, because currently converting even a 20 min clip takes astronomical amounts of disk space to store the scaled XYZ tiffs...
elmarikon

Re: Jpeg2000 encoding for digital cinema

Post by elmarikon »

for %%F IN (*.tif) DO convert %%F -alpha Off -resize 2048x1080 -recolor "0.412453 0.357580 0.180423 0.212671 0.715160 0.072169 0.019334 0.119193
great work!
I did already read, the rgb to xyz conversion in ImageMagick is not really what you need for D-Cinema...

Do you have any ideas, how to do sth similar with avisynth filters?
http://avisynth.org/mediawiki/External_ ... and_Chroma
Do you think this is possible??


All the best so far!

SwK
jusiponen

Re: Jpeg2000 encoding for digital cinema

Post by jusiponen »

Avisynth seems very powerful, but I'm not familiar with it at all...

The Avisynth project seems to have a similar user forum, maybe they can help you out?
elmarikon

Re: Jpeg2000 encoding for digital cinema

Post by elmarikon »

Okay, I will have a look at it...
But the documentation of 'recolor' seems a little curt.
Maybe you could explain me a little, what you did there, then I can try to "convert" into an Avisynth script and post it here.
-recolor matrixTranslate, scale, shear, or rotate image colors.

Although variable-sized matrices can be used, typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA. Populate the last row with normalized values to translate.


btw... how can you live without Avisynth?! I think it is just amazing!!


All the best!
jusiponen

Re: Jpeg2000 encoding for digital cinema

Post by jusiponen »

For this particular conversion the -recolor takes a 3x3 matrix. It should make more sense if I write it like this:

Code: Select all

-recolor "0.412453 0.357580 0.180423
          0.212671 0.715160 0.072169
          0.019334 0.119193 0.950227"
The matrix above is the standard RGB -> XYZ. More can be found at http://www.brucelindbloom.com/index.htm ... atrix.html.

You may find a better match there, depending on your working RGB space and reference white.

The standard conversion matrix seems to give reasonable results for me: no worse than what the Quvis Wraptor seems to do, but not as good as the Fraunhofer EasyDCP. As it is, the colors come out a bit too dark and saturated. Some other matrix might give better results, but I'd have to run a bunch of tests to find out...

Re Avisynth: I don't usually edit stuff, I just convert clips others have made.
elmarikon

Re: Jpeg2000 encoding for digital cinema

Post by elmarikon »

The matrix above is the standard RGB -> XYZ. More can be found at http://www.brucelindbloom.com/index.htm ... atrix.html.
Okay, great.
I will have a look at it...




I don't usually edit stuff, I just convert clips others have made.
That's exactly what I do, and avisynth is perfect for this. You can crop, resize, deinterlace, recolor, sharpen, unsharpen, add borders, sqeese, stretch, denoise, filter,......... all in one step...
fodevaux

Re: Jpeg2000 encoding for digital cinema

Post by fodevaux »

A small explanation on why JPEG 2000 codestreams created by Jasper cannot be decoded by DCinema servers.
The problem is that Jasper is currently not capable of generating codestreams compliant with the Digital Cinema Profile the JPEG committee (originally specified by the DCI). The codestreams require special parameters such as code-block size, precinct sizes, ...

To my knowledge, the only existing open-source solution is the OpenJPEG library (the following command line above posted by jusiponen calls the OpenJPEG library: image_to_j2k -cinema2K 24 -ImgDir tiff -OutFor j2c).
Wolfgang Woehl
Posts: 34
Joined: 2010-02-25T15:22:50-07:00
Authentication code: 8675308

Re: Jpeg2000 encoding for digital cinema

Post by Wolfgang Woehl »

jusiponen wrote:For this particular conversion the -recolor takes a 3x3 matrix. It should make more sense if I write it like this:

Code: Select all

-recolor "0.412453 0.357580 0.180423
          0.212671 0.715160 0.072169
          0.019334 0.119193 0.950227"
The matrix above is the standard RGB -> XYZ. More can be found at http://www.brucelindbloom.com/index.htm ... atrix.html.
Hi Jusiponen, I wonder: these numbers are close to what Bruce calculated for the sRGB to XYZ transform, but not quite? http://www.imagemagick.org/script/comma ... colorspace has yet another matrix. When I calculate a Rec. 709 to XYZ matrix (see http://github.com/wolfgangw/digital_cinema_tools for a script to do that) I come up with yet another set of numbers. Leaves me a bit baffled as I'd expect Bruce's sRGB numbers: same primary chromaticities and white point chromaticity. To my knowledge (too big a word here, really) the difference between sRGB and Rec. 709 one would need to respect is gamma. Hm, what gives?
jusiponen wrote:The standard conversion matrix seems to give reasonable results for me: no worse than what the Quvis Wraptor seems to do, but not as good as the Fraunhofer EasyDCP. As it is, the colors come out a bit too dark and saturated. Some other matrix might give better results, but I'd have to run a bunch of tests to find out...
You're not decoding gamma to perform the color transform on linear values. Also the order of operators would make a difference, I guess (no tests with this, though). Like

Code: Select all

convert srgb.tiff -alpha off -depth 12 -gamma $degamma_2_2 -recolor "$srgb_to_xyz" -gamma 2.6 dci-xyz.tiff
in order to perform the transform with the targeted bit depth and on linear values. $degamma_2_2 would contain the number 1/2.2 (approximated sRGB gamma) and $srgb_to_xyz the 3x3 matrix.

Another thing: For the JPEG 2000 encoding I'm experimenting with kdu_compress (Kakadu demo, see http://www.kakadusoftware.com/index.php ... &Itemid=22). It's not open source and its license allows for trial and redistribution minus commercial use. Point is: It is significantly faster than openjpeg's implementation. Make sure to read the notes (need to make a shared lib known to your system) and usage examples (grep for "cinema") and throw uncompressed tiffs at it

Code: Select all

convert input.tiff [...] -compress none output.tiff
.

Wolfgang
jusiponen

Re: Jpeg2000 encoding for digital cinema

Post by jusiponen »

Hello Wolfgang,

It's likely I have just copied the transform matrix from somewhere as I've foraged the Interwebs for answers :lol: So the numbers are different by accident rather than intent.

I ignored the whole gamma issue because I couldn't figure out how to get the de-gamma -> transform -> gamma pipeline correct.

Code: Select all

-resize 2048x1080
We've both so far ignored this issue: while -resize scales the input image to fit x or y boundary (whichever is more limiting), the result may still not be of correct size. Padding might be needed at top/bottom or left/right. Imagemagick can do that with:

-background black -gravity center -extent 1998x1080 (flat 2K)
-background black -gravity center -extent 2048x858 (scope 2K)

From your Github Wiki article:
Encoding time for a 2K tiff on a recent Intel core is around 2-4 seconds. That’s around 5 days for a 100-minute feature.
Assuming disk access does not become a bottleneck, one could write say a shell script to split a folder of images N ways and lauch N different processes to do the actual encoding.

Writing a script which takes all this into account and does the right things depending on the command line arguments and what it can determine from the images extracted by ffmpeg (or any other utility of choise) should take a day or two of tinkering. What still remains to be addressed is how to deal with audio assets -- I'm currently doing the necessary conversions with Audacity, if anyone knows how to accomplish this with command line tools, please let us know.
Post Reply