File: ssim.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (206 lines) | stat: -rw-r--r-- 5,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <math.h>
#include <stdio.h>

#include <gavl/gavl.h>

#include "ssim_tab.h"



/* Constants for suppressing instabilities for almost equal images */
static const double K1 = 0.01;
static const double K2 = 0.03;


/*
 *  Note: SSIM calculation is optimized for *accuracy*, not for *speed* !!
 */

/* Range for iterating through a frame */
typedef struct
  {
  int start;
  int len;
  const double * w;
  int coeffs_index;
  } range_t;

/* Wang, eq. 14 */

static double get_mu(range_t * ri, range_t * rj,
                     const float * data, int stride)
  {
  int i, j;
  double ret = 0.0;
  
  for(i = 0; i < ri->len; i++)
    {
    for(j = 0; j < rj->len; j++)
      {
      ret += ri->w[i] * rj->w[j] * data[j];
      }
    data += stride;
    }
  return ret;
  }

/* Wang, eq. 15 */

static double get_sigma(range_t * ri, range_t * rj,
                        const float * data, int stride,
                        double mu)
  {
  int i, j;
  double ret = 0.0;
  double diff;
  
  for(i = 0; i < ri->len; i++)
    {
    for(j = 0; j < rj->len; j++)
      {
      diff = data[j] - mu;
      ret += ri->w[i] * rj->w[j] * diff * diff;
      }
    data += stride;
    }
  return sqrt(ret);
  }

/* Wang, eq. 16 */

static double get_sigma_xy(range_t * ri, range_t * rj,
                           const float * data_x, int stride_x,
                           const float * data_y, int stride_y,
                           double mu_x, double mu_y)
  {
  int i, j;
  double ret = 0.0;
  
  for(i = 0; i < ri->len; i++)
    {
    for(j = 0; j < rj->len; j++)
      {
      ret += ri->w[i]*rj->w[j]*(data_x[j]-mu_x)*(data_y[j]-mu_y);
      }
    data_x += stride_x;
    data_y += stride_y;
    }
  return ret;
  }

static void setup_range(range_t * r, int center, int size)
  {
  int diff;
  
  r->start = center - SSIM_GAUSS_TAPS/2;
  r->len   = SSIM_GAUSS_TAPS;
  
  r->coeffs_index = SSIM_GAUSS_TAPS/2;
  
  if(r->start < 0)
    {
    diff = -r->start;

    r->len -= diff;
    r->coeffs_index -= diff;
    r->start = 0;
    }
  else if(r->start + r->len > size)
    {
    diff = r->start + r->len - size;
    
    r->len -= diff;
    r->coeffs_index += diff;
    }

  r->w = ssim_gauss_coeffs[r->coeffs_index];

  }

int gavl_video_frame_ssim(const gavl_video_frame_t * x,
                          const gavl_video_frame_t * y,
                          gavl_video_frame_t * dst,
                          const gavl_video_format_t * format)
  {
  int i, j;

  double sigma_x, sigma_y, mu_x, mu_y, sigma_xy;
  range_t ri, rj;
  float * dst_ptr;
  
  const float * data_x;
  const float * data_y;
  const float * data_start_x;
  const float * data_start_y;

  /* Dynamic range is 1.0 for our grayscale images */
  const float C1 = K1 * K1;
  const float C2 = K2 * K2;

  int stride_x, stride_y;

  stride_x = x->strides[0] / sizeof(float);
  stride_y = y->strides[0] / sizeof(float);
  
  if(format->pixelformat != GAVL_GRAY_FLOAT)
    return 0;

  if((format->image_width < SSIM_GAUSS_TAPS) ||
     (format->image_height < SSIM_GAUSS_TAPS))
    return 0;
  
  for(i = 0; i < format->image_height; i++)
    {
    dst_ptr = (float*)(dst->planes[0] + i * dst->strides[0]);
    setup_range(&ri, i, format->image_height);
    
    data_start_x = (float*)(x->planes[0] + ri.start * x->strides[0]);
    data_start_y = (float*)(y->planes[0] + ri.start * y->strides[0]);
    
    for(j = 0; j < format->image_width; j++)
      {
      setup_range(&rj, j, format->image_width);

      data_x = data_start_x + rj.start;
      data_y = data_start_y + rj.start;
      
      mu_x = get_mu(&ri, &rj, data_x, stride_x);
      mu_y = get_mu(&ri, &rj, data_y, stride_y);
      sigma_x =  get_sigma(&ri, &rj, data_x, stride_x, mu_x);
      sigma_y =  get_sigma(&ri, &rj, data_y, stride_y, mu_y);
      sigma_xy = get_sigma_xy(&ri, &rj,
                              data_x, stride_x,
                              data_y, stride_y,
                              mu_x, mu_y);
      
      /* Wang, eq. 13 */
      
      *dst_ptr = (2.0*mu_x*mu_y+C1)*(2.0*sigma_xy+C2) /
        ((mu_x*mu_x+mu_y*mu_y+C1)*(sigma_x*sigma_x+sigma_y*sigma_y+C2));
      dst_ptr++;
      }
    }
  
  return 1;
  }