• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "decoder_demo.h"
17 
18 #include <iostream>
19 #include <unistd.h>
20 
21 #include "avcodec_list.h"
22 #include "securec.h"
23 #include "wm_common.h"
24 #include "window.h"
25 #include "window_option.h"
26 
27 using namespace OHOS::DistributedHardware;
28 
29 static const int32_t ES_R[325] = {
30     11895, 8109, 1578, 1616, 1313, 572, 805, 837, 755, 706, 952, 879, 13193, 422, 389, 509, 725, 465, 479, 959,
31     677, 364, 541, 696, 9306, 322, 318, 767, 590, 422, 530, 403, 505, 566, 445, 508, 7783, 460, 405, 343, 451,
32     608, 431, 411, 543, 487, 527, 400, 6287, 385, 418, 391, 592, 434, 412, 398, 504, 492, 479, 561, 5413, 317,
33     355, 422, 467, 452, 476, 460, 490, 492, 485, 451, 5036, 312, 408, 460, 432, 502, 388, 475, 407, 544, 401,
34     487, 4404, 362, 378, 427, 416, 426, 456, 414, 438, 424, 442, 444, 4310, 362, 388, 393, 390, 441, 398, 423,
35     369, 443, 406, 392, 4231, 343, 363, 355, 390, 459, 371, 378, 381, 405, 392, 426, 3975, 387, 337, 393, 439,
36     378, 355, 374, 484, 381, 373, 423, 3869, 312, 350, 400, 345, 356, 320, 473, 431, 386, 338, 431, 3426, 268,
37     315, 416, 383, 373, 381, 354, 383, 328, 348, 418, 3715, 324, 361, 331, 350, 302, 409, 377, 359, 384, 334,
38     326, 3439, 266, 324, 329, 353, 405, 303, 357, 332, 292, 361, 333, 3542, 294, 284, 247, 331, 306, 322, 287,
39     367, 341, 276, 258, 3980, 246, 245, 259, 309, 333, 250, 275, 334, 281, 253, 371, 3640, 213, 231, 301, 302,
40     228, 289, 290, 281, 201, 284, 277, 4242, 205, 328, 237, 283, 295, 266, 230, 321, 348, 212, 308, 4103, 259,
41     238, 245, 298, 330, 265, 271, 287, 267, 286, 290, 3856, 269, 242, 209, 314, 267, 278, 280, 314, 250, 433,
42     238, 3654, 195, 246, 301, 298, 250, 270, 320, 269, 305, 258, 368, 3810, 231, 212, 279, 289, 252, 303, 287,
43     295, 206, 264, 349, 4071, 242, 296, 271, 231, 307, 265, 254, 267, 317, 232, 348, 4077, 259, 222, 268, 235,
44     324, 266, 256, 312, 246, 248, 325, 4000, 266, 201, 230, 293, 264, 265, 273, 301, 304, 253, 266, 3978, 228,
45     232, 250, 248, 281, 219, 243, 293, 287, 253, 328, 3719
46 };
47 
48 static const int32_t ES_W[183] = {
49     2111, 109091, 9316, 969, 13656, 1349, 959, 10484, 1219, 14839, 1092, 23378, 1653, 1725, 1526, 8500, 15407,
50     2058, 1346, 21066, 3758, 1734, 1950, 19955, 3997, 1732, 1784, 22687, 4392, 2448, 2180, 17456, 3930, 1851,
51     1802, 24227, 4393, 2639, 2778, 18314, 4023, 2392, 2283, 20566, 4118, 2664, 2013, 18964, 2624, 45258, 5860,
52     4124, 3473, 27772, 4687, 3140, 2939, 26288, 3808, 2967, 2823, 27219, 3943, 3242, 2667, 27372, 3522, 2899,
53     2316, 26608, 3284, 2853, 2285, 19581, 2894, 2436, 24898, 4002, 2876, 2807, 25730, 3903, 2874, 2975, 26309,
54     3771, 2763, 2666, 23404, 3826, 2410, 2644, 24629, 4145, 3121, 2878, 50773, 7040, 3945, 3292, 30828, 5210,
55     2883, 3277, 31501, 4809, 3068, 3220, 30746, 4715, 3461, 3583, 32278, 4798, 3398, 3389, 31404, 4921, 3382,
56     3766, 31821, 5848, 3860, 4047, 37642, 5793, 4271, 4094, 29853, 6163, 4399, 4063, 32151, 6038, 4332, 4041,
57     30390, 5679, 4098, 3921, 29401, 5307, 3996, 3945, 45997, 7060, 3716, 4183, 26357, 6190, 3714, 4250, 29086,
58     5929, 3491, 4489, 27772, 6656, 4219, 4348, 25851, 6088, 3617, 4477, 25722, 6303, 3856, 4208, 25348, 5896,
59     3816, 4521, 22462, 5914, 3673, 4594, 18091, 6474, 3878, 4492, 10890, 4823, 4148
60 };
61 
62 using namespace OHOS;
63 using namespace OHOS::Media;
64 using namespace std;
65 namespace {
66     constexpr uint32_t DEFAULT_FRAME_RATE = 30;
67     constexpr uint32_t MAX_INPUT_BUFFER_SIZE = 30000;
68     constexpr uint32_t FRAME_DURATION_US = 33000;
69     constexpr uint32_t VIDEO_DATA_FORMAT_NV12 = 2;
70     constexpr uint32_t VIDEO_DATA_FORMAT_RGBA = 5;
71     constexpr uint32_t SLEEP_THREE_SECOND = 3;
72     constexpr uint32_t INDEX_CONSTANT = 10000;
73     const string CODEC_NAME_H264 = "OMX_hisi_video_encoder_avc";
74     const string CODEC_NAME_MPEG4 = "avenc_mpeg4";
75 }
76 
RunCase()77 void VDecDemo::RunCase()
78 {
79     CheckCodecType();
80     CreateVdec();
81     Format format;
82     format.PutIntValue("width", width_);
83     format.PutIntValue("height", height_);
84     if (isW) {
85         format.PutIntValue("pixel_format", VIDEO_DATA_FORMAT_NV12);
86     } else {
87         format.PutIntValue("pixel_format", VIDEO_DATA_FORMAT_RGBA);
88     }
89     format.PutIntValue("frame_rate", DEFAULT_FRAME_RATE);
90     format.PutIntValue("max_input_size", MAX_INPUT_BUFFER_SIZE);
91     Configure(format);
92     SetSurface();
93     Prepare();
94     Start();
95     sleep(SLEEP_THREE_SECOND);
96     Stop();
97     Release();
98 }
99 
CreateVdec()100 int32_t VDecDemo::CreateVdec()
101 {
102     if (isW) {
103         vdec_ = VideoDecoderFactory::CreateByMime("video/avc");
104     } else {
105         vdec_ = VideoDecoderFactory::CreateByMime("video/mp4v-es");
106     }
107 
108     signal_ = make_shared<VDecSignal>();
109     cb_ = make_unique<VDecDemoCallback>(signal_);
110     vdec_->SetCallback(cb_);
111     return 0;
112 }
113 
Configure(const Format & format)114 int32_t VDecDemo::Configure(const Format &format)
115 {
116     return vdec_->Configure(format);
117 }
118 
Prepare()119 int32_t VDecDemo::Prepare()
120 {
121     return vdec_->Prepare();
122 }
123 
Start()124 int32_t VDecDemo::Start()
125 {
126     isRunning_.store(true);
127 
128     testFile_ = std::make_unique<std::ifstream>();
129     testFile_->open("/data/media/video.es", std::ios::in | std::ios::binary);
130 
131     inputLoop_ = make_unique<thread>(&VDecDemo::InputFunc, this);
132     outputLoop_ = make_unique<thread>(&VDecDemo::OutputFunc, this);
133     return vdec_->Start();
134 }
135 
Stop()136 int32_t VDecDemo::Stop()
137 {
138     isRunning_.store(false);
139 
140     if (inputLoop_ != nullptr && inputLoop_->joinable()) {
141         {
142             unique_lock<mutex> inLock(signal_->inMutex_);
143             signal_->inQueue_.push(INDEX_CONSTANT);
144             signal_->inCond_.notify_all();
145         }
146         inputLoop_->join();
147         inputLoop_.reset();
148     }
149 
150     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
151         {
152             unique_lock<mutex> outLock(signal_->outMutex_);
153             signal_->outQueue_.push(INDEX_CONSTANT);
154             signal_->outCond_.notify_all();
155         }
156         outputLoop_->join();
157         outputLoop_.reset();
158     }
159 
160     return vdec_->Stop();
161 }
162 
Flush()163 int32_t VDecDemo::Flush()
164 {
165     return vdec_->Flush();
166 }
167 
Reset()168 int32_t VDecDemo::Reset()
169 {
170     return vdec_->Reset();
171 }
172 
Release()173 int32_t VDecDemo::Release()
174 {
175     return vdec_->Release();
176 }
177 
SetOutputSurface(sptr<Surface> surface)178 void VDecDemo::SetOutputSurface(sptr<Surface> surface)
179 {
180     surface_ = surface;
181 }
182 
SetWindowSize(uint32_t width,uint32_t height)183 void VDecDemo::SetWindowSize(uint32_t width, uint32_t height)
184 {
185     width_ = width;
186     height_ = height;
187 }
188 
SetSurface()189 int32_t VDecDemo::SetSurface()
190 {
191     return vdec_->SetOutputSurface(surface_);
192 }
193 
CheckCodecType()194 void VDecDemo::CheckCodecType()
195 {
196     std::vector<std::string> localCodecArray;
197     std::shared_ptr<Media::AVCodecList> codecList = Media::AVCodecListFactory::CreateAVCodecList();
198     std::vector<std::shared_ptr<Media::VideoCaps>> caps = codecList->GetVideoEncoderCaps();
199     for (const auto &cap : caps) {
200         std::shared_ptr<Media::AVCodecInfo> codecInfo = cap->GetCodecInfo();
201         localCodecArray.push_back(codecInfo->GetName());
202     }
203 
204     if (std::find(localCodecArray.begin(), localCodecArray.end(),
205         CODEC_NAME_H264) != localCodecArray.end()) {
206         DHLOGI("device is W");
207         isW = true;
208     } else if (std::find(localCodecArray.begin(), localCodecArray.end(),
209         CODEC_NAME_MPEG4) != localCodecArray.end()) {
210         DHLOGI("device is R");
211         isW = false;
212     }
213 }
214 
GetFrameLen()215 const int32_t* VDecDemo::GetFrameLen()
216 {
217     const int32_t* frameLen = nullptr;
218     if (isW) {
219         frameLen = ES_W;
220         defaultFrameCount_ = sizeof(ES_W) / sizeof(ES_W[0]);
221     } else {
222         frameLen = ES_R;
223         defaultFrameCount_ = sizeof(ES_R) / sizeof(ES_R[0]);
224     }
225     return frameLen;
226 }
227 
InputFunc()228 void VDecDemo::InputFunc()
229 {
230     const int32_t *frameLen = GetFrameLen();
231 
232     while (true) {
233         if (!isRunning_.load()) {
234             return;
235         }
236 
237         unique_lock<mutex> lock(signal_->inMutex_);
238         signal_->inCond_.wait(lock, [this]() { return signal_->inQueue_.size() > 0; });
239 
240         if (!isRunning_.load()) {
241             return;
242         }
243 
244         uint32_t index = signal_->inQueue_.front();
245         auto buffer = vdec_->GetInputBuffer(index);
246 
247         char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * (*frameLen) + 1));
248         if (fileBuffer == nullptr) {
249             return;
250         }
251 
252         (void)testFile_->read(fileBuffer, *frameLen);
253         if (memcpy_s(buffer->GetBase(), buffer->GetSize(), fileBuffer, *frameLen) != EOK) {
254             free(fileBuffer);
255             DHLOGI("Fatal: memcpy fail");
256             return;
257         }
258 
259         AVCodecBufferInfo info;
260         info.size = *frameLen;
261         info.offset = 0;
262         info.presentationTimeUs = timeStamp_;
263 
264         int32_t ret = 0;
265         if (isFirstFrame_) {
266             ret = vdec_->QueueInputBuffer(index, info, AVCODEC_BUFFER_FLAG_CODEC_DATA);
267             isFirstFrame_ = false;
268         } else {
269             ret = vdec_->QueueInputBuffer(index, info, AVCODEC_BUFFER_FLAG_NONE);
270         }
271 
272         free(fileBuffer);
273         frameLen++;
274         timeStamp_ += FRAME_DURATION_US;
275         signal_->inQueue_.pop();
276 
277         frameCount_++;
278         if (frameCount_ == defaultFrameCount_) {
279             DHLOGI("Finish decode, exit");
280             return;
281         }
282 
283         if (ret != 0) {
284             DHLOGI("Fatal error, exit");
285             return;
286         }
287     }
288 }
289 
OutputFunc()290 void VDecDemo::OutputFunc()
291 {
292     while (true) {
293         if (!isRunning_.load()) {
294             return;
295         }
296 
297         unique_lock<mutex> lock(signal_->outMutex_);
298         signal_->outCond_.wait(lock, [this]() { return signal_->outQueue_.size() > 0; });
299 
300         if (!isRunning_.load()) {
301             return;
302         }
303 
304         uint32_t index = signal_->outQueue_.front();
305         if (vdec_->ReleaseOutputBuffer(index, true) != 0) {
306             DHLOGI("Fatal: ReleaseOutputBuffer fail");
307             return;
308         }
309 
310         signal_->outQueue_.pop();
311     }
312 }
313 
OnError(AVCodecErrorType errorType,int32_t errorCode)314 void VDecDemoCallback::OnError(AVCodecErrorType errorType, int32_t errorCode)
315 {
316     DHLOGI("Error received, errorType: %s, errorCode: %d", errorType, errorCode);
317 }
318 
OnOutputFormatChanged(const Format & format)319 void VDecDemoCallback::OnOutputFormatChanged(const Format &format)
320 {
321     DHLOGI("OnOutputFormatChanged received");
322 }
323 
OnInputBufferAvailable(uint32_t index)324 void VDecDemoCallback::OnInputBufferAvailable(uint32_t index)
325 {
326     DHLOGI("OnInputBufferAvailable received, index: %d", index);
327     unique_lock<mutex> lock(signal_->inMutex_);
328     signal_->inQueue_.push(index);
329     signal_->inCond_.notify_all();
330 }
331 
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)332 void VDecDemoCallback::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
333 {
334     DHLOGI("OnOutputBufferAvailable received, index: %d", index);
335     unique_lock<mutex> lock(signal_->outMutex_);
336     signal_->outQueue_.push(index);
337     signal_->outCond_.notify_all();
338 }
339