Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #include "./aomstats.h" |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 13 | |
| 14 | #include <math.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #include "./tools_common.h" |
| 19 | |
| 20 | int 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 Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 30 | size_t nbytes; |
| 31 | |
| 32 | stats->file = fopen(fpf, "rb"); |
| 33 | |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 34 | if (stats->file == NULL) fatal("First-pass stats file does not exist!"); |
Adrian Grange | f92c0b1 | 2015-01-30 10:42:29 -0800 | [diff] [blame] | 35 | |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 36 | 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 Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | int 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 | |
| 70 | void stats_close(stats_io_t *stats, int last_pass) { |
| 71 | if (stats->file) { |
| 72 | if (stats->pass == last_pass) { |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 73 | free(stats->buf.buf); |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | fclose(stats->file); |
| 77 | stats->file = NULL; |
| 78 | } else { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 79 | if (stats->pass == last_pass) free(stats->buf.buf); |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | void stats_write(stats_io_t *stats, const void *pkt, size_t len) { |
| 84 | if (stats->file) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 85 | (void)fwrite(pkt, 1, len, stats->file); |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 86 | } else { |
| 87 | if (stats->buf.sz + len > stats->buf_alloc_sz) { |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 88 | size_t new_sz = stats->buf_alloc_sz + 64 * 1024; |
| 89 | char *new_ptr = realloc(stats->buf.buf, new_sz); |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 90 | |
| 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 Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 106 | aom_fixed_buf_t stats_get(stats_io_t *stats) { return stats->buf; } |