-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathmeta_out.c
2858 lines (2655 loc) · 128 KB
/
meta_out.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
/* meta_out.c */
/* Dump MJ2, JP2 metadata (partial so far) to xml file */
/* Callable from mj2_to_metadata */
/* Contributed to Open JPEG by Glenn Pearson, contract software developer, U.S. National Library of Medicine.
The base code in this file was developed by the author as part of a video archiving
project for the U.S. National Library of Medicine, Bethesda, MD.
It is the policy of NLM (and U.S. government) to not assert copyright.
A non-exclusive copy of this code has been contributed to the Open JPEG project.
Except for copyright, inclusion of the code within Open JPEG for distribution and use
can be bound by the Open JPEG open-source license and disclaimer, expressed elsewhere.
*/
#include <windows.h> /* for time functions */
#include "opj_includes.h"
#include "mj2.h"
#include <time.h>
#include "meta_out.h"
static BOOL notes = TRUE;
static BOOL sampletables = FALSE;
static BOOL raw = TRUE;
static BOOL derived = TRUE;
opj_tcp_t *j2k_default_tcp;
/* Forwards */
int xml_write_overall_header(FILE *file, FILE *xmlout, opj_mj2_t * movie,
unsigned int sampleframe, opj_event_mgr_t *event_mgr);
int xml_write_moov(FILE *file, FILE *xmlout, opj_mj2_t * movie,
unsigned int sampleframe, opj_event_mgr_t *event_mgr);
void uint_to_chars(unsigned int value, char* buf);
void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum, unsigned int sampleframe, opj_event_mgr_t *event_mgr);
void xml_write_tkhd(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum);
void xml_write_udta(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum);
void xml_write_mdia(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum);
void xml_write_stbl(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum);
void UnixTimeToFileTime(time_t t, LPFILETIME pft);
void UnixTimeToSystemTime(time_t t, LPSYSTEMTIME pst);
void xml_time_out(FILE* xmlout, time_t t);
void int16_to_3packedchars(short int value, char* buf);
void xml_write_moov_udta(FILE* xmlout, opj_mj2_t * movie);
void xml_write_free_and_skip(FILE* xmlout, opj_mj2_t * movie);
void xml_write_uuid(FILE* xmlout, opj_mj2_t * movie);
int xml_out_frame(FILE* file, FILE* xmlout, mj2_sample_t *sample,
unsigned int snum, opj_event_mgr_t *event_mgr);
void xml_out_frame_siz(FILE* xmlout, opj_image_t *img, opj_cp_t *cp);
void xml_out_frame_cod(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_coc(FILE* xmlout, opj_tcp_t *tcp,
int numcomps); /* opj_image_t *img); */
BOOL same_component_style(opj_tccp_t *tccp1, opj_tccp_t *tccp2);
void xml_out_frame_qcd(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_qcc(FILE* xmlout, opj_tcp_t *tcp,
int numcomps); /* opj_image_t *img); */
BOOL same_component_quantization(opj_tccp_t *tccp1, opj_tccp_t *tccp2);
void xml_out_frame_rgn(FILE* xmlout, opj_tcp_t *tcp,
int numcomps);/* opj_image_t *img);*/
void xml_out_frame_poc(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_ppm(FILE* xmlout, opj_cp_t *cp);
void xml_out_frame_ppt(FILE* xmlout, opj_tcp_t *tcp);
void xml_out_frame_tlm(FILE*
xmlout); /* j2k_default_tcp is passed globally */ /* NO-OP. TLM NOT SAVED IN DATA STRUCTURE */
void xml_out_frame_plm(FILE*
xmlout); /* j2k_default_tcp is passed globally */ /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE. opt in main; can be used in conjunction with PLT */
void xml_out_frame_plt(FILE* xmlout,
opj_tcp_t *tcp); /* NO-OP. PLM NOT SAVED IN DATA STRUCTURE. opt in main; can be used in conjunction with PLT */
void xml_out_frame_crg(FILE*
xmlout); /* j2k_default_tcp is passed globally */ /* opt in main; */
void xml_out_frame_com(FILE* xmlout,
opj_tcp_t *tcp); /* NO-OP. COM NOT SAVED IN DATA STRUCTURE */ /* opt in main; */
void xml_out_dump_hex(FILE* xmlout, char *data, int data_len, char* s);
void xml_out_dump_hex_and_ascii(FILE* xmlout, char *data, int data_len,
char* s);
void xml_out_frame_jp2h(FILE* xmlout, opj_jp2_t *jp2_struct);
#ifdef NOTYET
/* Shown with cp, extended, as data structure... but it could be a new different one */
void xml_out_frame_jp2i(FILE* xmlout,
opj_cp_t *cp);/* IntellectualProperty 'jp2i' (no restrictions on location) */
void xml_out_frame_xml(FILE* xmlout,
opj_cp_t *cp); /* XML 'xml\040' (0x786d6c20). Can appear multiply */
void xml_out_frame_uuid(FILE* xmlout,
opj_cp_t *cp); /* UUID 'uuid' (top level only) */
void xml_out_frame_uinf(FILE* xmlout,
opj_cp_t *cp); /* UUIDInfo 'uinf', includes UUIDList 'ulst' and URL 'url\40' */
void xml_out_frame_unknown_type(FILE* xmlout, opj_cp_t *cp);
#endif
void xml_write_init(BOOL n, BOOL t, BOOL r, BOOL d)
{
/* Init file globals */
notes = n;
sampletables = t;
raw = r;
derived = d;
}
int xml_write_struct(FILE* file, FILE *xmlout, opj_mj2_t * movie,
unsigned int sampleframe, char* stringDTD, opj_event_mgr_t *event_mgr)
{
if (stringDTD != NULL) {
fprintf(xmlout, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
/* stringDTD is known to start with "SYSTEM " or "PUBLIC " */
/* typical: SYSTEM mj2_to_metadata.dtd */
stringDTD[6] =
'\0'; /* Break into two strings at space, so quotes can be inserted. */
fprintf(xmlout, "<!DOCTYPE MJ2_File %s \"%s\">\n", stringDTD, stringDTD + 7);
stringDTD[6] = ' '; /* restore for sake of debugger or memory allocator */
} else {
fprintf(xmlout, "<?xml version=\"1.0\" standalone=\"yes\"?>\n");
}
fprintf(xmlout, "<MJ2_File>\n");
xml_write_overall_header(file, xmlout, movie, sampleframe, event_mgr);
fprintf(xmlout, "</MJ2_File>");
return 0;
}
/* ------------- */
int xml_write_overall_header(FILE *file, FILE *xmlout, opj_mj2_t * movie,
unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
int i;
char buf[5];
buf[4] = '\0';
fprintf(xmlout,
" <JP2 BoxType=\"jP[space][space]\" Signature=\"0x0d0a870a\" />\n");
// Called after structure initialized by mj2_read_ftyp
fprintf(xmlout, " <FileType BoxType=\"ftyp\">\n");
uint_to_chars(movie->brand, buf);
fprintf(xmlout, " <Brand>%s</Brand>\n",
buf); /* 4 character; BR */
fprintf(xmlout, " <MinorVersion>%u</MinorVersion>\n",
movie->minversion); /* 4 char; MinV */
fprintf(xmlout, " <CompatibilityList Count=\"%d\">\n", movie->num_cl);
for (i = movie->num_cl - 1; i > -1;
i--) { /* read routine stored in reverse order, so let's undo damage */
uint_to_chars(movie->cl[i], buf);
fprintf(xmlout, " <CompatibleBrand>%s</CompatibleBrand>\n",
buf); /*4 characters, each CLi */
}
fprintf(xmlout, " </CompatibilityList>\n");
fprintf(xmlout, " </FileType>\n");
xml_write_moov(file, xmlout, movie, sampleframe, event_mgr);
// To come? <mdat> // This is the container for media data that can also be accessed through track structures,
// so is redundant, and simply not of interest as metadata
// <moof> // Allows incremental build up of movie. Probably not in Simple Profile
xml_write_free_and_skip(xmlout,
movie); /* NO OP so far */ /* May be a place where user squirrels metadata */
xml_write_uuid(xmlout,
movie); /* NO OP so far */ /* May be a place where user squirrels metadata */
return 0;
}
/* ------------- */
int xml_write_moov(FILE *file, FILE *xmlout, opj_mj2_t * movie,
unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
unsigned int tnum;
mj2_tk_t *track;
fprintf(xmlout, " <MovieBox BoxType=\"moov\">\n");
fprintf(xmlout, " <MovieHeader BoxType=\"mvhd\">\n");
fprintf(xmlout, " <CreationTime>\n");
if (raw) {
fprintf(xmlout, " <InSeconds>%u</InSeconds>\n", movie->creation_time);
}
if (notes) {
fprintf(xmlout,
" <!-- Seconds since start of Jan. 1, 1904 UTC (Greenwich) -->\n");
}
/* 2082844800 = seconds between 1/1/04 and 1/1/70 */
/* There's still a time zone offset problem not solved... but spec is ambiguous as to whether stored time
should be local or UTC */
if (derived) {
fprintf(xmlout, " <AsLocalTime>");
xml_time_out(xmlout, movie->creation_time - 2082844800);
fprintf(xmlout, "</AsLocalTime>\n");
}
fprintf(xmlout, " </CreationTime>\n");
fprintf(xmlout, " <ModificationTime>\n");
if (raw) {
fprintf(xmlout, " <InSeconds>%u</InSeconds>\n",
movie->modification_time);
}
if (derived) {
fprintf(xmlout, " <AsLocalTime>");
xml_time_out(xmlout, movie->modification_time - 2082844800);
fprintf(xmlout, "</AsLocalTime>\n");
}
fprintf(xmlout, " </ModificationTime>\n");
fprintf(xmlout, " <Timescale>%d</Timescale>\n", movie->timescale);
if (notes) {
fprintf(xmlout, " <!-- Timescale defines time units in one second -->\n");
}
fprintf(xmlout,
" <Rate>\n"); /* Rate to play presentation (default = 0x00010000) */
if (notes) {
fprintf(xmlout,
" <!-- Rate to play presentation is stored as fixed-point binary 16.16 value. Decimal value is approximation. -->\n");
fprintf(xmlout,
" <!-- Rate is expressed relative to normal (default) value of 0x00010000 (1.0) -->\n");
}
if (raw) {
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n", movie->rate);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n",
(double)movie->rate / (double)0x00010000);
}
fprintf(xmlout, " </Rate>\n");
fprintf(xmlout, " <Duration>\n");
if (raw) {
fprintf(xmlout, " <InTimeUnits>%u</InTimeUnits>\n", movie->duration);
}
if (derived) {
fprintf(xmlout, " <InSeconds>%12.3f</InSeconds>\n",
(double)movie->duration / (double)
movie->timescale); // Make this double later to get fractional seconds
}
fprintf(xmlout, " </Duration>\n");
#ifdef CURRENTSTRUCT
movie->volume = movie->volume << 8;
#endif
fprintf(xmlout, " <Volume>\n");
if (notes) {
fprintf(xmlout,
" <!-- Audio volume stored as fixed-point binary 8.8 value. Decimal value is approximation. -->\n");
fprintf(xmlout,
" <!-- Full, normal (default) value is 0x0100 (1.0) -->\n");
}
if (raw) {
fprintf(xmlout, " <AsHex>0x%04x</AsHex>\n", movie->volume);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%6.3f</AsDecimal>\n",
(double)movie->volume / (double)0x0100);
}
fprintf(xmlout, " </Volume>\n");
#ifdef CURRENTSTRUCT
if (notes) {
fprintf(xmlout,
" <!-- Current m2j_to_metadata implementation always shows bits to right of decimal as zeroed. -->\n");
}
movie->volume = movie->volume >> 8;
#endif
/* Transformation matrix for video */
fprintf(xmlout, " <TransformationMatrix>\n");
if (notes) {
fprintf(xmlout,
" <!-- 3 x 3 Video Transformation Matrix {a,b,u,c,d,v,x,y,w}. Required: u=0, v=0, w=1 -->\n");
fprintf(xmlout,
" <!-- Maps decompressed point (p,q) to rendered point (ap + cq + x, bp + dq + y) -->\n");
fprintf(xmlout,
" <!-- Stored as Fixed Point Hex: all are binary 16.16, except u,v,w are 2.30 -->\n");
fprintf(xmlout,
" <!-- Unity = 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 -->\n");
}
fprintf(xmlout, " <TMa>0x%08x</TMa>\n", movie->trans_matrix[0]);
fprintf(xmlout, " <TMb>0x%08x</TMb>\n", movie->trans_matrix[1]);
fprintf(xmlout, " <TMu>0x%08x</TMu>\n", movie->trans_matrix[2]);
fprintf(xmlout, " <TMc>0x%08x</TMc>\n", movie->trans_matrix[3]);
fprintf(xmlout, " <TMd>0x%08x</TMd>\n", movie->trans_matrix[4]);
fprintf(xmlout, " <TMv>0x%08x</TMv>\n", movie->trans_matrix[5]);
fprintf(xmlout, " <TMx>0x%08x</TMx>\n", movie->trans_matrix[6]);
fprintf(xmlout, " <TMy>0x%08x</TMy>\n", movie->trans_matrix[7]);
fprintf(xmlout, " <TMw>0x%08x</TMw>\n", movie->trans_matrix[8]);
fprintf(xmlout, " </TransformationMatrix>\n");
fprintf(xmlout, " </MovieHeader>\n");
fprintf(xmlout, " <Statistics>\n");
fprintf(xmlout, " <TracksFound>\n");
fprintf(xmlout, " <Video>%d</Video>\n", movie->num_vtk);
fprintf(xmlout, " <Audio>%d</Audio>\n", movie->num_stk);
fprintf(xmlout, " <Hint>%d</Hint>\n", movie->num_htk);
if (notes) {
fprintf(xmlout,
" <!-- Hint tracks for streaming video are not part of MJ2, but are a defined extension. -->\n");
}
/* See Part 3 Amend 2 Section 4.2 for relation of MJ2 to Part 12 Sections 7 and 10 hints */
fprintf(xmlout, " </TracksFound>\n");
fprintf(xmlout, " </Statistics>\n");
/* Idea for the future: It would be possible to add code to verify that the file values:
1) are legal and self-consistent
2) comply with particular JP2 and/or MJ2 profiles.
This could be reported here as additional XML elements */
// Find first video track
tnum = 0;
while (movie->tk[tnum].track_type != 0) {
tnum ++;
}
track = &(movie->tk[tnum]);
// For now, output info on first video track
xml_write_trak(file, xmlout, track, tnum, sampleframe, event_mgr);
// to come: <MovieExtends mvek> // possibly not in Simple Profile
xml_write_moov_udta(xmlout,
movie); /* NO OP so far */ /* <UserDataBox udta> contains <CopyrightBox cprt> */
fprintf(xmlout, " </MovieBox>\n");
return 0;
}
/* --------------- */
void uint_to_chars(unsigned int value, char* buf)
{
/* buf is at least char[5] */
int i;
for (i = 3; i >= 0; i--) {
buf[i] = (value & 0x000000ff);
value = (value >> 8);
}
buf[4] = '\0'; /* Precautionary */
}
/* ------------- */
/* WINDOWS SPECIFIC */
void UnixTimeToFileTime(time_t t, LPFILETIME pft)
{
/* Windows specific. From MS Q167296 */
/* 'time_t' represents seconds since midnight January 1, 1970 UTC (coordinated universal time). */
/* 64-bit FILETIME structure represents the number of 100-nanosecond intervals since January 1, 1601 UTC (coordinate universal time). */
LONGLONG ll; /* LONGLONG is a 64-bit value. */
ll = Int32x32To64(t, 10000000) + 116444736000000000;
pft->dwLowDateTime = (DWORD)ll;
/* pft->dwLowDateTime = (DWORD)(0x00000000ffffffff & ll); */
pft->dwHighDateTime = (DWORD)(ll >> 32);
}
// Once the UNIX time is converted to a FILETIME structure,
// other Win32 time formats can be easily obtained by using Win32 functions such
// as FileTimeToSystemTime() and FileTimeToDosDateTime().
/* ------------- */
void UnixTimeToSystemTime(time_t t, LPSYSTEMTIME pst)
{
/* Windows specific */
FILETIME ft;
UnixTimeToFileTime(t, &ft);
FileTimeToLocalFileTime(&ft, &ft); /* Adjust from UTC to local time zone */
FileTimeToSystemTime(&ft, pst);
}
/* ------------- */
void xml_time_out(FILE* xmlout, time_t t)
{
/* Windows specific */
SYSTEMTIME st;
char szLocalDate[255], szLocalTime[255];
UnixTimeToSystemTime(t, &st);
GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szLocalDate, 255);
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szLocalTime, 255);
fprintf(xmlout, "%s %s", szLocalDate, szLocalTime);
}
/* END WINDOWS SPECIFIC */
/* ------------- */
void xml_write_moov_udta(FILE* xmlout, opj_mj2_t * movie)
{
/* Compare with xml_write_udta */
#ifdef NOTYET
/* NO-OP so far. Optional UserData 'udta' (zero or one in moov or each trak)
can contain multiple Copyright 'cprt' with different language codes */
/* There may be nested non-standard boxes within udta */
IMAGINE movie->udta, movie->copyright_count,
movie->copyright_language[i](array of 16bit ints),
movie->copyright_notice[i](array of buffers)
PROBABLY ALSO NEED movie->udta_len or special handler for non - standard boxes
char buf[5];
int i;
if (movie->udta != 1) {
return; /* Not present */
}
fprintf(xmlout, " <UserData BoxType=\"udta\">\n");
for (i = 0; i < movie->copyright_count; i++) {
fprintf(xmlout, " <Copyright BoxType=\"cprt\"> Instance=\"%d\">\n",
i + 1);
int16_to_3packedchars((short int)movie->copyright_languages[i], buf);
fprintf(xmlout, " <Language>%s</Language>\n", buf); /* 3 chars */
fprintf(xmlout, " <Notice>%s</Notice>\n", movie->copyright_notices[i]);
fprintf(xmlout, " </Copyright>\n", i + 1);
}
/* TO DO: Non-standard boxes */
fprintf(xmlout, " </UserData>\n");
#endif
}
void xml_write_free_and_skip(FILE* xmlout, opj_mj2_t * movie)
{
#ifdef NOTYET
/* NO-OP so far. There can be zero or more instances of free and/or skip
at the top level of the file. This may be a place where the user squirrel's metadata.
Let's assume unstructured, and do a dump */
IMAGINE movie->free_and_skip, movie->free_and_skip_count,
movie->free_and_skip_content[i](array of buffers),
movie->free_and_skip_len[i](array of ints), movie->is_skip[i](array of BOOL)
int i;
if (movie->free_and_skip != 1) {
return; /* Not present */
}
for (i = 0; i < movie->free_and_skip_count; i++) {
if (movie->is_skip[i]) {
fprintf(xmlout, " <Skip BoxType=\"skip\">\n");
} else {
fprintf(xmlout, " <Free BoxType=\"free\">\n");
}
xml_out_dump_hex_and_ascii(xmlout, movie->free_and_skip_contents[i],
movie->free_and_skip_len[i]);
if (movie->is_skip[i]) {
fprintf(xmlout, " </Skip>\n");
} else {
fprintf(xmlout, " </Free>\n");
}
}
#endif
}
void xml_write_uuid(FILE* xmlout, opj_mj2_t * movie)
{
/* Universal Unique IDs of 16 bytes. */
#ifdef NOTYET
/* NO-OP so far. There can be zero or more instances of private uuid boxes in a file.
This function supports the top level of the file, but uuid may be elsewhere [not yet supported].
This may be a place where the user squirrel's metadata. Let's assume unstructured, and do a dump */
IMAGINE movie->uuid, movie->uuid_count,
movie->uuid_content[i](array of buffers),
movie->uuid_len[i](array of ints),
movie->uuid_type[i](array of 17 - byte(16 + null termination) buffers)
int i;
if (movie->uuid != 1) {
return; /* Not present */
}
for (i = 0; i < movie->uuid_count; i++) {
fprintf(xmlout, " <PrivateExtension BoxType=\"uuid\" UUID=\"%s\">\n",
movie->uuid_type[i]);
// See Part III section 5.2.1, 6.1, 6.2
xml_out_dump_hex_and_ascii(xmlout, movie->uuid_contents[i], movie->uuid_len[i]);
fprintf(xmlout, " </PrivateExtension>\n");
}
#endif
}
/* ------------- */
void xml_write_trak(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum, unsigned int sampleframe, opj_event_mgr_t *event_mgr)
{
fprintf(xmlout, " <Track BoxType=\"trak\" Instance=\"%d\">\n", tnum);
xml_write_tkhd(file, xmlout, track, tnum);
// TO DO: TrackReferenceContainer 'tref' just used in hint track
// TO DO: EditListContainer 'edts', contains EditList 'elst' with media-time, segment-duration, media-rate
xml_write_mdia(file, xmlout, track, tnum);
xml_write_udta(file, xmlout, track,
tnum); // NO-OP so far. Optional UserData 'udta', can contain multiple Copyright 'cprt'
if (track->track_type == 0) { /* Only do for visual track */
/* sampleframe is from user option -f. 1 = first frame */
/* sampleframe of 0 is a user requests: no jp2 header */
/* Treat out-of-bounds values in the same way */
if (sampleframe > 0 && sampleframe <= track->num_samples) {
mj2_sample_t *sample;
unsigned int snum;
snum = sampleframe - 1;
// Someday maybe do a smart range scan... for (snum=0; snum < track->num_samples; snum++){
// fprintf(stdout,"Frame %d: ",snum+1);
sample = &track->sample[snum];
if (xml_out_frame(file, xmlout, sample, snum, event_mgr)) {
return; /* Not great error handling here */
}
}
}
fprintf(xmlout, " </Track>\n");
}
/* ------------- */
void xml_write_tkhd(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum)
{
fprintf(xmlout, " <TrackHeader BoxType=\"tkhd\">\n");
if (notes) {
fprintf(xmlout,
" <!-- Not shown here: CreationTime, ModificationTime, Duration. -->\n");
fprintf(xmlout,
" <!-- These 3 fields are reported under MediaHeader below. When reading these 3, -->\n");
fprintf(xmlout,
" <!-- m2j_to_metadata currently doesn't distinguish between TrackHeader and MediaHeader source. -->\n");
fprintf(xmlout,
" <!-- If both found, value read from MediaHeader is used. -->\n");
}
fprintf(xmlout, " <TrackID>%u</TrackID>\n", track->track_ID);
if (track->track_type == 0) { /* For visual track */
fprintf(xmlout, " <TrackLayer>%d</TrackLayer>\n", track->layer);
if (notes) {
fprintf(xmlout,
" <!-- front-to-back ordering of video tracks. 0 = normal, -1 is closer, etc. -->\n");
}
}
if (track->track_type != 0) { /* volume irrelevant for visual track */
#ifdef CURRENTSTRUCT
track->volume = track->volume << 8;
#endif
fprintf(xmlout, " <Volume>\n");
if (notes) {
fprintf(xmlout,
" <!-- Track audio volume stored as fixed-point binary 8.8 value. Decimal value is approximation. -->\n");
fprintf(xmlout,
" <!-- Full, normal (default) value is 0x0100 (1.0) -->\n");
}
if (raw) {
fprintf(xmlout, " <AsHex>0x%04x</AsHex>\n", track->volume);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%6.3f</AsDecimal>\n",
(double)track->volume / (double)0x0100);
}
fprintf(xmlout, " </Volume>\n");
#ifdef CURRENTSTRUCT
if (notes) {
fprintf(xmlout,
" <!-- Current m2j_to_metadata implementation always shows bits to right of decimal as zeroed. -->\n");
}
track->volume = track->volume >> 8;
#endif
}
if (track->track_type == 0) {
/* Transformation matrix for video */
fprintf(xmlout, " <TransformationMatrix>\n");
if (notes) {
fprintf(xmlout,
" <!-- Comments about matrix in MovieHeader apply here as well. -->\n");
fprintf(xmlout,
" <!-- This matrix is applied before MovieHeader one. -->\n");
}
fprintf(xmlout, " <TMa>0x%08x</TMa>\n", track->trans_matrix[0]);
fprintf(xmlout, " <TMb>0x%08x</TMb>\n", track->trans_matrix[1]);
fprintf(xmlout, " <TMu>0x%08x</TMu>\n", track->trans_matrix[2]);
fprintf(xmlout, " <TMc>0x%08x</TMc>\n", track->trans_matrix[3]);
fprintf(xmlout, " <TMd>0x%08x</TMd>\n", track->trans_matrix[4]);
fprintf(xmlout, " <TMv>0x%08x</TMv>\n", track->trans_matrix[5]);
fprintf(xmlout, " <TMx>0x%08x</TMx>\n", track->trans_matrix[6]);
fprintf(xmlout, " <TMy>0x%08x</TMy>\n", track->trans_matrix[7]);
fprintf(xmlout, " <TMw>0x%08x</TMw>\n", track->trans_matrix[8]);
fprintf(xmlout, " </TransformationMatrix>\n");
}
#ifdef CURRENTSTRUCT
track->w = track->w << 16;
track->h = track->h << 16;
#endif
if (notes) {
fprintf(xmlout,
" <!-- Width and Height in pixels are for the presentation; frames will be scaled to this. -->\n");
fprintf(xmlout,
" <!-- Both stored as fixed-point binary 16.16 values. Decimal values are approximations. -->\n");
}
fprintf(xmlout, " <Width>\n");
if (raw) {
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n", track->w);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n",
(double)track->w / (double)
0x00010000); /* Rate to play presentation (default = 0x00010000) */
}
fprintf(xmlout, " </Width>\n");
fprintf(xmlout, " <Height>\n");
if (raw) {
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n", track->h);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n",
(double)track->h / (double)
0x00010000); /* Rate to play presentation (default = 0x00010000) */
}
fprintf(xmlout, " </Height>\n");
#ifdef CURRENTSTRUCT
if (notes) {
fprintf(xmlout,
" <!-- Current m2j_to_metadata implementation always shows bits to right of decimal as zeroed. -->\n");
fprintf(xmlout,
" <!-- Also, width and height values shown here will actually be those read from track's <VisualSampleEntry> if given. -->\n");
}
track->w = track->w >> 16;
track->h = track->h >> 16;
#endif
fprintf(xmlout, " </TrackHeader>\n");
}
/* ------------- */
void xml_write_udta(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum)
{
/* NO-OP so far. Optional UserData 'udta' (zero or one in moov or each trak)
can contain multiple Copyright 'cprt' with different language codes */
/* There may be nested non-standard boxes within udta */
#ifdef NOTYET
IMAGINE track->udta, track->copyright_count,
track->copyright_language[i](array of 16bit ints),
track->copyright_notice[i](array of buffers)
PROBABLY ALSO NEED track->udta_len or special handler for non - standard boxes
char buf[5];
int i;
if (track->udta != 1) {
return; /* Not present */
}
fprintf(xmlout, " <UserData BoxType=\"udta\">\n");
for (i = 0; i < track->copyright_count; i++) {
fprintf(xmlout, " <Copyright BoxType=\"cprt\"> Instance=\"%d\">\n",
i + 1);
int16_to_3packedchars((short int)track->copyright_languages[i], buf);
fprintf(xmlout, " <Language>%s</Language>\n", buf); /* 3 chars */
fprintf(xmlout, " <Notice>%s</Notice>\n",
track->copyright_notices[i]);
fprintf(xmlout, " </Copyright>\n", i + 1);
}
/* TO DO: Non-standard boxes */
fprintf(xmlout, " </UserData>\n");
#endif
}
/* ------------- */
void xml_write_mdia(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum)
{
char buf[5];
int i, k;
buf[4] = '\0';
fprintf(xmlout, " <Media BoxType=\"mdia\">\n");
fprintf(xmlout, " <MediaHeader BoxType=\"mdhd\">\n");
fprintf(xmlout, " <CreationTime>\n");
if (raw) {
fprintf(xmlout, " <InSeconds>%u</InSeconds>\n",
track->creation_time);
}
if (notes) {
fprintf(xmlout,
" <!-- Seconds since start of Jan. 1, 1904 UTC (Greenwich) -->\n");
}
/* 2082844800 = seconds between 1/1/04 and 1/1/70 */
/* There's still a time zone offset problem not solved... but spec is ambiguous as to whether stored time
should be local or UTC */
if (derived) {
fprintf(xmlout, " <AsLocalTime>");
xml_time_out(xmlout, track->creation_time - 2082844800);
fprintf(xmlout, "</AsLocalTime>\n");
}
fprintf(xmlout, " </CreationTime>\n");
fprintf(xmlout, " <ModificationTime>\n");
if (raw) {
fprintf(xmlout, " <InSeconds>%u</InSeconds>\n",
track->modification_time);
}
if (derived) {
fprintf(xmlout, " <AsLocalTime>");
xml_time_out(xmlout, track->modification_time - 2082844800);
fprintf(xmlout, "</AsLocalTime>\n");
}
fprintf(xmlout, " </ModificationTime>\n");
fprintf(xmlout, " <Timescale>%d</Timescale>\n", track->timescale);
if (notes) {
fprintf(xmlout,
" <!-- Timescale defines time units in one second -->\n");
}
fprintf(xmlout, " <Duration>\n");
if (raw) {
fprintf(xmlout, " <InTimeUnits>%u</InTimeUnits>\n",
track->duration);
}
if (derived) {
fprintf(xmlout, " <InSeconds>%12.3f</InSeconds>\n",
(double)track->duration / (double)
track->timescale); // Make this double later to get fractional seconds
}
fprintf(xmlout, " </Duration>\n");
int16_to_3packedchars((short int)track->language, buf);
fprintf(xmlout, " <Language>%s</Language>\n", buf); /* 3 chars */
fprintf(xmlout, " </MediaHeader>\n");
fprintf(xmlout, " <HandlerReference BoxType=\"hdlr\">\n");
switch (track->track_type) {
case 0:
fprintf(xmlout,
" <HandlerType Code=\"vide\">video media track</HandlerType>\n");
break;
case 1:
fprintf(xmlout, " <HandlerType Code=\"soun\">Sound</HandlerType>\n");
break;
case 2:
fprintf(xmlout, " <HandlerType Code=\"hint\">Hint</HandlerType>\n");
break;
}
if (notes) {
fprintf(xmlout,
" <!-- String value shown is not actually read from file. -->\n");
fprintf(xmlout,
" <!-- Shown value is one used for our encode. -->\n");
}
fprintf(xmlout, " </HandlerReference>\n");
fprintf(xmlout, " <MediaInfoContainer BoxType=\"minf\">\n");
switch (track->track_type) {
case 0:
fprintf(xmlout, " <VideoMediaHeader BoxType=\"vmhd\">\n");
fprintf(xmlout, " <GraphicsMode>0x%02x</GraphicsMode>\n",
track->graphicsmode);
if (notes) {
fprintf(xmlout, " <!-- Enumerated values of graphics mode: -->\n");
fprintf(xmlout, " <!-- 0x00 = copy (over existing image); -->\n");
fprintf(xmlout,
" <!-- 0x24 = transparent; 'blue-screen' this image using opcolor; -->\n");
fprintf(xmlout,
" <!-- 0x100 = alpha; alpha-blend this image -->\n");
/* fprintf(xmlout," <!-- 0x101 = whitealpha; alpha-blend this image, which has been blended with white; -->\n"); This was evidently dropped upon amendment */
fprintf(xmlout,
" <!-- 0x102 = pre-multiplied black alpha; image has been already been alpha-blended with black. -->\n");
fprintf(xmlout,
" <!-- 0x110 = component alpha; blend alpha channel(s) and color channels individually. -->\n");
}
fprintf(xmlout, " <Opcolor>\n");
fprintf(xmlout, " <Red>0x%02x</Red>\n", track->opcolor[0]);
fprintf(xmlout, " <Green>0x%02x</Green>\n", track->opcolor[1]);
fprintf(xmlout, " <Blue>0x%02x</Blue>\n", track->opcolor[2]);
fprintf(xmlout, " </Opcolor>\n");
fprintf(xmlout, " </VideoMediaHeader>\n");
break;
case 1:
fprintf(xmlout, " <SoundMediaHeader BoxType=\"smhd\">\n");
#ifdef CURRENTSTRUCT
track->balance = track->balance << 8;
#endif
fprintf(xmlout, " <Balance>\n");
if (notes) {
fprintf(xmlout,
" <!-- Track audio balance fixes mono track in stereo space. -->\n");
fprintf(xmlout,
" <!-- Stored as fixed-point binary 8.8 value. Decimal value is approximation. -->\n");
fprintf(xmlout,
" <!-- 0.0 = center, -1.0 = full left, 1.0 = full right -->\n");
}
if (raw) {
fprintf(xmlout, " <AsHex>0x%04x</AsHex>\n", track->balance);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%6.3f</AsDecimal>\n",
(double)track->balance / (double)0x0100);
}
fprintf(xmlout, " </Balance>\n");
#ifdef CURRENTSTRUCT
if (notes) {
fprintf(xmlout,
" <!-- Current m2j_to_metadata implementation always shows bits to right of decimal as zeroed. -->\n");
}
track->balance = track->balance >> 8;
#endif
fprintf(xmlout, " </SoundMediaHeader>\n");
break;
case 2:
fprintf(xmlout, " <HintMediaHeader BoxType=\"hmhd\">\n");
fprintf(xmlout, " <MaxPDU_Size>%d</MaxPDU_Size>\n",
track->maxPDUsize);
if (notes) {
fprintf(xmlout,
" <!-- Size in bytes of largest PDU in this hint stream. -->\n");
}
fprintf(xmlout, " <AvgPDU_Size>%d</AvgPDU_Size>\n",
track->avgPDUsize);
if (notes) {
fprintf(xmlout,
" <!-- Average size in bytes of a PDU over the entire presentation. -->\n");
}
fprintf(xmlout, " <MaxBitRate>%d</MaxBitRate>\n",
track->maxbitrate);
if (notes) {
fprintf(xmlout,
" <!-- Maximum rate in bits per second over any window of 1 second. -->\n");
}
fprintf(xmlout, " <AvgBitRate>%d</AvgBitRate>\n",
track->avgbitrate);
if (notes) {
fprintf(xmlout,
" <!-- Averate rate in bits per second over the entire presentation. -->\n");
}
fprintf(xmlout, " <SlidingAvgBit>%d</SlidingAvgBitRate>\n",
track->slidingavgbitrate);
if (notes) {
fprintf(xmlout,
" <!-- Maximum rate in bits per second over any window of one minute. -->\n");
}
fprintf(xmlout, " </HintMediaHeader>\n");
break;
}
fprintf(xmlout, " <DataInfo BoxType=\"dinf\">\n");
fprintf(xmlout,
" <DataReference BoxType=\"dref\" URL_Count=\"%d\" URN_Count=\"%d\">\n",
track->num_url, track->num_urn); // table w. flags, URLs, URNs
// Data structure does not distinguish between single URL, single URN, or DREF table or URLs & URNs.
// We could infer those, but for now just present everything as a DREF table.
if (notes) {
fprintf(xmlout,
" <!-- No entries here mean that file is self-contained, as required by Simple Profile. -->\n");
}
for (k = 0; k < track->num_url; k++) {
fprintf(xmlout,
" <DataEntryUrlBox BoxType=\"url[space]\">\n"); // table w. flags, URLs, URNs
if (notes) {
fprintf(xmlout,
" <!-- Only the first 16 bytes of URL location are recorded in mj2_to_metadata data structure. -->\n");
}
for (i = 0; i < 4; i++) {
uint_to_chars(track->url[track->num_url].location[i], buf);
fprintf(xmlout, " <Location>%s</Location>\n");
}
fprintf(xmlout,
" </DataEntryUrlBox>\n"); // table w. flags, URLs, URNs
}
for (k = 0; k < track->num_urn; k++) {
fprintf(xmlout,
" <DataEntryUrnBox BoxType=\"urn[space]\">\n"); // table w. flags, URLs, URNs
// Only the first 16 bytes are recorded in the data structure currently.
if (notes) {
fprintf(xmlout,
" <!-- Only the first 16 bytes each of URN name and optional location are recorded in mj2_to_metadata data structure. -->\n");
}
fprintf(xmlout, " <Name>");
for (i = 0; i < 4; i++) {
uint_to_chars(track->urn[track->num_urn].name[i], buf);
fprintf(xmlout, "%s", buf);
}
fprintf(xmlout, "</Name>\n");
fprintf(xmlout, " <Location>");
for (i = 0; i < 4; i++) {
uint_to_chars(track->urn[track->num_urn].location[i], buf);
fprintf(xmlout, "%s");
}
fprintf(xmlout, "</Location>\n");
fprintf(xmlout, " </DataEntryUrnBox>\n");
}
fprintf(xmlout, " </DataReference>\n");
fprintf(xmlout, " </DataInfo>\n");
xml_write_stbl(file, xmlout, track, tnum); /* SampleTable */
fprintf(xmlout, " </MediaInfoContainer>\n");
fprintf(xmlout, " </Media>\n");
}
/* ------------- */
void xml_write_stbl(FILE* file, FILE* xmlout, mj2_tk_t *track,
unsigned int tnum)
{
char buf[5], buf33[33];
int i, len;
buf[4] = '\0';
fprintf(xmlout, " <SampleTable BoxType=\"stbl\">\n");
if (notes) {
fprintf(xmlout,
" <!-- What follows are specific instances of generic SampleDescription BoxType=\"stsd\" -->\n");
}
switch (track->track_type) {
case 0:
// There could be multiple instances of this, but "entry_count" is just a local at read-time.
// And it's used wrong, too, as count of just visual type, when it's really all 3 types.
// This is referred to as "smj2" within mj2.c
fprintf(xmlout, " <VisualSampleEntry BoxType=\"mjp2\">\n");
if (notes) {
fprintf(xmlout,
" <!-- If multiple instances of this box, only first is shown here. -->\n");
fprintf(xmlout,
" <!-- Width and Height are in pixels. Unlike the Track Header, there is no fractional part. -->\n");
fprintf(xmlout,
" <!-- In mj2_to_metadata implementation, the values are not represented separately from Track Header's values. -->\n");
}
/* No shifting required. If CURRENTSTRUCT gets changed, then may need to revisit treatment of these */
fprintf(xmlout, " <WidthAsInteger>%d</WidthAsInteger>\n",
track->w);
fprintf(xmlout, " <HeightAsInteger>%d</HeightAsInteger>\n",
track->h);
// Horizresolution and vertresolution don't require shifting, already stored right in CURRENTSTRUCT
if (notes) {
fprintf(xmlout,
" <!-- Resolutions are in pixels per inch, for the highest-resolution component (typically luminance). -->\n");
fprintf(xmlout,
" <!-- Both stored as fixed-point binary 16.16 values. Decimal values are approximations. -->\n");
fprintf(xmlout,
" <!-- Typical value for both resolutions is 0x00480000 (72.0) -->\n");
}
fprintf(xmlout, " <HorizontalRes>\n");
if (raw) {
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n",
track->horizresolution);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n",
(double)track->horizresolution / (double)
0x00010000); /* Rate to play presentation (default = 0x00010000) */
}
fprintf(xmlout, " </HorizontalRes>\n");
fprintf(xmlout, " <VerticalRes>\n");
if (raw) {
fprintf(xmlout, " <AsHex>0x%08x</AsHex>\n",
track->vertresolution);
}
if (derived) {
fprintf(xmlout, " <AsDecimal>%12.6f</AsDecimal>\n",
(double)track->vertresolution / (double)
0x00010000); /* Rate to play presentation (default = 0x00010000) */
}
fprintf(xmlout, " </VerticalRes>\n");
buf33[0] = '\0';
for (i = 0; i < 8; i++) {
uint_to_chars((unsigned int)track->compressorname[i], buf);
strcat(buf33,
buf); /* This loads up (4 * 8) + 1 chars, but trailing ones are usually junk */
}
len = (int)
buf33[0]; /* First byte has string length in bytes. There may be garbage beyond it. */
buf33[len + 1] = '\0'; /* Suppress it */
fprintf(xmlout, " <CompressorName>%s</CompressorName>\n",
buf33 + 1); /* Start beyond first byte */
if (notes) {
fprintf(xmlout,
" <!-- Compressor name for debugging. Standard restricts max length to 31 bytes. -->\n");
fprintf(xmlout,
" <!-- Usually blank or \"Motion JPEG2000\" -->\n");
}
fprintf(xmlout, " <Depth>0x%02x</Depth>\n", track->depth);
if (notes) {
fprintf(xmlout, " <!-- Depth is: -->\n");
fprintf(xmlout,
" <!-- 0x20: alpha channels present (color or grayscale) -->\n");
fprintf(xmlout, " <!-- 0x28: grayscale without alpha -->\n");
fprintf(xmlout, " <!-- 0x18: color without alpha -->\n");
}
xml_out_frame_jp2h(xmlout, &(track->jp2_struct)); /* JP2 Header */
/* Following subboxes are optional */
fprintf(xmlout, " <FieldCoding BoxType=\"fiel\">\n");
fprintf(xmlout, " <FieldCount>%d</FieldCount>\n",
(unsigned int)track->fieldcount); /* uchar as 1 byte uint */
if (notes) {
fprintf(xmlout, " <!-- Must be either 1 or 2 -->\n");
}
fprintf(xmlout, " <FieldOrder>%d</FieldOrder>\n",
(unsigned int)track->fieldorder); /* uchar as 1 byte uint */
if (notes) {
fprintf(xmlout,
" <!-- When FieldCount=2, FieldOrder means: -->\n");
fprintf(xmlout, " <!-- 0: Field coding unknown -->\n");
fprintf(xmlout,
" <!-- 1: Field with topmost line is stored first in sample; fields are in temporal order -->\n");
fprintf(xmlout,
" <!-- 6: Field with topmost line is stored second in sample; fields are in temporal order -->\n");
fprintf(xmlout,
" <!-- Defaults: FieldCount=1, FieldOrder=0 if FieldCoding box not present -->\n");
fprintf(xmlout,
" <!-- Current implementation doesn't retain whether box was actually present. -->\n");
}
fprintf(xmlout, " </FieldCoding>\n");