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"
18 #include "./tools_common.h"
20 int stats_open_file(stats_io_t
*stats
, const char *fpf
, int pass
) {
25 stats
->file
= fopen(fpf
, "wb");
27 stats
->buf
.buf
= NULL
;
28 res
= (stats
->file
!= NULL
);
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
);
42 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
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
);
55 int stats_open_mem(stats_io_t
*stats
, int pass
) {
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
);
70 void stats_close(stats_io_t
*stats
, int last_pass
) {
72 if (stats
->pass
== last_pass
) {
79 if (stats
->pass
== last_pass
) free(stats
->buf
.buf
);
83 void stats_write(stats_io_t
*stats
, const void *pkt
, size_t len
) {
85 (void)fwrite(pkt
, 1, len
, stats
->file
);
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
);
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
;
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
; }