• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include <list>
17 #include <mutex>
18 #include <queue>
19 #include <shared_mutex>
20 #include <unordered_map>
21 #include "avcodec_errors.h"
22 #include "avcodec_log.h"
23 #include "avcodec_trace.h"
24 #include "avcodec_video_decoder.h"
25 #include "buffer/avsharedmemory.h"
26 #include "common/native_mfmagic.h"
27 #include "native_avbuffer.h"
28 #include "native_avcodec_base.h"
29 #include "native_avcodec_videodecoder.h"
30 #include "native_avmagic.h"
31 #include "native_window.h"
32 
33 #ifdef SUPPORT_DRM
34 #include "native_drm_object.h"
35 #endif
36 namespace {
37 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "NativeVideoDecoder"};
38 constexpr size_t MAX_TEMPNUM = 64;
39 
40 using namespace OHOS::MediaAVCodec;
41 using namespace OHOS::Media;
42 using namespace OHOS::DrmStandard;
43 class NativeVideoDecoderCallback;
44 
45 struct VideoDecoderObject : public OH_AVCodec {
VideoDecoderObject__anonf40ef2270111::VideoDecoderObject46     explicit VideoDecoderObject(const std::shared_ptr<AVCodecVideoDecoder> &decoder)
47         : OH_AVCodec(AVMagic::AVCODEC_MAGIC_VIDEO_DECODER), videoDecoder_(decoder)
48     {
49     }
50     ~VideoDecoderObject() = default;
51 
52     void ClearBufferList();
53     void StopCallback();
54     void BufferToTempFunc(std::unordered_map<uint32_t, OHOS::sptr<OH_AVBuffer>> &tempMap);
55     void MemoryToTempFunc(std::unordered_map<uint32_t, OHOS::sptr<OH_AVMemory>> &tempMap);
56     OH_AVBuffer *GetTransData(const uint32_t &index, std::shared_ptr<AVBuffer> &buffer, bool isOutput);
57     OH_AVMemory *GetTransData(const uint32_t &index, std::shared_ptr<AVSharedMemory> &memory, bool isOutput);
58 
59     const std::shared_ptr<AVCodecVideoDecoder> videoDecoder_;
60     std::queue<OHOS::sptr<MFObjectMagic>> tempList_;
61     std::unordered_map<uint32_t, OHOS::sptr<OH_AVMemory>> outputMemoryMap_;
62     std::unordered_map<uint32_t, OHOS::sptr<OH_AVMemory>> inputMemoryMap_;
63     std::unordered_map<uint32_t, OHOS::sptr<OH_AVBuffer>> outputBufferMap_;
64     std::unordered_map<uint32_t, OHOS::sptr<OH_AVBuffer>> inputBufferMap_;
65     std::shared_ptr<NativeVideoDecoderCallback> callback_ = nullptr;
66     bool isSetMemoryCallback_ = false;
67     bool isOutputSurfaceMode_ = false;
68     std::shared_mutex objListMutex_;
69 };
70 
71 class NativeVideoDecoderCallback : public AVCodecCallback, public MediaCodecCallback {
72 public:
NativeVideoDecoderCallback(OH_AVCodec * codec,struct OH_AVCodecAsyncCallback cb,void * userData)73     NativeVideoDecoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
74         : codec_(codec), asyncCallback_(cb), userData_(userData)
75     {
76     }
NativeVideoDecoderCallback(OH_AVCodec * codec,struct OH_AVCodecCallback cb,void * userData)77     NativeVideoDecoderCallback(OH_AVCodec *codec, struct OH_AVCodecCallback cb, void *userData)
78         : codec_(codec), callback_(cb), userData_(userData)
79     {
80     }
81     virtual ~NativeVideoDecoderCallback() = default;
82 
OnError(AVCodecErrorType errorType,int32_t errorCode)83     void OnError(AVCodecErrorType errorType, int32_t errorCode) override
84     {
85         std::shared_lock<std::shared_mutex> lock(mutex_);
86         (void)errorType;
87 
88         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
89         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
90         CHECK_AND_RETURN_LOG(asyncCallback_.onError != nullptr || callback_.onError != nullptr, "Callback is nullptr");
91         int32_t extErr = AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(errorCode));
92         if (asyncCallback_.onError != nullptr) {
93             asyncCallback_.onError(codec_, extErr, userData_);
94         } else if (callback_.onError != nullptr) {
95             callback_.onError(codec_, extErr, userData_);
96         }
97     }
98 
OnOutputFormatChanged(const Format & format)99     void OnOutputFormatChanged(const Format &format) override
100     {
101         std::shared_lock<std::shared_mutex> lock(mutex_);
102 
103         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
104         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
105         CHECK_AND_RETURN_LOG(asyncCallback_.onStreamChanged != nullptr || callback_.onStreamChanged != nullptr,
106                              "Callback is nullptr");
107         OHOS::sptr<OH_AVFormat> object = new (std::nothrow) OH_AVFormat(format);
108         CHECK_AND_RETURN_LOG(object != nullptr, "OH_AVFormat create failed");
109         // The object lifecycle is controlled by the current function stack
110         if (asyncCallback_.onStreamChanged != nullptr) {
111             asyncCallback_.onStreamChanged(codec_, reinterpret_cast<OH_AVFormat *>(object.GetRefPtr()), userData_);
112         } else if (callback_.onStreamChanged != nullptr) {
113             callback_.onStreamChanged(codec_, reinterpret_cast<OH_AVFormat *>(object.GetRefPtr()), userData_);
114         }
115     }
116 
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVSharedMemory> buffer)117     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override
118     {
119         std::shared_lock<std::shared_mutex> lock(mutex_);
120 
121         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
122         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
123         CHECK_AND_RETURN_LOG(asyncCallback_.onNeedInputData != nullptr, "Callback is nullptr");
124 
125         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
126         CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
127 
128         OH_AVMemory *data = videoDecObj->GetTransData(index, buffer, false);
129         asyncCallback_.onNeedInputData(codec_, index, data, userData_);
130     }
131 
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag,std::shared_ptr<AVSharedMemory> buffer)132     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
133                                  std::shared_ptr<AVSharedMemory> buffer) override
134     {
135         std::shared_lock<std::shared_mutex> lock(mutex_);
136 
137         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
138         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
139         CHECK_AND_RETURN_LOG(asyncCallback_.onNeedOutputData != nullptr, "Callback is nullptr");
140 
141         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
142         CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
143 
144         struct OH_AVCodecBufferAttr bufferAttr {
145             info.presentationTimeUs, info.size, info.offset, flag
146         };
147         OH_AVMemory *data = nullptr;
148         if (!videoDecObj->isOutputSurfaceMode_) {
149             data = videoDecObj->GetTransData(index, buffer, true);
150         }
151 
152         asyncCallback_.onNeedOutputData(codec_, index, data, &bufferAttr, userData_);
153     }
154 
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)155     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
156     {
157         std::shared_lock<std::shared_mutex> lock(mutex_);
158 
159         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
160         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
161         CHECK_AND_RETURN_LOG(callback_.onNeedInputBuffer != nullptr, "Callback is nullptr");
162 
163         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
164         CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
165 
166         OH_AVBuffer *data = videoDecObj->GetTransData(index, buffer, false);
167         callback_.onNeedInputBuffer(codec_, index, data, userData_);
168     }
169 
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)170     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override
171     {
172         std::shared_lock<std::shared_mutex> lock(mutex_);
173 
174         CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
175         CHECK_AND_RETURN_LOG(codec_->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, "Codec magic error!");
176         CHECK_AND_RETURN_LOG(callback_.onNewOutputBuffer != nullptr, "Callback is nullptr");
177 
178         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
179         CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
180 
181         OH_AVBuffer *data = nullptr;
182         data = videoDecObj->GetTransData(index, buffer, true);
183 
184         callback_.onNewOutputBuffer(codec_, index, data, userData_);
185     }
186 
StopCallback()187     void StopCallback()
188     {
189         std::lock_guard<std::shared_mutex> lock(mutex_);
190         codec_ = nullptr;
191     }
192 
UpdateCallback(const struct OH_AVCodecAsyncCallback & cb,void * userData)193     void UpdateCallback(const struct OH_AVCodecAsyncCallback &cb, void *userData)
194     {
195         std::lock_guard<std::shared_mutex> lock(mutex_);
196         userData_ = userData;
197         asyncCallback_ = cb;
198     }
199 
UpdateCallback(const struct OH_AVCodecCallback & cb,void * userData)200     void UpdateCallback(const struct OH_AVCodecCallback &cb, void *userData)
201     {
202         std::lock_guard<std::shared_mutex> lock(mutex_);
203         userData_ = userData;
204         callback_ = cb;
205     }
206 
207 private:
208     struct OH_AVCodec *codec_ = nullptr;
209     struct OH_AVCodecAsyncCallback asyncCallback_ = {nullptr, nullptr, nullptr, nullptr};
210     struct OH_AVCodecCallback callback_ = {nullptr, nullptr, nullptr, nullptr};
211     void *userData_ = nullptr;
212     std::shared_mutex mutex_;
213 };
214 
GetTransData(const uint32_t & index,std::shared_ptr<AVSharedMemory> & memory,bool isOutput)215 OH_AVMemory *VideoDecoderObject::GetTransData(const uint32_t &index, std::shared_ptr<AVSharedMemory> &memory,
216                                               bool isOutput)
217 {
218     auto &memoryMap = isOutput ? this->outputMemoryMap_ : this->inputMemoryMap_;
219     {
220         std::shared_lock<std::shared_mutex> lock(this->objListMutex_);
221         auto iter = memoryMap.find(index);
222         if (iter != memoryMap.end() && iter->second->IsEqualMemory(memory)) {
223             return reinterpret_cast<OH_AVMemory *>(iter->second.GetRefPtr());
224         }
225     }
226     OHOS::sptr<OH_AVMemory> object = new (std::nothrow) OH_AVMemory(memory);
227     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "AV memory create failed");
228 
229     std::lock_guard<std::shared_mutex> lock(this->objListMutex_);
230     auto iterAndRet = memoryMap.emplace(index, object);
231     if (!iterAndRet.second) {
232         auto &temp = iterAndRet.first->second;
233         temp->magic_ = MFMagic::MFMAGIC_UNKNOWN;
234         temp->memory_ = nullptr;
235         this->tempList_.push(std::move(temp));
236         iterAndRet.first->second = object;
237         if (this->tempList_.size() > MAX_TEMPNUM) {
238             this->tempList_.pop();
239         }
240     }
241     return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
242 }
243 
GetTransData(const uint32_t & index,std::shared_ptr<AVBuffer> & buffer,bool isOutput)244 OH_AVBuffer *VideoDecoderObject::GetTransData(const uint32_t &index, std::shared_ptr<AVBuffer> &buffer, bool isOutput)
245 {
246     auto &bufferMap = isOutput ? this->outputBufferMap_ : this->inputBufferMap_;
247     {
248         std::shared_lock<std::shared_mutex> lock(this->objListMutex_);
249         auto iter = bufferMap.find(index);
250         if (iter != bufferMap.end() && iter->second->IsEqualBuffer(buffer)) {
251             return reinterpret_cast<OH_AVBuffer *>(iter->second.GetRefPtr());
252         }
253     }
254     OHOS::sptr<OH_AVBuffer> object = new (std::nothrow) OH_AVBuffer(buffer);
255     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVBuffer");
256 
257     std::lock_guard<std::shared_mutex> lock(this->objListMutex_);
258     auto iterAndRet = bufferMap.emplace(index, object);
259     if (!iterAndRet.second) {
260         auto &temp = iterAndRet.first->second;
261         temp->magic_ = MFMagic::MFMAGIC_UNKNOWN;
262         temp->buffer_ = nullptr;
263         this->tempList_.push(std::move(temp));
264         iterAndRet.first->second = object;
265         if (this->tempList_.size() > MAX_TEMPNUM) {
266             this->tempList_.pop();
267         }
268     }
269     return reinterpret_cast<OH_AVBuffer *>(object.GetRefPtr());
270 }
271 
ClearBufferList()272 void VideoDecoderObject::ClearBufferList()
273 {
274     std::lock_guard<std::shared_mutex> lock(objListMutex_);
275     if (inputBufferMap_.size() > 0) {
276         BufferToTempFunc(inputBufferMap_);
277         inputBufferMap_.clear();
278     }
279     if (outputBufferMap_.size() > 0) {
280         BufferToTempFunc(outputBufferMap_);
281         outputBufferMap_.clear();
282     }
283     if (inputMemoryMap_.size() > 0) {
284         MemoryToTempFunc(inputMemoryMap_);
285         inputMemoryMap_.clear();
286     }
287     if (outputMemoryMap_.size() > 0) {
288         MemoryToTempFunc(outputMemoryMap_);
289         outputMemoryMap_.clear();
290     }
291     while (tempList_.size() > MAX_TEMPNUM) {
292         tempList_.pop();
293     }
294 }
295 
StopCallback()296 void VideoDecoderObject::StopCallback()
297 {
298     if (callback_ == nullptr) {
299         return;
300     }
301     callback_->StopCallback();
302 }
303 
BufferToTempFunc(std::unordered_map<uint32_t,OHOS::sptr<OH_AVBuffer>> & tempMap)304 void VideoDecoderObject::BufferToTempFunc(std::unordered_map<uint32_t, OHOS::sptr<OH_AVBuffer>> &tempMap)
305 {
306     for (auto &val : tempMap) {
307         val.second->magic_ = MFMagic::MFMAGIC_UNKNOWN;
308         val.second->buffer_ = nullptr;
309         tempList_.push(std::move(val.second));
310     }
311 }
312 
MemoryToTempFunc(std::unordered_map<uint32_t,OHOS::sptr<OH_AVMemory>> & tempMap)313 void VideoDecoderObject::MemoryToTempFunc(std::unordered_map<uint32_t, OHOS::sptr<OH_AVMemory>> &tempMap)
314 {
315     for (auto &val : tempMap) {
316         val.second->magic_ = MFMagic::MFMAGIC_UNKNOWN;
317         val.second->memory_ = nullptr;
318         tempList_.push(std::move(val.second));
319     }
320 }
321 } // namespace
322 namespace OHOS {
323 namespace MediaAVCodec {
324 #ifdef __cplusplus
325 extern "C" {
326 #endif
OH_VideoDecoder_CreateByMime(const char * mime)327 struct OH_AVCodec *OH_VideoDecoder_CreateByMime(const char *mime)
328 {
329     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "Mime is nullptr!");
330 
331     std::shared_ptr<AVCodecVideoDecoder> videoDecoder = VideoDecoderFactory::CreateByMime(mime);
332     CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "Video decoder create by mime failed!");
333 
334     struct VideoDecoderObject *object = new (std::nothrow) VideoDecoderObject(videoDecoder);
335     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "Video decoder create by mime failed!");
336 
337     return object;
338 }
339 
OH_VideoDecoder_CreateByName(const char * name)340 struct OH_AVCodec *OH_VideoDecoder_CreateByName(const char *name)
341 {
342     CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "Name is nullptr!");
343 
344     std::shared_ptr<AVCodecVideoDecoder> videoDecoder = VideoDecoderFactory::CreateByName(name);
345     CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "Video decoder create by name failed!");
346 
347     struct VideoDecoderObject *object = new (std::nothrow) VideoDecoderObject(videoDecoder);
348     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "Video decoder create by name failed!");
349 
350     return object;
351 }
352 
OH_VideoDecoder_Destroy(struct OH_AVCodec * codec)353 OH_AVErrCode OH_VideoDecoder_Destroy(struct OH_AVCodec *codec)
354 {
355     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
356     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
357                              "Codec magic error!");
358 
359     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
360 
361     if (videoDecObj != nullptr && videoDecObj->videoDecoder_ != nullptr) {
362         int32_t ret = videoDecObj->videoDecoder_->Release();
363         videoDecObj->StopCallback();
364         videoDecObj->ClearBufferList();
365         if (ret != AVCS_ERR_OK) {
366             AVCODEC_LOGE("Video decoder destroy failed!");
367             delete codec;
368             return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
369         }
370     } else {
371         AVCODEC_LOGD("Video decoder is nullptr!");
372     }
373 
374     delete codec;
375     return AV_ERR_OK;
376 }
377 
OH_VideoDecoder_Configure(struct OH_AVCodec * codec,struct OH_AVFormat * format)378 OH_AVErrCode OH_VideoDecoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
379 {
380     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
381     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
382                              "Codec magic error!");
383     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "Format is nullptr!");
384     CHECK_AND_RETURN_RET_LOG(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "Format magic error!");
385 
386     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
387     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
388 
389     int32_t ret = videoDecObj->videoDecoder_->Configure(format->format_);
390     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
391                              "Video decoder configure failed!");
392 
393     return AV_ERR_OK;
394 }
395 
OH_VideoDecoder_Prepare(struct OH_AVCodec * codec)396 OH_AVErrCode OH_VideoDecoder_Prepare(struct OH_AVCodec *codec)
397 {
398     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
399     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
400                              "Codec magic error!");
401 
402     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
403     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
404 
405     int32_t ret = videoDecObj->videoDecoder_->Prepare();
406     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
407                              "Video decoder prepare failed!");
408 
409     return AV_ERR_OK;
410 }
411 
OH_VideoDecoder_Start(struct OH_AVCodec * codec)412 OH_AVErrCode OH_VideoDecoder_Start(struct OH_AVCodec *codec)
413 {
414     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
415     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
416                              "Codec magic error!");
417 
418     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
419     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
420 
421     int32_t ret = videoDecObj->videoDecoder_->Start();
422     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
423                              "Video decoder start failed!");
424 
425     return AV_ERR_OK;
426 }
427 
OH_VideoDecoder_Stop(struct OH_AVCodec * codec)428 OH_AVErrCode OH_VideoDecoder_Stop(struct OH_AVCodec *codec)
429 {
430     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
431     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
432                              "Codec magic error!");
433 
434     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
435     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
436 
437     int32_t ret = videoDecObj->videoDecoder_->Stop();
438     if (ret != AVCS_ERR_OK) {
439         AVCODEC_LOGE("Video decoder stop failed");
440         return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
441     }
442     videoDecObj->ClearBufferList();
443     return AV_ERR_OK;
444 }
445 
OH_VideoDecoder_Flush(struct OH_AVCodec * codec)446 OH_AVErrCode OH_VideoDecoder_Flush(struct OH_AVCodec *codec)
447 {
448     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
449     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
450                              "Codec magic error!");
451 
452     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
453     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
454 
455     int32_t ret = videoDecObj->videoDecoder_->Flush();
456     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
457                              "Video decoder flush failed!");
458     videoDecObj->ClearBufferList();
459     return AV_ERR_OK;
460 }
461 
OH_VideoDecoder_Reset(struct OH_AVCodec * codec)462 OH_AVErrCode OH_VideoDecoder_Reset(struct OH_AVCodec *codec)
463 {
464     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
465     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
466                              "Codec magic error!");
467 
468     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
469     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
470 
471     int32_t ret = videoDecObj->videoDecoder_->Reset();
472     if (ret != AVCS_ERR_OK) {
473         AVCODEC_LOGE("Video decoder reset failed!");
474         return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
475     }
476     videoDecObj->isOutputSurfaceMode_ = false;
477     videoDecObj->ClearBufferList();
478     return AV_ERR_OK;
479 }
480 
OH_VideoDecoder_SetSurface(OH_AVCodec * codec,OHNativeWindow * window)481 OH_AVErrCode OH_VideoDecoder_SetSurface(OH_AVCodec *codec, OHNativeWindow *window)
482 {
483     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
484     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
485                              "Codec magic error!");
486     CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_ERR_INVALID_VAL, "Window is nullptr!");
487     CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_ERR_INVALID_VAL, "Input window surface is nullptr!");
488 
489     GSError gsRet = window->surface->Disconnect();
490     EXPECT_AND_LOGI(gsRet == GSERROR_OK, "Disconnect success");
491 
492     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
493     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
494 
495     int32_t ret = videoDecObj->videoDecoder_->SetOutputSurface(window->surface);
496     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
497                              "Video decoder set output surface failed!");
498     videoDecObj->isOutputSurfaceMode_ = true;
499 
500     return AV_ERR_OK;
501 }
502 
OH_VideoDecoder_PushInputData(struct OH_AVCodec * codec,uint32_t index,OH_AVCodecBufferAttr attr)503 OH_AVErrCode OH_VideoDecoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
504 {
505     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
506     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
507                              "Codec magic error!");
508     CHECK_AND_RETURN_RET_LOG(attr.size >= 0, AV_ERR_INVALID_VAL, "Invalid buffer size!");
509 
510     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
511     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
512     CHECK_AND_RETURN_RET_LOG(videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
513                              "The callback of OH_AVMemory is nullptr!");
514 
515     struct AVCodecBufferInfo bufferInfo;
516     bufferInfo.presentationTimeUs = attr.pts;
517     bufferInfo.size = attr.size;
518     bufferInfo.offset = attr.offset;
519     enum AVCodecBufferFlag bufferFlag = static_cast<enum AVCodecBufferFlag>(attr.flags);
520 
521     int32_t ret = videoDecObj->videoDecoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
522     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
523                              "Video decoder push input data failed!");
524     return AV_ERR_OK;
525 }
526 
OH_VideoDecoder_PushInputBuffer(struct OH_AVCodec * codec,uint32_t index)527 OH_AVErrCode OH_VideoDecoder_PushInputBuffer(struct OH_AVCodec *codec, uint32_t index)
528 {
529     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Input codec is nullptr!");
530     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
531 
532     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
533     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "video decoder is nullptr!");
534     CHECK_AND_RETURN_RET_LOG(!videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
535                              "Not support the callback of OH_AVMemory!");
536 
537     int32_t ret = videoDecObj->videoDecoder_->QueueInputBuffer(index);
538     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
539                              "videoDecoder QueueInputBuffer failed!");
540     return AV_ERR_OK;
541 }
542 
OH_VideoDecoder_GetOutputDescription(struct OH_AVCodec * codec)543 OH_AVFormat *OH_VideoDecoder_GetOutputDescription(struct OH_AVCodec *codec)
544 {
545     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
546     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
547 
548     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
549     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
550 
551     Format format;
552     int32_t ret = videoDecObj->videoDecoder_->GetOutputFormat(format);
553     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Video decoder get output description failed!");
554 
555     OH_AVFormat *avFormat = OH_AVFormat_Create();
556     CHECK_AND_RETURN_RET_LOG(avFormat != nullptr, nullptr, "Video decoder get output description failed!");
557     avFormat->format_ = format;
558 
559     return avFormat;
560 }
561 
OH_VideoDecoder_RenderOutputData(struct OH_AVCodec * codec,uint32_t index)562 OH_AVErrCode OH_VideoDecoder_RenderOutputData(struct OH_AVCodec *codec, uint32_t index)
563 {
564     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
565     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
566                              "Codec magic error!");
567 
568     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
569     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
570     CHECK_AND_RETURN_RET_LOG(videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
571                              "The callback of OH_AVMemory is nullptr!");
572 
573     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, true);
574     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
575                              "Video decoder render output data failed!");
576 
577     return AV_ERR_OK;
578 }
579 
OH_VideoDecoder_FreeOutputData(struct OH_AVCodec * codec,uint32_t index)580 OH_AVErrCode OH_VideoDecoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
581 {
582     AVCODEC_LOGD("In OH_VideoDecoder_FreeOutputData");
583     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
584     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
585                              "Codec magic error!");
586 
587     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
588     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
589     CHECK_AND_RETURN_RET_LOG(videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
590                              "The callback of OH_AVMemory is nullptr!");
591 
592     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, false);
593     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
594                              "Video decoder free output data failed!");
595 
596     return AV_ERR_OK;
597 }
598 
OH_VideoDecoder_RenderOutputBuffer(struct OH_AVCodec * codec,uint32_t index)599 OH_AVErrCode OH_VideoDecoder_RenderOutputBuffer(struct OH_AVCodec *codec, uint32_t index)
600 {
601     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
602     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
603                              "Codec magic error!");
604     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
605     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
606     CHECK_AND_RETURN_RET_LOG(!videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
607                              "Not support the callback of OH_AVMemory!");
608 
609     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, true);
610     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
611                              "Video decoder render output data failed!");
612 
613     return AV_ERR_OK;
614 }
615 
OH_VideoDecoder_RenderOutputBufferAtTime(OH_AVCodec * codec,uint32_t index,int64_t renderTimestampNs)616 OH_AVErrCode OH_VideoDecoder_RenderOutputBufferAtTime(OH_AVCodec *codec, uint32_t index, int64_t renderTimestampNs)
617 {
618     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
619     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
620                              "Codec magic error!");
621     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
622     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
623     CHECK_AND_RETURN_RET_LOG(!videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
624                              "Not support the callback of OH_AVMemory!");
625 
626     int32_t ret = videoDecObj->videoDecoder_->RenderOutputBufferAtTime(index, renderTimestampNs);
627     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
628                              "Video decoder render output buffer at time failed!");
629     return AV_ERR_OK;
630 }
631 
OH_VideoDecoder_FreeOutputBuffer(struct OH_AVCodec * codec,uint32_t index)632 OH_AVErrCode OH_VideoDecoder_FreeOutputBuffer(struct OH_AVCodec *codec, uint32_t index)
633 {
634     AVCODEC_LOGD("In OH_VideoDecoder_FreeOutputData");
635     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
636     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
637                              "Codec magic error!");
638 
639     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
640     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
641     CHECK_AND_RETURN_RET_LOG(!videoDecObj->isSetMemoryCallback_, AV_ERR_INVALID_STATE,
642                              "Not support the callback of OH_AVMemory!");
643 
644     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, false);
645     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
646                              "Video decoder free output data failed!");
647 
648     return AV_ERR_OK;
649 }
650 
OH_VideoDecoder_QueryInputBuffer(struct OH_AVCodec * codec,uint32_t * index,int64_t timeoutUs)651 OH_AVErrCode OH_VideoDecoder_QueryInputBuffer(struct OH_AVCodec *codec, uint32_t *index, int64_t timeoutUs)
652 {
653     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
654     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
655                              "Codec magic error!");
656 
657     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
658     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
659     int32_t ret = videoDecObj->videoDecoder_->QueryInputBuffer(*index, timeoutUs);
660     switch (ret) {
661         case AVCS_ERR_OK:
662             return AV_ERR_OK;
663         case AVCS_ERR_TRY_AGAIN:
664             return AV_ERR_TRY_AGAIN_LATER;
665         default:
666             AVCODEC_LOGE("Video decoder query input data failed!");
667     }
668     return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
669 }
670 
OH_VideoDecoder_QueryOutputBuffer(struct OH_AVCodec * codec,uint32_t * index,int64_t timeoutUs)671 OH_AVErrCode OH_VideoDecoder_QueryOutputBuffer(struct OH_AVCodec *codec, uint32_t *index, int64_t timeoutUs)
672 {
673     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
674     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
675                              "Codec magic error!");
676 
677     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
678     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
679 
680     int32_t ret = videoDecObj->videoDecoder_->QueryOutputBuffer(*index, timeoutUs);
681     switch (ret) {
682         case AVCS_ERR_OK:
683             return AV_ERR_OK;
684         case AVCS_ERR_TRY_AGAIN:
685             return AV_ERR_TRY_AGAIN_LATER;
686         case AVCS_ERR_STREAM_CHANGED:
687             return AV_ERR_STREAM_CHANGED;
688         default:
689             AVCODEC_LOGE("Video decoder query output data failed!");
690     }
691     return AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret));
692 }
693 
OH_VideoDecoder_GetInputBuffer(struct OH_AVCodec * codec,uint32_t index)694 OH_AVBuffer *OH_VideoDecoder_GetInputBuffer(struct OH_AVCodec *codec, uint32_t index)
695 {
696     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
697     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
698 
699     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
700     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
701 
702     std::shared_ptr<AVBuffer> buffer = videoDecObj->videoDecoder_->GetInputBuffer(index);
703     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "Buffer is nullptr, idx:%{public}u", index);
704 
705     return videoDecObj->GetTransData(index, buffer, false);
706 }
707 
OH_VideoDecoder_GetOutputBuffer(struct OH_AVCodec * codec,uint32_t index)708 OH_AVBuffer *OH_VideoDecoder_GetOutputBuffer(struct OH_AVCodec *codec, uint32_t index)
709 {
710     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
711     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
712 
713     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
714     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
715 
716     std::shared_ptr<AVBuffer> buffer = videoDecObj->videoDecoder_->GetOutputBuffer(index);
717     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "Buffer is nullptr, idx:%{public}u", index);
718 
719     return videoDecObj->GetTransData(index, buffer, true);
720 }
721 
OH_VideoDecoder_SetParameter(struct OH_AVCodec * codec,struct OH_AVFormat * format)722 OH_AVErrCode OH_VideoDecoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
723 {
724     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
725     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
726                              "Codec magic error!");
727     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "Format is nullptr!");
728     CHECK_AND_RETURN_RET_LOG(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "Format magic error!");
729 
730     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
731     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
732 
733     int32_t ret = videoDecObj->videoDecoder_->SetParameter(format->format_);
734     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
735                              "Video decoder set parameter failed!");
736 
737     return AV_ERR_OK;
738 }
739 
OH_VideoDecoder_SetCallback(struct OH_AVCodec * codec,struct OH_AVCodecAsyncCallback callback,void * userData)740 OH_AVErrCode OH_VideoDecoder_SetCallback(struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback,
741                                          void *userData)
742 {
743     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
744     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
745                              "Codec magic error!");
746 
747     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
748     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
749 
750     if (videoDecObj->callback_ == nullptr) {
751         videoDecObj->callback_ = std::make_shared<NativeVideoDecoderCallback>(codec, callback, userData);
752     } else {
753         std::static_pointer_cast<NativeVideoDecoderCallback>(videoDecObj->callback_)
754             ->UpdateCallback(callback, userData);
755     }
756     int32_t ret =
757         videoDecObj->videoDecoder_->SetCallback(std::static_pointer_cast<AVCodecCallback>(videoDecObj->callback_));
758     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
759                              "Video decoder set callback failed!");
760     videoDecObj->isSetMemoryCallback_ = true;
761     return AV_ERR_OK;
762 }
763 
OH_VideoDecoder_RegisterCallback(struct OH_AVCodec * codec,struct OH_AVCodecCallback callback,void * userData)764 OH_AVErrCode OH_VideoDecoder_RegisterCallback(struct OH_AVCodec *codec, struct OH_AVCodecCallback callback,
765                                               void *userData)
766 {
767     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
768     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
769                              "Codec magic error!");
770     CHECK_AND_RETURN_RET_LOG(callback.onNeedInputBuffer != nullptr, AV_ERR_INVALID_VAL,
771                              "Callback onNeedInputBuffer is nullptr");
772     CHECK_AND_RETURN_RET_LOG(callback.onNewOutputBuffer != nullptr, AV_ERR_INVALID_VAL,
773                              "Callback onNewOutputBuffer is nullptr");
774 
775     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
776     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
777 
778     if (videoDecObj->callback_ == nullptr) {
779         videoDecObj->callback_ = std::make_shared<NativeVideoDecoderCallback>(codec, callback, userData);
780     } else {
781         std::static_pointer_cast<NativeVideoDecoderCallback>(videoDecObj->callback_)
782             ->UpdateCallback(callback, userData);
783     }
784     int32_t ret =
785         videoDecObj->videoDecoder_->SetCallback(std::static_pointer_cast<MediaCodecCallback>(videoDecObj->callback_));
786     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
787                              "Video decoder register callback failed!");
788     return AV_ERR_OK;
789 }
790 
OH_VideoDecoder_IsValid(OH_AVCodec * codec,bool * isValid)791 OH_AVErrCode OH_VideoDecoder_IsValid(OH_AVCodec *codec, bool *isValid)
792 {
793     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
794     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
795                              "Codec magic error!");
796     CHECK_AND_RETURN_RET_LOG(isValid != nullptr, AV_ERR_INVALID_VAL, "Input isValid is nullptr!");
797     *isValid = true;
798     return AV_ERR_OK;
799 }
800 
801 #ifdef SUPPORT_DRM
OH_VideoDecoder_SetDecryptionConfig(OH_AVCodec * codec,MediaKeySession * mediaKeySession,bool secureVideoPath)802 OH_AVErrCode OH_VideoDecoder_SetDecryptionConfig(OH_AVCodec *codec, MediaKeySession *mediaKeySession,
803                                                  bool secureVideoPath)
804 {
805     AVCODEC_LOGI("OH_VideoDecoder_SetDecryptionConfig");
806     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
807     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
808                              "Codec magic error!");
809 
810     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
811     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
812 
813     struct MediaKeySessionObject *sessionObject = reinterpret_cast<MediaKeySessionObject *>(mediaKeySession);
814     CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, AV_ERR_INVALID_VAL, "sessionObject is nullptr!");
815     AVCODEC_LOGD("DRM sessionObject impl :0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(sessionObject));
816 
817     CHECK_AND_RETURN_RET_LOG(sessionObject->sessionImpl_ != nullptr, AV_ERR_INVALID_VAL,
818                              "sessionObject->impl is nullptr!");
819     AVCODEC_LOGD("DRM impl :0x%{public}06" PRIXPTR " Instances create",
820                  FAKE_POINTER(sessionObject->sessionImpl_.GetRefPtr()));
821 
822     int32_t ret = videoDecObj->videoDecoder_->SetDecryptConfig(
823         sessionObject->sessionImpl_->GetMediaKeySessionServiceProxy(), secureVideoPath);
824     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
825                              "Video decoder SetDecryptConfig failed!");
826 
827     return AV_ERR_OK;
828 }
829 #else
OH_VideoDecoder_SetDecryptionConfig(OH_AVCodec * codec,MediaKeySession * mediaKeySession,bool secureVideoPath)830 OH_AVErrCode OH_VideoDecoder_SetDecryptionConfig(OH_AVCodec *codec, MediaKeySession *mediaKeySession,
831                                                  bool secureVideoPath)
832 {
833     AVCODEC_LOGI("OH_VideoDecoder_SetDecryptionConfig");
834     (void)codec;
835     (void)mediaKeySession;
836     (void)secureVideoPath;
837     return AV_ERR_OK;
838 }
839 #endif
840 }
841 } // namespace MediaAVCodec
842 } // namespace OHOS