aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/libmp4v2/mp4atom.cpp
blob: 9df1a6f8f7c68e91dc8c4409808e854196b46193 (plain) (blame)
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
/*
 * 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 - 2004.  All Rights Reserved.
 * 
 * 3GPP features implementation is based on 3GPP's TS26.234-v5.60,
 * and was contributed by Ximpo Group Ltd.
 *
 * Portions created by Ximpo Group Ltd. are
 * Copyright (C) Ximpo Group Ltd. 2003, 2004.  All Rights Reserved.
 * 
 *  Portions created by Adnecto d.o.o. are
 *  Copyright (C) Adnecto d.o.o. 2005.  All Rights Reserved
 *
 * Contributor(s): 
 *		Dave Mackie			dmackie@cisco.com
 *		Alix Marchandise-Franquet	alix@cisco.com
 *              Ximpo Group Ltd.                mp4v2@ximpo.com
 *              Danijel Kopcinovic              danijel.kopcinovic@adnecto.net
 */

#include "mp4common.h"
#include "atoms.h"

MP4AtomInfo::MP4AtomInfo(const char* name, bool mandatory, bool onlyOne) 
{
	m_name = name;
	m_mandatory = mandatory;
	m_onlyOne = onlyOne;
	m_count = 0;
}

MP4Atom::MP4Atom(const char* type) 
{
	SetType(type);
	m_unknownType = FALSE;
	m_pFile = NULL;
	m_start = 0;
	m_end = 0;
	m_size = 0;
	m_pParentAtom = NULL;
	m_depth = 0xFF;
	memset(m_extendedType, 0, sizeof(m_extendedType));
}

MP4Atom::~MP4Atom()
{
	u_int32_t i;

	for (i = 0; i < m_pProperties.Size(); i++) {
		delete m_pProperties[i];
	}
	for (i = 0; i < m_pChildAtomInfos.Size(); i++) {
		delete m_pChildAtomInfos[i];
	}
	for (i = 0; i < m_pChildAtoms.Size(); i++) {
		delete m_pChildAtoms[i];
	}
}

MP4Atom* MP4Atom::CreateAtom(const char* type)
{
  MP4Atom* pAtom = NULL;

  if (type == NULL) {
    pAtom = new MP4RootAtom();
  } else {
    switch((uint8_t)type[0]) {
    case 'a':
      if (ATOMID(type) == ATOMID("avc1")) {
	pAtom = new MP4Avc1Atom();
      } else if (ATOMID(type) == ATOMID("avcC")) {
	pAtom = new MP4AvcCAtom();
      } else if (ATOMID(type) == ATOMID("alis")) {
	pAtom = new MP4UrlAtom("alis");
      } else if (ATOMID(type) == ATOMID("alaw")) {
	pAtom = new MP4SoundAtom(type);
      } else if (ATOMID(type) == ATOMID("alac")) {
	pAtom = new MP4SoundAtom(type);
      } else if (ATOMID(type) == ATOMID("albm")) {
	pAtom = new MP4Meta3Atom(type);
      }			
      break;
    case 'c':
      if (ATOMID(type) == ATOMID("chap")) {
	pAtom = new MP4TrefTypeAtom(type);
	 } else if (ATOMID(type) == ATOMID("chpl")) { 
 pAtom = new MP4ChplAtom(); 
	  }
	  break;
    case 'd':
      if (ATOMID(type) == ATOMID("d263")) {
	pAtom = new MP4D263Atom();
      } else if (ATOMID(type) == ATOMID("damr")) {
	pAtom = new MP4DamrAtom();
      } else if (ATOMID(type) == ATOMID("dref")) {
	pAtom = new MP4DrefAtom();
      } else if (ATOMID(type) == ATOMID("dpnd")) {
	pAtom = new MP4TrefTypeAtom(type);
      } else if (ATOMID(type) == ATOMID("data")) { /* Apple iTunes */
	pAtom = new MP4DataAtom();
      }
      break;
    case 'e':
      if (ATOMID(type) == ATOMID("elst")) {
	pAtom = new MP4ElstAtom();
      } else if (ATOMID(type) == ATOMID("enca")) {
	pAtom = new MP4EncaAtom();
      } else if (ATOMID(type) == ATOMID("encv")) {
	pAtom = new MP4EncvAtom();
      }
      break;
    case 'f':
      if (ATOMID(type) == ATOMID("free")) {
	pAtom = new MP4FreeAtom();
      } else if (ATOMID(type) == ATOMID("ftyp")) {
	pAtom = new MP4FtypAtom();
      }
      break;
    case 'g':
      if (ATOMID(type) == ATOMID("gmin")) {
	pAtom = new MP4GminAtom();
      }/* else if (ATOMID(type) == ATOMID("gnre")) { // TODO: benski look at atom_rtp for implementation of two atom types with same name
	pAtom = new MP4Meta3Atom(type);
      }	*/		
      break;
    case 'h':
      if (ATOMID(type) == ATOMID("hdlr")) {
	pAtom = new MP4HdlrAtom();
      } else if (ATOMID(type) == ATOMID("hint")) {
	pAtom = new MP4TrefTypeAtom(type);
      } else if (ATOMID(type) == ATOMID("hnti")) {
	pAtom = new MP4HntiAtom();
      } else if (ATOMID(type) == ATOMID("hinf")) {
	pAtom = new MP4HinfAtom();
      } else if (ATOMID(type) == ATOMID("h263")) {
	pAtom = new MP4VideoAtom("h263");
      } else if (ATOMID(type) == ATOMID("href")) {
	pAtom = new MP4HrefAtom();
      }
      break;
    case 'i':
      if (ATOMID(type) == ATOMID("ipir")) {
	pAtom = new MP4TrefTypeAtom(type);
      } else if (ATOMID(type) == ATOMID("ima4")) {
	pAtom = new MP4SoundAtom("ima4");
      }
      break;
    case 'j':
      if (ATOMID(type) == ATOMID("jpeg")) {
	pAtom = new MP4VideoAtom("jpeg");
      }
      break;
    case 'm':
      if (ATOMID(type) == ATOMID("mdhd")) {
	pAtom = new MP4MdhdAtom();
      } else if (ATOMID(type) == ATOMID("mvhd")) {
	pAtom = new MP4MvhdAtom();
      } else if (ATOMID(type) == ATOMID("mdat")) {
	pAtom = new MP4MdatAtom();
      } else if (ATOMID(type) == ATOMID("mpod")) {
	pAtom = new MP4TrefTypeAtom(type);
      } else if (ATOMID(type) == ATOMID("mp4a")) {
	pAtom = new MP4SoundAtom("mp4a");
      } else if (ATOMID(type) == ATOMID("mp4s")) {
	pAtom = new MP4Mp4sAtom();
      } else if (ATOMID(type) == ATOMID("mp4v")) {
	pAtom = new MP4Mp4vAtom();
      } else if (ATOMID(type) == ATOMID("mean")) { // iTunes
	pAtom = new MP4Meta1Atom(type);
      }
      break;
    case 'n':
      if (ATOMID(type) == ATOMID("name")) { // iTunes
	pAtom = new MP4Meta1Atom(type);
      }
      break;
    case 'o':
      if (ATOMID(type) == ATOMID("ohdr")) {
	pAtom = new MP4OhdrAtom();
      }
      break;
		case 'p':
			 if (ATOMID(type) == ATOMID("perf")) {
	pAtom = new MP4Meta3Atom(type);
      }			
			 break;
    case 'r':
      if (ATOMID(type) == ATOMID("rtp ")) {
	pAtom = new MP4RtpAtom();
      } else if (ATOMID(type) == ATOMID("raw ")) {
	pAtom = new MP4VideoAtom("raw ");
      }
      break;
    case 's':
      if (ATOMID(type) == ATOMID("s263"))  {
        pAtom = new MP4S263Atom();
      } else if (ATOMID(type) == ATOMID("samr")) {
	pAtom = new MP4AmrAtom("samr");
      } else if (ATOMID(type) == ATOMID("sawb")) {
	pAtom = new MP4AmrAtom("sawb");
      } else if (ATOMID(type) == ATOMID("stbl")) {
	pAtom = new MP4StblAtom();
      } else if (ATOMID(type) == ATOMID("stsd")) {
	pAtom = new MP4StsdAtom();
      } else if (ATOMID(type) == ATOMID("stsz")) {
	pAtom = new MP4StszAtom();
      } else if (ATOMID(type) == ATOMID("stsc")) {
	pAtom = new MP4StscAtom();
      } else if (ATOMID(type) == ATOMID("stz2")) {
	pAtom = new MP4Stz2Atom();
      } else if (ATOMID(type) == ATOMID("stdp")) {
	pAtom = new MP4StdpAtom();
      } else if (ATOMID(type) == ATOMID("sdp ")) {
	pAtom = new MP4SdpAtom();
      } else if (ATOMID(type) == ATOMID("sync")) {
	pAtom = new MP4TrefTypeAtom(type);
      } else if (ATOMID(type) == ATOMID("skip")) {
	pAtom = new MP4FreeAtom();
	pAtom->SetType("skip");
      } else if (ATOMID(type) == ATOMID("sowt")) {
	pAtom = new MP4SoundAtom("sowt");
      }
      break;
    case 't':
      if (ATOMID(type) == ATOMID("text")) {
	pAtom = new MP4TextAtom();
      } else if (ATOMID(type) == ATOMID("tkhd")) {
	pAtom = new MP4TkhdAtom();
      } else if (ATOMID(type) == ATOMID("tfhd")) {
	pAtom = new MP4TfhdAtom();
      } else if (ATOMID(type) == ATOMID("trun")) {
	pAtom = new MP4TrunAtom();
      } else if (ATOMID(type) == ATOMID("twos")) {
	pAtom = new MP4SoundAtom("twos");
			} else if (ATOMID(type) == ATOMID("titl")) {
	pAtom = new MP4Meta3Atom(type);
      }
      break;
    case 'u':
      if (ATOMID(type) == ATOMID("udta")) {
	pAtom = new MP4UdtaAtom();
      } else if (ATOMID(type) == ATOMID("url ")) {
	pAtom = new MP4UrlAtom();
      } else if (ATOMID(type) == ATOMID("urn ")) {
	pAtom = new MP4UrnAtom();
      } else if (ATOMID(type) == ATOMID("ulaw")) {
	pAtom = new MP4SoundAtom("ulaw");
      }
      break;
    case 'v':
      if (ATOMID(type) == ATOMID("vmhd")) {
	pAtom = new MP4VmhdAtom();
      }
      break;
    case 'y':
      if (ATOMID(type) == ATOMID("yuv2")) {
	pAtom = new MP4VideoAtom("yuv2");
      }
			else if (ATOMID(type) == ATOMID("yrrc")) {
	pAtom = new MP4Meta4Atom(type);
      }
      break;
    case 'S':
      if (ATOMID(type) == ATOMID("SVQ3")) {
	pAtom = new MP4VideoAtom("SVQ3");
      } else if (ATOMID(type) == ATOMID("SMI ")) {
	pAtom = new MP4SmiAtom();
      }
      break;
    case 0251:
      static const char name[5]={'\xA9','n', 'a', 'm', '\0'};
      static const char cmt[5]={'\xA9','c', 'm', 't', '\0'};
      static const char cpy[5]={'\xA9','c', 'p', 'y', '\0'};
      static const char des[5]={'\xA9','d', 'e', 's','\0'};
      static const char prd[5]={'\xA9', 'p', 'r', 'd', '\0'};
			static const char lyr[5]={'\xA9', 'l', 'y', 'r', '\0'};
      if (ATOMID(type) == ATOMID(name) ||
	  ATOMID(type) == ATOMID(cmt) ||
	  ATOMID(type) == ATOMID(cpy) ||
	  ATOMID(type) == ATOMID(prd) ||
	  ATOMID(type) == ATOMID(des) || 
		ATOMID(type) == ATOMID(lyr)) {
	pAtom = new MP4Meta2Atom(type);
      }
      break;
    }
  }

  if (pAtom == NULL) {
    pAtom = new MP4StandardAtom(type);
    // unknown type is set by StandardAtom type
  }

  ASSERT(pAtom);
  return pAtom;
}

// generate a skeletal self

void MP4Atom::Generate()
{
	u_int32_t i;

	// for all properties
	for (i = 0; i < m_pProperties.Size(); i++) {
		// ask it to self generate
		m_pProperties[i]->Generate();
	}

	// for all mandatory, single child atom types
	for (i = 0; i < m_pChildAtomInfos.Size(); i++) {
		if (m_pChildAtomInfos[i]->m_mandatory
		  && m_pChildAtomInfos[i]->m_onlyOne) {

			// create the mandatory, single child atom
			MP4Atom* pChildAtom = 
				CreateAtom(m_pChildAtomInfos[i]->m_name);

			AddChildAtom(pChildAtom);

			// and ask it to self generate
			pChildAtom->Generate();
		}
	}
}

MP4Atom* MP4Atom::ReadAtom(MP4File* pFile, MP4Atom* pParentAtom)
{
	u_int8_t hdrSize = 8;
	u_int8_t extendedType[16];

	u_int64_t pos = pFile->GetPosition();

	VERBOSE_READ(pFile->GetVerbosity(), 
		printf("ReadAtom: pos = 0x"X64"\n", pos));

	u_int64_t dataSize = pFile->ReadUInt32();

	char type[5];
	pFile->ReadBytes((u_int8_t*)&type[0], 4);
	type[4] = '\0';
	
	// extended size
	if (dataSize == 1) {
		dataSize = pFile->ReadUInt64(); 
		hdrSize += 8;
		pFile->Check64BitStatus(type);
	}

	// extended type
	if (ATOMID(type) == ATOMID("uuid")) {
		pFile->ReadBytes(extendedType, sizeof(extendedType));
		hdrSize += sizeof(extendedType);
	}

	if (dataSize == 0) {
		// extends to EOF
		dataSize = pFile->GetSize() - pos;
	}

	dataSize -= hdrSize;

	VERBOSE_READ(pFile->GetVerbosity(), 
		printf("ReadAtom: type = \"%s\" data-size = "U64" (0x"X64") hdr %u\n", 
		       type, dataSize, dataSize, hdrSize));

	if (pos + hdrSize + dataSize > pParentAtom->GetEnd()) {
		VERBOSE_ERROR(pFile->GetVerbosity(), 
			printf("ReadAtom: invalid atom size, extends outside parent atom - skipping to end of \"%s\" \"%s\" "U64" vs "U64"\n", 
			       pParentAtom->GetType(), type,
			       pos + hdrSize + dataSize, 
			       pParentAtom->GetEnd()));
		VERBOSE_READ(pFile->GetVerbosity(),
			     printf("parent %s ("U64") pos "U64" hdr %d data "U64" sum "U64"\n",
				    pParentAtom->GetType(),
				    pParentAtom->GetEnd(),
				    pos, 
				    hdrSize, 
				    dataSize,
				    pos + hdrSize + dataSize));
#if 0
		throw new MP4Error("invalid atom size", "ReadAtom");
#else
		//pFile->SetPosition(pParentAtom->GetEnd());
		//return 0;
		// TODO: benski> we should re-enable this if we can find a good way to very that the datasize might be correct.
		// skip to end of atom
		dataSize = pParentAtom->GetEnd() - pos - hdrSize;
#endif
	}


	MP4Atom* pAtom = CreateAtom(type);
	pAtom->SetFile(pFile);
	pAtom->SetStart(pos);
	pAtom->SetEnd(pos + hdrSize + dataSize);
	pAtom->SetSize(dataSize);
	if (ATOMID(type) == ATOMID("uuid")) {
		pAtom->SetExtendedType(extendedType);
	}
	if (pAtom->IsUnknownType()) {
		if (!IsReasonableType(pAtom->GetType())) {
			VERBOSE_READ(pFile->GetVerbosity(),
				printf("Warning: atom type %s is suspect\n", pAtom->GetType()));
		} else {
			VERBOSE_READ(pFile->GetVerbosity(),
				printf("Info: atom type %s is unknown\n", pAtom->GetType()));
		}

		if (dataSize > 0) {
			pAtom->AddProperty(
				new MP4BytesProperty("data", dataSize));
		}
	}

	pAtom->SetParentAtom(pParentAtom);

	pAtom->Read();

	return pAtom;
}

bool MP4Atom::IsReasonableType(const char* type)
{
	for (u_int8_t i = 0; i < 4; i++) {
		if (iscntrl((unsigned char)type[i])) {
			return false;
		}
		if (i == 3 && type[i] == ' ') {
			continue;
		}
		return false;
	}
	return true;
}

// generic read
void MP4Atom::Read()
{
	ASSERT(m_pFile);

	if (ATOMID(m_type) != 0 && m_size > 1000000) {
		VERBOSE_READ(GetVerbosity(), 
			printf("Warning: %s atom size "U64" is suspect\n",
				m_type, m_size));
	}

	ReadProperties();

	// read child atoms, if we expect there to be some
	if (m_pChildAtomInfos.Size() > 0) {
		ReadChildAtoms();
	}

	Skip();	// to end of atom
}

void MP4Atom::Skip()
{
	if (m_pFile->GetPosition() != m_end) {
		VERBOSE_READ(m_pFile->GetVerbosity(),
			printf("Skip: "U64" bytes\n", m_end - m_pFile->GetPosition()));
	}
	m_pFile->SetPosition(m_end);
}

MP4Atom* MP4Atom::FindAtomMP4(const char* name)
{
	if (!IsMe(name)) {
		return NULL;
	}

	if (!IsRootAtom()) {
		VERBOSE_FIND(m_pFile->GetVerbosity(),
			printf("FindAtomMP4: matched %s\n", name));

		name = MP4NameAfterFirst(name);

		// I'm the sought after atom 
		if (name == NULL) {
			return this;
		}
	}

	// else it's one of my children
	return FindChildAtom(name);
}

bool MP4Atom::FindProperty(const char *name, 
	MP4Property** ppProperty, u_int32_t* pIndex)
{
	if (!IsMe(name)) {
		return false;
	}

	if (!IsRootAtom()) {
		VERBOSE_FIND(m_pFile->GetVerbosity(),
			printf("FindProperty: matched %s\n", name));

		name = MP4NameAfterFirst(name);

		// no property name given
		if (name == NULL) {
			return false;
		}
	}

	return FindContainedProperty(name, ppProperty, pIndex);
}

bool MP4Atom::IsMe(const char* name)
{
	if (name == NULL) {
		return false;
	}

	// root atom always matches
	if (!strcmp(m_type, "")) {
		return true;
	}

	// check if our atom name is specified as the first component
	if (!MP4NameFirstMatches(m_type, name)) {
		return false;
	}

	return true;
}

MP4Atom* MP4Atom::FindChildAtom(const char* name)
{
	u_int32_t atomIndex = 0;

	// get the index if we have one, e.g. moov.trak[2].mdia...
	(void)MP4NameFirstIndex(name, &atomIndex);

	// need to get to the index'th child atom of the right type
	for (u_int32_t i = 0; i < m_pChildAtoms.Size(); i++) {
		if (MP4NameFirstMatches(m_pChildAtoms[i]->GetType(), name)) {
			if (atomIndex == 0) {
				// this is the one, ask it to match
				return m_pChildAtoms[i]->FindAtomMP4(name);
			}
			atomIndex--;
		}
	}

	return NULL;
}

bool MP4Atom::FindContainedProperty(const char *name,
	MP4Property** ppProperty, u_int32_t* pIndex)
{
	u_int32_t numProperties = m_pProperties.Size();
	u_int32_t i;
	// check all of our properties
	for (i = 0; i < numProperties; i++) {
		if (m_pProperties[i]->FindProperty(name, ppProperty, pIndex)) {
			return true;
		}
	}

	// not one of our properties, 
	// presumably one of our children's properties
	// check child atoms...

	// check if we have an index, e.g. trak[2].mdia...
	u_int32_t atomIndex = 0;
	(void)MP4NameFirstIndex(name, &atomIndex);

	// need to get to the index'th child atom of the right type
	for (i = 0; i < m_pChildAtoms.Size(); i++) {
		if (MP4NameFirstMatches(m_pChildAtoms[i]->GetType(), name)) {
			if (atomIndex == 0) {
				// this is the one, ask it to match
				return m_pChildAtoms[i]->FindProperty(name, ppProperty, pIndex);
			}
			atomIndex--;
		}
	}

	VERBOSE_FIND(m_pFile->GetVerbosity(),
		printf("FindProperty: no match for %s\n", name));
	return false;
}

void MP4Atom::ReadProperties(u_int32_t startIndex, u_int32_t count)
{
	u_int32_t numProperties = MIN(count, m_pProperties.Size() - startIndex);

	// read any properties of the atom
	for (u_int32_t i = startIndex; i < startIndex + numProperties; i++) {

		m_pProperties[i]->Read(m_pFile);

		if (m_pFile->GetPosition() > m_end) {
			VERBOSE_READ(GetVerbosity(), 
				printf("ReadProperties: insufficient data for property: %s pos 0x"X64" atom end 0x"X64"\n",
					m_pProperties[i]->GetName(), 
					m_pFile->GetPosition(), m_end)); 

			// benski> throw new MP4Error("atom is too small", "Atom ReadProperties");
			return;
		}
	}
}

void MP4Atom::ReadChildAtoms()
{
  bool this_is_udta = ATOMID(m_type) == ATOMID("udta");

	VERBOSE_READ(GetVerbosity(), 
		printf("ReadChildAtoms: of %s\n", m_type[0] ? m_type : "root"));
	for (u_int64_t position = m_pFile->GetPosition();
	     position < m_end;
	     position = m_pFile->GetPosition()) {
	  // make sure that we have enough to read at least 8 bytes
	  // size and type.
	  if (m_end - position < 2 * sizeof(uint32_t)) {
	    // if we're reading udta, it's okay to have 4 bytes of 0
	    if (this_is_udta &&
		m_end - position == sizeof(uint32_t)) {
	      u_int32_t mbz = m_pFile->ReadUInt32();
	      if (mbz != 0) {
		VERBOSE_WARNING(GetVerbosity(),
				printf("Error: In udta atom, end value is not zero %x\n", 
				       mbz));
	      }
	      continue;
	    }
	    // otherwise, output a warning, but don't care
	    VERBOSE_WARNING(GetVerbosity(),
			    printf("Error: In %s atom, extra "D64" bytes at end of atom\n", 
				   m_type, (m_end - position)));
	    for (uint64_t ix = 0; ix < m_end - position; ix++) {
	      (void)m_pFile->ReadUInt8();
	    }
	    continue;
	  }
		MP4Atom* pChildAtom = MP4Atom::ReadAtom(m_pFile, this);

		if (pChildAtom)
		{
		AddChildAtom(pChildAtom);

		MP4AtomInfo* pChildAtomInfo = FindAtomInfo(pChildAtom->GetType());

		// if child atom is of known type
		// but not expected here print warning
		if (pChildAtomInfo == NULL && !pChildAtom->IsUnknownType()) {
			VERBOSE_READ(GetVerbosity(),
				printf("Warning: In atom %s unexpected child atom %s\n",
					GetType(), pChildAtom->GetType()));
		}

		// if child atoms should have just one instance
		// and this is more than one, print warning
		if (pChildAtomInfo) {
			pChildAtomInfo->m_count++;

			if (pChildAtomInfo->m_onlyOne && pChildAtomInfo->m_count > 1) {
				VERBOSE_READ(GetVerbosity(),
					printf("Warning: In atom %s multiple child atoms %s\n",
						GetType(), pChildAtom->GetType()));
			}
		}

		}
	}

	// if mandatory child atom doesn't exist, print warning
	u_int32_t numAtomInfo = m_pChildAtomInfos.Size();
	for (u_int32_t i = 0; i < numAtomInfo; i++) {
		if (m_pChildAtomInfos[i]->m_mandatory
		  && m_pChildAtomInfos[i]->m_count == 0) {
				VERBOSE_READ(GetVerbosity(),
					printf("Warning: In atom %s missing child atom %s\n",
						GetType(), m_pChildAtomInfos[i]->m_name));
		}
	}

	VERBOSE_READ(GetVerbosity(), 
		printf("ReadChildAtoms: finished %s\n", m_type));
}

MP4AtomInfo* MP4Atom::FindAtomInfo(const char* name)
{
	u_int32_t numAtomInfo = m_pChildAtomInfos.Size();
	for (u_int32_t i = 0; i < numAtomInfo; i++) {
		if (ATOMID(m_pChildAtomInfos[i]->m_name) == ATOMID(name)) {
			return m_pChildAtomInfos[i];
		}
	}
	return NULL;
}

// generic write
void MP4Atom::Write()
{
	ASSERT(m_pFile);

	BeginWrite();

	WriteProperties();

	WriteChildAtoms();

	FinishWrite();
}

void MP4Atom::Rewrite()
{
       ASSERT(m_pFile);
       
       if (!m_end) {
	       // This atom hasn't been written yet...
	       return;
       }
       
       u_int64_t fPos = m_pFile->GetPosition();
       m_pFile->SetPosition(GetStart());
       Write();
       m_pFile->SetPosition(fPos);
}

void MP4Atom::BeginWrite(bool use64)
{
	m_start = m_pFile->GetPosition();
	//use64 = m_pFile->Use64Bits();
	if (use64) {
		m_pFile->WriteUInt32(1);
	} else {
		m_pFile->WriteUInt32(0);
	}
	m_pFile->WriteBytes((u_int8_t*)&m_type[0], 4);
	if (use64) {
		m_pFile->WriteUInt64(0);
	}
	if (ATOMID(m_type) == ATOMID("uuid")) {
		m_pFile->WriteBytes(m_extendedType, sizeof(m_extendedType));
	}
}

void MP4Atom::FinishWrite(bool use64)
{
	m_end = m_pFile->GetPosition();
	m_size = (m_end - m_start);
  VERBOSE_WRITE(GetVerbosity(), 
		printf("end: type %s "U64" "U64" size "U64"\n", m_type, 
		       m_start, m_end,
		       m_size));
	//use64 = m_pFile->Use64Bits();
	if (use64) {
		m_pFile->SetPosition(m_start + 8);
		m_pFile->WriteUInt64(m_size);
	} else {
		ASSERT(m_size <= (u_int64_t)0xFFFFFFFF);
		m_pFile->SetPosition(m_start);
		m_pFile->WriteUInt32(m_size);
	}
	m_pFile->SetPosition(m_end);

	// adjust size to just reflect data portion of atom
	m_size -= (use64 ? 16 : 8);
	if (ATOMID(m_type) == ATOMID("uuid")) {
		m_size -= sizeof(m_extendedType);
	}
}

void MP4Atom::WriteProperties(u_int32_t startIndex, u_int32_t count)
{
	u_int32_t numProperties = MIN(count, m_pProperties.Size() - startIndex);

	VERBOSE_WRITE(GetVerbosity(), 
		printf("Write: type %s\n", m_type));

	for (u_int32_t i = startIndex; i < startIndex + numProperties; i++) {
		m_pProperties[i]->Write(m_pFile);
	}
}

void MP4Atom::WriteChildAtoms()
{
	u_int32_t size = m_pChildAtoms.Size();
	for (u_int32_t i = 0; i < size; i++) {
		m_pChildAtoms[i]->Write();
	}

	VERBOSE_WRITE(GetVerbosity(), 
		printf("Write: finished %s\n", m_type));
}

void MP4Atom::AddProperty(MP4Property* pProperty) 
{
	ASSERT(pProperty);
	m_pProperties.Add(pProperty);
	pProperty->SetParentAtom(this);
}

void MP4Atom::AddVersionAndFlags()
{
	AddProperty(new MP4Integer8Property("version"));
	AddProperty(new MP4Integer24Property("flags"));
}

void MP4Atom::AddReserved(char* name, u_int32_t size) 
{
	MP4BytesProperty* pReserved = new MP4BytesProperty(name, size); 
	pReserved->SetReadOnly();
	AddProperty(pReserved);
}

void MP4Atom::ExpectChildAtom(const char* name, bool mandatory, bool onlyOne)
{
	m_pChildAtomInfos.Add(new MP4AtomInfo(name, mandatory, onlyOne));
}

u_int8_t MP4Atom::GetVersion()
{
	if (strcmp("version", m_pProperties[0]->GetName())) {
		return 0;
	}
	return ((MP4Integer8Property*)m_pProperties[0])->GetValue();
}

void MP4Atom::SetVersion(u_int8_t version) 
{
	if (strcmp("version", m_pProperties[0]->GetName())) {
		return;
	}
	((MP4Integer8Property*)m_pProperties[0])->SetValue(version);
}

u_int32_t MP4Atom::GetFlags()
{
	if (strcmp("flags", m_pProperties[1]->GetName())) {
		return 0;
	}
	return ((MP4Integer24Property*)m_pProperties[1])->GetValue();
}

void MP4Atom::SetFlags(u_int32_t flags) 
{
	if (strcmp("flags", m_pProperties[1]->GetName())) {
		return;
	}
	((MP4Integer24Property*)m_pProperties[1])->SetValue(flags);
}

u_int32_t MP4Atom::GetVerbosity() 
{
	ASSERT(m_pFile);
	return m_pFile->GetVerbosity();
}

u_int8_t MP4Atom::GetDepth()
{
	if (m_depth < 0xFF) {
		return m_depth;
	}

	MP4Atom *pAtom = this;
	m_depth = 0;

	while ((pAtom = pAtom->GetParentAtom()) != NULL) {
		m_depth++;
		ASSERT(m_depth < 255);
	}
	return m_depth;
}