vf_tonemap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2017 Vittorio Giovara <vittorio.giovara@gmail.com>
  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. /**
  21. * @file
  22. * tonemap algorithms
  23. */
  24. #include <float.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avfilter.h"
  33. #include "colorspace.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. enum TonemapAlgorithm {
  38. TONEMAP_NONE,
  39. TONEMAP_LINEAR,
  40. TONEMAP_GAMMA,
  41. TONEMAP_CLIP,
  42. TONEMAP_REINHARD,
  43. TONEMAP_HABLE,
  44. TONEMAP_MOBIUS,
  45. TONEMAP_MAX,
  46. };
  47. static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
  48. [AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 },
  49. [AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 },
  50. [AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 },
  51. [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
  52. [AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 },
  53. [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
  54. [AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 },
  55. };
  56. typedef struct TonemapContext {
  57. const AVClass *class;
  58. enum TonemapAlgorithm tonemap;
  59. double param;
  60. double desat;
  61. double peak;
  62. const struct LumaCoefficients *coeffs;
  63. } TonemapContext;
  64. static const enum AVPixelFormat pix_fmts[] = {
  65. AV_PIX_FMT_GBRPF32,
  66. AV_PIX_FMT_GBRAPF32,
  67. AV_PIX_FMT_NONE,
  68. };
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  72. }
  73. static av_cold int init(AVFilterContext *ctx)
  74. {
  75. TonemapContext *s = ctx->priv;
  76. switch(s->tonemap) {
  77. case TONEMAP_GAMMA:
  78. if (isnan(s->param))
  79. s->param = 1.8f;
  80. break;
  81. case TONEMAP_REINHARD:
  82. if (!isnan(s->param))
  83. s->param = (1.0f - s->param) / s->param;
  84. break;
  85. case TONEMAP_MOBIUS:
  86. if (isnan(s->param))
  87. s->param = 0.3f;
  88. break;
  89. }
  90. if (isnan(s->param))
  91. s->param = 1.0f;
  92. return 0;
  93. }
  94. static float hable(float in)
  95. {
  96. float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;
  97. return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
  98. }
  99. static float mobius(float in, float j, double peak)
  100. {
  101. float a, b;
  102. if (in <= j)
  103. return in;
  104. a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);
  105. b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6);
  106. return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
  107. }
  108. #define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
  109. static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
  110. const AVPixFmtDescriptor *desc, int x, int y, double peak)
  111. {
  112. const float *r_in = (const float *)(in->data[0] + x * desc->comp[0].step + y * in->linesize[0]);
  113. const float *b_in = (const float *)(in->data[1] + x * desc->comp[1].step + y * in->linesize[1]);
  114. const float *g_in = (const float *)(in->data[2] + x * desc->comp[2].step + y * in->linesize[2]);
  115. float *r_out = (float *)(out->data[0] + x * desc->comp[0].step + y * out->linesize[0]);
  116. float *b_out = (float *)(out->data[1] + x * desc->comp[1].step + y * out->linesize[1]);
  117. float *g_out = (float *)(out->data[2] + x * desc->comp[2].step + y * out->linesize[2]);
  118. float sig, sig_orig;
  119. /* load values */
  120. *r_out = *r_in;
  121. *b_out = *b_in;
  122. *g_out = *g_in;
  123. /* desaturate to prevent unnatural colors */
  124. if (s->desat > 0) {
  125. float luma = s->coeffs->cr * *r_in + s->coeffs->cg * *g_in + s->coeffs->cb * *b_in;
  126. float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6);
  127. *r_out = MIX(*r_in, luma, overbright);
  128. *g_out = MIX(*g_in, luma, overbright);
  129. *b_out = MIX(*b_in, luma, overbright);
  130. }
  131. /* pick the brightest component, reducing the value range as necessary
  132. * to keep the entire signal in range and preventing discoloration due to
  133. * out-of-bounds clipping */
  134. sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6);
  135. sig_orig = sig;
  136. switch(s->tonemap) {
  137. default:
  138. case TONEMAP_NONE:
  139. // do nothing
  140. break;
  141. case TONEMAP_LINEAR:
  142. sig = sig * s->param / peak;
  143. break;
  144. case TONEMAP_GAMMA:
  145. sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param)
  146. : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f;
  147. break;
  148. case TONEMAP_CLIP:
  149. sig = av_clipf(sig * s->param, 0, 1.0f);
  150. break;
  151. case TONEMAP_HABLE:
  152. sig = hable(sig) / hable(peak);
  153. break;
  154. case TONEMAP_REINHARD:
  155. sig = sig / (sig + s->param) * (peak + s->param) / peak;
  156. break;
  157. case TONEMAP_MOBIUS:
  158. sig = mobius(sig, s->param, peak);
  159. break;
  160. }
  161. /* apply the computed scale factor to the color,
  162. * linearly to prevent discoloration */
  163. *r_out *= sig / sig_orig;
  164. *g_out *= sig / sig_orig;
  165. *b_out *= sig / sig_orig;
  166. }
  167. typedef struct ThreadData {
  168. AVFrame *in, *out;
  169. const AVPixFmtDescriptor *desc;
  170. double peak;
  171. } ThreadData;
  172. static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  173. {
  174. TonemapContext *s = ctx->priv;
  175. ThreadData *td = arg;
  176. AVFrame *in = td->in;
  177. AVFrame *out = td->out;
  178. const AVPixFmtDescriptor *desc = td->desc;
  179. const int slice_start = (in->height * jobnr) / nb_jobs;
  180. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  181. double peak = td->peak;
  182. for (int y = slice_start; y < slice_end; y++)
  183. for (int x = 0; x < out->width; x++)
  184. tonemap(s, out, in, desc, x, y, peak);
  185. return 0;
  186. }
  187. static int filter_frame(AVFilterLink *link, AVFrame *in)
  188. {
  189. AVFilterContext *ctx = link->dst;
  190. TonemapContext *s = ctx->priv;
  191. AVFilterLink *outlink = ctx->outputs[0];
  192. ThreadData td;
  193. AVFrame *out;
  194. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  195. const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
  196. int ret, x, y;
  197. double peak = s->peak;
  198. if (!desc || !odesc) {
  199. av_frame_free(&in);
  200. return AVERROR_BUG;
  201. }
  202. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  203. if (!out) {
  204. av_frame_free(&in);
  205. return AVERROR(ENOMEM);
  206. }
  207. ret = av_frame_copy_props(out, in);
  208. if (ret < 0) {
  209. av_frame_free(&in);
  210. av_frame_free(&out);
  211. return ret;
  212. }
  213. /* input and output transfer will be linear */
  214. if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
  215. av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n");
  216. out->color_trc = AVCOL_TRC_LINEAR;
  217. } else if (in->color_trc != AVCOL_TRC_LINEAR)
  218. av_log(s, AV_LOG_WARNING, "Tonemapping works on linear light only\n");
  219. /* read peak from side data if not passed in */
  220. if (!peak) {
  221. peak = ff_determine_signal_peak(in);
  222. av_log(s, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak);
  223. }
  224. /* load original color space even if pixel format is RGB to compute overbrights */
  225. s->coeffs = &luma_coefficients[in->colorspace];
  226. if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) {
  227. if (in->colorspace == AVCOL_SPC_UNSPECIFIED)
  228. av_log(s, AV_LOG_WARNING, "Missing color space information, ");
  229. else if (!s->coeffs)
  230. av_log(s, AV_LOG_WARNING, "Unsupported color space '%s', ",
  231. av_color_space_name(in->colorspace));
  232. av_log(s, AV_LOG_WARNING, "desaturation is disabled\n");
  233. s->desat = 0;
  234. }
  235. /* do the tone map */
  236. td.out = out;
  237. td.in = in;
  238. td.desc = desc;
  239. td.peak = peak;
  240. ctx->internal->execute(ctx, tonemap_slice, &td, NULL, FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
  241. /* copy/generate alpha if needed */
  242. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
  243. av_image_copy_plane(out->data[3], out->linesize[3],
  244. in->data[3], in->linesize[3],
  245. out->linesize[3], outlink->h);
  246. } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
  247. for (y = 0; y < out->height; y++) {
  248. for (x = 0; x < out->width; x++) {
  249. AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
  250. av_float2int(1.0f));
  251. }
  252. }
  253. }
  254. av_frame_free(&in);
  255. ff_update_hdr_metadata(out, peak);
  256. return ff_filter_frame(outlink, out);
  257. }
  258. #define OFFSET(x) offsetof(TonemapContext, x)
  259. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  260. static const AVOption tonemap_options[] = {
  261. { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, "tonemap" },
  262. { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, "tonemap" },
  263. { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, "tonemap" },
  264. { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, "tonemap" },
  265. { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, "tonemap" },
  266. { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, "tonemap" },
  267. { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, "tonemap" },
  268. { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, "tonemap" },
  269. { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
  270. { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
  271. { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
  272. { NULL }
  273. };
  274. AVFILTER_DEFINE_CLASS(tonemap);
  275. static const AVFilterPad tonemap_inputs[] = {
  276. {
  277. .name = "default",
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .filter_frame = filter_frame,
  280. },
  281. { NULL }
  282. };
  283. static const AVFilterPad tonemap_outputs[] = {
  284. {
  285. .name = "default",
  286. .type = AVMEDIA_TYPE_VIDEO,
  287. },
  288. { NULL }
  289. };
  290. AVFilter ff_vf_tonemap = {
  291. .name = "tonemap",
  292. .description = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."),
  293. .init = init,
  294. .query_formats = query_formats,
  295. .priv_size = sizeof(TonemapContext),
  296. .priv_class = &tonemap_class,
  297. .inputs = tonemap_inputs,
  298. .outputs = tonemap_outputs,
  299. .flags = AVFILTER_FLAG_SLICE_THREADS,
  300. };