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 "avcodec_video_decoder_impl.h"
17 #include "i_avcodec_service.h"
18 #include "avcodec_log.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_dfx.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecVideoDecoderImpl"};
24 }
25
26 namespace OHOS {
27 namespace MediaAVCodec {
CreateByMime(const std::string & mime)28 std::shared_ptr<AVCodecVideoDecoder> VideoDecoderFactory::CreateByMime(const std::string &mime)
29 {
30 AVCODEC_SYNC_TRACE;
31
32 std::shared_ptr<AVCodecVideoDecoderImpl> impl = std::make_shared<AVCodecVideoDecoderImpl>();
33
34 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, true, mime);
35 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK,
36 nullptr, "AVCodec video decoder impl init failed");
37
38 return impl;
39 }
40
CreateByName(const std::string & name)41 std::shared_ptr<AVCodecVideoDecoder> VideoDecoderFactory::CreateByName(const std::string &name)
42 {
43 AVCODEC_SYNC_TRACE;
44
45 std::shared_ptr<AVCodecVideoDecoderImpl> impl = std::make_shared<AVCodecVideoDecoderImpl>();
46
47 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, false, name);
48 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK,
49 nullptr, "AVCodec video decoder impl init failed");
50
51 return impl;
52 }
53
Init(AVCodecType type,bool isMimeType,const std::string & name)54 int32_t AVCodecVideoDecoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
55 {
56 AVCODEC_SYNC_TRACE;
57 codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
58 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
59 AVCS_ERR_INVALID_OPERATION, "Codec service create failed");
60
61 return codecService_->Init(type, isMimeType, name);
62 }
63
AVCodecVideoDecoderImpl()64 AVCodecVideoDecoderImpl::AVCodecVideoDecoderImpl()
65 {
66 AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
67 }
68
~AVCodecVideoDecoderImpl()69 AVCodecVideoDecoderImpl::~AVCodecVideoDecoderImpl()
70 {
71 if (codecService_ != nullptr) {
72 (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
73 codecService_ = nullptr;
74 }
75 AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
76 }
77
Configure(const Format & format)78 int32_t AVCodecVideoDecoderImpl::Configure(const Format &format)
79 {
80 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
81 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
82
83 AVCODEC_SYNC_TRACE;
84 return codecService_->Configure(format);
85 }
86
Prepare()87 int32_t AVCodecVideoDecoderImpl::Prepare()
88 {
89 return AVCS_ERR_OK;
90 }
91
Start()92 int32_t AVCodecVideoDecoderImpl::Start()
93 {
94 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
95 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
96
97 AVCODEC_SYNC_TRACE;
98 return codecService_->Start();
99 }
100
Stop()101 int32_t AVCodecVideoDecoderImpl::Stop()
102 {
103 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
104 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
105
106 AVCODEC_SYNC_TRACE;
107 return codecService_->Stop();
108 }
109
Flush()110 int32_t AVCodecVideoDecoderImpl::Flush()
111 {
112 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
113 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
114
115 AVCODEC_SYNC_TRACE;
116 return codecService_->Flush();
117 }
118
Reset()119 int32_t AVCodecVideoDecoderImpl::Reset()
120 {
121 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
122 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
123
124 AVCODEC_SYNC_TRACE;
125 return codecService_->Reset();
126 }
127
Release()128 int32_t AVCodecVideoDecoderImpl::Release()
129 {
130 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
131 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
132
133 AVCODEC_SYNC_TRACE;
134 return codecService_->Release();
135 }
136
SetOutputSurface(sptr<Surface> surface)137 int32_t AVCodecVideoDecoderImpl::SetOutputSurface(sptr<Surface> surface)
138 {
139 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
140 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
141
142 AVCODEC_SYNC_TRACE;
143 return codecService_->SetOutputSurface(surface);
144 }
145
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)146 int32_t AVCodecVideoDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
147 {
148 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
149 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
150
151 AVCODEC_SYNC_TRACE;
152 return codecService_->QueueInputBuffer(index, info, flag);
153 }
154
GetOutputFormat(Format & format)155 int32_t AVCodecVideoDecoderImpl::GetOutputFormat(Format &format)
156 {
157 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
158 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
159
160 AVCODEC_SYNC_TRACE;
161 return codecService_->GetOutputFormat(format);
162 }
163
ReleaseOutputBuffer(uint32_t index,bool render)164 int32_t AVCodecVideoDecoderImpl::ReleaseOutputBuffer(uint32_t index, bool render)
165 {
166 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
167 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
168
169 AVCODEC_SYNC_TRACE;
170 return codecService_->ReleaseOutputBuffer(index, render);
171 }
172
SetParameter(const Format & format)173 int32_t AVCodecVideoDecoderImpl::SetParameter(const Format &format)
174 {
175 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
176 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
177
178 AVCODEC_SYNC_TRACE;
179 return codecService_->SetParameter(format);
180 }
181
SetCallback(const std::shared_ptr<AVCodecCallback> & callback)182 int32_t AVCodecVideoDecoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
183 {
184 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
185 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
186 CHECK_AND_RETURN_RET_LOG(callback != nullptr,
187 AVCS_ERR_INVALID_VAL, "Callback is nullptr");
188
189 AVCODEC_SYNC_TRACE;
190 return codecService_->SetCallback(callback);
191 }
192 } // namespace MediaAVCodec
193 } // namespace OHOS
194