-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmetadata.c
763 lines (626 loc) · 16 KB
/
metadata.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
/*! @file metadata.c
* @brief
*
* @version 1.0.0
*
* (C) Copyright 2017 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 <stddef.h>
#include "metadata.h"
/*
// Example of how to retrieve an item of metadata
CODEC_ERROR GetMetadataItemPixelAspectRatio(METADATA *metadata,
MetadataPixelAspectRatio *pixel_aspect_ratio_out,
size_t size)
{
CODEC_ERROR error = CODEC_ERROR_OKAY;
if (size != sizeof(MetadataPixelAspectRatio)) {
return CODEC_ERROR_INVALID_ARGUMENT;
}
return GetMetadataItem(metadata,
METADATA_ITEM_PIXEL_ASPECT_RATIO,
pixel_aspect_ratio_out,
size);
}
// Sample code for retrieving all of the keys in a metadata set
CODEC_ERROR DumpMetadata(METADATA *metadata, FILE *logfile)
{
CODEC_ERROR error = CODEC_ERROR_OKAY;
METADATA_KEY key;
METADATA_HEADER header;
// Start at the beginning of the metadata
GetMetadataFirstKey(metadata, &key);
// Loop through the metadata items
while (key != METADATA_KEY_NULL)
{
// Get the header for the item corresponding to the key
GetMetadataHeader(metadata, key, &header);
// Print the metadata item key and size (in bytes)
fprintf(logfile, "Metadata key: 5x%08X, size: %d\n", header.key, header.size);
// Get the key for the next item in the set of metadata
GetMetadataNextKey(metadata, &key);
}
return error;
}
*/
void *MetadataFind(void *data, // Points to the first tag
size_t datasize, // Size of the metadata chunk (in bytes)
METADATA_TAG find_tag, // Metadata tag to find
METADATA_SIZE *retsize, // Return the size of the metadata (in bytes)
METADATA_TYPE *rettype // Return the metadata type code
)
{
uint32_t *idata = data;
int pos = 0;
unsigned int tag;
unsigned int typesize;
int size,offset;
unsigned char type;
if(idata==NULL || datasize==0)
return NULL;
do
{
tag = *idata++; pos += 4;
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
if(find_tag == tag)
{
*rettype = type;
*retsize = size;
return (void *)idata;
}
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
}
while((size_t)pos < datasize);
return NULL;
}
void *MetadataFindAtUniqueFrame(void *data, // Points to the first tag
size_t datasize, // Size of the metadata chunk (in bytes)
uint32_t UFRM,
METADATA_TAG find_tag, // Metadata tag to find
METADATA_SIZE *retsize, // Return the size of the metadata (in bytes)
METADATA_TYPE *rettype // Return the metadata type code
)
{
uint32_t *idata = data;
int pos = 0;
unsigned int tag;
unsigned int typesize;
int size,offset;
unsigned char type;
void *ret = NULL;
int foundCP = 0;
uint32_t frstCP_UFRM = 0;
uint32_t scndCP_UFRM = 0;
if(idata==NULL || datasize==0)
return NULL;
do
{
tag = *idata++; pos += 4;
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
if(find_tag == tag)
{
*rettype = type;
*retsize = size;
ret = (void *)idata; // store the interpolated point here
}
if(ret && (tag & 0xffffff) == 0x4C5443)
{
if(((tag >> 24) & 0xff) >= 'a' && ((tag >> 24) & 0xff) <= 'z')
{
//control point
//uint32_t cpTYPE = idata[0];
uint32_t cpUFRM = idata[4];
void *ldata;
METADATA_SIZE lsize;
METADATA_TYPE lctype;
ldata = MetadataFind(&idata[5], size-24,
find_tag, &lsize, &lctype);
if(ldata)
{
int i,flts = lsize/sizeof(float);
if(UFRM >= cpUFRM)
{
float *src,*dst;
foundCP = 1;
frstCP_UFRM = cpUFRM;
dst = ret;
src = ldata;
for(i=0; i<flts; i++)
*(float *)dst++ = *(float *)src++;
if(frstCP_UFRM == UFRM) // found
return ret;
}
if(cpUFRM > UFRM)
{
float *cp1, *cp2;
if(foundCP == 0) // first control is later in time
{
float *src,*dst;
dst = ret;
src = ldata;
for(i=0; i<flts; i++)
*(float *)dst++ = *(float *)src++;
return ret;
}
scndCP_UFRM = cpUFRM;
cp1 = (float *)ret;
cp2 = (float *)ldata;
flts = lsize/sizeof(float);
for(i=0; i<flts; i++)
{
*cp1 += (*cp2++ - *cp1)*(float)(UFRM-frstCP_UFRM)/(float)(scndCP_UFRM-frstCP_UFRM);
cp1++;
}
return ret;
}
}
}
}
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
}
while((size_t)pos < datasize);
return ret;
}
void *MetadataFindFreeform(void *data, // Points to the first tag
size_t datasize, // Size of the metadata chunk (in bytes)
char *freeform, // Freeform metadata to find
METADATA_SIZE *retsize, // Return the size of the metadata (in bytes)
METADATA_TYPE *rettype // Return the metadata type code
)
{
uint32_t *idata = data;
int pos = 0;
unsigned int tag;
unsigned int typesize;
int size,offset;
unsigned char type;
int len = (int)strlen(freeform);
if(len == 0)
return NULL;
do
{
tag = *idata++; pos += 4;
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
if(TAG_REGISTRY_NAME == tag || TAG_NAME == tag)
{
int testsize = size;
if(testsize > 1 && ((char *)idata)[size-1] == 0)
testsize = (int)strlen((char *)idata);
if(testsize == len)
{
if(0 == strncmp((char *)idata, freeform, testsize))
{
// get next REGV or TAGV
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
tag = *idata++; pos += 4;
if(TAG_REGISTRY_VALUE == tag || TAG_VALUE == tag) // Fixed a situation where TAGN doesn't have a TAGV pair.
{
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
*rettype = type;
*retsize = size;
return (void *)idata;
}
else
{
return NULL;
}
}
}
}
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
} while((size_t)pos < datasize);
return NULL;
}
void *MetaDataFindNext(void *sampledata,
size_t sampledatasize,
void **startmetadata,
void *lastdata,
METADATA_TAG *rettag,
METADATA_SIZE *retsize,
METADATA_TYPE *rettype,
METADATA_FLAGS flags)
{
unsigned int *idata = (unsigned int *)lastdata;
int size,typesize,type,offset,tag;
intptr_t pos;
int datasize;
unsigned short *datasizeptr = (unsigned short *)*startmetadata;
if(flags == 0) // not in file sample metadata
{
datasize = (int)sampledatasize;
pos = (uintptr_t)lastdata - (uintptr_t)sampledata;
}
else
{
datasizeptr -= 5; //10 bytes
datasize = ((int)*datasizeptr);
datasize = (((datasize & 0xff) << 8) | ((datasize & 0xff00) >> 8)) << 2;
pos = (uintptr_t)lastdata - (uintptr_t)*startmetadata;
pos += 8; //previous tag and size
}
idata--; //getsize;
typesize = *idata++;
size = typesize & 0xffffff;
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
if(pos < datasize)
{
tag = *idata++; pos += 4;
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
*rettype = type;
*retsize = size;
*rettag = tag;
return (void *)idata;
}
else if(flags) //if in sample look for the next metadata chumk
{
//void *data;
size_t total_size = 0;
//void *metadatastart;
uint8_t *nexttuplet = (uint8_t *)*startmetadata + datasize - 8;
int remainder = (int)sampledatasize - (int)((uintptr_t)nexttuplet - (uintptr_t)sampledata);
if(remainder > 256)
{
*startmetadata = MetaDataFindFirst(nexttuplet, remainder,
&total_size, rettag, retsize, rettype);
return *startmetadata;
}
return NULL;
}
else
{
return NULL;
}
}
void *MetaDataFindNextOld(void *startmetadata,
size_t datasize,
void *lastdata,
METADATA_TAG *rettag,
METADATA_SIZE *retsize,
METADATA_TYPE *rettype)
{
unsigned int *idata = (unsigned int *)lastdata;
int size,typesize,type,offset,pos,tag;
ptrdiff_t diff = (intptr_t)lastdata - (intptr_t)startmetadata;
pos = (int)diff;
pos += 8; //previous tag and size
idata--; //getsize;
typesize = *idata++;
size = typesize & 0xffffff;
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
if ((size_t)pos < datasize)
{
tag = *idata++; pos += 4;
// if(tag == TAG_FREESPACE)
// {
// return NULL; // no more metadata
// }
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
*rettype = type;
*retsize = size;
*rettag = tag;
return (void *)idata;
}
else
return NULL;
}
void *MetaDataFindFirst(void *data,
size_t datasize,
size_t *retchunksize,
METADATA_TAG *rettag,
METADATA_SIZE *retsize,
METADATA_TYPE *rettype)
{
BITSTREAM myinput, *pinput = &myinput;
TAGVALUE segment;
TAGWORD tag,value;
int error = 0;
//char t[100];
#if 1
InitBitstreamBuffer(pinput, data, datasize, BITSTREAM_ACCESS_READ);
#else
myinput.lpCurrentWord = data;
myinput.nWordsUsed = datasize;
myinput.nBitsFree = BITSTREAM_LONG_SIZE;
#endif
do
{
bool optional = false;
int chunksize = 0;
// Read the next tag value pair from the bitstream
segment = GetSegment(pinput);
tag = segment.tuple.tag;
value = segment.tuple.value;
// Is this an optional tag?
if (tag < 0)
{
tag = NEG(tag);
optional = true;
}
if(tag & 0x2000)
{
chunksize = value;
chunksize &= 0xffff;
chunksize += ((tag&0xff)<<16);
}
else if(tag & 0x4000)
{
chunksize = value;
chunksize &= 0xffff;
}
else if(tag == CODEC_TAG_INDEX)
{
chunksize = value;
chunksize &= 0xffff;
}
else
{
chunksize = 0;
}
if((int)(tag) <= ((int)CODEC_TAG_LAST_NON_SIZED) || tag & 0x6000)
{
int skip = 1;
error = 0;
if(tag == (int)CODEC_TAG_METADATA || tag == (int)CODEC_TAG_METADATA_LARGE)
{
unsigned int *idata = (unsigned int *)pinput->lpCurrentWord;
unsigned int tag;
unsigned int typesize;
//int size;
//int offset;
//unsigned char type;
tag = *idata++;
typesize = *idata++;
if (chunksize * 4 < pinput->nWordsUsed)
{
*rettag = tag;
*rettype = (typesize >> 24) & 0xff;
*retsize = typesize & 0xffffff;
*retchunksize = chunksize * 4;
return (void*)idata;
}
else
return NULL;
}
if((tag & 0xff00) == 0x2200) //sample size
skip = 0;
if((tag & 0xff00) == 0x2300) //uncompressed sample size
skip = 1;
if((tag & 0xff00) == 0x2100) //level
skip = 0;
if(chunksize)
{
if(chunksize*4 > pinput->nWordsUsed || chunksize < 0)
{
break;
}
if(skip)
{
//unsigned int *iptr = (unsigned int *)pinput->lpCurrentWord;
pinput->lpCurrentWord += chunksize*4;
pinput->nWordsUsed -= chunksize*4;
}
}
}
else
{
error = 1;
}
} while(tag != CODEC_TAG_GROUP_TRAILER &&
tag != CODEC_TAG_FRAME_TRAILER &&
pinput->nWordsUsed>0 && !error);
return NULL;
}
int ValidMetadataLength(void *data, size_t len)
{
uint32_t *idata = (uint32_t *)data;
int size,typesize,pos;
METADATA_TAG tag;
if(len < 12)
return 0;
tag = *idata;
if(tag == 0)
return 0;
pos = 0;
do
{
tag = *idata++;
typesize = *idata++;
//type = (typesize >> 24) & 0xff;
size = 8/* tag + typesize*/ + (((typesize & 0xffffff) + 3) & ~0x3);
if ((size_t)(pos + size) <= len)
{
pos += size; idata+=((size-8)>>2);
tag = *idata;
}
else
{
break;
}
} while(tag && (size_t)pos < len);
return pos;
}
void *MetaDataFindInSample(void *data, size_t datasize,
METADATA_TAG findmetadatatag,
METADATA_SIZE *retsize,
METADATA_TYPE *rettype)
{
BITSTREAM myinput, *pinput = &myinput;
TAGVALUE segment;
TAGWORD tag,value;
int error = 0;
//char t[100];
#if 1
InitBitstreamBuffer(pinput, data, datasize, BITSTREAM_ACCESS_READ);
#else
myinput.lpCurrentWord = data;
myinput.nWordsUsed = datasize;
myinput.nBitsFree = BITSTREAM_LONG_SIZE;
#endif
do
{
bool optional = false;
int chunksize = 0;
// Read the next tag value pair from the bitstream
segment = GetSegment(pinput);
tag = segment.tuple.tag;
value = segment.tuple.value;
// Is this an optional tag?
if (tag < 0)
{
tag = NEG(tag);
optional = true;
}
if(tag & 0x2000)
{
chunksize = value;
chunksize &= 0xffff;
chunksize += ((tag&0xff)<<16);
}
else if(tag & 0x4000)
{
chunksize = value;
chunksize &= 0xffff;
}
else if(tag == CODEC_TAG_INDEX)
{
chunksize = value;
chunksize &= 0xffff;
}
else
{
chunksize = 0;
}
if((int)(tag) <= ((int)CODEC_TAG_LAST_NON_SIZED) || tag & 0x6000)
{
int skip = 1;
error = 0;
if(tag == (int)CODEC_TAG_METADATA || tag == (int)CODEC_TAG_METADATA_LARGE)
{
unsigned int *idata = (unsigned int *)pinput->lpCurrentWord;
void *retptr = MetadataFind(idata, chunksize*4, findmetadatatag, retsize, rettype);
if(retptr)
return retptr;
}
if((tag & 0xff00) == 0x2200) //sample size
skip = 0;
if((tag & 0xff00) == 0x2300) //uncompressed sample size
skip = 1;
if((tag & 0xff00) == 0x2100) //level
skip = 0;
if(chunksize)
{
if(chunksize*4 > pinput->nWordsUsed || chunksize < 0)
{
break;
}
if(skip)
{
//unsigned int *iptr = (unsigned int *)pinput->lpCurrentWord;
pinput->lpCurrentWord += chunksize*4;
pinput->nWordsUsed -= chunksize*4;
}
}
}
else
{
error = 1;
}
} while(tag != CODEC_TAG_GROUP_TRAILER &&
tag != CODEC_TAG_FRAME_TRAILER &&
pinput->nWordsUsed>0 && !error);
return NULL;
}
void *MetaDataFindTag(void *data, size_t datasize,
METADATA_TAG findmetadatatag,
METADATA_SIZE *retsize,
METADATA_TYPE *rettype)
{
int pos = 0;
unsigned int tag;
unsigned int typesize;
int size,offset;
unsigned char type;
unsigned int *idata = (unsigned int *)data;
if(idata && datasize)
{
do
{
tag = *idata++; pos += 4;
typesize = *idata++; pos += 4;
type = (typesize >> 24) & 0xff;
size = typesize & 0xffffff;
if(findmetadatatag == tag)
{
*rettype = type;
*retsize = size;
return (void *)idata;
}
offset = (size + 3) & 0xfffffc;
pos += offset;
idata += (offset >> 2);
} while(pos < (int)datasize);
}
return NULL;
}
bool FindMetadata(METADATA *metadata,
METADATA_TAG tag,
void **data_out,
METADATA_SIZE *size_out,
METADATA_TYPE *type_out)
{
METADATA_SIZE size;
METADATA_TYPE type;
// Search for the metadata item with the specified tag
void *data = MetadataFind(metadata->block, metadata->size, tag, &size, &type);
// Found the metadata item?
if (data != NULL)
{
if (data_out != NULL) {
*data_out = data;
}
if (size_out != NULL) {
*size_out = size;
}
if (type_out != NULL) {
*type_out = type;
}
return true;
}
return false;
}