ref: ac5e269b9469002b640983ba3abfa37497fc4580
parent: bb9511684f70a735b3b909712666021e178c93e7
parent: 11de1b838135307e7c02f7fde82ccd35d9ae4cad
author: Yue Chen <yuec@google.com>
date: Tue Jun 18 19:39:09 EDT 2019
Merge "Fix timestamp overflow issues"
--- a/test/timestamp_test.cc
+++ b/test/timestamp_test.cc
@@ -75,6 +75,8 @@
}
};
+class TimestampTestVp9Only : public TimestampTest {};
+
// Tests encoding in millisecond timebase.
TEST_P(TimestampTest, EncodeFrames) {
DummyTimebaseVideoSource video(1, 1000);
@@ -90,13 +92,9 @@
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
}
-// TODO(fgalligan): Enable test when
-// https://bugs.chromium.org/p/webm/issues/detail?id=701 is fixed. Tests
-// rollover of int64_t value in libvpx encoder interface. The rollover happens
-// in the algorithm "(pts + duration) * 10000000 * ctx->cfg.g_timebase.num". In
-// this test the second frame will rollover an int64_t and the resulting
-// timestamp will become negative.
-TEST_P(TimestampTest, DISABLED_TestVpxRollover) {
+// TODO(webm:701): Enable VP8 test when the overflow issue in
+// TestVpxRollover is fixed.
+TEST_P(TimestampTestVp9Only, TestVpxRollover) {
DummyTimebaseVideoSource video(1, 1000);
video.set_starting_pts(922337170351ll);
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
@@ -106,5 +104,6 @@
::testing::Values(::libvpx_test::kTwoPassGood));
VP9_INSTANTIATE_TEST_CASE(TimestampTest,
::testing::Values(::libvpx_test::kTwoPassGood));
-
+VP9_INSTANTIATE_TEST_CASE(TimestampTestVp9Only,
+ ::testing::Values(::libvpx_test::kTwoPassGood));
} // namespace
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -57,6 +57,9 @@
#define VP8_TEMPORAL_ALT_REF !CONFIG_REALTIME_ONLY
+/* vp8 uses 10,000,000 ticks/second as time stamp */
+#define TICKS_PER_SEC 10000000
+
typedef struct {
int kf_indicated;
unsigned int frames_since_key;
--- a/vp8/vp8_cx_iface.c
+++ b/vp8/vp8_cx_iface.c
@@ -18,6 +18,7 @@
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/system_state.h"
#include "vpx_ports/vpx_once.h"
+#include "vpx_util/vpx_timestamp.h"
#include "vp8/encoder/onyx_int.h"
#include "vpx/vp8cx.h"
#include "vp8/encoder/firstpass.h"
@@ -75,6 +76,9 @@
vpx_codec_priv_t base;
vpx_codec_enc_cfg_t cfg;
struct vp8_extracfg vp8_cfg;
+ vpx_rational64_t timestamp_ratio;
+ vpx_codec_pts_t pts_offset;
+ unsigned char pts_offset_initialized;
VP8_CONFIG oxcf;
struct VP8_COMP *cpi;
unsigned char *cx_data;
@@ -127,6 +131,22 @@
if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
} while (0)
+#if defined(_MSC_VER)
+#define COMPILE_TIME_ASSERT(boolexp) \
+ do { \
+ char compile_time_assert[(boolexp) ? 1 : -1]; \
+ (void)compile_time_assert; \
+ } while (0)
+#else /* !_MSC_VER */
+#define COMPILE_TIME_ASSERT(boolexp) \
+ do { \
+ struct { \
+ unsigned int compile_time_assert : (boolexp) ? 1 : -1; \
+ } compile_time_assert; \
+ (void)compile_time_assert; \
+ } while (0)
+#endif /* _MSC_VER */
+
static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
const vpx_codec_enc_cfg_t *cfg,
const struct vp8_extracfg *vp8_cfg,
@@ -658,6 +678,12 @@
res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
if (!res) {
+ priv->pts_offset_initialized = 0;
+ priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
+ priv->timestamp_ratio.num = (int64_t)priv->cfg.g_timebase.num;
+ priv->timestamp_ratio.num *= TICKS_PER_SEC;
+ reduce_ratio(&priv->timestamp_ratio);
+
set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
priv->cpi = vp8_create_compressor(&priv->oxcf);
if (!priv->cpi) res = VPX_CODEC_MEM_ERROR;
@@ -722,13 +748,15 @@
new_qc = MODE_BESTQUALITY;
if (deadline) {
+ /* Convert duration parameter from stream timebase to microseconds */
uint64_t duration_us;
- /* Convert duration parameter from stream timebase to microseconds */
- duration_us = (uint64_t)duration * 1000000 *
- (uint64_t)ctx->cfg.g_timebase.num /
- (uint64_t)ctx->cfg.g_timebase.den;
+ COMPILE_TIME_ASSERT(TICKS_PER_SEC > 1000000 &&
+ (TICKS_PER_SEC % 1000000) == 0);
+ duration_us = duration * (uint64_t)ctx->timestamp_ratio.num /
+ (ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
+
/* If the deadline is more that the duration this frame is to be shown,
* use good quality mode. Otherwise use realtime mode.
*/
@@ -806,6 +834,7 @@
volatile vpx_codec_err_t res = VPX_CODEC_OK;
// Make a copy as volatile to avoid -Wclobbered with longjmp.
volatile vpx_enc_frame_flags_t flags = enc_flags;
+ volatile vpx_codec_pts_t pts_val = pts;
if (!ctx->cfg.rc_target_bitrate) {
#if CONFIG_MULTI_RES_ENCODING
@@ -826,6 +855,12 @@
if (!res) res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
+ if (!ctx->pts_offset_initialized) {
+ ctx->pts_offset = pts_val;
+ ctx->pts_offset_initialized = 1;
+ }
+ pts_val -= ctx->pts_offset;
+
pick_quickcompress_mode(ctx, duration, deadline);
vpx_codec_pkt_list_init(&ctx->pkt_list);
@@ -875,11 +910,10 @@
/* Convert API flags to internal codec lib flags */
lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
- /* vp8 use 10,000,000 ticks/second as time stamp */
dst_time_stamp =
- pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
- dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num /
- ctx->cfg.g_timebase.den;
+ pts_val * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
+ dst_end_time_stamp = (pts_val + duration) * ctx->timestamp_ratio.num /
+ ctx->timestamp_ratio.den;
if (img != NULL) {
res = image2yuvconfig(img, &sd);
@@ -918,15 +952,17 @@
VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
/* Add the frame packet to the list of returned packets. */
- round = (vpx_codec_pts_t)10000000 * ctx->cfg.g_timebase.num / 2 - 1;
+ round = (vpx_codec_pts_t)ctx->timestamp_ratio.num / 2;
+ if (round > 0) --round;
delta = (dst_end_time_stamp - dst_time_stamp);
pkt.kind = VPX_CODEC_CX_FRAME_PKT;
pkt.data.frame.pts =
- (dst_time_stamp * ctx->cfg.g_timebase.den + round) /
- ctx->cfg.g_timebase.num / 10000000;
+ (dst_time_stamp * ctx->timestamp_ratio.den + round) /
+ ctx->timestamp_ratio.num +
+ ctx->pts_offset;
pkt.data.frame.duration =
- (unsigned long)((delta * ctx->cfg.g_timebase.den + round) /
- ctx->cfg.g_timebase.num / 10000000);
+ (unsigned long)((delta * ctx->timestamp_ratio.den + round) /
+ ctx->timestamp_ratio.num);
pkt.data.frame.flags = lib_flags << 16;
pkt.data.frame.width[0] = cpi->common.Width;
pkt.data.frame.height[0] = cpi->common.Height;
@@ -945,9 +981,9 @@
* Invisible frames have no duration.
*/
pkt.data.frame.pts =
- ((cpi->last_time_stamp_seen * ctx->cfg.g_timebase.den + round) /
- ctx->cfg.g_timebase.num / 10000000) +
- 1;
+ ((cpi->last_time_stamp_seen * ctx->timestamp_ratio.den + round) /
+ ctx->timestamp_ratio.num) +
+ ctx->pts_offset + 1;
pkt.data.frame.duration = 0;
}
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -15,6 +15,7 @@
#include "vpx/vpx_encoder.h"
#include "vpx_ports/vpx_once.h"
#include "vpx_ports/system_state.h"
+#include "vpx_util/vpx_timestamp.h"
#include "vpx/internal/vpx_codec_internal.h"
#include "./vpx_version.h"
#include "vp9/encoder/vp9_encoder.h"
@@ -94,6 +95,9 @@
vpx_codec_priv_t base;
vpx_codec_enc_cfg_t cfg;
struct vp9_extracfg extra_cfg;
+ vpx_rational64_t timestamp_ratio;
+ vpx_codec_pts_t pts_offset;
+ unsigned char pts_offset_initialized;
VP9EncoderConfig oxcf;
VP9_COMP *cpi;
unsigned char *cx_data;
@@ -151,6 +155,22 @@
if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
} while (0)
+#if defined(_MSC_VER)
+#define COMPILE_TIME_ASSERT(boolexp) \
+ do { \
+ char compile_time_assert[(boolexp) ? 1 : -1]; \
+ (void)compile_time_assert; \
+ } while (0)
+#else // !_MSC_VER
+#define COMPILE_TIME_ASSERT(boolexp) \
+ do { \
+ struct { \
+ unsigned int compile_time_assert : (boolexp) ? 1 : -1; \
+ } compile_time_assert; \
+ (void)compile_time_assert; \
+ } while (0)
+#endif // _MSC_VER
+
static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
const vpx_codec_enc_cfg_t *cfg,
const struct vp9_extracfg *extra_cfg) {
@@ -914,6 +934,12 @@
res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
if (res == VPX_CODEC_OK) {
+ priv->pts_offset_initialized = 0;
+ priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
+ priv->timestamp_ratio.num = (int64_t)priv->cfg.g_timebase.num;
+ priv->timestamp_ratio.num *= TICKS_PER_SEC;
+ reduce_ratio(&priv->timestamp_ratio);
+
set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
#if CONFIG_VP9_HIGHBITDEPTH
priv->oxcf.use_highbitdepth =
@@ -950,13 +976,15 @@
switch (ctx->cfg.g_pass) {
case VPX_RC_ONE_PASS:
if (deadline > 0) {
- const vpx_codec_enc_cfg_t *const cfg = &ctx->cfg;
-
// Convert duration parameter from stream timebase to microseconds.
- const uint64_t duration_us = (uint64_t)duration * 1000000 *
- (uint64_t)cfg->g_timebase.num /
- (uint64_t)cfg->g_timebase.den;
+ uint64_t duration_us;
+ COMPILE_TIME_ASSERT(TICKS_PER_SEC > 1000000 &&
+ (TICKS_PER_SEC % 1000000) == 0);
+
+ duration_us = duration * (uint64_t)ctx->timestamp_ratio.num /
+ (ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
+
// If the deadline is more that the duration this frame is to be shown,
// use good quality mode. Otherwise use realtime mode.
new_mode = (deadline > duration_us) ? GOOD : REALTIME;
@@ -1039,15 +1067,16 @@
return index_sz;
}
-static int64_t timebase_units_to_ticks(const vpx_rational_t *timebase,
+static int64_t timebase_units_to_ticks(const vpx_rational64_t *timestamp_ratio,
int64_t n) {
- return n * TICKS_PER_SEC * timebase->num / timebase->den;
+ return n * timestamp_ratio->num / timestamp_ratio->den;
}
-static int64_t ticks_to_timebase_units(const vpx_rational_t *timebase,
+static int64_t ticks_to_timebase_units(const vpx_rational64_t *timestamp_ratio,
int64_t n) {
- const int64_t round = (int64_t)TICKS_PER_SEC * timebase->num / 2 - 1;
- return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
+ int64_t round = timestamp_ratio->num / 2;
+ if (round > 0) --round;
+ return (n * timestamp_ratio->den + round) / timestamp_ratio->num;
}
static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
@@ -1077,7 +1106,7 @@
volatile vpx_codec_err_t res = VPX_CODEC_OK;
volatile vpx_enc_frame_flags_t flags = enc_flags;
VP9_COMP *const cpi = ctx->cpi;
- const vpx_rational_t *const timebase = &ctx->cfg.g_timebase;
+ const vpx_rational64_t *const timestamp_ratio = &ctx->timestamp_ratio;
size_t data_sz;
if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
@@ -1111,6 +1140,12 @@
}
}
+ if (!ctx->pts_offset_initialized) {
+ ctx->pts_offset = pts;
+ ctx->pts_offset_initialized = 1;
+ }
+ pts -= ctx->pts_offset;
+
pick_quickcompress_mode(ctx, duration, deadline);
vpx_codec_pkt_list_init(&ctx->pkt_list);
@@ -1143,13 +1178,13 @@
if (res == VPX_CODEC_OK) {
unsigned int lib_flags = 0;
YV12_BUFFER_CONFIG sd;
- int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
+ int64_t dst_time_stamp = timebase_units_to_ticks(timestamp_ratio, pts);
int64_t dst_end_time_stamp =
- timebase_units_to_ticks(timebase, pts + duration);
+ timebase_units_to_ticks(timestamp_ratio, pts + duration);
size_t size, cx_data_sz;
unsigned char *cx_data;
- cpi->svc.timebase_fac = timebase_units_to_ticks(timebase, 1);
+ cpi->svc.timebase_fac = timebase_units_to_ticks(timestamp_ratio, 1);
cpi->svc.time_stamp_superframe = dst_time_stamp;
// Set up internal flags
@@ -1212,9 +1247,10 @@
if (ctx->output_cx_pkt_cb.output_cx_pkt) {
pkt.kind = VPX_CODEC_CX_FRAME_PKT;
pkt.data.frame.pts =
- ticks_to_timebase_units(timebase, dst_time_stamp);
+ ticks_to_timebase_units(timestamp_ratio, dst_time_stamp) +
+ ctx->pts_offset;
pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
- timebase, dst_end_time_stamp - dst_time_stamp);
+ timestamp_ratio, dst_end_time_stamp - dst_time_stamp);
pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
pkt.data.frame.buf = ctx->pending_cx_data;
pkt.data.frame.sz = size;
@@ -1230,9 +1266,11 @@
// Add the frame packet to the list of returned packets.
pkt.kind = VPX_CODEC_CX_FRAME_PKT;
- pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
+ pkt.data.frame.pts =
+ ticks_to_timebase_units(timestamp_ratio, dst_time_stamp) +
+ ctx->pts_offset;
pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
- timebase, dst_end_time_stamp - dst_time_stamp);
+ timestamp_ratio, dst_end_time_stamp - dst_time_stamp);
pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
pkt.data.frame.height[cpi->svc.spatial_layer_id] = cpi->common.height;
--- /dev/null
+++ b/vpx_util/vpx_timestamp.h
@@ -1,0 +1,45 @@
+/*
+ * Copyright (c) 2019 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VPX_VPX_UTIL_VPX_TIMESTAMP_H_
+#define VPX_VPX_UTIL_VPX_TIMESTAMP_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+// Rational Number with an int64 numerator
+typedef struct vpx_rational64 {
+ int64_t num; // fraction numerator
+ int den; // fraction denominator
+} vpx_rational64_t; // alias for struct vpx_rational64_t
+
+static INLINE int gcd(int64_t a, int b) {
+ int r; // remainder
+ while (b > 0) {
+ r = (int)(a % b);
+ a = b;
+ b = r;
+ }
+
+ return (int)a;
+}
+
+static INLINE void reduce_ratio(vpx_rational64_t *ratio) {
+ const int denom = gcd(ratio->num, ratio->den);
+ ratio->num /= denom;
+ ratio->den /= denom;
+}
+
+#ifdef __cplusplus
+} // extern "C"
+#endif // __cplusplus
+
+#endif // VPX_VPX_UTIL_VPX_TIMESTAMP_H_
--- a/vpx_util/vpx_util.mk
+++ b/vpx_util/vpx_util.mk
@@ -15,5 +15,6 @@
UTIL_SRCS-yes += endian_inl.h
UTIL_SRCS-yes += vpx_write_yuv_frame.h
UTIL_SRCS-yes += vpx_write_yuv_frame.c
+UTIL_SRCS-yes += vpx_timestamp.h
UTIL_SRCS-$(or $(CONFIG_BITSTREAM_DEBUG),$(CONFIG_MISMATCH_DEBUG)) += vpx_debug_util.h
UTIL_SRCS-$(or $(CONFIG_BITSTREAM_DEBUG),$(CONFIG_MISMATCH_DEBUG)) += vpx_debug_util.c