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.

512 lines
16KB

  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/avstring.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "avfilter.h"
  38. #include "dualinput.h"
  39. #include "drawutils.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #include "ssim.h"
  43. #include "video.h"
  44. typedef struct SSIMContext {
  45. const AVClass *class;
  46. FFDualInputContext dinput;
  47. FILE *stats_file;
  48. char *stats_file_str;
  49. int nb_components;
  50. int max;
  51. uint64_t nb_frames;
  52. double ssim[4], ssim_total;
  53. char comps[4];
  54. float coefs[4];
  55. uint8_t rgba_map[4];
  56. int planewidth[4];
  57. int planeheight[4];
  58. int *temp;
  59. int is_rgb;
  60. float (*ssim_plane)(SSIMDSPContext *dsp,
  61. uint8_t *main, int main_stride,
  62. uint8_t *ref, int ref_stride,
  63. int width, int height, void *temp,
  64. int max);
  65. SSIMDSPContext dsp;
  66. } SSIMContext;
  67. #define OFFSET(x) offsetof(SSIMContext, x)
  68. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  69. static const AVOption ssim_options[] = {
  70. {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  71. {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  72. { NULL }
  73. };
  74. AVFILTER_DEFINE_CLASS(ssim);
  75. static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
  76. {
  77. char value[128];
  78. snprintf(value, sizeof(value), "%0.2f", d);
  79. if (comp) {
  80. char key2[128];
  81. snprintf(key2, sizeof(key2), "%s%c", key, comp);
  82. av_dict_set(metadata, key2, value, 0);
  83. } else {
  84. av_dict_set(metadata, key, value, 0);
  85. }
  86. }
  87. static void ssim_4x4xn_16bit(const uint8_t *main8, ptrdiff_t main_stride,
  88. const uint8_t *ref8, ptrdiff_t ref_stride,
  89. int64_t (*sums)[4], int width)
  90. {
  91. const uint16_t *main16 = (const uint16_t *)main8;
  92. const uint16_t *ref16 = (const uint16_t *)ref8;
  93. int x, y, z;
  94. main_stride >>= 1;
  95. ref_stride >>= 1;
  96. for (z = 0; z < width; z++) {
  97. uint64_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
  98. for (y = 0; y < 4; y++) {
  99. for (x = 0; x < 4; x++) {
  100. unsigned a = main16[x + y * main_stride];
  101. unsigned b = ref16[x + y * ref_stride];
  102. s1 += a;
  103. s2 += b;
  104. ss += a*a;
  105. ss += b*b;
  106. s12 += a*b;
  107. }
  108. }
  109. sums[z][0] = s1;
  110. sums[z][1] = s2;
  111. sums[z][2] = ss;
  112. sums[z][3] = s12;
  113. main16 += 4;
  114. ref16 += 4;
  115. }
  116. }
  117. static void ssim_4x4xn_8bit(const uint8_t *main, ptrdiff_t main_stride,
  118. const uint8_t *ref, ptrdiff_t ref_stride,
  119. int (*sums)[4], int width)
  120. {
  121. int x, y, z;
  122. for (z = 0; z < width; z++) {
  123. uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
  124. for (y = 0; y < 4; y++) {
  125. for (x = 0; x < 4; x++) {
  126. int a = main[x + y * main_stride];
  127. int b = ref[x + y * ref_stride];
  128. s1 += a;
  129. s2 += b;
  130. ss += a*a;
  131. ss += b*b;
  132. s12 += a*b;
  133. }
  134. }
  135. sums[z][0] = s1;
  136. sums[z][1] = s2;
  137. sums[z][2] = ss;
  138. sums[z][3] = s12;
  139. main += 4;
  140. ref += 4;
  141. }
  142. }
  143. static float ssim_end1x(int64_t s1, int64_t s2, int64_t ss, int64_t s12, int max)
  144. {
  145. int64_t ssim_c1 = (int64_t)(.01*.01*max*max*64 + .5);
  146. int64_t ssim_c2 = (int64_t)(.03*.03*max*max*64*63 + .5);
  147. int64_t fs1 = s1;
  148. int64_t fs2 = s2;
  149. int64_t fss = ss;
  150. int64_t fs12 = s12;
  151. int64_t vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
  152. int64_t covar = fs12 * 64 - fs1 * fs2;
  153. return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
  154. / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
  155. }
  156. static float ssim_end1(int s1, int s2, int ss, int s12)
  157. {
  158. static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
  159. static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
  160. int fs1 = s1;
  161. int fs2 = s2;
  162. int fss = ss;
  163. int fs12 = s12;
  164. int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
  165. int covar = fs12 * 64 - fs1 * fs2;
  166. return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
  167. / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
  168. }
  169. static float ssim_endn_16bit(const int64_t (*sum0)[4], const int64_t (*sum1)[4], int width, int max)
  170. {
  171. float ssim = 0.0;
  172. int i;
  173. for (i = 0; i < width; i++)
  174. ssim += ssim_end1x(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
  175. sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
  176. sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
  177. sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3],
  178. max);
  179. return ssim;
  180. }
  181. static float ssim_endn_8bit(const int (*sum0)[4], const int (*sum1)[4], int width)
  182. {
  183. float ssim = 0.0;
  184. int i;
  185. for (i = 0; i < width; i++)
  186. ssim += ssim_end1(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
  187. sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
  188. sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
  189. sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3]);
  190. return ssim;
  191. }
  192. #define SUM_LEN(w) (((w) >> 2) + 3)
  193. static float ssim_plane_16bit(SSIMDSPContext *dsp,
  194. uint8_t *main, int main_stride,
  195. uint8_t *ref, int ref_stride,
  196. int width, int height, void *temp,
  197. int max)
  198. {
  199. int z = 0, y;
  200. float ssim = 0.0;
  201. int64_t (*sum0)[4] = temp;
  202. int64_t (*sum1)[4] = sum0 + SUM_LEN(width);
  203. width >>= 2;
  204. height >>= 2;
  205. for (y = 1; y < height; y++) {
  206. for (; z <= y; z++) {
  207. FFSWAP(void*, sum0, sum1);
  208. ssim_4x4xn_16bit(&main[4 * z * main_stride], main_stride,
  209. &ref[4 * z * ref_stride], ref_stride,
  210. sum0, width);
  211. }
  212. ssim += ssim_endn_16bit((const int64_t (*)[4])sum0, (const int64_t (*)[4])sum1, width - 1, max);
  213. }
  214. return ssim / ((height - 1) * (width - 1));
  215. }
  216. static float ssim_plane(SSIMDSPContext *dsp,
  217. uint8_t *main, int main_stride,
  218. uint8_t *ref, int ref_stride,
  219. int width, int height, void *temp,
  220. int max)
  221. {
  222. int z = 0, y;
  223. float ssim = 0.0;
  224. int (*sum0)[4] = temp;
  225. int (*sum1)[4] = sum0 + SUM_LEN(width);
  226. width >>= 2;
  227. height >>= 2;
  228. for (y = 1; y < height; y++) {
  229. for (; z <= y; z++) {
  230. FFSWAP(void*, sum0, sum1);
  231. dsp->ssim_4x4_line(&main[4 * z * main_stride], main_stride,
  232. &ref[4 * z * ref_stride], ref_stride,
  233. sum0, width);
  234. }
  235. ssim += dsp->ssim_end_line((const int (*)[4])sum0, (const int (*)[4])sum1, width - 1);
  236. }
  237. return ssim / ((height - 1) * (width - 1));
  238. }
  239. static double ssim_db(double ssim, double weight)
  240. {
  241. return 10 * log10(weight / (weight - ssim));
  242. }
  243. static AVFrame *do_ssim(AVFilterContext *ctx, AVFrame *main,
  244. const AVFrame *ref)
  245. {
  246. AVDictionary **metadata = &main->metadata;
  247. SSIMContext *s = ctx->priv;
  248. float c[4], ssimv = 0.0;
  249. int i;
  250. s->nb_frames++;
  251. for (i = 0; i < s->nb_components; i++) {
  252. c[i] = s->ssim_plane(&s->dsp, main->data[i], main->linesize[i],
  253. ref->data[i], ref->linesize[i],
  254. s->planewidth[i], s->planeheight[i], s->temp,
  255. s->max);
  256. ssimv += s->coefs[i] * c[i];
  257. s->ssim[i] += c[i];
  258. }
  259. for (i = 0; i < s->nb_components; i++) {
  260. int cidx = s->is_rgb ? s->rgba_map[i] : i;
  261. set_meta(metadata, "lavfi.ssim.", s->comps[i], c[cidx]);
  262. }
  263. s->ssim_total += ssimv;
  264. set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
  265. set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(ssimv, 1.0));
  266. if (s->stats_file) {
  267. fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames);
  268. for (i = 0; i < s->nb_components; i++) {
  269. int cidx = s->is_rgb ? s->rgba_map[i] : i;
  270. fprintf(s->stats_file, "%c:%f ", s->comps[i], c[cidx]);
  271. }
  272. fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(ssimv, 1.0));
  273. }
  274. return main;
  275. }
  276. static av_cold int init(AVFilterContext *ctx)
  277. {
  278. SSIMContext *s = ctx->priv;
  279. if (s->stats_file_str) {
  280. if (!strcmp(s->stats_file_str, "-")) {
  281. s->stats_file = stdout;
  282. } else {
  283. s->stats_file = fopen(s->stats_file_str, "w");
  284. if (!s->stats_file) {
  285. int err = AVERROR(errno);
  286. char buf[128];
  287. av_strerror(err, buf, sizeof(buf));
  288. av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
  289. s->stats_file_str, buf);
  290. return err;
  291. }
  292. }
  293. }
  294. s->dinput.process = do_ssim;
  295. s->dinput.shortest = 1;
  296. s->dinput.repeatlast = 0;
  297. return 0;
  298. }
  299. static int query_formats(AVFilterContext *ctx)
  300. {
  301. static const enum AVPixelFormat pix_fmts[] = {
  302. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
  303. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  304. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  305. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  306. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  307. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  308. AV_PIX_FMT_GBRP,
  309. #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf, AV_PIX_FMT_GBR##suf
  310. PF(P9), PF(P10), PF(P12), PF(P14), PF(P16),
  311. AV_PIX_FMT_NONE
  312. };
  313. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  314. if (!fmts_list)
  315. return AVERROR(ENOMEM);
  316. return ff_set_common_formats(ctx, fmts_list);
  317. }
  318. static int config_input_ref(AVFilterLink *inlink)
  319. {
  320. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  321. AVFilterContext *ctx = inlink->dst;
  322. SSIMContext *s = ctx->priv;
  323. int sum = 0, i;
  324. s->nb_components = desc->nb_components;
  325. if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
  326. ctx->inputs[0]->h != ctx->inputs[1]->h) {
  327. av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
  328. return AVERROR(EINVAL);
  329. }
  330. if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
  331. av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
  332. return AVERROR(EINVAL);
  333. }
  334. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  335. s->comps[0] = s->is_rgb ? 'R' : 'Y';
  336. s->comps[1] = s->is_rgb ? 'G' : 'U';
  337. s->comps[2] = s->is_rgb ? 'B' : 'V';
  338. s->comps[3] = 'A';
  339. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  340. s->planeheight[0] = s->planeheight[3] = inlink->h;
  341. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  342. s->planewidth[0] = s->planewidth[3] = inlink->w;
  343. for (i = 0; i < s->nb_components; i++)
  344. sum += s->planeheight[i] * s->planewidth[i];
  345. for (i = 0; i < s->nb_components; i++)
  346. s->coefs[i] = (double) s->planeheight[i] * s->planewidth[i] / sum;
  347. s->temp = av_mallocz_array(2 * SUM_LEN(inlink->w), (desc->comp[0].depth > 8) ? sizeof(int64_t[4]) : sizeof(int[4]));
  348. if (!s->temp)
  349. return AVERROR(ENOMEM);
  350. s->max = (1 << desc->comp[0].depth) - 1;
  351. s->ssim_plane = desc->comp[0].depth > 8 ? ssim_plane_16bit : ssim_plane;
  352. s->dsp.ssim_4x4_line = ssim_4x4xn_8bit;
  353. s->dsp.ssim_end_line = ssim_endn_8bit;
  354. if (ARCH_X86)
  355. ff_ssim_init_x86(&s->dsp);
  356. return 0;
  357. }
  358. static int config_output(AVFilterLink *outlink)
  359. {
  360. AVFilterContext *ctx = outlink->src;
  361. SSIMContext *s = ctx->priv;
  362. AVFilterLink *mainlink = ctx->inputs[0];
  363. int ret;
  364. outlink->w = mainlink->w;
  365. outlink->h = mainlink->h;
  366. outlink->time_base = mainlink->time_base;
  367. outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
  368. outlink->frame_rate = mainlink->frame_rate;
  369. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  370. return ret;
  371. return 0;
  372. }
  373. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  374. {
  375. SSIMContext *s = inlink->dst->priv;
  376. return ff_dualinput_filter_frame(&s->dinput, inlink, buf);
  377. }
  378. static int request_frame(AVFilterLink *outlink)
  379. {
  380. SSIMContext *s = outlink->src->priv;
  381. return ff_dualinput_request_frame(&s->dinput, outlink);
  382. }
  383. static av_cold void uninit(AVFilterContext *ctx)
  384. {
  385. SSIMContext *s = ctx->priv;
  386. if (s->nb_frames > 0) {
  387. char buf[256];
  388. int i;
  389. buf[0] = 0;
  390. for (i = 0; i < s->nb_components; i++) {
  391. int c = s->is_rgb ? s->rgba_map[i] : i;
  392. av_strlcatf(buf, sizeof(buf), " %c:%f (%f)", s->comps[i], s->ssim[c] / s->nb_frames,
  393. ssim_db(s->ssim[c], s->nb_frames));
  394. }
  395. av_log(ctx, AV_LOG_INFO, "SSIM%s All:%f (%f)\n", buf,
  396. s->ssim_total / s->nb_frames, ssim_db(s->ssim_total, s->nb_frames));
  397. }
  398. ff_dualinput_uninit(&s->dinput);
  399. if (s->stats_file && s->stats_file != stdout)
  400. fclose(s->stats_file);
  401. av_freep(&s->temp);
  402. }
  403. static const AVFilterPad ssim_inputs[] = {
  404. {
  405. .name = "main",
  406. .type = AVMEDIA_TYPE_VIDEO,
  407. .filter_frame = filter_frame,
  408. },{
  409. .name = "reference",
  410. .type = AVMEDIA_TYPE_VIDEO,
  411. .filter_frame = filter_frame,
  412. .config_props = config_input_ref,
  413. },
  414. { NULL }
  415. };
  416. static const AVFilterPad ssim_outputs[] = {
  417. {
  418. .name = "default",
  419. .type = AVMEDIA_TYPE_VIDEO,
  420. .config_props = config_output,
  421. .request_frame = request_frame,
  422. },
  423. { NULL }
  424. };
  425. AVFilter ff_vf_ssim = {
  426. .name = "ssim",
  427. .description = NULL_IF_CONFIG_SMALL("Calculate the SSIM between two video streams."),
  428. .init = init,
  429. .uninit = uninit,
  430. .query_formats = query_formats,
  431. .priv_size = sizeof(SSIMContext),
  432. .priv_class = &ssim_class,
  433. .inputs = ssim_inputs,
  434. .outputs = ssim_outputs,
  435. };