xref: /aosp_15_r20/external/libaom/stats/aomstats.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 "stats/aomstats.h"
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
stats_open_file(stats_io_t * stats,const char * fpf,int pass)22*77c1e3ccSAndroid Build Coastguard Worker int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
23*77c1e3ccSAndroid Build Coastguard Worker   int res;
24*77c1e3ccSAndroid Build Coastguard Worker   stats->pass = pass;
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker   if (pass == 0) {
27*77c1e3ccSAndroid Build Coastguard Worker     stats->file = fopen(fpf, "wb");
28*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.sz = 0;
29*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.buf = NULL;
30*77c1e3ccSAndroid Build Coastguard Worker     res = (stats->file != NULL);
31*77c1e3ccSAndroid Build Coastguard Worker   } else {
32*77c1e3ccSAndroid Build Coastguard Worker     size_t nbytes;
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker     stats->file = fopen(fpf, "rb");
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker     if (stats->file == NULL) fatal("First-pass stats file does not exist!");
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker     if (fseek(stats->file, 0, SEEK_END))
39*77c1e3ccSAndroid Build Coastguard Worker       fatal("First-pass stats file must be seekable!");
40*77c1e3ccSAndroid Build Coastguard Worker 
41*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
42*77c1e3ccSAndroid Build Coastguard Worker     rewind(stats->file);
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.buf = malloc(stats->buf_alloc_sz);
45*77c1e3ccSAndroid Build Coastguard Worker 
46*77c1e3ccSAndroid Build Coastguard Worker     if (!stats->buf.buf)
47*77c1e3ccSAndroid Build Coastguard Worker       fatal("Failed to allocate first-pass stats buffer (%u bytes)",
48*77c1e3ccSAndroid Build Coastguard Worker             (unsigned int)stats->buf_alloc_sz);
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker     nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
51*77c1e3ccSAndroid Build Coastguard Worker     res = (nbytes == stats->buf.sz);
52*77c1e3ccSAndroid Build Coastguard Worker   }
53*77c1e3ccSAndroid Build Coastguard Worker 
54*77c1e3ccSAndroid Build Coastguard Worker   return res;
55*77c1e3ccSAndroid Build Coastguard Worker }
56*77c1e3ccSAndroid Build Coastguard Worker 
stats_open_mem(stats_io_t * stats,int pass)57*77c1e3ccSAndroid Build Coastguard Worker int stats_open_mem(stats_io_t *stats, int pass) {
58*77c1e3ccSAndroid Build Coastguard Worker   int res;
59*77c1e3ccSAndroid Build Coastguard Worker   stats->pass = pass;
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker   if (!pass) {
62*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.sz = 0;
63*77c1e3ccSAndroid Build Coastguard Worker     stats->buf_alloc_sz = 64 * 1024;
64*77c1e3ccSAndroid Build Coastguard Worker     stats->buf.buf = malloc(stats->buf_alloc_sz);
65*77c1e3ccSAndroid Build Coastguard Worker   }
66*77c1e3ccSAndroid Build Coastguard Worker 
67*77c1e3ccSAndroid Build Coastguard Worker   stats->buf_ptr = stats->buf.buf;
68*77c1e3ccSAndroid Build Coastguard Worker   res = (stats->buf.buf != NULL);
69*77c1e3ccSAndroid Build Coastguard Worker   return res;
70*77c1e3ccSAndroid Build Coastguard Worker }
71*77c1e3ccSAndroid Build Coastguard Worker 
stats_close(stats_io_t * stats,int last_pass)72*77c1e3ccSAndroid Build Coastguard Worker void stats_close(stats_io_t *stats, int last_pass) {
73*77c1e3ccSAndroid Build Coastguard Worker   if (stats->file) {
74*77c1e3ccSAndroid Build Coastguard Worker     if (stats->pass == last_pass) {
75*77c1e3ccSAndroid Build Coastguard Worker       free(stats->buf.buf);
76*77c1e3ccSAndroid Build Coastguard Worker     }
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker     fclose(stats->file);
79*77c1e3ccSAndroid Build Coastguard Worker     stats->file = NULL;
80*77c1e3ccSAndroid Build Coastguard Worker   } else {
81*77c1e3ccSAndroid Build Coastguard Worker     if (stats->pass == last_pass) free(stats->buf.buf);
82*77c1e3ccSAndroid Build Coastguard Worker   }
83*77c1e3ccSAndroid Build Coastguard Worker }
84*77c1e3ccSAndroid Build Coastguard Worker 
stats_write(stats_io_t * stats,const void * pkt,size_t len)85*77c1e3ccSAndroid Build Coastguard Worker void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
86*77c1e3ccSAndroid Build Coastguard Worker   if (stats->file) {
87*77c1e3ccSAndroid Build Coastguard Worker     (void)fwrite(pkt, 1, len, stats->file);
88*77c1e3ccSAndroid Build Coastguard Worker     return;
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker   assert(stats->buf.sz <= stats->buf_alloc_sz);
91*77c1e3ccSAndroid Build Coastguard Worker   assert(0 < stats->buf_alloc_sz);
92*77c1e3ccSAndroid Build Coastguard Worker   if (stats->buf.sz + len > stats->buf_alloc_sz) {
93*77c1e3ccSAndroid Build Coastguard Worker     // Grow by a factor of 1.5 each time, for amortized constant time.
94*77c1e3ccSAndroid Build Coastguard Worker     // Also make sure there is enough room for the data.
95*77c1e3ccSAndroid Build Coastguard Worker     size_t new_sz = AOMMAX((3 * stats->buf_alloc_sz) / 2, stats->buf.sz + len);
96*77c1e3ccSAndroid Build Coastguard Worker     char *new_ptr = realloc(stats->buf.buf, new_sz);
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker     if (new_ptr) {
99*77c1e3ccSAndroid Build Coastguard Worker       stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
100*77c1e3ccSAndroid Build Coastguard Worker       stats->buf.buf = new_ptr;
101*77c1e3ccSAndroid Build Coastguard Worker       stats->buf_alloc_sz = new_sz;
102*77c1e3ccSAndroid Build Coastguard Worker     } else {
103*77c1e3ccSAndroid Build Coastguard Worker       fatal("Failed to realloc firstpass stats buffer.");
104*77c1e3ccSAndroid Build Coastguard Worker     }
105*77c1e3ccSAndroid Build Coastguard Worker   }
106*77c1e3ccSAndroid Build Coastguard Worker 
107*77c1e3ccSAndroid Build Coastguard Worker   memcpy(stats->buf_ptr, pkt, len);
108*77c1e3ccSAndroid Build Coastguard Worker   stats->buf.sz += len;
109*77c1e3ccSAndroid Build Coastguard Worker   stats->buf_ptr += len;
110*77c1e3ccSAndroid Build Coastguard Worker }
111*77c1e3ccSAndroid Build Coastguard Worker 
stats_get(stats_io_t * stats)112*77c1e3ccSAndroid Build Coastguard Worker aom_fixed_buf_t stats_get(stats_io_t *stats) { return stats->buf; }
113