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 "VPXDecoder.h"
15 #include "mozilla/StaticPrefs_media.h"
16 
17 namespace mozilla {
18 
19 template <int V>
20 class FFmpegDecoderModule : public PlatformDecoderModule {
21  public:
Create(FFmpegLibWrapper * aLib)22   static already_AddRefed<PlatformDecoderModule> Create(
23       FFmpegLibWrapper* aLib) {
24     RefPtr<PlatformDecoderModule> pdm = new FFmpegDecoderModule(aLib);
25 
26     return pdm.forget();
27   }
28 
FFmpegDecoderModule(FFmpegLibWrapper * aLib)29   explicit FFmpegDecoderModule(FFmpegLibWrapper* aLib) : mLib(aLib) {}
30   virtual ~FFmpegDecoderModule() = default;
31 
CreateVideoDecoder(const CreateDecoderParams & aParams)32   already_AddRefed<MediaDataDecoder> CreateVideoDecoder(
33       const CreateDecoderParams& aParams) override {
34     // Temporary - forces use of VPXDecoder when alpha is present.
35     // Bug 1263836 will handle alpha scenario once implemented. It will shift
36     // the check for alpha to PDMFactory but not itself remove the need for a
37     // check.
38     if (aParams.VideoConfig().HasAlpha()) {
39       return nullptr;
40     }
41     if (VPXDecoder::IsVPX(aParams.mConfig.mMimeType) &&
42         aParams.mOptions.contains(CreateDecoderParams::Option::LowLatency) &&
43         !StaticPrefs::media_ffmpeg_low_latency_enabled()) {
44       // We refuse to create a decoder with low latency enabled if it's VP8 or
45       // VP9 unless specifically allowed: this will fallback to libvpx later.
46       // We do allow it for h264.
47       return nullptr;
48     }
49     RefPtr<MediaDataDecoder> decoder = new FFmpegVideoDecoder<V>(
50         mLib, aParams.mTaskQueue, aParams.VideoConfig(),
51         aParams.mKnowsCompositor, aParams.mImageContainer,
52         aParams.mOptions.contains(CreateDecoderParams::Option::LowLatency),
53         aParams.mOptions.contains(
54             CreateDecoderParams::Option::HardwareDecoderNotAllowed));
55     return decoder.forget();
56   }
57 
CreateAudioDecoder(const CreateDecoderParams & aParams)58   already_AddRefed<MediaDataDecoder> CreateAudioDecoder(
59       const CreateDecoderParams& aParams) override {
60     RefPtr<MediaDataDecoder> decoder = new FFmpegAudioDecoder<V>(
61         mLib, aParams.mTaskQueue, aParams.AudioConfig());
62     return decoder.forget();
63   }
64 
SupportsMimeType(const nsACString & aMimeType,DecoderDoctorDiagnostics * aDiagnostics)65   bool SupportsMimeType(const nsACString& aMimeType,
66                         DecoderDoctorDiagnostics* aDiagnostics) const override {
67     AVCodecID videoCodec = FFmpegVideoDecoder<V>::GetCodecId(aMimeType);
68     AVCodecID audioCodec = FFmpegAudioDecoder<V>::GetCodecId(aMimeType);
69     if (audioCodec == AV_CODEC_ID_NONE && videoCodec == AV_CODEC_ID_NONE) {
70       return false;
71     }
72     AVCodecID codec = audioCodec != AV_CODEC_ID_NONE ? audioCodec : videoCodec;
73     return !!FFmpegDataDecoder<V>::FindAVCodec(mLib, codec);
74   }
75 
76  protected:
SupportsColorDepth(gfx::ColorDepth aColorDepth,DecoderDoctorDiagnostics * aDiagnostics)77   bool SupportsColorDepth(
78       gfx::ColorDepth aColorDepth,
79       DecoderDoctorDiagnostics* aDiagnostics) const override {
80 #if defined(MOZ_WIDGET_ANDROID)
81     return aColorDepth == gfx::ColorDepth::COLOR_8;
82 #endif
83     return true;
84   }
85 
86  private:
87   FFmpegLibWrapper* mLib;
88 };
89 
90 }  // namespace mozilla
91 
92 #endif  // __FFmpegDecoderModule_h__
93