This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
avid_mxf_to_p2.c
1683 lines (1392 loc) · 68.8 KB
/
avid_mxf_to_p2.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
/*
* Transfers Avid MXF files to P2
*
* Copyright (C) 2006, British Broadcasting Corporation
* All Rights Reserved.
*
* Author: Philip de Nier, Stuart Cunningham
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the British Broadcasting Corporation nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "xml_writer.h"
#include "avid_mxf_to_p2.h"
#include "icon_avid_to_p2.h" // provides icon_avid_to_p2[]
#include <mxf/mxf_macros.h>
#define CALL_PROGRESS() \
if (transfer->progress != NULL) \
{ \
if (transfer->percentCompleted - transfer->lastCallPercentCompleted >= 0.1f || \
transfer->percentCompleted == 0.0f || transfer->percentCompleted == 100.0f) \
{ \
transfer->progress(transfer->percentCompleted); \
transfer->lastCallPercentCompleted = transfer->percentCompleted; \
} \
}
typedef struct
{
int hour;
int min;
int sec;
int frame;
} Timecode;
static const mxfUUID g_mxfIdentProductUID =
{0xae, 0x36, 0x89, 0x2c, 0x8e, 0xaf, 0x4c, 0xe4, 0x92, 0x04, 0x0a, 0xf6, 0x7f, 0xa4, 0xfa, 0xd0};
static const mxfUTF16Char *g_mxfIdentCompanyName = L"BBC Research";
static const mxfUTF16Char *g_mxfIdentProductName = L"Ingex Avid to P2 Exporter";
static const mxfUTF16Char *g_mxfIdentVersionString = L"Alpha version";
static const uint32_t g_p2_timecodeTrackID = 0;
static const uint32_t g_p2_timecodeTrackNumber = 1;
static const uint32_t g_p2_bodySID = 2;
static const uint32_t g_p2_indexSID = 1;
static const uint64_t g_p2_fixedStartByteOffset = 32768;
/* 32768 = 32620 + size(BodyPP) + size(essence element KL) = 32620 + 124 + 24 */
static const uint64_t g_p2_fixedBodyPPOffset = 32620;
/* buffer size must be >= max frame size */
#define ESSENCE_BUFFER_SIZE 288000
/* invalid because the registry version, byte8, is 0x01 rather than 0x02, and
byte14 should be 0x02 when multiple material package tracks are present */
static const mxfUL g_invalidAAFSDKOPAtomLabel =
{0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00};
/* invalid because it isn't a valid generic container label and it is has the
same value for all essence types. */
/* corresponds to kAAFContainerDef_AAF */
static const mxfUL g_invalidAAFSDKEssenceContainerLabel_1 =
{0x80, 0x9b, 0x00, 0x60, 0x08, 0x14, 0x3e, 0x6f, 0x43, 0x13, 0xb5, 0x71, 0xd8, 0xba, 0x11, 0xd2};
/* corresponds to kAAFContainerDef_AAFKLV */
static const mxfUL g_invalidAAFSDKEssenceContainerLabel_2 =
{0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0xff, 0x4b, 0x46, 0x41, 0x41, 0x00, 0x0d, 0x4d, 0x4f};
#if defined(_WIN32)
static const char *g_fileSeparator = "\\";
#else
static const char *g_fileSeparator = "/";
#endif
static const char *g_lastClipFilename = "LASTCLIP.TXT";
static const char *g_contentsDirname = "CONTENTS";
static const char *g_videoDirname = "VIDEO";
static const char *g_audioDirname = "AUDIO";
static const char *g_clipDirname = "CLIP";
static const char *g_iconDirname = "ICON";
static int is_invalid_aafsdk_op_atom_label(const mxfUL *label)
{
return mxf_equals_ul(label, &g_invalidAAFSDKOPAtomLabel);
}
static int is_invalid_aafsdk_esscont_label(const mxfUL *label)
{
return mxf_equals_ul(label, &g_invalidAAFSDKEssenceContainerLabel_1) ||
mxf_equals_ul(label, &g_invalidAAFSDKEssenceContainerLabel_2);
}
static int initialise_input_file(AvidMXFFile *input)
{
memset(input, 0, sizeof(AvidMXFFile));
return 1;
}
static int open_input_file(const char *filename, AvidMXFFile *input)
{
if (filename != NULL)
{
SAFE_FREE(input->filename);
CHK_ORET((input->filename = strdup(filename)) != NULL);
}
if (!mxf_disk_file_open_read(input->filename, &input->mxfFile))
{
mxf_log_error("Failed to open '%s'" LOG_LOC_FORMAT, input->filename, LOG_LOC_PARAMS);
return 0;
}
return 1;
}
static void close_input_file(AvidMXFFile *input)
{
if (input->mxfFile != NULL)
{
mxf_file_close(&input->mxfFile);
}
}
static void clear_input_file(AvidMXFFile *input)
{
if (input == NULL)
{
return;
}
close_input_file(input);
SAFE_FREE(input->filename);
mxf_close_essence_element(&input->essenceElement);
mxf_free_header_metadata(&input->headerMetadata);
mxf_free_data_model(&input->dataModel);
mxf_free_partition(&input->headerPartition);
mxf_free_list(&input->aList);
}
static int initialise_output_file(P2MXFFile *output)
{
memset(output, 0, sizeof(P2MXFFile));
CHK_ORET(mxf_create_file_partitions(&output->partitions));
return 1;
}
static int open_output_file(const char *filename, P2MXFFile *output)
{
if (filename != NULL)
{
SAFE_FREE(output->filename);
CHK_ORET((output->filename = strdup(filename)) != NULL);
}
if (!mxf_disk_file_open_new(output->filename, &output->mxfFile))
{
mxf_log_error("Failed to create '%s'" LOG_LOC_FORMAT, output->filename, LOG_LOC_PARAMS);
return 0;
}
return 1;
}
static void close_output_file(P2MXFFile *output)
{
if (output->mxfFile != NULL)
{
mxf_file_close(&output->mxfFile);
}
}
static void clear_output_file(P2MXFFile *output)
{
if (output == NULL)
{
return;
}
close_output_file(output);
SAFE_FREE(output->filename);
mxf_close_essence_element(&output->essenceElement);
mxf_free_header_metadata(&output->headerMetadata);
mxf_free_index_table_segment(&output->indexSegment);
mxf_free_file_partitions(&output->partitions);
mxf_free_data_model(&output->dataModel);
}
static void convert_frames_to_timecode(int64_t frameCount, int fps, int dropFrameFlag, Timecode *timecode)
{
int64_t workFrameCount = frameCount;
int64_t numFramesSkipped;
assert(fps == 25 || fps == 30);
assert(dropFrameFlag == 0 || fps == 30);
timecode->hour = (int)(workFrameCount / (60 * 60 * fps));
workFrameCount %= 60 * 60 * fps;
timecode->min = (int)(workFrameCount / (60 * fps));
workFrameCount %= 60 * fps;
timecode->sec = (int)(workFrameCount / fps);
timecode->frame = (int)(workFrameCount % fps);
if (dropFrameFlag)
{
/* first 2 frame numbers shall be omitted at the start of each minute,
except minutes 0, 10, 20, 30, 40 and 50 */
/* calculate number frames skipped */
numFramesSkipped = (60-6) * 2 * timecode->hour; /* every whole hour */
numFramesSkipped += (timecode->min / 10) * 9 * 2; /* every whole 10 min */
numFramesSkipped += (timecode->min % 10) * 2; /* every whole min, except min 0 */
/* re-calculate with skipped frames */
workFrameCount = frameCount + numFramesSkipped;
timecode->hour = (int)(workFrameCount / (60 * 60 * fps));
workFrameCount %= 60 * 60 * fps;
timecode->min = (int)(workFrameCount / (60 * fps));
workFrameCount %= 60 * fps;
timecode->sec = (int)(workFrameCount / fps);
timecode->frame = (int)(workFrameCount % fps);
}
}
static int preprocess_avid_input(AvidMXFToP2Transfer *transfer, int inputFileIndex, int outputFileIndex)
{
mxfKey key;
uint8_t llen;
uint64_t len;
uint8_t *avid_arrayData;
uint32_t avid_arrayDataLen;
MXFArrayItemIterator avid_arrayIter;
mxfUMID avid_fileSourcePackageUID;
mxfUMID umid;
int foundIt = 0;
size_t essenceContainerLen;
mxfUL *essenceContainerLabel;
MXFListIterator listIter;
AvidMXFFile *input = &transfer->inputs[inputFileIndex];
P2MXFFile *output = &transfer->outputs[outputFileIndex];
/* load the data model for the avid file */
CHK_ORET(mxf_load_data_model(&input->dataModel));
CHK_ORET(mxf_avid_load_extensions(input->dataModel));
CHK_ORET(mxf_finalise_data_model(input->dataModel));
/* read header partition pack */
if (!mxf_read_header_pp_kl(input->mxfFile, &key, &llen, &len))
{
mxf_log_error("Could not find header partition pack key" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
CHK_ORET(mxf_read_partition(input->mxfFile, &key, len, &input->headerPartition));
/* check the operational pattern is OP Atom */
if (!mxf_is_op_atom(&input->headerPartition->operationalPattern) &&
!is_invalid_aafsdk_op_atom_label(&input->headerPartition->operationalPattern))
{
mxf_log_error("Input file is not OP Atom" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
/* check the essence container label */
essenceContainerLen = mxf_get_list_length(&input->headerPartition->essenceContainers);
if (essenceContainerLen != 1)
{
mxf_log_error("Unexpected essence container labels - expecting 1 only" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
essenceContainerLabel = (mxfUL*)mxf_get_first_list_element(&input->headerPartition->essenceContainers);
if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(IECDV_25_625_50_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV25_420");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "50i");
output->frameRate.numerator = 25;
output->frameRate.denominator = 1;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(IECDV_25_625_50_ClipWrapped);
output->frameSize = 144000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 2;
output->horizSubsampling = 2;
output->pictureEssenceCoding = MXF_CMDEF_L(IECDV_25_625_50);
output->storedHeight = 288;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 335;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(IECDV_25_525_60_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV25_411");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "59.94i");
output->frameRate.numerator = 30000;
output->frameRate.denominator = 1001;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(IECDV_25_525_60_ClipWrapped);
output->frameSize = 120000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 1;
output->horizSubsampling = 4;
output->pictureEssenceCoding = MXF_CMDEF_L(IECDV_25_525_60);
output->storedHeight = 240;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 285;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(DVBased_25_625_50_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV25_411");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "50i");
output->frameRate.numerator = 25;
output->frameRate.denominator = 1;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(DVBased_25_625_50_ClipWrapped);
output->frameSize = 144000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 1;
output->horizSubsampling = 4;
output->pictureEssenceCoding = MXF_CMDEF_L(DVBased_25_625_50);
output->storedHeight = 288;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 335;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(DVBased_25_525_60_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV25_411");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "59.94i");
output->frameRate.numerator = 30000;
output->frameRate.denominator = 1001;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(DVBased_25_525_60_ClipWrapped);
output->frameSize = 120000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 1;
output->horizSubsampling = 4;
output->pictureEssenceCoding = MXF_CMDEF_L(DVBased_25_525_60);
output->storedHeight = 240;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 285;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(DVBased_50_625_50_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV50_422");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "50i");
output->frameRate.numerator = 25;
output->frameRate.denominator = 1;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(DVBased_50_625_50_ClipWrapped);
output->frameSize = 288000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 1;
output->horizSubsampling = 2;
output->pictureEssenceCoding = MXF_CMDEF_L(DVBased_50_625_50);
output->storedHeight = 288;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 335;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(DVBased_50_525_60_ClipWrapped)))
{
output->isPicture = 1;
output->sourceTrackNumber = MXF_DV_TRACK_NUM(0x01, 0x02, 0x01);
output->essElementKey = MXF_EE_K(DVClipWrapped);
mxf_snprintf(output->codecString, sizeof(output->codecString), "DV50_422");
mxf_snprintf(output->frameRateString, sizeof(output->frameRateString), "59.94i");
output->frameRate.numerator = 30000;
output->frameRate.denominator = 1001;
output->editRate = output->frameRate;
output->startByteOffset = g_p2_fixedStartByteOffset;
output->essenceContainerLabel = MXF_EC_L(DVBased_50_525_60_ClipWrapped);
output->frameSize = 240000;
output->dataDef = MXF_DDEF_L(Picture);
output->sourceTrackID = 0;
output->verticalSubsampling = 1;
output->horizSubsampling = 2;
output->pictureEssenceCoding = MXF_CMDEF_L(DVBased_50_525_60);
output->storedHeight = 240;
output->storedWidth = 720;
output->videoLineMap[0] = 23;
output->videoLineMap[1] = 285;
mxf_generate_umid(&output->sourcePackageUID);
transfer->pictureOutputIndex = outputFileIndex;
}
else if (mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(BWFClipWrapped)) ||
mxf_equals_ul(essenceContainerLabel, &MXF_EC_L(AES3ClipWrapped)))
{
output->isPicture = 0;
output->sourceTrackNumber = MXF_AES3BWF_TRACK_NUM(0x01, 0x04, 0x01);
output->essElementKey = MXF_EE_K(AES3ClipWrapped);
output->startByteOffset = g_p2_fixedStartByteOffset;
output->samplingRate.numerator = 48000;
output->samplingRate.denominator = 1;
output->editRate = output->samplingRate;
output->dataDef = MXF_DDEF_L(Sound);
output->sourceTrackID = 0;
mxf_generate_umid(&output->sourcePackageUID);
output->essenceContainerLabel = MXF_EC_L(AES3ClipWrapped);
}
else if (is_invalid_aafsdk_esscont_label(essenceContainerLabel))
{
mxf_log_error("Invalid AAF SDK essence container label in input file" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
else
{
char labelStr[KEY_STR_SIZE];
mxf_sprint_label(labelStr, essenceContainerLabel);
mxf_log_error("Unknown or unsupported essence container label '%s' in input file" LOG_LOC_FORMAT,
labelStr, LOG_LOC_PARAMS);
return 0;
}
/* create and read the header metadata (filter out meta-dictionary and dictionary except data defs) */
CHK_ORET(mxf_create_header_metadata(&input->headerMetadata, input->dataModel));
CHK_ORET(mxf_read_next_nonfiller_kl(input->mxfFile, &key, &llen, &len));
CHK_ORET(mxf_is_header_metadata(&key));
CHK_ORET(mxf_avid_read_filtered_header_metadata(input->mxfFile, 0, input->headerMetadata,
input->headerPartition->headerByteCount, &key, llen, len));
/* get the top file SourcePackage referenced by Preface::PrimaryPackage if it exists
or go via the EssenceData::LinkedPackageUID */
CHK_ORET(mxf_find_singular_set_by_key(input->headerMetadata, &MXF_SET_K(Preface), &input->prefaceSet));
if (mxf_have_item(input->prefaceSet, &MXF_ITEM_K(Preface, PrimaryPackage)))
{
/* the Preface::PrimaryPackage references the top level file SourcePackage */
CHK_ORET(mxf_get_weakref_item(input->prefaceSet, &MXF_ITEM_K(Preface, PrimaryPackage), &input->sourcePackageSet));
}
else
{
/* get the EssenceContainerData::LinkedPackageUID */
CHK_ORET(mxf_find_singular_set_by_key(input->headerMetadata, &MXF_SET_K(EssenceContainerData), &input->essContainerDataSet));
CHK_ORET(mxf_get_umid_item(input->essContainerDataSet, &MXF_ITEM_K(EssenceContainerData, LinkedPackageUID), &avid_fileSourcePackageUID));
/* Go through the SourcePackages looking for the one with PackageUID == LinkedPackageUID */
CHK_ORET(mxf_find_set_by_key(input->headerMetadata, &MXF_SET_K(SourcePackage), &input->aList));
mxf_initialise_list_iter(&listIter, input->aList);
foundIt = 0;
while (!foundIt && mxf_next_list_iter_element(&listIter))
{
MXFMetadataSet *set = (MXFMetadataSet*)mxf_get_iter_element(&listIter);
CHK_ORET(mxf_get_umid_item(set, &MXF_ITEM_K(GenericPackage, PackageUID), &umid));
if (mxf_equals_umid(&avid_fileSourcePackageUID, &umid))
{
input->sourcePackageSet = set;
foundIt = 1;
}
}
mxf_free_list(&input->aList);
if (!foundIt)
{
mxf_log_error("Could not find the top level file source package" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
}
/* get the file SourcePackage Sequence::Duration */
CHK_ORET(mxf_initialise_array_item_iterator(input->sourcePackageSet, &MXF_ITEM_K(GenericPackage, Tracks), &avid_arrayIter));
while (mxf_next_array_item_element(&avid_arrayIter, &avid_arrayData, &avid_arrayDataLen))
{
CHK_ORET(mxf_get_strongref(input->headerMetadata, avid_arrayData, &input->sourcePackageTrackSet));
CHK_ORET(mxf_get_strongref_item(input->sourcePackageTrackSet, &MXF_ITEM_K(GenericTrack, Sequence), &input->sequenceSet));
CHK_ORET(mxf_get_length_item(input->sequenceSet, &MXF_ITEM_K(StructuralComponent, Duration), &output->duration));
}
/* get the descriptor info from the file SourcePackage Descriptor */
CHK_ORET(mxf_get_strongref_item(input->sourcePackageSet, &MXF_ITEM_K(SourcePackage, Descriptor), &input->descriptorSet));
if (output->isPicture)
{
CHK_ORET(mxf_get_rational_item(input->descriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, AspectRatio), &output->aspectRatio));
}
else
{
CHK_ORET(mxf_get_rational_item(input->descriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, AudioSamplingRate), &output->samplingRate));
CHK_ORET(mxf_get_uint32_item(input->descriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, QuantizationBits), &output->bitsPerSample));
CHK_ORET(mxf_get_uint16_item(input->descriptorSet, &MXF_ITEM_K(WaveAudioDescriptor, BlockAlign), &output->blockAlign));
CHK_ORET(mxf_get_uint32_item(input->descriptorSet, &MXF_ITEM_K(WaveAudioDescriptor, AvgBps), &output->avgBps));
if (output->samplingRate.numerator != 48000 || output->samplingRate.denominator != 1)
{
mxf_log_error("Unsupported input audio sampling rate %d/%d" LOG_LOC_FORMAT,
output->samplingRate.numerator, output->samplingRate.denominator, LOG_LOC_PARAMS);
return 0;
}
if (output->bitsPerSample != 16 && output->bitsPerSample != 24)
{
mxf_log_error("Unsupported audio bits per sample %u" LOG_LOC_FORMAT,
output->bitsPerSample, LOG_LOC_PARAMS);
return 0;
}
if (output->bitsPerSample == 16)
{
if (output->blockAlign != 2)
{
mxf_log_error("Unsupported audio block alignment %u for %u bit quantization" LOG_LOC_FORMAT,
output->blockAlign, output->bitsPerSample, LOG_LOC_PARAMS);
return 0;
}
if (output->avgBps != 96000)
{
mxf_log_error("Unsupported audio avg. bps %u for %u bit quantization" LOG_LOC_FORMAT,
output->avgBps, output->bitsPerSample, LOG_LOC_PARAMS);
return 0;
}
}
else //24 bitsPerSample
{
if (output->blockAlign != 3)
{
mxf_log_error("Unsupported audio block alignment %u for %u bit quantization" LOG_LOC_FORMAT,
output->blockAlign, output->bitsPerSample, LOG_LOC_PARAMS);
return 0;
}
if (output->avgBps != 144000)
{
mxf_log_error("Unsupported audio avg. bps %u for %u bit quantization" LOG_LOC_FORMAT,
output->avgBps, output->bitsPerSample, LOG_LOC_PARAMS);
return 0;
}
}
}
/* skip the body partition pack and position at the start of the essence element */
CHK_ORET(mxf_read_next_nonfiller_kl(input->mxfFile, &key, &llen, &len));
if (!mxf_is_body_partition_pack(&key))
{
mxf_log_error("Expecting the body partition pack" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
CHK_ORET(mxf_skip(input->mxfFile, len));
CHK_ORET(mxf_read_next_nonfiller_kl(input->mxfFile, &key, &llen, &len));
if (!mxf_is_gc_essence_element(&key))
{
mxf_log_error("Expecting an essence element" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
/* number of essence bytes determines the contribution towards the progress % */
output->essenceBytesLength = len;
/* calculate the container duration */
if (output->isPicture)
{
output->containerDuration = len / output->frameSize;
}
else
{
output->containerDuration = len / output->blockAlign;
}
if (output->containerDuration != output->duration)
{
mxf_log_warn("File container duration %" PRId64 " does not equal track duration %" PRId64 "\n",
output->containerDuration, output->duration);
}
return 1;
}
static int transfer_to_p2(AvidMXFToP2Transfer *transfer, int inputFileIndex, int outputFileIndex)
{
mxfKey key;
uint8_t llen;
uint64_t len;
mxfUUID uuid;
MXFPartition *headerPartition;
MXFPartition *bodyPartition;
MXFPartition *footerPartition;
uint8_t buffer[ESSENCE_BUFFER_SIZE];
uint32_t numRead;
mxfUTF16Char materialPackageName[256];
uint32_t essenceReadSize;
mxfLength frameCount;
int i;
float percentCompletedStart = transfer->percentCompleted;
uint64_t totalBytesRead;
uint8_t *arrayElement;
AvidMXFFile *input = &transfer->inputs[inputFileIndex];
P2MXFFile *output = &transfer->outputs[outputFileIndex];
mbstowcs(materialPackageName, transfer->clipName, strlen(transfer->clipName) + 1);
/*
* Position the input at the essence element and open it
*/
while (1)
{
CHK_ORET(mxf_read_next_nonfiller_kl(input->mxfFile, &key, &llen, &len));
CHK_ORET(mxf_file_seek(input->mxfFile, len, SEEK_CUR));
if (mxf_is_body_partition_pack(&key))
{
break;
}
}
CHK_ORET(mxf_read_next_nonfiller_kl(input->mxfFile, &key, &llen, &len));
if (!mxf_is_gc_essence_element(&key))
{
mxf_log_error("Expecting an essence element" LOG_LOC_FORMAT, LOG_LOC_PARAMS);
return 0;
}
CHK_ORET(mxf_open_essence_element_read(input->mxfFile, &key, llen, len, &input->essenceElement));
/* set the minimum llen */
mxf_file_set_min_llen(output->mxfFile, 4);
/*
* Create the P2 header metadata from the Avid header metadata
*/
/* load the data model */
CHK_ORET(mxf_load_data_model(&output->dataModel));
/* create the header metadata */
CHK_ORET(mxf_create_header_metadata(&output->headerMetadata, output->dataModel));
/* Preface */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Preface), &output->prefaceSet));
CHK_ORET(mxf_set_timestamp_item(output->prefaceSet, &MXF_ITEM_K(Preface, LastModifiedDate), &transfer->now));
CHK_ORET(mxf_set_version_type_item(output->prefaceSet, &MXF_ITEM_K(Preface, Version), MXF_PREFACE_VER(1, 2)));
CHK_ORET(mxf_set_ul_item(output->prefaceSet, &MXF_ITEM_K(Preface, OperationalPattern), &MXF_OP_L(atom, NTracks_1SourceClip)));
CHK_ORET(mxf_alloc_array_item_elements(output->prefaceSet, &MXF_ITEM_K(Preface, EssenceContainers), mxfUL_extlen, 1, &arrayElement));
mxf_set_ul(&output->essenceContainerLabel, arrayElement);
CHK_ORET(mxf_set_empty_array_item(output->prefaceSet, &MXF_ITEM_K(Preface, DMSchemes), mxfUL_extlen));
/* Preface - Identification */
mxf_generate_uuid(&uuid);
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Identification), &output->identSet));
CHK_ORET(mxf_add_array_item_strongref(output->prefaceSet, &MXF_ITEM_K(Preface, Identifications), output->identSet));
CHK_ORET(mxf_set_uuid_item(output->identSet, &MXF_ITEM_K(Identification, ThisGenerationUID), &uuid));
CHK_ORET(mxf_set_utf16string_item(output->identSet, &MXF_ITEM_K(Identification, CompanyName), g_mxfIdentCompanyName));
CHK_ORET(mxf_set_utf16string_item(output->identSet, &MXF_ITEM_K(Identification, ProductName), g_mxfIdentProductName));
CHK_ORET(mxf_set_utf16string_item(output->identSet, &MXF_ITEM_K(Identification, VersionString), g_mxfIdentVersionString));
CHK_ORET(mxf_set_uuid_item(output->identSet, &MXF_ITEM_K(Identification, ProductUID), &g_mxfIdentProductUID));
CHK_ORET(mxf_set_timestamp_item(output->identSet, &MXF_ITEM_K(Identification, ModificationDate), &transfer->now));
CHK_ORET(mxf_set_product_version_item(output->identSet, &MXF_ITEM_K(Identification, ToolkitVersion), mxf_get_version()));
CHK_ORET(mxf_set_utf16string_item(output->identSet, &MXF_ITEM_K(Identification, Platform), mxf_get_platform_wstring()));
/* Preface - ContentStorage */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(ContentStorage), &output->contentStorageSet));
CHK_ORET(mxf_set_strongref_item(output->prefaceSet, &MXF_ITEM_K(Preface, ContentStorage), output->contentStorageSet));
/* Preface - ContentStorage - EssenceContainerData */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(EssenceContainerData), &output->essContainerDataSet));
CHK_ORET(mxf_add_array_item_strongref(output->contentStorageSet, &MXF_ITEM_K(ContentStorage, EssenceContainerData), output->essContainerDataSet));
CHK_ORET(mxf_set_umid_item(output->essContainerDataSet, &MXF_ITEM_K(EssenceContainerData, LinkedPackageUID), &output->sourcePackageUID));
CHK_ORET(mxf_set_uint32_item(output->essContainerDataSet, &MXF_ITEM_K(EssenceContainerData, IndexSID), g_p2_indexSID));
CHK_ORET(mxf_set_uint32_item(output->essContainerDataSet, &MXF_ITEM_K(EssenceContainerData, BodySID), g_p2_bodySID));
/* Preface - ContentStorage - MaterialPackage */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(MaterialPackage), &output->materialPackageSet));
CHK_ORET(mxf_add_array_item_strongref(output->contentStorageSet, &MXF_ITEM_K(ContentStorage, Packages), output->materialPackageSet));
CHK_ORET(mxf_set_umid_item(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, PackageUID), &transfer->globalClipID));
CHK_ORET(mxf_set_timestamp_item(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, PackageCreationDate), &transfer->now));
CHK_ORET(mxf_set_timestamp_item(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, PackageModifiedDate), &transfer->now));
CHK_ORET(mxf_set_utf16string_item(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, Name), materialPackageName));
/* Preface - ContentStorage - MaterialPackage Tracks */
/* Preface - ContentStorage - MaterialPackage - Timecode Track */
if (transfer->pictureOutputIndex >= 0)
{
uint16_t roundedTimecodeBase = (uint16_t)((float)transfer->outputs[transfer->pictureOutputIndex].frameRate.numerator /
(float)transfer->outputs[transfer->pictureOutputIndex].frameRate.denominator + 0.5);
/* Preface - ContentStorage - MaterialPackage - Timecode Track */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Track), &output->materialPackageTrackSet));
CHK_ORET(mxf_add_array_item_strongref(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, Tracks), output->materialPackageTrackSet));
CHK_ORET(mxf_set_uint32_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackID), g_p2_timecodeTrackID));
CHK_ORET(mxf_set_uint32_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackNumber), g_p2_timecodeTrackNumber));
CHK_ORET(mxf_set_rational_item(output->materialPackageTrackSet, &MXF_ITEM_K(Track, EditRate), &transfer->outputs[transfer->pictureOutputIndex].editRate));
CHK_ORET(mxf_set_position_item(output->materialPackageTrackSet, &MXF_ITEM_K(Track, Origin), 0));
/* Preface - ContentStorage - MaterialPackage - Timecode Track - Sequence */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Sequence), &output->sequenceSet));
CHK_ORET(mxf_set_strongref_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, Sequence), output->sequenceSet));
CHK_ORET(mxf_set_ul_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &MXF_DDEF_L(Timecode)));
CHK_ORET(mxf_set_length_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, Duration), transfer->outputs[transfer->pictureOutputIndex].duration));
/* Preface - ContentStorage - MaterialPackage - Timecode Track - Sequence - TimecodeComponent */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(TimecodeComponent), &output->timecodeComponentSet));
CHK_ORET(mxf_add_array_item_strongref(output->sequenceSet, &MXF_ITEM_K(Sequence, StructuralComponents), output->timecodeComponentSet));
CHK_ORET(mxf_set_ul_item(output->timecodeComponentSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &MXF_DDEF_L(Timecode)));
CHK_ORET(mxf_set_length_item(output->timecodeComponentSet, &MXF_ITEM_K(StructuralComponent, Duration), transfer->outputs[transfer->pictureOutputIndex].duration));
CHK_ORET(mxf_set_uint16_item(output->timecodeComponentSet, &MXF_ITEM_K(TimecodeComponent, RoundedTimecodeBase), roundedTimecodeBase));
CHK_ORET(mxf_set_boolean_item(output->timecodeComponentSet, &MXF_ITEM_K(TimecodeComponent, DropFrame), transfer->dropFrameFlag));
CHK_ORET(mxf_set_position_item(output->timecodeComponentSet, &MXF_ITEM_K(TimecodeComponent, StartTimecode), transfer->timecodeStart));
}
/* Preface - ContentStorage - MaterialPackage - Timeline Tracks */
for (i = 0; i < transfer->numInputs; i++)
{
/* Preface - ContentStorage - MaterialPackage - Timeline Track */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Track), &output->materialPackageTrackSet));
CHK_ORET(mxf_add_array_item_strongref(output->materialPackageSet, &MXF_ITEM_K(GenericPackage, Tracks), output->materialPackageTrackSet));
CHK_ORET(mxf_set_uint32_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackID), transfer->outputs[i].materialTrackID));
CHK_ORET(mxf_set_uint32_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackNumber), transfer->outputs[i].materialTrackNumber));
CHK_ORET(mxf_set_rational_item(output->materialPackageTrackSet, &MXF_ITEM_K(Track, EditRate), &transfer->outputs[i].editRate));
CHK_ORET(mxf_set_position_item(output->materialPackageTrackSet, &MXF_ITEM_K(Track, Origin), 0));
/* Preface - ContentStorage - MaterialPackage - Timeline Track - Sequence */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Sequence), &output->sequenceSet));
CHK_ORET(mxf_set_strongref_item(output->materialPackageTrackSet, &MXF_ITEM_K(GenericTrack, Sequence), output->sequenceSet));
CHK_ORET(mxf_set_ul_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &transfer->outputs[i].dataDef));
CHK_ORET(mxf_set_length_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, Duration), transfer->outputs[i].duration));
/* Preface - ContentStorage - MaterialPackage - Timeline Track - Sequence - SourceClip */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(SourceClip), &output->sourceClipSet));
CHK_ORET(mxf_add_array_item_strongref(output->sequenceSet, &MXF_ITEM_K(Sequence, StructuralComponents), output->sourceClipSet));
CHK_ORET(mxf_set_ul_item(output->sourceClipSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &transfer->outputs[i].dataDef));
CHK_ORET(mxf_set_length_item(output->sourceClipSet, &MXF_ITEM_K(StructuralComponent, Duration), transfer->outputs[i].duration));
CHK_ORET(mxf_set_position_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, StartPosition), 0));
CHK_ORET(mxf_set_umid_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, SourcePackageID), &transfer->outputs[i].sourcePackageUID));
CHK_ORET(mxf_set_uint32_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, SourceTrackID), transfer->outputs[i].sourceTrackID));
}
/* Preface - ContentStorage - SourcePackage */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(SourcePackage), &output->sourcePackageSet));
CHK_ORET(mxf_add_array_item_strongref(output->contentStorageSet, &MXF_ITEM_K(ContentStorage, Packages), output->sourcePackageSet));
CHK_ORET(mxf_set_weakref_item(output->prefaceSet, &MXF_ITEM_K(Preface, PrimaryPackage), output->sourcePackageSet));
CHK_ORET(mxf_set_umid_item(output->sourcePackageSet, &MXF_ITEM_K(GenericPackage, PackageUID), &output->sourcePackageUID));
CHK_ORET(mxf_set_timestamp_item(output->sourcePackageSet, &MXF_ITEM_K(GenericPackage, PackageCreationDate), &transfer->now));
CHK_ORET(mxf_set_timestamp_item(output->sourcePackageSet, &MXF_ITEM_K(GenericPackage, PackageModifiedDate), &transfer->now));
/* Preface - ContentStorage - SourcePackage - Timeline Track */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Track), &output->sourcePackageTrackSet));
CHK_ORET(mxf_add_array_item_strongref(output->sourcePackageSet, &MXF_ITEM_K(GenericPackage, Tracks), output->sourcePackageTrackSet));
CHK_ORET(mxf_set_uint32_item(output->sourcePackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackID), output->sourceTrackID));
CHK_ORET(mxf_set_uint32_item(output->sourcePackageTrackSet, &MXF_ITEM_K(GenericTrack, TrackNumber), output->sourceTrackNumber));
CHK_ORET(mxf_set_rational_item(output->sourcePackageTrackSet, &MXF_ITEM_K(Track, EditRate), &output->editRate));
CHK_ORET(mxf_set_position_item(output->sourcePackageTrackSet, &MXF_ITEM_K(Track, Origin), 0));
/* Preface - ContentStorage - SourcePackage - Timeline Track - Sequence */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(Sequence), &output->sequenceSet));
CHK_ORET(mxf_set_strongref_item(output->sourcePackageTrackSet, &MXF_ITEM_K(GenericTrack, Sequence), output->sequenceSet));
CHK_ORET(mxf_set_ul_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &output->dataDef));
CHK_ORET(mxf_set_length_item(output->sequenceSet, &MXF_ITEM_K(StructuralComponent, Duration), output->duration));
/* Preface - ContentStorage - SourcePackage - Timeline Track - Sequence - SourceClip */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(SourceClip), &output->sourceClipSet));
CHK_ORET(mxf_add_array_item_strongref(output->sequenceSet, &MXF_ITEM_K(Sequence, StructuralComponents), output->sourceClipSet));
CHK_ORET(mxf_set_ul_item(output->sourceClipSet, &MXF_ITEM_K(StructuralComponent, DataDefinition), &output->dataDef));
CHK_ORET(mxf_set_length_item(output->sourceClipSet, &MXF_ITEM_K(StructuralComponent, Duration), output->duration));
CHK_ORET(mxf_set_position_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, StartPosition), 0));
CHK_ORET(mxf_set_umid_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, SourcePackageID), &g_Null_UMID));
CHK_ORET(mxf_set_uint32_item(output->sourceClipSet, &MXF_ITEM_K(SourceClip, SourceTrackID), 0));
if (output->isPicture)
{
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(CDCIEssenceDescriptor), &output->cdciDescriptorSet));
CHK_ORET(mxf_set_strongref_item(output->sourcePackageSet, &MXF_ITEM_K(SourcePackage, Descriptor), output->cdciDescriptorSet));
CHK_ORET(mxf_set_rational_item(output->cdciDescriptorSet, &MXF_ITEM_K(FileDescriptor, SampleRate), &output->frameRate));
CHK_ORET(mxf_set_length_item(output->cdciDescriptorSet, &MXF_ITEM_K(FileDescriptor, ContainerDuration), output->containerDuration));
CHK_ORET(mxf_set_ul_item(output->cdciDescriptorSet, &MXF_ITEM_K(FileDescriptor, EssenceContainer), &output->essenceContainerLabel));
CHK_ORET(mxf_set_uint8_item(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, FrameLayout), MXF_SEPARATE_FIELDS));
CHK_ORET(mxf_set_uint32_item(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, StoredHeight), output->storedHeight));
CHK_ORET(mxf_set_uint32_item(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, StoredWidth), output->storedWidth));
CHK_ORET(mxf_alloc_array_item_elements(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, VideoLineMap), 4, 2, &arrayElement));
mxf_set_int32(output->videoLineMap[0], arrayElement);
mxf_set_int32(output->videoLineMap[1], &arrayElement[4]);
CHK_ORET(mxf_set_rational_item(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, AspectRatio), &output->aspectRatio));
CHK_ORET(mxf_set_uint32_item(output->cdciDescriptorSet, &MXF_ITEM_K(CDCIEssenceDescriptor, ComponentDepth), 8));
CHK_ORET(mxf_set_ul_item(output->cdciDescriptorSet, &MXF_ITEM_K(GenericPictureEssenceDescriptor, PictureEssenceCoding), &output->pictureEssenceCoding));
CHK_ORET(mxf_set_uint32_item(output->cdciDescriptorSet, &MXF_ITEM_K(CDCIEssenceDescriptor, HorizontalSubsampling), output->horizSubsampling));
CHK_ORET(mxf_set_uint32_item(output->cdciDescriptorSet, &MXF_ITEM_K(CDCIEssenceDescriptor, VerticalSubsampling), output->verticalSubsampling));
}
else
{
/* Preface - ContentStorage - SourcePackage - AES3EssenceDescriptor */
CHK_ORET(mxf_create_set(output->headerMetadata, &MXF_SET_K(AES3AudioDescriptor), &output->aes3DescriptorSet));
CHK_ORET(mxf_set_strongref_item(output->sourcePackageSet, &MXF_ITEM_K(SourcePackage, Descriptor), output->aes3DescriptorSet));
CHK_ORET(mxf_set_rational_item(output->aes3DescriptorSet, &MXF_ITEM_K(FileDescriptor, SampleRate), &output->samplingRate));
CHK_ORET(mxf_set_length_item(output->aes3DescriptorSet, &MXF_ITEM_K(FileDescriptor, ContainerDuration), output->containerDuration));
CHK_ORET(mxf_set_ul_item(output->aes3DescriptorSet, &MXF_ITEM_K(FileDescriptor, EssenceContainer), &output->essenceContainerLabel));
CHK_ORET(mxf_set_rational_item(output->aes3DescriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, AudioSamplingRate), &output->samplingRate));
CHK_ORET(mxf_set_boolean_item(output->aes3DescriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, Locked), 1));
CHK_ORET(mxf_set_uint32_item(output->aes3DescriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, ChannelCount), 1));
CHK_ORET(mxf_set_uint32_item(output->aes3DescriptorSet, &MXF_ITEM_K(GenericSoundEssenceDescriptor, QuantizationBits), output->bitsPerSample));
CHK_ORET(mxf_set_uint16_item(output->aes3DescriptorSet, &MXF_ITEM_K(WaveAudioDescriptor, BlockAlign), output->blockAlign));
CHK_ORET(mxf_set_uint32_item(output->aes3DescriptorSet, &MXF_ITEM_K(WaveAudioDescriptor, AvgBps), output->avgBps));
}
/*
* Write the Header Partition Pack
*/
CHK_ORET(mxf_append_new_partition(output->partitions, &headerPartition));
headerPartition->key = MXF_PP_K(ClosedComplete, Header);
headerPartition->majorVersion = 1;
headerPartition->minorVersion = 2;
headerPartition->kagSize = 0x01;
headerPartition->indexSID = g_p2_indexSID;
headerPartition->operationalPattern = MXF_OP_L(atom, NTracks_1SourceClip);
CHK_ORET(mxf_append_partition_esscont_label(headerPartition, &output->essenceContainerLabel));
CHK_ORET(mxf_write_partition(output->mxfFile, headerPartition));
/*
* Write the Header Metadata
*/
CHK_ORET(mxf_mark_header_start(output->mxfFile, headerPartition));
CHK_ORET(mxf_write_header_metadata(output->mxfFile, output->headerMetadata));
CHK_ORET(mxf_mark_header_end(output->mxfFile, headerPartition));
/*
* Write the Index Table Segment
*/
CHK_ORET(mxf_mark_index_start(output->mxfFile, headerPartition));
CHK_ORET(mxf_create_index_table_segment(&output->indexSegment));
mxf_generate_uuid(&output->indexSegment->instanceUID);
output->indexSegment->indexEditRate = output->editRate;
output->indexSegment->indexStartPosition = 0;
output->indexSegment->indexDuration = 0; /* zero value means the indexDuration == container duration */
if (output->isPicture)
{
output->indexSegment->editUnitByteCount = output->frameSize;
}
else
{
output->indexSegment->editUnitByteCount = output->bitsPerSample / 8;
}
output->indexSegment->indexSID = g_p2_indexSID;
output->indexSegment->bodySID = g_p2_bodySID;
output->indexSegment->sliceCount = 0;
output->indexSegment->posTableCount = 0;
output->indexSegment->deltaEntryArray = NULL;
output->indexSegment->indexEntryArray = NULL;
CHK_ORET(mxf_write_index_table_segment(output->mxfFile, output->indexSegment));
CHK_ORET(mxf_fill_to_position(output->mxfFile, g_p2_fixedBodyPPOffset));
CHK_ORET(mxf_mark_index_end(output->mxfFile, headerPartition));
/*
* Write the Body Partition Pack
*/
CHK_ORET(mxf_append_new_from_partition(output->partitions, headerPartition, &bodyPartition));
bodyPartition->key = MXF_PP_K(ClosedComplete, Body);
bodyPartition->bodySID = g_p2_bodySID;
CHK_ORET(mxf_write_partition(output->mxfFile, bodyPartition));
/*
* Transfer the essence data
*/
if (output->isPicture)
{
essenceReadSize = output->frameSize;
}
else
{
essenceReadSize = ESSENCE_BUFFER_SIZE;
}
CHK_ORET(mxf_open_essence_element_write(output->mxfFile, &output->essElementKey, 8, 0,
&output->essenceElement));
frameCount = 0;
totalBytesRead = 0;
while (1)
{
CHK_ORET(mxf_read_essence_element_data(input->mxfFile, input->essenceElement, essenceReadSize,
buffer, &numRead));
/* insert timecode into DV essence */
if (output->isPicture && transfer->insertTimecode != NULL &&