• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_VIDEO_FFMPEG_DECODER_PLUGIN_H
17 #define HISTREAMER_VIDEO_FFMPEG_DECODER_PLUGIN_H
18 
19 #ifdef VIDEO_SUPPORT
20 
21 #include <functional>
22 #include <map>
23 #include "osal/thread/task.h"
24 #include "utils/blocking_queue.h"
25 #include "plugin/interface/codec_plugin.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #include "libavcodec/avcodec.h"
31 #include "libswscale/swscale.h"
32 #include "libavutil/imgutils.h"
33 #ifdef __cplusplus
34 };
35 #endif
36 
37 #ifdef DUMP_RAW_DATA
38 #include <cstdio>
39 #endif
40 
41 namespace OHOS {
42 namespace Media {
43 namespace Plugin {
44 namespace Ffmpeg {
45 class VideoFfmpegDecoderPlugin : public CodecPlugin {
46 public:
47     explicit VideoFfmpegDecoderPlugin(std::string name);
48     ~VideoFfmpegDecoderPlugin() override = default;
49 
50     Status Init() override;
51     Status Deinit() override;
52     Status Prepare() override;
53     Status Reset() override;
54     Status Start() override;
55     Status Stop() override;
56     Status GetParameter(Tag tag, ValueType& value) override;
57     Status SetParameter(Tag tag, const ValueType& value) override;
58 
59     std::shared_ptr<Allocator> GetAllocator() override;
60 
SetCallback(Callback * cb)61     Status SetCallback(Callback* cb) override
62     {
63         return Status::OK;
64     }
65 
66     Status QueueInputBuffer(const std::shared_ptr<Buffer>& inputBuffer, int32_t timeoutMs) override;
67 
68     Status QueueOutputBuffer(const std::shared_ptr<Buffer>& outputBuffers, int32_t timeoutMs) override;
69 
70     Status Flush() override;
71 
SetDataCallback(DataCallback * dataCallback)72     Status SetDataCallback(DataCallback* dataCallback) override
73     {
74         dataCb_ = dataCallback;
75         return Status::OK;
76     }
77 
78 private:
79     Status CreateCodecContext();
80 
81     void InitCodecContext();
82 
83     void DeinitCodecContext();
84 
85     void SetCodecExtraData();
86 
87     Status OpenCodecContext();
88 
89     Status CloseCodecContext();
90 
91     Status ResetLocked();
92 
93     template<typename T>
94     void FindInParameterMapThenAssignLocked(Tag tag, T& assign);
95 
96     Status SendBufferLocked(const std::shared_ptr<Buffer>& inputBuffer);
97 
98     Status CreateSwsContext();
99 
100     Status ScaleVideoFrame();
101 
102     Status WriteYuvData(const std::shared_ptr<Buffer>& frameBuffer);
103 
104     Status WriteRgbData(const std::shared_ptr<Buffer>& frameBuffer);
105 
106 #ifndef OHOS_LITE
107     Status WriteYuvDataStride(const std::shared_ptr<Buffer>& frameBuffer, int32_t stride);
108 
109     Status WriteRgbDataStride(const std::shared_ptr<Buffer>& frameBuffer, int32_t stride);
110 #endif
111 
112     Status FillFrameBuffer(const std::shared_ptr<Buffer>& frameBuffer);
113 
114     Status ReceiveBufferLocked(const std::shared_ptr<Buffer>& frameBuffer);
115 
116     void ReceiveFrameBuffer();
117 
118 #ifdef DUMP_RAW_DATA
119     std::FILE* dumpFd_;
120     void DumpVideoRawOutData();
121 #endif
122 
123     void NotifyInputBufferDone(const std::shared_ptr<Buffer>& input);
124 
125     void NotifyOutputBufferDone(const std::shared_ptr<Buffer>& output);
126 
127     std::shared_ptr<const AVCodec> avCodec_;
128     std::shared_ptr<struct SwsContext> swsCtx_ {nullptr};
129     std::map<Tag, ValueType> videoDecParams_ {};
130     std::vector<uint8_t> paddedBuffer_;
131     size_t paddedBufferSize_ {0};
132     std::shared_ptr<AVFrame> cachedFrame_ {nullptr};
133     std::shared_ptr<AVPacket> avPacket_ {};
134 
135     uint8_t* scaleData_[AV_NUM_DATA_POINTERS];
136     int32_t scaleLineSize_[AV_NUM_DATA_POINTERS];
137     bool isAllocScaleData_ {false};
138     DataCallback* dataCb_ {};
139 
140     uint32_t width_;
141     uint32_t height_;
142     VideoPixelFormat pixelFormat_;
143 
144     mutable OSAL::Mutex avMutex_ {};
145     State state_ {State::CREATED};
146     std::shared_ptr<AVCodecContext> avCodecContext_ {};
147     OHOS::Media::BlockingQueue<std::shared_ptr<Buffer>> outBufferQ_;
148     std::shared_ptr<OHOS::Media::OSAL::Task> decodeTask_;
149 };
150 }
151 }
152 }
153 }
154 #endif
155 #endif // HISTREAMER_VIDEO_FFMPEG_DECODER_PLUGIN_H
156