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.

400 lines
12KB

  1. /*
  2. * Copyright (c) 2003-2013 Loren Merritt
  3. * Copyright (c) 2015 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /* Computes the Structural Similarity Metric between two video streams.
  22. * original algorithm:
  23. * Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
  24. * "Image quality assessment: From error visibility to structural similarity,"
  25. * IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
  26. *
  27. * To improve speed, this implementation uses the standard approximation of
  28. * overlapped 8x8 block sums, rather than the original gaussian weights.
  29. */
  30. /*
  31. * @file
  32. * Caculate the SSIM between two input videos.
  33. */
  34. #include "libavutil/opt.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "avfilter.h"
  37. #include "dualinput.h"
  38. #include "drawutils.h"
  39. #include "formats.h"
  40. #include "internal.h"
  41. #include "video.h"
  42. typedef struct SSIMContext {
  43. const AVClass *class;
  44. FFDualInputContext dinput;
  45. FILE *stats_file;
  46. char *stats_file_str;
  47. int nb_components;
  48. uint64_t nb_frames;
  49. double ssim[4];
  50. char comps[4];
  51. int *coefs;
  52. uint8_t rgba_map[4];
  53. int planewidth[4];
  54. int planeheight[4];
  55. int *temp;
  56. } SSIMContext;
  57. #define OFFSET(x) offsetof(SSIMContext, x)
  58. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  59. static const AVOption ssim_options[] = {
  60. {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  61. {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  62. { NULL }
  63. };
  64. AVFILTER_DEFINE_CLASS(ssim);
  65. static int rgb_coefs[4] = { 1, 1, 1, 3};
  66. static int yuv_coefs[4] = { 4, 1, 1, 6};
  67. static int gray_coefs[4] = { 1, 0, 0, 1};
  68. static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
  69. {
  70. char value[128];
  71. snprintf(value, sizeof(value), "%0.2f", d);
  72. if (comp) {
  73. char key2[128];
  74. snprintf(key2, sizeof(key2), "%s%c", key, comp);
  75. av_dict_set(metadata, key2, value, 0);
  76. } else {
  77. av_dict_set(metadata, key, value, 0);
  78. }
  79. }
  80. static void ssim_4x4x2_core(const uint8_t *main, int main_stride,
  81. const uint8_t *ref, int ref_stride,
  82. int sums[2][4])
  83. {
  84. int x, y, z;
  85. for (z = 0; z < 2; z++) {
  86. uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
  87. for (y = 0; y < 4; y++) {
  88. for (x = 0; x < 4; x++) {
  89. int a = main[x + y * main_stride];
  90. int b = ref[x + y * ref_stride];
  91. s1 += a;
  92. s2 += b;
  93. ss += a*a;
  94. ss += b*b;
  95. s12 += a*b;
  96. }
  97. }
  98. sums[z][0] = s1;
  99. sums[z][1] = s2;
  100. sums[z][2] = ss;
  101. sums[z][3] = s12;
  102. main += 4;
  103. ref += 4;
  104. }
  105. }
  106. static float ssim_end1(int s1, int s2, int ss, int s12)
  107. {
  108. static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
  109. static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
  110. int fs1 = s1;
  111. int fs2 = s2;
  112. int fss = ss;
  113. int fs12 = s12;
  114. int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
  115. int covar = fs12 * 64 - fs1 * fs2;
  116. return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
  117. / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
  118. }
  119. static float ssim_end4(int sum0[5][4], int sum1[5][4], int width)
  120. {
  121. float ssim = 0.0;
  122. int i;
  123. for (i = 0; i < width; i++)
  124. ssim += ssim_end1(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
  125. sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
  126. sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
  127. sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3]);
  128. return ssim;
  129. }
  130. static float ssim_plane(uint8_t *main, int main_stride,
  131. uint8_t *ref, int ref_stride,
  132. int width, int height, void *temp)
  133. {
  134. int z = 0;
  135. int x, y;
  136. float ssim = 0.0;
  137. int (*sum0)[4] = temp;
  138. int (*sum1)[4] = sum0 + (width >> 2) + 3;
  139. width >>= 2;
  140. height >>= 2;
  141. for (y = 1; y < height; y++) {
  142. for (; z <= y; z++) {
  143. FFSWAP(void*, sum0, sum1);
  144. for (x = 0; x < width; x+=2)
  145. ssim_4x4x2_core(&main[4 * (x + z * main_stride)], main_stride,
  146. &ref[4 * (x + z * ref_stride)], ref_stride,
  147. &sum0[x]);
  148. }
  149. for (x = 0; x < width - 1; x += 4)
  150. ssim += ssim_end4(sum0 + x, sum1 + x, FFMIN(4, width - x - 1));
  151. }
  152. return ssim / ((height - 1) * (width - 1));
  153. }
  154. static double ssim_db(double ssim, double weight)
  155. {
  156. return 10 * (log(weight) / log(10) - log(weight - ssim) / log(10));
  157. }
  158. static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main,
  159. const AVFrame *ref)
  160. {
  161. AVDictionary **metadata = avpriv_frame_get_metadatap(main);
  162. SSIMContext *s = ctx->priv;
  163. float c[4], ssimv;
  164. int i;
  165. s->nb_frames++;
  166. for (i = 0; i < s->nb_components; i++)
  167. c[i] = ssim_plane(main->data[i], main->linesize[i],
  168. ref->data[i], ref->linesize[i],
  169. s->planewidth[i], s->planeheight[i], s->temp);
  170. ssimv = (c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2]) / s->coefs[3];
  171. for (i = 0; i < s->nb_components; i++)
  172. set_meta(metadata, "lavfi.ssim.", s->comps[i], c[i]);
  173. set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
  174. set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2], s->coefs[3]));
  175. if (s->stats_file) {
  176. fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames);
  177. for (i = 0; i < s->nb_components; i++)
  178. fprintf(s->stats_file, "%c:%f ", s->comps[i], c[i]);
  179. fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(c[0] * s->coefs[0] + c[1] * s->coefs[1] + c[2] * s->coefs[2], s->coefs[3]));
  180. }
  181. s->ssim[0] += c[0];
  182. s->ssim[1] += c[1];
  183. s->ssim[2] += c[2];
  184. return main;
  185. }
  186. static av_cold int init(AVFilterContext *ctx)
  187. {
  188. SSIMContext *s = ctx->priv;
  189. if (s->stats_file_str) {
  190. s->stats_file = fopen(s->stats_file_str, "w");
  191. if (!s->stats_file) {
  192. int err = AVERROR(errno);
  193. char buf[128];
  194. av_strerror(err, buf, sizeof(buf));
  195. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  196. s->stats_file_str, buf);
  197. return err;
  198. }
  199. }
  200. s->dinput.process = do_ssim;
  201. s->dinput.shortest = 1;
  202. s->dinput.repeatlast = 0;
  203. return 0;
  204. }
  205. static int query_formats(AVFilterContext *ctx)
  206. {
  207. static const enum AVPixelFormat pix_fmts[] = {
  208. AV_PIX_FMT_GRAY8,
  209. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  210. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  211. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  212. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  213. AV_PIX_FMT_GBRP,
  214. AV_PIX_FMT_NONE
  215. };
  216. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  217. if (!fmts_list)
  218. return AVERROR(ENOMEM);
  219. return ff_set_common_formats(ctx, fmts_list);
  220. }
  221. static int config_input_ref(AVFilterLink *inlink)
  222. {
  223. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  224. AVFilterContext *ctx = inlink->dst;
  225. SSIMContext *s = ctx->priv;
  226. int is_rgb;
  227. s->nb_components = desc->nb_components;
  228. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  229. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  230. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  231. return AVERROR(EINVAL);
  232. }
  233. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  234. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  235. return AVERROR(EINVAL);
  236. }
  237. is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  238. s->comps[0] = is_rgb ? 'R' : 'Y';
  239. s->comps[1] = is_rgb ? 'G' : 'U';
  240. s->comps[2] = is_rgb ? 'B' : 'V';
  241. s->comps[3] = 'A';
  242. if (is_rgb) {
  243. s->coefs = rgb_coefs;
  244. } else if (s->nb_components == 1) {
  245. s->coefs = gray_coefs;
  246. } else {
  247. s->coefs = yuv_coefs;
  248. }
  249. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  250. s->planeheight[0] = s->planeheight[3] = inlink->h;
  251. s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  252. s->planewidth[0] = s->planewidth[3] = inlink->w;
  253. s->temp = av_malloc((2 * inlink->w + 12) * sizeof(*s->temp));
  254. if (!s->temp)
  255. return AVERROR(ENOMEM);
  256. return 0;
  257. }
  258. static int config_output(AVFilterLink *outlink)
  259. {
  260. AVFilterContext *ctx = outlink->src;
  261. SSIMContext *s = ctx->priv;
  262. AVFilterLink *mainlink = ctx->inputs[0];
  263. int ret;
  264. outlink->w = mainlink->w;
  265. outlink->h = mainlink->h;
  266. outlink->time_base = mainlink->time_base;
  267. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  268. outlink->frame_rate = mainlink->frame_rate;
  269. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  270. return ret;
  271. return 0;
  272. }
  273. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  274. {
  275. SSIMContext *s = inlink->dst->priv;
  276. return ff_dualinput_filter_frame(&s->dinput, inlink, buf);
  277. }
  278. static int request_frame(AVFilterLink *outlink)
  279. {
  280. SSIMContext *s = outlink->src->priv;
  281. return ff_dualinput_request_frame(&s->dinput, outlink);
  282. }
  283. static av_cold void uninit(AVFilterContext *ctx)
  284. {
  285. SSIMContext *s = ctx->priv;
  286. if (s->nb_frames > 0) {
  287. if (s->nb_components == 3) {
  288. av_log(ctx, AV_LOG_INFO, "SSIM %c:%f %c:%f %c:%f All:%f (%f)\n",
  289. s->comps[0], s->ssim[0] / s->nb_frames,
  290. s->comps[1], s->ssim[1] / s->nb_frames,
  291. s->comps[2], s->ssim[2] / s->nb_frames,
  292. (s->ssim[0] * 4 + s->ssim[1] + s->ssim[2]) / (s->nb_frames * 6),
  293. ssim_db(s->ssim[0] * 4 + s->ssim[1] + s->ssim[2], s->nb_frames * 6));
  294. } else if (s->nb_components == 1) {
  295. av_log(ctx, AV_LOG_INFO, "SSIM All:%f (%f)\n",
  296. s->ssim[0] / s->nb_frames, ssim_db(s->ssim[0], s->nb_frames));
  297. }
  298. }
  299. ff_dualinput_uninit(&s->dinput);
  300. if (s->stats_file)
  301. fclose(s->stats_file);
  302. av_freep(&s->temp);
  303. }
  304. static const AVFilterPad ssim_inputs[] = {
  305. {
  306. .name = "main",
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .filter_frame = filter_frame,
  309. },{
  310. .name = "reference",
  311. .type = AVMEDIA_TYPE_VIDEO,
  312. .filter_frame = filter_frame,
  313. .config_props = config_input_ref,
  314. },
  315. { NULL }
  316. };
  317. static const AVFilterPad ssim_outputs[] = {
  318. {
  319. .name = "default",
  320. .type = AVMEDIA_TYPE_VIDEO,
  321. .config_props = config_output,
  322. .request_frame = request_frame,
  323. },
  324. { NULL }
  325. };
  326. AVFilter ff_vf_ssim = {
  327. .name = "ssim",
  328. .description = NULL_IF_CONFIG_SMALL("Calculate the SSIM between two video streams."),
  329. .init = init,
  330. .uninit = uninit,
  331. .query_formats = query_formats,
  332. .priv_size = sizeof(SSIMContext),
  333. .priv_class = &ssim_class,
  334. .inputs = ssim_inputs,
  335. .outputs = ssim_outputs,
  336. };