// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/formats/mp2t/es_parser_mpeg1audio.h"

#include <memory>
#include <vector>

#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_span.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/bit_reader.h"
#include "media/base/channel_layout.h"
#include "media/base/media_util.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/timestamp_constants.h"
#include "media/formats/common/offset_byte_queue.h"
#include "media/formats/mp2t/mp2t_common.h"
#include "media/formats/mpeg/mpeg1_audio_stream_parser.h"

namespace media {
namespace mp2t {

struct EsParserMpeg1Audio::Mpeg1AudioFrame {
  // Pointer to the ES data.
  base::raw_span<const uint8_t> data;

  // Number of samples in the frame.
  int sample_count;

  // Frame offset in the ES queue.
  int64_t queue_offset;
};

EsParserMpeg1Audio::EsParserMpeg1Audio(
    const NewAudioConfigCB& new_audio_config_cb,
    EmitBufferCB emit_buffer_cb,
    MediaLog* media_log)
    : media_log_(MediaLog::CloneSafely(media_log)),
      new_audio_config_cb_(new_audio_config_cb),
      emit_buffer_cb_(std::move(emit_buffer_cb)) {
  DCHECK(media_log_);
}

EsParserMpeg1Audio::~EsParserMpeg1Audio() {
}

bool EsParserMpeg1Audio::ParseFromEsQueue() {
  // Look for every MPEG1 audio frame in the ES buffer.
  Mpeg1AudioFrame mpeg1audio_frame;
  while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
    // Update the audio configuration if needed.
    DCHECK_GE(mpeg1audio_frame.data.size(),
              MPEG1AudioStreamParser::kHeaderSize);
    if (!UpdateAudioConfiguration(mpeg1audio_frame.data)) {
      return false;
    }

    // Get the PTS & the duration of this access unit.
    TimingDesc current_timing_desc =
        GetTimingDescriptor(mpeg1audio_frame.queue_offset);
    if (current_timing_desc.pts != kNoTimestamp) {
      audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
    }

    if (!audio_timestamp_helper_->base_timestamp()) {
      DVLOG(1) << "Skipping audio frame with unknown timestamp";
      SkipMpeg1AudioFrame(mpeg1audio_frame);
      continue;
    }
    base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
    base::TimeDelta frame_duration =
        audio_timestamp_helper_->GetFrameDuration(
            mpeg1audio_frame.sample_count);

    // Emit an audio frame.
    bool is_key_frame = true;

    // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
    // type and allow multiple audio tracks. See https://crbug.com/341581.
    scoped_refptr<StreamParserBuffer> stream_parser_buffer =
        StreamParserBuffer::CopyFrom(mpeg1audio_frame.data, is_key_frame,
                                     DemuxerStream::AUDIO, kMp2tAudioTrackId);
    stream_parser_buffer->set_timestamp(current_pts);
    stream_parser_buffer->set_duration(frame_duration);
    emit_buffer_cb_.Run(stream_parser_buffer);

    // Update the PTS of the next frame.
    audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count);

    // Skip the current frame.
    SkipMpeg1AudioFrame(mpeg1audio_frame);
  }

  return true;
}

void EsParserMpeg1Audio::Flush() {
}

void EsParserMpeg1Audio::ResetInternal() {
  last_audio_decoder_config_ = AudioDecoderConfig();
}

bool EsParserMpeg1Audio::LookForMpeg1AudioFrame(
    Mpeg1AudioFrame* mpeg1audio_frame) {
  base::span<const uint8_t> es = es_queue_->Data();

  if (es.size() < MPEG1AudioStreamParser::kHeaderSize) {
    return false;
  }

  size_t max_offset = es.size() - MPEG1AudioStreamParser::kHeaderSize;
  for (size_t offset = 0; offset < max_offset; offset++) {
    base::span<const uint8_t> cur_buf = es.subspan(offset);
    if (cur_buf[0] != 0xff) {
      continue;
    }

    const auto header = MPEG1AudioStreamParser::ParseHeader(cur_buf);
    if (!header) {
      LIMITED_MEDIA_LOG(DEBUG, media_log_, mp3_parse_error_limit_, 5)
          << "Invalid MP3 header data";
      continue;
    }

    if (cur_buf.size() < header->frame_size) {
      // Not a full frame: will resume when we have more data.
      // Remove all the bytes located before the frame header,
      // these bytes will not be used anymore.
      es_queue_->Pop(offset);
      return false;
    }

    // Check whether there is another frame
    // |frame_size| apart from the current one.
    if (cur_buf.size() >= header->frame_size + 1 &&
        cur_buf[header->frame_size] != 0xff) {
      continue;
    }

    es_queue_->Pop(offset);
    mpeg1audio_frame->data = es_queue_->Data().first(header->frame_size);

    mpeg1audio_frame->queue_offset = es_queue_->head();
    mpeg1audio_frame->sample_count = header->sample_count;
    DVLOG(LOG_LEVEL_ES) << "MPEG1 audio syncword @ pos="
                        << mpeg1audio_frame->queue_offset
                        << " frame_size=" << mpeg1audio_frame->data.size();
    DVLOG(LOG_LEVEL_ES) << "MPEG1 audio header: "
                        << base::HexEncode(mpeg1audio_frame->data.first(
                               MPEG1AudioStreamParser::kHeaderSize));
    return true;
  }

  es_queue_->Pop(max_offset);
  return false;
}

bool EsParserMpeg1Audio::UpdateAudioConfiguration(
    base::span<const uint8_t> mpeg1audio_header) {
  const auto header = MPEG1AudioStreamParser::ParseHeader(mpeg1audio_header);
  if (!header) {
    LIMITED_MEDIA_LOG(DEBUG, media_log_, mp3_parse_error_limit_, 5)
        << "Invalid MP3 header data";
    return false;
  }

  // TODO(damienv): Verify whether Android playback requires the extra data
  // field for Mpeg1 audio. If yes, we should generate this field.
  AudioDecoderConfig audio_decoder_config(
      AudioCodec::kMP3, kSampleFormatS16,
      ChannelLayoutConfig::FromLayout(header->channel_layout),
      header->sample_rate, EmptyExtraData(), EncryptionScheme::kUnencrypted);

  if (!audio_decoder_config.IsValidConfig()) {
    DVLOG(1) << "Invalid config: "
             << audio_decoder_config.AsHumanReadableString();
    return false;
  }

  if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
    DVLOG(1) << "Sampling frequency: " << header->sample_rate;
    DVLOG(1) << "Channel layout: " << header->channel_layout;
    // Reset the timestamp helper to use a new time scale.
    if (audio_timestamp_helper_ && audio_timestamp_helper_->base_timestamp()) {
      base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
      audio_timestamp_helper_ =
          std::make_unique<AudioTimestampHelper>(header->sample_rate);
      audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
    } else {
      audio_timestamp_helper_ =
          std::make_unique<AudioTimestampHelper>(header->sample_rate);
    }
    // Audio config notification.
    last_audio_decoder_config_ = audio_decoder_config;
    new_audio_config_cb_.Run(audio_decoder_config);
  }

  return true;
}

void EsParserMpeg1Audio::SkipMpeg1AudioFrame(
    const Mpeg1AudioFrame& mpeg1audio_frame) {
  DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head());
  es_queue_->Pop(base::checked_cast<int>(mpeg1audio_frame.data.size()));
}

}  // namespace mp2t
}  // namespace media
