• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "buffer/avsharedmemory.h"
26 #include "meta/format.h"
27 #include "surface.h"
28 
29 namespace OHOS {
30 namespace Media {
31 enum InnerCodecMimeType : int32_t {
32     CODEC_MIME_TYPE_DEFAULT = -1,
33     /** H263 */
34     CODEC_MIME_TYPE_VIDEO_H263,
35     /** H264 */
36     CODEC_MIME_TYPE_VIDEO_AVC,
37     /** MPEG2 */
38     CODEC_MIME_TYPE_VIDEO_MPEG2,
39     /** HEVC */
40     CODEC_MIME_TYPE_VIDEO_HEVC,
41     /** MPEG4 */
42     CODEC_MIME_TYPE_VIDEO_MPEG4,
43     /** MP3 */
44     CODEC_MIME_TYPE_AUDIO_MPEG,
45     /** AAC */
46     CODEC_MIME_TYPE_AUDIO_AAC,
47     /** VORBIS */
48     CODEC_MIME_TYPE_AUDIO_VORBIS,
49     /** FLAC */
50     CODEC_MIME_TYPE_AUDIO_FLAC,
51     /** OPUS */
52     CODEC_MIME_TYPE_AUDIO_OPUS,
53 };
54 
55 enum VideoEncoderBitrateMode : int32_t {
56     VIDEO_ENCODER_BITRATE_MODE_CBR = 0,
57     VIDEO_ENCODER_BITRATE_MODE_VBR,
58     VIDEO_ENCODER_BITRATE_MODE_CQ,
59 };
60 
61 enum BufferFilterMode : int32_t {
62     BUFFER_FILTER_MODE_INVALID = 0,
63     BUFFER_FILTER_MODE_ADD_ADTS,
64 };
65 
66 struct AdtsFixedHeader {
67     uint32_t objectType = 0;
68     uint32_t samplingIndex = 0;
69     uint32_t channelConfig = 0;
70 };
71 
72 struct BufferWrapper {
73     enum Owner : int32_t {
74         APP = 0,
75         SERVER,
76         DOWNSTREAM
77     };
BufferWrapperBufferWrapper78     explicit BufferWrapper(Owner owner)
79         : owner_(owner)
80     {
81     }
82 
~BufferWrapperBufferWrapper83     ~BufferWrapper()
84     {
85         if (gstBuffer_ != nullptr) {
86             gst_buffer_unref(gstBuffer_);
87         }
88     }
89 
90     GstBuffer *gstBuffer_ = nullptr;
91     AVSharedMemory *mem_ = nullptr;
92     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
93     Owner owner_ = DOWNSTREAM;
94 };
95 
96 struct ProcessorConfig {
ProcessorConfigProcessorConfig97     ProcessorConfig(GstCaps *caps, bool isEncoder)
98         : caps_(caps),
99           isEncoder_(isEncoder)
100     {
101     }
~ProcessorConfigProcessorConfig102     ~ProcessorConfig()
103     {
104         if (caps_ != nullptr) {
105             gst_caps_unref(caps_);
106         }
107     }
108     GstCaps *caps_ = nullptr;
109     bool needCodecData_ = false;
110     bool needParser_ = false;
111     bool needFilter_ = false;
112     bool isEncoder_ = false;
113     uint32_t bufferSize_ = 0;
114     uint32_t videoRotation_ = 0;
115     BufferFilterMode filterMode_ = BUFFER_FILTER_MODE_INVALID;
116     AdtsFixedHeader adtsHead_;
117 };
118 
119 std::string PixelFormatToGst(VideoPixelFormat pixel);
120 std::string MPEG4ProfileToGst(MPEG4Profile profile);
121 std::string AVCProfileToGst(AVCProfile profile);
122 std::string HEVCProfileToGst(HEVCProfile profile);
123 std::string RawAudioFormatToGst(AudioStandard::AudioSampleFormat format);
124 int32_t MapCodecMime(const std::string &mime, InnerCodecMimeType &name);
125 int32_t CapsToFormat(GstCaps *caps, Format &format);
126 uint32_t PixelBufferSize(VideoPixelFormat pixel, uint32_t width, uint32_t height, uint32_t alignment);
127 } // namespace Media
128 } // namespace OHOS
129 #endif // CODEC_COMMON_H
130