Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited
9 : *
10 : * This program is free software; you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #if (defined (_MSCVER) || defined (_MSC_VER))
26 : #pragma warning(push)
27 : #pragma warning(disable: 4244)
28 : #endif
29 :
30 : extern "C" {
31 : #include "thirdparty/ffmpeg/v6/libavcodec/avcodec.h"
32 : #include "thirdparty/ffmpeg/v6/libavformat/avformat.h"
33 : #include "thirdparty/ffmpeg/v6/libswscale/swscale.h"
34 : }
35 :
36 : #if (defined (_MSCVER) || defined (_MSC_VER))
37 : #pragma warning(pop)
38 : #endif
39 :
40 : #include "io/path.h"
41 :
42 : namespace muse::media::ffmpeg::v6 {
43 : class FFmpegLibHandler
44 : {
45 : public:
46 0 : FFmpegLibHandler() = default;
47 : ~FFmpegLibHandler();
48 : FFmpegLibHandler(const FFmpegLibHandler&) = delete;
49 : FFmpegLibHandler& operator=(const FFmpegLibHandler&) = delete;
50 :
51 : bool loadLib(const io::path_t& avUtilPath, const io::path_t& avCodecPath, const io::path_t& avFormatPath, const io::path_t& swScalePath,
52 : const io::path_t& swResamplePath);
53 : bool loadApi();
54 : void unload();
55 :
56 : // libavutil
57 : int64_t (*av_rescale_q)(int64_t a, AVRational bq, AVRational cq) = nullptr;
58 : int (*av_image_fill_arrays)(uint8_t* dst_data[4], int dst_linesize[4], const uint8_t* src, AVPixelFormat pix_fmt, int width, int height,
59 : int align) = nullptr;
60 : int (*av_image_get_buffer_size)(AVPixelFormat pix_fmt, int width, int height, int align) = nullptr;
61 : int (*av_opt_set_int)(void* obj, const char* name, int64_t val, int search_flags) = nullptr;
62 : void*(*av_malloc)(size_t size) = nullptr;
63 :
64 : // libavformat
65 : AVStream*(*avformat_new_stream)(AVFormatContext* s, const AVCodec* c) = nullptr;
66 : int (*avformat_write_header)(AVFormatContext* s, AVDictionary** options) = nullptr;
67 : int (*av_write_trailer)(AVFormatContext* s) = nullptr;
68 : int (*avio_open)(AVIOContext** s, const char* url, int flags) = nullptr;
69 : int (*avio_close)(AVIOContext* s) = nullptr;
70 : int (*av_interleaved_write_frame)(AVFormatContext* s, AVPacket* pkt) = nullptr;
71 : int (*avformat_open_input)(AVFormatContext** ctx, const char* url, const AVInputFormat* fmt, AVDictionary** options) = nullptr;
72 : int (*avformat_find_stream_info)(AVFormatContext* ic, AVDictionary** options) = nullptr;
73 : void (*avformat_close_input)(AVFormatContext** s) = nullptr;
74 : void (*avformat_free_context)(AVFormatContext* s) = nullptr;
75 : int (*avformat_alloc_output_context2)(AVFormatContext** ctx, const AVOutputFormat* oformat, const char* format_name,
76 : const char* filename) = nullptr;
77 : int (*av_read_frame)(AVFormatContext* s, AVPacket* pkt) = nullptr;
78 : AVFormatContext*(*avformat_alloc_context)(void) = nullptr;
79 : AVIOContext*(*avio_alloc_context)(unsigned char* buffer, int buffer_size, int write_flag, void* opaque,
80 : int (*read_packet)(void* opaque, uint8_t* buf, int buf_size),
81 : int (*write_packet)(void* opaque, const uint8_t* buf, int buf_size),
82 : int64_t (*seek)(void* opaque, int64_t offset, int whence)) = nullptr;
83 : void (*avio_context_free)(AVIOContext** s) = nullptr;
84 :
85 : // libavcodec
86 : AVCodecContext*(*avcodec_alloc_context3)(const AVCodec* codec) = nullptr;
87 : const AVCodec*(*avcodec_find_encoder)(AVCodecID id) = nullptr;
88 : const AVCodec*(*avcodec_find_decoder)(AVCodecID id) = nullptr;
89 : void (*avcodec_free_context)(AVCodecContext** avctx) = nullptr;
90 : int (*avcodec_open2)(AVCodecContext* avctx, const AVCodec* codec, AVDictionary** options) = nullptr;
91 : int (*avcodec_parameters_from_context)(AVCodecParameters* par, const AVCodecContext* codec) = nullptr;
92 : int (*avcodec_parameters_to_context)(AVCodecContext* codec, const AVCodecParameters* par) = nullptr;
93 : int (*avcodec_parameters_copy)(AVCodecParameters* dst, const AVCodecParameters* src) = nullptr;
94 : int (*avcodec_send_frame)(AVCodecContext* avctx, const AVFrame* frame) = nullptr;
95 : int (*avcodec_receive_packet)(AVCodecContext* avctx, AVPacket* avpkt) = nullptr;
96 : int (*avcodec_send_packet)(AVCodecContext* avctx, const AVPacket* avpkt) = nullptr;
97 : int (*avcodec_receive_frame)(AVCodecContext* avctx, AVFrame* frame) = nullptr;
98 : AVFrame*(*av_frame_alloc)(void) = nullptr;
99 : void (*av_frame_free)(AVFrame** frame) = nullptr;
100 : AVPacket*(*av_packet_alloc)(void) = nullptr;
101 : void (*av_packet_free)(AVPacket** pkt) = nullptr;
102 : void (*av_packet_unref)(AVPacket* pkt) = nullptr;
103 : void (*av_packet_rescale_ts)(AVPacket* pkt, AVRational tb_src, AVRational tb_dst) = nullptr;
104 :
105 : // libswscale
106 : SwsContext*(*sws_getCachedContext)(SwsContext* context, int srcW, int srcH, AVPixelFormat srcFormat, int dstW, int dstH,
107 : AVPixelFormat dstFormat, int flags, SwsFilter* srcFilter, SwsFilter* dstFilter,
108 : const double* param) = nullptr;
109 : int (*sws_scale)(struct SwsContext* c, const uint8_t* const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH,
110 : uint8_t* const dst[], const int dstStride[]) = nullptr;
111 : void (*sws_freeContext)(SwsContext* swsContext) = nullptr;
112 :
113 : private: void* getSymbol(void* lib, const char* name) const;
114 : bool tryLoadPath(void*& lib, const io::path_t& fullPath);
115 : void closeLib(void*& lib);
116 : void clearFunctions();
117 : bool functionsValid() const;
118 :
119 : void* m_avUtilLibrary = nullptr;
120 : void* m_avCodecLibrary = nullptr;
121 : void* m_avFormatLibrary = nullptr;
122 : void* m_swsScaleLibrary = nullptr;
123 : void* m_swResampleLibrary = nullptr;
124 : };
125 : }
|