Skip to content

Instantly share code, notes, and snippets.

@johncf
Last active April 9, 2025 07:24
Show Gist options
  • Save johncf/7e18feee6acd717d72d87b7b9b5111a1 to your computer and use it in GitHub Desktop.
Save johncf/7e18feee6acd717d72d87b7b9b5111a1 to your computer and use it in GitHub Desktop.

Revisions

  1. johncf revised this gist Oct 9, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -104,3 +104,4 @@ Pillow-HEIF
    - Does `heif_image_handle_get_preferred_decoding_colorspace` or `heif_image_handle_get_nclx_color_profile` work on a handle of an auxiliary image? ([relevant source](https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/libheif/api/libheif/heif.h#L1181-L1188))
    - pyheif ref: https://github.com/carsales/pyheif/blob/master/pyheif/reader.py#L222-L248
    - https://pillow-heif.readthedocs.io/en/latest/installation.html ([windows requirements](https://stackoverflow.com/questions/40018405/cannot-open-include-file-io-h-no-such-file-or-directory#50210015))
    - https://nokiatech.github.io/heif/examples.html
  2. johncf revised this gist Oct 9, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -97,6 +97,7 @@ HDR Gain map
    - https://github.com/colour-science/colour/blob/develop/colour/models/rgb/transfer_functions/itur_bt_709.py#L39
    - https://stackoverflow.com/a/57034526
    - https://github.com/exiftool/exiftool/blob/405674e/lib/Image/ExifTool/Apple.pm#L175 (0x0021 = 33, 0x0030 = 48)
    - https://exiftool.org/forum/index.php?topic=14786.0

    Pillow-HEIF
    - https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/examples/heif_dec.cc#L604
  3. johncf revised this gist Oct 9, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -94,6 +94,9 @@ HDR Gain map
    - https://github.com/m13253/heif-hdrgainmap-decode/blob/master/heif-hdrgainmap-decode-y4m.py
    - https://developer.apple.com/documentation/appkit/images_and_pdf/applying_apple_hdr_effect_to_your_photos
    - https://en.wikipedia.org/wiki/Transfer_functions_in_imaging
    - https://github.com/colour-science/colour/blob/develop/colour/models/rgb/transfer_functions/itur_bt_709.py#L39
    - https://stackoverflow.com/a/57034526
    - https://github.com/exiftool/exiftool/blob/405674e/lib/Image/ExifTool/Apple.pm#L175 (0x0021 = 33, 0x0030 = 48)

    Pillow-HEIF
    - https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/examples/heif_dec.cc#L604
  4. johncf revised this gist Oct 8, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -98,4 +98,5 @@ HDR Gain map
    Pillow-HEIF
    - https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/examples/heif_dec.cc#L604
    - Does `heif_image_handle_get_preferred_decoding_colorspace` or `heif_image_handle_get_nclx_color_profile` work on a handle of an auxiliary image? ([relevant source](https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/libheif/api/libheif/heif.h#L1181-L1188))
    - pyheif ref: https://github.com/carsales/pyheif/blob/master/pyheif/reader.py#L222-L248
    - https://pillow-heif.readthedocs.io/en/latest/installation.html ([windows requirements](https://stackoverflow.com/questions/40018405/cannot-open-include-file-io-h-no-such-file-or-directory#50210015))
  5. johncf revised this gist Oct 8, 2024. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -85,5 +85,17 @@ Sources:

    ## Misc: HDR-merging and Tone-mapping using OpenCV

    https://learnopencv.com/high-dynamic-range-hdr-imaging-using-opencv-cpp-python/
    https://docs.opencv.org/3.4/d2/df0/tutorial_py_hdr.html
    - https://learnopencv.com/high-dynamic-range-hdr-imaging-using-opencv-cpp-python/
    - https://docs.opencv.org/3.4/d2/df0/tutorial_py_hdr.html

    ## Misc: Notes on HDR Gain Map and HEIF-Pillow

    HDR Gain map
    - https://github.com/m13253/heif-hdrgainmap-decode/blob/master/heif-hdrgainmap-decode-y4m.py
    - https://developer.apple.com/documentation/appkit/images_and_pdf/applying_apple_hdr_effect_to_your_photos
    - https://en.wikipedia.org/wiki/Transfer_functions_in_imaging

    Pillow-HEIF
    - https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/examples/heif_dec.cc#L604
    - Does `heif_image_handle_get_preferred_decoding_colorspace` or `heif_image_handle_get_nclx_color_profile` work on a handle of an auxiliary image? ([relevant source](https://github.com/strukturag/libheif/blob/88568e07a55627420338bee9685470da4dda34b9/libheif/api/libheif/heif.h#L1181-L1188))
    - https://pillow-heif.readthedocs.io/en/latest/installation.html ([windows requirements](https://stackoverflow.com/questions/40018405/cannot-open-include-file-io-h-no-such-file-or-directory#50210015))
  6. johncf revised this gist Oct 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ Sources:
    - https://wiki.x266.mov/docs/colorimetry/transfer#14-bt2020-10-bit
    - https://gist.github.com/kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a?permalink_comment_id=4754146#gistcomment-4754146

    ### Misc: HDR-merging and Tone-mapping using OpenCV
    ## Misc: HDR-merging and Tone-mapping using OpenCV

    https://learnopencv.com/high-dynamic-range-hdr-imaging-using-opencv-cpp-python/
    https://docs.opencv.org/3.4/d2/df0/tutorial_py_hdr.html
  7. johncf revised this gist Oct 6, 2024. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -82,3 +82,8 @@ Sources:
    - https://trac.ffmpeg.org/wiki/colorspace
    - https://wiki.x266.mov/docs/colorimetry/transfer#14-bt2020-10-bit
    - https://gist.github.com/kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a?permalink_comment_id=4754146#gistcomment-4754146

    ### Misc: HDR-merging and Tone-mapping using OpenCV

    https://learnopencv.com/high-dynamic-range-hdr-imaging-using-opencv-cpp-python/
    https://docs.opencv.org/3.4/d2/df0/tutorial_py_hdr.html
  8. johncf revised this gist Oct 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ Generate a look-up table (LUT) with [hlg-tools](https://github.com/wswartzendrub
    hlg-tools/pq2hlg -r 203 -s 128 pq2hlg.cube
    ```

    Note: there are three "popular" reference white points in use: 100 nits, 203 nits, and 300 nits. But 203 nits seems to be becoming the standard in photography.
    Note: popular reference white points in use are: 80 nits, 100 nits, 203 nits, and 300 nits. But 203 nits seems to be becoming the standard in photography.

    Map the pixel values with FFMPEG using the above generated LUT (`pq2hlg.cube`):

  9. johncf revised this gist Oct 6, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -33,10 +33,10 @@ Notes:

    ## PQ to HLG conversion

    Generate a look-up table (LUT) with [hlg-tools](https://github-wiki-see.page/m/wswartzendruber/hlg-tools) using a reference white-point:
    Generate a look-up table (LUT) with [hlg-tools](https://github.com/wswartzendruber/hlg-tools) using a reference white-point:

    ```
    hlg-tools/pq2hlg -r 203 pq2hlg.cube
    hlg-tools/pq2hlg -r 203 -s 128 pq2hlg.cube
    ```

    Note: there are three "popular" reference white points in use: 100 nits, 203 nits, and 300 nits. But 203 nits seems to be becoming the standard in photography.
  10. johncf revised this gist Oct 6, 2024. 1 changed file with 28 additions and 11 deletions.
    39 changes: 28 additions & 11 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -33,26 +33,42 @@ Notes:

    ## PQ to HLG conversion

    Generate a look-up table (LUT) with [hlg-tools](https://github-wiki-see.page/m/wswartzendruber/hlg-tools) using a reference white-point:

    ```
    hlg-tools/pq2hlg -r 203 pq2hlg.cube
    ```

    Note: there are three "popular" reference white points in use: 100 nits, 203 nits, and 300 nits. But 203 nits seems to be becoming the standard in photography.

    Map the pixel values with FFMPEG using the above generated LUT (`pq2hlg.cube`):

    ```
    # source https://github-wiki-see.page/m/wswartzendruber/hlg-tools
    hlg-tools/pq2hlg -r 210 -m 4000 pq2hlg.cube
    # color-map using the above LUT
    ffmpeg -i HDR_sample_pq.png -vf format=rgb48le,lut3d=pq2hlg.cube -color_primaries bt2020 -color_trc arib-std-b67 -colorspace bt2020nc HDR_sample_hlg.png
    # the above PNG will have bad metadata (ffmpeg issue?), so viewer apps will render it badly by default
    # convert to avif with the correct transfer function (HLG: 18)
    ```

    Note: `HDR_sample_hlg.png` will have bad metadata even though we specified the correct transfer function `arib-std-b67` (ffmpeg issue?), so viewer apps will render it badly.

    Convert to avif with the correct transfer function (the `18` in `9/18/9` indicates HLG):

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 HDR_sample_hlg.png HDR_sample_hlg.avif
    # copy EXIF tags from the original DNG file
    exiftool -TagsFromFile IMG_0025.DNG "-all:all>all:all" HDR_sample_hlg.avif
    ```

    Copy EXIF tags from the original DNG file:

    ```
    exiftool -TagsFromFile RAW_sample.DNG "-all:all>all:all" HDR_sample_hlg.avif
    ```

    ## PQ optimized

    Opening the AVIF file converted from the direct PNG (PQ or HLG) export from Affinity Photo 2 in iPhone is slower than doing it after these steps. The only explanation I could come up with is the removal of alpha channel perhaps... :thinking:
    Opening the AVIF file converted from the HDR PNG (PQ or HLG) export from Affinity Photo 2 in iPhone is slower than doing it after these steps. The only explanation I could come up with is the removal of alpha channel perhaps... :thinking:

    ```
    ffmpeg -i .\IMG_0025-0027_pq.png -vf format=rgb48le -color_primaries bt2020 -color_trc smpte2084 -colorspace bt2020nc IMG_0025-0027_pq2.png
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/16/9 IMG_0025-0027_pq2.png IMG_0025-0027_pq2.avif
    exiftool -TagsFromFile IMG_0025.DNG "-all:all>all:all" IMG_0025-0027_pq2.avif
    ffmpeg -i IMG_0025_pq.png -vf format=rgb48le -color_primaries bt2020 -color_trc smpte2084 -colorspace bt2020nc IMG_0025_pq2.png
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/16/9 IMG_0025_pq2.png IMG_0025_pq2.avif
    exiftool -TagsFromFile IMG_0025.DNG "-all:all>all:all" IMG_0025_pq2.avif
    ```

    Sources:
    @@ -65,3 +81,4 @@ Sources:
    - https://github-wiki-see.page/m/wswartzendruber/hlg-tools/wiki/pq2hlg
    - https://trac.ffmpeg.org/wiki/colorspace
    - https://wiki.x266.mov/docs/colorimetry/transfer#14-bt2020-10-bit
    - https://gist.github.com/kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a?permalink_comment_id=4754146#gistcomment-4754146
  11. johncf revised this gist Oct 1, 2024. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -30,10 +30,38 @@ Notes:
    - Setting `deltaq-mode` to `5` (HDR) doesn't seem to make a difference at all, `3` produces slightly smaller files for SDR images.
    - If `--cicp` is ignored and the source image doesn't specify any profile, `2/2/6` is used where `2` means unspecified. This is okay for sRGB.


    ## PQ to HLG conversion

    ```
    # source https://github-wiki-see.page/m/wswartzendruber/hlg-tools
    hlg-tools/pq2hlg -r 210 -m 4000 pq2hlg.cube
    # color-map using the above LUT
    ffmpeg -i HDR_sample_pq.png -vf format=rgb48le,lut3d=pq2hlg.cube -color_primaries bt2020 -color_trc arib-std-b67 -colorspace bt2020nc HDR_sample_hlg.png
    # the above PNG will have bad metadata (ffmpeg issue?), so viewer apps will render it badly by default
    # convert to avif with the correct transfer function (HLG: 18)
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 HDR_sample_hlg.png HDR_sample_hlg.avif
    # copy EXIF tags from the original DNG file
    exiftool -TagsFromFile IMG_0025.DNG "-all:all>all:all" HDR_sample_hlg.avif
    ```

    ## PQ optimized

    Opening the AVIF file converted from the direct PNG (PQ or HLG) export from Affinity Photo 2 in iPhone is slower than doing it after these steps. The only explanation I could come up with is the removal of alpha channel perhaps... :thinking:

    ```
    ffmpeg -i .\IMG_0025-0027_pq.png -vf format=rgb48le -color_primaries bt2020 -color_trc smpte2084 -colorspace bt2020nc IMG_0025-0027_pq2.png
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/16/9 IMG_0025-0027_pq2.png IMG_0025-0027_pq2.avif
    exiftool -TagsFromFile IMG_0025.DNG "-all:all>all:all" IMG_0025-0027_pq2.avif
    ```

    Sources:
    - https://github.com/AOMediaCodec/libavif/wiki/CICP
    - https://en.wikipedia.org/wiki/Coding-independent_code_points
    - https://old.reddit.com/r/AV1/comments/o7s8hk/high_quality_encoding_of_avif_images_using/
    - https://wiki.x266.mov/docs/encoders/aomenc#avif-encoding
    - (av1 video) https://www.reddit.com/r/AV1/comments/t59j32/encoder_tuning_part_4_a_2nd_generation_guide_to/
    - (av1 video) https://gist.github.com/nico-lab/a6508f283a77696e79a131e07abd86b9
    - https://github-wiki-see.page/m/wswartzendruber/hlg-tools/wiki/pq2hlg
    - https://trac.ffmpeg.org/wiki/colorspace
    - https://wiki.x266.mov/docs/colorimetry/transfer#14-bt2020-10-bit
  12. johncf revised this gist Sep 11, 2024. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,33 @@
    To convert an HDR PNG that's exported with HLG transfer function in Rec. 2020 color space, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 IMG_0186.png IMG_0186.avif
    ```

    For HDR with PQ transfer function in Rec. 2020 color space, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/16/9 IMG_0186.png IMG_0186.avif
    ```

    For the default SDR image (sRGB with BT.601 coefficients), simply do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=3 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 --cicp 1/13/6 IMG_0186.png IMG_0186.avif
    ```

    For the same with BT.709 coefficients, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=3 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 --cicp 1/13/1 IMG_0186.png IMG_0186.avif
    ```

    Notes:
    - `-a color:deltaq-mode=1` might be redundant (or ignored)
    - Setting `deltaq-mode` to `5` (HDR) doesn't seem to make a difference at all, `3` produces slightly smaller files for SDR images.
    - If `--cicp` is ignored and the source image doesn't specify any profile, `2/2/6` is used where `2` means unspecified. This is okay for sRGB.

    Sources:
  13. johncf revised this gist Sep 11, 2024. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -26,6 +26,10 @@ avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 --cicp 1/13/1 IMG_0186.png IMG_0186.avif
    ```

    Notes:
    - `-a color:deltaq-mode=1` might be redundant (or ignored)
    - If `--cicp` is ignored and the source image doesn't specify any profile, `2/2/6` is used where `2` means unspecified. This is okay for sRGB.

    Sources:
    - https://github.com/AOMediaCodec/libavif/wiki/CICP
    - https://en.wikipedia.org/wiki/Coding-independent_code_points
  14. johncf revised this gist Sep 11, 2024. 1 changed file with 32 additions and 2 deletions.
    34 changes: 32 additions & 2 deletions avifenc-hdr.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,35 @@
    To convert an HDR PNG that's exported with HLG transfer function in Rec. 2020 color space, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 IMG_0186.png IMG_0186.avif
    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 IMG_0186.png IMG_0186.avif
    ```

    For HDR with PQ transfer function in Rec. 2020 color space, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/16/9 IMG_0186.png IMG_0186.avif
    ```

    For the default SDR image (sRGB with BT.601 coefficients), simply do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 --cicp 1/13/6 IMG_0186.png IMG_0186.avif
    ```

    For the same with BT.709 coefficients, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim \
    -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 --cicp 1/13/1 IMG_0186.png IMG_0186.avif
    ```

    Sources:
    - https://github.com/AOMediaCodec/libavif/wiki/CICP
    - https://en.wikipedia.org/wiki/Coding-independent_code_points
    - https://old.reddit.com/r/AV1/comments/o7s8hk/high_quality_encoding_of_avif_images_using/
    - https://wiki.x266.mov/docs/encoders/aomenc#avif-encoding
    - (av1 video) https://www.reddit.com/r/AV1/comments/t59j32/encoder_tuning_part_4_a_2nd_generation_guide_to/
    - (av1 video) https://gist.github.com/nico-lab/a6508f283a77696e79a131e07abd86b9
  15. johncf renamed this gist Sep 10, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. johncf created this gist Sep 10, 2024.
    5 changes: 5 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    To convert an HDR PNG that's exported with HLG transfer function in Rec. 2020 color space, do:

    ```
    avifenc -s 4 -j 4 --min 1 --max 56 -a end-usage=q -a cq-level=18 -a color:deltaq-mode=1 -a tune=ssim -a color:enable-chroma-deltaq=1 -a color:enable-qm=1 -d 12 --cicp 9/18/9 IMG_0186.png IMG_0186.avif
    ```