File: GStreamerCodecUtilities.cpp

package info (click to toggle)
webkit2gtk 2.44.2-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 367,728 kB
  • sloc: cpp: 2,946,520; javascript: 194,328; ansic: 137,901; python: 45,716; ruby: 18,487; asm: 16,672; perl: 16,476; xml: 4,376; yacc: 2,350; sh: 2,127; java: 1,711; lex: 1,323; pascal: 333; makefile: 323
file content (243 lines) | stat: -rw-r--r-- 12,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2022 Igalia S.L
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * aint with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "GStreamerCodecUtilities.h"

#if USE(GSTREAMER)

#include "HEVCUtilities.h"
#include "VP9Utilities.h"
#include <gst/pbutils/codec-utils.h>
#include <gst/video/video.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringToIntegerConversion.h>
#include <wtf/text/WTFString.h>

GST_DEBUG_CATEGORY(webkit_gst_codec_utilities_debug);
#define GST_CAT_DEFAULT webkit_gst_codec_utilities_debug

namespace WebCore {

static void ensureDebugCategoryInitialized()
{
    static std::once_flag debugRegisteredFlag;
    std::call_once(debugRegisteredFlag, [] {
        GST_DEBUG_CATEGORY_INIT(webkit_gst_codec_utilities_debug, "webkitcodec", 0, "WebKit Codecs Utilities");
    });
}

std::pair<const char*, const char*> GStreamerCodecUtilities::parseH264ProfileAndLevel(const String& codec)
{
    ensureDebugCategoryInitialized();

    auto components = codec.split('.');
    long int spsAsInteger = strtol(components[1].utf8().data(), nullptr, 16);
    uint8_t sps[3];
    sps[0] = spsAsInteger >> 16;
    sps[1] = spsAsInteger >> 8;
    sps[2] = spsAsInteger;

    const char* profile = gst_codec_utils_h264_get_profile(sps, 3);
    const char* level = gst_codec_utils_h264_get_level(sps, 3);

    // To avoid going through a class hierarchy for such a simple
    // string conversion, we use a little trick here: See
    // https://bugs.webkit.org/show_bug.cgi?id=201870.
    char levelAsStringFallback[2] = { '\0', '\0' };
    if (!level && sps[2] > 0 && sps[2] <= 5) {
        levelAsStringFallback[0] = static_cast<char>('0' + sps[2]);
        level = levelAsStringFallback;
    }

    GST_DEBUG("Codec %s translates to H.264 profile %s and level %s", codec.utf8().data(), GST_STR_NULL(profile), GST_STR_NULL(level));
    return { profile, level };
}

const char* GStreamerCodecUtilities::parseHEVCProfile(const String& codec)
{
    ensureDebugCategoryInitialized();

    GST_DEBUG("Parsing HEVC codec string: %s", codec.ascii().data());
    auto parameters = parseHEVCCodecParameters(codec);
    if (!parameters) {
        GST_WARNING("Invalid HEVC codec: %s", codec.ascii().data());
        return nullptr;
    }

    if (parameters->generalProfileSpace > 3) {
        GST_WARNING("Invalid general_profile_space: %u", parameters->generalProfileSpace);
        return nullptr;
    }

    if (parameters->generalProfileIDC > 0x1F) {
        GST_WARNING("Invalid general_profile_idc: %u", parameters->generalProfileIDC);
        return nullptr;
    }

    uint8_t profileTierLevel[11] = { 0, };
    memset(profileTierLevel, 0, 11);
    profileTierLevel[0] = parameters->generalProfileIDC;

    if (profileTierLevel[0] >= 4) {
        auto& constraints = parameters->generalConstraintIndicatorFlags;
        for (unsigned i = 5, j = 0; i < 10; i++, j++)
            profileTierLevel[i] = constraints[j];
    }

    return gst_codec_utils_h265_get_profile(profileTierLevel, sizeof(profileTierLevel));
}

static std::pair<GRefPtr<GstCaps>, GRefPtr<GstCaps>> vpxCapsFromCodecString(const String& codecString, unsigned width, unsigned height, int framerateNumerator, int framerateDenominator)
{
    auto parameters = parseVPCodecParameters(codecString);
    if (!parameters)
        return { nullptr, nullptr };

    auto inputCaps = adoptGRef(gst_caps_new_any());

    if (parameters->codecName.startsWith("vp8"_s) || parameters->codecName.startsWith("vp08"_s))
        return { inputCaps, adoptGRef(gst_caps_new_empty_simple("video/x-vp8")) };

    auto outputCaps = adoptGRef(gst_caps_new_empty_simple("video/x-vp9"));
    auto profile = makeString(parameters->profile);
    gst_caps_set_simple(outputCaps.get(), "profile", G_TYPE_STRING, profile.ascii().data(), nullptr);
    const char* yuvFormat = "I420";
    if (parameters->chromaSubsampling == VPConfigurationChromaSubsampling::Subsampling_422)
        yuvFormat = "I422";
    else if (parameters->chromaSubsampling == VPConfigurationChromaSubsampling::Subsampling_444)
        yuvFormat = "Y444";
    StringBuilder formatBuilder;
    formatBuilder.append(yuvFormat);
    if (parameters->bitDepth > 8) {
        formatBuilder.append('_', parameters->bitDepth);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
        formatBuilder.append("LE"_s);
#else
        formatBuilder.append("BE"_s);
#endif
    }

    auto formatString = formatBuilder.toString();
    auto format = gst_video_format_from_string(formatString.ascii().data());
    GstVideoInfo info;
    gst_video_info_set_format(&info, format, width, height);

    if (parameters->videoFullRangeFlag == VPConfigurationRange::FullRange)
        GST_VIDEO_INFO_COLORIMETRY(&info).range = GST_VIDEO_COLOR_RANGE_0_255;
    else
        GST_VIDEO_INFO_COLORIMETRY(&info).range = GST_VIDEO_COLOR_RANGE_16_235;

    auto primaries = parameters->colorPrimaries;
    if (primaries == VPConfigurationColorPrimaries::BT_709_6)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
    else if (primaries == VPConfigurationColorPrimaries::BT_470_6_M)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
    else if (primaries == VPConfigurationColorPrimaries::BT_470_7_BG || primaries == VPConfigurationColorPrimaries::BT_601_7)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
    else if (primaries == VPConfigurationColorPrimaries::SMPTE_ST_240)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
    else if (primaries == VPConfigurationColorPrimaries::Film)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
    else if (primaries == VPConfigurationColorPrimaries::BT_2020_Nonconstant_Luminance)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
    else if (primaries == VPConfigurationColorPrimaries::SMPTE_ST_428_1)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTEST428;
    else if (primaries == VPConfigurationColorPrimaries::SMPTE_RP_431_2)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTERP431;
    else if (primaries == VPConfigurationColorPrimaries::SMPTE_EG_432_1)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTEEG432;
    else if (primaries == VPConfigurationColorPrimaries::EBU_Tech_3213_E)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_EBU3213;
    else if (primaries == VPConfigurationColorPrimaries::Unspecified)
        GST_VIDEO_INFO_COLORIMETRY(&info).primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;

    auto transfer = parameters->transferCharacteristics;
    if (transfer == VPConfigurationTransferCharacteristics::BT_709_6 || transfer == VPConfigurationTransferCharacteristics::BT_470_6_M || transfer == VPConfigurationTransferCharacteristics::BT_1361_0)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_BT709;
    else if (transfer == VPConfigurationTransferCharacteristics::BT_470_7_BG)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_GAMMA28;
    else if (transfer == VPConfigurationTransferCharacteristics::BT_601_7)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_BT601;
    else if (transfer == VPConfigurationTransferCharacteristics::SMPTE_ST_240)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_SMPTE240M;
    else if (transfer == VPConfigurationTransferCharacteristics::Linear)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_GAMMA10;
    else if (transfer == VPConfigurationTransferCharacteristics::Logrithmic)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_LOG100;
    else if (transfer == VPConfigurationTransferCharacteristics::Logrithmic_Sqrt)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_LOG316;
    else if (transfer == VPConfigurationTransferCharacteristics::IEC_61966_2_4)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_SRGB;
    else if (transfer == VPConfigurationTransferCharacteristics::IEC_61966_2_1) {
        GST_WARNING("VPConfigurationTransferCharacteristics::IEC_61966_2_1 not supported");
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_UNKNOWN;
    } else if (transfer == VPConfigurationTransferCharacteristics::BT_2020_10bit)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_BT2020_10;
    else if (transfer == VPConfigurationTransferCharacteristics::BT_2020_12bit)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_BT2020_12;
    else if (transfer == VPConfigurationTransferCharacteristics::SMPTE_ST_2084)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_SMPTE2084;
    else if (transfer == VPConfigurationTransferCharacteristics::SMPTE_ST_428_1) {
        GST_WARNING("VPConfigurationTransferCharacteristics::SMPTE_ST_428_1 not supported");
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_UNKNOWN;
    } else if (transfer == VPConfigurationTransferCharacteristics::BT_2100_HLG)
        GST_VIDEO_INFO_COLORIMETRY(&info).transfer = GST_VIDEO_TRANSFER_ARIB_STD_B67;

    auto matrix = parameters->matrixCoefficients;
    if (matrix == VPConfigurationMatrixCoefficients::Identity)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_RGB;
    else if (matrix == VPConfigurationMatrixCoefficients::BT_709_6)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_BT709;
    else if (matrix == VPConfigurationMatrixCoefficients::Unspecified)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
    else if (matrix == VPConfigurationMatrixCoefficients::FCC)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_FCC;
    else if (matrix == VPConfigurationMatrixCoefficients::BT_601_7)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_BT601;
    else if (matrix == VPConfigurationMatrixCoefficients::SMPTE_ST_240)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
    else if (matrix == VPConfigurationMatrixCoefficients::BT_2020_Constant_Luminance)
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
    else {
        GST_WARNING("Color matrix not supported: %u", matrix);
        GST_VIDEO_INFO_COLORIMETRY(&info).matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
    }
    inputCaps = adoptGRef(gst_video_info_to_caps(&info));

    gst_caps_set_simple(inputCaps.get(), "framerate", GST_TYPE_FRACTION, framerateNumerator, framerateDenominator, nullptr);

    return { inputCaps, outputCaps };
}

std::pair<GRefPtr<GstCaps>, GRefPtr<GstCaps>> GStreamerCodecUtilities::capsFromCodecString(const String& codecString, unsigned width, unsigned height, int framerateNumerator, int framerateDenominator)
{
    if (codecString.startsWith("vp8"_s) || codecString.startsWith("vp08"_s) || codecString.startsWith("vp9"_s) || codecString.startsWith("vp09"_s))
        return vpxCapsFromCodecString(codecString, width, height, framerateNumerator, framerateDenominator);

    if (codecString.startsWith("avc1"_s))
        return { nullptr, nullptr };
    return { nullptr, nullptr };
}

#undef GST_CAT_DEFAULT

} // namespace WebCore

#endif // USE(GSTREAMER)