No announcement yet.

Decompress Ambarella compressed RAW files

  • Filter
  • Time
  • Show
Clear All
new posts

  • Decompress Ambarella compressed RAW files

    Decompress Ambarella compressed RAW files

    Last year I managed to crack Xiaomi Yi compressed RAW files. It turns
    that FF8SE compressed RAW files use exactly the same algorithm. Thus I
    think that this will work for any Ambarella compressed RAW files.

    I wrote small C program: amba_compressed_raw.c

    gcc -Wall -O3 -D_COMPILE_EXE -lm -o amba_compressed_raw.exe amba_compressed_raw.c

    amba_compressed_raw.exe [-o FILE_OUT.RAW] [-size WIDTH HEIGHT] [-pitch PITCH] FILE_IN.RAW
    Simplest case: try to guess parameters from file size
    amba_compressed_raw.exe FILE_IN.RAW
    Normal case: provide width and height
    amba_compressed_raw.exe -size 3840 2880 FILE_IN.RAW
    Hard case: provide width and height and pitch (if file truncated or odd)
    amba_compressed_raw.exe -size 3840 2880 -pitch 3264 FILE_IN.RAW

    COMPRESSION: 32 pixels are compressed into 27 bytes
    thus instead of 32*12bits=384bits algorithm uses only 27*8=216bits
    compression ratio is 216/384=56.25%, e.g. instead of 12bits,
    compression saves about 6.75bits. That sounds very drastic, but
    in reality nearby pixels will be very similar. However
    transitions between dark and bright areas will be more affected:
    look for artifacts there.
    While cracking the algorithm I had an impression that compression
    program on ambarella is not always choosing optimal base levels,
    i.e. the employed compression algorithm could perform better.
    There can be also additional pixels, to padd line, but as far as I can tell they are bogus, and are ignored here.

    PACKING OF BITS (those 27 bytes that contain 32 pixels):
    3 bits (common upper bits of 4 pixels)
    (6,6,6,6) bits for lower bits of 4 individual pixels
    Repeat group (3,6,6,6,6) eight times: 8*(3+6*4)==216bits=27bytes
    Final step is to reorder those 32 pixels (ordering is bayer aware, groups by colors).
    For details see function amba_decompress_27b()

    Xiaomi Yi

    If you find compressed file that doesn't work let me know. I will try to see what goes on. Corresponding JPGs may not have same width and height as compressed raw files. So maybe one needs to guess width and height and pitch.
    Also: I never found file with width NOT multiple of 32. Might fail for some odd numbers.
    Very useful is to inspect RTOS log, which nicely tells all those parameters, see here:

    If there is interest I will try to boot my windows and compile standalone EXE in cygwin... I am exclusively on Linux and rarely use Windows... Or if somebody else is willing to compile and provide standalone EXE, just go ahead.

    BIG QUESTION: is this native Ambrarella compression useful? On one hand 6.75bits per pixel does seem drastic and I am bit sceptical if it retains all details. But honestly it needs testing. I will eventually get to it....

    To get RAW files on A12 cameras:

    To convert RAW to DNG (and then open that DNG in any raw development tool):
    this raw2dng also needs to guess the sizes. I am not sure if it will work for all weird combos. But it is easy to add setting to INI file.

    Attached Files