2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "./vpxstats.h"
17 #include "./tools_common.h"
19 int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
24 stats->file = fopen(fpf, "wb");
26 stats->buf.buf = NULL;
27 res = (stats->file != NULL);
33 fd = open(fpf, O_RDONLY);
34 stats->file = fdopen(fd, "rb");
36 stats->buf.sz = stat_buf.st_size;
37 stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE, fd, 0);
38 res = (stats->buf.buf != NULL);
42 stats->file = fopen(fpf, "rb");
44 if (stats->file == NULL)
45 fatal("First-pass stats file does not exist!");
47 if (fseek(stats->file, 0, SEEK_END))
48 fatal("First-pass stats file must be seekable!");
50 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
53 stats->buf.buf = malloc(stats->buf_alloc_sz);
56 fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
57 (unsigned int)stats->buf_alloc_sz);
59 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
60 res = (nbytes == stats->buf.sz);
61 #endif /* USE_POSIX_MMAP */
67 int stats_open_mem(stats_io_t *stats, int pass) {
73 stats->buf_alloc_sz = 64 * 1024;
74 stats->buf.buf = malloc(stats->buf_alloc_sz);
77 stats->buf_ptr = stats->buf.buf;
78 res = (stats->buf.buf != NULL);
82 void stats_close(stats_io_t *stats, int last_pass) {
84 if (stats->pass == last_pass) {
86 munmap(stats->buf.buf, stats->buf.sz);
89 #endif /* USE_POSIX_MMAP */
95 if (stats->pass == last_pass)
100 void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
102 (void) fwrite(pkt, 1, len, stats->file);
104 if (stats->buf.sz + len > stats->buf_alloc_sz) {
105 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
106 char *new_ptr = realloc(stats->buf.buf, new_sz);
109 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
110 stats->buf.buf = new_ptr;
111 stats->buf_alloc_sz = new_sz;
113 fatal("Failed to realloc firstpass stats buffer.");
117 memcpy(stats->buf_ptr, pkt, len);
118 stats->buf.sz += len;
119 stats->buf_ptr += len;
123 vpx_fixed_buf_t stats_get(stats_io_t *stats) {