mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
f12a92a2ed
- Bug 1202351 - Remove MDSM::mPlayDuration. r=cpearce. (c982bcf84d) - Bug 1202540 - Remove MDSM::mPlayStartTime. r=kinetik. (462de866a4) - Bug 1203047 - Make MediaDecoderReader know less about AudioData/VideoData by using MediaData instead. r=jya (3b8cda73da) - Bug 1172830 - Move buffering check out of MediaDecoderStateMachine::UpdateRenderedVideoFrames(). r=cpearce. (c4a174745f) - Bug 1206578 - Group public and private functions respectively for MDSM. r=gsquelart. (59008c04f6) - Bug 1206607 - Remove some dead code from MDSM. r=kinetik. (252e4af765) - Bug 1208932 - Remove ReadMetadataFailureReason::WAITING_FOR_RESOURCES. r=jya. (622e18eef0) - Bug 1208931 - Remove MediaDecoderReader::IsWaitingMediaResources(). r#=ya. (31cb8c85af) - Bug 1183888: Report empty buffered ranges unless we have a start time. r=bholley (9a32ffa1eb) - Bug 1208933 - Remove AbstractMediaDecoder::NotifyWaitingForResourcesSatusChanged(). r=jya. (b1870471ba) - Bug 1164697 - Fix time unit of AudioOffloadPlayer r=bholley (e36a51ca44) - Bug 1208930 - Remove usage of decoder monitor from MediaDecoder. r=jya. (8b3d93c3a3) - Bug 1209864. Part 1 - make all methods run on the main thread and remove usage of the decoder monitor. r=roc. (6d8b607236) - Bug 1209864. Part 2 - remove unused code. r=sotaro. (c69b8440ee) - Bug 1188643. Buffer more audio in audio capture mode to avoid glitches. r=cpearce. (11fc554d30) - Bug 1208934 - Remove usage of decoder monitor from MDSM. r=kinetik. (a67b4d9d01) - Bug 1211364 - Check frame validity earlier when decoded frames arrive in MDSM. r=jwwang (e6c35f6d49) - Bug 1212701. Part 1 - remove AbstractMediaDecoder::OnDecodeTaskQueue(). r=jya. (f1f840ebe9) - Bug 1212701. Part 2 - remove MediaDecoderStateMachine::OnDecodeTaskQueue() which is unused. r=jya. (2c51be5749) - Bug 1213897 - Extract DelayedScheduler out of MDSM to a common class.r=jwwang (4bfc24688a) - Bug 1206568: P1. Ensure FFmpeg decoder is only accessed through the decoder's task queue. r=cpearce (39ed961411) - Bug 1179667 - Use MozPromise to initialize Gonk PlatformDecodeModule. r=jya (86da0475fb) - Bug 1206568: P2. Clean up header declarations. r=cpearce (2d1aa1bdaa) - missing bits Bug 1206568: P1. (511c1ee0ef)
216 lines
6.9 KiB
C++
216 lines
6.9 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
#include "FFmpegRuntimeLinker.h"
|
|
#include "FFmpegAudioDecoder.h"
|
|
#include "TimeUnits.h"
|
|
|
|
#define MAX_CHANNELS 16
|
|
|
|
namespace mozilla
|
|
{
|
|
|
|
static int (*avcodec_decode_audio4)(AVCodecContext*,AVFrame*,
|
|
int*,const AVPacket*) = nullptr;
|
|
static void (*av_init_packet1)(AVPacket*) = nullptr;
|
|
|
|
FFmpegAudioDecoder<LIBAV_VER>::FFmpegAudioDecoder(
|
|
FlushableTaskQueue* aTaskQueue, MediaDataDecoderCallback* aCallback,
|
|
const AudioInfo& aConfig)
|
|
: FFmpegDataDecoder(aTaskQueue, aCallback, GetCodecId(aConfig.mMimeType))
|
|
{
|
|
MOZ_COUNT_CTOR(FFmpegAudioDecoder);
|
|
// Use a new MediaByteBuffer as the object will be modified during initialization.
|
|
mExtraData = new MediaByteBuffer;
|
|
mExtraData->AppendElements(*aConfig.mCodecSpecificConfig);
|
|
}
|
|
|
|
nsRefPtr<MediaDataDecoder::InitPromise>
|
|
FFmpegAudioDecoder<LIBAV_VER>::Init()
|
|
{
|
|
nsresult rv = InitDecoder();
|
|
|
|
if(rv == NS_OK) {
|
|
avcodec_decode_audio4 = (decltype(avcodec_decode_audio4))FFmpegRuntimeLinker::avc_ptr[_decode_audio4];
|
|
av_init_packet1 = (decltype(av_init_packet1))FFmpegRuntimeLinker::avc_ptr[_init_packet];
|
|
}
|
|
|
|
return rv == NS_OK ? InitPromise::CreateAndResolve(TrackInfo::kAudioTrack, __func__)
|
|
: InitPromise::CreateAndReject(DecoderFailureReason::INIT_ERROR, __func__);
|
|
}
|
|
|
|
void
|
|
FFmpegAudioDecoder<LIBAV_VER>::InitCodecContext()
|
|
{
|
|
MOZ_ASSERT(mCodecContext);
|
|
// We do not want to set this value to 0 as FFmpeg by default will
|
|
// use the number of cores, which with our mozlibavutil get_cpu_count
|
|
// isn't implemented.
|
|
mCodecContext->thread_count = 1;
|
|
// FFmpeg takes this as a suggestion for what format to use for audio samples.
|
|
uint32_t major, minor, micro;
|
|
FFmpegRuntimeLinker::GetVersion(major, minor, micro);
|
|
// LibAV 0.8 produces rubbish float interleaved samples, request 16 bits audio.
|
|
mCodecContext->request_sample_fmt =
|
|
(major == 53) ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_FLT;
|
|
}
|
|
|
|
static AudioDataValue*
|
|
CopyAndPackAudio(AVFrame* aFrame, uint32_t aNumChannels, uint32_t aNumAFrames)
|
|
{
|
|
MOZ_ASSERT(aNumChannels <= MAX_CHANNELS);
|
|
|
|
nsAutoArrayPtr<AudioDataValue> audio(
|
|
new AudioDataValue[aNumChannels * aNumAFrames]);
|
|
|
|
if (aFrame->format == AV_SAMPLE_FMT_FLT) {
|
|
// Audio data already packed. No need to do anything other than copy it
|
|
// into a buffer we own.
|
|
memcpy(audio, aFrame->data[0],
|
|
aNumChannels * aNumAFrames * sizeof(AudioDataValue));
|
|
} else if (aFrame->format == AV_SAMPLE_FMT_FLTP) {
|
|
// Planar audio data. Pack it into something we can understand.
|
|
AudioDataValue* tmp = audio;
|
|
AudioDataValue** data = reinterpret_cast<AudioDataValue**>(aFrame->data);
|
|
for (uint32_t frame = 0; frame < aNumAFrames; frame++) {
|
|
for (uint32_t channel = 0; channel < aNumChannels; channel++) {
|
|
*tmp++ = data[channel][frame];
|
|
}
|
|
}
|
|
} else if (aFrame->format == AV_SAMPLE_FMT_S16) {
|
|
// Audio data already packed. Need to convert from S16 to 32 bits Float
|
|
AudioDataValue* tmp = audio;
|
|
int16_t* data = reinterpret_cast<int16_t**>(aFrame->data)[0];
|
|
for (uint32_t frame = 0; frame < aNumAFrames; frame++) {
|
|
for (uint32_t channel = 0; channel < aNumChannels; channel++) {
|
|
*tmp++ = AudioSampleToFloat(*data++);
|
|
}
|
|
}
|
|
} else if (aFrame->format == AV_SAMPLE_FMT_S16P) {
|
|
// Planar audio data. Convert it from S16 to 32 bits float
|
|
// and pack it into something we can understand.
|
|
AudioDataValue* tmp = audio;
|
|
int16_t** data = reinterpret_cast<int16_t**>(aFrame->data);
|
|
for (uint32_t frame = 0; frame < aNumAFrames; frame++) {
|
|
for (uint32_t channel = 0; channel < aNumChannels; channel++) {
|
|
*tmp++ = AudioSampleToFloat(data[channel][frame]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return audio.forget();
|
|
}
|
|
|
|
void
|
|
FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
|
|
{
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
AVPacket packet;
|
|
av_init_packet1(&packet);
|
|
|
|
packet.data = const_cast<uint8_t*>(aSample->Data());
|
|
packet.size = aSample->Size();
|
|
|
|
if (!PrepareFrame()) {
|
|
NS_WARNING("FFmpeg audio decoder failed to allocate frame.");
|
|
mCallback->Error();
|
|
return;
|
|
}
|
|
|
|
int64_t samplePosition = aSample->mOffset;
|
|
media::TimeUnit pts = media::TimeUnit::FromMicroseconds(aSample->mTime);
|
|
|
|
while (packet.size > 0) {
|
|
int decoded;
|
|
int bytesConsumed =
|
|
avcodec_decode_audio4(mCodecContext, mFrame, &decoded, &packet);
|
|
|
|
if (bytesConsumed < 0) {
|
|
NS_WARNING("FFmpeg audio decoder error.");
|
|
mCallback->Error();
|
|
return;
|
|
}
|
|
|
|
if (decoded) {
|
|
uint32_t numChannels = mCodecContext->channels;
|
|
uint32_t samplingRate = mCodecContext->sample_rate;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> audio(
|
|
CopyAndPackAudio(mFrame, numChannels, mFrame->nb_samples));
|
|
|
|
media::TimeUnit duration =
|
|
FramesToTimeUnit(mFrame->nb_samples, samplingRate);
|
|
if (!duration.IsValid()) {
|
|
NS_WARNING("Invalid count of accumulated audio samples");
|
|
mCallback->Error();
|
|
return;
|
|
}
|
|
|
|
nsRefPtr<AudioData> data = new AudioData(samplePosition,
|
|
pts.ToMicroseconds(),
|
|
duration.ToMicroseconds(),
|
|
mFrame->nb_samples,
|
|
audio.forget(),
|
|
numChannels,
|
|
samplingRate);
|
|
mCallback->Output(data);
|
|
pts += duration;
|
|
if (!pts.IsValid()) {
|
|
NS_WARNING("Invalid count of accumulated audio samples");
|
|
mCallback->Error();
|
|
return;
|
|
}
|
|
}
|
|
packet.data += bytesConsumed;
|
|
packet.size -= bytesConsumed;
|
|
samplePosition += bytesConsumed;
|
|
}
|
|
|
|
if (mTaskQueue->IsEmpty()) {
|
|
mCallback->InputExhausted();
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
FFmpegAudioDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
|
|
{
|
|
nsCOMPtr<nsIRunnable> runnable(NS_NewRunnableMethodWithArg<nsRefPtr<MediaRawData>>(
|
|
this, &FFmpegAudioDecoder::DecodePacket, nsRefPtr<MediaRawData>(aSample)));
|
|
mTaskQueue->Dispatch(runnable.forget());
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
FFmpegAudioDecoder<LIBAV_VER>::ProcessDrain()
|
|
{
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
ProcessFlush();
|
|
mCallback->DrainComplete();
|
|
}
|
|
|
|
AVCodecID
|
|
FFmpegAudioDecoder<LIBAV_VER>::GetCodecId(const nsACString& aMimeType)
|
|
{
|
|
if (aMimeType.EqualsLiteral("audio/mpeg")) {
|
|
return AV_CODEC_ID_MP3;
|
|
}
|
|
|
|
if (aMimeType.EqualsLiteral("audio/mp4a-latm")) {
|
|
return AV_CODEC_ID_AAC;
|
|
}
|
|
|
|
return AV_CODEC_ID_NONE;
|
|
}
|
|
|
|
FFmpegAudioDecoder<LIBAV_VER>::~FFmpegAudioDecoder()
|
|
{
|
|
MOZ_COUNT_DTOR(FFmpegAudioDecoder);
|
|
}
|
|
|
|
} // namespace mozilla
|