tor-browser

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

FFmpegLibWrapper.h (8722B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef __FFmpegLibWrapper_h__
      6 #define __FFmpegLibWrapper_h__
      7 
      8 #include "ffvpx/tx.h"
      9 #include "mozilla/Attributes.h"
     10 #include "mozilla/DefineEnum.h"
     11 #include "mozilla/Preferences.h"
     12 
     13 struct AVCodec;
     14 struct AVCodecContext;
     15 struct AVCodecDescriptor;
     16 struct AVFrame;
     17 struct AVPacket;
     18 struct AVDictionary;
     19 struct AVCodecParserContext;
     20 struct PRLibrary;
     21 struct AVChannelLayout;
     22 struct AVCodecHWConfig;
     23 #ifdef MOZ_WIDGET_GTK
     24 struct AVVAAPIHWConfig;
     25 struct AVHWFramesConstraints;
     26 #endif
     27 struct AVBufferRef;
     28 #ifdef MOZ_WIDGET_ANDROID
     29 typedef struct MediaCodecBuffer AVMediaCodecBuffer;
     30 #endif
     31 
     32 namespace mozilla {
     33 
     34 struct MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FFmpegLibWrapper {
     35  // The class is used only in static storage and so is zero initialized.
     36  FFmpegLibWrapper() = default;
     37  // The libraries are not unloaded in the destructor, because doing so would
     38  // require a static constructor to register the static destructor.  As the
     39  // class is in static storage, the destructor would only run on shutdown
     40  // anyway.
     41  ~FFmpegLibWrapper() = default;
     42 
     43  MOZ_DEFINE_ENUM_CLASS_WITH_TOSTRING_AT_CLASS_SCOPE(
     44      LinkResult, (Success, NoProvidedLib, NoAVCodecVersion, CannotUseLibAV57,
     45                   BlockedOldLibAVVersion, UnknownFutureLibAVVersion,
     46                   UnknownFutureFFMpegVersion, UnknownOlderFFMpegVersion,
     47                   MissingFFMpegFunction, MissingLibAVFunction));
     48 
     49  // Examine mAVCodecLib and mAVUtilLib, and attempt to resolve
     50  // all symbols.
     51  // Upon failure, the entire object will be reset and any attached libraries
     52  // will be unlinked.
     53  LinkResult Link();
     54 
     55  // Reset the wrapper and unlink all attached libraries.
     56  void Unlink();
     57 
     58 #ifdef MOZ_WIDGET_GTK
     59  // Check if libva and libva-drm are available and we can use HW decode.
     60  bool IsVAAPIAvailable() const;
     61 #endif
     62 
     63  // Helpers for libavcodec/util logging to integrate with MOZ_LOG.
     64  static int ToLibLogLevel(LogLevel aLevel);
     65  static LogLevel FromLibLogLevel(int aLevel);
     66  static void Log(void* aPtr, int aLevel, const char* aFmt, va_list aArgs);
     67  void UpdateLogLevel();
     68  static void RegisterCallbackLogLevel(PrefChangedFunc aCallback);
     69 
     70  // indicate the version of libavcodec linked to.
     71  // 0 indicates that the function wasn't initialized with Link().
     72  int mVersion;
     73 
     74  // libavcodec
     75  unsigned (*avcodec_version)();
     76  int (*av_lockmgr_register)(int (*cb)(void** mutex, int op));
     77  AVCodecContext* (*avcodec_alloc_context3)(const AVCodec* codec);
     78  int (*avcodec_close)(AVCodecContext* avctx);
     79  int (*avcodec_decode_audio4)(AVCodecContext* avctx, AVFrame* frame,
     80                               int* got_frame_ptr, const AVPacket* avpkt);
     81  int (*avcodec_decode_video2)(AVCodecContext* avctx, AVFrame* picture,
     82                               int* got_picture_ptr, const AVPacket* avpkt);
     83  AVCodec* (*avcodec_find_decoder)(int id);
     84  AVCodec* (*avcodec_find_decoder_by_name)(const char* name);
     85  AVCodec* (*avcodec_find_encoder)(int id);
     86  AVCodec* (*avcodec_find_encoder_by_name)(const char* name);
     87  void (*avcodec_flush_buffers)(AVCodecContext* avctx);
     88  int (*avcodec_open2)(AVCodecContext* avctx, const AVCodec* codec,
     89                       AVDictionary** options);
     90  void (*avcodec_register_all)();
     91  void (*av_init_packet)(AVPacket* pkt);
     92  AVCodecParserContext* (*av_parser_init)(int codec_id);
     93  void (*av_parser_close)(AVCodecParserContext* s);
     94  int (*av_parser_parse2)(AVCodecParserContext* s, AVCodecContext* avctx,
     95                          uint8_t** poutbuf, int* poutbuf_size,
     96                          const uint8_t* buf, int buf_size, int64_t pts,
     97                          int64_t dts, int64_t pos);
     98  AVCodec* (*av_codec_iterate)(void** opaque);
     99  int (*av_codec_is_decoder)(const AVCodec* codec);
    100  int (*av_codec_is_encoder)(const AVCodec* codec);
    101  void (*avcodec_align_dimensions)(AVCodecContext* s, int* width, int* height);
    102  int (*av_strerror)(int errnum, char* errbuf, size_t errbuf_size);
    103  AVCodecDescriptor* (*avcodec_descriptor_get)(int id);
    104 
    105  // only used in libavcodec <= 54
    106  AVFrame* (*avcodec_alloc_frame)();
    107  void (*avcodec_get_frame_defaults)(AVFrame* pic);
    108 
    109  // libavcodec v54 only
    110  void (*avcodec_free_frame)(AVFrame** frame);
    111 
    112  // libavcodec >= v55
    113  int (*avcodec_default_get_buffer2)(AVCodecContext* s, AVFrame* frame,
    114                                     int flags);
    115 
    116  // libavcodec >= v57
    117  void (*av_packet_unref)(AVPacket* pkt);
    118  void (*av_packet_free)(AVPacket** pkt);
    119  void (*avcodec_free_context)(AVCodecContext** avctx);
    120 
    121  // libavcodec >= 61
    122  AVPacket* (*av_packet_alloc)();
    123 
    124  // libavcodec v58 and later only
    125  int (*avcodec_send_packet)(AVCodecContext* avctx, const AVPacket* avpkt);
    126  int (*avcodec_receive_packet)(AVCodecContext* avctx, AVPacket* avpkt);
    127  int (*avcodec_send_frame)(AVCodecContext* avctx, const AVFrame* frame);
    128  int (*avcodec_receive_frame)(AVCodecContext* avctx, AVFrame* frame);
    129 
    130  // libavutil
    131  void (*av_log_set_callback)(void (*callback)(void*, int, const char*,
    132                                               va_list));
    133  void (*av_log_set_level)(int level);
    134  void* (*av_malloc)(size_t size);
    135  void (*av_freep)(void* ptr);
    136  int (*av_image_check_size)(unsigned int w, unsigned int h, int log_offset,
    137                             void* log_ctx);
    138  int (*av_image_get_buffer_size)(int pix_fmt, int width, int height,
    139                                  int align);
    140  const char* (*av_get_sample_fmt_name)(int sample_fmt);
    141  void (*av_channel_layout_default)(AVChannelLayout* ch_layout,
    142                                    int nb_channels);
    143  void (*av_channel_layout_from_mask)(AVChannelLayout* ch_layout,
    144                                      uint64_t mask);
    145  int (*av_channel_layout_copy)(AVChannelLayout* dst, AVChannelLayout* src);
    146  int (*av_dict_set)(AVDictionary** pm, const char* key, const char* value,
    147                     int flags);
    148  void (*av_dict_free)(AVDictionary** m);
    149  int (*av_opt_set)(void* obj, const char* name, const char* val,
    150                    int search_flags);
    151  int (*av_opt_set_double)(void* obj, const char* name, double val,
    152                           int search_flags);
    153  int (*av_opt_set_int)(void* obj, const char* name, int64_t val,
    154                        int search_flags);
    155 
    156  // libavutil v55 and later only
    157  AVFrame* (*av_frame_alloc)();
    158  AVFrame* (*av_frame_clone)(const AVFrame* frame);
    159  void (*av_frame_free)(AVFrame** frame);
    160  void (*av_frame_unref)(AVFrame* frame);
    161  int (*av_frame_get_buffer)(AVFrame* frame, int align);
    162  int (*av_frame_make_writable)(AVFrame* frame);
    163  AVBufferRef* (*av_buffer_create)(uint8_t* data, int size,
    164                                   void (*free)(void* opaque, uint8_t* data),
    165                                   void* opaque, int flags);
    166 
    167  // libavutil >= v56
    168  void* (*av_buffer_get_opaque)(const AVBufferRef* buf);
    169 
    170  // libavutil optional
    171  int (*av_frame_get_colorspace)(const AVFrame* frame);
    172  int (*av_frame_get_color_range)(const AVFrame* frame);
    173 
    174  // libavcodec > 58
    175  const AVCodecHWConfig* (*avcodec_get_hw_config)(const AVCodec* codec,
    176                                                  int index);
    177  // libavutil >= 58
    178  AVBufferRef* (*av_hwdevice_ctx_alloc)(int);
    179  int (*av_hwdevice_ctx_init)(AVBufferRef* ref);
    180  AVBufferRef* (*av_hwframe_ctx_alloc)(AVBufferRef* device_ctx);
    181  int (*av_hwframe_ctx_init)(AVBufferRef* ref);
    182  AVBufferRef* (*av_buffer_ref)(AVBufferRef* buf);
    183  void (*av_buffer_unref)(AVBufferRef** buf);
    184 
    185 #ifdef MOZ_WIDGET_GTK
    186  AVVAAPIHWConfig* (*av_hwdevice_hwconfig_alloc)(AVBufferRef* device_ctx);
    187  AVHWFramesConstraints* (*av_hwdevice_get_hwframe_constraints)(
    188      AVBufferRef* ref, const void* hwconfig);
    189  void (*av_hwframe_constraints_free)(AVHWFramesConstraints** constraints);
    190  int (*av_hwframe_transfer_get_formats)(AVBufferRef* hwframe_ctx, int dir,
    191                                         int** formats, int flags);
    192  int (*av_hwdevice_ctx_create_derived)(AVBufferRef** dst_ctx, int type,
    193                                        AVBufferRef* src_ctx, int flags);
    194  const char* (*avcodec_get_name)(int id);
    195  char* (*av_get_pix_fmt_string)(char* buf, int buf_size, int pix_fmt);
    196 #endif
    197 
    198 #if defined(MOZ_WIDGET_ANDROID)
    199  int (*av_mediacodec_release_buffer)(AVMediaCodecBuffer*, int);
    200  int (*moz_avcodec_mediacodec_is_eos)(AVCodecContext*);
    201 #endif
    202 
    203  // Only ever used with ffvpx
    204  decltype(::av_tx_init)* av_tx_init;
    205  decltype(::av_tx_uninit)* av_tx_uninit;
    206 
    207  PRLibrary* mAVCodecLib;
    208  PRLibrary* mAVUtilLib;
    209 };
    210 
    211 }  // namespace mozilla
    212 
    213 #endif  // FFmpegLibWrapper