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