-
Notifications
You must be signed in to change notification settings - Fork 113
/
GPMF_mp4reader.c
1438 lines (1223 loc) · 40 KB
/
GPMF_mp4reader.c
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
/*! @file mp4reader.c
*
* @brief Way Too Crude MP4|MOV reader
*
* @version 1.7.3
*
* (C) Copyright 2017-2020 GoPro Inc (http://gopro.com/).
*
* Licensed under either:
* - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
* - MIT license, http://opensource.org/licenses/MIT
* at your option.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* This is not an elegant MP4 parser, only used to help demonstrate extraction of GPMF */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "GPMF_mp4reader.h"
#include "../GPMF_common.h"
#define PRINT_MP4_STRUCTURE 0
#ifdef _WINDOWS
#define LONGTELL _ftelli64
#else
#define LONGTELL ftell
#endif
uint32_t GetNumberPayloads(size_t handle)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4)
{
return mp4->indexcount;
}
return 0;
}
uint32_t *GetPayload(size_t handle, uint32_t *lastpayload, uint32_t index)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return NULL;
uint32_t *MP4buffer = NULL;
if (index < mp4->indexcount && mp4->mediafp)
{
if ((mp4->filesize >= mp4->metaoffsets[index]+mp4->metasizes[index]) && (mp4->metasizes[index] > 0))
{
MP4buffer = (uint32_t *)realloc((void *)lastpayload, mp4->metasizes[index]);
if (MP4buffer)
{
#ifdef _WINDOWS
_fseeki64(mp4->mediafp, (__int64) mp4->metaoffsets[index], SEEK_SET);
#else
fseeko(mp4->mediafp, (off_t) mp4->metaoffsets[index], SEEK_SET);
#endif
fread(MP4buffer, 1, mp4->metasizes[index], mp4->mediafp);
mp4->filepos = mp4->metaoffsets[index] + mp4->metasizes[index];
return MP4buffer;
}
}
}
if (lastpayload)
free(lastpayload);
return NULL;
}
uint32_t WritePayload(size_t handle, uint32_t *payload, uint32_t payloadsize, uint32_t index)
{
mp4object* mp4 = (mp4object*)handle;
if (mp4 == NULL) return 0;
if (index < mp4->indexcount && mp4->mediafp)
{
if ((mp4->filesize >= mp4->metaoffsets[index] + mp4->metasizes[index]) && mp4->metasizes[index] == payloadsize)
{
#ifdef _WINDOWS
_fseeki64(mp4->mediafp, (__int64)mp4->metaoffsets[index], SEEK_SET);
#else
fseeko(mp4->mediafp, (off_t)mp4->metaoffsets[index], SEEK_SET);
#endif
fwrite(payload, 1, payloadsize, mp4->mediafp);
mp4->filepos = mp4->metaoffsets[index] + payloadsize;
return payloadsize;
}
}
return 0;
}
void LongSeek(mp4object *mp4, int64_t offset)
{
if (mp4 && offset)
{
if (mp4->filepos + offset < mp4->filesize)
{
#ifdef _WINDOWS
_fseeki64(mp4->mediafp, (__int64)offset, SEEK_CUR);
#else
fseeko(mp4->mediafp, (off_t)offset, SEEK_CUR);
#endif
mp4->filepos += offset;
}
else
{
mp4->filepos = mp4->filesize;
}
}
}
void FreePayload(uint32_t *lastpayload)
{
if (lastpayload)
free(lastpayload);
}
uint32_t GetPayloadSize(size_t handle, uint32_t index)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return 0;
if (mp4->metasizes && mp4->metasize_count > index)
return mp4->metasizes[index] & (uint32_t)~0x3; //All GPMF payloads are 32-bit aligned and sized
return 0;
}
#define MAX_NEST_LEVEL 20
size_t OpenMP4Source(char *filename, uint32_t traktype, uint32_t traksubtype) //RAW or within MP4
{
mp4object *mp4 = (mp4object *)malloc(sizeof(mp4object));
if (mp4 == NULL) return 0;
memset(mp4, 0, sizeof(mp4object));
#ifdef _WINDOWS
struct _stat64 mp4stat;
_stat64(filename, &mp4stat);
#else
struct stat mp4stat;
stat(filename, &mp4stat);
#endif
mp4->filesize = (uint64_t) mp4stat.st_size;
// printf("filesize = %ld\n", mp4->filesize);
if (mp4->filesize < 64) return 0;
#ifdef _WINDOWS
fopen_s(&mp4->mediafp, filename, "rb+");
#else
mp4->mediafp = fopen(filename, "rb+");
#endif
if (mp4->mediafp)
{
uint32_t qttag, qtsize32, skip, type = 0, subtype = 0, num;
size_t len;
int32_t nest = 0;
uint64_t nestsize[MAX_NEST_LEVEL] = { 0 };
uint64_t lastsize = 0, qtsize;
do
{
len = fread(&qtsize32, 1, 4, mp4->mediafp);
len += fread(&qttag, 1, 4, mp4->mediafp);
mp4->filepos += len;
if (len == 8 && mp4->filepos < mp4->filesize)
{
if (mp4->filepos == 8 && qttag != MAKEID('f', 't', 'y', 'p'))
{
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
if (!GPMF_VALID_FOURCC(qttag))
{
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
qtsize32 = BYTESWAP32(qtsize32);
if (qtsize32 == 1) // 64-bit Atom
{
len = fread(&qtsize, 1, 8, mp4->mediafp);
mp4->filepos += len;
qtsize = BYTESWAP64(qtsize) - 8;
}
else
qtsize = qtsize32;
if(qtsize-len > (mp4->filesize - mp4->filepos)) // not parser truncated files.
{
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
nest++;
if (qtsize < 8) break;
if (nest >= MAX_NEST_LEVEL) break;
nestsize[nest] = qtsize;
lastsize = qtsize;
#if PRINT_MP4_STRUCTURE
for (int i = 1; i < nest; i++) printf(" ");
printf("%c%c%c%c (%lld)\n", (qttag & 0xff), ((qttag >> 8) & 0xff), ((qttag >> 16) & 0xff), ((qttag >> 24) & 0xff), qtsize);
if (qttag == MAKEID('m', 'd', 'a', 't') ||
qttag == MAKEID('f', 't', 'y', 'p') ||
qttag == MAKEID('u', 'd', 't', 'a') ||
qttag == MAKEID('f', 'r', 'e', 'e'))
{
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
continue;
}
#endif
if (qttag != MAKEID('m', 'o', 'o', 'v') && //skip over all but these atoms
qttag != MAKEID('m', 'v', 'h', 'd') &&
qttag != MAKEID('t', 'r', 'a', 'k') &&
qttag != MAKEID('m', 'd', 'i', 'a') &&
qttag != MAKEID('m', 'd', 'h', 'd') &&
qttag != MAKEID('m', 'i', 'n', 'f') &&
qttag != MAKEID('g', 'm', 'i', 'n') &&
qttag != MAKEID('d', 'i', 'n', 'f') &&
qttag != MAKEID('a', 'l', 'i', 's') &&
qttag != MAKEID('s', 't', 's', 'd') &&
qttag != MAKEID('s', 't', 'b', 'l') &&
qttag != MAKEID('s', 't', 't', 's') &&
qttag != MAKEID('s', 't', 's', 'c') &&
qttag != MAKEID('s', 't', 's', 'z') &&
qttag != MAKEID('s', 't', 'c', 'o') &&
qttag != MAKEID('c', 'o', '6', '4') &&
qttag != MAKEID('h', 'd', 'l', 'r') &&
qttag != MAKEID('e', 'd', 't', 's'))
{
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else
if (qttag == MAKEID('m', 'v', 'h', 'd')) //mvhd movie header
{
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&skip, 1, 4, mp4->mediafp);
len += fread(&skip, 1, 4, mp4->mediafp);
len += fread(&mp4->clockdemon, 1, 4, mp4->mediafp); mp4->clockdemon = BYTESWAP32(mp4->clockdemon);
len += fread(&mp4->clockcount, 1, 4, mp4->mediafp); mp4->clockcount = BYTESWAP32(mp4->clockcount);
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over mvhd
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('t', 'r', 'a', 'k')) //trak header
{
if (mp4->trak_num+1 < MAX_TRACKS)
mp4->trak_num++;
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('m', 'd', 'h', 'd')) //mdhd media header
{
media_header md;
len = fread(&md, 1, sizeof(md), mp4->mediafp);
if (len == sizeof(md))
{
md.creation_time = BYTESWAP32(md.creation_time);
md.modification_time = BYTESWAP32(md.modification_time);
md.time_scale = BYTESWAP32(md.time_scale);
md.duration = BYTESWAP32(md.duration);
mp4->trak_clockdemon = md.time_scale;
mp4->trak_clockcount = md.duration;
if (mp4->videolength == 0.0) // Get the video length from the first track
{
mp4->videolength = (float)((double)mp4->trak_clockcount / (double)mp4->trak_clockdemon);
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over mvhd
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('h', 'd', 'l', 'r')) //hldr
{
uint32_t temp;
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&skip, 1, 4, mp4->mediafp);
len += fread(&temp, 1, 4, mp4->mediafp); // type will be 'meta' for the correct trak.
if (temp != MAKEID('a', 'l', 'i', 's') && temp != MAKEID('u', 'r', 'l', ' '))
type = temp;
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over hldr
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('e', 'd', 't', 's')) //edit list
{
uint32_t elst,temp,readnum,i;
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&elst, 1, 4, mp4->mediafp);
if (elst == MAKEID('e', 'l', 's', 't'))
{
len += fread(&temp, 1, 4, mp4->mediafp);
if (temp == 0)
{
len += fread(&readnum, 1, 4, mp4->mediafp);
readnum = BYTESWAP32(readnum);
if (readnum <= ((qtsize - 8 - len) / 12))
{
int32_t segment_duration; //integer that specifies the duration of this edit segment in units of the movies time scale.
int32_t segment_mediaTime; //integer containing the starting time within the media of this edit segment(in media timescale units).If this field is set to 1, it is an empty edit.The last edit in a track should never be an empty edit.Any difference between the movies duration and the tracks duration is expressed as an implicit empty edit.
int32_t segment_mediaRate; //point number that specifies the relative rate at which to play the media corresponding to this edit segment.This rate value cannot be 0 or negative.
for (i = 0; i < readnum; i++)
{
len += fread(&segment_duration, 1, 4, mp4->mediafp);
len += fread(&segment_mediaTime, 1, 4, mp4->mediafp);
len += fread(&segment_mediaRate, 1, 4, mp4->mediafp);
segment_duration = BYTESWAP32(segment_duration); // in MP4 clock base
segment_mediaTime = BYTESWAP32(segment_mediaTime); // in trak clock base
segment_mediaRate = BYTESWAP32(segment_mediaRate); // Fixed-point 65536 = 1.0X
if (segment_mediaTime == -1) // the segment_duration for blanked time
mp4->trak_edit_list_offsets[mp4->trak_num] += segment_duration; //samples are delay, data starts after presentation time zero.
else if (i == 0) // If the first editlst starts after zero, the track is offset by this time (time before presentation time zero.)
mp4->trak_edit_list_offsets[mp4->trak_num] -= (int32_t)((double)segment_mediaTime/(double)mp4->trak_clockdemon*(double)mp4->clockdemon); //convert to MP4 clock base.
}
if (type == traktype) // GPMF metadata
{
mp4->metadataoffset_clockcount = mp4->trak_edit_list_offsets[mp4->trak_num]; //leave in MP4 clock base
}
}
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over edts
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('s', 't', 's', 'd')) //read the sample decription to determine the type of metadata
{
if (type == traktype) //like meta
{
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&skip, 1, 4, mp4->mediafp);
len += fread(&skip, 1, 4, mp4->mediafp);
len += fread(&subtype, 1, 4, mp4->mediafp); // type will be 'meta' for the correct trak.
if (len == 16)
{
if (subtype != traksubtype) // not MP4 metadata
{
type = 0; // MP4
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stsd
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('s', 't', 's', 'c')) // metadata stsc - offset chunks
{
if (type == traktype) // meta
{
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
if (num <= ((qtsize - 8 - len)/sizeof(SampleToChunk)))
{
mp4->metastsc_count = num;
if (mp4->metastsc)
{
free(mp4->metastsc);
mp4->metastsc = 0;
}
if (num > 0)
{
mp4->metastsc = (SampleToChunk *)malloc(num * sizeof(SampleToChunk));
if (mp4->metastsc)
{
len += fread(mp4->metastsc, 1, num * sizeof(SampleToChunk), mp4->mediafp);
do
{
num--;
mp4->metastsc[num].chunk_num = BYTESWAP32(mp4->metastsc[num].chunk_num);
mp4->metastsc[num].samples = BYTESWAP32(mp4->metastsc[num].samples);
mp4->metastsc[num].id = BYTESWAP32(mp4->metastsc[num].id);
} while (num > 0);
}
}
else
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stsx
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('s', 't', 's', 'z')) // metadata stsz - sizes
{
if (type == traktype) // meta
{
uint32_t equalsamplesize;
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&equalsamplesize, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
// if equalsamplesize != 0, it is the size of all the samples and the length should be 20 (size,fourcc,flags,samplesize,samplecount)
if ((num <= ((qtsize - 8 - len)/sizeof(uint32_t))) || (equalsamplesize != 0 && qtsize == 20))
{
mp4->metasize_count = num;
if (mp4->metasizes)
{
free(mp4->metasizes);
mp4->metasizes = 0;
}
if(num > 0)
{
mp4->metasizes = (uint32_t *)malloc(num * 4);
if (mp4->metasizes)
{
if (equalsamplesize == 0)
{
len += fread(mp4->metasizes, 1, num * 4, mp4->mediafp);
do
{
num--;
mp4->metasizes[num] = BYTESWAP32(mp4->metasizes[num]);
} while (num > 0);
}
else
{
equalsamplesize = BYTESWAP32(equalsamplesize);
do
{
num--;
mp4->metasizes[num] = equalsamplesize;
} while (num > 0);
}
}
}
else
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stsz
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('s', 't', 'c', 'o')) // metadata stco - offsets
{
if (type == traktype) // meta
{
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
if (num <= ((qtsize - 8 - len)/sizeof(uint32_t)))
{
mp4->metastco_count = num;
if (mp4->metastsc_count > 0 && num != mp4->metasize_count)
{
if (mp4->metaoffsets)
{
free(mp4->metaoffsets);
mp4->metaoffsets = 0;
}
if(num > 0)
{
mp4->metaoffsets = (uint64_t *)malloc(num * 8);
if (mp4->metaoffsets)
{
uint32_t *metaoffsets32 = NULL;
metaoffsets32 = (uint32_t *)malloc(num * 4);
if (metaoffsets32)
{
uint64_t fileoffset = 0;
int stsc_pos = 0;
int stco_pos = 0;
int repeat = 1;
len += fread(metaoffsets32, 1, num * 4, mp4->mediafp);
do
{
num--;
metaoffsets32[num] = BYTESWAP32(metaoffsets32[num]);
} while (num > 0);
mp4->metaoffsets[0] = fileoffset = metaoffsets32[stco_pos];
num = 1;
while (num < mp4->metastco_count)
{
if ((uint32_t)repeat == mp4->metastsc[stsc_pos].samples)
{
if ((uint32_t)stco_pos + 1 < mp4->metastco_count)
{
stco_pos++;
fileoffset = (uint64_t)metaoffsets32[stco_pos];
}
else
{
fileoffset += (uint64_t)mp4->metasizes[num - 1];
}
if ((uint32_t)stsc_pos + 1 < mp4->metastsc_count)
if (mp4->metastsc[stsc_pos + 1].chunk_num == (uint32_t)stco_pos + 1)
stsc_pos++;
repeat = 1;
}
else
{
fileoffset += (uint64_t)mp4->metasizes[num - 1];
repeat++;
}
mp4->metaoffsets[num] = fileoffset;
//int delta = metaoffsets[num] - metaoffsets[num - 1];
//printf("%3d:%08x, delta = %08x\n", num, (int)fileoffset, delta);
num++;
}
if (mp4->metastsc) free(mp4->metastsc);
mp4->metastsc = NULL;
mp4->metastsc_count = 0;
free(metaoffsets32);
}
}
}
else
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
}
else
{
if (mp4->metaoffsets)
{
free(mp4->metaoffsets);
mp4->metaoffsets = 0;
}
if (num > 0)
{
mp4->metaoffsets = (uint64_t *)malloc(num * 8);
if (mp4->metaoffsets)
{
uint32_t *metaoffsets32 = NULL;
metaoffsets32 = (uint32_t *)malloc(num * 4);
if (metaoffsets32)
{
size_t readlen = fread(metaoffsets32, 1, num * 4, mp4->mediafp);
len += readlen;
do
{
num--;
mp4->metaoffsets[num] = BYTESWAP32(metaoffsets32[num]);
} while (num > 0);
free(metaoffsets32);
}
}
}
else
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stco
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('c', 'o', '6', '4')) // metadata stco - offsets
{
if (type == traktype) // meta
{
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
if(num == 0)
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
if (num <= ((qtsize - 8 - len)/sizeof(uint64_t)))
{
mp4->metastco_count = num;
if (mp4->metastsc_count > 0 && num != mp4->metasize_count)
{
if (mp4->metaoffsets)
{
free(mp4->metaoffsets);
mp4->metaoffsets = 0;
}
if (mp4->metasize_count)
{
mp4->metaoffsets = (uint64_t *)malloc(mp4->metasize_count * 8);
if (mp4->metaoffsets)
{
uint64_t *metaoffsets64 = NULL;
metaoffsets64 = (uint64_t *)malloc(num * 8);
if (metaoffsets64)
{
uint64_t fileoffset = 0;
int stsc_pos = 0;
int stco_pos = 0;
len += fread(metaoffsets64, 1, num * 8, mp4->mediafp);
do
{
num--;
metaoffsets64[num] = BYTESWAP64(metaoffsets64[num]);
} while (num > 0);
fileoffset = metaoffsets64[0];
mp4->metaoffsets[0] = fileoffset;
//printf("%3d:%08x, delta = %08x\n", 0, (int)fileoffset, 0);
num = 1;
while (num < mp4->metasize_count)
{
if (num != mp4->metastsc[stsc_pos].chunk_num - 1 && 0 == (num - (mp4->metastsc[stsc_pos].chunk_num - 1)) % mp4->metastsc[stsc_pos].samples)
{
stco_pos++;
fileoffset = metaoffsets64[stco_pos];
}
else
{
fileoffset += (uint64_t)mp4->metasizes[num - 1];
}
mp4->metaoffsets[num] = fileoffset;
//int delta = metaoffsets[num] - metaoffsets[num - 1];
//printf("%3d:%08x, delta = %08x\n", num, (int)fileoffset, delta);
num++;
}
if (mp4->metastsc) free(mp4->metastsc);
mp4->metastsc = NULL;
mp4->metastsc_count = 0;
free(metaoffsets64);
}
}
}
else
{
//size of null
CloseSource((size_t)mp4);
mp4 = NULL;
break;
}
}
else
{
if (mp4->metaoffsets)
{
free(mp4->metaoffsets);
mp4->metaoffsets = 0;
}
mp4->metaoffsets = (uint64_t *)malloc(num * 8);
if (mp4->metaoffsets)
{
len += fread(mp4->metaoffsets, 1, num * 8, mp4->mediafp);
do
{
num--;
mp4->metaoffsets[num] = BYTESWAP64(mp4->metaoffsets[num]);
} while (num > 0);
}
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stco
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else if (qttag == MAKEID('s', 't', 't', 's')) // time to samples
{
if (type == MAKEID('v', 'i', 'd', 'e')) // video trak to get frame rate
{
uint32_t samples = 0;
uint32_t entries = 0;
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
if (num <= ((qtsize - 8 - len) / 8))
{
entries = num;
while (entries > 0)
{
uint32_t samplecount;
uint32_t duration;
len += fread(&samplecount, 1, 4, mp4->mediafp);
samplecount = BYTESWAP32(samplecount);
len += fread(&duration, 1, 4, mp4->mediafp);
duration = BYTESWAP32(duration);
samples += samplecount;
entries--;
if (mp4->video_framerate_numerator == 0)
{
mp4->video_framerate_numerator = mp4->trak_clockdemon;
mp4->video_framerate_denominator = duration;
}
}
mp4->video_frames = samples;
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stco
}
else
if (type == traktype) // meta
{
uint32_t totaldur = 0, samples = 0;
uint32_t entries = 0;
len = fread(&skip, 1, 4, mp4->mediafp);
len += fread(&num, 1, 4, mp4->mediafp);
num = BYTESWAP32(num);
if (num <= ((qtsize - 8 - len)/8))
{
entries = num;
mp4->meta_clockdemon = mp4->trak_clockdemon;
mp4->meta_clockcount = mp4->trak_clockcount;
while (entries > 0)
{
uint32_t samplecount;
uint32_t duration;
len += fread(&samplecount, 1, 4, mp4->mediafp);
samplecount = BYTESWAP32(samplecount);
len += fread(&duration, 1, 4, mp4->mediafp);
duration = BYTESWAP32(duration);
samples += samplecount;
entries--;
totaldur += duration;
mp4->metadatalength += (double)((double)samplecount * (double)duration / (double)mp4->meta_clockdemon);
if (samplecount > 1 || entries == 1)
mp4->basemetadataduration = mp4->metadatalength * (double)mp4->meta_clockdemon / (double)samples;
}
}
mp4->filepos += len;
LongSeek(mp4, qtsize - 8 - len); // skip over stco
}
else
LongSeek(mp4, qtsize - 8);
NESTSIZE(qtsize);
}
else
{
NESTSIZE(8);
}
}
else
{
break;
}
} while (len > 0);
if (mp4)
{
if (mp4->metasizes == NULL || mp4->metaoffsets == NULL)
{
CloseSource((size_t)mp4);
mp4 = NULL;
}
// set the numbers of payload with both size and offset
if (mp4 != NULL)
{
mp4->indexcount = mp4->metasize_count;
if (mp4->metastco_count < mp4->indexcount)
mp4->indexcount = mp4->metastco_count;
}
}
}
else
{
// printf("Could not open %s for input\n", filename);
// exit(1);
free(mp4);
mp4 = NULL;
}
return (size_t)mp4;
}
float GetDuration(size_t handle)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return 0.0;
return (float)mp4->metadatalength;
}
uint32_t GetVideoFrameRateAndCount(size_t handle, uint32_t *numer, uint32_t *demon)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return 0;
if (numer != NULL && demon != NULL && mp4->video_frames > 0)
{
*numer = mp4->video_framerate_numerator;
*demon = mp4->video_framerate_denominator;
return mp4->video_frames;
}
return 0;
}
void CloseSource(size_t handle)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL)
{
return;
}
if (mp4->mediafp)
{
fclose(mp4->mediafp);
mp4->mediafp = NULL;
}
if (mp4->metasizes)
{
free(mp4->metasizes);
mp4->metasizes = 0;
}
if (mp4->metaoffsets)
{
free(mp4->metaoffsets);
mp4->metaoffsets = 0;
}
if (mp4->metastsc)
{
free(mp4->metastsc);
mp4->metastsc = 0;
}
free(mp4);
}
uint32_t GetPayloadTime(size_t handle, uint32_t index, double *in, double *out)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return GPMF_ERROR_MEMORY;
if (mp4->metaoffsets == 0 || mp4->basemetadataduration == 0 || mp4->meta_clockdemon == 0 || in == NULL || out == NULL) return GPMF_ERROR_MEMORY;
*in = ((double)index * (double)mp4->basemetadataduration / (double)mp4->meta_clockdemon);
*out = ((double)(index + 1) * (double)mp4->basemetadataduration / (double)mp4->meta_clockdemon);
if (*out > (double)mp4->metadatalength)
*out = (double)mp4->metadatalength;
// Add any Edit List offset
*in += (double)mp4->metadataoffset_clockcount / (double)mp4->clockdemon;
*out += (double)mp4->metadataoffset_clockcount / (double)mp4->clockdemon;
return GPMF_OK;
}
uint32_t GetPayloadRationalTime(size_t handle, uint32_t index, int32_t *in_numerator, int32_t *out_numerator, uint32_t *denominator)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return GPMF_ERROR_MEMORY;
if (mp4->metaoffsets == 0 || mp4->basemetadataduration == 0 || mp4->meta_clockdemon == 0 || in_numerator == NULL || out_numerator == NULL) return GPMF_ERROR_MEMORY;
*in_numerator = (int32_t)(index * mp4->basemetadataduration);
*out_numerator = (int32_t)((index + 1) * mp4->basemetadataduration);
if (*out_numerator > (int32_t)((double)mp4->metadatalength*(double)mp4->meta_clockdemon))
*out_numerator = (int32_t)((double)mp4->metadatalength*(double)mp4->meta_clockdemon);
// Add any Edit List offset
*in_numerator += (int32_t)(((double)mp4->metadataoffset_clockcount / (double)mp4->clockdemon) * mp4->meta_clockdemon);
*out_numerator += (int32_t)(((double)mp4->metadataoffset_clockcount / (double)mp4->clockdemon) * mp4->meta_clockdemon);
*denominator = mp4->meta_clockdemon;
return GPMF_OK;
}
uint32_t GetEditListOffset(size_t handle, double *offset)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return GPMF_ERROR_MEMORY;
if (mp4->clockdemon == 0) return GPMF_ERROR_MEMORY;
*offset = (double)mp4->metadataoffset_clockcount / (double)mp4->clockdemon;
return GPMF_OK;
}
uint32_t GetEditListOffsetRationalTime(size_t handle, int32_t *offset_numerator, uint32_t *denominator)
{
mp4object *mp4 = (mp4object *)handle;
if (mp4 == NULL) return GPMF_ERROR_MEMORY;
if (mp4->clockdemon == 0) return GPMF_ERROR_MEMORY;
*offset_numerator = mp4->metadataoffset_clockcount;
*denominator = mp4->clockdemon;
return GPMF_OK;
}