-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoDecoder.js
More file actions
518 lines (427 loc) · 13.3 KB
/
VideoDecoder.js
File metadata and controls
518 lines (427 loc) · 13.3 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Video decoder
// Mp4.js is for decoding mp4 containers
// H264XXX.js is for decoding h264 frames
// this marrys them both, for a simpler user-centric "video decoder" class
// lots to do to make this simpler (eg. construct from url)
// but clock, data, is all controlled by user
//
// this should be platform (web,native) indepdent.
// so import.meta.url will need to change which is worker-specific stuff
import {Mp4Decoder,Atom_SampleDescriptionExtension_Avcc,Atom_t} from './Mp4.js'
import WebcodecDecoder from './PopH264WebApi.js'
import * as H264 from './H264.js'
import PromiseQueue from './PromiseQueue.js'
import {JoinTypedArrays,ChunkArray,BytesToString} from './PopApi.js'
import {Yield} from './PopWebApiCore.js'
//const UseMp4WebWorker = !isSafari();
const UseMp4WebWorker = false;
const UseChunkArray = true;
function GetWorkerJsUrl()
{
const ModuleUrl = import.meta.url;
const Paths = ModuleUrl.split('/');
Paths.pop();
Paths.push('Mp4DecoderWorker.js');
const Path = Paths.join('/');
return Path;
}
let Mp4DecoderWebWorker;
// give each decoder a "unique id"
let DecoderInstanceCounter = 1000;
class Mp4DecoderWebWorker_t
{
constructor()
{
this.Worker = new Worker( GetWorkerJsUrl() );
this.Worker.onerror = this.OnWorkerError.bind(this);
this.Worker.onmessage = this.OnWorkerMessage.bind(this);
this.Instances = {}; // [id] = Mp4DecoderWebWorkerInstance_t
}
Free()
{
if ( this.Worker )
{
this.Worker.terminate();
this.Worker = null;
}
}
AllocInstance()
{
const InstanceId = DecoderInstanceCounter++;
function PushData(Chunk)
{
this.PushData(Chunk,InstanceId);
}
const Instance = new Mp4DecoderWebWorkerInstance_t(PushData.bind(this));
this.Instances[InstanceId] = Instance;
return Instance;
}
OnWorkerMessage(Event)
{
try
{
if ( Event.data && Event.data.Error )
throw Event.data.Error;
const Message = Event.data;
if ( !Message.Instance )
throw `Message from web worker missing .Instance`;
const Instance = this.Instances[Message.Instance];
if ( !Instance )
throw `Message from web worker for non existant instance`;
Instance.OutputQueue.Push(Message.Data);
}
catch(e)
{
const Error = {};
Error.Message = e;
return this.OnWorkerError(Error);
}
}
OnWorkerError(Event)
{
let Location = Event.filename ? `${Event.filename}(${Event.lineno})` : GetWorkerJsUrl();
let Message = Event.message || '(no message)';
const Error = `${Location}: ${Message}`;
console.log(`Error from webworker`,Error);
this.OutputQueue.Reject(Error);
}
PushData(Chunk,Instance)
{
const Message = {};
Message.Data = Chunk;
Message.Instance = Instance;
this.Worker.postMessage(Message);
}
PushEndOfFile(Instance)
{
this.PushData(null,Instance);
}
}
function isSafari()
{
return (navigator.vendor.match(/apple/i) || "").length > 0
}
function AllocMp4DecoderInstance()
{
// our current setup doesn't work in safari (import()/use of modules inside a webworker)
// fall back
if ( !UseMp4WebWorker )
{
return new Mp4Decoder();
}
if ( !Mp4DecoderWebWorker )
Mp4DecoderWebWorker = new Mp4DecoderWebWorker_t();
return Mp4DecoderWebWorker.AllocInstance();
}
class Mp4DecoderWebWorkerInstance_t
{
constructor(PushData)
{
this.PushData = PushData;
this.OutputQueue = new PromiseQueue(`Mp4 worker output`);
}
async WaitForNextSamples()
{
return this.OutputQueue.WaitForNext();
}
PushEndOfFile()
{
this.PushData(null);
}
Free()
{
// put out a no-more-samples message for the h264 thread
this.OutputQueue.Reject('Freed');
}
}
export class VideoDecoder
{
constructor(DebugName=`Video`,OnMp4Decoded,OnMp4SampleExtracted,OnFrameFreed,WaitForAllowedStart)
{
// external control to stop webcodec/worker alloc
this.WaitForAllowedStart = async function(){};
// gone through all blocks
this.OnMp4Decoded = OnMp4Decoded || function(){};
this.OnFrameFreed = OnFrameFreed;
// sample meta has been extracted AND the data from MDAT has been extracted
this.OnMp4SampleExtracted = OnMp4SampleExtracted || function(){};
// make mp4 decoding thread
//this.Mp4Decoder = new Mp4Decoder();
this.Mp4Decoder = AllocMp4DecoderInstance();
// dictionary for h264time <-> sample/frame meta
this.FrameMetas = {}; // [FrameIndex] = Meta
// holding on to file contents for when we need to fetch it.
// Could remove this and build it into mp4 decoder to lookup mdats and take its data
this.Mp4FileContent = UseChunkArray ? new ChunkArray() : new Uint8Array(0);
this.Mp4HadEof = false;
this.Mp4FileChunkQueue = new PromiseQueue(`Mp4 Input data ${DebugName}`);
this.Samples = [];
this.VideoMetaChangedQueue = new PromiseQueue(`Video meta changes ${DebugName}`);
// make h264 decoding thread
this.H264Decoders = {}; // [Track] = Decoder
this.SubtitleDecoders = {};
this.OutputFrameQueue = new PromiseQueue(`Video Output Queue ${DebugName}`);
// make h264 consumer thread
this.Mp4DecoderThreadPromise = this.Mp4DecoderThread();
this.Mp4DecoderThreadPromise.catch(this.OnMp4ThreadError.bind(this));
}
GetSubtitleDecoder(Track)
{
if ( !Number.isInteger(Track) )
throw `Invalid track id for GetSubtitleDecoder(Track=${Track})`;
if ( !this.SubtitleDecoders.hasOwnProperty(Track) )
{
const Decoder = {};
Decoder.FrameCount = 0;
Decoder.Track = Track;
this.SubtitleDecoders[Track] = Decoder;
}
return this.SubtitleDecoders[Track];
}
GetH264Decoder(Track)
{
if ( !Number.isInteger(Track) )
throw `Invalid track id for GetH264Decoder(Track=${Track})`;
if ( !this.H264Decoders.hasOwnProperty(Track) )
{
const Decoder = {};
Decoder.Decoder = new WebcodecDecoder( this.OnFrameFreed );
Decoder.FrameCount = 0;
Decoder.Track = Track;
this.H264Decoders[Track] = Decoder;
Decoder.OutputThreadPromise = this.H264OutputThread(Decoder.Decoder,Track);
Decoder.OutputThreadPromise.catch(this.OnH264ThreadError.bind(this));
}
return this.H264Decoders[Track];
}
OnMp4ThreadError(Error)
{
this.OutputFrameQueue.Reject(Error);
}
OnH264ThreadError(Error)
{
this.OutputFrameQueue.Reject(Error);
}
async WaitForVideoMetaChange()
{
return this.VideoMetaChangedQueue.WaitForLatest();
}
async WaitForNextFrame()
{
return this.OutputFrameQueue.WaitForNext();
}
PushEndOfFile()
{
this.Mp4HadEof = true;
if ( this.Mp4Decoder )
{
this.Mp4Decoder.PushEndOfFile();
}
else
{
// mp4 decoder already free... ignore?
}
// notify anything waiting for more mp4 data that we got EOF
this.Mp4FileChunkQueue.Push(null);
}
PushData(Chunk)
{
if ( !this.Mp4Decoder )
{
// gr: doing this is heavy, lets just silently lose the data...
//throw `VideoDecoder.PushData() Mp4 decoder has been freed (or possible never allocated)`;
return;
}
this.Mp4Decoder.PushData(Chunk);
// if the data going in is a pre-decoded atom (ie, MOOV we've extracted from the tail)
// then it's not normal file data, so ignore it for below
if ( Chunk instanceof Atom_t )
return;
// storing data for sample(mdat) lookup later
// gr: Joining typed arrays is expensive, so instead we'll leave chunks and
// iterate as we need
if ( this.Mp4FileContent instanceof Uint8Array )
{
this.Mp4FileContent = JoinTypedArrays( [this.Mp4FileContent, Chunk] );
}
else
{
this.Mp4FileContent.push(Chunk);
}
// notify anything waiting for more mp4 data
this.Mp4FileChunkQueue.Push(Chunk);
}
async WaitForNextMp4FileData()
{
return await this.Mp4FileChunkQueue.WaitForNext();
}
async WaitForMp4FileDataChanged()
{
return await this.Mp4FileChunkQueue.WaitForLatest();
}
async GetSampleData(Sample)
{
const Start = Sample.DataFilePosition;
const Length = Sample.DataSize
const End = Start + Length;
// if data hasn't arrived yet, wait for some new data to arrive
// todo: may need to check here if we've got EOF and will never get
while ( !this.Mp4HadEof && this.Mp4FileContent.length < End )
{
console.log(`Waiting for MP4 chunk [${Start}...${End}]/${this.Mp4FileContent.length}...`);
// dont need to check each change, and we can assume last will be null
const NextData = await this.WaitForMp4FileDataChanged();
if ( !NextData )
{
console.log(`Waiting for MP4 chunk [${Start}...${End}]/${this.Mp4FileContent.length}... GOT EOF`);
break;
}
}
if ( this.Mp4FileContent.length < End )
throw `Wait for MP4 sample data, but out of range after EOF`;
const Data = this.Mp4FileContent.slice( Start, End );
return Data;
}
// this is our hack to use frame indexes
// wrapped in functions to highlight where the hack is
FrameMetaToH264Time(Sample,FrameIndex)
{
//this.FrameMetas[FrameIndex] = Sample;
//this.FrameMetas[FrameIndex].FrameIndex = FrameIndex;
return FrameIndex;
}
H264TimeToFrameIndex(PresentationTime)
{
const FrameIndex = PresentationTime;
return FrameIndex;
}
GetVideoMeta()
{
const Meta = {};
Meta.Samples = this.Samples.slice();
return Meta;
}
OnNewSamples(Samples)
{
this.Samples.push( ...Samples );
const Meta = this.GetVideoMeta();
this.VideoMetaChangedQueue.Push( Meta );
}
async Mp4DecoderThread()
{
// todo: we need to graciously handle OOP
// we need to fetch all the samples first, sort by decode order...
// i think thats always the order in mp4 though...
// gr: on web, with multiple tracks, we get all the samples, push them to decoder
// the decoder then outputs a burst of packets
// THEN we get samples for next track. I think they're in seperate atoms, but
// there's a noticable delay AFTER consuming new frames that track2 samples appear
// see if we can only process X samples at a time, but try and interleave samples so
// tracks can be played in sync
// Adding a pause for requestAnimationFrame/Pop.WaitForFrame() helps spread this out via h264 decder
while ( true )
{
const NextSamples = await this.Mp4Decoder.WaitForNextSamples();
// eof
if ( !NextSamples )
break;
//console.log(`got ${NextSamples.length} new samples (est track =${NextSamples[0].TrackId})`);
this.OnNewSamples(NextSamples);
// see if we ever get samples before we finish download
if ( false )
{
const FileSizeMb = this.Mp4FileContent.length / 1024 / 1024;
console.log(`Got samples when mp4 file size is ${FileSizeMb.toFixed(2)}`);
}
for ( let Sample of NextSamples )
{
// gr: this depends on mp4 mdat existing...
// this could be async and wait for mdat data to arrive
// todo: switch anyway and make sure we're not passing data that doesnt exist to decoder
//console.log(`Sample`,Sample);
// get data from original input data
let Data = Sample.Data;
if ( !Data )
Data = await this.GetSampleData(Sample);
if ( Sample.ContentType == 'H264' )
{
const H264Decoder = this.GetH264Decoder(Sample.TrackId);
const FrameIndex = H264Decoder.FrameCount;
// hack: I believe the mp4 decoder is decoding time wrong,
// but more importantly in order to sync, we're currently
// syncing by frame number
// pass the reliable frame index as a time, and get it out again
// we use a dumb function to explicitly highlight where the hack is used
// we should store meta
const PresentationTimeMs = this.FrameMetaToH264Time(Sample,FrameIndex);
this.OnMp4SampleExtracted(FrameIndex);
// decoder should figure this out
const IsKeyframe = Sample.IsKeyframe;
if ( !H264Decoder.Decoder.PushData( Data, PresentationTimeMs, IsKeyframe ) )
{
// gr: not
//throw `Error pushing sample to H264 decoder`;
}
// I thought maybe we're flooding the webcodec decoder a bit, but doesnt seem to make much difference
await Yield(0);
// increase frame index if not sps/pps
if ( !Sample.Data )
H264Decoder.FrameCount++;
}
else if ( Sample.ContentType == 'Subtitle' )
{
const SubtitleDecoder = this.GetSubtitleDecoder(Sample.TrackId);
// gr: first byte always 0?
// gr: second byte rarely an ascii
const Text = BytesToString(Data.slice(2));
const Frame = {};
Frame.Track = Sample.TrackId;
Frame.Data = `${Text}`;
Frame.FrameIndex = SubtitleDecoder.FrameCount;
this.OutputFrameQueue.Push(Frame);
SubtitleDecoder.FrameCount++;
}
else
{
console.log(`Unhandled sample type ${Sample.ContentType}`);
}
}
}
this.H264Decoder.PushEndOfFile();
//console.log(`Mp4(not h264) decoder thread finished`);
}
async H264OutputThread(Decoder,Track)
{
while ( true )
{
const FrameImage = await Decoder.WaitForNextFrame();
// eof
if ( !FrameImage )
break;
//console.log(`Got frame ${Track}/${FrameImage.timestamp} queue: ${this.OutputFrameQueue.GetQueueSize()}`);
const Frame = {};
Frame.Data = FrameImage;
Frame.FrameIndex = this.H264TimeToFrameIndex(FrameImage.timestamp);
Frame.Track = Track;
// Client is expected to call .Free() when they're done with this frame
this.OutputFrameQueue.Push(Frame);
}
// push an EOF frame
this.OutputFrameQueue.Push(null);
Decoder.Free();
}
Free()
{
if ( this.Mp4Decoder )
{
if ( this.Mp4Decoder.Free )
this.Mp4Decoder.Free();
this.Mp4Decoder = null;
}
if ( this.H264Decoder )
{
this.H264Decoder.Free();
this.H264Decoder = null;
}
}
}