• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "native_avcodec_base.h"
19 #include "native_avcodec_videodecoder.h"
20 #include "native_avmagic.h"
21 #include "native_window.h"
22 #include "avcodec_video_decoder.h"
23 #include "avsharedmemory.h"
24 #include "media_log.h"
25 #include "media_errors.h"
26 
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeVideoDecoder"};
29 }
30 
31 using namespace OHOS::Media;
32 class NativeVideoDecoderCallback;
33 
34 struct VideoDecoderObject : public OH_AVCodec {
VideoDecoderObjectVideoDecoderObject35     explicit VideoDecoderObject(const std::shared_ptr<AVCodecVideoDecoder> &decoder)
36         : OH_AVCodec(AVMagic::MEDIA_MAGIC_VIDEO_DECODER), videoDecoder_(decoder) {}
37     ~VideoDecoderObject() = default;
38 
39     const std::shared_ptr<AVCodecVideoDecoder> videoDecoder_;
40     std::list<OHOS::sptr<OH_AVMemory>> memoryObjList_;
41     std::shared_ptr<NativeVideoDecoderCallback> callback_ = nullptr;
42     std::atomic<bool> isFlushing_ = false;
43     std::atomic<bool> isStop_ = false;
44     std::atomic<bool> isEOS_ = false;
45 };
46 
47 class NativeVideoDecoderCallback : public AVCodecCallback {
48 public:
NativeVideoDecoderCallback(OH_AVCodec * codec,struct OH_AVCodecAsyncCallback cb,void * userData)49     NativeVideoDecoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
50         : codec_(codec), callback_(cb), userData_(userData) {}
51     virtual ~NativeVideoDecoderCallback() = default;
52 
OnError(AVCodecErrorType errorType,int32_t errorCode)53     void OnError(AVCodecErrorType errorType, int32_t errorCode) override
54     {
55         std::unique_lock<std::mutex> lock(mutex_);
56         (void)errorType;
57         if (codec_ != nullptr && callback_.onError != nullptr) {
58             int32_t extErr = MSErrorToExtError(static_cast<MediaServiceErrCode>(errorCode));
59             callback_.onError(codec_, extErr, userData_);
60         }
61     }
62 
OnOutputFormatChanged(const Format & format)63     void OnOutputFormatChanged(const Format &format) override
64     {
65         std::unique_lock<std::mutex> lock(mutex_);
66         if (codec_ != nullptr && callback_.onStreamChanged != nullptr) {
67             OHOS::sptr<OH_AVFormat> object = new(std::nothrow) OH_AVFormat(format);
68             // The object lifecycle is controlled by the current function stack
69             callback_.onStreamChanged(codec_, reinterpret_cast<OH_AVFormat *>(object.GetRefPtr()), userData_);
70         }
71     }
72 
OnInputBufferAvailable(uint32_t index)73     void OnInputBufferAvailable(uint32_t index) override
74     {
75         std::unique_lock<std::mutex> lock(mutex_);
76         if (codec_ != nullptr && callback_.onNeedInputData != nullptr) {
77             struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
78             CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "videoDecoder_ is nullptr!");
79 
80             if (videoDecObj->isFlushing_.load() || videoDecObj->isStop_.load() || videoDecObj->isEOS_.load()) {
81                 MEDIA_LOGD("At flush, eos or stop, no buffer available");
82                 return;
83             }
84             OH_AVMemory *data = GetInputData(codec_, index);
85             if (data != nullptr) {
86                 callback_.onNeedInputData(codec_, index, data, userData_);
87             }
88         }
89     }
90 
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)91     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override
92     {
93         std::unique_lock<std::mutex> lock(mutex_);
94         if (codec_ != nullptr && callback_.onNeedOutputData != nullptr) {
95             struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec_);
96             CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "videoDecoder_ is nullptr!");
97 
98             if (videoDecObj->isFlushing_.load() || videoDecObj->isStop_.load()) {
99                 MEDIA_LOGD("At flush or stop, ignore");
100                 return;
101             }
102             struct OH_AVCodecBufferAttr bufferAttr;
103             bufferAttr.pts = info.presentationTimeUs;
104             bufferAttr.size = info.size;
105             bufferAttr.offset = info.offset;
106             bufferAttr.flags = flag;
107             // The bufferInfo lifecycle is controlled by the current function stack
108             callback_.onNeedOutputData(codec_, index, nullptr, &bufferAttr, userData_);
109         }
110     }
111 
StopCallback()112     void StopCallback()
113     {
114         std::unique_lock<std::mutex> lock(mutex_);
115         codec_ = nullptr;
116     }
117 
118 private:
GetInputData(struct OH_AVCodec * codec,uint32_t index)119     OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index)
120     {
121         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
122         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, nullptr, "magic error!");
123 
124         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
125         CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "videoDecoder_ is nullptr!");
126 
127         std::shared_ptr<AVSharedMemory> memory = videoDecObj->videoDecoder_->GetInputBuffer(index);
128         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get input buffer is nullptr!");
129 
130         for (auto &memoryObj : videoDecObj->memoryObjList_) {
131             if (memoryObj->IsEqualMemory(memory)) {
132                 return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
133             }
134         }
135 
136         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
137         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
138 
139         videoDecObj->memoryObjList_.push_back(object);
140         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
141     }
142 
GetOutputData(struct OH_AVCodec * codec,uint32_t index)143     OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index)
144     {
145         CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
146         CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, nullptr, "magic error!");
147 
148         struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
149         CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "videoDecoder_ is nullptr!");
150 
151         std::shared_ptr<AVSharedMemory> memory = videoDecObj->videoDecoder_->GetOutputBuffer(index);
152         CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get output buffer is nullptr!");
153 
154         for (auto &memoryObj : videoDecObj->memoryObjList_) {
155             if (memoryObj->IsEqualMemory(memory)) {
156                 return reinterpret_cast<OH_AVMemory *>(memoryObj.GetRefPtr());
157             }
158         }
159 
160         OHOS::sptr<OH_AVMemory> object = new(std::nothrow) OH_AVMemory(memory);
161         CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
162 
163         videoDecObj->memoryObjList_.push_back(object);
164         return reinterpret_cast<OH_AVMemory *>(object.GetRefPtr());
165     }
166 
167     struct OH_AVCodec *codec_;
168     struct OH_AVCodecAsyncCallback callback_;
169     void *userData_;
170     std::mutex mutex_;
171 };
172 
OH_VideoDecoder_CreateByMime(const char * mime)173 struct OH_AVCodec *OH_VideoDecoder_CreateByMime(const char *mime)
174 {
175     CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
176 
177     std::shared_ptr<AVCodecVideoDecoder> videoDecoder = VideoDecoderFactory::CreateByMime(mime);
178     CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "failed to VideoDecoderFactory::CreateByMime");
179 
180     struct VideoDecoderObject *object = new(std::nothrow) VideoDecoderObject(videoDecoder);
181     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new VideoDecoderObject");
182 
183     return object;
184 }
185 
OH_VideoDecoder_CreateByName(const char * name)186 struct OH_AVCodec *OH_VideoDecoder_CreateByName(const char *name)
187 {
188     CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
189 
190     std::shared_ptr<AVCodecVideoDecoder> videoDecoder = VideoDecoderFactory::CreateByName(name);
191     CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "failed to VideoDecoderFactory::CreateByMime");
192 
193     struct VideoDecoderObject *object = new(std::nothrow) VideoDecoderObject(videoDecoder);
194     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new VideoDecoderObject");
195 
196     return object;
197 }
198 
OH_VideoDecoder_Destroy(struct OH_AVCodec * codec)199 OH_AVErrCode OH_VideoDecoder_Destroy(struct OH_AVCodec *codec)
200 {
201     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
202     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
203 
204     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
205 
206     if (videoDecObj != nullptr && videoDecObj->videoDecoder_ != nullptr) {
207         videoDecObj->callback_->StopCallback();
208         videoDecObj->memoryObjList_.clear();
209         videoDecObj->isStop_.store(false);
210         int32_t ret = videoDecObj->videoDecoder_->Release();
211         if (ret != MSERR_OK) {
212             MEDIA_LOGE("videoDecoder Release failed!");
213             delete codec;
214             return AV_ERR_OPERATE_NOT_PERMIT;
215         }
216     } else {
217         MEDIA_LOGD("videoDecoder_ is nullptr!");
218     }
219 
220     delete codec;
221     return AV_ERR_OK;
222 }
223 
OH_VideoDecoder_Configure(struct OH_AVCodec * codec,struct OH_AVFormat * format)224 OH_AVErrCode OH_VideoDecoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
225 {
226     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
227     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
228     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
229     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
230 
231     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
232     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder is nullptr!");
233 
234     int32_t ret = videoDecObj->videoDecoder_->Configure(format->format_);
235     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder Configure failed!");
236 
237     return AV_ERR_OK;
238 }
239 
OH_VideoDecoder_Prepare(struct OH_AVCodec * codec)240 OH_AVErrCode OH_VideoDecoder_Prepare(struct OH_AVCodec *codec)
241 {
242     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
243     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
244 
245     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
246     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
247 
248     int32_t ret = videoDecObj->videoDecoder_->Prepare();
249     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder Prepare failed!");
250 
251     return AV_ERR_OK;
252 }
253 
OH_VideoDecoder_Start(struct OH_AVCodec * codec)254 OH_AVErrCode OH_VideoDecoder_Start(struct OH_AVCodec *codec)
255 {
256     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
257     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
258 
259     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
260     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
261     videoDecObj->isStop_.store(false);
262     videoDecObj->isEOS_.store(false);
263     int32_t ret = videoDecObj->videoDecoder_->Start();
264     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder Start failed!");
265 
266     return AV_ERR_OK;
267 }
268 
OH_VideoDecoder_Stop(struct OH_AVCodec * codec)269 OH_AVErrCode OH_VideoDecoder_Stop(struct OH_AVCodec *codec)
270 {
271     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
272     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
273 
274     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
275     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
276 
277     videoDecObj->isStop_.store(true);
278     MEDIA_LOGD("Set stop status to true");
279 
280     int32_t ret = videoDecObj->videoDecoder_->Stop();
281     if (ret != MSERR_OK) {
282         videoDecObj->isStop_.store(false);
283         MEDIA_LOGE("videoDecoder Stop failed! Set stop status to false");
284         return AV_ERR_OPERATE_NOT_PERMIT;
285     }
286     videoDecObj->memoryObjList_.clear();
287 
288     return AV_ERR_OK;
289 }
290 
OH_VideoDecoder_Flush(struct OH_AVCodec * codec)291 OH_AVErrCode OH_VideoDecoder_Flush(struct OH_AVCodec *codec)
292 {
293     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
294     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
295 
296     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
297     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
298 
299     videoDecObj->isFlushing_.store(true);
300     MEDIA_LOGD("Set flush status to true");
301 
302     int32_t ret = videoDecObj->videoDecoder_->Flush();
303     if (ret != MSERR_OK) {
304         videoDecObj->isFlushing_.store(false);
305         MEDIA_LOGD("videoDecoder Flush failed! Set flush status to false");
306         return AV_ERR_OPERATE_NOT_PERMIT;
307     }
308 
309     videoDecObj->memoryObjList_.clear();
310     videoDecObj->isFlushing_.store(false);
311     MEDIA_LOGD("Set flush status to false");
312     return AV_ERR_OK;
313 }
314 
OH_VideoDecoder_Reset(struct OH_AVCodec * codec)315 OH_AVErrCode OH_VideoDecoder_Reset(struct OH_AVCodec *codec)
316 {
317     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
318     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
319 
320     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
321     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
322     videoDecObj->isStop_.store(false);
323     MEDIA_LOGD("Set stop status to true");
324     int32_t ret = videoDecObj->videoDecoder_->Reset();
325     if (ret != MSERR_OK) {
326         videoDecObj->isStop_.store(false);
327         MEDIA_LOGE("videoDecoder Reset failed! Set stop status to false");
328         return AV_ERR_OPERATE_NOT_PERMIT;
329     }
330 
331     videoDecObj->memoryObjList_.clear();
332     return AV_ERR_OK;
333 }
334 
OH_VideoDecoder_SetSurface(OH_AVCodec * codec,OHNativeWindow * window)335 OH_AVErrCode OH_VideoDecoder_SetSurface(OH_AVCodec *codec, OHNativeWindow *window)
336 {
337     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
338     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
339     CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_ERR_INVALID_VAL, "input window is nullptr!");
340     CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_ERR_INVALID_VAL, "input surface is nullptr!");
341 
342     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
343     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
344 
345     int32_t ret = videoDecObj->videoDecoder_->SetOutputSurface(window->surface);
346     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder SetOutputSurface failed!");
347 
348     return AV_ERR_OK;
349 }
350 
OH_VideoDecoder_PushInputData(struct OH_AVCodec * codec,uint32_t index,OH_AVCodecBufferAttr attr)351 OH_AVErrCode OH_VideoDecoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
352 {
353     MEDIA_LOGD("In OH_VideoDecoder_PushInputData");
354     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
355     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
356 
357     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
358     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
359 
360     struct AVCodecBufferInfo bufferInfo;
361     bufferInfo.presentationTimeUs = attr.pts;
362     bufferInfo.size = attr.size;
363     bufferInfo.offset = attr.offset;
364     enum AVCodecBufferFlag bufferFlag = static_cast<enum AVCodecBufferFlag>(attr.flags);
365 
366     int32_t ret = videoDecObj->videoDecoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
367     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder QueueInputBuffer failed!");
368     if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
369         videoDecObj->isEOS_.store(true);
370     }
371 
372     return AV_ERR_OK;
373 }
374 
OH_VideoDecoder_GetOutputDescription(struct OH_AVCodec * codec)375 OH_AVFormat *OH_VideoDecoder_GetOutputDescription(struct OH_AVCodec *codec)
376 {
377     CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
378     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, nullptr, "magic error!");
379 
380     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
381     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "videoDecoder_ is nullptr!");
382 
383     Format format;
384     int32_t ret = videoDecObj->videoDecoder_->GetOutputFormat(format);
385     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "videoDecoder GetOutputFormat failed!");
386 
387     OH_AVFormat *avFormat = OH_AVFormat_Create();
388     avFormat->format_ = format;
389 
390     return avFormat;
391 }
392 
OH_VideoDecoder_RenderOutputData(struct OH_AVCodec * codec,uint32_t index)393 OH_AVErrCode OH_VideoDecoder_RenderOutputData(struct OH_AVCodec *codec, uint32_t index)
394 {
395     MEDIA_LOGD("In OH_VideoDecoder_RenderOutputData");
396     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
397     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
398 
399     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
400     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
401 
402     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, true);
403     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder ReleaseOutputBuffer failed!");
404 
405     return AV_ERR_OK;
406 }
407 
OH_VideoDecoder_FreeOutputData(struct OH_AVCodec * codec,uint32_t index)408 OH_AVErrCode OH_VideoDecoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
409 {
410     MEDIA_LOGD("In OH_VideoDecoder_FreeOutputData");
411     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
412     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
413 
414     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
415     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
416 
417     int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, false);
418     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder ReleaseOutputBuffer failed!");
419 
420     return AV_ERR_OK;
421 }
422 
OH_VideoDecoder_SetParameter(struct OH_AVCodec * codec,struct OH_AVFormat * format)423 OH_AVErrCode OH_VideoDecoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
424 {
425     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
426     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
427     CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
428     CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::MEDIA_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
429 
430     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
431     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
432 
433     int32_t ret = videoDecObj->videoDecoder_->SetParameter(format->format_);
434     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder SetParameter failed!");
435 
436     return AV_ERR_OK;
437 }
438 
OH_VideoDecoder_SetCallback(struct OH_AVCodec * codec,struct OH_AVCodecAsyncCallback callback,void * userData)439 OH_AVErrCode OH_VideoDecoder_SetCallback(
440     struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback, void *userData)
441 {
442     CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
443     CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::MEDIA_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
444 
445     struct VideoDecoderObject *videoDecObj = reinterpret_cast<VideoDecoderObject *>(codec);
446     CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
447 
448     videoDecObj->callback_ = std::make_shared<NativeVideoDecoderCallback>(codec, callback, userData);
449     CHECK_AND_RETURN_RET_LOG(videoDecObj->callback_ != nullptr, AV_ERR_INVALID_VAL, "videoDecoder_ is nullptr!");
450 
451     int32_t ret = videoDecObj->videoDecoder_->SetCallback(videoDecObj->callback_);
452     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "videoDecoder SetCallback failed!");
453 
454     return AV_ERR_OK;
455 }
456