xref: /aosp_15_r20/external/libaom/av1/decoder/accounting.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/decoder/accounting.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
accounting_hash(const char * str)20*77c1e3ccSAndroid Build Coastguard Worker static int accounting_hash(const char *str) {
21*77c1e3ccSAndroid Build Coastguard Worker   uint32_t val;
22*77c1e3ccSAndroid Build Coastguard Worker   const unsigned char *ustr;
23*77c1e3ccSAndroid Build Coastguard Worker   val = 0;
24*77c1e3ccSAndroid Build Coastguard Worker   ustr = (const unsigned char *)str;
25*77c1e3ccSAndroid Build Coastguard Worker   /* This is about the worst hash one can design, but it should be good enough
26*77c1e3ccSAndroid Build Coastguard Worker      here. */
27*77c1e3ccSAndroid Build Coastguard Worker   while (*ustr) val += *ustr++;
28*77c1e3ccSAndroid Build Coastguard Worker   return val % AOM_ACCOUNTING_HASH_SIZE;
29*77c1e3ccSAndroid Build Coastguard Worker }
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker /* Dictionary lookup based on an open-addressing hash table. */
aom_accounting_dictionary_lookup(Accounting * accounting,const char * str)32*77c1e3ccSAndroid Build Coastguard Worker int aom_accounting_dictionary_lookup(Accounting *accounting, const char *str) {
33*77c1e3ccSAndroid Build Coastguard Worker   int hash;
34*77c1e3ccSAndroid Build Coastguard Worker   size_t len;
35*77c1e3ccSAndroid Build Coastguard Worker   AccountingDictionary *dictionary;
36*77c1e3ccSAndroid Build Coastguard Worker   dictionary = &accounting->syms.dictionary;
37*77c1e3ccSAndroid Build Coastguard Worker   hash = accounting_hash(str);
38*77c1e3ccSAndroid Build Coastguard Worker   while (accounting->hash_dictionary[hash] != -1) {
39*77c1e3ccSAndroid Build Coastguard Worker     if (strcmp(dictionary->strs[accounting->hash_dictionary[hash]], str) == 0) {
40*77c1e3ccSAndroid Build Coastguard Worker       return accounting->hash_dictionary[hash];
41*77c1e3ccSAndroid Build Coastguard Worker     }
42*77c1e3ccSAndroid Build Coastguard Worker     hash++;
43*77c1e3ccSAndroid Build Coastguard Worker     if (hash == AOM_ACCOUNTING_HASH_SIZE) hash = 0;
44*77c1e3ccSAndroid Build Coastguard Worker   }
45*77c1e3ccSAndroid Build Coastguard Worker   /* No match found. */
46*77c1e3ccSAndroid Build Coastguard Worker   assert(dictionary->num_strs + 1 < MAX_SYMBOL_TYPES);
47*77c1e3ccSAndroid Build Coastguard Worker   accounting->hash_dictionary[hash] = dictionary->num_strs;
48*77c1e3ccSAndroid Build Coastguard Worker   len = strlen(str);
49*77c1e3ccSAndroid Build Coastguard Worker   dictionary->strs[dictionary->num_strs] = malloc(len + 1);
50*77c1e3ccSAndroid Build Coastguard Worker   if (!dictionary->strs[dictionary->num_strs]) abort();
51*77c1e3ccSAndroid Build Coastguard Worker   snprintf(dictionary->strs[dictionary->num_strs], len + 1, "%s", str);
52*77c1e3ccSAndroid Build Coastguard Worker   dictionary->num_strs++;
53*77c1e3ccSAndroid Build Coastguard Worker   return dictionary->num_strs - 1;
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_init(Accounting * accounting)56*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_init(Accounting *accounting) {
57*77c1e3ccSAndroid Build Coastguard Worker   int i;
58*77c1e3ccSAndroid Build Coastguard Worker   accounting->num_syms_allocated = 1000;
59*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.syms =
60*77c1e3ccSAndroid Build Coastguard Worker       malloc(sizeof(AccountingSymbol) * accounting->num_syms_allocated);
61*77c1e3ccSAndroid Build Coastguard Worker   if (!accounting->syms.syms) abort();
62*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.dictionary.num_strs = 0;
63*77c1e3ccSAndroid Build Coastguard Worker   assert(AOM_ACCOUNTING_HASH_SIZE > 2 * MAX_SYMBOL_TYPES);
64*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < AOM_ACCOUNTING_HASH_SIZE; i++)
65*77c1e3ccSAndroid Build Coastguard Worker     accounting->hash_dictionary[i] = -1;
66*77c1e3ccSAndroid Build Coastguard Worker   aom_accounting_reset(accounting);
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_reset(Accounting * accounting)69*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_reset(Accounting *accounting) {
70*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.num_syms = 0;
71*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.num_binary_syms = 0;
72*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.num_multi_syms = 0;
73*77c1e3ccSAndroid Build Coastguard Worker   accounting->context.x = -1;
74*77c1e3ccSAndroid Build Coastguard Worker   accounting->context.y = -1;
75*77c1e3ccSAndroid Build Coastguard Worker   accounting->last_tell_frac = 0;
76*77c1e3ccSAndroid Build Coastguard Worker }
77*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_clear(Accounting * accounting)78*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_clear(Accounting *accounting) {
79*77c1e3ccSAndroid Build Coastguard Worker   int i;
80*77c1e3ccSAndroid Build Coastguard Worker   AccountingDictionary *dictionary;
81*77c1e3ccSAndroid Build Coastguard Worker   free(accounting->syms.syms);
82*77c1e3ccSAndroid Build Coastguard Worker   dictionary = &accounting->syms.dictionary;
83*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < dictionary->num_strs; i++) {
84*77c1e3ccSAndroid Build Coastguard Worker     free(dictionary->strs[i]);
85*77c1e3ccSAndroid Build Coastguard Worker   }
86*77c1e3ccSAndroid Build Coastguard Worker }
87*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_set_context(Accounting * accounting,int16_t x,int16_t y)88*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_set_context(Accounting *accounting, int16_t x, int16_t y) {
89*77c1e3ccSAndroid Build Coastguard Worker   accounting->context.x = x;
90*77c1e3ccSAndroid Build Coastguard Worker   accounting->context.y = y;
91*77c1e3ccSAndroid Build Coastguard Worker }
92*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_record(Accounting * accounting,const char * str,uint32_t bits)93*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_record(Accounting *accounting, const char *str,
94*77c1e3ccSAndroid Build Coastguard Worker                            uint32_t bits) {
95*77c1e3ccSAndroid Build Coastguard Worker   AccountingSymbol sym;
96*77c1e3ccSAndroid Build Coastguard Worker   // Reuse previous symbol if it has the same context and symbol id.
97*77c1e3ccSAndroid Build Coastguard Worker   if (accounting->syms.num_syms) {
98*77c1e3ccSAndroid Build Coastguard Worker     AccountingSymbol *last_sym;
99*77c1e3ccSAndroid Build Coastguard Worker     last_sym = &accounting->syms.syms[accounting->syms.num_syms - 1];
100*77c1e3ccSAndroid Build Coastguard Worker     if (memcmp(&last_sym->context, &accounting->context,
101*77c1e3ccSAndroid Build Coastguard Worker                sizeof(AccountingSymbolContext)) == 0) {
102*77c1e3ccSAndroid Build Coastguard Worker       uint32_t id;
103*77c1e3ccSAndroid Build Coastguard Worker       id = aom_accounting_dictionary_lookup(accounting, str);
104*77c1e3ccSAndroid Build Coastguard Worker       if (id == last_sym->id) {
105*77c1e3ccSAndroid Build Coastguard Worker         last_sym->bits += bits;
106*77c1e3ccSAndroid Build Coastguard Worker         last_sym->samples++;
107*77c1e3ccSAndroid Build Coastguard Worker         return;
108*77c1e3ccSAndroid Build Coastguard Worker       }
109*77c1e3ccSAndroid Build Coastguard Worker     }
110*77c1e3ccSAndroid Build Coastguard Worker   }
111*77c1e3ccSAndroid Build Coastguard Worker   sym.context = accounting->context;
112*77c1e3ccSAndroid Build Coastguard Worker   sym.samples = 1;
113*77c1e3ccSAndroid Build Coastguard Worker   sym.bits = bits;
114*77c1e3ccSAndroid Build Coastguard Worker   sym.id = aom_accounting_dictionary_lookup(accounting, str);
115*77c1e3ccSAndroid Build Coastguard Worker   assert(sym.id <= 255);
116*77c1e3ccSAndroid Build Coastguard Worker   if (accounting->syms.num_syms == accounting->num_syms_allocated) {
117*77c1e3ccSAndroid Build Coastguard Worker     accounting->num_syms_allocated *= 2;
118*77c1e3ccSAndroid Build Coastguard Worker     accounting->syms.syms =
119*77c1e3ccSAndroid Build Coastguard Worker         realloc(accounting->syms.syms,
120*77c1e3ccSAndroid Build Coastguard Worker                 sizeof(AccountingSymbol) * accounting->num_syms_allocated);
121*77c1e3ccSAndroid Build Coastguard Worker     if (!accounting->syms.syms) abort();
122*77c1e3ccSAndroid Build Coastguard Worker   }
123*77c1e3ccSAndroid Build Coastguard Worker   accounting->syms.syms[accounting->syms.num_syms++] = sym;
124*77c1e3ccSAndroid Build Coastguard Worker }
125*77c1e3ccSAndroid Build Coastguard Worker 
aom_accounting_dump(Accounting * accounting)126*77c1e3ccSAndroid Build Coastguard Worker void aom_accounting_dump(Accounting *accounting) {
127*77c1e3ccSAndroid Build Coastguard Worker   int i;
128*77c1e3ccSAndroid Build Coastguard Worker   AccountingSymbol *sym;
129*77c1e3ccSAndroid Build Coastguard Worker   printf("\n----- Number of recorded syntax elements = %d -----\n",
130*77c1e3ccSAndroid Build Coastguard Worker          accounting->syms.num_syms);
131*77c1e3ccSAndroid Build Coastguard Worker   printf("----- Total number of symbol calls = %d (%d binary) -----\n",
132*77c1e3ccSAndroid Build Coastguard Worker          accounting->syms.num_multi_syms + accounting->syms.num_binary_syms,
133*77c1e3ccSAndroid Build Coastguard Worker          accounting->syms.num_binary_syms);
134*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < accounting->syms.num_syms; i++) {
135*77c1e3ccSAndroid Build Coastguard Worker     sym = &accounting->syms.syms[i];
136*77c1e3ccSAndroid Build Coastguard Worker     printf("%s x: %d, y: %d bits: %f samples: %d\n",
137*77c1e3ccSAndroid Build Coastguard Worker            accounting->syms.dictionary.strs[sym->id], sym->context.x,
138*77c1e3ccSAndroid Build Coastguard Worker            sym->context.y, (float)sym->bits / 8.0, sym->samples);
139*77c1e3ccSAndroid Build Coastguard Worker   }
140*77c1e3ccSAndroid Build Coastguard Worker }
141