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 #include "processor_venc_impl.h"
17 #include "media_errors.h"
18 #include "media_log.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "ProcessorVencImpl"};
22 constexpr int32_t MAX_WIDTH = 8000;
23 constexpr int32_t MAX_HEIGHT = 5000;
24 }
25
26 namespace OHOS {
27 namespace Media {
ProcessorVencImpl()28 ProcessorVencImpl::ProcessorVencImpl()
29 {
30 }
31
~ProcessorVencImpl()32 ProcessorVencImpl::~ProcessorVencImpl()
33 {
34 }
35
ProcessMandatory(const Format & format)36 int32_t ProcessorVencImpl::ProcessMandatory(const Format &format)
37 {
38 CHECK_AND_RETURN_RET(format.GetIntValue("width", width_) == true, MSERR_INVALID_VAL);
39 CHECK_AND_RETURN_RET(format.GetIntValue("height", height_) == true, MSERR_INVALID_VAL);
40 CHECK_AND_RETURN_RET(format.GetIntValue("pixel_format", pixelFormat_) == true, MSERR_INVALID_VAL);
41 CHECK_AND_RETURN_RET(format.GetIntValue("frame_rate", frameRate_) == true, MSERR_INVALID_VAL);
42 MEDIA_LOGD("width:%{public}d, height:%{public}d, pixel:%{public}d, framerate:%{public}d",
43 width_, height_, pixelFormat_, frameRate_);
44
45 gstPixelFormat_ = PixelFormatToGst(static_cast<VideoPixelFormat>(pixelFormat_));
46
47 return MSERR_OK;
48 }
49
ProcessOptional(const Format & format)50 int32_t ProcessorVencImpl::ProcessOptional(const Format &format)
51 {
52 if (format.GetValueType(std::string_view("video_encode_bitrate_mode")) == FORMAT_TYPE_INT32) {
53 (void)format.GetIntValue("video_encode_bitrate_mode", bitrateMode_);
54 }
55
56 if (format.GetValueType(std::string_view("codec_profile")) == FORMAT_TYPE_INT32) {
57 (void)format.GetIntValue("codec_profile", profile_);
58 }
59
60 switch (codecName_) {
61 case CODEC_MIME_TYPE_VIDEO_MPEG4:
62 gstProfile_ = MPEG4ProfileToGst(static_cast<MPEG4Profile>(profile_));
63 break;
64 case CODEC_MIME_TYPE_VIDEO_AVC:
65 gstProfile_ = AVCProfileToGst(static_cast<AVCProfile>(profile_));
66 break;
67 case CODEC_MIME_TYPE_VIDEO_HEVC:
68 gstProfile_ = HEVCProfileToGst(static_cast<HEVCProfile>(profile_));
69 break;
70 default:
71 break;
72 }
73
74 if (format.GetValueType(std::string_view("i_frame_interval")) == FORMAT_TYPE_INT32) {
75 (void)format.GetIntValue("i_frame_interval", keyFrameInterval_);
76 }
77
78 return MSERR_OK;
79 }
80
GetInputPortConfig()81 std::shared_ptr<ProcessorConfig> ProcessorVencImpl::GetInputPortConfig()
82 {
83 CHECK_AND_RETURN_RET(width_ > 0 && static_cast<uint32_t>(width_) < MAX_WIDTH, nullptr);
84 CHECK_AND_RETURN_RET(height_ > 0 && static_cast<uint32_t>(height_) < MAX_HEIGHT, nullptr);
85
86 GstCaps *caps = gst_caps_new_simple("video/x-raw",
87 "width", G_TYPE_INT, width_,
88 "height", G_TYPE_INT, height_,
89 "format", G_TYPE_STRING, gstPixelFormat_.c_str(),
90 "framerate", GST_TYPE_FRACTION, frameRate_, 1, nullptr);
91 CHECK_AND_RETURN_RET_LOG(caps != nullptr, nullptr, "No memory");
92
93 auto config = std::make_shared<ProcessorConfig>(caps, true);
94 constexpr uint32_t alignment = 16;
95 config->bufferSize_ = PixelBufferSize(static_cast<VideoPixelFormat>(pixelFormat_),
96 static_cast<uint32_t>(width_), static_cast<uint32_t>(height_), alignment);
97
98 return config;
99 }
100
GetOutputPortConfig()101 std::shared_ptr<ProcessorConfig> ProcessorVencImpl::GetOutputPortConfig()
102 {
103 CHECK_AND_RETURN_RET(width_ > 0 && static_cast<uint32_t>(width_) < MAX_WIDTH, nullptr);
104 CHECK_AND_RETURN_RET(height_ > 0 && static_cast<uint32_t>(height_) < MAX_HEIGHT, nullptr);
105
106 GstCaps *caps = nullptr;
107 switch (codecName_) {
108 case CODEC_MIME_TYPE_VIDEO_MPEG4:
109 caps = gst_caps_new_simple("video/mpeg",
110 "mpegversion", G_TYPE_INT, 4,
111 "systemstream", G_TYPE_BOOLEAN, FALSE, nullptr);
112 break;
113 case CODEC_MIME_TYPE_VIDEO_AVC:
114 caps = gst_caps_new_simple("video/x-h264",
115 "stream-format", G_TYPE_STRING, "byte-stream", nullptr);
116 break;
117 case CODEC_MIME_TYPE_VIDEO_HEVC:
118 caps = gst_caps_new_simple("video/x-h265",
119 "stream-format", G_TYPE_STRING, "byte-stream", nullptr);
120 break;
121 default:
122 break;
123 }
124
125 CHECK_AND_RETURN_RET_LOG(caps != nullptr, nullptr, "Unsupported format");
126
127 auto config = std::make_shared<ProcessorConfig>(caps, true);
128 constexpr uint32_t alignment = 16;
129 config->bufferSize_ = PixelBufferSize(static_cast<VideoPixelFormat>(pixelFormat_), width_, height_, alignment);
130
131 return config;
132 }
133 } // namespace Media
134 } // namespace OHOS
135