Skip to content

Commit

Permalink
Bug 1826382 - Vendor necessary files to decode all pcm types needed u…
Browse files Browse the repository at this point in the history
…sing ffmpeg. r=alwu,media-playback-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D174655
  • Loading branch information
padenot committed Aug 8, 2023
1 parent dc1e05b commit 86837f4
Show file tree
Hide file tree
Showing 12 changed files with 859 additions and 32 deletions.
39 changes: 29 additions & 10 deletions dom/media/platforms/ffmpeg/FFmpegAudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "TimeUnits.h"
#include "VideoUtils.h"
#include "BufferReader.h"
#if defined(FFVPX_VERSION)
# include "libavutil/channel_layout.h"
#endif
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/Telemetry.h"

Expand All @@ -17,14 +20,15 @@ namespace mozilla {
using TimeUnit = media::TimeUnit;

FFmpegAudioDecoder<LIBAV_VER>::FFmpegAudioDecoder(FFmpegLibWrapper* aLib,
const AudioInfo& aConfig)
: FFmpegDataDecoder(aLib, GetCodecId(aConfig.mMimeType)) {
const AudioInfo& aInfo)
: FFmpegDataDecoder(aLib, GetCodecId(aInfo.mMimeType, aInfo)),
mAudioInfo(aInfo) {
MOZ_COUNT_CTOR(FFmpegAudioDecoder);

if (mCodecID == AV_CODEC_ID_AAC &&
aConfig.mCodecSpecificConfig.is<AacCodecSpecificData>()) {
aInfo.mCodecSpecificConfig.is<AacCodecSpecificData>()) {
const AacCodecSpecificData& aacCodecSpecificData =
aConfig.mCodecSpecificConfig.as<AacCodecSpecificData>();
aInfo.mCodecSpecificConfig.as<AacCodecSpecificData>();
mExtraData = new MediaByteBuffer;
// Ffmpeg expects the DecoderConfigDescriptor blob.
mExtraData->AppendElements(
Expand All @@ -40,13 +44,13 @@ FFmpegAudioDecoder<LIBAV_VER>::FFmpegAudioDecoder(FFmpegLibWrapper* aLib,

if (mCodecID == AV_CODEC_ID_FLAC) {
MOZ_DIAGNOSTIC_ASSERT(
aConfig.mCodecSpecificConfig.is<FlacCodecSpecificData>());
aInfo.mCodecSpecificConfig.is<FlacCodecSpecificData>());
// Gracefully handle bad data. If don't hit the preceding assert once this
// has been shipped for awhile, we can remove it and make the following code
// non-conditional.
if (aConfig.mCodecSpecificConfig.is<FlacCodecSpecificData>()) {
if (aInfo.mCodecSpecificConfig.is<FlacCodecSpecificData>()) {
const FlacCodecSpecificData& flacCodecSpecificData =
aConfig.mCodecSpecificConfig.as<FlacCodecSpecificData>();
aInfo.mCodecSpecificConfig.as<FlacCodecSpecificData>();
if (flacCodecSpecificData.mStreamInfoBinaryBlob->IsEmpty()) {
// Flac files without headers will be missing stream info. In this case
// we don't want to feed ffmpeg empty extra data as it will fail, just
Expand All @@ -63,7 +67,7 @@ FFmpegAudioDecoder<LIBAV_VER>::FFmpegAudioDecoder(FFmpegLibWrapper* aLib,

// Vorbis is handled by this case.
RefPtr<MediaByteBuffer> audioCodecSpecificBinaryBlob =
GetAudioCodecSpecificBlob(aConfig.mCodecSpecificConfig);
GetAudioCodecSpecificBlob(aInfo.mCodecSpecificConfig);
if (audioCodecSpecificBinaryBlob && audioCodecSpecificBinaryBlob->Length()) {
// Use a new MediaByteBuffer as the object will be modified during
// initialization.
Expand Down Expand Up @@ -105,6 +109,21 @@ void FFmpegAudioDecoder<LIBAV_VER>::InitCodecContext() {
}
mCodecContext->sample_rate = AssertedCast<int>(mAudioInfo.mRate);
#endif
#ifdef FFVPX_VERSION
// AudioInfo's layout first 32-bits are bit-per-bit compatible with
// WAVEFORMATEXTENSIBLE and FFmpeg's AVChannel enum. We can cast here.
mCodecContext->ch_layout.nb_channels =
AssertedCast<int>(mAudioInfo.mChannels);
if (mAudioInfo.mChannelMap != AudioConfig::ChannelLayout::UNKNOWN_MAP) {
mLib->av_channel_layout_from_mask(
&mCodecContext->ch_layout,
static_cast<uint64_t>(mAudioInfo.mChannelMap));
} else {
mLib->av_channel_layout_default(&mCodecContext->ch_layout,
AssertedCast<int>(mAudioInfo.mChannels));
}
mCodecContext->sample_rate = AssertedCast<int>(mAudioInfo.mRate);
#endif
}

static AlignedAudioBuffer CopyAndPackAudio(AVFrame* aFrame,
Expand Down Expand Up @@ -414,8 +433,8 @@ MediaResult FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
return NS_OK;
}

AVCodecID FFmpegAudioDecoder<LIBAV_VER>::GetCodecId(
const nsACString& aMimeType) {
AVCodecID FFmpegAudioDecoder<LIBAV_VER>::GetCodecId(const nsACString& aMimeType,
const AudioInfo& aInfo) {
if (aMimeType.EqualsLiteral("audio/mpeg")) {
#ifdef FFVPX_VERSION
if (!StaticPrefs::media_ffvpx_mp3_enabled()) {
Expand Down
6 changes: 4 additions & 2 deletions dom/media/platforms/ffmpeg/FFmpegAudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class FFmpegAudioDecoder<LIBAV_VER>
: public FFmpegDataDecoder<LIBAV_VER>,
public DecoderDoctorLifeLogger<FFmpegAudioDecoder<LIBAV_VER>> {
public:
FFmpegAudioDecoder(FFmpegLibWrapper* aLib, const AudioInfo& aConfig);
FFmpegAudioDecoder(FFmpegLibWrapper* aLib, const AudioInfo& aInfo);
virtual ~FFmpegAudioDecoder();

RefPtr<InitPromise> Init() override;
void InitCodecContext() MOZ_REQUIRES(sMutex) override;
static AVCodecID GetCodecId(const nsACString& aMimeType);
static AVCodecID GetCodecId(const nsACString& aMimeType,
const AudioInfo& aInfo);
nsCString GetDescriptionName() const override {
#ifdef USING_MOZFFVPX
return "ffvpx audio decoder"_ns;
Expand All @@ -49,6 +50,7 @@ class FFmpegAudioDecoder<LIBAV_VER>
MediaResult PostProcessOutput(bool aDecoded, MediaRawData* aSample,
DecodedData& aResults, bool* aGotFrame,
size_t aSamplePositionOffset);
const AudioInfo mAudioInfo;
};

} // namespace mozilla
Expand Down
4 changes: 3 additions & 1 deletion dom/media/platforms/ffmpeg/FFmpegDecoderModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class FFmpegDecoderModule : public PlatformDecoderModule {
}

AVCodecID videoCodec = FFmpegVideoDecoder<V>::GetCodecId(mimeType);
AVCodecID audioCodec = FFmpegAudioDecoder<V>::GetCodecId(mimeType);
AVCodecID audioCodec = FFmpegAudioDecoder<V>::GetCodecId(
mimeType,
trackInfo.GetAsAudioInfo() ? *trackInfo.GetAsAudioInfo() : AudioInfo());
if (audioCodec == AV_CODEC_ID_NONE && videoCodec == AV_CODEC_ID_NONE) {
MOZ_LOG(sPDMLog, LogLevel::Debug,
("FFmpeg decoder rejects requested type '%s'",
Expand Down
2 changes: 2 additions & 0 deletions dom/media/platforms/ffmpeg/FFmpegLibWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ FFmpegLibWrapper::LinkResult FFmpegLibWrapper::Link() {
AV_FUNC_AVUTIL_58 | AV_FUNC_AVUTIL_59 | AV_FUNC_AVUTIL_60))
AV_FUNC(av_image_check_size, AV_FUNC_AVUTIL_ALL)
AV_FUNC(av_image_get_buffer_size, AV_FUNC_AVUTIL_ALL)
AV_FUNC_OPTION(av_channel_layout_default, AV_FUNC_AVUTIL_60)
AV_FUNC_OPTION(av_channel_layout_from_mask, AV_FUNC_AVUTIL_60)
AV_FUNC_OPTION(av_buffer_get_opaque,
(AV_FUNC_AVUTIL_56 | AV_FUNC_AVUTIL_57 | AV_FUNC_AVUTIL_58 |
AV_FUNC_AVUTIL_59 | AV_FUNC_AVUTIL_60))
Expand Down
11 changes: 8 additions & 3 deletions dom/media/platforms/ffmpeg/FFmpegLibWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct AVPacket;
struct AVDictionary;
struct AVCodecParserContext;
struct PRLibrary;
struct AVChannelLayout;
#ifdef MOZ_WIDGET_GTK
struct AVCodecHWConfig;
struct AVVAAPIHWConfig;
Expand Down Expand Up @@ -123,6 +124,13 @@ struct MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FFmpegLibWrapper {
int (*av_image_get_buffer_size)(int pix_fmt, int width, int height,
int align);
const char* (*av_get_sample_fmt_name)(int sample_fmt);
void (*av_channel_layout_default)(AVChannelLayout* ch_layout,
int nb_channels);
void (*av_channel_layout_from_mask)(AVChannelLayout* ch_layout,
uint64_t mask);
int (*av_dict_set)(AVDictionary** pm, const char* key, const char* value,
int flags);
void (*av_dict_free)(AVDictionary** m);

// libavutil v55 and later only
AVFrame* (*av_frame_alloc)();
Expand Down Expand Up @@ -156,9 +164,6 @@ struct MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FFmpegLibWrapper {
int (*av_hwdevice_ctx_create_derived)(AVBufferRef** dst_ctx, int type,
AVBufferRef* src_ctx, int flags);
AVBufferRef* (*av_hwframe_ctx_alloc)(AVBufferRef* device_ctx);
int (*av_dict_set)(AVDictionary** pm, const char* key, const char* value,
int flags);
void (*av_dict_free)(AVDictionary** m);
const char* (*avcodec_get_name)(int id);
char* (*av_get_pix_fmt_string)(char* buf, int buf_size, int pix_fmt);

Expand Down
14 changes: 7 additions & 7 deletions media/ffvpx/config_components_audio_only.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,34 +424,34 @@
#define CONFIG_WS_SND1_DECODER 0
#define CONFIG_XMA1_DECODER 0
#define CONFIG_XMA2_DECODER 0
#define CONFIG_PCM_ALAW_DECODER 0
#define CONFIG_PCM_ALAW_DECODER 1
#define CONFIG_PCM_BLURAY_DECODER 0
#define CONFIG_PCM_DVD_DECODER 0
#define CONFIG_PCM_F16LE_DECODER 0
#define CONFIG_PCM_F24LE_DECODER 0
#define CONFIG_PCM_F32BE_DECODER 0
#define CONFIG_PCM_F32LE_DECODER 0
#define CONFIG_PCM_F32LE_DECODER 1
#define CONFIG_PCM_F64BE_DECODER 0
#define CONFIG_PCM_F64LE_DECODER 0
#define CONFIG_PCM_LXF_DECODER 0
#define CONFIG_PCM_MULAW_DECODER 0
#define CONFIG_PCM_MULAW_DECODER 1
#define CONFIG_PCM_S8_DECODER 0
#define CONFIG_PCM_S8_PLANAR_DECODER 0
#define CONFIG_PCM_S16BE_DECODER 0
#define CONFIG_PCM_S16BE_PLANAR_DECODER 0
#define CONFIG_PCM_S16LE_DECODER 0
#define CONFIG_PCM_S16LE_DECODER 1
#define CONFIG_PCM_S16LE_PLANAR_DECODER 0
#define CONFIG_PCM_S24BE_DECODER 0
#define CONFIG_PCM_S24DAUD_DECODER 0
#define CONFIG_PCM_S24LE_DECODER 0
#define CONFIG_PCM_S24LE_DECODER 1
#define CONFIG_PCM_S24LE_PLANAR_DECODER 0
#define CONFIG_PCM_S32BE_DECODER 0
#define CONFIG_PCM_S32LE_DECODER 0
#define CONFIG_PCM_S32LE_DECODER 1
#define CONFIG_PCM_S32LE_PLANAR_DECODER 0
#define CONFIG_PCM_S64BE_DECODER 0
#define CONFIG_PCM_S64LE_DECODER 0
#define CONFIG_PCM_SGA_DECODER 0
#define CONFIG_PCM_U8_DECODER 0
#define CONFIG_PCM_U8_DECODER 1
#define CONFIG_PCM_U16BE_DECODER 0
#define CONFIG_PCM_U16LE_DECODER 0
#define CONFIG_PCM_U24BE_DECODER 0
Expand Down
14 changes: 7 additions & 7 deletions media/ffvpx/config_components_audio_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,34 +437,34 @@
#define CONFIG_WS_SND1_DECODER 0
#define CONFIG_XMA1_DECODER 0
#define CONFIG_XMA2_DECODER 0
#define CONFIG_PCM_ALAW_DECODER 0
#define CONFIG_PCM_ALAW_DECODER 1
#define CONFIG_PCM_BLURAY_DECODER 0
#define CONFIG_PCM_DVD_DECODER 0
#define CONFIG_PCM_F16LE_DECODER 0
#define CONFIG_PCM_F24LE_DECODER 0
#define CONFIG_PCM_F32BE_DECODER 0
#define CONFIG_PCM_F32LE_DECODER 0
#define CONFIG_PCM_F32LE_DECODER 1
#define CONFIG_PCM_F64BE_DECODER 0
#define CONFIG_PCM_F64LE_DECODER 0
#define CONFIG_PCM_LXF_DECODER 0
#define CONFIG_PCM_MULAW_DECODER 0
#define CONFIG_PCM_MULAW_DECODER 1
#define CONFIG_PCM_S8_DECODER 0
#define CONFIG_PCM_S8_PLANAR_DECODER 0
#define CONFIG_PCM_S16BE_DECODER 0
#define CONFIG_PCM_S16BE_PLANAR_DECODER 0
#define CONFIG_PCM_S16LE_DECODER 0
#define CONFIG_PCM_S16LE_DECODER 1
#define CONFIG_PCM_S16LE_PLANAR_DECODER 0
#define CONFIG_PCM_S24BE_DECODER 0
#define CONFIG_PCM_S24DAUD_DECODER 0
#define CONFIG_PCM_S24LE_DECODER 0
#define CONFIG_PCM_S24LE_DECODER 1
#define CONFIG_PCM_S24LE_PLANAR_DECODER 0
#define CONFIG_PCM_S32BE_DECODER 0
#define CONFIG_PCM_S32LE_DECODER 0
#define CONFIG_PCM_S32LE_DECODER 1
#define CONFIG_PCM_S32LE_PLANAR_DECODER 0
#define CONFIG_PCM_S64BE_DECODER 0
#define CONFIG_PCM_S64LE_DECODER 0
#define CONFIG_PCM_SGA_DECODER 0
#define CONFIG_PCM_U8_DECODER 0
#define CONFIG_PCM_U8_DECODER 1
#define CONFIG_PCM_U16BE_DECODER 0
#define CONFIG_PCM_U16LE_DECODER 0
#define CONFIG_PCM_U24BE_DECODER 0
Expand Down
21 changes: 21 additions & 0 deletions media/ffvpx/libavcodec/codec_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,26 @@ static const FFCodec * const codec_list[] = {
#endif
#if CONFIG_LIBVORBIS_DECODER
&ff_libvorbis_decoder,
#endif
#if CONFIG_PCM_ALAW_DECODER
&ff_pcm_alaw_decoder,
#endif
#if CONFIG_PCM_F32LE_DECODER
&ff_pcm_f32le_decoder,
#endif
#if CONFIG_PCM_MULAW_DECODER
&ff_pcm_mulaw_decoder,
#endif
#if CONFIG_PCM_S16LE_DECODER
&ff_pcm_s16le_decoder,
#endif
#if CONFIG_PCM_S24LE_DECODER
&ff_pcm_s24le_decoder,
#endif
#if CONFIG_PCM_S32LE_DECODER
&ff_pcm_s32le_decoder,
#endif
#if CONFIG_PCM_U8_DECODER
&ff_pcm_u8_decoder,
#endif
NULL };
1 change: 1 addition & 0 deletions media/ffvpx/libavcodec/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ SOURCES += [
'options.c',
'parser.c',
'parsers.c',
'pcm.c',
'profiles.c',
'pthread.c',
'pthread_frame.c',
Expand Down
Loading

0 comments on commit 86837f4

Please sign in to comment.