Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@remotion/media-parser: Refactor for clarity #4768

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/media-parser/src/add-avc-profile-to-track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getCodecStringFromSpsAndPps} from './boxes/avc/codec-string';
import {createSpsPpsData} from './boxes/avc/create-sps-pps-data';
import {getCodecStringFromSpsAndPps} from './containers/avc/codec-string';
import {createSpsPpsData} from './containers/avc/create-sps-pps-data';
import type {VideoTrack} from './get-tracks';
import type {SpsAndPps} from './state/parser-state';

Expand Down
22 changes: 0 additions & 22 deletions packages/media-parser/src/boxes/riff/parse-riff.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/media-parser/src/buffer-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
knownIdsWithOneLength,
knownIdsWithThreeLength,
knownIdsWithTwoLength,
} from './boxes/webm/segments/all-segments';
} from './containers/webm/segments/all-segments';
import {detectFileType} from './file-types';

export class OffsetCounter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ import {
getSampleRateFromSampleFrequencyIndex,
mapAudioObjectTypeToCodecString,
} from '../../aac-codecprivate';
import type {BufferIterator} from '../../buffer-iterator';
import {convertAudioOrVideoSampleToWebCodecsTimestamps} from '../../convert-audio-or-video-sample';
import type {ParseResult} from '../../parse-result';
import {registerTrack} from '../../register-track';
import type {ParserState} from '../../state/parser-state';

export const parseAac = async ({
iterator,
state,
}: {
iterator: BufferIterator;
state: ParserState;
}): Promise<ParseResult> => {
export const parseAac = async (state: ParserState): Promise<ParseResult> => {
const {iterator} = state;
const startOffset = iterator.counter.getOffset();

iterator.startReadingBits();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {BufferIterator} from '../../buffer-iterator';
import {maySkipVideoData} from '../../may-skip-video-data/may-skip-video-data';
import type {ParseResult} from '../../parse-result';
import {maySkipVideoData} from '../../state/may-skip-video-data';
import type {ParserState} from '../../state/parser-state';
import {parseFlacFrame} from './parse-flac-frame';
import {parseFlacHeader} from './parse-header';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import type {BufferIterator} from '../../buffer-iterator';
import type {ParserState} from '../../state/parser-state';
import type {IsoBaseMediaBox} from './base-media-box';
import {processBox} from './process-box';

export const getIsoBaseMediaChildren = async ({
iterator,
state,
size,
}: {
iterator: BufferIterator;
state: ParserState;
size: number;
}): Promise<IsoBaseMediaBox[]> => {
const boxes: IsoBaseMediaBox[] = [];
const {iterator} = state;
const initial = iterator.counter.getOffset();

while (iterator.counter.getOffset() < size + initial) {
const parsed = await processBox({
iterator,
state,
});
const parsed = await processBox(state);
if (!parsed.box) {
throw new Error('Expected box');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import type {BufferIterator} from '../../../buffer-iterator';
import {convertAudioOrVideoSampleToWebCodecsTimestamps} from '../../../convert-audio-or-video-sample';
import {getHasTracks} from '../../../get-tracks';
import {maySkipVideoData} from '../../../may-skip-video-data/may-skip-video-data';
import type {FlatSample} from '../../../state/iso-base-media/cached-sample-positions';
import {calculateFlatSamples} from '../../../state/iso-base-media/cached-sample-positions';
import {maySkipVideoData} from '../../../state/may-skip-video-data';
import type {ParserState} from '../../../state/parser-state';

export const parseMdatSection = async ({
iterator,
state,
}: {
iterator: BufferIterator;
state: ParserState;
}): Promise<number | null> => {
export const parseMdatSection = async (
state: ParserState,
): Promise<number | null> => {
const videoSection = state.videoSection.getVideoSection();
// don't need mdat at all, can skip
if (maySkipVideoData({state})) {
Expand All @@ -38,6 +33,7 @@ export const parseMdatSection = async ({
}

const flatSamples = state.iso.flatSamples.getSamples() as FlatSample[];
const {iterator} = state;

const samplesWithIndex = flatSamples.find((sample) => {
return sample.samplePosition.offset === iterator.counter.getOffset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {BufferIterator} from '../../../buffer-iterator';
import type {AnySegment} from '../../../parse-result';
import type {ParserState} from '../../../state/parser-state';
import type {BaseBox} from '../base-type';
Expand All @@ -10,18 +9,15 @@ export interface MoovBox extends BaseBox {
}

export const parseMoov = async ({
iterator,
offset,
size,
state,
}: {
iterator: BufferIterator;
offset: number;
size: number;
state: ParserState;
}): Promise<MoovBox> => {
const children = await getIsoBaseMediaChildren({
iterator,
state,
size: size - 8,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
import type {BufferIterator} from '../../buffer-iterator';
import type {IsoBaseMediaStructure, ParseResult} from '../../parse-result';
import type {ParserState} from '../../state/parser-state';
import {parseMdatSection} from './mdat/mdat';
import {processBox} from './process-box';

export const parseIsoBaseMedia = async ({
iterator,
state,
}: {
iterator: BufferIterator;
state: ParserState;
}): Promise<ParseResult> => {
const videoSectionState = state.videoSection.isInVideoSectionState(iterator);
export const parseIsoBaseMedia = async (
state: ParserState,
): Promise<ParseResult> => {
const videoSectionState = state.videoSection.isInVideoSectionState(
state.iterator,
);

if (videoSectionState === 'in-section') {
const skipTo = await parseMdatSection({
iterator,
state,
});
const skipTo = await parseMdatSection(state);

return {
skipTo,
};
}

const result = await processBox({
iterator,
state,
});
const result = await processBox(state);
if (result.box) {
(state.structure.getStructure() as IsoBaseMediaStructure).boxes.push(
result.box,
);
}

const {iterator} = state;

if (
iterator.counter.getOffset() === state.contentLength &&
state.iso.getShouldReturnToVideoSectionAfterEnd()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {BufferIterator} from '../../buffer-iterator';
import {Log} from '../../log';
import {registerTrack} from '../../register-track';
import type {ParserState} from '../../state/parser-state';
Expand Down Expand Up @@ -32,13 +31,8 @@ import {parseTkhd} from './tkhd';
import {parseTrak} from './trak/trak';
import {parseTrun} from './trun';

export const processBox = async ({
iterator,
state,
}: {
iterator: BufferIterator;
state: ParserState;
}): Promise<BoxAndNext> => {
export const processBox = async (state: ParserState): Promise<BoxAndNext> => {
const {iterator} = state;
const fileOffset = iterator.counter.getOffset();
const {returnToCheckpoint} = iterator.startCheckpoint();
const bytesRemaining = iterator.bytesRemaining();
Expand Down Expand Up @@ -147,7 +141,6 @@ export const processBox = async ({

if (boxType === 'stsd') {
const box = await parseStsd({
iterator,
offset: fileOffset,
size: boxSize,
state,
Expand Down Expand Up @@ -240,7 +233,6 @@ export const processBox = async ({

if (boxType === 'mebx') {
const box = await parseMebx({
iterator,
offset: fileOffset,
size: boxSize,
state,
Expand Down Expand Up @@ -293,7 +285,6 @@ export const processBox = async ({
}

const box = await parseMoov({
iterator,
offset: fileOffset,
size: boxSize,
state,
Expand All @@ -309,7 +300,6 @@ export const processBox = async ({

if (boxType === 'trak') {
const box = await parseTrak({
data: iterator,
size: boxSize,
offsetAtStart: fileOffset,
state,
Expand Down Expand Up @@ -431,7 +421,6 @@ export const processBox = async ({
boxType === 'stsb'
) {
const children = await getIsoBaseMediaChildren({
iterator,
state,
size: boxSize - 8,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {BufferIterator} from '../../../buffer-iterator';
import type {AnySegment} from '../../../parse-result';
import type {ParserState} from '../../../state/parser-state';
import type {BaseBox} from '../base-type';
Expand All @@ -12,23 +11,20 @@ export interface MebxBox extends BaseBox {
}

export const parseMebx = async ({
iterator,
offset,
size,
state,
}: {
iterator: BufferIterator;
offset: number;
size: number;
state: ParserState;
}): Promise<MebxBox> => {
// reserved, 6 bit
iterator.discard(6);
state.iterator.discard(6);

const dataReferenceIndex = iterator.getUint16();
const dataReferenceIndex = state.iterator.getUint16();

const children = await getIsoBaseMediaChildren({
iterator,
state,
size: size - 8,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {BufferIterator} from '../../../buffer-iterator';
import type {AnySegment} from '../../../parse-result';
import type {ParserState} from '../../../state/parser-state';
import type {IsoBaseMediaBox} from '../base-media-box';
Expand Down Expand Up @@ -120,12 +119,11 @@ const audioTags = [
];

export const processIsoFormatBox = async ({
iterator,
state,
}: {
iterator: BufferIterator;
state: ParserState;
}): Promise<FormatBoxAndNext> => {
const {iterator} = state;
const fileOffset = iterator.counter.getOffset();
const bytesRemaining = iterator.bytesRemaining();
const boxSize = iterator.getUint32();
Expand Down Expand Up @@ -174,7 +172,6 @@ export const processIsoFormatBox = async ({
const sampleRate = iterator.getFixedPointUnsigned1616Number();

const children = await getIsoBaseMediaChildren({
iterator,
state,
size: boxSize - (iterator.counter.getOffset() - fileOffset),
});
Expand Down Expand Up @@ -217,7 +214,6 @@ export const processIsoFormatBox = async ({
const bytesPerSample = iterator.getUint32();

const children = await getIsoBaseMediaChildren({
iterator,
state,
size: boxSize - (iterator.counter.getOffset() - fileOffset),
});
Expand Down Expand Up @@ -263,7 +259,6 @@ export const processIsoFormatBox = async ({
const samplesPerPacket = iterator.getUint32();

const children = await getIsoBaseMediaChildren({
iterator,
state,
size: boxSize - (iterator.counter.getOffset() - fileOffset),
});
Expand Down Expand Up @@ -317,7 +312,6 @@ export const processIsoFormatBox = async ({
const children: IsoBaseMediaBox[] =
bytesRemainingInBox > 8
? await getIsoBaseMediaChildren({
iterator,
state,
size: bytesRemainingInBox,
})
Expand Down Expand Up @@ -353,14 +347,13 @@ export const processIsoFormatBox = async ({
};

export const parseIsoFormatBoxes = async ({
iterator,
maxBytes,
state,
}: {
iterator: BufferIterator;
maxBytes: number;
state: ParserState;
}): Promise<Sample[]> => {
const {iterator} = state;
const samples: Sample[] = [];
const initialOffset = iterator.counter.getOffset();

Expand All @@ -369,7 +362,6 @@ export const parseIsoFormatBoxes = async ({
iterator.counter.getOffset() - initialOffset < maxBytes
) {
const {sample} = await processIsoFormatBox({
iterator,
state,
});

Expand Down
Loading
Loading