-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.c
2916 lines (2685 loc) · 104 KB
/
main.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
/*
* Test writing video and audio to MXF files supported by Avid editing software
*
* Copyright (C) 2006, British Broadcasting Corporation
* All Rights Reserved.
*
* Author: Philip de Nier
*
* 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 "write_avid_mxf.h"
#include <mxf/mxf.h>
#include <mxf/mxf_avid.h>
#include <mxf/mxf_macros.h>
#define MAX_INPUTS 17
/* allow the last ntsc audio frame to be x samples less than the average */
#define MAX_AUDIO_VARIATION 5
#define MAX_USER_COMMENT_TAGS 64
#define DV_DIF_BLOCK_SIZE 80
#define DV_DIF_SEQUENCE_SIZE (150 * DV_DIF_BLOCK_SIZE)
typedef MXFFile RawFile;
typedef struct
{
const char *name;
const char *value;
} UserCommentTag;
typedef struct
{
const char *positionStr;
int64_t position;
const char *comment;
AvidRGBColor color;
} LocatorOption;
typedef struct
{
RawFile *file;
int64_t dataOffset;
int64_t dataSize;
uint16_t numAudioChannels;
mxfRational audioSamplingRate;
uint16_t nBlockAlign;
uint16_t audioSampleBits;
int bytesPerSample;
int64_t totalRead;
} WAVInput;
typedef struct
{
AvidMJPEGResolution resolution;
unsigned char *buffer;
uint32_t bufferSize;
uint32_t position;
uint32_t prevPosition;
uint32_t dataSize;
int endOfField;
int field2;
uint32_t skipCount;
int haveLenByte1;
int haveLenByte2;
// states
// 0 = search for 0xFF
// 1 = test for 0xD8 (start of image)
// 2 = search for 0xFF - start of marker
// 3 = test for 0xD9 (end of image), else skip
// 4 = skip marker segment data
//
// transitions
// 0 -> 1 (data == 0xFF)
// 1 -> 0 (data != 0xD8 && data != 0xFF)
// 1 -> 2 (data == 0xD8)
// 2 -> 3 (data == 0xFF)
// 3 -> 0 (data == 0xD9)
// 3 -> 2 (data >= 0xD0 && data <= 0xD7 || data == 0x01 || data == 0x00)
// 3 -> 4 (else and data != 0xFF)
int markerState;
} MJPEGState;
typedef struct
{
EssenceType essenceType;
int isDV;
int isVideo;
int trackNumber;
uint32_t materialTrackID;
EssenceInfo essenceInfo;
const char *filename;
RawFile *file;
uint32_t frameSize;
uint32_t frameSizeSeq[10];
uint32_t minFrameSize;
uint32_t availFrameSize;
int frameSeqLen;
int seqIndex;
unsigned char *buffer;
int bufferOffset;
/* DV input parameters */
int isPAL;
/* used when writing MJPEG */
MJPEGState mjpegState;
int isWAVFile;
int channelIndex; /* Note: channel 0 will own the WAVInput */
WAVInput wavInput;
unsigned char *channelBuffer;
int bytesPerSample;
} Input;
static const unsigned char RIFF_ID[4] = {'R', 'I', 'F', 'F'};
static const unsigned char WAVE_ID[4] = {'W', 'A', 'V', 'E'};
static const unsigned char FMT_ID[4] = {'f', 'm', 't', ' '};
static const unsigned char DATA_ID[4] = {'d', 'a', 't', 'a'};
static const uint16_t WAVE_FORMAT_PCM = 0x0001;
static const int g_defaultIMX30FrameSizePAL = 150000;
static const int g_defaultIMX40FrameSizePAL = 200000;
static const int g_defaultIMX50FrameSizePAL = 250000;
static const int g_defaultIMX30FrameSizeNTSC = 126976;
static const int g_defaultIMX40FrameSizeNTSC = 167936;
static const int g_defaultIMX50FrameSizeNTSC = 208896;
static RawFile* rf_open(const char *filename)
{
RawFile *file;
if (!mxf_disk_file_open_read(filename, &file))
return NULL;
return file;
}
static void rf_close(RawFile *file)
{
mxf_file_close(&file);
}
uint32_t rf_read(RawFile *file, uint8_t *data, uint32_t count)
{
return mxf_file_read(file, data, count);
}
static int rf_seek(RawFile *file, int64_t offset, int whence)
{
return mxf_file_seek(file, offset, whence);
}
static int64_t rf_tell(RawFile *file)
{
return mxf_file_tell(file);
}
static int rf_eof(RawFile *file)
{
return mxf_file_eof(file);
}
static int get_dv_stream_info(const char *filename, Input *input)
{
RawFile *inputFile;
unsigned char buffer[DV_DIF_SEQUENCE_SIZE];
uint32_t result;
unsigned char byte;
int isIEC;
inputFile = rf_open(filename);
if (inputFile == NULL)
{
fprintf(stderr, "%s: Failed to open DV file\n", filename);
return 0;
}
result = rf_read(inputFile, buffer, DV_DIF_SEQUENCE_SIZE);
rf_close(inputFile);
if (result != DV_DIF_SEQUENCE_SIZE)
{
fprintf(stderr, "%s: Failed to read first DV DIF sequence from DV file\n", filename);
return 0;
}
/* check section ids */
if ((buffer[0] & 0xe0) != 0x00 || /* header */
(buffer[DV_DIF_BLOCK_SIZE] & 0xe0) != 0x20 || /* subcode */
(buffer[3 * DV_DIF_BLOCK_SIZE] & 0xe0) != 0x40 || /* vaux */
(buffer[6 * DV_DIF_BLOCK_SIZE] & 0xe0) != 0x60 || /* audio */
(buffer[15 * DV_DIF_BLOCK_SIZE] & 0xe0) != 0x80) /* video */
{
fprintf(stderr, "%s: File is not a DV file\n", filename);
return 0;
}
/* check video and vaux section are transmitted */
if (buffer[6] & 0x80)
{
fprintf(stderr, "%s: No video in DV file\n", filename);
return 0;
}
/* IEC/DV is extracted from the APT in byte 4 */
byte = buffer[4];
if (byte & 0x03) /* DV-based if APT all 001 or all 111 */
{
isIEC = 0;
}
else
{
isIEC = 1;
}
/* 525/625 is extracted from the DSF in byte 3 */
byte = buffer[3];
if (byte & 0x80)
{
input->isPAL = 1;
}
else
{
input->isPAL = 0;
}
/* aspect ratio is extracted from the VAUX section, VSC pack, DISP bits */
byte = buffer[3 * DV_DIF_BLOCK_SIZE + 2 * DV_DIF_BLOCK_SIZE + 3 + 10 * 5 + 2];
if ((byte & 0x07) == 0x02)
{
input->essenceInfo.imageAspectRatio.numerator = 16;
input->essenceInfo.imageAspectRatio.denominator = 9;
}
else
{
/* either byte & 0x07 == 0x00 or we default to 4:3 */
input->essenceInfo.imageAspectRatio.numerator = 4;
input->essenceInfo.imageAspectRatio.denominator = 3;
}
/* mbps is extracted from the VAUX section, VS pack, STYPE bits */
byte = buffer[3 * DV_DIF_BLOCK_SIZE + 2 * DV_DIF_BLOCK_SIZE + 3 + 9 * 5 + 3];
byte &= 0x1f;
if (byte == 0x00)
{
if (isIEC)
{
input->essenceType = IECDV25;
}
else
{
input->essenceType = DVBased25;
}
}
else if (byte == 0x04)
{
input->essenceType = DVBased50;
}
else if (byte == 0x14)
{
input->essenceType = DV1080i;
}
else if (byte == 0x18)
{
input->essenceType = DV720p;
}
else
{
fprintf(stderr, "%s: Unknown DV bit rate\n", filename);
return 0;
}
input->isDV = 1;
return 1;
}
/* TODO: have a problem if start-of-frame marker not found directly after end-of-frame */
static int read_next_mjpeg_image_data(RawFile *file, MJPEGState *state, unsigned char **dataOut, uint32_t *dataOutSize,
int *haveImage)
{
*haveImage = 0;
if (state->position >= state->dataSize)
{
if (state->dataSize < state->bufferSize)
{
/* EOF if previous read was less than capacity of buffer */
return 0;
}
if ((state->dataSize = rf_read(file, state->buffer, state->bufferSize)) == 0)
{
/* EOF if nothing was read */
return 0;
}
state->prevPosition = 0;
state->position = 0;
}
/* locate start and end of image */
while (!(*haveImage) && state->position < state->dataSize)
{
switch (state->markerState)
{
case 0:
if (state->buffer[state->position] == 0xFF)
{
state->markerState = 1;
}
else
{
fprintf(stderr, "Warning: MJPEG image start is non-0xFF byte - trailing data ignored\n");
fprintf(stderr, "Warning: near file offset %" PRId64 "\n", rf_tell(file));
return 0;
}
break;
case 1:
if (state->buffer[state->position] == 0xD8) /* start of frame */
{
state->markerState = 2;
}
else if (state->buffer[state->position] != 0xFF) /* 0xFF is fill byte */
{
state->markerState = 0;
}
break;
case 2:
if (state->buffer[state->position] == 0xFF)
{
state->markerState = 3;
}
/* else wait here */
break;
case 3:
if (state->buffer[state->position] == 0xD9) /* end of field */
{
state->markerState = 0;
state->endOfField = 1;
}
/* 0xD0-0xD7 and 0x01 are empty markers and 0x00 is stuffed zero */
else if ((state->buffer[state->position] >= 0xD0 && state->buffer[state->position] <= 0xD7) ||
state->buffer[state->position] == 0x01 ||
state->buffer[state->position] == 0x00)
{
state->markerState = 2;
}
else if (state->buffer[state->position] != 0xFF) /* 0xFF is fill byte */
{
state->markerState = 4;
/* initialise for state 4 */
state->haveLenByte1 = 0;
state->haveLenByte2 = 0;
state->skipCount = 0;
}
break;
case 4:
if (!state->haveLenByte1)
{
state->haveLenByte1 = 1;
state->skipCount = state->buffer[state->position] << 8;
}
else if (!state->haveLenByte2)
{
state->haveLenByte2 = 1;
state->skipCount += state->buffer[state->position];
state->skipCount -= 1; /* length includes the 2 length bytes, one subtracted here and one below */
}
if (state->haveLenByte1 && state->haveLenByte2)
{
state->skipCount--;
if (state->skipCount == 0)
{
state->markerState = 2;
}
}
break;
default:
assert(0);
return 0;
break;
}
state->position++;
if (state->endOfField)
{
/* mjpeg151s and mjpeg101m resolutions are single field; other mjpeg resolutions are 2 fields */
if (state->resolution == Res151s || state->resolution == Res101m || state->resolution == Res41m ||
state->field2)
{
*haveImage = 1;
}
state->endOfField = 0;
state->field2 = !state->field2;
}
}
*dataOut = &state->buffer[state->prevPosition];
*dataOutSize = state->position - state->prevPosition;
state->prevPosition = state->position;
return 1;
}
static uint32_t get_uint32_le(unsigned char *buffer)
{
return (buffer[3]<<24) | (buffer[2]<<16) | (buffer[1]<<8) | (buffer[0]);
}
static uint16_t get_uint16_le(unsigned char *buffer)
{
return (buffer[1]<<8) | (buffer[0]);
}
static int prepare_wave_file(const char *filename, WAVInput *input)
{
int64_t size = 0;
int haveFormatData = 0;
int haveWAVEData = 0;
unsigned char buffer[512];
memset(input, 0, sizeof(WAVInput));
if ((input->file = rf_open(filename)) == NULL)
{
fprintf(stderr, "Failed to open WAV file '%s'\n", filename);
return 0;
}
/* 'RIFF'(4) + size (4) + 'WAVE' (4) */
if (rf_read(input->file, buffer, 12) < 12)
{
fprintf(stderr, "Failed to read wav RIFF format specifier\n");
return 0;
}
if (memcmp(buffer, RIFF_ID, 4) != 0 || memcmp(&buffer[8], WAVE_ID, 4) != 0)
{
fprintf(stderr, "Not a RIFF WAVE file\n");
return 0;
}
/* get the fmt data */
while (1)
{
/* read chunk id (4) plus chunk data size (4) */
if (rf_read(input->file, buffer, 8) < 8)
{
if (rf_eof(input->file) != 0)
{
break;
}
fprintf(stderr, "Failed to read next wav chunk name and size\n");
return 0;
}
size = get_uint32_le(&buffer[4]);
if (memcmp(buffer, FMT_ID, 4) == 0)
{
/* read the common fmt data */
if (rf_read(input->file, buffer, 14) < 14)
{
fprintf(stderr, "Failed to read the wav format chunk (common part)\n");
return 0;
}
if (get_uint16_le(buffer) != WAVE_FORMAT_PCM)
{
fprintf(stderr, "Unexpected wav format - expecting WAVE_FORMAT_PCM (0x0001)\n");
return 0;
}
input->numAudioChannels = get_uint16_le(&buffer[2]);
if (input->numAudioChannels == 0)
{
fprintf(stderr, "Number wav audio channels is zero\n");
return 0;
}
input->audioSamplingRate.numerator = get_uint32_le(&buffer[4]);
input->audioSamplingRate.denominator = 1;
input->nBlockAlign = get_uint16_le(&buffer[12]);
if (rf_read(input->file, buffer, 2) < 2)
{
fprintf(stderr, "Failed to read the wav PCM sample size\n");
return 0;
}
input->audioSampleBits = get_uint16_le(buffer);
input->bytesPerSample = (input->audioSampleBits + 7) / 8;
if (input->numAudioChannels * input->bytesPerSample != input->nBlockAlign)
{
fprintf(stderr, "WARNING: Block alignment in file, %d, is incorrect. Assuming value is %d\n",
input->nBlockAlign, input->numAudioChannels * input->bytesPerSample);
input->nBlockAlign = input->numAudioChannels * input->bytesPerSample;
}
if (rf_seek(input->file, size - 14 - 2, SEEK_CUR) < 0)
{
fprintf(stderr, "Failed to seek to end of wav chunk\n");
return 0;
}
haveFormatData = 1;
}
else if (memcmp(buffer, DATA_ID, 4) == 0)
{
/* get the wave data offset and size */
input->dataOffset = rf_tell(input->file);
input->dataSize = size;
if (rf_seek(input->file, size, SEEK_CUR) < 0)
{
fprintf(stderr, "Failed to seek to end of wav chunk\n");
return 0;
}
haveWAVEData = 1;
}
else
{
if (rf_seek(input->file, size, SEEK_CUR) < 0)
{
fprintf(stderr, "Failed to seek to end of wav chunk\n");
return 0;
}
}
}
if (!haveFormatData)
{
fprintf(stderr, "Missing 'fmt ' chunk in wav file\n");
return 0;
}
if (!haveWAVEData)
{
fprintf(stderr, "Missing 'data' chunk in wav file\n");
return 0;
}
/* position at wave data */
if (rf_seek(input->file, input->dataOffset, SEEK_SET) < 0)
{
fprintf(stderr, "Failed to seek to start of wav data chunk\n");
return 0;
}
return 1;
}
static int get_wave_data(WAVInput *input, unsigned char *buffer, uint32_t dataSize, uint32_t *numRead)
{
uint32_t numToRead;
uint32_t actualRead;
if (input->totalRead >= input->dataSize)
{
*numRead = 0;
return 1;
}
if (dataSize > input->dataSize - input->totalRead)
{
numToRead = (uint32_t)(input->dataSize - input->totalRead);
}
else
{
numToRead = dataSize;
}
if ((actualRead = rf_read(input->file, buffer, numToRead)) != numToRead)
{
fprintf(stderr, "Failed to read %u bytes of wave data. Actual read was %u\n", numToRead, actualRead);
return 0;
}
*numRead = actualRead;
input->totalRead += actualRead;
return 1;
}
static void get_wave_channel(WAVInput *input, uint32_t dataSize, unsigned char *buffer,
int channelIndex, unsigned char *channelBuffer)
{
uint32_t i;
int j;
int channelOffset = channelIndex * input->bytesPerSample;
uint32_t numSamples = dataSize / input->nBlockAlign;
for (i = 0; i < numSamples; i++)
{
for (j = 0; j < input->bytesPerSample; j++)
{
channelBuffer[i * input->bytesPerSample + j] = buffer[i * input->nBlockAlign + channelOffset + j];
}
}
}
static void get_filename(const char *filenamePrefix, int isVideo, int typeTrackNum, char *filename, size_t filenameSize)
{
if (isVideo)
{
mxf_snprintf(filename, filenameSize, "%s_v%d.mxf", filenamePrefix, typeTrackNum);
}
else
{
mxf_snprintf(filename, filenameSize, "%s_a%d.mxf", filenamePrefix, typeTrackNum);
}
}
static void get_track_name(int isVideo, int typeTrackNum, char *trackName, size_t trackNameSize)
{
if (isVideo)
{
mxf_snprintf(trackName, trackNameSize, "V%d", typeTrackNum);
}
else
{
mxf_snprintf(trackName, trackNameSize, "A%d", typeTrackNum);
}
}
static int parse_timestamp(const char *timestampStr, mxfTimestamp *ts)
{
int data[7];
int result;
result = sscanf(timestampStr, "%u-%u-%uT%u:%u:%u:%u",
&data[0], &data[1], &data[2], &data[3], &data[4], &data[5], &data[6]);
if (result != 7)
{
return 0;
}
ts->year = data[0];
ts->month = data[1];
ts->day = data[2];
ts->hour = data[3];
ts->min = data[4];
ts->sec = data[5];
ts->qmsec = data[6];
return 1;
}
static int parse_umid(const char *umidStr, mxfUMID *umid)
{
int bytes[32];
int result;
result = sscanf(umidStr,
"%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x"
"%02x%02x%02x%02x%02x%02x%02x%02x",
&bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[4], &bytes[5], &bytes[6], &bytes[7],
&bytes[8], &bytes[9], &bytes[10], &bytes[11], &bytes[12], &bytes[13], &bytes[14], &bytes[15],
&bytes[16], &bytes[17], &bytes[18], &bytes[19], &bytes[20], &bytes[21], &bytes[22], &bytes[23],
&bytes[24], &bytes[25], &bytes[26], &bytes[27], &bytes[28], &bytes[29], &bytes[30], &bytes[31]);
if (result != 32)
{
return 0;
}
umid->octet0 = bytes[0] & 0xff;
umid->octet1 = bytes[1] & 0xff;
umid->octet2 = bytes[2] & 0xff;
umid->octet3 = bytes[3] & 0xff;
umid->octet4 = bytes[4] & 0xff;
umid->octet5 = bytes[5] & 0xff;
umid->octet6 = bytes[6] & 0xff;
umid->octet7 = bytes[7] & 0xff;
umid->octet8 = bytes[8] & 0xff;
umid->octet9 = bytes[9] & 0xff;
umid->octet10 = bytes[10] & 0xff;
umid->octet11 = bytes[11] & 0xff;
umid->octet12 = bytes[12] & 0xff;
umid->octet13 = bytes[13] & 0xff;
umid->octet14 = bytes[14] & 0xff;
umid->octet15 = bytes[15] & 0xff;
umid->octet16 = bytes[16] & 0xff;
umid->octet17 = bytes[17] & 0xff;
umid->octet18 = bytes[18] & 0xff;
umid->octet19 = bytes[19] & 0xff;
umid->octet20 = bytes[20] & 0xff;
umid->octet21 = bytes[21] & 0xff;
umid->octet22 = bytes[22] & 0xff;
umid->octet23 = bytes[23] & 0xff;
umid->octet24 = bytes[24] & 0xff;
umid->octet25 = bytes[25] & 0xff;
umid->octet26 = bytes[26] & 0xff;
umid->octet27 = bytes[27] & 0xff;
umid->octet28 = bytes[28] & 0xff;
umid->octet29 = bytes[29] & 0xff;
umid->octet30 = bytes[30] & 0xff;
umid->octet31 = bytes[31] & 0xff;
return 1;
}
int parse_timecode(const char *timecodeStr, const mxfRational *videoSampleRate, int64_t *frameCount)
{
int result;
int hour, min, sec, frame;
int roundedTimecodeBase;
const char *checkPtr;
roundedTimecodeBase = (int)(videoSampleRate->numerator / (double)videoSampleRate->denominator + 0.5);
/* drop frame timecode */
result = sscanf(timecodeStr, "d%d:%d:%d:%d", &hour, &min, &sec, &frame);
if (result == 4)
{
*frameCount = hour * 60 * 60 * roundedTimecodeBase +
min * 60 * roundedTimecodeBase +
sec * roundedTimecodeBase +
frame;
if (roundedTimecodeBase == 25)
{
/* ignore 'd' - no drop frame for PAL */
return 1;
}
/* first 2 frame numbers shall be omitted at the start of each minute,
except minutes 0, 10, 20, 30, 40 and 50 */
/* add frames omitted */
*frameCount += (60 - 6) * 2 * hour; /* every whole hour */
*frameCount += (min / 10) * 9 * 2; /* every whole 10 min */
*frameCount += (min % 10) * 2; /* every whole min, except min 0 */
return 1;
}
/* non-drop frame timecode */
result = sscanf(timecodeStr, "%d:%d:%d:%d", &hour, &min, &sec, &frame);
if (result == 4)
{
*frameCount = hour * 60 * 60 * roundedTimecodeBase +
min * 60 * roundedTimecodeBase +
sec * roundedTimecodeBase +
frame;
return 1;
}
/* frame count */
result = sscanf(timecodeStr, "%" PRId64 "", frameCount);
if (result == 1)
{
/* make sure it was a number */
checkPtr = timecodeStr;
while (*checkPtr != 0)
{
if (*checkPtr < '0' || *checkPtr > '9')
{
return 0;
}
checkPtr++;
}
return 1;
}
return 0;
}
int parse_position(int64_t startPosition, const char *positionStr, const mxfRational *videoSampleRate, int64_t *position)
{
if (positionStr[0] == 'o')
{
return parse_timecode(positionStr + 1, videoSampleRate, position);
}
else
{
int64_t abs_position;
if (!parse_timecode(positionStr, videoSampleRate, &abs_position))
{
return 0;
}
*position = abs_position - startPosition;
return 1;
}
}
int parse_color(const char *colorStr, AvidRGBColor *color)
{
if (strcmp(colorStr, "white") == 0)
{
*color = AVID_WHITE;
return 1;
}
else if (strcmp(colorStr, "red") == 0)
{
*color = AVID_RED;
return 1;
}
else if (strcmp(colorStr, "yellow") == 0)
{
*color = AVID_YELLOW;
return 1;
}
else if (strcmp(colorStr, "green") == 0)
{
*color = AVID_GREEN;
return 1;
}
else if (strcmp(colorStr, "cyan") == 0)
{
*color = AVID_CYAN;
return 1;
}
else if (strcmp(colorStr, "blue") == 0)
{
*color = AVID_BLUE;
return 1;
}
else if (strcmp(colorStr, "magenta") == 0)
{
*color = AVID_MAGENTA;
return 1;
}
else if (strcmp(colorStr, "black") == 0)
{
*color = AVID_BLACK;
return 1;
}
else
{
return 0;
}
}
static int parse_boolean(const char *boolStr, int *value)
{
if (strcmp(boolStr, "1") == 0 ||
strcmp(boolStr, "T") == 0 ||
strcmp(boolStr, "t") == 0 ||
strcmp(boolStr, "true") == 0)
{
*value = 1;
return 1;
}
else if (strcmp(boolStr, "0") == 0 ||
strcmp(boolStr, "F") == 0 ||
strcmp(boolStr, "f") == 0 ||
strcmp(boolStr, "false") == 0)
{
*value = 0;
return 1;
}
return 0;
}
static int parse_frame_rate(const char *rateStr, mxfRational *frameRate)
{
unsigned int value;
if (sscanf(rateStr, "%u", &value) != 1)
return 0;
if (value == 24 || value == 25 || value == 30 || value == 50 || value == 60) {
frameRate->numerator = (int32_t)value;
frameRate->denominator = 1;
} else if (value == 23976) {
frameRate->numerator = 24000;
frameRate->denominator = 1001;
} else if (value == 2997) {
frameRate->numerator = 30000;
frameRate->denominator = 1001;
} else if (value == 5994) {
frameRate->numerator = 60000;
frameRate->denominator = 1001;
} else {
return 0;
}
return 1;
}
static void usage(const char *cmd)
{
fprintf(stderr, "Usage: %s <<options>> <<inputs>>\n", cmd);
fprintf(stderr, "\n");
fprintf(stderr, "Options: (options marked with * are required)\n");
fprintf(stderr, " -h, --help display this usage message\n");
fprintf(stderr, "* --prefix <filename> output filename prefix\n");
fprintf(stderr, " --clip <name> clip (MaterialPackage) name.\n");
fprintf(stderr, " --project <name> Avid project name.\n");
fprintf(stderr, " --tape <name> tape name.\n");
fprintf(stderr, " --ntsc NTSC framerate and frame size. Default is DV file frame rate or PAL\n");
fprintf(stderr, " --film24 use framerate of 24 instead of default 25fps\n");
fprintf(stderr, " --film23.976 use framerate of 23.976 (24000/1001) instead of default 25fps\n");
fprintf(stderr, " --fps <rate> set frame rate: 23976 (24000/1001), 24, 25, 2997 (30000/1001), 30, 50, 5994 (60000/1001) or 60.\n");
fprintf(stderr, " Default is the DV file frame rate, 50 for progressive video, otherwise 25\n");
fprintf(stderr, " --legacy use legacy DataDefs, for DV essence use legacy descriptor properties\n");
fprintf(stderr, " --legacy-umid use the legacy UMID generation method (e.g. for Pro Tools v5.3.1)\n");
fprintf(stderr, " --aspect <ratio> video aspect ratio x:y. Default is DV file aspect ratio or 4:3\n");
fprintf(stderr, " --comment <string> add 'Comments' user comment to the MaterialPackage\n");
fprintf(stderr, " --desc <string> add 'Descript' user comment to the MaterialPackage\n");
fprintf(stderr, " --tag <name> <string> add <name> user comment to the MaterialPackage. Option can be used multiple times\n");
fprintf(stderr, " --locator <position> <comment> <color>\n");
fprintf(stderr, " add locator at <position> with <comment> and <color>\n");
fprintf(stderr, " --start-tc <timecode> set the start timecode. Default is 0 frames\n");
fprintf(stderr, " --mp-uid <umid> set the MaterialPackage UMID. Autogenerated by default\n");
fprintf(stderr, " --mp-created <timestamp> set the MaterialPackage creation date. Default is now\n");
fprintf(stderr, " --tp-uid <umid> set the tape SourcePackage UMID. Autogenerated by default\n");
fprintf(stderr, " --tp-created <timestamp> set the tape SourcePackage creation date. Default is now\n");
fprintf(stderr, "Inputs:\n");
fprintf(stderr, " --mjpeg <filename> Avid MJPEG\n");
fprintf(stderr, " --res <resolution> Resolution '2:1' (default), '3:1', '10:1', '4:1m', '10:1m', '15:1s' or '20:1'\n");
fprintf(stderr, " --dv <filename> IEC DV 25, DV-based 25 / 50, DV 100 1080i / 720p (SMPTE 370M)\n");
fprintf(stderr, " --IMX30 <filename> IMX 30 Mbps MPEG-2 video (D-10, SMPTE 356M)\n");
fprintf(stderr, " --IMX40 <filename> IMX 40 Mbps MPEG-2 video (D-10, SMPTE 356M)\n");
fprintf(stderr, " --IMX50 <filename> IMX 50 Mbps MPEG-2 video (D-10, SMPTE 356M)\n");
fprintf(stderr, " --imx-size <size> IMX fixed frame size in bytes\n");
fprintf(stderr, " Default is %d/%d/%d for IMX30/40/50 PAL\n", g_defaultIMX30FrameSizePAL, g_defaultIMX40FrameSizePAL, g_defaultIMX50FrameSizePAL);
fprintf(stderr, " Default is %d/%d/%d for IMX30/40/50 NTSC\n", g_defaultIMX30FrameSizeNTSC, g_defaultIMX40FrameSizeNTSC, g_defaultIMX50FrameSizeNTSC);
fprintf(stderr, " --DNxHD1080p1235 <filename> DNxHD 1920x1080p 220/185/175 Mbps 10bit\n");
fprintf(stderr, " --DNxHD1080p1237 <filename> DNxHD 1920x1080p 145/120/115 Mbps\n");
fprintf(stderr, " --DNxHD1080p1238 <filename> DNxHD 1920x1080p 220/185/175 Mbps\n");
fprintf(stderr, " --DNxHD1080i1241 <filename> DNxHD 1920x1080i 220/185 Mbps 10bit\n");
fprintf(stderr, " --DNxHD1080i1242 <filename> DNxHD 1920x1080i 145/120 Mbps\n");
fprintf(stderr, " --DNxHD1080i1243 <filename> DNxHD 1920x1080i 220/185 Mbps\n");
fprintf(stderr, " --DNxHD720p1250 <filename> DNxHD 1280x720p 220/185/110/90 Mbps 10bit\n");
fprintf(stderr, " --DNxHD720p1251 <filename> DNxHD 1280x720p 220/185/110/90 Mbps\n");
fprintf(stderr, " --DNxHD720p1252 <filename> DNxHD 1280x720p 220/185/110/90 Mbps\n");
fprintf(stderr, " --DNxHD1080p1253 <filename> DNxHD 1920x1080p 45/36 Mbps\n");
fprintf(stderr, " --unc <filename> Uncompressed 8-bit UYVY SD\n");
fprintf(stderr, " --height <value> image height. Default is 576 for PAL or 486 for NTSC\n");
fprintf(stderr, " --unc1080i <filename> Uncompressed 8-bit UYVY HD 1920x1080i\n");
fprintf(stderr, " --unc1080p <filename> Uncompressed 8-bit UYVY HD 1920x1080p\n");
fprintf(stderr, " --unc720p <filename> Uncompressed 8-bit UYVY HD 1280x720p\n");
fprintf(stderr, " --pcm <filename> raw 48kHz PCM audio\n");
fprintf(stderr, " --bps <bits per sample> # bits per sample. Default is 16\n");
fprintf(stderr, " --locked <bool> true/false to indicate whether the number of audio samples is locked to the video (not set by default)\n");
fprintf(stderr, " --ref <level> audio reference level which gives the number of dBm for 0VU (not set by default)\n");
fprintf(stderr, " --dial-norm <value> gain to be applied to normalize perceived loudness of the clip (not set by default)\n");
fprintf(stderr, " --seq <offset> zero-based ordinal frame number of first audio essence data within five-frame sequence (not set by default)\n");
fprintf(stderr, " --wavpcm <filename> raw 48kHz PCM audio contained in a WAV file\n");
fprintf(stderr, " --locked <bool> true/false to indicate whether the number of audio samples is locked to the video (not set by default)\n");