timestamp.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * timestamp utils, mostly useful for debugging/logging purposes
  21. */
  22. #ifndef AVUTIL_TIMESTAMP_H
  23. #define AVUTIL_TIMESTAMP_H
  24. #include "common.h"
  25. #define AV_TS_MAX_STRING_SIZE 32
  26. /**
  27. * Fill the provided buffer with a string containing a timestamp
  28. * representation.
  29. *
  30. * @param buf a buffer with size in bytes of at least AV_TS_MAX_STRING_SIZE
  31. * @param ts the timestamp to represent
  32. * @return the buffer in input
  33. */
  34. static inline char *av_ts_make_string(char *buf, int64_t ts)
  35. {
  36. if (ts == AV_NOPTS_VALUE) snprintf(buf, AV_TS_MAX_STRING_SIZE, "NOPTS");
  37. else snprintf(buf, AV_TS_MAX_STRING_SIZE, "%"PRId64"", ts);
  38. return buf;
  39. }
  40. /**
  41. * Convenience macro, the return value should be used only directly in
  42. * function arguments but never stand-alone.
  43. */
  44. #define av_ts2str(ts) av_ts_make_string((char[AV_TS_MAX_STRING_SIZE]){0}, ts)
  45. /**
  46. * Fill the provided buffer with a string containing a timestamp time
  47. * representation.
  48. *
  49. * @param buf a buffer with size in bytes of at least AV_TS_MAX_STRING_SIZE
  50. * @param ts the timestamp to represent
  51. * @param tb the timebase of the timestamp
  52. * @return the buffer in input
  53. */
  54. static inline char *av_ts_make_time_string(char *buf, int64_t ts, AVRational *tb)
  55. {
  56. if (ts == AV_NOPTS_VALUE) snprintf(buf, AV_TS_MAX_STRING_SIZE, "NOPTS");
  57. else snprintf(buf, AV_TS_MAX_STRING_SIZE, "%.6g", av_q2d(*tb) * ts);
  58. return buf;
  59. }
  60. /**
  61. * Convenience macro, the return value should be used only directly in
  62. * function arguments but never stand-alone.
  63. */
  64. #define av_ts2timestr(ts, tb) av_ts_make_time_string((char[AV_TS_MAX_STRING_SIZE]){0}, ts, tb)
  65. #endif /* AVUTIL_TIMESTAMP_H */