Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 
     19 #include "aac_timestamp.h"
     20 
     21 
     22 //Initialize the parameters
     23 void AacTimeStampCalc::SetParameters(uint32 aFreq, uint32 aSamples)
     24 {
     25     if (0 != aFreq)
     26     {
     27         iSamplingFreq = aFreq;
     28     }
     29 
     30     iSamplesPerFrame = aSamples;
     31 }
     32 
     33 
     34 //Set the current timestamp equal to the input buffer timestamp
     35 void AacTimeStampCalc::SetFromInputTimestamp(OMX_TICKS aValue)
     36 {
     37     iCurrentTs = aValue;
     38     iCurrentSamples = 0;
     39 }
     40 
     41 
     42 void AacTimeStampCalc::UpdateTimestamp(uint32 aValue)
     43 {
     44     // rollover is not considered. Since samples are reset to 0
     45     // every time a new TS is applied to an output buffer - practically - rollover can't happen
     46     iCurrentSamples += aValue;
     47 }
     48 
     49 //Convert current samples into the output timestamp
     50 OMX_TICKS AacTimeStampCalc::GetConvertedTs()
     51 {
     52     OMX_TICKS Value = iCurrentTs;
     53 
     54     //Formula used: TS in OMX ticks = (samples * 10^6/sampling_freq)
     55     //Rounding added (add 0.5 to the result), extra check for divide by zero
     56     if (0 != iSamplingFreq)
     57     {
     58         Value = iCurrentTs + (((OMX_TICKS)iCurrentSamples * 1000000 + (iSamplingFreq >> 1)) / iSamplingFreq);
     59     }
     60 
     61     iCurrentTs = Value;
     62     iCurrentSamples = 0;
     63 
     64     return (Value);
     65 }
     66 
     67 
     68 /* Do not update iCurrentTs value, just calculate & return the current timestamp */
     69 OMX_TICKS AacTimeStampCalc::GetCurrentTimestamp()
     70 {
     71     OMX_TICKS Value = iCurrentTs;
     72 
     73     if (0 != iSamplingFreq)
     74     {
     75         Value = iCurrentTs + (((OMX_TICKS)iCurrentSamples * 1000000 + (iSamplingFreq >> 1)) / iSamplingFreq);
     76     }
     77 
     78     return (Value);
     79 }
     80 
     81 //Calculate the duration of single frame (in omx ticks)
     82 OMX_TICKS AacTimeStampCalc::GetFrameDuration()
     83 {
     84     OMX_TICKS Value = 0;
     85 
     86     if (0 != iSamplingFreq)
     87     {
     88         Value = ((OMX_TICKS)iSamplesPerFrame * 1000000 + (iSamplingFreq >> 1)) / iSamplingFreq;
     89     }
     90 
     91     return (Value);
     92 }
     93