blob: 0cfeea2f14387ca85652ca34712347f48b41614f [file] [log] [blame]
Tom Finegan78cb2e62013-11-07 21:28:45 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Tom Finegan78cb2e62013-11-07 21:28:45 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Tom Finegan78cb2e62013-11-07 21:28:45 -080010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomstats.h"
Tom Finegan78cb2e62013-11-07 21:28:45 -080013
14#include <math.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "./tools_common.h"
19
20int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
21 int res;
22 stats->pass = pass;
23
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 {
Tom Finegan78cb2e62013-11-07 21:28:45 -080030 size_t nbytes;
31
32 stats->file = fopen(fpf, "rb");
33
clang-format6c4d83e2016-08-08 19:03:30 -070034 if (stats->file == NULL) fatal("First-pass stats file does not exist!");
Adrian Grangef92c0b12015-01-30 10:42:29 -080035
Tom Finegan78cb2e62013-11-07 21:28:45 -080036 if (fseek(stats->file, 0, SEEK_END))
37 fatal("First-pass stats file must be seekable!");
38
39 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
40 rewind(stats->file);
41
42 stats->buf.buf = malloc(stats->buf_alloc_sz);
43
44 if (!stats->buf.buf)
45 fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
46 (unsigned int)stats->buf_alloc_sz);
47
48 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
49 res = (nbytes == stats->buf.sz);
Tom Finegan78cb2e62013-11-07 21:28:45 -080050 }
51
52 return res;
53}
54
55int stats_open_mem(stats_io_t *stats, int pass) {
56 int res;
57 stats->pass = pass;
58
59 if (!pass) {
60 stats->buf.sz = 0;
61 stats->buf_alloc_sz = 64 * 1024;
62 stats->buf.buf = malloc(stats->buf_alloc_sz);
63 }
64
65 stats->buf_ptr = stats->buf.buf;
66 res = (stats->buf.buf != NULL);
67 return res;
68}
69
70void stats_close(stats_io_t *stats, int last_pass) {
71 if (stats->file) {
72 if (stats->pass == last_pass) {
Tom Finegan78cb2e62013-11-07 21:28:45 -080073 free(stats->buf.buf);
Tom Finegan78cb2e62013-11-07 21:28:45 -080074 }
75
76 fclose(stats->file);
77 stats->file = NULL;
78 } else {
clang-format6c4d83e2016-08-08 19:03:30 -070079 if (stats->pass == last_pass) free(stats->buf.buf);
Tom Finegan78cb2e62013-11-07 21:28:45 -080080 }
81}
82
83void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
84 if (stats->file) {
clang-format6c4d83e2016-08-08 19:03:30 -070085 (void)fwrite(pkt, 1, len, stats->file);
Tom Finegan78cb2e62013-11-07 21:28:45 -080086 } else {
87 if (stats->buf.sz + len > stats->buf_alloc_sz) {
clang-format6c4d83e2016-08-08 19:03:30 -070088 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
89 char *new_ptr = realloc(stats->buf.buf, new_sz);
Tom Finegan78cb2e62013-11-07 21:28:45 -080090
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.");
97 }
98 }
99
100 memcpy(stats->buf_ptr, pkt, len);
101 stats->buf.sz += len;
102 stats->buf_ptr += len;
103 }
104}
105
Yaowu Xuf883b422016-08-30 14:01:10 -0700106aom_fixed_buf_t stats_get(stats_io_t *stats) { return stats->buf; }