1 /* 2 * Copyright (C) 2021 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 #ifndef CODEC_COMMON_H 17 #define CODEC_COMMON_H 18 19 #include <string> 20 #include <gst/audio/audio.h> 21 #include <gst/gst.h> 22 #include "avcodec_info.h" 23 #include "avcodec_common.h" 24 #include "audio_info.h" 25 #include "avsharedmemory.h" 26 #include "format.h" 27 #include "surface.h" 28 29 namespace OHOS { 30 namespace Media { 31 /** 32 * @brief Enumerates the codec mime type. 33 * 34 * @since 3.1 35 * @version 3.1 36 */ 37 enum InnerCodecMimeType : int32_t { 38 CODEC_MIMIE_TYPE_DEFAULT = -1, 39 /** H263 */ 40 CODEC_MIMIE_TYPE_VIDEO_H263, 41 /** H264 */ 42 CODEC_MIMIE_TYPE_VIDEO_AVC, 43 /** MPEG2 */ 44 CODEC_MIMIE_TYPE_VIDEO_MPEG2, 45 /** HEVC */ 46 CODEC_MIMIE_TYPE_VIDEO_HEVC, 47 /** MPEG4 */ 48 CODEC_MIMIE_TYPE_VIDEO_MPEG4, 49 /** MP3 */ 50 CODEC_MIMIE_TYPE_AUDIO_MPEG, 51 /** AAC */ 52 CODEC_MIMIE_TYPE_AUDIO_AAC, 53 /** VORBIS */ 54 CODEC_MIMIE_TYPE_AUDIO_VORBIS, 55 /** FLAC */ 56 CODEC_MIMIE_TYPE_AUDIO_FLAC, 57 /** OPUS */ 58 CODEC_MIMIE_TYPE_AUDIO_OPUS, 59 }; 60 61 enum VideoEncoderBitrateMode : int32_t { 62 VIDEO_ENCODER_BITRATE_MODE_CBR = 0, 63 VIDEO_ENCODER_BITRATE_MODE_VBR, 64 VIDEO_ENCODER_BITRATE_MODE_CQ, 65 }; 66 67 struct BufferWrapper { 68 enum Owner : int32_t { 69 APP = 0, 70 SERVER, 71 DOWNSTREAM 72 }; BufferWrapperBufferWrapper73 explicit BufferWrapper(Owner owner) 74 : owner_(owner) 75 { 76 } 77 ~BufferWrapperBufferWrapper78 ~BufferWrapper() 79 { 80 if (gstBuffer_ != nullptr) { 81 gst_buffer_unref(gstBuffer_); 82 } 83 } 84 85 GstBuffer *gstBuffer_ = nullptr; 86 AVSharedMemory *mem_ = nullptr; 87 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 88 Owner owner_ = DOWNSTREAM; 89 }; 90 91 struct ProcessorConfig { ProcessorConfigProcessorConfig92 ProcessorConfig(GstCaps *caps, bool isEncoder) 93 : caps_(caps), 94 isEncoder_(isEncoder) 95 { 96 } ~ProcessorConfigProcessorConfig97 ~ProcessorConfig() 98 { 99 if (caps_ != nullptr) { 100 gst_caps_unref(caps_); 101 } 102 } 103 GstCaps *caps_ = nullptr; 104 bool needCodecData_ = false; 105 bool needParser_ = false; 106 bool isEncoder_ = false; 107 uint32_t bufferSize_ = 0; 108 }; 109 110 std::string PixelFormatToGst(VideoPixelFormat pixel); 111 std::string MPEG4ProfileToGst(MPEG4Profile profile); 112 std::string AVCProfileToGst(AVCProfile profile); 113 std::string RawAudioFormatToGst(AudioStandard::AudioSampleFormat format); 114 int32_t MapCodecMime(const std::string &mime, InnerCodecMimeType &name); 115 int32_t CapsToFormat(GstCaps *caps, Format &format); 116 uint32_t PixelBufferSize(VideoPixelFormat pixel, uint32_t width, uint32_t height, uint32_t alignment); 117 uint32_t CompressedBufSize(uint32_t width, uint32_t height, bool isEncoder, InnerCodecMimeType type); 118 } // namespace Media 119 } // namespace OHOS 120 #endif // CODEC_COMMON_H 121