Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp timing printf #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "avif/internal.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

Expand All @@ -21,6 +22,63 @@ static void ipmaPush(struct ipmaArray * ipma, uint8_t assoc, avifBool essential)
++ipma->count;
}

// Note: Code copied from libwebp for illustrative purposes. Once direction is decided the code would be added to libavif properly.
#ifndef _MSC_VER
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
#define WEBP_INLINE inline
#else
#define WEBP_INLINE
#endif
#else
#define WEBP_INLINE __forceinline
#endif /* _MSC_VER */


#if defined _WIN32 && !defined __GNUC__
#include <windows.h>

typedef LARGE_INTEGER Stopwatch;

static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
QueryPerformanceCounter(watch);//
}

static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
const LARGE_INTEGER old_value = *watch;
LARGE_INTEGER freq;
if (!QueryPerformanceCounter(watch))
return 0.0;
if (!QueryPerformanceFrequency(&freq))
return 0.0;
if (freq.QuadPart == 0)
return 0.0;
return (watch->QuadPart - old_value.QuadPart) / (double)freq.QuadPart;
}


#else /* !_WIN32 */
#include <sys/time.h>

typedef struct timeval Stopwatch;

static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
gettimeofday(watch, NULL);
}

static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
struct timeval old_value;
double delta_sec, delta_usec;
memcpy(&old_value, watch, sizeof(old_value));
gettimeofday(watch, NULL);
delta_sec = (double)watch->tv_sec - old_value.tv_sec;
delta_usec = (double)watch->tv_usec - old_value.tv_usec;
return delta_sec + delta_usec / 1000000.0;
}

#endif /* _WIN32 */


// Used to store offsets in meta boxes which need to point at mdat offsets that
// aren't known yet. When an item's mdat payload is written, all registered fixups
// will have this now-known offset "fixed up".
Expand Down Expand Up @@ -1215,6 +1273,8 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
cellImage = paddedCellImage;
}
const int quantizer = item->alpha ? encoder->data->quantizerAlpha : encoder->data->quantizer;
Stopwatch watch;
StopwatchReset(&watch);
avifResult encodeResult = item->codec->encodeImage(item->codec,
encoder,
cellImage,
Expand All @@ -1225,6 +1285,7 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
encoderChanges,
addImageFlags,
item->encodeOutput);
double deltaSec = StopwatchReadAndReset(&watch);
if (paddedCellImage) {
avifImageDestroy(paddedCellImage);
}
Expand All @@ -1234,6 +1295,8 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
if (encodeResult != AVIF_RESULT_OK) {
return encodeResult;
}
double megapixelsPerSecEncode = ((cellImage->width * cellImage->height) / 1000000.0) / deltaSec;
printf("encodeImage %g milliseconds MP/s: %g\n", deltaSec * 1000.0, megapixelsPerSecEncode);
}
}

Expand Down