-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathmp4property.cpp
1275 lines (1089 loc) · 35.2 KB
/
mp4property.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
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
*
* Contributor(s):
* Dave Mackie [email protected]
* Kona Blend kona8lend@@gmail.com
*/
#include "src/impl.h"
namespace mp4v2 { namespace impl {
///////////////////////////////////////////////////////////////////////////////
MP4Property::MP4Property(MP4Atom& parentAtom, const char* name)
: m_parentAtom(parentAtom)
{
m_name = name;
m_readOnly = false;
m_implicit = false;
}
bool MP4Property::FindProperty(const char* name,
MP4Property** ppProperty, uint32_t* pIndex)
{
if (name == NULL) {
return false;
}
if (!strcasecmp(m_name, name)) {
log.verbose1f("\"%s\": FindProperty: matched %s",
m_parentAtom.GetFile().GetFilename().c_str(), name);
*ppProperty = this;
return true;
}
return false;
}
// Integer Property
uint64_t MP4IntegerProperty::GetValue(uint32_t index)
{
switch (this->GetType()) {
case Integer8Property:
return ((MP4Integer8Property*)this)->GetValue(index);
case Integer16Property:
return ((MP4Integer16Property*)this)->GetValue(index);
case Integer24Property:
return ((MP4Integer24Property*)this)->GetValue(index);
case Integer32Property:
return ((MP4Integer32Property*)this)->GetValue(index);
case Integer64Property:
return ((MP4Integer64Property*)this)->GetValue(index);
default:
ASSERT(false);
}
return (0);
}
void MP4IntegerProperty::SetValue(uint64_t value, uint32_t index)
{
switch (this->GetType()) {
case Integer8Property:
((MP4Integer8Property*)this)->SetValue(value, index);
break;
case Integer16Property:
((MP4Integer16Property*)this)->SetValue(value, index);
break;
case Integer24Property:
((MP4Integer24Property*)this)->SetValue(value, index);
break;
case Integer32Property:
((MP4Integer32Property*)this)->SetValue(value, index);
break;
case Integer64Property:
((MP4Integer64Property*)this)->SetValue(value, index);
break;
default:
ASSERT(false);
}
}
void MP4IntegerProperty::InsertValue(uint64_t value, uint32_t index)
{
switch (this->GetType()) {
case Integer8Property:
((MP4Integer8Property*)this)->InsertValue(value, index);
break;
case Integer16Property:
((MP4Integer16Property*)this)->InsertValue(value, index);
break;
case Integer24Property:
((MP4Integer24Property*)this)->InsertValue(value, index);
break;
case Integer32Property:
((MP4Integer32Property*)this)->InsertValue(value, index);
break;
case Integer64Property:
((MP4Integer64Property*)this)->InsertValue(value, index);
break;
default:
ASSERT(false);
}
}
void MP4IntegerProperty::DeleteValue(uint32_t index)
{
switch (this->GetType()) {
case Integer8Property:
((MP4Integer8Property*)this)->DeleteValue(index);
break;
case Integer16Property:
((MP4Integer16Property*)this)->DeleteValue(index);
break;
case Integer24Property:
((MP4Integer24Property*)this)->DeleteValue(index);
break;
case Integer32Property:
((MP4Integer32Property*)this)->DeleteValue(index);
break;
case Integer64Property:
((MP4Integer64Property*)this)->DeleteValue(index);
break;
default:
ASSERT(false);
}
}
void MP4IntegerProperty::IncrementValue(int32_t increment, uint32_t index)
{
SetValue(GetValue() + increment);
}
void MP4Integer8Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %u (0x%02x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %u (0x%02x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], m_values[index]);
}
void MP4Integer16Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %u (0x%04x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %u (0x%04x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], m_values[index]);
}
void MP4Integer24Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %u (0x%06x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %u (0x%06x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], m_values[index]);
}
void MP4Integer32Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %u (0x%08x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %u (0x%08x)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], m_values[index]);
}
void MP4Integer64Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %" PRIu64 " (0x%016" PRIx64 ")",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %" PRIu64 " (0x%016" PRIx64 ")",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], m_values[index]);
}
// MP4BitfieldProperty
void MP4BitfieldProperty::Read(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
m_values[index] = file.ReadBits(m_numBits);
}
void MP4BitfieldProperty::Write(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
file.WriteBits(m_values[index], m_numBits);
}
void MP4BitfieldProperty::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
uint8_t hexWidth = m_numBits / 4;
if (hexWidth == 0 || (m_numBits % 4)) {
hexWidth++;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1,
"\"%s\": %s[%u] = %" PRIu64 " (0x%0*" PRIx64 ") <%u bits>",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index], (int)hexWidth, m_values[index], m_numBits);
else
log.dump(indent, MP4_LOG_VERBOSE1,
"\"%s\": %s = %" PRIu64 " (0x%0*" PRIx64 ") <%u bits>",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index], (int)hexWidth, m_values[index], m_numBits);
}
// MP4Float32Property
void MP4Float32Property::Read(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
if (m_useFixed16Format) {
m_values[index] = file.ReadFixed16();
} else if (m_useFixed32Format) {
m_values[index] = file.ReadFixed32();
} else {
m_values[index] = file.ReadFloat();
}
}
void MP4Float32Property::Write(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
if (m_useFixed16Format) {
file.WriteFixed16(m_values[index]);
} else if (m_useFixed32Format) {
file.WriteFixed32(m_values[index]);
} else {
file.WriteFloat(m_values[index]);
}
}
void MP4Float32Property::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if (m_implicit && !dumpImplicits) {
return;
}
if (index != 0)
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s[%u] = %f",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, index, m_values[index]);
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s = %f",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, m_values[index]);
}
// MP4StringProperty
MP4StringProperty::MP4StringProperty(
MP4Atom& parentAtom,
const char* name,
bool useCountedFormat,
bool useUnicode,
bool arrayMode )
: MP4Property( parentAtom, name )
, m_arrayMode ( arrayMode )
, m_useCountedFormat ( useCountedFormat )
, m_useExpandedCount ( false )
, m_useUnicode ( useUnicode )
, m_fixedLength ( 0 )
{
SetCount( 1 );
m_values[0] = NULL;
}
MP4StringProperty::~MP4StringProperty()
{
uint32_t count = GetCount();
for (uint32_t i = 0; i < count; i++) {
MP4Free(m_values[i]);
}
}
void MP4StringProperty::SetCount(uint32_t count)
{
uint32_t oldCount = m_values.Size();
m_values.Resize(count);
for (uint32_t i = oldCount; i < count; i++) {
m_values[i] = NULL;
}
}
void MP4StringProperty::SetValue(const char* value, uint32_t index)
{
if (m_readOnly) {
ostringstream msg;
msg << "property " << m_name << "is read-only";
throw new PlatformException(msg.str().c_str(), EACCES, __FILE__, __LINE__, __FUNCTION__ );
}
MP4Free(m_values[index]);
if (m_fixedLength) {
m_values[index] = (char*)MP4Calloc(m_fixedLength + 1);
if (value) {
strncpy(m_values[index], value, m_fixedLength);
}
} else {
if (value) {
m_values[index] = MP4Stralloc(value);
} else {
m_values[index] = NULL;
}
}
}
void MP4StringProperty::Read( MP4File& file, uint32_t index )
{
if( m_implicit )
return;
uint32_t begin = index;
uint32_t max = index + 1;
if( m_arrayMode ) {
begin = 0;
max = GetCount();
}
for( uint32_t i = begin; i < max; i++ ) {
char*& value = m_values[i];
// Generally a default atom setting, e.g. see atom_avc1.cpp, "JVT/AVC Coding"; we'll leak this string if
// we don't free. Note that this code checks for null before calling free and sets the pointer to null
// after freeing it, to prevent a double free in case an exception occurs before the value is reassigned.
MP4Free( value );
value = NULL;
if( m_useCountedFormat ) {
value = file.ReadCountedString( (m_useUnicode ? 2 : 1), m_useExpandedCount, m_fixedLength );
}
else if( m_fixedLength ) {
value = (char*)MP4Calloc( m_fixedLength + 1 );
file.ReadBytes( (uint8_t*)value, m_fixedLength );
}
else {
value = file.ReadString();
}
}
}
void MP4StringProperty::Write( MP4File& file, uint32_t index )
{
if( m_implicit )
return;
uint32_t begin = index;
uint32_t max = index + 1;
if( m_arrayMode ) {
begin = 0;
max = GetCount();
}
for( uint32_t i = begin; i < max; i++ ) {
char*& value = m_values[i];
if( m_useCountedFormat ) {
file.WriteCountedString( value, (m_useUnicode ? 2 : 1), m_useExpandedCount, m_fixedLength );
}
else if( m_fixedLength ) {
file.WriteBytes( (uint8_t*)value, m_fixedLength );
}
else {
file.WriteString( value );
}
}
}
void MP4StringProperty::Dump( uint8_t indent, bool dumpImplicits, uint32_t index )
{
if( m_implicit && !dumpImplicits )
return;
if( !m_arrayMode ) {
char indexd[32];
if( index != 0 )
snprintf( indexd, 32, "[%u]", index );
else
indexd[0] = '\0';
if( m_useUnicode )
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s%s = %ls",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, indexd, (wchar_t*)m_values[index] );
else
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": %s%s = %s",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, indexd, m_values[index] );
}
else if( log.verbosity >= MP4_LOG_VERBOSE2 )
{
const uint32_t max = GetCount();
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s (size=%u)",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, max );
for( uint32_t i = 0; i < max; i++ ) {
char*& value = m_values[i];
if( m_useUnicode )
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s[%u] = %ls",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, i, (wchar_t*)value );
else
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s[%u] = %s",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, i, value );
}
}
else {
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": <table entries suppressed>",
m_parentAtom.GetFile().GetFilename().c_str() );
}
}
// MP4BytesProperty
MP4BytesProperty::MP4BytesProperty(MP4Atom& parentAtom, const char* name, uint32_t valueSize,
uint32_t defaultValueSize)
: MP4Property(parentAtom, name)
, m_fixedValueSize(0)
, m_defaultValueSize(defaultValueSize)
{
SetCount(1);
m_values[0] = (uint8_t*)MP4Calloc(valueSize);
m_valueSizes[0] = valueSize;
}
MP4BytesProperty::~MP4BytesProperty()
{
uint32_t count = GetCount();
for (uint32_t i = 0; i < count; i++) {
MP4Free(m_values[i]);
}
}
void MP4BytesProperty::SetCount(uint32_t count)
{
uint32_t oldCount = m_values.Size();
m_values.Resize(count);
m_valueSizes.Resize(count);
for (uint32_t i = oldCount; i < count; i++) {
m_values[i] = NULL;
m_valueSizes[i] = m_defaultValueSize;
}
}
void MP4BytesProperty::SetValue(const uint8_t* pValue, uint32_t valueSize,
uint32_t index)
{
if (m_readOnly) {
ostringstream msg;
msg << "property " << m_name << "is read-only";
throw new PlatformException(msg.str().c_str(), EACCES, __FILE__, __LINE__, __FUNCTION__ );
}
if (m_fixedValueSize) {
if (valueSize > m_fixedValueSize) {
ostringstream msg;
msg << GetParentAtom().GetType() << "." << GetName() << " value size " << valueSize << " exceeds fixed value size " << m_fixedValueSize;
throw new Exception(msg.str().c_str(), __FILE__, __LINE__, __FUNCTION__ );
}
if (m_values[index] == NULL) {
m_values[index] = (uint8_t*)MP4Calloc(m_fixedValueSize);
m_valueSizes[index] = m_fixedValueSize;
}
if (pValue) {
memcpy(m_values[index], pValue, valueSize);
}
} else {
MP4Free(m_values[index]);
if (pValue) {
m_values[index] = (uint8_t*)MP4Malloc(valueSize);
memcpy(m_values[index], pValue, valueSize);
m_valueSizes[index] = valueSize;
} else {
m_values[index] = NULL;
m_valueSizes[index] = 0;
}
}
}
void MP4BytesProperty::SetValueSize(uint32_t valueSize, uint32_t index)
{
if (m_fixedValueSize) {
throw new Exception("can't change size of fixed sized property",
__FILE__, __LINE__, __FUNCTION__ );
}
if (m_values[index] != NULL) {
m_values[index] = (uint8_t*)MP4Realloc(m_values[index], valueSize);
}
m_valueSizes[index] = valueSize;
}
void MP4BytesProperty::SetFixedSize(uint32_t fixedSize)
{
m_fixedValueSize = 0;
for (uint32_t i = 0; i < GetCount(); i++) {
SetValueSize(fixedSize, i);
}
m_fixedValueSize = fixedSize;
}
void MP4BytesProperty::Read(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
MP4Free(m_values[index]);
m_values[index] = (uint8_t*)MP4Malloc(m_valueSizes[index]);
file.ReadBytes(m_values[index], m_valueSizes[index]);
}
void MP4BytesProperty::Write(MP4File& file, uint32_t index)
{
if (m_implicit) {
return;
}
file.WriteBytes(m_values[index], m_valueSizes[index]);
}
void MP4BytesProperty::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
if( m_implicit && !dumpImplicits )
return;
const uint32_t size = m_valueSizes[index];
const uint8_t* const value = m_values[index];
if( size == 0 ) {
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s = <%u bytes>",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, size );
return;
}
if( size <= 16 ) {
ostringstream oss;
ostringstream text;
oss << " ";
for( uint32_t i = 0; i < size; i++ ) {
if( i )
oss << ' ';
oss << hex << setw(2) << setfill('0') << right << static_cast<uint32_t>(value[i]);
text << (isprint( static_cast<int>(value[i]) ) ? static_cast<char>(value[i]) : '.');
}
oss << " |" << text.str() << "|";
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s = <%u bytes>%s",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, size, oss.str().c_str() );
return;
}
// specialization for ilst item data always show all bytes except for covr
bool showall = false;
MP4Atom* const datac = m_parentAtom.GetParentAtom(); // data container
MP4Atom* const datacc = datac->GetParentAtom();
if( datacc &&
ATOMID( datacc->GetType() ) == ATOMID( "ilst" ) &&
ATOMID( datac->GetType() ) != ATOMID( "covr" ) )
{
showall = true;
}
uint32_t adjsize;
bool supressed;
if( showall ||
size < 128 || log.verbosity >= MP4_LOG_VERBOSE2 )
{
adjsize = size;
supressed = false;
}
else {
adjsize = 128;
supressed = true;
}
ostringstream oss;
ostringstream text;
log.dump(indent, MP4_LOG_VERBOSE2, "\"%s\": %s = <%u bytes>",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name, size );
log.hexDump(indent, MP4_LOG_VERBOSE2, value, adjsize, "\"%s\": %s",
m_parentAtom.GetFile().GetFilename().c_str(),
m_name);
if( supressed ) {
log.dump(indent, MP4_LOG_VERBOSE1, "\"%s\": <remaining bytes supressed>",
m_parentAtom.GetFile().GetFilename().c_str() );
}
}
// MP4TableProperty
MP4TableProperty::MP4TableProperty(MP4Atom& parentAtom, const char* name, MP4IntegerProperty* pCountProperty)
: MP4Property(parentAtom, name)
{
m_pCountProperty = pCountProperty;
m_pCountProperty->SetReadOnly();
}
MP4TableProperty::~MP4TableProperty()
{
for (uint32_t i = 0; i < m_pProperties.Size(); i++) {
delete m_pProperties[i];
}
}
void MP4TableProperty::AddProperty(MP4Property* pProperty)
{
ASSERT(pProperty);
ASSERT(pProperty->GetType() != TableProperty);
ASSERT(pProperty->GetType() != DescriptorProperty);
m_pProperties.Add(pProperty);
pProperty->SetCount(0);
}
bool MP4TableProperty::FindProperty(const char *name,
MP4Property** ppProperty, uint32_t* pIndex)
{
ASSERT(m_name);
// check if first component of name matches ourselves
if (!MP4NameFirstMatches(m_name, name)) {
return false;
}
// check if the specified table entry exists
uint32_t index;
bool haveIndex = MP4NameFirstIndex(name, &index);
if (haveIndex) {
if (index >= GetCount()) {
return false;
}
if (pIndex) {
*pIndex = index;
}
}
log.verbose1f("\"%s\": FindProperty: matched %s",
m_parentAtom.GetFile().GetFilename().c_str(), name);
// get name of table property
const char *tablePropName = MP4NameAfterFirst(name);
if (tablePropName == NULL) {
if (!haveIndex) {
*ppProperty = this;
return true;
}
return false;
}
// check if this table property exists
return FindContainedProperty(tablePropName, ppProperty, pIndex);
}
bool MP4TableProperty::FindContainedProperty(const char *name,
MP4Property** ppProperty, uint32_t* pIndex)
{
uint32_t numProperties = m_pProperties.Size();
for (uint32_t i = 0; i < numProperties; i++) {
if (m_pProperties[i]->FindProperty(name, ppProperty, pIndex)) {
return true;
}
}
return false;
}
struct FastRead32Attr
{
typedef uint32_t PropertyType;
typedef MP4Integer32Property MP4PropertyType;
static uint32_t ReverseBytes( const uint32_t& x ) { return MP4V2_BYTESWAP_32( x ); }
};
struct FastRead64Attr
{
typedef uint64_t PropertyType;
typedef MP4Integer64Property MP4PropertyType;
static uint64_t ReverseBytes( const uint64_t& x ) { return MP4V2_BYTESWAP_64( x ); }
};
template <class ATTR>
bool FastReadAttr( MP4File& file, MP4PropertyArray& properties, int32_t numEntries )
{
uint8_t buf[10000]; // use stack, since allocating on heap is slow
uint32_t numProperties = properties.Size();
uint32_t propertySize = sizeof( typename ATTR::PropertyType );
uint32_t entrySize = propertySize * numProperties;
int32_t numEntriesThatFitInBuffer = sizeof(buf) / entrySize;
typename ATTR::PropertyType* p = NULL;
for (int32_t i = 0; i < numEntries; i++)
{
if ( i % numEntriesThatFitInBuffer == 0 ) // refresh the buffer if necessary
{
int numEntriesLeft = numEntries - i;
int numEntriesToRead = numEntriesLeft < numEntriesThatFitInBuffer ? numEntriesLeft : numEntriesThatFitInBuffer;
file.ReadBytes( buf, numEntriesToRead * entrySize );
p = (typename ATTR::PropertyType*) buf;
}
for (uint32_t j = 0; j < numProperties; j++, p++)
{
((typename ATTR::MP4PropertyType*) properties[j])->SetValue( ATTR::ReverseBytes( *p ), i );
}
}
return true;
}
bool MP4TableProperty::FastRead(MP4File& file)
{
uint32_t numProperties = m_pProperties.Size();
if ( numProperties <= 0 )
return false;
MP4PropertyType propType = m_pProperties[0]->GetType();
// make sure all property types match
for (uint32_t j = 0; j < numProperties; j++)
if ( m_pProperties[j]->GetType() != propType )
return false;
// make sure no properties are implicit
for (uint32_t j = 0; j < numProperties; j++)
if ( m_pProperties[j]->IsImplicit() )
return false;
// make sure no properties are read-only
for (uint32_t j = 0; j < numProperties; j++)
if ( m_pProperties[j]->IsReadOnly() )
return false;
uint32_t numEntries = GetCount();
if ( propType == Integer32Property )
{
return FastReadAttr<FastRead32Attr>( file, m_pProperties, numEntries );
}
else if ( propType == Integer64Property )
{
return FastReadAttr<FastRead64Attr>( file, m_pProperties, numEntries );
}
return false;
}
void MP4TableProperty::Read(MP4File& file, uint32_t index)
{
ASSERT(index == 0);
if (m_implicit) {
return;
}
uint32_t numProperties = m_pProperties.Size();
if (numProperties == 0) {
WARNING(numProperties == 0);
return;
}
uint32_t numEntries = GetCount();
/* for each property set size */
for (uint32_t j = 0; j < numProperties; j++) {
m_pProperties[j]->SetCount(numEntries);
}
bool fastReadSucceeded = FastRead(file);
if ( !fastReadSucceeded )
{
for (uint32_t i = 0; i < numEntries; i++) {
ReadEntry(file, i);
}
}
}
void MP4TableProperty::ReadEntry(MP4File& file, uint32_t index)
{
for (uint32_t j = 0; j < m_pProperties.Size(); j++) {
m_pProperties[j]->Read(file, index);
}
}
void MP4TableProperty::Write(MP4File& file, uint32_t index)
{
ASSERT(index == 0);
if (m_implicit) {
return;
}
uint32_t numProperties = m_pProperties.Size();
if (numProperties == 0) {
WARNING(numProperties == 0);
return;
}
uint32_t numEntries = GetCount();
if (m_pProperties[0]->GetCount() != numEntries) {
log.errorf("%s: \"%s\": %s %s \"%s\"table entries %u doesn't match count %u",
__FUNCTION__, m_parentAtom.GetFile().GetFilename().c_str(),
GetParentAtom().GetType(),
GetName(), m_pProperties[0]->GetName(),
m_pProperties[0]->GetCount(), numEntries);
ASSERT(m_pProperties[0]->GetCount() == numEntries);
}
for (uint32_t i = 0; i < numEntries; i++) {
WriteEntry(file, i);
}
}
void MP4TableProperty::WriteEntry(MP4File& file, uint32_t index)
{
for (uint32_t j = 0; j < m_pProperties.Size(); j++) {
m_pProperties[j]->Write(file, index);
}
}
void MP4TableProperty::Dump(uint8_t indent,
bool dumpImplicits, uint32_t index)
{
ASSERT(index == 0);
// implicit tables just can't be dumped
if (m_implicit) {
return;
}
uint32_t numProperties = m_pProperties.Size();
if (numProperties == 0) {
WARNING(numProperties == 0);
return;
}
uint32_t numEntries = GetCount();
for (uint32_t i = 0; i < numEntries; i++) {
for (uint32_t j = 0; j < numProperties; j++) {
m_pProperties[j]->Dump(indent + 1, dumpImplicits, i);
}
}
}
// MP4DescriptorProperty
MP4DescriptorProperty::MP4DescriptorProperty(MP4Atom& parentAtom, const char* name,
uint8_t tagsStart, uint8_t tagsEnd, bool mandatory, bool onlyOne)
: MP4Property(parentAtom, name)
{
SetTags(tagsStart, tagsEnd);
m_sizeLimit = 0;
m_mandatory = mandatory;
m_onlyOne = onlyOne;
}
MP4DescriptorProperty::~MP4DescriptorProperty()
{
for (uint32_t i = 0; i < m_pDescriptors.Size(); i++) {
delete m_pDescriptors[i];
}
}
MP4Descriptor* MP4DescriptorProperty::AddDescriptor(uint8_t tag)
{
// check that tag is in expected range
ASSERT(tag >= m_tagsStart && tag <= m_tagsEnd);
MP4Descriptor* pDescriptor = CreateDescriptor(m_parentAtom, tag);
ASSERT(pDescriptor);
m_pDescriptors.Add(pDescriptor);
return pDescriptor;
}
void MP4DescriptorProperty::DeleteDescriptor(uint32_t index)
{
delete m_pDescriptors[index];
m_pDescriptors.Delete(index);
}
void MP4DescriptorProperty::Generate()
{
// generate a default descriptor
// if it is mandatory, and single
if (m_mandatory && m_onlyOne) {
MP4Descriptor* pDescriptor =
AddDescriptor(m_tagsStart);
pDescriptor->Generate();
}
}
bool MP4DescriptorProperty::FindProperty(const char *name,
MP4Property** ppProperty, uint32_t* pIndex)
{
// we're unnamed, so just check contained properties
if (m_name == NULL || !strcmp(m_name, "")) {
return FindContainedProperty(name, ppProperty, pIndex);
}
// check if first component of name matches ourselves
if (!MP4NameFirstMatches(m_name, name)) {
return false;
}
// check if the specific descriptor entry exists
uint32_t descrIndex;
bool haveDescrIndex = MP4NameFirstIndex(name, &descrIndex);
if (haveDescrIndex && descrIndex >= GetCount()) {
return false;
}