tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FFmpegDataDecoder.h (4002B)


      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 __FFmpegDataDecoder_h__
      8 #define __FFmpegDataDecoder_h__
      9 
     10 #include "FFmpegLibWrapper.h"
     11 #include "PlatformDecoderModule.h"
     12 #include "mozilla/StaticMutex.h"
     13 
     14 // This must be the last header included
     15 #include "FFmpegLibs.h"
     16 
     17 namespace mozilla {
     18 #if defined(MOZ_WIDGET_ANDROID) && defined(USING_MOZFFVPX)
     19 class MediaDrmCrypto;
     20 class MediaDrmRemoteCDMParent;
     21 #endif
     22 
     23 template <int V>
     24 class FFmpegDataDecoder : public MediaDataDecoder {};
     25 
     26 template <>
     27 class FFmpegDataDecoder<LIBAV_VER>;
     28 DDLoggedTypeNameAndBase(FFmpegDataDecoder<LIBAV_VER>, MediaDataDecoder);
     29 
     30 template <>
     31 class FFmpegDataDecoder<LIBAV_VER>
     32    : public MediaDataDecoder,
     33      public DecoderDoctorLifeLogger<FFmpegDataDecoder<LIBAV_VER>> {
     34 public:
     35  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FFmpegDataDecoder, final);
     36 
     37  FFmpegDataDecoder(const FFmpegLibWrapper* aLib, AVCodecID aCodecID,
     38                    PRemoteCDMActor* aCDM);
     39 
     40  static bool Link();
     41 
     42  RefPtr<InitPromise> Init() override = 0;
     43  RefPtr<DecodePromise> Decode(MediaRawData* aSample) override;
     44  RefPtr<DecodePromise> Drain() override;
     45  RefPtr<FlushPromise> Flush() override;
     46  RefPtr<ShutdownPromise> Shutdown() override;
     47 
     48  static AVCodec* FindSoftwareAVCodec(const FFmpegLibWrapper* aLib,
     49                                      AVCodecID aCodec);
     50 #ifdef MOZ_USE_HWDECODE
     51  static AVCodec* FindHardwareAVCodec(
     52      const FFmpegLibWrapper* aLib, AVCodecID aCodec,
     53      AVHWDeviceType aDeviceType = AV_HWDEVICE_TYPE_NONE);
     54 #endif
     55 
     56 protected:
     57  // Flush and Drain operation, always run
     58  virtual RefPtr<FlushPromise> ProcessFlush();
     59  virtual void ProcessShutdown();
     60  virtual void InitCodecContext() MOZ_REQUIRES(sMutex) {}
     61  void ReleaseCodecContext() MOZ_REQUIRES(sMutex);
     62  AVFrame* PrepareFrame();
     63  MediaResult InitSWDecoder(AVDictionary** aOptions);
     64  MediaResult InitDecoder(AVCodec* aCodec, AVDictionary** aOptions);
     65  MediaResult AllocateExtraData();
     66  MediaResult DoDecode(MediaRawData* aSample, bool* aGotFrame,
     67                       DecodedData& aResults);
     68 
     69 #if defined(MOZ_WIDGET_ANDROID) && defined(USING_MOZFFVPX)
     70  static void CryptoInfoAddRef(void* aCryptoInfo);
     71  static void CryptoInfoRelease(void* aCryptoInfo);
     72  MediaResult MaybeAttachCryptoInfo(MediaRawData* aSample, AVPacket* aPacket);
     73  MediaResult MaybeAttachCDM();
     74  void MaybeDetachCDM();
     75 #endif
     76 
     77  const FFmpegLibWrapper* mLib;  // set in constructor
     78 
     79  // mCodecContext is accessed on taskqueue only, no locking needed
     80  AVCodecContext* mCodecContext;
     81  AVCodecParserContext* mCodecParser;
     82  AVFrame* mFrame;
     83  RefPtr<MediaByteBuffer> mExtraData;
     84 #if defined(MOZ_WIDGET_ANDROID) && defined(FFVPX_VERSION)
     85  RefPtr<MediaDrmCrypto> mCrypto;
     86  RefPtr<MediaDrmRemoteCDMParent> mCDM;
     87 #endif
     88  AVCodecID mCodecID;  // set in constructor
     89  bool mVideoCodec;
     90 
     91 protected:
     92  virtual ~FFmpegDataDecoder();
     93 
     94  static StaticMutex sMutex;  // used to provide critical-section locking
     95                              // for calls into ffmpeg
     96  const RefPtr<TaskQueue> mTaskQueue;  // set in constructor
     97 
     98  RefPtr<DecodePromise> ProcessDrain();
     99  MozPromiseHolder<DecodePromise> mDrainPromise;
    100 
    101 private:
    102  RefPtr<DecodePromise> ProcessDecode(MediaRawData* aSample);
    103  virtual MediaResult DoDecode(MediaRawData* aSample, uint8_t* aData, int aSize,
    104                               bool* aGotFrame,
    105                               MediaDataDecoder::DecodedData& aOutResults) = 0;
    106  virtual bool NeedParser() const { return false; }
    107  virtual int ParserFlags() const { return PARSER_FLAG_COMPLETE_FRAMES; }
    108 
    109  MozPromiseHolder<DecodePromise> mPromise;
    110  media::TimeUnit mLastInputDts;  // used on Taskqueue
    111 };
    112 
    113 }  // namespace mozilla
    114 
    115 #endif  // __FFmpegDataDecoder_h__