Merge "Remove obsolete segment skip checks from tokenization."
[aom.git] / vpxstats.c
blob16728ce09637d33614457ae20b847133ca3ab8db
1 /*
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.
9 */
11 #include "./vpxstats.h"
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "./tools_common.h"
19 int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
20 int res;
21 stats->pass = pass;
23 if (pass == 0) {
24 stats->file = fopen(fpf, "wb");
25 stats->buf.sz = 0;
26 stats->buf.buf = NULL;
27 res = (stats->file != NULL);
28 } else {
29 size_t nbytes;
31 stats->file = fopen(fpf, "rb");
33 if (stats->file == NULL)
34 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)
80 free(stats->buf.buf);
84 void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
85 if (stats->file) {
86 (void) fwrite(pkt, 1, len, stats->file);
87 } else {
88 if (stats->buf.sz + len > stats->buf_alloc_sz) {
89 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
90 char *new_ptr = realloc(stats->buf.buf, new_sz);
92 if (new_ptr) {
93 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
94 stats->buf.buf = new_ptr;
95 stats->buf_alloc_sz = new_sz;
96 } else {
97 fatal("Failed to realloc firstpass stats buffer.");
101 memcpy(stats->buf_ptr, pkt, len);
102 stats->buf.sz += len;
103 stats->buf_ptr += len;
107 vpx_fixed_buf_t stats_get(stats_io_t *stats) {
108 return stats->buf;