1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. 3 */ 4 /* 5 * Copyright (C) 2023 Huawei Device Co., Ltd. 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef AVCODEC_SAMPLE_INFO_H 20 #define AVCODEC_SAMPLE_INFO_H 21 #include <bits/alltypes.h> 22 #include <cstdint> 23 #include <map> 24 #include <multimedia/player_framework/native_avcodec_videoencoder.h> 25 #include <multimedia/player_framework/lowpower_audio_sink_base.h> 26 #include <multimedia/player_framework/lowpower_avsink_base.h> 27 #include <string> 28 #include <condition_variable> 29 #include <queue> 30 #include <fstream> 31 #include <native_buffer/native_buffer.h> 32 #include "multimedia/player_framework/native_avcodec_base.h" 33 #include "multimedia/player_framework/native_avbuffer.h" 34 35 const std::string_view MIME_VIDEO_AVC = "video/avc"; 36 const std::string_view MIME_VIDEO_HEVC = "video/hevc"; 37 const std::string_view MIME_AUDIO_MPEG = "audio/mpeg"; 38 39 constexpr int32_t BITRATE_10M = 10 * 1024 * 1024; // 10Mbps 40 constexpr int32_t BITRATE_20M = 20 * 1024 * 1024; // 20Mbps 41 constexpr int32_t BITRATE_30M = 30 * 1024 * 1024; // 30Mbps 42 43 struct SampleInfo { 44 int32_t inputFd = -1; 45 int32_t outputFd = -1; 46 int64_t inputFileOffset = 0; 47 int64_t inputFileSize = 0; 48 std::string inputFilePath; 49 std::string videoCodecMime = ""; 50 std::string audioCodecMime = ""; 51 int32_t videoWidth = 0; 52 int32_t videoHeight = 0; 53 double frameRate = 0.0; 54 int64_t bitrate = 10 * 1024 * 1024; // 10Mbps; 55 int64_t frameInterval = 0; 56 OH_AVPixelFormat pixelFormat = AV_PIXEL_FORMAT_NV12; 57 uint32_t bitrateMode = CBR; 58 int32_t iFrameInterval = 100; 59 int32_t rangFlag = 1; 60 61 int32_t audioSampleForamt = 0; 62 int32_t audioSampleRate = 0; 63 int32_t audioChannelCount = 0; 64 int64_t audioChannelLayout = 0; 65 int64_t duration = 0; 66 uint8_t* audioCodecConfig = nullptr; 67 size_t audioCodecSize = 0; 68 bool isInit = false; 69 70 int32_t isHDRVivid = 0; 71 int32_t hevcProfile = HEVC_PROFILE_MAIN; 72 OH_ColorPrimary primary = COLOR_PRIMARY_BT2020; 73 OH_TransferCharacteristic transfer = TRANSFER_CHARACTERISTIC_HLG; 74 OH_MatrixCoefficient matrix = MATRIX_COEFFICIENT_BT2020_CL; 75 76 int32_t rotation = 0; 77 OHNativeWindow *window = nullptr; 78 79 void (*playDoneCallback)(void *context) = nullptr; 80 void (*progressCallback)(void *context) = nullptr; 81 void *playDoneCallbackData = nullptr; 82 void *progressCallbackData = nullptr; 83 }; 84 85 struct CodecBufferInfo { 86 uint32_t bufferIndex = 0; 87 uintptr_t *buffer = nullptr; 88 uint8_t *bufferAddr = nullptr; 89 OH_AVCodecBufferAttr attr = {0, 0, 0, AVCODEC_BUFFER_FLAGS_NONE}; 90 CodecBufferInfoCodecBufferInfo91 explicit CodecBufferInfo(uint8_t *addr) : bufferAddr(addr){}; CodecBufferInfoCodecBufferInfo92 CodecBufferInfo(uint8_t *addr, int32_t bufferSize) 93 : bufferAddr(addr), attr({0, bufferSize, 0, AVCODEC_BUFFER_FLAGS_NONE}){}; CodecBufferInfoCodecBufferInfo94 CodecBufferInfo(uint32_t argBufferIndex, OH_AVBuffer *argBuffer) 95 : bufferIndex(argBufferIndex), buffer(reinterpret_cast<uintptr_t *>(argBuffer)) 96 { 97 OH_AVBuffer_GetBufferAttr(argBuffer, &attr); 98 }; 99 }; 100 101 struct CodecUserData { 102 public: 103 SampleInfo *sampleInfo = nullptr; 104 105 uint32_t inputFrameCount = 0; 106 std::mutex inputMutex; 107 std::condition_variable inputCond; 108 std::queue<CodecBufferInfo> inputBufferInfoQueue; 109 110 uint32_t outputFrameCount = 0; 111 std::mutex outputMutex; 112 std::condition_variable outputCond; 113 std::mutex renderMutex; 114 std::condition_variable renderCond; 115 std::queue<CodecBufferInfo> outputBufferInfoQueue; 116 117 std::queue<unsigned char> renderQueue; 118 ClearQueueCodecUserData119 void ClearQueue() 120 { 121 { 122 std::unique_lock<std::mutex> lock(inputMutex); 123 auto emptyQueue = std::queue<CodecBufferInfo>(); 124 inputBufferInfoQueue.swap(emptyQueue); 125 } 126 { 127 std::unique_lock<std::mutex> lock(outputMutex); 128 auto emptyQueue = std::queue<CodecBufferInfo>(); 129 outputBufferInfoQueue.swap(emptyQueue); 130 } 131 } 132 }; 133 134 struct LppUserData { 135 public: 136 std::mutex inputMutex; 137 std::condition_variable inputCond; 138 std::condition_variable eosCond_; 139 std::mutex eosMutex; 140 std::mutex eosFlagMutex; 141 bool eosFlag_ {false}; 142 OH_AVSamplesBuffer *framePacket_ = nullptr; 143 std::queue<CodecBufferInfo> cacheQueue; 144 bool returnFrame = false; 145 int32_t num = 0; 146 int32_t count = 0; 147 std::mutex seekMutex_; 148 std::condition_variable seekCond_; 149 bool seekReturn_ = false; 150 int64_t position = 0; 151 }; 152 153 #endif // AVCODEC_SAMPLE_INFO_H