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 "mozilla/Attributes.h"
9 #include "mozilla/Types.h"
10 
11 struct AVCodec;
12 struct AVCodecContext;
13 struct AVFrame;
14 struct AVPacket;
15 struct AVDictionary;
16 struct AVCodecParserContext;
17 struct PRLibrary;
18 
19 namespace mozilla {
20 
21 struct MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS FFmpegLibWrapper {
22   // The class is used only in static storage and so is zero initialized.
23   FFmpegLibWrapper() = default;
24   // The libraries are not unloaded in the destructor, because doing so would
25   // require a static constructor to register the static destructor.  As the
26   // class is in static storage, the destructor would only run on shutdown
27   // anyway.
28   ~FFmpegLibWrapper() = default;
29 
30   enum class LinkResult {
31     Success,
32     NoProvidedLib,
33     NoAVCodecVersion,
34     CannotUseLibAV57,
35     BlockedOldLibAVVersion,
36     UnknownFutureLibAVVersion,
37     UnknownFutureFFMpegVersion,
38     UnknownOlderFFMpegVersion,
39     MissingFFMpegFunction,
40     MissingLibAVFunction,
41   };
42   // Examine mAVCodecLib and mAVUtilLib, and attempt to resolve all symbols.
43   // Upon failure, the entire object will be reset and any attached libraries
44   // will be unlinked.
45   LinkResult Link();
46 
47   // Reset the wrapper and unlink all attached libraries.
48   void Unlink();
49 
50   // indicate the version of libavcodec linked to.
51   // 0 indicates that the function wasn't initialized with Link().
52   int mVersion;
53 
54   // libavcodec
55   unsigned (*avcodec_version)();
56   int (*av_lockmgr_register)(int (*cb)(void** mutex, int op));
57   AVCodecContext* (*avcodec_alloc_context3)(const AVCodec* codec);
58   int (*avcodec_close)(AVCodecContext* avctx);
59   int (*avcodec_decode_audio4)(AVCodecContext* avctx, AVFrame* frame,
60                                int* got_frame_ptr, const AVPacket* avpkt);
61   int (*avcodec_decode_video2)(AVCodecContext* avctx, AVFrame* picture,
62                                int* got_picture_ptr, const AVPacket* avpkt);
63   AVCodec* (*avcodec_find_decoder)(int id);
64   void (*avcodec_flush_buffers)(AVCodecContext* avctx);
65   int (*avcodec_open2)(AVCodecContext* avctx, const AVCodec* codec,
66                        AVDictionary** options);
67   void (*avcodec_register_all)();
68   void (*av_init_packet)(AVPacket* pkt);
69   AVCodecParserContext* (*av_parser_init)(int codec_id);
70   void (*av_parser_close)(AVCodecParserContext* s);
71   int (*av_parser_parse2)(AVCodecParserContext* s, AVCodecContext* avctx,
72                           uint8_t** poutbuf, int* poutbuf_size,
73                           const uint8_t* buf, int buf_size, int64_t pts,
74                           int64_t dts, int64_t pos);
75 
76   // only used in libavcodec <= 54
77   AVFrame* (*avcodec_alloc_frame)();
78   void (*avcodec_get_frame_defaults)(AVFrame* pic);
79   // libavcodec v54 only
80   void (*avcodec_free_frame)(AVFrame** frame);
81 
82   // libavutil
83   void (*av_log_set_level)(int level);
84   void* (*av_malloc)(size_t size);
85   void (*av_freep)(void* ptr);
86 
87   // libavutil v55 and later only
88   AVFrame* (*av_frame_alloc)();
89   void (*av_frame_free)(AVFrame** frame);
90   void (*av_frame_unref)(AVFrame* frame);
91 
92   // libavutil optional
93   int (*av_frame_get_colorspace)(const AVFrame* frame);
94 
95   PRLibrary* mAVCodecLib;
96   PRLibrary* mAVUtilLib;
97 
98  private:
99 };
100 
101 }  // namespace mozilla
102 
103 #endif  // FFmpegLibWrapper
104