Skip to content

Commit

Permalink
Refactor compute methods of psnr and ansnr (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeoliva authored and li-zhi committed May 7, 2018
1 parent a3d5dfe commit 6118f5c
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 88 deletions.
6 changes: 6 additions & 0 deletions Xcode/vmaf_xcode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
B10D906920A0BEA20014D895 /* psnr_tools.c in Sources */ = {isa = PBXBuildFile; fileRef = B10D906820A0BEA10014D895 /* psnr_tools.c */; };
C7FEEB811DAA2826001FC78F /* adm_tools.c in Sources */ = {isa = PBXBuildFile; fileRef = C7FEEB781DAA2826001FC78F /* adm_tools.c */; };
C7FEEB821DAA2826001FC78F /* adm.c in Sources */ = {isa = PBXBuildFile; fileRef = C7FEEB7A1DAA2826001FC78F /* adm.c */; };
C7FEEB831DAA2826001FC78F /* all.c in Sources */ = {isa = PBXBuildFile; fileRef = C7FEEB7C1DAA2826001FC78F /* all.c */; };
Expand Down Expand Up @@ -245,6 +246,8 @@
C7FEEB8E1DAA2844001FC78F /* psnr_main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = psnr_main.c; path = ../feature/src/psnr_main.c; sourceTree = "<group>"; };
C7FEEB8F1DAA2844001FC78F /* psnr_options.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = psnr_options.h; path = ../feature/src/psnr_options.h; sourceTree = "<group>"; };
C7FEEB901DAA2844001FC78F /* psnr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = psnr.c; path = ../feature/src/psnr.c; sourceTree = "<group>"; };
B10D906720A0BEA10014D895 /* psnr_tools.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = psnr_tools.h; path = ../feature/src/psnr_tools.h; sourceTree = "<group>"; };
B10D906820A0BEA10014D895 /* psnr_tools.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = psnr_tools.c; path = ../feature/src/psnr_tools.c; sourceTree = "<group>"; };
C7FEEB911DAA2844001FC78F /* ssim_main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ssim_main.c; path = ../feature/src/ssim_main.c; sourceTree = "<group>"; };
C7FEEB921DAA2844001FC78F /* ssim.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ssim.c; path = ../feature/src/ssim.c; sourceTree = "<group>"; };
C7FEEB931DAA2844001FC78F /* vif_options.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vif_options.h; path = ../feature/src/vif_options.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -349,6 +352,8 @@
C7FEEBA41DAA2849001FC78F /* iqa */,
C7FEEB871DAA2844001FC78F /* moment_options.h */,
C7FEEB881DAA2844001FC78F /* moment.c */,
B10D906820A0BEA10014D895 /* psnr_tools.c */,
B10D906720A0BEA10014D895 /* psnr_tools.h */,
C7FEEB891DAA2844001FC78F /* motion_options.h */,
C7FEEB8A1DAA2844001FC78F /* motion_tools.h */,
C7FEEB8B1DAA2844001FC78F /* motion.c */,
Expand Down Expand Up @@ -603,6 +608,7 @@
C7FEEB9E1DAA2844001FC78F /* psnr.c in Sources */,
C7FEEBA21DAA2844001FC78F /* vif.c in Sources */,
C7FEEB991DAA2844001FC78F /* moment.c in Sources */,
B10D906920A0BEA20014D895 /* psnr_tools.c in Sources */,
C7FEEB9C1DAA2844001FC78F /* ms_ssim.c in Sources */,
C7FEEBB81DAA28D1001FC78F /* math_utils.c in Sources */,
C7FEEBB71DAA28D1001FC78F /* decimate.c in Sources */,
Expand Down
Binary file not shown.
42 changes: 17 additions & 25 deletions feature/src/all.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "common/file_io.h"
#include "common/convolution.h"
#include "common/convolution_internal.h"
#include "psnr_tools.h"
#include "motion_tools.h"
#include "all_options.h"
#include "vif_options.h"
Expand Down Expand Up @@ -58,6 +59,8 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i

size_t data_sz;
int stride;
double peak;
double psnr_max;
int ret = 1;

if (w <= 0 || h <= 0 || (size_t)w > ALIGN_FLOOR(INT_MAX) / sizeof(float))
Expand All @@ -72,6 +75,14 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i
goto fail_or_end;
}

ret = psnr_constants(fmt, &peak, &psnr_max);
if (ret)
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}

data_sz = (size_t)stride * h;

if (!(ref_buf = aligned_malloc(data_sz, MAX_ALIGN)))
Expand Down Expand Up @@ -114,7 +125,8 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i
{
ret = read_frame(ref_buf, dis_buf, temp_buf, stride, user_data);

if(ret == 1){
if (ret == 1)
{
goto fail_or_end;
}
if (ret == 2)
Expand All @@ -136,34 +148,17 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i
goto fail_or_end;
}
printf("adm: %d %f\n", frm_idx, score);
fflush(stdout);
printf("adm_num: %d %f\n", frm_idx, score_num);
fflush(stdout);
printf("adm_den: %d %f\n", frm_idx, score_den);
fflush(stdout);
for(int scale=0;scale<4;scale++){
printf("adm_num_scale%d: %d %f\n", scale, frm_idx, scores[2*scale]);
printf("adm_den_scale%d: %d %f\n", scale, frm_idx, scores[2*scale+1]);
}
fflush(stdout);

/* =========== ansnr ============== */
if (!strcmp(fmt, "yuv420p") || !strcmp(fmt, "yuv422p") || !strcmp(fmt, "yuv444p"))
{
// max psnr 60.0 for 8-bit per Ioannis
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, 255.0, 60.0);
}
else if (!strcmp(fmt, "yuv420p10le") || !strcmp(fmt, "yuv422p10le") || !strcmp(fmt, "yuv444p10le"))
{
// 10 bit gets normalized to 8 bit, peak is 1023 / 4.0 = 255.75
// max psnr 72.0 for 10-bit per Ioannis
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, 255.75, 72.0);
}
else
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, peak, psnr_max);

if (ret)
{
printf("error: compute_ansnr failed.\n");
Expand All @@ -172,7 +167,6 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i
}

printf("ansnr: %d %f\n", frm_idx, score);
fflush(stdout);
printf("anpsnr: %d %f\n", frm_idx, score_psnr);
fflush(stdout);

Expand Down Expand Up @@ -216,15 +210,13 @@ int all(int (*read_frame)(float *ref_data, float *main_data, float *temp_data, i
goto fail_or_end;
}
printf("vif: %d %f\n", frm_idx, score);
fflush(stdout);
printf("vif_num: %d %f\n", frm_idx, score_num);
fflush(stdout);
printf("vif_den: %d %f\n", frm_idx, score_den);
fflush(stdout);
for(int scale=0;scale<4;scale++){
printf("vif_num_scale%d: %d %f\n", scale, frm_idx, scores[2*scale]);
printf("vif_den_scale%d: %d %f\n", scale, frm_idx, scores[2*scale+1]);
}
fflush(stdout);

frm_idx++;
}
Expand Down
37 changes: 17 additions & 20 deletions feature/src/ansnr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "common/alloc.h"
#include "common/file_io.h"
#include "psnr_tools.h"
#include "ansnr_options.h"
#include "ansnr_tools.h"

Expand Down Expand Up @@ -122,6 +123,8 @@ int ansnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
float *temp_buf = 0;
size_t data_sz;
int stride;
double peak;
double psnr_max;
int ret = 1;

if (w <= 0 || h <= 0 || (size_t)w > ALIGN_FLOOR(INT_MAX) / sizeof(float))
Expand All @@ -136,6 +139,14 @@ int ansnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
goto fail_or_end;
}

ret = psnr_constants(fmt, &peak, &psnr_max);
if (ret)
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}

data_sz = (size_t)stride * h;

if (!(ref_buf = aligned_malloc(data_sz, MAX_ALIGN)))
Expand All @@ -156,13 +167,14 @@ int ansnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
fflush(stdout);
goto fail_or_end;
}

int frm_idx = 0;
while (1)
{
ret = read_frame(ref_buf, dis_buf, temp_buf, stride, user_data);

if(ret == 1){
if (ret == 1)
{
goto fail_or_end;
}
if (ret == 2)
Expand All @@ -176,23 +188,9 @@ int ansnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
offset_image(ref_buf, OPT_RANGE_PIXEL_OFFSET, w, h, stride);
offset_image(dis_buf, OPT_RANGE_PIXEL_OFFSET, w, h, stride);

if (!strcmp(fmt, "yuv420p") || !strcmp(fmt, "yuv422p") || !strcmp(fmt, "yuv444p"))
{
// max psnr 60.0 for 8-bit per Ioannis
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, 255.0, 60.0);
}
else if (!strcmp(fmt, "yuv420p10le") || !strcmp(fmt, "yuv422p10le") || !strcmp(fmt, "yuv444p10le"))
{
// 10 bit gets normalized to 8 bit, peak is 1023 / 4.0 = 255.75
// max psnr 72.0 for 10-bit per Ioannis
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, 255.75, 72.0);
}
else
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}
// compute
ret = compute_ansnr(ref_buf, dis_buf, w, h, stride, stride, &score, &score_psnr, peak, psnr_max);

if (ret)
{
printf("error: compute_ansnr failed.\n");
Expand All @@ -202,7 +200,6 @@ int ansnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,


printf("ansnr: %d %f\n", frm_idx, score);
fflush(stdout);
printf("anpsnr: %d %f\n", frm_idx, score_psnr);
fflush(stdout);

Expand Down
33 changes: 15 additions & 18 deletions feature/src/psnr.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "common/alloc.h"
#include "common/file_io.h"
#include "psnr_tools.h"
#include "psnr_options.h"

#define read_image_b read_image_b2s
Expand Down Expand Up @@ -68,6 +69,8 @@ int psnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
float *temp_buf = 0;
size_t data_sz;
int stride;
double peak;
double psnr_max;
int ret = 1;

if (w <= 0 || h <= 0 || (size_t)w > ALIGN_FLOOR(INT_MAX) / sizeof(float))
Expand All @@ -82,6 +85,14 @@ int psnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
goto fail_or_end;
}

ret = psnr_constants(fmt, &peak, &psnr_max);
if (ret)
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}

data_sz = (size_t)stride * h;

if (!(ref_buf = aligned_malloc(data_sz, MAX_ALIGN)))
Expand All @@ -108,7 +119,8 @@ int psnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
{
ret = read_frame(ref_buf, dis_buf, temp_buf, stride, user_data);

if(ret == 1){
if (ret == 1)
{
goto fail_or_end;
}
if (ret == 2)
Expand All @@ -117,23 +129,8 @@ int psnr(int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
}

// compute
if (!strcmp(fmt, "yuv420p") || !strcmp(fmt, "yuv422p") || !strcmp(fmt, "yuv444p"))
{
// max psnr 60.0 for 8-bit per Ioannis
ret = compute_psnr(ref_buf, dis_buf, w, h, stride, stride, &score, 255.0, 60.0);
}
else if (!strcmp(fmt, "yuv420p10le") || !strcmp(fmt, "yuv422p10le") || !strcmp(fmt, "yuv444p10le"))
{
// 10 bit gets normalized to 8 bit, peak is 1023 / 4.0 = 255.75
// max psnr 72.0 for 10-bit per Ioannis
ret = compute_psnr(ref_buf, dis_buf, w, h, stride, stride, &score, 255.75, 72.0);
}
else
{
printf("error: unknown format %s.\n", fmt);
fflush(stdout);
goto fail_or_end;
}
ret = compute_psnr(ref_buf, dis_buf, w, h, stride, stride, &score, peak, psnr_max);

if (ret)
{
printf("error: compute_psnr failed.\n");
Expand Down
45 changes: 45 additions & 0 deletions feature/src/psnr_tools.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
*
* Copyright 2016-2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <stdio.h>
#include "psnr_tools.h"

/*
* For a given format, returns the constants to use in psnr based calculations
*/
int psnr_constants(const char *fmt, double *peak, double *psnr_max) {
if (!strcmp(fmt, "yuv420p") || !strcmp(fmt, "yuv422p") || !strcmp(fmt, "yuv444p"))
{
// max psnr 60.0 for 8-bit per Ioannis
*peak = 255.0;
*psnr_max = 60.0;
}
else if (!strcmp(fmt, "yuv420p10le") || !strcmp(fmt, "yuv422p10le") || !strcmp(fmt, "yuv444p10le"))
{
// 10 bit gets normalized to 8 bit, peak is 1023 / 4.0 = 255.75
// max psnr 72.0 for 10-bit per Ioannis
*peak = 255.75;
*psnr_max = 72.0;
}
else
{
return 1;
}

return 0;
}
24 changes: 24 additions & 0 deletions feature/src/psnr_tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
*
* Copyright 2016-2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef PSNR_TOOLS_H_
#define PSNR_TOOLS_H_

int psnr_constants(const char *fmt, double *peak, double *psnr_max);

#endif /* PSNR_TOOLS_H_ */
4 changes: 4 additions & 0 deletions wrapper/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ OBJS = \
$(OBJDIR)/cpu.o \
$(OBJDIR)/convolution.o \
$(OBJDIR)/convolution_avx.o \
$(OBJDIR)/psnr_tools.o \
$(OBJDIR)/adm.o \
$(OBJDIR)/adm_tools.o \
$(OBJDIR)/ansnr.o \
Expand Down Expand Up @@ -72,6 +73,9 @@ $(OBJDIR)/cpu.o: $(FEATURESRCDIR)/common/cpu.c
$(OBJDIR)/convolution_avx.o: $(FEATURESRCDIR)/common/convolution_avx.c
$(CC) -c -o $@ $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) $<

$(OBJDIR)/psnr_tools.o: $(FEATURESRCDIR)/psnr_tools.c
$(CC) -c -o $@ $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) $<

$(OBJDIR)/adm.o: $(FEATURESRCDIR)/adm.c
$(CC) -c -o $@ $(CFLAGS) $(CPPFLAGS) $<

Expand Down
Loading

0 comments on commit 6118f5c

Please sign in to comment.