From a2ae62b99d404a024401778ea1fa468b6ae6dd26 Mon Sep 17 00:00:00 2001 From: Derek Buitenhuis Date: Fri, 4 Jul 2014 18:36:11 +0100 Subject: [PATCH] Initial Commit Signed-off-by: Derek Buitenhuis --- COPYING | 13 +++++ Makefile | 40 +++++++++++++ README.md | 21 +++++++ api.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ build.bat | 4 ++ ijgdec.c | 130 ++++++++++++++++++++++++++++++++++++++++++ ijgdec.h | 30 ++++++++++ ijgenc.c | 92 ++++++++++++++++++++++++++++++ ijgenc.h | 24 ++++++++ main.c | 89 +++++++++++++++++++++++++++++ metric.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++ metric.h | 22 ++++++++ smallfry.def | 6 ++ smallfry.h | 46 +++++++++++++++ smallfry.ver | 8 +++ 15 files changed, 829 insertions(+) create mode 100644 COPYING create mode 100644 Makefile create mode 100644 README.md create mode 100644 api.c create mode 100644 build.bat create mode 100644 ijgdec.c create mode 100644 ijgdec.h create mode 100644 ijgenc.c create mode 100644 ijgenc.h create mode 100644 main.c create mode 100644 metric.c create mode 100644 metric.h create mode 100644 smallfry.def create mode 100644 smallfry.h create mode 100644 smallfry.ver diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..0b0a702 --- /dev/null +++ b/COPYING @@ -0,0 +1,13 @@ +Copyright (c) 2014, Derek Buitenhuis + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ed6f3c8 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +OBJ = ijgdec.o \ + ijgenc.o \ + metric.o \ + api.o \ + main.o + +LIBOBJ = ijgdec.lo \ + ijgenc.lo \ + metric.lo \ + api.lo + +PREFIX=/usr/local +CFLAGS=-Wall +LDFLAGS=-s +LIBS=-ljpeg -lm + +all: smallfry lib + +smallfry: $(OBJ) + $(CC) -o $@ $^ $(LIBS) + +$(LIBOBJ): + $(CC) -fPIC $(CFLAGS) -c $(@:.lo=.c) -o $@ + +libsmallfry.so: $(LIBOBJ) + $(CC) -Wl,--version-script,smallfry.ver -shared -o $@ $^ $(LIBS) + +lib: libsmallfry.so + +clean: + @rm -vf ./*.o + @rm -vf ./smallfry + @rm -vf ./*.lo + @rm -vf ./libsmallfry.so + +install: smallfry + @cp -vf ./smallfry $(PREFIX)/bin/smallfry + +uninstall: + @rm -vf $(PREFIX)/bin/smallfry diff --git a/README.md b/README.md new file mode 100644 index 0000000..44ad41c --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +## SmallFry ## + +SmallFry is a small proof-of-concept JPEG "optimizer" with a permissive license. What it does is try and find the most you can compress a JPEG without suffering obvious perceived quality loss. It works similarly to how [JPEGmini](http://www.jpegmini.com) works, with some borrowed metrics from their SPIE papers. If you want a nice GUI and easier batch automation, take a look at JPEGmini. + +**Background** + +I wrote this hack-of-a-program in early 2013, and largely ignored it since. Now that [mozjpeg](https://github.com/mozilla/mozjpeg) has gain some small mindshared, I thought it might be useful to others, since now it can take advantage of trellis quantization and scan optimization provided by it. + +**Usability** + +A small CLI util is provided, along with a (crappy) API and library. The code is just C89, and can be compiled with pretty much any compiler. Binaries are provided for Windows in the releases section. + +The code is very sub-optimal and slow. Pull requests are welcome. I am lazy. + +Currently only JPEG input is supported, but it is absolutely trivial to add other input via [ImageMagick](http://www.imagemagick.org) or [libavcodec](http://ffmpeg.org) (maybe via [FFMS2](https://github.com/FFMS/ffms2)). The current code is just what I had hacked together previously; it is not optimal. + +**Disclaimer** + +This code is entirely mine, and unrelated to my employer in any way, and is not used by my employer. + +Users of this code may want to look into any patents they may require to deploy it. I am not a lawyer. diff --git a/api.c b/api.c new file mode 100644 index 0000000..ed5059d --- /dev/null +++ b/api.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "smallfry.h" +#include "ijgdec.h" +#include "ijgenc.h" +#include "metric.h" + +void smallfryfreectx(smallfryctx **in) +{ + smallfryctx *ctx = *in; + + if (!ctx) + return; + + if (ctx->outbuf) + free(ctx->outbuf); + + if (ctx->decinput) + free(ctx->decinput); + + if (ctx->decoutput) + free(ctx->decoutput); + + if (ctx->inplanar) + free(ctx->inplanar); + + if (ctx->outplanar) + free(ctx->outplanar); + + free(ctx); + + *in = NULL; +} + +smallfryctx *smallfryallocctx(uint8_t *inbuf, uint64_t size) +{ + smallfryctx *ret; + int bufsize; + + ret = calloc(sizeof(smallfryctx), 1); + if (!ret) + goto allocfail; + + ret->inbuf = inbuf; + ret->isize = size; + ret->osize = 0; + + ret->res = jpeg_get_res(ret->inbuf, ret->isize); + if (!ret->res.width) + goto allocfail; + + bufsize = ret->res.width * ret->res.height * 3; + + ret->outbuf = malloc(bufsize); + ret->decinput = malloc(bufsize); + ret->decoutput = malloc(bufsize); + ret->inplanar = malloc(bufsize / 3); + ret->outplanar = malloc(bufsize / 3); + if (!ret->outbuf || !ret->decinput || !ret->decoutput || !ret->inplanar || !ret->outplanar) + goto allocfail; + + return ret; + +allocfail: + smallfryfreectx(&ret); + return NULL; +} + +static void process_planar(uint8_t *interleaved, uint8_t *planar, jinfo res) +{ + int i; + int j = 0; + uint8_t *out = planar; + + for (i = 0; i < res.width * res.height * 3; i += 3) { + out[j] = interleaved[i]; + j++; + } +} + +int smallfryoptimize(smallfryctx *ctx) +{ + const double threshold = 99.91 + 4.13 / 2; + + int ret; + int qmin = 0, qmax = 100; + double cur = threshold + 1; + + ret = jpeg_decode(ctx->inbuf, ctx->decinput, ctx->isize); + if (ret < 0) + return EINVAL; + + process_planar(ctx->decinput, ctx->inplanar, ctx->res); + + while (qmin <= qmax) { + int size; + int quality = (qmin + qmax) / 2; + + size = jpeg_encode(ctx->decinput, ctx->outbuf, ctx->res, quality); + + jpeg_decode(ctx->outbuf, ctx->decoutput, size); + + process_planar(ctx->decoutput, ctx->outplanar, ctx->res); + + cur = getmetric(ctx->inplanar, ctx->outplanar, ctx->res.width, ctx->res.height); + + if (cur < threshold) + qmin = quality + 1; + else + qmax = quality - 1; + } + + printf("Using quality = %d.\n", qmax + 1); + + ctx->osize = jpeg_encode(ctx->decinput, ctx->outbuf, ctx->res, qmax + 1); + + return 0; +} + +uint64_t smallfrygetsize(smallfryctx *ctx) +{ + return ctx->osize; +} + +uint8_t *smallfrygetbuffer(smallfryctx *ctx) +{ + return ctx->outbuf; +} diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..633e83b --- /dev/null +++ b/build.bat @@ -0,0 +1,4 @@ +@echo off +cl /MT /GL /c /I. *.c +link /LTCG /out:smallfry.exe *.obj jpeg-static.lib +link /LTCG /dll /def:smallfry.def /out:smallfry.dll *.obj jpeg-static.lib \ No newline at end of file diff --git a/ijgdec.c b/ijgdec.c new file mode 100644 index 0000000..d23647e --- /dev/null +++ b/ijgdec.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include + +#include "ijgdec.h" + +static const JOCTET EOI_BUFFER[1] = { JPEG_EOI }; + +static void init(j_decompress_ptr cinfo) +{ +} + +static boolean input(j_decompress_ptr cinfo) +{ + cinfo->src->next_input_byte = EOI_BUFFER; + cinfo->src->bytes_in_buffer = 1; + + return TRUE; +} + +static void skip(j_decompress_ptr cinfo, long num_bytes) +{ + if (cinfo->src->bytes_in_buffer < num_bytes) { + cinfo->src->next_input_byte = EOI_BUFFER; + cinfo->src->bytes_in_buffer = 1; + } else { + cinfo->src->next_input_byte += num_bytes; + cinfo->src->bytes_in_buffer -= num_bytes; + } +} + +static void term(j_decompress_ptr cinfo) +{ +} + +jinfo jpeg_get_res(uint8_t *inb, uint64_t insize) +{ + struct jpeg_decompress_struct cinfo; + struct jpeg_source_mgr s; + struct jpeg_error_mgr jerr; + jinfo ret; + + jpeg_create_decompress(&cinfo); + + cinfo.err = jpeg_std_error(&jerr); + cinfo.src = &s; + + cinfo.src->init_source = init; + cinfo.src->fill_input_buffer = input; + cinfo.src->skip_input_data = skip; + cinfo.src->resync_to_restart = jpeg_resync_to_restart; + cinfo.src->term_source = term; + cinfo.src->bytes_in_buffer = insize; + cinfo.src->next_input_byte = (const JOCTET *) inb; + + jpeg_read_header(&cinfo, TRUE); + jpeg_start_decompress(&cinfo); + + ret.width = cinfo.output_width; + ret.height = cinfo.output_height; + ret.vsamp = cinfo.comp_info[0].v_samp_factor; + ret.hsamp = cinfo.comp_info[0].h_samp_factor; + + jpeg_destroy_decompress(&cinfo); + + return ret; +} + +int jpeg_decode(uint8_t *inb, uint8_t *o, uint64_t insize) +{ + JSAMPARRAY outb; + struct jpeg_decompress_struct cinfo; + struct jpeg_source_mgr s; + struct jpeg_error_mgr jerr; + + jpeg_create_decompress(&cinfo); + + cinfo.err = jpeg_std_error(&jerr); + cinfo.src = &s; + + cinfo.src->init_source = init; + cinfo.src->fill_input_buffer = input; + cinfo.src->skip_input_data = skip; + cinfo.src->resync_to_restart = jpeg_resync_to_restart; + cinfo.src->term_source = term; + cinfo.src->bytes_in_buffer = insize; + cinfo.src->next_input_byte = (const JOCTET *) inb; + + jpeg_read_header(&cinfo, TRUE); + + cinfo.out_color_space = JCS_YCbCr; + + jpeg_start_decompress(&cinfo); + + outb = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, + cinfo.output_width * cinfo.output_components, 1); + if (!outb) + return -1; + + while (cinfo.output_scanline < cinfo.output_height) { + jpeg_read_scanlines(&cinfo, outb, 1); + memcpy(o + cinfo.output_width * cinfo.output_components * (cinfo.output_scanline - 1), + outb[0], cinfo.output_width * cinfo.output_components); + } + + jpeg_finish_decompress(&cinfo); + + jpeg_destroy_decompress(&cinfo); + + return 0; +} diff --git a/ijgdec.h b/ijgdec.h new file mode 100644 index 0000000..176e7f1 --- /dev/null +++ b/ijgdec.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef IJGDEC_H +#define IJGDEC_H + +typedef struct jinfo { + int width; + int height; + int vsamp; + int hsamp; +} jinfo; + +jinfo jpeg_get_res(uint8_t *inb, uint64_t insize); +int jpeg_decode(uint8_t *inb, uint8_t *o, uint64_t insize); + +#endif diff --git a/ijgenc.c b/ijgenc.c new file mode 100644 index 0000000..d73157a --- /dev/null +++ b/ijgenc.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include +#include + +#include "ijgdec.h" +#include "ijgenc.h" + +static void init(j_compress_ptr cinfo) +{ +} + +static boolean empty(j_compress_ptr cinfo) +{ + return TRUE; +} + +static void term(j_compress_ptr cinfo) +{ +} + +int jpeg_encode(uint8_t *in, uint8_t *out, jinfo res, int quality) +{ + JSAMPROW curline[1]; + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + struct jpeg_destination_mgr d; + int size; + + cinfo.err = jpeg_std_error(&jerr); + + jpeg_create_compress(&cinfo); + + cinfo.dest = &d; + cinfo.image_width = res.width; + cinfo.image_height = res.height; + cinfo.input_components = 3; + cinfo.in_color_space = JCS_YCbCr; + + cinfo.use_moz_defaults = TRUE; + + jpeg_set_defaults(&cinfo); + + cinfo.use_flat_quant_tbl = TRUE; + cinfo.lambda_log_scale1 = 10.5; + cinfo.lambda_log_scale2 = 13.0; + + cinfo.use_lambda_weight_tbl = TRUE; + + cinfo.dest->init_destination = &init; + cinfo.dest->empty_output_buffer = ∅ + cinfo.dest->term_destination = &term; + cinfo.dest->free_in_buffer = res.width * res.height * 3; + cinfo.dest->next_output_byte = (JOCTET *) out; + + cinfo.comp_info[0].v_samp_factor = res.vsamp; + cinfo.comp_info[0].h_samp_factor = res.hsamp; + + jpeg_set_quality(&cinfo, quality, TRUE); + + jpeg_start_compress(&cinfo, TRUE); + + while (cinfo.next_scanline < cinfo.image_height) { + curline[0] = &in[cinfo.next_scanline * res.width * 3]; + jpeg_write_scanlines(&cinfo, curline, 1); + } + + jpeg_finish_compress(&cinfo); + + size = (res.width * res.height * 3) - cinfo.dest->free_in_buffer; + + jpeg_destroy_compress(&cinfo); + + return size; +} diff --git a/ijgenc.h b/ijgenc.h new file mode 100644 index 0000000..34f0731 --- /dev/null +++ b/ijgenc.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef IJGENC_H +#define IJGENC_H + +#include "ijgdec.h" + +int jpeg_encode(uint8_t *in, uint8_t *out, jinfo res, int quality); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..1d5909d --- /dev/null +++ b/main.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "smallfry.h" + +#ifdef _MSC_VER +#pragma warning(disable: 4996) +#define fseeko _fseeki64 +#define ftello _ftelli64 +#endif + +int main(int argc, char **argv) +{ + smallfryctx *ctx; + FILE *in, *out; + uint8_t *inbuf; + uint64_t size; + int ret; + + if (argc < 3) { + printf("Usage: %s in.jpg out.jpg\n", argv[0]); + return -1; + } + + in = fopen(argv[1], "rb"); + if (!in) { + printf("Cannot open %s.\n", argv[1]); + return -1; + } + + out = fopen(argv[2], "wb"); + if (!out) { + fclose(in); + printf("Cannot open %s.\n", argv[2]); + return -1; + } + + fseeko(in, 0, SEEK_END); + size = ftello(in); + fseeko(in, 0, SEEK_SET); + + inbuf = malloc(size); + if (!inbuf) { + fclose(out); + fclose(in); + printf("Cannot allocate input buffer.\n"); + return -1; + } + + fread(inbuf, 1, size, in); + fclose(in); + + ctx = smallfryallocctx(inbuf, size); + if (!ctx) { + fclose(out); + free(inbuf); + printf("Context allocation failed.\n"); + return -1; + } + + ret = smallfryoptimize(ctx); + if (ret) + printf("Cannot decode.\n"); + + fwrite(smallfrygetbuffer(ctx), 1, smallfrygetsize(ctx), out); + fclose(out); + + smallfryfreectx(&ctx); + free(inbuf); + + return 0; +} diff --git a/metric.c b/metric.c new file mode 100644 index 0000000..9d2639e --- /dev/null +++ b/metric.c @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "metric.h" + +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a < b ? a : b) + +static double psnr_factor(uint8_t *orig, uint8_t *cmp, int orig_stride, + int cmp_stride, int width, int height, uint8_t max) +{ + uint8_t *old, *new; + double ret; + int sum; + int i, j; + + sum = 0; + old = orig; + new = cmp; + + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) + sum += (old[j] - new[j]) * (old[j] - new[j]); + + old += orig_stride; + new += cmp_stride; + } + + ret = (double) sum / (double) (width * height); + ret = 10.0 * log10(65025.0 / ret); + + if (max > 128) + ret /= 50.0; + else + ret /= (0.0016 * (double) (max * max)) - (0.38 * (double) max + 72.5); + + return MAX(MIN(ret, 1.0), 0.0); +} + +#define DVAL(i) (abs(old[i] - new[i])) +#define HDVAL(i,j) (abs(old[i + j * orig_stride] - new[i + j * cmp_stride])) +static double aae_factor(uint8_t *orig, uint8_t *cmp, int orig_stride, + int cmp_stride, int width, int height, uint8_t max) +{ + uint8_t *old, *new; + double ret; + double sum; + double cfmax, cf; + int i, j; + int cnt; + + sum = 0.0; + cnt = 0; + old = orig; + new = cmp; + + for (i = 0; i < height; i++) { + for (j = 7; j < width - 1; j += 8) { + double calc; + + cnt++; + + calc = abs(DVAL(j) - DVAL(j + 1)); + calc /= (abs(DVAL(j - 1) - DVAL(j)) + abs(DVAL(j + 1) - DVAL(j + 2)) + 0.0001) / 2.0; + + if (calc > 5.0) + sum += 1.0; + else if (calc > 2.0) + sum += (calc - 2.0) / (5.0 - 2.0); + } + + old += orig_stride; + new += cmp_stride; + } + + old = orig + 7 * orig_stride; + new = cmp + 7 * cmp_stride; + + for (i = 7; i < height - 1; i += 8) { + for (j = 0; j < width; j++) { + double calc; + + cnt++; + + calc = abs(DVAL(j) - HDVAL(j, 1)); + calc /= (abs(HDVAL(j, -1) - DVAL(j)) + abs(HDVAL(j, 1) - HDVAL(j, 2)) + 0.0001) / 2.0; + + if (calc > 5.0) + sum += 1.0; + else if (calc > 2.0) + sum += (calc - 2.0) / (5.0 - 2.0); + } + + old += 8 * orig_stride; + new += 8 * cmp_stride; + } + + ret = 1 - (sum / (double) cnt); + + if (max > 128) + cfmax = 0.65; + else + cfmax = 0.65 + 0.35 * ((128.0 - (double) max) / 128.0); + + cf = MAX(cfmax, MIN(1, 0.25 + (1000.0 * (double) cnt) / sum)); + + return ret * cf; +} + +static uint8_t maxluma(uint8_t *buf, int stride, int width, int height) +{ + uint8_t *in = buf; + uint8_t max = 0; + int i, j; + + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) + max = MAX(in[j], max); + + in += stride; + } + + return max; +} + +double getmetric(uint8_t *inbuf, uint8_t *outbuf, int width, int height) +{ + double p, a, b; + uint8_t max; + + max = maxluma(inbuf, width, width, height); + + p = psnr_factor(inbuf, outbuf, width, width, width, height, max); + a = aae_factor(inbuf, outbuf, width, width, width, height, max); + + b = p * 37.1891885161239 + a * 78.5328607296973; + + return b; +} diff --git a/metric.h b/metric.h new file mode 100644 index 0000000..6b5c3a8 --- /dev/null +++ b/metric.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef METRIC_H +#define METRIC_H + +double getmetric(uint8_t *inbuf, uint8_t *outbuf, int width, int height); + +#endif diff --git a/smallfry.def b/smallfry.def new file mode 100644 index 0000000..dcf2d55 --- /dev/null +++ b/smallfry.def @@ -0,0 +1,6 @@ +EXPORTS + smallfryfreectx + smallfryoptimize + smallfrygetsize + smallfrygetbuffer + smallfryallocctx \ No newline at end of file diff --git a/smallfry.h b/smallfry.h new file mode 100644 index 0000000..6fb7dcf --- /dev/null +++ b/smallfry.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014, Derek Buitenhuis + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef SMALLFRY_H +#define SMALLFRY_H + +#include + +#include "ijgdec.h" + +typedef struct smallfryctx { + uint8_t *inbuf; + uint8_t *outbuf; + + uint8_t *decinput; + uint8_t *decoutput; + + uint8_t *inplanar; + uint8_t *outplanar; + + jinfo res; + + uint64_t isize; + uint64_t osize; +} smallfryctx; + +smallfryctx *smallfryallocctx(uint8_t *inbuf, uint64_t size); +void smallfryfreectx(smallfryctx **in); +int smallfryoptimize(smallfryctx *ctx); +uint64_t smallfrygetsize(smallfryctx *ctx); +uint8_t *smallfrygetbuffer(smallfryctx *ctx); + +#endif diff --git a/smallfry.ver b/smallfry.ver new file mode 100644 index 0000000..df52a37 --- /dev/null +++ b/smallfry.ver @@ -0,0 +1,8 @@ +SMALLFRY_1 { + global: smallfryfreectx; + smallfryoptimize; + smallfrygetsize; + smallfrygetbuffer; + smallfryallocctx; + local: *; +};