FFmpeg
Loading...
Searching...
No Matches
imf_cpl.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/*
20 *
21 * Copyright (c) Sandflow Consulting LLC
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are met:
25 *
26 * * Redistributions of source code must retain the above copyright notice, this
27 * list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copyright notice,
29 * this list of conditions and the following disclaimer in the documentation
30 * and/or other materials provided with the distribution.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
43 */
44
45/**
46 * Implements IMP CPL processing
47 *
48 * @author Pierre-Anthony Lemieux
49 * @file
50 * @ingroup lavu_imf
51 */
52
53#include "imf.h"
54#include "libavutil/bprint.h"
55#include "libavutil/error.h"
56#include "libavutil/mem.h"
57#include <libxml/parser.h>
58
59xmlNodePtr ff_imf_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8)
60{
61 xmlNodePtr cur_element;
62
63 cur_element = xmlFirstElementChild(parent);
64 while (cur_element) {
65 if (xmlStrcmp(cur_element->name, name_utf8) == 0)
66 return cur_element;
67
68 cur_element = xmlNextElementSibling(cur_element);
69 }
70 return NULL;
71}
72
73int ff_imf_xml_read_uuid(xmlNodePtr element, AVUUID uuid)
74{
75 int ret = 0;
76
77 xmlChar *element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
78 if (!element_text)
80 ret = av_uuid_urn_parse(element_text, uuid);
81 if (ret)
83 xmlFree(element_text);
84
85 return ret;
86}
87
88int ff_imf_xml_read_rational(xmlNodePtr element, AVRational *rational)
89{
90 int ret = 0;
91
92 xmlChar *element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
93 if (element_text == NULL || sscanf(element_text, "%i %i", &rational->num, &rational->den) != 2
94 || rational->num <= 0 || rational->den <= 0)
96 xmlFree(element_text);
97
98 return ret;
99}
100
101int ff_imf_xml_read_uint32(xmlNodePtr element, uint32_t *number)
102{
103 int ret = 0;
104
105 xmlChar *element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
106 if (element_text == NULL || sscanf(element_text, "%" PRIu32, number) != 1)
108 xmlFree(element_text);
109
110 return ret;
111}
112
113static int ff_imf_xml_read_boolean(xmlNodePtr element, int *value)
114{
115 int ret = 0;
116
117 xmlChar *element_text = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
118 if (xmlStrcmp(element_text, "true") == 0 || xmlStrcmp(element_text, "1") == 0)
119 *value = 1;
120 else if (xmlStrcmp(element_text, "false") == 0 || xmlStrcmp(element_text, "0") == 0)
121 *value = 0;
122 else
123 ret = 1;
124 xmlFree(element_text);
125
126 return ret;
127}
128
130{
131 memset(track->id_uuid, 0, sizeof(track->id_uuid));
132}
133
140
148
150{
151 rsrc->duration = 0;
152 rsrc->edit_rate = av_make_q(0, 1);
153 rsrc->entry_point = 0;
154 rsrc->repeat_count = 1;
155}
156
158{
160 rsrc->marker_count = 0;
161 rsrc->markers = NULL;
162}
163
164static void imf_marker_init(FFIMFMarker *marker)
165{
166 marker->label_utf8 = NULL;
167 marker->offset = 0;
168 marker->scope_utf8 = NULL;
169}
170
172{
174 memset(rsrc->track_file_uuid, 0, sizeof(rsrc->track_file_uuid));
175}
176
177static int fill_content_title(xmlNodePtr cpl_element, FFIMFCPL *cpl)
178{
179 xmlNodePtr element = NULL;
180
181 if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "ContentTitle")))
182 return AVERROR_INVALIDDATA;
183 cpl->content_title_utf8 = xmlNodeListGetString(cpl_element->doc,
184 element->xmlChildrenNode,
185 1);
186 if (!cpl->content_title_utf8)
187 cpl->content_title_utf8 = xmlStrdup("");
188 if (!cpl->content_title_utf8)
189 return AVERROR(ENOMEM);
190
191 return 0;
192}
193
194static int digit_to_int(char digit)
195{
196 if (digit >= '0' && digit <= '9')
197 return digit - '0';
198 return -1;
199}
200
201/**
202 * Parses a string that conform to the TimecodeType used in IMF CPL and defined
203 * in SMPTE ST 2067-3.
204 * @param[in] s string to parse
205 * @param[out] tc_comps pointer to an array of 4 integers where the parsed HH,
206 * MM, SS and FF fields of the timecode are returned.
207 * @return 0 on success, < 0 AVERROR code on error.
208 */
209static int parse_cpl_tc_type(const char *s, int *tc_comps)
210{
211 if (av_strnlen(s, 11) != 11)
212 return AVERROR(EINVAL);
213
214 for (int i = 0; i < 4; i++) {
215 int hi;
216 int lo;
217
218 hi = digit_to_int(s[i * 3]);
219 lo = digit_to_int(s[i * 3 + 1]);
220
221 if (hi == -1 || lo == -1)
222 return AVERROR(EINVAL);
223
224 tc_comps[i] = 10 * hi + lo;
225 }
226
227 return 0;
228}
229
230static int fill_timecode(xmlNodePtr cpl_element, FFIMFCPL *cpl)
231{
232 xmlNodePtr tc_element = NULL;
233 xmlNodePtr element = NULL;
234 xmlChar *tc_str = NULL;
235 int df = 0;
236 int comps[4];
237 int ret = 0;
238
239 tc_element = ff_imf_xml_get_child_element_by_name(cpl_element, "CompositionTimecode");
240 if (!tc_element)
241 return 0;
242
243 element = ff_imf_xml_get_child_element_by_name(tc_element, "TimecodeDropFrame");
244 if (!element)
245 return AVERROR_INVALIDDATA;
246
247 if (ff_imf_xml_read_boolean(element, &df))
248 return AVERROR_INVALIDDATA;
249
250 element = ff_imf_xml_get_child_element_by_name(tc_element, "TimecodeStartAddress");
251 if (!element)
252 return AVERROR_INVALIDDATA;
253
254 tc_str = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1);
255 if (!tc_str)
256 return AVERROR_INVALIDDATA;
257 ret = parse_cpl_tc_type(tc_str, comps);
258 xmlFree(tc_str);
259 if (ret)
260 return ret;
261
262 cpl->tc = av_malloc(sizeof(AVTimecode));
263 if (!cpl->tc)
264 return AVERROR(ENOMEM);
267 comps[0], comps[1], comps[2], comps[3],
268 NULL);
269
270 return ret;
271}
272
273static int fill_edit_rate(xmlNodePtr cpl_element, FFIMFCPL *cpl)
274{
275 xmlNodePtr element = NULL;
276
277 if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "EditRate")))
278 return AVERROR_INVALIDDATA;
279
280 return ff_imf_xml_read_rational(element, &cpl->edit_rate);
281}
282
283static int fill_id(xmlNodePtr cpl_element, FFIMFCPL *cpl)
284{
285 xmlNodePtr element = NULL;
286
287 if (!(element = ff_imf_xml_get_child_element_by_name(cpl_element, "Id")))
288 return AVERROR_INVALIDDATA;
289
290 return ff_imf_xml_read_uuid(element, cpl->id_uuid);
291}
292
293static int fill_marker(xmlNodePtr marker_elem, FFIMFMarker *marker)
294{
295 xmlNodePtr element = NULL;
296 int ret = 0;
297
298 /* read Offset */
299 if (!(element = ff_imf_xml_get_child_element_by_name(marker_elem, "Offset")))
300 return AVERROR_INVALIDDATA;
301
302 if ((ret = ff_imf_xml_read_uint32(element, &marker->offset)))
303 return ret;
304
305 /* read Label and Scope */
306 if (!(element = ff_imf_xml_get_child_element_by_name(marker_elem, "Label")))
307 return AVERROR_INVALIDDATA;
308
309 if (!(marker->label_utf8 = xmlNodeListGetString(element->doc, element->xmlChildrenNode, 1)))
310 return AVERROR_INVALIDDATA;
311
312 if (!(marker->scope_utf8 = xmlGetNoNsProp(element, "scope"))) {
313 marker->scope_utf8
314 = xmlCharStrdup("http://www.smpte-ra.org/schemas/2067-3/2013#standard-markers");
315 if (!marker->scope_utf8)
316 return AVERROR(ENOMEM);
317 }
318
319 return ret;
320}
321
322static int fill_base_resource(void *log_ctx, xmlNodePtr resource_elem,
323 FFIMFBaseResource *resource, FFIMFCPL *cpl)
324{
325 xmlNodePtr element = NULL;
326 int ret = 0;
327
328 /* read EditRate */
329 if (!(element = ff_imf_xml_get_child_element_by_name(resource_elem, "EditRate"))) {
330 resource->edit_rate = cpl->edit_rate;
331 } else if ((ret = ff_imf_xml_read_rational(element, &resource->edit_rate))) {
332 av_log(log_ctx, AV_LOG_ERROR, "Invalid EditRate element found in a Resource\n");
333 return ret;
334 }
335
336 /* read EntryPoint */
337 if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "EntryPoint"))) {
338 if ((ret = ff_imf_xml_read_uint32(element, &resource->entry_point))) {
339 av_log(log_ctx, AV_LOG_ERROR, "Invalid EntryPoint element found in a Resource\n");
340 return ret;
341 }
342 } else {
343 resource->entry_point = 0;
344 }
345
346 /* read IntrinsicDuration */
347 if (!(element = ff_imf_xml_get_child_element_by_name(resource_elem, "IntrinsicDuration"))) {
348 av_log(log_ctx, AV_LOG_ERROR, "IntrinsicDuration element missing from Resource\n");
349 return AVERROR_INVALIDDATA;
350 }
351 if ((ret = ff_imf_xml_read_uint32(element, &resource->duration))) {
352 av_log(log_ctx, AV_LOG_ERROR, "Invalid IntrinsicDuration element found in a Resource\n");
353 return ret;
354 }
355 if (resource->entry_point > resource->duration) {
356 av_log(log_ctx, AV_LOG_ERROR, "EntryPoint is beyond the IntrinsicDuration of a Resource\n");
357 return AVERROR_INVALIDDATA;
358 }
359 resource->duration -= resource->entry_point;
360
361 /* read SourceDuration */
362 if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "SourceDuration"))) {
363 if ((ret = ff_imf_xml_read_uint32(element, &resource->duration))) {
364 av_log(log_ctx, AV_LOG_ERROR, "SourceDuration element missing from Resource\n");
365 return ret;
366 }
367 }
368
369 /* read RepeatCount */
370 if ((element = ff_imf_xml_get_child_element_by_name(resource_elem, "RepeatCount")))
371 ret = ff_imf_xml_read_uint32(element, &resource->repeat_count);
372
373 return ret;
374}
375
376static int fill_trackfile_resource(void *log_ctx, xmlNodePtr tf_resource_elem,
377 FFIMFTrackFileResource *tf_resource,
378 FFIMFCPL *cpl)
379{
380 xmlNodePtr element = NULL;
381 int ret = 0;
382
383 if ((ret = fill_base_resource(log_ctx, tf_resource_elem, (FFIMFBaseResource *)tf_resource, cpl)))
384 return ret;
385
386 /* read TrackFileId */
387 if ((element = ff_imf_xml_get_child_element_by_name(tf_resource_elem, "TrackFileId"))) {
388 if ((ret = ff_imf_xml_read_uuid(element, tf_resource->track_file_uuid))) {
389 av_log(log_ctx, AV_LOG_ERROR, "Invalid TrackFileId element found in Resource\n");
390 return ret;
391 }
392 } else {
393 av_log(log_ctx, AV_LOG_ERROR, "TrackFileId element missing from Resource\n");
394 return AVERROR_INVALIDDATA;
395 }
396
397 return ret;
398}
399
400static int fill_marker_resource(void *log_ctx, xmlNodePtr marker_resource_elem,
401 FFIMFMarkerResource *marker_resource,
402 FFIMFCPL *cpl)
403{
404 xmlNodePtr element = NULL;
405 int ret = 0;
406
407 if ((ret = fill_base_resource(log_ctx, marker_resource_elem, (FFIMFBaseResource *)marker_resource, cpl)))
408 return ret;
409
410 /* read markers */
411 element = xmlFirstElementChild(marker_resource_elem);
412 while (element) {
413 if (xmlStrcmp(element->name, "Marker") == 0) {
414 void *tmp;
415
416 if (marker_resource->marker_count == UINT32_MAX)
417 return AVERROR(ENOMEM);
418 tmp = av_realloc_array(marker_resource->markers,
419 marker_resource->marker_count + 1,
420 sizeof(FFIMFMarker));
421 if (!tmp)
422 return AVERROR(ENOMEM);
423 marker_resource->markers = tmp;
424
425 imf_marker_init(&marker_resource->markers[marker_resource->marker_count]);
426 ret = fill_marker(element,
427 &marker_resource->markers[marker_resource->marker_count]);
428 marker_resource->marker_count++;
429 if (ret) {
430 av_log(log_ctx, AV_LOG_ERROR, "Invalid Marker element\n");
431 return ret;
432 }
433 }
434
435 element = xmlNextElementSibling(element);
436 }
437
438 return ret;
439}
440
441static int push_marker_sequence(void *log_ctx, xmlNodePtr marker_sequence_elem, FFIMFCPL *cpl)
442{
443 int ret = 0;
444 AVUUID uuid;
445 xmlNodePtr resource_list_elem = NULL;
446 xmlNodePtr resource_elem = NULL;
447 xmlNodePtr track_id_elem = NULL;
448 unsigned long resource_elem_count;
449 void *tmp;
450
451 /* read TrackID element */
452 if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(marker_sequence_elem, "TrackId"))) {
453 av_log(log_ctx, AV_LOG_ERROR, "TrackId element missing from Sequence\n");
454 return AVERROR_INVALIDDATA;
455 }
456 if (ff_imf_xml_read_uuid(track_id_elem, uuid)) {
457 av_log(log_ctx, AV_LOG_ERROR, "Invalid TrackId element found in Sequence\n");
458 return AVERROR_INVALIDDATA;
459 }
460 av_log(log_ctx,
462 "Processing IMF CPL Marker Sequence for Virtual Track " AV_PRI_UUID "\n",
463 AV_UUID_ARG(uuid));
464
465 /* create main marker virtual track if it does not exist */
466 if (!cpl->main_markers_track) {
468 if (!cpl->main_markers_track)
469 return AVERROR(ENOMEM);
472
473 } else if (!av_uuid_equal(cpl->main_markers_track->base.id_uuid, uuid)) {
474 av_log(log_ctx, AV_LOG_ERROR, "Multiple marker virtual tracks were found\n");
475 return AVERROR_INVALIDDATA;
476 }
477
478 /* process resources */
479 resource_list_elem = ff_imf_xml_get_child_element_by_name(marker_sequence_elem, "ResourceList");
480 if (!resource_list_elem)
481 return 0;
482
483 resource_elem_count = xmlChildElementCount(resource_list_elem);
484 if (resource_elem_count > UINT32_MAX
485 || cpl->main_markers_track->resource_count > UINT32_MAX - resource_elem_count)
486 return AVERROR(ENOMEM);
488 cpl->main_markers_track->resource_count + resource_elem_count,
489 sizeof(FFIMFMarkerResource));
490 if (!tmp) {
491 av_log(log_ctx, AV_LOG_ERROR, "Cannot allocate Marker Resources\n");
492 return AVERROR(ENOMEM);
493 }
495
496 resource_elem = xmlFirstElementChild(resource_list_elem);
497 while (resource_elem) {
499 ret = fill_marker_resource(log_ctx, resource_elem,
501 cpl);
503 if (ret)
504 return ret;
505
506 resource_elem = xmlNextElementSibling(resource_elem);
507 }
508
509 return ret;
510}
511
512static int has_stereo_resources(xmlNodePtr element)
513{
514 if (xmlStrcmp(element->name, "Left") == 0 || xmlStrcmp(element->name, "Right") == 0)
515 return 1;
516
517 element = xmlFirstElementChild(element);
518 while (element) {
519 if (has_stereo_resources(element))
520 return 1;
521
522 element = xmlNextElementSibling(element);
523 }
524
525 return 0;
526}
527
528static int push_main_audio_sequence(void *log_ctx, xmlNodePtr audio_sequence_elem, FFIMFCPL *cpl)
529{
530 int ret = 0;
531 AVUUID uuid;
532 xmlNodePtr resource_list_elem = NULL;
533 xmlNodePtr resource_elem = NULL;
534 xmlNodePtr track_id_elem = NULL;
535 unsigned long resource_elem_count;
537 void *tmp;
538
539 /* read TrackID element */
540 if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(audio_sequence_elem, "TrackId"))) {
541 av_log(log_ctx, AV_LOG_ERROR, "TrackId element missing from audio sequence\n");
542 return AVERROR_INVALIDDATA;
543 }
544 if ((ret = ff_imf_xml_read_uuid(track_id_elem, uuid))) {
545 av_log(log_ctx, AV_LOG_ERROR, "Invalid TrackId element found in audio sequence\n");
546 return ret;
547 }
548 av_log(log_ctx,
550 "Processing IMF CPL Audio Sequence for Virtual Track " AV_PRI_UUID "\n",
551 AV_UUID_ARG(uuid));
552
553 /* get the main audio virtual track corresponding to the sequence */
554 for (uint32_t i = 0; i < cpl->main_audio_track_count; i++) {
555 if (av_uuid_equal(cpl->main_audio_tracks[i].base.id_uuid, uuid)) {
556 vt = &cpl->main_audio_tracks[i];
557 break;
558 }
559 }
560
561 /* create a main audio virtual track if none exists for the sequence */
562 if (!vt) {
563 if (cpl->main_audio_track_count == UINT32_MAX)
564 return AVERROR(ENOMEM);
566 cpl->main_audio_track_count + 1,
568 if (!tmp)
569 return AVERROR(ENOMEM);
570
571 cpl->main_audio_tracks = tmp;
575 av_uuid_copy(vt->base.id_uuid, uuid);
576 }
577
578 /* process resources */
579 resource_list_elem = ff_imf_xml_get_child_element_by_name(audio_sequence_elem, "ResourceList");
580 if (!resource_list_elem)
581 return 0;
582
583 resource_elem_count = xmlChildElementCount(resource_list_elem);
584 if (resource_elem_count > UINT32_MAX
585 || vt->resource_count > UINT32_MAX - resource_elem_count)
586 return AVERROR(ENOMEM);
589 (vt->resource_count + resource_elem_count)
590 * sizeof(FFIMFTrackFileResource));
591 if (!tmp) {
592 av_log(log_ctx, AV_LOG_ERROR, "Cannot allocate Main Audio Resources\n");
593 return AVERROR(ENOMEM);
594 }
595 vt->resources = tmp;
596
597 resource_elem = xmlFirstElementChild(resource_list_elem);
598 while (resource_elem) {
600 ret = fill_trackfile_resource(log_ctx, resource_elem,
601 &vt->resources[vt->resource_count],
602 cpl);
603 if (ret)
604 av_log(log_ctx, AV_LOG_ERROR, "Invalid Resource\n");
605 else
606 vt->resource_count++;
607
608 resource_elem = xmlNextElementSibling(resource_elem);
609 }
610
611 return ret;
612}
613
614static int push_main_image_2d_sequence(void *log_ctx, xmlNodePtr image_sequence_elem, FFIMFCPL *cpl)
615{
616 int ret = 0;
617 AVUUID uuid;
618 xmlNodePtr resource_list_elem = NULL;
619 xmlNodePtr resource_elem = NULL;
620 xmlNodePtr track_id_elem = NULL;
621 void *tmp;
622 unsigned long resource_elem_count;
623
624 /* skip stereoscopic resources */
625 if (has_stereo_resources(image_sequence_elem)) {
626 av_log(log_ctx, AV_LOG_ERROR, "Stereoscopic 3D image virtual tracks not supported\n");
628 }
629
630 /* read TrackId element*/
631 if (!(track_id_elem = ff_imf_xml_get_child_element_by_name(image_sequence_elem, "TrackId"))) {
632 av_log(log_ctx, AV_LOG_ERROR, "TrackId element missing from audio sequence\n");
633 return AVERROR_INVALIDDATA;
634 }
635 if ((ret = ff_imf_xml_read_uuid(track_id_elem, uuid))) {
636 av_log(log_ctx, AV_LOG_ERROR, "Invalid TrackId element found in audio sequence\n");
637 return ret;
638 }
639
640 /* create main image virtual track if one does not exist */
641 if (!cpl->main_image_2d_track) {
643 if (!cpl->main_image_2d_track)
644 return AVERROR(ENOMEM);
647
648 } else if (!av_uuid_equal(cpl->main_image_2d_track->base.id_uuid, uuid)) {
649 av_log(log_ctx, AV_LOG_ERROR, "Multiple MainImage virtual tracks found\n");
650 return AVERROR_INVALIDDATA;
651 }
652 av_log(log_ctx,
654 "Processing IMF CPL Main Image Sequence for Virtual Track " AV_PRI_UUID "\n",
655 AV_UUID_ARG(uuid));
656
657 /* process resources */
658 resource_list_elem = ff_imf_xml_get_child_element_by_name(image_sequence_elem, "ResourceList");
659 if (!resource_list_elem)
660 return 0;
661
662 resource_elem_count = xmlChildElementCount(resource_list_elem);
663 if (resource_elem_count > UINT32_MAX
664 || cpl->main_image_2d_track->resource_count > UINT32_MAX - resource_elem_count
665 || (cpl->main_image_2d_track->resource_count + resource_elem_count)
666 > INT_MAX / sizeof(FFIMFTrackFileResource))
667 return AVERROR(ENOMEM);
670 (cpl->main_image_2d_track->resource_count + resource_elem_count)
671 * sizeof(FFIMFTrackFileResource));
672 if (!tmp) {
673 av_log(log_ctx, AV_LOG_ERROR, "Cannot allocate Main Image Resources\n");
674 return AVERROR(ENOMEM);
675 }
677
678 resource_elem = xmlFirstElementChild(resource_list_elem);
679 while (resource_elem) {
682 ret = fill_trackfile_resource(log_ctx, resource_elem,
684 cpl);
685 if (ret)
686 av_log(log_ctx, AV_LOG_ERROR, "Invalid Resource\n");
687 else
689
690 resource_elem = xmlNextElementSibling(resource_elem);
691 }
692
693 return 0;
694}
695
696static int fill_virtual_tracks(void *log_ctx, xmlNodePtr cpl_element, FFIMFCPL *cpl)
697{
698 int ret = 0;
699 xmlNodePtr segment_list_elem = NULL;
700 xmlNodePtr segment_elem = NULL;
701 xmlNodePtr sequence_list_elem = NULL;
702 xmlNodePtr sequence_elem = NULL;
703
704 if (!(segment_list_elem = ff_imf_xml_get_child_element_by_name(cpl_element, "SegmentList"))) {
705 av_log(log_ctx, AV_LOG_ERROR, "SegmentList element missing\n");
706 return AVERROR_INVALIDDATA;
707 }
708
709 /* process sequences */
710 segment_elem = xmlFirstElementChild(segment_list_elem);
711 while (segment_elem) {
712 av_log(log_ctx, AV_LOG_DEBUG, "Processing IMF CPL Segment\n");
713
714 sequence_list_elem = ff_imf_xml_get_child_element_by_name(segment_elem, "SequenceList");
715 if (sequence_list_elem) {
716
717 sequence_elem = xmlFirstElementChild(sequence_list_elem);
718 while (sequence_elem) {
719 if (xmlStrcmp(sequence_elem->name, "MarkerSequence") == 0)
720 ret = push_marker_sequence(log_ctx, sequence_elem, cpl);
721
722 else if (xmlStrcmp(sequence_elem->name, "MainImageSequence") == 0)
723 ret = push_main_image_2d_sequence(log_ctx, sequence_elem, cpl);
724
725 else if (xmlStrcmp(sequence_elem->name, "MainAudioSequence") == 0)
726 ret = push_main_audio_sequence(log_ctx, sequence_elem, cpl);
727
728 else
729 av_log(log_ctx,
731 "The following Sequence is not supported and is ignored: %s\n",
732 sequence_elem->name);
733
734 /* abort parsing only if memory error occurred */
735 if (ret == AVERROR(ENOMEM))
736 return ret;
737
738 sequence_elem = xmlNextElementSibling(sequence_elem);
739 }
740 }
741
742 segment_elem = xmlNextElementSibling(segment_elem);
743 }
744
745 return ret;
746}
747
748int ff_imf_parse_cpl_from_xml_dom(void *log_ctx, xmlDocPtr doc, FFIMFCPL **cpl)
749{
750 int ret = 0;
751 xmlNodePtr cpl_element = NULL;
752
753 *cpl = ff_imf_cpl_alloc();
754 if (!*cpl) {
755 ret = AVERROR(ENOMEM);
756 goto cleanup;
757 }
758
759 cpl_element = xmlDocGetRootElement(doc);
760 if (!cpl_element || xmlStrcmp(cpl_element->name, "CompositionPlaylist")) {
761 av_log(log_ctx, AV_LOG_ERROR, "The root element of the CPL is not CompositionPlaylist\n");
763 goto cleanup;
764 }
765
766 if ((ret = fill_content_title(cpl_element, *cpl))) {
767 av_log(log_ctx, AV_LOG_ERROR, "Cannot read the ContentTitle element from the IMF CPL\n");
768 goto cleanup;
769 }
770 if ((ret = fill_id(cpl_element, *cpl))) {
771 av_log(log_ctx, AV_LOG_ERROR, "Id element not found in the IMF CPL\n");
772 goto cleanup;
773 }
774 if ((ret = fill_edit_rate(cpl_element, *cpl))) {
775 av_log(log_ctx, AV_LOG_ERROR, "EditRate element not found in the IMF CPL\n");
776 goto cleanup;
777 }
778 if ((ret = fill_timecode(cpl_element, *cpl))) {
779 av_log(log_ctx, AV_LOG_ERROR, "Invalid CompositionTimecode element found in the IMF CPL\n");
780 goto cleanup;
781 }
782 if ((ret = fill_virtual_tracks(log_ctx, cpl_element, *cpl)))
783 goto cleanup;
784
785cleanup:
786 if (*cpl && ret) {
787 ff_imf_cpl_free(*cpl);
788 *cpl = NULL;
789 }
790 return ret;
791}
792
793static void imf_marker_free(FFIMFMarker *marker)
794{
795 if (!marker)
796 return;
797 xmlFree(marker->label_utf8);
798 xmlFree(marker->scope_utf8);
799}
800
802{
803 if (!rsrc)
804 return;
805 for (uint32_t i = 0; i < rsrc->marker_count; i++)
806 imf_marker_free(&rsrc->markers[i]);
807 av_freep(&rsrc->markers);
808}
809
811{
812 if (!vt)
813 return;
814 for (uint32_t i = 0; i < vt->resource_count; i++)
816 av_freep(&vt->resources);
817}
818
820{
821 if (!vt)
822 return;
823 av_freep(&vt->resources);
824}
825
826static void imf_cpl_init(FFIMFCPL *cpl)
827{
828 av_uuid_nil(cpl->id_uuid);
830 cpl->edit_rate = av_make_q(0, 1);
831 cpl->tc = NULL;
834 cpl->main_audio_track_count = 0;
835 cpl->main_audio_tracks = NULL;
836}
837
839{
840 FFIMFCPL *cpl;
841
842 cpl = av_malloc(sizeof(FFIMFCPL));
843 if (!cpl)
844 return NULL;
845 imf_cpl_init(cpl);
846 return cpl;
847}
848
850{
851 if (!cpl)
852 return;
853
854 if (cpl->tc)
855 av_freep(&cpl->tc);
856
857 xmlFree(cpl->content_title_utf8);
858
860
861 if (cpl->main_markers_track)
863
865
866 if (cpl->main_image_2d_track)
868
869 for (uint32_t i = 0; i < cpl->main_audio_track_count; i++)
871
872 if (cpl->main_audio_tracks)
874
875 av_freep(&cpl);
876}
877
878int ff_imf_parse_cpl(void *log_ctx, AVIOContext *in, FFIMFCPL **cpl)
879{
880 AVBPrint buf;
881 xmlDoc *doc = NULL;
882 int ret = 0;
883
884 av_bprint_init(&buf, 0, INT_MAX); // xmlReadMemory uses integer length
885
886 ret = avio_read_to_bprint(in, &buf, SIZE_MAX);
887 if (ret < 0 || !avio_feof(in)) {
888 av_log(log_ctx, AV_LOG_ERROR, "Cannot read IMF CPL\n");
889 if (ret == 0)
891 goto clean_up;
892 }
893
894 LIBXML_TEST_VERSION
895
896 doc = xmlReadMemory(buf.str, buf.len, NULL, NULL, 0);
897 if (!doc) {
898 av_log(log_ctx,
900 "XML parsing failed when reading the IMF CPL\n");
902 goto clean_up;
903 }
904
905 if ((ret = ff_imf_parse_cpl_from_xml_dom(log_ctx, doc, cpl))) {
906 av_log(log_ctx, AV_LOG_ERROR, "Cannot parse IMF CPL\n");
907 } else {
908 av_log(log_ctx,
910 "IMF CPL ContentTitle: %s\n",
911 (*cpl)->content_title_utf8);
912 av_log(log_ctx,
914 "IMF CPL Id: " AV_PRI_UUID "\n",
915 AV_UUID_ARG((*cpl)->id_uuid));
916 }
917
918 xmlFreeDoc(doc);
919
920clean_up:
922
923 return ret;
924}
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition aviobuf.c:1222
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
error code definitions
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
static av_cold void cleanup(FlashSV2Context *s)
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:601
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
Definition avstring.h:141
Public header file for the processing of Interoperable Master Format (IMF) packages.
xmlNodePtr ff_imf_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8)
Returns the first child element with the specified local name.
Definition imf_cpl.c:59
static int ff_imf_xml_read_boolean(xmlNodePtr element, int *value)
Definition imf_cpl.c:113
static void imf_marker_resource_init(FFIMFMarkerResource *rsrc)
Definition imf_cpl.c:157
static void imf_marker_free(FFIMFMarker *marker)
Definition imf_cpl.c:793
FFIMFCPL * ff_imf_cpl_alloc(void)
Allocates and initializes an FFIMFCPL data structure.
Definition imf_cpl.c:838
static void imf_cpl_init(FFIMFCPL *cpl)
Definition imf_cpl.c:826
static void imf_marker_init(FFIMFMarker *marker)
Definition imf_cpl.c:164
int ff_imf_parse_cpl(void *log_ctx, AVIOContext *in, FFIMFCPL **cpl)
Parse an IMF Composition Playlist document into the FFIMFCPL data structure.
Definition imf_cpl.c:878
static int fill_trackfile_resource(void *log_ctx, xmlNodePtr tf_resource_elem, FFIMFTrackFileResource *tf_resource, FFIMFCPL *cpl)
Definition imf_cpl.c:376
static int fill_marker_resource(void *log_ctx, xmlNodePtr marker_resource_elem, FFIMFMarkerResource *marker_resource, FFIMFCPL *cpl)
Definition imf_cpl.c:400
static int has_stereo_resources(xmlNodePtr element)
Definition imf_cpl.c:512
static void imf_trackfile_resource_init(FFIMFTrackFileResource *rsrc)
Definition imf_cpl.c:171
static void imf_marker_resource_free(FFIMFMarkerResource *rsrc)
Definition imf_cpl.c:801
static void imf_base_resource_init(FFIMFBaseResource *rsrc)
Definition imf_cpl.c:149
static int push_main_image_2d_sequence(void *log_ctx, xmlNodePtr image_sequence_elem, FFIMFCPL *cpl)
Definition imf_cpl.c:614
static int fill_id(xmlNodePtr cpl_element, FFIMFCPL *cpl)
Definition imf_cpl.c:283
static void imf_marker_virtual_track_init(FFIMFMarkerVirtualTrack *track)
Definition imf_cpl.c:134
int ff_imf_parse_cpl_from_xml_dom(void *log_ctx, xmlDocPtr doc, FFIMFCPL **cpl)
Parse an IMF CompositionPlaylist element into the FFIMFCPL data structure.
Definition imf_cpl.c:748
static void imf_trackfile_virtual_track_init(FFIMFTrackFileVirtualTrack *track)
Definition imf_cpl.c:141
int ff_imf_xml_read_rational(xmlNodePtr element, AVRational *rational)
Reads a positive AVRational from an XML element.
Definition imf_cpl.c:88
static int parse_cpl_tc_type(const char *s, int *tc_comps)
Parses a string that conform to the TimecodeType used in IMF CPL and defined in SMPTE ST 2067-3.
Definition imf_cpl.c:209
static int fill_marker(xmlNodePtr marker_elem, FFIMFMarker *marker)
Definition imf_cpl.c:293
static int digit_to_int(char digit)
Definition imf_cpl.c:194
void ff_imf_cpl_free(FFIMFCPL *cpl)
Deletes an FFIMFCPL data structure previously instantiated with ff_imf_cpl_alloc().
Definition imf_cpl.c:849
static int push_marker_sequence(void *log_ctx, xmlNodePtr marker_sequence_elem, FFIMFCPL *cpl)
Definition imf_cpl.c:441
static void imf_trackfile_virtual_track_free(FFIMFTrackFileVirtualTrack *vt)
Definition imf_cpl.c:819
static void imf_marker_virtual_track_free(FFIMFMarkerVirtualTrack *vt)
Definition imf_cpl.c:810
static int fill_content_title(xmlNodePtr cpl_element, FFIMFCPL *cpl)
Definition imf_cpl.c:177
static int fill_base_resource(void *log_ctx, xmlNodePtr resource_elem, FFIMFBaseResource *resource, FFIMFCPL *cpl)
Definition imf_cpl.c:322
static int push_main_audio_sequence(void *log_ctx, xmlNodePtr audio_sequence_elem, FFIMFCPL *cpl)
Definition imf_cpl.c:528
int ff_imf_xml_read_uint32(xmlNodePtr element, uint32_t *number)
Reads an unsigned 32-bit integer from an XML element.
Definition imf_cpl.c:101
int ff_imf_xml_read_uuid(xmlNodePtr element, AVUUID uuid)
Reads a UUID from an XML element.
Definition imf_cpl.c:73
static void imf_base_virtual_track_init(FFIMFBaseVirtualTrack *track)
Definition imf_cpl.c:129
static int fill_timecode(xmlNodePtr cpl_element, FFIMFCPL *cpl)
Definition imf_cpl.c:230
static int fill_edit_rate(xmlNodePtr cpl_element, FFIMFCPL *cpl)
Definition imf_cpl.c:273
static int fill_virtual_tracks(void *log_ctx, xmlNodePtr cpl_element, FFIMFCPL *cpl)
Definition imf_cpl.c:696
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
Bytestream IO Context.
Definition avio.h:160
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
IMF Composition Playlist Base Resource.
Definition imf.h:68
AVRational edit_rate
BaseResourceType/EditRate.
Definition imf.h:69
uint32_t duration
BaseResourceType/Duration.
Definition imf.h:71
uint32_t repeat_count
BaseResourceType/RepeatCount.
Definition imf.h:72
uint32_t entry_point
BaseResourceType/EntryPoint.
Definition imf.h:70
IMF Composition Playlist Virtual Track.
Definition imf.h:104
AVUUID id_uuid
TrackId associated with the Virtual Track.
Definition imf.h:105
IMF Composition Playlist.
Definition imf.h:130
FFIMFMarkerVirtualTrack * main_markers_track
Main Marker Virtual Track.
Definition imf.h:135
AVRational edit_rate
CompositionPlaylist/EditRate element.
Definition imf.h:133
FFIMFTrackFileVirtualTrack * main_audio_tracks
Main Audio Virtual Tracks.
Definition imf.h:138
xmlChar * content_title_utf8
CompositionPlaylist/ContentTitle element.
Definition imf.h:132
uint32_t main_audio_track_count
Number of Main Audio Virtual Tracks.
Definition imf.h:137
FFIMFTrackFileVirtualTrack * main_image_2d_track
Main Image Virtual Track.
Definition imf.h:136
AVUUID id_uuid
CompositionPlaylist/Id element.
Definition imf.h:131
AVTimecode * tc
CompositionPlaylist/CompositionTimecode element.
Definition imf.h:134
IMF Composition Playlist Marker Resource.
Definition imf.h:95
uint32_t marker_count
Number of Marker elements.
Definition imf.h:97
FFIMFMarker * markers
Marker elements.
Definition imf.h:98
IMF Composition Playlist Virtual Track that consists of Marker Resources.
Definition imf.h:121
uint32_t resource_count
Number of Resource elements present in the Virtual Track.
Definition imf.h:123
FFIMFBaseVirtualTrack base
Definition imf.h:122
FFIMFMarkerResource * resources
Resource elements of the Virtual Track.
Definition imf.h:124
IMF Marker.
Definition imf.h:86
xmlChar * label_utf8
Marker/Label.
Definition imf.h:87
uint32_t offset
Marker/Offset.
Definition imf.h:89
xmlChar * scope_utf8
Marker/Label/@scope.
Definition imf.h:88
IMF Composition Playlist Track File Resource.
Definition imf.h:78
AVUUID track_file_uuid
TrackFileResourceType/TrackFileId.
Definition imf.h:80
IMF Composition Playlist Virtual Track that consists of Track File Resources.
Definition imf.h:111
FFIMFTrackFileResource * resources
Resource elements of the Virtual Track.
Definition imf.h:114
uint32_t resource_count
Number of Resource elements present in the Virtual Track.
Definition imf.h:113
unsigned int resources_alloc_sz
Size of the resources buffer.
Definition imf.h:115
FFIMFBaseVirtualTrack base
Definition imf.h:112
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int av_timecode_init_from_components(AVTimecode *tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void *log_ctx)
Init a timecode struct from the passed timecode components.
Definition timecode.c:211
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition timecode.h:36
int av_uuid_urn_parse(const char *in, AVUUID uu)
Parses a URN representation of a UUID, as specified at IETF RFC 4122, into an AVUUID.
Definition uuid.c:136
static void av_uuid_copy(AVUUID dest, const AVUUID src)
Copies the bytes of src into dest.
Definition uuid.h:130
static int av_uuid_equal(const AVUUID uu1, const AVUUID uu2)
Compares two UUIDs for equality.
Definition uuid.h:119
static void av_uuid_nil(AVUUID uu)
Sets a UUID to the nil UUID, i.e.
Definition uuid.h:141
#define AV_UUID_ARG(x)
Definition uuid.h:51
uint8_t AVUUID[AV_UUID_LEN]
Definition uuid.h:60
#define AV_PRI_UUID
Definition uuid.h:39
#define df(A, B)
Definition vf_xbr.c:91