lut3d.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2024 Niklas Haas
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef SWSCALE_LUT3D_H
  21. #define SWSCALE_LUT3D_H
  22. #include <stdint.h>
  23. #include "libavutil/csp.h"
  24. #include "libavutil/pixfmt.h"
  25. #include "cms.h"
  26. #include "csputils.h"
  27. #include "utils.h"
  28. enum {
  29. /* Input LUT size. This is only calculated once. */
  30. INPUT_LUT_BITS = 6,
  31. INPUT_LUT_SIZE = (1 << INPUT_LUT_BITS) + 1, /* +1 to simplify interpolation */
  32. /* Tone mapping LUT size. This is regenerated possibly per frame. */
  33. TONE_LUT_BITS = 8,
  34. TONE_LUT_SIZE = (1 << TONE_LUT_BITS) + 1,
  35. /* Output LUT size (for dynamic tone mapping). This is only calculated once. */
  36. OUTPUT_LUT_BITS_I = 6,
  37. OUTPUT_LUT_BITS_PT = 7,
  38. OUTPUT_LUT_SIZE_I = (1 << OUTPUT_LUT_BITS_I) + 1,
  39. OUTPUT_LUT_SIZE_PT = (1 << OUTPUT_LUT_BITS_PT) + 1,
  40. };
  41. typedef struct SwsLut3D {
  42. SwsColorMap map;
  43. bool dynamic;
  44. /* Gamut mapping 3DLUT(s) */
  45. v3u16_t input[INPUT_LUT_SIZE][INPUT_LUT_SIZE][INPUT_LUT_SIZE];
  46. v3u16_t output[OUTPUT_LUT_SIZE_PT][OUTPUT_LUT_SIZE_PT][OUTPUT_LUT_SIZE_I];
  47. /* Split tone mapping LUT (for dynamic tone mapping) */
  48. v2u16_t tone_map[TONE_LUT_SIZE]; /* new luma, desaturation */
  49. } SwsLut3D;
  50. SwsLut3D *ff_sws_lut3d_alloc(void);
  51. void ff_sws_lut3d_free(SwsLut3D **lut3d);
  52. /**
  53. * Test to see if a given format is supported by the 3DLUT input/output code.
  54. */
  55. bool ff_sws_lut3d_test_fmt(enum AVPixelFormat fmt, int output);
  56. /**
  57. * Pick the best compatible pixfmt for a given SwsFormat.
  58. */
  59. enum AVPixelFormat ff_sws_lut3d_pick_pixfmt(SwsFormat fmt, int output);
  60. /**
  61. * Recalculate the (static) 3DLUT state with new settings. This will recompute
  62. * everything. To only update per-frame tone mapping state, instead call
  63. * ff_sws_lut3d_update().
  64. *
  65. * Returns 0 or a negative error code.
  66. */
  67. int ff_sws_lut3d_generate(SwsLut3D *lut3d, enum AVPixelFormat fmt_in,
  68. enum AVPixelFormat fmt_out, const SwsColorMap *map);
  69. /**
  70. * Update the tone mapping state. This will only use per-frame metadata. The
  71. * static metadata is ignored.
  72. */
  73. void ff_sws_lut3d_update(SwsLut3D *lut3d, const SwsColor *new_src);
  74. /**
  75. * Applies a color transformation to a plane. The format must match the format
  76. * provided during ff_sws_lut3d_update().
  77. */
  78. void ff_sws_lut3d_apply(const SwsLut3D *lut3d, const uint8_t *in, int in_stride,
  79. uint8_t *out, int out_stride, int w, int h);
  80. #endif /* SWSCALE_LUT3D_H */