You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
12KB

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