1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef __FFmpegDecoderModule_h__
8 #define __FFmpegDecoderModule_h__
9 
10 #include "FFmpegAudioDecoder.h"
11 #include "FFmpegLibWrapper.h"
12 #include "FFmpegVideoDecoder.h"
13 #include "PlatformDecoderModule.h"
14 #include "VideoUtils.h"
15 #include "VPXDecoder.h"
16 #include "mozilla/StaticPrefs_media.h"
17 
18 namespace mozilla {
19 
20 template <int V>
21 class FFmpegDecoderModule : public PlatformDecoderModule {
22  public:
Create(FFmpegLibWrapper * aLib)23   static already_AddRefed<PlatformDecoderModule> Create(
24       FFmpegLibWrapper* aLib) {
25     RefPtr<PlatformDecoderModule> pdm = new FFmpegDecoderModule(aLib);
26 
27     return pdm.forget();
28   }
29 
FFmpegDecoderModule(FFmpegLibWrapper * aLib)30   explicit FFmpegDecoderModule(FFmpegLibWrapper* aLib) : mLib(aLib) {}
31   virtual ~FFmpegDecoderModule() = default;
32 
CreateVideoDecoder(const CreateDecoderParams & aParams)33   already_AddRefed<MediaDataDecoder> CreateVideoDecoder(
34       const CreateDecoderParams& aParams) override {
35     if (!Supports(SupportDecoderParams(aParams), nullptr)) {
36       return nullptr;
37     }
38     RefPtr<MediaDataDecoder> decoder = new FFmpegVideoDecoder<V>(
39         mLib, aParams.VideoConfig(), aParams.mKnowsCompositor,
40         aParams.mImageContainer,
41         aParams.mOptions.contains(CreateDecoderParams::Option::LowLatency),
42         aParams.mOptions.contains(
43             CreateDecoderParams::Option::HardwareDecoderNotAllowed));
44     return decoder.forget();
45   }
46 
CreateAudioDecoder(const CreateDecoderParams & aParams)47   already_AddRefed<MediaDataDecoder> CreateAudioDecoder(
48       const CreateDecoderParams& aParams) override {
49     if (!Supports(SupportDecoderParams(aParams), nullptr)) {
50       return nullptr;
51     }
52     RefPtr<MediaDataDecoder> decoder =
53         new FFmpegAudioDecoder<V>(mLib, aParams.AudioConfig());
54     return decoder.forget();
55   }
56 
SupportsMimeType(const nsACString & aMimeType,DecoderDoctorDiagnostics * aDiagnostics)57   bool SupportsMimeType(const nsACString& aMimeType,
58                         DecoderDoctorDiagnostics* aDiagnostics) const override {
59     UniquePtr<TrackInfo> trackInfo = CreateTrackInfoWithMIMEType(aMimeType);
60     if (!trackInfo) {
61       return false;
62     }
63     return Supports(SupportDecoderParams(*trackInfo), aDiagnostics);
64   }
65 
Supports(const SupportDecoderParams & aParams,DecoderDoctorDiagnostics * aDiagnostics)66   bool Supports(const SupportDecoderParams& aParams,
67                 DecoderDoctorDiagnostics* aDiagnostics) const override {
68     const auto& trackInfo = aParams.mConfig;
69     const nsACString& mimeType = trackInfo.mMimeType;
70 
71     // Temporary - forces use of VPXDecoder when alpha is present.
72     // Bug 1263836 will handle alpha scenario once implemented. It will shift
73     // the check for alpha to PDMFactory but not itself remove the need for a
74     // check.
75     if (VPXDecoder::IsVPX(mimeType) && trackInfo.GetAsVideoInfo()->HasAlpha()) {
76       return false;
77     }
78 
79     AVCodecID videoCodec = FFmpegVideoDecoder<V>::GetCodecId(mimeType);
80     AVCodecID audioCodec = FFmpegAudioDecoder<V>::GetCodecId(mimeType);
81     if (audioCodec == AV_CODEC_ID_NONE && videoCodec == AV_CODEC_ID_NONE) {
82       return false;
83     }
84     AVCodecID codec = audioCodec != AV_CODEC_ID_NONE ? audioCodec : videoCodec;
85     return !!FFmpegDataDecoder<V>::FindAVCodec(mLib, codec);
86   }
87 
88  protected:
SupportsColorDepth(gfx::ColorDepth aColorDepth,DecoderDoctorDiagnostics * aDiagnostics)89   bool SupportsColorDepth(
90       gfx::ColorDepth aColorDepth,
91       DecoderDoctorDiagnostics* aDiagnostics) const override {
92 #if defined(MOZ_WIDGET_ANDROID)
93     return aColorDepth == gfx::ColorDepth::COLOR_8;
94 #endif
95     return true;
96   }
97 
98  private:
99   FFmpegLibWrapper* mLib;
100 };
101 
102 }  // namespace mozilla
103 
104 #endif  // __FFmpegDecoderModule_h__
105