MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / createCodecContext

Method createCodecContext

src/video_decoder.ts:91–145  ·  view source on GitHub ↗
(config: VideoDecoderConfig)

Source from the content-addressed store, hash-verified

89 }
90
91 private static createCodecContext(config: VideoDecoderConfig) {
92 const inferred = inferCodecFromCodecString(config.codec);
93 if (!inferred) {
94 return null;
95 }
96
97 let codec: Codec | null;
98 if (inferred === AV_CODEC_ID_AV1 || config.hardwareAcceleration !== 'prefer-hardware') {
99 // https://github.com/opencv/opencv/issues/24430
100 codec = Codec.findDecoder(inferred);
101 } else {
102 codec = getHardwareDecoderCodec(inferred) ?? Codec.findDecoder(inferred);
103 }
104
105 if (!codec) {
106 return null;
107 }
108
109 const codecContext = new CodecContext();
110 codecContext.allocContext3(codec);
111
112 codecContext.width = config.codedWidth ?? 0; // Fucky that the dimensions can be optional, but oh well
113 codecContext.height = config.codedHeight ?? 0;
114 codecContext.codecType = AVMEDIA_TYPE_VIDEO;
115 codecContext.codecId = inferred;
116 codecContext.extraData = config.description
117 ? Buffer.from(toUint8Array(config.description))
118 : null;
119
120 if (config.colorSpace?.primaries) {
121 const mapped = mapColorPrimaries(config.colorSpace.primaries);
122 if (mapped !== null) {
123 codecContext.colorPrimaries = mapped;
124 }
125 }
126 if (config.colorSpace?.transfer) {
127 const mapped = mapTransferCharacteristics(config.colorSpace.transfer);
128 if (mapped !== null) {
129 codecContext.colorTrc = mapped;
130 }
131 }
132 if (config.colorSpace?.matrix) {
133 const mapped = mapMatrixCoefficients(config.colorSpace.matrix);
134 if (mapped !== null) {
135 codecContext.colorSpace = mapped;
136 }
137 }
138 if (config.colorSpace?.fullRange != null) {
139 codecContext.colorRange = config.colorSpace.fullRange
140 ? AVCOL_RANGE_JPEG
141 : AVCOL_RANGE_MPEG;
142 }
143
144 return codecContext;
145 }
146
147 /**
148 * Checks if the decoder can support the given configuration.

Callers 2

isConfigSupportedMethod · 0.95
configureInternalMethod · 0.45

Calls 6

getHardwareDecoderCodecFunction · 0.85
toUint8ArrayFunction · 0.85
mapColorPrimariesFunction · 0.85
mapMatrixCoefficientsFunction · 0.85

Tested by

no test coverage detected