/
isom_read.c
6082 lines (5321 loc) · 185 KB
/
isom_read.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2000-2022
* All rights reserved
*
* This file is part of GPAC / ISO Media File Format sub-project
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/internal/isomedia_dev.h>
#include <gpac/constants.h>
#ifndef GPAC_DISABLE_ISOM
//the only static var. Used to store any error happening while opening a movie
static GF_Err MP4_API_IO_Err;
void gf_isom_set_last_error(GF_ISOFile *movie, GF_Err error)
{
if (!movie) {
MP4_API_IO_Err = error;
} else {
movie->LastError = error;
}
}
GF_EXPORT
GF_Err gf_isom_last_error(GF_ISOFile *the_file)
{
if (!the_file) return MP4_API_IO_Err;
return the_file->LastError;
}
GF_EXPORT
u8 gf_isom_get_mode(GF_ISOFile *the_file)
{
if (!the_file) return 0;
return the_file->openMode;
}
#if 0 //unused
/*! gets file size of an ISO file
\param isom_file the target ISO file
\return the file size in bytes
*/
u64 gf_isom_get_file_size(GF_ISOFile *the_file)
{
if (!the_file) return 0;
if (the_file->movieFileMap) return gf_bs_get_size(the_file->movieFileMap->bs);
#ifndef GPAC_DISABLE_ISOM_WRITE
if (the_file->editFileMap) return gf_bs_get_size(the_file->editFileMap->bs);
#endif
return 0;
}
#endif
GF_EXPORT
GF_Err gf_isom_freeze_order(GF_ISOFile *file)
{
u32 i=0;
GF_Box *box;
if (!file) return GF_BAD_PARAM;
while ((box=gf_list_enum(file->TopBoxes, &i))) {
gf_isom_box_freeze_order(box);
}
return GF_OK;
}
GF_EXPORT
GF_Err gf_isom_set_inplace_padding(GF_ISOFile *file, u32 padding)
{
if (!file) return GF_BAD_PARAM;
file->padding = padding;
return GF_OK;
}
/**************************************************************
Sample Manip
**************************************************************/
//creates a new empty sample
GF_EXPORT
GF_ISOSample *gf_isom_sample_new()
{
GF_ISOSample *tmp;
GF_SAFEALLOC(tmp, GF_ISOSample);
return tmp;
}
//delete a sample
GF_EXPORT
void gf_isom_sample_del(GF_ISOSample **samp)
{
if (!samp || ! *samp) return;
if ((*samp)->data && (*samp)->dataLength) gf_free((*samp)->data);
gf_free(*samp);
*samp = NULL;
}
static u32 gf_isom_probe_type(u32 type)
{
switch (type) {
case GF_ISOM_BOX_TYPE_FTYP:
case GF_ISOM_BOX_TYPE_MOOV:
return 2;
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
case GF_ISOM_BOX_TYPE_MOOF:
case GF_ISOM_BOX_TYPE_STYP:
case GF_ISOM_BOX_TYPE_SIDX:
case GF_ISOM_BOX_TYPE_EMSG:
case GF_ISOM_BOX_TYPE_PRFT:
//we map free as segment when it is first in the file - a regular file shall start with ftyp or a file sig, not free
//since our route stack may patch boxes to free for incomplete segments, we must map this to free
case GF_ISOM_BOX_TYPE_FREE:
return 3;
#ifndef GPAC_DISABLE_ISOM_ADOBE
/*Adobe specific*/
case GF_ISOM_BOX_TYPE_AFRA:
case GF_ISOM_BOX_TYPE_ABST:
#endif
#endif
case GF_ISOM_BOX_TYPE_MDAT:
case GF_ISOM_BOX_TYPE_SKIP:
case GF_ISOM_BOX_TYPE_UDTA:
case GF_ISOM_BOX_TYPE_META:
case GF_ISOM_BOX_TYPE_VOID:
case GF_ISOM_BOX_TYPE_JP:
case GF_QT_BOX_TYPE_WIDE:
return 1;
default:
return 0;
}
}
GF_EXPORT
u32 gf_isom_probe_file_range(const char *fileName, u64 start_range, u64 end_range)
{
u32 type = 0;
if (!strncmp(fileName, "gmem://", 7)) {
u32 size;
u8 *mem_address;
if (gf_blob_get(fileName, &mem_address, &size, NULL) != GF_OK) {
return 0;
}
if (size && (size > start_range + 8)) {
type = GF_4CC(mem_address[start_range + 4], mem_address[start_range + 5], mem_address[start_range + 6], mem_address[start_range + 7]);
}
gf_blob_release(fileName);
} else if (!strncmp(fileName, "isobmff://", 10)) {
return 2;
} else {
u32 nb_read;
unsigned char data[4];
FILE *f = gf_fopen(fileName, "rb");
if (!f) return 0;
if (start_range) gf_fseek(f, start_range, SEEK_SET);
type = 0;
nb_read = (u32) gf_fread(data, 4, f);
if (nb_read == 4) {
if (gf_fread(data, 4, f) == 4) {
type = GF_4CC(data[0], data[1], data[2], data[3]);
}
}
gf_fclose(f);
if (!nb_read) return 0;
}
return gf_isom_probe_type(type);
}
GF_EXPORT
u32 gf_isom_probe_file(const char *fileName)
{
return gf_isom_probe_file_range(fileName, 0, 0);
}
GF_EXPORT
u32 gf_isom_probe_data(const u8*inBuf, u32 inSize)
{
u32 type;
if (inSize < 8) return 0;
type = GF_4CC(inBuf[4], inBuf[5], inBuf[6], inBuf[7]);
return gf_isom_probe_type(type);
}
#ifndef GPAC_DISABLE_AV_PARSERS
#include <gpac/internal/media_dev.h>
#endif
static GF_Err isom_create_init_from_mem(const char *fileName, GF_ISOFile *file)
{
u32 sample_rate=0;
u32 nb_channels=0;
u32 bps=0;
//u32 atag=0;
u32 nal_len=4;
u32 width = 0;
u32 height = 0;
u32 timescale = 10000000;
u64 tfdt = 0;
char sz4cc[5];
char CodecParams[2048];
u32 CodecParamLen=0;
char *sep, *val;
GF_TrackBox *trak;
GF_TrackExtendsBox *trex;
GF_SampleTableBox *stbl;
sz4cc[0] = 0;
val = (char*) ( fileName + strlen("isobmff://") );
while (1) {
sep = strchr(val, ' ');
if (sep) sep[0] = 0;
if (!strncmp(val, "4cc=", 4)) strcpy(sz4cc, val+4);
else if (!strncmp(val, "init=", 5)) {
char szH[3], *data = val+5;
u32 i, len = (u32) strlen(data);
for (i=0; i<len; i+=2) {
u32 v;
//init is hex-encoded so 2 input bytes for one output char
szH[0] = data[i];
szH[1] = data[i+1];
szH[2] = 0;
sscanf(szH, "%X", &v);
CodecParams[CodecParamLen] = (char) v;
CodecParamLen++;
}
}
else if (!strncmp(val, "nal=", 4)) nal_len = atoi(val+4);
else if (!strncmp(val, "bps=", 4)) bps = atoi(val+4);
//else if (!strncmp(val, "atag=", 5)) atag = atoi(val+5);
else if (!strncmp(val, "ch=", 3)) nb_channels = atoi(val+3);
else if (!strncmp(val, "srate=", 6)) sample_rate = atoi(val+6);
else if (!strncmp(val, "w=", 2)) width = atoi(val+2);
else if (!strncmp(val, "h=", 2)) height = atoi(val+2);
else if (!strncmp(val, "scale=", 6)) timescale = atoi(val+6);
else if (!strncmp(val, "tfdt=", 5)) {
sscanf(val+5, LLX, &tfdt);
}
if (!sep) break;
sep[0] = ' ';
val = sep+1;
}
if (!stricmp(sz4cc, "H264") || !stricmp(sz4cc, "AVC1")) {
}
else if (!stricmp(sz4cc, "AACL")) {
}
else {
GF_LOG(GF_LOG_WARNING, GF_LOG_CONTAINER, ("[iso file] Cannot convert smooth media type %s to ISO init segment\n", sz4cc));
return GF_NOT_SUPPORTED;
}
file->moov = (GF_MovieBox *) gf_isom_box_new(GF_ISOM_BOX_TYPE_MOOV);
if (!file->moov) return GF_OUT_OF_MEM;
gf_list_add(file->TopBoxes, file->moov);
file->moov->mov = file;
file->is_smooth = GF_TRUE;
file->moov->mvhd = (GF_MovieHeaderBox *) gf_isom_box_new_parent(&file->moov->child_boxes, GF_ISOM_BOX_TYPE_MVHD);
if (!file->moov->mvhd) return GF_OUT_OF_MEM;
file->moov->mvhd->timeScale = timescale;
file->moov->mvex = (GF_MovieExtendsBox *) gf_isom_box_new_parent(&file->moov->child_boxes, GF_ISOM_BOX_TYPE_MVEX);
if (!file->moov->mvex) return GF_OUT_OF_MEM;
trex = (GF_TrackExtendsBox *) gf_isom_box_new_parent(&file->moov->mvex->child_boxes, GF_ISOM_BOX_TYPE_TREX);
if (!trex) return GF_OUT_OF_MEM;
trex->def_sample_desc_index = 1;
trex->trackID = 1;
gf_list_add(file->moov->mvex->TrackExList, trex);
trak = (GF_TrackBox *) gf_isom_box_new_parent(&file->moov->child_boxes, GF_ISOM_BOX_TYPE_TRAK);
if (!trak) return GF_OUT_OF_MEM;
trak->moov = file->moov;
gf_list_add(file->moov->trackList, trak);
trak->Header = (GF_TrackHeaderBox *) gf_isom_box_new_parent(&trak->child_boxes, GF_ISOM_BOX_TYPE_TKHD);
if (!trak->Header) return GF_OUT_OF_MEM;
trak->Header->trackID = 1;
trak->Header->flags |= 1;
trak->Header->width = width;
trak->Header->height = height;
trak->Media = (GF_MediaBox *) gf_isom_box_new_parent(&trak->child_boxes, GF_ISOM_BOX_TYPE_MDIA);
if (!trak->Media) return GF_OUT_OF_MEM;
trak->Media->mediaTrack = trak;
trak->Media->mediaHeader = (GF_MediaHeaderBox *) gf_isom_box_new_parent(&trak->Media->child_boxes, GF_ISOM_BOX_TYPE_MDHD);
if (!trak->Media->mediaHeader) return GF_OUT_OF_MEM;
trak->Media->mediaHeader->timeScale = timescale;
trak->Media->handler = (GF_HandlerBox *) gf_isom_box_new_parent(&trak->Media->child_boxes,GF_ISOM_BOX_TYPE_HDLR);
if (!trak->Media->handler) return GF_OUT_OF_MEM;
//we assume by default vide for handler type (only used for smooth streaming)
trak->Media->handler->handlerType = width ? GF_ISOM_MEDIA_VISUAL : GF_ISOM_MEDIA_AUDIO;
trak->Media->information = (GF_MediaInformationBox *) gf_isom_box_new_parent(&trak->Media->child_boxes, GF_ISOM_BOX_TYPE_MINF);
if (!trak->Media->information) return GF_OUT_OF_MEM;
trak->Media->information->sampleTable = (GF_SampleTableBox *) gf_isom_box_new_parent(&trak->Media->information->child_boxes, GF_ISOM_BOX_TYPE_STBL);
if (!trak->Media->information->sampleTable) return GF_OUT_OF_MEM;
stbl = trak->Media->information->sampleTable;
stbl->SampleSize = (GF_SampleSizeBox *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STSZ);
if (!stbl->SampleSize) return GF_OUT_OF_MEM;
stbl->TimeToSample = (GF_TimeToSampleBox *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STTS);
if (!stbl->TimeToSample) return GF_OUT_OF_MEM;
stbl->ChunkOffset = (GF_Box *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STCO);
if (!stbl->ChunkOffset) return GF_OUT_OF_MEM;
stbl->SampleToChunk = (GF_SampleToChunkBox *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STSC);
if (!stbl->SampleToChunk) return GF_OUT_OF_MEM;
stbl->SyncSample = (GF_SyncSampleBox *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STSS);
if (!stbl->SyncSample) return GF_OUT_OF_MEM;
stbl->SampleDescription = (GF_SampleDescriptionBox *) gf_isom_box_new_parent(&stbl->child_boxes, GF_ISOM_BOX_TYPE_STSD);
if (!stbl->SampleDescription) return GF_OUT_OF_MEM;
trak->dts_at_seg_start = tfdt;
trak->dts_at_next_frag_start = tfdt;
if (!stricmp(sz4cc, "H264") || !stricmp(sz4cc, "AVC1")) {
#ifndef GPAC_DISABLE_AV_PARSERS
u32 pos = 0;
u32 end, sc_size=0;
#endif
GF_MPEGVisualSampleEntryBox *avc = (GF_MPEGVisualSampleEntryBox *) gf_isom_box_new_parent(&stbl->SampleDescription->child_boxes, GF_ISOM_BOX_TYPE_AVC1);
if (!avc) return GF_OUT_OF_MEM;
avc->avc_config = (GF_AVCConfigurationBox *) gf_isom_box_new_parent(&avc->child_boxes, GF_ISOM_BOX_TYPE_AVCC);
if (!avc->avc_config) return GF_OUT_OF_MEM;
avc->Width = width;
avc->Height = height;
avc->avc_config->config = gf_odf_avc_cfg_new();
avc->avc_config->config->nal_unit_size = nal_len;
avc->avc_config->config->configurationVersion = 1;
#ifndef GPAC_DISABLE_AV_PARSERS
//locate pps and sps
gf_media_nalu_next_start_code((u8 *) CodecParams, CodecParamLen, &sc_size);
pos += sc_size;
while (pos<CodecParamLen) {
GF_NALUFFParam *slc;
u8 nal_type;
char *nal = &CodecParams[pos];
end = gf_media_nalu_next_start_code(nal, CodecParamLen-pos, &sc_size);
if (!end) end = CodecParamLen;
GF_SAFEALLOC(slc, GF_NALUFFParam);
if (!slc) break;
slc->size = end;
slc->data = gf_malloc(sizeof(char)*slc->size);
if (!slc->data) return GF_OUT_OF_MEM;
memcpy(slc->data, nal, sizeof(char)*slc->size);
nal_type = nal[0] & 0x1F;
if (nal_type == GF_AVC_NALU_SEQ_PARAM) {
/* AVCState avcc;
u32 idx = gf_avc_read_sps(slc->data, slc->size, &avcc, 0, NULL);
avc->avc_config->config->profile_compatibility = avcc.sps[idx].prof_compat;
avc->avc_config->config->AVCProfileIndication = avcc.sps[idx].profile_idc;
avc->avc_config->config->AVCLevelIndication = avcc.sps[idx].level_idc;
avc->avc_config->config->chroma_format = avcc.sps[idx].chroma_format;
avc->avc_config->config->luma_bit_depth = 8 + avcc.sps[idx].luma_bit_depth_m8;
avc->avc_config->config->chroma_bit_depth = 8 + avcc.sps[idx].chroma_bit_depth_m8;
*/
gf_list_add(avc->avc_config->config->sequenceParameterSets, slc);
} else {
gf_list_add(avc->avc_config->config->pictureParameterSets, slc);
}
pos += slc->size + sc_size;
}
#endif
AVC_RewriteESDescriptor(avc);
}
else if (!stricmp(sz4cc, "AACL")) {
#ifndef GPAC_DISABLE_AV_PARSERS
GF_M4ADecSpecInfo aacinfo;
#endif
GF_MPEGAudioSampleEntryBox *aac = (GF_MPEGAudioSampleEntryBox *) gf_isom_box_new_parent(&stbl->SampleDescription->child_boxes, GF_ISOM_BOX_TYPE_MP4A);
if (!aac) return GF_OUT_OF_MEM;
aac->esd = (GF_ESDBox *) gf_isom_box_new_parent(&aac->child_boxes, GF_ISOM_BOX_TYPE_ESDS);
if (!aac->esd) return GF_OUT_OF_MEM;
aac->esd->desc = gf_odf_desc_esd_new(2);
if (!aac->esd->desc) return GF_OUT_OF_MEM;
#ifndef GPAC_DISABLE_AV_PARSERS
memset(&aacinfo, 0, sizeof(GF_M4ADecSpecInfo));
aacinfo.nb_chan = nb_channels;
aacinfo.base_object_type = GF_M4A_AAC_LC;
aacinfo.base_sr = sample_rate;
gf_m4a_write_config(&aacinfo, &aac->esd->desc->decoderConfig->decoderSpecificInfo->data, &aac->esd->desc->decoderConfig->decoderSpecificInfo->dataLength);
#endif
aac->esd->desc->decoderConfig->streamType = GF_STREAM_AUDIO;
aac->esd->desc->decoderConfig->objectTypeIndication = GF_CODECID_AAC_MPEG4;
aac->bitspersample = bps;
aac->samplerate_hi = sample_rate;
aac->channel_count = nb_channels;
}
return GF_OK;
}
/**************************************************************
File Opening in streaming mode
the file map is regular (through FILE handles)
**************************************************************/
GF_EXPORT
GF_Err gf_isom_open_progressive_ex(const char *fileName, u64 start_range, u64 end_range, Bool enable_frag_bounds, GF_ISOFile **the_file, u64 *BytesMissing, u32 *outBoxType)
{
GF_Err e;
GF_ISOFile *movie;
if (!BytesMissing || !the_file)
return GF_BAD_PARAM;
*BytesMissing = 0;
*the_file = NULL;
movie = gf_isom_new_movie();
if (!movie) return GF_OUT_OF_MEM;
movie->fileName = gf_strdup(fileName);
movie->openMode = GF_ISOM_OPEN_READ;
movie->signal_frag_bounds = enable_frag_bounds;
#ifndef GPAC_DISABLE_ISOM_WRITE
movie->editFileMap = NULL;
movie->finalName = NULL;
#endif /*GPAC_DISABLE_ISOM_WRITE*/
if (!strncmp(fileName, "isobmff://", 10)) {
movie->movieFileMap = NULL;
e = isom_create_init_from_mem(fileName, movie);
} else {
//do NOT use FileMapping on incomplete files
e = gf_isom_datamap_new(fileName, NULL, GF_ISOM_DATA_MAP_READ, &movie->movieFileMap);
if (e) {
gf_isom_delete_movie(movie);
return e;
}
if (start_range || end_range) {
if (end_range>start_range) {
gf_bs_seek(movie->movieFileMap->bs, end_range+1);
gf_bs_truncate(movie->movieFileMap->bs);
}
gf_bs_seek(movie->movieFileMap->bs, start_range);
}
e = gf_isom_parse_movie_boxes(movie, outBoxType, BytesMissing, GF_TRUE);
}
if (e == GF_ISOM_INCOMPLETE_FILE) {
//if we have a moov, we're fine
if (movie->moov) {
*the_file = (GF_ISOFile *)movie;
return GF_OK;
}
//if not, delete the movie
gf_isom_delete_movie(movie);
return e;
} else if (e) {
//if not, delete the movie
gf_isom_delete_movie(movie);
return e;
}
//OK, let's return
*the_file = (GF_ISOFile *)movie;
return GF_OK;
}
GF_EXPORT
GF_Err gf_isom_open_progressive(const char *fileName, u64 start_range, u64 end_range, Bool enable_frag_bounds, GF_ISOFile **the_file, u64 *BytesMissing)
{
return gf_isom_open_progressive_ex(fileName, start_range, end_range, enable_frag_bounds, the_file, BytesMissing, NULL);
}
/**************************************************************
File Reading
**************************************************************/
GF_EXPORT
GF_ISOFile *gf_isom_open(const char *fileName, GF_ISOOpenMode OpenMode, const char *tmp_dir)
{
GF_ISOFile *movie;
MP4_API_IO_Err = GF_OK;
switch (OpenMode & 0xFF) {
case GF_ISOM_OPEN_READ_DUMP:
case GF_ISOM_OPEN_READ:
movie = gf_isom_open_file(fileName, OpenMode, NULL);
break;
#ifndef GPAC_DISABLE_ISOM_WRITE
case GF_ISOM_OPEN_WRITE:
movie = gf_isom_create_movie(fileName, OpenMode, tmp_dir);
break;
case GF_ISOM_OPEN_EDIT:
case GF_ISOM_OPEN_READ_EDIT:
case GF_ISOM_OPEN_KEEP_FRAGMENTS:
movie = gf_isom_open_file(fileName, OpenMode, tmp_dir);
break;
case GF_ISOM_WRITE_EDIT:
movie = gf_isom_create_movie(fileName, OpenMode, tmp_dir);
break;
#endif /*GPAC_DISABLE_ISOM_WRITE*/
default:
return NULL;
}
return (GF_ISOFile *) movie;
}
#if 0
/*! gets access to the data bitstream - see \ref gf_isom_open
\param isom_file the target ISO file
\param out_bs set to the file input bitstream - do NOT destroy
\return error if any
*/
GF_Err gf_isom_get_bs(GF_ISOFile *movie, GF_BitStream **out_bs)
{
#ifndef GPAC_DISABLE_ISOM_WRITE
if (!movie || movie->openMode != GF_ISOM_OPEN_WRITE || !movie->editFileMap) //memory mode
return GF_NOT_SUPPORTED;
if (movie->segment_bs)
*out_bs = movie->segment_bs;
else
*out_bs = movie->editFileMap->bs;
if (movie->moof)
movie->moof->fragment_offset = 0;
return GF_OK;
#else
return GF_NOT_SUPPORTED;
#endif
}
#endif
GF_EXPORT
GF_Err gf_isom_write(GF_ISOFile *movie) {
GF_Err e;
if (movie == NULL) return GF_ISOM_INVALID_FILE;
e = GF_OK;
#ifndef GPAC_DISABLE_ISOM_WRITE
//write our movie to the file
if ((movie->openMode != GF_ISOM_OPEN_READ) && (movie->openMode != GF_ISOM_OPEN_READ_EDIT)) {
gf_isom_get_duration(movie);
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
//movie fragment mode, just store the fragment
if ( (movie->openMode == GF_ISOM_OPEN_WRITE) && (movie->FragmentsFlags & GF_ISOM_FRAG_WRITE_READY) ) {
e = gf_isom_close_fragments(movie);
if (e) return e;
//in case of mfra box usage -> create mfro, calculate box sizes and write it out
if (movie->mfra) {
if (!movie->mfra->mfro) {
movie->mfra->mfro = (GF_MovieFragmentRandomAccessOffsetBox *)gf_isom_box_new_parent(&movie->mfra->child_boxes, GF_ISOM_BOX_TYPE_MFRO);
if (!movie->mfra->mfro) return GF_OUT_OF_MEM;
}
e = gf_isom_box_size((GF_Box *)movie->mfra);
if (e) return e;
movie->mfra->mfro->container_size = (u32) movie->mfra->size;
//write mfra
if (!strcmp(movie->fileName, "_gpac_isobmff_redirect") && movie->on_block_out) {
GF_BitStream *bs = gf_bs_new_cbk(movie->on_block_out, movie->on_block_out_usr_data, movie->on_block_out_block_size);
e = gf_isom_box_write((GF_Box *)movie->mfra, bs);
gf_bs_del(bs);
} else {
e = gf_isom_box_write((GF_Box *)movie->mfra, movie->editFileMap->bs);
}
}
} else
#endif
e = WriteToFile(movie, GF_FALSE);
}
#endif /*GPAC_DISABLE_ISOM_WRITE*/
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (movie->moov) {
u32 i;
for (i=0; i<gf_list_count(movie->moov->trackList); i++) {
GF_TrackBox *trak = (GF_TrackBox*)gf_list_get(movie->moov->trackList, i);
/*delete any pending dataHandler of scalable enhancements*/
if (trak->Media && trak->Media->information && trak->Media->information->scalableDataHandler && (trak->Media->information->scalableDataHandler != movie->movieFileMap))
gf_isom_datamap_del(trak->Media->information->scalableDataHandler);
}
}
#endif
return e;
}
GF_EXPORT
GF_Err gf_isom_close(GF_ISOFile *movie)
{
GF_Err e=GF_OK;
if (movie == NULL) return GF_ISOM_INVALID_FILE;
e = gf_isom_write(movie);
//free and return;
gf_isom_delete_movie(movie);
return e;
}
#if 0 //unused
/*! checks if files has root OD/IOD or not
\param isom_file the target ISO file
\return GF_TRUE if the file has a root OD or IOD */
Bool gf_isom_has_root_od(GF_ISOFile *movie)
{
if (!movie || !movie->moov || !movie->moov->iods || !movie->moov->iods->descriptor) return GF_FALSE;
return GF_TRUE;
}
#endif
GF_EXPORT
void gf_isom_disable_odf_conversion(GF_ISOFile *movie, Bool disable)
{
if (movie) movie->disable_odf_translate = disable ? 1 : 0;
}
//this funct is used for exchange files, where the iods contains an OD
GF_EXPORT
GF_Descriptor *gf_isom_get_root_od(GF_ISOFile *movie)
{
GF_Descriptor *desc;
GF_ObjectDescriptor *od;
GF_InitialObjectDescriptor *iod;
GF_IsomObjectDescriptor *isom_od;
GF_IsomInitialObjectDescriptor *isom_iod;
GF_ESD *esd;
GF_ES_ID_Inc *inc;
u32 i;
u8 useIOD;
if (!movie || !movie->moov) return NULL;
if (!movie->moov->iods) return NULL;
if (movie->disable_odf_translate) {
//duplicate our descriptor
movie->LastError = gf_odf_desc_copy((GF_Descriptor *) movie->moov->iods->descriptor, &desc);
if (movie->LastError) return NULL;
return desc;
}
od = NULL;
iod = NULL;
switch (movie->moov->iods->descriptor->tag) {
case GF_ODF_ISOM_OD_TAG:
od = (GF_ObjectDescriptor*)gf_malloc(sizeof(GF_ObjectDescriptor));
if (!od) return NULL;
memset(od, 0, sizeof(GF_ObjectDescriptor));
od->ESDescriptors = gf_list_new();
useIOD = 0;
break;
case GF_ODF_ISOM_IOD_TAG:
iod = (GF_InitialObjectDescriptor*)gf_malloc(sizeof(GF_InitialObjectDescriptor));
if (!iod) return NULL;
memset(iod, 0, sizeof(GF_InitialObjectDescriptor));
iod->ESDescriptors = gf_list_new();
useIOD = 1;
break;
default:
return NULL;
}
//duplicate our descriptor
movie->LastError = gf_odf_desc_copy((GF_Descriptor *) movie->moov->iods->descriptor, &desc);
if (movie->LastError) {
if (od) {
gf_list_del(od->ESDescriptors);
gf_free(od);
}
if (iod) {
gf_list_del(iod->ESDescriptors);
gf_free(iod);
}
return NULL;
}
if (!useIOD) {
isom_od = (GF_IsomObjectDescriptor *)desc;
od->objectDescriptorID = isom_od->objectDescriptorID;
od->extensionDescriptors = isom_od->extensionDescriptors;
isom_od->extensionDescriptors = NULL;
od->IPMP_Descriptors = isom_od->IPMP_Descriptors;
isom_od->IPMP_Descriptors = NULL;
od->OCIDescriptors = isom_od->OCIDescriptors;
isom_od->OCIDescriptors = NULL;
od->URLString = isom_od->URLString;
isom_od->URLString = NULL;
od->tag = GF_ODF_OD_TAG;
//then recreate the desc in Inc
i=0;
while ((inc = (GF_ES_ID_Inc*)gf_list_enum(isom_od->ES_ID_IncDescriptors, &i))) {
movie->LastError = GetESDForTime(movie->moov, inc->trackID, 0, &esd);
if (!movie->LastError) movie->LastError = gf_list_add(od->ESDescriptors, esd);
if (movie->LastError) {
gf_odf_desc_del(desc);
gf_odf_desc_del((GF_Descriptor *) od);
return NULL;
}
}
gf_odf_desc_del(desc);
return (GF_Descriptor *)od;
} else {
isom_iod = (GF_IsomInitialObjectDescriptor *)desc;
iod->objectDescriptorID = isom_iod->objectDescriptorID;
iod->extensionDescriptors = isom_iod->extensionDescriptors;
isom_iod->extensionDescriptors = NULL;
iod->IPMP_Descriptors = isom_iod->IPMP_Descriptors;
isom_iod->IPMP_Descriptors = NULL;
iod->OCIDescriptors = isom_iod->OCIDescriptors;
isom_iod->OCIDescriptors = NULL;
iod->URLString = isom_iod->URLString;
isom_iod->URLString = NULL;
iod->tag = GF_ODF_IOD_TAG;
iod->audio_profileAndLevel = isom_iod->audio_profileAndLevel;
iod->graphics_profileAndLevel = isom_iod->graphics_profileAndLevel;
iod->inlineProfileFlag = isom_iod->inlineProfileFlag;
iod->OD_profileAndLevel = isom_iod->OD_profileAndLevel;
iod->scene_profileAndLevel = isom_iod->scene_profileAndLevel;
iod->visual_profileAndLevel = isom_iod->visual_profileAndLevel;
iod->IPMPToolList = isom_iod->IPMPToolList;
isom_iod->IPMPToolList = NULL;
//then recreate the desc in Inc
i=0;
while ((inc = (GF_ES_ID_Inc*)gf_list_enum(isom_iod->ES_ID_IncDescriptors, &i))) {
movie->LastError = GetESDForTime(movie->moov, inc->trackID, 0, &esd);
if (!movie->LastError) movie->LastError = gf_list_add(iod->ESDescriptors, esd);
if (movie->LastError) {
gf_odf_desc_del(desc);
gf_odf_desc_del((GF_Descriptor *) iod);
return NULL;
}
}
gf_odf_desc_del(desc);
return (GF_Descriptor *)iod;
}
}
GF_EXPORT
u32 gf_isom_get_track_count(GF_ISOFile *movie)
{
if (!movie || !movie->moov) return 0;
if (!movie->moov->trackList) {
movie->LastError = GF_ISOM_INVALID_FILE;
return 0;
}
return gf_list_count(movie->moov->trackList);
}
GF_EXPORT
GF_ISOTrackID gf_isom_get_track_id(GF_ISOFile *movie, u32 trackNumber)
{
GF_TrackBox *trak;
if (!movie) return 0;
trak = gf_isom_get_track_from_file(movie, trackNumber);
if (!trak || !trak->Header) return 0;
return trak->Header->trackID;
}
GF_EXPORT
u32 gf_isom_get_track_by_id(GF_ISOFile *the_file, GF_ISOTrackID trackID)
{
u32 count;
u32 i;
if (the_file == NULL) return 0;
count = gf_isom_get_track_count(the_file);
if (!count) return 0;
for (i = 0; i < count; i++) {
GF_TrackBox *trak = gf_isom_get_track_from_file(the_file, i+1);
if (!trak || !trak->Header) return 0;
if (trak->Header->trackID == trackID) return i+1;
}
return 0;
}
GF_EXPORT
GF_ISOTrackID gf_isom_get_track_original_id(GF_ISOFile *movie, u32 trackNumber)
{
GF_TrackBox *trak;
if (!movie) return 0;
trak = gf_isom_get_track_from_file(movie, trackNumber);
if (!trak) return 0;
return trak->originalID;
}
//return the timescale of the movie, 0 if error
GF_EXPORT
Bool gf_isom_has_movie(GF_ISOFile *file)
{
if (file && file->moov) return GF_TRUE;
return GF_FALSE;
}
#ifndef GPAC_DISABLE_ISOM
GF_EXPORT
Bool gf_isom_has_segment(GF_ISOFile *file, u32 *brand, u32 *version)
{
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
u32 i;
GF_Box *a;
i = 0;
while (NULL != (a = (GF_Box*)gf_list_enum(file->TopBoxes, &i))) {
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (a->type == GF_ISOM_BOX_TYPE_STYP) {
GF_FileTypeBox *styp = (GF_FileTypeBox *)a;
*brand = styp->majorBrand;
*version = styp->minorVersion;
return GF_TRUE;
}
#endif
}
#endif
return GF_FALSE;
}
GF_EXPORT
u32 gf_isom_segment_get_fragment_count(GF_ISOFile *file)
{
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (file) {
u32 i, count = 0;
for (i=0; i<gf_list_count(file->TopBoxes); i++) {
GF_Box *a = (GF_Box*)gf_list_get(file->TopBoxes, i);
if (a->type==GF_ISOM_BOX_TYPE_MOOF) count++;
}
return count;
}
#endif
return 0;
}
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
static GF_MovieFragmentBox *gf_isom_get_moof(GF_ISOFile *file, u32 moof_index)
{
u32 i;
for (i=0; i<gf_list_count(file->TopBoxes); i++) {
GF_Box *a = (GF_Box*)gf_list_get(file->TopBoxes, i);
if (a->type==GF_ISOM_BOX_TYPE_MOOF) {
moof_index--;
if (!moof_index) return (GF_MovieFragmentBox *) a;
}
}
return NULL;
}
#endif /* GPAC_DISABLE_ISOM_FRAGMENTS */
GF_EXPORT
u32 gf_isom_segment_get_track_fragment_count(GF_ISOFile *file, u32 moof_index)
{
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
GF_MovieFragmentBox *moof;
if (!file) return 0;
gf_list_count(file->TopBoxes);
moof = gf_isom_get_moof(file, moof_index);
return moof ? gf_list_count(moof->TrackList) : 0;
#endif
return 0;
}
GF_EXPORT
u32 gf_isom_segment_get_track_fragment_decode_time(GF_ISOFile *file, u32 moof_index, u32 traf_index, u64 *decode_time)
{
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
GF_MovieFragmentBox *moof;
GF_TrackFragmentBox *traf;
if (!file) return 0;
gf_list_count(file->TopBoxes);
moof = gf_isom_get_moof(file, moof_index);
traf = moof ? (GF_TrackFragmentBox*)gf_list_get(moof->TrackList, traf_index-1) : NULL;
if (!traf) return 0;
if (decode_time) {
*decode_time = traf->tfdt ? traf->tfdt->baseMediaDecodeTime : 0;
}
return traf->tfhd->trackID;
#endif
return 0;
}
GF_EXPORT
u64 gf_isom_segment_get_fragment_size(GF_ISOFile *file, u32 moof_index, u32 *moof_size)
{
#ifndef GPAC_DISABLE_ISOM_FRAGMENTS
if (!file) return 0;
u32 moof_sn=0;
Bool moof_after_mdat = GF_FALSE;
u64 size=0;
u32 i, count = gf_list_count(file->TopBoxes);
if (moof_size) *moof_size = 0;
for (i=0; i<count; i++) {
GF_Box *b = gf_list_get(file->TopBoxes, i);
size += b->size;
if (b->type == GF_ISOM_BOX_TYPE_MOOF) {
if (!moof_after_mdat && (moof_sn == moof_index))
return size - b->size;
if (moof_size) *moof_size = (u32) b->size;
moof_sn++;
if (moof_after_mdat && (moof_sn == moof_index))
return size;
if (moof_after_mdat) size=0;
if ((moof_sn>1) && !moof_after_mdat) size = b->size;
}
if (b->type == GF_ISOM_BOX_TYPE_MDAT) {
if (!moof_sn) moof_after_mdat = GF_TRUE;
}
}
return size;
#endif
return 0;
}
#endif
//return the timescale of the movie, 0 if error
GF_EXPORT
u32 gf_isom_get_timescale(GF_ISOFile *movie)
{
if (!movie || !movie->moov || !movie->moov->mvhd) return 0;
return movie->moov->mvhd->timeScale;
}
//return the duration of the movie, 0 if error
GF_EXPORT
u64 gf_isom_get_duration(GF_ISOFile *movie)
{
if (!movie || !movie->moov || !movie->moov->mvhd) return 0;
//if file was open in Write or Edit mode, recompute the duration
//the duration of a movie is the MaxDuration of all the tracks...
#ifndef GPAC_DISABLE_ISOM_WRITE
gf_isom_update_duration(movie);
#endif /*GPAC_DISABLE_ISOM_WRITE*/
return movie->moov->mvhd->duration;
}
//return the duration of the movie, 0 if error
GF_EXPORT
u64 gf_isom_get_original_duration(GF_ISOFile *movie)
{
if (!movie || !movie->moov|| !movie->moov->mvhd) return 0;
return movie->moov->mvhd->original_duration;
}
//return the creation info of the movie
GF_EXPORT
GF_Err gf_isom_get_creation_time(GF_ISOFile *movie, u64 *creationTime, u64 *modificationTime)
{
if (!movie || !movie->moov) return GF_BAD_PARAM;
if (creationTime) *creationTime = movie->moov->mvhd->creationTime;
if (creationTime) *modificationTime = movie->moov->mvhd->modificationTime;
return GF_OK;
}
GF_EXPORT
GF_Err gf_isom_get_track_creation_time(GF_ISOFile *movie, u32 trackNumber, u64 *creationTime, u64 *modificationTime)
{
GF_TrackBox *trak;
if (!movie || !movie->moov) return GF_BAD_PARAM;
trak = gf_isom_get_track_from_file(movie, trackNumber);
if (!trak) return 0;