os/mm/mmplugins/lib3gp/impl/inc/mp4atom.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #ifndef _MP4ATOM_H
    17 #define _MP4ATOM_H
    18 
    19 #include <3gplibrary/mp4config.h>
    20 #include <3gplibrary/mp4lib.h>
    21 #include "mp4list.h"
    22 #include "filewriter.h" /* Async buffering filewriter */
    23 #include "metadatafilewriter.h" /* Async buffering filewriter */
    24 #include "asyncfileparser.h" /*Async file reader for parsing */
    25 
    26 #define ATOMTYPE_FTYP 0x66747970 /* 'ftyp' */
    27 #define ATOMTYPE_MOOV 0x6d6f6f76 /* 'moov' */
    28 #define ATOMTYPE_MDAT 0x6d646174 /* 'mdat' */
    29 #define ATOMTYPE_FREE 0x66726565 /* 'free' */
    30 #define ATOMTYPE_SKIP 0x736b6970 /* 'skip' */
    31 #define ATOMTYPE_UDTA 0x75647461 /* 'udta' */
    32 #define ATOMTYPE_MVHD 0x6d766864 /* 'mvhd' */
    33 #define ATOMTYPE_IODS 0x696f6473 /* 'iods' */
    34 #define ATOMTYPE_TRAK 0x7472616b /* 'trak' */
    35 #define ATOMTYPE_TKHD 0x746b6864 /* 'tkhd' */
    36 #define ATOMTYPE_MDIA 0x6d646961 /* 'mdia' */
    37 #define ATOMTYPE_MDHD 0x6d646864 /* 'mdhd' */
    38 #define ATOMTYPE_HDLR 0x68646c72 /* 'hdlr' */
    39 #define ATOMTYPE_MINF 0x6d696e66 /* 'minf' */
    40 #define ATOMTYPE_VMHD 0x766d6864 /* 'vmhd' */
    41 #define ATOMTYPE_SMHD 0x736d6864 /* 'smhd' */
    42 #define ATOMTYPE_DINF 0x64696e66 /* 'dinf' */
    43 #define ATOMTYPE_DREF 0x64726566 /* 'dref' */
    44 #define ATOMTYPE_TREF 0x74726566 /* 'tref' */
    45 #define ATOMTYPE_STBL 0x7374626c /* 'stbl' */
    46 #define ATOMTYPE_STTS 0x73747473 /* 'stts' */
    47 #define ATOMTYPE_CTTS 0x63747473 /* 'ctts' */
    48 #define ATOMTYPE_STSS 0x73747373 /* 'stss' */
    49 #define ATOMTYPE_STSD 0x73747364 /* 'stsd' */
    50 #define ATOMTYPE_STSZ 0x7374737a /* 'stsz' */
    51 #define ATOMTYPE_STZ2 0x73747a32 /* 'stz2' */
    52 #define ATOMTYPE_STSC 0x73747363 /* 'stsc' */
    53 #define ATOMTYPE_STCO 0x7374636f /* 'stco' */
    54 #define ATOMTYPE_CO64 0x636f3634 /* 'co64' */
    55 #define ATOMTYPE_MP4V 0x6d703476 /* 'mp4v' */
    56 #define ATOMTYPE_MP4A 0x6d703461 /* 'mp4a' */
    57 #define ATOMTYPE_MP4S 0x6d703473 /* 'mp4s' */
    58 #define ATOMTYPE_S263 0x73323633 /* 's263' */
    59 #define ATOMTYPE_D263 0x64323633 /* 'd263' */
    60 #define ATOMTYPE_BITR 0x62697472 /* 'bitr' */
    61 #define ATOMTYPE_SAMR 0x73616d72 /* 'samr' */
    62 #define ATOMTYPE_SAWB 0x73617762 /* 'sawb' */
    63 #define ATOMTYPE_DAMR 0x64616d72 /* 'damr' */
    64 #define ATOMTYPE_ESD  0x65736473 /* 'esds' */
    65 #define ATOMTYPE_URL  0x75726c20 /* 'url ' */
    66 #define ATOMTYPE_URN  0x75726e20 /* 'urn ' */
    67 #define ATOMTYPE_UUID 0x75756964 /* 'uuid' */
    68 #define ATOMTYPE_EDTS 0x65647473 /* 'edts' */
    69 #define ATOMTYPE_META 0x6d657461 /* 'meta' */
    70 /* AVC Additions */
    71 #define ATOMTYPE_AVCC 0x61766343 /* 'avcC' */
    72 #define ATOMTYPE_BTRT 0x62747274 /* 'btrt' */
    73 #define ATOMTYPE_AVC1 0x61766331 /* 'avc1' */
    74 #define ATOMTYPE_M4DS 0x6d346473 /* 'm4ds' */
    75 #define ATOMTYPE_SDTP 0x73647470 /* 'sdtp' */
    76 #define ATOMTYPE_AVCP 0x61766370 /* 'avcp' */
    77 /* QCELP 13K Additions */
    78 #define ATOMTYPE_SQCP 0x73716370 /* 'sqcp' */
    79 #define ATOMTYPE_DQCP 0x64716370 /* 'dqcp' */
    80 /* inline Addition */
    81 #define ATOMTYPE_ID32 0x49443332 /* 'ID32' */
    82 
    83 #define HANDLERTYPE_ODSM 0x6f64736d /* 'odsm' */
    84 #define HANDLERTYPE_VIDE 0x76696465 /* 'vide' */
    85 #define HANDLERTYPE_SOUN 0x736f756e /* 'soun' */
    86 
    87 
    88 
    89 #define FTYP_SIZE    24     /* Size of FTYP box in bytes */
    90 
    91 #define READBUFSIZE  8192   /* Size of file reading buffer */
    92 #define WRITEBUFSIZE 2048   /* Size of file writing buffer */
    93 
    94 #define BLOCK_LIMIT  200  /* Max number of metadata blocks to keep in memory */
    95 
    96 #define MAXSAMPLESPERSECOND  150 /* magic number used to try validate input data to parser from possibly corrupted file */
    97 
    98 
    99 #define METADATAFILE_VIDEO_STTS_SAMPLE_COUNT     0
   100 #define METADATAFILE_VIDEO_STTS_SAMPLE_DELTA     1
   101 #define METADATAFILE_VIDEO_STSZ_ENTRY_SIZE       2
   102 #define METADATAFILE_VIDEO_STCO_CHUNK_OFFSET     3
   103 #define METADATAFILE_VIDEO_STSS_SAMPLE_NUMBER    4
   104 #define METADATAFILE_AUDIO_STTS_SAMPLE_COUNT     5
   105 #define METADATAFILE_AUDIO_STTS_SAMPLE_DELTA     6
   106 #define METADATAFILE_AUDIO_STSZ_ENTRY_SIZE       7
   107 #define METADATAFILE_AUDIO_STCO_CHUNK_OFFSET     8
   108 #define METADATAFILE_VIDEO_SDTP_SAMPLE_DEPENDENCY 9
   109 
   110 /* Multiple Sample Entry Inclusion */
   111 #define STSDMAXSAMPLEENTRYCOUNT 10   /* defined the maximum number of sample entried allowed in a Sample Description Box */
   112 
   113 
   114 typedef struct atomHeader
   115 {
   116   mp4_u32  size;
   117   mp4_u32  type;
   118   mp4_u64  largeSize;
   119   mp4_u8   extendedType[16];
   120   mp4_u8   version;
   121   mp4_u8   flags[3];
   122 } atomHeader;
   123 
   124 
   125 typedef struct fileTypeAtom
   126 {
   127   atomHeader  *atomhdr;
   128   mp4_u32     majorBrand;
   129   mp4_u32     minorVersion;
   130   mp4_u32     *compatibleBrands;
   131 } fileTypeAtom;
   132 
   133 
   134 typedef struct movieHeaderAtom
   135 {
   136   atomHeader      *atomhdr;
   137   mp4_u32         creationTime;
   138   mp4_u32         modificationTime;
   139   mp4_u32         timeScale;
   140   mp4_u32         duration;
   141   mp4_u32         nextTrackID;
   142   mp4_u64         creationTime64;
   143   mp4_u64         modificationTime64;
   144   mp4_u64         duration64;
   145 } movieHeaderAtom;
   146 
   147 
   148 typedef struct objectDescriptorAtom
   149 {
   150   atomHeader      *atomhdr;
   151   mp4_u8          tag;
   152   mp4_u32         sizeOfInstance;
   153   mp4_u16         flags;
   154   mp4_u8          ODProfileLevelIndication;
   155   mp4_u8          SceneProfileLevelIndication;
   156   mp4_u8          AudioProfileLevelIndication;
   157   mp4_u8          VisualProfileLevelIndication;
   158   mp4_u8          GraphicsProfileLevelIndication;
   159 } objectDescriptorAtom;
   160 
   161 
   162 typedef struct trackHeaderAtom
   163 {
   164   atomHeader      *atomhdr;
   165   mp4_u32         creationTime;
   166   mp4_u32         modificationTime;
   167   mp4_u32         trackID;
   168   mp4_u32         reserved;
   169   mp4_u32         duration;
   170   mp4_u64         creationTime64;
   171   mp4_u64         modificationTime64;
   172   mp4_u64         duration64;
   173   mp4_u16         width;
   174   mp4_u16         height;
   175   mp4_bool        isAudio;
   176   mp4_bool        isVisual;
   177 } trackHeaderAtom;
   178 
   179 
   180 typedef struct mediaHeaderAtom
   181 {
   182   atomHeader  *atomhdr;
   183   mp4_u32     creationTime;
   184   mp4_u32     modificationTime;
   185   mp4_u32     timeScale;
   186   mp4_u32     duration;
   187   mp4_u64     creationTime64;
   188   mp4_u64     modificationTime64;
   189   mp4_u64     duration64;
   190 } mediaHeaderAtom;
   191 
   192 
   193 typedef struct handlerAtom
   194 {
   195   atomHeader    *atomhdr;
   196   mp4_u32       handlerType;
   197   mp4_u8        *name;
   198 } handlerAtom;
   199 
   200 
   201 typedef struct dataEntryURLAtom
   202 {
   203   atomHeader  *atomhdr;
   204 } dataEntryURLAtom;
   205 
   206 
   207 typedef struct dataEntryURNAtom
   208 {
   209   atomHeader  *atomhdr;
   210 } dataEntryURNAtom;
   211 
   212 
   213 typedef struct dataReferenceAtom
   214 {
   215   atomHeader        *atomhdr;
   216   mp4_u32           entryCount;
   217   dataEntryURLAtom  *url;
   218   dataEntryURNAtom  *urn;
   219 } dataReferenceAtom;
   220 
   221 
   222 typedef struct dataInformationAtom
   223 {
   224   atomHeader         *atomhdr;
   225   dataReferenceAtom  *dref;
   226 } dataInformationAtom;
   227 
   228 
   229 typedef struct timeToSampleAtom
   230 {
   231   atomHeader  *atomhdr;
   232   mp4_u32      entryCount;
   233   mp4_u32      *sampleCount;
   234   mp4_i32      *sampleDelta;
   235 } timeToSampleAtom;
   236 
   237 
   238 typedef struct compositionTimeToSampleAtom
   239 {
   240   atomHeader      *atomhdr;
   241   mp4_u32         entryCount;
   242   mp4_u32         *sampleCount;
   243   mp4_u32         *sampleOffset;
   244 } compositionTimeToSampleAtom;
   245 
   246 
   247 typedef struct ESDAtom
   248 {
   249   atomHeader  *atomhdr;
   250   mp4_u8      esDescrTag;
   251   mp4_u32     size;
   252   mp4_u16     ESID;
   253   mp4_u8      flags;
   254   mp4_u16     dependsOnESID;
   255   mp4_u8      URLLength;
   256   mp4_u8      *URLString;
   257   mp4_u16     OCRESID;
   258   mp4_u8      decConfDescrTag;
   259   mp4_u32     decConfDescrSize;
   260   mp4_u8      objectTypeIndication;
   261   mp4_u8      stream;
   262   mp4_u32     bufferSizeDB;
   263   mp4_u32     maxBitrate;
   264   mp4_u32     avgBitrate;
   265   mp4_u8      decSpecificInfoTag;
   266   mp4_u32     decSpecificInfoSize;
   267   mp4_u8      *decSpecificInfo;
   268 } ESDAtom;
   269 
   270 
   271 typedef struct visualSampleEntry
   272 {
   273   atomHeader  *atomhdr;
   274   mp4_u16     dataReferenceIndex;
   275   mp4_u16     width;
   276   mp4_u16     height;
   277   ESDAtom     *esd;
   278 } visualSampleEntry;
   279 
   280 
   281 typedef struct audioSampleEntry
   282 {
   283   atomHeader  *atomhdr;
   284   mp4_u16     dataReferenceIndex;
   285   mp4_u16     timeScale;
   286   ESDAtom     *esd;
   287 } audioSampleEntry;
   288 
   289 
   290 typedef struct mpegSampleEntry
   291 {
   292   atomHeader  *atomhdr;
   293   mp4_u16     dataReferenceIndex;
   294   ESDAtom     *esd;
   295 } mpegSampleEntry;
   296 
   297 
   298 typedef struct bitrateAtom
   299 {
   300   atomHeader  *atomhdr;
   301   mp4_u32     avgBitrate;
   302   mp4_u32     maxBitrate;
   303 } bitrateAtom;
   304 
   305 
   306 typedef struct h263SpecificAtom
   307 {
   308   atomHeader  *atomhdr;
   309   mp4_u32     vendor;
   310   mp4_u8      decoderVersion;
   311   mp4_u8      h263Level;
   312   mp4_u8      h263Profile;
   313   bitrateAtom *bitr;
   314 } h263SpecificAtom;
   315 
   316 
   317 typedef struct h263SampleEntry
   318 {
   319   atomHeader        *atomhdr;
   320   mp4_u16           dataReferenceIndex;
   321   mp4_u16           width;
   322   mp4_u16           height;
   323   h263SpecificAtom  *d263;
   324 } h263SampleEntry;
   325 
   326 
   327 typedef struct amrDecSpecStruc
   328 {
   329   atomHeader  *atomhdr;
   330   mp4_u32     vendor;
   331   mp4_u8      decoderVersion;
   332   mp4_u16     modeSet;
   333   mp4_u8      modeChangePeriod;
   334   mp4_u8      framesPerSample;
   335 } amrDecSpecStruc;
   336 
   337 
   338 typedef struct amrSampleEntry
   339 {
   340   atomHeader        *atomhdr;
   341   mp4_u16           dataReferenceIndex;
   342   mp4_u16           timeScale;
   343   amrDecSpecStruc   *damr;
   344 } amrSampleEntry;
   345 
   346 typedef struct avcConfigurationAtom
   347 {
   348   atomHeader		*atomhdr;
   349   mp4_u8				*avcConfig;
   350   mp4_u32				avcConfigSize;
   351 } avcConfigurationAtom;
   352 
   353 typedef struct	mpeg4BitrateAtom
   354 {
   355     atomHeader		*atomhdr;
   356 	mp4_u32		bufferSizeDB;
   357 	mp4_u32		maxBitRate;
   358 	mp4_u32		avgBitrate;
   359 } mpeg4BitrateAtom;
   360 
   361 typedef struct mpeg4ExtensionDescriptorsAtom
   362 {
   363     atomHeader		*atomhdr;
   364 	mp4_u8		*descr;
   365 	mp4_u32		 descrSize;
   366 } mpeg4ExtensionDescriptorsAtom;
   367 
   368 typedef struct avcSampleEntry
   369 {
   370 	atomHeader			*atomhdr;
   371     mp4_u16           dataReferenceIndex;
   372     mp4_u16           width;
   373     mp4_u16           height;
   374 	avcConfigurationAtom	*avcc;
   375 	mpeg4BitrateAtom			*btrt;
   376 	mpeg4ExtensionDescriptorsAtom	*m4ds;
   377 } avcSampleEntry;
   378 
   379 typedef struct qcelpDecSpecStruc
   380 {
   381   atomHeader  *atomhdr;
   382   mp4_u32     vendor;
   383   mp4_u8      decoderVersion;
   384   mp4_u8      framesPerSample;
   385 } qcelpDecSpecStruc;
   386 
   387 typedef struct qcelpSampleEntry
   388 {
   389   atomHeader        *atomhdr;
   390   mp4_u16           dataReferenceIndex;
   391   mp4_u16           timeScale;
   392   qcelpDecSpecStruc   *dqcp;
   393 } qcelpSampleEntry;
   394 
   395 typedef struct avcParameterSampleEntry
   396 {
   397 	
   398 } avcParameterSampleEntry;
   399 
   400 typedef struct sampleDescriptionAtom
   401 {
   402   atomHeader          *atomhdr;
   403   mp4_u32             entryCount;
   404   visualSampleEntry   *mp4v[STSDMAXSAMPLEENTRYCOUNT];
   405   audioSampleEntry    *mp4a[STSDMAXSAMPLEENTRYCOUNT];
   406   mpegSampleEntry     *mp4s[STSDMAXSAMPLEENTRYCOUNT];
   407   h263SampleEntry     *s263[STSDMAXSAMPLEENTRYCOUNT];
   408   amrSampleEntry      *samr[STSDMAXSAMPLEENTRYCOUNT];
   409   amrSampleEntry      *sawb[STSDMAXSAMPLEENTRYCOUNT];
   410   /* AVC addition */
   411   avcSampleEntry	*avc1[STSDMAXSAMPLEENTRYCOUNT];
   412   /* QCELP 13K addition */
   413   qcelpSampleEntry  *sqcp[STSDMAXSAMPLEENTRYCOUNT];
   414   avcParameterSampleEntry *avcp[STSDMAXSAMPLEENTRYCOUNT];
   415 } sampleDescriptionAtom;
   416 
   417 
   418 typedef struct sampleSizeAtom
   419 {
   420   atomHeader  *atomhdr;
   421   mp4_u32     sampleSize;
   422   mp4_u32     sampleCount;
   423   mp4_u32     *entrySize;
   424 } sampleSizeAtom;
   425 
   426 
   427 typedef struct sampleToChunkAtom
   428 {
   429   atomHeader  *atomhdr;
   430   mp4_u32     entryCount;
   431   mp4_u32     *firstChunk;
   432   mp4_u32     *samplesPerChunk;
   433   mp4_u32     *sampleDescriptionIndex;
   434 } sampleToChunkAtom;
   435 
   436 
   437 typedef struct chunkOffsetAtom
   438 {
   439   atomHeader  *atomhdr;
   440   mp4_u32     entryCount;
   441   mp4_u32     *chunkOffset;
   442 } chunkOffsetAtom;
   443 
   444 typedef struct chunkOffset64Atom
   445 {
   446   atomHeader  *atomhdr;
   447   mp4_u32     entryCount;
   448   mp4_u64     *chunkOffset;
   449 } chunkOffset64Atom;
   450 
   451 
   452 typedef struct syncSampleAtom
   453 {
   454   atomHeader  *atomhdr;
   455   mp4_u32     entryCount;
   456   mp4_u32     *sampleNumber;
   457 } syncSampleAtom;
   458 
   459 
   460 typedef struct shadowSyncSampleAtom
   461 {
   462   atomHeader  *atomhdr;
   463 } shadowSyncSampleAtom;
   464 
   465 typedef struct sampleDependency
   466 {
   467 	mp4_u8  sDependsOn;
   468 	mp4_u8  sIsDependentOn;
   469 	mp4_u8  sHasRedundancy;
   470 } sampleDependency;
   471 
   472 typedef struct sampleDependencyAtom
   473 {
   474 	atomHeader        *atomhdr;
   475 	mp4_u32           sampleCount;
   476 	sampleDependency  *dep;
   477 } sampleDependencyAtom;
   478 
   479 typedef struct sampleTableAtom
   480 {
   481   atomHeader                    *atomhdr;
   482   timeToSampleAtom              *stts;
   483   compositionTimeToSampleAtom   *ctts;
   484   sampleDescriptionAtom         *stsd;
   485   sampleSizeAtom                *stsz;
   486   sampleToChunkAtom             *stsc;
   487   mp4_bool 						is32BitOffsets;
   488   union 
   489   {
   490   	chunkOffsetAtom             *stco;
   491   	chunkOffset64Atom           *stco64;
   492   };
   493   syncSampleAtom                *stss;
   494   shadowSyncSampleAtom          *stsh;
   495   sampleDependencyAtom          *sdtp;
   496 } sampleTableAtom;
   497 
   498 
   499 typedef struct videoMediaHeaderAtom
   500 {
   501   atomHeader  *atomhdr;
   502 } videoMediaHeaderAtom;
   503 
   504 
   505 typedef struct soundMediaHeaderAtom
   506 {
   507   atomHeader  *atomhdr;
   508 } soundMediaHeaderAtom;
   509 
   510 
   511 typedef struct mediaInformationAtom
   512 {
   513   atomHeader            *atomhdr;
   514   videoMediaHeaderAtom  *vmhd;
   515   soundMediaHeaderAtom  *smhd;
   516   dataInformationAtom   *dinf;
   517   sampleTableAtom       *stbl;
   518 } mediaInformationAtom;
   519 
   520 
   521 typedef struct mediaAtom
   522 {
   523   atomHeader            *atomhdr;
   524   mediaHeaderAtom       *mdhd;
   525   handlerAtom           *hdlr;
   526   mediaInformationAtom  *minf;
   527 } mediaAtom;
   528 
   529 
   530 typedef struct trackReferenceAtom
   531 {
   532   atomHeader  *atomhdr;
   533 } trackReferenceAtom;
   534 
   535 
   536 typedef struct editListContainerAtom
   537 {
   538   atomHeader  *atomhdr;
   539 } editListContainerAtom;
   540 
   541 
   542 typedef struct copyrightAtom
   543 {
   544   void *p;
   545 } copyrightAtom;
   546 
   547 typedef struct userDataAtom
   548 {
   549   atomHeader    *atomhdr;
   550   mp4_u64       atomcontentloc;   //used for parsing
   551   mp4_u32       atomcontentsize;  //used for parsing and composing
   552   mp4_u8        *contentdata;     //used for composing
   553 } userDataAtom;
   554 
   555 typedef struct trackAtom
   556 {
   557   atomHeader             *atomhdr;
   558   trackHeaderAtom        *tkhd;
   559   trackReferenceAtom     *tref;
   560   editListContainerAtom  *edts;
   561   mediaAtom              *mdia;
   562   userDataAtom           *udta;
   563 } trackAtom;
   564 
   565 typedef struct ID32Atom
   566 {
   567   atomHeader *atomhdr;
   568   mp4_u64    atomcontentloc;
   569   mp4_u16    language; // top bit is padding, remaining 15 bits is Packed ISO-639-2/T language code 
   570 } ID32Atom;
   571 
   572 typedef struct metaAtom
   573 {
   574   atomHeader  *atomhdr;
   575   handlerAtom *hdlr;
   576   ID32Atom    *ID32;
   577 } metaAtom;
   578 
   579 typedef struct movieAtom
   580 {
   581   atomHeader            *atomhdr;
   582   movieHeaderAtom       *mvhd;
   583   trackAtom             *trakAudio; // audio
   584   trackAtom             *trakVideo; // video
   585   objectDescriptorAtom  *iods;
   586   userDataAtom          *udta;
   587   metaAtom              *meta;
   588 } movieAtom;
   589 
   590 
   591 typedef struct mediaDataAtom
   592 {
   593   atomHeader  *atomhdr;
   594   mp4_u8      *data;
   595 } mediaDataAtom;
   596 
   597 
   598 typedef struct sampleTable
   599 {
   600   mp4_u32    currentChunk;
   601 
   602   /* Decoding time to sample */
   603   mp4_u32    sttsEntryCount;
   604   mp4_u32    *sttsSampleCount;
   605   mp4_u32    *sttsSampleDelta;
   606   mp4_u32    sttsMaxEntryCount;
   607   mp4_u32    sttsCurrentEntryCount;
   608 
   609   /* Sample size */
   610   mp4_u32    stszSampleSize;
   611   mp4_u32    stszSampleCount;
   612   mp4_u32    *stszEntrySize;
   613   mp4_u32    stszMaxSampleCount;
   614   mp4_u32    stszCurrentSampleCount;
   615 
   616   /* Sample to chunk */
   617   mp4_u32    stscEntryCount;
   618   mp4_u32    *stscFirstChunk;
   619   mp4_u32    *stscSamplesPerChunk;
   620   mp4_u32    *stscSampleDescriptionIndex;
   621   mp4_u32    stscMaxEntryCount;
   622   mp4_u32    stscCurrentEntryCount;
   623 
   624   /* Chunk offset */
   625   mp4_u32    stcoEntryCount;
   626   mp4_u64    *stcoChunkOffset;
   627   mp4_u32    stcoMaxEntryCount;
   628   mp4_u32    stcoCurrentEntryCount;
   629   mp4_bool   stcoNeed64Bits;
   630     
   631   /* Sync sample */
   632   mp4_u32    stssEntryCount;
   633   mp4_u32    *stssSampleNumber;
   634   mp4_u32    stssMaxEntryCount;
   635   mp4_u32    stssCurrentEntryCount;
   636   /* Sample Dependency */
   637   mp4_u32    sdtpEntryCount;
   638   mp4_u8    *sdtpSampleDependency;
   639   mp4_u32    sdtpMaxEntryCount;
   640   mp4_u32    sdtpCurrentEntryCount;
   641 } sampleTable;
   642 
   643 
   644 typedef struct MP4HandleStruct
   645 {
   646   mp4_u8        sourceType;                /* Determines the type of the source file (CAF or RFile64) */
   647   void          *file;                     /* FILE * of possible file */
   648   void          *fs;                       /* File server */
   649   void          *rfile;                    /* File handle */
   650   ContentAccess::CData *cfile;             /* used when reading via the CAF and not the file based interfaces */
   651   mp4_i32		cafError;				   /* variable to store possible CAF error to be reported to client */
   652   void          *tmpfile;                  /* FILE * of temporary output file */
   653   void          *metaDataFile[NUM_MDF];    /* File handles for metadata files */
   654   MP4FileName   metaDataFileName[NUM_MDF]; /* Names of the temporary meta data files */
   655   MP4FileName   fileName;                  /* Name of the file */
   656   MP4FileName   tmpFileName;               /* Name of the temporary file */
   657   mp4_u64       bytesInTmpFile;            /* Number of bytes in temporary file */
   658   mp4_u64       mediaDataBytes;            /* Number of media data bytes */
   659   mp4_bool      ftypWritten;               /* Has FTYP been written */
   660   mp4_u32       flags;                     /* Composing flags */
   661   mp4_u32       type;                      /* Type of audio/video */
   662   list_s        *mem;                      /* Memory list */
   663   mp4_u32       position;                  /* Current position in memory */
   664   mp4_u32       absPosition;               /* Current absolute position in memory */
   665   mp4_u64       lastAccessedPosInFile;     /* Last accessed position in a file */
   666   mp4_u8        *buf;                      /* Temporary buffer space */
   667   mp4_u8        *diskReadBuf;              /* Memory for file buffering */
   668   mp4_u32       diskReadPos;               /* Reading position in the file */
   669   mp4_u64       diskReadBufStart;          /* Position of the start of the buffer */
   670   mp4_u32       diskReadBufPos;            /* Reading position inside buffer */
   671   mp4_u32       diskReadSize;              /* Number of bytes in buffer */
   672   mp4_u8        *diskWriteBuf;             /* Memory for file buffering */
   673   mp4_bool      ftypRead;                  /* FTYP atom has been read */
   674   fileTypeAtom  *ftyp;                     /* Pointer to file type atom structure */
   675   mp4_bool      metaDataComplete;          /* Metadata has been completeley read */
   676   movieAtom     *moov;                     /* Pointer to movie atom structure */
   677   mp4_u32       videoSampleNum;            /* Current video sample number */
   678   mp4_u64       videoFrameOffset;          /* Absolute position of current video frame */
   679   mp4_u32       videoFrameSize;            /* Size of current video frame in bytes */
   680   mp4_bool      videoLast;                 /* Has last video sample been read */
   681   mp4_u32       videoTimeScale;            /* Video timescale */
   682   mp4_u64       videoDuration;             /* Duration of video when composing */
   683   mp4_u16       videoWidth;                /* Video image width */
   684   mp4_u16       videoHeight;               /* Video image height */
   685   mp4_u32       videoMaxBitrate;           /* MPEG-4 video maximum bitrate */
   686   mp4_u32       videoAvgBitrate;           /* MPEG-4 video average bitrate */
   687   mp4_u8        *videoDecSpecificInfo;     /* MPEG-4 video DecoderSpecificInfo */
   688   mp4_u32       videoDecSpecificInfoSize;  /* MPEG-4 video DecoderSpecificInfo size in bytes */
   689   sampleTable   *videoSampleTable;         /* Video sample table */
   690   mp4_u8        videoLevel;                /* Video codec level, default is H.263 level 10 */
   691   mp4_u32       audioSampleNum;            /* Current audio sample number */
   692   mp4_u32       audioSampleSize;           /* Size of current audio sample in bytes */
   693   mp4_u64       audioSampleOffset;         /* Absolute position of current audio frame (in MP4 file) */
   694   mp4_u32       audioFrameCount;           /* Number of audio frames when composing */
   695   mp4_u32       audioTimeScale;            /* Audio timescale */
   696   mp4_u64       audioDuration;             /* Duration of audio when composing */
   697   mp4_bool      audioLast;                 /* Has last audio sample been read */
   698   mp4_u8        *audioDecSpecificInfo;     /* MPEG audio DecoderSpecificInfo */
   699   mp4_u32       audioDecSpecificInfoSize;  /* MPEG audio DecoderSpecificInfo size in bytes */
   700   sampleTable   *audioSampleTable;         /* Audio sample table */
   701   mp4_u8        audioFramesPerSample;      /* AMR frames per sample value */
   702   mp4_u16       audioModeSet;              /* AMR mode set */
   703   mp4_u32       metaDataBlocks;            /* Number of metadata blocks in memory */
   704   mp4_bool      metaDataOnDisk;            /* Is (a part of) metadata on the disk */
   705   mp4_u32       metaDataSize;              /* Size of metadata */
   706   mp4_u32		videoSampleEntryIndex;	   /* SampleEntry index of the video frame being read */
   707   mp4_u32		audioSampleEntryIndex;     /* SampleEntry index of the audio frame being read */
   708   mp4_u8        avcNalUnitLengthSize;      /* byte size of the length field */
   709   mp4_bool      generate3G2;               /* 3GPP2 File Format Generation is Selected: default:FALSE (Generate 3GP)*/
   710   mp4_bool      generateMP4;               /* MPEG-4 File Format Generation is Selected: defailt:FALSE (Generate 3GP)*/
   711   mp4_bool      qcelpStoredAsMPEGAudio;    /* QCELP 13K is registered as MPEG4 Audio: default: FALSE (in QCELPSampleEntry)*/
   712   mp4_u64       audioMediaDataSize;        /* Total byte size of the written audio data. This is used to calculate the max and avg bitrates */
   713   mp4_u32       ES_DescriptorSize;         /* size of the ES_Descriptor inside the ESDS box */ 
   714 
   715   // composeToBuffer:
   716   mp4_u8 *composeBuffer;
   717   mp4_bool bufferWrite;
   718   mp4_u32 bytesProgressed;
   719   mp4_u32 ftypdelta;
   720   mp4_u32 *composedSize;
   721 
   722   CFileWriter*  filewriter;                /* Async buffering filewriter */
   723   CMetaDataFileWriter* metadatafilewriter;  /* Async buffering metadata temp file writer */
   724 
   725   mp4_bool      LastWriteDataCalled;       /* When TRUE, it indicates that ParseWriteData() function is called for the last time */ 
   726   mp4_bool      FileHandleFromOutside;     /* When TRUE file handle is passed to library instead of file name, don't close file in end */
   727 
   728   mp4_bool      metaDataFileEmpty[NUM_MDF]; /* when TRUE it indicates that metadata file has been read and rest of data is in readbuffer of metadatafilewriter */
   729   
   730   userDataAtom* moovUDTA;   // used to compose MOOV UDTA (User Data) 
   731   userDataAtom* audioUDTA;  // used to compose AudioTrack UDTA (User Data) 
   732   userDataAtom* videoUDTA;  // used to compose VideoTrack UDTA (User Data)
   733   
   734   TDriveNumber   fileHandleDrive;  /* Used when we get file handle to library from outside. Used to indicate which drive are metadata temp files written.*/
   735   
   736   // Custom buffer size additions:
   737   mp4_u32 mediaWriteBufferSize;
   738   mp4_u32 metaWriteBufferSize;
   739   mp4_u32 writeBufferMaxCount;
   740   mp4_u32 readBufferSize;
   741   
   742   // Async parsing observer
   743   M3GPMP4LibAsyncObserver* asyncObserver;
   744   CFileAsyncParser* asyncReader;
   745   M3GPMP4LibAsyncTempFileRemoverObserver* tempFileRemoverObserver;
   746   
   747   void *file32Duplicate; //this is a RFile64 duplicate of the RFile used in Open 
   748 } MP4HandleStruct;
   749 
   750 
   751 typedef MP4HandleStruct *MP4HandleImp;
   752 
   753 
   754 mp4_i32 metaDataAvailable(MP4HandleImp handle);
   755 mp4_i32 readFTYP(MP4HandleImp handle, fileTypeAtom *ftyp);
   756 mp4_i32 readMetaData(MP4HandleImp handle);
   757 mp4_i32 readMoov(MP4HandleImp handle, movieAtom *moov);
   758 mp4_i32 readAtomHeader(MP4HandleImp handle, atomHeader *ah);
   759 mp4_i32 readFullAtomHeader(MP4HandleImp handle, atomHeader *ah);
   760 mp4_i32 readMVHD(MP4HandleImp handle, movieHeaderAtom *mvhd);
   761 mp4_i32 readIODS(MP4HandleImp handle, objectDescriptorAtom *iods);
   762 mp4_i32 readTRAK(MP4HandleImp handle, trackAtom *trak);
   763 mp4_i32 readUnknown(MP4HandleImp handle);
   764 mp4_i32 readTKHD(MP4HandleImp handle, trackHeaderAtom *tkhd);
   765 mp4_i32 readTREF(MP4HandleImp handle, trackReferenceAtom *tref);
   766 mp4_i32 readEDTS(MP4HandleImp handle, editListContainerAtom *edts);
   767 mp4_i32 readMDIA(MP4HandleImp handle, mediaAtom *mdia);
   768 mp4_i32 readMDHD(MP4HandleImp handle, mediaHeaderAtom *mdhd);
   769 mp4_i32 readHDLR(MP4HandleImp handle, handlerAtom *hdlr);
   770 mp4_i32 readMINF(MP4HandleImp handle, mediaInformationAtom *minf);
   771 mp4_i32 readVMHD(MP4HandleImp handle, videoMediaHeaderAtom *vmhd);
   772 mp4_i32 readSMHD(MP4HandleImp handle, soundMediaHeaderAtom *smhd);
   773 mp4_i32 readDINF(MP4HandleImp handle, dataInformationAtom *dinf);
   774 mp4_i32 readDREF(MP4HandleImp handle, dataReferenceAtom *dref);
   775 mp4_i32 readURL(MP4HandleImp handle, dataEntryURLAtom *url);
   776 mp4_i32 readURN(MP4HandleImp handle, dataEntryURNAtom *urn);
   777 mp4_i32 readSTBL(MP4HandleImp handle, sampleTableAtom *stbl);
   778 mp4_i32 readSTTS(MP4HandleImp handle, timeToSampleAtom *stts);
   779 mp4_i32 readCTTS(MP4HandleImp handle, compositionTimeToSampleAtom *ctts);
   780 mp4_i32 readSTSS(MP4HandleImp handle, syncSampleAtom *stss);
   781 mp4_i32 readSTSD(MP4HandleImp handle, sampleDescriptionAtom *stsd);
   782 mp4_i32 readSTSZ(MP4HandleImp handle, sampleSizeAtom *stsz);
   783 mp4_i32 readSTZ2(MP4HandleImp handle, sampleSizeAtom *stsz);
   784 mp4_i32 readSTSC(MP4HandleImp handle, sampleToChunkAtom *stsc);
   785 mp4_i32 readSTCO(MP4HandleImp handle, chunkOffsetAtom *stco);
   786 mp4_i32 readCO64(MP4HandleImp handle, chunkOffset64Atom *stco64);
   787 mp4_i32 readMP4V(MP4HandleImp handle, visualSampleEntry *mp4v);
   788 mp4_i32 readMP4A(MP4HandleImp handle, audioSampleEntry *mp4a);
   789 mp4_i32 readMP4S(MP4HandleImp handle, mpegSampleEntry *mp4s);
   790 mp4_i32 readS263(MP4HandleImp handle, h263SampleEntry *s263);
   791 mp4_i32 readSAMR(MP4HandleImp handle, amrSampleEntry *samr);
   792 mp4_i32 readSAWB(MP4HandleImp handle, amrSampleEntry *sawb);
   793 mp4_i32 readESD(MP4HandleImp handle, ESDAtom *esd);
   794 mp4_i32 readD263(MP4HandleImp handle, h263SpecificAtom *d263);
   795 mp4_i32 readBITR(MP4HandleImp handle, bitrateAtom *bitr);
   796 mp4_i32 readDAMR(MP4HandleImp handle, amrDecSpecStruc *damr);
   797 mp4_i32 readMeta(MP4HandleImp handle, metaAtom *meta);
   798 /* avc inclusions*/
   799 mp4_i32 readAVC1(MP4HandleImp handle, avcSampleEntry *avc1);
   800 mp4_i32 readSDTP(MP4HandleImp handle, sampleDependencyAtom *sdtp,  mp4_i32 sample_count);
   801 /* QCELP 13k inclusions */
   802 mp4_i32 readSQCP(MP4HandleImp handle, qcelpSampleEntry *sqcp);
   803 mp4_i32 readDQCP(MP4HandleImp handle, qcelpDecSpecStruc *dqcp);
   804 /* inline related */
   805 mp4_i32 readID32(MP4HandleImp handle, ID32Atom *ID32);
   806 
   807 mp4_i32 freeFTYP(fileTypeAtom *ftyp);
   808 mp4_i32 freeMOOV(movieAtom *moov);
   809 mp4_i32 freeAtomHeader(atomHeader *atomhdr);
   810 mp4_i32 freeMVHD(movieHeaderAtom *mvhd);
   811 mp4_i32 freeTRAK(trackAtom *trak);
   812 mp4_i32 freeTKHD(trackHeaderAtom *tkhd);
   813 mp4_i32 freeTREF(trackReferenceAtom *tref);
   814 mp4_i32 freeEDTS(editListContainerAtom *edts);
   815 mp4_i32 freeMDIA(mediaAtom *mdia);
   816 mp4_i32 freeMDHD(mediaHeaderAtom *mdhd);
   817 mp4_i32 freeHDLR(handlerAtom *hdlr);
   818 mp4_i32 freeMINF(mediaInformationAtom *minf);
   819 mp4_i32 freeVMHD(videoMediaHeaderAtom *vmhd);
   820 mp4_i32 freeSMHD(soundMediaHeaderAtom *smhd);
   821 mp4_i32 freeDINF(dataInformationAtom *dinf);
   822 mp4_i32 freeDREF(dataReferenceAtom *dref);
   823 mp4_i32 freeURL(dataEntryURLAtom *url);
   824 mp4_i32 freeURN(dataEntryURNAtom *urn);
   825 mp4_i32 freeSTBL(sampleTableAtom *stbl);
   826 mp4_i32 freeSTTS(timeToSampleAtom *stts);
   827 mp4_i32 freeCTTS(compositionTimeToSampleAtom *ctts);
   828 mp4_i32 freeSTSD(sampleDescriptionAtom *stsd);
   829 mp4_i32 freeMP4V(visualSampleEntry *mp4v);
   830 mp4_i32 freeESD(ESDAtom *esd);
   831 mp4_i32 freeMP4A(audioSampleEntry *mp4a);
   832 mp4_i32 freeMP4S(mpegSampleEntry *mp4s);
   833 mp4_i32 freeS263(h263SampleEntry *s263);
   834 mp4_i32 freeD263(h263SpecificAtom *d263);
   835 mp4_i32 freeBITR(bitrateAtom *bitr);
   836 mp4_i32 freeSAMR(amrSampleEntry *samr);
   837 mp4_i32 freeSAWB(amrSampleEntry *sawb);
   838 mp4_i32 freeDAMR(amrDecSpecStruc *damr);
   839 mp4_i32 freeSTSZ(sampleSizeAtom *stsz);
   840 mp4_i32 freeSTSC(sampleToChunkAtom *stsc);
   841 mp4_i32 freeSTCO(chunkOffsetAtom *stco);
   842 mp4_i32 freeSTCO64(chunkOffset64Atom *stco64);
   843 mp4_i32 freeSTSS(syncSampleAtom *stss);
   844 mp4_i32 freeSTSH(shadowSyncSampleAtom *stsh);
   845 mp4_i32 freeIODS(objectDescriptorAtom *iods);
   846 mp4_i32 readUDTA(MP4HandleImp handle, userDataAtom *udta);
   847 mp4_i32 freeUDTA(userDataAtom *udta);
   848 mp4_i32 freeSDTP(sampleDependencyAtom* sdtp);
   849 mp4_i32 freeMETA(metaAtom *meta);
   850 /* The following and AVC related */
   851 mp4_i32 freeAVC1(avcSampleEntry *avc1);
   852 mp4_i32 freeM4DS(mpeg4ExtensionDescriptorsAtom *m4ds);
   853 mp4_i32 freeBTRT(mpeg4BitrateAtom *btrt);
   854 mp4_i32 freeAVCC(avcConfigurationAtom *avcc);
   855 /* QCELP 13K related */
   856 mp4_i32 freeSQCP(qcelpSampleEntry *sqcp);
   857 mp4_i32 freeDQCP(qcelpDecSpecStruc *dqcp);
   858 /* inline related */
   859 mp4_i32 freeID32(ID32Atom *ID32);
   860 
   861 mp4_i32 determineVideoLength(MP4HandleImp handle, mp4_u32 *videolength);
   862 mp4_i32 determineFrameRate(MP4HandleImp handle, mp4_double *framerate);
   863 mp4_i32 determineVideoType(MP4HandleImp handle, mp4_u32 *videotype);
   864 mp4_i32 determineVideoResolution(MP4HandleImp handle, mp4_u32 *videowidth, mp4_u32 *videoheight);
   865 mp4_i32 determineVideoTimeScale(MP4HandleImp handle, mp4_u32 *timescale);
   866 mp4_i32 determineAudioLength(MP4HandleImp handle, mp4_u32 *audiolength);
   867 mp4_i32 determineAudioType(MP4HandleImp handle, mp4_u32 *audiotype);
   868 mp4_i32 determineAudioFramesPerSample(MP4HandleImp handle, mp4_u8 *framespersample);
   869 mp4_i32 determineAudioTimeScale(MP4HandleImp handle, mp4_u32 *timescale);
   870 mp4_i32 determineAudioAverageBitRate(MP4HandleImp handle, mp4_u32 *averagebitrate);
   871 mp4_i32 determineStreamSize(MP4HandleImp handle, mp4_u32 *streamsize);
   872 mp4_i32 determineStreamAverageBitRate(MP4HandleImp handle, mp4_u32 *streamaveragebitrate, mp4_u32 streamsize);
   873 mp4_i32 advanceVideoFrame(MP4HandleImp handle, trackAtom *trak);
   874 mp4_i32 resolveVideoSampleOffset(MP4HandleImp handle, sampleTableAtom *stbl);
   875 mp4_i32 resolveVideoSampleSize(MP4HandleImp handle, sampleSizeAtom *stsz);
   876 mp4_i32 fetchVideoFrame(MP4HandleImp handle, trackAtom *trak, mp4_u8 *buffer, mp4_u32 buffersize, mp4_u32 *framesize, mp4_u32 *framenumber, mp4_bool *keyframe, mp4_u32 *timestamp2);
   877 mp4_i32 isVideoFrameKeyFrame(MP4HandleImp handle, trackAtom *trak, mp4_bool *keyframe);
   878 mp4_i32 convertVideoSampleToTime(MP4HandleImp handle, mediaAtom *mdia, mp4_u32 *framenumber, mp4_u32 *timestamp2);
   879 mp4_i32 advanceAudioSample(MP4HandleImp handle, trackAtom *trak);
   880 mp4_i32 resolveAudioSampleOffset(MP4HandleImp handle, sampleTableAtom *stbl);
   881 mp4_i32 resolveAudioSampleSize(MP4HandleImp handle, sampleSizeAtom *stsz);
   882 mp4_i32 fetchAudioSample(MP4HandleImp handle, trackAtom *trak, mp4_u8 *buffer, mp4_u32 buffersize, mp4_u32 *framesize, mp4_u32 *timestamp, mp4_u32 *returnedframes, mp4_u32 *timestamp2);
   883 mp4_i32 convertAudioSampleToTime(MP4HandleImp handle, mediaAtom *mdia, mp4_u32 *timestamp, mp4_u32 *timestamp2);
   884 mp4_i32 convertTimeToSample(MP4HandleImp handle, trackAtom *trak, mp4_u32 position, mp4_u32 *sample);
   885 mp4_i32 goToVideoSample(MP4HandleImp handle, trackAtom *trak, mp4_u32 sample);
   886 mp4_i32 goToAudioSample(MP4HandleImp handle, trackAtom *trak, mp4_u32 sample);
   887 mp4_i32 findVideoKeyFrame(MP4HandleImp handle, trackAtom *trak, mp4_u32 sample, mp4_u32 *newsample);
   888 mp4_i32 fetchAudioSampleAsync(MP4HandleImp handle, trackAtom *trak, mp4_u8 *buffer, mp4_u32* buffersize);
   889 mp4_i32 fetchVideoFrameAsync(MP4HandleImp handle, trackAtom *trak, mp4_u8 *buffer, mp4_u32* buffersize);
   890 mp4_i64 getChunkOffset(sampleTableAtom *stbl, mp4_u32 index); 
   891 #endif
   892 
   893  
   894 // End of File