AVPKit
com::avpkit::core::IRational Class Referenceabstract

This class wraps represents a Rational number for the AVPKit. More...

#include <IRational.h>

Inheritance diagram for com::avpkit::core::IRational:
Collaboration diagram for com::avpkit::core::IRational:

Public Types

enum  Rounding {
  ROUND_ZERO = 0 , ROUND_INF = 1 , ROUND_DOWN = 2 , ROUND_UP = 3 ,
  ROUND_NEAR_INF = 5
}
 
typedef enum com::avpkit::core::IRational::Rounding Rounding
 

Public Member Functions

virtual int32_t getNumerator ()=0
 Get the numerator for this rational. More...
 
virtual int32_t getDenominator ()=0
 Get the denominator for this rational. More...
 
virtual IRationalcopy ()=0
 Creates a new IRational object by copying (by value) this object. More...
 
virtual int32_t compareTo (IRational *other)=0
 Compare a rational to this rational. More...
 
virtual double getDouble ()=0
 Rational to double conversion. More...
 
virtual int32_t reduce (int64_t num, int64_t den, int64_t max)=0
 Reduce a fraction to it's lowest common denominators. More...
 
virtual IRationalmultiply (IRational *arg)=0
 Multiplies this number by arg. More...
 
virtual IRationaldivide (IRational *arg)=0
 Divides this rational by arg. More...
 
virtual IRationalsubtract (IRational *arg)=0
 Subtracts arg from this rational. More...
 
virtual IRationaladd (IRational *arg)=0
 Adds arg to this rational. More...
 
virtual int64_t rescale (int64_t origValue, IRational *origBase)=0
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
virtual int64_t rescale (int64_t origValue, IRational *origBase, Rounding rounding)=0
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
virtual void setNumerator (int32_t value)=0
 Sets the numerator on this object. More...
 
virtual void setDenominator (int32_t value)=0
 Sets the denominator on this object. More...
 
virtual void setValue (double value)=0
 Sets the numerator and denominator on this object by reducing the double to the closest integer numerator and denominator. More...
 
virtual double getValue ()=0
 An alias for getDouble() but matching JavaBean conventions. More...
 
virtual bool isFinalized ()=0
 Returns true if init() has been called and this object is now considered finalized and immutable. More...
 
virtual void init ()=0
 Marks this object as finalized and immutable. More...
 
- Public Member Functions inherited from com::avpkit::ferry::RefCounted
virtual int32_t acquire ()
 Internal Only. More...
 
virtual int32_t release ()
 Internal Only. More...
 
virtual RefCountedcopyReference ()
 Create a new Java object that refers to the same native object. More...
 
virtual int32_t getCurrentRefCount ()
 Return the current reference count on this object. More...
 
void setJavaAllocator (void *allocator)
 This method is public but not part of the standard API. More...
 
void * getJavaAllocator ()
 This method is public but not part of the standard API. More...
 

Static Public Member Functions

static int32_t sCompareTo (IRational *a, IRational *b)
 Compare two rationals. More...
 
static int32_t sReduce (IRational *dst, int64_t num, int64_t den, int64_t max)
 Reduce a fraction to it's lowest common denominators. More...
 
static IRationalsMultiply (IRational *a, IRational *b)
 Multiples a by b. More...
 
static IRationalsDivide (IRational *a, IRational *b)
 Divides a by b. More...
 
static IRationalsSubtract (IRational *a, IRational *b)
 Subtracts a from b. More...
 
static IRationalsAdd (IRational *a, IRational *b)
 Adds a to b. More...
 
static int64_t sRescale (int64_t origValue, IRational *origBase, IRational *newBase)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
static IRationalmake ()
 Get a new rational that will be set to 0/0. More...
 
static IRationalmake (double d)
 Converts a double precision floating point number to a rational. More...
 
static IRationalmake (IRational *src)
 Creates deep copy of a Rational from another Rational. More...
 
static IRationalmake (int32_t num, int32_t den)
 Create a rational from a numerator and denominator. More...
 
static int64_t sRescale (int64_t origValue, IRational *origBase, IRational *newBase, Rounding rounding)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
static int64_t rescale (int64_t srcValue, int32_t dstNumerator, int32_t dstDenominator, int32_t srcNumerator, int32_t srcDenominator, Rounding rounding)
 Rescales a long value to another long value. More...
 

Additional Inherited Members

- Protected Member Functions inherited from com::avpkit::ferry::RefCounted
virtual void destroy ()
 This method is called by RefCounted objects when their Ref Count reaches zero and they are about to be destroyed.
 
- Protected Attributes inherited from com::avpkit::ferry::RefCounted
AtomicIntegermRefCount
 This is the internal reference count, represented as an AtomicInteger to make sure it is thread safe.
 
void * mAllocator
 Not part of public API.
 

Detailed Description

This class wraps represents a Rational number for the AVPKit.

Video formats often use rational numbers, and converting between them willy nilly can lead to rounding errors, and eventually, out of sync problems. Therefore we use IRational objects to pass around Rational Numbers and avoid conversion until the very last moment.

Note: There are some static convenience methods in this class that start with s*. They start with s (as opposed to overloading methods (e.g. sAdd(...) vs. add(...)).

Definition at line 42 of file IRational.h.

Member Function Documentation

◆ add()

virtual IRational* com::avpkit::core::IRational::add ( IRational arg)
pure virtual

Adds arg to this rational.

Parameters
argThe amount to add to this.
Returns
this+arg.

Implemented in com::avpkit::core::Rational.

Referenced by sAdd().

◆ compareTo()

virtual int32_t com::avpkit::core::IRational::compareTo ( IRational other)
pure virtual

Compare a rational to this rational.

Parameters
othersecond rational
Returns
0 if this==other, 1 if this>other and -1 if this<other.

Implemented in com::avpkit::core::Rational.

Referenced by sCompareTo().

◆ copy()

virtual IRational* com::avpkit::core::IRational::copy ( )
pure virtual

Creates a new IRational object by copying (by value) this object.

Returns
the new object

Implemented in com::avpkit::core::Rational.

◆ divide()

virtual IRational* com::avpkit::core::IRational::divide ( IRational arg)
pure virtual

Divides this rational by arg.

Parameters
argThe divisor to use.
Returns
this/arg.

Implemented in com::avpkit::core::Rational.

Referenced by sDivide().

◆ getDenominator()

virtual int32_t com::avpkit::core::IRational::getDenominator ( )
pure virtual

◆ getDouble()

virtual double com::avpkit::core::IRational::getDouble ( )
pure virtual

Rational to double conversion.

Returns
(double) a

Implemented in com::avpkit::core::Rational.

◆ getNumerator()

virtual int32_t com::avpkit::core::IRational::getNumerator ( )
pure virtual

◆ getValue()

virtual double com::avpkit::core::IRational::getValue ( )
pure virtual

An alias for getDouble() but matching JavaBean conventions.

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ init()

virtual void com::avpkit::core::IRational::init ( )
pure virtual

Marks this object as finalized and immutable.

Any setters called after the first init() call will be ignored.

Most make methods will call this method automatically, with the exception of the blank factory method make().

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ isFinalized()

virtual bool com::avpkit::core::IRational::isFinalized ( )
pure virtual

Returns true if init() has been called and this object is now considered finalized and immutable.

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ make() [1/4]

◆ make() [2/4]

IRational * com::avpkit::core::IRational::make ( double  d)
static

Converts a double precision floating point number to a rational.

Parameters
ddouble to convert
Returns
A new Rational; caller must release() when done.

Definition at line 86 of file IRational.cpp.

87  {
88  Global::init();
89  return Rational::make(d);
90  }

References com::avpkit::core::Global::init(), and make().

◆ make() [3/4]

IRational * com::avpkit::core::IRational::make ( int32_t  num,
int32_t  den 
)
static

Create a rational from a numerator and denominator.

We will always reduce this to the lowest num/den pair we can, but never having den exceed what was passed in.

Parameters
numThe numerator of the resulting Rational
denThe denominator of the resulting Rational
Returns
A new Rational;

Definition at line 106 of file IRational.cpp.

107  {
108  Global::init();
109  return Rational::make(num, den);
110  }

References com::avpkit::core::Global::init(), and make().

◆ make() [4/4]

IRational * com::avpkit::core::IRational::make ( IRational src)
static

Creates deep copy of a Rational from another Rational.

Parameters
srcThe source Rational to copy.
Returns
A new Rational; Returns null if src is null.

Definition at line 93 of file IRational.cpp.

94  {
95  Global::init();
96  Rational* src = dynamic_cast<Rational*>(aSrc);
97  IRational* retval = 0;
98  if (src)
99  {
100  retval = Rational::make(src);
101  }
102  return retval;
103  }

References com::avpkit::core::Global::init(), and make().

◆ multiply()

virtual IRational* com::avpkit::core::IRational::multiply ( IRational arg)
pure virtual

Multiplies this number by arg.

Parameters
argnumber to mulitply by.
Returns
this*arg.

Implemented in com::avpkit::core::Rational.

Referenced by sMultiply().

◆ reduce()

virtual int32_t com::avpkit::core::IRational::reduce ( int64_t  num,
int64_t  den,
int64_t  max 
)
pure virtual

Reduce a fraction to it's lowest common denominators.

This is useful for framerate calculations.

Parameters
numthe src numerator.
denthe src denominator.
maxthe maximum allowed for nom & den in the reduced fraction.
Returns
1 if exact, 0 otherwise

Implemented in com::avpkit::core::Rational.

Referenced by sReduce().

◆ rescale() [1/3]

virtual int64_t com::avpkit::core::IRational::rescale ( int64_t  origValue,
IRational origBase 
)
pure virtual

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
Returns
The new integer value, scaled in units of this IRational.

Implemented in com::avpkit::core::Rational.

Referenced by com::avpkit::core::BufferSink::fillAudioSamples(), com::avpkit::core::BufferSink::fillVideoPicture(), and sRescale().

◆ rescale() [2/3]

virtual int64_t com::avpkit::core::IRational::rescale ( int64_t  origValue,
IRational origBase,
Rounding  rounding 
)
pure virtual

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
roundingHow you want rounding to occur
Returns
The new integer value, scaled in units of this IRational.

Implemented in com::avpkit::core::Rational.

◆ rescale() [3/3]

int64_t com::avpkit::core::IRational::rescale ( int64_t  srcValue,
int32_t  dstNumerator,
int32_t  dstDenominator,
int32_t  srcNumerator,
int32_t  srcDenominator,
Rounding  rounding 
)
static

Rescales a long value to another long value.

This method doesn't use IRational values, but instead uses numerators and denominators passed in by the caller. It will not result in any memory allocations.

Parameters
srcValueThe value to rescale.
dstNumeratorThe numerator of the units you want to scale to. Must be non-zero.
dstDenominatorThe denominator of the units you want to scale to. Must be non-zero.
srcNumeratorThe numerator of the units srcValue is expressed in. Must be non-zero.
srcDenominatorThe denominator of the units srcValue is expressed in. Must be non-zero.
roundingHow you want rounding to occur
Returns
The new integer value, scaled in units of dstNumerator/dstNumerator, or 0 if there is a parameter error.

Definition at line 123 of file IRational.cpp.

129  {
130  return Rational::rescale(srcValue,
131  dstNumerator, dstDenominator,
132  srcNumerator, srcDenominator,
133  rounding);
134  }
virtual int64_t rescale(int64_t origValue, IRational *origBase)
Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this...
Definition: Rational.cpp:244

References com::avpkit::core::Rational::rescale().

◆ sAdd()

IRational * com::avpkit::core::IRational::sAdd ( IRational a,
IRational b 
)
static

Adds a to b.

Parameters
aThe first number. @parma b The second number.
Returns
a+b.

Definition at line 61 of file IRational.cpp.

62  {
63  return a->add(b);
64  }

References add().

◆ sCompareTo()

int32_t com::avpkit::core::IRational::sCompareTo ( IRational a,
IRational b 
)
static

Compare two rationals.

Parameters
athe first rational
bthe second rational
Returns
0 if a==b, 1 if a>b and -1 if b<a.

Definition at line 36 of file IRational.cpp.

37  {
38  return a->compareTo(b);
39  }

References compareTo().

◆ sDivide()

IRational * com::avpkit::core::IRational::sDivide ( IRational a,
IRational b 
)
static

Divides a by b.

Parameters
aThe first number. @parma b The second number.
Returns
a/b.

Definition at line 49 of file IRational.cpp.

50  {
51  return a->divide(b);
52  }

References divide().

◆ setDenominator()

virtual void com::avpkit::core::IRational::setDenominator ( int32_t  value)
pure virtual

Sets the denominator on this object.

If isFinalized is true, then this method is ignored.

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ setNumerator()

virtual void com::avpkit::core::IRational::setNumerator ( int32_t  value)
pure virtual

Sets the numerator on this object.

If isFinalized is true, then this method is ignored.

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ setValue()

virtual void com::avpkit::core::IRational::setValue ( double  value)
pure virtual

Sets the numerator and denominator on this object by reducing the double to the closest integer numerator and denominator.

If isFinalized is true, then this method is ignored.

Since
3.2

Implemented in com::avpkit::core::Rational.

◆ sMultiply()

IRational * com::avpkit::core::IRational::sMultiply ( IRational a,
IRational b 
)
static

Multiples a by b.

Parameters
athe first number
bthe second number.
Returns
a*b

Definition at line 67 of file IRational.cpp.

68  {
69  return a->multiply(b);
70  }

References multiply().

◆ sReduce()

int32_t com::avpkit::core::IRational::sReduce ( IRational dst,
int64_t  num,
int64_t  den,
int64_t  max 
)
static

Reduce a fraction to it's lowest common denominators.

This is useful for framerate calculations.

Parameters
dstThe destination rational
numthe src numerator.
denthe src denominator.
maxthe maximum allowed for nom & den in the reduced fraction.
Returns
1 if exact, 0 otherwise

Definition at line 42 of file IRational.cpp.

44  {
45  return (dst ? dst->reduce(num, den, max) : 0);
46  }

References reduce().

◆ sRescale() [1/2]

int64_t com::avpkit::core::IRational::sRescale ( int64_t  origValue,
IRational origBase,
IRational newBase 
)
static

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
newBaseThe rational you want to rescale origValue into.
Returns
The new integer value, scaled in units of this IRational.

Definition at line 73 of file IRational.cpp.

74  {
75  return newBase->rescale(origValue, origBase);
76  }

References rescale().

◆ sRescale() [2/2]

int64_t com::avpkit::core::IRational::sRescale ( int64_t  origValue,
IRational origBase,
IRational newBase,
Rounding  rounding 
)
static

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
newBaseThe rational you want to rescale origValue into.
roundingHow you want rounding to occur
Returns
The new integer value, scaled in units of this IRational.

Definition at line 113 of file IRational.cpp.

116  {
117  if (!origBase || !newBase)
118  return origValue;
119  return newBase->rescale(origValue, origBase, rounding);
120  }

References rescale().

◆ sSubtract()

IRational * com::avpkit::core::IRational::sSubtract ( IRational a,
IRational b 
)
static

Subtracts a from b.

Parameters
aThe first number. @parma b The second number.
Returns
a-b.

Definition at line 55 of file IRational.cpp.

56  {
57  return a->subtract(b);
58  }

References subtract().

◆ subtract()

virtual IRational* com::avpkit::core::IRational::subtract ( IRational arg)
pure virtual

Subtracts arg from this rational.

Parameters
argThe amount to subtract from this.
Returns
this-arg.

Implemented in com::avpkit::core::Rational.

Referenced by sSubtract().


The documentation for this class was generated from the following files: