/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.exoplayer2.mediacodec;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.audio.MpegAudioUtil;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.util.Log;

/**
 * Tracks the number of processed samples to calculate an accurate current timestamp, matching the
 * calculations made in the Codec2 Mp3 decoder.
 */
/* package */ final class C2Mp3TimestampTracker {

  // Mirroring the actual codec, as can be found at
  // https://cs.android.com/android/platform/superproject/+/master:frameworks/av/media/codec2/components/mp3/C2SoftMp3Dec.h;l=55;drc=3665390c9d32a917398b240c5a46ced07a3b65eb
  private static final long DECODER_DELAY_SAMPLES = 529;
  private static final String TAG = "C2Mp3TimestampTracker";

  private long processedSamples;
  private long anchorTimestampUs;
  private boolean seenInvalidMpegAudioHeader;

  /**
   * Resets the timestamp tracker.
   *
   * <p>This should be done when the codec is flushed.
   */
  public void reset() {
    processedSamples = 0;
    anchorTimestampUs = 0;
    seenInvalidMpegAudioHeader = false;
  }

  /**
   * Updates the tracker with the given input buffer and returns the expected output timestamp.
   *
   * @param format The format associated with the buffer.
   * @param buffer The current input buffer.
   * @return The expected output presentation time, in microseconds.
   */
  public long updateAndGetPresentationTimeUs(Format format, DecoderInputBuffer buffer) {
    if (seenInvalidMpegAudioHeader || buffer.data == null) {
      return buffer.timeUs;
    }

    // These calculations mirror the timestamp calculations in the Codec2 Mp3 Decoder.
    // https://cs.android.com/android/platform/superproject/+/master:frameworks/av/media/codec2/components/mp3/C2SoftMp3Dec.cpp;l=464;drc=ed134640332fea70ca4b05694289d91a5265bb46
    long presentationTimeUs = processedSamples * C.MICROS_PER_SECOND / format.sampleRate;
    int sampleHeaderData = 0;
    for (int i = 0; i < 4; i++) {
      sampleHeaderData <<= 8;
      sampleHeaderData |= buffer.data.get(i) & 0xFF;
    }

    int frameCount = MpegAudioUtil.parseMpegAudioFrameSampleCount(sampleHeaderData);
    if (frameCount == C.LENGTH_UNSET) {
      seenInvalidMpegAudioHeader = true;
      Log.w(TAG, "MPEG audio header is invalid.");
      return buffer.timeUs;
    }
    long outSize = frameCount * format.channelCount * 2L;
    boolean isFirstSample = processedSamples == 0;
    long outOffset = 0;
    if (isFirstSample) {
      anchorTimestampUs = buffer.timeUs;
      outOffset = DECODER_DELAY_SAMPLES;
    }
    processedSamples += (outSize / (format.channelCount * 2L)) - outOffset;
    return anchorTimestampUs + presentationTimeUs;
  }
}

@@ -914,19 +914,101 @@
 r2.util.
-Log
+Assertions;%0Aimport com.google.android.exoplayer2.util.Log;%0Aimport java.nio.ByteBuffer
 ;%0A%0A/**%0A 
@@ -2308,31 +2308,8 @@
 ader
- %7C%7C buffer.data == null
 ) %7B%0A
@@ -2351,358 +2351,62 @@
     
-// These calculations mirror the timestamp calculations in the Codec2 Mp3 Decoder.%0A    // https://cs.android.com/android/platform/superproject/+/master:frameworks/av/media/codec2/components/mp3/C2SoftMp3Dec.cpp;l=464;drc=ed134640332fea70ca4b05694289d91a5265bb46%0A    long presentationTimeUs = processedSamples * C.MICROS_PER_SECOND / format.sampleRate
+ByteBuffer data = Assertions.checkNotNull(buffer.data)
 ;%0A  
@@ -2523,23 +2523,16 @@
 Data %7C= 
-buffer.
 data.get
@@ -2805,246 +2805,440 @@
   %7D%0A
+%0A
     
-long outSize = frameCount * format.channelCount * 2L;%0A    boolean isFirstSample = processedSamples == 0;%0A    long outOffset = 0;%0A    if (isFirstSample) %7B%0A      anchorTimestampUs = buffer.timeUs;%0A      outOffset = DECODER_DELAY_SAMPLES
+// These calculations mirror the timestamp calculations in the Codec2 Mp3 Decoder.%0A    // https://cs.android.com/android/platform/superproject/+/master:frameworks/av/media/codec2/components/mp3/C2SoftMp3Dec.cpp;l=464;drc=ed134640332fea70ca4b05694289d91a5265bb46%0A    if (processedSamples == 0) %7B%0A      anchorTimestampUs = buffer.timeUs;%0A      processedSamples = frameCount - DECODER_DELAY_SAMPLES;%0A      return anchorTimestampUs
 ;%0A  
@@ -3241,24 +3241,29 @@
 ;%0A    %7D%0A    
+long 
 processedSam
@@ -3263,120 +3263,270 @@
 ssed
-Sample
+DurationU
 s 
-+
 = 
-(outSize / (format.channelCount * 2L)) - outOffset;%0A    return anchorTimestampUs + presentationTimeUs
+getProcessedDurationUs(format);%0A    processedSamples += frameCount;%0A    return anchorTimestampUs + processedDurationUs;%0A  %7D%0A%0A  private long getProcessedDurationUs(Format format) %7B%0A    return processedSamples * C.MICROS_PER_SECOND / format.sampleRate
 ;%0A  

Simplify timestamp tracking