Files
palemoon27/dom/media/fmp4/MP4Decoder.cpp
T
roytam1 0be1a20aba import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1206977: P1. Remove unused PDM function members. r=cpearce (3ff104135b)
- Bug 1193670: P1. Remove use of SharedDecoderManager. r=cpearce (adb5606e3e)
- Bug 1193670: P2. Remove use of SharedDecoderManager from MediaDecoderReader. r=cpearce (61b9170c51)
- Bug 1194612: P1. Dont reject init promise when initialising H264Converter. r=alfredo (d7d9e81361)
- Bug 1194612: P2. Don't drop first sample with SPS/PPS NALs. r=alfredo (ea42652155)
- Bug 1194612: P3. Remove redundant member. r=alfredo (2d30f940c9)
- Bug 1195625 - Use correct TaskQueue in SharedDecoderManager and H264Converter promise. r=jya (87e52cd322)
- Bug 1194518 - Part 1: Passthrough decoder wrapper, useful to spy on MediaFormatReader-decoder calls. r=jya (8e6f7df905)
- Bug 1194518 - Part 2: Using passthrough wrapper if pref 'media.fuzz.vdeo-decode-passthrough' is true. r=jya (2a50be71f0)
- Bug 1194518 - Part 3: Delaying decoder wrapper, ensures a decoder appears consistently slow. r=jya (9d461352e5)
- Bug 1194518 - Part 4: Using delaying wrapper according to pref 'media.fuzz.video-decode-minimum-frame-interval-ms'. r=jya (9ec9684024)
- Bug 1194518 - Part 5: Using std::deque instead of nsTArray to store delayed frames. r=jya (6f3f26578c)
- Bug 1197145 - Added BaseTimeDuration::IsZero(), BaseTimeDuration::operator bool(), TimeStamp::operator bool(). r=nfroyd (1ba008c9fa)
- Bug 1202556 - Detect underflow in TimeStamp addition/subtraction operators; r=froydnj (864a288877)
- Bug 1176731 - Don't mark static inline functions as MFBT_API in TimeStamp.h. r=Waldo (6c94439c61)
- bug 1170586 - Make TimeStamp::FromSystemTime available on iOS. r=froydnj (f9ae7bd8b8)
- Bug 1193670: P3. Remove no longer needed SharedDecoderManager class. r=cpearce (c56ef98e6d)
- Bug 1206977: P2. Wrap PDM creation in a new PDMFactory class. r=cpearce (6425ab58ca)
- Bug 1206977: P3. Allow PDM fallback. r=cpearce (6cce420063)
- Bug 1206977: P4. Add AgnosticDecoderModule object. r=cpearce (1d4581f74c)
- Bug 1206977: P5. Update PlatformDecoderModule documentation. r=cpearce (af14651d29)
- Bug 1206977: P6. Make PlatformDecoderModule::SupportsMimeType pure virtual. r=cpearce (f328eb0a48)
- Bug 1146086: Properly marking overridden member with override keyword. a=bustage (c809b2311c)
- Bug 1204776: P1. Have the PlatformDecoderModules use their own log. r=cpearce (b8565d268b)
- Bug 1204776: P2. Make Apple PDM use PlatformDecoderModule log. r=cpearce (74b9985af8)
- Bug 1204776: P3. Have FFmpeg PDM use PlatformDecoderModule log. r=cpearce (caa498a139)
- Bug 1204776: P4. Have VPX/Opus/Vorbis decoder use PlatformDecoderModule log. r=cpearce (f2fce6c79a)
- fix (215c918930)
- Bug 1206977: [webm] P7. Remove IntelWebMVideoDecoder. r=kinetik (c455347fe1)
- Bug 1206977: P8. Have PDMFactory directly manage the EMEDecoderModule. r=cpearce (d830cbe570)
- Bug 1212164: Prevent use of demuxer before initialization completes. r=cpearce (99c268d31d)
- Bug 1152652: Part1. Use mStandardMozillaStyle for crypto classes. r=edwin (af58e4e822)
- Bug 1206977: P9. Ensure PDMs are only ever created through the PDMFactory. r=cpearce (4d7375ea3d)
- Bug 1206977: P10. Remove redundant code. r=cpearce (8abd18ffe1)
- Bug 1206977: P11. Don't rely on SupportsMimeType to determine if a track can be played. r=cpearce (b569a8f5e1)
- Bug 1212246. Part 1 - remove the aBorrowedTaskQueue parameter from the MediaDecoderReader constructor. r=jya. (0f481aa22b)
- Bug 1212246. Part 2 - remove mTaskQueueIsBorrowed and unnecessary checks for mTaskQueue is never null. r=jya. (27ec74d634)
- Bug 1212723. Part 1 - don't share mBufferedState per bug 1212723 comment 6. r=jya. (59e1b89bee)
- Bug 1212723. Part 2 - remove unused argument aCloneDonor from MediaDecoderReader::Init(). r=jya. (45bc778f41)
2022-10-13 09:51:06 +08:00

231 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 "MP4Decoder.h"
#include "MediaDecoderStateMachine.h"
#include "MediaFormatReader.h"
#include "MP4Demuxer.h"
#include "mozilla/Preferences.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsContentTypeParser.h"
#include "VideoUtils.h"
#include "mozilla/Logging.h"
#ifdef XP_WIN
#include "mozilla/WindowsVersion.h"
#include "WMFDecoderModule.h"
#endif
#ifdef MOZ_WIDGET_ANDROID
#include "nsIGfxInfo.h"
#include "AndroidBridge.h"
#endif
#ifdef MOZ_APPLEMEDIA
#include "AppleDecoderModule.h"
#endif
#include "mozilla/layers/LayersTypes.h"
#include "PDMFactory.h"
namespace mozilla {
#if defined(MOZ_GONK_MEDIACODEC) || defined(XP_WIN) || defined(MOZ_APPLEMEDIA) || defined(MOZ_FFMPEG)
#define MP4_READER_DORMANT_HEURISTIC
#else
#undef MP4_READER_DORMANT_HEURISTIC
#endif
MP4Decoder::MP4Decoder()
{
#if defined(MP4_READER_DORMANT_HEURISTIC)
mDormantSupported = Preferences::GetBool("media.decoder.heuristic.dormant.enabled", false);
#endif
}
MediaDecoderStateMachine* MP4Decoder::CreateStateMachine()
{
MediaDecoderReader* reader = new MediaFormatReader(this, new MP4Demuxer(GetResource()));
return new MediaDecoderStateMachine(this, reader);
}
static bool
IsWhitelistedH264Codec(const nsAString& aCodec)
{
int16_t profile = 0, level = 0;
if (!ExtractH264CodecDetails(aCodec, profile, level)) {
return false;
}
#if 0
// Disable 4k video on windows vista since it performs poorly.
if (!IsWin7OrLater() &&
level >= H264_LEVEL_5) {
return false;
}
#endif
// Just assume what we can play on all platforms the codecs/formats that
// WMF can play, since we don't have documentation about what other
// platforms can play... According to the WMF documentation:
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd797815%28v=vs.85%29.aspx
// "The Media Foundation H.264 video decoder is a Media Foundation Transform
// that supports decoding of Baseline, Main, and High profiles, up to level
// 5.1.". We also report that we can play Extended profile, as there are
// bitstreams that are Extended compliant that are also Baseline compliant.
return level >= H264_LEVEL_1 &&
level <= H264_LEVEL_5_1 &&
(profile == H264_PROFILE_BASE ||
profile == H264_PROFILE_MAIN ||
profile == H264_PROFILE_EXTENDED ||
profile == H264_PROFILE_HIGH);
}
/* static */
bool
MP4Decoder::CanHandleMediaType(const nsACString& aMIMETypeExcludingCodecs,
const nsAString& aCodecs)
{
if (!IsEnabled()) {
return false;
}
// Whitelist MP4 types, so they explicitly match what we encounter on
// the web, as opposed to what we use internally (i.e. what our demuxers
// etc output).
const bool isMP4Audio = aMIMETypeExcludingCodecs.EqualsASCII("audio/mp4") ||
aMIMETypeExcludingCodecs.EqualsASCII("audio/x-m4a");
const bool isMP4Video = aMIMETypeExcludingCodecs.EqualsASCII("video/mp4") ||
#ifdef XP_LINUX
aMIMETypeExcludingCodecs.EqualsASCII("video/quicktime") ||
#endif
aMIMETypeExcludingCodecs.EqualsASCII("video/x-m4v");
if (!isMP4Audio && !isMP4Video) {
return false;
}
#ifdef MOZ_GONK_MEDIACODEC
if (aMIMETypeExcludingCodecs.EqualsASCII(VIDEO_3GPP)) {
return Preferences::GetBool("media.gonk.enabled", false);
}
#endif
nsTArray<nsCString> codecMimes;
if (aCodecs.IsEmpty()) {
// No codecs specified. Assume AAC/H.264
if (isMP4Audio) {
codecMimes.AppendElement(NS_LITERAL_CSTRING("audio/mp4a-latm"));
} else {
MOZ_ASSERT(isMP4Video);
codecMimes.AppendElement(NS_LITERAL_CSTRING("video/avc"));
}
} else {
// Verify that all the codecs specified are ones that we expect that
// we can play.
nsTArray<nsString> codecs;
if (!ParseCodecsString(aCodecs, codecs)) {
return false;
}
for (const nsString& codec : codecs) {
if (IsAACCodecString(codec)) {
codecMimes.AppendElement(NS_LITERAL_CSTRING("audio/mp4a-latm"));
continue;
}
if (codec.EqualsLiteral("mp3")) {
codecMimes.AppendElement(NS_LITERAL_CSTRING("audio/mpeg"));
continue;
}
// Note: Only accept H.264 in a video content type, not in an audio
// content type.
if (IsWhitelistedH264Codec(codec) && isMP4Video) {
codecMimes.AppendElement(NS_LITERAL_CSTRING("video/avc"));
continue;
}
// Some unsupported codec.
return false;
}
}
// Verify that we have a PDM that supports the whitelisted types.
PDMFactory::Init();
nsRefPtr<PDMFactory> platform = new PDMFactory();
for (const nsCString& codecMime : codecMimes) {
if (!platform->SupportsMimeType(codecMime)) {
return false;
}
}
return true;
}
/* static */ bool
MP4Decoder::CanHandleMediaType(const nsAString& aContentType)
{
nsContentTypeParser parser(aContentType);
nsAutoString mimeType;
nsresult rv = parser.GetType(mimeType);
if (NS_FAILED(rv)) {
return false;
}
nsString codecs;
parser.GetParameter("codecs", codecs);
return CanHandleMediaType(NS_ConvertUTF16toUTF8(mimeType), codecs);
}
/* static */
bool
MP4Decoder::IsEnabled()
{
return Preferences::GetBool("media.mp4.enabled");
}
static const uint8_t sTestH264ExtraData[] = {
0x01, 0x64, 0x00, 0x0a, 0xff, 0xe1, 0x00, 0x17, 0x67, 0x64,
0x00, 0x0a, 0xac, 0xd9, 0x44, 0x26, 0x84, 0x00, 0x00, 0x03,
0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xc8, 0x3c, 0x48, 0x96,
0x58, 0x01, 0x00, 0x06, 0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0
};
static already_AddRefed<MediaDataDecoder>
CreateTestH264Decoder(layers::LayersBackend aBackend,
VideoInfo& aConfig)
{
aConfig.mMimeType = "video/avc";
aConfig.mId = 1;
aConfig.mDuration = 40000;
aConfig.mMediaTime = 0;
aConfig.mDisplay = nsIntSize(64, 64);
aConfig.mImage = nsIntRect(0, 0, 64, 64);
aConfig.mExtraData = new MediaByteBuffer();
aConfig.mExtraData->AppendElements(sTestH264ExtraData,
MOZ_ARRAY_LENGTH(sTestH264ExtraData));
PDMFactory::Init();
nsRefPtr<PDMFactory> platform = new PDMFactory();
nsRefPtr<MediaDataDecoder> decoder(
platform->CreateDecoder(aConfig, nullptr, nullptr, aBackend, nullptr));
return decoder.forget();
}
/* static */ bool
MP4Decoder::IsVideoAccelerated(layers::LayersBackend aBackend, nsACString& aFailureReason)
{
VideoInfo config;
nsRefPtr<MediaDataDecoder> decoder(CreateTestH264Decoder(aBackend, config));
if (!decoder) {
aFailureReason.AssignLiteral("Failed to create H264 decoder");
return false;
}
bool result = decoder->IsHardwareAccelerated(aFailureReason);
return result;
}
} // namespace mozilla