Libav
tiny_psnr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
26 #include <math.h>
27 #include <float.h>
28 
29 #include "libavutil/intfloat.h"
30 #include "libavutil/intreadwrite.h"
31 
32 #define FFMIN(a, b) ((a) > (b) ? (b) : (a))
33 #define F 100
34 #define SIZE 2048
35 
36 uint64_t exp16_table[21] = {
37  65537,
38  65538,
39  65540,
40  65544,
41  65552,
42  65568,
43  65600,
44  65664,
45  65793,
46  66050,
47  66568,
48  67616,
49  69763,
50  74262,
51  84150,
52  108051,
53  178145,
54  484249,
55  3578144,
56  195360063,
57  582360139072LL,
58 };
59 
60 // 16.16 fixpoint log()
61 static int64_t log16(uint64_t a)
62 {
63  int i;
64  int out = 0;
65 
66  if (a < 1 << 16)
67  return -log16((1LL << 32) / a);
68  a <<= 16;
69 
70  for (i = 20; i >= 0; i--) {
71  int64_t b = exp16_table[i];
72  if (a < (b << 16))
73  continue;
74  out |= 1 << i;
75  a = ((a / b) << 16) + (((a % b) << 16) + b / 2) / b;
76  }
77  return out;
78 }
79 
80 static uint64_t int_sqrt(uint64_t a)
81 {
82  uint64_t ret = 0;
83  uint64_t ret_sq = 0;
84  int s;
85 
86  for (s = 31; s >= 0; s--) {
87  uint64_t b = ret_sq + (1ULL << (s * 2)) + (ret << s) * 2;
88  if (b <= a) {
89  ret_sq = b;
90  ret += 1ULL << s;
91  }
92  }
93  return ret;
94 }
95 
96 static int16_t get_s16l(uint8_t *p)
97 {
98  union {
99  uint16_t u;
100  int16_t s;
101  } v;
102  v.u = p[0] | p[1] << 8;
103  return v.s;
104 }
105 
106 static float get_f32l(uint8_t *p)
107 {
108  union av_intfloat32 v;
109  v.i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
110  return v.f;
111 }
112 
113 static double get_f64l(uint8_t *p)
114 {
115  return av_int2double(AV_RL64(p));
116 }
117 
118 int main(int argc, char *argv[])
119 {
120  int i, j;
121  uint64_t sse = 0;
122  double sse_d = 0.0;
123  FILE *f[2];
124  uint8_t buf[2][SIZE];
125  int len = 1;
126  int64_t max;
127  int shift = argc < 5 ? 0 : atoi(argv[4]);
128  int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
129  int size0 = 0;
130  int size1 = 0;
131  uint64_t maxdist = 0;
132  double maxdist_d = 0.0;
133 
134  if (argc < 3) {
135  printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n");
136  printf("WAV headers are skipped automatically.\n");
137  return 1;
138  }
139 
140  if (argc > 3) {
141  if (!strcmp(argv[3], "u8")) {
142  len = 1;
143  } else if (!strcmp(argv[3], "s16")) {
144  len = 2;
145  } else if (!strcmp(argv[3], "f32")) {
146  len = 4;
147  } else if (!strcmp(argv[3], "f64")) {
148  len = 8;
149  } else {
150  char *end;
151  len = strtol(argv[3], &end, 0);
152  if (*end || len < 1 || len > 2) {
153  fprintf(stderr, "Unsupported sample format: %s\n", argv[3]);
154  return 1;
155  }
156  }
157  }
158 
159  max = (1LL << (8 * len)) - 1;
160 
161  f[0] = fopen(argv[1], "rb");
162  f[1] = fopen(argv[2], "rb");
163  if (!f[0] || !f[1]) {
164  fprintf(stderr, "Could not open input files.\n");
165  return 1;
166  }
167 
168  for (i = 0; i < 2; i++) {
169  uint8_t *p = buf[i];
170  if (fread(p, 1, 12, f[i]) != 12)
171  return 1;
172  if (!memcmp(p, "RIFF", 4) &&
173  !memcmp(p + 8, "WAVE", 4)) {
174  if (fread(p, 1, 8, f[i]) != 8)
175  return 1;
176  while (memcmp(p, "data", 4)) {
177  int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
178  fseek(f[i], s, SEEK_CUR);
179  if (fread(p, 1, 8, f[i]) != 8)
180  return 1;
181  }
182  } else {
183  fseek(f[i], -12, SEEK_CUR);
184  }
185  }
186 
187  fseek(f[shift < 0], abs(shift), SEEK_CUR);
188 
189  fseek(f[0], skip_bytes, SEEK_CUR);
190  fseek(f[1], skip_bytes, SEEK_CUR);
191 
192  for (;;) {
193  int s0 = fread(buf[0], 1, SIZE, f[0]);
194  int s1 = fread(buf[1], 1, SIZE, f[1]);
195 
196  for (j = 0; j < FFMIN(s0, s1); j += len) {
197  switch (len) {
198  case 1:
199  case 2: {
200  int64_t a = buf[0][j];
201  int64_t b = buf[1][j];
202  int dist;
203  if (len == 2) {
204  a = get_s16l(buf[0] + j);
205  b = get_s16l(buf[1] + j);
206  } else {
207  a = buf[0][j];
208  b = buf[1][j];
209  }
210  sse += (a - b) * (a - b);
211  dist = abs(a - b);
212  if (dist > maxdist)
213  maxdist = dist;
214  break;
215  }
216  case 4:
217  case 8: {
218  double dist, a, b;
219  if (len == 8) {
220  a = get_f64l(buf[0] + j);
221  b = get_f64l(buf[1] + j);
222  } else {
223  a = get_f32l(buf[0] + j);
224  b = get_f32l(buf[1] + j);
225  }
226  dist = fabs(a - b);
227  sse_d += (a - b) * (a - b);
228  if (dist > maxdist_d)
229  maxdist_d = dist;
230  break;
231  }
232  }
233  }
234  size0 += s0;
235  size1 += s1;
236  if (s0 + s1 <= 0)
237  break;
238  }
239 
240  i = FFMIN(size0, size1) / len;
241  if (!i)
242  i = 1;
243  switch (len) {
244  case 1:
245  case 2: {
246  uint64_t psnr;
247  uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
248  if (sse)
249  psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
250  284619LL * F + (1LL << 31)) / (1LL << 32);
251  else
252  psnr = 1000 * F - 1; // floating point free infinity :)
253 
254  printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9d/%9d\n",
255  (int)(dev / F), (int)(dev % F),
256  (int)(psnr / F), (int)(psnr % F),
257  maxdist, size0, size1);
258  break;
259  }
260  case 4:
261  case 8: {
262  char psnr_str[64];
263  double dev = sqrt(sse_d / i);
264  uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32);
265 
266  if (sse_d) {
267  double psnr = 2 * log(DBL_MAX) - log(i / sse_d);
268  snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr);
269  } else
270  snprintf(psnr_str, sizeof(psnr_str), "inf");
271 
272  maxdist = maxdist_d * scale;
273 
274  printf("stddev:%10.2f PSNR:%s MAXDIFF:%10"PRIu64" bytes:%9d/%9d\n",
275  dev * scale, psnr_str, maxdist, size0, size1);
276  break;
277  }
278  }
279  return 0;
280 }
#define F
Definition: tiny_psnr.c:33
static int64_t log16(uint64_t a)
Definition: tiny_psnr.c:61
uint64_t exp16_table[21]
Definition: tiny_psnr.c:36
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define AV_RL64
Definition: intreadwrite.h:173
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
uint8_t
#define b
Definition: input.c:52
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
int main(int argc, char *argv[])
Definition: tiny_psnr.c:118
static uint64_t int_sqrt(uint64_t a)
Definition: tiny_psnr.c:80
uint32_t i
Definition: intfloat.h:28
static float get_f32l(uint8_t *p)
Definition: tiny_psnr.c:106
static double psnr(double d)
Definition: avconv.c:603
#define SIZE
Definition: tiny_psnr.c:34
static int16_t get_s16l(uint8_t *p)
Definition: tiny_psnr.c:96
int len
static double get_f64l(uint8_t *p)
Definition: tiny_psnr.c:113
#define FFMIN(a, b)
Definition: tiny_psnr.c:32