1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FFMPEG_API_WRAP_H
17 #define FFMPEG_API_WRAP_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 #include "libavformat/avformat.h"
23 #include "libavcodec/avcodec.h"
24 #include "libavutil/avutil.h"
25 #include "libswresample/swresample.h"
26 #ifdef __cplusplus
27 }
28 #endif
29 
30 namespace OHOS {
31 namespace Media {
32 namespace MediaMonitor {
33 
34 using CodecFindEncoderFunc = AVCodec *(*)(enum AVCodecID id);
35 using CodecAllocContextFunc = AVCodecContext *(*)(const AVCodec *codec);
36 using CodecOpenFunc = int (*)(AVCodecContext *avctx,
37         const AVCodec *codec, AVDictionary **options);
38 using FormatAllocOutputContextFunc = int (*)(AVFormatContext **ctx,
39         const AVOutputFormat *oformat,
40         const char *formatName,
41         const char *filename);
42 using FormatNewStreamFunc = AVStream *(*)(AVFormatContext *s, const AVCodec *c);
43 using CodecParamFromContextFunc = int (*)(AVCodecParameters *par,
44         const AVCodecContext *codec);
45 using IoOpenFunc = int (*)(AVIOContext **s, const char *url, int flags,
46         const AVIOInterruptCB *intCb, AVDictionary **options);
47 using IoFlushFunc = void (*)(AVIOContext *s);
48 using IoCloseFunc = int (*)(AVIOContext *s);
49 
50 using FormatWriteHeaderFunc = int (*)(AVFormatContext *s, AVDictionary **options);
51 using FormatWriteTrailerFunc = int (*)(AVFormatContext *s);
52 
53 using PacketAllocFunc = AVPacket *(*)(void);
54 using FrameAllocFunc = AVFrame *(*)(void);
55 using FrameGetBufferFunc = int (*)(AVFrame *frame, int align);
56 
57 using CodecSendFrameFunc = int (*)(AVCodecContext *avctx, const AVFrame *frame);
58 using CodecRecvPacketFunc = int (*)(AVCodecContext *avctx, AVPacket *avpkt);
59 using FormatWriteFrameFunc = int (*)(AVFormatContext *s, AVPacket *pkt);
60 using FormatFlushFunc = int (*)(AVFormatContext *s);
61 
62 using FormatFreeContextFunc = void (*)(AVFormatContext *s);
63 using CodecFreeContextFunc = void(*)(AVCodecContext **avctx);
64 
65 using FrameFreeFunc = void (*)(AVFrame **frame);
66 using PacketFreeFunc = void (*)(AVPacket **pkt);
67 using PacketUnrefFunc = void (*)(AVPacket *pkt);
68 
69 using GetChannelLayoutFunc = int64_t (*)(int nbChannels);
70 using GetBytesPerSampleFunc = int (*)(enum AVSampleFormat sampleFmt);
71 using SampleFmtIsPlannarFunc = int (*)(enum AVSampleFormat sampleFmt);
72 
73 using SwrSetOptsFunc = struct SwrContext *(*)(struct SwrContext *s,
74         int64_t outChLayout, enum AVSampleFormat outSampleFmt, int outSampleRate,
75         int64_t inChLayout, enum AVSampleFormat inSampleFmt, int inSampleRate,
76         int logOffset, void *logCtx);
77 using SwrInitFunc = int (*)(struct SwrContext *s);
78 using SwrAllocFunc = struct SwrContext *(*)();
79 using SwrFreeFunc = void (*)(struct SwrContext **s);
80 using SwrConvertFunc = int (*)(struct SwrContext *s,
81     uint8_t **out, int outCount, const uint8_t **in, int inCount);
82 
83 class FFmpegApiWrap {
84 public:
85     FFmpegApiWrap();
86     ~FFmpegApiWrap();
87     bool Open();
88     void Close();
89     AVCodec *CodecFindEncoder(AVCodecID id);
90     AVCodecContext *CodecAllocContext(const AVCodec *codec);
91     int CodecOpen(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
92 
93     int FormatAllocOutputContext(AVFormatContext **ctx,
94         const AVOutputFormat *oformat, const char *formatName, const char *filename);
95     AVStream *FormatNewStream(AVFormatContext *s, const AVCodec *c);
96     int CodecParamFromContext(AVCodecParameters *par, const AVCodecContext *codec);
97     int IoOpen(AVIOContext **s, const char *url, int flags,
98         const AVIOInterruptCB *intCb, AVDictionary **options);
99     void IoFlush(AVIOContext *s);
100     int IoClose(AVIOContext *s);
101     int FormatWriteHeader(AVFormatContext *s, AVDictionary **options);
102     int FormatWriteTrailer(AVFormatContext *s);
103     int CodecSendFrame(AVCodecContext *avctx, const AVFrame *frame);
104     int CodecRecvPacket(AVCodecContext *avctx, AVPacket *avpkt);
105     int FormatWriteFrame(AVFormatContext *s, AVPacket *pkt);
106     int FormatFlush(AVFormatContext *s);
107     void FormatFreeContext(AVFormatContext *s);
108     void CodecFreeContext(AVCodecContext **avctx);
109 
110     AVPacket *PacketAlloc();
111     AVFrame *FrameAlloc();
112     int FrameGetBuffer(AVFrame *frame, int align);
113     void FrameFree(AVFrame **frame);
114     void PacketFree(AVPacket **pkt);
115     void PacketUnref(AVPacket *pkt);
116 
117     int64_t GetChannelLayout(int nbChannels);
118     int GetBytesPerSample(AVSampleFormat sampleFmt);
119     int SampleFmtIsPlannar(AVSampleFormat sampleFmt);
120     struct SwrContext *SwrAlloc();
121     struct SwrContext *SwrSetOpts(struct SwrContext *s,
122         int64_t outChLayout, AVSampleFormat outSampleFmt, int outSampleRate,
123         int64_t inChLayout, AVSampleFormat inSampleFmt, int inSampleRate,
124         int logOffset, void *logCtx);
125     int SwrInit(struct SwrContext *s);
126     void SwrFree(struct SwrContext **s);
127     int SwrConvert(struct SwrContext *s,
128         uint8_t **out, int outCount, const uint8_t **in, int inCount);
129 private:
130     bool LoadFormatApi();
131     bool LoadCodecApi();
132     bool LoadUtilsApi();
133     bool LoadResampleApi();
134     void *handler = nullptr;
135     FormatAllocOutputContextFunc foramtAllocOutputFunc = nullptr;
136     FormatNewStreamFunc formatNewStreamFunc = nullptr;
137     FormatWriteHeaderFunc formatWriteHeaderFunc = nullptr;
138     FormatWriteTrailerFunc formatWriteTrailerFunc = nullptr;
139     FormatWriteFrameFunc writeFrameFunc = nullptr;
140     FormatFlushFunc formatFlushFunc = nullptr;
141     FormatFreeContextFunc formatFreeContextFunc = nullptr;
142     IoOpenFunc ioOpenFunc = nullptr;
143     IoFlushFunc ioFlushFunc = nullptr;
144     IoCloseFunc ioCloseFunc = nullptr;
145 
146     CodecFindEncoderFunc codecFindFunc = nullptr;
147     CodecAllocContextFunc codecAllocFunc = nullptr;
148     CodecOpenFunc codecOpenFunc = nullptr;
149     CodecParamFromContextFunc codecParamFromContextFunc = nullptr;
150     CodecSendFrameFunc sendFrameFunc = nullptr;
151     CodecRecvPacketFunc recvPacketFunc = nullptr;
152     CodecFreeContextFunc codecFreeContextFunc = nullptr;
153 
154     PacketAllocFunc packetAllocFunc = nullptr;
155     PacketFreeFunc packetFreeFunc = nullptr;
156     PacketUnrefFunc packetUnrefFunc = nullptr;
157 
158     FrameAllocFunc frameAllocFunc = nullptr;
159     FrameGetBufferFunc frameGetBufferFunc = nullptr;
160     FrameFreeFunc frameFreeFunc = nullptr;
161     GetChannelLayoutFunc getChannelLayoutFunc = nullptr;
162     GetBytesPerSampleFunc getBytesPerSampleFunc = nullptr;
163     SampleFmtIsPlannarFunc sampleFmtIsPlannarFunc = nullptr;
164 
165     SwrSetOptsFunc swrSetOptsFunc = nullptr;
166     SwrAllocFunc swrAllocFunc = nullptr;
167     SwrFreeFunc swrFreeFunc = nullptr;
168     SwrInitFunc swrInitFunc = nullptr;
169     SwrConvertFunc swrConvertFunc = nullptr;
170 };
171 
172 } // namespace MediaMonitor
173 } // namespace Media
174 } // namespace OHOS
175 #endif // FFMPEG_API_WRAP_H