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 "FFmpegRDFTTypes.h"  // for AvRdftInitFn, etc.
9 #include "mozilla/Attributes.h"
10 #include "mozilla/Types.h"
11 
12 struct AVCodec;
13 struct AVCodecContext;
14 struct AVFrame;
15 struct AVPacket;
16 struct AVDictionary;
17 struct AVCodecParserContext;
18 struct PRLibrary;
19 #ifdef MOZ_WAYLAND
20 struct AVCodecHWConfig;
21 struct AVBufferRef;
22 #endif
23 
24 namespace mozilla {
25 
26 struct MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FFmpegLibWrapper {
27   // The class is used only in static storage and so is zero initialized.
28   FFmpegLibWrapper() = default;
29   // The libraries are not unloaded in the destructor, because doing so would
30   // require a static constructor to register the static destructor.  As the
31   // class is in static storage, the destructor would only run on shutdown
32   // anyway.
33   ~FFmpegLibWrapper() = default;
34 
35   enum class LinkResult {
36     Success,
37     NoProvidedLib,
38     NoAVCodecVersion,
39     CannotUseLibAV57,
40     BlockedOldLibAVVersion,
41     UnknownFutureLibAVVersion,
42     UnknownFutureFFMpegVersion,
43     UnknownOlderFFMpegVersion,
44     MissingFFMpegFunction,
45     MissingLibAVFunction,
46   };
47   // Examine mAVCodecLib, mAVUtilLib and mVALib, and attempt to resolve
48   // all symbols.
49   // Upon failure, the entire object will be reset and any attached libraries
50   // will be unlinked.
51   LinkResult Link();
52 
53   // Reset the wrapper and unlink all attached libraries.
54   void Unlink();
55 
56 #ifdef MOZ_WAYLAND
57   // Check if mVALib are available and we can use HW decode.
58   bool IsVAAPIAvailable();
59 #endif
60 
61   // indicate the version of libavcodec linked to.
62   // 0 indicates that the function wasn't initialized with Link().
63   int mVersion;
64 
65   // libavcodec
66   unsigned (*avcodec_version)();
67   int (*av_lockmgr_register)(int (*cb)(void** mutex, int op));
68   AVCodecContext* (*avcodec_alloc_context3)(const AVCodec* codec);
69   int (*avcodec_close)(AVCodecContext* avctx);
70   int (*avcodec_decode_audio4)(AVCodecContext* avctx, AVFrame* frame,
71                                int* got_frame_ptr, const AVPacket* avpkt);
72   int (*avcodec_decode_video2)(AVCodecContext* avctx, AVFrame* picture,
73                                int* got_picture_ptr, const AVPacket* avpkt);
74   AVCodec* (*avcodec_find_decoder)(int id);
75   void (*avcodec_flush_buffers)(AVCodecContext* avctx);
76   int (*avcodec_open2)(AVCodecContext* avctx, const AVCodec* codec,
77                        AVDictionary** options);
78   void (*avcodec_register_all)();
79   void (*av_init_packet)(AVPacket* pkt);
80   AVCodecParserContext* (*av_parser_init)(int codec_id);
81   void (*av_parser_close)(AVCodecParserContext* s);
82   int (*av_parser_parse2)(AVCodecParserContext* s, AVCodecContext* avctx,
83                           uint8_t** poutbuf, int* poutbuf_size,
84                           const uint8_t* buf, int buf_size, int64_t pts,
85                           int64_t dts, int64_t pos);
86 
87   // only used in libavcodec <= 54
88   AVFrame* (*avcodec_alloc_frame)();
89   void (*avcodec_get_frame_defaults)(AVFrame* pic);
90   // libavcodec v54 only
91   void (*avcodec_free_frame)(AVFrame** frame);
92 
93   // libavcodec v58 and later only
94   int (*avcodec_send_packet)(AVCodecContext* avctx, const AVPacket* avpkt);
95   int (*avcodec_receive_frame)(AVCodecContext* avctx, AVFrame* frame);
96 
97   // libavcodec optional
98   AvRdftInitFn av_rdft_init;
99   AvRdftCalcFn av_rdft_calc;
100   AvRdftEndFn av_rdft_end;
101 
102   // libavutil
103   void (*av_log_set_level)(int level);
104   void* (*av_malloc)(size_t size);
105   void (*av_freep)(void* ptr);
106 
107   // libavutil v55 and later only
108   AVFrame* (*av_frame_alloc)();
109   void (*av_frame_free)(AVFrame** frame);
110   void (*av_frame_unref)(AVFrame* frame);
111 
112   // libavutil optional
113   int (*av_frame_get_colorspace)(const AVFrame* frame);
114   int (*av_frame_get_color_range)(const AVFrame* frame);
115 
116 #ifdef MOZ_WAYLAND
117   const AVCodecHWConfig* (*avcodec_get_hw_config)(const AVCodec* codec,
118                                                   int index);
119   AVBufferRef* (*av_hwdevice_ctx_alloc)(int);
120   int (*av_hwdevice_ctx_init)(AVBufferRef* ref);
121 
122   AVBufferRef* (*av_buffer_ref)(AVBufferRef* buf);
123   void (*av_buffer_unref)(AVBufferRef** buf);
124   int (*av_hwframe_transfer_get_formats)(AVBufferRef* hwframe_ctx, int dir,
125                                          int** formats, int flags);
126   int (*av_hwdevice_ctx_create_derived)(AVBufferRef** dst_ctx, int type,
127                                         AVBufferRef* src_ctx, int flags);
128   AVBufferRef* (*av_hwframe_ctx_alloc)(AVBufferRef* device_ctx);
129   int (*av_dict_set)(AVDictionary** pm, const char* key, const char* value,
130                      int flags);
131   void (*av_dict_free)(AVDictionary** m);
132 
133   int (*vaExportSurfaceHandle)(void*, unsigned int, uint32_t, uint32_t, void*);
134   int (*vaSyncSurface)(void*, unsigned int);
135   int (*vaInitialize)(void* dpy, int* major_version, int* minor_version);
136   int (*vaTerminate)(void* dpy);
137   void* (*vaGetDisplayWl)(struct wl_display* display);
138 #endif
139 
140   PRLibrary* mAVCodecLib;
141   PRLibrary* mAVUtilLib;
142 #ifdef MOZ_WAYLAND
143   PRLibrary* mVALib;
144   PRLibrary* mVALibWayland;
145 #endif
146 
147  private:
148 };
149 
150 }  // namespace mozilla
151 
152 #endif  // FFmpegLibWrapper
153