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
|
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
*
* Copyright (c) Sandflow Consulting LLC
*
* 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.
*
* 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 HOLDER 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.
*/
/**
* Implements IMP CPL processing
*
* @author Pierre-Anthony Lemieux
* @file
* @ingroup lavu_imf
*/
#include "imf.h"
#include "libavformat/mxf.h"
#include "libavutil/bprint.h"
#include "libavutil/error.h"
#include <libxml/parser.h>
xmlNodePtr ff_imf_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8)
{
xmlNodePtr cur_element;
cur_element = xmlFirstElementChild(parent);
while (cur_element) {
if (xmlStrcmp(cur_element->name, name_utf8) == 0)
return cur_element;
cur_element = xmlNextElementSibling(cur_element);
}
return NULL;
}
int ff_imf_xml_read_uuid(xmlNodePtr element, uint8_t uuid[16])
{
xmlChar *element_text = NULL;
int scanf_ret;
int ret = 0;
element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
scanf_ret = sscanf(element_text,
FF_IMF_UUID_FORMAT,
&uuid[0],
&uuid[1],
&uuid[2],
&uuid[3],
&uuid[4],
&uuid[5],
&uuid[6],
&uuid[7],
&uuid[8],
&uuid[9],
&uuid[10],
&uuid[11],
&uuid[12],
&uuid[13],
&uuid[14],
&uuid[15]);
if (scanf_ret != 16) {
av_log(NULL, AV_LOG_ERROR, "Invalid UUID\n");
ret = AVERROR_INVALIDDATA;
}
xmlFree(element_text);
return ret;
}
int ff_imf_xml_read_rational(xmlNodePtr element, AVRational *rational)
{
xmlChar *element_text = NULL;
int ret = 0;
element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
if (sscanf(element_text, "%i %i", &rational->num, &rational->den) != 2) {
av_log(NULL, AV_LOG_ERROR, "Invalid rational number\n");
ret = AVERROR_INVALIDDATA;
}
xmlFree(element_text);
return ret;
}
int ff_imf_xml_read_uint32(xmlNodePtr element, uint32_t *number)
{
xmlChar *element_text = NULL;
int ret = 0;
element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
if (sscanf(element_text, "%" PRIu32, number) != 1) {
av_log(NULL, AV_LOG_ERROR, "Invalid unsigned 32-bit integer");
ret = AVERROR_INVALIDDATA;
}
xmlFree(element_text);
return ret;
}
static void imf_base_virtual_track_init(FFIMFBaseVirtualTrack *track)
{
memset(track->id_uuid, 0, sizeof(track->id_uuid));
}
static void imf_marker_virtual_track_init(FFIMFMarkerVirtualTrack *track)
{
imf_base_virtual_track_init((FFIMFBaseVirtualTrack *)track);
track->resource_count = 0;
track->resources = NULL;
}
static void imf_trackfile_virtual_track_init(FFIMFTrackFileVirtualTrack *track)
{
imf_base_virtual_track_init((FFIMFBaseVirtualTrack *)track);
track->resource_count = 0;
track->resources_alloc_sz = 0;
track->resources = NULL;
}
static void imf_base_resource_init(FFIMFBaseResource *rsrc)
{
rsrc->duration = 0;
rsrc->edit_rate = av_make_q(0, 1);
rsrc->entry_point = 0;
rsrc->repeat_count = 1;
}
static void imf_marker_resource_init(FFIMFMarkerResource *rsrc)
{
imf_base_resource_init((FFIMFBaseResource *)rsrc);
rsrc->marker_count = 0;
rsrc->markers = NULL;
}
static void imf_marker_init(FFIMFMarker *marker)
{
marker->label_utf8 = NULL;
marker->offset = 0;
marker->scope_utf8 = NULL;
}
static void imf_trackfile_resource_init(FFIMFTrackFileResource *rsrc)
{
imf_base_resource_init((FFIMFBaseResource *)rsrc);
memset(rsrc->track_file_uuid, 0, sizeof(rsrc->track_file_uuid));
}
static int fill_content_title(xmlNodePtr cpl_element, FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "ContentTitle"))) {
av_log(NULL, AV_LOG_ERROR, "ContentTitle element not found in the IMF CPL\n");
return AVERROR_INVALIDDATA;
}
cpl->content_title_utf8 = xmlNodeListGetString(cpl_element->doc,
element->xmlChildrenNode,
1);
return 0;
}
static int fill_edit_rate(xmlNodePtr cpl_element, FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "EditRate"))) {
av_log(NULL, AV_LOG_ERROR, "EditRate element not found in the IMF CPL\n");
return AVERROR_INVALIDDATA;
}
return ff_imf_xml_read_rational(element, &cpl->edit_rate);
}
static int fill_id(xmlNodePtr cpl_element, FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "Id"))) {
av_log(NULL, AV_LOG_ERROR, "Id element not found in the IMF CPL\n");
return AVERROR_INVALIDDATA;
}
return ff_imf_xml_read_uuid(element, cpl->id_uuid);
}
static int fill_marker(xmlNodePtr marker_elem, FFIMFMarker *marker)
{
xmlNodePtr element = NULL;
int ret = 0;
/* read Offset */
if (!(element = ff_imf_xml_get_child_element_by_name(marker_elem, "Offset"))) {
av_log(NULL, AV_LOG_ERROR, "Offset element not found in a Marker\n");
return AVERROR_INVALIDDATA;
}
if ((ret = ff_imf_xml_read_uint32(element, &marker->offset)))
return ret;
/* read Label and Scope */
if (!(element = ff_imf_xml_get_child_element_by_name(marker_elem, "Label"))) {
av_log(NULL, AV_LOG_ERROR, "Label element not found in a Marker\n");
return AVERROR_INVALIDDATA;
}
if (!(marker->label_utf8 = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1))) {
av_log(NULL, AV_LOG_ERROR, "Empty Label element found in a Marker\n");
return AVERROR_INVALIDDATA;
}
if (!(marker->scope_utf8 = xmlGetNoNsProp(element, "scope"))) {
marker->scope_utf8
= xmlCharStrdup("http://www.smpte-ra.org/schemas/2067-3/2013#standard-markers");
if (!marker->scope_utf8) {
xmlFree(marker->label_utf8);
return AVERROR(ENOMEM);
}
}
return ret;
}
static int fill_base_resource(xmlNodePtr resource_elem, FFIMFBaseResource *resource, FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
int ret = 0;
/* read EditRate */
if (!(element = ff_imf_xml_get_child_element_by_name(resource_elem, "EditRate"))) {
resource->edit_rate = cpl->edit_rate;
} else if ((ret = ff_imf_xml_read_rational(element, &resource->edit_rate))) {
av_log(NULL, AV_LOG_ERROR, "Invalid EditRate element found in a Resource\n");
return ret;
}
/* read EntryPoint */
if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "EntryPoint"))) {
if ((ret = ff_imf_xml_read_uint32(element, &resource->entry_point))) {
av_log(NULL, AV_LOG_ERROR, "Invalid EntryPoint element found in a Resource\n");
return ret;
}
} else {
resource->entry_point = 0;
}
/* read IntrinsicDuration */
if (!(element = ff_imf_xml_get_child_element_by_name(resource_elem, "IntrinsicDuration"))) {
av_log(NULL, AV_LOG_ERROR, "IntrinsicDuration element missing from Resource\n");
return AVERROR_INVALIDDATA;
}
if ((ret = ff_imf_xml_read_uint32(element, &resource->duration))) {
av_log(NULL, AV_LOG_ERROR, "Invalid IntrinsicDuration element found in a Resource\n");
return ret;
}
resource->duration -= resource->entry_point;
/* read SourceDuration */
if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "SourceDuration"))) {
if ((ret = ff_imf_xml_read_uint32(element, &resource->duration))) {
av_log(NULL, AV_LOG_ERROR, "SourceDuration element missing from Resource\n");
return ret;
}
}
/* read RepeatCount */
if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "RepeatCount")))
ret = ff_imf_xml_read_uint32(element, &resource->repeat_count);
return ret;
}
static int fill_trackfile_resource(xmlNodePtr tf_resource_elem,
FFIMFTrackFileResource *tf_resource,
FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
int ret = 0;
if ((ret = fill_base_resource(tf_resource_elem, (FFIMFBaseResource *)tf_resource, cpl)))
return ret;
/* read TrackFileId */
if ((element = ff_imf_xml_get_child_element_by_name(tf_resource_elem, "TrackFileId"))) {
if ((ret = ff_imf_xml_read_uuid(element, tf_resource->track_file_uuid))) {
av_log(NULL, AV_LOG_ERROR, "Invalid TrackFileId element found in Resource\n");
return ret;
}
} else {
av_log(NULL, AV_LOG_ERROR, "TrackFileId element missing from Resource\n");
return AVERROR_INVALIDDATA;
}
return ret;
}
static int fill_marker_resource(xmlNodePtr marker_resource_elem,
FFIMFMarkerResource *marker_resource,
FFIMFCPL *cpl)
{
xmlNodePtr element = NULL;
int ret = 0;
if ((ret = fill_base_resource(marker_resource_elem, (FFIMFBaseResource *)marker_resource, cpl)))
return ret;
/* read markers */
element = xmlFirstElementChild(marker_resource_elem);
while (element) {
if (xmlStrcmp(element->name, "Marker") == 0) {
void *tmp;
if (marker_resource->marker_count == UINT32_MAX)
return AVERROR(ENOMEM);
tmp = av_realloc_array(marker_resource->markers,
marker_resource->marker_count + 1,
sizeof(FFIMFMarker));
if (!tmp)
return AVERROR(ENOMEM);
marker_resource->markers = tmp;
imf_marker_init(&marker_resource->markers[marker_resource->marker_count]);
ret = fill_marker(element,
&marker_resource->markers[marker_resource->marker_count]);
marker_resource->marker_count++;
if (ret)
return ret;
}
element = xmlNextElementSibling(element);
}
return ret;
}
static int push_marker_sequence(xmlNodePtr marker_sequence_elem, FFIMFCPL *cpl)
{
int ret = 0;
uint8_t uuid[16];
xmlNodePtr resource_list_elem = NULL;
xmlNodePtr resource_elem = NULL;
xmlNodePtr track_id_elem = NULL;
unsigned long resource_elem_count;
void *tmp;
/* read TrackID element */
if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(marker_sequence_elem, "TrackId"))) {
av_log(NULL, AV_LOG_ERROR, "TrackId element missing from Sequence\n");
return AVERROR_INVALIDDATA;
}
if (ff_imf_xml_read_uuid(track_id_elem, uuid)) {
av_log(NULL, AV_LOG_ERROR, "Invalid TrackId element found in Sequence\n");
return AVERROR_INVALIDDATA;
}
av_log(NULL,
AV_LOG_DEBUG,
"Processing IMF CPL Marker Sequence for Virtual Track " FF_IMF_UUID_FORMAT "\n",
UID_ARG(uuid));
/* create main marker virtual track if it does not exist */
if (!cpl->main_markers_track) {
cpl->main_markers_track = av_malloc(sizeof(FFIMFMarkerVirtualTrack));
if (!cpl->main_markers_track)
return AVERROR(ENOMEM);
imf_marker_virtual_track_init(cpl->main_markers_track);
memcpy(cpl->main_markers_track->base.id_uuid, uuid, sizeof(uuid));
} else if (memcmp(cpl->main_markers_track->base.id_uuid, uuid, sizeof(uuid)) != 0) {
av_log(NULL, AV_LOG_ERROR, "Multiple marker virtual tracks were found\n");
return AVERROR_INVALIDDATA;
}
/* process resources */
resource_list_elem = ff_imf_xml_get_child_element_by_name(marker_sequence_elem, "ResourceList");
if (!resource_list_elem)
return 0;
resource_elem_count = xmlChildElementCount(resource_list_elem);
if (resource_elem_count > UINT32_MAX
|| cpl->main_markers_track->resource_count > UINT32_MAX - resource_elem_count)
return AVERROR(ENOMEM);
tmp = av_realloc_array(cpl->main_markers_track->resources,
cpl->main_markers_track->resource_count + resource_elem_count,
sizeof(FFIMFMarkerResource));
if (!tmp) {
av_log(NULL, AV_LOG_ERROR, "Cannot allocate Marker Resources\n");
return AVERROR(ENOMEM);
}
cpl->main_markers_track->resources = tmp;
resource_elem = xmlFirstElementChild(resource_list_elem);
while (resource_elem) {
imf_marker_resource_init(&cpl->main_markers_track->resources[cpl->main_markers_track->resource_count]);
ret = fill_marker_resource(resource_elem,
&cpl->main_markers_track->resources[cpl->main_markers_track->resource_count],
cpl);
cpl->main_markers_track->resource_count++;
if (ret)
return ret;
resource_elem = xmlNextElementSibling(resource_elem);
}
return ret;
}
static int has_stereo_resources(xmlNodePtr element)
{
if (xmlStrcmp(element->name, "Left") == 0 || xmlStrcmp(element->name, "Right") == 0)
return 1;
element = xmlFirstElementChild(element);
while (element) {
if (has_stereo_resources(element))
return 1;
element = xmlNextElementSibling(element);
}
return 0;
}
static int push_main_audio_sequence(xmlNodePtr audio_sequence_elem, FFIMFCPL *cpl)
{
int ret = 0;
uint8_t uuid[16];
xmlNodePtr resource_list_elem = NULL;
xmlNodePtr resource_elem = NULL;
xmlNodePtr track_id_elem = NULL;
unsigned long resource_elem_count;
FFIMFTrackFileVirtualTrack *vt = NULL;
void *tmp;
/* read TrackID element */
if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(audio_sequence_elem, "TrackId"))) {
av_log(NULL, AV_LOG_ERROR, "TrackId element missing from audio sequence\n");
return AVERROR_INVALIDDATA;
}
if ((ret = ff_imf_xml_read_uuid(track_id_elem, uuid))) {
av_log(NULL, AV_LOG_ERROR, "Invalid TrackId element found in audio sequence\n");
return ret;
}
av_log(NULL,
AV_LOG_DEBUG,
"Processing IMF CPL Audio Sequence for Virtual Track " FF_IMF_UUID_FORMAT "\n",
UID_ARG(uuid));
/* get the main audio virtual track corresponding to the sequence */
for (uint32_t i = 0; i < cpl->main_audio_track_count; i++) {
if (memcmp(cpl->main_audio_tracks[i].base.id_uuid, uuid, sizeof(uuid)) == 0) {
vt = &cpl->main_audio_tracks[i];
break;
}
}
/* create a main audio virtual track if none exists for the sequence */
if (!vt) {
if (cpl->main_audio_track_count == UINT32_MAX)
return AVERROR(ENOMEM);
tmp = av_realloc_array(cpl->main_audio_tracks,
cpl->main_audio_track_count + 1,
sizeof(FFIMFTrackFileVirtualTrack));
if (!tmp)
return AVERROR(ENOMEM);
cpl->main_audio_tracks = tmp;
vt = &cpl->main_audio_tracks[cpl->main_audio_track_count];
imf_trackfile_virtual_track_init(vt);
cpl->main_audio_track_count++;
memcpy(vt->base.id_uuid, uuid, sizeof(uuid));
}
/* process resources */
resource_list_elem = ff_imf_xml_get_child_element_by_name(audio_sequence_elem, "ResourceList");
if (!resource_list_elem)
return 0;
resource_elem_count = xmlChildElementCount(resource_list_elem);
if (resource_elem_count > UINT32_MAX
|| vt->resource_count > UINT32_MAX - resource_elem_count)
return AVERROR(ENOMEM);
tmp = av_fast_realloc(vt->resources,
&vt->resources_alloc_sz,
(vt->resource_count + resource_elem_count)
* sizeof(FFIMFTrackFileResource));
if (!tmp) {
av_log(NULL, AV_LOG_ERROR, "Cannot allocate Main Audio Resources\n");
return AVERROR(ENOMEM);
}
vt->resources = tmp;
resource_elem = xmlFirstElementChild(resource_list_elem);
while (resource_elem) {
imf_trackfile_resource_init(&vt->resources[vt->resource_count]);
ret = fill_trackfile_resource(resource_elem,
&vt->resources[vt->resource_count],
cpl);
vt->resource_count++;
if (ret) {
av_log(NULL, AV_LOG_ERROR, "Invalid Resource\n");
continue;
}
resource_elem = xmlNextElementSibling(resource_elem);
}
return ret;
}
static int push_main_image_2d_sequence(xmlNodePtr image_sequence_elem, FFIMFCPL *cpl)
{
int ret = 0;
uint8_t uuid[16];
xmlNodePtr resource_list_elem = NULL;
xmlNodePtr resource_elem = NULL;
xmlNodePtr track_id_elem = NULL;
void *tmp;
unsigned long resource_elem_count;
/* skip stereoscopic resources */
if (has_stereo_resources(image_sequence_elem)) {
av_log(NULL, AV_LOG_ERROR, "Stereoscopic 3D image virtual tracks not supported\n");
return AVERROR_PATCHWELCOME;
}
/* read TrackId element*/
if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(image_sequence_elem, "TrackId"))) {
av_log(NULL, AV_LOG_ERROR, "TrackId element missing from audio sequence\n");
return AVERROR_INVALIDDATA;
}
if ((ret = ff_imf_xml_read_uuid(track_id_elem, uuid))) {
av_log(NULL, AV_LOG_ERROR, "Invalid TrackId element found in audio sequence\n");
return ret;
}
/* create main image virtual track if one does not exist */
if (!cpl->main_image_2d_track) {
cpl->main_image_2d_track = av_malloc(sizeof(FFIMFTrackFileVirtualTrack));
if (!cpl->main_image_2d_track)
return AVERROR(ENOMEM);
imf_trackfile_virtual_track_init(cpl->main_image_2d_track);
memcpy(cpl->main_image_2d_track->base.id_uuid, uuid, sizeof(uuid));
} else if (memcmp(cpl->main_image_2d_track->base.id_uuid, uuid, sizeof(uuid)) != 0) {
av_log(NULL, AV_LOG_ERROR, "Multiple MainImage virtual tracks found\n");
return AVERROR_INVALIDDATA;
}
av_log(NULL,
AV_LOG_DEBUG,
"Processing IMF CPL Main Image Sequence for Virtual Track " FF_IMF_UUID_FORMAT "\n",
UID_ARG(uuid));
/* process resources */
resource_list_elem = ff_imf_xml_get_child_element_by_name(image_sequence_elem, "ResourceList");
if (!resource_list_elem)
return 0;
resource_elem_count = xmlChildElementCount(resource_list_elem);
if (resource_elem_count > UINT32_MAX
|| cpl->main_image_2d_track->resource_count > UINT32_MAX - resource_elem_count
|| (cpl->main_image_2d_track->resource_count + resource_elem_count)
> INT_MAX / sizeof(FFIMFTrackFileResource))
return AVERROR(ENOMEM);
tmp = av_fast_realloc(cpl->main_image_2d_track->resources,
&cpl->main_image_2d_track->resources_alloc_sz,
(cpl->main_image_2d_track->resource_count + resource_elem_count)
* sizeof(FFIMFTrackFileResource));
if (!tmp) {
av_log(NULL, AV_LOG_ERROR, "Cannot allocate Main Image Resources\n");
return AVERROR(ENOMEM);
}
cpl->main_image_2d_track->resources = tmp;
resource_elem = xmlFirstElementChild(resource_list_elem);
while (resource_elem) {
imf_trackfile_resource_init(
&cpl->main_image_2d_track->resources[cpl->main_image_2d_track->resource_count]);
ret = fill_trackfile_resource(resource_elem,
&cpl->main_image_2d_track->resources[cpl->main_image_2d_track->resource_count],
cpl);
cpl->main_image_2d_track->resource_count++;
if (ret) {
av_log(NULL, AV_LOG_ERROR, "Invalid Resource\n");
continue;
}
resource_elem = xmlNextElementSibling(resource_elem);
}
return 0;
}
static int fill_virtual_tracks(xmlNodePtr cpl_element, FFIMFCPL *cpl)
{
int ret = 0;
xmlNodePtr segment_list_elem = NULL;
xmlNodePtr segment_elem = NULL;
xmlNodePtr sequence_list_elem = NULL;
xmlNodePtr sequence_elem = NULL;
if (!(segment_list_elem = ff_imf_xml_get_child_element_by_name(cpl_element, "SegmentList"))) {
av_log(NULL, AV_LOG_ERROR, "SegmentList element missing\n");
return AVERROR_INVALIDDATA;
}
/* process sequences */
segment_elem = xmlFirstElementChild(segment_list_elem);
while (segment_elem) {
av_log(NULL, AV_LOG_DEBUG, "Processing IMF CPL Segment\n");
sequence_list_elem = ff_imf_xml_get_child_element_by_name(segment_elem, "SequenceList");
if (!segment_list_elem)
continue;
sequence_elem = xmlFirstElementChild(sequence_list_elem);
while (sequence_elem) {
if (xmlStrcmp(sequence_elem->name, "MarkerSequence") == 0)
ret = push_marker_sequence(sequence_elem, cpl);
else if (xmlStrcmp(sequence_elem->name, "MainImageSequence") == 0)
ret = push_main_image_2d_sequence(sequence_elem, cpl);
else if (xmlStrcmp(sequence_elem->name, "MainAudioSequence") == 0)
ret = push_main_audio_sequence(sequence_elem, cpl);
else
av_log(NULL,
AV_LOG_INFO,
"The following Sequence is not supported and is ignored: %s\n",
sequence_elem->name);
/* abort parsing only if memory error occurred */
if (ret == AVERROR(ENOMEM))
return ret;
sequence_elem = xmlNextElementSibling(sequence_elem);
}
segment_elem = xmlNextElementSibling(segment_elem);
}
return ret;
}
int ff_imf_parse_cpl_from_xml_dom(xmlDocPtr doc, FFIMFCPL **cpl)
{
int ret = 0;
xmlNodePtr cpl_element = NULL;
*cpl = ff_imf_cpl_alloc();
if (!*cpl) {
ret = AVERROR(ENOMEM);
goto cleanup;
}
cpl_element = xmlDocGetRootElement(doc);
if (!cpl_element || xmlStrcmp(cpl_element->name, "CompositionPlaylist")) {
av_log(NULL, AV_LOG_ERROR, "The root element of the CPL is not CompositionPlaylist\n");
ret = AVERROR_INVALIDDATA;
goto cleanup;
}
if ((ret = fill_content_title(cpl_element, *cpl)))
goto cleanup;
if ((ret = fill_id(cpl_element, *cpl)))
goto cleanup;
if ((ret = fill_edit_rate(cpl_element, *cpl)))
goto cleanup;
if ((ret = fill_virtual_tracks(cpl_element, *cpl)))
goto cleanup;
cleanup:
if (*cpl && ret) {
ff_imf_cpl_free(*cpl);
*cpl = NULL;
}
return ret;
}
static void imf_marker_free(FFIMFMarker *marker)
{
if (!marker)
return;
xmlFree(marker->label_utf8);
xmlFree(marker->scope_utf8);
}
static void imf_marker_resource_free(FFIMFMarkerResource *rsrc)
{
if (!rsrc)
return;
for (uint32_t i = 0; i < rsrc->marker_count; i++)
imf_marker_free(&rsrc->markers[i]);
av_freep(&rsrc->markers);
}
static void imf_marker_virtual_track_free(FFIMFMarkerVirtualTrack *vt)
{
if (!vt)
return;
for (uint32_t i = 0; i < vt->resource_count; i++)
imf_marker_resource_free(&vt->resources[i]);
av_freep(&vt->resources);
}
static void imf_trackfile_virtual_track_free(FFIMFTrackFileVirtualTrack *vt)
{
if (!vt)
return;
av_freep(&vt->resources);
}
static void imf_cpl_init(FFIMFCPL *cpl)
{
memset(cpl->id_uuid, 0, sizeof(cpl->id_uuid));
cpl->content_title_utf8 = NULL;
cpl->edit_rate = av_make_q(0, 1);
cpl->main_markers_track = NULL;
cpl->main_image_2d_track = NULL;
cpl->main_audio_track_count = 0;
cpl->main_audio_tracks = NULL;
}
FFIMFCPL *ff_imf_cpl_alloc(void)
{
FFIMFCPL *cpl;
cpl = av_malloc(sizeof(FFIMFCPL));
if (!cpl)
return NULL;
imf_cpl_init(cpl);
return cpl;
}
void ff_imf_cpl_free(FFIMFCPL *cpl)
{
if (!cpl)
return;
xmlFree(cpl->content_title_utf8);
imf_marker_virtual_track_free(cpl->main_markers_track);
if (cpl->main_markers_track)
av_freep(&cpl->main_markers_track);
imf_trackfile_virtual_track_free(cpl->main_image_2d_track);
if (cpl->main_image_2d_track)
av_freep(&cpl->main_image_2d_track);
for (uint32_t i = 0; i < cpl->main_audio_track_count; i++)
imf_trackfile_virtual_track_free(&cpl->main_audio_tracks[i]);
if (cpl->main_audio_tracks)
av_freep(&cpl->main_audio_tracks);
av_freep(&cpl);
}
int ff_imf_parse_cpl(AVIOContext *in, FFIMFCPL **cpl)
{
AVBPrint buf;
xmlDoc *doc = NULL;
int ret = 0;
int64_t filesize = 0;
filesize = avio_size(in);
filesize = filesize > 0 ? filesize : 8192;
av_bprint_init(&buf, filesize + 1, AV_BPRINT_SIZE_UNLIMITED);
ret = avio_read_to_bprint(in, &buf, UINT_MAX - 1);
if (ret < 0 || !avio_feof(in) || buf.len == 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot read IMF CPL\n");
if (ret == 0)
ret = AVERROR_INVALIDDATA;
goto clean_up;
}
LIBXML_TEST_VERSION
filesize = buf.len;
doc = xmlReadMemory(buf.str, filesize, NULL, NULL, 0);
if (!doc) {
av_log(NULL,
AV_LOG_ERROR,
"XML parsing failed when reading the IMF CPL\n");
ret = AVERROR_INVALIDDATA;
goto clean_up;
}
if ((ret = ff_imf_parse_cpl_from_xml_dom(doc, cpl))) {
av_log(NULL, AV_LOG_ERROR, "Cannot parse IMF CPL\n");
} else {
av_log(NULL,
AV_LOG_INFO,
"IMF CPL ContentTitle: %s\n",
(*cpl)->content_title_utf8);
av_log(NULL,
AV_LOG_INFO,
"IMF CPL Id: " FF_IMF_UUID_FORMAT "\n",
UID_ARG((*cpl)->id_uuid));
}
xmlFreeDoc(doc);
clean_up:
av_bprint_finalize(&buf, NULL);
return ret;
}
|