-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMovieWriter.cpp
1524 lines (1355 loc) · 36.3 KB
/
MovieWriter.cpp
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MovieWriter.cpp: implementation of basic file structure classes.
//
//
// Geraint Davies, May 2004
//
// Copyright (c) GDCL 2004-6. All Rights Reserved.
// You are free to re-use this as the basis for your own filter development,
// provided you retain this copyright notice in the source.
// http://www.gdcl.co.uk
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MovieWriter.h"
#include "TypeHandler.h"
Atom::Atom(AtomWriter* pContainer, LONGLONG llOffset, DWORD type)
: m_pContainer(pContainer),
m_cBytes(0),
m_llOffset(llOffset),
m_bClosed(false)
{
// write the initial length and type dwords
BYTE b[8];
WriteLong(8, b);
WriteLong(type, b+4);
Append(b, 8);
}
HRESULT
Atom::Close()
{
m_bClosed = true;
// we only support 32-bit lengths for atoms
// (otherwise you would have to either decide in the constructor
// or shift the whole atom down).
if (m_cBytes > 0xffffffff)
{
return E_INVALIDARG;
}
BYTE b[4];
WriteLong(long(m_cBytes), b);
return Replace(0, b, 4);
}
Atom*
Atom::CreateAtom(DWORD type)
{
return new Atom(this, Length(), type);
}
// --------------------------------------------------------------------
MovieWriter::MovieWriter(AtomWriter* pContainer)
: m_pContainer(pContainer),
m_bStopped(false),
m_bFTYPInserted(false)
{
}
TrackWriter*
MovieWriter::MakeTrack(const CMediaType* pmt)
{
TypeHandler* ph = TypeHandler::Make(pmt);
if (!ph)
{
return NULL;
}
TrackWriter* pTrack = new TrackWriter(this, (long)m_Tracks.size(), ph);
m_Tracks.push_back(pTrack);
return pTrack;
}
HRESULT
MovieWriter::Close(REFERENCE_TIME* pDuration)
{
// get longest duration of all tracks
// also get earliest sample
vector<TrackWriterPtr>::iterator it;
REFERENCE_TIME tEarliest = -1;
REFERENCE_TIME tThis;
for (it = m_Tracks.begin(); it != m_Tracks.end(); it++)
{
TrackWriter* pTrack = *it;
tThis = pTrack->Earliest();
if (tThis != -1)
{
if ((tEarliest == -1) || (tThis < tEarliest))
{
tEarliest = tThis;
}
}
}
// adjust track start times so that the earliest track starts at 0
REFERENCE_TIME tDur = 0;
REFERENCE_TIME tAdj = -tEarliest;
for (it = m_Tracks.begin(); it != m_Tracks.end(); it++)
{
TrackWriter* pTrack = *it;
pTrack->AdjustStart(tAdj);
tThis = pTrack->Duration();
if (tThis > tDur)
{
tDur = tThis;
}
}
*pDuration = tDur;
LONGLONG tScaledDur = tDur * MovieScale() / UNITS;
// finish writing mdat
if (m_patmMDAT)
{
m_patmMDAT->Close();
m_patmMDAT = NULL;
}
// create moov atom
HRESULT hr = S_OK;
smart_ptr<Atom> pmoov = new Atom(m_pContainer, m_pContainer->Length(), DWORD('moov'));
// movie header
// we are using 90khz as the movie timescale, so
// we may need 64-bits.
smart_ptr<Atom> pmvhd = pmoov->CreateAtom('mvhd');
BYTE b[28*4];
ZeroMemory(b, (28*4));
int cHdr;
if (tScaledDur > 0x7fffffff)
{
b[0] = 1; // version 1
// create time 64bit
// modify time 64bit
// timescale 32-bit
// duration 64-bit
WriteLong(MovieScale(), b + (5*4));
WriteI64(tScaledDur, b + (6 * 4));
cHdr = 8 * 4;
}
else
{
long lDur = long(tScaledDur);
WriteLong(MovieScale(), b + (3 * 4));
WriteLong(lDur, b + (4 * 4));
cHdr = 5 * 4;
}
b[cHdr + 1] = 0x01;
b[cHdr + 4] = 0x01;
b[cHdr + 17] = 0x01;
b[cHdr + 33] = 0x01;
b[cHdr + 48] = 0x40;
WriteLong((long)m_Tracks.size() + 1, b + cHdr + 76); // one-based next-track-id
pmvhd->Append(b, cHdr + 80);
pmvhd->Close();
MakeIODS(pmoov);
for (it = m_Tracks.begin(); it != m_Tracks.end(); it++)
{
TrackWriter* pTrack = *it;
hr = pTrack->Close(pmoov);
if (FAILED(hr))
{
break;
}
}
pmoov->Close();
return hr;
}
void
MovieWriter::Stop()
{
CAutoLock lock(&m_csWrite);
m_bStopped = true;
}
void
MovieWriter::InsertFTYP(AtomWriter* pFile)
{
if (!m_bFTYPInserted)
{
smart_ptr<Atom> pFTYP = new Atom(pFile, pFile->Length(), DWORD('ftyp'));
// file type
BYTE b[4];
WriteLong(DWORD('mp42'), b);
pFTYP->Append(b, 4);
// minor version
WriteLong(0, b);
pFTYP->Append(b, 4);
// additional compatible specs
WriteLong(DWORD('mp42'), b);
pFTYP->Append(b, 4);
WriteLong(DWORD('isom'), b);
pFTYP->Append(b, 4);
pFTYP->Close();
m_bFTYPInserted = true;
}
}
bool
MovieWriter::CheckQueues()
{
CAutoLock lock(&m_csWrite);
if (m_bStopped)
{
return false;
}
// threading notes: we don't lock the
// individual track queues except during the
// actual access functions. The tracks are free to
// add data to the end of the queue. The head of the queue
// will not be removed except during Stop and by us. The
// m_bStopped flag ensures that we are not running when the
// tracks enter Stop.
// we need to return true if the whole set is at EOS
// and all queues emptied
bool bAllFinished;
for(;;)
{
bAllFinished = true; // ... until proven otherwise
// scan tracks to find which if any should write a chunk
bool bSomeNotReady = false;
bool bSomeAtEOS = false;
LONGLONG tEarliestNotReady = -1;
LONGLONG tEarliestReady = -1;
int indexReady = -1;
for (UINT i = 0; i < m_Tracks.size(); i++)
{
LONGLONG tHead;
if (!m_Tracks[i]->GetHeadTime(&tHead))
{
// no chunk ready -- ok if finished
if (!m_Tracks[i]->IsAtEOS())
{
bAllFinished = false;
// note last write time
bSomeNotReady = true;
LONGLONG tWritten = m_Tracks[i]->LastWrite();
if ((tEarliestNotReady == -1) || (tWritten < tEarliestNotReady))
{
// remember the time of the track that is furthest
// behind
tEarliestNotReady = tWritten;
}
} else {
bSomeAtEOS = true;
}
} else {
if (m_Tracks[i]->IsAtEOS())
{
bSomeAtEOS = true;
}
bAllFinished = false; // queue not empty -> not finished
// remember the earliest of the ready blocks
if ((tEarliestReady == -1) || (tHead < tEarliestReady))
{
tEarliestReady = tHead;
indexReady = i;
}
}
}
// is there anything to write
if (indexReady < 0)
{
break;
}
// mustn't get too far ahead of any blocked tracks (unless we have reached EOS)
if (!bSomeAtEOS && bSomeNotReady && ((tEarliestReady - tEarliestNotReady) > UNITS))
{
// wait for more data on earliest-not-ready track
break;
}
WriteTrack(indexReady);
}
return bAllFinished;
}
void
MovieWriter::WriteTrack(int indexReady)
{
// make sure we have space in an mdat atom
// -- make a new atom every 1Gb
if ((m_patmMDAT) && (m_patmMDAT->Length() >= 1024*1024*1024))
{
m_patmMDAT->Close();
m_patmMDAT = NULL;
}
if (m_patmMDAT == NULL)
{
if (!m_bFTYPInserted)
{
InsertFTYP(m_pContainer);
}
m_patmMDAT = new Atom(m_pContainer, m_pContainer->Length(), DWORD('mdat'));
}
// write earliest block
m_Tracks[indexReady]->WriteHead(m_patmMDAT);
}
void
MovieWriter::WriteOnStop()
{
CAutoLock lock(&m_csWrite);
ASSERT(m_bStopped);
// loop writing as long as there are blocks queued at the pins
for (;;)
{
LONGLONG tReady = 0;
int idxReady = -1;
// find the earliest
for (UINT i = 0; i < m_Tracks.size(); i++)
{
LONGLONG tHead;
if (m_Tracks[i]->GetHeadTime(&tHead))
{
if ((idxReady == -1) ||
(tHead < tReady))
{
idxReady = i;
tReady = tHead;
}
}
}
if (idxReady == -1)
{
// all done
return;
}
WriteTrack(idxReady);
}
}
REFERENCE_TIME
MovieWriter::CurrentPosition()
{
CAutoLock lock(&m_csWrite);
LONGLONG tEarliest = -1;
for (UINT i = 0; i < m_Tracks.size(); i++)
{
LONGLONG tWritten = m_Tracks[i]->LastWrite();
if ((tEarliest < 0) || (tWritten < tEarliest))
{
tEarliest = tWritten;
}
}
return tEarliest;
}
void
MovieWriter::MakeIODS(Atom* pmoov)
{
smart_ptr<Atom> piods = pmoov->CreateAtom('iods');
Descriptor iod(Descriptor::MP4_IOD);
BYTE b[16];
WriteShort(0x004f, b); // object id 1, no url, no inline profile + reserved bits
b[2] = 0xff; // no od capability required
b[3] = 0xff; // no scene graph capability required
b[4] = 0x0f; // audio profile
b[5] = 0x03; // video profile
b[6] = 0xff; // no graphics capability required
iod.Append(b, 7);
// append the id of each media track
for (UINT i = 0; i < m_Tracks.size(); i++)
{
if (m_Tracks[i]->IsVideo() || m_Tracks[i]->IsAudio())
{
// use 32-bit track id in IODS
Descriptor es(Descriptor::ES_ID_Inc);
WriteLong(m_Tracks[i]->ID(), b);
es.Append(b, 4);
iod.Append(&es);
}
}
WriteLong(0, b);
piods->Append(b, 4); // ver/flags
iod.Write(piods);
piods->Close();
}
// -------- Track -------------------------------------------------------
TrackWriter::TrackWriter(MovieWriter* pMovie, int index, TypeHandler* pType)
: m_bEOS(false),
m_bStopped(false),
m_index(index),
m_pType(pType),
m_tLast(0),
m_StartAt(0),
m_pMovie(pMovie),
m_Durations(90000), // scale: 90KHz
m_SC(1) // dataref 1
{
// adjust scale to media type (mostly because audio scales must be 16 bits);
m_Durations.SetScale(pType->Scale());
m_Durations.SetFrameDuration(m_pType->FrameDuration());
}
HRESULT
TrackWriter::Add(IMediaSample* pSample)
{
HRESULT hr = S_OK;
{
// restrict scope of cs so we don't hold it
// during the CheckQueues call
CAutoLock lock(&m_csQueue);
if (m_bEOS || m_bStopped)
{
hr = VFW_E_WRONG_STATE;
} else {
if (m_pCurrent == NULL)
{
m_pCurrent = new MediaChunk(this);
}
m_pCurrent->AddSample(pSample);
if (m_pCurrent->IsFull())
{
m_Queue.push_back(m_pCurrent);
m_pCurrent = NULL;
}
}
}
m_pMovie->CheckQueues();
return hr;
}
// returns true if all tracks now at end
bool
TrackWriter::OnEOS()
{
{
CAutoLock lock(&m_csQueue);
m_bEOS = true;
// queue final partial chunk
if (m_pCurrent && (m_pCurrent->Samples() > 0))
{
m_Queue.push_back(m_pCurrent);
m_pCurrent = NULL;
}
}
return m_pMovie->CheckQueues();
}
// no more writes accepted -- partial/queued writes abandoned
void
TrackWriter::Stop(bool bFlush)
{
CAutoLock lock(&m_csQueue);
// prevent further writes
m_bStopped = true;
if (bFlush)
{
// discard queued but unwritten samples
m_pCurrent = NULL;
m_Queue.clear();
}
else
{
// queue current partial block
if (m_pCurrent && (m_pCurrent->Samples() > 0))
{
m_Queue.push_back(m_pCurrent);
m_pCurrent = NULL;
}
}
}
bool
TrackWriter::GetHeadTime(LONGLONG* ptHead)
{
CAutoLock lock(&m_csQueue);
if (m_Queue.size() == 0)
{
return false;
}
MediaChunkPtr pChunk = *m_Queue.begin();
REFERENCE_TIME tLast;
pChunk->GetTime(ptHead, &tLast);
return true;
}
HRESULT
TrackWriter::WriteHead(Atom* patm)
{
CAutoLock lock(&m_csQueue);
if (m_Queue.size() == 0)
{
return E_FAIL;
}
MediaChunkPtr pChunk = *m_Queue.begin();
m_Queue.pop_front();
REFERENCE_TIME tStart, tEnd;
pChunk->GetTime(&tStart, &tEnd);
// the chunk will call back to us to index
// the samples during this call, once
// the media data is successfully written
HRESULT hr = pChunk->Write(patm);
if (SUCCEEDED(hr))
{
m_tLast = tEnd;
}
return hr;
}
REFERENCE_TIME
TrackWriter::LastWrite()
{
CAutoLock lock(&m_csQueue);
return m_tLast;
}
void
TrackWriter::IndexChunk(LONGLONG posChunk, long nSamples)
{
m_SC.Add(nSamples);
m_CO.Add(posChunk);
}
void
TrackWriter::IndexSample(bool bSync, REFERENCE_TIME tStart, REFERENCE_TIME tStop, long cBytes)
{
// CTS offset means ES type-specific content parser?
// -- this is done now by calculation from the frames start time (heuristically!)
m_Sizes.Add(cBytes);
m_Durations.Add(tStart, tStop);
m_Syncs.Add(bSync);
}
HRESULT
TrackWriter::Close(Atom* patm)
{
smart_ptr<Atom> ptrak = patm->CreateAtom('trak');
// track header tkhd
smart_ptr<Atom> ptkhd = ptrak->CreateAtom('tkhd');
BYTE b[24*4];
ZeroMemory(b, (24*4));
// duration in movie timescale
LONGLONG scaledur = long(Duration() * m_pMovie->MovieScale() / UNITS);
int cHdr = 6 * 4;
if (scaledur > 0x7fffffff)
{
// use 64-bit version (64-bit create/modify and duration
cHdr = 9*4;
b[0] = 1;
WriteLong(ID(), b+(5*4));
WriteI64(scaledur, b+(7*4));
}
else
{
WriteLong(ID(), b+(3*4)); // 1-base track id
WriteLong(long(scaledur), b+(5*4));
cHdr = 6*4;
}
b[3] = 7; // enabled, in movie and in preview
if (IsAudio())
{
b[cHdr + 12] = 0x01;
}
b[cHdr + 17] = 1;
b[cHdr + 33] = 1;
b[cHdr + 48] = 0x40;
if (IsVideo())
{
WriteShort(m_pType->Width(), &b[cHdr + 52]);
WriteShort(m_pType->Height(), &b[cHdr + 56]);
}
ptkhd->Append(b, cHdr + 60);
ptkhd->Close();
// track ref tref
Handler()->WriteTREF(ptrak);
// edts -- used for first-sample offet
// -- note, this is in movie timescale, not track
m_Durations.WriteEDTS(ptrak, m_pMovie->MovieScale());
smart_ptr<Atom> pmdia = ptrak->CreateAtom('mdia');
// Media Header mdhd
smart_ptr<Atom> pmdhd = pmdia->CreateAtom('mdhd');
ZeroMemory(b, 9*4);
// duration now in track timescale
scaledur = m_Durations.Duration() * m_Durations.Scale() / UNITS;
if (scaledur > 0x7fffffff)
{
b[0] = 1; // 64-bit
WriteLong(m_Durations.Scale(), b+20);
WriteI64(scaledur, b+24);
cHdr = 8*4;
}
else
{
WriteLong(m_Durations.Scale(), b+12);
WriteLong(long(scaledur), b+16);
cHdr = 5*4;
}
// 'eng' as offset from 0x60 in 0 pad bit plus 3x5-bit (05 0xe 07)
b[cHdr] = 0x15;
b[cHdr+1] = 0xc7;
pmdhd->Append(b, cHdr + 4);
pmdhd->Close();
// handler id hdlr
smart_ptr<Atom> phdlr = pmdia->CreateAtom('hdlr');
ZeroMemory(b, 25);
WriteLong(Handler()->Handler(), b+8);
phdlr->Append(b, 25);
phdlr->Close();
smart_ptr<Atom> pminf = pmdia->CreateAtom('minf');
// media information header vmhd/smhd
ZeroMemory(b, sizeof(b));
if (IsVideo())
{
smart_ptr<Atom> pvmhd = pminf->CreateAtom('vmhd');
b[3] = 1;
pvmhd->Append(b, 12);
pvmhd->Close();
} else if (IsAudio())
{
smart_ptr<Atom> psmhd = pminf->CreateAtom('smhd');
psmhd->Append(b, 8);
psmhd->Close();
} else {
smart_ptr<Atom> pnmhd = pminf->CreateAtom('nmhd');
pnmhd->Append(b, 4);
pnmhd->Close();
}
// dinf/dref -- data reference
smart_ptr<Atom> pdinf = pminf->CreateAtom('dinf');
smart_ptr<Atom> pdref = pdinf->CreateAtom('dref');
WriteLong(0, b); // ver/flags
WriteLong(1, b+4); // entries
pdref->Append(b, 8);
smart_ptr<Atom> purl = pdref->CreateAtom('url ');
// self-contained flag set, and no string required
// -- all data is in this file
b[3] = 1;
purl->Append(b, 4);
purl->Close();
pdref->Close();
pdinf->Close();
smart_ptr<Atom> pstbl = pminf->CreateAtom('stbl');
// Sample description
// -- contains one descriptor atom mp4v/mp4a/... for each data reference.
smart_ptr<Atom> pstsd = pstbl->CreateAtom('stsd');
WriteLong(0, b); // ver/flags
WriteLong(1, b+4); // count of entries
pstsd->Append(b, 8);
Handler()->WriteDescriptor(pstsd, ID(), 1, m_Durations.Scale()); // dataref = 1
pstsd->Close();
HRESULT hr = m_Durations.WriteTable(pstbl);
if (SUCCEEDED(hr))
{
hr = m_Syncs.Write(pstbl);
}
if (SUCCEEDED(hr))
{
hr = m_SC.Write(pstbl);
}
if (SUCCEEDED(hr))
{
hr = m_Sizes.Write(pstbl);
}
if (SUCCEEDED(hr))
{
hr = m_CO.Write(pstbl);
}
pstbl->Close();
pminf->Close();
pmdia->Close();
ptrak->Close();
return hr;
}
// -- Media Chunk ----------------------
MediaChunk::MediaChunk(TrackWriter* pTrack)
: m_cBytes(0),
m_pTrack(pTrack),
m_tStart(0),
m_tEnd(0)
{
m_nSamplesPerChunk = pTrack->SampleRate();
}
MediaChunk::~MediaChunk()
{
// I wanted to use list<IMediaSamplePtr> but the
// compiler could not handle the deep nesting of templates
while(m_Samples.size() > 0)
{
IMediaSample* pSample = m_Samples.front();
pSample->Release();
m_Samples.pop_front();
}
}
HRESULT
MediaChunk::AddSample(IMediaSample* pSample)
{
REFERENCE_TIME tStart, tEnd;
HRESULT hr = pSample->GetTime(&tStart, &tEnd);
if (hr == S_OK)
{
// H264 samples from large frames
// may be broken across several buffers, with the
// time set on the last sample
// record overall time
if (m_cBytes == 0)
{
// first sample
m_tStart = tStart;
m_tEnd = tEnd;
} else {
if (tStart < m_tStart)
{
m_tStart = tStart;
}
if (tEnd > m_tEnd)
{
m_tEnd = tEnd;
}
}
}
m_cBytes += pSample->GetActualDataLength();
pSample->AddRef();
m_Samples.push_back(pSample);
return S_OK;
}
HRESULT
MediaChunk::Write(Atom* patm)
{
// record chunk start position
LONGLONG posChunk = patm->Position() + patm->Length();
// Remember that large H264 samples may be broken
// across several buffers, with Sync flag at start and
// time on last buffer.
bool bSync = false;
long cBytes = 0;
long nSamples = 0;
// loop once through the samples writing the data
list<IMediaSample*>::iterator it;
for (it = m_Samples.begin(); it != m_Samples.end(); it++)
{
IMediaSample* pSample = *it;
// record positive sync flag, but for
// multiple-buffer samples, only one sync flag will be present
// so don't overwrite with later negatives.
if (pSample->IsSyncPoint() == S_OK)
{
bSync = true;
}
// write payload, including any transformation (eg BSF to length-prepended)
BYTE* pBuffer;
pSample->GetPointer(&pBuffer);
int cActual = 0;
m_pTrack->Handler()->WriteData(patm, pBuffer, pSample->GetActualDataLength(), &cActual);
cBytes += cActual;
REFERENCE_TIME tStart, tEnd;
HRESULT hr = pSample->GetTime(&tStart, &tEnd);
if (hr == S_OK)
{
// this is the last buffer in the sample
m_pTrack->IndexSample(bSync, tStart, tEnd, cBytes);
// reset for new sample
bSync = false;
cBytes = 0;
nSamples++;
}
}
// add chunk position to index
m_pTrack->IndexChunk(posChunk, nSamples);
return S_OK;
}
bool
MediaChunk::IsFull()
{
if (m_pTrack->IsAudio())
{
return (m_tEnd - m_tStart) > UNITS;
}
else
{
return Samples() > m_nSamplesPerChunk;
}
}
// ---- index classes --------------------
ListOfLongs::ListOfLongs()
: m_nEntriesInLast(0)
{
m_Blocks.push_back(new BYTE[EntriesPerBlock * 4]);
}
void
ListOfLongs::Append(long l)
{
if (m_nEntriesInLast >= EntriesPerBlock)
{
m_Blocks.push_back(new BYTE[EntriesPerBlock * 4]);
m_nEntriesInLast = 0;
}
BytePtr p = m_Blocks[m_Blocks.size() - 1];
WriteLong(l, p + m_nEntriesInLast*4);
m_nEntriesInLast++;
}
HRESULT
ListOfLongs::Write(Atom* patm)
{
// write all the full blocks
for (UINT i = 0; i < m_Blocks.size() -1; i++)
{
BytePtr p = m_Blocks[i];
HRESULT hr = patm->Append(p, EntriesPerBlock*4);
if (FAILED(hr))
{
return hr;
}
}
// partial last block
if (m_nEntriesInLast > 0)
{
BytePtr p = m_Blocks[m_Blocks.size() - 1];
HRESULT hr = patm->Append(p, m_nEntriesInLast * 4);
if (FAILED(hr))
{
return hr;
}
}
return S_OK;
}
long
ListOfLongs::Entry(long nEntry)
{
// read back a value (for 32 to 64 conversion)
long nValue = 0;
if (nEntry < Entries())
{
BytePtr p = m_Blocks[nEntry/EntriesPerBlock];
nValue = ReadLong(p + (nEntry % EntriesPerBlock)*4);
}
return nValue;
}
ListOfI64::ListOfI64()
: m_nEntriesInLast(0)
{
m_Blocks.push_back(new BYTE[EntriesPerBlock * 8]);
}
void
ListOfI64::Append(LONGLONG ll)
{
if (m_nEntriesInLast >= EntriesPerBlock)
{
m_Blocks.push_back(new BYTE[EntriesPerBlock * 8]);
m_nEntriesInLast = 0;
}
BytePtr p = m_Blocks[m_Blocks.size() - 1];
WriteI64(ll, p + m_nEntriesInLast*8);
m_nEntriesInLast++;
}
HRESULT
ListOfI64::Write(Atom* patm)
{
// write all the full blocks
for (UINT i = 0; i < m_Blocks.size() -1; i++)
{
BytePtr p = m_Blocks[i];
HRESULT hr = patm->Append(p, EntriesPerBlock*8);
if (FAILED(hr))
{
return hr;
}
}
// partial last block
if (m_nEntriesInLast > 0)
{
BytePtr p = m_Blocks[m_Blocks.size() - 1];
HRESULT hr = patm->Append(p, m_nEntriesInLast * 8);
if (FAILED(hr))
{
return hr;
}
}
return S_OK;
}
ListOfPairs::ListOfPairs()
: m_cEntries(0),
m_lCount(0)
{
}
void
ListOfPairs::Append(long val)
{
if (m_lCount == 0)
{
m_lCount = 1;
m_lValue = val;
}
else if (val == m_lValue)
{
m_lCount++;
} else {
m_Table.Append(m_lCount);
m_Table.Append(m_lValue);
m_lCount = 1;
m_lValue = val;
}
m_cEntries++;
}
HRESULT
ListOfPairs::Write(Atom* patm)
{
if (m_lCount > 0)
{
m_Table.Append(m_lCount);
m_Table.Append(m_lValue);
}
// ver/flags == 0
// nEntries
// pairs of <count, value>
BYTE b[8];
ZeroMemory(b, 8);
// entry count is count of pairs
WriteLong(m_Table.Entries() / 2, b+4);
HRESULT hr = patm->Append(b, 8);
if (SUCCEEDED(hr))
{
hr = m_Table.Write(patm);
}
return hr;
}