Rise
The Vieneo Province
Loading...
Searching...
No Matches
AnimatedAdMetadata.h
Go to the documentation of this file.
1#pragma once
2#include <DirectXMath.h>
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstdint>
7#include <vector>
8
9namespace Rendering
10{
12 {
13 unsigned width=0, height=0;
14 DirectX::XMFLOAT4 bounds{}, meanColor{};
16 uint64_t hash=14695981039346656037ull;
17 bool empty=true;
18 };
19
20 // Only compact statistics are retained per frame. One temporary canvas is
21 // scanned incrementally; callers share a pixel budget across all artwork.
23 {
24 public:
25 static constexpr unsigned MaximumPixels=1024*1024;
26 void Initialize(unsigned width, unsigned height, const std::vector<uint64_t>& ends, uint64_t hash)
27 { *this=AnimatedAdMetadata{};w=width;h=height;contentHash=hash;endTimes=ends;frames.resize(ends.size());ready.assign(ends.size(),false); }
28 bool Supported() const { return w && h && uint64_t(w)*h<=MaximumPixels; }
29 bool Pending() const { return !pixels.empty(); }
30 // Paused artwork must not retain a partial snapshot and monopolize the
31 // shared analysis budget. Completed frame statistics remain reusable.
32 void DiscardPending() { std::vector<uint32_t>().swap(pixels); }
33 const AdCreativeMetadata* Frame(size_t index) const
34 { return index<ready.size() && ready[index] ? &frames[index] : nullptr; }
35 unsigned Analyze(size_t index, const std::vector<uint32_t>& canvas, unsigned budget, bool allowSnapshot)
36 {
37 if(!Supported() || !budget)return 0;
38 if(!Pending())
39 {
40 if(!allowSnapshot || index>=ready.size() || ready[index] || canvas.size()!=size_t(w)*h)return 0;
42 }
43 static const auto linear=[] {
44 std::array<double,256> values{};
45 for(unsigned i=0;i<256;++i){const double c=i/255.;values[i]=c<=.04045 ? c/12.92 : std::pow((c+.055)/1.055,2.4);}
46 return values;
47 }();
48 const unsigned count=unsigned((std::min)(size_t(budget),pixels.size()-cursor));
49 for(size_t end=cursor+count;cursor<end;++cursor)
50 {
51 const uint32_t p=pixels[cursor];const unsigned r=(p>>16)&255,g=(p>>8)&255,b=p&255;
52 const double a=(p>>24)/255.;alpha+=a;
53 // The production GIF texture is BGRA UNORM, not an sRGB view.
54 red+=a*r/255.;green+=a*g/255.;blue+=a*b/255.;
55 luminance+=a*(linear[r]*.2126+linear[g]*.7152+linear[b]*.0722);
56 }
57 if(cursor==pixels.size())
58 {
59 auto& value=frames[pending];value.width=w;value.height=h;value.hash=contentHash;
60 // A billboard's designated creative area is its fixed full
61 // canvas. Frame-by-frame alpha bounds must not resize it.
62 value.bounds={0,0,1,1};value.alphaPixels=alpha;value.fillFraction=alpha/(double(w)*h);value.empty=alpha<=0;
63 if(alpha>0){value.meanColor={float(red/alpha),float(green/alpha),float(blue/alpha),float(value.fillFraction)};value.meanLuminance=luminance/alpha;}
64 ready[pending]=true;std::vector<uint32_t>().swap(pixels);
65 const uint64_t duration=endTimes[pending]-(pending ? endTimes[pending-1] : 0);
66 measuredDuration+=duration;weightedLuminance+=value.meanLuminance*duration;weightedAlpha+=value.fillFraction*duration;
67 }
68 return count;
69 }
70 struct Summary { double luminance=0, alphaFraction=0, analyzedFraction=0; };
72 {
73 Summary out;
75 if(!endTimes.empty() && endTimes.back())out.analyzedFraction=double(measuredDuration)/endTimes.back();
76 else if(!frames.empty() && ready.back()){out={frames.back().meanLuminance,frames.back().fillFraction,1};}
77 return out;
78 }
79 private:
80 unsigned w=0,h=0;uint64_t contentHash=0;
81 std::vector<uint64_t> endTimes;
82 std::vector<AdCreativeMetadata> frames;
83 std::vector<bool> ready;
84 std::vector<uint32_t> pixels;
85 size_t cursor=0,pending=0;
86 double alpha=0,red=0,green=0,blue=0,luminance=0;
87 uint64_t measuredDuration=0;
89 };
90}
std::vector< uint32_t > pixels
std::vector< AdCreativeMetadata > frames
static constexpr unsigned MaximumPixels
void Initialize(unsigned width, unsigned height, const std::vector< uint64_t > &ends, uint64_t hash)
const AdCreativeMetadata * Frame(size_t index) const
unsigned Analyze(size_t index, const std::vector< uint32_t > &canvas, unsigned budget, bool allowSnapshot)
std::vector< uint64_t > endTimes