2

I am trying to use ffmpeg to decode an image buffer from a DICOM image using YBR_FULL_422 encoding. According to the definition:

Two Y values shall be stored followed by one CB and one CR value. The CB and CR values shall be sampled at the location of the first of the two Y values. For each Row of Pixels, the first CB and CR samples shall be at the location of the first Y sample. The next CB and CR samples shall be at the location of the third Y sample etc.

Let's assume I have:

$ gdcminfo YBR_FULL_422.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.7 [Secondary Capture Image Storage]
TransferSyntax is 1.2.840.10008.1.2.1 [Explicit VR Little Endian]
NumberOfDimensions: 2
Dimensions: (600,430,1)
SamplesPerPixel    :3
BitsAllocated      :8
BitsStored         :8
HighBit            :7
PixelRepresentation:0
ScalarType found   :UINT8
PhotometricInterpretation: YBR_FULL_422
PlanarConfiguration: 0
...

So I simply tried to extract the raw buffer:

$ gdcmraw YBR_FULL_422.dcm YBR_FULL_422.raw
$ du -sb YBR_FULL_422.raw
516000  YBR_FULL_422.raw

Which is compatible with the size of the image: 600*430*2 = 516000

But I cannot convert it to regular rgb24:

$ ffmpeg -y -f rawvideo -pix_fmt yuv422p -s:v 600x430 -i YBR_FULL_422.raw rgb24.ppm
Input #0, rawvideo, from 'YBR_FULL_422.raw':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 103200 kb/s
    Stream #0:0: Video: rawvideo (Y42B / 0x42323459), yuv422p, 600x430, 103200 kb/s, 25 tbr, 25 tbn, 25 tbc
Output #0, image2, to 'rgb24.ppm':
  Metadata:
    encoder         : Lavf57.56.101
    Stream #0:0: Video: ppm, rgb24, 600x430, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.64.101 ppm
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> ppm (native))
Press [q] to stop, [?] for help
frame=    1 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=36.9x    
video:756kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

The output image is greenish with black strides.

malat
  • 1,374

1 Answers1

4

yuv422p is a planar format i.e. all Y for a frame followed by all Cb, then Cr..etc. You want a packed format. FFmpeg supports three of them with 8-bit depth and 4:2:2 subsampling: yuyv422, uyvy422 and yvyu422. Since Cb is stored first, that rules out the last one. Try the first two.


Since the pixel format appears to be "yyuv422", try

 ffmpeg -y -f rawvideo -pix_fmt yuyv422 -video_size 600x430 -i YBR_FULL_422.raw -vf format=yuv422p,geq=lum='if(mod(X,2),cb((X-1)/2,Y),p(X,Y))':cb='lum(X*2+1,Y)':cr='p(X,Y)' rgb24.ppm
Gyan
  • 38,955