2
0

vpxstats.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  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. */
  10. #include "./vpxstats.h"
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "./tools_common.h"
  15. int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
  16. int res;
  17. stats->pass = pass;
  18. if (pass == 0) {
  19. stats->file = fopen(fpf, "wb");
  20. stats->buf.sz = 0;
  21. stats->buf.buf = NULL;
  22. res = (stats->file != NULL);
  23. } else {
  24. size_t nbytes;
  25. stats->file = fopen(fpf, "rb");
  26. if (stats->file == NULL) fatal("First-pass stats file does not exist!");
  27. if (fseek(stats->file, 0, SEEK_END))
  28. fatal("First-pass stats file must be seekable!");
  29. stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
  30. rewind(stats->file);
  31. stats->buf.buf = malloc(stats->buf_alloc_sz);
  32. if (!stats->buf.buf)
  33. fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
  34. (unsigned int)stats->buf_alloc_sz);
  35. nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
  36. res = (nbytes == stats->buf.sz);
  37. }
  38. return res;
  39. }
  40. int stats_open_mem(stats_io_t *stats, int pass) {
  41. int res;
  42. stats->pass = pass;
  43. if (!pass) {
  44. stats->buf.sz = 0;
  45. stats->buf_alloc_sz = 64 * 1024;
  46. stats->buf.buf = malloc(stats->buf_alloc_sz);
  47. }
  48. stats->buf_ptr = stats->buf.buf;
  49. res = (stats->buf.buf != NULL);
  50. return res;
  51. }
  52. void stats_close(stats_io_t *stats, int last_pass) {
  53. if (stats->file) {
  54. if (stats->pass == last_pass) {
  55. free(stats->buf.buf);
  56. }
  57. fclose(stats->file);
  58. stats->file = NULL;
  59. } else {
  60. if (stats->pass == last_pass) free(stats->buf.buf);
  61. }
  62. }
  63. void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
  64. if (stats->file) {
  65. (void)fwrite(pkt, 1, len, stats->file);
  66. } else {
  67. if (stats->buf.sz + len > stats->buf_alloc_sz) {
  68. size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
  69. char *new_ptr = realloc(stats->buf.buf, new_sz);
  70. if (new_ptr) {
  71. stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
  72. stats->buf.buf = new_ptr;
  73. stats->buf_alloc_sz = new_sz;
  74. } else {
  75. fatal("Failed to realloc firstpass stats buffer.");
  76. }
  77. }
  78. memcpy(stats->buf_ptr, pkt, len);
  79. stats->buf.sz += len;
  80. stats->buf_ptr += len;
  81. }
  82. }
  83. vpx_fixed_buf_t stats_get(stats_io_t *stats) { return stats->buf; }