Debian Codegraph @

src/videofilters/h26x/videotoolbox.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 <bctoolbox/defs.h>
22
23 #include "filter-wrapper/decoding-filter-wrapper.h"
24 #include "filter-wrapper/encoding-filter-wrapper.h"
25 #include "h26x-decoder-filter.h"
26 #include "h26x-encoder-filter.h"
27 #include "videotoolbox-decoder.h"
28 #include "videotoolbox-encoder.h"
29 #include "videotoolbox-utils.h"
30
31 using namespace std;
32
33 namespace mediastreamer {
34
35 const MSVideoConfiguration vth264enc_video_confs[] = {
36 /*
37 * Formats above 720P are disabled. Indeed, there are not supported in baseline profile of H264, and it was observed
38 * that when we ask a MediaCodec to output a 1080P stream with baseline profile, we get interoperability issues:
39 * the remote decoder decodes it improperly, even on iOS.
40 * TODO: enable use of higher profiles to use formats above 720P.
41 */
42 #if 0
43 MS_VIDEO_CONF(1536000, 3000000, SXGA_MINUS, 30, 2),
44 #endif
45 MS_VIDEO_CONF(1024000, 2048000, 720P, 30, 2), MS_VIDEO_CONF(850000, 2048000, XGA, 30, 2),
46 MS_VIDEO_CONF(750000, 1500000, SVGA, 30, 2), MS_VIDEO_CONF(600000, 2000000, VGA, 30, 2),
47 MS_VIDEO_CONF(400000, 800000, VGA, 20, 1), MS_VIDEO_CONF(200000, 350000, CIF, 18, 1),
48 MS_VIDEO_CONF(150000, 200000, QVGA, 15, 1), MS_VIDEO_CONF(100000, 150000, QVGA, 10, 1),
49 MS_VIDEO_CONF(64000, 100000, QCIF, 12, 1), MS_VIDEO_CONF(0, 64000, QCIF, 5, 1)};
50
51 class VideoToolboxH264EncoderFilterImpl : public H26xEncoderFilter {
52 public:
53 VideoToolboxH264EncoderFilterImpl(MSFilter *f)
54 : H26xEncoderFilter(f,
55 new VideoToolboxEncoder("video/avc", ms_factory_get_payload_max_size(f->factory)),
56 vth264enc_video_confs) {
57 }
58 };
59
60 class VideoToolboxH264DecoderFilterImpl : public H26xDecoderFilter {
61 public:
62 VideoToolboxH264DecoderFilterImpl(MSFilter *f) : H26xDecoderFilter(f, new VideoToolboxDecoder("video/avc")) {
63 }
64 };
65
66 class VideoToolboxH265EncoderFilterImpl : public H26xEncoderFilter {
67 public:
68 VideoToolboxH265EncoderFilterImpl(MSFilter *f)
69 : H26xEncoderFilter(f,
70 new VideoToolboxEncoder("video/hevc", ms_factory_get_payload_max_size(f->factory)),
71 vth264enc_video_confs) {
72 }
73 };
74
75 class VideoToolboxH265DecoderFilterImpl : public H26xDecoderFilter {
76 public:
77 VideoToolboxH265DecoderFilterImpl(MSFilter *f) : H26xDecoderFilter(f, new VideoToolboxDecoder("video/hevc")) {
78 }
79 };
80
81 } // namespace mediastreamer
82
83 using namespace mediastreamer;
84
85 MS_ENCODING_FILTER_WRAPPER_METHODS_DECLARATION(VideoToolboxH264Encoder);
86 MS_ENCODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(VideoToolboxH264Encoder,
87 MS_VT_H264_ENC_ID,
88 "H264 hardware encoder for iOS and MacOSX",
89 "H264",
90 MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED);
91
92 MS_DECODING_FILTER_WRAPPER_METHODS_DECLARATION(VideoToolboxH264Decoder);
93 MS_DECODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(VideoToolboxH264Decoder,
94 MS_VT_H264_DEC_ID,
95 "H264 hardware decoder for iOS and MacOSX",
96 "H264",
97 MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED);
98
99 MS_ENCODING_FILTER_WRAPPER_METHODS_DECLARATION(VideoToolboxH265Encoder);
100 MS_ENCODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(VideoToolboxH265Encoder,
101 MS_VT_H265_ENC_ID,
102 "H265 hardware encoder for iOS and MacOSX",
103 "H265",
104 MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED);
105
106 MS_DECODING_FILTER_WRAPPER_METHODS_DECLARATION(VideoToolboxH265Decoder);
107 MS_DECODING_FILTER_WRAPPER_DESCRIPTION_DECLARATION(VideoToolboxH265Decoder,
108 MS_VT_H265_DEC_ID,
109 "H265 hardware decoder for iOS and MacOSX",
110 "H265",
111 MS_FILTER_IS_PUMP | MS_FILTER_IS_HW_ACCELERATED);
112
113 extern "C" void _register_videotoolbox_if_supported(BCTBX_UNUSED(MSFactory *factory)) {
114 #if TARGET_OS_SIMULATOR
115 (void)factory;
116 ms_message("VideoToolbox H264 codec is not supported on simulators");
117 #else
118 if (kCFCoreFoundationVersionNumber >= 744.00) { // MacOS >= 10.8 or iOS >= 8.0
119 unique_ptr<VideoToolboxUtilities> codecInfo(VideoToolboxUtilities::create("video/avc"));
120 if (codecInfo->encoderIsAvailable()) {
121 ms_message("Registering VideoToolbox H264 codec");
122 ms_factory_register_filter(factory, &ms_VideoToolboxH264Encoder_desc);
123 ms_factory_register_filter(factory, &ms_VideoToolboxH264Decoder_desc);
124 } else {
125 ms_message("No H264 encoder found on this device");
126 }
127 } else {
128 ms_message("Cannot register VideoToolbox H264 codec. That "
129 "requires iOS 8 or MacOS 10.8");
130 }
131
132 if (kCFCoreFoundationVersionNumber >= 1400) { // MacOS >= 10.13 or iOS >= 11.0
133 unique_ptr<VideoToolboxUtilities> codecInfo(VideoToolboxUtilities::create("video/hevc"));
134 if (codecInfo->encoderIsAvailable()) {
135 ms_message("Registering VideoToolbox H265 codec");
136 ms_factory_register_filter(factory, &ms_VideoToolboxH265Encoder_desc);
137 ms_factory_register_filter(factory, &ms_VideoToolboxH265Decoder_desc);
138 } else {
139 ms_message("No H265 encoder found on this device");
140 }
141 } else {
142 ms_message("Cannot register VideoToolbox H265 codec. That "
143 "requires iOS 11.0 or MacOS 10.13");
144 }
145
146 #endif // !TARGET_OS_SIMULATOR
147 }