forked from gopro/gpmf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPMF_parser.c
2257 lines (1923 loc) · 60.9 KB
/
GPMF_parser.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 GPMF_parser.c
*
* @brief GPMF Parser library
*
* @version 2.1.0
*
* (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.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "GPMF_parser.h"
#include "GPMF_bitstream.h"
#ifdef DBG
#if _WINDOWS
#define DBG_MSG printf
#else
#define DBG_MSG(...)
#endif
#else
#define DBG_MSG(...)
#endif
GPMF_ERR IsValidSize(GPMF_stream *ms, uint32_t size) // size is in longs not bytes.
{
if (ms)
{
uint32_t nestsize = (uint32_t)ms->nest_size[ms->nest_level];
if (nestsize == 0 && ms->nest_level == 0)
nestsize = ms->buffer_size_longs;
if (size + 2 <= nestsize) return GPMF_OK;
}
return GPMF_ERROR_BAD_STRUCTURE;
}
GPMF_ERR SkipLevel(GPMF_stream* ms)
{
if (ms)
{
ms->pos += ms->nest_size[ms->nest_level];
ms->nest_size[ms->nest_level] = 0;
while (ms->nest_level > 0 && ms->nest_size[ms->nest_level] == 0)
ms->nest_level--;
uint32_t size = (GPMF_DATA_SIZE(ms->buffer[ms->pos + 1]) >> 2);
if (GPMF_OK != IsValidSize(ms, size))
return GPMF_ERROR_BAD_STRUCTURE;
else
return GPMF_OK;
}
return GPMF_ERROR_BAD_STRUCTURE;
}
GPMF_ERR GPMF_Validate(GPMF_stream *ms, GPMF_LEVELS recurse)
{
if (ms)
{
GPMF_ERROR ret = GPMF_OK;
uint32_t currpos = ms->pos;
uint32_t nestsize = ms->nest_size[ms->nest_level];
if (nestsize == 0 && ms->nest_level == 0)
nestsize = ms->buffer_size_longs;
while (ms->pos+1 < ms->buffer_size_longs && nestsize > 0)
{
uint32_t key = ms->buffer[ms->pos];
if (ms->nest_level == 0 && key != GPMF_KEY_DEVICE && ms->device_count == 0 && ms->pos == 0)
{
DBG_MSG("ERROR: uninitized -- GPMF_ERROR_BAD_STRUCTURE\n");
return GPMF_ERROR_BAD_STRUCTURE;
}
if (GPMF_VALID_FOURCC(key))
{
uint32_t type_size_repeat = ms->buffer[ms->pos + 1];
uint32_t size = GPMF_DATA_SIZE(type_size_repeat) >> 2;
uint8_t type = GPMF_SAMPLE_TYPE(type_size_repeat);
if(type != GPMF_TYPE_NEST && type != GPMF_TYPE_COMPLEX && type != GPMF_TYPE_COMPRESSED && GPMF_SizeofType((GPMF_SampleType)type) == 0)
{
ret = GPMF_ERROR_UNKNOWN_TYPE;
DBG_MSG("MINOR ERROR: unknown datatype-- GPMF_ERROR_UNKNOWN_TYPE\n");
//continue looking for structural errors
}
if (GPMF_SAMPLE_SIZE(type_size_repeat) == 0)
{
DBG_MSG("ERROR: zero for datatype size-- GPMF_ERROR_BAD_STRUCTURE\n");
return GPMF_ERROR_BAD_STRUCTURE;
}
if (size + 2 > nestsize)
{
DBG_MSG("ERROR: nest size too small within %c%c%c%c-- GPMF_ERROR_BAD_STRUCTURE\n", PRINTF_4CC(key));
return GPMF_ERROR_BAD_STRUCTURE;
}
if (type == GPMF_TYPE_NEST && recurse == GPMF_RECURSE_LEVELS)
{
uint32_t validnest;
ms->pos += 2;
ms->nest_level++;
if (ms->nest_level > GPMF_NEST_LIMIT)
{
DBG_MSG("ERROR: nest level within %c%c%c%c too deep -- GPMF_ERROR_BAD_STRUCTURE\n", PRINTF_4CC(key));
return GPMF_ERROR_BAD_STRUCTURE;
}
ms->nest_size[ms->nest_level] = size;
validnest = GPMF_Validate(ms, recurse);
ms->nest_level--;
if (GPMF_ERROR_BAD_STRUCTURE == validnest)
{
DBG_MSG("ERROR: invalid nest within %c%c%c%c -- GPMF_ERROR_BAD_STRUCTURE\n", PRINTF_4CC(key));
return GPMF_ERROR_BAD_STRUCTURE;
}
else
{
if (GPMF_ERROR_UNKNOWN_TYPE == validnest)
ret = GPMF_ERROR_UNKNOWN_TYPE;
if (ms->nest_level == 0)
ms->device_count++;
}
ms->pos += size;
nestsize -= 2 + size;
while (ms->pos < ms->buffer_size_longs && nestsize > 0 && ms->buffer[ms->pos] == GPMF_KEY_END)
{
ms->pos++;
nestsize--;
}
}
else
{
ms->pos += 2 + size;
nestsize -= 2 + size;
}
if (ms->pos == ms->buffer_size_longs)
{
ms->pos = currpos;
return ret;
}
}
else
{
if (key == GPMF_KEY_END)
{
do
{
ms->pos++;
nestsize--;
} while (ms->pos < ms->buffer_size_longs && nestsize > 0 && ms->buffer[ms->pos] == 0);
}
else if (ms->nest_level == 0 && ms->device_count > 0)
{
ms->pos = currpos;
return ret;
}
else
{
DBG_MSG("ERROR: bad struct within %c%c%c%c -- GPMF_ERROR_BAD_STRUCTURE\n", PRINTF_4CC(key));
return GPMF_ERROR_BAD_STRUCTURE;
}
}
}
ms->pos = currpos;
return ret;
}
else
{
DBG_MSG("ERROR: Invalid handle -- GPMF_ERROR_MEMORY\n");
return GPMF_ERROR_MEMORY;
}
}
GPMF_ERR GPMF_ResetState(GPMF_stream *ms)
{
if (ms)
{
ms->pos = 0;
ms->nest_level = 0;
ms->device_count = 0;
ms->nest_size[ms->nest_level] = 0;
ms->last_level_pos[ms->nest_level] = 0;
ms->last_seek[ms->nest_level] = 0;
ms->device_id = 0;
ms->device_name[0] = 0;
return GPMF_OK;
}
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_Init(GPMF_stream *ms, uint32_t *buffer, uint32_t datasize)
{
if(ms && buffer && datasize > 0)
{
uint32_t pos = 0;
//Validate DEVC GPMF
while((pos+1) * 4 < datasize && buffer[pos] == GPMF_KEY_DEVICE)
{
uint32_t size = GPMF_DATA_SIZE(buffer[pos+1]);
pos += 2 + (size >> 2);
}
if (pos > 0 && pos * 4 <= datasize)
{
ms->buffer = buffer;
ms->buffer_size_longs = pos;
ms->cbhandle = 0;
GPMF_ResetState(ms);
return GPMF_OK;
}
else
{
return GPMF_ERROR_BAD_STRUCTURE;
}
}
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_CopyState(GPMF_stream *msrc, GPMF_stream *mdst)
{
if (msrc && mdst)
{
memcpy(mdst, msrc, sizeof(GPMF_stream));
return GPMF_OK;
}
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_Next(GPMF_stream *ms, GPMF_LEVELS recurse)
{
if (ms)
{
if (ms->pos+1 < ms->buffer_size_longs)
{
uint32_t key, type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
uint32_t size = (GPMF_DATA_SIZE(ms->buffer[ms->pos + 1]) >> 2);
if (GPMF_OK != IsValidSize(ms, size))
{
if (recurse & GPMF_TOLERANT && recurse & GPMF_RECURSE_LEVELS) // Skip this nest level as the sizes within this level are corrupt.
return SkipLevel(ms);
else
return GPMF_ERROR_BAD_STRUCTURE;
}
if (GPMF_TYPE_NEST == type && GPMF_KEY_DEVICE == ms->buffer[ms->pos] && ms->nest_level == 0)
{
ms->last_level_pos[ms->nest_level] = ms->pos;
ms->nest_size[ms->nest_level] = size;
if (recurse & GPMF_RECURSE_LEVELS)
ms->pos += 2;
else
ms->pos += 2 + size;
}
else
{
if (size + 2 > ms->nest_size[ms->nest_level])
return GPMF_ERROR_BAD_STRUCTURE;
if (recurse & GPMF_RECURSE_LEVELS && type == GPMF_TYPE_NEST)
{
ms->last_level_pos[ms->nest_level] = ms->pos;
ms->pos += 2;
ms->nest_size[ms->nest_level] -= size + 2;
ms->nest_level++;
if (ms->nest_level > GPMF_NEST_LIMIT)
return GPMF_ERROR_BAD_STRUCTURE;
ms->nest_size[ms->nest_level] = size;
}
else
{
if (recurse & GPMF_RECURSE_LEVELS)
{
ms->pos += size + 2;
ms->nest_size[ms->nest_level] -= size + 2;
}
else
{
if (ms->nest_size[ms->nest_level] - (size + 2) > 0)
{
ms->pos += size + 2;
ms->nest_size[ms->nest_level] -= size + 2;
}
else
{
return GPMF_ERROR_LAST;
}
}
}
}
while (ms->pos < ms->buffer_size_longs && ms->nest_size[ms->nest_level] > 0 && ms->buffer[ms->pos] == GPMF_KEY_END)
{
ms->pos++;
ms->nest_size[ms->nest_level]--;
}
while (ms->nest_level > 0 && ms->nest_size[ms->nest_level] == 0)
{
ms->nest_level--;
//if (ms->nest_level == 0)
//{
// ms->device_count++;
//}
}
if (ms->pos < ms->buffer_size_longs)
{
while (ms->pos+1 < ms->buffer_size_longs && ms->nest_size[ms->nest_level] > 0 && ms->buffer[ms->pos] == GPMF_KEY_END)
{
ms->pos++;
ms->nest_size[ms->nest_level]--;
}
if (ms->pos + 1 < ms->buffer_size_longs)
{
key = ms->buffer[ms->pos];
if (!GPMF_VALID_FOURCC(key))
{
if (recurse & GPMF_TOLERANT && recurse & GPMF_RECURSE_LEVELS) // Skip this nest level as the sizes within this level are corrupt.
return SkipLevel(ms);
else
return GPMF_ERROR_BAD_STRUCTURE;
}
if (GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 1]) == 0)
{
if (recurse & GPMF_TOLERANT && recurse & GPMF_RECURSE_LEVELS) // Skip this nest level as the sizes within this level are corrupt.
return SkipLevel(ms);
else
return GPMF_ERROR_BAD_STRUCTURE;
}
type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos+1]);
if (type != GPMF_TYPE_NEST && type != GPMF_TYPE_COMPLEX && type != GPMF_TYPE_COMPRESSED && GPMF_SizeofType((GPMF_SampleType)type) == 0)
{
if (recurse & GPMF_TOLERANT)
return GPMF_Next(ms, recurse);
else
return GPMF_ERROR_UNKNOWN_TYPE;
}
if (key == GPMF_KEY_DEVICE_ID && ms->pos + 2 < ms->buffer_size_longs)
ms->device_id = BYTESWAP32(ms->buffer[ms->pos + 2]);
if (key == GPMF_KEY_DEVICE_NAME)
{
if (ms->pos + 1 >= ms->buffer_size_longs)
return GPMF_ERROR_BAD_STRUCTURE;
size = GPMF_DATA_SIZE(ms->buffer[ms->pos + 1]); // in bytes
if (size > sizeof(ms->device_name) - 1)
size = sizeof(ms->device_name) - 1;
if ((ms->pos + 1 + ((size + 3) >> 2)) >= ms->buffer_size_longs)
return GPMF_ERROR_BAD_STRUCTURE;
memcpy(ms->device_name, &ms->buffer[ms->pos + 2], size);
ms->device_name[size] = 0;
}
}
else
{
// end of buffer
return GPMF_ERROR_BUFFER_END;
}
}
else
{
// end of buffer
return GPMF_ERROR_BUFFER_END;
}
size = (GPMF_DATA_SIZE(ms->buffer[ms->pos + 1]) >> 2);
if (GPMF_OK != IsValidSize(ms, size))
{
if (recurse & GPMF_TOLERANT && recurse & GPMF_RECURSE_LEVELS) // Skip this nest level as the sizes within this level are corrupt.
return SkipLevel(ms);
else
return GPMF_ERROR_BAD_STRUCTURE;
}
return GPMF_OK;
}
else
{
// end of buffer
return GPMF_ERROR_BUFFER_END;
}
}
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_FindNext(GPMF_stream *ms, uint32_t fourcc, GPMF_LEVELS recurse)
{
GPMF_stream prevstate;
if (ms)
{
memcpy(&prevstate, ms, sizeof(GPMF_stream));
if (ms->pos < ms->buffer_size_longs)
{
GPMF_ERR ret = GPMF_OK;
do
{
ret = GPMF_Next(ms, recurse);
if (GPMF_OK == ret)
{
if (ms->buffer[ms->pos] == fourcc)
{
return GPMF_OK; //found match
}
}
} while (GPMF_OK == ret);
memcpy(ms, &prevstate, sizeof(GPMF_stream)); // restore read position
return ret; // the error code returned from GPMF_Next()
}
else
return GPMF_ERROR_BUFFER_END;
}
else
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_Reserved(uint32_t key)
{
if(key == GPMF_KEY_DEVICE)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_DEVICE_ID)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_DEVICE_NAME)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_STREAM)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_STREAM_NAME)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_SI_UNITS)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_UNITS)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_SCALE)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_TYPE)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_TOTAL_SAMPLES)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_TICK)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_TOCK)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_EMPTY_PAYLOADS)
return GPMF_ERROR_RESERVED;
if(key == GPMF_KEY_REMARK)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_MATRIX)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_ORIENTATION_IN)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_ORIENTATION_OUT)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_TIME_STAMP)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_TIME_STAMPS)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_PREFORMATTED)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_TEMPERATURE_C)
return GPMF_ERROR_RESERVED;
if (key == GPMF_KEY_FREESPACE)
return GPMF_ERROR_RESERVED;
return GPMF_OK;
}
uint32_t GPMF_PayloadSampleCount(GPMF_stream *ms)
{
uint32_t count = 0;
if (ms)
{
uint32_t fourcc = GPMF_Key(ms);
GPMF_stream find_stream;
GPMF_CopyState(ms, &find_stream);
if (GPMF_OK == GPMF_FindNext(&find_stream, fourcc, GPMF_CURRENT_LEVEL|GPMF_TOLERANT)) // Count the instances, not the repeats
{
count=2;
while (GPMF_OK == GPMF_FindNext(&find_stream, fourcc, GPMF_CURRENT_LEVEL|GPMF_TOLERANT))
{
count++;
}
}
else
{
count = GPMF_Repeat(ms);
if (count == 0) // this can happen with an empty FACE, yet this is still a FACE fouce
count = 1;
}
}
return count;
}
GPMF_ERR GPMF_SeekToSamples(GPMF_stream *ms)
{
GPMF_stream prevstate;
if (ms)
{
if (ms->pos + 1 < ms->buffer_size_longs)
{
GPMF_ERROR ret = GPMF_OK;
uint32_t size, type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
memcpy(&prevstate, ms, sizeof(GPMF_stream));
if (type == GPMF_TYPE_NEST)
ret = GPMF_Next(ms, GPMF_RECURSE_LEVELS | GPMF_TOLERANT); // open STRM and recurse in
if (GPMF_OK != ret)
{
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return ret;
}
while (GPMF_OK == (ret = GPMF_Next(ms, GPMF_CURRENT_LEVEL | GPMF_TOLERANT)))
{
if (ms->pos + 1 >= ms->buffer_size_longs)
{
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return GPMF_ERROR_BAD_STRUCTURE;
}
size = (GPMF_DATA_SIZE(ms->buffer[ms->pos + 1]) >> 2);
if (GPMF_OK != IsValidSize(ms, size))
{
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return GPMF_ERROR_BAD_STRUCTURE;
}
type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
if (type == GPMF_TYPE_NEST) // Nest with-in nest
{
return GPMF_OK; //found match
}
if (size + 2 == ms->nest_size[ms->nest_level])
{
uint32_t key = GPMF_Key(ms);
if (GPMF_ERROR_RESERVED == GPMF_Reserved(key))
{
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return GPMF_ERROR_FIND;
}
return GPMF_OK; //found match
}
if (ms->pos + size + 2 >= ms->buffer_size_longs)
{
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return GPMF_ERROR_BAD_STRUCTURE;
}
if (ms->buffer[ms->pos] == ms->buffer[ms->pos + size + 2]) // Matching tags
{
return GPMF_OK; //found match
}
}
// restore read position
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return ret;
}
else
return GPMF_ERROR_BUFFER_END;
}
else
return GPMF_ERROR_MEMORY;
}
GPMF_ERR GPMF_FindPrev(GPMF_stream *ms, uint32_t fourcc, GPMF_LEVELS recurse)
{
GPMF_stream prevstate;
if (ms)
{
uint32_t curr_level = ms->nest_level;
memcpy(&prevstate, ms, sizeof(GPMF_stream));
if (ms->pos < ms->buffer_size_longs && curr_level > 0)
{
do
{
ms->last_seek[curr_level] = ms->pos;
ms->pos = ms->last_level_pos[curr_level - 1] + 2;
ms->nest_size[curr_level] += ms->last_seek[curr_level] - ms->pos;
do
{
if (ms->last_seek[curr_level] > ms->pos && ms->buffer[ms->pos] == fourcc)
{
return GPMF_OK; //found match
}
} while (ms->last_seek[curr_level] > ms->pos && GPMF_OK == GPMF_Next(ms, GPMF_CURRENT_LEVEL|(recurse&GPMF_TOLERANT)));
curr_level--;
} while (recurse & GPMF_RECURSE_LEVELS && curr_level > 0);
// restore read position
memcpy(ms, &prevstate, sizeof(GPMF_stream));
return GPMF_ERROR_FIND;
}
else
return GPMF_ERROR_BUFFER_END;
}
else
return GPMF_ERROR_MEMORY;
}
uint32_t GPMF_Key(GPMF_stream *ms)
{
if (ms)
{
uint32_t key = ms->buffer[ms->pos];
return key;
}
return 0;
}
GPMF_SampleType GPMF_Type(GPMF_stream *ms)
{
if (ms && ms->pos+1 < ms->buffer_size_longs)
{
GPMF_SampleType type = (GPMF_SampleType)GPMF_SAMPLE_TYPE(ms->buffer[ms->pos+1]);
if (type == GPMF_TYPE_COMPRESSED && ms->pos+2 < ms->buffer_size_longs)
{
type = (GPMF_SampleType)GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 2]);
}
return type;
}
return GPMF_TYPE_ERROR;
}
uint32_t GPMF_StructSize(GPMF_stream *ms)
{
if (ms && ms->pos+1 < ms->buffer_size_longs)
{
uint32_t ssize = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 1]);
uint32_t type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
if (type == GPMF_TYPE_COMPRESSED && ms->pos+2 < ms->buffer_size_longs)
{
ssize = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 2]);
}
return ssize;
}
return 0;
}
uint32_t GPMF_ElementsInStruct(GPMF_stream *ms)
{
if (ms && ms->pos+1 < ms->buffer_size_longs)
{
uint32_t ssize = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 1]);
uint32_t type = GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
if (type != GPMF_TYPE_NEST && type != GPMF_TYPE_COMPLEX && type != GPMF_TYPE_COMPRESSED)
{
uint32_t tsize = GPMF_SizeofType((GPMF_SampleType)type);
if (tsize > 0)
return ssize / tsize;
else
return 0;
}
if (type == GPMF_TYPE_COMPLEX)
{
GPMF_stream find_stream;
GPMF_CopyState(ms, &find_stream);
if (GPMF_OK == GPMF_FindPrev(&find_stream, GPMF_KEY_TYPE, GPMF_CURRENT_LEVEL|GPMF_TOLERANT))
{
char tmp[64] = "";
uint32_t tmpsize = sizeof(tmp);
char *data = (char *)GPMF_RawData(&find_stream);
uint32_t size = GPMF_RawDataSize(&find_stream);
if (GPMF_OK == GPMF_ExpandComplexTYPE(data, size, tmp, &tmpsize))
return tmpsize;
}
}
if (type == GPMF_TYPE_COMPRESSED && ms->pos+2 < ms->buffer_size_longs)
{
type = (GPMF_SampleType)GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 2]);
ssize = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 2]);
uint32_t tsize = GPMF_SizeofType((GPMF_SampleType)type);
if (tsize > 0)
return ssize / tsize;
else
return 0;
}
}
return 0;
}
uint32_t GPMF_Repeat(GPMF_stream *ms)
{
if (ms && ms->pos+1 < ms->buffer_size_longs)
{
GPMF_SampleType type = (GPMF_SampleType)GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
uint32_t repeat = GPMF_SAMPLES(ms->buffer[ms->pos + 1]);
if(type == GPMF_TYPE_COMPRESSED && ms->pos+2 < ms->buffer_size_longs)
{
repeat = GPMF_SAMPLES(ms->buffer[ms->pos + 2]);
}
return repeat;
}
return 0;
}
uint32_t GPMF_RawDataSize(GPMF_stream *ms)
{
if (ms && ms->pos+1 < ms->buffer_size_longs)
{
uint32_t size = GPMF_DATA_PACKEDSIZE(ms->buffer[ms->pos + 1]);
if (GPMF_OK != IsValidSize(ms, size >> 2)) return 0;
return size;
}
return 0;
}
uint32_t GPMF_FormattedDataSize(GPMF_stream *ms)
{
if (ms && ms->pos + 1 < ms->buffer_size_longs)
{
GPMF_SampleType type = (GPMF_SampleType)GPMF_SAMPLE_TYPE(ms->buffer[ms->pos + 1]);
uint32_t size = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 1])*GPMF_SAMPLES(ms->buffer[ms->pos + 1]);
if (type == GPMF_TYPE_COMPRESSED && ms->pos+2 < ms->buffer_size_longs)
{
size = GPMF_SAMPLE_SIZE(ms->buffer[ms->pos + 2])*GPMF_SAMPLES(ms->buffer[ms->pos + 2]);
}
return size;
}
return 0;
}
uint32_t GPMF_ScaledDataSize(GPMF_stream *ms, GPMF_SampleType type)
{
if (ms && ms->pos + 1 < ms->buffer_size_longs)
{
uint32_t elements = GPMF_ElementsInStruct(ms);
uint32_t samples = GPMF_Repeat(ms);
return GPMF_SizeofType((GPMF_SampleType)type) * elements * samples;
}
return 0;
}
uint32_t GPMF_NestLevel(GPMF_stream *ms)
{
if (ms)
{
return ms->nest_level;
}
return 0;
}
uint32_t GPMF_DeviceID(GPMF_stream *ms)
{
if (ms)
{
return ms->device_id;
}
return 0;
}
GPMF_ERR GPMF_DeviceName(GPMF_stream *ms, char *devicenamebuf, uint32_t devicename_buf_size)
{
if (ms && devicenamebuf)
{
uint32_t len = (uint32_t)strlen(ms->device_name);
if (len >= devicename_buf_size)
return GPMF_ERROR_MEMORY;
memcpy(devicenamebuf, ms->device_name, len);
devicenamebuf[len] = 0;
return GPMF_OK;
}
return GPMF_ERROR_MEMORY;
}
void *GPMF_RawData(GPMF_stream *ms)
{
if (ms)
{
return (void *)&ms->buffer[ms->pos + 2];
}
return NULL;
}
int32_t GPMFTypeEndianSize(int type)
{
int32_t ssize = -1;
switch ((int)type)
{
case GPMF_TYPE_STRING_ASCII: ssize = 1; break;
case GPMF_TYPE_SIGNED_BYTE: ssize = 1; break;
case GPMF_TYPE_UNSIGNED_BYTE: ssize = 1; break;
case GPMF_TYPE_STRING_UTF8: ssize = 1; break;
// These datatype can always be stored in Big-Endian
case GPMF_TYPE_SIGNED_SHORT: ssize = 2; break;
case GPMF_TYPE_UNSIGNED_SHORT: ssize = 2; break;
case GPMF_TYPE_FLOAT: ssize = 4; break;
case GPMF_TYPE_FOURCC: ssize = 1; break;
case GPMF_TYPE_SIGNED_LONG: ssize = 4; break;
case GPMF_TYPE_UNSIGNED_LONG: ssize = 4; break;
case GPMF_TYPE_Q15_16_FIXED_POINT: ssize = 4; break;
case GPMF_TYPE_Q31_32_FIXED_POINT: ssize = 8; break;
case GPMF_TYPE_DOUBLE: ssize = 8; break;
case GPMF_TYPE_SIGNED_64BIT_INT: ssize = 8; break;
case GPMF_TYPE_UNSIGNED_64BIT_INT: ssize = 8; break;
case GPMF_TYPE_GUID: ssize = 1; break; // Do not byte swap
case GPMF_TYPE_UTC_DATE_TIME: ssize = 1; break; // Do not byte swap
//All unknown,complex or larger than 8-bytes store as is:
default: ssize = -1; // unsupported for structsize type
}
return ssize;
}
void ByteSwap2Buffer(uint32_t* input, uint32_t* output, GPMF_SampleType data_type, uint32_t structSize, uint32_t repeat)
{
int32_t i, len = 0, endianSize = GPMFTypeEndianSize(data_type);
if (endianSize == 8) // 64-bit swap required
{
for (i = 0; i < (int32_t)((repeat * structSize + 3) / sizeof(int32_t)); i += 2)
{
output[len++] = BYTESWAP32(input[i + 1]);
output[len++] = BYTESWAP32(input[i]);
}
}
else if (endianSize >= 1)
{
for (i = 0; i < (int32_t)((repeat * structSize + 3) / sizeof(int32_t)); i++)
{
switch (endianSize)
{
case 2: output[len++] = BYTESWAP2x16(input[i]); break;
case 4: output[len++] = BYTESWAP32(input[i]); break;
default: output[len++] = input[i]; break;
}
}
}
}
//find and inplace overwrite a GPMF KLV with new KLV, if the lengths match.
GPMF_ERR GPMF_Modify(GPMF_stream* ms, uint32_t origfourCC, uint32_t newfourCC,
GPMF_SampleType newType, uint32_t newStructSize, uint32_t newRepeat, void* newData)
{
uint32_t dataSizeLongs = (newStructSize * newRepeat + 3) >> 2;
if (ms && ms->pos + 1 + dataSizeLongs < ms->buffer_size_longs)
{
GPMF_stream fs;
GPMF_CopyState(ms, &fs);
uint32_t key = fs.buffer[fs.pos];
uint32_t tsr = fs.buffer[fs.pos + 1];
uint32_t ssize = GPMF_SAMPLE_SIZE(tsr);
uint32_t repeat = GPMF_SAMPLES(tsr);
if (key == origfourCC && (((ssize * repeat + 3) >> 2) == ((newStructSize * newRepeat + 3) >> 2))) // no find required and data will fit
{
fs.buffer[fs.pos] = newfourCC;
fs.buffer[fs.pos + 1] = GPMF_MAKE_TYPE_SIZE_COUNT(newType, newStructSize, newRepeat);
ByteSwap2Buffer((uint32_t*)newData, (uint32_t*)&fs.buffer[fs.pos + 2], newType, newStructSize, newRepeat);
return GPMF_OK;
}
else
{
// search forward from the current position at this level
if (GPMF_OK == GPMF_FindNext(&fs, origfourCC, GPMF_CURRENT_LEVEL|GPMF_TOLERANT))
{
tsr = fs.buffer[fs.pos + 1];
ssize = GPMF_SAMPLE_SIZE(tsr);
repeat = GPMF_SAMPLES(tsr);
if (((ssize * repeat + 3) >> 2) == ((newStructSize * newRepeat + 3) >> 2)) //will the new data fit
{
fs.buffer[fs.pos] = newfourCC;
fs.buffer[fs.pos + 1] = GPMF_MAKE_TYPE_SIZE_COUNT(newType, newStructSize, newRepeat);
ByteSwap2Buffer((uint32_t*)newData, (uint32_t*)&fs.buffer[fs.pos + 2], newType, newStructSize, newRepeat);
return GPMF_OK;
}
return GPMF_ERROR_BAD_STRUCTURE; // sizes don't match
}
// search backward from the current position at this level
else if (GPMF_OK == GPMF_FindPrev(&fs, origfourCC, GPMF_CURRENT_LEVEL|GPMF_TOLERANT))
{
tsr = fs.buffer[fs.pos + 1];
ssize = GPMF_SAMPLE_SIZE(tsr);
repeat = GPMF_SAMPLES(tsr);
if (((ssize * repeat + 3) >> 2) == ((newStructSize * newRepeat + 3) >> 2)) //will the new data fit
{
fs.buffer[fs.pos] = newfourCC;
fs.buffer[fs.pos + 1] = GPMF_MAKE_TYPE_SIZE_COUNT(newType, newStructSize, newRepeat);
ByteSwap2Buffer((uint32_t*)newData, (uint32_t*)&fs.buffer[fs.pos + 2], newType, newStructSize, newRepeat);
return GPMF_OK;
}
return GPMF_ERROR_BAD_STRUCTURE; // sizes don't match
}
else