1// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8// -----------------------------------------------------------------------------
9//
10// Paginated token buffer
11//
12//  A 'token' is a bit value associated with a probability, either fixed
13// or a later-to-be-determined after statistics have been collected.
14// For dynamic probability, we just record the slot id (idx) for the probability
15// value in the final probability array (uint8_t* probas in VP8EmitTokens).
16//
17// Author: Skal (pascal.massimino@gmail.com)
18
19#include <assert.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "./cost.h"
24#include "./vp8enci.h"
25#include "../utils/utils.h"
26
27#if !defined(DISABLE_TOKEN_BUFFER)
28
29// we use pages to reduce the number of memcpy()
30#define MIN_PAGE_SIZE 8192          // minimum number of token per page
31#define FIXED_PROBA_BIT (1u << 14)
32
33typedef uint16_t token_t;  // bit #15: bit value
34                           // bit #14: flags for constant proba or idx
35                           // bits #0..13: slot or constant proba
36struct VP8Tokens {
37  VP8Tokens* next_;        // pointer to next page
38};
39// Token data is located in memory just after the next_ field.
40// This macro is used to return their address and hide the trick.
41#define TOKEN_DATA(p) ((const token_t*)&(p)[1])
42
43//------------------------------------------------------------------------------
44
45void VP8TBufferInit(VP8TBuffer* const b, int page_size) {
46  b->tokens_ = NULL;
47  b->pages_ = NULL;
48  b->last_page_ = &b->pages_;
49  b->left_ = 0;
50  b->page_size_ = (page_size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : page_size;
51  b->error_ = 0;
52}
53
54void VP8TBufferClear(VP8TBuffer* const b) {
55  if (b != NULL) {
56    VP8Tokens* p = b->pages_;
57    while (p != NULL) {
58      VP8Tokens* const next = p->next_;
59      WebPSafeFree(p);
60      p = next;
61    }
62    VP8TBufferInit(b, b->page_size_);
63  }
64}
65
66static int TBufferNewPage(VP8TBuffer* const b) {
67  VP8Tokens* page = NULL;
68  if (!b->error_) {
69    const size_t size = sizeof(*page) + b->page_size_ * sizeof(token_t);
70    page = (VP8Tokens*)WebPSafeMalloc(1ULL, size);
71  }
72  if (page == NULL) {
73    b->error_ = 1;
74    return 0;
75  }
76  page->next_ = NULL;
77
78  *b->last_page_ = page;
79  b->last_page_ = &page->next_;
80  b->left_ = b->page_size_;
81  b->tokens_ = (token_t*)TOKEN_DATA(page);
82  return 1;
83}
84
85//------------------------------------------------------------------------------
86
87#define TOKEN_ID(t, b, ctx) \
88    (NUM_PROBAS * ((ctx) + NUM_CTX * ((b) + NUM_BANDS * (t))))
89
90static WEBP_INLINE uint32_t AddToken(VP8TBuffer* const b,
91                                     uint32_t bit, uint32_t proba_idx) {
92  assert(proba_idx < FIXED_PROBA_BIT);
93  assert(bit <= 1);
94  if (b->left_ > 0 || TBufferNewPage(b)) {
95    const int slot = --b->left_;
96    b->tokens_[slot] = (bit << 15) | proba_idx;
97  }
98  return bit;
99}
100
101static WEBP_INLINE void AddConstantToken(VP8TBuffer* const b,
102                                         uint32_t bit, uint32_t proba) {
103  assert(proba < 256);
104  assert(bit <= 1);
105  if (b->left_ > 0 || TBufferNewPage(b)) {
106    const int slot = --b->left_;
107    b->tokens_[slot] = (bit << 15) | FIXED_PROBA_BIT | proba;
108  }
109}
110
111int VP8RecordCoeffTokens(const int ctx, const int coeff_type,
112                         int first, int last,
113                         const int16_t* const coeffs,
114                         VP8TBuffer* const tokens) {
115  int n = first;
116  uint32_t base_id = TOKEN_ID(coeff_type, n, ctx);
117  if (!AddToken(tokens, last >= 0, base_id + 0)) {
118    return 0;
119  }
120
121  while (n < 16) {
122    const int c = coeffs[n++];
123    const int sign = c < 0;
124    const uint32_t v = sign ? -c : c;
125    if (!AddToken(tokens, v != 0, base_id + 1)) {
126      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 0);  // ctx=0
127      continue;
128    }
129    if (!AddToken(tokens, v > 1, base_id + 2)) {
130      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 1);  // ctx=1
131    } else {
132      if (!AddToken(tokens, v > 4, base_id + 3)) {
133        if (AddToken(tokens, v != 2, base_id + 4))
134          AddToken(tokens, v == 4, base_id + 5);
135      } else if (!AddToken(tokens, v > 10, base_id + 6)) {
136        if (!AddToken(tokens, v > 6, base_id + 7)) {
137          AddConstantToken(tokens, v == 6, 159);
138        } else {
139          AddConstantToken(tokens, v >= 9, 165);
140          AddConstantToken(tokens, !(v & 1), 145);
141        }
142      } else {
143        int mask;
144        const uint8_t* tab;
145        uint32_t residue = v - 3;
146        if (residue < (8 << 1)) {          // VP8Cat3  (3b)
147          AddToken(tokens, 0, base_id + 8);
148          AddToken(tokens, 0, base_id + 9);
149          residue -= (8 << 0);
150          mask = 1 << 2;
151          tab = VP8Cat3;
152        } else if (residue < (8 << 2)) {   // VP8Cat4  (4b)
153          AddToken(tokens, 0, base_id + 8);
154          AddToken(tokens, 1, base_id + 9);
155          residue -= (8 << 1);
156          mask = 1 << 3;
157          tab = VP8Cat4;
158        } else if (residue < (8 << 3)) {   // VP8Cat5  (5b)
159          AddToken(tokens, 1, base_id + 8);
160          AddToken(tokens, 0, base_id + 10);
161          residue -= (8 << 2);
162          mask = 1 << 4;
163          tab = VP8Cat5;
164        } else {                         // VP8Cat6 (11b)
165          AddToken(tokens, 1, base_id + 8);
166          AddToken(tokens, 1, base_id + 10);
167          residue -= (8 << 3);
168          mask = 1 << 10;
169          tab = VP8Cat6;
170        }
171        while (mask) {
172          AddConstantToken(tokens, !!(residue & mask), *tab++);
173          mask >>= 1;
174        }
175      }
176      base_id = TOKEN_ID(coeff_type, VP8EncBands[n], 2);  // ctx=2
177    }
178    AddConstantToken(tokens, sign, 128);
179    if (n == 16 || !AddToken(tokens, n <= last, base_id + 0)) {
180      return 1;   // EOB
181    }
182  }
183  return 1;
184}
185
186#undef TOKEN_ID
187
188//------------------------------------------------------------------------------
189// This function works, but isn't currently used. Saved for later.
190
191#if 0
192
193static void Record(int bit, proba_t* const stats) {
194  proba_t p = *stats;
195  if (p >= 0xffff0000u) {               // an overflow is inbound.
196    p = ((p + 1u) >> 1) & 0x7fff7fffu;  // -> divide the stats by 2.
197  }
198  // record bit count (lower 16 bits) and increment total count (upper 16 bits).
199  p += 0x00010000u + bit;
200  *stats = p;
201}
202
203void VP8TokenToStats(const VP8TBuffer* const b, proba_t* const stats) {
204  const VP8Tokens* p = b->pages_;
205  while (p != NULL) {
206    const int N = (p->next_ == NULL) ? b->left_ : 0;
207    int n = MAX_NUM_TOKEN;
208    const token_t* const tokens = TOKEN_DATA(p);
209    while (n-- > N) {
210      const token_t token = tokens[n];
211      if (!(token & FIXED_PROBA_BIT)) {
212        Record((token >> 15) & 1, stats + (token & 0x3fffu));
213      }
214    }
215    p = p->next_;
216  }
217}
218
219#endif   // 0
220
221//------------------------------------------------------------------------------
222// Final coding pass, with known probabilities
223
224int VP8EmitTokens(VP8TBuffer* const b, VP8BitWriter* const bw,
225                  const uint8_t* const probas, int final_pass) {
226  const VP8Tokens* p = b->pages_;
227  assert(!b->error_);
228  while (p != NULL) {
229    const VP8Tokens* const next = p->next_;
230    const int N = (next == NULL) ? b->left_ : 0;
231    int n = b->page_size_;
232    const token_t* const tokens = TOKEN_DATA(p);
233    while (n-- > N) {
234      const token_t token = tokens[n];
235      const int bit = (token >> 15) & 1;
236      if (token & FIXED_PROBA_BIT) {
237        VP8PutBit(bw, bit, token & 0xffu);  // constant proba
238      } else {
239        VP8PutBit(bw, bit, probas[token & 0x3fffu]);
240      }
241    }
242    if (final_pass) WebPSafeFree((void*)p);
243    p = next;
244  }
245  if (final_pass) b->pages_ = NULL;
246  return 1;
247}
248
249// Size estimation
250size_t VP8EstimateTokenSize(VP8TBuffer* const b, const uint8_t* const probas) {
251  size_t size = 0;
252  const VP8Tokens* p = b->pages_;
253  assert(!b->error_);
254  while (p != NULL) {
255    const VP8Tokens* const next = p->next_;
256    const int N = (next == NULL) ? b->left_ : 0;
257    int n = b->page_size_;
258    const token_t* const tokens = TOKEN_DATA(p);
259    while (n-- > N) {
260      const token_t token = tokens[n];
261      const int bit = token & (1 << 15);
262      if (token & FIXED_PROBA_BIT) {
263        size += VP8BitCost(bit, token & 0xffu);
264      } else {
265        size += VP8BitCost(bit, probas[token & 0x3fffu]);
266      }
267    }
268    p = next;
269  }
270  return size;
271}
272
273//------------------------------------------------------------------------------
274
275#else     // DISABLE_TOKEN_BUFFER
276
277void VP8TBufferInit(VP8TBuffer* const b) {
278  (void)b;
279}
280void VP8TBufferClear(VP8TBuffer* const b) {
281  (void)b;
282}
283
284#endif    // !DISABLE_TOKEN_BUFFER
285
286