Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / dom / media / VideoUtils.cpp
blobcacda4032786b242f3e7e36f44bd94219a3d580e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "VideoUtils.h"
7 #include <functional>
8 #include <stdint.h>
10 #include "CubebUtils.h"
11 #include "ImageContainer.h"
12 #include "MediaContainerType.h"
13 #include "MediaResource.h"
14 #include "TimeUnits.h"
15 #include "VorbisUtils.h"
16 #include "mozilla/Base64.h"
17 #include "mozilla/dom/ContentChild.h"
18 #include "mozilla/SchedulerGroup.h"
19 #include "mozilla/SharedThreadPool.h"
20 #include "mozilla/StaticPrefs_accessibility.h"
21 #include "mozilla/StaticPrefs_media.h"
22 #include "mozilla/TaskCategory.h"
23 #include "mozilla/TaskQueue.h"
24 #include "mozilla/Telemetry.h"
25 #include "nsCharSeparatedTokenizer.h"
26 #include "nsContentTypeParser.h"
27 #include "nsIConsoleService.h"
28 #include "nsINetworkLinkService.h"
29 #include "nsIRandomGenerator.h"
30 #include "nsMathUtils.h"
31 #include "nsNetCID.h"
32 #include "nsServiceManagerUtils.h"
33 #include "nsThreadUtils.h"
34 #include "AudioStream.h"
36 namespace mozilla {
38 using gfx::ColorRange;
39 using gfx::CICP::ColourPrimaries;
40 using gfx::CICP::MatrixCoefficients;
41 using gfx::CICP::TransferCharacteristics;
42 using layers::PlanarYCbCrImage;
43 using media::TimeUnit;
45 double ToMicrosecondResolution(double aSeconds) {
46 double integer;
47 modf(aSeconds * USECS_PER_S, &integer);
48 return integer / USECS_PER_S;
51 CheckedInt64 SaferMultDiv(int64_t aValue, uint64_t aMul, uint64_t aDiv) {
52 if (aMul > INT64_MAX || aDiv > INT64_MAX) {
53 return CheckedInt64(INT64_MAX) + 1; // Return an invalid checked int.
55 int64_t mul = aMul;
56 int64_t div = aDiv;
57 int64_t major = aValue / div;
58 int64_t remainder = aValue % div;
59 return CheckedInt64(remainder) * mul / div + CheckedInt64(major) * mul;
62 // Converts from number of audio frames to microseconds, given the specified
63 // audio rate.
64 CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate) {
65 return SaferMultDiv(aFrames, USECS_PER_S, aRate);
68 // Converts from microseconds to number of audio frames, given the specified
69 // audio rate.
70 CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate) {
71 return SaferMultDiv(aUsecs, aRate, USECS_PER_S);
74 // Format TimeUnit as number of frames at given rate.
75 CheckedInt64 TimeUnitToFrames(const TimeUnit& aTime, uint32_t aRate) {
76 return aTime.IsValid() ? UsecsToFrames(aTime.ToMicroseconds(), aRate)
77 : CheckedInt64(INT64_MAX) + 1;
80 nsresult SecondsToUsecs(double aSeconds, int64_t& aOutUsecs) {
81 if (aSeconds * double(USECS_PER_S) > double(INT64_MAX)) {
82 return NS_ERROR_FAILURE;
84 aOutUsecs = int64_t(aSeconds * double(USECS_PER_S));
85 return NS_OK;
88 static int32_t ConditionDimension(float aValue) {
89 // This will exclude NaNs and too-big values.
90 if (aValue > 1.0 && aValue <= float(INT32_MAX)) {
91 return int32_t(NS_round(aValue));
93 return 0;
96 void ScaleDisplayByAspectRatio(gfx::IntSize& aDisplay, float aAspectRatio) {
97 if (aAspectRatio > 1.0) {
98 // Increase the intrinsic width
99 aDisplay.width = ConditionDimension(aAspectRatio * aDisplay.width);
100 } else {
101 // Increase the intrinsic height
102 aDisplay.height = ConditionDimension(aDisplay.height / aAspectRatio);
106 static int64_t BytesToTime(int64_t offset, int64_t length, int64_t durationUs) {
107 NS_ASSERTION(length > 0, "Must have positive length");
108 double r = double(offset) / double(length);
109 if (r > 1.0) {
110 r = 1.0;
112 return int64_t(double(durationUs) * r);
115 media::TimeIntervals GetEstimatedBufferedTimeRanges(
116 mozilla::MediaResource* aStream, int64_t aDurationUsecs) {
117 media::TimeIntervals buffered;
118 // Nothing to cache if the media takes 0us to play.
119 if (aDurationUsecs <= 0 || !aStream) {
120 return buffered;
123 // Special case completely cached files. This also handles local files.
124 if (aStream->IsDataCachedToEndOfResource(0)) {
125 buffered += media::TimeInterval(TimeUnit::Zero(),
126 TimeUnit::FromMicroseconds(aDurationUsecs));
127 return buffered;
130 int64_t totalBytes = aStream->GetLength();
132 // If we can't determine the total size, pretend that we have nothing
133 // buffered. This will put us in a state of eternally-low-on-undecoded-data
134 // which is not great, but about the best we can do.
135 if (totalBytes <= 0) {
136 return buffered;
139 int64_t startOffset = aStream->GetNextCachedData(0);
140 while (startOffset >= 0) {
141 int64_t endOffset = aStream->GetCachedDataEnd(startOffset);
142 // Bytes [startOffset..endOffset] are cached.
143 NS_ASSERTION(startOffset >= 0, "Integer underflow in GetBuffered");
144 NS_ASSERTION(endOffset >= 0, "Integer underflow in GetBuffered");
146 int64_t startUs = BytesToTime(startOffset, totalBytes, aDurationUsecs);
147 int64_t endUs = BytesToTime(endOffset, totalBytes, aDurationUsecs);
148 if (startUs != endUs) {
149 buffered += media::TimeInterval(TimeUnit::FromMicroseconds(startUs),
150 TimeUnit::FromMicroseconds(endUs));
152 startOffset = aStream->GetNextCachedData(endOffset);
154 return buffered;
157 void DownmixStereoToMono(mozilla::AudioDataValue* aBuffer, uint32_t aFrames) {
158 MOZ_ASSERT(aBuffer);
159 const int channels = 2;
160 for (uint32_t fIdx = 0; fIdx < aFrames; ++fIdx) {
161 #ifdef MOZ_SAMPLE_TYPE_FLOAT32
162 float sample = 0.0;
163 #else
164 int sample = 0;
165 #endif
166 // The sample of the buffer would be interleaved.
167 sample = (aBuffer[fIdx * channels] + aBuffer[fIdx * channels + 1]) * 0.5f;
168 aBuffer[fIdx * channels] = aBuffer[fIdx * channels + 1] = sample;
172 uint32_t DecideAudioPlaybackChannels(const AudioInfo& info) {
173 if (StaticPrefs::accessibility_monoaudio_enable()) {
174 return 1;
177 if (StaticPrefs::media_forcestereo_enabled()) {
178 return 2;
181 return info.mChannels;
184 uint32_t DecideAudioPlaybackSampleRate(const AudioInfo& aInfo,
185 bool aShouldResistFingerprinting) {
186 bool resampling = StaticPrefs::media_resampling_enabled();
188 uint32_t rate = 0;
190 if (resampling) {
191 rate = 48000;
192 } else if (aInfo.mRate >= 44100) {
193 // The original rate is of good quality and we want to minimize unecessary
194 // resampling, so we let cubeb decide how to resample (if needed).
195 rate = aInfo.mRate;
196 } else {
197 // We will resample all data to match cubeb's preferred sampling rate.
198 rate = CubebUtils::PreferredSampleRate(aShouldResistFingerprinting);
199 if (rate > 384000) {
200 // bogus rate, fall back to something else;
201 rate = 48000;
204 MOZ_DIAGNOSTIC_ASSERT(rate, "output rate can't be 0.");
206 return rate;
209 bool IsDefaultPlaybackDeviceMono() {
210 return CubebUtils::MaxNumberOfChannels() == 1;
213 bool IsVideoContentType(const nsCString& aContentType) {
214 constexpr auto video = "video"_ns;
215 return FindInReadable(video, aContentType);
218 bool IsValidVideoRegion(const gfx::IntSize& aFrame,
219 const gfx::IntRect& aPicture,
220 const gfx::IntSize& aDisplay) {
221 return aFrame.width > 0 && aFrame.width <= PlanarYCbCrImage::MAX_DIMENSION &&
222 aFrame.height > 0 &&
223 aFrame.height <= PlanarYCbCrImage::MAX_DIMENSION &&
224 aFrame.width * aFrame.height <= MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
225 aPicture.width > 0 &&
226 aPicture.width <= PlanarYCbCrImage::MAX_DIMENSION &&
227 aPicture.x < PlanarYCbCrImage::MAX_DIMENSION &&
228 aPicture.x + aPicture.width < PlanarYCbCrImage::MAX_DIMENSION &&
229 aPicture.height > 0 &&
230 aPicture.height <= PlanarYCbCrImage::MAX_DIMENSION &&
231 aPicture.y < PlanarYCbCrImage::MAX_DIMENSION &&
232 aPicture.y + aPicture.height < PlanarYCbCrImage::MAX_DIMENSION &&
233 aPicture.width * aPicture.height <=
234 MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT &&
235 aDisplay.width > 0 &&
236 aDisplay.width <= PlanarYCbCrImage::MAX_DIMENSION &&
237 aDisplay.height > 0 &&
238 aDisplay.height <= PlanarYCbCrImage::MAX_DIMENSION &&
239 aDisplay.width * aDisplay.height <= MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT;
242 already_AddRefed<SharedThreadPool> GetMediaThreadPool(MediaThreadType aType) {
243 const char* name;
244 uint32_t threads = 4;
245 switch (aType) {
246 case MediaThreadType::PLATFORM_DECODER:
247 name = "MediaPDecoder";
248 break;
249 case MediaThreadType::WEBRTC_CALL_THREAD:
250 name = "WebrtcCallThread";
251 threads = 1;
252 break;
253 case MediaThreadType::WEBRTC_WORKER:
254 name = "WebrtcWorker";
255 break;
256 case MediaThreadType::MDSM:
257 name = "MediaDecoderStateMachine";
258 threads = 1;
259 break;
260 case MediaThreadType::PLATFORM_ENCODER:
261 name = "MediaPEncoder";
262 break;
263 default:
264 MOZ_FALLTHROUGH_ASSERT("Unexpected MediaThreadType");
265 case MediaThreadType::SUPERVISOR:
266 name = "MediaSupervisor";
267 break;
270 RefPtr<SharedThreadPool> pool =
271 SharedThreadPool::Get(nsDependentCString(name), threads);
273 // Ensure a larger stack for platform decoder threads
274 if (aType == MediaThreadType::PLATFORM_DECODER) {
275 const uint32_t minStackSize = 512 * 1024;
276 uint32_t stackSize;
277 MOZ_ALWAYS_SUCCEEDS(pool->GetThreadStackSize(&stackSize));
278 if (stackSize < minStackSize) {
279 MOZ_ALWAYS_SUCCEEDS(pool->SetThreadStackSize(minStackSize));
283 return pool.forget();
286 bool ExtractVPXCodecDetails(const nsAString& aCodec, uint8_t& aProfile,
287 uint8_t& aLevel, uint8_t& aBitDepth) {
288 uint8_t dummyChromaSubsampling = 1;
289 VideoColorSpace dummyColorspace;
290 return ExtractVPXCodecDetails(aCodec, aProfile, aLevel, aBitDepth,
291 dummyChromaSubsampling, dummyColorspace);
294 bool ExtractVPXCodecDetails(const nsAString& aCodec, uint8_t& aProfile,
295 uint8_t& aLevel, uint8_t& aBitDepth,
296 uint8_t& aChromaSubsampling,
297 VideoColorSpace& aColorSpace) {
298 // Assign default value.
299 aChromaSubsampling = 1;
300 auto splitter = aCodec.Split(u'.');
301 auto fieldsItr = splitter.begin();
302 auto fourCC = *fieldsItr;
304 if (!fourCC.EqualsLiteral("vp09") && !fourCC.EqualsLiteral("vp08")) {
305 // Invalid 4CC
306 return false;
308 ++fieldsItr;
309 uint8_t primary, transfer, matrix, range;
310 uint8_t* fields[] = {&aProfile, &aLevel, &aBitDepth, &aChromaSubsampling,
311 &primary, &transfer, &matrix, &range};
312 int fieldsCount = 0;
313 nsresult rv;
314 for (; fieldsItr != splitter.end(); ++fieldsItr, ++fieldsCount) {
315 if (fieldsCount > 7) {
316 // No more than 8 fields are expected.
317 return false;
319 *(fields[fieldsCount]) =
320 static_cast<uint8_t>((*fieldsItr).ToInteger(&rv, 10));
321 // We got invalid field value, parsing error.
322 NS_ENSURE_SUCCESS(rv, false);
324 // Mandatory Fields
325 // <sample entry 4CC>.<profile>.<level>.<bitDepth>.
326 // Optional Fields
327 // <chromaSubsampling>.<colourPrimaries>.<transferCharacteristics>.
328 // <matrixCoefficients>.<videoFullRangeFlag>
329 // First three fields are mandatory(we have parsed 4CC).
330 if (fieldsCount < 3) {
331 // Invalid number of fields.
332 return false;
334 // Start to validate the parsing value.
336 // profile should be 0,1,2 or 3.
337 // See https://www.webmproject.org/vp9/profiles/
338 if (aProfile > 3) {
339 // Invalid profile.
340 return false;
343 // level, See https://www.webmproject.org/vp9/mp4/#semantics_1
344 switch (aLevel) {
345 case 10:
346 case 11:
347 case 20:
348 case 21:
349 case 30:
350 case 31:
351 case 40:
352 case 41:
353 case 50:
354 case 51:
355 case 52:
356 case 60:
357 case 61:
358 case 62:
359 break;
360 default:
361 // Invalid level.
362 return false;
365 if (aBitDepth != 8 && aBitDepth != 10 && aBitDepth != 12) {
366 // Invalid bitDepth:
367 return false;
370 if (fieldsCount == 3) {
371 // No more options.
372 return true;
375 // chromaSubsampling should be 0,1,2,3...4~7 are reserved.
376 if (aChromaSubsampling > 3) {
377 return false;
380 if (fieldsCount == 4) {
381 // No more options.
382 return true;
385 // It is an integer that is defined by the "Colour primaries"
386 // section of ISO/IEC 23001-8:2016 Table 2.
387 // We treat reserved value as false case.
388 if (primary == 0 || primary == 3 || primary > 22) {
389 // reserved value.
390 return false;
392 if (primary > 12 && primary < 22) {
393 // 13~21 are reserved values.
394 return false;
396 aColorSpace.mPrimaries = static_cast<ColourPrimaries>(primary);
398 if (fieldsCount == 5) {
399 // No more options.
400 return true;
403 // It is an integer that is defined by the
404 // "Transfer characteristics" section of ISO/IEC 23001-8:2016 Table 3.
405 // We treat reserved value as false case.
406 if (transfer == 0 || transfer == 3 || transfer > 18) {
407 // reserved value.
408 return false;
410 aColorSpace.mTransfer = static_cast<TransferCharacteristics>(transfer);
412 if (fieldsCount == 6) {
413 // No more options.
414 return true;
417 // It is an integer that is defined by the
418 // "Matrix coefficients" section of ISO/IEC 23001-8:2016 Table 4.
419 // We treat reserved value as false case.
420 if (matrix == 3 || matrix > 11) {
421 return false;
423 aColorSpace.mMatrix = static_cast<MatrixCoefficients>(matrix);
425 // If matrixCoefficients is 0 (RGB), then chroma subsampling MUST be 3
426 // (4:4:4).
427 if (aColorSpace.mMatrix == MatrixCoefficients::MC_IDENTITY &&
428 aChromaSubsampling != 3) {
429 return false;
432 if (fieldsCount == 7) {
433 // No more options.
434 return true;
437 // videoFullRangeFlag indicates the black level and range of the luma and
438 // chroma signals. 0 = legal range (e.g. 16-235 for 8 bit sample depth);
439 // 1 = full range (e.g. 0-255 for 8-bit sample depth).
440 aColorSpace.mRange = static_cast<ColorRange>(range);
441 return range <= 1;
444 bool ExtractH264CodecDetails(const nsAString& aCodec, uint8_t& aProfile,
445 uint8_t& aConstraint, uint8_t& aLevel) {
446 // H.264 codecs parameters have a type defined as avcN.PPCCLL, where
447 // N = avc type. avc3 is avcc with SPS & PPS implicit (within stream)
448 // PP = profile_idc, CC = constraint_set flags, LL = level_idc.
449 // We ignore the constraint_set flags, as it's not clear from any
450 // documentation what constraints the platform decoders support.
451 // See
452 // http://blog.pearce.org.nz/2013/11/what-does-h264avc1-codecs-parameters.html
453 // for more details.
454 if (aCodec.Length() != strlen("avc1.PPCCLL")) {
455 return false;
458 // Verify the codec starts with "avc1." or "avc3.".
459 const nsAString& sample = Substring(aCodec, 0, 5);
460 if (!sample.EqualsASCII("avc1.") && !sample.EqualsASCII("avc3.")) {
461 return false;
464 // Extract the profile_idc, constraint_flags and level_idc.
465 nsresult rv = NS_OK;
466 aProfile = Substring(aCodec, 5, 2).ToInteger(&rv, 16);
467 NS_ENSURE_SUCCESS(rv, false);
469 // Constraint flags are stored on the 6 most significant bits, first two bits
470 // are reserved_zero_2bits.
471 aConstraint = Substring(aCodec, 7, 2).ToInteger(&rv, 16);
472 NS_ENSURE_SUCCESS(rv, false);
474 aLevel = Substring(aCodec, 9, 2).ToInteger(&rv, 16);
475 NS_ENSURE_SUCCESS(rv, false);
477 if (aLevel == 9) {
478 aLevel = H264_LEVEL_1_b;
479 } else if (aLevel <= 5) {
480 aLevel *= 10;
483 return true;
486 bool ExtractAV1CodecDetails(const nsAString& aCodec, uint8_t& aProfile,
487 uint8_t& aLevel, uint8_t& aTier, uint8_t& aBitDepth,
488 bool& aMonochrome, bool& aSubsamplingX,
489 bool& aSubsamplingY, uint8_t& aChromaSamplePosition,
490 VideoColorSpace& aColorSpace) {
491 auto fourCC = Substring(aCodec, 0, 4);
493 if (!fourCC.EqualsLiteral("av01")) {
494 // Invalid 4CC
495 return false;
498 // Format is:
499 // av01.N.NN[MH].NN.B.BBN.NN.NN.NN.B
500 // where
501 // N = decimal digit
502 // [] = single character
503 // B = binary digit
504 // Field order:
505 // <sample entry 4CC>.<profile>.<level><tier>.<bitDepth>
506 // [.<monochrome>.<chromaSubsampling>
507 // .<colorPrimaries>.<transferCharacteristics>.<matrixCoefficients>
508 // .<videoFullRangeFlag>]
510 // If any optional field is found, all the rest must be included.
512 // Parsing stops but does not fail upon encountering unexpected characters
513 // at the end of an otherwise well-formed string.
515 // See https://aomediacodec.github.io/av1-isobmff/#codecsparam
517 struct AV1Field {
518 uint8_t* field;
519 size_t length;
521 uint8_t monochrome;
522 uint8_t subsampling;
523 uint8_t primary;
524 uint8_t transfer;
525 uint8_t matrix;
526 uint8_t range;
527 AV1Field fields[] = {{&aProfile, 1},
528 {&aLevel, 2},
529 // parsing loop skips tier
530 {&aBitDepth, 2},
531 {&monochrome, 1},
532 {&subsampling, 3},
533 {&primary, 2},
534 {&transfer, 2},
535 {&matrix, 2},
536 {&range, 1}};
538 auto splitter = aCodec.Split(u'.');
539 auto iter = splitter.begin();
540 ++iter;
541 size_t fieldCount = 0;
542 while (iter != splitter.end()) {
543 // Exit if there are too many fields.
544 if (fieldCount >= 9) {
545 return false;
548 AV1Field& field = fields[fieldCount];
549 auto fieldStr = *iter;
551 if (field.field == &aLevel) {
552 // Parse tier and remove it from the level field.
553 if (fieldStr.Length() < 3) {
554 return false;
556 auto tier = fieldStr[2];
557 switch (tier) {
558 case 'M':
559 aTier = 0;
560 break;
561 case 'H':
562 aTier = 1;
563 break;
564 default:
565 return false;
567 fieldStr.SetLength(2);
570 if (fieldStr.Length() < field.length) {
571 return false;
574 // Manually parse values since nsString.ToInteger silently stops parsing
575 // upon encountering unknown characters.
576 uint8_t value = 0;
577 for (size_t i = 0; i < field.length; i++) {
578 uint8_t oldValue = value;
579 char16_t character = fieldStr[i];
580 if ('0' <= character && character <= '9') {
581 value = (value * 10) + (character - '0');
582 } else {
583 return false;
585 if (value < oldValue) {
586 // Overflow is possible on the 3-digit subsampling field.
587 return false;
591 *field.field = value;
593 ++fieldCount;
594 ++iter;
596 // Field had extra characters, exit early.
597 if (fieldStr.Length() > field.length) {
598 // Disallow numbers as unexpected characters.
599 char16_t character = fieldStr[field.length];
600 if ('0' <= character && character <= '9') {
601 return false;
603 break;
607 // Spec requires profile, level/tier, bitdepth, or for all possible fields to
608 // be present.
609 if (fieldCount != 3 && fieldCount != 9) {
610 return false;
613 // Valid profiles are: Main (0), High (1), Professional (2).
614 // Levels range from 0 to 23, or 31 to remove level restrictions.
615 if (aProfile > 2 || (aLevel > 23 && aLevel != 31)) {
616 return false;
619 if (fieldCount == 3) {
620 // If only required fields are included, set to the spec defaults for the
621 // rest and continue validating.
622 aMonochrome = false;
623 aSubsamplingX = true;
624 aSubsamplingY = true;
625 aChromaSamplePosition = 0;
626 aColorSpace.mPrimaries = ColourPrimaries::CP_BT709;
627 aColorSpace.mTransfer = TransferCharacteristics::TC_BT709;
628 aColorSpace.mMatrix = MatrixCoefficients::MC_BT709;
629 aColorSpace.mRange = ColorRange::LIMITED;
630 } else {
631 // Extract the individual values for the remaining fields, and check for
632 // valid values for each.
634 // Monochrome is a boolean.
635 if (monochrome > 1) {
636 return false;
638 aMonochrome = !!monochrome;
640 // Extract individual digits of the subsampling field.
641 // Subsampling is two binary digits for x and y
642 // and one enumerated sample position field of
643 // Unknown (0), Vertical (1), Colocated (2).
644 uint8_t subsamplingX = (subsampling / 100) % 10;
645 uint8_t subsamplingY = (subsampling / 10) % 10;
646 if (subsamplingX > 1 || subsamplingY > 1) {
647 return false;
649 aSubsamplingX = !!subsamplingX;
650 aSubsamplingY = !!subsamplingY;
651 aChromaSamplePosition = subsampling % 10;
652 if (aChromaSamplePosition > 2) {
653 return false;
656 // We can validate the color space values using CICP enums, as the values
657 // are standardized in Rec. ITU-T H.273.
658 aColorSpace.mPrimaries = static_cast<ColourPrimaries>(primary);
659 aColorSpace.mTransfer = static_cast<TransferCharacteristics>(transfer);
660 aColorSpace.mMatrix = static_cast<MatrixCoefficients>(matrix);
661 if (gfx::CICP::IsReserved(aColorSpace.mPrimaries) ||
662 gfx::CICP::IsReserved(aColorSpace.mTransfer) ||
663 gfx::CICP::IsReserved(aColorSpace.mMatrix)) {
664 return false;
666 // Range is a boolean, true meaning full and false meaning limited range.
667 if (range > 1) {
668 return false;
670 aColorSpace.mRange = static_cast<ColorRange>(range);
673 // Begin validating all parameter values:
675 // Only Levels 8 and above (4.0 and greater) can specify Tier.
676 // See: 5.5.1. General sequence header OBU syntax,
677 // if ( seq_level_idx[ i ] > 7 ) seq_tier[ i ] = f(1)
678 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=42
679 // Also: Annex A, A.3. Levels, columns MainMbps and HighMbps
680 // at https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=652
681 if (aLevel < 8 && aTier > 0) {
682 return false;
685 // Supported bit depths are 8, 10 and 12.
686 if (aBitDepth != 8 && aBitDepth != 10 && aBitDepth != 12) {
687 return false;
689 // Profiles 0 and 1 only support 8-bit and 10-bit.
690 if (aProfile < 2 && aBitDepth == 12) {
691 return false;
694 // x && y subsampling is used to specify monochrome 4:0:0 as well
695 bool is420or400 = aSubsamplingX && aSubsamplingY;
696 bool is422 = aSubsamplingX && !aSubsamplingY;
697 bool is444 = !aSubsamplingX && !aSubsamplingY;
699 // Profile 0 only supports 4:2:0.
700 if (aProfile == 0 && !is420or400) {
701 return false;
703 // Profile 1 only supports 4:4:4.
704 if (aProfile == 1 && !is444) {
705 return false;
707 // Profile 2 only allows 4:2:2 at 10 bits and below.
708 if (aProfile == 2 && aBitDepth < 12 && !is422) {
709 return false;
711 // Chroma sample position can only be specified with 4:2:0.
712 if (aChromaSamplePosition != 0 && !is420or400) {
713 return false;
716 // When video is monochrome, subsampling must be 4:0:0.
717 if (aMonochrome && (aChromaSamplePosition != 0 || !is420or400)) {
718 return false;
720 // Monochrome can only be signaled when profile is 0 or 2.
721 // Note: This check is redundant with the above subsampling check,
722 // as profile 1 only supports 4:4:4.
723 if (aMonochrome && aProfile != 0 && aProfile != 2) {
724 return false;
727 // Identity matrix requires 4:4:4 subsampling.
728 if (aColorSpace.mMatrix == MatrixCoefficients::MC_IDENTITY &&
729 (aSubsamplingX || aSubsamplingY ||
730 aColorSpace.mRange != gfx::ColorRange::FULL)) {
731 return false;
734 return true;
737 nsresult GenerateRandomName(nsCString& aOutSalt, uint32_t aLength) {
738 nsresult rv;
739 nsCOMPtr<nsIRandomGenerator> rg =
740 do_GetService("@mozilla.org/security/random-generator;1", &rv);
741 if (NS_FAILED(rv)) {
742 return rv;
745 // For each three bytes of random data we will get four bytes of ASCII.
746 const uint32_t requiredBytesLength =
747 static_cast<uint32_t>((aLength + 3) / 4 * 3);
749 uint8_t* buffer;
750 rv = rg->GenerateRandomBytes(requiredBytesLength, &buffer);
751 if (NS_FAILED(rv)) {
752 return rv;
755 nsCString temp;
756 nsDependentCSubstring randomData(reinterpret_cast<const char*>(buffer),
757 requiredBytesLength);
758 rv = Base64Encode(randomData, temp);
759 free(buffer);
760 buffer = nullptr;
761 if (NS_FAILED(rv)) {
762 return rv;
765 aOutSalt = std::move(temp);
766 return NS_OK;
769 nsresult GenerateRandomPathName(nsCString& aOutSalt, uint32_t aLength) {
770 nsresult rv = GenerateRandomName(aOutSalt, aLength);
771 if (NS_FAILED(rv)) {
772 return rv;
775 // Base64 characters are alphanumeric (a-zA-Z0-9) and '+' and '/', so we need
776 // to replace illegal characters -- notably '/'
777 aOutSalt.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '_');
778 return NS_OK;
781 already_AddRefed<TaskQueue> CreateMediaDecodeTaskQueue(const char* aName) {
782 RefPtr<TaskQueue> queue = TaskQueue::Create(
783 GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER), aName);
784 return queue.forget();
787 void SimpleTimer::Cancel() {
788 if (mTimer) {
789 #ifdef DEBUG
790 nsCOMPtr<nsIEventTarget> target;
791 mTimer->GetTarget(getter_AddRefs(target));
792 bool onCurrent;
793 nsresult rv = target->IsOnCurrentThread(&onCurrent);
794 MOZ_ASSERT(NS_SUCCEEDED(rv) && onCurrent);
795 #endif
796 mTimer->Cancel();
797 mTimer = nullptr;
799 mTask = nullptr;
802 NS_IMETHODIMP
803 SimpleTimer::Notify(nsITimer* timer) {
804 RefPtr<SimpleTimer> deathGrip(this);
805 if (mTask) {
806 mTask->Run();
807 mTask = nullptr;
809 return NS_OK;
812 NS_IMETHODIMP
813 SimpleTimer::GetName(nsACString& aName) {
814 aName.AssignLiteral("SimpleTimer");
815 return NS_OK;
818 nsresult SimpleTimer::Init(nsIRunnable* aTask, uint32_t aTimeoutMs,
819 nsIEventTarget* aTarget) {
820 nsresult rv;
822 // Get target thread first, so we don't have to cancel the timer if it fails.
823 nsCOMPtr<nsIEventTarget> target;
824 if (aTarget) {
825 target = aTarget;
826 } else {
827 target = GetMainThreadSerialEventTarget();
828 if (!target) {
829 return NS_ERROR_NOT_AVAILABLE;
833 rv = NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, aTimeoutMs,
834 nsITimer::TYPE_ONE_SHOT, target);
835 if (NS_FAILED(rv)) {
836 return rv;
839 mTask = aTask;
840 return NS_OK;
843 NS_IMPL_ISUPPORTS(SimpleTimer, nsITimerCallback, nsINamed)
845 already_AddRefed<SimpleTimer> SimpleTimer::Create(nsIRunnable* aTask,
846 uint32_t aTimeoutMs,
847 nsIEventTarget* aTarget) {
848 RefPtr<SimpleTimer> t(new SimpleTimer());
849 if (NS_FAILED(t->Init(aTask, aTimeoutMs, aTarget))) {
850 return nullptr;
852 return t.forget();
855 void LogToBrowserConsole(const nsAString& aMsg) {
856 if (!NS_IsMainThread()) {
857 nsString msg(aMsg);
858 nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction(
859 "LogToBrowserConsole", [msg]() { LogToBrowserConsole(msg); });
860 SchedulerGroup::Dispatch(TaskCategory::Other, task.forget());
861 return;
863 nsCOMPtr<nsIConsoleService> console(
864 do_GetService("@mozilla.org/consoleservice;1"));
865 if (!console) {
866 NS_WARNING("Failed to log message to console.");
867 return;
869 nsAutoString msg(aMsg);
870 console->LogStringMessage(msg.get());
873 bool ParseCodecsString(const nsAString& aCodecs,
874 nsTArray<nsString>& aOutCodecs) {
875 aOutCodecs.Clear();
876 bool expectMoreTokens = false;
877 nsCharSeparatedTokenizer tokenizer(aCodecs, ',');
878 while (tokenizer.hasMoreTokens()) {
879 const nsAString& token = tokenizer.nextToken();
880 expectMoreTokens = tokenizer.separatorAfterCurrentToken();
881 aOutCodecs.AppendElement(token);
883 if (expectMoreTokens) {
884 // Last codec name was empty
885 return false;
887 return true;
890 bool ParseMIMETypeString(const nsAString& aMIMEType,
891 nsString& aOutContainerType,
892 nsTArray<nsString>& aOutCodecs) {
893 nsContentTypeParser parser(aMIMEType);
894 nsresult rv = parser.GetType(aOutContainerType);
895 if (NS_FAILED(rv)) {
896 return false;
899 nsString codecsStr;
900 parser.GetParameter("codecs", codecsStr);
901 return ParseCodecsString(codecsStr, aOutCodecs);
904 template <int N>
905 static bool StartsWith(const nsACString& string, const char (&prefix)[N]) {
906 if (N - 1 > string.Length()) {
907 return false;
909 return memcmp(string.Data(), prefix, N - 1) == 0;
912 bool IsH264CodecString(const nsAString& aCodec) {
913 uint8_t profile = 0;
914 uint8_t constraint = 0;
915 uint8_t level = 0;
916 return ExtractH264CodecDetails(aCodec, profile, constraint, level);
919 bool IsAACCodecString(const nsAString& aCodec) {
920 return aCodec.EqualsLiteral("mp4a.40.2") || // MPEG4 AAC-LC
921 aCodec.EqualsLiteral(
922 "mp4a.40.02") || // MPEG4 AAC-LC(for compatibility)
923 aCodec.EqualsLiteral("mp4a.40.5") || // MPEG4 HE-AAC
924 aCodec.EqualsLiteral(
925 "mp4a.40.05") || // MPEG4 HE-AAC(for compatibility)
926 aCodec.EqualsLiteral("mp4a.67") || // MPEG2 AAC-LC
927 aCodec.EqualsLiteral("mp4a.40.29"); // MPEG4 HE-AACv2
930 bool IsVP8CodecString(const nsAString& aCodec) {
931 uint8_t profile = 0;
932 uint8_t level = 0;
933 uint8_t bitDepth = 0;
934 return aCodec.EqualsLiteral("vp8") || aCodec.EqualsLiteral("vp8.0") ||
935 (StartsWith(NS_ConvertUTF16toUTF8(aCodec), "vp08") &&
936 ExtractVPXCodecDetails(aCodec, profile, level, bitDepth));
939 bool IsVP9CodecString(const nsAString& aCodec) {
940 uint8_t profile = 0;
941 uint8_t level = 0;
942 uint8_t bitDepth = 0;
943 return aCodec.EqualsLiteral("vp9") || aCodec.EqualsLiteral("vp9.0") ||
944 (StartsWith(NS_ConvertUTF16toUTF8(aCodec), "vp09") &&
945 ExtractVPXCodecDetails(aCodec, profile, level, bitDepth));
948 bool IsAV1CodecString(const nsAString& aCodec) {
949 uint8_t profile, level, tier, bitDepth, chromaPosition;
950 bool monochrome, subsamplingX, subsamplingY;
951 VideoColorSpace colorSpace;
952 return aCodec.EqualsLiteral("av1") ||
953 (StartsWith(NS_ConvertUTF16toUTF8(aCodec), "av01") &&
954 ExtractAV1CodecDetails(aCodec, profile, level, tier, bitDepth,
955 monochrome, subsamplingX, subsamplingY,
956 chromaPosition, colorSpace));
959 UniquePtr<TrackInfo> CreateTrackInfoWithMIMEType(
960 const nsACString& aCodecMIMEType) {
961 UniquePtr<TrackInfo> trackInfo;
962 if (StartsWith(aCodecMIMEType, "audio/")) {
963 trackInfo.reset(new AudioInfo());
964 trackInfo->mMimeType = aCodecMIMEType;
965 } else if (StartsWith(aCodecMIMEType, "video/")) {
966 trackInfo.reset(new VideoInfo());
967 trackInfo->mMimeType = aCodecMIMEType;
969 return trackInfo;
972 UniquePtr<TrackInfo> CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
973 const nsACString& aCodecMIMEType,
974 const MediaContainerType& aContainerType) {
975 UniquePtr<TrackInfo> trackInfo = CreateTrackInfoWithMIMEType(aCodecMIMEType);
976 if (trackInfo) {
977 VideoInfo* videoInfo = trackInfo->GetAsVideoInfo();
978 if (videoInfo) {
979 Maybe<int32_t> maybeWidth = aContainerType.ExtendedType().GetWidth();
980 if (maybeWidth && *maybeWidth > 0) {
981 videoInfo->mImage.width = *maybeWidth;
982 videoInfo->mDisplay.width = *maybeWidth;
984 Maybe<int32_t> maybeHeight = aContainerType.ExtendedType().GetHeight();
985 if (maybeHeight && *maybeHeight > 0) {
986 videoInfo->mImage.height = *maybeHeight;
987 videoInfo->mDisplay.height = *maybeHeight;
989 } else if (trackInfo->GetAsAudioInfo()) {
990 AudioInfo* audioInfo = trackInfo->GetAsAudioInfo();
991 Maybe<int32_t> maybeChannels =
992 aContainerType.ExtendedType().GetChannels();
993 if (maybeChannels && *maybeChannels > 0) {
994 audioInfo->mChannels = *maybeChannels;
996 Maybe<int32_t> maybeSamplerate =
997 aContainerType.ExtendedType().GetSamplerate();
998 if (maybeSamplerate && *maybeSamplerate > 0) {
999 audioInfo->mRate = *maybeSamplerate;
1003 return trackInfo;
1006 bool OnCellularConnection() {
1007 uint32_t linkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
1008 if (XRE_IsContentProcess()) {
1009 mozilla::dom::ContentChild* cpc =
1010 mozilla::dom::ContentChild::GetSingleton();
1011 if (!cpc) {
1012 NS_WARNING("Can't get ContentChild singleton in content process!");
1013 return false;
1015 linkType = cpc->NetworkLinkType();
1016 } else {
1017 nsresult rv;
1018 nsCOMPtr<nsINetworkLinkService> nls =
1019 do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID, &rv);
1020 if (NS_FAILED(rv)) {
1021 NS_WARNING("Can't get nsINetworkLinkService.");
1022 return false;
1025 rv = nls->GetLinkType(&linkType);
1026 if (NS_FAILED(rv)) {
1027 NS_WARNING("Can't get network link type.");
1028 return false;
1032 switch (linkType) {
1033 case nsINetworkLinkService::LINK_TYPE_UNKNOWN:
1034 case nsINetworkLinkService::LINK_TYPE_ETHERNET:
1035 case nsINetworkLinkService::LINK_TYPE_USB:
1036 case nsINetworkLinkService::LINK_TYPE_WIFI:
1037 return false;
1038 case nsINetworkLinkService::LINK_TYPE_WIMAX:
1039 case nsINetworkLinkService::LINK_TYPE_MOBILE:
1040 return true;
1043 return false;
1046 } // end namespace mozilla