utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_UTILS_H
  21. #define SWSCALE_UTILS_H
  22. #include "libavutil/csp.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "swscale.h"
  25. static inline int ff_q_isnan(const AVRational a)
  26. {
  27. return !a.num && !a.den;
  28. }
  29. /* Like av_cmp_q but considers NaN == NaN */
  30. static inline int ff_q_equal(const AVRational a, const AVRational b)
  31. {
  32. return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
  33. }
  34. static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
  35. {
  36. return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
  37. }
  38. static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
  39. const AVPrimaryCoefficients *b)
  40. {
  41. return ff_cie_xy_equal(a->r, b->r) &&
  42. ff_cie_xy_equal(a->g, b->g) &&
  43. ff_cie_xy_equal(a->b, b->b);
  44. }
  45. enum {
  46. FIELD_TOP, /* top/even rows, or progressive */
  47. FIELD_BOTTOM, /* bottom/odd rows */
  48. };
  49. typedef struct SwsColor {
  50. enum AVColorPrimaries prim;
  51. enum AVColorTransferCharacteristic trc;
  52. AVPrimaryCoefficients gamut; /* mastering display gamut */
  53. AVRational min_luma; /* minimum luminance in nits */
  54. AVRational max_luma; /* maximum luminance in nits */
  55. AVRational frame_peak; /* per-frame/scene peak luminance, or 0 */
  56. AVRational frame_avg; /* per-frame/scene average luminance, or 0 */
  57. } SwsColor;
  58. static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
  59. {
  60. dst->frame_peak = src->frame_peak;
  61. dst->frame_avg = src->frame_avg;
  62. }
  63. /* Subset of AVFrame parameters that uniquely determine pixel representation */
  64. typedef struct SwsFormat {
  65. int width, height;
  66. int interlaced;
  67. enum AVPixelFormat format;
  68. enum AVColorRange range;
  69. enum AVColorSpace csp;
  70. enum AVChromaLocation loc;
  71. const AVPixFmtDescriptor *desc; /* convenience */
  72. SwsColor color;
  73. } SwsFormat;
  74. /**
  75. * This function also sanitizes and strips the input data, removing irrelevant
  76. * fields for certain formats.
  77. */
  78. SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field);
  79. static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
  80. {
  81. return c1->prim == c2->prim &&
  82. c1->trc == c2->trc &&
  83. ff_q_equal(c1->min_luma, c2->min_luma) &&
  84. ff_q_equal(c1->max_luma, c2->max_luma) &&
  85. ff_prim_equal(&c1->gamut, &c2->gamut);
  86. }
  87. /* Tests only the static components of a colorspace, ignoring dimensions and per-frame data */
  88. static inline int ff_props_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
  89. {
  90. return fmt1->interlaced == fmt2->interlaced &&
  91. fmt1->format == fmt2->format &&
  92. fmt1->range == fmt2->range &&
  93. fmt1->csp == fmt2->csp &&
  94. fmt1->loc == fmt2->loc &&
  95. ff_color_equal(&fmt1->color, &fmt2->color);
  96. }
  97. /* Tests only the static components of a colorspace, ignoring per-frame data */
  98. static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
  99. {
  100. return fmt1->width == fmt2->width &&
  101. fmt1->height == fmt2->height &&
  102. ff_props_equal(fmt1, fmt2);
  103. }
  104. static inline int ff_fmt_align(enum AVPixelFormat fmt)
  105. {
  106. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  107. if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
  108. return 2;
  109. } else {
  110. return 1 << desc->log2_chroma_h;
  111. }
  112. }
  113. int ff_test_fmt(const SwsFormat *fmt, int output);
  114. /* Returns 1 if the formats are incomplete, 0 otherwise */
  115. int ff_infer_colors(SwsColor *src, SwsColor *dst);
  116. #endif /* SWSCALE_UTILS_H */