Debian Codegraph @

src/videofilters/h26x/media-codec-h264-encoder.cpp

raw
1 /*
2 * Copyright (c) 2010-2022 Belledonne Communications SARL.
3 *
4 * This file is part of mediastreamer2
5 * (see https://gitlab.linphone.org/BC/public/mediastreamer2).
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "mediastreamer2/devices.h"
22
23 #include "filter-wrapper/encoding-filter-wrapper.h"
24 #include "h26x-encoder-filter.h"
25 #include "media-codec-encoder.h"
26
27 #define MS_MEDIACODECH265_CONF(required_bitrate, bitrate_limit, resolution, fps, ncpus) \
28 { \
29 required_bitrate, bitrate_limit, {MS_VIDEO_SIZE_##resolution##_W, MS_VIDEO_SIZE_##resolution##_H}, fps, ncpus, \
30 nullptr \
31 }
32
33 static const MSVideoConfiguration _media_codec_h264_conf_list[] = {
34 /*
35 * Formats above 720P are disabled. Indeed, there are not supported in baseline profile of H264, and it was observed
36 * that when we ask a MediaCodec to output a 1080P stream with baseline profile, we get interoperability issues:
37 * the remote decoder decodes it improperly, even on iOS.
38 * TODO: enable use of higher profiles to use formats above 720P.
39 */
40 #if 0
41 MS_MEDIACODECH265_CONF(2048000, 5000000, UXGA, 25, 2),
42 MS_MEDIACODECH265_CONF(1500000, 3000000, SXGA_MINUS, 25, 2),
43 #endif
44 MS_MEDIACODECH265_CONF(1024000, 2048000, 720P, 30, 2), MS_MEDIACODECH265_CONF(850000, 2048000, XGA, 25, 2),
45 MS_MEDIACODECH265_CONF(750000, 1500000, SVGA, 30, 2), MS_MEDIACODECH265_CONF(600000, 3000000, VGA, 30, 2),
46 MS_MEDIACODECH265_CONF(400000, 800000, VGA, 15, 2), MS_MEDIACODECH265_CONF(128000, 512000, CIF, 15, 1),
47 MS_MEDIACODECH265_CONF(100000, 380000, QVGA, 15, 1), MS_MEDIACODECH265_CONF(0, 170000, QCIF, 10, 1),
48 };
49
50 namespace mediastreamer {
51
52 class MediaCodecH264Encoder : public MediaCodecEncoder {
53 public:
54 MediaCodecH264Encoder() : MediaCodecEncoder("video/avc") {
55 }
56
57 private:
58 AMediaFormat *createMediaFormat() const override {
59 AMediaFormat *format = MediaCodecEncoder::createMediaFormat();
60 AMediaFormat_setInt32(format, "profile", _profile);
61 AMediaFormat_setInt32(format, "level", _level);
62 return format;
63 }
64
65 static const int32_t _profile = 1; // AVCProfileBaseline
66 static const int32_t _level = 512; // AVCLevel31
67 };
68
69 class MediaCodecH264EncoderFilterImpl : public H26xEncoderFilter {
70 public:
71 MediaCodecH264EncoderFilterImpl(MSFilter *f)
72 : H26xEncoderFilter(f, new MediaCodecH264Encoder(), _media_codec_h264_conf_list) {
73 SoundDeviceDescription *info = ms_devices_info_get_sound_device_description(f->factory->devices_info);
74 auto &encoder = static_cast<MediaCodecEncoder &>(*_encoder);
75 encoder.enablePixelFormatConversion((info->flags & DEVICE_MCH264ENC_NO_PIX_FMT_CONV) == 0);
76 }
77 };
78
79 } // namespace mediastreamer
80
81 using namespace mediastreamer;
82
83 MS_ENCODING_FILTER_WRAPPER_METHODS_DECLARATION(MediaCodecH264Encoder);
84 MS_ENCODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(MediaCodecH264Encoder,
85 MS_MEDIACODEC_H264_ENC_ID,
86 "A H264 encoder based on MediaCodec API.",
87 "H264",
88 MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED);