Fix behavior with files with invalid pasp box (with num & den === 0) (fixes #362)

This commit is contained in:
Vanilagy
2026-05-02 14:27:25 +02:00
parent 62dfc5dd1c
commit 9ed21bf9c6
6 changed files with 70 additions and 26 deletions
+31
View File
@@ -784,6 +784,37 @@ export const validateVideoChunkMetadata = (metadata: EncodedVideoChunkMetadata |
'Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).',
);
}
if (
metadata.decoderConfig.displayAspectWidth !== undefined
&& (
!Number.isInteger(metadata.decoderConfig.displayAspectWidth)
|| metadata.decoderConfig.displayAspectWidth <= 0
)
) {
throw new TypeError(
'Video chunk metadata decoder configuration displayAspectWidth, when defined, must be a positive integer.',
);
}
if (
metadata.decoderConfig.displayAspectHeight !== undefined
&& (
!Number.isInteger(metadata.decoderConfig.displayAspectHeight)
|| metadata.decoderConfig.displayAspectHeight <= 0
)
) {
throw new TypeError(
'Video chunk metadata decoder configuration displayAspectHeight, when defined, must be a positive integer.',
);
}
if (
(metadata.decoderConfig.displayAspectWidth !== undefined)
!== (metadata.decoderConfig.displayAspectHeight !== undefined)
) {
throw new TypeError(
'Video chunk metadata decoder configuration must specify both displayAspectWidth and displayAspectHeight,'
+ ' or neither.',
);
}
if (metadata.decoderConfig.description !== undefined) {
if (!isAllowSharedBufferSource(metadata.decoderConfig.description)) {
throw new TypeError(
+7 -4
View File
@@ -1475,10 +1475,13 @@ export class IsobmffDemuxer extends Demuxer {
const num = readU32Be(slice);
const den = readU32Be(slice);
if (num > den) {
track.info.squarePixelWidth = Math.round(track.info.width * num / den);
} else {
track.info.squarePixelHeight = Math.round(track.info.height * den / num);
// https://github.com/Vanilagy/mediabunny/issues/362
if (num > 0 && den > 0) {
if (num > den) {
track.info.squarePixelWidth = Math.round(track.info.width * num / den);
} else {
track.info.squarePixelHeight = Math.round(track.info.height * den / num);
}
}
}; break;
+10 -8
View File
@@ -1051,14 +1051,16 @@ export class MatroskaDemuxer extends Demuxer {
const num = this.currentTrack.info.displayWidth * this.currentTrack.info.height;
const den = this.currentTrack.info.displayHeight * this.currentTrack.info.width;
if (num > den) {
this.currentTrack.info.squarePixelWidth = Math.round(
this.currentTrack.info.width * num / den,
);
} else {
this.currentTrack.info.squarePixelHeight = Math.round(
this.currentTrack.info.height * den / num,
);
if (num > 0 && den > 0) {
if (num > den) {
this.currentTrack.info.squarePixelWidth = Math.round(
this.currentTrack.info.width * num / den,
);
} else {
this.currentTrack.info.squarePixelHeight = Math.round(
this.currentTrack.info.height * den / num,
);
}
}
}
+2
View File
@@ -938,6 +938,8 @@ export type Rational = {
};
export const simplifyRational = (rational: Rational): Rational => {
assert(Number.isInteger(rational.num));
assert(Number.isInteger(rational.den));
assert(rational.den !== 0);
let a = Math.abs(rational.num);
+16 -12
View File
@@ -474,18 +474,22 @@ export class MpegTsDemuxer extends Demuxer {
elementaryStream.info.width = spsInfo.displayWidth;
elementaryStream.info.height = spsInfo.displayHeight;
if (spsInfo.pixelAspectRatio.num > spsInfo.pixelAspectRatio.den) {
elementaryStream.info.squarePixelWidth = Math.round(
elementaryStream.info.width
* spsInfo.pixelAspectRatio.num / spsInfo.pixelAspectRatio.den,
);
elementaryStream.info.squarePixelHeight = elementaryStream.info.height;
} else {
elementaryStream.info.squarePixelWidth = elementaryStream.info.width;
elementaryStream.info.squarePixelHeight = Math.round(
elementaryStream.info.height
* spsInfo.pixelAspectRatio.den / spsInfo.pixelAspectRatio.num,
);
const num = spsInfo.pixelAspectRatio.num;
const den = spsInfo.pixelAspectRatio.den;
if (num > 0 && den > 0) {
if (num > den) {
elementaryStream.info.squarePixelWidth = Math.round(
elementaryStream.info.width * num / den,
);
elementaryStream.info.squarePixelHeight = elementaryStream.info.height;
} else {
elementaryStream.info.squarePixelWidth = elementaryStream.info.width;
elementaryStream.info.squarePixelHeight = Math.round(
elementaryStream.info.height * den / num,
);
}
}
elementaryStream.info.colorSpace = {