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 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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
26 
27 #include "libavutil/intfloat.h"
28 
29 #define FFMIN(a, b) ((a) > (b) ? (b) : (a))
30 #define F 100
31 #define SIZE 2048
32 
33 uint64_t exp16_table[21] = {
34  65537,
35  65538,
36  65540,
37  65544,
38  65552,
39  65568,
40  65600,
41  65664,
42  65793,
43  66050,
44  66568,
45  67616,
46  69763,
47  74262,
48  84150,
49  108051,
50  178145,
51  484249,
52  3578144,
53  195360063,
54  582360139072LL,
55 };
56 
57 #if 0
58 // 16.16 fixpoint exp()
59 static unsigned int exp16(unsigned int a){
60  int i;
61  int out= 1<<16;
62 
63  for(i=19;i>=0;i--){
64  if(a&(1<<i))
65  out= (out*exp16_table[i] + (1<<15))>>16;
66  }
67 
68  return out;
69 }
70 #endif
71 
72 // 16.16 fixpoint log()
73 static int64_t log16(uint64_t a)
74 {
75  int i;
76  int out = 0;
77 
78  if (a < 1 << 16)
79  return -log16((1LL << 32) / a);
80  a <<= 16;
81 
82  for (i = 20; i >= 0; i--) {
83  int64_t b = exp16_table[i];
84  if (a < (b << 16))
85  continue;
86  out |= 1 << i;
87  a = ((a / b) << 16) + (((a % b) << 16) + b / 2) / b;
88  }
89  return out;
90 }
91 
92 static uint64_t int_sqrt(uint64_t a)
93 {
94  uint64_t ret = 0;
95  uint64_t ret_sq = 0;
96  int s;
97 
98  for (s = 31; s >= 0; s--) {
99  uint64_t b = ret_sq + (1ULL << (s * 2)) + (ret << s) * 2;
100  if (b <= a) {
101  ret_sq = b;
102  ret += 1ULL << s;
103  }
104  }
105  return ret;
106 }
107 
108 static int16_t get_s16l(uint8_t *p)
109 {
110  union {
111  uint16_t u;
112  int16_t s;
113  } v;
114  v.u = p[0] | p[1] << 8;
115  return v.s;
116 }
117 
118 static float get_f32l(uint8_t *p)
119 {
120  union av_intfloat32 v;
121  v.i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
122  return v.f;
123 }
124 
125 static int run_psnr(FILE *f[2], int len, int shift, int skip_bytes)
126 {
127  int i, j;
128  uint64_t sse = 0;
129  uint64_t dev;
130  uint8_t buf[2][SIZE];
131  uint64_t psnr;
132  int64_t max = (1LL << (8 * len)) - 1;
133  int size0 = 0;
134  int size1 = 0;
135  int maxdist = 0;
136  int noseek;
137 
138  noseek = fseek(f[0], 0, SEEK_SET) ||
139  fseek(f[1], 0, SEEK_SET);
140 
141  if (!noseek) {
142  for (i = 0; i < 2; i++) {
143  uint8_t *p = buf[i];
144  if (fread(p, 1, 12, f[i]) != 12)
145  return 1;
146  if (!memcmp(p, "RIFF", 4) &&
147  !memcmp(p + 8, "WAVE", 4)) {
148  if (fread(p, 1, 8, f[i]) != 8)
149  return 1;
150  while (memcmp(p, "data", 4)) {
151  int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
152  fseek(f[i], s, SEEK_CUR);
153  if (fread(p, 1, 8, f[i]) != 8)
154  return 1;
155  }
156  } else {
157  fseek(f[i], -12, SEEK_CUR);
158  }
159  }
160 
161  fseek(f[shift < 0], abs(shift), SEEK_CUR);
162 
163  fseek(f[0], skip_bytes, SEEK_CUR);
164  fseek(f[1], skip_bytes, SEEK_CUR);
165  }
166 
167  for (;;) {
168  int s0 = fread(buf[0], 1, SIZE, f[0]);
169  int s1 = fread(buf[1], 1, SIZE, f[1]);
170 
171  for (j = 0; j < FFMIN(s0, s1); j += len) {
172  int64_t a = buf[0][j];
173  int64_t b = buf[1][j];
174  int dist;
175  if (len == 2) {
176  a = get_s16l(buf[0] + j);
177  b = get_s16l(buf[1] + j);
178  } else if (len == 4) {
179  a = get_f32l(buf[0] + j) * (1 << 24);
180  b = get_f32l(buf[1] + j) * (1 << 24);
181  } else {
182  a = buf[0][j];
183  b = buf[1][j];
184  }
185  sse += (a - b) * (a - b);
186  dist = abs(a - b);
187  if (dist > maxdist)
188  maxdist = dist;
189  }
190  size0 += s0;
191  size1 += s1;
192  if (s0 + s1 <= 0)
193  break;
194  }
195 
196  i = FFMIN(size0, size1) / len;
197  if (!i)
198  i = 1;
199  dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
200  if (sse)
201  psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
202  284619LL * F + (1LL << 31)) / (1LL << 32);
203  else
204  psnr = 1000 * F - 1; // floating point free infinity :)
205 
206  printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5d bytes:%9d/%9d\n",
207  (int)(dev / F), (int)(dev % F),
208  (int)(psnr / F), (int)(psnr % F),
209  maxdist, size0, size1);
210  return psnr;
211 }
212 
213 int main(int argc, char *argv[])
214 {
215  FILE *f[2];
216  int len = 1;
217  int shift_first= argc < 5 ? 0 : atoi(argv[4]);
218  int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
219  int shift_last = shift_first + (argc < 7 ? 0 : atoi(argv[6]));
220  int shift;
221  int max_psnr = -1;
222  int max_psnr_shift = 0;
223 
224  if (argc > 3) {
225  if (!strcmp(argv[3], "u8")) {
226  len = 1;
227  } else if (!strcmp(argv[3], "s16")) {
228  len = 2;
229  } else if (!strcmp(argv[3], "f32")) {
230  len = 4;
231  } else {
232  char *end;
233  len = strtol(argv[3], &end, 0);
234  if (*end || len < 1 || len > 2) {
235  fprintf(stderr, "Unsupported sample format: %s\n", argv[3]);
236  return 1;
237  }
238  }
239  }
240 
241  if (argc < 3) {
242  printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes> [<shift search range>]]]]\n");
243  printf("WAV headers are skipped automatically.\n");
244  return 1;
245  }
246 
247  f[0] = fopen(argv[1], "rb");
248  f[1] = fopen(argv[2], "rb");
249  if (!f[0] || !f[1]) {
250  fprintf(stderr, "Could not open input files.\n");
251  return 1;
252  }
253 
254  for (shift = shift_first; shift <= shift_last; shift++) {
255  int psnr = run_psnr(f, len, shift, skip_bytes);
256  if (psnr > max_psnr || (shift < 0 && psnr == max_psnr)) {
257  max_psnr = psnr;
258  max_psnr_shift = shift;
259  }
260  }
261  if (shift_last > shift_first)
262  printf("Best PSNR is %3d.%02d for shift %i\n", (int)(max_psnr / F), (int)(max_psnr % F), max_psnr_shift);
263  return 0;
264 }
float v
const char * s
Definition: avisynth_c.h:668
static int shift(int a, int b)
Definition: sonic.c:86
#define F
Definition: tiny_psnr.c:30
if max(w)>1 w=0.9 *w/max(w)
static int64_t log16(uint64_t a)
Definition: tiny_psnr.c:73
Sinusoidal phase f
uint64_t exp16_table[21]
Definition: tiny_psnr.c:33
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride)
uint8_t
#define b
Definition: input.c:42
end end
static double psnr(double d)
Definition: ffmpeg.c:972
int main(int argc, char *argv[])
Definition: tiny_psnr.c:213
#define s0
Definition: regdef.h:37
static uint64_t int_sqrt(uint64_t a)
Definition: tiny_psnr.c:92
ret
Definition: avfilter.c:821
uint32_t i
Definition: intfloat.h:28
float u
static float get_f32l(uint8_t *p)
Definition: tiny_psnr.c:118
void * buf
Definition: avisynth_c.h:594
synthesis window for stochastic i
#define SIZE
Definition: tiny_psnr.c:31
static int16_t get_s16l(uint8_t *p)
Definition: tiny_psnr.c:108
#define s1
Definition: regdef.h:38
static int run_psnr(FILE *f[2], int len, int shift, int skip_bytes)
Definition: tiny_psnr.c:125
int len
printf("static const uint8_t my_array[100] = {\n")
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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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_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)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define FFMIN(a, b)
Definition: tiny_psnr.c:29