Improved aom_smooth_predictor_16x 32,16,8
[aom.git] / aomstats.c
blob0cfeea2f14387ca85652ca34712347f48b41614f
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #include "./aomstats.h"
14 #include <math.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "./tools_common.h"
20 int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
21 int res;
22 stats->pass = pass;
24 if (pass == 0) {
25 stats->file = fopen(fpf, "wb");
26 stats->buf.sz = 0;
27 stats->buf.buf = NULL;
28 res = (stats->file != NULL);
29 } else {
30 size_t nbytes;
32 stats->file = fopen(fpf, "rb");
34 if (stats->file == NULL) fatal("First-pass stats file does not exist!");
36 if (fseek(stats->file, 0, SEEK_END))
37 fatal("First-pass stats file must be seekable!");
39 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
40 rewind(stats->file);
42 stats->buf.buf = malloc(stats->buf_alloc_sz);
44 if (!stats->buf.buf)
45 fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
46 (unsigned int)stats->buf_alloc_sz);
48 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
49 res = (nbytes == stats->buf.sz);
52 return res;
55 int stats_open_mem(stats_io_t *stats, int pass) {
56 int res;
57 stats->pass = pass;
59 if (!pass) {
60 stats->buf.sz = 0;
61 stats->buf_alloc_sz = 64 * 1024;
62 stats->buf.buf = malloc(stats->buf_alloc_sz);
65 stats->buf_ptr = stats->buf.buf;
66 res = (stats->buf.buf != NULL);
67 return res;
70 void stats_close(stats_io_t *stats, int last_pass) {
71 if (stats->file) {
72 if (stats->pass == last_pass) {
73 free(stats->buf.buf);
76 fclose(stats->file);
77 stats->file = NULL;
78 } else {
79 if (stats->pass == last_pass) free(stats->buf.buf);
83 void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
84 if (stats->file) {
85 (void)fwrite(pkt, 1, len, stats->file);
86 } else {
87 if (stats->buf.sz + len > stats->buf_alloc_sz) {
88 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
89 char *new_ptr = realloc(stats->buf.buf, new_sz);
91 if (new_ptr) {
92 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
93 stats->buf.buf = new_ptr;
94 stats->buf_alloc_sz = new_sz;
95 } else {
96 fatal("Failed to realloc firstpass stats buffer.");
100 memcpy(stats->buf_ptr, pkt, len);
101 stats->buf.sz += len;
102 stats->buf_ptr += len;
106 aom_fixed_buf_t stats_get(stats_io_t *stats) { return stats->buf; }