1 /*
2  * Copyright (C) 2024 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 "sample_utils.h"
17 #include <chrono>
18 #include <thread>
19 #include "av_codec_sample_log.h"
20 #include "native_buffer.h"
21 #include "native_avcodec_base.h"
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_TEST, "SampleUtils"};
25 }
26 
27 namespace OHOS {
28 namespace MediaAVCodec {
29 namespace Sample {
ThreadSleep(bool isValid,int32_t interval)30 void ThreadSleep(bool isValid, int32_t interval)
31 {
32     CHECK_AND_RETURN(isValid && interval > 0);
33 
34     thread_local auto lastPushTime = std::chrono::system_clock::now();
35     auto beforeSleepTime = std::chrono::system_clock::now();
36     std::this_thread::sleep_until(lastPushTime + std::chrono::milliseconds(interval));
37     lastPushTime = std::chrono::system_clock::now();
38 
39     AVCODEC_LOGV("Sleep time: %{public}2.2fms",
40         static_cast<std::chrono::duration<double, std::milli>>(lastPushTime - beforeSleepTime).count());
41 }
42 
ToGraphicPixelFormat(int32_t avPixelFormat,int32_t profile)43 int32_t ToGraphicPixelFormat(int32_t avPixelFormat, int32_t profile)
44 {
45     if (profile == HEVC_PROFILE_MAIN_10) {
46         return NATIVEBUFFER_PIXEL_FMT_YCBCR_P010;
47     }
48     switch (avPixelFormat) {
49         case AV_PIXEL_FORMAT_RGBA:
50             return NATIVEBUFFER_PIXEL_FMT_RGBA_8888;
51         case AV_PIXEL_FORMAT_YUVI420:
52             return NATIVEBUFFER_PIXEL_FMT_YCBCR_420_P;
53         case AV_PIXEL_FORMAT_NV21:
54             return NATIVEBUFFER_PIXEL_FMT_YCRCB_420_SP;
55         default:    // NV12 and others
56             return NATIVEBUFFER_PIXEL_FMT_YCBCR_420_SP;
57     }
58 }
59 } // Sample
60 } // MediaAVCodec
61 } // OHOS