From 39cd6d6981c5f5ac976ecdb4d699ad783d95a3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Thu, 24 Jun 2010 06:43:26 +0000 Subject: [PATCH] Use a simpler, but sufficient, timescaler --- src/dvr/mkmux.c | 10 +++------ src/htsp.c | 9 +++----- src/tvhead.h | 11 +++++---- src/utils.c | 60 ------------------------------------------------- 4 files changed, 11 insertions(+), 79 deletions(-) diff --git a/src/dvr/mkmux.c b/src/dvr/mkmux.c index 94f5931c..8234e479 100644 --- a/src/dvr/mkmux.c +++ b/src/dvr/mkmux.c @@ -36,10 +36,6 @@ TAILQ_HEAD(mk_cue_queue, mk_cue); #define MATROSKA_TIMESCALE 1000000 // in nS -static const Rational mpeg_tc = {1, 90000}; -static const Rational mkv_tc = {1, 1000}; -static const Rational ns_tc = {1, 1000000000}; - /** * */ @@ -237,7 +233,7 @@ mk_build_tracks(mk_mux_t *mkm, const struct streaming_start *ss) } if(ssc->ssc_frameduration) { - int d = rescale_q(ssc->ssc_frameduration, mpeg_tc, ns_tc); + int d = ts_rescale(ssc->ssc_frameduration, 1000000000); ebml_append_uint(t, 0x23e383, d); } @@ -580,8 +576,8 @@ mk_write_frame_i(mk_mux_t *mkm, mk_track *t, th_pkt_t *pkt) if(pts != PTS_UNSET) { t->nextpts = pts + (pkt->pkt_duration >> pkt->pkt_field); - nxt = rescale_q(t->nextpts, mpeg_tc, mkv_tc); - pts = rescale_q(pts, mpeg_tc, mkv_tc); + nxt = ts_rescale(t->nextpts, 1000000000 / MATROSKA_TIMESCALE); + pts = ts_rescale(pts, 1000000000 / MATROSKA_TIMESCALE); if(mkm->totduration < nxt) mkm->totduration = nxt; diff --git a/src/htsp.c b/src/htsp.c index fbeda54c..54008c6b 100644 --- a/src/htsp.c +++ b/src/htsp.c @@ -1372,9 +1372,6 @@ const static char frametypearray[PKT_NTYPES] = { [PKT_B_FRAME] = 'B', }; -const static Rational mpeg_tc = {1, 90000}; -const static Rational htsp_tc = {1, 1000000}; - /** * Build a htsmsg from a th_pkt and enqueue it on our HTSP transport */ @@ -1407,16 +1404,16 @@ htsp_stream_deliver(htsp_subscription_t *hs, th_pkt_t *pkt) if(pkt->pkt_pts != PTS_UNSET) { - int64_t pts = rescale_q(pkt->pkt_pts, mpeg_tc, htsp_tc); + int64_t pts = ts_rescale(pkt->pkt_pts, 1000000); htsmsg_add_s64(m, "pts", pts); } if(pkt->pkt_dts != PTS_UNSET) { - int64_t dts = rescale_q(pkt->pkt_dts, mpeg_tc, htsp_tc); + int64_t dts = ts_rescale(pkt->pkt_dts, 1000000); htsmsg_add_s64(m, "dts", dts); } - uint32_t dur = rescale_q(pkt->pkt_duration, mpeg_tc, htsp_tc); + uint32_t dur = ts_rescale(pkt->pkt_duration, 1000000); htsmsg_add_u32(m, "duration", dur); pkt = pkt_merge_global(pkt); diff --git a/src/tvhead.h b/src/tvhead.h index fe28e9d5..efb46f38 100644 --- a/src/tvhead.h +++ b/src/tvhead.h @@ -842,11 +842,10 @@ int base64_decode(uint8_t *out, const char *in, int out_size); int put_utf8(char *out, int c); -typedef struct Rational{ - int num; ///< numerator - int den; ///< denominator -} Rational; - -int64_t rescale_q(int64_t a, Rational bq, Rational cq); +static inline int64_t ts_rescale(int64_t ts, int tb) +{ + // return (ts * tb + (tb / 2)) / 90000LL; + return (ts * tb ) / 90000LL; +} #endif /* TV_HEAD_H */ diff --git a/src/utils.c b/src/utils.c index 33e8b466..50788217 100644 --- a/src/utils.c +++ b/src/utils.c @@ -210,63 +210,3 @@ put_utf8(char *out, int c) *out++ = 0x80 | (0x3f & c); return 6; } - - - -enum AVRounding { - AV_ROUND_ZERO = 0, ///< Round toward zero. - AV_ROUND_INF = 1, ///< Round away from zero. - AV_ROUND_DOWN = 2, ///< Round toward -infinity. - AV_ROUND_UP = 3, ///< Round toward +infinity. - AV_ROUND_NEAR_INF = 5, ///< Round to nearest and halfway cases away from zero. -}; - - -static int64_t rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){ - int64_t r=0; - assert(c > 0); - assert(b >=0); - assert(rnd >=0 && rnd<=5 && rnd!=4); - - if(a<0 && a != INT64_MIN) return -rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1)); - - if(rnd==AV_ROUND_NEAR_INF) r= c/2; - else if(rnd&1) r= c-1; - - if(b<=INT_MAX && c<=INT_MAX){ - if(a<=INT_MAX) - return (a * b + r)/c; - else - return a/c*b + (a%c*b + r)/c; - }else{ - uint64_t a0= a&0xFFFFFFFF; - uint64_t a1= a>>32; - uint64_t b0= b&0xFFFFFFFF; - uint64_t b1= b>>32; - uint64_t t1= a0*b1 + a1*b0; - uint64_t t1a= t1<<32; - int i; - - a0 = a0*b0 + t1a; - a1 = a1*b1 + (t1>>32) + (a0=0; i--){ -// int o= a1 & 0x8000000000000000ULL; - a1+= a1 + ((a0>>i)&1); - t1+=t1; - if(/*o || */c <= a1){ - a1 -= c; - t1++; - } - } - return t1; - } -} - -int64_t rescale_q(int64_t a, Rational bq, Rational cq){ - int64_t b= bq.num * (int64_t)cq.den; - int64_t c= cq.num * (int64_t)bq.den; - return rescale_rnd(a, b, c, AV_ROUND_NEAR_INF); -}